ogl_beamforming

Ultrasound Beamforming Implemented with OpenGL
git clone anongit@rnpnr.xyz:ogl_beamforming.git
Log | Files | Refs | Feed | Submodules | README | LICENSE

util.h (10472B)


      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 Sign(a)          ((a) < 0 ? -1 : 1)
    125 #define Between(x, a, b) ((x) >= (a) && (x) <= (b))
    126 #define Clamp(x, a, b)   ((x) < (a) ? (a) : (x) > (b) ? (b) : (x))
    127 #define Min(a, b)        ((a) < (b) ? (a) : (b))
    128 #define Max(a, b)        ((a) > (b) ? (a) : (b))
    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 	iz    count;    \
    151 	iz    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 { iz 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 { iz len; u16 *data; } s16;
    216 
    217 typedef struct { u32 cp, consumed; } UnicodeDecode;
    218 
    219 typedef enum {
    220 	IntegerConversionResult_Invalid,
    221 	IntegerConversionResult_OutOfRange,
    222 	IntegerConversionResult_Success,
    223 } IntegerConversionResult;
    224 
    225 typedef struct {
    226 	IntegerConversionResult result;
    227 	union {
    228 		u64 U64;
    229 		i64 S64;
    230 	};
    231 	s8 unparsed;
    232 } IntegerConversion;
    233 
    234 typedef struct { u64 start, stop; } RangeU64;
    235 
    236 typedef union {
    237 	struct { i32 x, y; };
    238 	struct { i32 w, h; };
    239 	i32 E[2];
    240 } iv2;
    241 
    242 typedef union {
    243 	struct { i32 x, y, z; };
    244 	struct { i32 w, h, d; };
    245 	iv2 xy;
    246 	i32 E[3];
    247 } iv3;
    248 
    249 typedef union {
    250 	struct { i32 x, y, z, w; };
    251 	struct { iv3 xyz; i32 _w; };
    252 	i32 E[4];
    253 } iv4;
    254 
    255 typedef union {
    256 	struct { u32 x, y; };
    257 	struct { u32 w, h; };
    258 	u32 E[2];
    259 } uv2;
    260 
    261 typedef union {
    262 	struct { u32 x, y, z; };
    263 	struct { u32 w, h, d; };
    264 	uv2 xy;
    265 	u32 E[3];
    266 } uv3;
    267 
    268 typedef union {
    269 	struct { u32 x, y, z, w; };
    270 	struct { uv3 xyz; u32 _w; };
    271 	u32 E[4];
    272 } uv4;
    273 
    274 typedef union {
    275 	struct { f32 x, y; };
    276 	struct { f32 w, h; };
    277 	f32 E[2];
    278 } v2;
    279 
    280 typedef union {
    281 	struct { f32 x,  y, z;   };
    282 	struct { f32 w,  h, d;   };
    283 	struct { v2  xy; f32 _1; };
    284 	struct { f32 _2; v2 yz;  };
    285 	f32 E[3];
    286 } v3;
    287 
    288 typedef union {
    289 	struct { f32 x, y, z, w; };
    290 	struct { f32 r, g, b, a; };
    291 	struct { v3 xyz; f32 _1; };
    292 	struct { f32 _2; v3 yzw; };
    293 	struct { v2 xy, zw; };
    294 	f32 E[4];
    295 } v4;
    296 
    297 #define XZ(v) (v2){.x = v.x, .y = v.z}
    298 #define YZ(v) (v2){.x = v.y, .y = v.z}
    299 #define XY(v) (v2){.x = v.x, .y = v.y}
    300 
    301 typedef union {
    302 	struct { v4 x, y, z, w; };
    303 	v4  c[4];
    304 	f32 E[16];
    305 } m4;
    306 
    307 /* TODO(rnp): delete raylib */
    308 typedef struct {
    309 	v3 origin;
    310 	v3 direction;
    311 } ray;
    312 
    313 typedef struct { v2 pos, size; } Rect;
    314 #define INVERTED_INFINITY_RECT (Rect){.pos  = {.x = -F32_INFINITY, .y = -F32_INFINITY}, \
    315                                       .size = {.x = -F32_INFINITY, .y = -F32_INFINITY}}
    316 
    317 typedef struct {
    318 	u8   *data;
    319 	i32   widx;
    320 	i32   cap;
    321 	b32   errors;
    322 } Stream;
    323 
    324 #define INVALID_FILE       (-1)
    325 
    326 #ifndef OSInvalidHandleValue
    327   #define OSInvalidHandleValue ((u64)-1)
    328   typedef struct { u64 value[1]; } OSBarrier;
    329   typedef struct { u64 value[1]; } OSHandle;
    330   typedef struct { u64 value[1]; } OSLibrary;
    331   typedef struct { u64 value[1]; } OSThread;
    332   typedef struct { u64 value[1]; } OSW32Semaphore;
    333 #endif
    334 
    335 #define ValidHandle(h)     ((h).value[0] != OSInvalidHandleValue)
    336 #define InvalidHandle(h)   ((h).value[0] == OSInvalidHandleValue)
    337 
    338 typedef struct OS OS;
    339 
    340 typedef struct {
    341 	u64        index;
    342 	u64        count;
    343 	OSBarrier  barrier;
    344 	u64 *      broadcast_memory;
    345 } LaneContext;
    346 
    347 typedef struct {
    348 	u8   name[16];
    349 	u64  name_length;
    350 
    351 	LaneContext lane_context;
    352 } ThreadContext;
    353 
    354 #define OS_ALLOC_ARENA_FN(name)        Arena name(iz capacity)
    355 #define OS_READ_ENTIRE_FILE_FN(name)   i64   name(const char *file, void *buffer, i64 buffer_capacity)
    356 #define OS_WAIT_ON_ADDRESS_FN(name)    b32   name(i32 *value, i32 current, u32 timeout_ms)
    357 #define OS_WAKE_ALL_WAITERS_FN(name)   void  name(i32 *sync)
    358 #define OS_THREAD_ENTRY_POINT_FN(name) u64   name(void *user_context)
    359 
    360 #define OS_WRITE_NEW_FILE_FN(name) b32 name(char *fname, s8 raw)
    361 typedef OS_WRITE_NEW_FILE_FN(os_write_new_file_fn);
    362 
    363 #define RENDERDOC_GET_API_FN(name) b32 name(u32 version, void **out_api)
    364 typedef RENDERDOC_GET_API_FN(renderdoc_get_api_fn);
    365 
    366 #define RENDERDOC_START_FRAME_CAPTURE_FN(name) void name(iptr gl_context, iptr window_handle)
    367 typedef RENDERDOC_START_FRAME_CAPTURE_FN(renderdoc_start_frame_capture_fn);
    368 
    369 #define RENDERDOC_END_FRAME_CAPTURE_FN(name) b32 name(iptr gl_context, iptr window_handle)
    370 typedef RENDERDOC_END_FRAME_CAPTURE_FN(renderdoc_end_frame_capture_fn);
    371 
    372 typedef alignas(16) u8 RenderDocAPI[216];
    373 #define RENDERDOC_API_FN_ADDR(a, offset) (*(iptr *)((*a) + offset))
    374 #define RENDERDOC_START_FRAME_CAPTURE(a) (renderdoc_start_frame_capture_fn *)RENDERDOC_API_FN_ADDR(a, 152)
    375 #define RENDERDOC_END_FRAME_CAPTURE(a)   (renderdoc_end_frame_capture_fn *)  RENDERDOC_API_FN_ADDR(a, 168)
    376 
    377 #define LABEL_GL_OBJECT(type, id, s) {s8 _s = (s); glObjectLabel(type, id, (i32)_s.len, (c8 *)_s.data);}
    378 
    379 #include "util.c"
    380 #include "math.c"
    381 
    382 #endif /* _UTIL_H_ */