scann-core 0.2.1

Rust bindings for scann-core: ScaNN's approximate nearest-neighbour vector search core as a standalone library (derived from Google's ScaNN; not an official Google product)
Documentation
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
#include <cstdint>

#ifndef HWY_DISABLED_TARGETS
#define HWY_DISABLED_TARGETS HWY_ALL_SVE
#endif

#undef HWY_TARGET_INCLUDE
#define HWY_TARGET_INCLUDE "scann/hashes/internal/lut16_highway.inc"
#include "hwy/foreach_target.h"
#include "scann/hashes/internal/lut16_args.h"

#if !defined(__x86_64__) || defined(SCANN_FORCE_HIGHWAY_LUT16)

#include "absl/base/prefetch.h"
#include "hwy/highway.h"
#include "scann/hashes/internal/lut16_highway.h"
#include "scann/oss_wrappers/scann_bits.h"
#include "scann/utils/bits.h"
#include "scann/utils/common.h"
#include "scann/utils/intrinsics/attributes.h"

HWY_BEFORE_NAMESPACE();

namespace research_scann {
namespace asymmetric_hashing_internal {

namespace HWY_NAMESPACE {
namespace hn = hwy::HWY_NAMESPACE;

SCANN_HIGHWAY_INLINE void ExpandToInt32AccumAndStore(
    const int16_t* HWY_RESTRICT dist16, int32_t* HWY_RESTRICT dist32) {
  const hn::ScalableTag<int16_t> d16;
  const hn::ScalableTag<int32_t> d32;
  const hn::Half<decltype(d16)> d16_half;
  using V16 = decltype(hn::Zero(d16));
  using V32 = decltype(hn::Zero(d32));
  using V8 = decltype(hn::Zero(d16_half));
  const size_t N = hn::Lanes(d16);
  const size_t num_iters = 32 / N;
  for (size_t i = 0; i < num_iters; ++i) {
    V16 val16 = hn::LoadU(d16, dist16 + i * N);

    V8 val16_hi = hn::UpperHalf(d16_half, val16);
    V8 val16_lo = hn::LowerHalf(d16_half, val16);

    V32 val32_hi = hn::PromoteTo(d32, val16_hi);
    V32 val32_lo = hn::PromoteTo(d32, val16_lo);

    V32 dist32_lo = hn::LoadU(d32, dist32 + (i * N));
    V32 dist32_hi = hn::LoadU(d32, dist32 + (i * N) + (N / 2));

    hn::StoreU(hn::Add(dist32_lo, val32_lo), d32, dist32 + (i * N));
    hn::StoreU(hn::Add(dist32_hi, val32_hi), d32, dist32 + (i * N) + (N / 2));
  }
}

SCANN_HIGHWAY_INLINE void ConvertToFloatMultiplyAndStore(const int32_t* dist32,
                                                         const float mult,
                                                         float* distf32) {
  const hn::ScalableTag<int32_t> d32;
  const hn::ScalableTag<float> df;
  using V32 = decltype(hn::Zero(d32));
  using VF = decltype(hn::Zero(df));
  const size_t N = hn::Lanes(d32);
  const size_t num_iters = 32 / N;
  auto mults = hn::Set(df, mult);
  for (size_t i = 0; i < num_iters; ++i) {
    V32 val32 = hn::LoadU(d32, dist32 + (i * N));
    VF valf32 = hn::ConvertTo(df, val32);
    hn::StoreU(hn::Mul(valf32, mults), df, distf32 + (i * N));
  }
}

SCANN_HIGHWAY_INLINE uint32_t
ComputePushMask(const int16_t* HWY_RESTRICT dist16, const int16_t threshold) {
  const hn::ScalableTag<int16_t> d16;
  using V16 = decltype(hn::Zero(d16));
  const size_t N = hn::Lanes(d16);
  const size_t num_iters = 32 / N;

  uint8_t mask_bits[8];
  uint32_t ret = 0;
  size_t total_bytes = 0;

  V16 simd_threshold = hn::Set(d16, threshold);
  for (size_t i = 0; i < num_iters; ++i) {
    auto mask = hn::Lt(hn::LoadU(d16, dist16 + (i * N)), simd_threshold);
    total_bytes += hn::StoreMaskBits(d16, mask, &mask_bits[i * N / 8]);
  }

  DCHECK_EQ(total_bytes, 4);
  for (size_t i = 0; i < total_bytes; ++i) {
    ret |= (uint32_t)mask_bits[i] << (i * 8);
  }
  return ret;
}

SCANN_HIGHWAY_INLINE void ExpandConvertToFloatAndStore(
    const int16_t* HWY_RESTRICT dist16, float* HWY_RESTRICT distf32) {
  const hn::ScalableTag<int16_t> d16;
  const hn::ScalableTag<int32_t> d32;
  const hn::ScalableTag<float> df;
  const hn::Half<decltype(d16)> d16_half;
  using V16 = decltype(hn::Zero(d16));
  using V32 = decltype(hn::Zero(d32));
  using V8 = decltype(hn::Zero(d16_half));
  const size_t N = hn::Lanes(d16);
  const size_t num_iters = 32 / N;
  for (size_t i = 0; i < num_iters; ++i) {
    V16 val16 = hn::LoadU(d16, dist16 + i * N);

    V8 val16_hi = hn::UpperHalf(d16_half, val16);
    V8 val16_lo = hn::LowerHalf(d16_half, val16);

    V32 val32_hi = hn::PromoteTo(d32, val16_hi);
    V32 val32_lo = hn::PromoteTo(d32, val16_lo);

    hn::StoreU(hn::ConvertTo(df, val32_lo), df, distf32 + (i * N));
    hn::StoreU(hn::ConvertTo(df, val32_hi), df, distf32 + (i * N) + (N / 2));
  }
}

SCANN_HIGHWAY_INLINE void MultiplyAddAndStoreFloat(
    const float* HWY_RESTRICT distf32, const float mult, const float biase,
    float* HWY_RESTRICT result) {
  const hn::ScalableTag<float> df;
  using VF = decltype(hn::Zero(df));
  const size_t N = hn::Lanes(df);
  const size_t num_iters = 32 / N;
  VF mults = hn::Set(df, mult);
  VF biases = hn::Set(df, biase);
  for (size_t i = 0; i < num_iters; ++i) {
    VF fval = hn::LoadU(df, distf32 + (i * N));
    hn::StoreU(hn::MulAdd(fval, mults, biases), df, result + (i * N));
  }
}

template <class D, class D16 = hn::Repartition<int16_t, D>>
SCANN_HIGHWAY_INLINE void Accumulate(const D d, hn::Vec<D> res,
                                     hn::Vec<D16>& int16_accums0,
                                     hn::Vec<D16>& int16_accums1) {
  const hn::Repartition<int16_t, D> dto16;
  const hn::Repartition<uint16_t, D> dtou16;
  int16_accums0 =
      hn::Add(int16_accums0, hn::BitCast(dto16, hn::BitCast(dtou16, res)));
  int16_accums1 =
      hn::Add(int16_accums1,
              hn::BitCast(dto16, hn::ShiftRight<8>(hn::BitCast(dtou16, res))));
}

template <PrefetchStrategy kPrefetch, class V16>
SCANN_HIGHWAY_INLINE void HighwayLUT16BottomLoopAccum(
    const uint8_t* HWY_RESTRICT data_start, const uint8_t* lookup_starts,
    const DimensionIndex num_blocks, V16& int16_accums0, V16& int16_accums1,
    V16& int16_accums2, V16& int16_accums3) {
  const hn::ScalableTag<uint8_t> d;
  using V = decltype(hn::Zero(d));
  const size_t N = hn::Lanes(d);
  const size_t num_blocks_per_iter = N / 16;
  const size_t num_iters = num_blocks / num_blocks_per_iter;
  V bitmask = hn::Set(d, 0x0F);

  for (size_t i = 0; i < num_iters; ++i) {
    if constexpr (kPrefetch == PrefetchStrategy::kSeq) {
      absl::PrefetchToLocalCache((data_start + i * N) + kPrefetchBytesAhead);
    } else if constexpr (kPrefetch == PrefetchStrategy::kSmart) {
      absl::PrefetchToLocalCacheNta((data_start + i * N) + kPrefetchBytesAhead);
    }

    V dp = hn::LoadU(d, data_start + i * N);
    V dp0 = hn::And(dp, bitmask);
    V dp1 = hn::ShiftRight<4>(dp);
    V lut = hn::LoadU(d, lookup_starts + i * N);
    V res0 = hn::TableLookupBytes(lut, dp0);
    V res1 = hn::TableLookupBytes(lut, dp1);
    Accumulate(d, res0, int16_accums0, int16_accums1);
    Accumulate(d, res1, int16_accums2, int16_accums3);
  }

  const size_t rem = num_blocks % num_blocks_per_iter;
  if (rem > 0) {
    if (N == 32) {
      const hn::Half<decltype(d)> dh;
      V dp =
          hn::ZeroExtendVector(d, hn::LoadU(dh, data_start + (num_iters * N)));
      V dp0 = hn::And(dp, bitmask);
      V dp1 = hn::ShiftRight<4>(dp);
      V lut = hn::ZeroExtendVector(
          d, hn::LoadU(dh, lookup_starts + (num_iters * N)));
      V res0 = hn::TableLookupBytes(lut, dp0);
      V res1 = hn::TableLookupBytes(lut, dp1);
      Accumulate(d, res0, int16_accums0, int16_accums1);
      Accumulate(d, res1, int16_accums2, int16_accums3);
    } else {
      auto mask = hn::FirstN(d, 16 * rem);
      V dp = hn::MaskedLoad(mask, d, data_start + (num_iters * N));
      V dp0 = hn::And(dp, bitmask);
      V dp1 = hn::ShiftRight<4>(dp);
      V lut = MaskedLoad(mask, d, lookup_starts + (num_iters * N));
      V res0 = hn::TableLookupBytes(lut, dp0);
      V res1 = hn::TableLookupBytes(lut, dp1);
      Accumulate(d, res0, int16_accums0, int16_accums1);
      Accumulate(d, res1, int16_accums2, int16_accums3);
    }
  }
}

template <class D16>
SCANN_HIGHWAY_INLINE void HighwayPostProcess128Bits(
    D16 d, const DimensionIndex num_blocks, std::array<int16_t, 32>& dist16,
    hn::Vec<D16>& int16_accums0, hn::Vec<D16>& int16_accums1,
    hn::Vec<D16>& int16_accums2, hn::Vec<D16>& int16_accums3) {
  const hn::ScalableTag<int16_t> d16;
  using V16 = decltype(hn::Zero(d16));
  V16 total_bias = Set(d16, num_blocks * 128);
  V16 v0 = int16_accums0;
  V16 v1 = int16_accums1;

  V16 even = hn::Sub(v0, hn::ShiftLeft<8>(v1));

  v0 = hn::Sub(hn::InterleaveLower(d16, even, v1), total_bias);
  v1 = hn::Sub(hn::InterleaveUpper(d16, even, v1), total_bias);
  hn::StoreU(v0, d16, dist16.data());
  hn::StoreU(v1, d16, dist16.data() + 8);

  v0 = int16_accums2;
  v1 = int16_accums3;
  even = hn::Sub(v0, hn::ShiftLeft<8>(v1));
  v0 = hn::Sub(hn::InterleaveLower(d16, even, v1), total_bias);
  v1 = hn::Sub(hn::InterleaveUpper(d16, even, v1), total_bias);
  hn::StoreU(v0, d16, dist16.data() + 16);
  hn::StoreU(v1, d16, dist16.data() + 24);
}

template <class D16>
SCANN_HIGHWAY_INLINE void HighwayPostProcess256Bits(
    D16 d, const DimensionIndex num_blocks, std::array<int16_t, 32>& dist16,
    hn::Vec<D16>& int16_accums0, hn::Vec<D16>& int16_accums1,
    hn::Vec<D16>& int16_accums2, hn::Vec<D16>& int16_accums3) {
  const hn::ScalableTag<int16_t> d16;
  using V16 = decltype(hn::Zero(d16));

  V16 total_bias = Set(d16, num_blocks * 128);
  V16 v0 = int16_accums0;
  V16 v1 = int16_accums1;
  V16 even = hn::Sub(v0, hn::ShiftLeft<8>(v1));
  v0 = hn::InterleaveLower(d16, even, v1);
  v1 = hn::InterleaveUpper(d16, even, v1);
  V16 lu = hn::ConcatLowerUpper(d16, v1, v0);
  V16 ul = hn::ConcatUpperLower(d16, v1, v0);
  v0 = hn::Sub(hn::Add(lu, ul), total_bias);
  hn::StoreU(v0, d16, dist16.data());
  v0 = int16_accums2;
  v1 = int16_accums3;
  even = hn::Sub(v0, hn::ShiftLeft<8>(v1));
  v0 = hn::InterleaveLower(d16, even, v1);
  v1 = hn::InterleaveUpper(d16, even, v1);
  lu = hn::ConcatLowerUpper(d16, v1, v0);
  ul = hn::ConcatUpperLower(d16, v1, v0);
  v0 = hn::Sub(hn::Add(lu, ul), total_bias);
  hn::StoreU(v0, d16, dist16.data() + 16);
}

template <class D16>
SCANN_HIGHWAY_INLINE void HighwayPostProcess512Bits(
    D16 d, const DimensionIndex num_blocks, std::array<int16_t, 32>& dist16,
    hn::Vec<D16>& int16_accums0, hn::Vec<D16>& int16_accums1,
    hn::Vec<D16>& int16_accums2, hn::Vec<D16>& int16_accums3) {
  const hn::ScalableTag<int16_t> d16;
  const hn::Half<decltype(d16)> d16_half;
  using V16 = decltype(hn::Zero(d16));
  using V8 = decltype(hn::Zero(d16_half));
  V8 total_bias = hn::Set(d16_half, num_blocks * 128);

  V16 v0 = int16_accums0;
  V16 v1 = int16_accums1;
  V16 even = hn::Sub(v0, hn::ShiftLeft<8>(v1));
  v0 = hn::InterleaveLower(d16, even, v1);
  v1 = hn::InterleaveUpper(d16, even, v1);
  V16 lu = hn::ConcatLowerUpper(d16, v1, v0);
  V16 ul = hn::ConcatUpperLower(d16, v1, v0);
  v0 = hn::Add(lu, ul);
  V8 v_lo = hn::LowerHalf(d16_half, v0);
  V8 v_hi = hn::UpperHalf(d16_half, v0);
  V8 luh = hn::ConcatLowerUpper(d16_half, v_hi, v_lo);
  V8 ulh = hn::ConcatUpperLower(d16_half, v_hi, v_lo);
  v_lo = hn::Sub(hn::Add(luh, ulh), total_bias);
  hn::StoreU(v_lo, d16_half, dist16.data());
  v0 = int16_accums2;
  v1 = int16_accums3;
  even = hn::Sub(v0, hn::ShiftLeft<8>(v1));
  v0 = hn::InterleaveLower(d16, even, v1);
  v1 = hn::InterleaveUpper(d16, even, v1);
  lu = hn::ConcatLowerUpper(d16, v1, v0);
  ul = hn::ConcatUpperLower(d16, v1, v0);
  v0 = hn::Add(lu, ul);
  v_lo = hn::LowerHalf(d16_half, v0);
  v_hi = hn::UpperHalf(d16_half, v0);
  luh = hn::ConcatLowerUpper(d16_half, v_hi, v_lo);
  ulh = hn::ConcatUpperLower(d16_half, v_hi, v_lo);
  v_lo = hn::Sub(hn::Add(luh, ulh), total_bias);
  hn::StoreU(v_lo, d16_half, dist16.data() + 16);
}

template <size_t kNumQueries, PrefetchStrategy kPrefetch>
SCANN_HIGHWAY_INLINE void HighwayLUT16BottomLoop(
    const uint8_t* HWY_RESTRICT data_start,
    array<const uint8_t*, kNumQueries> lookup_starts,
    const DimensionIndex num_blocks,
    array<array<int16_t, 32>, kNumQueries>& dist16) {
  static_assert(kNumQueries <= 3,
                "Register spilling happens when kNumQueries > 3");
  const hn::ScalableTag<uint8_t> d;
  const hn::ScalableTag<int16_t> d16;
  const size_t N = hn::Lanes(d);
  const size_t num_32dp_blocks = N / 16;
  using V16 = decltype(hn::Zero(d16));

  V16 int16_accums0 = hn::Zero(d16);
  V16 int16_accums1 = int16_accums0;
  V16 int16_accums2 = int16_accums0;
  V16 int16_accums3 = int16_accums0;
  V16 int16_accums4 = int16_accums0;
  V16 int16_accums5 = int16_accums0;
  V16 int16_accums6 = int16_accums0;
  V16 int16_accums7 = int16_accums0;
  V16 int16_accums8 = int16_accums0;
  V16 int16_accums9 = int16_accums0;
  V16 int16_accums10 = int16_accums0;
  V16 int16_accums11 = int16_accums0;

  HighwayLUT16BottomLoopAccum<kPrefetch>(
      data_start, lookup_starts[0], num_blocks, int16_accums0, int16_accums1,
      int16_accums2, int16_accums3);
  if constexpr (kNumQueries > 1)
    HighwayLUT16BottomLoopAccum<kPrefetch>(
        data_start, lookup_starts[1], num_blocks, int16_accums4, int16_accums5,
        int16_accums6, int16_accums7);
  if constexpr (kNumQueries > 2)
    HighwayLUT16BottomLoopAccum<kPrefetch>(
        data_start, lookup_starts[2], num_blocks, int16_accums8, int16_accums9,
        int16_accums10, int16_accums11);

  if (num_32dp_blocks == 1) {
    HighwayPostProcess128Bits(d16, num_blocks, dist16[0], int16_accums0,
                              int16_accums1, int16_accums2, int16_accums3);
    if constexpr (kNumQueries > 1)
      HighwayPostProcess128Bits(d16, num_blocks, dist16[1], int16_accums4,
                                int16_accums5, int16_accums6, int16_accums7);
    if constexpr (kNumQueries > 2)
      HighwayPostProcess128Bits(d16, num_blocks, dist16[2], int16_accums8,
                                int16_accums9, int16_accums10, int16_accums11);
  } else if (num_32dp_blocks == 2) {
    HighwayPostProcess256Bits(d16, num_blocks, dist16[0], int16_accums0,
                              int16_accums1, int16_accums2, int16_accums3);
    if constexpr (kNumQueries > 1)
      HighwayPostProcess256Bits(d16, num_blocks, dist16[1], int16_accums4,
                                int16_accums5, int16_accums6, int16_accums7);
    if constexpr (kNumQueries > 2)
      HighwayPostProcess256Bits(d16, num_blocks, dist16[2], int16_accums8,
                                int16_accums9, int16_accums10, int16_accums11);
  } else if (num_32dp_blocks == 4) {
    HighwayPostProcess512Bits(d16, num_blocks, dist16[0], int16_accums0,
                              int16_accums1, int16_accums2, int16_accums3);
    if constexpr (kNumQueries > 1)
      HighwayPostProcess512Bits(d16, num_blocks, dist16[1], int16_accums4,
                                int16_accums5, int16_accums6, int16_accums7);
    if constexpr (kNumQueries > 2)
      HighwayPostProcess512Bits(d16, num_blocks, dist16[2], int16_accums8,
                                int16_accums9, int16_accums10, int16_accums11);
  } else {
    LOG(FATAL) << ("Unimplemented Vector Size\n");
  }
}

template <size_t kBottomLevelBatchSize, size_t kNumQueries>
SCANN_HIGHWAY_INLINE array<const uint8_t*, kBottomLevelBatchSize>
MakeBottomLevelBatchLookupArray(
    array<const uint8_t*, kNumQueries> mid_level_lookups, size_t start) {
  DCHECK_LE(start + kBottomLevelBatchSize, kNumQueries);
  array<const uint8_t*, kBottomLevelBatchSize> result;
  for (size_t j : Seq(kBottomLevelBatchSize)) {
    result[j] = mid_level_lookups[start + j];
  }
  return result;
}

template <size_t kNumQueries, PrefetchStrategy kPrefetch>
SCANN_HIGHWAY_INLINE void HighwayLUT16MiddleLoop(
    const uint8_t* HWY_RESTRICT data_start,
    array<const uint8_t*, kNumQueries>& lookup_starts,
    const DimensionIndex num_blocks,
    array<array<int16_t, 32>, kNumQueries>& result16) {
  constexpr size_t kSizeB = (kNumQueries == 1) ? 1 : 2;
  constexpr size_t kNumBCases[] = {0, 2, 1};
  constexpr size_t kNumB = (kNumQueries == 1) ? 1 : kNumBCases[kNumQueries % 3];

  constexpr size_t kRemaining = kNumQueries - kNumB * kSizeB;
  static_assert(kRemaining % 3 == 0, "");

  constexpr size_t kSizeA = 3;
  constexpr size_t kNumA = kRemaining / 3;

  for (size_t j : Seq(kNumA)) {
    array<array<int16_t, 32>, kSizeA> dist16;
    const size_t start = j * kSizeA;
    auto bottom_level_lookups =
        MakeBottomLevelBatchLookupArray<kSizeA>(lookup_starts, start);
    HighwayLUT16BottomLoop<kSizeA, kPrefetch>(data_start, bottom_level_lookups,
                                              num_blocks, dist16);
    for (size_t jj : Seq(kSizeA)) {
      std::copy(dist16[jj].begin(), dist16[jj].end(),
                result16[start + jj].begin());
    }
  }

  for (size_t j : Seq(kNumB)) {
    array<array<int16_t, 32>, kSizeB> dist16;
    const size_t start = kNumA * kSizeA + j * kSizeB;
    auto bottom_level_lookups =
        MakeBottomLevelBatchLookupArray<kSizeB>(lookup_starts, start);
    HighwayLUT16BottomLoop<kSizeB, kPrefetch>(data_start, bottom_level_lookups,
                                              num_blocks, dist16);
    for (size_t jj : Seq(kSizeB)) {
      std::copy(dist16[jj].begin(), dist16[jj].end(),
                result16[start + jj].begin());
    }
  }
}

template <size_t kNumQueries, PrefetchStrategy kPrefetch>
SCANN_HIGHWAY_INLINE void HighwayLUT16MiddleLoopInt32(
    const uint8_t* HWY_RESTRICT data_start,
    array<const uint8_t*, kNumQueries> lookup_starts,
    const DimensionIndex num_blocks,
    array<array<int32_t, 32>, kNumQueries>& result32) {
  array<array<int16_t, 32>, kNumQueries> result16;

  for (int i = 0; i < kNumQueries; ++i) {
    result32[i].fill(0);
  }

  for (DimensionIndex k = 0; k < num_blocks;) {
    DimensionIndex reaccumulate_limit = std::min(num_blocks - k, uint64_t{256});
    k += reaccumulate_limit;
    HighwayLUT16MiddleLoop<kNumQueries, kPrefetch>(
        data_start, lookup_starts, reaccumulate_limit, result16);
    data_start += 16 * reaccumulate_limit;
    for (size_t j : Seq(kNumQueries)) {
      ExpandToInt32AccumAndStore(result16[j].data(), result32[j].data());
      lookup_starts[j] += 16 * reaccumulate_limit;
    }
  }
}

template <size_t size, typename T>
SCANN_HIGHWAY_INLINE array<T, size> ToLocalArray(ConstSpan<T> span) {
  DCHECK_EQ(span.size(), size);
  array<T, size> result;
  std::copy(span.begin(), span.begin() + size, result.begin());
  return result;
}

#define SCANN_GET_INT16_DISTANCES_IMPL(batch_size, prefetch_strategy_int)    \
  SCANN_HIGHWAY_INLINE void                                                  \
      GetInt16Distances##batch_size##prefetch_strategy_int(                  \
          LUT16Args<int16_t> args) {                                         \
    const uint8_t* packed_dataset = args.packed_dataset;                     \
    const size_t num_32dp_simd_iters = args.num_32dp_simd_iters;             \
    const size_t num_blocks = args.num_blocks;                               \
    auto lookups = ToLocalArray<batch_size>(args.lookups);                   \
    auto distances = ToLocalArray<batch_size>(args.distances);               \
    array<array<int16_t, 32>, batch_size> result16;                          \
    for (size_t k : Seq(num_32dp_simd_iters)) {                              \
      const size_t dp_idx = k * 32;                                          \
      const uint8_t* data_start = packed_dataset + dp_idx * num_blocks / 2;  \
      HWY_NAMESPACE::HighwayLUT16MiddleLoop<                                 \
          batch_size, static_cast<PrefetchStrategy>(prefetch_strategy_int)>( \
          data_start, lookups, num_blocks, result16);                        \
      for (size_t j : Seq(batch_size)) {                                     \
        std::copy(result16[j].begin(), result16[j].end(),                    \
                  distances[j] + dp_idx);                                    \
      }                                                                      \
    }                                                                        \
  }

#define SCANN_GET_INT16_DISTANCES(batch_size)    \
  SCANN_GET_INT16_DISTANCES_IMPL(batch_size, 0); \
  SCANN_GET_INT16_DISTANCES_IMPL(batch_size, 1); \
  SCANN_GET_INT16_DISTANCES_IMPL(batch_size, 2);

SCANN_GET_INT16_DISTANCES(1);
SCANN_GET_INT16_DISTANCES(2);
SCANN_GET_INT16_DISTANCES(3);
SCANN_GET_INT16_DISTANCES(4);
SCANN_GET_INT16_DISTANCES(5);
SCANN_GET_INT16_DISTANCES(6);
SCANN_GET_INT16_DISTANCES(7);
SCANN_GET_INT16_DISTANCES(8);
SCANN_GET_INT16_DISTANCES(9);
#undef SCANN_GET_INT16_DISTANCES
#undef SCANN_GET_INT16_DISTANCES_IMPL

#define SCANN_GET_INT32_DISTANCES_IMPL(batch_size, prefetch_strategy_int)    \
  SCANN_HIGHWAY_INLINE void                                                  \
      GetInt32Distances##batch_size##prefetch_strategy_int(                  \
          LUT16Args<int32_t> args) {                                         \
    const uint8_t* packed_dataset = args.packed_dataset;                     \
    const size_t num_32dp_simd_iters = args.num_32dp_simd_iters;             \
    const size_t num_blocks = args.num_blocks;                               \
    auto lookups = ToLocalArray<batch_size>(args.lookups);                   \
    auto distances = ToLocalArray<batch_size>(args.distances);               \
    array<array<int32_t, 32>, batch_size> result32;                          \
    for (size_t k : Seq(num_32dp_simd_iters)) {                              \
      const size_t dp_idx = k * 32;                                          \
      const uint8_t* data_start = packed_dataset + dp_idx * num_blocks / 2;  \
      HWY_NAMESPACE::HighwayLUT16MiddleLoopInt32<                            \
          batch_size, static_cast<PrefetchStrategy>(prefetch_strategy_int)>( \
          data_start, lookups, num_blocks, result32);                        \
      for (size_t j : Seq(batch_size)) {                                     \
        std::copy(result32[j].begin(), result32[j].end(),                    \
                  distances[j] + dp_idx);                                    \
      }                                                                      \
    }                                                                        \
  }

#define SCANN_GET_INT32_DISTANCES(batch_size)    \
  SCANN_GET_INT32_DISTANCES_IMPL(batch_size, 0); \
  SCANN_GET_INT32_DISTANCES_IMPL(batch_size, 1); \
  SCANN_GET_INT32_DISTANCES_IMPL(batch_size, 2);

SCANN_GET_INT32_DISTANCES(1);
SCANN_GET_INT32_DISTANCES(2);
SCANN_GET_INT32_DISTANCES(3);
SCANN_GET_INT32_DISTANCES(4);
SCANN_GET_INT32_DISTANCES(5);
SCANN_GET_INT32_DISTANCES(6);
SCANN_GET_INT32_DISTANCES(7);
SCANN_GET_INT32_DISTANCES(8);
SCANN_GET_INT32_DISTANCES(9);
#undef SCANN_GET_INT32_DISTANCES
#undef SCANN_GET_INT32_DISTANCES_IMPL

#define SCANN_GET_FLOAT_DISTANCES_IMPL(batch_size, prefetch_strategy_int)    \
  SCANN_HIGHWAY_INLINE void                                                  \
      GetFloatDistances##batch_size##prefetch_strategy_int(                  \
          LUT16Args<float> args, ConstSpan<float> inv_fp_multipliers) {      \
    const uint8_t* packed_dataset = args.packed_dataset;                     \
    const size_t num_32dp_simd_iters = args.num_32dp_simd_iters;             \
    const size_t num_blocks = args.num_blocks;                               \
    auto lookups = ToLocalArray<batch_size>(args.lookups);                   \
    auto distances = ToLocalArray<batch_size>(args.distances);               \
    auto mults = ToLocalArray<batch_size>(inv_fp_multipliers);               \
    array<array<int32_t, 32>, batch_size> dist32;                            \
    for (size_t k : Seq(num_32dp_simd_iters)) {                              \
      const size_t dp_idx = k * 32;                                          \
      const uint8_t* data_start = packed_dataset + dp_idx * num_blocks / 2;  \
      HWY_NAMESPACE::HighwayLUT16MiddleLoopInt32<                            \
          batch_size, static_cast<PrefetchStrategy>(prefetch_strategy_int)>( \
          data_start, lookups, num_blocks, dist32);                          \
      for (size_t j : Seq(batch_size)) {                                     \
        HWY_NAMESPACE::ConvertToFloatMultiplyAndStore(                       \
            dist32[j].data(), mults[j], distances[j] + dp_idx);              \
      }                                                                      \
    }                                                                        \
  }

