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