util.h (5865B)
1 /* See LICENSE for license details. */ 2 #ifndef _UTIL_H_ 3 #define _UTIL_H_ 4 5 #include <stddef.h> 6 #include <stdint.h> 7 8 #ifndef asm 9 #define asm __asm__ 10 #endif 11 12 #ifndef typeof 13 #define typeof __typeof__ 14 #endif 15 16 #ifndef unreachable 17 #ifdef _MSC_VER 18 #define unreachable() __assume(0) 19 #else 20 #define unreachable() __builtin_unreachable() 21 #endif 22 #endif 23 24 #ifdef _DEBUG 25 #ifdef _WIN32 26 #define DEBUG_EXPORT __declspec(dllexport) 27 #else 28 #define DEBUG_EXPORT 29 #endif 30 #define ASSERT(c) do { if (!(c)) debugbreak(); } while (0); 31 #else 32 #define DEBUG_EXPORT static 33 #define ASSERT(c) 34 #endif 35 36 #define INVALID_CODE_PATH ASSERT(0) 37 38 #define static_assert _Static_assert 39 40 #define ARRAY_COUNT(a) (sizeof(a) / sizeof(*a)) 41 #define ABS(x) ((x) < 0 ? (-x) : (x)) 42 #define CLAMP(x, a, b) ((x) < (a) ? (a) : (x) > (b) ? (b) : (x)) 43 #define CLAMP01(x) CLAMP(x, 0, 1) 44 #define ISPOWEROF2(a) (((a) & ((a) - 1)) == 0) 45 #define MIN(a, b) ((a) < (b) ? (a) : (b)) 46 #define MAX(a, b) ((a) > (b) ? (a) : (b)) 47 #define ORONE(x) ((x)? (x) : 1) 48 #define SIGN(x) ((x) < 0? -1 : 1) 49 50 #define MEGABYTE (1024ULL * 1024ULL) 51 #define GIGABYTE (1024ULL * 1024ULL * 1024ULL) 52 53 #define U32_MAX (0xFFFFFFFFUL) 54 #define F32_INFINITY (__builtin_inff()) 55 56 typedef char c8; 57 typedef uint8_t u8; 58 typedef int16_t i16; 59 typedef uint16_t u16; 60 typedef int32_t i32; 61 typedef uint32_t u32; 62 typedef int64_t i64; 63 typedef uint64_t u64; 64 typedef uint32_t b32; 65 typedef float f32; 66 typedef double f64; 67 typedef ptrdiff_t size; 68 typedef ptrdiff_t iptr; 69 typedef size_t uptr; 70 71 #include "intrinsics.c" 72 73 typedef struct { u8 *beg, *end; } Arena; 74 typedef struct { Arena *arena; u8 *old_beg; } TempArena; 75 76 typedef struct { size len; u8 *data; } s8; 77 #define s8(s) (s8){.len = ARRAY_COUNT(s) - 1, .data = (u8 *)s} 78 79 /* NOTE: raylib stubs */ 80 #ifndef RAYLIB_H 81 typedef struct { f32 x, y; } Vector2; 82 typedef struct { f32 x, y, w, h; } Rectangle; 83 #endif 84 85 typedef union { 86 struct { i32 x, y; }; 87 struct { i32 w, h; }; 88 i32 E[2]; 89 } iv2; 90 91 typedef union { 92 struct { i32 x, y, z; }; 93 struct { i32 w, h, d; }; 94 iv2 xy; 95 i32 E[3]; 96 } iv3; 97 98 typedef union { 99 struct { u32 x, y; }; 100 struct { u32 w, h; }; 101 u32 E[2]; 102 } uv2; 103 104 typedef union { 105 struct { u32 x, y, z; }; 106 struct { u32 w, h, d; }; 107 uv2 xy; 108 u32 E[3]; 109 } uv3; 110 111 typedef union { 112 struct { u32 x, y, z, w; }; 113 struct { uv3 xyz; u32 _w; }; 114 u32 E[4]; 115 } uv4; 116 117 typedef union { 118 struct { f32 x, y; }; 119 struct { f32 w, h; }; 120 Vector2 rl; 121 f32 E[2]; 122 } v2; 123 124 typedef union { 125 struct { f32 x, y, z; }; 126 struct { f32 w, h, d; }; 127 f32 E[3]; 128 } v3; 129 130 typedef union { 131 struct { f32 x, y, z, w; }; 132 struct { f32 r, g, b, a; }; 133 struct { v3 xyz; f32 _1; }; 134 struct { f32 _2; v3 yzw; }; 135 struct { v2 xy, zw; }; 136 f32 E[4]; 137 } v4; 138 139 typedef union { 140 struct { v4 x, y, z, w; }; 141 v4 c[4]; 142 f32 E[16]; 143 } m4; 144 145 typedef union { 146 struct { v2 pos, size; }; 147 Rectangle rl; 148 } Rect; 149 #define INVERTED_INFINITY_RECT (Rect){.pos = {.x = -F32_INFINITY, .y = -F32_INFINITY}, \ 150 .size = {.x = -F32_INFINITY, .y = -F32_INFINITY}} 151 152 typedef struct { 153 iptr file; 154 char *name; 155 } Pipe; 156 #define INVALID_FILE (-1) 157 158 typedef struct { 159 size filesize; 160 f64 timestamp; 161 } FileStats; 162 #define ERROR_FILE_STATS (FileStats){.filesize = -1} 163 164 #define FILE_WATCH_CALLBACK_FN(name) b32 name(s8 path, iptr user_data, Arena tmp) 165 typedef FILE_WATCH_CALLBACK_FN(file_watch_callback); 166 167 typedef struct { 168 iptr user_data; 169 u64 hash; 170 file_watch_callback *callback; 171 } FileWatch; 172 173 typedef struct { 174 u64 hash; 175 iptr handle; 176 s8 name; 177 FileWatch file_watches[16]; 178 u32 file_watch_count; 179 Arena buffer; 180 } FileWatchDirectory; 181 182 typedef struct { 183 FileWatchDirectory directory_watches[4]; 184 iptr handle; 185 u32 directory_watch_count; 186 } FileWatchContext; 187 188 typedef struct { 189 u8 *data; 190 u32 widx; 191 u32 cap; 192 b32 errors; 193 } Stream; 194 195 enum variable_type { 196 VT_NULL, 197 VT_B32, 198 VT_F32, 199 VT_I32, 200 VT_GROUP, 201 }; 202 203 enum variable_group_type { 204 VG_LISTING, 205 VG_V2, 206 VG_V4, 207 VG_UV4, 208 }; 209 210 typedef struct { 211 void *store; 212 union { 213 v2 f32_limits; 214 iv2 i32_limits; 215 }; 216 f32 display_scale; 217 f32 scroll_scale; 218 u32 type; 219 u32 flags; 220 } Variable; 221 222 typedef struct { 223 Variable *first; 224 Variable *last; 225 u32 type; 226 } VariableGroup; 227 228 #define NULL_VARIABLE (Variable){.store = 0, .type = VT_NULL} 229 230 typedef struct Platform Platform; 231 232 #define PLATFORM_ALLOC_ARENA_FN(name) Arena name(Arena old, size capacity) 233 typedef PLATFORM_ALLOC_ARENA_FN(platform_alloc_arena_fn); 234 235 #define PLATFORM_ADD_FILE_WATCH_FN(name) void name(Platform *platform, Arena *a, s8 path, \ 236 file_watch_callback *callback, iptr user_data) 237 typedef PLATFORM_ADD_FILE_WATCH_FN(platform_add_file_watch_fn); 238 239 #define PLATFORM_CLOSE_FN(name) void name(iptr file) 240 typedef PLATFORM_CLOSE_FN(platform_close_fn); 241 242 #define PLATFORM_OPEN_FOR_WRITE_FN(name) iptr name(c8 *fname) 243 typedef PLATFORM_OPEN_FOR_WRITE_FN(platform_open_for_write_fn); 244 245 #define PLATFORM_READ_PIPE_FN(name) size name(iptr pipe, void *buf, size len) 246 typedef PLATFORM_READ_PIPE_FN(platform_read_pipe_fn); 247 248 #define PLATFORM_WRITE_NEW_FILE_FN(name) b32 name(char *fname, s8 raw) 249 typedef PLATFORM_WRITE_NEW_FILE_FN(platform_write_new_file_fn); 250 251 #define PLATFORM_WRITE_FILE_FN(name) b32 name(iptr file, s8 raw) 252 typedef PLATFORM_WRITE_FILE_FN(platform_write_file_fn); 253 254 #define PLATFORM_FNS \ 255 X(add_file_watch) \ 256 X(alloc_arena) \ 257 X(close) \ 258 X(open_for_write) \ 259 X(read_pipe) \ 260 X(write_new_file) \ 261 X(write_file) 262 263 #define X(name) platform_ ## name ## _fn *name; 264 struct Platform { 265 PLATFORM_FNS 266 FileWatchContext file_watch_context; 267 iptr os_context; 268 }; 269 #undef X 270 271 typedef struct { 272 b32 executable_reloaded; 273 b32 pipe_data_available; 274 iptr pipe_handle; 275 276 v2 mouse; 277 v2 last_mouse; 278 } BeamformerInput; 279 280 #include "util.c" 281 282 #endif /* _UTIL_H_ */