-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnormalize.c
565 lines (530 loc) · 17.9 KB
/
normalize.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
#include "./cglm/include/cglm/cglm.h"
#define NORMALIZE_I8_SCALAR 127.f
#define NORMALIZE_U8_SCALAR 255.f
#define NORMALIZE_I16_SCALAR 32767.f
#define NORMALIZE_U16_SCALAR 65535.f
#define DENORMALIZE_I8_SCALAR (1.f / NORMALIZE_I8_SCALAR)
#define DENORMALIZE_U8_SCALAR (1.f / NORMALIZE_U8_SCALAR)
#define DENORMALIZE_I16_SCALAR (1.f / NORMALIZE_I16_SCALAR)
#define DENORMALIZE_U16_SCALAR (1.f / NORMALIZE_U16_SCALAR)
//#define NORMALIZE_SIMD_UNROLL
CGLM_INLINE void normalize_scalar(
float * ptr,
const size_t size,
const size_t stride,
const size_t count,
const float scalar
) {
for (size_t i = 0; i < count; ++i) {
float * curr_ptr = ptr;
for (size_t j = 0; j < size; ++j) {
*curr_ptr = roundf((*curr_ptr) * scalar);
curr_ptr++;
}
ptr += stride;
}
}
CGLM_INLINE void denormalize_scalar_i(
float * ptr,
const size_t size,
const size_t stride,
const size_t count,
const float scalar
) {
for (size_t i = 0; i < count; ++i) {
float * curr_ptr = ptr;
for (size_t j = 0; j < size; ++j) {
*curr_ptr = fmaxf((*curr_ptr) * (scalar), -1.f);
curr_ptr++;
}
ptr += stride;
}
}
CGLM_INLINE void denormalize_scalar_u(
float * ptr,
const size_t size,
const size_t stride,
const size_t count,
const float scalar
) {
for (size_t i = 0; i < count; ++i) {
float * curr_ptr = ptr;
for (size_t j = 0; j < size; ++j) {
*curr_ptr = fmaxf((*curr_ptr) * (scalar), -1.f);
curr_ptr++;
}
ptr += stride;
}
}
#if defined(CGLM_SIMD_WASM)
static inline void normalize_internal(float *ptr, const size_t length, const float scalar)
{
#ifdef NORMALIZE_SIMD_UNROLL
glmm_128 v1, v2, v3, v4, v5;
#else
glmm_128 v1, v2;
#endif
size_t num;
num = length;
v1 = wasm_f32x4_splat(scalar);
#ifdef NORMALIZE_SIMD_UNROLL
// loop unrolling
while (num > 15)
{
v2 = glmm_load(ptr);
v3 = glmm_load(ptr + 4);
v4 = glmm_load(ptr + 8);
v5 = glmm_load(ptr + 12);
v2 = wasm_f32x4_mul(v2, v1);
v3 = wasm_f32x4_mul(v3, v1);
v4 = wasm_f32x4_mul(v4, v1);
v5 = wasm_f32x4_mul(v5, v1);
v2 = wasm_f32x4_nearest(v2);
v3 = wasm_f32x4_nearest(v3);
v4 = wasm_f32x4_nearest(v4);
v5 = wasm_f32x4_nearest(v5);
glmm_store(ptr, v2);
glmm_store(ptr + 4, v3);
glmm_store(ptr + 8, v4);
glmm_store(ptr + 12, v5);
ptr += 16;
num -= 16;
}
#endif
while (num > 3)
{
v2 = glmm_load(ptr);
v2 = wasm_f32x4_mul(v2, v1);
v2 = wasm_f32x4_nearest(v2);
glmm_store(ptr, v2);
ptr += 4;
num -= 4;
}
while (num > 0)
{
*ptr = roundf((*ptr) * scalar);
ptr++;
num--;
}
}
CGLM_INLINE void normalize_vec4(
float * ptr,
const size_t stride,
const size_t count,
const float scalar
) {
glmm_128 v1, v2;
v1 = wasm_f32x4_splat(scalar);
for (size_t i = 0; i < count; ++i) {
v2 = glmm_load(ptr);
v2 = wasm_f32x4_mul(v2, v1);
v2 = wasm_f32x4_nearest(v2);
glmm_store(ptr, v2);
ptr += stride;
}
}
CGLM_INLINE void normalize_vec3(
float * ptr,
const size_t stride,
const size_t count,
const float scalar
) {
glmm_128 v1, v2;
v1 = wasm_f32x4_splat(scalar);
for (size_t i = 0; i < count; ++i) {
v2 = glmm_load3(ptr);
v2 = wasm_f32x4_mul(v2, v1);
v2 = wasm_f32x4_nearest(v2);
glmm_store3(ptr, v2);
ptr += stride;
}
}
#define denormalize_max_i(v, min_val) (v) = wasm_f32x4_pmax((v), (min_val))
#define denormalize_max_u(v, min_val)
#define denormalize_scalar_i(scalar, min_val) *ptr = fmaxf((*ptr) * (scalar), min_val)
#define denormalize_scalar_u(scalar, min_val) *ptr = (*ptr) * (scalar)
#ifdef NORMALIZE_SIMD_UNROLL
#define denormalize_fn(name, max_simd_fn, scalar_fn) \
static inline void name(float *ptr, const size_t length, const float scalar) \
{ \
glmm_128 v1, v2, v3, v4, v5, v6; \
size_t num; \
num = length; \
v1 = wasm_f32x4_splat(scalar); \
v2 = wasm_f32x4_const_splat(-1.f); \
/* loop unrolling */ \
while (num > 15) \
{ \
v3 = glmm_load(ptr); \
v4 = glmm_load(ptr + 4); \
v5 = glmm_load(ptr + 8); \
v6 = glmm_load(ptr + 12); \
v3 = wasm_f32x4_mul(v3, v1); \
v4 = wasm_f32x4_mul(v4, v1); \
v5 = wasm_f32x4_mul(v5, v1); \
v6 = wasm_f32x4_mul(v6, v1); \
max_simd_fn(v3, v2); \
max_simd_fn(v4, v2); \
max_simd_fn(v5, v2); \
max_simd_fn(v6, v2); \
glmm_store(ptr, v3); \
glmm_store(ptr + 4, v4); \
glmm_store(ptr + 8, v5); \
glmm_store(ptr + 12, v6); \
ptr += 16; \
num -= 16; \
} \
\
while (num > 3) \
{ \
v3 = glmm_load(ptr); \
v3 = wasm_f32x4_mul(v3, v1); \
max_simd_fn(v3, v2); \
glmm_store(ptr, v3); \
ptr += 4; \
num -= 4; \
} \
\
while (num > 0) \
{ \
scalar_fn(DENORMALIZE_I8_SCALAR, -1.f); \
ptr++; \
num--; \
} \
}
#else
#define denormalize_fn(name, max_simd_fn, scalar_fn) \
static inline void name(float *ptr, const size_t length, const float scalar) \
{ \
glmm_128 v1, v2, v3; \
size_t num; \
num = length; \
v1 = wasm_f32x4_splat(scalar); \
v2 = wasm_f32x4_const_splat(-1.f); \
\
while (num > 3) \
{ \
v3 = glmm_load(ptr); \
v3 = wasm_f32x4_mul(v3, v1); \
max_simd_fn(v3, v2); \
glmm_store(ptr, v3); \
ptr += 4; \
num -= 4; \
} \
\
while (num > 0) \
{ \
scalar_fn(DENORMALIZE_I8_SCALAR, -1.f); \
ptr++; \
num--; \
} \
}
#endif
denormalize_fn(denormalize_i, denormalize_max_i, denormalize_scalar_i);
denormalize_fn(denormalize_u, denormalize_max_u, denormalize_scalar_u);
#undef denormalize_fn
#undef denormalize_max_i
#undef denormalize_max_u
#undef denormalize_scalar_i
#undef denormalize_scalar_u
CGLM_INLINE void denormalize_vec4_i(
float * ptr,
const size_t stride,
const size_t count,
const float scalar
) {
glmm_128 v1, v2, v3;
v1 = wasm_f32x4_splat(scalar);
v2 = wasm_f32x4_const_splat(-1.f);
for (size_t i = 0; i < count; ++i) {
v3 = glmm_load(ptr);
v3 = wasm_f32x4_mul(v3, v1);
v3 = wasm_f32x4_max(v2, v3);
glmm_store(ptr, v3);
ptr += stride;
}
}
CGLM_INLINE void denormalize_vec4_u(
float * ptr,
const size_t stride,
const size_t count,
const float scalar
) {
glmm_128 v1, v2;
v1 = wasm_f32x4_splat(scalar);
for (size_t i = 0; i < count; ++i) {
v2 = glmm_load(ptr);
v2 = wasm_f32x4_mul(v2, v1);
glmm_store(ptr, v2);
ptr += stride;
}
}
CGLM_INLINE void denormalize_vec3_i(
float * ptr,
const size_t stride,
const size_t count,
const float scalar
) {
glmm_128 v1, v2, v3;
v1 = wasm_f32x4_splat(scalar);
v2 = wasm_f32x4_const_splat(-1.f);
for (size_t i = 0; i < count; ++i) {
v3 = glmm_load3(ptr);
v3 = wasm_f32x4_mul(v3, v1);
v3 = wasm_f32x4_max(v2, v3);
glmm_store3(ptr, v3);
ptr += stride;
}
}
CGLM_INLINE void denormalize_vec3_u(
float * ptr,
const size_t stride,
const size_t count,
const float scalar
) {
glmm_128 v1, v2;
v1 = wasm_f32x4_splat(scalar);
for (size_t i = 0; i < count; ++i) {
v2 = glmm_load3(ptr);
v2 = wasm_f32x4_mul(v2, v1);
glmm_store3(ptr, v2);
ptr += stride;
}
}
#else
static inline void normalize_internal(float *ptr, const size_t length, const float scalar)
{
float v1, v2, v3, v4;
size_t num;
num = length;
// loop unrolling
while (num > 3)
{
v1 = *(ptr);
v2 = *(ptr + 1);
v3 = *(ptr + 2);
v4 = *(ptr + 3);
v1 = v1 * scalar;
v2 = v2 * scalar;
v3 = v3 * scalar;
v4 = v4 * scalar;
v1 = roundf(v1);
v2 = roundf(v2);
v3 = roundf(v3);
v4 = roundf(v4);
*(ptr) = v1;
*(ptr + 1) = v2;
*(ptr + 2) = v3;
*(ptr + 3) = v4;
ptr += 4;
num -= 4;
}
while (num > 0)
{
*ptr = roundf((*ptr) * scalar);
ptr++;
num--;
}
}
static inline void denormalize_u(float *ptr, const size_t length, const float scalar)
{
float v1, v2, v3, v4;
size_t num;
num = length;
// loop unrolling
while (num > 3)
{
v1 = *(ptr);
v2 = *(ptr + 1);
v3 = *(ptr + 2);
v4 = *(ptr + 3);
v1 = v1 * scalar;
v2 = v2 * scalar;
v3 = v3 * scalar;
v4 = v4 * scalar;
*(ptr) = v1;
*(ptr + 1) = v2;
*(ptr + 2) = v3;
*(ptr + 3) = v4;
ptr += 4;
num -= 4;
}
while (num > 0)
{
*ptr = *ptr * scalar;
ptr++;
num--;
}
}
static inline void denormalize_i(float *ptr, const size_t length, const float scalar)
{
float v1, v2, v3, v4;
size_t num;
num = length;
// loop unrolling
while (num > 3)
{
v1 = *(ptr);
v2 = *(ptr + 1);
v3 = *(ptr + 2);
v4 = *(ptr + 3);
v1 = v1 * scalar;
v2 = v2 * scalar;
v3 = v3 * scalar;
v4 = v4 * scalar;
v1 = fmaxf(v1, -1.f);
v2 = fmaxf(v2, -1.f);
v3 = fmaxf(v3, -1.f);
v4 = fmaxf(v4, -1.f);
*(ptr) = v1;
*(ptr + 1) = v2;
*(ptr + 2) = v3;
*(ptr + 3) = v4;
ptr += 4;
num -= 4;
}
while (num > 0)
{
*ptr = fmaxf(*ptr * scalar, -1.f);
ptr++;
num--;
}
}
#endif
typedef enum component_type
{
BYTE = 5120,
UNSIGNED_BYTE = 5121,
SHORT = 5122,
UNSIGNED_SHORT = 5123,
} component_type_t;
size_t normalize(
float *ptr,
const size_t size,
const size_t stride,
const size_t count,
const component_type_t component_type
)
{
float scalar = 0.f;
switch (component_type)
{
case BYTE:
scalar = NORMALIZE_I8_SCALAR;
break;
case UNSIGNED_BYTE:
scalar = NORMALIZE_U8_SCALAR;
break;
case SHORT:
scalar = NORMALIZE_I16_SCALAR;
break;
case UNSIGNED_SHORT:
scalar = NORMALIZE_U16_SCALAR;
break;
}
if (scalar > 0.f)
{
if (size == stride) {
size_t length = size * count;
normalize_internal(ptr, length, scalar);
}
#if defined(CGLM_SIMD_WASM)
if (size == 4) {
normalize_vec4(ptr, stride, count, scalar);
} else if (size == 3) {
normalize_vec3(ptr, stride, count, scalar);
} else {
#endif
normalize_scalar(ptr, size, stride, count, scalar);
#if defined(CGLM_SIMD_WASM)
}
#endif
return size * count;
}
// unknown type
return 0;
}
size_t denormalize(
float *ptr,
const size_t size,
const size_t stride,
const size_t count,
const component_type_t component_type
)
{
size_t length = size * count;
if (size == stride) {
switch (component_type) {
case BYTE:
denormalize_i(ptr, length, DENORMALIZE_I8_SCALAR);
return length;
case UNSIGNED_BYTE:
denormalize_u(ptr, length, DENORMALIZE_U8_SCALAR);
return length;
case SHORT:
denormalize_i(ptr, length, DENORMALIZE_I16_SCALAR);
return length;
case UNSIGNED_SHORT:
denormalize_u(ptr, length, DENORMALIZE_U16_SCALAR);
return length;
}
// unknown type
return 0;
}
#if defined(CGLM_SIMD_WASM)
if (size == 4) {
switch (component_type) {
case BYTE:
denormalize_vec4_i(ptr, stride, count, DENORMALIZE_I8_SCALAR);
return length;
case UNSIGNED_BYTE:
denormalize_vec4_u(ptr, stride, count, DENORMALIZE_U8_SCALAR);
return length;
case SHORT:
denormalize_vec4_i(ptr, stride, count, DENORMALIZE_I16_SCALAR);
return length;
case UNSIGNED_SHORT:
denormalize_vec4_u(ptr, stride, count, DENORMALIZE_U16_SCALAR);
return length;
}
// unknown type
return 0;
} else if (size == 3) {
switch (component_type) {
case BYTE:
denormalize_vec3_i(ptr, stride, count, DENORMALIZE_I8_SCALAR);
return length;
case UNSIGNED_BYTE:
denormalize_vec3_u(ptr, stride, count, DENORMALIZE_U8_SCALAR);
return length;
case SHORT:
denormalize_vec3_i(ptr, stride, count, DENORMALIZE_I16_SCALAR);
return length;
case UNSIGNED_SHORT:
denormalize_vec3_u(ptr, stride, count, DENORMALIZE_U16_SCALAR);
return length;
}
return 0;
} else {
#endif
switch (component_type) {
case BYTE:
denormalize_scalar_i(ptr, size, stride, count, DENORMALIZE_I8_SCALAR);
return length;
case UNSIGNED_BYTE:
denormalize_scalar_u(ptr, size, stride, count, DENORMALIZE_U8_SCALAR);
return length;
case SHORT:
denormalize_scalar_i(ptr, size, stride, count, DENORMALIZE_I16_SCALAR);
return length;
case UNSIGNED_SHORT:
denormalize_scalar_u(ptr, size, stride, count, DENORMALIZE_U16_SCALAR);
return length;
}
return 0;
#if defined(CGLM_SIMD_WASM)
}
#endif
// unknown type
return 0;
}