-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpospopcnt.c
4653 lines (3977 loc) · 184 KB
/
pospopcnt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
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) 2019
* Author(s): Marcus D. R. Klarqvist, Wojciech Muła, and Daniel Lemire
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include <string.h> //memset
#include <assert.h> //assert
#include "pospopcnt.h"
#if __clang__ == 1 || __llvm__ == 1
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunsequenced"
#endif
int pospopcnt_u16(const uint16_t* data, uint32_t len, uint32_t* flags) {
#if POSPOPCNT_SIMD_VERSION >= 6
if (len < 32) return(pospopcnt_u16_sse_sad(data, len, flags)); // small
else if (len < 256) return(pospopcnt_u16_sse_blend_popcnt_unroll8(data, len, flags)); // small
else if (len < 512) return(pospopcnt_u16_avx512bw_blend_popcnt_unroll8(data, len, flags)); // medium
else if (len < 4096) return(pospopcnt_u16_avx512bw_adder_forest(data, len, flags)); // medium3
else return(pospopcnt_u16_avx512_harley_seal(data, len, flags)); // fix
#elif POSPOPCNT_SIMD_VERSION >= 5
if (len < 128) return(pospopcnt_u16_sse_sad(data, len, flags)); // small
else if (len < 1024) return(pospopcnt_u16_avx2_blend_popcnt_unroll8(data, len, flags)); // medium
else return(pospopcnt_u16_avx2_harley_seal(data, len, flags)); // large
#elif POSPOPCNT_SIMD_VERSION >= 3
return(pospopcnt_u16_sse_harley_seal(data, len, flags));
#else
#ifndef _MSC_VER
return(pospopcnt_u16_scalar_umul128_unroll2(data, len, flags)); // fallback scalar
#else
return(pospopcnt_u16_scalar_naive(data, len, flags));
#endif
#endif
}
int pospopcnt_u16_method(PPOPCNT_U16_METHODS method, const uint16_t* data, uint32_t len, uint32_t* flags) {
pospopcnt_u16_method_type pospopcnt_u16 = get_pospopcnt_u16_method(method);
return pospopcnt_u16(data, len, flags);
}
pospopcnt_u16_method_type get_pospopcnt_u16_method(PPOPCNT_U16_METHODS method) {
switch(method) {
case(PPOPCNT_AUTO): return pospopcnt_u16;
case(PPOPCNT_SCALAR): return pospopcnt_u16_scalar_naive;
case(PPOPCNT_SCALAR_NOSIMD): return pospopcnt_u16_scalar_naive_nosimd;
case(PPOPCNT_SCALAR_PARTITION): return pospopcnt_u16_scalar_partition;
case(PPOPCNT_SCALAR_HIST1X4): return pospopcnt_u16_scalar_hist1x4;
case(PPOPCNT_SCALAR_UMUL128): return pospopcnt_u16_scalar_umul128;
case(PPOPCNT_SCALAR_UMUL128_UR2): return pospopcnt_u16_scalar_umul128_unroll2;
case(PPOPCNT_SSE_SINGLE): return pospopcnt_u16_sse_single;
case(PPOPCNT_SSE_BLEND_POPCNT): return pospopcnt_u16_sse_blend_popcnt;
case(PPOPCNT_SSE_BLEND_POPCNT_UR4): return pospopcnt_u16_sse_blend_popcnt_unroll4;
case(PPOPCNT_SSE_BLEND_POPCNT_UR8): return pospopcnt_u16_sse_blend_popcnt_unroll8;
case(PPOPCNT_SSE_BLEND_POPCNT_UR16): return pospopcnt_u16_sse_blend_popcnt_unroll16;
case(PPOPCNT_SSE_SAD): return pospopcnt_u16_sse_sad;
case(PPOPCNT_SSE_HARLEY_SEAL): return pospopcnt_u16_sse_harley_seal;
case(PPOPCNT_SSE_HARLEY_SEAL_IMPROVED): return pospopcnt_u16_sse_harley_seal_improved;
case(PPOPCNT_AVX2_POPCNT): return pospopcnt_u16_avx2_popcnt;
case(PPOPCNT_AVX2): return pospopcnt_u16_avx2;
case(PPOPCNT_AVX2_POPCNT_NAIVE): return pospopcnt_u16_avx2_naive_counter;
case(PPOPCNT_AVX2_SINGLE): return pospopcnt_u16_avx2_single;
case(PPOPCNT_AVX2_LEMIRE1): return pospopcnt_u16_avx2_lemire;
case(PPOPCNT_AVX2_LEMIRE2): return pospopcnt_u16_avx2_lemire2;
case(PPOPCNT_AVX2_BLEND_POPCNT): return pospopcnt_u16_avx2_blend_popcnt;
case(PPOPCNT_AVX2_BLEND_POPCNT_UR4): return pospopcnt_u16_avx2_blend_popcnt_unroll4;
case(PPOPCNT_AVX2_BLEND_POPCNT_UR8): return pospopcnt_u16_avx2_blend_popcnt_unroll8;
case(PPOPCNT_AVX2_BLEND_POPCNT_UR16): return pospopcnt_u16_avx2_blend_popcnt_unroll16;
case(PPOPCNT_AVX2_ADDER_FOREST): return pospopcnt_u16_avx2_adder_forest;
case(PPOPCNT_AVX2_HARLEY_SEAL): return pospopcnt_u16_avx2_harley_seal;
case(PPOPCNT_AVX2_HARLEY_SEAL_IMPROVED): return pospopcnt_u16_avx2_harley_seal_improved;
case(PPOPCNT_AVX512): return pospopcnt_u16_avx512;
case(PPOPCNT_AVX512BW_MASK32): return pospopcnt_u16_avx512bw_popcnt32_mask;
case(PPOPCNT_AVX512BW_MASK64): return pospopcnt_u16_avx512bw_popcnt64_mask;
case(PPOPCNT_AVX512_MASKED_OPS): return pospopcnt_u16_avx512_masked_ops;
case(PPOPCNT_AVX512_POPCNT): return pospopcnt_u16_avx512_popcnt;
case(PPOPCNT_AVX512BW_BLEND_POPCNT): return pospopcnt_u16_avx512bw_blend_popcnt;
case(PPOPCNT_AVX512BW_BLEND_POPCNT_UR4): return pospopcnt_u16_avx512bw_blend_popcnt_unroll4;
case(PPOPCNT_AVX512BW_BLEND_POPCNT_UR8): return pospopcnt_u16_avx512bw_blend_popcnt_unroll8;
case(PPOPCNT_AVX512BW_ADDER_FOREST): return pospopcnt_u16_avx512bw_adder_forest;
case(PPOPCNT_AVX512_MULA2): return pospopcnt_u16_avx512_mula2;
case(PPOPCNT_AVX512BW_HARLEY_SEAL): return pospopcnt_u16_avx512bw_harley_seal;
case(PPOPCNT_AVX512VBMI_HARLEY_SEAL): return pospopcnt_u16_avx512vbmi_harley_seal;
case PPOPCNT_NUMBER_METHODS: break; /* -Wswitch */
}
assert(0);
return 0; /* unreachable, but some compilers complain without it */
}
pospopcnt_u8_method_type get_pospopcnt_u8_method(PPOPCNT_U8_METHODS method) {
switch(method) {
case PPOPCNT_U8_AUTO: return pospopcnt_u8_scalar_naive; /* TODO: implement something similar to pospopcnt_u16 */
case PPOPCNT_U8_SCALAR: return pospopcnt_u8_scalar_naive;
case PPOPCNT_U8_SCALAR_NOSIMD: return pospopcnt_u8_scalar_naive_nosimd;
case PPOPCNT_U8_SCALAR_PARTITION: return pospopcnt_u8_scalar_partition;
case PPOPCNT_U8_SCALAR_HIST1X4: return pospopcnt_u8_scalar_hist1x4;
case PPOPCNT_U8_SCALAR_UMUL128: return pospopcnt_u8_scalar_umul128;
case PPOPCNT_U8_SCALAR_UMUL128_UR2: return pospopcnt_u8_scalar_umul128_unroll2;
case PPOPCNT_U8_SSE_SINGLE: return pospopcnt_u8_sse_single;
case PPOPCNT_U8_SSE_BLEND_POPCNT: return pospopcnt_u8_sse_blend_popcnt;
case PPOPCNT_U8_SSE_BLEND_POPCNT_UR4: return pospopcnt_u8_sse_blend_popcnt_unroll4;
case PPOPCNT_U8_SSE_BLEND_POPCNT_UR8: return pospopcnt_u8_sse_blend_popcnt_unroll8;
case PPOPCNT_U8_SSE_BLEND_POPCNT_UR16: return pospopcnt_u8_sse_blend_popcnt_unroll16;
case PPOPCNT_U8_SSE_SAD: return pospopcnt_u8_sse_sad;
case PPOPCNT_U8_SSE_HARLEY_SEAL: return pospopcnt_u8_sse_harley_seal;
case PPOPCNT_U8_SSE_POPCNT4BIT: return pospopcnt_u8_sse_popcnt4bit;
case PPOPCNT_U8_SSE_HORIZREDUCE: return pospopcnt_u8_sse_horizreduce;
case PPOPCNT_U8_AVX2_POPCNT: return pospopcnt_u8_avx2_popcnt;
case PPOPCNT_U8_AVX2: return pospopcnt_u8_avx2;
case PPOPCNT_U8_AVX2_POPCNT_NAIVE: return pospopcnt_u8_avx2_naive_counter;
case PPOPCNT_U8_AVX2_SINGLE: return pospopcnt_u8_avx2_single;
case PPOPCNT_U8_AVX2_LEMIRE1: return pospopcnt_u8_avx2_lemire;
case PPOPCNT_U8_AVX2_LEMIRE2: return pospopcnt_u8_avx2_lemire2;
case PPOPCNT_U8_AVX2_BLEND_POPCNT: return pospopcnt_u8_avx2_blend_popcnt;
case PPOPCNT_U8_AVX2_BLEND_POPCNT_UR4: return pospopcnt_u8_avx2_blend_popcnt_unroll4;
case PPOPCNT_U8_AVX2_BLEND_POPCNT_UR8: return pospopcnt_u8_avx2_blend_popcnt_unroll8;
case PPOPCNT_U8_AVX2_BLEND_POPCNT_UR16: return pospopcnt_u8_avx2_blend_popcnt_unroll16;
case PPOPCNT_U8_AVX2_ADDER_FOREST: return pospopcnt_u8_avx2_adder_forest;
case PPOPCNT_U8_AVX2_HARLEY_SEAL: return pospopcnt_u8_avx2_harley_seal;
case PPOPCNT_U8_AVX2_POPCNT4BIT: return pospopcnt_u8_avx2_popcnt4bit;
case PPOPCNT_U8_AVX2_HORIZREDUCE: return pospopcnt_u8_avx2_horizreduce;
case PPOPCNT_U8_AVX512: return pospopcnt_u8_avx512;
case PPOPCNT_U8_AVX512BW_MASK32: return pospopcnt_u8_avx512bw_popcnt32_mask;
case PPOPCNT_U8_AVX512BW_MASK64: return pospopcnt_u8_avx512bw_popcnt64_mask;
case PPOPCNT_U8_AVX512_MASKED_OPS: return pospopcnt_u8_avx512_masked_ops;
case PPOPCNT_U8_AVX512_POPCNT: return pospopcnt_u8_avx512_popcnt;
case PPOPCNT_U8_AVX512BW_BLEND_POPCNT: return pospopcnt_u8_avx512bw_blend_popcnt;
case PPOPCNT_U8_AVX512BW_BLEND_POPCNT_UR4: return pospopcnt_u8_avx512bw_blend_popcnt_unroll4;
case PPOPCNT_U8_AVX512BW_BLEND_POPCNT_UR8: return pospopcnt_u8_avx512bw_blend_popcnt_unroll8;
case PPOPCNT_U8_AVX512BW_ADDER_FOREST: return pospopcnt_u8_avx512bw_adder_forest;
case PPOPCNT_U8_AVX512_MULA2: return pospopcnt_u8_avx512_mula2;
case PPOPCNT_U8_AVX512BW_HARLEY_SEAL: return pospopcnt_u8_avx512bw_harley_seal;
case PPOPCNT_U8_AVX512BW_POPCNT4BIT: return pospopcnt_u8_avx512bw_popcnt4bit;
case PPOPCNT_U8_AVX512BW_SADBW: return pospopcnt_u8_avx512bw_sadbw;
case PPOPCNT_U8_AVX512VBMI_HARLEY_SEAL: return pospopcnt_u8_avx512vbmi_harley_seal;
case PPOPCNT_U8_NUMBER_METHODS: break; /* -Wswitch */
}
assert(0);
return 0; /* unreachable, but some compilers complain without it */
}
pospopcnt_u32_method_type get_pospopcnt_u32_method(PPOPCNT_U32_METHODS method) {
switch(method) {
case PPOPCNT_U32_AUTO: return pospopcnt_u32_scalar_naive; /* TODO: implement something similar to pospopcnt_u16 */
case PPOPCNT_U32_SCALAR: return pospopcnt_u32_scalar_naive;
case PPOPCNT_U32_SSE_HARLEY_SEAL: return pospopcnt_u32_sse_harley_seal;
case PPOPCNT_U32_SSE_HARLEY_SEAL_IMPROVED: return pospopcnt_u32_sse_harley_seal_improved;
case PPOPCNT_U32_AVX2_HARLEY_SEAL: return pospopcnt_u32_avx2_harley_seal;
case PPOPCNT_U32_AVX2_HARLEY_SEAL_IMPROVED: return pospopcnt_u32_avx2_harley_seal_improved;
case PPOPCNT_U32_NUMBER_METHODS: break; /* -Wswitch */
}
assert(0);
return 0; /* unreachable, but some compilers complain without it */
}
void pospopcnt_u8_scalar_naive(const uint8_t* data, size_t len, uint32_t* out) {
for (size_t i = 0; i < len; ++i) {
for (int j = 0; j < 8; ++j) {
out[j] += ((data[i] & (1 << j)) >> j);
}
}
}
#define pospopcnt_u16_stub(name) \
int name(const uint16_t* data, uint32_t len, uint32_t* flags) { (void)data; (void)len; (void)flags; return(0); }
#define pospopcnt_u8_stub(name) \
void name(const uint8_t* data, size_t len, uint32_t* flags) { (void)data; (void)len; (void)flags; }
#define pospopcnt_u32_stub(name) \
void name(const uint32_t* data, size_t len, uint32_t* flags) { (void)data; (void)len; (void)flags; }
// A pospopcnt 8-bit procedure can be expressed with a 16-bit procedure
// Following macro is used to define such wrapper.
#define make_pospopcnt_u8_from_u16(u8_function, pospopcnt_u16_function) \
void u8_function(const uint8_t* data, size_t len, uint32_t* flags) { \
uint32_t pospopcnt16[16] = {0}; \
pospopcnt_u16_function((uint16_t*)data, len/2, pospopcnt16); \
for (int i=0; i < 8; i++) \
flags[i] = pospopcnt16[i + 0] + pospopcnt16[i + 8]; \
if (len % 2 == 1) /* update the last byte, if len is odd */ \
pospopcnt_u8_scalar_naive_single(data[len - 1], flags); \
}
void pospopcnt_u8_scalar_naive_single(uint8_t data, uint32_t* out) {
for (int i = 0; i < 8; ++i)
out[i] += ((data & (1 << i)) >> i);
}
void pospopcnt_u32_scalar_naive(const uint32_t* data, size_t len, uint32_t* out) {
for (size_t i = 0; i < len; ++i) {
for (int j = 0; j < 32; ++j) {
out[j] += ((data[i] & (1 << j)) >> j);
}
}
}
#if POSPOPCNT_SIMD_VERSION >= 5
int pospopcnt_u16_avx2_popcnt(const uint16_t* data, uint32_t len, uint32_t* flags) {
__m256i masks[16];
__m256i stubs[16];
for (int i = 0; i < 16; ++i) {
masks[i] = _mm256_set1_epi16(1 << i);
stubs[i] = _mm256_set1_epi16(0);
}
uint32_t out_counters[16] = {0};
const __m256i* data_vectors = (const __m256i*)(data);
const uint32_t n_cycles = len / 16;
const uint32_t n_cycles_updates = n_cycles / 16;
#define UPDATE(idx, shift) stubs[idx] = _mm256_or_si256(stubs[idx], _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(_mm256_loadu_si256(data_vectors+pos), masks[idx]), idx), shift));
#define ITERATION(idx) { \
UPDATE(0,idx); UPDATE(1,idx); UPDATE(2,idx); UPDATE(3,idx); \
UPDATE(4,idx); UPDATE(5,idx); UPDATE(6,idx); UPDATE(7,idx); \
UPDATE(8,idx); UPDATE(9,idx); UPDATE(10,idx); UPDATE(11,idx);\
UPDATE(12,idx); UPDATE(13,idx); UPDATE(14,idx); UPDATE(15,idx);\
++pos; \
}
#define BLOCK { \
ITERATION(0); ITERATION(1); ITERATION(2); ITERATION(3); \
ITERATION(4); ITERATION(5); ITERATION(6); ITERATION(7); \
ITERATION(8); ITERATION(9); ITERATION(10); ITERATION(11);\
ITERATION(12); ITERATION(13); ITERATION(14); ITERATION(15);\
}
uint32_t pos = 0;
for (size_t i = 0; i < n_cycles_updates; ++i) {
BLOCK // unrolled
// Not unrolled
/*
for (int c = 0; c < 16; ++c) { // 16 iterations per register
ITERATION(c)
// for (int j = 0; j < 16; ++j) { // each 1-hot per register
// UPDATE(j,c)
// }
}
*/
for (int j = 0; j < 16; ++j) {
PIL_POPCOUNT_AVX2(out_counters[j], stubs[j])
stubs[j] = _mm256_set1_epi16(0);
}
}
// residual
for (size_t i = pos*16; i < len; ++i) {
for (int j = 0; j < 16; ++j) {
out_counters[j] += ((data[i] & (1 << j)) >> j);
}
}
for (int i = 0; i < 16; ++i)
flags[i] = out_counters[i];
#undef BLOCK
#undef ITERATION
#undef UPDATE
return 0;
}
int pospopcnt_u16_avx2(const uint16_t* data, uint32_t len, uint32_t* flags) {
__m256i masks[16];
__m256i counters[16];
for (int i = 0; i < 16; ++i) {
masks[i] = _mm256_set1_epi16(1 << i);
counters[i] = _mm256_set1_epi16(0);
}
uint32_t out_counters[16] = {0};
const __m256i hi_mask = _mm256_set1_epi32((int32_t)0xFFFF0000);
const __m256i lo_mask = _mm256_set1_epi32(0x0000FFFF);
const __m256i* data_vectors = (const __m256i*)(data);
const uint32_t n_cycles = len / 16;
const uint32_t n_update_cycles = n_cycles / 65536;
#define UPDATE(idx) counters[idx] = _mm256_add_epi16(counters[idx], _mm256_srli_epi16(_mm256_and_si256(_mm256_loadu_si256(data_vectors+pos), masks[idx]), idx))
#define ITERATION { \
UPDATE(0); UPDATE(1); UPDATE(2); UPDATE(3); \
UPDATE(4); UPDATE(5); UPDATE(6); UPDATE(7); \
UPDATE(8); UPDATE(9); UPDATE(10); UPDATE(11);\
UPDATE(12); UPDATE(13); UPDATE(14); UPDATE(15);\
++pos; \
}
uint32_t pos = 0;
for (size_t i = 0; i < n_update_cycles; ++i) { // each block of 2^16 values
for (int k = 0; k < 65536; ++k) // max sum of each 16-bit value in a register
ITERATION // unrolled
// Compute vector sum
for (int k = 0; k < 16; ++k) { // each flag register
// Accumulator
// ((16-bit high & 16 high) >> 16) + (16-bit low & 16-low)
__m256i x = _mm256_add_epi32(
_mm256_srli_epi32(_mm256_and_si256(counters[k], hi_mask), 16),
_mm256_and_si256(counters[k], lo_mask));
__m256i t1 = _mm256_hadd_epi32(x,x);
__m256i t2 = _mm256_hadd_epi32(t1,t1);
__m128i t4 = _mm_add_epi32(_mm256_castsi256_si128(t2),_mm256_extractf128_si256(t2,1));
out_counters[k] += _mm_cvtsi128_si32(t4);
/*
// Naive counter
uint16_t* d = (uint16_t*)(&counters[k]);
for (int j = 0; j < 16; ++j) { // each uint16_t in the register
out_counters[k] += d[j];
}
*/
counters[k] = _mm256_set1_epi16(0);
}
}
// residual
for (size_t i = pos*16; i < len; ++i) {
for (int j = 0; j < 16; ++j)
out_counters[j] += ((data[i] & (1 << j)) >> j);
}
for (int i = 0; i < 16; ++i) flags[i] = out_counters[i];
#undef ITERATION
#undef UPDATE
return 0;
}
int pospopcnt_u16_avx2_naive_counter(const uint16_t* data, uint32_t len, uint32_t* flags) {
__m256i masks[16];
__m256i counters[16];
for (int i = 0; i < 16; ++i) {
masks[i] = _mm256_set1_epi16(1 << i);
counters[i] = _mm256_set1_epi16(0);
}
uint32_t out_counters[16] = {0};
const __m256i* data_vectors = (const __m256i*)(data);
const uint32_t n_cycles = len / 16;
const uint32_t n_update_cycles = n_cycles / 65536;
#define UPDATE(idx) counters[idx] = _mm256_add_epi16(counters[idx], _mm256_srli_epi16(_mm256_and_si256(_mm256_loadu_si256(data_vectors+pos), masks[idx]), idx))
uint32_t pos = 0;
for (size_t i = 0; i < n_update_cycles; ++i) { // each block of 2^16 values
for (int k = 0; k < 65536; ++pos,++k) { // max sum of each 16-bit value in a register
for (int p = 0; p < 16; ++p) // Not unrolled
UPDATE(p);
}
// Compute vector sum
for (int k = 0; k < 16; ++k) { // each flag register
// Naive counter
uint16_t* d = (uint16_t*)(&counters[k]);
for (int j = 0; j < 16; ++j) // each uint16_t in the register
out_counters[k] += d[j];
counters[k] = _mm256_set1_epi16(0);
}
}
// residual
for (size_t i = pos*16; i < len; ++i) {
for (int j = 0; j < 16; ++j)
out_counters[j] += ((data[i] & (1 << j)) >> j);
}
for (int i = 0; i < 16; ++i) flags[i] = out_counters[i];
#undef UPDATE
return 0;
}
int pospopcnt_u16_avx2_single(const uint16_t* data, uint32_t len, uint32_t* flags) {
__m256i counter = _mm256_set1_epi16(0);
const __m256i one_mask = _mm256_set1_epi16(1);
// set_epi is parameterized backwards (15->0)
const __m256i masks = _mm256_set_epi16(-32768, 1 << 14, 1 << 13, 1 << 12,
1 << 11, 1 << 10, 1 << 9, 1 << 8,
1 << 7, 1 << 6, 1 << 5, 1 << 4,
1 << 3, 1 << 2, 1 << 1, 1 << 0);
uint32_t out_counters[16] = {0};
const __m256i* data_vectors = (const __m256i*)(data);
const uint32_t n_cycles = len / 16;
const uint32_t n_update_cycles = n_cycles / 4096;
#define UPDATE(idx) counter = _mm256_add_epi16(counter, _mm256_and_si256(_mm256_cmpeq_epi16(_mm256_and_si256(_mm256_set1_epi16(_mm256_extract_epi16(_mm256_loadu_si256(data_vectors+pos), idx)), masks), masks), one_mask));
#define BLOCK { \
UPDATE(0) UPDATE(1) UPDATE(2) UPDATE(3) \
UPDATE(4) UPDATE(5) UPDATE(6) UPDATE(7) \
UPDATE(8) UPDATE(9) UPDATE(10) UPDATE(11) \
UPDATE(12) UPDATE(13) UPDATE(14) UPDATE(15) \
}
uint32_t pos = 0;
for (size_t i = 0; i < n_update_cycles; ++i) { // each block of 65536 values
for (int k = 0; k < 4096; ++k, ++pos) { // max sum of each 16-bit value in a register (65536/16)
BLOCK
}
// Compute vector sum
// Unroll to prevent clang (LLVM) from throwing constant integer error such as:
// error: argument to '__builtin_ia32_vec_ext_v16hi' must be a constant integer
out_counters[0] += _mm256_extract_epi16(counter, 0);
out_counters[1] += _mm256_extract_epi16(counter, 1);
out_counters[2] += _mm256_extract_epi16(counter, 2);
out_counters[3] += _mm256_extract_epi16(counter, 3);
out_counters[4] += _mm256_extract_epi16(counter, 4);
out_counters[5] += _mm256_extract_epi16(counter, 5);
out_counters[6] += _mm256_extract_epi16(counter, 6);
out_counters[7] += _mm256_extract_epi16(counter, 7);
out_counters[8] += _mm256_extract_epi16(counter, 8);
out_counters[9] += _mm256_extract_epi16(counter, 9);
out_counters[10] += _mm256_extract_epi16(counter, 10);
out_counters[11] += _mm256_extract_epi16(counter, 11);
out_counters[12] += _mm256_extract_epi16(counter, 12);
out_counters[13] += _mm256_extract_epi16(counter, 13);
out_counters[14] += _mm256_extract_epi16(counter, 14);
out_counters[15] += _mm256_extract_epi16(counter, 15);
counter = _mm256_set1_epi16(0);
}
#undef UPDATE
#undef BLOCK
// residual
for (size_t i = pos*16; i < len; ++i) {
for (int j = 0; j < 16; ++j)
out_counters[j] += ((data[i] & (1 << j)) >> j);
}
for (int i = 0; i < 16; ++i)
flags[i] = out_counters[i];
return 0;
}
make_pospopcnt_u8_from_u16(pospopcnt_u8_avx2_popcnt, pospopcnt_u16_avx2_popcnt)
make_pospopcnt_u8_from_u16(pospopcnt_u8_avx2, pospopcnt_u16_avx2)
make_pospopcnt_u8_from_u16(pospopcnt_u8_avx2_naive_counter, pospopcnt_u16_avx2_naive_counter)
make_pospopcnt_u8_from_u16(pospopcnt_u8_avx2_single, pospopcnt_u16_avx2_single)
#else
pospopcnt_u16_stub(pospopcnt_u16_avx2_popcnt)
pospopcnt_u16_stub(pospopcnt_u16_avx2)
pospopcnt_u16_stub(pospopcnt_u16_avx2_naive_counter)
pospopcnt_u16_stub(pospopcnt_u16_avx2_single)
pospopcnt_u8_stub(pospopcnt_u8_avx2_popcnt)
pospopcnt_u8_stub(pospopcnt_u8_avx2)
pospopcnt_u8_stub(pospopcnt_u8_avx2_naive_counter)
pospopcnt_u8_stub(pospopcnt_u8_avx2_single)
#endif
#if POSPOPCNT_SIMD_VERSION >= 3
int pospopcnt_u16_sse_single(const uint16_t* data, uint32_t len, uint32_t* flags) {
__m128i counterLo = _mm_set1_epi16(0);
__m128i counterHi = _mm_set1_epi16(0);
const __m128i one_mask = _mm_set1_epi16(1);
// set_epi is parameterized backwards (15->0)
const __m128i masksLo = _mm_set_epi16(-32768, 1 << 14, 1 << 13, 1 << 12,
1 << 11, 1 << 10, 1 << 9, 1 << 8);
const __m128i masksHi = _mm_set_epi16(1 << 7, 1 << 6, 1 << 5, 1 << 4,
1 << 3, 1 << 2, 1 << 1, 1 << 0);
uint32_t out_counters[16] = {0};
const __m128i* data_vectors = (const __m128i*)(data);
const uint32_t n_cycles = len / 8;
const uint32_t n_update_cycles = n_cycles / 4096;
#define UPDATE_LO(idx) counterLo = _mm_add_epi16(counterLo, _mm_and_si128(_mm_cmpeq_epi16(_mm_and_si128(_mm_set1_epi16(_mm_extract_epi16(_mm_loadu_si128(data_vectors+pos), idx)), masksLo), masksLo), one_mask));
#define UPDATE_HI(idx) counterHi = _mm_add_epi16(counterHi, _mm_and_si128(_mm_cmpeq_epi16(_mm_and_si128(_mm_set1_epi16(_mm_extract_epi16(_mm_loadu_si128(data_vectors+pos), idx)), masksHi), masksHi), one_mask));
#define BLOCK { \
UPDATE_LO(0) UPDATE_LO(1) UPDATE_LO(2) UPDATE_LO(3) \
UPDATE_LO(4) UPDATE_LO(5) UPDATE_LO(6) UPDATE_LO(7) \
UPDATE_HI(0) UPDATE_HI(1) UPDATE_HI(2) UPDATE_HI(3) \
UPDATE_HI(4) UPDATE_HI(5) UPDATE_HI(6) UPDATE_HI(7) \
}
#define UH(idx) out_counters[idx] += _mm_extract_epi16(counterLo, idx - 8);
#define UL(idx) out_counters[idx] += _mm_extract_epi16(counterHi, idx);
uint32_t pos = 0;
for (size_t i = 0; i < n_update_cycles; ++i) { // each block of 65536 values
for (int k = 0; k < 4096; ++k, ++pos) { // max sum of each 16-bit value in a register (65536/16)
BLOCK
}
// Compute vector sum (unroll to prevent possible compiler errors
// regarding constness of parameter N in _mm_extract_epi16).
UL(0) UL(1) UL(2) UL(3)
UL(4) UL(5) UL(6) UL(7)
UH(8) UH(9) UH(10) UH(11)
UH(12) UH(13) UH(14) UH(15)
counterLo = _mm_set1_epi16(0);
counterHi = _mm_set1_epi16(0);
}
#undef UL
#undef UH
#undef BLOCK
#undef UPDATE_HI
#undef UPDATE_LO
// residual
for (size_t i = pos*8; i < len; ++i) {
for (int j = 0; j < 16; ++j)
out_counters[j] += ((data[i] & (1 << j)) >> j);
}
for (int i = 0; i < 16; ++i) flags[i] = out_counters[i];
return 0;
}
// By @aqrit (https://github.com/aqrit)
// @see: https://gist.github.com/aqrit/cb52b2ac5b7d0dfe9319c09d27237bf3
int pospopcnt_u16_sse_sad(const uint16_t* data, uint32_t len, uint32_t* flag_counts) {
const __m128i zero = _mm_setzero_si128();
const __m128i mask_lo_byte = _mm_srli_epi16(_mm_cmpeq_epi8(zero, zero), 8);
const __m128i mask_lo_cnt = _mm_srli_epi16(mask_lo_byte, 2);
const __m128i mask_bits_a = _mm_set1_epi8(0x41); // 01000001
const __m128i mask_bits_b = _mm_add_epi8(mask_bits_a, mask_bits_a);
uint32_t buffer[16];
__m128i counterA = zero;
__m128i counterB = zero;
__m128i counterC = zero;
__m128i counterD = zero;
for (const uint16_t* end = &data[(len & ~31)]; data != end; data += 32) {
__m128i r0 = _mm_loadu_si128((__m128i*)&data[0]);
__m128i r1 = _mm_loadu_si128((__m128i*)&data[8]);
__m128i r2 = _mm_loadu_si128((__m128i*)&data[16]);
__m128i r3 = _mm_loadu_si128((__m128i*)&data[24]);
__m128i r4, r5, r6, r7;
// seperate LOBYTE and HIBYTE of each WORD
// (emulate PSHUFB F,D,B,9,7,5,3,1, E,C,A,8,6,4,2,0)
r4 = _mm_and_si128(mask_lo_byte, r0);
r5 = _mm_and_si128(mask_lo_byte, r1);
r6 = _mm_and_si128(mask_lo_byte, r2);
r7 = _mm_and_si128(mask_lo_byte, r3);
r0 = _mm_srli_epi16(r0, 8);
r1 = _mm_srli_epi16(r1, 8);
r2 = _mm_srli_epi16(r2, 8);
r3 = _mm_srli_epi16(r3, 8);
r0 = _mm_packus_epi16(r0, r4);
r1 = _mm_packus_epi16(r1, r5);
r2 = _mm_packus_epi16(r2, r6);
r3 = _mm_packus_epi16(r3, r7);
// isolate bits to count
r4 = _mm_and_si128(mask_bits_a, r0);
r5 = _mm_and_si128(mask_bits_a, r1);
r6 = _mm_and_si128(mask_bits_a, r2);
r7 = _mm_and_si128(mask_bits_a, r3);
// horizontal sum of qwords
r4 = _mm_sad_epu8(r4, zero);
r5 = _mm_sad_epu8(r5, zero);
r6 = _mm_sad_epu8(r6, zero);
r7 = _mm_sad_epu8(r7, zero);
// sum 6-bit counts
r4 = _mm_add_epi16(r4,r5);
r4 = _mm_add_epi16(r4,r6);
r4 = _mm_add_epi16(r4,r7);
// unpack 6-bit counts to 32-bits
r5 = _mm_and_si128(mask_lo_cnt, r4);
r4 = _mm_srli_epi16(r4, 6);
r4 = _mm_packs_epi32(r4, r5);
// accumulate
counterA = _mm_add_epi32(counterA, r4);
// do it again...
r4 = _mm_and_si128(mask_bits_b, r0);
r5 = _mm_and_si128(mask_bits_b, r1);
r6 = _mm_and_si128(mask_bits_b, r2);
r7 = _mm_and_si128(mask_bits_b, r3);
r4 = _mm_sad_epu8(r4, zero);
r5 = _mm_sad_epu8(r5, zero);
r6 = _mm_sad_epu8(r6, zero);
r7 = _mm_sad_epu8(r7, zero);
r4 = _mm_add_epi16(r4,r5);
r4 = _mm_add_epi16(r4,r6);
r4 = _mm_add_epi16(r4,r7);
r5 = _mm_avg_epu8(zero, r4); // shift right 1
r5 = _mm_and_si128(r5, mask_lo_cnt);
r4 = _mm_srli_epi16(r4, 7);
r4 = _mm_packs_epi32(r4, r5);
counterB = _mm_add_epi32(counterB, r4); // accumulate
// rotate right 4
r4 = _mm_slli_epi16(r0, 12);
r5 = _mm_slli_epi16(r1, 12);
r6 = _mm_slli_epi16(r2, 12);
r7 = _mm_slli_epi16(r3, 12);
r0 = _mm_srli_epi16(r0, 4);
r1 = _mm_srli_epi16(r1, 4);
r2 = _mm_srli_epi16(r2, 4);
r3 = _mm_srli_epi16(r3, 4);
r0 = _mm_or_si128(r0, r4);
r1 = _mm_or_si128(r1, r5);
r2 = _mm_or_si128(r2, r6);
r3 = _mm_or_si128(r3, r7);
// do it again...
r4 = _mm_and_si128(mask_bits_a, r0);
r5 = _mm_and_si128(mask_bits_a, r1);
r6 = _mm_and_si128(mask_bits_a, r2);
r7 = _mm_and_si128(mask_bits_a, r3);
r4 = _mm_sad_epu8(r4, zero);
r5 = _mm_sad_epu8(r5, zero);
r6 = _mm_sad_epu8(r6, zero);
r7 = _mm_sad_epu8(r7, zero);
r4 = _mm_add_epi16(r4,r5);
r4 = _mm_add_epi16(r4,r6);
r4 = _mm_add_epi16(r4,r7);
r5 = _mm_and_si128(mask_lo_cnt, r4);
r4 = _mm_srli_epi16(r4, 6);
r4 = _mm_packs_epi32(r4, r5);
counterC = _mm_add_epi32(counterC, r4); // accumulate
// do it again...
r0 = _mm_and_si128(r0, mask_bits_b);
r1 = _mm_and_si128(r1, mask_bits_b);
r2 = _mm_and_si128(r2, mask_bits_b);
r3 = _mm_and_si128(r3, mask_bits_b);
r0 = _mm_sad_epu8(r0, zero);
r1 = _mm_sad_epu8(r1, zero);
r2 = _mm_sad_epu8(r2, zero);
r3 = _mm_sad_epu8(r3, zero);
r0 = _mm_add_epi16(r0,r1);
r0 = _mm_add_epi16(r0,r2);
r0 = _mm_add_epi16(r0,r3);
r1 = _mm_avg_epu8(zero, r0);
r1 = _mm_and_si128(r1, mask_lo_cnt);
r0 = _mm_srli_epi16(r0, 7);
r0 = _mm_packs_epi32(r0, r1);
counterD = _mm_add_epi32(counterD, r0); // accumulate
}
// transpose then store counters
__m128i counter_1098 = _mm_unpackhi_epi32(counterA, counterB);
__m128i counter_76FE = _mm_unpacklo_epi32(counterA, counterB);
__m128i counter_32BA = _mm_unpacklo_epi32(counterC, counterD);
__m128i counter_54DC = _mm_unpackhi_epi32(counterC, counterD);
__m128i counter_7654 = _mm_unpackhi_epi64(counter_54DC, counter_76FE);
__m128i counter_FEDC = _mm_unpacklo_epi64(counter_54DC, counter_76FE);
__m128i counter_3210 = _mm_unpackhi_epi64(counter_1098, counter_32BA);
__m128i counter_BA98 = _mm_unpacklo_epi64(counter_1098, counter_32BA);
_mm_storeu_si128((__m128i*)&buffer[0], counter_3210);
_mm_storeu_si128((__m128i*)&buffer[4], counter_7654);
_mm_storeu_si128((__m128i*)&buffer[8], counter_BA98);
_mm_storeu_si128((__m128i*)&buffer[12], counter_FEDC);
for (int i = 0; i < 16; ++i) flag_counts[i] += buffer[i];
// scalar tail loop
int tail = len & 31;
if (tail != 0) {
uint64_t countsA = 0;
uint64_t countsB = 0;
do {
// zero-extend a bit to 8-bits then accumulate
// (emulate pdep)
const uint64_t mask_01 = UINT64_C(0x0101010101010101);// 100000001000000010000000100000001000000010000000100000001
const uint64_t magic = UINT64_C(0x0000040010004001);// 000000000000001000000000000010000000000000100000000000001
// 1+(1<<14)+(1<<28)+(1<<42)
uint64_t x = *data++;
countsA += ((x & 0x5555) * magic) & mask_01; // 0101010101010101
countsB += (((x >> 1) & 0x5555) * magic) & mask_01;
} while (--tail);
// transpose then store counters
flag_counts[0] += countsA & 0xFF;
flag_counts[8] += (countsA >> 8) & 0xFF;
flag_counts[2] += (countsA >> 16) & 0xFF;
flag_counts[10] += (countsA >> 24) & 0xFF;
flag_counts[4] += (countsA >> 32) & 0xFF;
flag_counts[12] += (countsA >> 40) & 0xFF;
flag_counts[6] += (countsA >> 48) & 0xFF;
flag_counts[14] += (countsA >> 56) & 0xFF;
flag_counts[1] += countsB & 0xFF;
flag_counts[9] += (countsB >> 8) & 0xFF;
flag_counts[3] += (countsB >> 16) & 0xFF;
flag_counts[11] += (countsB >> 24) & 0xFF;
flag_counts[5] += (countsB >> 32) & 0xFF;
flag_counts[13] += (countsB >> 40) & 0xFF;
flag_counts[7] += (countsB >> 48) & 0xFF;
flag_counts[15] += (countsB >> 56) & 0xFF;
}
return 0;
}
make_pospopcnt_u8_from_u16(pospopcnt_u8_sse_single, pospopcnt_u16_sse_single)
make_pospopcnt_u8_from_u16(pospopcnt_u8_sse_sad, pospopcnt_u16_sse_sad)
#else
pospopcnt_u16_stub(pospopcnt_u16_sse_single)
pospopcnt_u16_stub(pospopcnt_u16_sse_sad)
pospopcnt_u8_stub(pospopcnt_u8_sse_single)
pospopcnt_u8_stub(pospopcnt_u8_sse_sad)
#endif
#if !defined(__clang__) && !defined(_MSC_VER)
__attribute__((optimize("no-tree-vectorize")))
#endif
int pospopcnt_u16_scalar_naive_nosimd(const uint16_t* data, uint32_t len, uint32_t* flags) {
for (uint32_t i = 0; i < len; ++i) {
for (int j = 0; j < 16; ++j) {
flags[j] += ((data[i] & (1 << j)) >> j);
}
}
return 0;
}
int pospopcnt_u16_scalar_naive(const uint16_t* data, uint32_t len, uint32_t* flags) {
for (uint32_t i = 0; i < len; ++i) {
for (int j = 0; j < 16; ++j) {
flags[j] += ((data[i] & (1 << j)) >> j);
}
}
return 0;
}
int pospopcnt_u16_scalar_partition(const uint16_t* data, uint32_t len, uint32_t* flags) {
uint32_t low[256] = {0}, high[256] = {0};
for (uint32_t i = 0; i < len; ++i) {
++low[data[i] & 255];
++high[(data[i] >> 8) & 255];
}
for (int i = 0; i < 256; ++i) {
for (int k = 0; k < 8; ++k) {
flags[k] += ((i & (1 << k)) >> k) * low[i];
}
}
for (int i = 0; i < 256; ++i) {
for (int k = 0; k < 8; ++k) {
flags[k+8] += ((i & (1 << k)) >> k) * high[i];
}
}
return 0;
}
int pospopcnt_u16_scalar_hist1x4(const uint16_t* data, uint32_t len, uint32_t* flags) {
uint32_t low[256] = {0}, high[256] = {0};
uint32_t i = 0;
for (i = 0; i < (len & ~3); i+=4) {
++low[data[i+0] & 255];
++high[(data[i+0] >> 8) & 255];
++low[data[i+1] & 255];
++high[(data[i+1] >> 8) & 255];
++low[data[i+2] & 255];
++high[(data[i+2] >> 8) & 255];
++low[data[i+3] & 255];
++high[(data[i+3] >> 8) & 255];
}
while (i < len) {
++low[data[i] & 255];
++high[(data[i++] >> 8) & 255];
}
for (int i = 0; i < 256; ++i) {
for (int k = 0; k < 8; ++k) {
flags[k] += ((i & (1 << k)) >> k) * low[i];
}
}
for (int i = 0; i < 256; ++i) {
for (int k = 0; k < 8; ++k) {
flags[k+8] += ((i & (1 << k)) >> k) * high[i];
}
}
return 0;
}
make_pospopcnt_u8_from_u16(pospopcnt_u8_scalar_partition, pospopcnt_u16_scalar_partition)
make_pospopcnt_u8_from_u16(pospopcnt_u8_scalar_naive_nosimd, pospopcnt_u16_scalar_naive_nosimd)
make_pospopcnt_u8_from_u16(pospopcnt_u8_scalar_hist1x4, pospopcnt_u16_scalar_hist1x4)
#ifndef _MSC_VER
// By @aqrit (https://github.com/aqrit)
// @see: https://gist.github.com/aqrit/c729815b0165c139d0bac642ab7ee104
int pospopcnt_u16_scalar_umul128(const uint16_t* in, uint32_t n, uint32_t* out) {
const uint64_t mask_bits = UINT64_C(0x1249124912491249); // 00000000 00000100 01110000 00010010 10001110 10110000 01101010 11110001
const uint64_t mask_cnts = UINT64_C(0x003801C00E007007); // 00000000 00111000 00000001 11000000 00001110 00000000 01110000 00001000
const uint64_t mask_0001 = UINT64_C(0x0001000100010001); // 00000000 00000001 00000000 00000001 00000000 00000001 00000000 00000001
while (n >= 4) {
uint64_t counter_a = 0; // 5 packed 12-bit counters (4 unused bits).
uint64_t counter_b = 0;
uint64_t counter_c = 0;
uint64_t counter_d = 0;
// Terminate before overflowing the counters.
uint32_t len = ((n < 0x0FFF) ? n : 0x0FFF) & ~3;
n -= len;
for (const uint16_t* end = &in[len]; in != end; in += 4) {
uint64_t v = pospopcnt_loadu_u64(in);
uint64_t a = v & mask_bits; // 0b0001001001001001
uint64_t b = (v >> 1) & mask_bits; // 0b0010010010010010 >> 1
uint64_t c = (v >> 2) & mask_bits; // 0b0100100100100100 >> 2
uint64_t d = (v >> 15) & mask_0001; // 0b1000000000000000 >> 15
uint64_t hi_a, hi_b, hi_c;
a = pospopcnt_umul128(a, mask_0001, &hi_a);
b = pospopcnt_umul128(b, mask_0001, &hi_b);
c = pospopcnt_umul128(c, mask_0001, &hi_c);
a += hi_a; // Broadcast 3-bit counts.
b += hi_b;
c += hi_c;
counter_a += a & mask_cnts;
counter_b += b & mask_cnts;
counter_c += c & mask_cnts;
counter_d += d;
}
out[0] += counter_a & 0x0FFF;
out[1] += counter_b & 0x0FFF;
out[2] += counter_c & 0x0FFF;
out[3] += (counter_a >> 51);
out[4] += (counter_b >> 51);
out[5] += (counter_c >> 51);
out[6] += (counter_a >> 38) & 0x0FFF;
out[7] += (counter_b >> 38) & 0x0FFF;
out[8] += (counter_c >> 38) & 0x0FFF;
out[9] += (counter_a >> 25) & 0x0FFF;
out[10] += (counter_b >> 25) & 0x0FFF;
out[11] += (counter_c >> 25) & 0x0FFF;
out[12] += (counter_a >> 12) & 0x0FFF;
out[13] += (counter_b >> 12) & 0x0FFF;
out[14] += (counter_c >> 12) & 0x0FFF;
out[15] += (counter_d * mask_0001) >> 48;
}
// Residual words.
for (const uint16_t* tail_end = &in[n]; in != tail_end; ++in) {
uint16_t x = *in;
for (int i = 0; i != 16; ++i) {
out[i] += x & 1;
x >>= 1;
}
}
return 0;
}
int pospopcnt_u16_scalar_umul128_unroll2(const uint16_t* in, uint32_t n, uint32_t* out) {
while (n >= 8) {
uint64_t counter_a = 0; // 4 packed 12-bit counters
uint64_t counter_b = 0;
uint64_t counter_c = 0;
uint64_t counter_d = 0;
// end before overflowing the counters
uint32_t len = ((n < 0x0FFF) ? n : 0x0FFF) & ~7;
n -= len;
for (const uint16_t* end = &in[len]; in != end; in += 8) {
const uint64_t mask_a = UINT64_C(0x1111111111111111);
const uint64_t mask_b = mask_a + mask_a;
const uint64_t mask_c = mask_b + mask_b;
const uint64_t mask_0001 = UINT64_C(0x0001000100010001);
const uint64_t mask_cnts = UINT64_C(0x000000F00F00F00F);
uint64_t v0 = pospopcnt_loadu_u64(&in[0]);
uint64_t v1 = pospopcnt_loadu_u64(&in[4]);
uint64_t a = (v0 & mask_a) + (v1 & mask_a);
uint64_t b = ((v0 & mask_b) + (v1 & mask_b)) >> 1;
uint64_t c = ((v0 & mask_c) + (v1 & mask_c)) >> 2;
uint64_t d = ((v0 >> 3) & mask_a) + ((v1 >> 3) & mask_a);
uint64_t hi;
a = pospopcnt_umul128(a, mask_0001, &hi);
a += hi; // broadcast 4-bit counts
b = pospopcnt_umul128(b, mask_0001, &hi);
b += hi;
c = pospopcnt_umul128(c, mask_0001, &hi);
c += hi;
d = pospopcnt_umul128(d, mask_0001, &hi);
d += hi;
counter_a += a & mask_cnts;
counter_b += b & mask_cnts;
counter_c += c & mask_cnts;
counter_d += d & mask_cnts;
}
out[0] += counter_a & 0x0FFF;
out[1] += counter_b & 0x0FFF;
out[2] += counter_c & 0x0FFF;
out[3] += counter_d & 0x0FFF;
out[4] += (counter_a >> 36);
out[5] += (counter_b >> 36);
out[6] += (counter_c >> 36);
out[7] += (counter_d >> 36);
out[8] += (counter_a >> 24) & 0x0FFF;
out[9] += (counter_b >> 24) & 0x0FFF;
out[10] += (counter_c >> 24) & 0x0FFF;
out[11] += (counter_d >> 24) & 0x0FFF;
out[12] += (counter_a >> 12) & 0x0FFF;
out[13] += (counter_b >> 12) & 0x0FFF;
out[14] += (counter_c >> 12) & 0x0FFF;
out[15] += (counter_d >> 12) & 0x0FFF;
}
// assert(n < 8)
if (n != 0) {
uint64_t tail_counter_a = 0;
uint64_t tail_counter_b = 0;
do { // zero-extend a bit to 8-bits (emulate pdep) then accumulate
const uint64_t mask_01 = UINT64_C(0x0101010101010101);
const uint64_t magic = UINT64_C(0x0000040010004001); // 1+(1<<14)+(1<<28)+(1<<42)
uint64_t x = *in++;
tail_counter_a += ((x & 0x5555) * magic) & mask_01; // 0101010101010101
tail_counter_b += (((x >> 1) & 0x5555) * magic) & mask_01;
} while (--n);
out[0] += tail_counter_a & 0xFF;
out[8] += (tail_counter_a >> 8) & 0xFF;
out[2] += (tail_counter_a >> 16) & 0xFF;
out[10] += (tail_counter_a >> 24) & 0xFF;
out[4] += (tail_counter_a >> 32) & 0xFF;
out[12] += (tail_counter_a >> 40) & 0xFF;
out[6] += (tail_counter_a >> 48) & 0xFF;
out[14] += (tail_counter_a >> 56) & 0xFF;
out[1] += tail_counter_b & 0xFF;
out[9] += (tail_counter_b >> 8) & 0xFF;
out[3] += (tail_counter_b >> 16) & 0xFF;
out[11] += (tail_counter_b >> 24) & 0xFF;
out[5] += (tail_counter_b >> 32) & 0xFF;
out[13] += (tail_counter_b >> 40) & 0xFF;
out[7] += (tail_counter_b >> 48) & 0xFF;
out[15] += (tail_counter_b >> 56) & 0xFF;
}
return 0;
}
make_pospopcnt_u8_from_u16(pospopcnt_u8_scalar_umul128, pospopcnt_u16_scalar_umul128)
make_pospopcnt_u8_from_u16(pospopcnt_u8_scalar_umul128_unroll2, pospopcnt_u16_scalar_umul128_unroll2)
#else
pospopcnt_u16_stub(pospopcnt_u16_scalar_umul128)
pospopcnt_u16_stub(pospopcnt_u16_scalar_umul128_unroll2)
pospopcnt_u8_stub(pospopcnt_u8_scalar_umul128)
pospopcnt_u8_stub(pospopcnt_u8_scalar_umul128_unroll2)
#endif
#if POSPOPCNT_SIMD_VERSION >= 6
#if defined(__AVX512BW__) && __AVX512BW__ == 1
int pospopcnt_u16_avx512bw_popcnt32_mask(const uint16_t* data, uint32_t len, uint32_t* flags) {
__m512i masks[16];
for (int i = 0; i < 16; ++i) {
masks[i] = _mm512_set1_epi32(((1 << i) << 16) | (1 << i));
}
uint32_t out_counters[16] = {0};
const __m512i* data_vectors = (const __m512i*)(data);
const uint32_t n_cycles = len / 32;