#define SCANN_GET_FLOAT_DISTANCES(batch_size)    \
  SCANN_GET_FLOAT_DISTANCES_IMPL(batch_size, 0); \
  SCANN_GET_FLOAT_DISTANCES_IMPL(batch_size, 1); \
  SCANN_GET_FLOAT_DISTANCES_IMPL(batch_size, 2);

SCANN_GET_FLOAT_DISTANCES(1);
SCANN_GET_FLOAT_DISTANCES(2);
SCANN_GET_FLOAT_DISTANCES(3);
SCANN_GET_FLOAT_DISTANCES(4);
SCANN_GET_FLOAT_DISTANCES(5);
SCANN_GET_FLOAT_DISTANCES(6);
SCANN_GET_FLOAT_DISTANCES(7);
SCANN_GET_FLOAT_DISTANCES(8);
SCANN_GET_FLOAT_DISTANCES(9);
#undef SCANN_GET_FLOAT_DISTANCES
#undef SCANN_GET_FLOAT_DISTANCES_IMPL

template <size_t kNumQueries, PrefetchStrategy kPrefetch, typename TopN>
SCANN_HIGHWAY_INLINE void GetTopInt16DistancesImpl(
    LUT16ArgsTopN<int16_t, TopN> args) {
  const uint8_t* packed_dataset = args.packed_dataset;
  const size_t num_32dp_simd_iters = args.num_32dp_simd_iters;
  const size_t num_blocks = args.num_blocks;
  auto lookups = ToLocalArray<kNumQueries>(args.lookups);
  const DatapointIndex first_dp_index = args.first_dp_index;
  const uint32_t final_mask = GetFinalMask32(args.num_datapoints);
  DCHECK_EQ(num_32dp_simd_iters, DivRoundUp(args.num_datapoints, 32));

  array<array<int16_t, 32>, kNumQueries> dist16;
  int16_t distances_buffer[32];

  int16_t simd_thresholds[kNumQueries];
  for (size_t j : Seq(kNumQueries)) {
    const int16_t int16_threshold = args.fast_topns[j]->epsilon();
    simd_thresholds[j] = int16_threshold;
  }

  typename TopN::Mutator topn_mutators[kNumQueries];
  for (size_t j : Seq(kNumQueries)) {
    args.fast_topns[j]->AcquireMutator(&topn_mutators[j]);
  }

  auto restrict_whitelist_ptrs =
      args.template GetRestrictWhitelistPtrs<kNumQueries>();
  for (DatapointIndex k : Seq(num_32dp_simd_iters)) {
    bool can_skip_batch = true;
    for (size_t j : Seq(kNumQueries)) {
      if (restrict_whitelist_ptrs[j] == nullptr ||
          restrict_whitelist_ptrs[j][k] != 0) {
        can_skip_batch = false;
        break;
      }
    }
    if (can_skip_batch) continue;

    const size_t dp_idx = k * 32;
    const uint8_t* data_start = packed_dataset + dp_idx * num_blocks / 2;
    HWY_NAMESPACE::HighwayLUT16MiddleLoop<kNumQueries, kPrefetch>(
        data_start, lookups, num_blocks, dist16);
    for (size_t j : Seq(kNumQueries)) {
      uint32_t push_mask =
          HWY_NAMESPACE::ComputePushMask(dist16[j].data(), simd_thresholds[j]);

      if (!push_mask) continue;

      std::copy(dist16[j].begin(), dist16[j].end(), distances_buffer);

      if (k == num_32dp_simd_iters - 1) {
        push_mask &= final_mask;
      }
      if (restrict_whitelist_ptrs[j]) {
        push_mask &= restrict_whitelist_ptrs[j][k];
      }

      while (push_mask) {
        const int offset = bits::FindLSBSetNonZero(push_mask);
        push_mask &= (push_mask - 1);
        const DatapointIndex dp_idx = first_dp_index + 32 * k + offset;
        DCHECK(
            !restrict_whitelist_ptrs[j] ||
            args.restrict_whitelists[j].IsWhitelisted(dp_idx - first_dp_index))
            << dp_idx;
        const int16_t distance = distances_buffer[offset];
        const bool needs_collection = topn_mutators[j].Push(dp_idx, distance);
        if (ABSL_PREDICT_FALSE(needs_collection)) {
          topn_mutators[j].GarbageCollect();

          simd_thresholds[j] = topn_mutators[j].epsilon();

          push_mask &= HWY_NAMESPACE::ComputePushMask(dist16[j].data(),
                                                      simd_thresholds[j]);
        }
      }
    }
  }
}

