ogl_beamforming

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

util.h (9856B)


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