forked from ChenHuajun/pg_roaringbitmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
roaring.h
7225 lines (6409 loc) · 283 KB
/
roaring.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
/* auto-generated on Sat Jun 27 12:40:38 2020. Do not edit! */
/* begin file include/roaring/roaring_version.h */
// /include/roaring/roaring_version.h automatically generated by release.py, do not change by hand
#ifndef ROARING_INCLUDE_ROARING_VERSION
#define ROARING_INCLUDE_ROARING_VERSION
#define ROARING_VERSION = 0.2.66,
enum {
ROARING_VERSION_MAJOR = 0,
ROARING_VERSION_MINOR = 2,
ROARING_VERSION_REVISION = 66
};
#endif // ROARING_INCLUDE_ROARING_VERSION
/* end file include/roaring/roaring_version.h */
/* begin file include/roaring/portability.h */
/*
* portability.h
*
*/
#ifndef INCLUDE_PORTABILITY_H_
#define INCLUDE_PORTABILITY_H_
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#ifndef __STDC_FORMAT_MACROS
#define __STDC_FORMAT_MACROS 1
#endif
#if !(defined(_POSIX_C_SOURCE)) || (_POSIX_C_SOURCE < 200809L)
#define _POSIX_C_SOURCE 200809L
#endif
#if !(defined(_XOPEN_SOURCE)) || (_XOPEN_SOURCE < 700)
#define _XOPEN_SOURCE 700
#endif
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h> // will provide posix_memalign with _POSIX_C_SOURCE as defined above
#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(_MSC_VER) && !defined(__clang__) && !defined(_WIN64) && !defined(ROARING_ACK_32BIT)
#pragma message( \
"You appear to be attempting a 32-bit build under Visual Studio. We recommend a 64-bit build instead.")
#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
#if defined(_MSC_VER)
#define __restrict__ __restrict
#endif
#ifndef DISABLE_X64 // some users may want to compile as if they did not have
// an x64 processor
///////////////////////
/// We support X64 hardware in the following manner:
///
/// if IS_X64 is defined then we have at least SSE and SSE2
/// (All Intel processors sold in the recent past have at least SSE and SSE2 support,
/// going back to the Pentium 4.)
///
/// if USESSE4 is defined then we assume at least SSE4.2, SSE4.1,
/// SSSE3, SSE3... + IS_X64
/// if USEAVX is defined, then we assume AVX2, AVX + USESSE4
///
/// So if you have hardware that supports AVX but not AVX2, then "USEAVX"
/// won't be enabled.
/// If you have hardware that supports SSE4.1, but not SSE4.2, then USESSE4
/// won't be defined.
//////////////////////
// unless DISABLEAVX was defined, if we have __AVX2__, we enable AVX
#if (!defined(USEAVX)) && (!defined(DISABLEAVX)) && (defined(__AVX2__))
#define USEAVX
#endif
// if we have __SSE4_2__, we enable SSE4
#if (defined(__POPCNT__)) && (defined(__SSE4_2__))
#define USESSE4
#endif
#if defined(USEAVX) || defined(__x86_64__) || defined(_M_X64)
// we have an x64 processor
#define IS_X64
// we include the intrinsic header
#ifndef _MSC_VER
/* Non-Microsoft C/C++-compatible compiler */
#include <x86intrin.h> // on some recent GCC, this will declare posix_memalign
#endif
#endif
#if !defined(USENEON) && !defined(DISABLENEON) && defined(__ARM_NEON)
# define USENEON
#endif
#if defined(USENEON)
# include <arm_neon.h>
#endif
#ifndef _MSC_VER
/* Non-Microsoft C/C++-compatible compiler, assumes that it supports inline
* assembly */
#define ROARING_INLINE_ASM
#endif
#ifdef USEAVX
#define USESSE4 // if we have AVX, then we have SSE4
#define USE_BMI // we assume that AVX2 and BMI go hand and hand
#define USEAVX2FORDECODING // optimization
// vector operations should work on not just AVX
#define ROARING_VECTOR_OPERATIONS_ENABLED // vector unions (optimization)
#endif
#endif // DISABLE_X64
#ifdef _MSC_VER
/* Microsoft C/C++-compatible compiler */
#include <intrin.h>
#ifndef __clang__ // if one compiles with MSVC *with* clang, then these
// intrinsics are defined!!!
// sadly there is no way to check whether we are missing these intrinsics
// specifically.
/* wrappers for Visual Studio built-ins that look like gcc built-ins */
/* result might be undefined when input_num is zero */
static inline int __builtin_ctzll(unsigned long long input_num) {
unsigned long index;
#ifdef _WIN64 // highly recommended!!!
_BitScanForward64(&index, input_num);
#else // if we must support 32-bit Windows
if ((uint32_t)input_num != 0) {
_BitScanForward(&index, (uint32_t)input_num);
} else {
_BitScanForward(&index, (uint32_t)(input_num >> 32));
index += 32;
}
#endif
return index;
}
/* result might be undefined when input_num is zero */
static inline int __builtin_clzll(unsigned long long input_num) {
unsigned long index;
#ifdef _WIN64 // highly recommended!!!
_BitScanReverse64(&index, input_num);
#else // if we must support 32-bit Windows
if (input_num > 0xFFFFFFFF) {
_BitScanReverse(&index, (uint32_t)(input_num >> 32));
index += 32;
} else {
_BitScanReverse(&index, (uint32_t)(input_num));
}
#endif
return 63 - index;
}
/* result might be undefined when input_num is zero */
#ifdef USESSE4
/* POPCNT support was added to processors around the release of SSE4.2 */
/* USESSE4 flag guarantees POPCNT support */
static inline int __builtin_popcountll(unsigned long long input_num) {
#ifdef _WIN64 // highly recommended!!!
return (int)__popcnt64(input_num);
#else // if we must support 32-bit Windows
return (int)(__popcnt((uint32_t)input_num) +
__popcnt((uint32_t)(input_num >> 32)));
#endif
}
#else
/* software implementation avoids POPCNT */
static inline int __builtin_popcountll(unsigned long long input_num) {
const uint64_t m1 = 0x5555555555555555; //binary: 0101...
const uint64_t m2 = 0x3333333333333333; //binary: 00110011..
const uint64_t m4 = 0x0f0f0f0f0f0f0f0f; //binary: 4 zeros, 4 ones ...
const uint64_t h01 = 0x0101010101010101; //the sum of 256 to the power of 0,1,2,3...
input_num -= (input_num >> 1) & m1;
input_num = (input_num & m2) + ((input_num >> 2) & m2);
input_num = (input_num + (input_num >> 4)) & m4;
return (input_num * h01) >> 56;
}
#endif
/* Use #define so this is effective even under /Ob0 (no inline) */
#define __builtin_unreachable() __assume(0)
#endif
#endif
// without the following, we get lots of warnings about posix_memalign
#ifndef __cplusplus
extern int posix_memalign(void **__memptr, size_t __alignment, size_t __size);
#endif //__cplusplus // C++ does not have a well defined signature
// portable version of posix_memalign
static inline void *roaring_bitmap_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;
}
static inline void roaring_bitmap_aligned_free(void *memblock) {
#ifdef _MSC_VER
_aligned_free(memblock);
#elif defined(__MINGW32__) || defined(__MINGW64__)
__mingw_aligned_free(memblock);
#else
free(memblock);
#endif
}
#if defined(_MSC_VER)
#define ALIGNED(x) __declspec(align(x))
#else
#if defined(__GNUC__)
#define ALIGNED(x) __attribute__((aligned(x)))
#endif
#endif
#ifdef __GNUC__
#define WARN_UNUSED __attribute__((warn_unused_result))
#else
#define WARN_UNUSED
#endif
#define IS_BIG_ENDIAN (*(uint16_t *)"\0\xff" < 0x100)
static inline int hamming(uint64_t x) {
#ifdef USESSE4
return (int) _mm_popcnt_u64(x);
#else
// won't work under visual studio, but hopeful we have _mm_popcnt_u64 in
// many cases
return __builtin_popcountll(x);
#endif
}
#ifndef UINT64_C
#define UINT64_C(c) (c##ULL)
#endif
#ifndef UINT32_C
#define UINT32_C(c) (c##UL)
#endif
#endif /* INCLUDE_PORTABILITY_H_ */
/* end file include/roaring/portability.h */
/* begin file include/roaring/containers/perfparameters.h */
#ifndef PERFPARAMETERS_H_
#define PERFPARAMETERS_H_
#include <stdbool.h>
/**
During lazy computations, we can transform array containers into bitset
containers as
long as we can expect them to have ARRAY_LAZY_LOWERBOUND values.
*/
enum { ARRAY_LAZY_LOWERBOUND = 1024 };
/* default initial size of a run container
setting it to zero delays the malloc.*/
enum { RUN_DEFAULT_INIT_SIZE = 0 };
/* default initial size of an array container
setting it to zero delays the malloc */
enum { ARRAY_DEFAULT_INIT_SIZE = 0 };
/* automatic bitset conversion during lazy or */
#ifndef LAZY_OR_BITSET_CONVERSION
#define LAZY_OR_BITSET_CONVERSION true
#endif
/* automatically attempt to convert a bitset to a full run during lazy
* evaluation */
#ifndef LAZY_OR_BITSET_CONVERSION_TO_FULL
#define LAZY_OR_BITSET_CONVERSION_TO_FULL true
#endif
/* automatically attempt to convert a bitset to a full run */
#ifndef OR_BITSET_CONVERSION_TO_FULL
#define OR_BITSET_CONVERSION_TO_FULL true
#endif
#endif
/* end file include/roaring/containers/perfparameters.h */
/* begin file include/roaring/array_util.h */
#ifndef ARRAY_UTIL_H
#define ARRAY_UTIL_H
#include <stddef.h> // for size_t
#include <stdint.h>
/*
* Good old binary search.
* Assumes that array is sorted, has logarithmic complexity.
* if the result is x, then:
* if ( x>0 ) you have array[x] = ikey
* if ( x<0 ) then inserting ikey at position -x-1 in array (insuring that array[-x-1]=ikey)
* keys the array sorted.
*/
inline int32_t binarySearch(const uint16_t *array, int32_t lenarray,
uint16_t ikey) {
int32_t low = 0;
int32_t high = lenarray - 1;
while (low <= high) {
int32_t middleIndex = (low + high) >> 1;
uint16_t middleValue = array[middleIndex];
if (middleValue < ikey) {
low = middleIndex + 1;
} else if (middleValue > ikey) {
high = middleIndex - 1;
} else {
return middleIndex;
}
}
return -(low + 1);
}
/**
* Galloping search
* Assumes that array is sorted, has logarithmic complexity.
* if the result is x, then if x = length, you have that all values in array between pos and length
* are smaller than min.
* otherwise returns the first index x such that array[x] >= min.
*/
static inline int32_t advanceUntil(const uint16_t *array, int32_t pos,
int32_t length, uint16_t min) {
int32_t lower = pos + 1;
if ((lower >= length) || (array[lower] >= min)) {
return lower;
}
int32_t spansize = 1;
while ((lower + spansize < length) && (array[lower + spansize] < min)) {
spansize <<= 1;
}
int32_t upper = (lower + spansize < length) ? lower + spansize : length - 1;
if (array[upper] == min) {
return upper;
}
if (array[upper] < min) {
// means
// array
// has no
// item
// >= min
// pos = array.length;
return length;
}
// we know that the next-smallest span was too small
lower += (spansize >> 1);
int32_t mid = 0;
while (lower + 1 != upper) {
mid = (lower + upper) >> 1;
if (array[mid] == min) {
return mid;
} else if (array[mid] < min) {
lower = mid;
} else {
upper = mid;
}
}
return upper;
}
/**
* Returns number of elements which are less then $ikey.
* Array elements must be unique and sorted.
*/
static inline int32_t count_less(const uint16_t *array, int32_t lenarray,
uint16_t ikey) {
if (lenarray == 0) return 0;
int32_t pos = binarySearch(array, lenarray, ikey);
return pos >= 0 ? pos : -(pos+1);
}
/**
* Returns number of elements which are greater then $ikey.
* Array elements must be unique and sorted.
*/
static inline int32_t count_greater(const uint16_t *array, int32_t lenarray,
uint16_t ikey) {
if (lenarray == 0) return 0;
int32_t pos = binarySearch(array, lenarray, ikey);
if (pos >= 0) {
return lenarray - (pos+1);
} else {
return lenarray - (-pos-1);
}
}
/**
* From Schlegel et al., Fast Sorted-Set Intersection using SIMD Instructions
* Optimized by D. Lemire on May 3rd 2013
*
* C should have capacity greater than the minimum of s_1 and s_b + 8
* where 8 is sizeof(__m128i)/sizeof(uint16_t).
*/
int32_t intersect_vector16(const uint16_t *__restrict__ A, size_t s_a,
const uint16_t *__restrict__ B, size_t s_b,
uint16_t *C);
/**
* Compute the cardinality of the intersection using SSE4 instructions
*/
int32_t intersect_vector16_cardinality(const uint16_t *__restrict__ A,
size_t s_a,
const uint16_t *__restrict__ B,
size_t s_b);
/* Computes the intersection between one small and one large set of uint16_t.
* Stores the result into buffer and return the number of elements. */
int32_t intersect_skewed_uint16(const uint16_t *smallarray, size_t size_s,
const uint16_t *largearray, size_t size_l,
uint16_t *buffer);
/* Computes the size of the intersection between one small and one large set of
* uint16_t. */
int32_t intersect_skewed_uint16_cardinality(const uint16_t *smallarray,
size_t size_s,
const uint16_t *largearray,
size_t size_l);
/* Check whether the size of the intersection between one small and one large set of uint16_t is non-zero. */
bool intersect_skewed_uint16_nonempty(const uint16_t *smallarray, size_t size_s,
const uint16_t *largearray, size_t size_l);
/**
* Generic intersection function.
*/
int32_t intersect_uint16(const uint16_t *A, const size_t lenA,
const uint16_t *B, const size_t lenB, uint16_t *out);
/**
* Compute the size of the intersection (generic).
*/
int32_t intersect_uint16_cardinality(const uint16_t *A, const size_t lenA,
const uint16_t *B, const size_t lenB);
/**
* Checking whether the size of the intersection is non-zero.
*/
bool intersect_uint16_nonempty(const uint16_t *A, const size_t lenA,
const uint16_t *B, const size_t lenB);
/**
* Generic union function.
*/
size_t union_uint16(const uint16_t *set_1, size_t size_1, const uint16_t *set_2,
size_t size_2, uint16_t *buffer);
/**
* Generic XOR function.
*/
int32_t xor_uint16(const uint16_t *array_1, int32_t card_1,
const uint16_t *array_2, int32_t card_2, uint16_t *out);
/**
* Generic difference function (ANDNOT).
*/
int difference_uint16(const uint16_t *a1, int length1, const uint16_t *a2,
int length2, uint16_t *a_out);
/**
* Generic intersection function.
*/
size_t intersection_uint32(const uint32_t *A, const size_t lenA,
const uint32_t *B, const size_t lenB, uint32_t *out);
/**
* Generic intersection function, returns just the cardinality.
*/
size_t intersection_uint32_card(const uint32_t *A, const size_t lenA,
const uint32_t *B, const size_t lenB);
/**
* Generic union function.
*/
size_t union_uint32(const uint32_t *set_1, size_t size_1, const uint32_t *set_2,
size_t size_2, uint32_t *buffer);
/**
* A fast SSE-based union function.
*/
uint32_t union_vector16(const uint16_t *__restrict__ set_1, uint32_t size_1,
const uint16_t *__restrict__ set_2, uint32_t size_2,
uint16_t *__restrict__ buffer);
/**
* A fast SSE-based XOR function.
*/
uint32_t xor_vector16(const uint16_t *__restrict__ array1, uint32_t length1,
const uint16_t *__restrict__ array2, uint32_t length2,
uint16_t *__restrict__ output);
/**
* A fast SSE-based difference function.
*/
int32_t difference_vector16(const uint16_t *__restrict__ A, size_t s_a,
const uint16_t *__restrict__ B, size_t s_b,
uint16_t *C);
/**
* Generic union function, returns just the cardinality.
*/
size_t union_uint32_card(const uint32_t *set_1, size_t size_1,
const uint32_t *set_2, size_t size_2);
/**
* combines union_uint16 and union_vector16 optimally
*/
size_t fast_union_uint16(const uint16_t *set_1, size_t size_1, const uint16_t *set_2,
size_t size_2, uint16_t *buffer);
bool memequals(const void *s1, const void *s2, size_t n);
#endif
/* end file include/roaring/array_util.h */
/* begin file include/roaring/roaring_types.h */
/*
Typedefs used by various components
*/
#ifndef ROARING_TYPES_H
#define ROARING_TYPES_H
typedef bool (*roaring_iterator)(uint32_t value, void *param);
typedef bool (*roaring_iterator64)(uint64_t value, void *param);
/**
* (For advanced users.)
* The roaring_statistics_t can be used to collect detailed statistics about
* the composition of a roaring bitmap.
*/
typedef struct roaring_statistics_s {
uint32_t n_containers; /* number of containers */
uint32_t n_array_containers; /* number of array containers */
uint32_t n_run_containers; /* number of run containers */
uint32_t n_bitset_containers; /* number of bitmap containers */
uint32_t
n_values_array_containers; /* number of values in array containers */
uint32_t n_values_run_containers; /* number of values in run containers */
uint32_t
n_values_bitset_containers; /* number of values in bitmap containers */
uint32_t n_bytes_array_containers; /* number of allocated bytes in array
containers */
uint32_t n_bytes_run_containers; /* number of allocated bytes in run
containers */
uint32_t n_bytes_bitset_containers; /* number of allocated bytes in bitmap
containers */
uint32_t
max_value; /* the maximal value, undefined if cardinality is zero */
uint32_t
min_value; /* the minimal value, undefined if cardinality is zero */
uint64_t sum_value; /* the sum of all values (could be used to compute
average) */
uint64_t cardinality; /* total number of values stored in the bitmap */
// and n_values_arrays, n_values_rle, n_values_bitmap
} roaring_statistics_t;
#endif /* ROARING_TYPES_H */
/* end file include/roaring/roaring_types.h */
/* begin file include/roaring/utilasm.h */
/*
* utilasm.h
*
*/
#ifndef INCLUDE_UTILASM_H_
#define INCLUDE_UTILASM_H_
#if defined(USE_BMI) & defined(ROARING_INLINE_ASM)
#define ASMBITMANIPOPTIMIZATION // optimization flag
#define ASM_SHIFT_RIGHT(srcReg, bitsReg, destReg) \
__asm volatile("shrx %1, %2, %0" \
: "=r"(destReg) \
: /* write */ \
"r"(bitsReg), /* read only */ \
"r"(srcReg) /* read only */ \
)
#define ASM_INPLACESHIFT_RIGHT(srcReg, bitsReg) \
__asm volatile("shrx %1, %0, %0" \
: "+r"(srcReg) \
: /* read/write */ \
"r"(bitsReg) /* read only */ \
)
#define ASM_SHIFT_LEFT(srcReg, bitsReg, destReg) \
__asm volatile("shlx %1, %2, %0" \
: "=r"(destReg) \
: /* write */ \
"r"(bitsReg), /* read only */ \
"r"(srcReg) /* read only */ \
)
// set bit at position testBit within testByte to 1 and
// copy cmovDst to cmovSrc if that bit was previously clear
#define ASM_SET_BIT_INC_WAS_CLEAR(testByte, testBit, count) \
__asm volatile( \
"bts %2, %0\n" \
"sbb $-1, %1\n" \
: "+r"(testByte), /* read/write */ \
"+r"(count) \
: /* read/write */ \
"r"(testBit) /* read only */ \
)
#define ASM_CLEAR_BIT_DEC_WAS_SET(testByte, testBit, count) \
__asm volatile( \
"btr %2, %0\n" \
"sbb $0, %1\n" \
: "+r"(testByte), /* read/write */ \
"+r"(count) \
: /* read/write */ \
"r"(testBit) /* read only */ \
)
#define ASM_BT64(testByte, testBit, count) \
__asm volatile( \
"bt %2,%1\n" \
"sbb %0,%0" /*could use setb */ \
: "=r"(count) \
: /* write */ \
"r"(testByte), /* read only */ \
"r"(testBit) /* read only */ \
)
#endif // USE_BMI
#endif /* INCLUDE_UTILASM_H_ */
/* end file include/roaring/utilasm.h */
/* begin file include/roaring/bitset_util.h */
#ifndef BITSET_UTIL_H
#define BITSET_UTIL_H
#include <stdint.h>
/*
* Set all bits in indexes [begin,end) to true.
*/
static inline void bitset_set_range(uint64_t *bitmap, uint32_t start,
uint32_t end) {
if (start == end) return;
uint32_t firstword = start / 64;
uint32_t endword = (end - 1) / 64;
if (firstword == endword) {
bitmap[firstword] |= ((~UINT64_C(0)) << (start % 64)) &
((~UINT64_C(0)) >> ((~end + 1) % 64));
return;
}
bitmap[firstword] |= (~UINT64_C(0)) << (start % 64);
for (uint32_t i = firstword + 1; i < endword; i++) bitmap[i] = ~UINT64_C(0);
bitmap[endword] |= (~UINT64_C(0)) >> ((~end + 1) % 64);
}
/*
* Find the cardinality of the bitset in [begin,begin+lenminusone]
*/
static inline int bitset_lenrange_cardinality(uint64_t *bitmap, uint32_t start,
uint32_t lenminusone) {
uint32_t firstword = start / 64;
uint32_t endword = (start + lenminusone) / 64;
if (firstword == endword) {
return hamming(bitmap[firstword] &
((~UINT64_C(0)) >> ((63 - lenminusone) % 64))
<< (start % 64));
}
int answer = hamming(bitmap[firstword] & ((~UINT64_C(0)) << (start % 64)));
for (uint32_t i = firstword + 1; i < endword; i++) {
answer += hamming(bitmap[i]);
}
answer +=
hamming(bitmap[endword] &
(~UINT64_C(0)) >> (((~start + 1) - lenminusone - 1) % 64));
return answer;
}
/*
* Check whether the cardinality of the bitset in [begin,begin+lenminusone] is 0
*/
static inline bool bitset_lenrange_empty(uint64_t *bitmap, uint32_t start,
uint32_t lenminusone) {
uint32_t firstword = start / 64;
uint32_t endword = (start + lenminusone) / 64;
if (firstword == endword) {
return (bitmap[firstword] & ((~UINT64_C(0)) >> ((63 - lenminusone) % 64))
<< (start % 64)) == 0;
}
if(((bitmap[firstword] & ((~UINT64_C(0)) << (start%64)))) != 0) return false;
for (uint32_t i = firstword + 1; i < endword; i++) {
if(bitmap[i] != 0) return false;
}
if((bitmap[endword] & (~UINT64_C(0)) >> (((~start + 1) - lenminusone - 1) % 64)) != 0) return false;
return true;
}
/*
* Set all bits in indexes [begin,begin+lenminusone] to true.
*/
static inline void bitset_set_lenrange(uint64_t *bitmap, uint32_t start,
uint32_t lenminusone) {
uint32_t firstword = start / 64;
uint32_t endword = (start + lenminusone) / 64;
if (firstword == endword) {
bitmap[firstword] |= ((~UINT64_C(0)) >> ((63 - lenminusone) % 64))
<< (start % 64);
return;
}
uint64_t temp = bitmap[endword];
bitmap[firstword] |= (~UINT64_C(0)) << (start % 64);
for (uint32_t i = firstword + 1; i < endword; i += 2)
bitmap[i] = bitmap[i + 1] = ~UINT64_C(0);
bitmap[endword] =
temp | (~UINT64_C(0)) >> (((~start + 1) - lenminusone - 1) % 64);
}
/*
* Flip all the bits in indexes [begin,end).
*/
static inline void bitset_flip_range(uint64_t *bitmap, uint32_t start,
uint32_t end) {
if (start == end) return;
uint32_t firstword = start / 64;
uint32_t endword = (end - 1) / 64;
bitmap[firstword] ^= ~((~UINT64_C(0)) << (start % 64));
for (uint32_t i = firstword; i < endword; i++) bitmap[i] = ~bitmap[i];
bitmap[endword] ^= ((~UINT64_C(0)) >> ((~end + 1) % 64));
}
/*
* Set all bits in indexes [begin,end) to false.
*/
static inline void bitset_reset_range(uint64_t *bitmap, uint32_t start,
uint32_t end) {
if (start == end) return;
uint32_t firstword = start / 64;
uint32_t endword = (end - 1) / 64;
if (firstword == endword) {
bitmap[firstword] &= ~(((~UINT64_C(0)) << (start % 64)) &
((~UINT64_C(0)) >> ((~end + 1) % 64)));
return;
}
bitmap[firstword] &= ~((~UINT64_C(0)) << (start % 64));
for (uint32_t i = firstword + 1; i < endword; i++) bitmap[i] = UINT64_C(0);
bitmap[endword] &= ~((~UINT64_C(0)) >> ((~end + 1) % 64));
}
/*
* Given a bitset containing "length" 64-bit words, write out the position
* of all the set bits to "out", values start at "base".
*
* The "out" pointer should be sufficient to store the actual number of bits
* set.
*
* Returns how many values were actually decoded.
*
* This function should only be expected to be faster than
* bitset_extract_setbits
* when the density of the bitset is high.
*
* This function uses AVX2 decoding.
*/
size_t bitset_extract_setbits_avx2(uint64_t *bitset, size_t length, void *vout,
size_t outcapacity, uint32_t base);
/*
* Given a bitset containing "length" 64-bit words, write out the position
* of all the set bits to "out", values start at "base".
*
* The "out" pointer should be sufficient to store the actual number of bits
*set.
*
* Returns how many values were actually decoded.
*/
size_t bitset_extract_setbits(uint64_t *bitset, size_t length, void *vout,
uint32_t base);
/*
* Given a bitset containing "length" 64-bit words, write out the position
* of all the set bits to "out" as 16-bit integers, values start at "base" (can
*be set to zero)
*
* The "out" pointer should be sufficient to store the actual number of bits
*set.
*
* Returns how many values were actually decoded.
*
* This function should only be expected to be faster than
*bitset_extract_setbits_uint16
* when the density of the bitset is high.
*
* This function uses SSE decoding.
*/
size_t bitset_extract_setbits_sse_uint16(const uint64_t *bitset, size_t length,
uint16_t *out, size_t outcapacity,
uint16_t base);
/*
* Given a bitset containing "length" 64-bit words, write out the position
* of all the set bits to "out", values start at "base"
* (can be set to zero)
*
* The "out" pointer should be sufficient to store the actual number of bits
*set.
*
* Returns how many values were actually decoded.
*/
size_t bitset_extract_setbits_uint16(const uint64_t *bitset, size_t length,
uint16_t *out, uint16_t base);
/*
* Given two bitsets containing "length" 64-bit words, write out the position
* of all the common set bits to "out", values start at "base"
* (can be set to zero)
*
* The "out" pointer should be sufficient to store the actual number of bits
* set.
*
* Returns how many values were actually decoded.
*/
size_t bitset_extract_intersection_setbits_uint16(const uint64_t * __restrict__ bitset1,
const uint64_t * __restrict__ bitset2,
size_t length, uint16_t *out,
uint16_t base);
/*
* Given a bitset having cardinality card, set all bit values in the list (there
* are length of them)
* and return the updated cardinality. This evidently assumes that the bitset
* already contained data.
*/
uint64_t bitset_set_list_withcard(void *bitset, uint64_t card,
const uint16_t *list, uint64_t length);
/*
* Given a bitset, set all bit values in the list (there
* are length of them).
*/
void bitset_set_list(void *bitset, const uint16_t *list, uint64_t length);
/*
* Given a bitset having cardinality card, unset all bit values in the list
* (there are length of them)
* and return the updated cardinality. This evidently assumes that the bitset
* already contained data.
*/
uint64_t bitset_clear_list(void *bitset, uint64_t card, const uint16_t *list,
uint64_t length);
/*
* Given a bitset having cardinality card, toggle all bit values in the list
* (there are length of them)
* and return the updated cardinality. This evidently assumes that the bitset
* already contained data.
*/
uint64_t bitset_flip_list_withcard(void *bitset, uint64_t card,
const uint16_t *list, uint64_t length);
void bitset_flip_list(void *bitset, const uint16_t *list, uint64_t length);
#ifdef USEAVX
/***
* BEGIN Harley-Seal popcount functions.
*/
/**
* Compute the population count of a 256-bit word
* This is not especially fast, but it is convenient as part of other functions.
*/
static inline __m256i popcount256(__m256i v) {
const __m256i lookuppos = _mm256_setr_epi8(
/* 0 */ 4 + 0, /* 1 */ 4 + 1, /* 2 */ 4 + 1, /* 3 */ 4 + 2,
/* 4 */ 4 + 1, /* 5 */ 4 + 2, /* 6 */ 4 + 2, /* 7 */ 4 + 3,
/* 8 */ 4 + 1, /* 9 */ 4 + 2, /* a */ 4 + 2, /* b */ 4 + 3,
/* c */ 4 + 2, /* d */ 4 + 3, /* e */ 4 + 3, /* f */ 4 + 4,
/* 0 */ 4 + 0, /* 1 */ 4 + 1, /* 2 */ 4 + 1, /* 3 */ 4 + 2,
/* 4 */ 4 + 1, /* 5 */ 4 + 2, /* 6 */ 4 + 2, /* 7 */ 4 + 3,
/* 8 */ 4 + 1, /* 9 */ 4 + 2, /* a */ 4 + 2, /* b */ 4 + 3,
/* c */ 4 + 2, /* d */ 4 + 3, /* e */ 4 + 3, /* f */ 4 + 4);
const __m256i lookupneg = _mm256_setr_epi8(
/* 0 */ 4 - 0, /* 1 */ 4 - 1, /* 2 */ 4 - 1, /* 3 */ 4 - 2,
/* 4 */ 4 - 1, /* 5 */ 4 - 2, /* 6 */ 4 - 2, /* 7 */ 4 - 3,
/* 8 */ 4 - 1, /* 9 */ 4 - 2, /* a */ 4 - 2, /* b */ 4 - 3,
/* c */ 4 - 2, /* d */ 4 - 3, /* e */ 4 - 3, /* f */ 4 - 4,
/* 0 */ 4 - 0, /* 1 */ 4 - 1, /* 2 */ 4 - 1, /* 3 */ 4 - 2,
/* 4 */ 4 - 1, /* 5 */ 4 - 2, /* 6 */ 4 - 2, /* 7 */ 4 - 3,
/* 8 */ 4 - 1, /* 9 */ 4 - 2, /* a */ 4 - 2, /* b */ 4 - 3,
/* c */ 4 - 2, /* d */ 4 - 3, /* e */ 4 - 3, /* f */ 4 - 4);
const __m256i low_mask = _mm256_set1_epi8(0x0f);
const __m256i lo = _mm256_and_si256(v, low_mask);
const __m256i hi = _mm256_and_si256(_mm256_srli_epi16(v, 4), low_mask);
const __m256i popcnt1 = _mm256_shuffle_epi8(lookuppos, lo);
const __m256i popcnt2 = _mm256_shuffle_epi8(lookupneg, hi);
return _mm256_sad_epu8(popcnt1, popcnt2);
}
/**
* Simple CSA over 256 bits
*/
static inline void CSA(__m256i *h, __m256i *l, __m256i a, __m256i b,
__m256i c) {
const __m256i u = _mm256_xor_si256(a, b);
*h = _mm256_or_si256(_mm256_and_si256(a, b), _mm256_and_si256(u, c));
*l = _mm256_xor_si256(u, c);
}
/**
* Fast Harley-Seal AVX population count function
*/
inline static uint64_t avx2_harley_seal_popcount256(const __m256i *data,
const uint64_t size) {
__m256i total = _mm256_setzero_si256();
__m256i ones = _mm256_setzero_si256();
__m256i twos = _mm256_setzero_si256();
__m256i fours = _mm256_setzero_si256();
__m256i eights = _mm256_setzero_si256();
__m256i sixteens = _mm256_setzero_si256();
__m256i twosA, twosB, foursA, foursB, eightsA, eightsB;
const uint64_t limit = size - size % 16;
uint64_t i = 0;
for (; i < limit; i += 16) {
CSA(&twosA, &ones, ones, _mm256_lddqu_si256(data + i),
_mm256_lddqu_si256(data + i + 1));
CSA(&twosB, &ones, ones, _mm256_lddqu_si256(data + i + 2),
_mm256_lddqu_si256(data + i + 3));
CSA(&foursA, &twos, twos, twosA, twosB);
CSA(&twosA, &ones, ones, _mm256_lddqu_si256(data + i + 4),
_mm256_lddqu_si256(data + i + 5));
CSA(&twosB, &ones, ones, _mm256_lddqu_si256(data + i + 6),
_mm256_lddqu_si256(data + i + 7));
CSA(&foursB, &twos, twos, twosA, twosB);
CSA(&eightsA, &fours, fours, foursA, foursB);
CSA(&twosA, &ones, ones, _mm256_lddqu_si256(data + i + 8),
_mm256_lddqu_si256(data + i + 9));
CSA(&twosB, &ones, ones, _mm256_lddqu_si256(data + i + 10),
_mm256_lddqu_si256(data + i + 11));
CSA(&foursA, &twos, twos, twosA, twosB);
CSA(&twosA, &ones, ones, _mm256_lddqu_si256(data + i + 12),
_mm256_lddqu_si256(data + i + 13));
CSA(&twosB, &ones, ones, _mm256_lddqu_si256(data + i + 14),
_mm256_lddqu_si256(data + i + 15));
CSA(&foursB, &twos, twos, twosA, twosB);
CSA(&eightsB, &fours, fours, foursA, foursB);
CSA(&sixteens, &eights, eights, eightsA, eightsB);
total = _mm256_add_epi64(total, popcount256(sixteens));
}
total = _mm256_slli_epi64(total, 4); // * 16
total = _mm256_add_epi64(
total, _mm256_slli_epi64(popcount256(eights), 3)); // += 8 * ...
total = _mm256_add_epi64(
total, _mm256_slli_epi64(popcount256(fours), 2)); // += 4 * ...
total = _mm256_add_epi64(
total, _mm256_slli_epi64(popcount256(twos), 1)); // += 2 * ...
total = _mm256_add_epi64(total, popcount256(ones));
for (; i < size; i++)
total =
_mm256_add_epi64(total, popcount256(_mm256_lddqu_si256(data + i)));
return (uint64_t)(_mm256_extract_epi64(total, 0)) +
(uint64_t)(_mm256_extract_epi64(total, 1)) +
(uint64_t)(_mm256_extract_epi64(total, 2)) +
(uint64_t)(_mm256_extract_epi64(total, 3));
}