#define SCANN_GET_TOP_INT16_DISTANCES_IMPL(batch_size, prefetch_strategy_int)  \
  SCANN_HIGHWAY_INLINE void                                                    \
      GetTopInt16Distances1_##batch_size##prefetch_strategy_int(               \
          LUT16ArgsTopN<int16_t> args) {                                       \
    return GetTopInt16DistancesImpl<batch_size, static_cast<PrefetchStrategy>( \
                                                    prefetch_strategy_int)>(   \
        std::move(args));                                                      \
  }

#define SCANN_GET_TOP_INT16_DISTANCES(batch_size)    \
  SCANN_GET_TOP_INT16_DISTANCES_IMPL(batch_size, 0); \
  SCANN_GET_TOP_INT16_DISTANCES_IMPL(batch_size, 1); \
  SCANN_GET_TOP_INT16_DISTANCES_IMPL(batch_size, 2);

SCANN_GET_TOP_INT16_DISTANCES(1);
SCANN_GET_TOP_INT16_DISTANCES(2);
SCANN_GET_TOP_INT16_DISTANCES(3);
SCANN_GET_TOP_INT16_DISTANCES(4);
SCANN_GET_TOP_INT16_DISTANCES(5);
SCANN_GET_TOP_INT16_DISTANCES(6);
SCANN_GET_TOP_INT16_DISTANCES(7);
SCANN_GET_TOP_INT16_DISTANCES(8);
SCANN_GET_TOP_INT16_DISTANCES(9);
#undef SCANN_GET_TOP_INT16_DISTANCES
#undef SCANN_GET_TOP_INT16_DISTANCES_IMPL

