ogl_beamforming

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

util.c (21172B)


      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, i64 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, u64 alignment)
    110 {
    111 	u64 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, u64 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, i64 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, i64 length)
    142 {
    143 	a->beg -= length;
    144 }
    145 
    146 typedef enum {
    147 	ArenaAllocateFlags_NoZero = 1 << 0,
    148 } ArenaAllocateFlags;
    149 
    150 typedef struct {
    151 	i64 size;
    152 	u64 align;
    153 	i64 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 		i64 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, i64 size, u64 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, i64 len, u64 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)) * (u64)(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, (u64)(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, i64 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 function str8
    334 stream_to_str8(Stream *s)
    335 {
    336 	str8 result = str8("");
    337 	if (!s->errors) result = (str8){.length = s->widx, .data = s->data};
    338 	return result;
    339 }
    340 
    341 function void
    342 stream_reset(Stream *s, i32 index)
    343 {
    344 	s->errors = s->cap <= index;
    345 	if (!s->errors)
    346 		s->widx = index;
    347 }
    348 
    349 function void
    350 stream_commit(Stream *s, i32 count)
    351 {
    352 	s->errors |= !Between(s->widx + count, 0, s->cap);
    353 	if (!s->errors)
    354 		s->widx += count;
    355 }
    356 
    357 function void
    358 stream_append(Stream *s, void *data, i64 count)
    359 {
    360 	s->errors |= (s->cap - s->widx) < count;
    361 	if (!s->errors) {
    362 		memory_copy(s->data + s->widx, data, (u64)count);
    363 		s->widx += (i32)count;
    364 	}
    365 }
    366 
    367 function void
    368 stream_append_codepoint(Stream *s, u32 codepoint)
    369 {
    370 	u8 buffer[4];
    371 	stream_append(s, buffer, utf8_encode(buffer, codepoint));
    372 }
    373 
    374 // TODO(rnp): replace with handwritten version
    375 #include <stdarg.h>
    376 #include <stdio.h>
    377 function void
    378 stream_appendfv(Stream *s, const char *format, va_list args)
    379 {
    380 	i32 written = vsnprintf((char *)s->data + s->widx, s->cap - s->widx, format, args);
    381 	s->errors |= written > (s->cap - s->widx);
    382 	if (!s->errors) s->widx += written;
    383 }
    384 
    385 function print_format(2, 3) void
    386 stream_appendf(Stream *s, const char *format, ...)
    387 {
    388 	va_list args;
    389 	va_start(args, format);
    390 	stream_appendfv(s, format, args);
    391 	va_end(args);
    392 }
    393 
    394 function void
    395 stream_append_byte(Stream *s, u8 b)
    396 {
    397 	stream_append(s, &b, 1);
    398 }
    399 
    400 function void
    401 stream_pad(Stream *s, u8 b, i32 n)
    402 {
    403 	while (n > 0) stream_append_byte(s, b), n--;
    404 }
    405 
    406 function void
    407 stream_append_str8(Stream *s, str8 str)
    408 {
    409 	stream_append(s, str.data, str.length);
    410 }
    411 
    412 #define stream_append_str8s(s, ...) stream_append_str8s_(s, arg_list(str8, ##__VA_ARGS__))
    413 function void
    414 stream_append_str8s_(Stream *s, str8 *strs, i64 count)
    415 {
    416 	for (i64 i = 0; i < count; i++)
    417 		stream_append(s, strs[i].data, strs[i].length);
    418 }
    419 
    420 function void
    421 stream_append_u64_width(Stream *s, u64 n, u64 min_width)
    422 {
    423 	u8 tmp[64];
    424 	u8 *end = tmp + sizeof(tmp);
    425 	u8 *beg = end;
    426 	min_width = Min(sizeof(tmp), min_width);
    427 
    428 	do { *--beg = (u8)('0' + (n % 10)); } while (n /= 10);
    429 	while (end - beg > 0 && (u64)(end - beg) < min_width)
    430 		*--beg = '0';
    431 
    432 	stream_append(s, beg, end - beg);
    433 }
    434 
    435 function void
    436 stream_append_u64(Stream *s, u64 n)
    437 {
    438 	stream_append_u64_width(s, n, 0);
    439 }
    440 
    441 function void
    442 stream_append_hex_u64_width(Stream *s, u64 n, i64 width)
    443 {
    444 	assert(width <= 16);
    445 	if (!s->errors) {
    446 		u8  buf[16];
    447 		u8 *end = buf + sizeof(buf);
    448 		u8 *beg = end;
    449 		while (n) {
    450 			*--beg = (u8)"0123456789abcdef"[n & 0x0F];
    451 			n >>= 4;
    452 		}
    453 		while (end - beg < width)
    454 			*--beg = '0';
    455 		stream_append(s, beg, end - beg);
    456 	}
    457 }
    458 
    459 function void
    460 stream_append_hex_u64(Stream *s, u64 n)
    461 {
    462 	stream_append_hex_u64_width(s, n, 2);
    463 }
    464 
    465 function void
    466 stream_append_i64(Stream *s, i64 n)
    467 {
    468 	if (n < 0) {
    469 		stream_append_byte(s, '-');
    470 		n *= -1;
    471 	}
    472 	stream_append_u64(s, (u64)n);
    473 }
    474 
    475 function void
    476 stream_append_f64(Stream *s, f64 f, u64 prec)
    477 {
    478 	if (f < 0) {
    479 		stream_append_byte(s, '-');
    480 		f *= -1;
    481 	}
    482 
    483 	/* NOTE: round last digit */
    484 	f += 0.5f / (f64)prec;
    485 
    486 	if (f >= (f64)(-1UL >> 1)) {
    487 		stream_append_str8(s, str8("inf"));
    488 	} else {
    489 		u64 integral = (u64)f;
    490 		u64 fraction = (u64)((f - (f64)integral) * (f64)prec);
    491 		stream_append_u64(s, integral);
    492 		stream_append_byte(s, '.');
    493 		for (u64 i = prec / 10; i > 1; i /= 10) {
    494 			if (i > fraction)
    495 				stream_append_byte(s, '0');
    496 		}
    497 		stream_append_u64(s, fraction);
    498 	}
    499 }
    500 
    501 function void
    502 stream_append_f64_e(Stream *s, f64 f)
    503 {
    504 	/* TODO: there should be a better way of doing this */
    505 	#if 0
    506 	/* NOTE: we ignore subnormal numbers for now */
    507 	union { f64 f; u64 u; } u = {.f = f};
    508 	i32 exponent = ((u.u >> 52) & 0x7ff) - 1023;
    509 	f32 log_10_of_2 = 0.301f;
    510 	i32 scale       = (exponent * log_10_of_2);
    511 	/* NOTE: normalize f */
    512 	for (i32 i = ABS(scale); i > 0; i--)
    513 		f *= (scale > 0)? 0.1f : 10.0f;
    514 	#else
    515 	i32 scale = 0;
    516 	if (f != 0) {
    517 		while (f > 1) {
    518 			f *= 0.1f;
    519 			scale++;
    520 		}
    521 		while (f < 1) {
    522 			f *= 10.0f;
    523 			scale--;
    524 		}
    525 	}
    526 	#endif
    527 
    528 	u32 prec = 100;
    529 	stream_append_f64(s, f, prec);
    530 	stream_append_byte(s, 'e');
    531 	stream_append_byte(s, scale >= 0? '+' : '-');
    532 	for (u32 i = prec / 10; i > 1; i /= 10)
    533 		stream_append_byte(s, '0');
    534 	stream_append_u64(s, (u64)Abs(scale));
    535 }
    536 
    537 function Stream
    538 arena_stream(Arena a)
    539 {
    540 	Stream result = {0};
    541 	result.data   = a.beg;
    542 	result.cap    = (i32)(a.end - a.beg);
    543 
    544 	/* TODO(rnp): no idea what to do here if we want to maintain the ergonomics */
    545 	asan_unpoison_region(result.data, result.cap);
    546 
    547 	return result;
    548 }
    549 
    550 function str8
    551 arena_stream_commit(Arena *a, Stream *s)
    552 {
    553 	assert(s->data == a->beg);
    554 	str8 result = stream_to_str8(s);
    555 	arena_commit(a, result.length);
    556 	return result;
    557 }
    558 
    559 function str8
    560 arena_stream_commit_zero(Arena *a, Stream *s)
    561 {
    562 	b32 error = s->errors || s->widx == s->cap;
    563 	if (!error)
    564 		s->data[s->widx] = 0;
    565 	str8 result = stream_to_str8(s);
    566 	arena_commit(a, result.length + 1);
    567 	return result;
    568 }
    569 
    570 function str8
    571 arena_stream_commit_and_reset(Arena *arena, Stream *s)
    572 {
    573 	str8 result = arena_stream_commit_zero(arena, s);
    574 	*s = arena_stream(*arena);
    575 	return result;
    576 }
    577 
    578 #if !defined(XXH_IMPLEMENTATION)
    579 # define XXH_INLINE_ALL
    580 # define XXH_IMPLEMENTATION
    581 # define XXH_STATIC_LINKING_ONLY
    582 # include "external/xxhash.h"
    583 #endif
    584 
    585 function u128
    586 u128_hash_from_data(void *data, u64 size)
    587 {
    588 	u128 result = {0};
    589 	XXH128_hash_t hash = XXH3_128bits_withSeed(data, size, 4969);
    590 	memory_copy(&result, &hash, sizeof(result));
    591 	return result;
    592 }
    593 
    594 function u64
    595 u64_hash_from_str8_seed(str8 string, u64 seed)
    596 {
    597 	u64 result = XXH3_64bits_withSeed(string.data, (u64)string.length, seed);
    598 	return result;
    599 }
    600 
    601 function u64
    602 u64_hash_from_str8(str8 v)
    603 {
    604 	u64 result = u64_hash_from_str8_seed(v, 4969);
    605 	return result;
    606 }
    607 
    608 function str8
    609 str8_from_c_str(char *cstr)
    610 {
    611 	str8 result = {.data = (u8 *)cstr};
    612 	if (cstr) while (*cstr) cstr++;
    613 	result.length = (u8 *)cstr - result.data;
    614 	return result;
    615 }
    616 
    617 function str8
    618 str8_range(u8 *start, u8 *one_past_last)
    619 {
    620 	str8 result;
    621 	result.data   = start;
    622 	result.length = one_past_last - start;
    623 	return result;
    624 }
    625 
    626 function str8
    627 str8_skip(str8 s, i64 count)
    628 {
    629 	str8 result = s;
    630 	if (count > 0) {
    631 		result.data   += count;
    632 		result.length -= count;
    633 	}
    634 	return result;
    635 }
    636 
    637 function b32
    638 str8_equal(str8 a, str8 b)
    639 {
    640 	b32 result = a.length == b.length;
    641 	for (i64 i = 0; result && i < a.length; i++)
    642 		result = a.data[i] == b.data[i];
    643 	return result;
    644 }
    645 
    646 /* NOTE(rnp): returns < 0 if byte is not found */
    647 function i64
    648 str8_scan_backwards(str8 s, u8 byte)
    649 {
    650 	i64 result = (u8 *)memory_scan_backwards(s.data, byte, s.length) - s.data;
    651 	return result;
    652 }
    653 
    654 function str8
    655 str8_cut_head(str8 s, i64 cut)
    656 {
    657 	str8 result = s;
    658 	if (cut > 0) {
    659 		result.data   += cut;
    660 		result.length -= cut;
    661 	}
    662 	result.length = Max(0, result.length);
    663 	return result;
    664 }
    665 
    666 function b32
    667 str8_match(str8 a, str8 b, StringMatchFlags flags)
    668 {
    669 	b32 result = 0;
    670 	if (flags == 0) {
    671 		result = str8_equal(a, b);
    672 	} else if (a.length == b.length || (flags & StringMatchFlag_SloppySize)) {
    673 		result = 1;
    674 		i64 length = Min(a.length, b.length);
    675 		for (i64 it = 0; it < length && result; it++) {
    676 			u8 ab = a.data[it], bb = b.data[it];
    677 			if (flags & StringMatchFlag_CaseInsensitive) {
    678 				ab |= 0x20;
    679 				bb |= 0x20;
    680 			}
    681 			result &= ab == bb;
    682 		}
    683 	}
    684 	return result;
    685 }
    686 
    687 function i64
    688 str8_find_needle(str8 string, str8 needle, StringMatchFlags flags)
    689 {
    690 	u8 *s  = string.data;
    691 	u8 *se = string.data + Max(string.length + 1, needle.length) - needle.length;
    692 	if (needle.length > 0) {
    693 		flags |= StringMatchFlag_SloppySize;
    694 
    695 		u8 nb = needle.data[0];
    696 		if (flags & StringMatchFlag_CaseInsensitive)
    697 			nb |= 0x20;
    698 
    699 		str8 needle_tail = str8_skip(needle, 1);
    700 		u8 *s_opl = string.data + string.length;
    701 		for (; s < se; s++) {
    702 			u8 sb = *s;
    703 			if (flags & StringMatchFlag_CaseInsensitive)
    704 				sb |= 0x20;
    705 
    706 			if (sb == nb && str8_match(str8_range(s + 1, s_opl), needle_tail, flags))
    707 				break;
    708 		}
    709 	}
    710 
    711 	i64 result = string.length;
    712 	if (s < se)
    713 		result = s - string.data;
    714 	return result;
    715 }
    716 
    717 
    718 function str8
    719 str8_alloc(Arena *a, i64 length)
    720 {
    721 	str8 result = {.data = push_array(a, u8, length), .length = length};
    722 	return result;
    723 }
    724 
    725 function str8
    726 str8_from_str16(Arena *a, str16 in)
    727 {
    728 	str8 result = str8("");
    729 	if (in.length) {
    730 		i64 commit = in.length * 4;
    731 		i64 length = 0;
    732 		u8 *data = arena_commit(a, commit + 1);
    733 		u16 *beg = in.data;
    734 		u16 *end = in.data + in.length;
    735 		while (beg < end) {
    736 			UnicodeDecode decode = utf16_decode(beg, end - beg);
    737 			length += utf8_encode(data + length, decode.cp);
    738 			beg    += decode.consumed;
    739 		}
    740 		data[length] = 0;
    741 		result = (str8){.length = length, .data = data};
    742 		arena_pop(a, commit - length);
    743 	}
    744 	return result;
    745 }
    746 
    747 function str16
    748 str16_from_str8(Arena *a, str8 in)
    749 {
    750 	str16 result = {0};
    751 	if (in.length) {
    752 		i64  length   = 0;
    753 		i64  required = 2 * in.length + 1;
    754 		u16 *data     = push_array(a, u16, required);
    755 		/* TODO(rnp): utf8_decode */
    756 		for (i64 i = 0; i < in.length; i++) {
    757 			u32 cp  = in.data[i];
    758 			length += utf16_encode(data + length, cp);
    759 		}
    760 		result = (str16){.length = length, .data = data};
    761 		arena_pop(a, required - length);
    762 	}
    763 	return result;
    764 }
    765 
    766 #define push_str8_from_parts(a, j, ...) push_str8_from_parts_((a), (j), arg_list(str8, __VA_ARGS__))
    767 function str8
    768 push_str8_from_parts_(Arena *arena, str8 joiner, str8 *parts, i64 count)
    769 {
    770 	i64 length = joiner.length * (count - 1);
    771 	for (i64 i = 0; i < count; i++)
    772 		length += parts[i].length;
    773 
    774 	str8 result = {.length = length, .data = arena_commit(arena, length + 1)};
    775 
    776 	i64 offset = 0;
    777 	for (i64 i = 0; i < count; i++) {
    778 		if (i != 0) {
    779 			memory_copy(result.data + offset, joiner.data, (u64)joiner.length);
    780 			offset += joiner.length;
    781 		}
    782 		memory_copy(result.data + offset, parts[i].data, (u64)parts[i].length);
    783 		offset += parts[i].length;
    784 	}
    785 	result.data[result.length] = 0;
    786 
    787 	return result;
    788 }
    789 
    790 function str8
    791 push_str8(Arena *a, str8 str)
    792 {
    793 	str8 result    = str8_alloc(a, str.length + 1);
    794 	result.length -= 1;
    795 	memory_copy(result.data, str.data, (u64)result.length);
    796 	return result;
    797 }
    798 
    799 // TODO(rnp): replace with handwritten version
    800 function str8
    801 push_str8_fv(Arena *arena, const char *format, va_list args)
    802 {
    803 	Stream sb = arena_stream(*arena);
    804 	stream_appendfv(&sb, format, args);
    805 	str8 result = arena_stream_commit(arena, &sb);
    806 	return result;
    807 }
    808 
    809 /* NOTE(rnp): from Hacker's Delight */
    810 function force_inline u64
    811 round_down_power_of_two(u64 a)
    812 {
    813 	u64 result = 0x8000000000000000ULL >> clz_u64(a);
    814 	return result;
    815 }
    816 
    817 function force_inline u64
    818 round_up_power_of_two(u64 a)
    819 {
    820 	u64 result = 0x8000000000000000ULL >> (clz_u64(a - 1) - 1);
    821 	return result;
    822 }
    823 
    824 function force_inline i64
    825 round_up_to(i64 value, i64 multiple)
    826 {
    827 	i64 result = value;
    828 	if (value % multiple != 0)
    829 		result += multiple - value % multiple;
    830 	return result;
    831 }
    832 
    833 function NumberConversion
    834 integer_from_str8(str8 raw)
    835 {
    836 	read_only local_persist alignas(64) i8 lut[64] = {
    837 		 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, -1, -1, -1, -1, -1, -1,
    838 		-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    839 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    840 		-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    841 	};
    842 
    843 	NumberConversion result = {.unparsed = raw};
    844 
    845 	i64 i     = 0;
    846 	i64 scale = 1;
    847 	if (raw.length > 0 && raw.data[0] == '-') {
    848 		scale = -1;
    849 		i     =  1;
    850 	}
    851 
    852 	b32 hex = 0;
    853 	if (raw.length - i > 2 && raw.data[i] == '0' && (raw.data[1] == 'x' || raw.data[1] == 'X')) {
    854 		hex = 1;
    855 		i += 2;
    856 	}
    857 
    858 	#define integer_conversion_body(radix, clamp) do {\
    859 		for (; i < raw.length; i++) {\
    860 			i64 value = lut[Min((u8)(raw.data[i] - (u8)'0'), clamp)];\
    861 			if (value >= 0) {\
    862 				if (result.U64 > (U64_MAX - (u64)value) / radix) {\
    863 					result.result = NumberConversionResult_OutOfRange;\
    864 					result.U64    = U64_MAX;\
    865 					return result;\
    866 				} else {\
    867 					result.U64 = radix * result.U64 + (u64)value;\
    868 				}\
    869 			} else {\
    870 				break;\
    871 			}\
    872 		}\
    873 	} while (0)
    874 
    875 	if (hex) integer_conversion_body(16u, 63u);
    876 	else     integer_conversion_body(10u, 15u);
    877 
    878 	#undef integer_conversion_body
    879 
    880 	result.unparsed = (str8){.length = raw.length - i, .data = raw.data + i};
    881 	result.result   = i > 0 ? NumberConversionResult_Success : NumberConversionResult_Invalid;
    882 	result.kind     = NumberConversionKind_Integer;
    883 	if (scale < 0) result.U64 = 0 - result.U64;
    884 
    885 	return result;
    886 }
    887 
    888 function NumberConversion
    889 number_from_str8(str8 s)
    890 {
    891 	NumberConversion result  = {.unparsed = s};
    892 	NumberConversion integer = integer_from_str8(s);
    893 	if (integer.result == NumberConversionResult_Success) {
    894 		if (integer.unparsed.length != 0 && integer.unparsed.data[0] == '.') {
    895 			s = integer.unparsed;
    896 			s.data++;
    897 			s.length--;
    898 
    899 			while (s.length > 0 && s.data[s.length - 1] == '0') s.length--;
    900 
    901 			NumberConversion fractional = integer_from_str8(s);
    902 			if (fractional.result == NumberConversionResult_Success || s.length == 0) {
    903 				result.F64 = (f64)fractional.U64;
    904 
    905 				u64 divisor = (u64)(fractional.unparsed.data - s.data);
    906 				while (divisor > 0) { result.F64 /= 10.0; divisor--; }
    907 
    908 				result.F64 += (f64)integer.S64;
    909 
    910 				result.result   = NumberConversionResult_Success;
    911 				result.kind     = NumberConversionKind_Float;
    912 				result.unparsed = fractional.unparsed;
    913 			}
    914 		} else {
    915 			result = integer;
    916 		}
    917 	}
    918 	return result;
    919 }