-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargminmax.cpp
More file actions
1044 lines (1012 loc) · 42.7 KB
/
argminmax.cpp
File metadata and controls
1044 lines (1012 loc) · 42.7 KB
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
#include "omp.h"
#include "argminmax.h"
#include "x86intrin.h"
#include "aesctr/wy.h"
#include "ctz.h"
#include "macros.h"
#include "reds.h"
#include <limits>
#include <type_traits>
using namespace reservoir_simd;
// Forward declaratios of core kernels
// Single-sample
template<LoadFormat aln, ArgReduction AR, bool MT> uint64_t double_argsel_fmt(const double *weights, size_t n);
template<LoadFormat aln, ArgReduction AR, bool MT> uint64_t float_argsel_fmt(const float * weights, size_t n);
template<LoadFormat aln, ArgReduction AR, bool MT> ptrdiff_t double_argsel_k_fmt(const double * weights, size_t n, ptrdiff_t k, uint64_t *ret);
template<LoadFormat aln, ArgReduction AR, bool MT> ptrdiff_t float_argsel_k_fmt(const float * weights, size_t n, ptrdiff_t k, uint64_t *ret);
// TODO: add top-k selection methods
extern "C" {
SIMD_SAMPLING_API uint64_t fargsel(const float *weights, size_t n, ArgReduction ar, int mt)
{
const bool aligned = reinterpret_cast<uint64_t>(weights) % LSS_ALIGNMENT == 0;
uint64_t ret;
switch((int(aligned) << 2) | (int(ar == ARGMAX) << 1) | mt) {
case 0: ret = float_argsel_fmt<UNALIGNED, ARGMIN, false>(weights, n); break;
case 1: ret = float_argsel_fmt<UNALIGNED, ARGMIN, true>(weights, n); break;
case 2: ret = float_argsel_fmt<UNALIGNED, ARGMAX, false>(weights, n); break;
case 3: ret = float_argsel_fmt<UNALIGNED, ARGMAX, true>(weights, n); break;
case 4: ret = float_argsel_fmt<ALIGNED, ARGMIN, false>(weights, n); break;
case 5: ret = float_argsel_fmt<ALIGNED, ARGMIN, true>(weights, n); break;
case 6: ret = float_argsel_fmt<ALIGNED, ARGMAX, false>(weights, n); break;
case 7: ret = float_argsel_fmt<ALIGNED, ARGMAX, true>(weights, n); break;
default: __builtin_unreachable();
}
return ret;
}
SIMD_SAMPLING_API uint64_t dargmin_mt(const double *weights, size_t n) {
return reinterpret_cast<uint64_t>(weights) % LSS_ALIGNMENT ?
double_argsel_fmt<UNALIGNED, ARGMIN, true>(weights, n): double_argsel_fmt<ALIGNED, ARGMIN, true>(weights, n);
}
SIMD_SAMPLING_API uint64_t dargmin_st(const double *weights, size_t n) {
return reinterpret_cast<uint64_t>(weights) % LSS_ALIGNMENT ?
double_argsel_fmt<UNALIGNED, ARGMIN, false>(weights, n): double_argsel_fmt<ALIGNED, ARGMIN, false>(weights, n);
}
SIMD_SAMPLING_API uint64_t dargmin(const double *weights, size_t n, int mt) {
return mt ? dargmin_mt(weights, n): dargmin_st(weights, n);
}
SIMD_SAMPLING_API uint64_t fargmin_mt(const float *weights, size_t n) {
return reinterpret_cast<uint64_t>(weights) % LSS_ALIGNMENT ?
float_argsel_fmt<UNALIGNED, ARGMIN, true>(weights, n): float_argsel_fmt<ALIGNED, ARGMIN, true>(weights, n);
}
SIMD_SAMPLING_API uint64_t fargmin_st(const float *weights, size_t n) {
return reinterpret_cast<uint64_t>(weights) % LSS_ALIGNMENT ?
float_argsel_fmt<UNALIGNED, ARGMIN, false>(weights, n): float_argsel_fmt<ALIGNED, ARGMIN, false>(weights, n);
}
SIMD_SAMPLING_API uint64_t fargmin(const float *weights, size_t n, int mt) {
return mt ? fargmin_mt(weights, n): fargmin_st(weights, n);
}
SIMD_SAMPLING_API uint64_t dargmax_mt(const double *weights, size_t n) {
return reinterpret_cast<uint64_t>(weights) % LSS_ALIGNMENT ?
double_argsel_fmt<UNALIGNED, ARGMAX, true>(weights, n): double_argsel_fmt<ALIGNED, ARGMAX, true>(weights, n);
}
SIMD_SAMPLING_API uint64_t dargmax_st(const double *weights, size_t n) {
return reinterpret_cast<uint64_t>(weights) % LSS_ALIGNMENT ?
double_argsel_fmt<UNALIGNED, ARGMAX, false>(weights, n): double_argsel_fmt<ALIGNED, ARGMAX, false>(weights, n);
}
SIMD_SAMPLING_API uint64_t dargmax(const double *weights, size_t n, int mt) {
return mt ? dargmax_mt(weights, n): dargmax_st(weights, n);
}
SIMD_SAMPLING_API uint64_t fargmax_mt(const float *weights, size_t n) {
return reinterpret_cast<uint64_t>(weights) % LSS_ALIGNMENT ?
float_argsel_fmt<UNALIGNED, ARGMAX, true>(weights, n): float_argsel_fmt<ALIGNED, ARGMAX, true>(weights, n);
}
SIMD_SAMPLING_API uint64_t fargmax_st(const float *weights, size_t n) {
return reinterpret_cast<uint64_t>(weights) % LSS_ALIGNMENT ?
float_argsel_fmt<UNALIGNED, ARGMAX, false>(weights, n): float_argsel_fmt<ALIGNED, ARGMAX, false>(weights, n);
}
SIMD_SAMPLING_API uint64_t fargmax(const float *weights, size_t n, int mt) {
return mt ? fargmax_mt(weights, n): fargmax_st(weights, n);
}
SIMD_SAMPLING_API uint64_t dargsel(const double *weights, size_t n, ArgReduction ar, int mt)
{
const bool aligned = reinterpret_cast<uint64_t>(weights) % LSS_ALIGNMENT == 0;
uint64_t ret;
switch((int(aligned) << 2) | (int(ar == ARGMAX) << 1) | mt) {
case 0: ret = double_argsel_fmt<UNALIGNED, ARGMIN, false>(weights, n); break;
case 1: ret = double_argsel_fmt<UNALIGNED, ARGMIN, true>(weights, n); break;
case 2: ret = double_argsel_fmt<UNALIGNED, ARGMAX, false>(weights, n); break;
case 3: ret = double_argsel_fmt<UNALIGNED, ARGMAX, true>(weights, n); break;
case 4: ret = double_argsel_fmt<ALIGNED, ARGMIN, false>(weights, n); break;
case 5: ret = double_argsel_fmt<ALIGNED, ARGMIN, true>(weights, n); break;
case 6: ret = double_argsel_fmt<ALIGNED, ARGMAX, false>(weights, n); break;
case 7: ret = double_argsel_fmt<ALIGNED, ARGMAX, true>(weights, n); break;
default: __builtin_unreachable();
}
return ret;
}
SIMD_SAMPLING_API ptrdiff_t fargsel_k(const float *weights, size_t n, ptrdiff_t k, uint64_t *ret, ArgReduction ar, int mt)
{
const bool aligned = reinterpret_cast<uint64_t>(weights) % LSS_ALIGNMENT == 0;
ptrdiff_t rv;
switch((int(aligned) << 2) | (int(ar == ARGMAX) << 1) | mt) {
case 0: rv = float_argsel_k_fmt<UNALIGNED, ARGMIN, false>(weights, n, k, ret); break;
case 1: rv = float_argsel_k_fmt<UNALIGNED, ARGMIN, true>(weights, n, k, ret); break;
case 2: rv = float_argsel_k_fmt<UNALIGNED, ARGMAX, false>(weights, n, k, ret); break;
case 3: rv = float_argsel_k_fmt<UNALIGNED, ARGMAX, true>(weights, n, k, ret); break;
case 4: rv = float_argsel_k_fmt<ALIGNED, ARGMIN, false>(weights, n, k, ret); break;
case 5: rv = float_argsel_k_fmt<ALIGNED, ARGMIN, true>(weights, n, k, ret); break;
case 6: rv = float_argsel_k_fmt<ALIGNED, ARGMAX, false>(weights, n, k, ret); break;
case 7: rv = float_argsel_k_fmt<ALIGNED, ARGMAX, true>(weights, n, k, ret); break;
default: __builtin_unreachable();
}
return rv;
}
SIMD_SAMPLING_API ptrdiff_t dargsel_k(const double *weights, size_t n, ptrdiff_t k, uint64_t *ret, ArgReduction ar, int mt)
{
const bool aligned = reinterpret_cast<uint64_t>(weights) % LSS_ALIGNMENT == 0;
ptrdiff_t rv;
switch((int(aligned) << 2) | (int(ar == ARGMAX) << 1) | mt) {
case 0: rv = double_argsel_k_fmt<UNALIGNED, ARGMIN, false>(weights, n, k, ret); break;
case 1: rv = double_argsel_k_fmt<UNALIGNED, ARGMIN, true>(weights, n, k, ret); break;
case 2: rv = double_argsel_k_fmt<UNALIGNED, ARGMAX, false>(weights, n, k, ret); break;
case 3: rv = double_argsel_k_fmt<UNALIGNED, ARGMAX, true>(weights, n, k, ret); break;
case 4: rv = double_argsel_k_fmt<ALIGNED, ARGMIN, false>(weights, n, k, ret); break;
case 5: rv = double_argsel_k_fmt<ALIGNED, ARGMIN, true>(weights, n, k, ret); break;
case 6: rv = double_argsel_k_fmt<ALIGNED, ARGMAX, false>(weights, n, k, ret); break;
case 7: rv = double_argsel_k_fmt<ALIGNED, ARGMAX, true>(weights, n, k, ret); break;
default: __builtin_unreachable();
}
return rv;
}
SIMD_SAMPLING_API ptrdiff_t dargsel_k_mt(const double *weights, size_t n, ptrdiff_t k, uint64_t *ret, enum ArgReduction ar) {
const bool aligned = reinterpret_cast<uint64_t>(weights) % LSS_ALIGNMENT == 0;
ptrdiff_t rv;
switch((int(aligned) << 1) | (int(ar == ARGMAX))) {
case 0: rv = double_argsel_k_fmt<UNALIGNED, ARGMIN, true>(weights, n, k, ret); break;
case 1: rv = double_argsel_k_fmt<UNALIGNED, ARGMAX, true>(weights, n, k, ret); break;
case 2: rv = double_argsel_k_fmt<ALIGNED, ARGMIN, true>(weights, n, k, ret); break;
case 3: rv = double_argsel_k_fmt<ALIGNED, ARGMAX, true>(weights, n, k, ret); break;
default: __builtin_unreachable();
}
return rv;
}
SIMD_SAMPLING_API ptrdiff_t dargsel_k_st(const double *weights, size_t n, ptrdiff_t k, uint64_t *ret, enum ArgReduction ar) {
const bool aligned = reinterpret_cast<uint64_t>(weights) % LSS_ALIGNMENT == 0;
ptrdiff_t rv;
switch((int(aligned) << 1) | (int(ar == ARGMAX))) {
case 0: rv = double_argsel_k_fmt<UNALIGNED, ARGMIN, false>(weights, n, k, ret); break;
case 1: rv = double_argsel_k_fmt<UNALIGNED, ARGMAX, false>(weights, n, k, ret); break;
case 2: rv = double_argsel_k_fmt<ALIGNED, ARGMIN, false>(weights, n, k, ret); break;
case 3: rv = double_argsel_k_fmt<ALIGNED, ARGMAX, false>(weights, n, k, ret); break;
default: __builtin_unreachable();
}
return rv;
}
SIMD_SAMPLING_API ptrdiff_t fargsel_k_mt(const float *weights, size_t n, ptrdiff_t k, uint64_t *ret, enum ArgReduction ar) {
const bool aligned = reinterpret_cast<uint64_t>(weights) % LSS_ALIGNMENT == 0;
ptrdiff_t rv;
switch((int(aligned) << 1) | (int(ar == ARGMAX))) {
case 0: rv = float_argsel_k_fmt<UNALIGNED, ARGMIN, true>(weights, n, k, ret); break;
case 1: rv = float_argsel_k_fmt<UNALIGNED, ARGMAX, true>(weights, n, k, ret); break;
case 2: rv = float_argsel_k_fmt<ALIGNED, ARGMIN, true>(weights, n, k, ret); break;
case 3: rv = float_argsel_k_fmt<ALIGNED, ARGMAX, true>(weights, n, k, ret); break;
default: __builtin_unreachable();
}
return rv;
}
SIMD_SAMPLING_API ptrdiff_t fargsel_k_st(const float *weights, size_t n, ptrdiff_t k, uint64_t *ret, enum ArgReduction ar) {
const bool aligned = reinterpret_cast<uint64_t>(weights) % LSS_ALIGNMENT == 0;
ptrdiff_t rv;
switch((int(aligned) << 1) | (int(ar == ARGMAX))) {
case 0: rv = float_argsel_k_fmt<UNALIGNED, ARGMIN, false>(weights, n, k, ret); break;
case 1: rv = float_argsel_k_fmt<UNALIGNED, ARGMAX, false>(weights, n, k, ret); break;
case 2: rv = float_argsel_k_fmt<ALIGNED, ARGMIN, false>(weights, n, k, ret); break;
case 3: rv = float_argsel_k_fmt<ALIGNED, ARGMAX, false>(weights, n, k, ret); break;
default: __builtin_unreachable();
}
return rv;
}
SIMD_SAMPLING_API ptrdiff_t dargmax_k(const double *weights, size_t n, ptrdiff_t k, uint64_t *ret, int mt) {
if(mt) return dargmax_k_mt(weights, n, k, ret);
else return dargmax_k_st(weights, n, k, ret);
}
SIMD_SAMPLING_API ptrdiff_t fargmax_k(const float *weights, size_t n, ptrdiff_t k, uint64_t *ret, int mt) {
if(mt) return fargmax_k_mt(weights, n, k, ret);
else return fargmax_k_st(weights, n, k, ret);
}
SIMD_SAMPLING_API ptrdiff_t dargmin_k(const double *weights, size_t n, ptrdiff_t k, uint64_t *ret, int mt) {
if(mt) return dargmin_k_mt(weights, n, k, ret);
else return dargmin_k_st(weights, n, k, ret);
}
SIMD_SAMPLING_API ptrdiff_t fargmin_k(const float *weights, size_t n, ptrdiff_t k, uint64_t *ret, int mt) {
if(mt) return fargmin_k_mt(weights, n, k, ret);
else return fargmin_k_st(weights, n, k, ret);
}
SIMD_SAMPLING_API ptrdiff_t fargmax_k_st(const float *weights, size_t n, ptrdiff_t k, uint64_t *ret) {
const bool aligned = reinterpret_cast<uint64_t>(weights) % LSS_ALIGNMENT == 0;
ptrdiff_t rv;
if(aligned) rv = float_argsel_k_fmt<ALIGNED, ARGMIN, false>(weights, n, k, ret);
else rv = float_argsel_k_fmt<UNALIGNED, ARGMIN, false>(weights, n, k, ret);
return rv;
}
SIMD_SAMPLING_API ptrdiff_t dargmax_k_st(const double *weights, size_t n, ptrdiff_t k, uint64_t *ret) {
const bool aligned = reinterpret_cast<uint64_t>(weights) % LSS_ALIGNMENT == 0;
ptrdiff_t rv;
if(aligned) rv = double_argsel_k_fmt<ALIGNED, ARGMIN, false>(weights, n, k, ret);
else rv = double_argsel_k_fmt<UNALIGNED, ARGMIN, false>(weights, n, k, ret);
return rv;
}
SIMD_SAMPLING_API ptrdiff_t fargmax_k_mt(const float *weights, size_t n, ptrdiff_t k, uint64_t *ret) {
const bool aligned = reinterpret_cast<uint64_t>(weights) % LSS_ALIGNMENT == 0;
ptrdiff_t rv;
if(aligned) rv = float_argsel_k_fmt<ALIGNED, ARGMIN, true>(weights, n, k, ret);
else rv = float_argsel_k_fmt<UNALIGNED, ARGMIN, true>(weights, n, k, ret);
return rv;
}
SIMD_SAMPLING_API ptrdiff_t dargmax_k_mt(const double *weights, size_t n, ptrdiff_t k, uint64_t *ret) {
const bool aligned = reinterpret_cast<uint64_t>(weights) % LSS_ALIGNMENT == 0;
ptrdiff_t rv;
if(aligned) rv = double_argsel_k_fmt<ALIGNED, ARGMIN, true>(weights, n, k, ret);
else rv = double_argsel_k_fmt<UNALIGNED, ARGMIN, true>(weights, n, k, ret);
return rv;
}
SIMD_SAMPLING_API ptrdiff_t fargmin_k_st(const float *weights, size_t n, ptrdiff_t k, uint64_t *ret) {
const bool aligned = reinterpret_cast<uint64_t>(weights) % LSS_ALIGNMENT == 0;
ptrdiff_t rv;
if(aligned) rv = float_argsel_k_fmt<ALIGNED, ARGMIN, false>(weights, n, k, ret);
else rv = float_argsel_k_fmt<UNALIGNED, ARGMIN, false>(weights, n, k, ret);
return rv;
}
SIMD_SAMPLING_API ptrdiff_t dargmin_k_st(const double *weights, size_t n, ptrdiff_t k, uint64_t *ret) {
const bool aligned = reinterpret_cast<uint64_t>(weights) % LSS_ALIGNMENT == 0;
ptrdiff_t rv;
if(aligned) rv = double_argsel_k_fmt<ALIGNED, ARGMIN, false>(weights, n, k, ret);
else rv = double_argsel_k_fmt<UNALIGNED, ARGMIN, false>(weights, n, k, ret);
return rv;
}
SIMD_SAMPLING_API ptrdiff_t fargmin_k_mt(const float *weights, size_t n, ptrdiff_t k, uint64_t *ret) {
const bool aligned = reinterpret_cast<uint64_t>(weights) % LSS_ALIGNMENT == 0;
ptrdiff_t rv;
if(aligned) rv = float_argsel_k_fmt<ALIGNED, ARGMIN, true>(weights, n, k, ret);
else rv = float_argsel_k_fmt<UNALIGNED, ARGMIN, true>(weights, n, k, ret);
return rv;
}
SIMD_SAMPLING_API ptrdiff_t dargmin_k_mt(const double *weights, size_t n, ptrdiff_t k, uint64_t *ret) {
const bool aligned = reinterpret_cast<uint64_t>(weights) % LSS_ALIGNMENT == 0;
ptrdiff_t rv;
if(aligned) rv = double_argsel_k_fmt<ALIGNED, ARGMIN, true>(weights, n, k, ret);
else rv = double_argsel_k_fmt<UNALIGNED, ARGMIN, true>(weights, n, k, ret);
return rv;
}
} // extern "C"
template<ArgReduction AR>
struct Cmp {
#if __AVX512F__
static INLINE __mmask8 eq(__m512d x, __m512d y) {
return _mm512_cmp_pd_mask(x, y, _CMP_EQ_OQ);
}
static INLINE __mmask16 eq(__m512 x, __m512 y) {
return _mm512_cmp_ps_mask(x, y, _CMP_EQ_OQ);
}
static INLINE __mmask16 cmp(__m512 x, __m512 y, std::true_type) {
return _mm512_cmp_ps_mask(x, y, _CMP_GT_OQ);
}
static INLINE __mmask16 cmp(__m512 x, __m512 y, std::false_type) {
return _mm512_cmp_ps_mask(x, y, _CMP_LT_OQ);
}
static INLINE __mmask8 cmp(__m512d x, __m512d y, std::true_type) {
return _mm512_cmp_pd_mask(x, y, _CMP_GT_OQ);
}
static INLINE __mmask8 cmp(__m512d x, __m512d y, std::false_type) {
return _mm512_cmp_pd_mask(x, y, _CMP_LT_OQ);
}
static INLINE __mmask16 cmp(__m512 x, __m512 y) {
return cmp(x, y, std::integral_constant<bool, AR == ARGMAX>());
}
static INLINE __mmask8 cmp(__m512d x, __m512d y) {
return cmp(x, y, std::integral_constant<bool, AR == ARGMAX>());
}
static INLINE float reduce(__m512 x, std::true_type) {
return _mm512_reduce_max_ps(x);
}
static INLINE float reduce(__m512 x, std::false_type) {
return _mm512_reduce_min_ps(x);
}
static INLINE double reduce(__m512d x, std::true_type) {
return _mm512_reduce_max_pd(x);
}
static INLINE double reduce(__m512d x, std::false_type) {
return _mm512_reduce_min_pd(x);
}
static INLINE double reduce(__m512d x) {
return reduce(x, std::integral_constant<bool, AR == ARGMAX>());
}
static INLINE float reduce(__m512 x) {
return reduce(x, std::integral_constant<bool, AR == ARGMAX>());
}
static INLINE __m512d max(__m512d x, __m512d y, std::true_type) {
return _mm512_max_pd(x, y);
}
static INLINE __m512d max(__m512d x, __m512d y, std::false_type) {
return _mm512_min_pd(x, y);
}
static INLINE __m512 max(__m512 x, __m512 y, std::true_type) {
return _mm512_max_ps(x, y);
}
static INLINE __m512 max(__m512 x, __m512 y, std::false_type) {
return _mm512_min_ps(x, y);
}
#endif
#if __AVX2__
static INLINE __m256d max(__m256d x, __m256d y, std::true_type) {
return _mm256_max_pd(x, y);
}
static INLINE __m256d max(__m256d x, __m256d y, std::false_type) {
return _mm256_min_pd(x, y);
}
static INLINE __m256 max(__m256 x, __m256 y, std::true_type) {
return _mm256_max_ps(x, y);
}
static INLINE __m256 max(__m256 x, __m256 y, std::false_type) {
return _mm256_min_ps(x, y);
}
static INLINE __m256 cmp(__m256 x, __m256 y, std::true_type) {
return _mm256_cmp_ps(x, y, _CMP_GT_OQ);
}
static INLINE __m256 cmp(__m256 x, __m256 y, std::false_type) {
return _mm256_cmp_ps(x, y, _CMP_LT_OQ);
}
static INLINE __m256d cmp(__m256d x, __m256d y, std::true_type) {
return _mm256_cmp_pd(x, y, _CMP_GT_OQ);
}
static INLINE __m256d cmp(__m256d x, __m256d y, std::false_type) {
return _mm256_cmp_pd(x, y, _CMP_LT_OQ);
}
static INLINE __m256 eq(__m256 x, __m256 y) {
return _mm256_cmp_ps(x, y, _CMP_EQ_OQ);
}
static INLINE __m256d eq(__m256d x, __m256d y) {
return _mm256_cmp_pd(x, y, _CMP_EQ_OQ);
}
static INLINE __m256d max(__m256d x, std::false_type) {
return broadcast_min(x);
}
static INLINE __m256d max(__m256d x, std::true_type) {
return broadcast_max(x);
}
static INLINE __m256d max(__m256d x) {
return max(x, std::integral_constant<bool, AR == ARGMAX>());
}
static INLINE __m256 max(__m256 x) {
return max(x, std::integral_constant<bool, AR == ARGMAX>());
}
static INLINE __m256 max(__m256 x, std::false_type) {
return broadcast_min(x);
}
static INLINE __m256 max(__m256 x, std::true_type) {
return broadcast_max(x);
}
#endif
#ifdef __SSE2__
static INLINE __m128 cmp(__m128 x, __m128 y, std::true_type) {
return _mm_cmp_ps(x, y, _CMP_GT_OQ);
}
static INLINE __m128 cmp(__m128 x, __m128 y, std::false_type) {
return _mm_cmp_ps(x, y, _CMP_LT_OQ);
}
static INLINE __m128 max(__m128 x, std::false_type) {
return broadcast_min(x);
}
static INLINE __m128 max(__m128 x, std::true_type) {
return broadcast_max(x);
}
#endif
template<typename T>
static INLINE T max(T x, T y) {
return max(x, y, std::integral_constant<bool, AR == ARGMAX>());
}
template<typename T>
static INLINE T max(T x) {
return max(x, std::integral_constant<bool, AR == ARGMAX>());
}
static INLINE float cmp(const float x, const float y, std::false_type) {
return x < y;
}
static INLINE float cmp(const float x, const float y, std::true_type) {
return x > y;
}
template<typename T>
static INLINE T cmp(T x, T y) {
return cmp(x, y, std::integral_constant<bool, AR == ARGMAX>());
}
static INLINE double cmp(const double x, const double y, std::false_type) {
return x < y;
}
static INLINE double cmp(const double x, const double y, std::true_type) {
return x > y;
}
static INLINE double cmp(const double x, const double y) {
return cmp(x, y, std::integral_constant<bool, AR == ARGMAX>());
}
};
template<LoadFormat aln, ArgReduction AR, bool MT>
SIMD_SAMPLING_API uint64_t double_argsel_fmt(const double *weights, size_t n)
{
uint64_t bestind = 0;
#ifdef __AVX512F__
static constexpr double STARTVAL = (AR == ARGMAX) ? -std::numeric_limits<double>::max(): std::numeric_limits<double>::max();
constexpr size_t nperel = sizeof(__m512d) / sizeof(double);
const size_t e = n / nperel;
__m512d vmaxv = _mm512_set1_pd(STARTVAL);
if(MT) {
OMP_PFOR
for(size_t o = 0; o < e; ++o) {
__m512d ov = load<aln>((const double *)&weights[o * nperel]);
auto cmpmask = Cmp<AR>::cmp(ov, vmaxv);
if(cmpmask) {
auto newmaxv = _mm512_set1_pd(_mm512_reduce_max_pd(ov));
if((cmpmask = Cmp<AR>::eq(ov, newmaxv))) {
OMP_CRITICAL
if(Cmp<AR>::cmp(ov, vmaxv)) {
vmaxv = newmaxv;
bestind = ctz(cmpmask) + o * nperel;
}
}
}
}
} else {
for(size_t o = 0; o < e; ++o) {
__m512d ov = load<aln>((const double *)&weights[o * nperel]);
auto cmpmask = Cmp<AR>::cmp(ov, vmaxv);
if(cmpmask) {
auto newmaxv = _mm512_set1_pd(_mm512_reduce_max_pd(ov));
if((cmpmask = Cmp<AR>::eq(ov, newmaxv))) {
vmaxv = newmaxv;
bestind = ctz(cmpmask) + o * nperel;
}
}
}
}
double maxv = _mm512_cvtsd_f64(vmaxv);
for(size_t p = e * nperel; p != n; ++p) {
if(Cmp<AR>::cmp(weights[p], maxv)) {
maxv = weights[p], bestind = p;
}
}
#elif __AVX2__
static constexpr double STARTVAL = (AR == ARGMAX) ? -std::numeric_limits<double>::max(): std::numeric_limits<double>::max();
constexpr size_t nperel = sizeof(__m256d) / sizeof(double);
const size_t e = (n / nperel);
__m256d vmaxv = _mm256_set1_pd(STARTVAL);
if(MT) {
OMP_PFOR
for(size_t o = 0; o < e; ++o) {
__m256d ov = load<aln>((const double *)&weights[o * nperel]);
auto cmp = Cmp<AR>::cmp(ov, vmaxv);
auto cmpmask = _mm256_movemask_pd(cmp);
if(cmpmask) {
__m256d newmax = Cmp<AR>::max(ov);
{
OMP_CRITICAL
if(_mm256_movemask_pd(Cmp<AR>::cmp(ov, vmaxv))) {
vmaxv = newmax;
bestind = ctz(cmpmask) + o * nperel;
}
}
}
}
} else {
for(size_t o = 0; o < e; ++o) {
__m256d ov = load<aln>((const double *)&weights[o * nperel]);
auto cmp = Cmp<AR>::cmp(ov, vmaxv);
auto cmpmask = _mm256_movemask_pd(cmp);
if(cmpmask) {
__m256d newmax = Cmp<AR>::max(ov);
cmpmask = _mm256_movemask_pd(Cmp<AR>::cmp(ov, vmaxv));
vmaxv = newmax;
bestind = ctz(cmpmask) + o * nperel;
}
}
}
double maxv = _mm256_cvtsd_f64(vmaxv);
for(size_t p = e * nperel; p != n; ++p) {
if(Cmp<AR>::cmp(weights[p], maxv)) maxv = weights[p], bestind = p;
}
#else
static constexpr double STARTVAL = (AR == ARGMAX) ? -std::numeric_limits<double>::max(): std::numeric_limits<double>::max();
double bestv = STARTVAL;
if(MT) {
OMP_PFOR
for(size_t i = 0; i < n; ++i) {
auto v = weights[i];
if(Cmp<AR>::cmp(v, bestv)) {
OMP_CRITICAL
if(Cmp<AR>::cmp(v, bestv)) bestv = v, bestind = i;
}
}
} else {
for(size_t i = 0; i < n; ++i) {
auto v = weights[i];
if(Cmp<AR>::cmp(v, bestv)) {
bestv = v, bestind = i;
}
}
}
#endif
return bestind;
}
template<LoadFormat aln, ArgReduction AR, bool MT>
SIMD_SAMPLING_API uint64_t float_argsel_fmt(const float * weights, size_t n)
{
uint64_t bestind = 0;
#ifdef __AVX512F__
static constexpr float STARTVAL = (AR == ARGMAX) ? -std::numeric_limits<float>::max(): std::numeric_limits<float>::max();
constexpr size_t nperel = sizeof(__m512) / sizeof(float);
const size_t e = n / nperel;
__m512 vmaxv = _mm512_set1_ps(STARTVAL);
if(MT) {
OMP_PFOR
for(size_t o = 0; o < e; ++o) {
__m512 lv = load<aln>((const float *)&weights[o * nperel]);
auto cmpmask = Cmp<AR>::cmp(lv, vmaxv);
if(cmpmask) {
auto newmaxv = _mm512_set1_ps(Cmp<AR>::reduce(lv));
if((cmpmask = Cmp<AR>::eq(lv, newmaxv))) {
OMP_CRITICAL
if(Cmp<AR>::cmp(lv, vmaxv)) {
vmaxv = newmaxv;
bestind = ctz(cmpmask) + o * nperel;
}
}
}
}
} else {
for(size_t o = 0; o < e; ++o) {
__m512 lv = load<aln>((const float *)&weights[o * nperel]);
auto cmpmask = Cmp<AR>::cmp(lv, vmaxv);
if(cmpmask) {
auto newmaxv = _mm512_set1_ps(Cmp<AR>::reduce(lv));
if((cmpmask = Cmp<AR>::eq(lv, newmaxv))) {
vmaxv = newmaxv;
bestind = ctz(cmpmask) + o * nperel;
}
}
}
}
float maxv = _mm512_cvtss_f32(vmaxv);
for(size_t p = e * nperel; p != n; ++p) {
auto v = weights[p];
if(Cmp<AR>::cmp(v, maxv))
bestind = p, maxv = v;
}
#elif __AVX2__
static constexpr float STARTVAL = (AR == ARGMAX) ? -std::numeric_limits<float>::max(): std::numeric_limits<float>::max();
constexpr size_t nperel = sizeof(__m256) / sizeof(float);
const size_t e = (n / nperel);
__m256 vmaxv = _mm256_set1_ps(STARTVAL);
if(MT) {
OMP_PFOR
for(size_t o = 0; o < e; ++o) {
__m256 divv = load<aln>((const float *) &weights[o * nperel]);
auto cmp = Cmp<AR>::cmp(divv, vmaxv);
auto cmpmask = _mm256_movemask_ps(cmp);
if(cmpmask) {
const __m256 m2 = Cmp<AR>::max(divv);
OMP_CRITICAL
{
cmp = Cmp<AR>::eq(m2, divv);
cmpmask = _mm256_movemask_ps(cmp); // set to 1 where the largest value in cmp is
if(_mm256_movemask_ps(Cmp<AR>::cmp(m2, vmaxv))) { // If any in m2 are > vmaxv
vmaxv = m2;
bestind = ctz(cmpmask) + o * nperel;
}
}
}
}
} else {
for(size_t o = 0; o < e; ++o) {
__m256 divv = load<aln>((const float *) &weights[o * nperel]);
auto cmp = Cmp<AR>::cmp(divv, vmaxv);
auto cmpmask = _mm256_movemask_ps(cmp);
if(cmpmask) {
const __m256 m2 = Cmp<AR>::max(divv);
{
cmp = Cmp<AR>::eq(m2, divv);
cmpmask = _mm256_movemask_ps(cmp); // set to 1 where the largest value in cmp is
if(_mm256_movemask_ps(Cmp<AR>::cmp(m2, vmaxv))) { // If any in m2 are > vmaxv
vmaxv = m2;
bestind = ctz(cmpmask) + o * nperel;
}
}
}
}
}
float maxv = _mm256_cvtss_f32(vmaxv);
for(size_t p = e * nperel; p != n; ++p) {
if(Cmp<AR>::cmp(weights[p], maxv)) {
OMP_CRITICAL
if(Cmp<AR>::cmp(weights[p], maxv))
maxv = weights[p], bestind = p;
}
}
#else
float bestv = weights[0];
bestind = 0;
if(MT) {
OMP_PFOR
for(size_t i = 1; i < n; ++i) {
if(Cmp<AR>::cmp(weights[i], bestv)) {
OMP_CRITICAL
if(Cmp<AR>::cmp(weights[i], bestv))
bestv = weights[i], bestind = i;
}
}
} else {
for(size_t i = 1; i < n; ++i) {
if(Cmp<AR>::cmp(weights[i], bestv)) {
if(Cmp<AR>::cmp(weights[i], bestv))
bestv = weights[i], bestind = i;
}
}
}
#endif
return bestind;
}
template<typename T, ArgReduction AR>
struct Comparator {
using type = typename std::conditional<AR == ARGMAX, std::greater<T>, std::less<T>>::type;
template<typename OT>
using otype = typename std::conditional<AR == ARGMAX, std::greater<OT>, std::less<OT>>::type;
template<typename T2>
static INLINE bool compare(const T2 &x, const T2 &y) {
return otype<T2>()(x, y);
}
};
template<typename FT, ArgReduction AR>
struct argminpq_t: public std::priority_queue<std::pair<FT, uint64_t>, std::vector<std::pair<FT, uint64_t>>, typename Comparator<std::pair<FT, uint64_t>, AR>::type> {
using value_t = std::pair<FT, uint64_t>;
using vec_t = std::vector<std::pair<FT, uint64_t>>;
using cmp_t = Comparator<std::pair<FT, uint64_t>, AR>;
//using fcmp_t = typename Comparator<std::pair<FT, uint64_t>, AR>::otype<FT>;
uint32_t k_;
argminpq_t(int k): k_(k) {
this->c.reserve(k);
}
INLINE void add(FT val, uint64_t id) {
if(this->size() < k_) {
this->push(value_t(val, id));
} else if(cmp_t::compare(val, this->top().first)) {
this->pop();
this->push(value_t(val, id));
}
}
};
template<LoadFormat aln, ArgReduction AR, bool MT>
ptrdiff_t float_argsel_k_fmt(const float * weights, size_t n, ptrdiff_t k, uint64_t *ret)
{
std::vector<argminpq_t<float, AR>> pqs;
if(MT) {
int nt;
#pragma omp parallel
{
nt = omp_get_num_threads();
}
pqs.resize(nt, argminpq_t<float, AR>(k));
} else {
pqs.emplace_back(k);
}
#ifdef __AVX512F__
static constexpr float STARTVAL = (AR == ARGMAX) ? -std::numeric_limits<float>::max(): std::numeric_limits<float>::max();
constexpr size_t nperel = sizeof(__m512) / sizeof(float);
const size_t e = n / nperel;
__m512 *vmaxvs;
if(posix_memalign((void **)&vmaxvs, LSS_ALIGNMENT, sizeof(__m512) * pqs.size()))
throw std::bad_alloc();
for(size_t i = 0; i < pqs.size(); ++i) {
vmaxvs[i] = _mm512_set1_ps(STARTVAL);
}
if(MT) {
OMP_PFOR
for(size_t o = 0; o < e; ++o) {
const int tid = MT ? omp_get_thread_num(): 0;
auto &vmaxv = vmaxvs[tid];
argminpq_t<float, AR> &pq = pqs[tid];
__m512 lv = load<aln>((const float *)&weights[o * nperel]);
auto cmpmask = Cmp<AR>::cmp(lv, vmaxv);
if(cmpmask) {
#define DOITER {\
auto ind = ctz(cmpmask);\
pq.add(lv[ind], ind + o * nperel);\
cmpmask ^= (1u << ind); }
switch(__builtin_popcount(cmpmask)) {
case 16: DOITER;__attribute__((fallthrough));
case 15: DOITER;__attribute__((fallthrough));
case 14: DOITER;__attribute__((fallthrough));
case 13: DOITER;__attribute__((fallthrough));
case 12: DOITER;__attribute__((fallthrough));
case 11: DOITER;__attribute__((fallthrough));
case 10: DOITER;__attribute__((fallthrough));
case 9: DOITER;__attribute__((fallthrough));
case 8: DOITER;__attribute__((fallthrough));
case 7: DOITER;__attribute__((fallthrough));
case 6: DOITER;__attribute__((fallthrough));
case 5: DOITER;__attribute__((fallthrough));
case 4: DOITER;__attribute__((fallthrough));
case 3: DOITER;__attribute__((fallthrough));
case 2: DOITER;__attribute__((fallthrough));
case 1: DOITER;
}
vmaxv = _mm512_set1_ps(pq.top().first);
}
}
} else {
for(size_t o = 0; o < e; ++o) {
argminpq_t<float, AR> &pq = pqs[0];
auto &vmaxv = vmaxvs[0];
__m512 lv = load<aln>((const float *)&weights[o * nperel]);
auto cmpmask = Cmp<AR>::cmp(lv, vmaxv);
if(cmpmask) {
switch(__builtin_popcount(cmpmask)) {
case 16: DOITER; __attribute__((fallthrough));
case 15: DOITER; __attribute__((fallthrough));
case 14: DOITER; __attribute__((fallthrough));
case 13: DOITER; __attribute__((fallthrough));
case 12: DOITER; __attribute__((fallthrough));
case 11: DOITER; __attribute__((fallthrough));
case 10: DOITER; __attribute__((fallthrough));
case 9: DOITER; __attribute__((fallthrough));
case 8: DOITER; __attribute__((fallthrough));
case 7: DOITER; __attribute__((fallthrough));
case 6: DOITER; __attribute__((fallthrough));
case 5: DOITER; __attribute__((fallthrough));
case 4: DOITER; __attribute__((fallthrough));
case 3: DOITER; __attribute__((fallthrough));
case 2: DOITER; __attribute__((fallthrough));
case 1: DOITER;
#undef DOITER
}
vmaxv = _mm512_set1_ps(pq.top().first);
}
}
}
std::free(vmaxvs);
#elif __AVX2__
static constexpr float STARTVAL = (AR == ARGMAX) ? -std::numeric_limits<float>::max(): std::numeric_limits<float>::max();
constexpr size_t nperel = sizeof(__m256) / sizeof(float);
const size_t e = (n / nperel);
__m256 *vmaxvs;
if(posix_memalign((void **)&vmaxvs, LSS_ALIGNMENT, sizeof(__m256) * pqs.size()))
throw std::bad_alloc();
for(size_t i = 0; i < pqs.size(); ++i) {
vmaxvs[i] = _mm256_set1_ps(STARTVAL);
}
#define DOITER {\
auto ind = ctz(cmpmask);\
pq.add(lv[ind], ind + o * nperel);\
cmpmask ^= (1u << ind); }
if(MT) {
OMP_PFOR
for(size_t o = 0; o < e; ++o) {
const int tid = MT ? omp_get_thread_num(): 0;
auto &vmaxv = vmaxvs[tid];
auto &pq = pqs[tid];
__m256 lv = load<aln>((const float *) &weights[o * nperel]);
auto cmp = Cmp<AR>::cmp(lv, vmaxv);
auto cmpmask = _mm256_movemask_ps(cmp);
if(cmpmask) {
switch(__builtin_popcount(cmpmask)) {
case 8: DOITER __attribute__ ((fallthrough));
case 7: DOITER __attribute__ ((fallthrough));
case 6: DOITER __attribute__ ((fallthrough));
case 5: DOITER __attribute__ ((fallthrough));
case 4: DOITER __attribute__ ((fallthrough));
case 3: DOITER __attribute__ ((fallthrough));
case 2: DOITER __attribute__ ((fallthrough));
case 1: DOITER
}
vmaxv = _mm256_set1_ps(pq.top().first);
}
}
} else {
for(size_t o = 0; o < e; ++o) {
auto &pq = pqs[0];
auto &vmaxv = vmaxvs[0];
__m256 lv = load<aln>((const float *) &weights[o * nperel]);
auto cmp = Cmp<AR>::cmp(lv, vmaxv);
auto cmpmask = _mm256_movemask_ps(cmp);
if(cmpmask) {
switch(__builtin_popcount(cmpmask)) {
case 8: DOITER __attribute__ ((fallthrough));
case 7: DOITER __attribute__ ((fallthrough));
case 6: DOITER __attribute__ ((fallthrough));
case 5: DOITER __attribute__ ((fallthrough));
case 4: DOITER __attribute__ ((fallthrough));
case 3: DOITER __attribute__ ((fallthrough));
case 2: DOITER __attribute__ ((fallthrough));
case 1: DOITER
}
vmaxv = _mm256_set1_ps(pq.top().first);
}
}
}
std::free(vmaxvs);
#undef DOITER
#else
if(MT) {
OMP_PFOR
for(size_t i = 0; i < n; ++i) {
const int tid = omp_get_thread_num();
auto &pq = pqs[tid];
if(Cmp<AR>::cmp(weights[i], pq.top().first)) {
pq.add(weights[i], i);
}
}
} else {
auto &pr = pqs[0];
#pragma GCC unroll 4
for(size_t i = 0; i < n; ++i) {
if(pr.size() < static_cast<size_t>(k) || Cmp<AR>::cmp(weights[i], pr.top().first))
pr.add(weights[i], i);
}
}
static constexpr size_t nperel = 1;
const size_t e = n;
#endif
// TODO: parallelize merging
auto &basepq = pqs[0];
for(ptrdiff_t i = pqs.size() - 1; i > 0; --i) {
auto &opq = pqs[i];
while(opq.size()) {
basepq.add(opq.top().first, opq.top().second);
if(basepq.size() > size_t(k)) basepq.pop();
}
pqs.pop_back();
}
for(size_t p = e * nperel; p != n; ++p) {
basepq.add(weights[p], p);
if(basepq.size() > size_t(k)) basepq.pop();
}
const ptrdiff_t rv = basepq.size();
for(ptrdiff_t i = 0; i < rv; ++i) {
ret[rv - i - 1] = basepq.top().second;
basepq.pop();
}
return rv;
}
template<LoadFormat aln, ArgReduction AR, bool MT> ptrdiff_t
double_argsel_k_fmt(const double * weights, size_t n, ptrdiff_t k, uint64_t *ret)
{
std::vector<argminpq_t<double, AR>> pqs;
if(MT) {
int nt;
#pragma omp parallel
{
nt = omp_get_num_threads();
}
pqs.resize(nt, argminpq_t<double, AR>(k));
} else {
pqs.emplace_back(k);
}
#ifdef __AVX512F__
static constexpr double STARTVAL = (AR == ARGMAX) ? -std::numeric_limits<double>::max(): std::numeric_limits<double>::max();
constexpr size_t nperel = sizeof(__m512) / sizeof(double);
const size_t e = n / nperel;
__m512d *vmaxvs;
if(posix_memalign((void **)&vmaxvs, LSS_ALIGNMENT, sizeof(__m512d) * pqs.size()))
throw std::bad_alloc();
for(size_t i = 0; i < pqs.size(); ++i) {
vmaxvs[i] = _mm512_set1_pd(STARTVAL);
}
if(MT) {
OMP_PFOR
for(size_t o = 0; o < e; ++o) {
const int tid = MT ? omp_get_thread_num(): 0;
auto &vmaxv = vmaxvs[tid];
argminpq_t<double, AR> &pq = pqs[tid];
__m512d lv = load<aln>((const double *)&weights[o * nperel]);
auto cmpmask = Cmp<AR>::cmp(lv, vmaxv);
if(cmpmask) {
#define DOITER do {\
auto ind = ctz(cmpmask);\
pq.add(lv[ind], ind + o * nperel);\
cmpmask ^= (1u << ind); } while(0)
switch(__builtin_popcount(cmpmask)) {
case 8: DOITER; __attribute__((fallthrough));
case 7: DOITER; __attribute__((fallthrough));
case 6: DOITER; __attribute__((fallthrough));
case 5: DOITER; __attribute__((fallthrough));
case 4: DOITER; __attribute__((fallthrough));
case 3: DOITER; __attribute__((fallthrough));
case 2: DOITER; __attribute__((fallthrough));
case 1: DOITER;
}
vmaxv = _mm512_set1_pd(pq.top().first);
}
}
} else {
for(size_t o = 0; o < e; ++o) {
argminpq_t<double, AR> &pq = pqs[0];
auto &vmaxv = vmaxvs[0];
__m512d lv = load<aln>((const double *)&weights[o * nperel]);
auto cmpmask = Cmp<AR>::cmp(lv, vmaxv);
if(cmpmask) {
switch(__builtin_popcount(cmpmask)) {
case 8: DOITER;__attribute__((fallthrough));
case 7: DOITER;__attribute__((fallthrough));
case 6: DOITER;__attribute__((fallthrough));
case 5: DOITER;__attribute__((fallthrough));
case 4: DOITER;__attribute__((fallthrough));
case 3: DOITER;__attribute__((fallthrough));
case 2: DOITER;__attribute__((fallthrough));
case 1: DOITER;
#undef DOITER
}
vmaxv = _mm512_set1_pd(pq.top().first);
}
}
}
std::free(vmaxvs);
#elif __AVX2__
static constexpr double STARTVAL = (AR == ARGMAX) ? -std::numeric_limits<double>::max(): std::numeric_limits<double>::max();
constexpr size_t nperel = sizeof(__m256) / sizeof(double);
const size_t e = (n / nperel);
__m256d *vmaxvs;
if(posix_memalign((void **)&vmaxvs, LSS_ALIGNMENT, sizeof(__m256) * pqs.size()))
throw std::bad_alloc();
for(size_t i = 0; i < pqs.size(); ++i) {
vmaxvs[i] = _mm256_set1_pd(STARTVAL);
}
#define DOITER {\
auto ind = ctz(cmpmask);\
pq.add(lv[ind], ind + o * nperel);\
cmpmask ^= (1u << ind); }
if(MT) {
OMP_PFOR
for(size_t o = 0; o < e; ++o) {
const int tid = MT ? omp_get_thread_num(): 0;
auto &vmaxv = vmaxvs[tid];
auto &pq = pqs[tid];
__m256d lv = load<aln>((const double *) &weights[o * nperel]);
auto cmp = Cmp<AR>::cmp(lv, vmaxv);
auto cmpmask = _mm256_movemask_pd(cmp);
if(cmpmask) {
switch(__builtin_popcount(cmpmask)) {
case 4: DOITER __attribute__ ((fallthrough));
case 3: DOITER __attribute__ ((fallthrough));
case 2: DOITER __attribute__ ((fallthrough));
case 1: DOITER
}
vmaxv = _mm256_set1_pd(pq.top().first);
}
}
} else {
for(size_t o = 0; o < e; ++o) {
auto &pq = pqs[0];
auto &vmaxv = vmaxvs[0];
__m256d lv = load<aln>((const double *) &weights[o * nperel]);
auto cmp = Cmp<AR>::cmp(lv, vmaxv);
auto cmpmask = _mm256_movemask_pd(cmp);
if(cmpmask) {
switch(__builtin_popcount(cmpmask)) {
case 4: DOITER __attribute__ ((fallthrough));
case 3: DOITER __attribute__ ((fallthrough));
case 2: DOITER __attribute__ ((fallthrough));
case 1: DOITER
}
vmaxv = _mm256_set1_pd(pq.top().first);
}
}
}