-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwasm_math.h
478 lines (445 loc) · 13.7 KB
/
wasm_math.h
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
#pragma once
#include "base.h"
#include <wasm_simd128.h>
union v128 {
f32 Float;
v2 Vector2;
v3 Vector3;
v4 Vector4;
f32x4 Float4;
u32x4 Uint4;
v128_t Register;
inline v128() { Register = wasm_f32x4_const_splat(0.0f); };
inline v128(f32 Value) : Float(Value) { };
inline v128(const v2 &Value) : Vector2(Value) { };
inline v128(const v3 &Value) : Vector3(Value) { };
inline v128(const f32x4 &Value) : Float4(Value) { };
inline v128(const u32x4 &Value) : Uint4(Value) { };
inline v128(const v4 &Value) : Vector4(Value) { };
inline v128(const v128_t &SIMDLane) : Register(SIMDLane) { };
explicit operator f32() const { return Float; }
explicit operator v2() const { return Vector2; }
explicit operator v3() const { return Vector3; }
explicit operator v4() const { return Vector4; }
explicit operator f32x4() const { return Float4; }
explicit operator u32x4() const { return Uint4; }
operator v128_t() const { return Register; }
MATHCALL v128 CreateMask(bool Value) {
v128 Result = wasm_u32x4_splat((u32)Value * -1);
return Result;
}
};
inline f32x4::f32x4(const u32x4 &V) {
v128 Result = wasm_f32x4_make(V[0], V[1], V[2], V[3]); // wasm_convert ?
*this = (f32x4)Result;
}
static inline f32 SquareRoot(f32 Value) {
return __builtin_sqrt(Value);
}
MATHCALL f32 Abs(f32 Value) {
if (Value < 0.0f) return -Value;
return Value;
}
static inline f32 Max(f32 A, f32 B) {
return __builtin_wasm_max_f32(A, B);
}
static inline f32 Min(f32 A, f32 B) {
return __builtin_wasm_min_f32(A, B);
}
MATHCALL u32 Min(u32 a, u32 b) {
u32 DOZ = (a - b) & -(a >= b);
u32 Result = a - DOZ;
return Result;
}
MATHCALL f32 Negate(f32 Value) {
return Value * -1.0f; // f32.neg
}
static inline f32 Sign(f32 Value) {
f32 Result = __builtin_copysign(1.0f, Value);
return Result;
}
MATHCALL f32 Reciprocal(f32 Value) {
return 1.0f / Value;
}
MATHCALL f32 Saturate(f32 Value) {
if (Value < 0.0f) return 0.0f;
if (Value > 1.0f) return 1.0f;
return Value;
}
MATHCALL f32 FMA(f32 A, f32 B, f32 C) {
return A*B + C;
}
inline v2::v2(f32 X) {
v128 Value = wasm_f32x4_splat(X);
*this = (v2)Value;
}
inline v3::v3(const f32 &X) {
v128 Value = wasm_f32x4_splat(X);
*this = (v3)Value;
}
inline v4::v4(f32 X) {
v128 Value = wasm_f32x4_splat(X);
*this = (v4)Value;
}
inline v2::v2(f32 X, f32 Y) {
v128 Value = wasm_f32x4_make(X, Y, 0.0f, 0.0f);
*this = (v2)Value;
}
inline v3::v3(const f32 &X, const f32 &Y, const f32 &Z) {
v128 Value = wasm_f32x4_make(X, Y, Z, 0.0f);
*this = (v3)Value;
}
inline v4::v4(f32 X, f32 Y, f32 Z, f32 W) {
v128 Value = wasm_f32x4_make(X, Y, Z, W);
*this = (v4)Value;
}
MATHCALL u32 RoundNearestInt(f32 a) {
return __builtin_nearbyintf(a);
}
MATHCALL u64 RoundNearestInt(f64 a) {
return __builtin_nearbyint(a);
}
MATHCALL u32 PopCount(u32 a) {
return __builtin_popcount(a);
}
MATHCALL u64 PopCount(u64 a) {
return __builtin_popcount(a);
}
MATHCALL u32 RotateRight32(u32 Value, s32 Rotation) {
return __builtin_rotateright32(Value, Rotation);
}
MATHCALL u64 RotateRight64(u64 Value, s32 Rotation) {
return __builtin_rotateright64(Value, Rotation);
}
MATHCALL u32 RotateLeft32(u32 Value, s32 Rotation) {
return __builtin_rotateleft32(Value, Rotation);
}
MATHCALL u64 RotateLeft64(u64 Value, s32 Rotation) {
return __builtin_rotateleft64(Value, Rotation);
}
MATHCALL v2 operator+(const v2 &A, const v2 &B) {
v128 Result = wasm_f32x4_add(v128(A), v128(B));
return (v2)Result;
}
MATHCALL v2 operator-(const v2 &A, const v2 &B) {
v128 Result = wasm_f32x4_sub(v128(A), v128(B));
return (v2)Result;
}
MATHCALL v2 operator*(const v2 &A, const v2 &B) {
v128 Result = wasm_f32x4_mul(v128(A), v128(B));
return (v2)Result;
}
MATHCALL v2 operator/(const v2 &A, const v2 &B) {
v128 Result = wasm_f32x4_div(v128(A), v128(B));
return (v2)Result;
}
inline f32 v3::Dot(const v3 &A, const v3 &B) {
v3 Mul = A * B;
return Mul.x + Mul.y + Mul.z;
}
inline f32 v3::LengthSquared(const v3 &Value) {
return v3::Dot(Value, Value);
}
inline f32 v3::Length(const v3 &Value) {
return SquareRoot(v3::LengthSquared(Value));
}
inline v3 v3::Normalize(const v3 &Value) {
f32 LengthSquared = v3::LengthSquared(Value);
bool LengthGreaterThanZero = LengthSquared > F32Epsilon;
v128 Mask = v128::CreateMask(LengthGreaterThanZero);
f32 Length = SquareRoot(LengthSquared);
v3 Result = Value / Length;
v128 MaskedResult = wasm_v128_and(v128(Result), Mask);
return (v3)MaskedResult;
}
inline v3 v3::NormalizeFast(const v3 &Value) {
return v3::Normalize(Value);
}
MATHCALL v3 operator+(const v3 &A, const v3 &B) {
v128 Result = wasm_f32x4_add(v128(A), v128(B));
return (v3)Result;
}
MATHCALL v3 operator-(const v3 &A, const v3 &B) {
v128 Result = wasm_f32x4_sub(v128(A), v128(B));
return (v3)Result;
}
MATHCALL v3 operator*(const v3 &A, const v3 &B) {
v128 Result = wasm_f32x4_mul(v128(A), v128(B));
return (v3)Result;
}
MATHCALL v3 operator/(const v3 &A, const v3 &B) {
v128 Result = wasm_f32x4_div(v128(A), v128(B));
return (v3)Result;
}
MATHCALL v3 operator-(const v3 &A) {
v128 Result = wasm_f32x4_neg(v128(A));
return (v3)Result;
}
MATHCALL v4 operator+(const v4 &A, const v4 &B) {
v128 Result = wasm_f32x4_add(v128(A), v128(B));
return (v4)Result;
}
MATHCALL v4 operator-(const v4 &A, const v4 &B) {
v128 Result = wasm_f32x4_sub(v128(A), v128(B));
return (v4)Result;
}
MATHCALL v4 operator*(const v4 &A, const v4 &B) {
v128 Result = wasm_f32x4_mul(v128(A), v128(B));
return (v4)Result;
}
MATHCALL v4 operator/(const v4 &A, const v4 &B) {
v128 Result = wasm_f32x4_div(v128(A), v128(B));
return (v4)Result;
}
inline v3 v3::Cross(const v3 &A, const v3 &B) {
v3 Result;
Result.x = A.y * B.z - A.z * B.y;
Result.y = A.z * B.x - A.x * B.z;
Result.z = A.x * B.y - A.y * B.x;
return Result;
}
MATHCALL u32 RoundUpPowerOf2(u32 Value, u32 Power2) {
Assert(PopCount(Power2) == 1);
u32 Result = Value;
u32 Mask = Power2 - 1;
Result += Mask;
Result &= ~Mask;
return Result;
}
MATHCALL u64 RoundUpPowerOf2(u64 Value, u64 Power2) {
Assert(PopCount(Power2) == 1);
u64 Result = Value;
u64 Mask = Power2 - 1;
Result += Mask;
Result &= ~Mask;
return Result;
}
inline f32x v3x::Dot(const v3x &A, const v3x &B) {
v3x C = A * B;
f32x Result = C.x + C.y + C.z;
return Result;
}
inline f32x v3x::Length(const v3x &A) {
f32x LengthSquared = v3x::Dot(A, A);
f32x Length = f32x::SquareRoot(LengthSquared);
return Length;
}
static inline f32x LengthSquared(const v3x &A) {
v3x C = A * A;
f32x Result = C.x + C.y + C.z;
return Result;
}
inline f32x v3x::LengthSquared(const v3x &A) {
v3x C = A * A;
f32x Result = C.x + C.y + C.z;
return Result;
}
inline v3x v3x::Normalize(const v3x &Value) {
f32x LengthSquared = v3x::LengthSquared(Value);
f32x LengthGreaterThanZeroMask = LengthSquared > F32Epsilon;
f32x Length = f32x::SquareRoot(LengthSquared);
v3x Result = Value / Length;
v3x MaskedResult = Result & LengthGreaterThanZeroMask;
return MaskedResult;
}
inline v3x v3x::NormalizeFast(const v3x &Value) {
return v3x::Normalize(Value);
}
inline void v3x::ConditionalMove(v3x *A, const v3x &B, const f32x &MoveMask) {
f32x::ConditionalMove(&A->x, B.x, MoveMask);
f32x::ConditionalMove(&A->y, B.y, MoveMask);
f32x::ConditionalMove(&A->z, B.z, MoveMask);
}
inline f32 f32x4::HorizontalMin(const f32x4 &Value) {
v128 UpperHalf = wasm_f32x4_make(Value[2], Value[3], 0, 0);
v128 Result = wasm_f32x4_min(v128(Value), UpperHalf);
Result = wasm_f32x4_min(Result, wasm_f32x4_make(Result.Float4[1], 0, 0, 0));
return (f32)Result;
}
inline u32 f32x4::HorizontalMinIndex(const f32x4 &Value) {
f32 MinValue = f32x4::HorizontalMin(Value);
return f32x4::FindFirstIndex(Value, MinValue);
}
inline u32 f32x4::FindFirstIndex(const f32x4 &A, f32 Value) {
v128 Comparison = A == f32x4(Value);
u32 MoveMask = wasm_i32x4_bitmask(Comparison);
u32 Result = __builtin_ctz(MoveMask);
return Result;
}
inline f32 f32x4::Extract(u32 Index) {
u32 I1 = Index * 4 + 0;
u32 I2 = Index * 4 + 1;
u32 I3 = Index * 4 + 2;
u32 I4 = Index * 4 + 3;
v128_t SwizzleIndices = wasm_i8x16_make(
I1, I2, I3, I4,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0
);
v128_t ResultVector = wasm_i8x16_swizzle(v128(*this), SwizzleIndices);
f32 Result = wasm_f32x4_extract_lane(ResultVector, 0);
return Result;
}
inline u32 u32x4::Extract(u32 Index) {
u32 I1 = Index * 4 + 0;
u32 I2 = Index * 4 + 1;
u32 I3 = Index * 4 + 2;
u32 I4 = Index * 4 + 3;
v128_t SwizzleIndices = wasm_i8x16_make(
I1, I2, I3, I4,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0
);
v128_t ResultVector = wasm_i8x16_swizzle(v128(*this), SwizzleIndices);
u32 Result = wasm_u32x4_extract_lane(ResultVector, 0);
return Result;
}
MATHCALL f32x4 operator+(const f32x4 &A, const f32x4 &B) {
v128 Result = wasm_f32x4_add(v128(A), v128(B));
return (f32x4)Result;
}
MATHCALL f32x4 operator-(const f32x4 &A, const f32x4 &B) {
v128 Result = wasm_f32x4_sub(v128(A), v128(B));
return (f32x4)Result;
}
MATHCALL f32x4 operator*(const f32x4 &A, const f32x4 &B) {
v128 Result = wasm_f32x4_mul(v128(A), v128(B));
return (f32x4)Result;
}
MATHCALL f32x4 operator/(const f32x4 &A, const f32x4 &B) {
v128 Result = wasm_f32x4_div(v128(A), v128(B));
return (f32x4)Result;
}
inline f32x4 f32x4::SquareRoot(const f32x4 &A) {
v128 Result = wasm_f32x4_sqrt(v128(A));
return (f32x4)Result;
}
inline f32x4 f32x4::Min(const f32x4 &A, const f32x4 &B) {
v128 Result = wasm_f32x4_min(v128(A), v128(B));
return (f32x4)Result;
}
inline f32x4 f32x4::Max(const f32x4 &A, const f32x4 &B) {
v128 Result = wasm_f32x4_max(v128(A), v128(B));
return (f32x4)Result;
}
inline f32x4 f32x4::Reciprocal(const f32x4 &A) {
return f32x4(1.0f) / A;
}
inline void f32x4::ConditionalMove(f32x4 *A, const f32x4 &B, const f32x4 &MoveMask) {
v128 Result = wasm_v128_bitselect(v128(B), v128(*A), v128(MoveMask));
*A = (f32x4)Result;
}
MATHCALL f32x4 operator==(const f32x4 &A, const f32x4 &B) {
v128 Result = wasm_f32x4_eq(v128(A), v128(B));
return (f32x4)Result;
}
MATHCALL f32x4 operator!=(const f32x4 &A, const f32x4 &B) {
v128 Result = wasm_f32x4_ne(v128(A), v128(B));
return (f32x4)Result;
}
MATHCALL f32x4 operator>(const f32x4 &A, const f32x4 &B) {
v128 Result = wasm_f32x4_gt(v128(A), v128(B));
return (f32x4)Result;
}
MATHCALL f32x4 operator<(const f32x4 &A, const f32x4 &B) {
v128 Result = wasm_f32x4_lt(v128(A), v128(B));
return (f32x4)Result;
}
MATHCALL f32x4 operator&(const f32x4 &A, const f32x4 &B) {
v128 Result = wasm_v128_and(v128(A), v128(B));
return (f32x4)Result;
}
MATHCALL f32x4 operator|(const f32x4 &A, const f32x4 &B) {
v128 Result = wasm_v128_or(v128(A), v128(B));
return (f32x4)Result;
}
MATHCALL f32x4 operator^(const f32x4 &A, const f32x4 &B) {
v128 Result = wasm_v128_xor(v128(A), v128(B));
return (f32x4)Result;
}
MATHCALL bool IsZero(const f32x4 &Value) {
v128 ComparisonResult = wasm_f32x4_eq(v128(Value), wasm_f32x4_const_splat(0.0f));
bool Result = wasm_i32x4_all_true(ComparisonResult);
return Result;
}
MATHCALL u32x4 operator+(const u32x4 &A, const u32x4 &B) {
v128 Result = wasm_i32x4_add(v128(A), v128(B));
return (u32x4)Result;
}
MATHCALL u32x4 operator-(const u32x4 &A, const u32x4 &B) {
v128 Result = wasm_i32x4_sub(v128(A), v128(B));
return (u32x4)Result;
}
MATHCALL u32x4 operator*(const u32x4 &A, const u32x4 &B) {
v128 Result = wasm_i32x4_mul(v128(A), v128(B));
return (u32x4)Result;
}
MATHCALL u32x4 operator==(const u32x4 &A, const u32x4 &B) {
v128 Result = wasm_i32x4_eq(v128(A), v128(B));
return (u32x4)Result;
}
MATHCALL u32x4 operator!=(const u32x4 &A, const u32x4 &B) {
v128 Result = wasm_i32x4_ne(v128(A), v128(B));
return (u32x4)Result;
}
MATHCALL u32x4 operator>(const u32x4 &A, const u32x4 &B) {
v128 Result = wasm_u32x4_gt(v128(A), v128(B));
return (u32x4)Result;
}
MATHCALL u32x4 operator<(const u32x4 &A, const u32x4 &B) {
v128 Result = wasm_u32x4_lt(v128(A), v128(B));
return (u32x4)Result;
}
MATHCALL u32x4 operator&(const u32x4 &A, const u32x4 &B) {
v128 Result = wasm_v128_and(v128(A), v128(B));
return (u32x4)Result;
}
MATHCALL u32x4 operator|(const u32x4 &A, const u32x4 &B) {
v128 Result = wasm_v128_or(v128(A), v128(B));
return (u32x4)Result;
}
MATHCALL u32x4 operator^(const u32x4 &A, const u32x4 &B) {
v128 Result = wasm_v128_xor(v128(A), v128(B));
return (u32x4)Result;
}
MATHCALL u32x4 operator~(const u32x4 &A) {
v128 Result = wasm_i32x4_neg(v128(A));
return (u32x4)Result;
}
MATHCALL u32x4 operator>>(const u32x4 &A, const u32x4 &B) {
u32x4 Result;
Result[0] = A[0] >> B[0];
Result[1] = A[1] >> B[1];
Result[2] = A[2] >> B[2];
Result[3] = A[3] >> B[3];
return (u32x4)Result;
}
MATHCALL u32x4 operator<<(const u32x4 &A, const u32x4 &B) {
u32x4 Result;
Result[0] = A[0] >> B[0];
Result[1] = A[1] >> B[1];
Result[2] = A[2] >> B[2];
Result[3] = A[3] >> B[3];
return (u32x4)Result;
}
MATHCALL u32x4 operator>>(const u32x4 &A, const u32 &&B) {
v128 Result = wasm_u32x4_shr(v128(A), B);
return (u32x4)Result;
}
MATHCALL u32x4 operator<<(const u32x4 &A, const u32 &&B) {
v128 Result = wasm_i32x4_shr(v128(A), B);
return (u32x4)Result;
}
inline void u32x4::ConditionalMove(u32x4 *A, const u32x4 &B, const u32x4 &MoveMask) {
v128 Result = wasm_v128_bitselect(v128(B), v128(*A), v128(MoveMask));
*A = (u32x4)Result;
}
#include <math.h>
static inline f32 Cosine(f32 radians) {
return cosf(radians);
}
static inline f32 Sin(f32 radians) {
return sinf(radians);
}