util.h (11242B)
1 /* See LICENSE for license details. */ 2 #ifndef _UTIL_H_ 3 #define _UTIL_H_ 4 5 #include "compiler.h" 6 7 #define da_count i32 8 9 #if COMPILER_MSVC 10 typedef unsigned __int64 u64; 11 typedef signed __int64 i64; 12 typedef unsigned __int32 u32; 13 typedef signed __int32 i32; 14 typedef unsigned __int16 u16; 15 typedef signed __int16 i16; 16 typedef unsigned __int8 u8; 17 typedef signed __int8 i8; 18 #else 19 typedef __UINT64_TYPE__ u64; 20 typedef __INT64_TYPE__ i64; 21 typedef __UINT32_TYPE__ u32; 22 typedef __INT32_TYPE__ i32; 23 typedef __UINT16_TYPE__ u16; 24 typedef __INT16_TYPE__ i16; 25 typedef __UINT8_TYPE__ u8; 26 typedef __INT8_TYPE__ i8; 27 #endif 28 29 typedef char c8; 30 typedef u8 b8; 31 typedef u16 b16; 32 typedef u32 b32; 33 typedef _Float16 f16; 34 typedef float f32; 35 typedef double f64; 36 typedef i64 iptr; 37 typedef u64 uptr; 38 39 #ifndef asm 40 #define asm __asm__ 41 #endif 42 43 #ifndef typeof 44 #define typeof __typeof__ 45 #endif 46 47 #if OS_WINDOWS 48 #define EXPORT __declspec(dllexport) 49 #else 50 #define EXPORT 51 #endif 52 53 #ifdef _DEBUG 54 #define DEBUG_EXPORT EXPORT 55 #ifdef _BEAMFORMER_DLL 56 #if OS_WINDOWS 57 #define DEBUG_IMPORT __declspec(dllimport) 58 #else 59 #define DEBUG_IMPORT extern 60 #endif 61 #else 62 #define DEBUG_IMPORT DEBUG_EXPORT 63 #endif 64 #define DEBUG_DECL(a) a 65 #define assert(c) do { if (!(c)) debugbreak(); } while (0) 66 #else 67 #define DEBUG_IMPORT global 68 #define DEBUG_EXPORT function 69 #define DEBUG_DECL(a) 70 #define assert(c) (void)(c) 71 #endif 72 73 #if ASAN_ACTIVE 74 void __asan_poison_memory_region(void *, i64); 75 void __asan_unpoison_memory_region(void *, i64); 76 #define asan_poison_region(region, size) __asan_poison_memory_region((region), (size)) 77 #define asan_unpoison_region(region, size) __asan_unpoison_memory_region((region), (size)) 78 #else 79 #define asan_poison_region(...) 80 #define asan_unpoison_region(...) 81 #endif 82 83 #define InvalidCodePath assert(0) 84 #define InvalidDefaultCase default: assert(0); break 85 86 #define arg_list(type, ...) (type []){__VA_ARGS__}, sizeof((type []){__VA_ARGS__}) / sizeof(type) 87 88 #define function static 89 #define global static 90 #define local_persist static 91 92 #if COMPILER_MSVC 93 #define thread_static __declspec(thread) 94 #elif COMPILER_CLANG || COMPILER_GCC 95 #define thread_static __thread 96 #else 97 #error thread_static not defined for this compiler 98 #endif 99 100 #define alignof _Alignof 101 #define static_assert _Static_assert 102 103 /* NOTE: garbage to get the prepocessor to properly stringize the value of a macro */ 104 #define str_(...) #__VA_ARGS__ 105 #define str(...) str_(__VA_ARGS__) 106 107 #define swap(a, b) do {typeof(a) __tmp = (a); (a) = (b); (b) = __tmp;} while(0) 108 109 #define Abs(a) ((a) < 0 ? -(a) : (a)) 110 #define Sign(a) ((a) < 0 ? -1 : 1) 111 #define Between(x, a, b) ((x) >= (a) && (x) <= (b)) 112 #define Clamp(x, a, b) ((x) < (a) ? (a) : (x) > (b) ? (b) : (x)) 113 #define Clamp01(x) Clamp(x, 0, 1) 114 #define Min(a, b) ((a) < (b) ? (a) : (b)) 115 #define Max(a, b) ((a) > (b) ? (a) : (b)) 116 #define IsPowerOfTwo(a) (((a) & ((a) - 1)) == 0) 117 118 #define IsDigit(c) (Between((c), '0', '9')) 119 #define IsUpper(c) (((c) & 0x20u) == 0) 120 #define ToLower(c) (((c) | 0x20u)) 121 #define ToUpper(c) (((c) & ~(0x20u))) 122 123 #define f32_equal(x, y) (Abs((x) - (y)) <= F32_EPSILON * Max(1.0f, Max(Abs(x), Abs(y)))) 124 125 #define DeferLoop(begin, end) for (i32 _i_ = ((begin), 0); !_i_; _i_ += 1, (end)) 126 127 #define EachBit(a, it) (u64 it = ctz_u64(a); it != 64; a &= ~(1u << (it)), it = ctz_u64(a)) 128 #define EachElement(array, it) (u64 it = 0; it < countof(array); it += 1) 129 #define EachEnumValue(type, it) (type it = (type)0; it < type##_Count; it = (type)(it + 1)) 130 #define EachNonZeroEnumValue(type, it) (type it = (type)1; it < type##_Count; it = (type)(it + 1)) 131 #define EachIndex(count, it) (u64 it = 0; it < count; it += 1) 132 133 #define spin_wait(c) while ((c)) cpu_yield() 134 135 // NOTE(rnp): typically for enums, wtf is wrong with modern compilers 136 #define circular_add(v, add, max) (((u64)(v) + (u64)(max) + (i64)(add)) % (u64)(max)) 137 138 #define DA_STRUCT(kind, name) typedef struct { \ 139 kind *data; \ 140 da_count count; \ 141 da_count capacity; \ 142 } name ##List; 143 144 #define SLLStackPush(list, n, next) ((n)->next = (list), (list) = (n)) 145 // TODO(rnp): clean this up 146 #define SLLPush(v, list) SLLStackPush(list, v, next) 147 148 /* NOTE(rnp): no guarantees about actually getting an element */ 149 #define SLLPop(l, next) (l); ((l) = (l) ? (l)->next : 0) 150 #define SLLStackPop(l, next) ((l) = (l)->next) 151 152 #define SLLPopFreelist(list) list; do { \ 153 asan_unpoison_region((list), sizeof(*(list))); \ 154 (void)SLLPop((list), next); \ 155 } while(0) 156 157 #define SLLPushFreelist(v, list) do { \ 158 SLLPush((v), (list)); \ 159 asan_poison_region((v), sizeof(*(v))); \ 160 } while(0) 161 162 #define DLLInsert(nil, f, l, n, next, prev) (\ 163 ((f) == 0 || (f) == nil) ? ((f) = (l) = (n), (n)->next = (n)->prev = nil) :\ 164 ((n)->next = (f), (n)->prev = (f)->prev, (f)->prev = (n), (f) = (n)),\ 165 (((n)->prev && (n)->prev != nil) ? ((n)->prev->next = (n)) : (0))) 166 167 #define DLLInsertFirst(nil, f, l, n, next, prev) DLLInsert(nil, f, l, n, next, prev) 168 #define DLLInsertLast(nil, f, l, n, next, prev) DLLInsert(nil, l, f, n, prev, next) 169 170 #define DLLRemove(nil, f, l, n, next, prev) (\ 171 ((n) == (f) ? (f) = (n)->next : (0)),\ 172 ((n) == (l) ? (l) = (n)->prev : (0)),\ 173 (((n)->prev != nil && (n)->prev != 0) ? (n)->prev->next = (n)->next : (0)),\ 174 (((n)->next != nil && (n)->next != 0) ? (n)->next->prev = (n)->prev : (0)),\ 175 (!(f) && (l) ? (f) = (l) : (0)),\ 176 (!(l) && (f) ? (l) = (f) : (0))) 177 178 #define KB(a) ((u64)(a) << 10ULL) 179 #define MB(a) ((u64)(a) << 20ULL) 180 #define GB(a) ((u64)(a) << 30ULL) 181 182 #define I8_MAX (0x0000007FL) 183 #define I32_MAX (0x7FFFFFFFL) 184 #define U8_MAX (0x000000FFUL) 185 #define U16_MAX (0x0000FFFFUL) 186 #define U32_MAX (0xFFFFFFFFUL) 187 #define U64_MAX (0xFFFFFFFFFFFFFFFFULL) 188 #define F32_EPSILON (1e-6f) 189 #ifndef PI 190 #define PI (3.14159265358979323846f) 191 #endif 192 193 #include "intrinsics.c" 194 195 typedef enum { 196 Axis2_X = 0, 197 Axis2_Y = 1, 198 Axis2_Count, 199 } Axis2; 200 #define axis2_flip(v) (!(v)) 201 202 typedef alignas(16) union { 203 u8 U8[16]; 204 u16 U16[8]; 205 u32 U32[4]; 206 u64 U64[2]; 207 u32x4 U32x4; 208 } u128; 209 210 typedef struct { u8 *beg, *end; } Arena; 211 typedef struct { Arena *arena, original_arena; } TempArena; 212 213 typedef struct { i64 length; u8 *data; } str8; 214 #define str8(s) (str8){.length = countof(s) - 1, .data = (u8 *)s} 215 #define str8_comp(s) {sizeof(s) - 1, (u8 *)s} 216 #define str8_struct(v) (str8){.length = sizeof(*v), .data = (u8 *)v} 217 218 typedef struct { i64 length; u16 *data; } str16; 219 220 typedef enum { 221 StringMatchFlag_CaseInsensitive = (1 << 0), 222 StringMatchFlag_SloppySize = (1 << 1), 223 } StringMatchFlags; 224 225 typedef struct { u32 cp, consumed; } UnicodeDecode; 226 227 typedef enum { 228 NumberConversionResult_Invalid, 229 NumberConversionResult_OutOfRange, 230 NumberConversionResult_Success, 231 } NumberConversionResult; 232 233 typedef enum { 234 NumberConversionKind_Invalid, 235 NumberConversionKind_Integer, 236 NumberConversionKind_Float, 237 } NumberConversionKind; 238 239 typedef struct { 240 NumberConversionResult result; 241 NumberConversionKind kind; 242 union { 243 u64 U64; 244 i64 S64; 245 f64 F64; 246 }; 247 str8 unparsed; 248 } NumberConversion; 249 250 typedef struct { u64 start, stop; } RangeU64; 251 252 typedef union { 253 struct { i32 x, y; }; 254 struct { i32 w, h; }; 255 i32 E[2]; 256 } iv2; 257 258 typedef union { 259 struct { i32 x, y, z; }; 260 struct { i32 w, h, d; }; 261 iv2 xy; 262 i32 E[3]; 263 } iv3; 264 265 typedef union { 266 struct { i32 x, y, z, w; }; 267 struct { iv3 xyz; i32 _w; }; 268 i32 E[4]; 269 } iv4; 270 271 typedef union { 272 struct { u32 x, y; }; 273 struct { u32 w, h; }; 274 u32 E[2]; 275 } uv2; 276 277 typedef union { 278 struct { u32 x, y, z; }; 279 struct { u32 w, h, d; }; 280 uv2 xy; 281 u32 E[3]; 282 } uv3; 283 284 typedef union { 285 struct { u32 x, y, z, w; }; 286 struct { uv3 xyz; u32 _w; }; 287 u32 E[4]; 288 } uv4; 289 290 typedef union { 291 struct { b32 x, y, z; }; 292 b32 E[3]; 293 } bv3; 294 295 typedef union { 296 struct { f32 x, y; }; 297 struct { f32 w, h; }; 298 f32 E[2]; 299 } v2; 300 #define V2_INFINITY (v2){{-inf32(), inf32()}} 301 302 typedef union { 303 struct { f32 x, y, z; }; 304 struct { f32 w, h, d; }; 305 struct { v2 xy; f32 _1; }; 306 struct { f32 _2; v2 yz; }; 307 f32 E[3]; 308 } v3; 309 310 typedef union { 311 struct { f32 x, y, z, w; }; 312 struct { f32 r, g, b, a; }; 313 struct { v3 xyz; f32 _1; }; 314 struct { f32 _2; v3 yzw; }; 315 struct { v2 xy, zw; }; 316 f32 E[4]; 317 } v4; 318 319 #define XZ(v) (v2){.x = v.x, .y = v.z} 320 #define YZ(v) (v2){.x = v.y, .y = v.z} 321 #define XY(v) (v2){.x = v.x, .y = v.y} 322 323 typedef union { 324 struct { v4 x, y, z, w; }; 325 v4 c[4]; 326 f32 E[16]; 327 } m4; 328 329 /* TODO(rnp): delete raylib */ 330 typedef struct { 331 v3 origin; 332 v3 direction; 333 } ray; 334 335 typedef struct { v2 pos, size; } Rect; 336 337 typedef struct { 338 u8 *data; 339 i32 widx; 340 i32 cap; 341 b32 errors; 342 } Stream; 343 344 #define INVALID_FILE (-1) 345 346 #ifndef OSInvalidHandleValue 347 #define OSInvalidHandleValue ((u64)-1) 348 typedef struct { u64 value[1]; } OSBarrier; 349 typedef struct { u64 value[1]; } OSHandle; 350 typedef struct { u64 value[1]; } OSLibrary; 351 typedef struct { u64 value[1]; } OSThread; 352 typedef struct { u64 value[1]; } OSW32Semaphore; 353 #endif 354 355 #define ValidHandle(h) ((h).value[0] != OSInvalidHandleValue) 356 #define InvalidHandle(h) ((h).value[0] == OSInvalidHandleValue) 357 358 typedef struct { 359 u64 index; 360 u64 count; 361 OSBarrier barrier; 362 u64 * broadcast_memory; 363 } LaneContext; 364 365 typedef struct { 366 u8 name[16]; 367 u64 name_length; 368 369 LaneContext lane_context; 370 } ThreadContext; 371 372 #define OS_ALLOC_ARENA_FN(name) Arena name(i64 capacity) 373 #define OS_READ_ENTIRE_FILE_FN(name) i64 name(const char *file, void *buffer, i64 buffer_capacity) 374 #define OS_WAIT_ON_ADDRESS_FN(name) b32 name(i32 *value, i32 current, u32 timeout_ms) 375 #define OS_WAKE_ALL_WAITERS_FN(name) void name(i32 *sync) 376 #define OS_THREAD_ENTRY_POINT_FN(name) u64 name(void *user_context) 377 378 #define OS_WRITE_NEW_FILE_FN(name) b32 name(char *fname, str8 raw) 379 typedef OS_WRITE_NEW_FILE_FN(os_write_new_file_fn); 380 381 #define RENDERDOC_GET_API_FN(name) b32 name(u32 version, void **out_api) 382 typedef RENDERDOC_GET_API_FN(renderdoc_get_api_fn); 383 384 #define RENDERDOC_START_FRAME_CAPTURE_FN(name) void name(void *instance_handle, iptr window_handle) 385 typedef RENDERDOC_START_FRAME_CAPTURE_FN(renderdoc_start_frame_capture_fn); 386 387 #define RENDERDOC_END_FRAME_CAPTURE_FN(name) b32 name(void *instance_handle, iptr window_handle) 388 typedef RENDERDOC_END_FRAME_CAPTURE_FN(renderdoc_end_frame_capture_fn); 389 390 #define RENDERDOC_SET_CAPTURE_PATH_TEMPLATE_FN(name) void name(const char *template) 391 typedef RENDERDOC_SET_CAPTURE_PATH_TEMPLATE_FN(renderdoc_set_capture_path_template_fn); 392 393 typedef alignas(16) u8 RenderDocAPI[216]; 394 #define RENDERDOC_API_FN_ADDR(a, offset) (*(iptr *)((*a) + offset)) 395 #define RENDERDOC_START_FRAME_CAPTURE(a) (renderdoc_start_frame_capture_fn *) RENDERDOC_API_FN_ADDR(a, 152) 396 #define RENDERDOC_END_FRAME_CAPTURE(a) (renderdoc_end_frame_capture_fn *) RENDERDOC_API_FN_ADDR(a, 168) 397 #define RENDERDOC_SET_CAPTURE_PATH_TEMPLATE(a) (renderdoc_set_capture_path_template_fn *) RENDERDOC_API_FN_ADDR(a, 184) 398 399 #include "util.c" 400 #include "math.c" 401 402 #endif /* _UTIL_H_ */