SCANN_HIGHWAY_INLINE int16_t GetInt16Threshold(float float_threshold) {
  constexpr float kMaxValue = numeric_limits<int16_t>::max();
  return std::min(float_threshold, kMaxValue);
}

template <size_t kNumQueries, PrefetchStrategy kPrefetch, typename TopN>
SCANN_HIGHWAY_INLINE void GetTopFloatDistancesImpl(
    const bool with_spilling, LUT16ArgsTopN<float, TopN> args) {
  const uint8_t* packed_dataset = args.packed_dataset;
  const size_t num_32dp_simd_iters = args.num_32dp_simd_iters;
  const size_t num_blocks = args.num_blocks;
  auto lookups = ToLocalArray<kNumQueries>(args.lookups);
  const DatapointIndex first_dp_index = args.first_dp_index;
  const uint32_t final_mask = GetFinalMask32(args.num_datapoints);
  array<array<int16_t, 32>, kNumQueries> dist16;
  DCHECK_EQ(num_32dp_simd_iters, DivRoundUp(args.num_datapoints, 32));

  auto biases = ToLocalArray<kNumQueries>(args.biases);
  float simd_biases[kNumQueries];
  for (size_t j : Seq(kNumQueries)) {
    simd_biases[j] = biases[j];
  }

  auto mults = ToLocalArray<kNumQueries>(args.fixed_point_multipliers);
  float inv_mults[kNumQueries];
  for (size_t j : Seq(kNumQueries)) {
    inv_mults[j] = 1.0f / mults[j];
  }

  int16_t simd_thresholds[kNumQueries];
  for (size_t j : Seq(kNumQueries)) {
    const float epsilon = args.fast_topns[j]->epsilon();
    const float float_threshold = (epsilon - biases[j]) * mults[j];
    const int16_t int16_threshold = GetInt16Threshold(float_threshold);
    simd_thresholds[j] = int16_threshold;
  }

  typename TopN::Mutator topn_mutators[kNumQueries];
  for (size_t j : Seq(kNumQueries)) {
    args.fast_topns[j]->AcquireMutator(&topn_mutators[j]);
  }

  float fvals[32];
  float distances_buffer[32];
  auto restrict_whitelist_ptrs =
      args.template GetRestrictWhitelistPtrs<kNumQueries>();
  for (DatapointIndex k : Seq(num_32dp_simd_iters)) {
    bool can_skip_batch = true;
    for (size_t j : Seq(kNumQueries)) {
      if (restrict_whitelist_ptrs[j] == nullptr ||
          restrict_whitelist_ptrs[j][k] != 0) {
        can_skip_batch = false;
        break;
      }
    }
    if (can_skip_batch) continue;

    const uint8_t* data_start = packed_dataset + k * 16 * num_blocks;
    HWY_NAMESPACE::HighwayLUT16MiddleLoop<kNumQueries, kPrefetch>(
        data_start, lookups, num_blocks, dist16);
    for (size_t j : Seq(kNumQueries)) {
      uint32_t push_mask =
          HWY_NAMESPACE::ComputePushMask(dist16[j].data(), simd_thresholds[j]);

      if (!push_mask) continue;

      HWY_NAMESPACE::ExpandConvertToFloatAndStore(dist16[j].data(), fvals);
      HWY_NAMESPACE::MultiplyAddAndStoreFloat(fvals, inv_mults[j],
                                              simd_biases[j], distances_buffer);

      if (k == num_32dp_simd_iters - 1) {
        push_mask &= final_mask;
      }
      if (restrict_whitelist_ptrs[j]) {
        push_mask &= restrict_whitelist_ptrs[j][k];
      }
      if (args.batch_filter_predicate) {
        push_mask =
            args.batch_filter_predicate(first_dp_index + 32 * k, push_mask,
                                        args.datapoint_translation_predicate);
      }

      while (push_mask) {
        const int offset = bits::FindLSBSetNonZero(push_mask);
        push_mask &= (push_mask - 1);
        DatapointIndex dp_idx;
        if (!with_spilling) {
          dp_idx = first_dp_index + 32 * k + offset;
        }

        DCHECK(
            !restrict_whitelist_ptrs[j] ||
            args.restrict_whitelists[j].IsWhitelisted(dp_idx - first_dp_index))
            << dp_idx;
        if (args.datapoint_translation_predicate) {
          dp_idx = args.datapoint_translation_predicate(dp_idx);
        }

        DCHECK_LE(distances_buffer[offset],
                  topn_mutators[j].epsilon() + inv_mults[j]);
        const bool needs_gc = topn_mutators[j].PushNoEpsilonCheck(
            dp_idx, distances_buffer[offset]);
        if (ABSL_PREDICT_FALSE(needs_gc)) {
          topn_mutators[j].GarbageCollect();

          const float new_epsilon = topn_mutators[j].epsilon();
          const float float_threshold = (new_epsilon - biases[j]) * mults[j];
          const int16_t int16_threshold = GetInt16Threshold(float_threshold);
          simd_thresholds[j] = int16_threshold;

          push_mask &= HWY_NAMESPACE::ComputePushMask(dist16[j].data(),
                                                      simd_thresholds[j]);
        }
      }
    }
  }
}

