-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathdistance-neon.c
More file actions
1271 lines (1044 loc) · 44.9 KB
/
distance-neon.c
File metadata and controls
1271 lines (1044 loc) · 44.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// distance-neon.c
// sqlitevector
//
// Created by Marco Bambini on 20/06/25.
//
#include "distance-neon.h"
#include "distance-cpu.h"
#include <stdbool.h>
#include <stdlib.h>
#include <math.h>
#if defined(__ARM_NEON) || defined(__ARM_NEON__)
#if __SIZEOF_POINTER__ == 4
#define _ARM32BIT_ 1
#endif
#include <arm_neon.h>
extern distance_function_t dispatch_distance_table[VECTOR_DISTANCE_MAX][VECTOR_TYPE_MAX];
extern char *distance_backend_name;
// Helper function for 32-bit ARM: vmaxv_u16 is not available in ARMv7 NEON
#ifdef _ARM32BIT_
static inline uint16_t vmaxv_u16_compat(uint16x4_t v) {
// Use pairwise max to reduce vector
uint16x4_t m = vpmax_u16(v, v); // [max(v0,v1), max(v2,v3), max(v0,v1), max(v2,v3)]
m = vpmax_u16(m, m); // [max(all), max(all), max(all), max(all)]
return vget_lane_u16(m, 0);
}
#define vmaxv_u16 vmaxv_u16_compat
#endif
// MARK: FLOAT32 -
float float32_distance_l2_impl_neon (const void *v1, const void *v2, int n, bool use_sqrt) {
const float *a = (const float *)v1;
const float *b = (const float *)v2;
float32x4_t acc = vdupq_n_f32(0.0f);
int i = 0;
for (; i <= n - 4; i += 4) {
float32x4_t va = vld1q_f32(a + i);
float32x4_t vb = vld1q_f32(b + i);
float32x4_t d = vsubq_f32(va, vb);
acc = vmlaq_f32(acc, d, d); // acc += d * d
}
float sum;
#if defined(__aarch64__)
sum = vaddvq_f32(acc); // fast horizontal add on arm64
#else
float tmp[4]; vst1q_f32(tmp, acc);
sum = tmp[0] + tmp[1] + tmp[2] + tmp[3];
#endif
for (; i < n; ++i) {
float d = a[i] - b[i];
sum += d * d;
}
return use_sqrt ? sqrtf(sum) : sum;
}
float float32_distance_l2_neon (const void *v1, const void *v2, int n) {
return float32_distance_l2_impl_neon(v1, v2, n, true);
}
float float32_distance_l2_squared_neon (const void *v1, const void *v2, int n) {
return float32_distance_l2_impl_neon(v1, v2, n, false);
}
float float32_distance_cosine_neon (const void *v1, const void *v2, int n) {
const float *a = (const float *)v1;
const float *b = (const float *)v2;
float32x4_t acc_dot = vdupq_n_f32(0.0f);
float32x4_t acc_a2 = vdupq_n_f32(0.0f);
float32x4_t acc_b2 = vdupq_n_f32(0.0f);
int i = 0;
for (; i <= n - 4; i += 4) {
float32x4_t va = vld1q_f32(a + i);
float32x4_t vb = vld1q_f32(b + i);
acc_dot = vmlaq_f32(acc_dot, va, vb); // dot += a * b
acc_a2 = vmlaq_f32(acc_a2, va, va); // norm_a += a * a
acc_b2 = vmlaq_f32(acc_b2, vb, vb); // norm_b += b * b
}
float d[4], a2[4], b2[4];
vst1q_f32(d, acc_dot);
vst1q_f32(a2, acc_a2);
vst1q_f32(b2, acc_b2);
float dot = d[0] + d[1] + d[2] + d[3];
float norm_a = a2[0] + a2[1] + a2[2] + a2[3];
float norm_b = b2[0] + b2[1] + b2[2] + b2[3];
for (; i < n; ++i) {
float ai = a[i];
float bi = b[i];
dot += ai * bi;
norm_a += ai * ai;
norm_b += bi * bi;
}
if (norm_a == 0.0f || norm_b == 0.0f) return 1.0f;
return 1.0f - (dot / (sqrtf(norm_a) * sqrtf(norm_b)));
}
float float32_distance_dot_neon (const void *v1, const void *v2, int n) {
const float *a = (const float *)v1;
const float *b = (const float *)v2;
float32x4_t acc = vdupq_n_f32(0.0f);
int i = 0;
for (; i <= n - 4; i += 4) {
float32x4_t va = vld1q_f32(a + i);
float32x4_t vb = vld1q_f32(b + i);
acc = vmlaq_f32(acc, va, vb); // acc += a * b
}
float tmp[4];
vst1q_f32(tmp, acc);
float dot = tmp[0] + tmp[1] + tmp[2] + tmp[3];
for (; i < n; ++i) {
dot += a[i] * b[i];
}
return -dot;
}
float float32_distance_l1_neon (const void *v1, const void *v2, int n) {
const float *a = (const float *)v1;
const float *b = (const float *)v2;
float32x4_t acc = vdupq_n_f32(0.0f);
int i = 0;
for (; i <= n - 4; i += 4) {
float32x4_t va = vld1q_f32(a + i);
float32x4_t vb = vld1q_f32(b + i);
float32x4_t d = vabdq_f32(va, vb); // |a - b|
acc = vaddq_f32(acc, d);
}
float tmp[4];
vst1q_f32(tmp, acc);
float sum = tmp[0] + tmp[1] + tmp[2] + tmp[3];
for (; i < n; ++i) {
sum += fabsf(a[i] - b[i]);
}
return sum;
}
// MARK: - BFLOAT16 -
static inline float32x4_t bf16x4_to_f32x4_u16 (uint16x4_t h) {
// widen u16 -> u32 and shift left 16: exact bf16->f32 bit pattern
uint32x4_t u32 = vshll_n_u16(h, 16);
return vreinterpretq_f32_u32(u32);
}
float bfloat16_distance_l2_impl_neon (const void *v1, const void *v2, int n, bool use_sqrt) {
const uint16_t *a = (const uint16_t *)v1;
const uint16_t *b = (const uint16_t *)v2;
#ifdef _ARM32BIT_
// 32-bit ARM: use scalar double accumulation (no float64x2_t in NEON)
double sum = 0.0;
int i = 0;
for (; i <= n - 4; i += 4) {
uint16x4_t av16 = vld1_u16(a + i);
uint16x4_t bv16 = vld1_u16(b + i);
float32x4_t va = bf16x4_to_f32x4_u16(av16);
float32x4_t vb = bf16x4_to_f32x4_u16(bv16);
float32x4_t d = vsubq_f32(va, vb);
// mask-out NaNs: m = (d==d)
uint32x4_t m = vceqq_f32(d, d);
d = vbslq_f32(m, d, vdupq_n_f32(0.0f));
// Store and accumulate in scalar double
float tmp[4];
vst1q_f32(tmp, d);
for (int j = 0; j < 4; j++) {
double dj = (double)tmp[j];
sum = fma(dj, dj, sum);
}
}
#else
// Accumulate in f64 to avoid overflow from huge bf16 values.
float64x2_t acc0 = vdupq_n_f64(0.0), acc1 = vdupq_n_f64(0.0);
int i = 0;
for (; i <= n - 8; i += 8) {
uint16x8_t av16 = vld1q_u16(a + i);
uint16x8_t bv16 = vld1q_u16(b + i);
// low 4
float32x4_t va0 = bf16x4_to_f32x4_u16(vget_low_u16(av16));
float32x4_t vb0 = bf16x4_to_f32x4_u16(vget_low_u16(bv16));
float32x4_t d0 = vsubq_f32(va0, vb0);
// mask-out NaNs: m = (d==d)
uint32x4_t m0 = vceqq_f32(d0, d0);
d0 = vbslq_f32(m0, d0, vdupq_n_f32(0.0f));
float64x2_t d0lo = vcvt_f64_f32(vget_low_f32(d0));
float64x2_t d0hi = vcvt_f64_f32(vget_high_f32(d0));
acc0 = vfmaq_f64(acc0, d0lo, d0lo);
acc1 = vfmaq_f64(acc1, d0hi, d0hi);
// high 4
float32x4_t va1 = bf16x4_to_f32x4_u16(vget_high_u16(av16));
float32x4_t vb1 = bf16x4_to_f32x4_u16(vget_high_u16(bv16));
float32x4_t d1 = vsubq_f32(va1, vb1);
uint32x4_t m1 = vceqq_f32(d1, d1);
d1 = vbslq_f32(m1, d1, vdupq_n_f32(0.0f));
float64x2_t d1lo = vcvt_f64_f32(vget_low_f32(d1));
float64x2_t d1hi = vcvt_f64_f32(vget_high_f32(d1));
acc0 = vfmaq_f64(acc0, d1lo, d1lo);
acc1 = vfmaq_f64(acc1, d1hi, d1hi);
}
if (i <= n - 4) {
uint16x4_t av16 = vld1_u16(a + i);
uint16x4_t bv16 = vld1_u16(b + i);
float32x4_t d = vsubq_f32(bf16x4_to_f32x4_u16(av16),
bf16x4_to_f32x4_u16(bv16));
uint32x4_t m = vceqq_f32(d, d);
d = vbslq_f32(m, d, vdupq_n_f32(0.0f));
float64x2_t dlo = vcvt_f64_f32(vget_low_f32(d));
float64x2_t dhi = vcvt_f64_f32(vget_high_f32(d));
acc0 = vfmaq_f64(acc0, dlo, dlo);
acc1 = vfmaq_f64(acc1, dhi, dhi);
i += 4;
}
double sum = vaddvq_f64(vaddq_f64(acc0, acc1));
#endif
// scalar tail; treat NaN as 0, Inf as +Inf result
for (; i < n; ++i) {
float d = bfloat16_to_float32(a[i]) - bfloat16_to_float32(b[i]);
if (isinf(d)) return INFINITY;
if (!isnan(d)) sum = fma((double)d, (double)d, sum);
}
return use_sqrt ? (float)sqrt(sum) : (float)sum;
}
float bfloat16_distance_l2_neon (const void *v1, const void *v2, int n) {
return bfloat16_distance_l2_impl_neon(v1, v2, n, true);
}
float bfloat16_distance_l2_squared_neon (const void *v1, const void *v2, int n) {
return bfloat16_distance_l2_impl_neon(v1, v2, n, false);
}
float bfloat16_distance_cosine_neon (const void *v1, const void *v2, int n) {
const uint16_t *restrict a = (const uint16_t *restrict)v1;
const uint16_t *restrict b = (const uint16_t *restrict)v2;
float32x4_t acc_dot = vdupq_n_f32(0.0f);
float32x4_t acc_a2 = vdupq_n_f32(0.0f);
float32x4_t acc_b2 = vdupq_n_f32(0.0f);
int i = 0;
// process 8 elements per iteration
for (; i <= n - 8; i += 8) {
uint16x8_t av16 = vld1q_u16(a + i);
uint16x8_t bv16 = vld1q_u16(b + i);
// low 4
float32x4_t va0 = bf16x4_to_f32x4_u16(vget_low_u16(av16));
float32x4_t vb0 = bf16x4_to_f32x4_u16(vget_low_u16(bv16));
acc_dot = vmlaq_f32(acc_dot, va0, vb0);
acc_a2 = vmlaq_f32(acc_a2, va0, va0);
acc_b2 = vmlaq_f32(acc_b2, vb0, vb0);
// high 4
float32x4_t va1 = bf16x4_to_f32x4_u16(vget_high_u16(av16));
float32x4_t vb1 = bf16x4_to_f32x4_u16(vget_high_u16(bv16));
acc_dot = vmlaq_f32(acc_dot, va1, vb1);
acc_a2 = vmlaq_f32(acc_a2, va1, va1);
acc_b2 = vmlaq_f32(acc_b2, vb1, vb1);
}
// optional mid-tail of 4
if (i <= n - 4) {
uint16x4_t av16 = vld1_u16(a + i);
uint16x4_t bv16 = vld1_u16(b + i);
float32x4_t va = bf16x4_to_f32x4_u16(av16);
float32x4_t vb = bf16x4_to_f32x4_u16(bv16);
acc_dot = vmlaq_f32(acc_dot, va, vb);
acc_a2 = vmlaq_f32(acc_a2, va, va);
acc_b2 = vmlaq_f32(acc_b2, vb, vb);
i += 4;
}
// horizontal reduction
float dot, norm_a, norm_b;
#if defined(__aarch64__)
dot = vaddvq_f32(acc_dot);
norm_a = vaddvq_f32(acc_a2);
norm_b = vaddvq_f32(acc_b2);
#else
float d[4], a2[4], b2[4];
vst1q_f32(d, acc_dot);
vst1q_f32(a2, acc_a2);
vst1q_f32(b2, acc_b2);
dot = d[0] + d[1] + d[2] + d[3];
norm_a = a2[0] + a2[1] + a2[2] + a2[3];
norm_b = b2[0] + b2[1] + b2[2] + b2[3];
#endif
// scalar tail
for (; i < n; ++i) {
float fa = bfloat16_to_float32(a[i]);
float fb = bfloat16_to_float32(b[i]);
dot += fa * fb;
norm_a += fa * fa;
norm_b += fb * fb;
}
if (norm_a == 0.0f || norm_b == 0.0f) return 1.0f;
return 1.0f - (dot / (sqrtf(norm_a) * sqrtf(norm_b)));
}
float bfloat16_distance_dot_neon (const void *v1, const void *v2, int n) {
const uint16_t *restrict a = (const uint16_t *restrict)v1;
const uint16_t *restrict b = (const uint16_t *restrict)v2;
float32x4_t acc = vdupq_n_f32(0.0f);
int i = 0;
// process 8 elements per iteration
for (; i <= n - 8; i += 8) {
uint16x8_t av16 = vld1q_u16(a + i);
uint16x8_t bv16 = vld1q_u16(b + i);
// low 4
float32x4_t va0 = bf16x4_to_f32x4_u16(vget_low_u16(av16));
float32x4_t vb0 = bf16x4_to_f32x4_u16(vget_low_u16(bv16));
acc = vmlaq_f32(acc, va0, vb0);
// high 4
float32x4_t va1 = bf16x4_to_f32x4_u16(vget_high_u16(av16));
float32x4_t vb1 = bf16x4_to_f32x4_u16(vget_high_u16(bv16));
acc = vmlaq_f32(acc, va1, vb1);
}
// optional mid-tail of 4
if (i <= n - 4) {
uint16x4_t av16 = vld1_u16(a + i);
uint16x4_t bv16 = vld1_u16(b + i);
float32x4_t va = bf16x4_to_f32x4_u16(av16);
float32x4_t vb = bf16x4_to_f32x4_u16(bv16);
acc = vmlaq_f32(acc, va, vb);
i += 4;
}
// horizontal sum
float dot;
#if defined(__aarch64__)
dot = vaddvq_f32(acc);
#else
float tmp[4]; vst1q_f32(tmp, acc);
dot = tmp[0] + tmp[1] + tmp[2] + tmp[3];
#endif
// scalar tail
for (; i < n; ++i) {
dot += bfloat16_to_float32(a[i]) * bfloat16_to_float32(b[i]);
}
return -dot;
}
float bfloat16_distance_l1_neon (const void *v1, const void *v2, int n) {
const uint16_t *a = (const uint16_t *)v1;
const uint16_t *b = (const uint16_t *)v2;
float32x4_t acc = vdupq_n_f32(0.0f);
int i = 0;
for (; i <= n - 4; i += 4) {
uint16x4_t av16 = vld1_u16(a + i);
uint16x4_t bv16 = vld1_u16(b + i);
float32x4_t va = bf16x4_to_f32x4_u16(av16);
float32x4_t vb = bf16x4_to_f32x4_u16(bv16);
float32x4_t d = vabdq_f32(va, vb); // |a - b|
acc = vaddq_f32(acc, d);
}
// horizontal reduction
float sum;
#if defined(__aarch64__)
sum = vaddvq_f32(acc);
#else
float tmp[4]; vst1q_f32(tmp, acc);
sum = tmp[0] + tmp[1] + tmp[2] + tmp[3];
#endif
// scalar tail
for (; i < n; ++i) {
float fa = bfloat16_to_float32(a[i]);
float fb = bfloat16_to_float32(b[i]);
sum += fabsf(fa - fb);
}
return sum;
}
// MARK: - FLOAT16 -
// vector converter: 4×f16 bits (u16) -> f32x4
static inline float32x4_t f16x4_to_f32x4_u16(uint16x4_t h) {
#if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC)
/* Fast path: NEON FP16 -> FP32 */
float16x4_t h16 = vreinterpret_f16_u16(h);
return vcvt_f32_f16(h16);
#else
/* Portable per-lane conversion via your helper */
float tmp[4];
tmp[0] = float16_to_float32(vget_lane_u16(h, 0));
tmp[1] = float16_to_float32(vget_lane_u16(h, 1));
tmp[2] = float16_to_float32(vget_lane_u16(h, 2));
tmp[3] = float16_to_float32(vget_lane_u16(h, 3));
return vld1q_f32(tmp);
#endif
}
float float16_distance_l2_impl_neon (const void *v1, const void *v2, int n, bool use_sqrt) {
const uint16_t *a = (const uint16_t *)v1;
const uint16_t *b = (const uint16_t *)v2;
const uint16x4_t EXP_MASK = vdup_n_u16(0x7C00u);
const uint16x4_t FRAC_MASK = vdup_n_u16(0x03FFu);
const uint16x4_t SIGN_MASK = vdup_n_u16(0x8000u);
const uint16x4_t ZERO16 = vdup_n_u16(0);
#ifdef _ARM32BIT_
// 32-bit ARM: use scalar double accumulation
double sum = 0.0;
int i = 0;
#else
// 64-bit ARM: use float64x2_t NEON intrinsics
float64x2_t acc0 = vdupq_n_f64(0.0), acc1 = vdupq_n_f64(0.0);
int i = 0;
#endif
for (; i <= n - 4; i += 4) {
uint16x4_t av16 = vld1_u16(a + i);
uint16x4_t bv16 = vld1_u16(b + i);
/* detect Inf mismatches: (a Inf XOR b Inf) OR (both Inf and sign differs) */
uint16x4_t a_exp_all1 = vceq_u16(vand_u16(av16, EXP_MASK), EXP_MASK);
uint16x4_t b_exp_all1 = vceq_u16(vand_u16(bv16, EXP_MASK), EXP_MASK);
uint16x4_t a_frac_zero= vceq_u16(vand_u16(av16, FRAC_MASK), ZERO16);
uint16x4_t b_frac_zero= vceq_u16(vand_u16(bv16, FRAC_MASK), ZERO16);
uint16x4_t a_inf = vand_u16(a_exp_all1, a_frac_zero);
uint16x4_t b_inf = vand_u16(b_exp_all1, b_frac_zero);
uint16x4_t a_sign = vand_u16(av16, SIGN_MASK);
uint16x4_t b_sign = vand_u16(bv16, SIGN_MASK);
uint16x4_t same_sign = vceq_u16(veor_u16(a_sign, b_sign), ZERO16);
uint16x4_t sign_diff = vmvn_u16(same_sign);
uint16x4_t mismatch = vorr_u16(
vorr_u16(vand_u16(a_inf, vmvn_u16(b_inf)),
vand_u16(b_inf, vmvn_u16(a_inf))),
vand_u16(vand_u16(a_inf, b_inf), sign_diff));
if (vmaxv_u16(mismatch)) return INFINITY;
/* convert to f32 then to f64, subtract in f64, mask NaNs to zero */
float32x4_t af = f16x4_to_f32x4_u16(av16);
float32x4_t bf = f16x4_to_f32x4_u16(bv16);
float32x4_t d32 = vsubq_f32(af, bf);
uint32x4_t m = vceqq_f32(d32, d32); /* true where not-NaN */
d32 = vbslq_f32(m, d32, vdupq_n_f32(0.0f));
#ifdef _ARM32BIT_
// 32-bit ARM: accumulate in scalar double
float tmp[4];
vst1q_f32(tmp, d32);
for (int j = 0; j < 4; j++) {
double dj = (double)tmp[j];
sum = fma(dj, dj, sum);
}
#else
// 64-bit ARM: use NEON f64 operations
float64x2_t dlo = vcvt_f64_f32(vget_low_f32(d32));
float64x2_t dhi = vcvt_f64_f32(vget_high_f32(d32));
#if defined(__ARM_FEATURE_FMA)
acc0 = vfmaq_f64(acc0, dlo, dlo);
acc1 = vfmaq_f64(acc1, dhi, dhi);
#else
acc0 = vaddq_f64(acc0, vmulq_f64(dlo, dlo));
acc1 = vaddq_f64(acc1, vmulq_f64(dhi, dhi));
#endif
#endif
}
#ifndef _ARM32BIT_
double sum = vaddvq_f64(vaddq_f64(acc0, acc1));
#endif
/* tail (scalar; same Inf/NaN policy) */
for (; i < n; ++i) {
uint16_t ai=a[i], bi=b[i];
if ((f16_is_inf(ai) || f16_is_inf(bi)) && !(f16_is_inf(ai) && f16_is_inf(bi) && f16_sign(ai)==f16_sign(bi))) return INFINITY;
float xa = float16_to_float32(ai);
float xb = float16_to_float32(bi);
float d = xa - xb;
if (!isnan(d)) sum = fma((double)d, (double)d, sum);
}
return use_sqrt ? (float)sqrt(sum) : (float)sum;
}
float float16_distance_l2_neon (const void *v1, const void *v2, int n) {
return float16_distance_l2_impl_neon(v1, v2, n, true);
}
float float16_distance_l2_squared_neon (const void *v1, const void *v2, int n) {
return float16_distance_l2_impl_neon(v1, v2, n, false);
}
/* =========================================================================
Cosine distance (1 - dot/(||a||*||b||)) -- float16 (uint16_t)
========================================================================= */
float float16_distance_cosine_neon (const void *v1, const void *v2, int n) {
const uint16_t *a = (const uint16_t *)v1;
const uint16_t *b = (const uint16_t *)v2;
const uint16x4_t EXP_MASK = vdup_n_u16(0x7C00u);
const uint16x4_t FRAC_MASK = vdup_n_u16(0x03FFu);
const uint16x4_t ZERO16 = vdup_n_u16(0);
#ifdef _ARM32BIT_
// 32-bit ARM: use scalar double accumulation
double dot = 0.0, normx = 0.0, normy = 0.0;
int i = 0;
#else
// 64-bit ARM: use float64x2_t NEON intrinsics
float64x2_t acc_dot_lo = vdupq_n_f64(0.0), acc_dot_hi = vdupq_n_f64(0.0);
float64x2_t acc_a2_lo = vdupq_n_f64(0.0), acc_a2_hi = vdupq_n_f64(0.0);
float64x2_t acc_b2_lo = vdupq_n_f64(0.0), acc_b2_hi = vdupq_n_f64(0.0);
int i = 0;
#endif
for (; i <= n - 4; i += 4) {
uint16x4_t av16 = vld1_u16(a + i);
uint16x4_t bv16 = vld1_u16(b + i);
/* if any lane has ±Inf, return 1.0 (max distance) */
uint16x4_t a_inf = vand_u16(vceq_u16(vand_u16(av16, EXP_MASK), EXP_MASK),
vceq_u16(vand_u16(av16, FRAC_MASK), ZERO16));
uint16x4_t b_inf = vand_u16(vceq_u16(vand_u16(bv16, EXP_MASK), EXP_MASK),
vceq_u16(vand_u16(bv16, FRAC_MASK), ZERO16));
if (vmaxv_u16(vorr_u16(a_inf, b_inf))) return 1.0f;
float32x4_t ax = f16x4_to_f32x4_u16(av16);
float32x4_t by = f16x4_to_f32x4_u16(bv16);
/* zero out NaNs */
uint32x4_t mx = vceqq_f32(ax, ax);
uint32x4_t my = vceqq_f32(by, by);
ax = vbslq_f32(mx, ax, vdupq_n_f32(0.0f));
by = vbslq_f32(my, by, vdupq_n_f32(0.0f));
#ifdef _ARM32BIT_
// 32-bit ARM: accumulate in scalar double
float ax_tmp[4], by_tmp[4];
vst1q_f32(ax_tmp, ax);
vst1q_f32(by_tmp, by);
for (int j = 0; j < 4; j++) {
double x = (double)ax_tmp[j];
double y = (double)by_tmp[j];
dot += x * y;
normx += x * x;
normy += y * y;
}
#else
/* widen to f64 and accumulate */
float64x2_t ax_lo = vcvt_f64_f32(vget_low_f32(ax)), ax_hi = vcvt_f64_f32(vget_high_f32(ax));
float64x2_t by_lo = vcvt_f64_f32(vget_low_f32(by)), by_hi = vcvt_f64_f32(vget_high_f32(by));
#if defined(__ARM_FEATURE_FMA)
acc_dot_lo = vfmaq_f64(acc_dot_lo, ax_lo, by_lo);
acc_dot_hi = vfmaq_f64(acc_dot_hi, ax_hi, by_hi);
acc_a2_lo = vfmaq_f64(acc_a2_lo, ax_lo, ax_lo);
acc_a2_hi = vfmaq_f64(acc_a2_hi, ax_hi, ax_hi);
acc_b2_lo = vfmaq_f64(acc_b2_lo, by_lo, by_lo);
acc_b2_hi = vfmaq_f64(acc_b2_hi, by_hi, by_hi);
#else
acc_dot_lo = vaddq_f64(acc_dot_lo, vmulq_f64(ax_lo, by_lo));
acc_dot_hi = vaddq_f64(acc_dot_hi, vmulq_f64(ax_hi, by_hi));
acc_a2_lo = vaddq_f64(acc_a2_lo, vmulq_f64(ax_lo, ax_lo));
acc_a2_hi = vaddq_f64(acc_a2_hi, vmulq_f64(ax_hi, ax_hi));
acc_b2_lo = vaddq_f64(acc_b2_lo, vmulq_f64(by_lo, by_lo));
acc_b2_hi = vaddq_f64(acc_b2_hi, vmulq_f64(by_hi, by_hi));
#endif
#endif
}
#ifndef _ARM32BIT_
double dot = vaddvq_f64(vaddq_f64(acc_dot_lo, acc_dot_hi));
double normx= vaddvq_f64(vaddq_f64(acc_a2_lo, acc_a2_hi));
double normy= vaddvq_f64(vaddq_f64(acc_b2_lo, acc_b2_hi));
#endif
/* tail (scalar) */
for (; i < n; ++i) {
uint16_t ai=a[i], bi=b[i];
if (f16_is_nan(ai) || f16_is_nan(bi)) continue;
if (f16_is_inf(ai) || f16_is_inf(bi)) return 1.0f;
double x = (double)float16_to_float32(ai);
double y = (double)float16_to_float32(bi);
dot += x * y;
normx+= x * x;
normy+= y * y;
}
double denom = sqrt(normx) * sqrt(normy);
if (!(denom > 0.0) || !isfinite(denom) || !isfinite(dot)) return 1.0f;
double c = dot / denom;
if (c > 1.0) c = 1.0;
if (c < -1.0) c = -1.0;
return (float)(1.0 - c);
}
/* =========================================================================
Dot (returns -dot) -- float16 (uint16_t)
========================================================================= */
float float16_distance_dot_neon (const void *v1, const void *v2, int n) {
const uint16_t *a = (const uint16_t *)v1;
const uint16_t *b = (const uint16_t *)v2;
const uint16x4_t EXP_MASK = vdup_n_u16(0x7C00u);
const uint16x4_t FRAC_MASK = vdup_n_u16(0x03FFu);
const uint16x4_t ZERO16 = vdup_n_u16(0);
#ifdef _ARM32BIT_
// 32-bit ARM: use scalar double accumulation
double dot = 0.0;
int i = 0;
#else
// 64-bit ARM: use float64x2_t NEON intrinsics
float64x2_t acc_lo = vdupq_n_f64(0.0), acc_hi = vdupq_n_f64(0.0);
int i = 0;
#endif
for (; i <= n - 4; i += 4) {
uint16x4_t av16 = vld1_u16(a + i);
uint16x4_t bv16 = vld1_u16(b + i);
/* if any lane is ±Inf, do scalar fallback for this block to get sign-correct ±Inf */
uint16x4_t a_inf = vand_u16(vceq_u16(vand_u16(av16, EXP_MASK), EXP_MASK),
vceq_u16(vand_u16(av16, FRAC_MASK), ZERO16));
uint16x4_t b_inf = vand_u16(vceq_u16(vand_u16(bv16, EXP_MASK), EXP_MASK),
vceq_u16(vand_u16(bv16, FRAC_MASK), ZERO16));
if (vmaxv_u16(vorr_u16(a_inf, b_inf))) {
for (int k=0;k<4;++k){
float x = float16_to_float32(a[i+k]);
float y = float16_to_float32(b[i+k]);
if (isnan(x) || isnan(y)) continue;
double p = (double)x * (double)y;
if (isinf(p)) return (p>0)? -INFINITY : INFINITY;
#ifdef _ARM32BIT_
dot += p;
#else
acc_lo = vsetq_lane_f64(vgetq_lane_f64(acc_lo,0)+p, acc_lo, 0); /* cheap add */
#endif
}
continue;
}
float32x4_t ax = f16x4_to_f32x4_u16(av16);
float32x4_t by = f16x4_to_f32x4_u16(bv16);
/* zero out NaNs */
uint32x4_t mx = vceqq_f32(ax, ax);
uint32x4_t my = vceqq_f32(by, by);
ax = vbslq_f32(mx, ax, vdupq_n_f32(0.0f));
by = vbslq_f32(my, by, vdupq_n_f32(0.0f));
float32x4_t prod = vmulq_f32(ax, by);
#ifdef _ARM32BIT_
// 32-bit ARM: accumulate in scalar double
float prod_tmp[4];
vst1q_f32(prod_tmp, prod);
for (int j = 0; j < 4; j++) {
dot += (double)prod_tmp[j];
}
#else
// 64-bit ARM: use NEON f64 operations
float64x2_t lo = vcvt_f64_f32(vget_low_f32(prod));
float64x2_t hi = vcvt_f64_f32(vget_high_f32(prod));
acc_lo = vaddq_f64(acc_lo, lo);
acc_hi = vaddq_f64(acc_hi, hi);
#endif
}
#ifndef _ARM32BIT_
double dot = vaddvq_f64(vaddq_f64(acc_lo, acc_hi));
#endif
for (; i < n; ++i) {
float x = float16_to_float32(a[i]);
float y = float16_to_float32(b[i]);
if (isnan(x) || isnan(y)) continue;
double p = (double)x * (double)y;
if (isinf(p)) return (p>0)? -INFINITY : INFINITY;
dot += p;
}
return (float)(-dot);
}
/* =========================================================================
L1 (sum |a-b|) -- float16 (uint16_t)
========================================================================= */
float float16_distance_l1_neon (const void *v1, const void *v2, int n) {
const uint16_t *a = (const uint16_t *)v1;
const uint16_t *b = (const uint16_t *)v2;
const uint16x4_t EXP_MASK = vdup_n_u16(0x7C00u);
const uint16x4_t FRAC_MASK = vdup_n_u16(0x03FFu);
const uint16x4_t SIGN_MASK = vdup_n_u16(0x8000u);
const uint16x4_t ZERO16 = vdup_n_u16(0);
#ifdef _ARM32BIT_
// 32-bit ARM: use scalar double accumulation
double sum = 0.0;
int i = 0;
#else
// 64-bit ARM: use float64x2_t NEON intrinsics
float64x2_t acc = vdupq_n_f64(0.0);
int i = 0;
#endif
for (; i <= n - 4; i += 4) {
uint16x4_t av16 = vld1_u16(a + i);
uint16x4_t bv16 = vld1_u16(b + i);
/* Inf mismatch => +Inf */
uint16x4_t a_exp_all1 = vceq_u16(vand_u16(av16, EXP_MASK), EXP_MASK);
uint16x4_t b_exp_all1 = vceq_u16(vand_u16(bv16, EXP_MASK), EXP_MASK);
uint16x4_t a_frac_zero= vceq_u16(vand_u16(av16, FRAC_MASK), ZERO16);
uint16x4_t b_frac_zero= vceq_u16(vand_u16(bv16, FRAC_MASK), ZERO16);
uint16x4_t a_inf = vand_u16(a_exp_all1, a_frac_zero);
uint16x4_t b_inf = vand_u16(b_exp_all1, b_frac_zero);
uint16x4_t a_sign = vand_u16(av16, SIGN_MASK);
uint16x4_t b_sign = vand_u16(bv16, SIGN_MASK);
uint16x4_t same_sign = vceq_u16(veor_u16(a_sign, b_sign), ZERO16);
uint16x4_t sign_diff = vmvn_u16(same_sign);
uint16x4_t mismatch = vorr_u16(
vorr_u16(vand_u16(a_inf, vmvn_u16(b_inf)),
vand_u16(b_inf, vmvn_u16(a_inf))),
vand_u16(vand_u16(a_inf, b_inf), sign_diff));
if (vmaxv_u16(mismatch)) return INFINITY;
float32x4_t af = f16x4_to_f32x4_u16(av16);
float32x4_t bf = f16x4_to_f32x4_u16(bv16);
float32x4_t d = vabdq_f32(af, bf); /* |a-b| */
uint32x4_t m = vceqq_f32(d, d); /* mask NaNs -> 0 */
d = vbslq_f32(m, d, vdupq_n_f32(0.0f));
#ifdef _ARM32BIT_
// 32-bit ARM: accumulate in scalar double
float tmp[4];
vst1q_f32(tmp, d);
for (int j = 0; j < 4; j++) {
sum += (double)tmp[j];
}
#else
// 64-bit ARM: use NEON f64 operations
float64x2_t lo = vcvt_f64_f32(vget_low_f32(d));
float64x2_t hi = vcvt_f64_f32(vget_high_f32(d));
acc = vaddq_f64(acc, lo);
acc = vaddq_f64(acc, hi);
#endif
}
#ifndef _ARM32BIT_
double sum = vaddvq_f64(acc);
#endif
for (; i < n; ++i) {
uint16_t ai=a[i], bi=b[i];
if ((f16_is_inf(ai) || f16_is_inf(bi)) && !(f16_is_inf(ai) && f16_is_inf(bi) && f16_sign(ai)==f16_sign(bi))) return INFINITY;
float da = float16_to_float32(ai);
float db = float16_to_float32(bi);
float d = fabsf(da - db);
if (!isnan(d)) sum += d;
}
return (float)sum;
}
// MARK: - UINT8 -
static inline float uint8_distance_l2_impl_neon(const void *v1, const void *v2, int n, bool use_sqrt) {
const uint8_t *a = (const uint8_t *)v1;
const uint8_t *b = (const uint8_t *)v2;
uint32x4_t acc = vmovq_n_u32(0);
int i = 0;
for (; i <= n - 16; i += 16) {
uint8x16_t va = vld1q_u8(a + i);
uint8x16_t vb = vld1q_u8(b + i);
// compute 8-bit differences widened to signed 16-bit
int16x8_t diff_lo = (int16x8_t)vsubl_u8(vget_low_u8(va), vget_low_u8(vb));
int16x8_t diff_hi = (int16x8_t)vsubl_u8(vget_high_u8(va), vget_high_u8(vb));
// widen to signed 32-bit and square
int32x4_t diff_lo_0 = vmovl_s16(vget_low_s16(diff_lo));
int32x4_t diff_lo_1 = vmovl_s16(vget_high_s16(diff_lo));
int32x4_t diff_hi_0 = vmovl_s16(vget_low_s16(diff_hi));
int32x4_t diff_hi_1 = vmovl_s16(vget_high_s16(diff_hi));
diff_lo_0 = vmulq_s32(diff_lo_0, diff_lo_0);
diff_lo_1 = vmulq_s32(diff_lo_1, diff_lo_1);
diff_hi_0 = vmulq_s32(diff_hi_0, diff_hi_0);
diff_hi_1 = vmulq_s32(diff_hi_1, diff_hi_1);
// accumulate into uint32_t accumulator
acc = vaddq_u32(acc, vreinterpretq_u32_s32(diff_lo_0));
acc = vaddq_u32(acc, vreinterpretq_u32_s32(diff_lo_1));
acc = vaddq_u32(acc, vreinterpretq_u32_s32(diff_hi_0));
acc = vaddq_u32(acc, vreinterpretq_u32_s32(diff_hi_1));
}
// horizontal sum
uint64x2_t sum64 = vpaddlq_u32(acc);
uint64_t final_sum = vgetq_lane_u64(sum64, 0) + vgetq_lane_u64(sum64, 1);
// tail
for (; i < n; ++i) {
int diff = (int)a[i] - (int)b[i];
final_sum += (uint64_t)(diff * diff);
}
return use_sqrt ? sqrtf((float)final_sum) : (float)final_sum;
}
float uint8_distance_l2_neon (const void *v1, const void *v2, int n) {
return uint8_distance_l2_impl_neon(v1, v2, n, true);
}
float uint8_distance_l2_squared_neon (const void *v1, const void *v2, int n) {
return uint8_distance_l2_impl_neon(v1, v2, n, false);
}
float uint8_distance_cosine_neon (const void *v1, const void *v2, int n) {
const uint8_t *a = (const uint8_t *)v1;
const uint8_t *b = (const uint8_t *)v2;
uint32x4_t dot_acc = vmovq_n_u32(0);
uint32x4_t norm_a_acc = vmovq_n_u32(0);
uint32x4_t norm_b_acc = vmovq_n_u32(0);
int i = 0;
for (; i <= n - 16; i += 16) {
// Load 16 bytes from each vector
uint8x16_t va_u8 = vld1q_u8(a + i);
uint8x16_t vb_u8 = vld1q_u8(b + i);
// Convert to uint16x8_t
uint16x8_t va_lo_u16 = vmovl_u8(vget_low_u8(va_u8));
uint16x8_t va_hi_u16 = vmovl_u8(vget_high_u8(va_u8));
uint16x8_t vb_lo_u16 = vmovl_u8(vget_low_u8(vb_u8));
uint16x8_t vb_hi_u16 = vmovl_u8(vget_high_u8(vb_u8));
// Multiply for dot product
uint32x4_t dot_lo = vmull_u16(vget_low_u16(va_lo_u16), vget_low_u16(vb_lo_u16));
uint32x4_t dot_hi = vmull_u16(vget_high_u16(va_lo_u16), vget_high_u16(vb_lo_u16));
uint32x4_t dot_lo2 = vmull_u16(vget_low_u16(va_hi_u16), vget_low_u16(vb_hi_u16));
uint32x4_t dot_hi2 = vmull_u16(vget_high_u16(va_hi_u16), vget_high_u16(vb_hi_u16));
// Multiply for norms
uint32x4_t a2_lo = vmull_u16(vget_low_u16(va_lo_u16), vget_low_u16(va_lo_u16));
uint32x4_t a2_hi = vmull_u16(vget_high_u16(va_lo_u16), vget_high_u16(va_lo_u16));
uint32x4_t a2_lo2 = vmull_u16(vget_low_u16(va_hi_u16), vget_low_u16(va_hi_u16));
uint32x4_t a2_hi2 = vmull_u16(vget_high_u16(va_hi_u16), vget_high_u16(va_hi_u16));
uint32x4_t b2_lo = vmull_u16(vget_low_u16(vb_lo_u16), vget_low_u16(vb_lo_u16));
uint32x4_t b2_hi = vmull_u16(vget_high_u16(vb_lo_u16), vget_high_u16(vb_lo_u16));
uint32x4_t b2_lo2 = vmull_u16(vget_low_u16(vb_hi_u16), vget_low_u16(vb_hi_u16));
uint32x4_t b2_hi2 = vmull_u16(vget_high_u16(vb_hi_u16), vget_high_u16(vb_hi_u16));
// Accumulate
dot_acc = vaddq_u32(dot_acc, dot_lo);
dot_acc = vaddq_u32(dot_acc, dot_hi);
dot_acc = vaddq_u32(dot_acc, dot_lo2);
dot_acc = vaddq_u32(dot_acc, dot_hi2);
norm_a_acc = vaddq_u32(norm_a_acc, a2_lo);
norm_a_acc = vaddq_u32(norm_a_acc, a2_hi);
norm_a_acc = vaddq_u32(norm_a_acc, a2_lo2);
norm_a_acc = vaddq_u32(norm_a_acc, a2_hi2);
norm_b_acc = vaddq_u32(norm_b_acc, b2_lo);
norm_b_acc = vaddq_u32(norm_b_acc, b2_hi);
norm_b_acc = vaddq_u32(norm_b_acc, b2_lo2);
norm_b_acc = vaddq_u32(norm_b_acc, b2_hi2);
}
// Horizontal sum
uint32_t dot = vgetq_lane_u32(dot_acc, 0) + vgetq_lane_u32(dot_acc, 1) +
vgetq_lane_u32(dot_acc, 2) + vgetq_lane_u32(dot_acc, 3);
uint32_t norm_a = vgetq_lane_u32(norm_a_acc, 0) + vgetq_lane_u32(norm_a_acc, 1) +
vgetq_lane_u32(norm_a_acc, 2) + vgetq_lane_u32(norm_a_acc, 3);
uint32_t norm_b = vgetq_lane_u32(norm_b_acc, 0) + vgetq_lane_u32(norm_b_acc, 1) +
vgetq_lane_u32(norm_b_acc, 2) + vgetq_lane_u32(norm_b_acc, 3);
// Tail loop
for (; i < n; ++i) {
int ai = a[i];
int bi = b[i];
dot += ai * bi;
norm_a += ai * ai;
norm_b += bi * bi;
}
if (norm_a == 0 || norm_b == 0) return 1.0f;
return 1.0f - (dot / (sqrtf((float)norm_a) * sqrtf((float)norm_b)));
}
float uint8_distance_dot_neon (const void *v1, const void *v2, int n) {
const uint8_t *a = (const uint8_t *)v1;
const uint8_t *b = (const uint8_t *)v2;
uint32x4_t dot_acc = vmovq_n_u32(0); // 4-lane accumulator
int i = 0;
for (; i <= n - 16; i += 16) {
uint8x16_t va_u8 = vld1q_u8(a + i);
uint8x16_t vb_u8 = vld1q_u8(b + i);
// Widen to 16-bit
uint16x8_t va_lo = vmovl_u8(vget_low_u8(va_u8));
uint16x8_t vb_lo = vmovl_u8(vget_low_u8(vb_u8));
uint16x8_t va_hi = vmovl_u8(vget_high_u8(va_u8));
uint16x8_t vb_hi = vmovl_u8(vget_high_u8(vb_u8));
// Multiply low and high halves
uint32x4_t dot_lo = vmull_u16(vget_low_u16(va_lo), vget_low_u16(vb_lo));
uint32x4_t dot_hi = vmull_u16(vget_high_u16(va_lo), vget_high_u16(vb_lo));
uint32x4_t dot_lo2 = vmull_u16(vget_low_u16(va_hi), vget_low_u16(vb_hi));
uint32x4_t dot_hi2 = vmull_u16(vget_high_u16(va_hi), vget_high_u16(vb_hi));
// Accumulate
dot_acc = vaddq_u32(dot_acc, dot_lo);
dot_acc = vaddq_u32(dot_acc, dot_hi);
dot_acc = vaddq_u32(dot_acc, dot_lo2);
dot_acc = vaddq_u32(dot_acc, dot_hi2);
}
// Horizontal add of 4 lanes
uint32_t dot = vgetq_lane_u32(dot_acc, 0) +
vgetq_lane_u32(dot_acc, 1) +
vgetq_lane_u32(dot_acc, 2) +
vgetq_lane_u32(dot_acc, 3);
// Tail loop
for (; i < n; ++i) {
dot += a[i] * b[i];
}
return -(float)dot; // negative dot product = dot distance
}
float uint8_distance_l1_neon (const void *v1, const void *v2, int n) {
const uint8_t *a = (const uint8_t *)v1;
const uint8_t *b = (const uint8_t *)v2;
uint16x8_t sum_acc_lo = vdupq_n_u16(0);
uint16x8_t sum_acc_hi = vdupq_n_u16(0);
int i = 0;