rstd_core.h (2483B)
1 #ifndef RSTD_CORE_H 2 #define RSTD_CORE_H 3 4 ///////////////////////// 5 // NOTE: Standard Macros 6 #define function static 7 #define global static 8 #define local_persist static 9 10 #ifndef asm 11 #define asm __asm__ 12 #endif 13 14 #ifndef typeof 15 #define typeof __typeof__ 16 #endif 17 18 #define alignof _Alignof 19 #define static_assert _Static_assert 20 21 #define countof(a) (sizeof(a) / sizeof(*a)) 22 23 #define arg_list(type, ...) (type []){__VA_ARGS__}, sizeof((type []){__VA_ARGS__}) / sizeof(type) 24 25 #define Abs(a) ((a) < 0 ? (-a) : (a)) 26 #define Between(x, a, b) ((x) >= (a) && (x) <= (b)) 27 #define Clamp(x, a, b) ((x) < (a) ? (a) : (x) > (b) ? (b) : (x)) 28 #define Min(a, b) ((a) < (b) ? (a) : (b)) 29 #define Max(a, b) ((a) > (b) ? (a) : (b)) 30 31 #define IsDigit(c) (Between((c), '0', '9')) 32 33 #ifdef _DEBUG 34 #define assert(c) do { if (!(c)) debugbreak(); } while (0) 35 #else /* !_DEBUG */ 36 #define assert(c) 37 #endif /* !_DEBUG */ 38 39 #define InvalidCodePath assert(0) 40 #define InvalidDefaultCase default:{ assert(0); }break 41 42 //////////////////////// 43 // NOTE: Core Functions 44 45 #if COMPILER_MSVC 46 47 function force_inline u32 48 clz_u32(u32 a) 49 { 50 u32 result = 32, index; 51 if (a) { 52 _BitScanReverse(&index, a); 53 result = index; 54 } 55 return result; 56 } 57 58 function force_inline u32 59 ctz_u32(u32 a) 60 { 61 u32 result = 32, index; 62 if (a) { 63 _BitScanForward(&index, a); 64 result = index; 65 } 66 return result; 67 } 68 69 function force_inline u64 70 ctz_u64(u64 a) 71 { 72 u64 result = 64, index; 73 if (a) { 74 _BitScanForward64(&index, a); 75 result = index; 76 } 77 return result; 78 } 79 80 #else /* !COMPILER_MSVC */ 81 82 function force_inline u32 83 clz_u32(u32 a) 84 { 85 u32 result = 32; 86 if (a) result = (u32)__builtin_clz(a); 87 return result; 88 } 89 90 function force_inline u32 91 ctz_u32(u32 a) 92 { 93 u32 result = 32; 94 if (a) result = (u32)__builtin_ctz(a); 95 return result; 96 } 97 98 function force_inline u64 99 ctz_u64(u64 a) 100 { 101 u64 result = 64; 102 if (a) result = (u64)__builtin_ctzll(a); 103 return result; 104 } 105 106 #endif /* !COMPILER_MSVC */ 107 108 function void * 109 memory_clear(void *restrict destination, u8 byte, s64 size) 110 { 111 u8 *p = destination; 112 while (size > 0) p[--size] = byte; 113 return p; 114 } 115 116 function void 117 memory_copy(void *restrict destination, void *restrict source, s64 size) 118 { 119 u8 *s = source, *d = destination; 120 for (; size > 0; size--) *d++ = *s++; 121 } 122 123 function force_inline s64 124 round_up_to(s64 value, s64 multiple) 125 { 126 s64 result = value; 127 if (value % multiple != 0) 128 result += multiple - value % multiple; 129 return result; 130 } 131 132 #endif /* RSTD_CORE_H */