#define SCANN_GET_TOP_FLOAT_DISTANCES_IMPL(batch_size, prefetch_strategy_int)  \
  SCANN_HIGHWAY_INLINE void                                                    \
      GetTopFloatDistances1_##batch_size##prefetch_strategy_int(               \
          LUT16ArgsTopN<float> args) {                                         \
    return GetTopFloatDistancesImpl<batch_size, static_cast<PrefetchStrategy>( \
                                                    prefetch_strategy_int)>(   \
        false, std::move(args));                                               \
  }

#define SCANN_GET_TOP_FLOAT_DISTANCES(batch_size)   \
  SCANN_GET_TOP_FLOAT_DISTANCES_IMPL(batch_size, 0) \
  SCANN_GET_TOP_FLOAT_DISTANCES_IMPL(batch_size, 1) \
  SCANN_GET_TOP_FLOAT_DISTANCES_IMPL(batch_size, 2)

SCANN_GET_TOP_FLOAT_DISTANCES(1);
SCANN_GET_TOP_FLOAT_DISTANCES(2);
SCANN_GET_TOP_FLOAT_DISTANCES(3);
SCANN_GET_TOP_FLOAT_DISTANCES(4);
SCANN_GET_TOP_FLOAT_DISTANCES(5);
SCANN_GET_TOP_FLOAT_DISTANCES(6);
SCANN_GET_TOP_FLOAT_DISTANCES(7);
SCANN_GET_TOP_FLOAT_DISTANCES(8);
SCANN_GET_TOP_FLOAT_DISTANCES(9);
#undef SCANN_GET_TOP_FLOAT_DISTANCES
#undef SCANN_GET_TOP_FLOAT_DISTANCES_IMPL

