-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.c
444 lines (395 loc) · 8.42 KB
/
util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/* See LICENSE for license details. */
static i32 hadamard_12_12_transpose[] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, -1, -1, 1, -1, -1, -1, 1, 1, 1, -1, 1,
1, 1, -1, -1, 1, -1, -1, -1, 1, 1, 1, -1,
1, -1, 1, -1, -1, 1, -1, -1, -1, 1, 1, 1,
1, 1, -1, 1, -1, -1, 1, -1, -1, -1, 1, 1,
1, 1, 1, -1, 1, -1, -1, 1, -1, -1, -1, 1,
1, 1, 1, 1, -1, 1, -1, -1, 1, -1, -1, -1,
1, -1, 1, 1, 1, -1, 1, -1, -1, 1, -1, -1,
1, -1, -1, 1, 1, 1, -1, 1, -1, -1, 1, -1,
1, -1, -1, -1, 1, 1, 1, -1, 1, -1, -1, 1,
1, 1, -1, -1, -1, 1, 1, 1, -1, 1, -1, -1,
1, -1, 1, -1, -1, -1, 1, 1, 1, -1, 1, -1,
};
static void *
mem_clear(void *p_, u8 c, size len)
{
u8 *p = p_;
while (len) p[--len] = c;
return p;
}
static void
mem_copy(void *restrict src, void *restrict dest, size n)
{
ASSERT(n >= 0);
u8 *s = src, *d = dest;
for (; n; n--) *d++ = *s++;
}
static void
mem_move(u8 *src, u8 *dest, size n)
{
if (dest < src) mem_copy(src, dest, n);
else while (n) { n--; dest[n] = src[n]; }
}
#define alloc(a, t, n) (t *)alloc_(a, sizeof(t), _Alignof(t), n)
static void *
alloc_(Arena *a, size len, size align, size count)
{
/* NOTE: special case 0 arena */
if (a->beg == 0)
return 0;
size padding = -(uintptr_t)a->beg & (align - 1);
size available = a->end - a->beg - padding;
if (available < 0 || count > available / len)
ASSERT(0 && "arena OOM\n");
void *p = a->beg + padding;
a->beg += padding + count * len;
/* TODO: Performance? */
return mem_clear(p, 0, count * len);
}
static Arena
sub_arena(Arena *a, size size)
{
Arena result = {0};
if ((a->end - a->beg) >= size) {
result.beg = a->beg;
result.end = a->beg + size;
a->beg += size;
}
return result;
}
static TempArena
begin_temp_arena(Arena *a)
{
TempArena result = {.arena = a, .old_beg = a->beg};
return result;
}
static void
end_temp_arena(TempArena ta)
{
Arena *a = ta.arena;
if (a) {
ASSERT(a->beg >= ta.old_beg)
a->beg = ta.old_beg;
}
}
static Stream
arena_stream(Arena *a)
{
Stream result = {0};
result.data = a->beg;
result.cap = a->end - a->beg;
a->beg = a->end;
return result;
}
static Stream
stream_alloc(Arena *a, size cap)
{
Stream result = {.cap = cap};
result.data = alloc(a, u8, cap);
return result;
}
static s8
stream_to_s8(Stream *s)
{
s8 result = {.len = s->widx, .data = s->data};
return result;
}
static void
stream_append_byte(Stream *s, u8 b)
{
s->errors |= s->widx + 1 > s->cap;
if (!s->errors)
s->data[s->widx++] = b;
}
static void
stream_append_s8(Stream *s, s8 str)
{
s->errors |= (s->cap - s->widx) < str.len;
if (!s->errors) {
mem_copy(str.data, s->data + s->widx, str.len);
s->widx += str.len;
}
}
static void
stream_append_s8_array(Stream *s, s8 *strs, size count)
{
while (count > 0) {
stream_append_s8(s, *strs);
strs++;
count--;
}
}
static void
stream_append_u64(Stream *s, u64 n)
{
u8 tmp[64];
u8 *end = tmp + sizeof(tmp);
u8 *beg = end;
do { *--beg = '0' + (n % 10); } while (n /= 10);
stream_append_s8(s, (s8){.len = end - beg, .data = beg});
}
static void
stream_append_i64(Stream *s, i64 n)
{
if (n < 0) {
stream_append_byte(s, '-');
n *= -1;
}
stream_append_u64(s, n);
}
static void
stream_append_f64(Stream *s, f64 f, i64 prec)
{
if (f < 0) {
stream_append_byte(s, '-');
f *= -1;
}
/* NOTE: round last digit */
f += 0.5f / prec;
if (f >= (f64)(-1UL >> 1)) {
stream_append_s8(s, s8("inf"));
} else {
u64 integral = f;
u64 fraction = (f - integral) * prec;
stream_append_u64(s, integral);
stream_append_byte(s, '.');
for (i64 i = prec / 10; i > 1; i /= 10) {
if (i > fraction)
stream_append_byte(s, '0');
}
stream_append_u64(s, fraction);
}
}
static void
stream_append_f64_e(Stream *s, f64 f)
{
/* TODO: there should be a better way of doing this */
#if 0
/* NOTE: we ignore subnormal numbers for now */
union { f64 f; u64 u; } u = {.f = f};
i32 exponent = ((u.u >> 52) & 0x7ff) - 1023;
f32 log_10_of_2 = 0.301f;
i32 scale = (exponent * log_10_of_2);
/* NOTE: normalize f */
for (i32 i = ABS(scale); i > 0; i--)
f *= (scale > 0)? 0.1f : 10.0f;
#else
i32 scale = 0;
if (f != 0) {
while (f > 1) {
f *= 0.1f;
scale++;
}
while (f < 1) {
f *= 10.0f;
scale--;
}
}
#endif
i32 prec = 100;
stream_append_f64(s, f, prec);
stream_append_byte(s, 'e');
stream_append_byte(s, scale >= 0? '+' : '-');
for (i32 i = prec / 10; i > 1; i /= 10)
stream_append_byte(s, '0');
stream_append_u64(s, ABS(scale));
}
static void
stream_append_variable(Stream *s, Variable *var)
{
switch (var->type) {
case VT_F32: {
f32 *f32_val = var->store;
stream_append_f64(s, *f32_val * var->display_scale, 100);
} break;
case VT_I32: {
i32 *i32_val = var->store;
stream_append_i64(s, *i32_val * var->display_scale);
} break;
default: INVALID_CODE_PATH;
}
}
static s8
cstr_to_s8(char *cstr)
{
s8 result = {.data = (u8 *)cstr};
while (*cstr) { result.len++; cstr++; }
return result;
}
static s8
s8_cut_head(s8 s, size cut)
{
s8 result = s;
if (cut > 0) {
result.data += cut;
result.len -= cut;
}
return result;
}
static s8
s8alloc(Arena *a, size len)
{
return (s8){ .data = alloc(a, u8, len), .len = len };
}
static s8
push_s8(Arena *a, s8 str)
{
s8 result = s8alloc(a, str.len);
mem_copy(str.data, result.data, result.len);
return result;
}
static b32
uv4_equal(uv4 a, uv4 b)
{
return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w;
}
static u32
round_down_power_of_2(u32 a)
{
u32 result = 0x80000000UL >> clz_u32(a);
return result;
}
static v3
cross(v3 a, v3 b)
{
v3 result = {
.x = a.y * b.z - a.z * b.y,
.y = a.z * b.x - a.x * b.z,
.z = a.x * b.y - a.y * b.x,
};
return result;
}
static v3
sub_v3(v3 a, v3 b)
{
v3 result = {
.x = a.x - b.x,
.y = a.y - b.y,
.z = a.z - b.z,
};
return result;
}
static f32
length_v3(v3 a)
{
f32 result = a.x * a.x + a.y * a.y + a.z * a.z;
return result;
}
static v3
normalize_v3(v3 a)
{
f32 length = length_v3(a);
v3 result = {.x = a.x / length, .y = a.y / length, .z = a.z / length};
return result;
}
static v2
sub_v2(v2 a, v2 b)
{
v2 result = {
.x = a.x - b.x,
.y = a.y - b.y,
};
return result;
}
static v2
mul_v2(v2 a, v2 b)
{
v2 result = {
.x = a.x * b.x,
.y = a.y * b.y,
};
return result;
}
static f32
magnitude_v2(v2 a)
{
f32 result = sqrt_f32(a.x * a.x + a.y * a.y);
return result;
}
static f64
parse_f64(s8 s)
{
f64 integral = 0, fractional = 0, sign = 1;
if (s.len && *s.data == '-') {
sign = -1;
s.data++;
s.len--;
}
while (s.len && *s.data != '.') {
integral *= 10;
integral += *s.data - '0';
s.data++;
s.len--;
}
if (*s.data == '.') { s.data++; s.len--; }
while (s.len) {
ASSERT(s.data[s.len - 1] != '.');
fractional /= 10;
fractional += (f64)(s.data[--s.len] - '0') / 10.0;
}
f64 result = sign * (integral + fractional);
return result;
}
static void
fill_kronecker_sub_matrix(i32 *out, i32 out_stride, i32 scale, i32 *b, uv2 b_dim)
{
f32x4 vscale = dup_f32x4(scale);
for (u32 i = 0; i < b_dim.y; i++) {
for (u32 j = 0; j < b_dim.x; j += 4, b += 4) {
f32x4 vb = cvt_i32x4_f32x4(load_i32x4(b));
store_i32x4(cvt_f32x4_i32x4(mul_f32x4(vscale, vb)), out + j);
}
out += out_stride;
}
}
/* NOTE: this won't check for valid space/etc and assumes row major order */
static void
kronecker_product(i32 *out, i32 *a, uv2 a_dim, i32 *b, uv2 b_dim)
{
uv2 out_dim = {.x = a_dim.x * b_dim.x, .y = a_dim.y * b_dim.y};
ASSERT(out_dim.y % 4 == 0);
for (u32 i = 0; i < a_dim.y; i++) {
i32 *vout = out;
for (u32 j = 0; j < a_dim.x; j++, a++) {
fill_kronecker_sub_matrix(vout, out_dim.y, *a, b, b_dim);
vout += b_dim.y;
}
out += out_dim.y * b_dim.x;
}
}
/* NOTE/TODO: to support even more hadamard sizes use the Paley construction */
static void
fill_hadamard_transpose(i32 *out, i32 *tmp, u32 dim)
{
ASSERT(dim);
b32 power_of_2 = ISPOWEROF2(dim);
b32 multiple_of_12 = dim % 12 == 0;
if (!power_of_2 && !multiple_of_12)
return;
if (!power_of_2) {
ASSERT(multiple_of_12);
dim /= 12;
}
i32 *m;
if (power_of_2) m = out;
else m = tmp;
#define IND(i, j) ((i) * dim + (j))
m[0] = 1;
for (u32 k = 1; k < dim; k *= 2) {
for (u32 i = 0; i < k; i++) {
for (u32 j = 0; j < k; j++) {
i32 val = m[IND(i, j)];
m[IND(i + k, j)] = val;
m[IND(i, j + k)] = val;
m[IND(i + k, j + k)] = -val;
}
}
}
#undef IND
if (!power_of_2)
kronecker_product(out, tmp, (uv2){.x = dim, .y = dim}, hadamard_12_12_transpose,
(uv2){.x = 12, .y = 12});
}