util.h (6975B)
1 /* See LICENSE for license details. */ 2 #ifndef _UTIL_H_ 3 #define _UTIL_H_ 4 5 #include "base_platform.h" 6 7 #define da_count i32 8 9 #if OS_WINDOWS 10 #define EXPORT __declspec(dllexport) 11 #else 12 #define EXPORT 13 #endif 14 15 #ifdef _DEBUG 16 #define DEBUG_EXPORT EXPORT 17 #ifdef _BEAMFORMER_DLL 18 #if OS_WINDOWS 19 #define DEBUG_IMPORT __declspec(dllimport) 20 #else 21 #define DEBUG_IMPORT extern 22 #endif 23 #else 24 #define DEBUG_IMPORT DEBUG_EXPORT 25 #endif 26 #define DEBUG_DECL(a) a 27 #define assert(c) do { if (!(c)) debugbreak(); } while (0) 28 #else 29 #define DEBUG_IMPORT global 30 #define DEBUG_EXPORT function 31 #define DEBUG_DECL(a) 32 #define assert(c) (void)(c) 33 #endif 34 35 #if ASAN_ACTIVE 36 void __asan_poison_memory_region(void *, i64); 37 void __asan_unpoison_memory_region(void *, i64); 38 #define asan_poison_region(region, size) __asan_poison_memory_region((region), (size)) 39 #define asan_unpoison_region(region, size) __asan_unpoison_memory_region((region), (size)) 40 #else 41 #define asan_poison_region(...) 42 #define asan_unpoison_region(...) 43 #endif 44 45 #define InvalidCodePath assert(0) 46 #define InvalidDefaultCase default: assert(0); break 47 48 #define arg_list(type, ...) (type []){__VA_ARGS__}, sizeof((type []){__VA_ARGS__}) / sizeof(type) 49 50 #if COMPILER_MSVC 51 #define thread_static __declspec(thread) 52 #elif COMPILER_CLANG || COMPILER_GCC 53 #define thread_static __thread 54 #else 55 #error thread_static not defined for this compiler 56 #endif 57 58 #define alignof _Alignof 59 #define static_assert _Static_assert 60 61 /* NOTE: garbage to get the prepocessor to properly stringize the value of a macro */ 62 #define str_(...) #__VA_ARGS__ 63 #define str(...) str_(__VA_ARGS__) 64 65 #define swap(a, b) do {typeof(a) __tmp = (a); (a) = (b); (b) = __tmp;} while(0) 66 67 #define Abs(a) ((a) < 0 ? -(a) : (a)) 68 #define Sign(a) ((a) < 0 ? -1 : 1) 69 #define Between(x, a, b) ((x) >= (a) && (x) <= (b)) 70 #define Clamp(x, a, b) ((x) < (a) ? (a) : (x) > (b) ? (b) : (x)) 71 #define Clamp01(x) Clamp(x, 0, 1) 72 #define Min(a, b) ((a) < (b) ? (a) : (b)) 73 #define Max(a, b) ((a) > (b) ? (a) : (b)) 74 75 #define IsPowerOfTwo(a) (((a) & ((a) - 1)) == 0) 76 #define AlignUpPowerOfTwo(v, a) (((v) + (a) - 1) & (~((a) - 1))) 77 #define DivRoundUp(a, b) (((a) + (b) - 1) / (b)) 78 79 #define IsDigit(c) (Between((c), '0', '9')) 80 #define IsUpper(c) (((c) & 0x20u) == 0) 81 #define ToLower(c) (((c) | 0x20u)) 82 #define ToUpper(c) (((c) & ~(0x20u))) 83 84 #define IsPunctuation(c) (Between(c, '!', '/') || Between(c, ':', '@') || Between(c, '[', '`') || Between(c, '{', '~')) 85 #define IsWordBoundary(c) (((c) == ' ') || IsPunctuation(c)) 86 87 #define f32_equal(x, y) (Abs((x) - (y)) <= F32_EPSILON * Max(1.0f, Max(Abs(x), Abs(y)))) 88 89 #define DeferLoop(begin, end) for (i32 _i_ = ((begin), 0); !_i_; _i_ += 1, (end)) 90 91 #define EachBit(a, it) (u64 _##it = a, it = ctz_u64( _##it ); it != 64; _##it &= ~(1u << (it)), it = ctz_u64( _##it )) 92 #define EachElement(array, it) (u64 it = 0; it < countof(array); it += 1) 93 #define EachEnumValue(type, it) (type it = (type)0; it < type##_Count; it = (type)(it + 1)) 94 #define EachNonZeroEnumValue(type, it) (type it = (type)1; it < type##_Count; it = (type)(it + 1)) 95 #define EachIndex(count, it) (u64 it = 0; it < count; it += 1) 96 97 #define spin_while_true(c) while ((c)) cpu_yield() 98 #define spin_while_false(c) while (!(c)) cpu_yield() 99 100 // NOTE(rnp): typically for enums, wtf is wrong with modern compilers 101 #define circular_add(v, add, max) (((u64)(v) + (u64)(max) + (i64)(add)) % (u64)(max)) 102 103 #define DA_STRUCT(kind, name) typedef struct { \ 104 kind *data; \ 105 da_count count; \ 106 da_count capacity; \ 107 } name ##List; 108 109 #define SLLStackPush(list, n, next) ((n)->next = (list), (list) = (n)) 110 // TODO(rnp): clean this up 111 #define SLLPush(v, list) SLLStackPush(list, v, next) 112 113 /* NOTE(rnp): no guarantees about actually getting an element */ 114 #define SLLPop(l, next) (l); ((l) = (l) ? (l)->next : 0) 115 #define SLLStackPop(l, next) ((l) = (l)->next) 116 117 #define SLLPopFreelist(list) list; do { \ 118 asan_unpoison_region((list), sizeof(*(list))); \ 119 (void)SLLPop((list), next); \ 120 } while(0) 121 122 #define SLLPushFreelist(v, list) do { \ 123 SLLPush((v), (list)); \ 124 asan_poison_region((v), sizeof(*(v))); \ 125 } while(0) 126 127 #define DLLInsert(nil, f, l, n, next, prev) (\ 128 ((f) == 0 || (f) == nil) ? ((f) = (l) = (n), (n)->next = (n)->prev = nil) :\ 129 ((n)->next = (f), (n)->prev = (f)->prev, (f)->prev = (n), (f) = (n)),\ 130 (((n)->prev && (n)->prev != nil) ? ((n)->prev->next = (n)) : (0))) 131 132 #define DLLInsertFirst(nil, f, l, n, next, prev) DLLInsert(nil, f, l, n, next, prev) 133 #define DLLInsertLast(nil, f, l, n, next, prev) DLLInsert(nil, l, f, n, prev, next) 134 135 #define DLLRemove(nil, f, l, n, next, prev) (\ 136 ((n) == (f) ? (f) = (n)->next : (0)),\ 137 ((n) == (l) ? (l) = (n)->prev : (0)),\ 138 (((n)->prev != nil && (n)->prev != 0) ? (n)->prev->next = (n)->next : (0)),\ 139 (((n)->next != nil && (n)->next != 0) ? (n)->next->prev = (n)->prev : (0)),\ 140 (!(f) && (l) ? (f) = (l) : (0)),\ 141 (!(l) && (f) ? (l) = (f) : (0))) 142 143 typedef enum { 144 Axis2_X = 0, 145 Axis2_Y = 1, 146 Axis2_Count, 147 } Axis2; 148 #define axis2_flip(v) (!(v)) 149 150 typedef alignas(16) union { 151 u8 U8[16]; 152 u16 U16[8]; 153 u32 U32[4]; 154 u64 U64[2]; 155 u32x4 U32x4; 156 } u128; 157 158 typedef enum { 159 StringMatchFlag_CaseInsensitive = (1 << 0), 160 StringMatchFlag_SloppySize = (1 << 1), 161 } StringMatchFlags; 162 163 typedef struct { u32 cp, consumed; } UnicodeDecode; 164 165 typedef enum { 166 NumberConversionResult_Invalid, 167 NumberConversionResult_OutOfRange, 168 NumberConversionResult_Success, 169 } NumberConversionResult; 170 171 typedef enum { 172 NumberConversionKind_Invalid, 173 NumberConversionKind_Integer, 174 NumberConversionKind_Float, 175 } NumberConversionKind; 176 177 typedef struct { 178 NumberConversionResult result; 179 NumberConversionKind kind; 180 union { 181 u64 U64; 182 i64 S64; 183 f64 F64; 184 }; 185 str8 unparsed; 186 } NumberConversion; 187 188 /* TODO(rnp): delete raylib */ 189 typedef struct { 190 v3 origin; 191 v3 direction; 192 } ray; 193 194 typedef struct { v2 pos, size; } Rect; 195 196 typedef struct { 197 u8 *data; 198 i32 widx; 199 i32 cap; 200 b32 errors; 201 } Stream; 202 203 #define INVALID_FILE (-1) 204 205 #ifndef OSInvalidHandleValue 206 #define OSInvalidHandleValue ((u64)-1) 207 typedef struct { u64 value[1]; } OSBarrier; 208 typedef struct { u64 value[1]; } OSHandle; 209 typedef struct { u64 value[1]; } OSLibrary; 210 typedef struct { u64 value[1]; } OSThread; 211 typedef struct { u64 value[1]; } OSW32Semaphore; 212 #endif 213 214 #define ValidHandle(h) ((h).value[0] != OSInvalidHandleValue) 215 #define InvalidHandle(h) ((h).value[0] == OSInvalidHandleValue) 216 217 typedef struct { 218 u64 index; 219 u64 count; 220 OSBarrier barrier; 221 u64 * broadcast_memory; 222 } LaneContext; 223 224 typedef struct { 225 u8 name[16]; 226 u64 name_length; 227 228 LaneContext lane_context; 229 } ThreadContext; 230 231 #define OS_THREAD_ENTRY_POINT_FN(name) u64 name(void *user_context) 232 233 #include "meta.h" 234 #include "util.c" 235 #include "math.c" 236 237 #endif /* _UTIL_H_ */