-
Notifications
You must be signed in to change notification settings - Fork 0
/
SIMD.h
468 lines (425 loc) · 9.41 KB
/
SIMD.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
#ifndef RR_SIMD
#define RR_SIMD
typedef __m128 f32Wide;
typedef __m128i u32Wide;
static f32Wide Zerof32Wide()
{
return _mm_setzero_ps();
}
static u32Wide ZeroU32Wide()
{
return _mm_setzero_si128();
}
static f32Wide Load(f32 f)
{
return _mm_set1_ps(f);
}
static f32Wide Load(f32 f1, f32 f2, f32 f3, f32 f4)
{
return _mm_setr_ps(f1, f2, f3, f4);
}
static u32Wide Load(u32 u)
{
return _mm_set1_epi32(u);
}
static u32Wide Load(u32 u1, u32 u2, u32 u3, u32 u4)
{
return _mm_setr_epi32(u1, u2, u3, u4);
}
static u32Wide LoadFromArray(u32 *arr)
{
return Load(arr[0], arr[1], arr[2], arr[3]);
}
static f32Wide operator*(f32Wide f1, f32Wide f2)
{
return _mm_mul_ps(f1, f2);
}
static f32Wide operator*(f32Wide f1, f32 f2)
{
return _mm_mul_ps(f1, _mm_set1_ps(f2));
}
static f32Wide operator*(f32 f2, f32Wide f1)
{
return _mm_mul_ps(f1, _mm_set1_ps(f2));
}
static f32Wide CastToF32(u32Wide f)
{
return _mm_cvtepi32_ps(f);
}
static f32Wide LoadU32(u32 i)
{
return CastToF32(Load(i));
}
static f32Wide operator/(f32Wide f1, f32Wide f2)
{
return _mm_div_ps(f1, f2);
}
static f32Wide operator/(f32Wide f1, f32 f2)
{
return _mm_div_ps(f1, Load(f2));
}
static f32Wide operator/(f32 f1, f32Wide f2)
{
return _mm_div_ps(Load(f1), f2);
}
static f32Wide operator+(f32Wide f1, f32Wide f2)
{
return _mm_add_ps(f1, f2);
}
static u32Wide operator+(u32Wide f1, u32Wide f2)
{
return _mm_add_epi32(f1, f2);
}
static f32Wide operator-(f32Wide f1, f32Wide f2)
{
return _mm_sub_ps(f1, f2);
}
static f32Wide operator-(f32Wide f)
{
return (_mm_setzero_ps() - f);
}
static f32Wide Min(f32Wide f1, f32Wide f2)
{
return _mm_min_ps(f1, f2);
}
static f32Wide Max(f32Wide f1, f32Wide f2)
{
return _mm_max_ps(f1, f2);
}
static f32Wide Min(f32Wide f1, f32 f2)
{
return _mm_min_ps(f1, Load(f2));
}
static f32Wide Max(f32Wide f1, f32 f2)
{
return _mm_max_ps(f1, Load(f2));
}
static f32Wide Min(f32 f1, f32Wide f2)
{
return _mm_min_ps(Load(f1), f2);
}
static f32Wide Max(f32 f1, f32Wide f2)
{
return _mm_max_ps(Load(f1), f2);
}
static f32Wide Clamp(f32Wide val, f32Wide min, f32Wide max)
{
return Min(Max(val, min), max);
}
static f32Wide Clamp(f32Wide val, f32 min, f32 max)
{
return Min(Max(val, min), max);
}
static f32Wide Sqrt(f32Wide f1)
{
return _mm_sqrt_ps(f1);
}
static u32Wide operator<(f32Wide f1, f32Wide f2)
{
return _mm_castps_si128(_mm_cmplt_ps(f1, f2));
}
static u32Wide operator>(f32Wide f1, f32Wide f2)
{
return _mm_castps_si128(_mm_cmpgt_ps(f1, f2));
}
static u32Wide operator>=(f32Wide f1, f32Wide f2)
{
return _mm_castps_si128(_mm_cmpge_ps(f1, f2));
}
static u32Wide operator<=(f32Wide f1, f32Wide f2)
{
return _mm_castps_si128(_mm_cmple_ps(f1, f2));
}
static u32Wide operator==(f32Wide f1, f32Wide f2)
{
return _mm_castps_si128(_mm_cmpeq_ps(f1, f2));
}
static u32Wide operator!=(f32Wide f1, f32Wide f2)
{
return _mm_castps_si128(_mm_cmpneq_ps(f1, f2));
}
static u32Wide operator<(u32Wide u1, u32Wide u2)
{
return _mm_cmplt_epi32(u1, u2);
}
static u32Wide operator>(u32Wide u1, u32Wide u2)
{
return _mm_cmpgt_epi32(u1, u2);
}
static u32Wide operator==(u32Wide u1, u32Wide u2)
{
return _mm_cmpeq_epi32(u1, u2);
}
static u32Wide operator^(u32Wide a, u32Wide b)
{
return _mm_xor_si128(a, b);
}
static u32Wide operator&(u32Wide a, u32Wide b)
{
return _mm_and_si128(a, b);
}
//(not b) and a
static u32Wide AndNot(u32Wide a, u32Wide b)
{
return _mm_andnot_si128(b, a);
}
static f32Wide operator&(u32Wide a, f32Wide b)
{
return _mm_and_ps(_mm_castsi128_ps(a), b);
}
static u32Wide operator|(u32Wide a, u32Wide b)
{
return _mm_or_si128(a, b);
}
static u32Wide operator<<(u32Wide a, u32 shift)
{
return _mm_slli_epi32(a, shift);
}
static u32Wide operator>>(u32Wide a, u32 shift)
{
return _mm_srli_epi32(a, shift);
}
struct v3Wide
{
f32Wide x;
f32Wide y;
f32Wide z;
};
static v3Wide ZeroV3Wide()
{
return {Zerof32Wide(), Zerof32Wide(), Zerof32Wide() };
}
static v3Wide Zerov3Wide()
{
return { Zerof32Wide(), Zerof32Wide(), Zerof32Wide() };
}
static v3Wide Load(v3 a)
{
return {Load(a.x), Load(a.y), Load(a.z)};
}
static v3Wide Load(v3 a, v3 b, v3 c, v3 d)
{
v3Wide ret;
ret.x = Load(a.x, b.x, c.x, d.x);
ret.y = Load(a.y, b.y, c.y, d.y);
ret.z = Load(a.z, b.z, c.z, d.z);
return ret;
}
#define LoadFromStruct(str, thing) Load(str[0]->thing, str[1]->thing, str[2]->thing, str[3]->thing)
static v3Wide LoadFromArray(v3 *arr)
{
return Load(arr[0], arr[1], arr[2], arr[3]);
}
static v3Wide V3(f32Wide x, f32Wide y, f32Wide z)
{
v3Wide ret;
ret.x = x;
ret.y = y;
ret.z = z;
return ret;
}
static v3Wide operator*(v3Wide a, f32Wide f)
{
return {_mm_mul_ps(a.x, f), _mm_mul_ps(a.y, f) ,_mm_mul_ps(a.z, f) };
}
static v3Wide operator*(f32Wide f, v3Wide a)
{
/*v3Wide ret;
ret.x = a.x * f;
ret.y = a.y * f;
ret.z = a.z * f;
return ret;*/
return { _mm_mul_ps(a.x, f), _mm_mul_ps(a.y, f) ,_mm_mul_ps(a.z, f) };
}
static v3Wide operator*(v3Wide a, f32 f)
{
v3Wide ret;
ret.x = a.x * f;
ret.y = a.y * f;
ret.z = a.z * f;
return ret;
}
static v3Wide operator*(f32 f, v3Wide a)
{
v3Wide ret;
ret.x = a.x * f;
ret.y = a.y * f;
ret.z = a.z * f;
return ret;
}
static v3Wide operator/(v3Wide a, f32Wide f)
{
v3Wide ret;
ret.x = a.x / f;
ret.y = a.y / f;
ret.z = a.z / f;
return ret;
}
static v3Wide operator*(v3Wide a, v3Wide b)
{
v3Wide ret;
ret.x = a.x * b.x;
ret.y = a.y * b.y;
ret.z = a.z * b.z;
return ret;
}
static v3Wide operator+(v3Wide a, v3Wide b)
{
v3Wide ret;
ret.x = a.x + b.x;
ret.y = a.y + b.y;
ret.z = a.z + b.z;
return ret;
}
static v3Wide operator-(v3Wide a, v3Wide b)
{
v3Wide ret;
ret.x = a.x - b.x;
ret.y = a.y - b.y;
ret.z = a.z - b.z;
return ret;
}
static v3Wide& operator+=(v3Wide& a, v3Wide b)
{
a = a + b;
return a;
}
static v3Wide& operator-=(v3Wide& a, v3Wide b)
{
a = a - b;
return a;
}
static v3Wide& operator*=(v3Wide& a, f32Wide b)
{
a = a * b;
return a;
}
static v3Wide operator-(v3Wide a)
{
v3Wide ret;
ret.x = -a.x;
ret.y = -a.y;
ret.z = -a.z;
return ret;
}
static v3Wide LerpV3Wide(v3Wide a, v3Wide b, f32Wide percent)
{
return (Load(1.0f) - percent) * a + percent * b;
}
static f32Wide Dot(v3Wide a, v3Wide b)
{
return (a.x * b.x + a.y * b.y + a.z * b.z);
}
static f32Wide Norm(v3Wide a)
{
return Sqrt(a.x * a.x + a.y * a.y + a.z * a.z);
}
static void ConditionalAssign(v3Wide *dest, u32Wide mask, v3Wide source)
{
__m128 MaskPS = _mm_castsi128_ps(mask);
dest->x = _mm_or_ps(_mm_andnot_ps(MaskPS, dest->x), _mm_and_ps(MaskPS, source.x));
dest->y = _mm_or_ps(_mm_andnot_ps(MaskPS, dest->y), _mm_and_ps(MaskPS, source.y));
dest->z = _mm_or_ps(_mm_andnot_ps(MaskPS, dest->z), _mm_and_ps(MaskPS, source.z));
}
static void ConditionalAssign(f32Wide *dest, u32Wide mask, f32Wide source)
{
__m128 MaskPS = _mm_castsi128_ps(mask);
*dest = _mm_or_ps(_mm_andnot_ps(MaskPS, *dest), _mm_and_ps(MaskPS, source));
}
static void ConditionalAssign(u32Wide *dest, u32Wide mask, u32Wide source)
{
*dest = _mm_or_si128(_mm_andnot_si128(mask, *dest), _mm_and_si128(mask, source));
}
static v3Wide NOZ(v3Wide a)
{
v3Wide ret = Load(V3(0, 0, 0));
f32Wide norm = Norm(a);
u32Wide mask = (norm > Load(0.0001f));
f32Wide oneDivNorm = (1.0f / norm);
v3Wide normalizedVector = oneDivNorm * a;
ConditionalAssign(&ret, mask, normalizedVector);
return ret;
}
static f32Wide QuadNorm(v3Wide a)
{
return ((a.x * a.x) + (a.y * a.y) + (a.z * a.z));
}
static f32Wide NormSquared(v3Wide a)
{
return ((a.x * a.x) + (a.y * a.y) + (a.z * a.z));
}
static f32Wide QuadDist(v3Wide a, v3Wide b)
{
return QuadNorm(a - b);
}
static v3Wide FastNormalize(v3Wide a)
{
f32Wide quadNorm = QuadNorm(a);
f32Wide inverseNorm = _mm_rsqrt_ps(quadNorm);
return (a * inverseNorm);
}
static v3Wide Unpack3x8(u32Wide *pack)
{
u32Wide OxFF = Load(0xFFu);
f32Wide ignored = CastToF32((*pack >> 24) & OxFF) / 255.0f;
f32Wide r = CastToF32((*pack >> 16) & OxFF) / 255.0f;
f32Wide g = CastToF32((*pack >> 8) & OxFF) / 255.0f;
f32Wide b = CastToF32((*pack >> 0) & OxFF) / 255.0f;
return V3(r, g, b);
}
static f32 Lane(f32Wide f, u32 lane)
{
return ((f32 *)(&f))[lane];
}
static u32 Lane(u32Wide f, u32 lane)
{
return ((u32 *)(&f))[lane];
}
static v3 Lane(v3Wide v, u32 lane)
{
return V3(Lane(v.x, lane), Lane(v.y, lane), Lane(v.z, lane));
}
static f32 SumLanes(f32Wide f)
{
f32 *val = (f32 *)&f;
return (val[0] + val[1] + val[2] + val[3]);
}
static v3 SumLanes(v3Wide f)
{
return { SumLanes(f.x), SumLanes(f.y), SumLanes(f.z) };
}
static bool MaskIsZero(u32Wide a)
{
i32 Mask = _mm_movemask_epi8(a);
return(Mask == 0);
}
static bool AnyTrue(u32Wide a)
{
i32 Mask = _mm_movemask_epi8(a);
return !(Mask == 0);
}
static bool AnyFalse(u32Wide a)
{
i32 Mask = _mm_movemask_epi8(a);
return !(Mask == 0xffff);
}
static bool AllFalse(u32Wide a)
{
i32 Mask = _mm_movemask_epi8(a);
return (Mask == 0);
}
static bool AllTrue(u32Wide a)
{
i32 Mask = _mm_movemask_epi8(a);
return (Mask == 0xffff);
}
static u32 FirstZeroLane(u32Wide a)
{
i32 mask = _mm_movemask_epi8(a);
if (!(mask & 0xf000)) return 0;
if (!(mask & 0xf00)) return 1;
if (!(mask & 0xf0)) return 2;
if (!(mask & 0xf)) return 3;
return 0xFFFFFFFF;
}
#endif