util.h (11051B)
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 iz; 37 typedef u64 uz; 38 typedef i64 iptr; 39 typedef u64 uptr; 40 41 #ifndef asm 42 #define asm __asm__ 43 #endif 44 45 #ifndef typeof 46 #define typeof __typeof__ 47 #endif 48 49 #if OS_WINDOWS 50 #define EXPORT __declspec(dllexport) 51 #else 52 #define EXPORT 53 #endif 54 55 #ifdef _DEBUG 56 #define DEBUG_EXPORT EXPORT 57 #ifdef _BEAMFORMER_DLL 58 #if OS_WINDOWS 59 #define DEBUG_IMPORT __declspec(dllimport) 60 #else 61 #define DEBUG_IMPORT extern 62 #endif 63 #else 64 #define DEBUG_IMPORT DEBUG_EXPORT 65 #endif 66 #define DEBUG_DECL(a) a 67 #define assert(c) do { if (!(c)) debugbreak(); } while (0) 68 #else 69 #define DEBUG_IMPORT global 70 #define DEBUG_EXPORT function 71 #define DEBUG_DECL(a) 72 #define assert(c) (void)(c) 73 #endif 74 #define ASSERT assert 75 76 #if ASAN_ACTIVE 77 void __asan_poison_memory_region(void *, i64); 78 void __asan_unpoison_memory_region(void *, i64); 79 #define asan_poison_region(region, size) __asan_poison_memory_region((region), (size)) 80 #define asan_unpoison_region(region, size) __asan_unpoison_memory_region((region), (size)) 81 #else 82 #define asan_poison_region(...) 83 #define asan_unpoison_region(...) 84 #endif 85 86 #define InvalidCodePath assert(0) 87 #define InvalidDefaultCase default: assert(0); break 88 89 #define arg_list(type, ...) (type []){__VA_ARGS__}, sizeof((type []){__VA_ARGS__}) / sizeof(type) 90 91 #define function static 92 #define global static 93 #define local_persist static 94 95 #if COMPILER_MSVC 96 #define thread_static __declspec(thread) 97 #elif COMPILER_CLANG || COMPILER_GCC 98 #define thread_static __thread 99 #else 100 #error thread_static not defined for this compiler 101 #endif 102 103 #define alignof _Alignof 104 #define static_assert _Static_assert 105 106 /* NOTE: garbage to get the prepocessor to properly stringize the value of a macro */ 107 #define str_(...) #__VA_ARGS__ 108 #define str(...) str_(__VA_ARGS__) 109 110 #define countof(a) (iz)(sizeof(a) / sizeof(*a)) 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 ISPOWEROF2(a) (((a) & ((a) - 1)) == 0) 115 #define MIN(a, b) ((a) < (b) ? (a) : (b)) 116 #define MAX(a, b) ((a) > (b) ? (a) : (b)) 117 #define ORONE(x) ((x)? (x) : 1) 118 #define SIGN(x) ((x) < 0? -1 : 1) 119 #define swap(a, b) do {typeof(a) __tmp = (a); (a) = (b); (b) = __tmp;} while(0) 120 121 #define Abs(a) ((a) < 0 ? -(a) : (a)) 122 #define Sign(a) ((a) < 0 ? -1 : 1) 123 #define Between(x, a, b) ((x) >= (a) && (x) <= (b)) 124 #define Clamp(x, a, b) ((x) < (a) ? (a) : (x) > (b) ? (b) : (x)) 125 #define Clamp01(x) Clamp(x, 0, 1) 126 #define Min(a, b) ((a) < (b) ? (a) : (b)) 127 #define Max(a, b) ((a) > (b) ? (a) : (b)) 128 #define IsPowerOfTwo(a) (((a) & ((a) - 1)) == 0) 129 130 #define IsDigit(c) (Between((c), '0', '9')) 131 #define IsUpper(c) (((c) & 0x20u) == 0) 132 #define ToLower(c) (((c) | 0x20u)) 133 #define ToUpper(c) (((c) & ~(0x20u))) 134 135 #define f32_equal(x, y) (Abs((x) - (y)) <= F32_EPSILON * Max(1.0f, Max(Abs(x), Abs(y)))) 136 137 #define DeferLoop(begin, end) for (i32 _i_ = ((begin), 0); !_i_; _i_ += 1, (end)) 138 #define DeferLoopTag(begin, end, tag) for (i32 __##tag = ((begin), 0); !__##tag ; __##tag += 1, (end)) 139 140 #define EachBit(a, it) (u64 it = ctz_u64(a); it != 64; a &= ~(1u << (it)), it = ctz_u64(a)) 141 #define EachElement(array, it) (u64 it = 0; it < countof(array); it += 1) 142 #define EachEnumValue(type, it) (type it = (type)0; it < type##_Count; it = (type)(it + 1)) 143 #define EachNonZeroEnumValue(type, it) (type it = (type)1; it < type##_Count; it = (type)(it + 1)) 144 #define EachIndex(count, it) (u64 it = 0; it < count; it += 1) 145 146 #define spin_wait(c) while ((c)) cpu_yield() 147 148 #define DA_STRUCT(kind, name) typedef struct { \ 149 kind *data; \ 150 da_count count; \ 151 da_count capacity; \ 152 } name ##List; 153 154 /* NOTE(rnp): no guarantees about actually getting an element */ 155 #define SLLPop(list) list; list = list ? list->next : 0 156 #define SLLPush(v, list) do { \ 157 (v)->next = (list); \ 158 (list) = v; \ 159 } while (0) 160 161 #define SLLPopFreelist(list) list; do { \ 162 asan_unpoison_region((list), sizeof(*(list))); \ 163 (void)SLLPop((list)); \ 164 } while(0) 165 166 #define SLLPushFreelist(v, list) do { \ 167 SLLPush((v), (list)); \ 168 asan_poison_region((v), sizeof(*(v))); \ 169 } while(0) 170 171 #define DLLPushDown(v, list) do { \ 172 (v)->next = (list); \ 173 if ((v)->next) (v)->next->prev = (v); \ 174 (list) = (v); \ 175 } while (0) 176 177 #define DLLRemove(v) do { \ 178 if ((v)->next) (v)->next->prev = (v)->prev; \ 179 if ((v)->prev) (v)->prev->next = (v)->next; \ 180 } while (0) 181 182 #define KB(a) ((u64)(a) << 10ULL) 183 #define MB(a) ((u64)(a) << 20ULL) 184 #define GB(a) ((u64)(a) << 30ULL) 185 186 #define I8_MAX (0x0000007FL) 187 #define I32_MAX (0x7FFFFFFFL) 188 #define U8_MAX (0x000000FFUL) 189 #define U16_MAX (0x0000FFFFUL) 190 #define U32_MAX (0xFFFFFFFFUL) 191 #define U64_MAX (0xFFFFFFFFFFFFFFFFULL) 192 #define F32_INFINITY (1e+300*1e+300) 193 #define F32_EPSILON (1e-6f) 194 #ifndef PI 195 #define PI (3.14159265358979323846f) 196 #endif 197 198 #include "intrinsics.c" 199 200 typedef alignas(16) union { 201 u8 U8[16]; 202 u16 U16[8]; 203 u32 U32[4]; 204 u64 U64[2]; 205 u32x4 U32x4; 206 } u128; 207 208 typedef struct { u8 *beg, *end; } Arena; 209 typedef struct { Arena *arena, original_arena; } TempArena; 210 211 typedef struct { i64 len; u8 *data; } s8; 212 #define s8(s) (s8){.len = countof(s) - 1, .data = (u8 *)s} 213 #define s8_comp(s) {sizeof(s) - 1, (u8 *)s} 214 215 typedef struct { i64 length; u8 *data; } str8; 216 #define str8(s) (str8){.length = countof(s) - 1, .data = (u8 *)s} 217 #define str8_comp(s) {sizeof(s) - 1, (u8 *)s} 218 #define str8_struct(v) (str8){.length = sizeof(*v), .data = (u8 *)v} 219 220 #define str8_from_s8(s) (str8){.length = (s).len, .data = (s).data} 221 #define s8_from_str8(s) (s8){.len = (s).length, .data = (s).data} 222 223 typedef struct { i64 len; u16 *data; } s16; 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 s8 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 { f32 x, y; }; 292 struct { f32 w, h; }; 293 f32 E[2]; 294 } v2; 295 296 typedef union { 297 struct { f32 x, y, z; }; 298 struct { f32 w, h, d; }; 299 struct { v2 xy; f32 _1; }; 300 struct { f32 _2; v2 yz; }; 301 f32 E[3]; 302 } v3; 303 304 typedef union { 305 struct { f32 x, y, z, w; }; 306 struct { f32 r, g, b, a; }; 307 struct { v3 xyz; f32 _1; }; 308 struct { f32 _2; v3 yzw; }; 309 struct { v2 xy, zw; }; 310 f32 E[4]; 311 } v4; 312 313 #define XZ(v) (v2){.x = v.x, .y = v.z} 314 #define YZ(v) (v2){.x = v.y, .y = v.z} 315 #define XY(v) (v2){.x = v.x, .y = v.y} 316 317 typedef union { 318 struct { v4 x, y, z, w; }; 319 v4 c[4]; 320 f32 E[16]; 321 } m4; 322 323 /* TODO(rnp): delete raylib */ 324 typedef struct { 325 v3 origin; 326 v3 direction; 327 } ray; 328 329 typedef struct { v2 pos, size; } Rect; 330 #define INVERTED_INFINITY_RECT (Rect){.pos = {.x = -F32_INFINITY, .y = -F32_INFINITY}, \ 331 .size = {.x = -F32_INFINITY, .y = -F32_INFINITY}} 332 333 typedef struct { 334 u8 *data; 335 i32 widx; 336 i32 cap; 337 b32 errors; 338 } Stream; 339 340 #define INVALID_FILE (-1) 341 342 #ifndef OSInvalidHandleValue 343 #define OSInvalidHandleValue ((u64)-1) 344 typedef struct { u64 value[1]; } OSBarrier; 345 typedef struct { u64 value[1]; } OSHandle; 346 typedef struct { u64 value[1]; } OSLibrary; 347 typedef struct { u64 value[1]; } OSThread; 348 typedef struct { u64 value[1]; } OSW32Semaphore; 349 #endif 350 351 #define ValidHandle(h) ((h).value[0] != OSInvalidHandleValue) 352 #define InvalidHandle(h) ((h).value[0] == OSInvalidHandleValue) 353 354 typedef struct OS OS; 355 356 typedef struct { 357 u64 index; 358 u64 count; 359 OSBarrier barrier; 360 u64 * broadcast_memory; 361 } LaneContext; 362 363 typedef struct { 364 u8 name[16]; 365 u64 name_length; 366 367 LaneContext lane_context; 368 } ThreadContext; 369 370 #define OS_ALLOC_ARENA_FN(name) Arena name(iz capacity) 371 #define OS_READ_ENTIRE_FILE_FN(name) i64 name(const char *file, void *buffer, i64 buffer_capacity) 372 #define OS_WAIT_ON_ADDRESS_FN(name) b32 name(i32 *value, i32 current, u32 timeout_ms) 373 #define OS_WAKE_ALL_WAITERS_FN(name) void name(i32 *sync) 374 #define OS_THREAD_ENTRY_POINT_FN(name) u64 name(void *user_context) 375 376 #define OS_WRITE_NEW_FILE_FN(name) b32 name(char *fname, s8 raw) 377 typedef OS_WRITE_NEW_FILE_FN(os_write_new_file_fn); 378 379 #define RENDERDOC_GET_API_FN(name) b32 name(u32 version, void **out_api) 380 typedef RENDERDOC_GET_API_FN(renderdoc_get_api_fn); 381 382 #define RENDERDOC_START_FRAME_CAPTURE_FN(name) void name(iptr gl_context, iptr window_handle) 383 typedef RENDERDOC_START_FRAME_CAPTURE_FN(renderdoc_start_frame_capture_fn); 384 385 #define RENDERDOC_END_FRAME_CAPTURE_FN(name) b32 name(iptr gl_context, iptr window_handle) 386 typedef RENDERDOC_END_FRAME_CAPTURE_FN(renderdoc_end_frame_capture_fn); 387 388 typedef alignas(16) u8 RenderDocAPI[216]; 389 #define RENDERDOC_API_FN_ADDR(a, offset) (*(iptr *)((*a) + offset)) 390 #define RENDERDOC_START_FRAME_CAPTURE(a) (renderdoc_start_frame_capture_fn *)RENDERDOC_API_FN_ADDR(a, 152) 391 #define RENDERDOC_END_FRAME_CAPTURE(a) (renderdoc_end_frame_capture_fn *) RENDERDOC_API_FN_ADDR(a, 168) 392 393 #define LABEL_GL_OBJECT(type, id, s) {s8 _s = (s); glObjectLabel(type, id, (i32)_s.len, (c8 *)_s.data);} 394 395 #include "util.c" 396 #include "math.c" 397 398 #endif /* _UTIL_H_ */