Last active
December 27, 2025 18:06
-
-
Save xenobrain/8db333f1aa88b5df966187a609cc7aa2 to your computer and use it in GitHub Desktop.
Current best version of this header
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| // ============================================================================ | |
| // Build Configuration | |
| // ============================================================================ | |
| #if defined(_DEBUG) || defined(DEBUG) | |
| # define XC_DEBUG 1 | |
| # define XC_RELEASE 0 | |
| #else | |
| # define XC_DEBUG 0 | |
| # define XC_RELEASE 1 | |
| #endif | |
| // ============================================================================ | |
| // Macro Utilities | |
| // ============================================================================ | |
| #define XC_CONCAT(a, b) a##b | |
| #define XC_CONCAT_(a, b) XC_CONCAT(a, b) | |
| #define XC_UNIQUE_SUFFIX(x) XC_CONCAT_(x, __COUNTER__) | |
| #define XC_STRINGIFY(x) #x | |
| #define XC_STRINGIFY_(x) XC_STRINGIFY(x) | |
| #define XC_FILE_LINE(msg) __FILE__ "(" XC_STRINGIFY_(__LINE__) "): " msg | |
| // ============================================================================ | |
| // Compiler Detection | |
| // ============================================================================ | |
| #if defined(_MSC_VER) | |
| # define XC_COMPILER_MSVC 1 | |
| # define XC_COMPILER_CLANG 0 | |
| # define XC_COMPILER_GCC 0 | |
| #elif defined(__clang__) | |
| # define XC_COMPILER_MSVC 0 | |
| # define XC_COMPILER_CLANG 1 | |
| # define XC_COMPILER_GCC 0 | |
| #elif defined(__GNUC__) | |
| # define XC_COMPILER_MSVC 0 | |
| # define XC_COMPILER_CLANG 0 | |
| # define XC_COMPILER_GCC 1 | |
| #else | |
| # error Unsupported compiler | |
| #endif | |
| // ============================================================================ | |
| // Debug Break | |
| // ============================================================================ | |
| #if XC_COMPILER_MSVC | |
| # define XC_DEBUG_BREAK() __debugbreak() | |
| #elif defined(__has_builtin) | |
| # if __has_builtin(__builtin_debugtrap) | |
| # define XC_DEBUG_BREAK() __builtin_debugtrap() | |
| # else | |
| # define XC_DEBUG_BREAK() __builtin_trap() | |
| # endif | |
| #else | |
| # define XC_DEBUG_BREAK() __builtin_trap() | |
| #endif | |
| // ============================================================================ | |
| // Inlining / no-inline | |
| // ============================================================================ | |
| #if XC_COMPILER_MSVC | |
| # define XC_FORCE_INLINE __forceinline inline | |
| # define XC_NO_INLINE __declspec(noinline) | |
| #else | |
| # define XC_FORCE_INLINE static inline __attribute__((always_inline)) | |
| # define XC_NO_INLINE __attribute__((noinline)) | |
| #endif | |
| // ============================================================================ | |
| // Attributes: nodiscard, fallthrough, unused, align | |
| // ============================================================================ | |
| #if XC_COMPILER_MSVC | |
| # define XC_UNUSED(x) static_cast<void>(x) | |
| # define XC_ALIGN(n) __declspec(align(n)) | |
| #else | |
| # define XC_UNUSED(x) static_cast<void>(x) | |
| # define XC_ALIGN(n) __attribute__((aligned(n))) | |
| #endif | |
| // ============================================================================ | |
| // Assumptions | |
| // ============================================================================ | |
| #if XC_COMPILER_MSVC | |
| # define XC_ASSUME(x) __assume(x) | |
| #else | |
| # define XC_ASSUME(x) do { if (!(x)) __builtin_unreachable(); } while (0) | |
| #endif | |
| // ============================================================================ | |
| // Warning control | |
| // ============================================================================ | |
| #if XC_COMPILER_MSVC | |
| # define XC_PUSH_WARNING __pragma(warning(push)) | |
| # define XC_POP_WARNING __pragma(warning(pop)) | |
| # define XC_DISABLE_WARNING(w) __pragma(warning(disable: w)) | |
| #elif XC_COMPILER_CLANG || XC_COMPILER_GCC | |
| # define XC_PRAGMA_STR(x) #x | |
| # define XC_PRAGMA(x) _Pragma(XC_PRAGMA_STR(x)) | |
| # if XC_COMPILER_CLANG | |
| # define XC_PUSH_WARNING XC_PRAGMA(clang diagnostic push) | |
| # define XC_POP_WARNING XC_PRAGMA(clang diagnostic pop) | |
| # define XC_DISABLE_WARNING(w) XC_PRAGMA(clang diagnostic ignored w) | |
| # else // GCC | |
| # define XC_PUSH_WARNING XC_PRAGMA(GCC diagnostic push) | |
| # define XC_POP_WARNING XC_PRAGMA(GCC diagnostic pop) | |
| # define XC_DISABLE_WARNING(w) XC_PRAGMA(GCC diagnostic ignored w) | |
| # endif | |
| #endif | |
| // ============================================================================ | |
| // Visibility / API export | |
| // ============================================================================ | |
| #if XC_COMPILER_MSVC | |
| # ifdef XC_STATIC | |
| # define XC_API | |
| # elif defined(XC_EXPORT) | |
| # define XC_API __declspec(dllexport) | |
| # else | |
| # define XC_API __declspec(dllimport) | |
| # endif | |
| #else | |
| # define XC_API __attribute__((visibility("default"))) | |
| #endif | |
| // ============================================================================ | |
| // Core Types and Traits | |
| // ============================================================================ | |
| namespace xc { | |
| using i8 = signed char; | |
| using u8 = unsigned char; | |
| using i16 = short; | |
| using u16 = unsigned short; | |
| using i32 = int; | |
| using u32 = unsigned int; | |
| using i64 = long long; | |
| using u64 = unsigned long long; | |
| using f32 = float; | |
| using f64 = double; | |
| static_assert(sizeof(i8) == 1); | |
| static_assert(sizeof(u8) == 1); | |
| static_assert(sizeof(i16) == 2); | |
| static_assert(sizeof(u16) == 2); | |
| static_assert(sizeof(i32) == 4); | |
| static_assert(sizeof(u32) == 4); | |
| static_assert(sizeof(i64) == 8); | |
| static_assert(sizeof(u64) == 8); | |
| static_assert(sizeof(f32) == 4); | |
| static_assert(sizeof(f64) == 8); | |
| static_assert(sizeof(void*) == 4 || sizeof(void*) == 8); | |
| template<bool Cond, class T, class F> | |
| struct Select { using type = T; }; | |
| template<class T, class F> | |
| struct Select<false, T, F> { using type = F; }; | |
| template<bool Cond, class T, class F> | |
| using select_t = Select<Cond, T, F>::type; | |
| using uintptr = select_t<sizeof(void*) == 8, u64, u32>; | |
| using intptr = select_t<sizeof(void*) == 8, i64, i32>; | |
| using ptrdiff = select_t<sizeof(void*) == 8, i64, i32>; | |
| using isize = select_t<sizeof(void*) == 8, i64, i32>; | |
| // ========================================================================= | |
| // Numeric limits (two's complement, intrinsic) | |
| // ========================================================================= | |
| template<class S, class U> | |
| struct limits { | |
| static_assert(sizeof(S) == sizeof(U)); | |
| static constexpr U u_min = U(0); | |
| static constexpr U u_max = ~U(0); | |
| static constexpr S s_max = S(u_max >> 1); | |
| static constexpr S s_min = S(~(u_max >> 1)); | |
| }; | |
| inline constexpr i8 i8_max { limits<i8, u8 >::s_max }; | |
| inline constexpr i8 i8_min { limits<i8, u8 >::s_min }; | |
| inline constexpr u8 u8_max { limits<i8, u8 >::u_max }; | |
| inline constexpr u8 u8_min { limits<i8, u8 >::u_min }; | |
| inline constexpr i16 i16_max { limits<i16, u16>::s_max }; | |
| inline constexpr i16 i16_min { limits<i16, u16>::s_min }; | |
| inline constexpr u16 u16_max { limits<i16, u16>::u_max }; | |
| inline constexpr u16 u16_min { limits<i16, u16>::u_min }; | |
| inline constexpr i32 i32_max { limits<i32, u32>::s_max }; | |
| inline constexpr i32 i32_min { limits<i32, u32>::s_min }; | |
| inline constexpr u32 u32_max { limits<i32, u32>::u_max }; | |
| inline constexpr u32 u32_min { limits<i32, u32>::u_min }; | |
| inline constexpr i64 i64_max { limits<i64, u64>::s_max }; | |
| inline constexpr i64 i64_min { limits<i64, u64>::s_min }; | |
| inline constexpr u64 u64_max { limits<i64, u64>::u_max }; | |
| inline constexpr u64 u64_min { limits<i64, u64>::u_min }; | |
| inline constexpr f32 f32_max { 3.402823466e+38F }; | |
| inline constexpr f32 f32_min { 1.175494351e-38F }; | |
| inline constexpr f32 f32_lowest { -f32_max }; | |
| inline constexpr f64 f64_max { 1.797693134862315708e+308 }; | |
| inline constexpr f64 f64_min { 2.2250738585072014e-308 }; | |
| inline constexpr f64 f64_lowest { -f64_max }; | |
| // ========================================================================= | |
| // Intrinsic-only traits | |
| // ========================================================================= | |
| #if !(XC_COMPILER_MSVC || XC_COMPILER_CLANG || XC_COMPILER_GCC) | |
| # error Intrinsic type traits not supported on this compiler | |
| #endif | |
| template<class T> struct IsSigned { static constexpr bool value = __is_signed(T); }; | |
| template<class T> struct IsUnsigned { static constexpr bool value = __is_unsigned(T); }; | |
| template<class T> struct IsInteger { static constexpr bool value = __is_integral(T); }; | |
| template<class T> struct IsFloatingPoint { static constexpr bool value = __is_floating_point(T); }; | |
| template<class T> struct IsPointer { static constexpr bool value = __is_pointer(T); }; | |
| template<class T> struct IsEnum { static constexpr bool value = __is_enum(T); }; | |
| template<class T> struct IsVoid { static constexpr bool value = __is_void(T); }; | |
| template<class A, class B> struct IsSame { static constexpr bool value = __is_same(A, B); }; | |
| template<class T> struct IsStandardLayout { static constexpr bool value = __is_standard_layout(T); }; | |
| template<class T> struct IsTriviallyCopyable { static constexpr bool value = __is_trivially_copyable(T); }; | |
| template<class T> struct IsTriviallyDestructible { static constexpr bool value = __is_trivially_destructible(T); }; | |
| // ========================================================================= | |
| // EnableIf / Conditional | |
| // ========================================================================= | |
| template<bool Cond, class T = void> | |
| struct EnableIf {}; | |
| template<class T> | |
| struct EnableIf<true, T> { using type = T; }; | |
| template<bool Cond, class T = void> | |
| using enable_if_t = EnableIf<Cond, T>::type; | |
| template<bool Cond, class T, class F> | |
| using conditional_t = Select<Cond, T, F>::type; | |
| // ========================================================================= | |
| // Remove CV / Ref (portable, minimal) | |
| // ========================================================================= | |
| template<class T> struct RemoveConst { using type = T; }; | |
| template<class T> struct RemoveConst<T const> { using type = T; }; | |
| template<class T> struct RemoveVolatile { using type = T; }; | |
| template<class T> struct RemoveVolatile<T volatile> { using type = T; }; | |
| template<class T> struct RemoveCV { using type = RemoveVolatile<typename RemoveConst<T>::type>::type; }; | |
| template<class T> using remove_cv_t = RemoveCV<T>::type; | |
| template<class T> struct RemoveRef { using type = T; }; | |
| template<class T> struct RemoveRef<T&> { using type = T; }; | |
| template<class T> struct RemoveRef<T&&> { using type = T; }; | |
| // ========================================================================= | |
| // Vector math types | |
| // ========================================================================= | |
| template<typename T, int N> struct Vector; | |
| template<typename T> struct Vector<T,1> { T x; }; | |
| template<typename T> struct Vector<T,2> { T x, y; }; | |
| template<typename T> struct Vector<T,3> { T x, y, z; }; | |
| template<typename T> struct Vector<T,4> { T x, y, z, w; }; | |
| using Vector2 = Vector<f32,2>; | |
| using Vector3 = Vector<f32,3>; | |
| using Vector4 = Vector<f32,4>; | |
| template<typename T, int M, int N> struct Matrix; | |
| template<typename T, int M> struct Matrix<T,M,1> { Vector<T,M> x; }; | |
| template<typename T, int M> struct Matrix<T,M,2> { Vector<T,M> x, y; }; | |
| template<typename T, int M> struct Matrix<T,M,3> { Vector<T,M> x, y, z; }; | |
| template<typename T, int M> struct Matrix<T,M,4> { Vector<T,M> x, y, z, w; }; | |
| using Matrix2 = Matrix<f32,2,2>; | |
| using Matrix3 = Matrix<f32,3,3>; | |
| using Matrix4 = Matrix<f32,4,4>; | |
| using Quaternion = Vector<f32,4>; | |
| // ========================================================================= | |
| // Graphics types | |
| // ========================================================================= | |
| template<typename T> struct Rectangle { T x, y, w, h; }; | |
| template<typename T> struct Color { T r, g, b, a; }; | |
| template<typename T, int N> struct AABB { Vector<T,N> min, max; }; | |
| using AABB2 = AABB<f32,2>; | |
| using AABB3 = AABB<f32,3>; | |
| template<typename T, int N> struct OOBB { Matrix<T,4,4> transform; AABB<T,N> aabb; }; | |
| using OOBB2 = OOBB<f32,2>; | |
| using OOBB3 = OOBB<f32,3>; | |
| } // namespace xc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment