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