/src/croaring/include/roaring/array_util.h
Line | Count | Source |
1 | | /* |
2 | | * array_util.h |
3 | | * |
4 | | * This header provides low-level utility routines for sorted arrays of |
5 | | * 16-bit integers, which are used heavily by CRoaring's array-based |
6 | | * containers and set-operation kernels. It includes search helpers, counting |
7 | | * helpers, and array intersection/difference primitives. |
8 | | * |
9 | | * Some of the routines also have SIMD-accelerated implementations on supported |
10 | | * platforms, allowing efficient operations on sorted integer arrays that form |
11 | | * the basis of sparse container processing. |
12 | | */ |
13 | | #ifndef CROARING_ARRAY_UTIL_H |
14 | | #define CROARING_ARRAY_UTIL_H |
15 | | |
16 | | #include <stddef.h> // for size_t |
17 | | #include <stdint.h> |
18 | | |
19 | | #include <roaring/portability.h> |
20 | | |
21 | | #if CROARING_IS_X64 |
22 | | #ifndef CROARING_COMPILER_SUPPORTS_AVX512 |
23 | | #error "CROARING_COMPILER_SUPPORTS_AVX512 needs to be defined." |
24 | | #endif // CROARING_COMPILER_SUPPORTS_AVX512 |
25 | | #endif |
26 | | #if defined(__GNUC__) && !defined(__clang__) |
27 | | #pragma GCC diagnostic push |
28 | | #pragma GCC diagnostic ignored "-Wuninitialized" |
29 | | #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" |
30 | | #endif |
31 | | #ifdef __cplusplus |
32 | | extern "C" { |
33 | | namespace roaring { |
34 | | namespace internal { |
35 | | #endif |
36 | | |
37 | | /* |
38 | | * Sorted-array search. |
39 | | * Assumes that array is sorted, has logarithmic complexity. |
40 | | * if the result is x, then: |
41 | | * if ( x>0 ) you have array[x] = ikey |
42 | | * if ( x<0 ) then inserting ikey at position -x-1 in array (insuring that |
43 | | * array[-x-1]=ikey) keys the array sorted. |
44 | | * |
45 | | * Adapted from array_container_contains: a SIMD-quad block-narrowing |
46 | | * search at gap=16 (Daniel Lemire, |
47 | | * https://lemire.me/blog/2026/04/27/you-can-beat-the-binary-search/) |
48 | | * followed by a scalar in-block scan that recovers the exact insertion |
49 | | * point required by the binarySearch contract. |
50 | | */ |
51 | | CROARING_ALLOW_UNALIGNED |
52 | | inline int32_t binarySearch(const uint16_t *array, int32_t lenarray, |
53 | 393k | uint16_t ikey) { |
54 | 393k | const int32_t gap = 16; |
55 | 393k | if (lenarray < gap) { |
56 | 185k | for (int32_t j = 0; j < lenarray; j++) { |
57 | 185k | if (array[j] >= ikey) { |
58 | 170k | return (array[j] == ikey) ? j : -(j + 1); |
59 | 170k | } |
60 | 185k | } |
61 | 0 | return -(lenarray + 1); |
62 | 170k | } |
63 | 223k | const int32_t num_blocks = lenarray / gap; |
64 | 223k | int32_t base = 0; |
65 | 223k | int32_t n = num_blocks; |
66 | 642k | while (n > 3) { |
67 | 419k | int32_t quarter = n >> 2; |
68 | | |
69 | 419k | int32_t k1 = array[(base + quarter + 1) * gap - 1]; |
70 | 419k | int32_t k2 = array[(base + 2 * quarter + 1) * gap - 1]; |
71 | 419k | int32_t k3 = array[(base + 3 * quarter + 1) * gap - 1]; |
72 | | |
73 | 419k | int32_t c1 = (k1 < ikey); |
74 | 419k | int32_t c2 = (k2 < ikey); |
75 | 419k | int32_t c3 = (k3 < ikey); |
76 | | |
77 | 419k | base += (c1 + c2 + c3) * quarter; |
78 | 419k | n -= 3 * quarter; |
79 | 419k | } |
80 | 419k | while (n > 1) { |
81 | 196k | int32_t half = n >> 1; |
82 | 196k | base = (array[(base + half + 1) * gap - 1] < ikey) ? base + half : base; |
83 | 196k | n -= half; |
84 | 196k | } |
85 | 223k | int32_t lo = (array[(base + 1) * gap - 1] < ikey) ? base + 1 : base; |
86 | | |
87 | 223k | if (lo < num_blocks) { |
88 | 144k | const int32_t start = lo * gap; |
89 | 144k | #if defined(CROARING_IS_X64) |
90 | | // SSE2: subs_epu16 yields zero where lane >= ikey. movemask of an |
91 | | // epi16 compare gives 2 bits per lane; ctz>>1 = lane index. Scan |
92 | | // the first 8 lanes first and exit early when they contain the |
93 | | // answer; otherwise the block-narrowing invariant guarantees the |
94 | | // second-half mask is non-zero. |
95 | 144k | __m128i needle = _mm_set1_epi16((short)ikey); |
96 | 144k | __m128i zero = _mm_setzero_si128(); |
97 | 144k | __m128i v0 = _mm_loadu_si128((const __m128i *)(array + start)); |
98 | 144k | __m128i ge0 = _mm_cmpeq_epi16(_mm_subs_epu16(needle, v0), zero); |
99 | 144k | unsigned m0 = (unsigned)_mm_movemask_epi8(ge0); |
100 | 144k | if (m0 != 0) { |
101 | 84.7k | int32_t j = start + (int32_t)(roaring_trailing_zeroes(m0) >> 1); |
102 | 84.7k | return (array[j] == ikey) ? j : -(j + 1); |
103 | 84.7k | } |
104 | 60.2k | __m128i v1 = _mm_loadu_si128((const __m128i *)(array + start + 8)); |
105 | 60.2k | __m128i ge1 = _mm_cmpeq_epi16(_mm_subs_epu16(needle, v1), zero); |
106 | 60.2k | unsigned m1 = (unsigned)_mm_movemask_epi8(ge1); |
107 | 60.2k | int32_t j = start + 8 + (int32_t)(roaring_trailing_zeroes(m1) >> 1); |
108 | 60.2k | return (array[j] == ikey) ? j : -(j + 1); |
109 | | #else |
110 | | const int32_t end = start + gap; |
111 | | for (int32_t j = start; j < end; j++) { |
112 | | if (array[j] >= ikey) { |
113 | | return (array[j] == ikey) ? j : -(j + 1); |
114 | | } |
115 | | } |
116 | | // Unreachable: the narrowing guarantees the last element of the |
117 | | // selected block is >= ikey. |
118 | | return -(end + 1); |
119 | | #endif |
120 | 144k | } |
121 | | |
122 | 573k | for (int32_t j = num_blocks * gap; j < lenarray; j++) { |
123 | 573k | if (array[j] >= ikey) { |
124 | 78.1k | return (array[j] == ikey) ? j : -(j + 1); |
125 | 78.1k | } |
126 | 573k | } |
127 | 0 | return -(lenarray + 1); |
128 | 78.1k | } |
129 | | |
130 | | /** |
131 | | * Galloping search |
132 | | * Assumes that array is sorted, has logarithmic complexity. |
133 | | * if the result is x, then if x = length, you have that all values in array |
134 | | * between pos and length are smaller than min. otherwise returns the first |
135 | | * index x such that array[x] >= min. |
136 | | */ |
137 | | static inline int32_t advanceUntil(const uint16_t *array, int32_t pos, |
138 | 0 | int32_t length, uint16_t min) { |
139 | 0 | int32_t lower = pos + 1; |
140 | |
|
141 | 0 | if ((lower >= length) || (array[lower] >= min)) { |
142 | 0 | return lower; |
143 | 0 | } |
144 | | |
145 | 0 | int32_t spansize = 1; |
146 | |
|
147 | 0 | while ((lower + spansize < length) && (array[lower + spansize] < min)) { |
148 | 0 | spansize <<= 1; |
149 | 0 | } |
150 | 0 | int32_t upper = (lower + spansize < length) ? lower + spansize : length - 1; |
151 | |
|
152 | 0 | if (array[upper] == min) { |
153 | 0 | return upper; |
154 | 0 | } |
155 | 0 | if (array[upper] < min) { |
156 | | // means |
157 | | // array |
158 | | // has no |
159 | | // item |
160 | | // >= min |
161 | | // pos = array.length; |
162 | 0 | return length; |
163 | 0 | } |
164 | | |
165 | | // we know that the next-smallest span was too small |
166 | 0 | lower += (spansize >> 1); |
167 | |
|
168 | 0 | int32_t mid = 0; |
169 | 0 | while (lower + 1 != upper) { |
170 | 0 | mid = (lower + upper) >> 1; |
171 | 0 | if (array[mid] == min) { |
172 | 0 | return mid; |
173 | 0 | } else if (array[mid] < min) { |
174 | 0 | lower = mid; |
175 | 0 | } else { |
176 | 0 | upper = mid; |
177 | 0 | } |
178 | 0 | } |
179 | 0 | return upper; |
180 | 0 | } Unexecuted instantiation: croaring_fuzzer.c:advanceUntil Unexecuted instantiation: array_util.c:advanceUntil Unexecuted instantiation: containers.c:advanceUntil Unexecuted instantiation: run.c:advanceUntil Unexecuted instantiation: roaring.c:advanceUntil Unexecuted instantiation: roaring64.c:advanceUntil Unexecuted instantiation: roaring_array.c:advanceUntil Unexecuted instantiation: array.c:advanceUntil Unexecuted instantiation: bitset.c:advanceUntil Unexecuted instantiation: convert.c:advanceUntil Unexecuted instantiation: mixed_intersection.c:advanceUntil Unexecuted instantiation: mixed_union.c:advanceUntil Unexecuted instantiation: mixed_equal.c:advanceUntil Unexecuted instantiation: mixed_subset.c:advanceUntil Unexecuted instantiation: mixed_negation.c:advanceUntil Unexecuted instantiation: mixed_xor.c:advanceUntil Unexecuted instantiation: mixed_andnot.c:advanceUntil |
181 | | |
182 | | /** |
183 | | * Returns number of elements which are less than ikey. |
184 | | * Array elements must be unique and sorted. |
185 | | */ |
186 | | static inline int32_t count_less(const uint16_t *array, int32_t lenarray, |
187 | 0 | uint16_t ikey) { |
188 | 0 | if (lenarray == 0) return 0; |
189 | 0 | int32_t pos = binarySearch(array, lenarray, ikey); |
190 | 0 | return pos >= 0 ? pos : -(pos + 1); |
191 | 0 | } Unexecuted instantiation: croaring_fuzzer.c:count_less Unexecuted instantiation: array_util.c:count_less Unexecuted instantiation: containers.c:count_less Unexecuted instantiation: run.c:count_less Unexecuted instantiation: roaring.c:count_less Unexecuted instantiation: roaring64.c:count_less Unexecuted instantiation: roaring_array.c:count_less Unexecuted instantiation: array.c:count_less Unexecuted instantiation: bitset.c:count_less Unexecuted instantiation: convert.c:count_less Unexecuted instantiation: mixed_intersection.c:count_less Unexecuted instantiation: mixed_union.c:count_less Unexecuted instantiation: mixed_equal.c:count_less Unexecuted instantiation: mixed_subset.c:count_less Unexecuted instantiation: mixed_negation.c:count_less Unexecuted instantiation: mixed_xor.c:count_less Unexecuted instantiation: mixed_andnot.c:count_less |
192 | | |
193 | | /** |
194 | | * Returns number of elements which are greater than ikey. |
195 | | * Array elements must be unique and sorted. |
196 | | */ |
197 | | static inline int32_t count_greater(const uint16_t *array, int32_t lenarray, |
198 | 0 | uint16_t ikey) { |
199 | 0 | if (lenarray == 0) return 0; |
200 | 0 | int32_t pos = binarySearch(array, lenarray, ikey); |
201 | 0 | if (pos >= 0) { |
202 | 0 | return lenarray - (pos + 1); |
203 | 0 | } else { |
204 | 0 | return lenarray - (-pos - 1); |
205 | 0 | } |
206 | 0 | } Unexecuted instantiation: croaring_fuzzer.c:count_greater Unexecuted instantiation: array_util.c:count_greater Unexecuted instantiation: containers.c:count_greater Unexecuted instantiation: run.c:count_greater Unexecuted instantiation: roaring.c:count_greater Unexecuted instantiation: roaring64.c:count_greater Unexecuted instantiation: roaring_array.c:count_greater Unexecuted instantiation: array.c:count_greater Unexecuted instantiation: bitset.c:count_greater Unexecuted instantiation: convert.c:count_greater Unexecuted instantiation: mixed_intersection.c:count_greater Unexecuted instantiation: mixed_union.c:count_greater Unexecuted instantiation: mixed_equal.c:count_greater Unexecuted instantiation: mixed_subset.c:count_greater Unexecuted instantiation: mixed_negation.c:count_greater Unexecuted instantiation: mixed_xor.c:count_greater Unexecuted instantiation: mixed_andnot.c:count_greater |
207 | | |
208 | | /** |
209 | | * From Schlegel et al., Fast Sorted-Set Intersection using SIMD Instructions |
210 | | * Optimized by D. Lemire on May 3rd 2013 |
211 | | * |
212 | | * C should have capacity greater than the minimum of s_1 and s_b + 8 |
213 | | * where 8 is sizeof(__m128i)/sizeof(uint16_t). |
214 | | */ |
215 | | int32_t intersect_vector16(const uint16_t *A, size_t s_a, const uint16_t *B, |
216 | | size_t s_b, uint16_t *C); |
217 | | |
218 | | int32_t intersect_vector16_inplace(uint16_t *A, size_t s_a, const uint16_t *B, |
219 | | size_t s_b); |
220 | | |
221 | | /** |
222 | | * Take an array container and write it out to a 32-bit array, using base |
223 | | * as the offset. |
224 | | */ |
225 | | int array_container_to_uint32_array_vector16(void *vout, const uint16_t *array, |
226 | | size_t cardinality, uint32_t base); |
227 | | #if CROARING_COMPILER_SUPPORTS_AVX512 |
228 | | int avx512_array_container_to_uint32_array(void *vout, const uint16_t *array, |
229 | | size_t cardinality, uint32_t base); |
230 | | #endif |
231 | | /** |
232 | | * Compute the cardinality of the intersection using SSE4 instructions |
233 | | */ |
234 | | int32_t intersect_vector16_cardinality(const uint16_t *A, size_t s_a, |
235 | | const uint16_t *B, size_t s_b); |
236 | | |
237 | | /* Computes the intersection between one small and one large set of uint16_t. |
238 | | * Stores the result into buffer and return the number of elements. */ |
239 | | int32_t intersect_skewed_uint16(const uint16_t *smallarray, size_t size_s, |
240 | | const uint16_t *largearray, size_t size_l, |
241 | | uint16_t *buffer); |
242 | | |
243 | | /* Computes the size of the intersection between one small and one large set of |
244 | | * uint16_t. */ |
245 | | int32_t intersect_skewed_uint16_cardinality(const uint16_t *smallarray, |
246 | | size_t size_s, |
247 | | const uint16_t *largearray, |
248 | | size_t size_l); |
249 | | |
250 | | /* Check whether the size of the intersection between one small and one large |
251 | | * set of uint16_t is non-zero. */ |
252 | | bool intersect_skewed_uint16_nonempty(const uint16_t *smallarray, size_t size_s, |
253 | | const uint16_t *largearray, |
254 | | size_t size_l); |
255 | | /** |
256 | | * Generic intersection function. |
257 | | */ |
258 | | int32_t intersect_uint16(const uint16_t *A, const size_t lenA, |
259 | | const uint16_t *B, const size_t lenB, uint16_t *out); |
260 | | /** |
261 | | * Compute the size of the intersection (generic). |
262 | | */ |
263 | | int32_t intersect_uint16_cardinality(const uint16_t *A, const size_t lenA, |
264 | | const uint16_t *B, const size_t lenB); |
265 | | |
266 | | /** |
267 | | * Checking whether the size of the intersection is non-zero. |
268 | | */ |
269 | | bool intersect_uint16_nonempty(const uint16_t *A, const size_t lenA, |
270 | | const uint16_t *B, const size_t lenB); |
271 | | |
272 | | #ifndef CROARING_SKEWED_MERGE_RATIO |
273 | 0 | #define CROARING_SKEWED_MERGE_RATIO 8 |
274 | | #endif |
275 | | |
276 | | /** |
277 | | * True when an array of size_small values is short enough, next to one of |
278 | | * size_large values, that the scalar merges (union_uint16, xor_uint16, |
279 | | * difference_uint16 and the end of the intersections) gallop through the long |
280 | | * array instead of merging. |
281 | | */ |
282 | 0 | static inline bool croaring_skewed_sizes(size_t size_small, size_t size_large) { |
283 | 0 | return size_small * CROARING_SKEWED_MERGE_RATIO < size_large; |
284 | 0 | } Unexecuted instantiation: croaring_fuzzer.c:croaring_skewed_sizes Unexecuted instantiation: array_util.c:croaring_skewed_sizes Unexecuted instantiation: containers.c:croaring_skewed_sizes Unexecuted instantiation: run.c:croaring_skewed_sizes Unexecuted instantiation: roaring.c:croaring_skewed_sizes Unexecuted instantiation: roaring64.c:croaring_skewed_sizes Unexecuted instantiation: roaring_array.c:croaring_skewed_sizes Unexecuted instantiation: array.c:croaring_skewed_sizes Unexecuted instantiation: bitset.c:croaring_skewed_sizes Unexecuted instantiation: convert.c:croaring_skewed_sizes Unexecuted instantiation: mixed_intersection.c:croaring_skewed_sizes Unexecuted instantiation: mixed_union.c:croaring_skewed_sizes Unexecuted instantiation: mixed_equal.c:croaring_skewed_sizes Unexecuted instantiation: mixed_subset.c:croaring_skewed_sizes Unexecuted instantiation: mixed_negation.c:croaring_skewed_sizes Unexecuted instantiation: mixed_xor.c:croaring_skewed_sizes Unexecuted instantiation: mixed_andnot.c:croaring_skewed_sizes |
285 | | |
286 | | /** |
287 | | * The vectorized union and xor run at several values per cycle, so they only |
288 | | * lose to the galloping merges when the sizes differ by more. |
289 | | */ |
290 | | #ifndef CROARING_SKEWED_VECTOR_MERGE_RATIO |
291 | 0 | #define CROARING_SKEWED_VECTOR_MERGE_RATIO 32 |
292 | | #endif |
293 | | |
294 | | /** |
295 | | * Generic union function. |
296 | | */ |
297 | | size_t union_uint16(const uint16_t *set_1, size_t size_1, const uint16_t *set_2, |
298 | | size_t size_2, uint16_t *buffer); |
299 | | |
300 | | /** |
301 | | * Generic XOR function. |
302 | | */ |
303 | | int32_t xor_uint16(const uint16_t *array_1, int32_t card_1, |
304 | | const uint16_t *array_2, int32_t card_2, uint16_t *out); |
305 | | |
306 | | /** |
307 | | * Generic difference function (ANDNOT). |
308 | | */ |
309 | | int difference_uint16(const uint16_t *a1, int length1, const uint16_t *a2, |
310 | | int length2, uint16_t *a_out); |
311 | | |
312 | | /** |
313 | | * Generic intersection function. |
314 | | */ |
315 | | size_t intersection_uint32(const uint32_t *A, const size_t lenA, |
316 | | const uint32_t *B, const size_t lenB, uint32_t *out); |
317 | | |
318 | | /** |
319 | | * Generic intersection function, returns just the cardinality. |
320 | | */ |
321 | | size_t intersection_uint32_card(const uint32_t *A, const size_t lenA, |
322 | | const uint32_t *B, const size_t lenB); |
323 | | |
324 | | /** |
325 | | * Generic union function. |
326 | | */ |
327 | | size_t union_uint32(const uint32_t *set_1, size_t size_1, const uint32_t *set_2, |
328 | | size_t size_2, uint32_t *buffer); |
329 | | |
330 | | /** |
331 | | * A fast SSE-based union function. |
332 | | */ |
333 | | uint32_t union_vector16(const uint16_t *set_1, uint32_t size_1, |
334 | | const uint16_t *set_2, uint32_t size_2, |
335 | | uint16_t *buffer); |
336 | | |
337 | | #if CROARING_COMPILER_SUPPORTS_AVX512 |
338 | | /** |
339 | | * AVX-512 union of two sorted uint16 arrays, using a Batcher bitonic merge |
340 | | * network over 32 lanes. Same contract as union_vector16: `buffer` must have |
341 | | * room for size_1 + size_2 values. |
342 | | */ |
343 | | uint32_t avx512_union_uint16(const uint16_t *set_1, uint32_t size_1, |
344 | | const uint16_t *set_2, uint32_t size_2, |
345 | | uint16_t *buffer); |
346 | | #endif // CROARING_COMPILER_SUPPORTS_AVX512 |
347 | | |
348 | | /** |
349 | | * A fast SSE-based XOR function. |
350 | | */ |
351 | | uint32_t xor_vector16(const uint16_t *array1, uint32_t length1, |
352 | | const uint16_t *array2, uint32_t length2, |
353 | | uint16_t *output); |
354 | | |
355 | | /** |
356 | | * A fast SSE-based difference function. |
357 | | */ |
358 | | int32_t difference_vector16(const uint16_t *A, size_t s_a, const uint16_t *B, |
359 | | size_t s_b, uint16_t *C); |
360 | | |
361 | | /** |
362 | | * Generic union function, returns just the cardinality. |
363 | | */ |
364 | | size_t union_uint32_card(const uint32_t *set_1, size_t size_1, |
365 | | const uint32_t *set_2, size_t size_2); |
366 | | |
367 | | /** |
368 | | * combines union_uint16 and union_vector16 optimally |
369 | | */ |
370 | | size_t fast_union_uint16(const uint16_t *set_1, size_t size_1, |
371 | | const uint16_t *set_2, size_t size_2, |
372 | | uint16_t *buffer); |
373 | | |
374 | | bool memequals(const void *s1, const void *s2, size_t n); |
375 | | |
376 | | #ifdef __cplusplus |
377 | | } |
378 | | } |
379 | | } // extern "C" { namespace roaring { namespace internal { |
380 | | #endif |
381 | | #if defined(__GNUC__) && !defined(__clang__) |
382 | | #pragma GCC diagnostic pop |
383 | | #endif |
384 | | #endif |