usearch 2.15.1 → 2.15.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,807 @@
1
+ /**
2
+ * @file sparse.h
3
+ * @brief SIMD-accelerated functions for Sparse Vectors.
4
+ * @author Ash Vardanian
5
+ * @date March 21, 2024
6
+ *
7
+ * Contains:
8
+ * - Set Intersection ~ Jaccard Distance
9
+ *
10
+ * For datatypes:
11
+ * - u16: for vocabularies under 64 thousand tokens
12
+ * - u32: for vocabularies under 4 billion tokens
13
+ *
14
+ * For hardware architectures:
15
+ * - x86 (AVX512)
16
+ * - Arm (SVE2)
17
+ *
18
+ * Interestingly, to implement sparse distances and products, the most important function
19
+ * is analogous to `std::set_intersection`, that outputs the intersection of two sorted
20
+ * sequences. The naive implementation of that function would look like:
21
+ *
22
+ * std::size_t intersection = 0;
23
+ * while (i != a_length && j != b_length) {
24
+ * scalar_t ai = a[i];
25
+ * scalar_t bj = b[j];
26
+ * intersection += ai == bj;
27
+ * i += ai < bj;
28
+ * j += ai >= bj;
29
+ * }
30
+ *
31
+ * Assuming we are dealing with sparse arrays, most of the time we are just evaluating
32
+ * branches and skipping entries. So what if we could skip multiple entries at a time
33
+ * searching for the next chunk, where an intersection is possible.
34
+ *
35
+ * x86 intrinsics: https://www.intel.com/content/www/us/en/docs/intrinsics-guide/
36
+ * Arm intrinsics: https://developer.arm.com/architectures/instruction-sets/intrinsics/
37
+ */
38
+ #ifndef SIMSIMD_SPARSE_H
39
+ #define SIMSIMD_SPARSE_H
40
+
41
+ #include "types.h"
42
+
43
+ #ifdef __cplusplus
44
+ extern "C" {
45
+ #endif
46
+
47
+ // clang-format off
48
+
49
+ /* Implements the serial set intersection algorithm, similar to `std::set_intersection in C++ STL`,
50
+ * but uses clever galloping logic, if the arrays significantly differ in size.
51
+ */
52
+ SIMSIMD_PUBLIC void simsimd_intersect_u16_serial(simsimd_u16_t const* a, simsimd_u16_t const* b, simsimd_size_t a_length, simsimd_size_t b_length, simsimd_distance_t* results);
53
+ SIMSIMD_PUBLIC void simsimd_intersect_u32_serial(simsimd_u32_t const* a, simsimd_u32_t const* b, simsimd_size_t a_length, simsimd_size_t b_length, simsimd_distance_t* results);
54
+
55
+ /* Implements the most naive set intersection algorithm, similar to `std::set_intersection in C++ STL`,
56
+ * naively enumerating the elements of two arrays.
57
+ */
58
+ SIMSIMD_PUBLIC void simsimd_intersect_u16_accurate(simsimd_u16_t const* a, simsimd_u16_t const* b, simsimd_size_t a_length, simsimd_size_t b_length, simsimd_distance_t* results);
59
+ SIMSIMD_PUBLIC void simsimd_intersect_u32_accurate(simsimd_u32_t const* a, simsimd_u32_t const* b, simsimd_size_t a_length, simsimd_size_t b_length, simsimd_distance_t* results);
60
+
61
+ /* SIMD-powered backends for Arm SVE, mostly using 32-bit arithmetic over variable-length platform-defined word sizes.
62
+ * Designed for Arm Graviton 3, Microsoft Cobalt, as well as Nvidia Grace and newer Ampere Altra CPUs.
63
+ */
64
+ SIMSIMD_PUBLIC void simsimd_intersect_u32_neon(simsimd_u32_t const* a, simsimd_u32_t const* b, simsimd_size_t a_length, simsimd_size_t b_length, simsimd_distance_t* results);
65
+ SIMSIMD_PUBLIC void simsimd_intersect_u16_neon(simsimd_u16_t const* a, simsimd_u16_t const* b, simsimd_size_t a_length, simsimd_size_t b_length, simsimd_distance_t* results);
66
+ SIMSIMD_PUBLIC void simsimd_intersect_u32_sve2(simsimd_u32_t const* a, simsimd_u32_t const* b, simsimd_size_t a_length, simsimd_size_t b_length, simsimd_distance_t* results);
67
+ SIMSIMD_PUBLIC void simsimd_intersect_u16_sve2(simsimd_u16_t const* a, simsimd_u16_t const* b, simsimd_size_t a_length, simsimd_size_t b_length, simsimd_distance_t* results);
68
+
69
+ /* SIMD-powered backends for various generations of AVX512 CPUs.
70
+ * Skylake is handy, as it supports masked loads and other operations, avoiding the need for the tail loop.
71
+ */
72
+ SIMSIMD_PUBLIC void simsimd_intersect_u32_ice(simsimd_u32_t const* a, simsimd_u32_t const* b, simsimd_size_t a_length, simsimd_size_t b_length, simsimd_distance_t* results);
73
+ SIMSIMD_PUBLIC void simsimd_intersect_u16_ice(simsimd_u16_t const* a, simsimd_u16_t const* b, simsimd_size_t a_length, simsimd_size_t b_length, simsimd_distance_t* results);
74
+ // clang-format on
75
+
76
+ #define SIMSIMD_MAKE_INTERSECT_LINEAR(name, input_type, accumulator_type) \
77
+ SIMSIMD_PUBLIC void simsimd_intersect_##input_type##_##name( \
78
+ simsimd_##input_type##_t const* a, simsimd_##input_type##_t const* b, simsimd_size_t a_length, \
79
+ simsimd_size_t b_length, simsimd_distance_t* result) { \
80
+ simsimd_##accumulator_type##_t intersection = 0; \
81
+ simsimd_size_t i = 0, j = 0; \
82
+ while (i != a_length && j != b_length) { \
83
+ simsimd_##input_type##_t ai = a[i]; \
84
+ simsimd_##input_type##_t bj = b[j]; \
85
+ intersection += ai == bj; \
86
+ i += ai < bj; \
87
+ j += ai >= bj; \
88
+ } \
89
+ *result = intersection; \
90
+ }
91
+
92
+ SIMSIMD_MAKE_INTERSECT_LINEAR(accurate, u16, size) // simsimd_intersect_u16_accurate
93
+ SIMSIMD_MAKE_INTERSECT_LINEAR(accurate, u32, size) // simsimd_intersect_u32_accurate
94
+
95
+ #define SIMSIMD_MAKE_INTERSECT_GALLOPING(name, input_type, accumulator_type) \
96
+ SIMSIMD_PUBLIC simsimd_size_t simsimd_galloping_search_##input_type(simsimd_##input_type##_t const* array, \
97
+ simsimd_size_t start, simsimd_size_t length, \
98
+ simsimd_##input_type##_t val) { \
99
+ simsimd_size_t low = start; \
100
+ simsimd_size_t high = start + 1; \
101
+ while (high < length && array[high] < val) { \
102
+ low = high; \
103
+ high = (2 * high < length) ? 2 * high : length; \
104
+ } \
105
+ while (low < high) { \
106
+ simsimd_size_t mid = low + (high - low) / 2; \
107
+ if (array[mid] < val) { \
108
+ low = mid + 1; \
109
+ } else { \
110
+ high = mid; \
111
+ } \
112
+ } \
113
+ return low; \
114
+ } \
115
+ \
116
+ SIMSIMD_PUBLIC void simsimd_intersect_##input_type##_##name( \
117
+ simsimd_##input_type##_t const* shorter, simsimd_##input_type##_t const* longer, \
118
+ simsimd_size_t shorter_length, simsimd_size_t longer_length, simsimd_distance_t* result) { \
119
+ /* Swap arrays if necessary, as we want "longer" to be larger than "shorter" */ \
120
+ if (longer_length < shorter_length) { \
121
+ simsimd_##input_type##_t const* temp = shorter; \
122
+ shorter = longer; \
123
+ longer = temp; \
124
+ simsimd_size_t temp_length = shorter_length; \
125
+ shorter_length = longer_length; \
126
+ longer_length = temp_length; \
127
+ } \
128
+ \
129
+ /* Use the accurate implementation if galloping is not beneficial */ \
130
+ if (longer_length < 64 * shorter_length) { \
131
+ simsimd_intersect_##input_type##_accurate(shorter, longer, shorter_length, longer_length, result); \
132
+ return; \
133
+ } \
134
+ \
135
+ /* Perform galloping, shrinking the target range */ \
136
+ simsimd_##accumulator_type##_t intersection = 0; \
137
+ simsimd_size_t j = 0; \
138
+ for (simsimd_size_t i = 0; i < shorter_length; ++i) { \
139
+ simsimd_##input_type##_t shorter_i = shorter[i]; \
140
+ j = simsimd_galloping_search_##input_type(longer, j, longer_length, shorter_i); \
141
+ if (j < longer_length && longer[j] == shorter_i) { \
142
+ intersection++; \
143
+ } \
144
+ } \
145
+ *result = intersection; \
146
+ }
147
+
148
+ SIMSIMD_MAKE_INTERSECT_GALLOPING(serial, u16, size) // simsimd_intersect_u16_serial
149
+ SIMSIMD_MAKE_INTERSECT_GALLOPING(serial, u32, size) // simsimd_intersect_u32_serial
150
+
151
+ #if SIMSIMD_TARGET_X86
152
+ #if SIMSIMD_TARGET_ICE
153
+ #pragma GCC push_options
154
+ #pragma GCC target("avx2", "avx512f", "avx512vl", "bmi2", "lzcnt", "popcnt", "avx512bw", "avx512vbmi2")
155
+ #pragma clang attribute push(__attribute__((target("avx2,avx512f,avx512vl,bmi2,lzcnt,popcnt,avx512bw,avx512vbmi2"))), \
156
+ apply_to = function)
157
+
158
+ /* The AVX-512 implementations are inspired by the "Faster-Than-Native Alternatives
159
+ * for x86 VP2INTERSECT Instructions" paper by Guille Diez-Canas, 2022.
160
+ *
161
+ * https://github.com/mozonaut/vp2intersect
162
+ * https://arxiv.org/pdf/2112.06342.pdf
163
+ *
164
+ * For R&D purposes, it's important to keep the following latencies in mind:
165
+ *
166
+ * - `_mm512_permutex_epi64` - needs F - 3 cycles latency
167
+ * - `_mm512_shuffle_epi8` - needs BW - 1 cycle latency
168
+ * - `_mm512_permutexvar_epi16` - needs BW - 4-6 cycles latency
169
+ * - `_mm512_permutexvar_epi8` - needs VBMI - 3 cycles latency
170
+ */
171
+
172
+ SIMSIMD_INTERNAL simsimd_u32_t _mm512_2intersect_epi16_mask(__m512i a, __m512i b) {
173
+ __m512i a1 = _mm512_alignr_epi32(a, a, 4);
174
+ __m512i a2 = _mm512_alignr_epi32(a, a, 8);
175
+ __m512i a3 = _mm512_alignr_epi32(a, a, 12);
176
+
177
+ __m512i b1 = _mm512_shuffle_epi32(b, _MM_PERM_ADCB);
178
+ __m512i b2 = _mm512_shuffle_epi32(b, _MM_PERM_BADC);
179
+ __m512i b3 = _mm512_shuffle_epi32(b, _MM_PERM_CBAD);
180
+
181
+ __m512i b01 = _mm512_shrdi_epi32(b, b, 16);
182
+ __m512i b11 = _mm512_shrdi_epi32(b1, b1, 16);
183
+ __m512i b21 = _mm512_shrdi_epi32(b2, b2, 16);
184
+ __m512i b31 = _mm512_shrdi_epi32(b3, b3, 16);
185
+
186
+ __mmask32 nm00 = _mm512_cmpneq_epi16_mask(a, b);
187
+ __mmask32 nm01 = _mm512_cmpneq_epi16_mask(a1, b);
188
+ __mmask32 nm02 = _mm512_cmpneq_epi16_mask(a2, b);
189
+ __mmask32 nm03 = _mm512_cmpneq_epi16_mask(a3, b);
190
+
191
+ __mmask32 nm10 = _mm512_mask_cmpneq_epi16_mask(nm00, a, b01);
192
+ __mmask32 nm11 = _mm512_mask_cmpneq_epi16_mask(nm01, a1, b01);
193
+ __mmask32 nm12 = _mm512_mask_cmpneq_epi16_mask(nm02, a2, b01);
194
+ __mmask32 nm13 = _mm512_mask_cmpneq_epi16_mask(nm03, a3, b01);
195
+
196
+ __mmask32 nm20 = _mm512_mask_cmpneq_epi16_mask(nm10, a, b1);
197
+ __mmask32 nm21 = _mm512_mask_cmpneq_epi16_mask(nm11, a1, b1);
198
+ __mmask32 nm22 = _mm512_mask_cmpneq_epi16_mask(nm12, a2, b1);
199
+ __mmask32 nm23 = _mm512_mask_cmpneq_epi16_mask(nm13, a3, b1);
200
+
201
+ __mmask32 nm30 = _mm512_mask_cmpneq_epi16_mask(nm20, a, b11);
202
+ __mmask32 nm31 = _mm512_mask_cmpneq_epi16_mask(nm21, a1, b11);
203
+ __mmask32 nm32 = _mm512_mask_cmpneq_epi16_mask(nm22, a2, b11);
204
+ __mmask32 nm33 = _mm512_mask_cmpneq_epi16_mask(nm23, a3, b11);
205
+
206
+ __mmask32 nm40 = _mm512_mask_cmpneq_epi16_mask(nm30, a, b2);
207
+ __mmask32 nm41 = _mm512_mask_cmpneq_epi16_mask(nm31, a1, b2);
208
+ __mmask32 nm42 = _mm512_mask_cmpneq_epi16_mask(nm32, a2, b2);
209
+ __mmask32 nm43 = _mm512_mask_cmpneq_epi16_mask(nm33, a3, b2);
210
+
211
+ __mmask32 nm50 = _mm512_mask_cmpneq_epi16_mask(nm40, a, b21);
212
+ __mmask32 nm51 = _mm512_mask_cmpneq_epi16_mask(nm41, a1, b21);
213
+ __mmask32 nm52 = _mm512_mask_cmpneq_epi16_mask(nm42, a2, b21);
214
+ __mmask32 nm53 = _mm512_mask_cmpneq_epi16_mask(nm43, a3, b21);
215
+
216
+ __mmask32 nm60 = _mm512_mask_cmpneq_epi16_mask(nm50, a, b3);
217
+ __mmask32 nm61 = _mm512_mask_cmpneq_epi16_mask(nm51, a1, b3);
218
+ __mmask32 nm62 = _mm512_mask_cmpneq_epi16_mask(nm52, a2, b3);
219
+ __mmask32 nm63 = _mm512_mask_cmpneq_epi16_mask(nm53, a3, b3);
220
+
221
+ __mmask32 nm70 = _mm512_mask_cmpneq_epi16_mask(nm60, a, b31);
222
+ __mmask32 nm71 = _mm512_mask_cmpneq_epi16_mask(nm61, a1, b31);
223
+ __mmask32 nm72 = _mm512_mask_cmpneq_epi16_mask(nm62, a2, b31);
224
+ __mmask32 nm73 = _mm512_mask_cmpneq_epi16_mask(nm63, a3, b31);
225
+
226
+ return ~(simsimd_u32_t)(nm70 & simsimd_u32_rol(nm71, 8) & simsimd_u32_rol(nm72, 16) & simsimd_u32_ror(nm73, 8));
227
+ }
228
+
229
+ SIMSIMD_INTERNAL simsimd_u16_t _mm512_2intersect_epi32_mask(__m512i a, __m512i b) {
230
+ __m512i a1 = _mm512_alignr_epi32(a, a, 4);
231
+ __m512i b1 = _mm512_shuffle_epi32(b, _MM_PERM_ADCB);
232
+ __mmask16 nm00 = _mm512_cmpneq_epi32_mask(a, b);
233
+
234
+ __m512i a2 = _mm512_alignr_epi32(a, a, 8);
235
+ __m512i a3 = _mm512_alignr_epi32(a, a, 12);
236
+ __mmask16 nm01 = _mm512_cmpneq_epi32_mask(a1, b);
237
+ __mmask16 nm02 = _mm512_cmpneq_epi32_mask(a2, b);
238
+
239
+ __mmask16 nm03 = _mm512_cmpneq_epi32_mask(a3, b);
240
+ __mmask16 nm10 = _mm512_mask_cmpneq_epi32_mask(nm00, a, b1);
241
+ __mmask16 nm11 = _mm512_mask_cmpneq_epi32_mask(nm01, a1, b1);
242
+
243
+ __m512i b2 = _mm512_shuffle_epi32(b, _MM_PERM_BADC);
244
+ __mmask16 nm12 = _mm512_mask_cmpneq_epi32_mask(nm02, a2, b1);
245
+ __mmask16 nm13 = _mm512_mask_cmpneq_epi32_mask(nm03, a3, b1);
246
+ __mmask16 nm20 = _mm512_mask_cmpneq_epi32_mask(nm10, a, b2);
247
+
248
+ __m512i b3 = _mm512_shuffle_epi32(b, _MM_PERM_CBAD);
249
+ __mmask16 nm21 = _mm512_mask_cmpneq_epi32_mask(nm11, a1, b2);
250
+ __mmask16 nm22 = _mm512_mask_cmpneq_epi32_mask(nm12, a2, b2);
251
+ __mmask16 nm23 = _mm512_mask_cmpneq_epi32_mask(nm13, a3, b2);
252
+
253
+ __mmask16 nm0 = _mm512_mask_cmpneq_epi32_mask(nm20, a, b3);
254
+ __mmask16 nm1 = _mm512_mask_cmpneq_epi32_mask(nm21, a1, b3);
255
+ __mmask16 nm2 = _mm512_mask_cmpneq_epi32_mask(nm22, a2, b3);
256
+ __mmask16 nm3 = _mm512_mask_cmpneq_epi32_mask(nm23, a3, b3);
257
+
258
+ return ~(simsimd_u16_t)(nm0 & simsimd_u16_rol(nm1, 4) & simsimd_u16_rol(nm2, 8) & simsimd_u16_ror(nm3, 4));
259
+ }
260
+
261
+ SIMSIMD_PUBLIC void simsimd_intersect_u16_ice(simsimd_u16_t const* a, simsimd_u16_t const* b, simsimd_size_t a_length,
262
+ simsimd_size_t b_length, simsimd_distance_t* results) {
263
+
264
+ // The baseline implementation for very small arrays (2 registers or less) can be quite simple:
265
+ if (a_length < 64 && b_length < 64) {
266
+ simsimd_intersect_u16_serial(a, b, a_length, b_length, results);
267
+ return;
268
+ }
269
+
270
+ simsimd_u16_t const* const a_end = a + a_length;
271
+ simsimd_u16_t const* const b_end = b + b_length;
272
+ simsimd_size_t c = 0;
273
+ union vec_t {
274
+ __m512i zmm;
275
+ simsimd_u16_t u16[32];
276
+ simsimd_u8_t u8[64];
277
+ } a_vec, b_vec;
278
+
279
+ while (a + 32 < a_end && b + 32 < b_end) {
280
+ a_vec.zmm = _mm512_loadu_si512((__m512i const*)a);
281
+ b_vec.zmm = _mm512_loadu_si512((__m512i const*)b);
282
+
283
+ // Intersecting registers with `_mm512_2intersect_epi16_mask` involves a lot of shuffling
284
+ // and comparisons, so we want to avoid it if the slices don't overlap at all..
285
+ simsimd_u16_t a_min;
286
+ simsimd_u16_t a_max = a_vec.u16[31];
287
+ simsimd_u16_t b_min = b_vec.u16[0];
288
+ simsimd_u16_t b_max = b_vec.u16[31];
289
+
290
+ // If the slices don't overlap, advance the appropriate pointer
291
+ while (a_max < b_min && a + 64 < a_end) {
292
+ a += 32;
293
+ a_vec.zmm = _mm512_loadu_si512((__m512i const*)a);
294
+ a_max = a_vec.u16[31];
295
+ }
296
+ a_min = a_vec.u16[0];
297
+ while (b_max < a_min && b + 64 < b_end) {
298
+ b += 32;
299
+ b_vec.zmm = _mm512_loadu_si512((__m512i const*)b);
300
+ b_max = b_vec.u16[31];
301
+ }
302
+ b_min = b_vec.u16[0];
303
+
304
+ // Now we are likely to have some overlap, so we can intersect the registers
305
+ __mmask32 a_matches = _mm512_2intersect_epi16_mask(a_vec.zmm, b_vec.zmm);
306
+
307
+ // The paper also contained a very nice procedure for exporting the matches,
308
+ // but we don't need it here:
309
+ // _mm512_mask_compressstoreu_epi16(c, a_matches, a_vec);
310
+ c += _mm_popcnt_u32(a_matches); // The `_popcnt32` symbol isn't recognized by MSVC
311
+
312
+ __m512i a_last_broadcasted = _mm512_set1_epi16(*(short const*)&a_max);
313
+ __m512i b_last_broadcasted = _mm512_set1_epi16(*(short const*)&b_max);
314
+ __mmask32 a_step_mask = _mm512_cmple_epu16_mask(a_vec.zmm, b_last_broadcasted);
315
+ __mmask32 b_step_mask = _mm512_cmple_epu16_mask(b_vec.zmm, a_last_broadcasted);
316
+ a += 32 - _lzcnt_u32((simsimd_u32_t)a_step_mask);
317
+ b += 32 - _lzcnt_u32((simsimd_u32_t)b_step_mask);
318
+ }
319
+
320
+ simsimd_intersect_u16_serial(a, b, a_end - a, b_end - b, results);
321
+ *results += c;
322
+ }
323
+
324
+ SIMSIMD_PUBLIC void simsimd_intersect_u32_ice(simsimd_u32_t const* a, simsimd_u32_t const* b, simsimd_size_t a_length,
325
+ simsimd_size_t b_length, simsimd_distance_t* results) {
326
+
327
+ // The baseline implementation for very small arrays (2 registers or less) can be quite simple:
328
+ if (a_length < 32 && b_length < 32) {
329
+ simsimd_intersect_u32_serial(a, b, a_length, b_length, results);
330
+ return;
331
+ }
332
+
333
+ simsimd_u32_t const* const a_end = a + a_length;
334
+ simsimd_u32_t const* const b_end = b + b_length;
335
+ simsimd_size_t c = 0;
336
+ union vec_t {
337
+ __m512i zmm;
338
+ simsimd_u32_t u32[16];
339
+ simsimd_u8_t u8[64];
340
+ } a_vec, b_vec;
341
+
342
+ while (a + 16 < a_end && b + 16 < b_end) {
343
+ a_vec.zmm = _mm512_loadu_si512((__m512i const*)a);
344
+ b_vec.zmm = _mm512_loadu_si512((__m512i const*)b);
345
+
346
+ // Intersecting registers with `_mm512_2intersect_epi32_mask` involves a lot of shuffling
347
+ // and comparisons, so we want to avoid it if the slices don't overlap at all..
348
+ simsimd_u32_t a_min;
349
+ simsimd_u32_t a_max = a_vec.u32[15];
350
+ simsimd_u32_t b_min = b_vec.u32[0];
351
+ simsimd_u32_t b_max = b_vec.u32[15];
352
+
353
+ // If the slices don't overlap, advance the appropriate pointer
354
+ while (a_max < b_min && a + 32 < a_end) {
355
+ a += 16;
356
+ a_vec.zmm = _mm512_loadu_si512((__m512i const*)a);
357
+ a_max = a_vec.u32[15];
358
+ }
359
+ a_min = a_vec.u32[0];
360
+ while (b_max < a_min && b + 32 < b_end) {
361
+ b += 16;
362
+ b_vec.zmm = _mm512_loadu_si512((__m512i const*)b);
363
+ b_max = b_vec.u32[15];
364
+ }
365
+ b_min = b_vec.u32[0];
366
+
367
+ // Now we are likely to have some overlap, so we can intersect the registers
368
+ __mmask16 a_matches = _mm512_2intersect_epi32_mask(a_vec.zmm, b_vec.zmm);
369
+
370
+ // The paper also contained a very nice procedure for exporting the matches,
371
+ // but we don't need it here:
372
+ // _mm512_mask_compressstoreu_epi32(c, a_matches, a_vec);
373
+ c += _mm_popcnt_u32(a_matches); // The `_popcnt32` symbol isn't recognized by MSVC
374
+
375
+ __m512i a_last_broadcasted = _mm512_set1_epi32(*(int const*)&a_max);
376
+ __m512i b_last_broadcasted = _mm512_set1_epi32(*(int const*)&b_max);
377
+ __mmask16 a_step_mask = _mm512_cmple_epu32_mask(a_vec.zmm, b_last_broadcasted);
378
+ __mmask16 b_step_mask = _mm512_cmple_epu32_mask(b_vec.zmm, a_last_broadcasted);
379
+ a += 32 - _lzcnt_u32((simsimd_u32_t)a_step_mask);
380
+ b += 32 - _lzcnt_u32((simsimd_u32_t)b_step_mask);
381
+ }
382
+
383
+ simsimd_intersect_u32_serial(a, b, a_end - a, b_end - b, results);
384
+ *results += c;
385
+ }
386
+
387
+ #pragma clang attribute pop
388
+ #pragma GCC pop_options
389
+ #endif // SIMSIMD_TARGET_ICE
390
+ #endif // SIMSIMD_TARGET_X86
391
+
392
+ #if SIMSIMD_TARGET_ARM
393
+ #if SIMSIMD_TARGET_NEON
394
+ #pragma GCC push_options
395
+ #pragma GCC target("arch=armv8.2-a")
396
+ #pragma clang attribute push(__attribute__((target("arch=armv8.2-a"))), apply_to = function)
397
+
398
+ /**
399
+ * @brief Uses `vshrn` to produce a bitmask, similar to `movemask` in SSE.
400
+ * https://community.arm.com/arm-community-blogs/b/infrastructure-solutions-blog/posts/porting-x86-vector-bitmask-optimizations-to-arm-neon
401
+ */
402
+ SIMSIMD_INTERNAL simsimd_u64_t _simsimd_u8_to_u4_neon(uint8x16_t vec) {
403
+ return vget_lane_u64(vreinterpret_u64_u8(vshrn_n_u16(vreinterpretq_u16_u8(vec), 4)), 0);
404
+ }
405
+
406
+ SIMSIMD_INTERNAL int _simsimd_clz_u64(simsimd_u64_t x) {
407
+ // On GCC and Clang use the builtin, otherwise use the generic implementation
408
+ #if defined(__GNUC__) || defined(__clang__)
409
+ return __builtin_clzll(x);
410
+ #else
411
+ int n = 0;
412
+ while ((x & 0x8000000000000000ull) == 0)
413
+ n++, x <<= 1;
414
+ return n;
415
+ #endif
416
+ }
417
+
418
+ SIMSIMD_INTERNAL uint32x4_t _simsimd_intersect_u32x4_neon(uint32x4_t a, uint32x4_t b) {
419
+ uint32x4_t b1 = vextq_u32(b, b, 1);
420
+ uint32x4_t b2 = vextq_u32(b, b, 2);
421
+ uint32x4_t b3 = vextq_u32(b, b, 3);
422
+ uint32x4_t nm00 = vceqq_u32(a, b);
423
+ uint32x4_t nm01 = vceqq_u32(a, b1);
424
+ uint32x4_t nm02 = vceqq_u32(a, b2);
425
+ uint32x4_t nm03 = vceqq_u32(a, b3);
426
+ uint32x4_t nm = vorrq_u32(vorrq_u32(nm00, nm01), vorrq_u32(nm02, nm03));
427
+ return nm;
428
+ }
429
+
430
+ SIMSIMD_INTERNAL uint16x8_t _simsimd_intersect_u16x8_neon(uint16x8_t a, uint16x8_t b) {
431
+ uint16x8_t b1 = vextq_u16(b, b, 1);
432
+ uint16x8_t b2 = vextq_u16(b, b, 2);
433
+ uint16x8_t b3 = vextq_u16(b, b, 3);
434
+ uint16x8_t b4 = vextq_u16(b, b, 4);
435
+ uint16x8_t b5 = vextq_u16(b, b, 5);
436
+ uint16x8_t b6 = vextq_u16(b, b, 6);
437
+ uint16x8_t b7 = vextq_u16(b, b, 7);
438
+ uint16x8_t nm00 = vceqq_u16(a, b);
439
+ uint16x8_t nm01 = vceqq_u16(a, b1);
440
+ uint16x8_t nm02 = vceqq_u16(a, b2);
441
+ uint16x8_t nm03 = vceqq_u16(a, b3);
442
+ uint16x8_t nm04 = vceqq_u16(a, b4);
443
+ uint16x8_t nm05 = vceqq_u16(a, b5);
444
+ uint16x8_t nm06 = vceqq_u16(a, b6);
445
+ uint16x8_t nm07 = vceqq_u16(a, b7);
446
+ uint16x8_t nm = vorrq_u16(vorrq_u16(vorrq_u16(nm00, nm01), vorrq_u16(nm02, nm03)),
447
+ vorrq_u16(vorrq_u16(nm04, nm05), vorrq_u16(nm06, nm07)));
448
+ return nm;
449
+ }
450
+
451
+ SIMSIMD_PUBLIC void simsimd_intersect_u16_neon(simsimd_u16_t const* a, simsimd_u16_t const* b, simsimd_size_t a_length,
452
+ simsimd_size_t b_length, simsimd_distance_t* results) {
453
+
454
+ // The baseline implementation for very small arrays (2 registers or less) can be quite simple:
455
+ if (a_length < 32 && b_length < 32) {
456
+ simsimd_intersect_u16_serial(a, b, a_length, b_length, results);
457
+ return;
458
+ }
459
+
460
+ simsimd_u16_t const* const a_end = a + a_length;
461
+ simsimd_u16_t const* const b_end = b + b_length;
462
+ union vec_t {
463
+ uint16x8_t u16x8;
464
+ simsimd_u16_t u16[8];
465
+ simsimd_u8_t u8[16];
466
+ } a_vec, b_vec, c_counts_vec;
467
+ c_counts_vec.u16x8 = vdupq_n_u16(0);
468
+
469
+ while (a + 8 < a_end && b + 8 < b_end) {
470
+ a_vec.u16x8 = vld1q_u16(a);
471
+ b_vec.u16x8 = vld1q_u16(b);
472
+
473
+ // Intersecting registers with `_simsimd_intersect_u16x8_neon` involves a lot of shuffling
474
+ // and comparisons, so we want to avoid it if the slices don't overlap at all..
475
+ simsimd_u16_t a_min;
476
+ simsimd_u16_t a_max = a_vec.u16[7];
477
+ simsimd_u16_t b_min = b_vec.u16[0];
478
+ simsimd_u16_t b_max = b_vec.u16[7];
479
+
480
+ // If the slices don't overlap, advance the appropriate pointer
481
+ while (a_max < b_min && a + 16 < a_end) {
482
+ a += 8;
483
+ a_vec.u16x8 = vld1q_u16(a);
484
+ a_max = a_vec.u16[7];
485
+ }
486
+ a_min = a_vec.u16[0];
487
+ while (b_max < a_min && b + 16 < b_end) {
488
+ b += 8;
489
+ b_vec.u16x8 = vld1q_u16(b);
490
+ b_max = b_vec.u16[7];
491
+ }
492
+ b_min = b_vec.u16[0];
493
+
494
+ // Now we are likely to have some overlap, so we can intersect the registers.
495
+ // We can do it by performing a population count at every cycle, but it's not the cheapest in terms of cycles.
496
+ //
497
+ // simsimd_u64_t a_matches = __builtin_popcountll(
498
+ // _simsimd_u8_to_u4_neon(vreinterpretq_u8_u16(
499
+ // _simsimd_intersect_u16x8_neon(a_vec.u16x8, b_vec.u16x8))));
500
+ // c += a_matches / 8;
501
+ //
502
+ // Alternatively, we can we can transform match-masks into "ones", accumulate them between the cycles,
503
+ // and merge all together in the end.
504
+ uint16x8_t a_matches = _simsimd_intersect_u16x8_neon(a_vec.u16x8, b_vec.u16x8);
505
+ c_counts_vec.u16x8 = vaddq_u16(c_counts_vec.u16x8, vandq_u16(a_matches, vdupq_n_u16(1)));
506
+
507
+ // Counting leading zeros is tricky. On Arm we can use inline Assembly to get the result,
508
+ // but MSVC doesn't support that:
509
+ //
510
+ // SIMSIMD_INTERNAL int _simsimd_clz_u64(simsimd_u64_t value) {
511
+ // simsimd_u64_t result;
512
+ // __asm__("clz %x0, %x1" : "=r"(result) : "r"(value));
513
+ // return (int)result;
514
+ // }
515
+ //
516
+ // Alternatively, we can use the `vclz_u32` NEON intrinsic.
517
+ // It will compute the leading zeros number for both `a_step` and `b_step` in parallel.
518
+ uint16x8_t a_last_broadcasted = vdupq_n_u16(a_max);
519
+ uint16x8_t b_last_broadcasted = vdupq_n_u16(b_max);
520
+ simsimd_u64_t a_step = _simsimd_clz_u64(_simsimd_u8_to_u4_neon( //
521
+ vreinterpretq_u8_u16(vcleq_u16(a_vec.u16x8, b_last_broadcasted))));
522
+ simsimd_u64_t b_step = _simsimd_clz_u64(_simsimd_u8_to_u4_neon( //
523
+ vreinterpretq_u8_u16(vcleq_u16(b_vec.u16x8, a_last_broadcasted))));
524
+ a += (64 - a_step) / 8;
525
+ b += (64 - b_step) / 8;
526
+ }
527
+
528
+ simsimd_intersect_u16_serial(a, b, a_end - a, b_end - b, results);
529
+ *results += vaddvq_u16(c_counts_vec.u16x8);
530
+ }
531
+
532
+ SIMSIMD_PUBLIC void simsimd_intersect_u32_neon(simsimd_u32_t const* a, simsimd_u32_t const* b, simsimd_size_t a_length,
533
+ simsimd_size_t b_length, simsimd_distance_t* results) {
534
+
535
+ // The baseline implementation for very small arrays (2 registers or less) can be quite simple:
536
+ if (a_length < 32 && b_length < 32) {
537
+ simsimd_intersect_u32_serial(a, b, a_length, b_length, results);
538
+ return;
539
+ }
540
+
541
+ simsimd_u32_t const* const a_end = a + a_length;
542
+ simsimd_u32_t const* const b_end = b + b_length;
543
+ union vec_t {
544
+ uint32x4_t u32x4;
545
+ simsimd_u32_t u32[4];
546
+ simsimd_u8_t u8[16];
547
+ } a_vec, b_vec, c_counts_vec;
548
+ c_counts_vec.u32x4 = vdupq_n_u32(0);
549
+
550
+ while (a + 4 < a_end && b + 4 < b_end) {
551
+ a_vec.u32x4 = vld1q_u32(a);
552
+ b_vec.u32x4 = vld1q_u32(b);
553
+
554
+ // Intersecting registers with `_simsimd_intersect_u32x4_neon` involves a lot of shuffling
555
+ // and comparisons, so we want to avoid it if the slices don't overlap at all..
556
+ simsimd_u32_t a_min;
557
+ simsimd_u32_t a_max = a_vec.u32[3];
558
+ simsimd_u32_t b_min = b_vec.u32[0];
559
+ simsimd_u32_t b_max = b_vec.u32[3];
560
+
561
+ // If the slices don't overlap, advance the appropriate pointer
562
+ while (a_max < b_min && a + 8 < a_end) {
563
+ a += 4;
564
+ a_vec.u32x4 = vld1q_u32(a);
565
+ a_max = a_vec.u32[3];
566
+ }
567
+ a_min = a_vec.u32[0];
568
+ while (b_max < a_min && b + 8 < b_end) {
569
+ b += 4;
570
+ b_vec.u32x4 = vld1q_u32(b);
571
+ b_max = b_vec.u32[3];
572
+ }
573
+ b_min = b_vec.u32[0];
574
+
575
+ // Now we are likely to have some overlap, so we can intersect the registers
576
+ // We can do it by performing a population count at every cycle, but it's not the cheapest in terms of cycles.
577
+ //
578
+ // simsimd_u64_t a_matches = __builtin_popcountll(
579
+ // _simsimd_u8_to_u4_neon(vreinterpretq_u8_u32(
580
+ // _simsimd_intersect_u32x4_neon(a_vec.u32x4, b_vec.u32x4))));
581
+ // c += a_matches / 16;
582
+ //
583
+ // Alternatively, we can we can transform match-masks into "ones", accumulate them between the cycles,
584
+ // and merge all together in the end.
585
+ uint32x4_t a_matches = _simsimd_intersect_u32x4_neon(a_vec.u32x4, b_vec.u32x4);
586
+ c_counts_vec.u32x4 = vaddq_u32(c_counts_vec.u32x4, vandq_u32(a_matches, vdupq_n_u32(1)));
587
+
588
+ uint32x4_t a_last_broadcasted = vdupq_n_u32(a_max);
589
+ uint32x4_t b_last_broadcasted = vdupq_n_u32(b_max);
590
+ simsimd_u64_t a_step = _simsimd_clz_u64(_simsimd_u8_to_u4_neon( //
591
+ vreinterpretq_u8_u32(vcleq_u32(a_vec.u32x4, b_last_broadcasted))));
592
+ simsimd_u64_t b_step = _simsimd_clz_u64(_simsimd_u8_to_u4_neon( //
593
+ vreinterpretq_u8_u32(vcleq_u32(b_vec.u32x4, a_last_broadcasted))));
594
+ a += (64 - a_step) / 16;
595
+ b += (64 - b_step) / 16;
596
+ }
597
+
598
+ simsimd_intersect_u32_serial(a, b, a_end - a, b_end - b, results);
599
+ *results += vaddvq_u32(c_counts_vec.u32x4);
600
+ }
601
+
602
+ #pragma clang attribute pop
603
+ #pragma GCC pop_options
604
+ #endif // SIMSIMD_TARGET_NEON
605
+
606
+ #if SIMSIMD_TARGET_SVE2
607
+ #pragma GCC push_options
608
+ #pragma GCC target("arch=armv8.2-a+sve+sve2")
609
+ #pragma clang attribute push(__attribute__((target("arch=armv8.2-a+sve+sve2"))), apply_to = function)
610
+
611
+ /* SVE2 introduces many new integer-oriented instructions, extending some of the NEON functionality to
612
+ * variable-length SVE registers. Those include "compare multiple" intrinsics:
613
+ *
614
+ * - `svmatch[_u16]` that matches each scalar in first vector against all members of a 128-bit lane in the second.
615
+ * - `svhistcnt[_s32]_z` does something similar, performing an inclusive prefix scan.
616
+ * - `svtbx[_u16]` does extended table lookup
617
+ *
618
+ * Other notable instructions:
619
+ *
620
+ * - `DUP`: Broadcast indexed predicate element
621
+ * https://developer.arm.com/documentation/ddi0602/2021-06/SVE-Instructions/DUP--predicate---Broadcast-indexed-predicate-element-?lang=en
622
+ * - `SCLAMP` and `UCLAMP`: clamp values, i.e. combined min+max
623
+ * https://developer.arm.com/documentation/ddi0602/2021-06/SVE-Instructions/SCLAMP--Signed-clamp-to-minimum-maximum-vector-?lang=en
624
+ * https://developer.arm.com/documentation/ddi0602/2021-06/SVE-Instructions/UCLAMP--Unsigned-clamp-to-minimum-maximum-vector-?lang=en
625
+ * - `TBLQ`: Table lookup quadword
626
+ * https://developer.arm.com/documentation/ddi0602/2022-12/SVE-Instructions/TBLQ--Programmable-table-lookup-within-each-quadword-vector-segment--zeroing--?lang=en
627
+ * -
628
+ * https://developer.arm.com/documentation/ddi0602/2021-06/SVE-Instructions/INCB--INCD--INCH--INCW--scalar---Increment-scalar-by-multiple-of-predicate-constraint-element-count-
629
+ *
630
+ * Great resources for SVE2 intrinsics:
631
+ *
632
+ * > ARM’s Scalable Vector Extensions: A Critical Look at SVE2 For Integer Workloads
633
+ * https://gist.github.com/zingaburga/805669eb891c820bd220418ee3f0d6bd
634
+ */
635
+
636
+ SIMSIMD_PUBLIC void simsimd_intersect_u16_sve2(simsimd_u16_t const* a, simsimd_u16_t const* b, simsimd_size_t a_length,
637
+ simsimd_size_t b_length, simsimd_distance_t* results) {
638
+
639
+ // A single SVE lane is 128 bits wide, so one lane fits 8 values.
640
+ simsimd_size_t const register_size = svcnth();
641
+ simsimd_size_t const lanes_count = register_size / 8;
642
+ simsimd_size_t a_idx = 0, b_idx = 0;
643
+ simsimd_size_t c = 0;
644
+
645
+ while (a_idx < a_length && b_idx < b_length) {
646
+ // Load `a_member` and broadcast it, load `b_members_vec` from memory
647
+ svbool_t a_progress = svwhilelt_b16_u64(a_idx, a_length);
648
+ svbool_t b_progress = svwhilelt_b16_u64(b_idx, b_length);
649
+ svuint16_t a_vec = svld1_u16(a_progress, a + a_idx);
650
+ svuint16_t b_vec = svld1_u16(b_progress, b + b_idx);
651
+
652
+ // Intersecting registers with `svmatch_u16` involves a lot of shuffling
653
+ // and comparisons, so we want to avoid it if the slices don't overlap at all..
654
+ simsimd_u16_t a_min;
655
+ simsimd_u16_t a_max = svlastb(a_progress, a_vec);
656
+ simsimd_u16_t b_min = svlasta(svpfalse_b(), b_vec);
657
+ simsimd_u16_t b_max = svlastb(b_progress, b_vec);
658
+
659
+ // If the slices don't overlap, advance the appropriate pointer
660
+ while (a_max < b_min && (a_idx + register_size) < a_length) {
661
+ a_idx += register_size;
662
+ a_progress = svwhilelt_b16_u64(a_idx, a_length);
663
+ a_vec = svld1_u16(a_progress, a + a_idx);
664
+ a_max = svlastb(a_progress, a_vec);
665
+ }
666
+ a_min = svlasta(svpfalse_b(), a_vec);
667
+ while (b_max < a_min && (b_idx + register_size) < b_length) {
668
+ b_idx += register_size;
669
+ b_progress = svwhilelt_b16_u64(b_idx, b_length);
670
+ b_vec = svld1_u16(b_progress, b + b_idx);
671
+ b_max = svlastb(b_progress, b_vec);
672
+ }
673
+ b_min = svlasta(svpfalse_b(), b_vec);
674
+
675
+ // Before we evaluate the intersection size, obfurscating the order in `b_vec`,
676
+ // let's estimate how much we will need to advance the pointers afterwards.
677
+ // For that, we don't even need to broadcast the values in SVE, as the whole
678
+ // register can be compared against a scalar:
679
+ //
680
+ // svuint16_t a_last_broadcasted = svdup_n_u16(a_max);
681
+ // svuint16_t b_last_broadcasted = svdup_n_u16(b_max);
682
+ svbool_t a_mask = svcmple_n_u16(a_progress, a_vec, b_max);
683
+ svbool_t b_mask = svcmple_n_u16(b_progress, b_vec, a_max);
684
+ simsimd_u64_t a_step = svcntp_b16(a_progress, a_mask);
685
+ simsimd_u64_t b_step = svcntp_b16(b_progress, b_mask);
686
+
687
+ // Compare `a_vec` with each lane of `b_vec`
688
+ svbool_t equal_mask = svmatch_u16(a_progress, a_vec, b_vec);
689
+ for (simsimd_size_t i = 1; i < lanes_count; i++) {
690
+ b_vec = svext_u16(b_vec, b_vec, 8);
691
+ equal_mask = svorr_z(svptrue_b16(), equal_mask, svmatch_u16(a_progress, a_vec, b_vec));
692
+ }
693
+ simsimd_size_t equal_count = svcntp_b16(svptrue_b16(), equal_mask);
694
+
695
+ // Advance
696
+ a_idx += a_step;
697
+ b_idx += b_step;
698
+ c += equal_count;
699
+ }
700
+ *results = c;
701
+ }
702
+
703
+ SIMSIMD_PUBLIC void simsimd_intersect_u32_sve2(simsimd_u32_t const* a, simsimd_u32_t const* b, simsimd_size_t a_length,
704
+ simsimd_size_t b_length, simsimd_distance_t* results) {
705
+
706
+ // A single SVE lane is 128 bits wide, so one lane fits 4 values.
707
+ simsimd_size_t const register_size = svcntw();
708
+ simsimd_size_t const lanes_count = register_size / 4;
709
+ simsimd_size_t a_idx = 0, b_idx = 0;
710
+ simsimd_size_t c = 0;
711
+
712
+ while (a_idx < a_length && b_idx < b_length) {
713
+ // Load `a_member` and broadcast it, load `b_members_vec` from memory
714
+ svbool_t a_progress = svwhilelt_b32_u64(a_idx, a_length);
715
+ svbool_t b_progress = svwhilelt_b32_u64(b_idx, b_length);
716
+ svuint32_t a_vec = svld1_u32(a_progress, a + a_idx);
717
+ svuint32_t b_vec = svld1_u32(b_progress, b + b_idx);
718
+
719
+ // Intersecting registers with `svmatch_u16` involves a lot of shuffling
720
+ // and comparisons, so we want to avoid it if the slices don't overlap at all..
721
+ simsimd_u32_t a_min;
722
+ simsimd_u32_t a_max = svlastb(a_progress, a_vec);
723
+ simsimd_u32_t b_min = svlasta(svpfalse_b(), b_vec);
724
+ simsimd_u32_t b_max = svlastb(b_progress, b_vec);
725
+
726
+ // If the slices don't overlap, advance the appropriate pointer
727
+ while (a_max < b_min && (a_idx + register_size) < a_length) {
728
+ a_idx += register_size;
729
+ a_progress = svwhilelt_b32_u64(a_idx, a_length);
730
+ a_vec = svld1_u32(a_progress, a + a_idx);
731
+ a_max = svlastb(a_progress, a_vec);
732
+ }
733
+ a_min = svlasta(svpfalse_b(), a_vec);
734
+ while (b_max < a_min && (b_idx + register_size) < b_length) {
735
+ b_idx += register_size;
736
+ b_progress = svwhilelt_b32_u64(b_idx, b_length);
737
+ b_vec = svld1_u32(b_progress, b + b_idx);
738
+ b_max = svlastb(b_progress, b_vec);
739
+ }
740
+ b_min = svlasta(svpfalse_b(), b_vec);
741
+
742
+ // Before we evaluate the intersection size, obfurscating the order in `b_vec`,
743
+ // let's estimate how much we will need to advance the pointers afterwards.
744
+ // For that, we don't even need to broadcast the values in SVE, as the whole
745
+ // register can be compared against a scalar:
746
+ //
747
+ // svuint32_t a_last_broadcasted = svdup_n_u32(a_max);
748
+ // svuint32_t b_last_broadcasted = svdup_n_u32(b_max);
749
+ svbool_t a_mask = svcmple_n_u32(a_progress, a_vec, b_max);
750
+ svbool_t b_mask = svcmple_n_u32(b_progress, b_vec, a_max);
751
+ simsimd_u64_t a_step = svcntp_b32(a_progress, a_mask);
752
+ simsimd_u64_t b_step = svcntp_b32(b_progress, b_mask);
753
+
754
+ // Comparing `a_vec` with each lane of `b_vec` can't be done with `svmatch`,
755
+ // the same way as in `simsimd_intersect_u16_sve2`, as that instruction is only
756
+ // available for 8-bit and 16-bit integers.
757
+ //
758
+ // svbool_t equal_mask = svpfalse_b();
759
+ // for (simsimd_size_t i = 0; i < register_size; i++) {
760
+ // equal_mask = svorr_z(svptrue_b32(), equal_mask, svcmpeq_u32(a_progress, a_vec, b_vec));
761
+ // b_vec = svext_u32(b_vec, b_vec, 1);
762
+ // }
763
+ // simsimd_size_t equal_count = svcntp_b32(a_progress, equal_mask);
764
+ //
765
+ // Alternatively, one can use histogram instructions, like `svhistcnt_u32_z`.
766
+ // They practically compute the prefix-matching count, which is equivalent to
767
+ // the lower triangle of the row-major intersection matrix.
768
+ // To compute the upper triangle, we can reverse (with `svrev_b32`) the order of
769
+ // elements and repeat the operation, accumulating the results for top and bottom.
770
+ // Let's look at 4x element registers as an example:
771
+ //
772
+ // ⊐ α = {A, B, C, D}, β = {X, Y, Z, W}:
773
+ //
774
+ // hist(α, β): hist(α_rev, β_rev):
775
+ //
776
+ // X Y Z W W Z Y X
777
+ // A 1 0 0 0 D 1 0 0 0
778
+ // B 1 1 0 0 C 1 1 0 0
779
+ // C 1 1 1 0 B 1 1 1 0
780
+ // D 1 1 1 1 A 1 1 1 1
781
+ //
782
+ svuint32_t hist_lower = svhistcnt_u32_z(a_progress, a_vec, b_vec);
783
+ svuint32_t a_rev_vec = svrev_u32(a_vec);
784
+ svuint32_t b_rev_vec = svrev_u32(b_vec);
785
+ svuint32_t hist_upper = svrev_u32(svhistcnt_u32_z(svptrue_b32(), a_rev_vec, b_rev_vec));
786
+ svuint32_t hist = svorr_u32_x(a_progress, hist_lower, hist_upper);
787
+ svbool_t equal_mask = svcmpne_n_u32(a_progress, hist, 0);
788
+ simsimd_size_t equal_count = svcntp_b32(a_progress, equal_mask);
789
+
790
+ // Advance
791
+ a_idx += a_step;
792
+ b_idx += b_step;
793
+ c += equal_count;
794
+ }
795
+ *results = c;
796
+ }
797
+
798
+ #pragma clang attribute pop
799
+ #pragma GCC pop_options
800
+ #endif // SIMSIMD_TARGET_SVE2
801
+ #endif // SIMSIMD_TARGET_ARM
802
+
803
+ #ifdef __cplusplus
804
+ }
805
+ #endif
806
+
807
+ #endif