rstd_core.h (3313B)
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 Clamp01(a) Clamp(a, 0, 1) 29 #define Min(a, b) ((a) < (b) ? (a) : (b)) 30 #define Max(a, b) ((a) > (b) ? (a) : (b)) 31 32 #define IsDigit(c) (Between((c), '0', '9')) 33 34 #ifdef _DEBUG 35 #define assert(c) do { if (!(c)) debugbreak(); } while (0) 36 #else /* !_DEBUG */ 37 #define assert(c) 38 #endif /* !_DEBUG */ 39 40 #define InvalidCodePath assert(0) 41 #define InvalidDefaultCase default:{ assert(0); }break 42 43 //////////////////////// 44 // NOTE: Core Functions 45 46 #if COMPILER_MSVC 47 48 function force_inline u32 49 clz_u32(u32 a) 50 { 51 u32 result = 32, index; 52 if (a) { 53 _BitScanReverse(&index, a); 54 result = index; 55 } 56 return result; 57 } 58 59 function force_inline u32 60 ctz_u32(u32 a) 61 { 62 u32 result = 32, index; 63 if (a) { 64 _BitScanForward(&index, a); 65 result = index; 66 } 67 return result; 68 } 69 70 function force_inline u64 71 clz_u64(u64 a) 72 { 73 u64 result = 64, index; 74 if (a) { 75 _BitScanReverse64(&index, a); 76 result = index; 77 } 78 return result; 79 } 80 81 function force_inline u64 82 ctz_u64(u64 a) 83 { 84 u64 result = 64, index; 85 if (a) { 86 _BitScanForward64(&index, a); 87 result = index; 88 } 89 return result; 90 } 91 92 #else /* !COMPILER_MSVC */ 93 94 function force_inline u32 95 clz_u32(u32 a) 96 { 97 u32 result = 32; 98 if (a) result = (u32)__builtin_clz(a); 99 return result; 100 } 101 102 function force_inline u32 103 ctz_u32(u32 a) 104 { 105 u32 result = 32; 106 if (a) result = (u32)__builtin_ctz(a); 107 return result; 108 } 109 110 function force_inline u64 111 clz_u64(u64 a) 112 { 113 u64 result = 64; 114 if (a) result = (u64)__builtin_clzll(a); 115 return result; 116 } 117 118 function force_inline u64 119 ctz_u64(u64 a) 120 { 121 u64 result = 64; 122 if (a) result = (u64)__builtin_ctzll(a); 123 return result; 124 } 125 126 #endif /* !COMPILER_MSVC */ 127 128 function void * 129 memory_clear(void *restrict destination, u8 byte, s64 size) 130 { 131 u8 *p = destination; 132 while (size > 0) p[--size] = byte; 133 return p; 134 } 135 136 function void 137 memory_copy(void *restrict destination, void *restrict source, s64 size) 138 { 139 u8 *s = source, *d = destination; 140 for (; size > 0; size--) *d++ = *s++; 141 } 142 143 function force_inline s64 144 round_up_to(s64 value, s64 multiple) 145 { 146 s64 result = value; 147 if (value % multiple != 0) 148 result += multiple - value % multiple; 149 return result; 150 } 151 152 // NOTE: from Hacker's Delight 153 function force_inline u64 154 round_down_power_of_two(u64 a) 155 { 156 u64 result = 0x8000000000000000ULL >> clz_u64(a); 157 return result; 158 } 159 160 function force_inline u64 161 round_up_power_of_two(u64 a) 162 { 163 u64 result = 0x8000000000000000ULL >> (clz_u64(a - 1) - 1); 164 return result; 165 } 166 167 168 ////////////////////////// 169 // NOTE: String Functions 170 171 function str8 172 str8_from_c_str(char *s) 173 { 174 str8 result = {.data = (u8 *)s}; 175 if (s) { 176 while (*s) s++; 177 result.length = (u8 *)s - result.data; 178 } 179 return result; 180 } 181 182 #endif /* RSTD_CORE_H */