#define SCANN_GET_TOP_FLOAT_DISTANCES_SPILLED_IMPL(batch_size,                 \
                                                   prefetch_strategy_int)      \
  SCANN_HIGHWAY_INLINE void                                                    \
      GetTopFloatDistancesSpilled1_##batch_size##prefetch_strategy_int(        \
          LUT16ArgsTopN<float> args) {                                         \
    return GetTopFloatDistancesImpl<batch_size, static_cast<PrefetchStrategy>( \
                                                    prefetch_strategy_int)>(   \
        true, std::move(args));                                                \
  }

#define SCANN_GET_TOP_FLOAT_DISTANCES_SPILLED(batch_size)   \
  SCANN_GET_TOP_FLOAT_DISTANCES_SPILLED_IMPL(batch_size, 0) \
  SCANN_GET_TOP_FLOAT_DISTANCES_SPILLED_IMPL(batch_size, 1) \
  SCANN_GET_TOP_FLOAT_DISTANCES_SPILLED_IMPL(batch_size, 2)

SCANN_GET_TOP_FLOAT_DISTANCES_SPILLED(1);
SCANN_GET_TOP_FLOAT_DISTANCES_SPILLED(2);
SCANN_GET_TOP_FLOAT_DISTANCES_SPILLED(3);
SCANN_GET_TOP_FLOAT_DISTANCES_SPILLED(4);
SCANN_GET_TOP_FLOAT_DISTANCES_SPILLED(5);
SCANN_GET_TOP_FLOAT_DISTANCES_SPILLED(6);
SCANN_GET_TOP_FLOAT_DISTANCES_SPILLED(7);
SCANN_GET_TOP_FLOAT_DISTANCES_SPILLED(8);
SCANN_GET_TOP_FLOAT_DISTANCES_SPILLED(9);
#undef SCANN_GET_TOP_FLOAT_DISTANCES_SPILLED
#undef SCANN_GET_TOP_FLOAT_DISTANCES_SPILLED_IMPL

}  // namespace HWY_NAMESPACE
}  // namespace asymmetric_hashing_internal
}  // namespace research_scann
HWY_AFTER_NAMESPACE();

