ogl_beamforming

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

util.c (22300B)


      1 /* See LICENSE for license details. */
      2 #if   COMPILER_CLANG
      3   #pragma GCC diagnostic ignored "-Winitializer-overrides"
      4 #elif COMPILER_GCC
      5   #pragma GCC diagnostic ignored "-Woverride-init"
      6 #endif
      7 
      8 #define zero_struct(s) memory_clear((s), 0, sizeof(*(s)))
      9 function void *
     10 memory_clear(void *restrict p_, u8 c, u64 size)
     11 {
     12 	u8 *p = p_;
     13 	while (size > 0) p[--size] = c;
     14 	return p;
     15 }
     16 
     17 function b32
     18 memory_equal(void *restrict left, void *restrict right, u64 n)
     19 {
     20 	u8 *a = left, *b = right;
     21 	b32 result = 1;
     22 	for (; result && n; n--)
     23 		result &= *a++ == *b++;
     24 	return result;
     25 }
     26 
     27 function void
     28 memory_copy(void *restrict dest, void *restrict src, u64 n)
     29 {
     30 	u8 *s = src, *d = dest;
     31 	#ifdef __AVX512BW__
     32 	{
     33 		for (; n >= 64; n -= 64, s += 64, d += 64)
     34 			_mm512_storeu_epi8(d, _mm512_loadu_epi8(s));
     35 		__mmask64 k = _cvtu64_mask64(_bzhi_u64(-1ULL, n));
     36 		_mm512_mask_storeu_epi8(d, k, _mm512_maskz_loadu_epi8(k, s));
     37 	}
     38 	#else
     39 		for (; n; n--) *d++ = *s++;
     40 	#endif
     41 }
     42 
     43 /* IMPORTANT: this function may fault if dest, src, and n are not multiples of 64 */
     44 function void
     45 memory_copy_non_temporal(void *restrict dest, void *restrict src, u64 n)
     46 {
     47 	assume(((u64)dest & 63) == 0);
     48 	assume(((u64)src  & 63) == 0);
     49 	assume(((u64)n    & 63) == 0);
     50 	u8 *s = src, *d = dest;
     51 
     52 	#if defined(__AVX512BW__)
     53 	{
     54 		for (; n >= 64; n -= 64, s += 64, d += 64)
     55 			_mm512_stream_si512((__m512i *)d, _mm512_stream_load_si512((__m512i *)s));
     56 	}
     57 	#elif defined(__AVX2__)
     58 	{
     59 		for (; n >= 32; n -= 32, s += 32, d += 32)
     60 			_mm256_stream_si256((__m256i *)d, _mm256_stream_load_si256((__m256i *)s));
     61 	}
     62 	#elif ARCH_ARM64 && !COMPILER_MSVC
     63 	{
     64 		asm volatile (
     65 			"cbz  %2, 2f\n"
     66 			"1: ldnp q0, q1, [%1]\n"
     67 			"subs %2, %2, #32\n"
     68 			"add  %1, %1, #32\n"
     69 			"stnp q0, q1, [%0]\n"
     70 			"add  %0, %0, #32\n"
     71 			"b.ne 1b\n"
     72 			"2:"
     73 			:  "+r"(d), "+r"(s), "+r"(n)
     74 			:: "memory", "v0", "v1"
     75 		);
     76 	}
     77 	#else
     78 		memory_copy(d, s, n);
     79 	#endif
     80 }
     81 
     82 function void
     83 memory_move(void *dest, void *src, u64 n)
     84 {
     85 	u8 *d = dest, *s = src;
     86 	if (d < s) memory_copy(d, s, n);
     87 	else            while (n) { n--; d[n] = s[n]; }
     88 }
     89 
     90 function void *
     91 memory_scan_backwards(void *memory, u8 byte, iz n)
     92 {
     93 	void *result = 0;
     94 	u8   *s      = memory;
     95 	while (n > 0) if (s[--n] == byte) { result = s + n; break; }
     96 	return result;
     97 }
     98 
     99 function Arena
    100 arena_from_memory(void *memory, u64 size)
    101 {
    102 	Arena result;
    103 	result.beg = memory;
    104 	result.end = result.beg + size;
    105 	return result;
    106 }
    107 
    108 function void *
    109 align_pointer_up(void *p, uz alignment)
    110 {
    111 	uz padding = -(u64)p & (alignment - 1);
    112 	void *result = (u8 *)p + padding;
    113 	return result;
    114 }
    115 
    116 function void *
    117 arena_aligned_start(Arena a, uz alignment)
    118 {
    119 	return align_pointer_up(a.beg, alignment);
    120 }
    121 
    122 #define arena_capacity(a, t) arena_capacity_(a, sizeof(t), alignof(t))
    123 function i64
    124 arena_capacity_(Arena *a, i64 size, u64 alignment)
    125 {
    126 	i64 available = a->end - (u8 *)arena_aligned_start(*a, alignment);
    127 	i64 result    = available / size;
    128 	return result;
    129 }
    130 
    131 function u8 *
    132 arena_commit(Arena *a, iz size)
    133 {
    134 	assert(a->end - a->beg >= size);
    135 	u8 *result = a->beg;
    136 	a->beg += size;
    137 	return result;
    138 }
    139 
    140 function void
    141 arena_pop(Arena *a, iz length)
    142 {
    143 	a->beg -= length;
    144 }
    145 
    146 typedef enum {
    147 	ArenaAllocateFlags_NoZero = 1 << 0,
    148 } ArenaAllocateFlags;
    149 
    150 typedef struct {
    151 	iz size;
    152 	uz align;
    153 	iz count;
    154 	ArenaAllocateFlags flags;
    155 } ArenaAllocateInfo;
    156 
    157 #define arena_alloc(a, ...)         arena_alloc_(a, (ArenaAllocateInfo){.align = 8, .count = 1, ##__VA_ARGS__})
    158 #define push_array(a, t, n)         (t *)arena_alloc(a, .size = sizeof(t), .align = alignof(t), .count = n)
    159 #define push_array_no_zero(a, t, n) (t *)arena_alloc(a, .size = sizeof(t), .align = alignof(t), .count = n, .flags = ArenaAllocateFlags_NoZero)
    160 #define push_struct(a, t)           push_array(a, t, 1)
    161 #define push_struct_no_zero(a, t)   push_array_no_zero(a, t, 1)
    162 
    163 function void *
    164 arena_alloc_(Arena *a, ArenaAllocateInfo info)
    165 {
    166 	void *result = 0;
    167 	if (a->beg) {
    168 		u8 *start = arena_aligned_start(*a, info.align);
    169 		iz available = a->end - start;
    170 		assert((available >= 0 && info.count <= available / info.size));
    171 		asan_unpoison_region(start, info.count * info.size);
    172 		a->beg = start + info.count * info.size;
    173 		result = start;
    174 		if ((info.flags & ArenaAllocateFlags_NoZero) == 0)
    175 			result = memory_clear(start, 0, info.count * info.size);
    176 	}
    177 	return result;
    178 }
    179 
    180 function Arena
    181 sub_arena(Arena *a, iz size, uz align)
    182 {
    183 	Arena result = {.beg = arena_alloc(a, .size = size, .align = align, .flags = ArenaAllocateFlags_NoZero)};
    184 	result.end   = result.beg + size;
    185 	return result;
    186 }
    187 
    188 function Arena
    189 sub_arena_end(Arena *a, iz len, uz align)
    190 {
    191 	Arena result;
    192 	result.beg = (u8 *)((u64)(a->end - len) & ~(align - 1)),
    193 	result.end = a->end,
    194 
    195 	a->end = result.beg;
    196 	assert(a->end >= a->beg);
    197 
    198 	return result;
    199 }
    200 
    201 function TempArena
    202 begin_temp_arena(Arena *a)
    203 {
    204 	TempArena result = {.arena = a, .original_arena = *a};
    205 	return result;
    206 }
    207 
    208 function void
    209 end_temp_arena(TempArena ta)
    210 {
    211 	Arena *a = ta.arena;
    212 	if (a) {
    213 		assert(a->beg >= ta.original_arena.beg);
    214 		*a = ta.original_arena;
    215 	}
    216 }
    217 
    218 
    219 enum { DA_INITIAL_CAP = 16 };
    220 
    221 #define da_index(it, s) ((it) - (s)->data)
    222 #define da_reserve(a, s, n) \
    223   (s)->data = da_reserve_((a), (s)->data, &(s)->capacity, (s)->count + n, \
    224                           _Alignof(typeof(*(s)->data)), sizeof(*(s)->data))
    225 
    226 #define da_append_count(a, s, items, item_count) do { \
    227 	da_reserve((a), (s), (item_count));                                             \
    228 	memory_copy((s)->data + (s)->count, (items), sizeof(*(items)) * (uz)(item_count)); \
    229 	(s)->count += (item_count);                                                     \
    230 } while (0)
    231 
    232 #define da_push(a, s) \
    233   ((typeof((s)->data))memory_clear((s)->count == (s)->capacity  \
    234     ? da_reserve(a, s, 1),      \
    235       (s)->data + (s)->count++  \
    236     : (s)->data + (s)->count++, 0, sizeof(*(s)->data)))
    237 
    238 function void *
    239 da_reserve_(Arena *a, void *data, da_count *capacity, da_count needed, u64 align, i64 size)
    240 {
    241 	da_count cap = *capacity;
    242 
    243 	/* NOTE(rnp): handle both 0 initialized DAs and DAs that need to be moved (they started
    244 	 * on the stack or someone allocated something in the middle of the arena during usage) */
    245 	if (!data || a->beg != (u8 *)data + cap * size) {
    246 		void *copy = arena_alloc(a, .size = size, .align = align, .count = cap);
    247 		if (data) memory_copy(copy, data, (uz)(cap * size));
    248 		data = copy;
    249 	}
    250 
    251 	if (!cap) cap = DA_INITIAL_CAP;
    252 	while (cap < needed) cap *= 2;
    253 	arena_alloc(a, .size = size, .align = align, .count = cap - *capacity);
    254 	*capacity = cap;
    255 	return data;
    256 }
    257 
    258 function u32
    259 utf8_encode(u8 *out, u32 cp)
    260 {
    261 	u32 result = 1;
    262 	if (cp <= 0x7F) {
    263 		out[0] = cp & 0x7F;
    264 	} else if (cp <= 0x7FF) {
    265 		result = 2;
    266 		out[0] = ((cp >>  6) & 0x1F) | 0xC0;
    267 		out[1] = ((cp >>  0) & 0x3F) | 0x80;
    268 	} else if (cp <= 0xFFFF) {
    269 		result = 3;
    270 		out[0] = ((cp >> 12) & 0x0F) | 0xE0;
    271 		out[1] = ((cp >>  6) & 0x3F) | 0x80;
    272 		out[2] = ((cp >>  0) & 0x3F) | 0x80;
    273 	} else if (cp <= 0x10FFFF) {
    274 		result = 4;
    275 		out[0] = ((cp >> 18) & 0x07) | 0xF0;
    276 		out[1] = ((cp >> 12) & 0x3F) | 0x80;
    277 		out[2] = ((cp >>  6) & 0x3F) | 0x80;
    278 		out[3] = ((cp >>  0) & 0x3F) | 0x80;
    279 	} else {
    280 		out[0] = '?';
    281 	}
    282 	return result;
    283 }
    284 
    285 function UnicodeDecode
    286 utf16_decode(u16 *data, iz length)
    287 {
    288 	UnicodeDecode result = {.cp = U32_MAX};
    289 	if (length) {
    290 		result.consumed = 1;
    291 		result.cp = data[0];
    292 		if (length > 1 && Between(data[0], 0xD800u, 0xDBFFu)
    293 		               && Between(data[1], 0xDC00u, 0xDFFFu))
    294 		{
    295 			result.consumed = 2;
    296 			result.cp = ((data[0] - 0xD800u) << 10u) | ((data[1] - 0xDC00u) + 0x10000u);
    297 		}
    298 	}
    299 	return result;
    300 }
    301 
    302 function u32
    303 utf16_encode(u16 *out, u32 cp)
    304 {
    305 	u32 result = 1;
    306 	if (cp == U32_MAX) {
    307 		out[0] = '?';
    308 	} else if (cp < 0x10000u) {
    309 		out[0] = (u16)cp;
    310 	} else {
    311 		u32 value = cp - 0x10000u;
    312 		out[0] = (u16)(0xD800u + (value >> 10u));
    313 		out[1] = (u16)(0xDC00u + (value & 0x3FFu));
    314 		result = 2;
    315 	}
    316 	return result;
    317 }
    318 
    319 function Stream
    320 stream_from_buffer(u8 *buffer, u32 capacity)
    321 {
    322 	Stream result = {.data = buffer, .cap = (i32)capacity};
    323 	return result;
    324 }
    325 
    326 function Stream
    327 stream_alloc(Arena *a, i32 cap)
    328 {
    329 	Stream result = stream_from_buffer(arena_commit(a, cap), (u32)cap);
    330 	return result;
    331 }
    332 
    333 #define stream_to_s8(s) s8_from_str8(stream_to_str8(s))
    334 function str8
    335 stream_to_str8(Stream *s)
    336 {
    337 	str8 result = str8("");
    338 	if (!s->errors) result = (str8){.length = s->widx, .data = s->data};
    339 	return result;
    340 }
    341 
    342 function void
    343 stream_reset(Stream *s, i32 index)
    344 {
    345 	s->errors = s->cap <= index;
    346 	if (!s->errors)
    347 		s->widx = index;
    348 }
    349 
    350 function void
    351 stream_commit(Stream *s, i32 count)
    352 {
    353 	s->errors |= !Between(s->widx + count, 0, s->cap);
    354 	if (!s->errors)
    355 		s->widx += count;
    356 }
    357 
    358 function void
    359 stream_append(Stream *s, void *data, iz count)
    360 {
    361 	s->errors |= (s->cap - s->widx) < count;
    362 	if (!s->errors) {
    363 		memory_copy(s->data + s->widx, data, (uz)count);
    364 		s->widx += (i32)count;
    365 	}
    366 }
    367 
    368 function void
    369 stream_append_codepoint(Stream *s, u32 codepoint)
    370 {
    371 	u8 buffer[4];
    372 	stream_append(s, buffer, utf8_encode(buffer, codepoint));
    373 }
    374 
    375 // TODO(rnp): replace with handwritten version
    376 #include <stdarg.h>
    377 #include <stdio.h>
    378 function void
    379 stream_appendfv(Stream *s, const char *format, va_list args)
    380 {
    381 	i32 written = vsnprintf((char *)s->data + s->widx, s->cap - s->widx, format, args);
    382 	s->errors |= written > (s->cap - s->widx);
    383 	if (!s->errors) s->widx += written;
    384 }
    385 
    386 function print_format(2, 3) void
    387 stream_appendf(Stream *s, const char *format, ...)
    388 {
    389 	va_list args;
    390 	va_start(args, format);
    391 	stream_appendfv(s, format, args);
    392 	va_end(args);
    393 }
    394 
    395 function void
    396 stream_append_byte(Stream *s, u8 b)
    397 {
    398 	stream_append(s, &b, 1);
    399 }
    400 
    401 function void
    402 stream_pad(Stream *s, u8 b, i32 n)
    403 {
    404 	while (n > 0) stream_append_byte(s, b), n--;
    405 }
    406 
    407 function void
    408 stream_append_s8(Stream *s, s8 str)
    409 {
    410 	stream_append(s, str.data, str.len);
    411 }
    412 
    413 #define stream_append_s8s(s, ...) stream_append_s8s_(s, arg_list(s8, ##__VA_ARGS__))
    414 function void
    415 stream_append_s8s_(Stream *s, s8 *strs, iz count)
    416 {
    417 	for (iz i = 0; i < count; i++)
    418 		stream_append(s, strs[i].data, strs[i].len);
    419 }
    420 
    421 function void
    422 stream_append_u64_width(Stream *s, u64 n, u64 min_width)
    423 {
    424 	u8 tmp[64];
    425 	u8 *end = tmp + sizeof(tmp);
    426 	u8 *beg = end;
    427 	min_width = Min(sizeof(tmp), min_width);
    428 
    429 	do { *--beg = (u8)('0' + (n % 10)); } while (n /= 10);
    430 	while (end - beg > 0 && (uz)(end - beg) < min_width)
    431 		*--beg = '0';
    432 
    433 	stream_append(s, beg, end - beg);
    434 }
    435 
    436 function void
    437 stream_append_u64(Stream *s, u64 n)
    438 {
    439 	stream_append_u64_width(s, n, 0);
    440 }
    441 
    442 function void
    443 stream_append_hex_u64_width(Stream *s, u64 n, iz width)
    444 {
    445 	assert(width <= 16);
    446 	if (!s->errors) {
    447 		u8  buf[16];
    448 		u8 *end = buf + sizeof(buf);
    449 		u8 *beg = end;
    450 		while (n) {
    451 			*--beg = (u8)"0123456789abcdef"[n & 0x0F];
    452 			n >>= 4;
    453 		}
    454 		while (end - beg < width)
    455 			*--beg = '0';
    456 		stream_append(s, beg, end - beg);
    457 	}
    458 }
    459 
    460 function void
    461 stream_append_hex_u64(Stream *s, u64 n)
    462 {
    463 	stream_append_hex_u64_width(s, n, 2);
    464 }
    465 
    466 function void
    467 stream_append_i64(Stream *s, i64 n)
    468 {
    469 	if (n < 0) {
    470 		stream_append_byte(s, '-');
    471 		n *= -1;
    472 	}
    473 	stream_append_u64(s, (u64)n);
    474 }
    475 
    476 function void
    477 stream_append_f64(Stream *s, f64 f, u64 prec)
    478 {
    479 	if (f < 0) {
    480 		stream_append_byte(s, '-');
    481 		f *= -1;
    482 	}
    483 
    484 	/* NOTE: round last digit */
    485 	f += 0.5f / (f64)prec;
    486 
    487 	if (f >= (f64)(-1UL >> 1)) {
    488 		stream_append_s8(s, s8("inf"));
    489 	} else {
    490 		u64 integral = (u64)f;
    491 		u64 fraction = (u64)((f - (f64)integral) * (f64)prec);
    492 		stream_append_u64(s, integral);
    493 		stream_append_byte(s, '.');
    494 		for (u64 i = prec / 10; i > 1; i /= 10) {
    495 			if (i > fraction)
    496 				stream_append_byte(s, '0');
    497 		}
    498 		stream_append_u64(s, fraction);
    499 	}
    500 }
    501 
    502 function void
    503 stream_append_f64_e(Stream *s, f64 f)
    504 {
    505 	/* TODO: there should be a better way of doing this */
    506 	#if 0
    507 	/* NOTE: we ignore subnormal numbers for now */
    508 	union { f64 f; u64 u; } u = {.f = f};
    509 	i32 exponent = ((u.u >> 52) & 0x7ff) - 1023;
    510 	f32 log_10_of_2 = 0.301f;
    511 	i32 scale       = (exponent * log_10_of_2);
    512 	/* NOTE: normalize f */
    513 	for (i32 i = ABS(scale); i > 0; i--)
    514 		f *= (scale > 0)? 0.1f : 10.0f;
    515 	#else
    516 	i32 scale = 0;
    517 	if (f != 0) {
    518 		while (f > 1) {
    519 			f *= 0.1f;
    520 			scale++;
    521 		}
    522 		while (f < 1) {
    523 			f *= 10.0f;
    524 			scale--;
    525 		}
    526 	}
    527 	#endif
    528 
    529 	u32 prec = 100;
    530 	stream_append_f64(s, f, prec);
    531 	stream_append_byte(s, 'e');
    532 	stream_append_byte(s, scale >= 0? '+' : '-');
    533 	for (u32 i = prec / 10; i > 1; i /= 10)
    534 		stream_append_byte(s, '0');
    535 	stream_append_u64(s, (u64)Abs(scale));
    536 }
    537 
    538 function Stream
    539 arena_stream(Arena a)
    540 {
    541 	Stream result = {0};
    542 	result.data   = a.beg;
    543 	result.cap    = (i32)(a.end - a.beg);
    544 
    545 	/* TODO(rnp): no idea what to do here if we want to maintain the ergonomics */
    546 	asan_unpoison_region(result.data, result.cap);
    547 
    548 	return result;
    549 }
    550 
    551 function s8
    552 arena_stream_commit(Arena *a, Stream *s)
    553 {
    554 	assert(s->data == a->beg);
    555 	s8 result = stream_to_s8(s);
    556 	arena_commit(a, result.len);
    557 	return result;
    558 }
    559 
    560 function s8
    561 arena_stream_commit_zero(Arena *a, Stream *s)
    562 {
    563 	b32 error = s->errors || s->widx == s->cap;
    564 	if (!error)
    565 		s->data[s->widx] = 0;
    566 	s8 result = stream_to_s8(s);
    567 	arena_commit(a, result.len + 1);
    568 	return result;
    569 }
    570 
    571 function s8
    572 arena_stream_commit_and_reset(Arena *arena, Stream *s)
    573 {
    574 	s8 result = arena_stream_commit_zero(arena, s);
    575 	*s = arena_stream(*arena);
    576 	return result;
    577 }
    578 
    579 #if !defined(XXH_IMPLEMENTATION)
    580 # define XXH_INLINE_ALL
    581 # define XXH_IMPLEMENTATION
    582 # define XXH_STATIC_LINKING_ONLY
    583 # include "external/xxhash.h"
    584 #endif
    585 
    586 function u128
    587 u128_hash_from_data(void *data, uz size)
    588 {
    589 	u128 result = {0};
    590 	XXH128_hash_t hash = XXH3_128bits_withSeed(data, size, 4969);
    591 	memory_copy(&result, &hash, sizeof(result));
    592 	return result;
    593 }
    594 
    595 function u64
    596 u64_hash_from_str8_seed(str8 string, u64 seed)
    597 {
    598 	u64 result = XXH3_64bits_withSeed(string.data, (uz)string.length, seed);
    599 	return result;
    600 }
    601 
    602 function u64
    603 u64_hash_from_str8(str8 v)
    604 {
    605 	u64 result = u64_hash_from_str8_seed(v, 4969);
    606 	return result;
    607 }
    608 
    609 function str8
    610 str8_from_c_str(char *cstr)
    611 {
    612 	str8 result = {.data = (u8 *)cstr};
    613 	if (cstr) while (*cstr) cstr++;
    614 	result.length = (u8 *)cstr - result.data;
    615 	return result;
    616 }
    617 
    618 function s8
    619 c_str_to_s8(char *cstr)
    620 {
    621 	str8 s = str8_from_c_str(cstr);
    622 	s8 result = s8_from_str8(s);
    623 	return result;
    624 }
    625 
    626 function str8
    627 str8_range(u8 *start, u8 *one_past_last)
    628 {
    629 	str8 result;
    630 	result.data   = start;
    631 	result.length = one_past_last - start;
    632 	return result;
    633 }
    634 
    635 function str8
    636 str8_skip(str8 s, i64 count)
    637 {
    638 	str8 result = s;
    639 	if (count > 0) {
    640 		result.data   += count;
    641 		result.length -= count;
    642 	}
    643 	return result;
    644 }
    645 
    646 function b32
    647 str8_equal(str8 a, str8 b)
    648 {
    649 	b32 result = a.length == b.length;
    650 	for (i64 i = 0; result && i < a.length; i++)
    651 		result = a.data[i] == b.data[i];
    652 	return result;
    653 }
    654 
    655 function b32
    656 s8_equal(s8 a, s8 b)
    657 {
    658 	return str8_equal(str8_from_s8(a), str8_from_s8(b));
    659 }
    660 
    661 /* NOTE(rnp): returns < 0 if byte is not found */
    662 function i64
    663 str8_scan_backwards(str8 s, u8 byte)
    664 {
    665 	i64 result = (u8 *)memory_scan_backwards(s.data, byte, s.length) - s.data;
    666 	return result;
    667 }
    668 
    669 function str8
    670 str8_cut_head(str8 s, i64 cut)
    671 {
    672 	str8 result = s;
    673 	if (cut > 0) {
    674 		result.data   += cut;
    675 		result.length -= cut;
    676 	}
    677 	result.length = Max(0, result.length);
    678 	return result;
    679 }
    680 
    681 function b32
    682 str8_match(str8 a, str8 b, StringMatchFlags flags)
    683 {
    684 	b32 result = 0;
    685 	if (flags == 0) {
    686 		result = str8_equal(a, b);
    687 	} else if (a.length == b.length || (flags & StringMatchFlag_SloppySize)) {
    688 		result = 1;
    689 		i64 length = Min(a.length, b.length);
    690 		for (i64 it = 0; it < length && result; it++) {
    691 			u8 ab = a.data[it], bb = b.data[it];
    692 			if (flags & StringMatchFlag_CaseInsensitive) {
    693 				ab |= 0x20;
    694 				bb |= 0x20;
    695 			}
    696 			result &= ab == bb;
    697 		}
    698 	}
    699 	return result;
    700 }
    701 
    702 function i64
    703 str8_find_needle(str8 string, str8 needle, StringMatchFlags flags)
    704 {
    705 	u8 *s  = string.data;
    706 	u8 *se = string.data + Max(string.length + 1, needle.length) - needle.length;
    707 	if (needle.length > 0) {
    708 		flags |= StringMatchFlag_SloppySize;
    709 
    710 		u8 nb = needle.data[0];
    711 		if (flags & StringMatchFlag_CaseInsensitive)
    712 			nb |= 0x20;
    713 
    714 		str8 needle_tail = str8_skip(needle, 1);
    715 		u8 *s_opl = string.data + string.length;
    716 		for (; s < se; s++) {
    717 			u8 sb = *s;
    718 			if (flags & StringMatchFlag_CaseInsensitive)
    719 				sb |= 0x20;
    720 
    721 			if (sb == nb && str8_match(str8_range(s + 1, s_opl), needle_tail, flags))
    722 				break;
    723 		}
    724 	}
    725 
    726 	i64 result = string.length;
    727 	if (s < se)
    728 		result = s - string.data;
    729 	return result;
    730 }
    731 
    732 
    733 function str8
    734 str8_alloc(Arena *a, i64 length)
    735 {
    736 	str8 result = {.data = push_array(a, u8, length), .length = length};
    737 	return result;
    738 }
    739 
    740 function str8
    741 str8_from_str16(Arena *a, str16 in)
    742 {
    743 	str8 result = str8("");
    744 	if (in.length) {
    745 		i64 commit = in.length * 4;
    746 		i64 length = 0;
    747 		u8 *data = arena_commit(a, commit + 1);
    748 		u16 *beg = in.data;
    749 		u16 *end = in.data + in.length;
    750 		while (beg < end) {
    751 			UnicodeDecode decode = utf16_decode(beg, end - beg);
    752 			length += utf8_encode(data + length, decode.cp);
    753 			beg    += decode.consumed;
    754 		}
    755 		data[length] = 0;
    756 		result = (str8){.length = length, .data = data};
    757 		arena_pop(a, commit - length);
    758 	}
    759 	return result;
    760 }
    761 
    762 function str16
    763 str16_from_str8(Arena *a, str8 in)
    764 {
    765 	str16 result = {0};
    766 	if (in.length) {
    767 		i64  length   = 0;
    768 		i64  required = 2 * in.length + 1;
    769 		u16 *data     = push_array(a, u16, required);
    770 		/* TODO(rnp): utf8_decode */
    771 		for (i64 i = 0; i < in.length; i++) {
    772 			u32 cp  = in.data[i];
    773 			length += utf16_encode(data + length, cp);
    774 		}
    775 		result = (str16){.length = length, .data = data};
    776 		arena_pop(a, required - length);
    777 	}
    778 	return result;
    779 }
    780 
    781 #define push_str8_from_parts(a, j, ...) push_str8_from_parts_((a), (j), arg_list(str8, __VA_ARGS__))
    782 function str8
    783 push_str8_from_parts_(Arena *arena, str8 joiner, str8 *parts, i64 count)
    784 {
    785 	i64 length = joiner.length * (count - 1);
    786 	for (i64 i = 0; i < count; i++)
    787 		length += parts[i].length;
    788 
    789 	str8 result = {.length = length, .data = arena_commit(arena, length + 1)};
    790 
    791 	i64 offset = 0;
    792 	for (i64 i = 0; i < count; i++) {
    793 		if (i != 0) {
    794 			memory_copy(result.data + offset, joiner.data, (uz)joiner.length);
    795 			offset += joiner.length;
    796 		}
    797 		memory_copy(result.data + offset, parts[i].data, (uz)parts[i].length);
    798 		offset += parts[i].length;
    799 	}
    800 	result.data[result.length] = 0;
    801 
    802 	return result;
    803 }
    804 
    805 #define push_s8_from_parts(a, j, ...) push_s8_from_parts_((a), (j), arg_list(s8, __VA_ARGS__))
    806 function s8
    807 push_s8_from_parts_(Arena *arena, s8 joiner, s8 *parts, iz count)
    808 {
    809 	iz length = joiner.len * (count - 1);
    810 	for (iz i = 0; i < count; i++)
    811 		length += parts[i].len;
    812 
    813 	s8 result = {.len = length, .data = arena_commit(arena, length + 1)};
    814 
    815 	iz offset = 0;
    816 	for (iz i = 0; i < count; i++) {
    817 		if (i != 0) {
    818 			memory_copy(result.data + offset, joiner.data, (uz)joiner.len);
    819 			offset += joiner.len;
    820 		}
    821 		memory_copy(result.data + offset, parts[i].data, (uz)parts[i].len);
    822 		offset += parts[i].len;
    823 	}
    824 	result.data[result.len] = 0;
    825 
    826 	return result;
    827 }
    828 
    829 function str8
    830 push_str8(Arena *a, str8 str)
    831 {
    832 	str8 result    = str8_alloc(a, str.length + 1);
    833 	result.length -= 1;
    834 	memory_copy(result.data, str.data, (uz)result.length);
    835 	return result;
    836 }
    837 
    838 function s8
    839 push_s8(Arena *a, s8 str)
    840 {
    841 	str8 copy   = push_str8(a, str8_from_s8(str));
    842 	s8   result = s8_from_str8(copy);
    843 	return result;
    844 }
    845 
    846 // TODO(rnp): replace with handwritten version
    847 function str8
    848 push_str8_fv(Arena *arena, const char *format, va_list args)
    849 {
    850 	Stream sb = arena_stream(*arena);
    851 	stream_appendfv(&sb, format, args);
    852 	s8 s = arena_stream_commit(arena, &sb);
    853 	str8 result = {.length = s.len, .data = s.data};
    854 	return result;
    855 }
    856 
    857 /* NOTE(rnp): from Hacker's Delight */
    858 function force_inline u64
    859 round_down_power_of_two(u64 a)
    860 {
    861 	u64 result = 0x8000000000000000ULL >> clz_u64(a);
    862 	return result;
    863 }
    864 
    865 function force_inline u64
    866 round_up_power_of_two(u64 a)
    867 {
    868 	u64 result = 0x8000000000000000ULL >> (clz_u64(a - 1) - 1);
    869 	return result;
    870 }
    871 
    872 function force_inline iz
    873 round_up_to(iz value, iz multiple)
    874 {
    875 	iz result = value;
    876 	if (value % multiple != 0)
    877 		result += multiple - value % multiple;
    878 	return result;
    879 }
    880 
    881 function NumberConversion
    882 integer_from_str8(str8 raw)
    883 {
    884 	read_only local_persist alignas(64) i8 lut[64] = {
    885 		 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, -1, -1, -1, -1, -1, -1,
    886 		-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    887 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    888 		-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    889 	};
    890 
    891 	NumberConversion result = {.unparsed = raw};
    892 
    893 	i64 i     = 0;
    894 	i64 scale = 1;
    895 	if (raw.length > 0 && raw.data[0] == '-') {
    896 		scale = -1;
    897 		i     =  1;
    898 	}
    899 
    900 	b32 hex = 0;
    901 	if (raw.length - i > 2 && raw.data[i] == '0' && (raw.data[1] == 'x' || raw.data[1] == 'X')) {
    902 		hex = 1;
    903 		i += 2;
    904 	}
    905 
    906 	#define integer_conversion_body(radix, clamp) do {\
    907 		for (; i < raw.length; i++) {\
    908 			i64 value = lut[Min((u8)(raw.data[i] - (u8)'0'), clamp)];\
    909 			if (value >= 0) {\
    910 				if (result.U64 > (U64_MAX - (u64)value) / radix) {\
    911 					result.result = NumberConversionResult_OutOfRange;\
    912 					result.U64    = U64_MAX;\
    913 					return result;\
    914 				} else {\
    915 					result.U64 = radix * result.U64 + (u64)value;\
    916 				}\
    917 			} else {\
    918 				break;\
    919 			}\
    920 		}\
    921 	} while (0)
    922 
    923 	if (hex) integer_conversion_body(16u, 63u);
    924 	else     integer_conversion_body(10u, 15u);
    925 
    926 	#undef integer_conversion_body
    927 
    928 	result.unparsed = (str8){.length = raw.length - i, .data = raw.data + i};
    929 	result.result   = i > 0 ? NumberConversionResult_Success : NumberConversionResult_Invalid;
    930 	result.kind     = NumberConversionKind_Integer;
    931 	if (scale < 0) result.U64 = 0 - result.U64;
    932 
    933 	return result;
    934 }
    935 
    936 function NumberConversion
    937 number_from_str8(str8 s)
    938 {
    939 	NumberConversion result  = {.unparsed = s};
    940 	NumberConversion integer = integer_from_str8(s);
    941 	if (integer.result == NumberConversionResult_Success) {
    942 		if (integer.unparsed.length != 0 && integer.unparsed.data[0] == '.') {
    943 			s = integer.unparsed;
    944 			s.data++;
    945 			s.length--;
    946 
    947 			while (s.length > 0 && s.data[s.length - 1] == '0') s.length--;
    948 
    949 			NumberConversion fractional = integer_from_str8(s);
    950 			if (fractional.result == NumberConversionResult_Success || s.length == 0) {
    951 				result.F64 = (f64)fractional.U64;
    952 
    953 				u64 divisor = (u64)(fractional.unparsed.data - s.data);
    954 				while (divisor > 0) { result.F64 /= 10.0; divisor--; }
    955 
    956 				result.F64 += (f64)integer.S64;
    957 
    958 				result.result   = NumberConversionResult_Success;
    959 				result.kind     = NumberConversionKind_Float;
    960 				result.unparsed = fractional.unparsed;
    961 			}
    962 		} else {
    963 			result = integer;
    964 		}
    965 	}
    966 	return result;
    967 }