-
Notifications
You must be signed in to change notification settings - Fork 0
/
normalize.c
285 lines (266 loc) · 9.29 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
#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)
#if defined(CGLM_SIMD_WASM)
static inline void normalize_internal(float *ptr, const size_t length, const float scalar)
{
glmm_128 v1, v2, v3, v4, v5;
size_t num;
num = length;
v1 = wasm_f32x4_splat(scalar);
// 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;
}
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--;
}
}
#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)
#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--; \
} \
}
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
#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 length, 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)
{
normalize_internal(ptr, length, scalar);
return length;
}
// unknown type
return 0;
}
size_t denormalize(float *ptr, const size_t length, const component_type_t component_type)
{
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;
}