ogl_beamforming

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

math.c (20411B)


      1 #include "external/cephes.c"
      2 
      3 function void
      4 fill_kronecker_sub_matrix(f32 *out, i32 out_stride, f32 scale, f32 *b, iv2 b_dim)
      5 {
      6 	f32x4 vscale = dup_f32x4((f32)scale);
      7 	for (i32 i = 0; i < b_dim.y; i++) {
      8 		for (i32 j = 0; j < b_dim.x; j += 4, b += 4) {
      9 			f32x4 vb = load_f32x4(b);
     10 			store_f32x4(out + j, mul_f32x4(vscale, vb));
     11 		}
     12 		out += out_stride;
     13 	}
     14 }
     15 
     16 /* NOTE: this won't check for valid space/etc and assumes row major order */
     17 function void
     18 kronecker_product(f32 *out, f32 *a, iv2 a_dim, f32 *b, iv2 b_dim)
     19 {
     20 	iv2 out_dim = {{a_dim.x * b_dim.x, a_dim.y * b_dim.y}};
     21 	assert(out_dim.y % 4 == 0);
     22 	for (i32 i = 0; i < a_dim.y; i++) {
     23 		f32 *vout = out;
     24 		for (i32 j = 0; j < a_dim.x; j++, a++) {
     25 			fill_kronecker_sub_matrix(vout, out_dim.y, *a, b, b_dim);
     26 			vout += b_dim.y;
     27 		}
     28 		out += out_dim.y * b_dim.x;
     29 	}
     30 }
     31 
     32 /* NOTE/TODO: to support even more hadamard sizes use the Paley construction */
     33 function f32 *
     34 make_hadamard_transpose(Arena *a, i32 dim)
     35 {
     36 	read_only local_persist	f32 hadamard_12_12_transpose[] = {
     37 		1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
     38 		1, -1, -1,  1, -1, -1, -1,  1,  1,  1, -1,  1,
     39 		1,  1, -1, -1,  1, -1, -1, -1,  1,  1,  1, -1,
     40 		1, -1,  1, -1, -1,  1, -1, -1, -1,  1,  1,  1,
     41 		1,  1, -1,  1, -1, -1,  1, -1, -1, -1,  1,  1,
     42 		1,  1,  1, -1,  1, -1, -1,  1, -1, -1, -1,  1,
     43 		1,  1,  1,  1, -1,  1, -1, -1,  1, -1, -1, -1,
     44 		1, -1,  1,  1,  1, -1,  1, -1, -1,  1, -1, -1,
     45 		1, -1, -1,  1,  1,  1, -1,  1, -1, -1,  1, -1,
     46 		1, -1, -1, -1,  1,  1,  1, -1,  1, -1, -1,  1,
     47 		1,  1, -1, -1, -1,  1,  1,  1, -1,  1, -1, -1,
     48 		1, -1,  1, -1, -1, -1,  1,  1,  1, -1,  1, -1,
     49 	};
     50 
     51 	read_only local_persist f32 hadamard_20_20_transpose[] = {
     52 		1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
     53 		1, -1, -1,  1,  1, -1, -1, -1, -1,  1, -1,  1, -1,  1,  1,  1,  1, -1, -1,  1,
     54 		1, -1,  1,  1, -1, -1, -1, -1,  1, -1,  1, -1,  1,  1,  1,  1, -1, -1,  1, -1,
     55 		1,  1,  1, -1, -1, -1, -1,  1, -1,  1, -1,  1,  1,  1,  1, -1, -1,  1, -1, -1,
     56 		1,  1, -1, -1, -1, -1,  1, -1,  1, -1,  1,  1,  1,  1, -1, -1,  1, -1, -1,  1,
     57 		1, -1, -1, -1, -1,  1, -1,  1, -1,  1,  1,  1,  1, -1, -1,  1, -1, -1,  1,  1,
     58 		1, -1, -1, -1,  1, -1,  1, -1,  1,  1,  1,  1, -1, -1,  1, -1, -1,  1,  1, -1,
     59 		1, -1, -1,  1, -1,  1, -1,  1,  1,  1,  1, -1, -1,  1, -1, -1,  1,  1, -1, -1,
     60 		1, -1,  1, -1,  1, -1,  1,  1,  1,  1, -1, -1,  1, -1, -1,  1,  1, -1, -1, -1,
     61 		1,  1, -1,  1, -1,  1,  1,  1,  1, -1, -1,  1, -1, -1,  1,  1, -1, -1, -1, -1,
     62 		1, -1,  1, -1,  1,  1,  1,  1, -1, -1,  1, -1, -1,  1,  1, -1, -1, -1, -1,  1,
     63 		1,  1, -1,  1,  1,  1,  1, -1, -1,  1, -1, -1,  1,  1, -1, -1, -1, -1,  1, -1,
     64 		1, -1,  1,  1,  1,  1, -1, -1,  1, -1, -1,  1,  1, -1, -1, -1, -1,  1, -1,  1,
     65 		1,  1,  1,  1,  1, -1, -1,  1, -1, -1,  1,  1, -1, -1, -1, -1,  1, -1,  1, -1,
     66 		1,  1,  1,  1, -1, -1,  1, -1, -1,  1,  1, -1, -1, -1, -1,  1, -1,  1, -1,  1,
     67 		1,  1,  1, -1, -1,  1, -1, -1,  1,  1, -1, -1, -1, -1,  1, -1,  1, -1,  1,  1,
     68 		1,  1, -1, -1,  1, -1, -1,  1,  1, -1, -1, -1, -1,  1, -1,  1, -1,  1,  1,  1,
     69 		1, -1, -1,  1, -1, -1,  1,  1, -1, -1, -1, -1,  1, -1,  1, -1,  1,  1,  1,  1,
     70 		1, -1,  1, -1, -1,  1,  1, -1, -1, -1, -1,  1, -1,  1, -1,  1,  1,  1,  1, -1,
     71 		1,  1, -1, -1,  1,  1, -1, -1, -1, -1,  1, -1,  1, -1,  1,  1,  1,  1, -1, -1,
     72 	};
     73 
     74 	f32 *result = 0;
     75 
     76 	b32 power_of_2     = ISPOWEROF2(dim);
     77 	b32 multiple_of_12 = dim % 12 == 0;
     78 	b32 multiple_of_20 = dim % 20 == 0;
     79 	iz elements        = dim * dim;
     80 
     81 	i32 base_dim = 0;
     82 	if (power_of_2) {
     83 		base_dim  = dim;
     84 	} else if (multiple_of_20 && ISPOWEROF2(dim / 20)) {
     85 		base_dim  = 20;
     86 		dim      /= 20;
     87 	} else if (multiple_of_12 && ISPOWEROF2(dim / 12)) {
     88 		base_dim  = 12;
     89 		dim      /= 12;
     90 	}
     91 
     92 	if (ISPOWEROF2(dim) && base_dim && arena_capacity(a, f32) >= elements * (1 + (dim != base_dim))) {
     93 		result = push_array(a, f32, elements);
     94 
     95 		Arena tmp = *a;
     96 		f32 *m = dim == base_dim ? result : push_array(&tmp, f32, elements);
     97 
     98 		#define IND(i, j) ((i) * dim + (j))
     99 		m[0] = 1;
    100 		for (i32 k = 1; k < dim; k *= 2) {
    101 			for (i32 i = 0; i < k; i++) {
    102 				for (i32 j = 0; j < k; j++) {
    103 					f32 val = m[IND(i, j)];
    104 					m[IND(i + k, j)]     =  val;
    105 					m[IND(i, j + k)]     =  val;
    106 					m[IND(i + k, j + k)] = -val;
    107 				}
    108 			}
    109 		}
    110 		#undef IND
    111 
    112 		f32 *m2 = 0;
    113 		iv2 m2_dim;
    114 		switch (base_dim) {
    115 		case 12:{ m2 = hadamard_12_12_transpose; m2_dim = (iv2){{12, 12}}; }break;
    116 		case 20:{ m2 = hadamard_20_20_transpose; m2_dim = (iv2){{20, 20}}; }break;
    117 		}
    118 		if (m2) kronecker_product(result, m, (iv2){{dim, dim}}, m2, m2_dim);
    119 	}
    120 
    121 	return result;
    122 }
    123 
    124 function b32
    125 u128_equal(u128 a, u128 b)
    126 {
    127 	b32 result = a.U64[0] == b.U64[0] && a.U64[1] == b.U64[1];
    128 	return result;
    129 }
    130 
    131 function RangeU64
    132 subrange_n_from_n_m_count(u64 n, u64 n_count, u64 m)
    133 {
    134 	assert(n < n_count);
    135 
    136 	u64 per_lane            = m / n_count;
    137 	u64 leftover            = m - per_lane * n_count;
    138 	u64 leftovers_before_n  = MIN(leftover, n);
    139 	u64 base_index          = n * per_lane + leftovers_before_n;
    140 	u64 one_past_last_index = base_index + per_lane + ((n < leftover) ? 1 : 0);
    141 
    142 	RangeU64 result = {base_index, one_past_last_index};
    143 	return result;
    144 }
    145 
    146 function b32
    147 iv2_equal(iv2 a, iv2 b)
    148 {
    149 	b32 result = a.x == b.x && a.y == b.y;
    150 	return result;
    151 }
    152 
    153 function b32
    154 iv3_equal(iv3 a, iv3 b)
    155 {
    156 	b32 result = a.x == b.x && a.y == b.y && a.z == b.z;
    157 	return result;
    158 }
    159 
    160 function v2
    161 clamp_v2_rect(v2 v, Rect r)
    162 {
    163 	v2 result = v;
    164 	result.x = CLAMP(v.x, r.pos.x, r.pos.x + r.size.x);
    165 	result.y = CLAMP(v.y, r.pos.y, r.pos.y + r.size.y);
    166 	return result;
    167 }
    168 
    169 function v2
    170 v2_scale(v2 a, f32 scale)
    171 {
    172 	v2 result;
    173 	result.x = a.x * scale;
    174 	result.y = a.y * scale;
    175 	return result;
    176 }
    177 
    178 function v2
    179 v2_add(v2 a, v2 b)
    180 {
    181 	v2 result;
    182 	result.x = a.x + b.x;
    183 	result.y = a.y + b.y;
    184 	return result;
    185 }
    186 
    187 function v2
    188 v2_sub(v2 a, v2 b)
    189 {
    190 	v2 result = v2_add(a, v2_scale(b, -1.0f));
    191 	return result;
    192 }
    193 
    194 function v2
    195 v2_mul(v2 a, v2 b)
    196 {
    197 	v2 result;
    198 	result.x = a.x * b.x;
    199 	result.y = a.y * b.y;
    200 	return result;
    201 }
    202 
    203 function v2
    204 v2_div(v2 a, v2 b)
    205 {
    206 	v2 result;
    207 	result.x = a.x / b.x;
    208 	result.y = a.y / b.y;
    209 	return result;
    210 }
    211 
    212 function v2
    213 v2_floor(v2 a)
    214 {
    215 	v2 result;
    216 	result.x = (f32)((i32)a.x);
    217 	result.y = (f32)((i32)a.y);
    218 	return result;
    219 }
    220 
    221 function f32
    222 v2_magnitude_squared(v2 a)
    223 {
    224 	f32 result = a.x * a.x + a.y * a.y;
    225 	return result;
    226 }
    227 
    228 function f32
    229 v2_magnitude(v2 a)
    230 {
    231 	f32 result = sqrt_f32(a.x * a.x + a.y * a.y);
    232 	return result;
    233 }
    234 
    235 function v3
    236 cross(v3 a, v3 b)
    237 {
    238 	v3 result;
    239 	result.x = a.y * b.z - a.z * b.y;
    240 	result.y = a.z * b.x - a.x * b.z;
    241 	result.z = a.x * b.y - a.y * b.x;
    242 	return result;
    243 }
    244 
    245 function v3
    246 v3_from_iv3(iv3 v)
    247 {
    248 	v3 result;
    249 	result.E[0] = (f32)v.E[0];
    250 	result.E[1] = (f32)v.E[1];
    251 	result.E[2] = (f32)v.E[2];
    252 	return result;
    253 }
    254 
    255 function v3
    256 v3_abs(v3 a)
    257 {
    258 	v3 result;
    259 	result.x = ABS(a.x);
    260 	result.y = ABS(a.y);
    261 	result.z = ABS(a.z);
    262 	return result;
    263 }
    264 
    265 function v3
    266 v3_scale(v3 a, f32 scale)
    267 {
    268 	v3 result;
    269 	result.x = scale * a.x;
    270 	result.y = scale * a.y;
    271 	result.z = scale * a.z;
    272 	return result;
    273 }
    274 
    275 function v3
    276 v3_add(v3 a, v3 b)
    277 {
    278 	v3 result;
    279 	result.x = a.x + b.x;
    280 	result.y = a.y + b.y;
    281 	result.z = a.z + b.z;
    282 	return result;
    283 }
    284 
    285 function v3
    286 v3_sub(v3 a, v3 b)
    287 {
    288 	v3 result = v3_add(a, v3_scale(b, -1.0f));
    289 	return result;
    290 }
    291 
    292 function v3
    293 v3_div(v3 a, v3 b)
    294 {
    295 	v3 result;
    296 	result.x = a.x / b.x;
    297 	result.y = a.y / b.y;
    298 	result.z = a.z / b.z;
    299 	return result;
    300 }
    301 
    302 function f32
    303 v3_dot(v3 a, v3 b)
    304 {
    305 	f32 result = a.x * b.x + a.y * b.y + a.z * b.z;
    306 	return result;
    307 }
    308 
    309 function f32
    310 v3_magnitude_squared(v3 a)
    311 {
    312 	f32 result = v3_dot(a, a);
    313 	return result;
    314 }
    315 
    316 function f32
    317 v3_magnitude(v3 a)
    318 {
    319 	f32 result = sqrt_f32(v3_dot(a, a));
    320 	return result;
    321 }
    322 
    323 function v3
    324 v3_normalize(v3 a)
    325 {
    326 	v3 result = v3_scale(a, 1.0f / v3_magnitude(a));
    327 	return result;
    328 }
    329 
    330 function v4
    331 v4_scale(v4 a, f32 scale)
    332 {
    333 	v4 result;
    334 	result.x = scale * a.x;
    335 	result.y = scale * a.y;
    336 	result.z = scale * a.z;
    337 	result.w = scale * a.w;
    338 	return result;
    339 }
    340 
    341 function v4
    342 v4_add(v4 a, v4 b)
    343 {
    344 	v4 result;
    345 	result.x = a.x + b.x;
    346 	result.y = a.y + b.y;
    347 	result.z = a.z + b.z;
    348 	result.w = a.w + b.w;
    349 	return result;
    350 }
    351 
    352 function v4
    353 v4_sub(v4 a, v4 b)
    354 {
    355 	v4 result = v4_add(a, v4_scale(b, -1));
    356 	return result;
    357 }
    358 
    359 function f32
    360 v4_dot(v4 a, v4 b)
    361 {
    362 	f32 result = a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
    363 	return result;
    364 }
    365 
    366 function v4
    367 v4_lerp(v4 a, v4 b, f32 t)
    368 {
    369 	v4 result = v4_add(a, v4_scale(v4_sub(b, a), t));
    370 	return result;
    371 }
    372 
    373 function m4
    374 m4_identity(void)
    375 {
    376 	m4 result;
    377 	result.c[0] = (v4){{1, 0, 0, 0}};
    378 	result.c[1] = (v4){{0, 1, 0, 0}};
    379 	result.c[2] = (v4){{0, 0, 1, 0}};
    380 	result.c[3] = (v4){{0, 0, 0, 1}};
    381 	return result;
    382 }
    383 
    384 function v4
    385 m4_row(m4 a, u32 row)
    386 {
    387 	v4 result;
    388 	result.E[0] = a.c[0].E[row];
    389 	result.E[1] = a.c[1].E[row];
    390 	result.E[2] = a.c[2].E[row];
    391 	result.E[3] = a.c[3].E[row];
    392 	return result;
    393 }
    394 
    395 function m4
    396 m4_mul(m4 a, m4 b)
    397 {
    398 	m4 result;
    399 	for (u32 i = 0; i < 4; i++) {
    400 		for (u32 j = 0; j < 4; j++) {
    401 			result.c[i].E[j] = v4_dot(m4_row(a, j), b.c[i]);
    402 		}
    403 	}
    404 	return result;
    405 }
    406 
    407 /* NOTE(rnp): based on:
    408  * https://web.archive.org/web/20131215123403/ftp://download.intel.com/design/PentiumIII/sml/24504301.pdf
    409  * TODO(rnp): redo with SIMD as given in the link (but need to rewrite for column-major)
    410  */
    411 function m4
    412 m4_inverse(m4 m)
    413 {
    414 	m4 result;
    415 	result.E[ 0] =  m.E[5] * m.E[10] * m.E[15] - m.E[5] * m.E[11] * m.E[14] - m.E[9] * m.E[6] * m.E[15] + m.E[9] * m.E[7] * m.E[14] + m.E[13] * m.E[6] * m.E[11] - m.E[13] * m.E[7] * m.E[10];
    416 	result.E[ 4] = -m.E[4] * m.E[10] * m.E[15] + m.E[4] * m.E[11] * m.E[14] + m.E[8] * m.E[6] * m.E[15] - m.E[8] * m.E[7] * m.E[14] - m.E[12] * m.E[6] * m.E[11] + m.E[12] * m.E[7] * m.E[10];
    417 	result.E[ 8] =  m.E[4] * m.E[ 9] * m.E[15] - m.E[4] * m.E[11] * m.E[13] - m.E[8] * m.E[5] * m.E[15] + m.E[8] * m.E[7] * m.E[13] + m.E[12] * m.E[5] * m.E[11] - m.E[12] * m.E[7] * m.E[ 9];
    418 	result.E[12] = -m.E[4] * m.E[ 9] * m.E[14] + m.E[4] * m.E[10] * m.E[13] + m.E[8] * m.E[5] * m.E[14] - m.E[8] * m.E[6] * m.E[13] - m.E[12] * m.E[5] * m.E[10] + m.E[12] * m.E[6] * m.E[ 9];
    419 	result.E[ 1] = -m.E[1] * m.E[10] * m.E[15] + m.E[1] * m.E[11] * m.E[14] + m.E[9] * m.E[2] * m.E[15] - m.E[9] * m.E[3] * m.E[14] - m.E[13] * m.E[2] * m.E[11] + m.E[13] * m.E[3] * m.E[10];
    420 	result.E[ 5] =  m.E[0] * m.E[10] * m.E[15] - m.E[0] * m.E[11] * m.E[14] - m.E[8] * m.E[2] * m.E[15] + m.E[8] * m.E[3] * m.E[14] + m.E[12] * m.E[2] * m.E[11] - m.E[12] * m.E[3] * m.E[10];
    421 	result.E[ 9] = -m.E[0] * m.E[ 9] * m.E[15] + m.E[0] * m.E[11] * m.E[13] + m.E[8] * m.E[1] * m.E[15] - m.E[8] * m.E[3] * m.E[13] - m.E[12] * m.E[1] * m.E[11] + m.E[12] * m.E[3] * m.E[ 9];
    422 	result.E[13] =  m.E[0] * m.E[ 9] * m.E[14] - m.E[0] * m.E[10] * m.E[13] - m.E[8] * m.E[1] * m.E[14] + m.E[8] * m.E[2] * m.E[13] + m.E[12] * m.E[1] * m.E[10] - m.E[12] * m.E[2] * m.E[ 9];
    423 	result.E[ 2] =  m.E[1] * m.E[ 6] * m.E[15] - m.E[1] * m.E[ 7] * m.E[14] - m.E[5] * m.E[2] * m.E[15] + m.E[5] * m.E[3] * m.E[14] + m.E[13] * m.E[2] * m.E[ 7] - m.E[13] * m.E[3] * m.E[ 6];
    424 	result.E[ 6] = -m.E[0] * m.E[ 6] * m.E[15] + m.E[0] * m.E[ 7] * m.E[14] + m.E[4] * m.E[2] * m.E[15] - m.E[4] * m.E[3] * m.E[14] - m.E[12] * m.E[2] * m.E[ 7] + m.E[12] * m.E[3] * m.E[ 6];
    425 	result.E[10] =  m.E[0] * m.E[ 5] * m.E[15] - m.E[0] * m.E[ 7] * m.E[13] - m.E[4] * m.E[1] * m.E[15] + m.E[4] * m.E[3] * m.E[13] + m.E[12] * m.E[1] * m.E[ 7] - m.E[12] * m.E[3] * m.E[ 5];
    426 	result.E[14] = -m.E[0] * m.E[ 5] * m.E[14] + m.E[0] * m.E[ 6] * m.E[13] + m.E[4] * m.E[1] * m.E[14] - m.E[4] * m.E[2] * m.E[13] - m.E[12] * m.E[1] * m.E[ 6] + m.E[12] * m.E[2] * m.E[ 5];
    427 	result.E[ 3] = -m.E[1] * m.E[ 6] * m.E[11] + m.E[1] * m.E[ 7] * m.E[10] + m.E[5] * m.E[2] * m.E[11] - m.E[5] * m.E[3] * m.E[10] - m.E[ 9] * m.E[2] * m.E[ 7] + m.E[ 9] * m.E[3] * m.E[ 6];
    428 	result.E[ 7] =  m.E[0] * m.E[ 6] * m.E[11] - m.E[0] * m.E[ 7] * m.E[10] - m.E[4] * m.E[2] * m.E[11] + m.E[4] * m.E[3] * m.E[10] + m.E[ 8] * m.E[2] * m.E[ 7] - m.E[ 8] * m.E[3] * m.E[ 6];
    429 	result.E[11] = -m.E[0] * m.E[ 5] * m.E[11] + m.E[0] * m.E[ 7] * m.E[ 9] + m.E[4] * m.E[1] * m.E[11] - m.E[4] * m.E[3] * m.E[ 9] - m.E[ 8] * m.E[1] * m.E[ 7] + m.E[ 8] * m.E[3] * m.E[ 5];
    430 	result.E[15] =  m.E[0] * m.E[ 5] * m.E[10] - m.E[0] * m.E[ 6] * m.E[ 9] - m.E[4] * m.E[1] * m.E[10] + m.E[4] * m.E[2] * m.E[ 9] + m.E[ 8] * m.E[1] * m.E[ 6] - m.E[ 8] * m.E[2] * m.E[ 5];
    431 
    432 	f32 determinant = m.E[0] * result.E[0] + m.E[1] * result.E[4] + m.E[2] * result.E[8] + m.E[3] * result.E[12];
    433 	determinant = 1.0f / determinant;
    434 	for(i32 i = 0; i < 16; i++)
    435 		result.E[i] *= determinant;
    436 	return result;
    437 }
    438 
    439 function m4
    440 m4_translation(v3 delta)
    441 {
    442 	m4 result;
    443 	result.c[0] = (v4){{1, 0, 0, 0}};
    444 	result.c[1] = (v4){{0, 1, 0, 0}};
    445 	result.c[2] = (v4){{0, 0, 1, 0}};
    446 	result.c[3] = (v4){{delta.x, delta.y, delta.z, 1}};
    447 	return result;
    448 }
    449 
    450 function m4
    451 m4_scale(v3 scale)
    452 {
    453 	m4 result;
    454 	result.c[0] = (v4){{scale.x, 0,       0,       0}};
    455 	result.c[1] = (v4){{0,       scale.y, 0,       0}};
    456 	result.c[2] = (v4){{0,       0,       scale.z, 0}};
    457 	result.c[3] = (v4){{0,       0,       0,       1}};
    458 	return result;
    459 }
    460 
    461 function m4
    462 m4_rotation_about_z(f32 turns)
    463 {
    464 	f32 sa = sin_f32(turns * 2 * PI);
    465 	f32 ca = cos_f32(turns * 2 * PI);
    466 	m4 result;
    467 	result.c[0] = (v4){{ca, -sa, 0, 0}};
    468 	result.c[1] = (v4){{sa,  ca, 0, 0}};
    469 	result.c[2] = (v4){{0,    0, 1, 0}};
    470 	result.c[3] = (v4){{0,    0, 0, 1}};
    471 	return result;
    472 }
    473 
    474 function m4
    475 m4_rotation_about_y(f32 turns)
    476 {
    477 	f32 sa = sin_f32(turns * 2 * PI);
    478 	f32 ca = cos_f32(turns * 2 * PI);
    479 	m4 result;
    480 	result.c[0] = (v4){{ca, 0, -sa, 0}};
    481 	result.c[1] = (v4){{0,  1,  0,  0}};
    482 	result.c[2] = (v4){{sa, 0,  ca, 0}};
    483 	result.c[3] = (v4){{0,  0,  0,  1}};
    484 	return result;
    485 }
    486 
    487 function m4
    488 y_aligned_volume_transform(v3 extent, v3 translation, f32 rotation_turns)
    489 {
    490 	m4 T = m4_translation(translation);
    491 	m4 R = m4_rotation_about_y(rotation_turns);
    492 	m4 S = m4_scale(extent);
    493 	m4 result = m4_mul(T, m4_mul(R, S));
    494 	return result;
    495 }
    496 
    497 function v4
    498 m4_mul_v4(m4 a, v4 v)
    499 {
    500 	v4 result;
    501 	result.x = v4_dot(m4_row(a, 0), v);
    502 	result.y = v4_dot(m4_row(a, 1), v);
    503 	result.z = v4_dot(m4_row(a, 2), v);
    504 	result.w = v4_dot(m4_row(a, 3), v);
    505 	return result;
    506 }
    507 
    508 function m4
    509 orthographic_projection(f32 n, f32 f, f32 t, f32 r)
    510 {
    511 	m4 result;
    512 	f32 a = -2 / (f - n);
    513 	f32 b = - (f + n) / (f - n);
    514 	result.c[0] = (v4){{1 / r, 0,     0,  0}};
    515 	result.c[1] = (v4){{0,     1 / t, 0,  0}};
    516 	result.c[2] = (v4){{0,     0,     a,  0}};
    517 	result.c[3] = (v4){{0,     0,     b,  1}};
    518 	return result;
    519 }
    520 
    521 function m4
    522 perspective_projection(f32 n, f32 f, f32 fov, f32 aspect)
    523 {
    524 	m4 result;
    525 	f32 t = tan_f32(fov / 2.0f);
    526 	f32 r = t * aspect;
    527 	f32 a = -(f + n) / (f - n);
    528 	f32 b = -2 * f * n / (f - n);
    529 	result.c[0] = (v4){{1 / r, 0,     0,  0}};
    530 	result.c[1] = (v4){{0,     1 / t, 0,  0}};
    531 	result.c[2] = (v4){{0,     0,     a, -1}};
    532 	result.c[3] = (v4){{0,     0,     b,  0}};
    533 	return result;
    534 }
    535 
    536 function m4
    537 camera_look_at(v3 camera, v3 point)
    538 {
    539 	v3 orthogonal = {{0, 1.0f, 0}};
    540 	v3 normal     = v3_normalize(v3_sub(camera, point));
    541 	v3 right      = cross(orthogonal, normal);
    542 	v3 up         = cross(normal,     right);
    543 
    544 	v3 translate;
    545 	camera      = v3_sub((v3){0}, camera);
    546 	translate.x = v3_dot(camera, right);
    547 	translate.y = v3_dot(camera, up);
    548 	translate.z = v3_dot(camera, normal);
    549 
    550 	m4 result;
    551 	result.c[0] = (v4){{right.x,     up.x,        normal.x,    0}};
    552 	result.c[1] = (v4){{right.y,     up.y,        normal.y,    0}};
    553 	result.c[2] = (v4){{right.z,     up.z,        normal.z,    0}};
    554 	result.c[3] = (v4){{translate.x, translate.y, translate.z, 1}};
    555 	return result;
    556 }
    557 
    558 /* NOTE(rnp): adapted from "Essential Mathematics for Games and Interactive Applications" (Verth, Bishop) */
    559 function f32
    560 obb_raycast(m4 obb_orientation, v3 obb_size, v3 obb_center, ray r)
    561 {
    562 	v3 p = v3_sub(obb_center, r.origin);
    563 	v3 X = obb_orientation.c[0].xyz;
    564 	v3 Y = obb_orientation.c[1].xyz;
    565 	v3 Z = obb_orientation.c[2].xyz;
    566 
    567 	/* NOTE(rnp): projects direction vector onto OBB axis */
    568 	v3 f;
    569 	f.x = v3_dot(X, r.direction);
    570 	f.y = v3_dot(Y, r.direction);
    571 	f.z = v3_dot(Z, r.direction);
    572 
    573 	/* NOTE(rnp): projects relative vector onto OBB axis */
    574 	v3 e;
    575 	e.x = v3_dot(X, p);
    576 	e.y = v3_dot(Y, p);
    577 	e.z = v3_dot(Z, p);
    578 
    579 	f32 result = 0;
    580 	f32 t[6] = {0};
    581 	for (i32 i = 0; i < 3; i++) {
    582 		if (f32_cmp(f.E[i], 0)) {
    583 			if (-e.E[i] - obb_size.E[i] > 0 || -e.E[i] + obb_size.E[i] < 0)
    584 				result = -1.0f;
    585 			f.E[i] = F32_EPSILON;
    586 		}
    587 		t[i * 2 + 0] = (e.E[i] + obb_size.E[i]) / f.E[i];
    588 		t[i * 2 + 1] = (e.E[i] - obb_size.E[i]) / f.E[i];
    589 	}
    590 
    591 	if (result != -1) {
    592 		f32 tmin = MAX(MAX(MIN(t[0], t[1]), MIN(t[2], t[3])), MIN(t[4], t[5]));
    593 		f32 tmax = MIN(MIN(MAX(t[0], t[1]), MAX(t[2], t[3])), MAX(t[4], t[5]));
    594 		if (tmax >= 0 && tmin <= tmax) {
    595 			result = tmin > 0 ? tmin : tmax;
    596 		} else {
    597 			result = -1;
    598 		}
    599 	}
    600 
    601 	return result;
    602 }
    603 
    604 function f32
    605 complex_filter_first_moment(v2 *filter, i32 length, f32 sampling_frequency)
    606 {
    607 	f32 n = 0, d = 0;
    608 	for (i32 i = 0; i < length; i++) {
    609 		f32 t = v2_magnitude_squared(filter[i]);
    610 		n += (f32)i * t;
    611 		d += t;
    612 	}
    613 	f32 result = n / d / sampling_frequency;
    614 	return result;
    615 }
    616 
    617 function f32
    618 real_filter_first_moment(f32 *filter, i32 length, f32 sampling_frequency)
    619 {
    620 	f32 n = 0, d = 0;
    621 	for (i32 i = 0; i < length; i++) {
    622 		f32 t = filter[i] * filter[i];
    623 		n += (f32)i * t;
    624 		d += t;
    625 	}
    626 	f32 result = n / d / sampling_frequency;
    627 	return result;
    628 }
    629 
    630 function f32
    631 tukey_window(f32 t, f32 tapering)
    632 {
    633 	f32 r = tapering;
    634 	f32 result = 1;
    635 	if (t < r / 2)      result = 0.5f * (1 + cos_f32(2 * PI * (t - r / 2)     / r));
    636 	if (t >= 1 - r / 2) result = 0.5f * (1 + cos_f32(2 * PI * (t - 1 + r / 2) / r));
    637 	return result;
    638 }
    639 
    640 /* NOTE(rnp): adapted from "Discrete Time Signal Processing" (Oppenheim) */
    641 function f32 *
    642 kaiser_low_pass_filter(Arena *arena, f32 cutoff_frequency, f32 sampling_frequency, f32 beta, i32 length)
    643 {
    644 	f32 *result = push_array(arena, f32, length);
    645 	f32 wc      = 2 * PI * cutoff_frequency / sampling_frequency;
    646 	f32 a       = (f32)length / 2.0f;
    647 	f32 pi_i0_b = PI * (f32)cephes_i0(beta);
    648 
    649 	for (i32 n = 0; n < length; n++) {
    650 		f32 t       = (f32)n - a;
    651 		f32 impulse = !f32_cmp(t, 0) ? sin_f32(wc * t) / t : wc;
    652 		t           = t / a;
    653 		f32 window  = (f32)cephes_i0(beta * sqrt_f32(1 - t * t)) / pi_i0_b;
    654 		result[n]   = impulse * window;
    655 	}
    656 
    657 	return result;
    658 }
    659 
    660 function f32 *
    661 rf_chirp(Arena *arena, f32 min_frequency, f32 max_frequency, f32 sampling_frequency,
    662          i32 length, b32 reverse)
    663 {
    664 	f32 *result = push_array(arena, f32, length);
    665 	for (i32 i = 0; i < length; i++) {
    666 		i32 index = reverse? length - 1 - i : i;
    667 		f32 fc    = min_frequency + (f32)i * (max_frequency - min_frequency) / (2 * (f32)length);
    668 		f32 arg   = 2 * PI * fc * (f32)i / sampling_frequency;
    669 		result[index] = sin_f32(arg) * tukey_window((f32)i / (f32)length, 0.2f);
    670 	}
    671 	return result;
    672 }
    673 
    674 function v2 *
    675 baseband_chirp(Arena *arena, f32 min_frequency, f32 max_frequency, f32 sampling_frequency,
    676                i32 length, b32 reverse, f32 scale)
    677 {
    678 	v2 *result    = push_array(arena, v2, length);
    679 	f32 conjugate = reverse ? -1 : 1;
    680 	for (i32 i = 0; i < length; i++) {
    681 		i32 index = reverse? length - 1 - i : i;
    682 		f32 fc    = min_frequency + (f32)i * (max_frequency - min_frequency) / (2 * (f32)length);
    683 		f32 arg   = 2 * PI * fc * (f32)i / sampling_frequency;
    684 		v2 sample = {{scale * cos_f32(arg), conjugate * scale * sin_f32(arg)}};
    685 		result[index] = v2_scale(sample, tukey_window((f32)i / (f32)length, 0.2f));
    686 	}
    687 	return result;
    688 }
    689 
    690 function v4
    691 hsv_to_rgb(v4 hsv)
    692 {
    693 	/* f(k(n))   = V - V*S*max(0, min(k, min(4 - k, 1)))
    694 	 * k(n)      = fmod((n + H * 6), 6)
    695 	 * (R, G, B) = (f(n = 5), f(n = 3), f(n = 1))
    696 	 */
    697 	alignas(16) f32 nval[4] = {5.0f, 3.0f, 1.0f, 0.0f};
    698 	f32x4 n   = load_f32x4(nval);
    699 	f32x4 H   = dup_f32x4(hsv.x);
    700 	f32x4 S   = dup_f32x4(hsv.y);
    701 	f32x4 V   = dup_f32x4(hsv.z);
    702 	f32x4 six = dup_f32x4(6);
    703 
    704 	f32x4 t   = add_f32x4(n, mul_f32x4(six, H));
    705 	f32x4 rem = floor_f32x4(div_f32x4(t, six));
    706 	f32x4 k   = sub_f32x4(t, mul_f32x4(rem, six));
    707 
    708 	t = min_f32x4(sub_f32x4(dup_f32x4(4), k), dup_f32x4(1));
    709 	t = max_f32x4(dup_f32x4(0), min_f32x4(k, t));
    710 	t = mul_f32x4(t, mul_f32x4(S, V));
    711 
    712 	v4 rgba;
    713 	store_f32x4(rgba.E, sub_f32x4(V, t));
    714 	rgba.a = hsv.a;
    715 	return rgba;
    716 }
    717 
    718 function f32
    719 ease_in_out_cubic(f32 t)
    720 {
    721 	f32 result;
    722 	if (t < 0.5f) {
    723 		result = 4.0f * t * t * t;
    724 	} else {
    725 		t      = -2.0f * t + 2.0f;
    726 		result =  1.0f - t * t * t / 2.0f;
    727 	}
    728 	return result;
    729 }
    730 
    731 function f32
    732 ease_in_out_quartic(f32 t)
    733 {
    734 	f32 result;
    735 	if (t < 0.5f) {
    736 		result = 8.0f * t * t * t * t;
    737 	} else {
    738 		t      = -2.0f * t + 2.0f;
    739 		result =  1.0f - t * t * t * t / 2.0f;
    740 	}
    741 	return result;
    742 }