#if HWY_ONCE
namespace research_scann {
namespace asymmetric_hashing_internal {

#define SCANN_EXPORT_ALL_PREFETCH_STRATEGIES(base_fn, batch_size) \
  HWY_EXPORT(base_fn##batch_size##0);                             \
  HWY_EXPORT(base_fn##batch_size##1);                             \
  HWY_EXPORT(base_fn##batch_size##2);

#define SCANN_HWY_EXPORT_ALL(batch_size)                                    \
  SCANN_EXPORT_ALL_PREFETCH_STRATEGIES(GetInt16Distances, batch_size);      \
  SCANN_EXPORT_ALL_PREFETCH_STRATEGIES(GetInt32Distances, batch_size);      \
  SCANN_EXPORT_ALL_PREFETCH_STRATEGIES(GetFloatDistances, batch_size);      \
  SCANN_EXPORT_ALL_PREFETCH_STRATEGIES(GetTopInt16Distances1_, batch_size); \
  SCANN_EXPORT_ALL_PREFETCH_STRATEGIES(GetTopFloatDistances1_, batch_size);

SCANN_HWY_EXPORT_ALL(1);
SCANN_HWY_EXPORT_ALL(2);
SCANN_HWY_EXPORT_ALL(3);
SCANN_HWY_EXPORT_ALL(4);
SCANN_HWY_EXPORT_ALL(5);
SCANN_HWY_EXPORT_ALL(6);
SCANN_HWY_EXPORT_ALL(7);
SCANN_HWY_EXPORT_ALL(8);
SCANN_HWY_EXPORT_ALL(9);
#undef SCANN_HWY_EXPORT_ALL
#undef SCANN_EXPORT_ALL_PREFETCH_STRATEGIES

#define SCANN_HWY_DISPATCH_PREFETCH(batch_size, prefetch_strategy, func_name, \
                                    ...)                                      \
  if constexpr (prefetch_strategy == PrefetchStrategy::kOff) {                \
    return HWY_DYNAMIC_DISPATCH(func_name##batch_size##0)(__VA_ARGS__);       \
  } else if constexpr (prefetch_strategy == PrefetchStrategy::kSeq) {         \
    return HWY_DYNAMIC_DISPATCH(func_name##batch_size##1)(__VA_ARGS__);       \
  } else {                                                                    \
    return HWY_DYNAMIC_DISPATCH(func_name##batch_size##2)(__VA_ARGS__);       \
  }

#define SCANN_HWY_DYNAMIC_DISPATCH(batch_size, prefetch_strategy, func_name, \
                                   ...)                                      \
  switch (batch_size) {                                                      \
    case 1:                                                                  \
      SCANN_HWY_DISPATCH_PREFETCH(1, prefetch_strategy, func_name,           \
                                  __VA_ARGS__);                              \
    case 2:                                                                  \
      SCANN_HWY_DISPATCH_PREFETCH(2, prefetch_strategy, func_name,           \
                                  __VA_ARGS__);                              \
    case 3:                                                                  \
      SCANN_HWY_DISPATCH_PREFETCH(3, prefetch_strategy, func_name,           \
                                  __VA_ARGS__);                              \
    case 4:                                                                  \
      SCANN_HWY_DISPATCH_PREFETCH(4, prefetch_strategy, func_name,           \
                                  __VA_ARGS__);                              \
    case 5:                                                                  \
      SCANN_HWY_DISPATCH_PREFETCH(5, prefetch_strategy, func_name,           \
                                  __VA_ARGS__);                              \
    case 6:                                                                  \
      SCANN_HWY_DISPATCH_PREFETCH(6, prefetch_strategy, func_name,           \
                                  __VA_ARGS__);                              \
    case 7:                                                                  \
      SCANN_HWY_DISPATCH_PREFETCH(7, prefetch_strategy, func_name,           \
                                  __VA_ARGS__);                              \
    case 8:                                                                  \
      SCANN_HWY_DISPATCH_PREFETCH(8, prefetch_strategy, func_name,           \
                                  __VA_ARGS__);                              \
    case 9:                                                                  \
      SCANN_HWY_DISPATCH_PREFETCH(9, prefetch_strategy, func_name,           \
                                  __VA_ARGS__);                              \
    default:                                                                 \
      DLOG(FATAL) << "Invalid Batch Size";                                   \
  }

template <size_t kNumQueries, PrefetchStrategy kPrefetch>
SCANN_HIGHWAY_OUTLINE void
LUT16Highway<kNumQueries, kPrefetch>::GetInt16Distances(
    LUT16Args<int16_t> args) {
  SCANN_HWY_DYNAMIC_DISPATCH(kNumQueries, kPrefetch, GetInt16Distances, args);
}

template <size_t kNumQueries, PrefetchStrategy kPrefetch>
SCANN_HIGHWAY_OUTLINE void
LUT16Highway<kNumQueries, kPrefetch>::GetInt32Distances(
    LUT16Args<int32_t> args) {
  SCANN_HWY_DYNAMIC_DISPATCH(kNumQueries, kPrefetch, GetInt32Distances, args);
}

template <size_t kNumQueries, PrefetchStrategy kPrefetch>
SCANN_HIGHWAY_OUTLINE void
LUT16Highway<kNumQueries, kPrefetch>::GetFloatDistances(
    LUT16Args<float> args, ConstSpan<float> inv_fp_multipliers) {
  SCANN_HWY_DYNAMIC_DISPATCH(kNumQueries, kPrefetch, GetFloatDistances, args,
                             inv_fp_multipliers);
}

template <size_t kNumQueries, PrefetchStrategy kPrefetch>
SCANN_HIGHWAY_OUTLINE void
LUT16Highway<kNumQueries, kPrefetch>::GetTopInt16Distances(
    LUT16ArgsTopN<int16_t> args) {
  SCANN_HWY_DYNAMIC_DISPATCH(kNumQueries, kPrefetch, GetTopInt16Distances1_,
                             args);
}

template <size_t kNumQueries, PrefetchStrategy kPrefetch>
SCANN_HIGHWAY_OUTLINE void
LUT16Highway<kNumQueries, kPrefetch>::GetTopFloatDistances(
    LUT16ArgsTopN<float> args) {
  SCANN_HWY_DYNAMIC_DISPATCH(kNumQueries, kPrefetch, GetTopFloatDistances1_,
                             args);
}

#undef SCANN_HWY_DYNAMIC_DISPATCH

}  // namespace asymmetric_hashing_internal
}  // namespace research_scann
#endif
#endif