util.h (1454B)
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 #ifdef _DEBUG 13 #define ASSERT(c) do { if (!(c)) asm("int3; nop"); } while (0); 14 #else 15 #define ASSERT(c) 16 #endif 17 18 #define static_assert _Static_assert 19 20 #define ARRAY_COUNT(a) (sizeof(a) / sizeof(*a)) 21 #define ABS(x) ((x) < 0 ? (-x) : (x)) 22 #define CLAMP(x, a, b) ((x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)) 23 #define CLAMP01(x) CLAMP(x, 0, 1) 24 #define ISPOWEROF2(a) (((a) & ((a) - 1)) == 0) 25 #define MIN(a, b) ((a) < (b) ? (a) : (b)) 26 #define MAX(a, b) ((a) > (b) ? (a) : (b)) 27 #define ORONE(x) ((x)? (x) : 1) 28 29 #define MEGABYTE (1024ULL * 1024ULL) 30 #define GIGABYTE (1024ULL * 1024ULL * 1024ULL) 31 32 #define U32_MAX (0xFFFFFFFFUL) 33 34 typedef uint8_t u8; 35 typedef int16_t i16; 36 typedef uint16_t u16; 37 typedef int32_t i32; 38 typedef uint32_t u32; 39 typedef uint64_t u64; 40 typedef uint32_t b32; 41 typedef float f32; 42 typedef double f64; 43 typedef ptrdiff_t size; 44 45 typedef struct { u8 *beg, *end; } Arena; 46 47 typedef struct { size len; u8 *data; } s8; 48 #define s8(s) (s8){.len = ARRAY_COUNT(s) - 1, .data = (u8 *)s} 49 50 typedef union { 51 struct { i32 x, y; }; 52 struct { i32 w, h; }; 53 i32 E[2]; 54 } iv2; 55 56 typedef union { 57 struct { u32 x, y; }; 58 struct { u32 w, h; }; 59 u32 E[2]; 60 } uv2; 61 62 typedef union { 63 struct { u32 x, y, z, w; }; 64 u32 E[4]; 65 } uv4; 66 67 #include "util.c" 68 69 #endif /* _UTIL_H_ */