-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlibalgebra.h
3557 lines (2995 loc) · 116 KB
/
libalgebra.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
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
// License for libalgebra.h
/*
* Copyright (c) 2019 Marcus D. R. Klarqvist
* Author(s): Marcus D. R. Klarqvist
*
* 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.
*/
// License for pospopcnt.h
/*
* 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.
*/
// License for libpopcnt.h
/*
* libpopcnt.h - C/C++ library for counting the number of 1 bits (bit
* population count) in an array as quickly as possible using
* specialized CPU instructions i.e. POPCNT, AVX2, AVX512, NEON.
*
* Copyright (c) 2016 - 2018, Kim Walisch
* Copyright (c) 2016 - 2018, Wojciech Muła
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef LIBALGEBRA_H_8723467365934
#define LIBALGEBRA_H_8723467365934
/* *************************************
* Includes
***************************************/
#include <stdint.h>
#include <assert.h>
#include <memory.h>
#include <string.h>
#include <math.h>
/* *************************************
* Safety
***************************************/
#if !(defined(__APPLE__)) && !(defined(__FreeBSD__))
#include <malloc.h> // this should never be needed but there are some reports that it is needed.
#endif
#if defined(__SIZEOF_LONG_LONG__) && __SIZEOF_LONG_LONG__ != 8
#error This code assumes 64-bit long longs (by use of the GCC intrinsics). Your system is not currently supported.
#endif
/****************************
* Memory management
*
* The subroutines aligned_malloc and aligned_free had to be renamed to
* STORM_aligned_malloc and STORM_aligned_free to prevent clashing with the
* same subroutines in Roaring. These subroutines are included here
* since there is no hard dependency on using Roaring bitmaps.
*
* These subroutines and definitions are taken from the CRoaring repo
* by Daniel Lemire et al. available under the Apache 2.0 License
* (same as libalgebra.h):
* https://github.com/RoaringBitmap/CRoaring/
****************************/
// portable version of posix_memalign
#ifndef _MSC_VER
#include <x86intrin.h>
#endif
#ifndef STORM_aligned_malloc
static
void* STORM_aligned_malloc(size_t alignment, size_t size) {
void *p;
#ifdef _MSC_VER
p = _aligned_malloc(size, alignment);
#elif defined(__MINGW32__) || defined(__MINGW64__)
p = __mingw_aligned_malloc(size, alignment);
#else
// somehow, if this is used before including "x86intrin.h", it creates an
// implicit defined warning.
if (posix_memalign(&p, alignment, size) != 0)
return NULL;
#endif
return p;
}
#endif
#ifndef STORM_aligned_free
static
void STORM_aligned_free(void* memblock) {
#ifdef _MSC_VER
_aligned_free(memblock);
#elif defined(__MINGW32__) || defined(__MINGW64__)
__mingw_aligned_free(memblock);
#else
free(memblock);
#endif
}
#endif
// portable alignment
#if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) /* C11+ */
# include <stdalign.h>
# define STORM_ALIGN(n) alignas(n)
#elif defined(__GNUC__)
# define STORM_ALIGN(n) __attribute__ ((aligned(n)))
#elif defined(_MSC_VER)
# define STORM_ALIGN(n) __declspec(align(n))
#else
# define STORM_ALIGN(n) /* disabled */
#endif
/* *************************************
* Compiler Specific Options
***************************************/
// Taken from XXHASH
#ifdef _MSC_VER /* Visual Studio */
# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
# define STORM_FORCE_INLINE static __forceinline
# define STORM_NO_INLINE static __declspec(noinline)
#else
# if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
# ifdef __GNUC__
# define STORM_FORCE_INLINE static inline __attribute__((always_inline))
# define STORM_NO_INLINE static __attribute__((noinline))
# else
# define STORM_FORCE_INLINE static inline
# define STORM_NO_INLINE static
# endif
# else
# define STORM_FORCE_INLINE static
# define STORM_NO_INLINE static
# endif /* __STDC_VERSION__ */
#endif
/****************************
* General checks
****************************/
#ifndef __has_builtin
#define STORM_HAS_BUILTIN(x) 0
#else
#define STORM_HAS_BUILTIN(x) __has_builtin(x)
#endif
#ifndef __has_attribute
#define STORM_HAS_ATTRIBUTE(x) 0
#else
#define STORM_HAS_ATTRIBUTE(x) __has_attribute(x)
#endif
// disable noise
#ifdef __GNUC__
#define STORM_WARN_UNUSED __attribute__((warn_unused_result))
#else
#define STORM_WARN_UNUSED
#endif
#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* >= C99 */
# define STORM_RESTRICT restrict
#else
/* note : it might be useful to define __restrict or STORM_RESTRICT for some C++ compilers */
# define STORM_RESTRICT /* disable */
#endif
#ifdef __GNUC__
#define GNUC_PREREQ(x, y) \
(__GNUC__ > x || (__GNUC__ == x && __GNUC_MINOR__ >= y))
#else
#define GNUC_PREREQ(x, y) 0
#endif
#ifdef __clang__
#define CLANG_PREREQ(x, y) \
(__clang_major__ > x || (__clang_major__ == x && __clang_minor__ >= y))
#else
#define CLANG_PREREQ(x, y) 0
#endif
#if (defined(__i386__) || \
defined(__x86_64__) || \
defined(_M_IX86) || \
defined(_M_X64))
#define X86_OR_X64
#endif
#if defined(X86_OR_X64) && \
(defined(__cplusplus) || \
defined(_MSC_VER) || \
(GNUC_PREREQ(4, 2) || \
STORM_HAS_BUILTIN(__sync_val_compare_and_swap)))
#define STORM_HAVE_CPUID
#endif
#if GNUC_PREREQ(4, 2) || \
STORM_HAS_BUILTIN(__builtin_popcount)
#define STORM_HAVE_BUILTIN_POPCOUNT
#endif
#if GNUC_PREREQ(4, 2) || \
CLANG_PREREQ(3, 0)
#define STORM_HAVE_ASM_POPCNT
#endif
#if defined(STORM_HAVE_CPUID) && \
(defined(STORM_HAVE_ASM_POPCNT) || \
defined(_MSC_VER))
#define STORM_HAVE_POPCNT
#endif
#if defined(STORM_HAVE_CPUID) && \
GNUC_PREREQ(4, 9)
#define STORM_HAVE_SSE42
#define STORM_HAVE_AVX2
#endif
#if defined(STORM_HAVE_CPUID) && \
GNUC_PREREQ(5, 0)
#define STORM_HAVE_AVX512
#endif
#if defined(STORM_HAVE_CPUID) && \
defined(_MSC_VER) && \
defined(__AVX2__)
#define STORM_HAVE_SSE42
#define STORM_HAVE_AVX2
#endif
#if defined(STORM_HAVE_CPUID) && \
defined(_MSC_VER) && \
defined(__AVX512__)
#define STORM_HAVE_AVX512
#endif
#if defined(STORM_HAVE_CPUID) && \
CLANG_PREREQ(3, 8) && \
STORM_HAS_ATTRIBUTE(target) && \
(!defined(_MSC_VER) || defined(__AVX2__)) && \
(!defined(__apple_build_version__) || __apple_build_version__ >= 8000000)
#define STORM_HAVE_SSE42
#define STORM_HAVE_AVX2
#define STORM_HAVE_AVX512
#endif
// Target attribute
#if !defined(_MSC_VER)
#define STORM_TARGET(x) __attribute__ ((target (x)))
#else
#define STORM_TARGET(x) 0
#endif
/****************************
* CPUID and SIMD
****************************/
#define STORM_SSE_ALIGNMENT 16
#define STORM_AVX2_ALIGNMENT 32
#define STORM_AVX512_ALIGNMENT 64
#ifdef __cplusplus
extern "C" {
#endif
#if defined(STORM_HAVE_CPUID)
#if defined(_MSC_VER)
#include <intrin.h>
#include <immintrin.h>
#endif
// CPUID flags. See https://en.wikipedia.org/wiki/CPUID for more info.
/* %ecx bit flags */
#define STORM_CPUID_runtime_bit_POPCNT (1 << 23) // POPCNT instruction
#define STORM_CPUID_runtime_bit_SSE41 (1 << 19) // CPUID.01H:ECX.SSE41[Bit 19]
#define STORM_CPUID_runtime_bit_SSE42 (1 << 20) // CPUID.01H:ECX.SSE41[Bit 20]
/* %ebx bit flags */
#define STORM_CPUID_runtime_bit_AVX2 (1 << 5) // CPUID.(EAX=07H, ECX=0H):EBX.AVX2[bit 5]
#define STORM_CPUID_runtime_bit_AVX512BW (1 << 30) // AVX-512 Byte and Word Instructions
/* xgetbv bit flags */
#define STORM_XSTATE_SSE (1 << 1)
#define STORM_XSTATE_YMM (1 << 2)
#define STORM_XSTATE_ZMM (7 << 5)
static
void STORM_run_cpuid(int eax, int ecx, int* abcd) {
#if defined(_MSC_VER)
__cpuidex(abcd, eax, ecx);
#else
int ebx = 0;
int edx = 0;
#if defined(__i386__) && \
defined(__PIC__)
/* in case of PIC under 32-bit EBX cannot be clobbered */
__asm__ ("movl %%ebx, %%edi;"
"cpuid;"
"xchgl %%ebx, %%edi;"
: "=D" (ebx),
"+a" (eax),
"+c" (ecx),
"=d" (edx));
#else
__asm__ ("cpuid;"
: "+b" (ebx),
"+a" (eax),
"+c" (ecx),
"=d" (edx));
#endif
abcd[0] = eax;
abcd[1] = ebx;
abcd[2] = ecx;
abcd[3] = edx;
#endif
}
#if defined(STORM_HAVE_AVX2) || \
defined(STORM_HAVE_AVX512)
static
int STORM_get_xcr0() {
int xcr0;
#if defined(_MSC_VER)
xcr0 = (int) _xgetbv(0);
#else
__asm__ ("xgetbv" : "=a" (xcr0) : "c" (0) : "%edx" );
#endif
return xcr0;
}
#endif
static
int STORM_get_cpuid() {
int flags = 0;
int abcd[4];
STORM_run_cpuid(1, 0, abcd);
// Check for POPCNT instruction
if ((abcd[2] & STORM_CPUID_runtime_bit_POPCNT) == STORM_CPUID_runtime_bit_POPCNT)
flags |= STORM_CPUID_runtime_bit_POPCNT;
// Check for SSE4.1 instruction set
if ((abcd[2] & STORM_CPUID_runtime_bit_SSE41) == STORM_CPUID_runtime_bit_SSE41)
flags |= STORM_CPUID_runtime_bit_SSE41;
// Check for SSE4.2 instruction set
if ((abcd[2] & STORM_CPUID_runtime_bit_SSE42) == STORM_CPUID_runtime_bit_SSE42)
flags |= STORM_CPUID_runtime_bit_SSE42;
#if defined(STORM_HAVE_AVX2) || \
defined(STORM_HAVE_AVX512)
int osxsave_mask = (1 << 27);
/* ensure OS supports extended processor state management */
if ((abcd[2] & osxsave_mask) != osxsave_mask)
return 0;
int ymm_mask = STORM_XSTATE_SSE | STORM_XSTATE_YMM;
int zmm_mask = STORM_XSTATE_SSE | STORM_XSTATE_YMM | STORM_XSTATE_ZMM;
int xcr0 = STORM_get_xcr0();
if ((xcr0 & ymm_mask) == ymm_mask) {
STORM_run_cpuid(7, 0, abcd);
if ((abcd[1] & STORM_CPUID_runtime_bit_AVX2) == STORM_CPUID_runtime_bit_AVX2)
flags |= STORM_CPUID_runtime_bit_AVX2;
if ((xcr0 & zmm_mask) == zmm_mask) {
if ((abcd[1] & STORM_CPUID_runtime_bit_AVX512BW) == STORM_CPUID_runtime_bit_AVX512BW)
flags |= STORM_CPUID_runtime_bit_AVX512BW;
}
}
#endif
return flags;
}
#endif // defined(STORM_HAVE_CPUID)
/// Taken from libpopcnt.h
#if defined(STORM_HAVE_ASM_POPCNT) && \
defined(__x86_64__)
STORM_FORCE_INLINE
uint64_t STORM_POPCOUNT(uint64_t x)
{
__asm__ ("popcnt %1, %0" : "=r" (x) : "0" (x));
return x;
}
#elif defined(STORM_HAVE_ASM_POPCNT) && \
defined(__i386__)
STORM_FORCE_INLINE
uint32_t STORM_popcnt32(uint32_t x)
{
__asm__ ("popcnt %1, %0" : "=r" (x) : "0" (x));
return x;
}
STORM_FORCE_INLINE
uint64_t STORM_POPCOUNT(uint64_t x)
{
return STORM_popcnt32((uint32_t) x) +
STORM_popcnt32((uint32_t)(x >> 32));
}
#elif defined(_MSC_VER) && \
defined(_M_X64)
#include <nmmintrin.h>
STORM_FORCE_INLINE
uint64_t STORM_POPCOUNT(uint64_t x) {
return _mm_popcnt_u64(x);
}
#elif defined(_MSC_VER) && \
defined(_M_IX86)
#include <nmmintrin.h>
STORM_FORCE_INLINE
uint64_t STORM_POPCOUNT(uint64_t x)
{
return _mm_popcnt_u32((uint32_t) x) +
_mm_popcnt_u32((uint32_t)(x >> 32));
}
/* non x86 CPUs */
#elif defined(STORM_HAVE_BUILTIN_POPCOUNT)
STORM_FORCE_INLINE
uint64_t STORM_POPCOUNT(uint64_t x) {
return __builtin_popcountll(x);
}
/* no hardware POPCNT,
* use pure integer algorithm */
#else
STORM_FORCE_INLINE
uint64_t STORM_POPCOUNT(uint64_t x) {
return STORM_popcount64(x);
}
#endif
static
uint64_t STORM_intersect_count_unrolled(const uint64_t* STORM_RESTRICT data1,
const uint64_t* STORM_RESTRICT data2,
size_t size)
{
const uint64_t limit = size - size % 4;
uint64_t cnt = 0;
uint64_t i = 0;
for (/**/; i < limit; i += 4) {
cnt += STORM_POPCOUNT(data1[i+0] & data2[i+0]);
cnt += STORM_POPCOUNT(data1[i+1] & data2[i+1]);
cnt += STORM_POPCOUNT(data1[i+2] & data2[i+2]);
cnt += STORM_POPCOUNT(data1[i+3] & data2[i+3]);
}
for (/**/; i < size; ++i)
cnt += STORM_POPCOUNT(data1[i] & data2[i]);
return cnt;
}
static
uint64_t STORM_union_count_unrolled(const uint64_t* STORM_RESTRICT data1,
const uint64_t* STORM_RESTRICT data2,
size_t size)
{
const uint64_t limit = size - size % 4;
uint64_t cnt = 0;
uint64_t i = 0;
for (/**/; i < limit; i += 4) {
cnt += STORM_POPCOUNT(data1[i+0] | data2[i+0]);
cnt += STORM_POPCOUNT(data1[i+1] | data2[i+1]);
cnt += STORM_POPCOUNT(data1[i+2] | data2[i+2]);
cnt += STORM_POPCOUNT(data1[i+3] | data2[i+3]);
}
for (/**/; i < size; ++i)
cnt += STORM_POPCOUNT(data1[i] | data2[i]);
return cnt;
}
static
uint64_t STORM_diff_count_unrolled(const uint64_t* STORM_RESTRICT data1,
const uint64_t* STORM_RESTRICT data2,
size_t size)
{
const uint64_t limit = size - size % 4;
uint64_t cnt = 0;
uint64_t i = 0;
for (/**/; i < limit; i += 4) {
cnt += STORM_POPCOUNT(data1[i+0] ^ data2[i+0]);
cnt += STORM_POPCOUNT(data1[i+1] ^ data2[i+1]);
cnt += STORM_POPCOUNT(data1[i+2] ^ data2[i+2]);
cnt += STORM_POPCOUNT(data1[i+3] ^ data2[i+3]);
}
for (/**/; i < size; ++i)
cnt += STORM_POPCOUNT(data1[i] ^ data2[i]);
return cnt;
}
static
int STORM_pospopcnt_u16_scalar_naive(const uint16_t* data, size_t len, uint32_t* out) {
for (int i = 0; i < len; ++i) {
for (int j = 0; j < 16; ++j) {
out[j] += ((data[i] & (1 << j)) >> j);
}
}
return 0;
}
#ifndef _MSC_VER
STORM_FORCE_INLINE
uint64_t STORM_pospopcnt_umul128(uint64_t a, uint64_t b, uint64_t* hi) {
unsigned __int128 x = (unsigned __int128)a * (unsigned __int128)b;
*hi = (uint64_t)(x >> 64);
return (uint64_t)x;
}
STORM_FORCE_INLINE
uint64_t STORM_pospopcnt_loadu_u64(const void* ptr) {
uint64_t data;
memcpy(&data, ptr, sizeof(data));
return data;
}
// By @aqrit (https://github.com/aqrit)
// @see: https://gist.github.com/aqrit/c729815b0165c139d0bac642ab7ee104
static
int STORM_pospopcnt_u16_scalar_umul128_unroll2(const uint16_t* in, size_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 = STORM_pospopcnt_loadu_u64(&in[0]);
uint64_t v1 = STORM_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 = STORM_pospopcnt_umul128(a, mask_0001, &hi);
a += hi; // broadcast 4-bit counts
b = STORM_pospopcnt_umul128(b, mask_0001, &hi);
b += hi;
c = STORM_pospopcnt_umul128(c, mask_0001, &hi);
c += hi;
d = STORM_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;
}
#endif
/*
* This uses fewer arithmetic operations than any other known
* implementation on machines with fast multiplication.
* It uses 12 arithmetic operations, one of which is a multiply.
* http://en.wikipedia.org/wiki/Hamming_weight#Efficient_implementation
*/
STORM_FORCE_INLINE
uint64_t STORM_popcount64(uint64_t x)
{
uint64_t m1 = UINT64_C(0x5555555555555555);
uint64_t m2 = UINT64_C(0x3333333333333333);
uint64_t m4 = UINT64_C(0x0F0F0F0F0F0F0F0F);
uint64_t h01 = UINT64_C(0x0101010101010101);
x -= (x >> 1) & m1;
x = (x & m2) + ((x >> 2) & m2);
x = (x + (x >> 4)) & m4;
return (x * h01) >> 56;
}
static
const uint8_t STORM_popcnt_lookup8bit[256] = {
/* 0 */ 0, /* 1 */ 1, /* 2 */ 1, /* 3 */ 2,
/* 4 */ 1, /* 5 */ 2, /* 6 */ 2, /* 7 */ 3,
/* 8 */ 1, /* 9 */ 2, /* a */ 2, /* b */ 3,
/* c */ 2, /* d */ 3, /* e */ 3, /* f */ 4,
/* 10 */ 1, /* 11 */ 2, /* 12 */ 2, /* 13 */ 3,
/* 14 */ 2, /* 15 */ 3, /* 16 */ 3, /* 17 */ 4,
/* 18 */ 2, /* 19 */ 3, /* 1a */ 3, /* 1b */ 4,
/* 1c */ 3, /* 1d */ 4, /* 1e */ 4, /* 1f */ 5,
/* 20 */ 1, /* 21 */ 2, /* 22 */ 2, /* 23 */ 3,
/* 24 */ 2, /* 25 */ 3, /* 26 */ 3, /* 27 */ 4,
/* 28 */ 2, /* 29 */ 3, /* 2a */ 3, /* 2b */ 4,
/* 2c */ 3, /* 2d */ 4, /* 2e */ 4, /* 2f */ 5,
/* 30 */ 2, /* 31 */ 3, /* 32 */ 3, /* 33 */ 4,
/* 34 */ 3, /* 35 */ 4, /* 36 */ 4, /* 37 */ 5,
/* 38 */ 3, /* 39 */ 4, /* 3a */ 4, /* 3b */ 5,
/* 3c */ 4, /* 3d */ 5, /* 3e */ 5, /* 3f */ 6,
/* 40 */ 1, /* 41 */ 2, /* 42 */ 2, /* 43 */ 3,
/* 44 */ 2, /* 45 */ 3, /* 46 */ 3, /* 47 */ 4,
/* 48 */ 2, /* 49 */ 3, /* 4a */ 3, /* 4b */ 4,
/* 4c */ 3, /* 4d */ 4, /* 4e */ 4, /* 4f */ 5,
/* 50 */ 2, /* 51 */ 3, /* 52 */ 3, /* 53 */ 4,
/* 54 */ 3, /* 55 */ 4, /* 56 */ 4, /* 57 */ 5,
/* 58 */ 3, /* 59 */ 4, /* 5a */ 4, /* 5b */ 5,
/* 5c */ 4, /* 5d */ 5, /* 5e */ 5, /* 5f */ 6,
/* 60 */ 2, /* 61 */ 3, /* 62 */ 3, /* 63 */ 4,
/* 64 */ 3, /* 65 */ 4, /* 66 */ 4, /* 67 */ 5,
/* 68 */ 3, /* 69 */ 4, /* 6a */ 4, /* 6b */ 5,
/* 6c */ 4, /* 6d */ 5, /* 6e */ 5, /* 6f */ 6,
/* 70 */ 3, /* 71 */ 4, /* 72 */ 4, /* 73 */ 5,
/* 74 */ 4, /* 75 */ 5, /* 76 */ 5, /* 77 */ 6,
/* 78 */ 4, /* 79 */ 5, /* 7a */ 5, /* 7b */ 6,
/* 7c */ 5, /* 7d */ 6, /* 7e */ 6, /* 7f */ 7,
/* 80 */ 1, /* 81 */ 2, /* 82 */ 2, /* 83 */ 3,
/* 84 */ 2, /* 85 */ 3, /* 86 */ 3, /* 87 */ 4,
/* 88 */ 2, /* 89 */ 3, /* 8a */ 3, /* 8b */ 4,
/* 8c */ 3, /* 8d */ 4, /* 8e */ 4, /* 8f */ 5,
/* 90 */ 2, /* 91 */ 3, /* 92 */ 3, /* 93 */ 4,
/* 94 */ 3, /* 95 */ 4, /* 96 */ 4, /* 97 */ 5,
/* 98 */ 3, /* 99 */ 4, /* 9a */ 4, /* 9b */ 5,
/* 9c */ 4, /* 9d */ 5, /* 9e */ 5, /* 9f */ 6,
/* a0 */ 2, /* a1 */ 3, /* a2 */ 3, /* a3 */ 4,
/* a4 */ 3, /* a5 */ 4, /* a6 */ 4, /* a7 */ 5,
/* a8 */ 3, /* a9 */ 4, /* aa */ 4, /* ab */ 5,
/* ac */ 4, /* ad */ 5, /* ae */ 5, /* af */ 6,
/* b0 */ 3, /* b1 */ 4, /* b2 */ 4, /* b3 */ 5,
/* b4 */ 4, /* b5 */ 5, /* b6 */ 5, /* b7 */ 6,
/* b8 */ 4, /* b9 */ 5, /* ba */ 5, /* bb */ 6,
/* bc */ 5, /* bd */ 6, /* be */ 6, /* bf */ 7,
/* c0 */ 2, /* c1 */ 3, /* c2 */ 3, /* c3 */ 4,
/* c4 */ 3, /* c5 */ 4, /* c6 */ 4, /* c7 */ 5,
/* c8 */ 3, /* c9 */ 4, /* ca */ 4, /* cb */ 5,
/* cc */ 4, /* cd */ 5, /* ce */ 5, /* cf */ 6,
/* d0 */ 3, /* d1 */ 4, /* d2 */ 4, /* d3 */ 5,
/* d4 */ 4, /* d5 */ 5, /* d6 */ 5, /* d7 */ 6,
/* d8 */ 4, /* d9 */ 5, /* da */ 5, /* db */ 6,
/* dc */ 5, /* dd */ 6, /* de */ 6, /* df */ 7,
/* e0 */ 3, /* e1 */ 4, /* e2 */ 4, /* e3 */ 5,
/* e4 */ 4, /* e5 */ 5, /* e6 */ 5, /* e7 */ 6,
/* e8 */ 4, /* e9 */ 5, /* ea */ 5, /* eb */ 6,
/* ec */ 5, /* ed */ 6, /* ee */ 6, /* ef */ 7,
/* f0 */ 4, /* f1 */ 5, /* f2 */ 5, /* f3 */ 6,
/* f4 */ 5, /* f5 */ 6, /* f6 */ 6, /* f7 */ 7,
/* f8 */ 5, /* f9 */ 6, /* fa */ 6, /* fb */ 7,
/* fc */ 6, /* fd */ 7, /* fe */ 7, /* ff */ 8
};
/****************************
* SSE4.1 functions
****************************/
#if defined(STORM_HAVE_SSE42)
#include <immintrin.h>
STORM_TARGET("sse4.2")
STORM_FORCE_INLINE
uint64_t STORM_POPCOUNT_SSE(const __m128i n) {
return(STORM_POPCOUNT(_mm_cvtsi128_si64(n)) +
STORM_POPCOUNT(_mm_cvtsi128_si64(_mm_unpackhi_epi64(n, n))));
}
STORM_TARGET("sse4.2")
STORM_FORCE_INLINE
void STORM_CSA128(__m128i* h, __m128i* l, __m128i a, __m128i b, __m128i c) {
__m128i u = _mm_xor_si128(a, b);
*h = _mm_or_si128(_mm_and_si128(a, b), _mm_and_si128(u, c));
*l = _mm_xor_si128(u, c);
}
/**
* Carry-save adder update step.
* @see https://en.wikipedia.org/wiki/Carry-save_adder#Technical_details
*
* Steps:
* 1) U = *L ⊕ B
* 2) *H = (*L ^ B) | (U ^ C)
* 3) *L = *L ⊕ B ⊕ C = U ⊕ C
*
* B and C are 16-bit staggered registers such that &C - &B = 1.
*
* Example usage:
* pospopcnt_csa_sse(&twosA, &v1, _mm_loadu_si128(data + i + 0), _mm_loadu_si128(data + i + 1));
*
* @param h
* @param l
* @param b
* @param c
*/
STORM_TARGET("sse4.2")
STORM_FORCE_INLINE
void STORM_pospopcnt_csa_sse(__m128i* STORM_RESTRICT h,
__m128i* STORM_RESTRICT l,
const __m128i b,
const __m128i c)
{
const __m128i u = _mm_xor_si128(*l, b);
*h = _mm_or_si128(*l & b, u & c); // shift carry (sc_i).
*l = _mm_xor_si128(u, c); // partial sum (ps).
}
// By @aqrit (https://github.com/aqrit)
// @see: https://gist.github.com/aqrit/cb52b2ac5b7d0dfe9319c09d27237bf3
STORM_TARGET("sse4.2")
static
int STORM_pospopcnt_u16_sse_sad(const uint16_t* data, size_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)