-
Notifications
You must be signed in to change notification settings - Fork 17
/
nudge.cpp
4928 lines (3774 loc) · 176 KB
/
nudge.cpp
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright (c) 2017 Rasmus Barringer
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#include "nudge.h"
#include <assert.h>
#include <immintrin.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#ifdef _WIN32
#include <intrin.h>
#define NUDGE_ALIGNED(n) __declspec(align(n))
#define NUDGE_FORCEINLINE __forceinline
#else
#define NUDGE_ALIGNED(n) __attribute__((aligned(n)))
#define NUDGE_FORCEINLINE inline __attribute__((always_inline))
#endif
#ifdef __AVX2__
#define NUDGE_SIMDV_WIDTH 256
#else
#define NUDGE_SIMDV_WIDTH 128
#endif
#define NUDGE_ARENA_SCOPE(A) Arena& scope_arena_##A = A; Arena A = scope_arena_##A
namespace nudge {
static const float allowed_penetration = 1e-3f;
static const float bias_factor = 2.0f;
#if NUDGE_SIMDV_WIDTH == 128
#define NUDGE_SIMDV_ALIGNED NUDGE_ALIGNED(16)
static const unsigned simdv_width32 = 4;
static const unsigned simdv_width32_log2 = 2;
#elif NUDGE_SIMDV_WIDTH == 256
#define NUDGE_SIMDV_ALIGNED NUDGE_ALIGNED(32)
static const unsigned simdv_width32 = 8;
static const unsigned simdv_width32_log2 = 3;
#endif
#ifdef _WIN32
NUDGE_FORCEINLINE __m128 operator - (__m128 a) {
return _mm_xor_ps(a, _mm_set1_ps(-0.0f));
}
NUDGE_FORCEINLINE __m128 operator + (__m128 a, __m128 b) {
return _mm_add_ps(a, b);
}
NUDGE_FORCEINLINE __m128 operator - (__m128 a, __m128 b) {
return _mm_sub_ps(a, b);
}
NUDGE_FORCEINLINE __m128 operator * (__m128 a, __m128 b) {
return _mm_mul_ps(a, b);
}
NUDGE_FORCEINLINE __m128 operator / (__m128 a, __m128 b) {
return _mm_div_ps(a, b);
}
NUDGE_FORCEINLINE __m128& operator += (__m128& a, __m128 b) {
return a = _mm_add_ps(a, b);
}
NUDGE_FORCEINLINE __m128& operator -= (__m128& a, __m128 b) {
return a = _mm_sub_ps(a, b);
}
NUDGE_FORCEINLINE __m128& operator *= (__m128& a, __m128 b) {
return a = _mm_mul_ps(a, b);
}
NUDGE_FORCEINLINE __m128& operator /= (__m128& a, __m128 b) {
return a = _mm_div_ps(a, b);
}
#ifdef __AVX2__
NUDGE_FORCEINLINE __m256 operator - (__m256 a) {
return _mm256_xor_ps(a, _mm256_set1_ps(-0.0f));
}
NUDGE_FORCEINLINE __m256 operator + (__m256 a, __m256 b) {
return _mm256_add_ps(a, b);
}
NUDGE_FORCEINLINE __m256 operator - (__m256 a, __m256 b) {
return _mm256_sub_ps(a, b);
}
NUDGE_FORCEINLINE __m256 operator * (__m256 a, __m256 b) {
return _mm256_mul_ps(a, b);
}
NUDGE_FORCEINLINE __m256 operator / (__m256 a, __m256 b) {
return _mm256_div_ps(a, b);
}
NUDGE_FORCEINLINE __m256& operator += (__m256& a, __m256 b) {
return a = _mm256_add_ps(a, b);
}
NUDGE_FORCEINLINE __m256& operator -= (__m256& a, __m256 b) {
return a = _mm256_sub_ps(a, b);
}
NUDGE_FORCEINLINE __m256& operator *= (__m256& a, __m256 b) {
return a = _mm256_mul_ps(a, b);
}
NUDGE_FORCEINLINE __m256& operator /= (__m256& a, __m256 b) {
return a = _mm256_div_ps(a, b);
}
#endif
#endif
typedef __m128 simd4_float;
typedef __m128i simd4_int32;
namespace simd128 {
NUDGE_FORCEINLINE __m128 unpacklo32(__m128 x, __m128 y) {
return _mm_unpacklo_ps(x, y);
}
NUDGE_FORCEINLINE __m128 unpackhi32(__m128 x, __m128 y) {
return _mm_unpackhi_ps(x, y);
}
NUDGE_FORCEINLINE __m128i unpacklo32(__m128i x, __m128i y) {
return _mm_unpacklo_epi32(x, y);
}
NUDGE_FORCEINLINE __m128i unpackhi32(__m128i x, __m128i y) {
return _mm_unpackhi_epi32(x, y);
}
template<unsigned x0, unsigned x1, unsigned y0, unsigned y1>
NUDGE_FORCEINLINE __m128 concat2x32(__m128 x, __m128 y) {
return _mm_shuffle_ps(x, y, _MM_SHUFFLE(y1, y0, x1, x0));
}
template<unsigned i0, unsigned i1, unsigned i2, unsigned i3>
NUDGE_FORCEINLINE __m128 shuffle32(__m128 x) {
return _mm_shuffle_ps(x, x, _MM_SHUFFLE(i3, i2, i1, i0));
}
template<unsigned i0, unsigned i1, unsigned i2, unsigned i3>
NUDGE_FORCEINLINE __m128i shuffle32(__m128i x) {
return _mm_shuffle_epi32(x, _MM_SHUFFLE(i3, i2, i1, i0));
}
NUDGE_FORCEINLINE void transpose32(simd4_float& x, simd4_float& y, simd4_float& z, simd4_float& w) {
_MM_TRANSPOSE4_PS(x, y, z, w);
}
}
namespace simd {
NUDGE_FORCEINLINE unsigned signmask32(__m128 x) {
return _mm_movemask_ps(x);
}
NUDGE_FORCEINLINE unsigned signmask32(__m128i x) {
return _mm_movemask_ps(_mm_castsi128_ps(x));
}
NUDGE_FORCEINLINE __m128 bitwise_xor(__m128 x, __m128 y) {
return _mm_xor_ps(x, y);
}
NUDGE_FORCEINLINE __m128 bitwise_or(__m128 x, __m128 y) {
return _mm_or_ps(x, y);
}
NUDGE_FORCEINLINE __m128 bitwise_and(__m128 x, __m128 y) {
return _mm_and_ps(x, y);
}
NUDGE_FORCEINLINE __m128 bitwise_notand(__m128 x, __m128 y) {
return _mm_andnot_ps(x, y);
}
NUDGE_FORCEINLINE __m128i bitwise_xor(__m128i x, __m128i y) {
return _mm_xor_si128(x, y);
}
NUDGE_FORCEINLINE __m128i bitwise_or(__m128i x, __m128i y) {
return _mm_or_si128(x, y);
}
NUDGE_FORCEINLINE __m128i bitwise_and(__m128i x, __m128i y) {
return _mm_and_si128(x, y);
}
NUDGE_FORCEINLINE __m128i bitwise_notand(__m128i x, __m128i y) {
return _mm_andnot_si128(x, y);
}
NUDGE_FORCEINLINE __m128 blendv32(__m128 x, __m128 y, __m128 s) {
#if defined(__SSE4_1__) || defined(__AVX__)
#define NUDGE_NATIVE_BLENDV32
return _mm_blendv_ps(x, y, s);
#else
s = _mm_castsi128_ps(_mm_srai_epi32(_mm_castps_si128(s), 31));
return _mm_or_ps(_mm_andnot_ps(s, x), _mm_and_ps(s, y));
#endif
}
NUDGE_FORCEINLINE __m128i blendv32(__m128i x, __m128i y, __m128i s) {
return _mm_castps_si128(blendv32(_mm_castsi128_ps(x), _mm_castsi128_ps(y), _mm_castsi128_ps(s)));
}
}
namespace simd_float {
NUDGE_FORCEINLINE float extract_first_float(simd4_float x) {
return _mm_cvtss_f32(x);
}
NUDGE_FORCEINLINE simd4_float zero4() {
return _mm_setzero_ps();
}
NUDGE_FORCEINLINE simd4_float make4(float x) {
return _mm_set1_ps(x);
}
NUDGE_FORCEINLINE simd4_float make4(float x, float y, float z, float w) {
return _mm_setr_ps(x, y, z, w);
}
NUDGE_FORCEINLINE simd4_float broadcast_load4(const float* p) {
return _mm_set1_ps(*p);
}
NUDGE_FORCEINLINE simd4_float load4(const float* p) {
return _mm_load_ps(p);
}
NUDGE_FORCEINLINE simd4_float loadu4(const float* p) {
return _mm_loadu_ps(p);
}
NUDGE_FORCEINLINE void store4(float* p, simd4_float x) {
_mm_store_ps(p, x);
}
NUDGE_FORCEINLINE void storeu4(float* p, simd4_float x) {
_mm_storeu_ps(p, x);
}
NUDGE_FORCEINLINE simd4_float madd(simd4_float x, simd4_float y, simd4_float z) {
#ifdef __FMA__
return _mm_fmadd_ps(x, y, z);
#else
return _mm_add_ps(_mm_mul_ps(x, y), z);
#endif
}
NUDGE_FORCEINLINE simd4_float msub(simd4_float x, simd4_float y, simd4_float z) {
#ifdef __FMA__
return _mm_fmsub_ps(x, y, z);
#else
return _mm_sub_ps(_mm_mul_ps(x, y), z);
#endif
}
// Note: First operand is returned on NaN.
NUDGE_FORCEINLINE simd4_float min(simd4_float x, simd4_float y) {
return _mm_min_ps(y, x); // Note: For SSE, second operand is returned on NaN.
}
// Note: First operand is returned on NaN.
NUDGE_FORCEINLINE simd4_float max(simd4_float x, simd4_float y) {
return _mm_max_ps(y, x); // Note: For SSE, second operand is returned on NaN.
}
NUDGE_FORCEINLINE simd4_float rsqrt(simd4_float x) {
return _mm_rsqrt_ps(x);
}
NUDGE_FORCEINLINE simd4_float recip(simd4_float x) {
return _mm_rcp_ps(x);
}
NUDGE_FORCEINLINE simd4_float sqrt(simd4_float x) {
return _mm_sqrt_ps(x);
}
NUDGE_FORCEINLINE simd4_float abs(simd4_float x) {
return _mm_andnot_ps(_mm_set1_ps(-0.0f), x);
}
NUDGE_FORCEINLINE simd4_float cmp_gt(simd4_float x, simd4_float y) {
return _mm_cmpgt_ps(x, y);
}
NUDGE_FORCEINLINE simd4_float cmp_ge(simd4_float x, simd4_float y) {
return _mm_cmpge_ps(x, y);
}
NUDGE_FORCEINLINE simd4_float cmp_le(simd4_float x, simd4_float y) {
return _mm_cmple_ps(x, y);
}
NUDGE_FORCEINLINE simd4_float cmp_eq(simd4_float x, simd4_float y) {
return _mm_cmpeq_ps(x, y);
}
NUDGE_FORCEINLINE simd4_float cmp_neq(simd4_float x, simd4_float y) {
return _mm_cmpneq_ps(x, y);
}
NUDGE_FORCEINLINE simd4_int32 asint(simd4_float x) {
return _mm_castps_si128(x);
}
NUDGE_FORCEINLINE simd4_int32 toint(simd4_float x) {
return _mm_cvttps_epi32(x);
}
}
namespace simd_int32 {
NUDGE_FORCEINLINE simd4_int32 zero4() {
return _mm_setzero_si128();
}
NUDGE_FORCEINLINE simd4_int32 make4(int32_t x) {
return _mm_set1_epi32(x);
}
NUDGE_FORCEINLINE simd4_int32 make4(int32_t x, int32_t y, int32_t z, int32_t w) {
return _mm_setr_epi32(x, y, z, w);
}
NUDGE_FORCEINLINE simd4_int32 load4(const int32_t* p) {
return _mm_load_si128((const __m128i*)p);
}
NUDGE_FORCEINLINE simd4_int32 loadu4(const int32_t* p) {
return _mm_loadu_si128((const __m128i*)p);
}
NUDGE_FORCEINLINE void store4(int32_t* p, simd4_int32 x) {
_mm_store_si128((__m128i*)p, x);
}
NUDGE_FORCEINLINE void storeu4(int32_t* p, simd4_int32 x) {
_mm_storeu_si128((__m128i*)p, x);
}
template<unsigned bits>
NUDGE_FORCEINLINE simd4_int32 shift_left(simd4_int32 x) {
return _mm_slli_epi32(x, bits);
}
template<unsigned bits>
NUDGE_FORCEINLINE simd4_int32 shift_right(simd4_int32 x) {
return _mm_srli_epi32(x, bits);
}
NUDGE_FORCEINLINE simd4_int32 add(simd4_int32 x, simd4_int32 y) {
return _mm_add_epi32(x, y);
}
NUDGE_FORCEINLINE simd4_int32 cmp_eq(simd4_int32 x, simd4_int32 y) {
return _mm_cmpeq_epi32(x, y);
}
NUDGE_FORCEINLINE simd4_float asfloat(simd4_int32 x) {
return _mm_castsi128_ps(x);
}
}
#ifdef __AVX2__
typedef __m256 simd8_float;
typedef __m256i simd8_int32;
namespace simd128 {
NUDGE_FORCEINLINE __m256 unpacklo32(__m256 x, __m256 y) {
return _mm256_unpacklo_ps(x, y);
}
NUDGE_FORCEINLINE __m256 unpackhi32(__m256 x, __m256 y) {
return _mm256_unpackhi_ps(x, y);
}
NUDGE_FORCEINLINE __m256i unpacklo32(__m256i x, __m256i y) {
return _mm256_unpacklo_epi32(x, y);
}
NUDGE_FORCEINLINE __m256i unpackhi32(__m256i x, __m256i y) {
return _mm256_unpackhi_epi32(x, y);
}
template<unsigned x0, unsigned x1, unsigned y0, unsigned y1>
NUDGE_FORCEINLINE __m256 concat2x32(__m256 x, __m256 y) {
return _mm256_shuffle_ps(x, y, _MM_SHUFFLE(y1, y0, x1, x0));
}
template<unsigned i0, unsigned i1, unsigned i2, unsigned i3>
NUDGE_FORCEINLINE __m256 shuffle32(__m256 x) {
return _mm256_shuffle_ps(x, x, _MM_SHUFFLE(i3, i2, i1, i0));
}
template<unsigned i0, unsigned i1, unsigned i2, unsigned i3>
NUDGE_FORCEINLINE __m256i shuffle32(__m256i x) {
return _mm256_shuffle_epi32(x, _MM_SHUFFLE(i3, i2, i1, i0));
}
NUDGE_FORCEINLINE void transpose32(simd8_float& x, simd8_float& y, simd8_float& z, simd8_float& w) {
__m256 t0 = _mm256_unpacklo_ps(x, y);
__m256 t1 = _mm256_unpacklo_ps(z, w);
__m256 t2 = _mm256_unpackhi_ps(x, y);
__m256 t3 = _mm256_unpackhi_ps(z, w);
x = _mm256_shuffle_ps(t0, t1, _MM_SHUFFLE(1,0,1,0));
y = _mm256_shuffle_ps(t0, t1, _MM_SHUFFLE(3,2,3,2));
z = _mm256_shuffle_ps(t2, t3, _MM_SHUFFLE(1,0,1,0));
w = _mm256_shuffle_ps(t2, t3, _MM_SHUFFLE(3,2,3,2));
}
}
namespace simd256 {
template<unsigned i0, unsigned i1>
NUDGE_FORCEINLINE simd8_float permute128(simd8_float x, simd8_float y) {
return _mm256_castsi256_ps(_mm256_permute2x128_si256(_mm256_castps_si256(x), _mm256_castps_si256(y), i0 | (i1 << 4)));
}
template<unsigned i0, unsigned i1>
NUDGE_FORCEINLINE simd8_int32 permute128(simd8_int32 x, simd8_int32 y) {
return _mm256_permute2x128_si256(x, y, i0 | (i1 << 4));
}
template<unsigned i0, unsigned i1>
NUDGE_FORCEINLINE simd8_float shuffle128(simd8_float x) {
return _mm256_castsi256_ps(_mm256_permute2x128_si256(_mm256_castps_si256(x), _mm256_castps_si256(x), i0 | (i1 << 4)));
}
template<unsigned i0, unsigned i1>
NUDGE_FORCEINLINE simd8_int32 shuffle128(simd8_int32 x) {
return _mm256_permute2x128_si256(x, x, i0 | (i1 << 4));
}
NUDGE_FORCEINLINE simd8_float broadcast(simd4_float x) {
return _mm256_insertf128_ps(_mm256_castps128_ps256(x), x, 1);
}
NUDGE_FORCEINLINE simd8_int32 broadcast(simd4_int32 x) {
return _mm256_insertf128_si256(_mm256_castsi128_si256(x), x, 1);
}
}
namespace simd {
NUDGE_FORCEINLINE simd8_float concat(simd4_float x, simd4_float y) {
return _mm256_insertf128_ps(_mm256_castps128_ps256(x), y, 1);
}
NUDGE_FORCEINLINE simd4_float extract_low(simd8_float x) {
return _mm256_castps256_ps128(x);
}
NUDGE_FORCEINLINE simd4_float extract_high(simd8_float x) {
return _mm256_extractf128_ps(x, 1);
}
NUDGE_FORCEINLINE simd4_int32 extract_low(simd8_int32 x) {
return _mm256_castsi256_si128(x);
}
NUDGE_FORCEINLINE simd4_int32 extract_high(simd8_int32 x) {
return _mm256_extractf128_si256(x, 1);
}
NUDGE_FORCEINLINE unsigned signmask32(__m256 x) {
return _mm256_movemask_ps(x);
}
NUDGE_FORCEINLINE unsigned signmask32(__m256i x) {
return _mm256_movemask_ps(_mm256_castsi256_ps(x));
}
NUDGE_FORCEINLINE __m256 bitwise_xor(__m256 x, __m256 y) {
return _mm256_xor_ps(x, y);
}
NUDGE_FORCEINLINE __m256 bitwise_or(__m256 x, __m256 y) {
return _mm256_or_ps(x, y);
}
NUDGE_FORCEINLINE __m256 bitwise_and(__m256 x, __m256 y) {
return _mm256_and_ps(x, y);
}
NUDGE_FORCEINLINE __m256 bitwise_notand(__m256 x, __m256 y) {
return _mm256_andnot_ps(x, y);
}
NUDGE_FORCEINLINE __m256i bitwise_xor(__m256i x, __m256i y) {
return _mm256_xor_si256(x, y);
}
NUDGE_FORCEINLINE __m256i bitwise_or(__m256i x, __m256i y) {
return _mm256_or_si256(x, y);
}
NUDGE_FORCEINLINE __m256i bitwise_and(__m256i x, __m256i y) {
return _mm256_and_si256(x, y);
}
NUDGE_FORCEINLINE __m256i bitwise_notand(__m256i x, __m256i y) {
return _mm256_andnot_si256(x, y);
}
NUDGE_FORCEINLINE __m256 blendv32(__m256 x, __m256 y, __m256 s) {
return _mm256_blendv_ps(x, y, s);
}
NUDGE_FORCEINLINE __m256i blendv32(__m256i x, __m256i y, __m256i s) {
return _mm256_castps_si256(_mm256_blendv_ps(_mm256_castsi256_ps(x), _mm256_castsi256_ps(y), _mm256_castsi256_ps(s)));
}
}
namespace simd_float {
NUDGE_FORCEINLINE float extract_first_float(simd8_float x) {
return _mm_cvtss_f32(_mm256_castps256_ps128(x));
}
NUDGE_FORCEINLINE simd8_float zero8() {
return _mm256_setzero_ps();
}
NUDGE_FORCEINLINE simd8_float make8(float x) {
return _mm256_set1_ps(x);
}
NUDGE_FORCEINLINE simd8_float make8(float x0, float y0, float z0, float w0, float x1, float y1, float z1, float w1) {
return _mm256_setr_ps(x0, y0, z0, w0, x1, y1, z1, w1);
}
NUDGE_FORCEINLINE simd8_float broadcast_load8(const float* p) {
return _mm256_broadcast_ss(p);
}
NUDGE_FORCEINLINE simd8_float load8(const float* p) {
return _mm256_load_ps(p);
}
NUDGE_FORCEINLINE simd8_float loadu8(const float* p) {
return _mm256_loadu_ps(p);
}
NUDGE_FORCEINLINE void store8(float* p, simd8_float x) {
_mm256_store_ps(p, x);
}
NUDGE_FORCEINLINE void storeu8(float* p, simd8_float x) {
_mm256_storeu_ps(p, x);
}
NUDGE_FORCEINLINE simd8_float madd(simd8_float x, simd8_float y, simd8_float z) {
#ifdef __FMA__
return _mm256_fmadd_ps(x, y, z);
#else
return _mm256_add_ps(_mm256_mul_ps(x, y), z);
#endif
}
NUDGE_FORCEINLINE simd8_float msub(simd8_float x, simd8_float y, simd8_float z) {
#ifdef __FMA__
return _mm256_fmsub_ps(x, y, z);
#else
return _mm256_sub_ps(_mm256_mul_ps(x, y), z);
#endif
}
// Note: First operand is returned on NaN.
NUDGE_FORCEINLINE simd8_float min(simd8_float x, simd8_float y) {
return _mm256_min_ps(y, x); // Note: For SSE, second operand is returned on NaN.
}
// Note: First operand is returned on NaN.
NUDGE_FORCEINLINE simd8_float max(simd8_float x, simd8_float y) {
return _mm256_max_ps(y, x); // Note: For SSE, second operand is returned on NaN.
}
NUDGE_FORCEINLINE simd8_float rsqrt(simd8_float x) {
return _mm256_rsqrt_ps(x);
}
NUDGE_FORCEINLINE simd8_float recip(simd8_float x) {
return _mm256_rcp_ps(x);
}
NUDGE_FORCEINLINE simd8_float sqrt(simd8_float x) {
return _mm256_sqrt_ps(x);
}
NUDGE_FORCEINLINE simd8_float abs(simd8_float x) {
return _mm256_andnot_ps(_mm256_set1_ps(-0.0f), x);
}
NUDGE_FORCEINLINE simd8_float cmp_gt(simd8_float x, simd8_float y) {
return _mm256_cmp_ps(x, y, _CMP_GT_OQ);
}
NUDGE_FORCEINLINE simd8_float cmp_ge(simd8_float x, simd8_float y) {
return _mm256_cmp_ps(x, y, _CMP_GE_OQ);
}
NUDGE_FORCEINLINE simd8_float cmp_le(simd8_float x, simd8_float y) {
return _mm256_cmp_ps(x, y, _CMP_LE_OQ);
}
NUDGE_FORCEINLINE simd8_float cmp_eq(simd8_float x, simd8_float y) {
return _mm256_cmp_ps(x, y, _CMP_EQ_OQ);
}
NUDGE_FORCEINLINE simd8_float cmp_neq(simd8_float x, simd8_float y) {
return _mm256_cmp_ps(x, y, _CMP_NEQ_OQ);
}
NUDGE_FORCEINLINE simd8_int32 asint(simd8_float x) {
return _mm256_castps_si256(x);
}
NUDGE_FORCEINLINE simd8_int32 toint(simd8_float x) {
return _mm256_cvttps_epi32(x);
}
}
namespace simd_int32 {
NUDGE_FORCEINLINE simd8_int32 zero8() {
return _mm256_setzero_si256();
}
NUDGE_FORCEINLINE simd8_int32 make8(int32_t x) {
return _mm256_set1_epi32(x);
}
NUDGE_FORCEINLINE simd8_int32 make8(int32_t x0, int32_t y0, int32_t z0, int32_t w0, int32_t x1, int32_t y1, int32_t z1, int32_t w1) {
return _mm256_setr_epi32(x0, y0, z0, w0, x1, y1, z1, w1);
}
NUDGE_FORCEINLINE simd8_int32 load8(const int32_t* p) {
return _mm256_load_si256((const __m256i*)p);
}
NUDGE_FORCEINLINE simd8_int32 loadu8(const int32_t* p) {
return _mm256_loadu_si256((const __m256i*)p);
}
NUDGE_FORCEINLINE void store8(int32_t* p, simd8_int32 x) {
_mm256_store_si256((__m256i*)p, x);
}
NUDGE_FORCEINLINE void storeu8(int32_t* p, simd8_int32 x) {
_mm256_storeu_si256((__m256i*)p, x);
}
template<unsigned bits>
NUDGE_FORCEINLINE simd8_int32 shift_left(simd8_int32 x) {
return _mm256_slli_epi32(x, bits);
}
template<unsigned bits>
NUDGE_FORCEINLINE simd8_int32 shift_right(simd8_int32 x) {
return _mm256_srli_epi32(x, bits);
}
NUDGE_FORCEINLINE simd8_int32 add(simd8_int32 x, simd8_int32 y) {
return _mm256_add_epi32(x, y);
}
NUDGE_FORCEINLINE simd8_int32 cmp_eq(simd8_int32 x, simd8_int32 y) {
return _mm256_cmpeq_epi32(x, y);
}
NUDGE_FORCEINLINE simd8_float asfloat(simd8_int32 x) {
return _mm256_castsi256_ps(x);
}
}
#endif
#if NUDGE_SIMDV_WIDTH == 128
typedef simd4_float simdv_float;
typedef simd4_int32 simdv_int32;
namespace simd_float {
NUDGE_FORCEINLINE simdv_float zerov() {
return zero4();
}
NUDGE_FORCEINLINE simdv_float makev(float x) {
return make4(x);
}
NUDGE_FORCEINLINE simdv_float broadcast_loadv(const float* p) {
return broadcast_load4(p);
}
NUDGE_FORCEINLINE simdv_float loadv(const float* p) {
return load4(p);
}
NUDGE_FORCEINLINE simdv_float loaduv(const float* p) {
return loadu4(p);
}
NUDGE_FORCEINLINE void storev(float* p, simdv_float x) {
store4(p, x);
}
NUDGE_FORCEINLINE void storeuv(float* p, simdv_float x) {
storeu4(p, x);
}
}
namespace simd_int32 {
NUDGE_FORCEINLINE simdv_int32 zerov() {
return zero4();
}
NUDGE_FORCEINLINE simdv_int32 makev(int32_t x) {
return make4(x);
}
NUDGE_FORCEINLINE simdv_int32 loadv(const int32_t* p) {
return load4(p);
}
NUDGE_FORCEINLINE simdv_int32 loaduv(const int32_t* p) {
return loadu4(p);
}
NUDGE_FORCEINLINE void storev(int32_t* p, simdv_int32 x) {
store4(p, x);
}
NUDGE_FORCEINLINE void storeuv(int32_t* p, simdv_int32 x) {
storeu4(p, x);
}
}
#elif NUDGE_SIMDV_WIDTH == 256
typedef simd8_float simdv_float;
typedef simd8_int32 simdv_int32;
namespace simd_float {
NUDGE_FORCEINLINE simdv_float zerov() {
return zero8();
}
NUDGE_FORCEINLINE simdv_float makev(float x) {
return make8(x);
}
NUDGE_FORCEINLINE simdv_float broadcast_loadv(const float* p) {
return broadcast_load8(p);
}
NUDGE_FORCEINLINE simdv_float loadv(const float* p) {
return load8(p);
}
NUDGE_FORCEINLINE simdv_float loaduv(const float* p) {
return loadu8(p);
}
NUDGE_FORCEINLINE void storev(float* p, simdv_float x) {
store8(p, x);
}
NUDGE_FORCEINLINE void storeuv(float* p, simdv_float x) {
storeu8(p, x);
}
}
namespace simd_int32 {
NUDGE_FORCEINLINE simdv_int32 zerov() {
return zero8();
}
NUDGE_FORCEINLINE simdv_int32 makev(int32_t x) {
return make8(x);
}
NUDGE_FORCEINLINE simdv_int32 loadv(const int32_t* p) {
return load8(p);
}
NUDGE_FORCEINLINE simdv_int32 loaduv(const int32_t* p) {
return loadu8(p);
}
NUDGE_FORCEINLINE void storev(int32_t* p, simdv_int32 x) {
store8(p, x);
}
NUDGE_FORCEINLINE void storeuv(int32_t* p, simdv_int32 x) {
storeu8(p, x);
}
}
#endif
namespace simd_aos {
NUDGE_FORCEINLINE simd4_float dot(simd4_float a, simd4_float b) {
simd4_float c = a*b;
return simd128::shuffle32<0,0,0,0>(c) + simd128::shuffle32<1,1,1,1>(c) + simd128::shuffle32<2,2,2,2>(c);
}
NUDGE_FORCEINLINE simd4_float cross(simd4_float a, simd4_float b) {
simd4_float c = simd128::shuffle32<1,2,0,0>(a) * simd128::shuffle32<2,0,1,0>(b);
simd4_float d = simd128::shuffle32<2,0,1,0>(a) * simd128::shuffle32<1,2,0,0>(b);
return c - d;
}
}
namespace simd_soa {
NUDGE_FORCEINLINE void cross(simd4_float ax, simd4_float ay, simd4_float az, simd4_float bx, simd4_float by, simd4_float bz, simd4_float& rx, simd4_float& ry, simd4_float& rz) {
rx = ay*bz - az*by;
ry = az*bx - ax*bz;
rz = ax*by - ay*bx;
}
NUDGE_FORCEINLINE void normalize(simd4_float& x, simd4_float& y, simd4_float& z) {
simd4_float f = simd_float::rsqrt(x*x + y*y + z*z);
x *= f;
y *= f;
z *= f;
}
#if NUDGE_SIMDV_WIDTH >= 256
NUDGE_FORCEINLINE void cross(simd8_float ax, simd8_float ay, simd8_float az, simd8_float bx, simd8_float by, simd8_float bz, simd8_float& rx, simd8_float& ry, simd8_float& rz) {
rx = ay*bz - az*by;
ry = az*bx - ax*bz;
rz = ax*by - ay*bx;
}
NUDGE_FORCEINLINE void normalize(simd8_float& x, simd8_float& y, simd8_float& z) {
simd8_float f = simd_float::rsqrt(x*x + y*y + z*z);
x *= f;
y *= f;
z *= f;
}
#endif
}
namespace {
struct float3 {
float x, y, z;
};
struct float3x3 {
float3 c0, c1, c2;
};
struct Rotation {
float3 v;
float s;
};
struct AABB {
float3 min;
float unused0;
float3 max;
float unused1;
};
struct AABBV {
float min_x[simdv_width32];
float max_x[simdv_width32];
float min_y[simdv_width32];
float max_y[simdv_width32];
float min_z[simdv_width32];
float max_z[simdv_width32];
};
struct ContactSlotV {
uint32_t indices[simdv_width32];
};
struct ContactPairV {
uint32_t ab[simdv_width32];
};
struct ContactConstraintV {
uint16_t a[simdv_width32];
uint16_t b[simdv_width32];
float pa_z[simdv_width32];
float pa_x[simdv_width32];
float pa_y[simdv_width32];
float pb_z[simdv_width32];
float pb_x[simdv_width32];
float pb_y[simdv_width32];
float n_x[simdv_width32];
float u_x[simdv_width32];
float v_x[simdv_width32];
float n_y[simdv_width32];
float u_y[simdv_width32];
float v_y[simdv_width32];
float n_z[simdv_width32];
float u_z[simdv_width32];
float v_z[simdv_width32];
float bias[simdv_width32];
float friction[simdv_width32];
float normal_velocity_to_normal_impulse[simdv_width32];
float friction_coefficient_x[simdv_width32];
float friction_coefficient_y[simdv_width32];
float friction_coefficient_z[simdv_width32];
float na_x[simdv_width32];
float na_y[simdv_width32];
float na_z[simdv_width32];
float nb_x[simdv_width32];
float nb_y[simdv_width32];
float nb_z[simdv_width32];
float ua_x[simdv_width32];
float ua_y[simdv_width32];
float ua_z[simdv_width32];
float va_x[simdv_width32];
float va_y[simdv_width32];
float va_z[simdv_width32];
float ub_x[simdv_width32];
float ub_y[simdv_width32];
float ub_z[simdv_width32];
float vb_x[simdv_width32];
float vb_y[simdv_width32];
float vb_z[simdv_width32];
};
struct ContactConstraintStateV {
float applied_normal_impulse[simdv_width32];
float applied_friction_impulse_x[simdv_width32];
float applied_friction_impulse_y[simdv_width32];
};
struct InertiaTransform {
float xx;
float yy;
float zz;
float unused0;
float xy;
float xz;
float yz;
float unused1;
};
}
#ifdef _WIN32
static inline unsigned first_set_bit(unsigned x) {
unsigned long r = 0;
_BitScanForward(&r, x);
return r;
}
#else
static inline unsigned first_set_bit(unsigned x) {
return __builtin_ctz(x);
}
#endif
static inline void* align(Arena* arena, uintptr_t alignment) {
uintptr_t data = (uintptr_t)arena->data;
uintptr_t end = data + arena->size;
uintptr_t mask = alignment-1;
data = (data + mask) & ~mask;
arena->data = (void*)data;
arena->size = end - data;
assert((intptr_t)arena->size >= 0); // Out of memory.