hessboost 0.2.2

Fast, deterministic gradient boosting (GBDT) in Rust: conformal intervals, explainable boosting machines, distributional boosting, tree-based diffusion, and XGBoost model interchange
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
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
use super::{
    BINARY_LOG_LOSS_EPSILON, LOG_LOSS_EPSILON, MAX_FAST_EXP_INPUT, MIN_POSITIVE_PREDICTION,
    RowWeights, scalar, sigmoid_scalar,
};
use crate::objective::GradPair;
#[allow(
    clippy::wildcard_imports,
    reason = "intrinsic modules are used wholesale"
)]
use std::arch::aarch64::*;

// Arithmetic-only helpers express the NEON precondition through an unsafe
// intrinsic function pointer. This keeps their unsafe blocks valid whether or
// not the stdarch in use marks these intrinsics safe, without lint overrides.
// The compiler inlines these constant function pointers.

const VECTOR_WIDTH: usize = 4;
// `objective::GRADIENT_BLOCK_ROWS`'s contract: its blocks start on vector blocks.
const _: () = assert!(crate::objective::GRADIENT_BLOCK_ROWS.is_multiple_of(VECTOR_WIDTH));
// Shared vector-loop scaffolding for the gradient and metric-sum kernels below.
// Lane formulas stay inline in each kernel; only the identical accumulate,
// fallback, store, and reduction shells live here, so numerics are untouched.
macro_rules! accumulate_metric_sum {
    ($weights:expr, $index:ident, $value_low:expr, $value_high:expr, $sum_low:ident, $sum_high:ident, $weight_low:ident, $weight_high:ident) => {
        match $weights {
            Some(weights) => {
                let weight = lane_weights(RowWeights::from(weights), $index);
                let current_weight_low = vcvt_f64_f32(vget_low_f32(weight));
                let current_weight_high = vcvt_high_f64_f32(weight);
                $sum_low = vfmaq_f64($sum_low, $value_low, current_weight_low);
                $sum_high = vfmaq_f64($sum_high, $value_high, current_weight_high);
                $weight_low = vaddq_f64($weight_low, current_weight_low);
                $weight_high = vaddq_f64($weight_high, current_weight_high);
            }
            None => {
                $sum_low = vaddq_f64($sum_low, $value_low);
                $sum_high = vaddq_f64($sum_high, $value_high);
            }
        }
    };
}
macro_rules! metric_finite_guard {
    ($pred:expr, $index:ident, $fallback:ident, $scalar:expr) => {
        if !finite_input($pred) {
            let partial = $scalar;
            $fallback.0 += partial.0;
            $fallback.1 += partial.1;
            $index += VECTOR_WIDTH;
            continue;
        }
    };
}
macro_rules! finish_metric_sum {
    ($sum_low:ident, $sum_high:ident, $weight_low:ident, $weight_high:ident, $fallback:expr, $tail:expr, $weights:expr, $len:expr) => {{
        let loss = vaddvq_f64(vaddq_f64($sum_low, $sum_high)) + $fallback.0 + $tail.0;
        let weight_sum = match $weights {
            Some(_) => vaddvq_f64(vaddq_f64($weight_low, $weight_high)) + $fallback.1 + $tail.1,
            None => $len as f64,
        };
        (loss, weight_sum)
    }};
}
macro_rules! gradient_guard {
    ($cond:expr, $index:ident, $fallback:expr) => {
        if $cond {
            $fallback;
            $index += VECTOR_WIDTH;
            continue;
        }
    };
}
/// The weights of the four cells from `index`: one vector load for one
/// weight per cell, else each cell's row weight gathered into the lanes.
#[inline]
#[target_feature(enable = "neon")]
unsafe fn lane_weights(weights: RowWeights<'_>, index: usize) -> float32x4_t {
    if weights.stride == 1 {
        // SAFETY: the caller guarantees NEON support and weights covering
        // the four cells from `index`.
        unsafe { vld1q_f32(weights.values.as_ptr().add(index)) }
    } else {
        let lanes = [
            weights.get(index),
            weights.get(index + 1),
            weights.get(index + 2),
            weights.get(index + 3),
        ];
        // SAFETY: the caller guarantees NEON support; `lanes` holds four f32s.
        unsafe { vld1q_f32(lanes.as_ptr()) }
    }
}
#[inline]
#[target_feature(enable = "neon")]
unsafe fn store_grad_pairs(out: *mut GradPair, index: usize, grad: float32x4_t, hess: float32x4_t) {
    // SAFETY: the caller guarantees NEON support and four writable pairs at `index`.
    unsafe {
        vst2q_f32(out.add(index).cast::<f32>(), float32x4x2_t(grad, hess));
    }
}

/// Exponential for finite f32 lanes in [-80, 80]. Range
/// reduction keeps the polynomial input in [-ln(2)/2, ln(2)/2], where a
/// seventh-order Taylor polynomial is within a few f32 ULPs.
#[inline]
#[target_feature(enable = "neon")]
unsafe fn expq_f32<const ESTRIN: bool>(value: float32x4_t) -> float32x4_t {
    // SAFETY: the caller guarantees NEON support; all operations use registers.
    unsafe {
        let scaled = vmulq_n_f32(value, std::f32::consts::LOG2_E);
        let exponent = vcvtnq_s32_f32(scaled);
        let exponent_f32 = vcvtq_f32_s32(exponent);

        // Split ln(2) so the range reduction loses fewer low bits.
        let mut reduced = vfmsq_n_f32(value, exponent_f32, 0.693_359_4);
        reduced = vfmaq_n_f32(reduced, exponent_f32, 2.121_944_4e-4);

        let polynomial = if ESTRIN {
            // Estrin evaluation exposes independent pairs instead of seven dependent FMAs.
            let squared = vmulq_f32(reduced, reduced);
            let fourth = vmulq_f32(squared, squared);
            let pair_0 = vaddq_f32(vdupq_n_f32(1.0), reduced);
            let pair_1 = vfmaq_f32(vdupq_n_f32(0.5), vdupq_n_f32(1.0 / 6.0), reduced);
            let pair_2 = vfmaq_f32(vdupq_n_f32(1.0 / 24.0), vdupq_n_f32(1.0 / 120.0), reduced);
            let pair_3 = vfmaq_f32(
                vdupq_n_f32(1.0 / 720.0),
                vdupq_n_f32(1.0 / 5_040.0),
                reduced,
            );
            let low = vfmaq_f32(pair_0, pair_1, squared);
            let high = vfmaq_f32(pair_2, pair_3, squared);
            vfmaq_f32(low, high, fourth)
        } else {
            // Wide in-place softmax favors Horner evaluation for throughput.
            let mut polynomial = vdupq_n_f32(1.0 / 5_040.0);
            polynomial = vfmaq_f32(vdupq_n_f32(1.0 / 720.0), polynomial, reduced);
            polynomial = vfmaq_f32(vdupq_n_f32(1.0 / 120.0), polynomial, reduced);
            polynomial = vfmaq_f32(vdupq_n_f32(1.0 / 24.0), polynomial, reduced);
            polynomial = vfmaq_f32(vdupq_n_f32(1.0 / 6.0), polynomial, reduced);
            polynomial = vfmaq_f32(vdupq_n_f32(0.5), polynomial, reduced);
            polynomial = vfmaq_f32(vdupq_n_f32(1.0), polynomial, reduced);
            vfmaq_f32(vdupq_n_f32(1.0), polynomial, reduced)
        };

        let exponent_bits = vshlq_n_s32(vaddq_s32(exponent, vdupq_n_s32(127)), 23);
        let multiply: unsafe fn(float32x4_t, float32x4_t) -> float32x4_t = vmulq_f32;
        multiply(polynomial, vreinterpretq_f32_s32(exponent_bits))
    }
}

#[inline]
#[target_feature(enable = "neon")]
unsafe fn sigmoidq_f32(value: float32x4_t) -> float32x4_t {
    // SAFETY: the caller guarantees NEON support; all operations use registers.
    unsafe {
        let magnitude = vabsq_f32(value);
        let exp = expq_f32::<true>(vnegq_f32(magnitude));
        let denominator = vaddq_f32(vdupq_n_f32(1.0), exp);
        let positive = vdivq_f32(vdupq_n_f32(1.0), denominator);
        let negative = vdivq_f32(exp, denominator);
        vbslq_f32(vcgeq_f32(value, vdupq_n_f32(0.0)), positive, negative)
    }
}

#[inline]
#[target_feature(enable = "neon")]
unsafe fn regular_input(value: float32x4_t) -> bool {
    // SAFETY: the caller guarantees NEON support; all operations use registers.
    unsafe {
        let magnitude = vabsq_f32(value);
        let in_range = vcleq_f32(magnitude, vdupq_n_f32(MAX_FAST_EXP_INPUT));
        let minimum: unsafe fn(uint32x4_t) -> u32 = vminvq_u32;
        minimum(in_range) == u32::MAX
    }
}

#[inline]
#[target_feature(enable = "neon")]
unsafe fn finite_input(value: float32x4_t) -> bool {
    // SAFETY: the caller guarantees NEON support; all operations use registers.
    unsafe {
        let finite = vcleq_f32(vabsq_f32(value), vdupq_n_f32(f32::MAX));
        let minimum: unsafe fn(uint32x4_t) -> u32 = vminvq_u32;
        minimum(finite) == u32::MAX
    }
}

#[inline]
#[target_feature(enable = "neon")]
unsafe fn finite_min_max(values: &[f32]) -> Option<(f32, f32)> {
    // SAFETY: the caller guarantees NEON support. Pointer bounds are
    // documented at each memory access below.
    unsafe {
        let mut maximum = vdupq_n_f32(f32::NEG_INFINITY);
        let mut minimum = vdupq_n_f32(f32::INFINITY);
        let mut index = 0;
        while index + VECTOR_WIDTH <= values.len() {
            // SAFETY: the loop condition leaves four readable values.
            let value = vld1q_f32(values.as_ptr().add(index));
            // These min/max instructions propagate NaNs, so checking the final
            // extrema also rejects non-finite lanes from any earlier block.
            maximum = vmaxq_f32(maximum, value);
            minimum = vminq_f32(minimum, value);
            index += VECTOR_WIDTH;
        }

        let mut max = vmaxvq_f32(maximum);
        let mut min = vminvq_f32(minimum);
        if !min.is_finite() || !max.is_finite() {
            return None;
        }
        for &value in &values[index..] {
            if !value.is_finite() {
                return None;
            }
            max = max.max(value);
            min = min.min(value);
        }
        Some((min, max))
    }
}

/// Natural logarithm for positive, finite, normal f64 lanes. Mantissas are
/// reduced around one and evaluated with an atanh series through z^15.
#[inline]
#[target_feature(enable = "neon")]
unsafe fn logq_f64(value: float64x2_t) -> float64x2_t {
    // SAFETY: the caller guarantees NEON support; all operations use registers.
    unsafe {
        const MANTISSA_MASK: u64 = (1u64 << 52) - 1;
        const ONE_BITS: u64 = 1023u64 << 52;
        let bits = vreinterpretq_u64_f64(value);
        let raw_exponent = vandq_u64(vshrq_n_u64::<52>(bits), vdupq_n_u64(0x7ff));
        let mut exponent = vsubq_f64(vcvtq_f64_u64(raw_exponent), vdupq_n_f64(1023.0));
        let mut mantissa = vreinterpretq_f64_u64(vorrq_u64(
            vandq_u64(bits, vdupq_n_u64(MANTISSA_MASK)),
            vdupq_n_u64(ONE_BITS),
        ));

        let halve = vcgtq_f64(mantissa, vdupq_n_f64(std::f64::consts::SQRT_2));
        mantissa = vbslq_f64(halve, vmulq_n_f64(mantissa, 0.5), mantissa);
        exponent = vaddq_f64(
            exponent,
            vbslq_f64(halve, vdupq_n_f64(1.0), vdupq_n_f64(0.0)),
        );

        let one = vdupq_n_f64(1.0);
        let z = vdivq_f64(vsubq_f64(mantissa, one), vaddq_f64(mantissa, one));
        let z_squared = vmulq_f64(z, z);
        // Estrin evaluation exposes independent FMAs instead of a seven-FMA chain.
        let z_fourth = vmulq_f64(z_squared, z_squared);
        let z_eighth = vmulq_f64(z_fourth, z_fourth);
        let pair_0 = vfmaq_f64(one, vdupq_n_f64(1.0 / 3.0), z_squared);
        let pair_1 = vfmaq_f64(vdupq_n_f64(1.0 / 5.0), vdupq_n_f64(1.0 / 7.0), z_squared);
        let pair_2 = vfmaq_f64(vdupq_n_f64(1.0 / 9.0), vdupq_n_f64(1.0 / 11.0), z_squared);
        let pair_3 = vfmaq_f64(vdupq_n_f64(1.0 / 13.0), vdupq_n_f64(1.0 / 15.0), z_squared);
        let low = vfmaq_f64(pair_0, pair_1, z_fourth);
        let high = vfmaq_f64(pair_2, pair_3, z_fourth);
        let polynomial = vfmaq_f64(low, high, z_eighth);
        let mantissa_log = vmulq_n_f64(vmulq_f64(z, polynomial), 2.0);
        let result = vfmaq_n_f64(mantissa_log, exponent, 0.693_145_751_953_125);
        let multiply_add: unsafe fn(float64x2_t, float64x2_t, f64) -> float64x2_t = vfmaq_n_f64;
        multiply_add(result, exponent, 1.428_606_820_309_417_3e-6)
    }
}

/// Exponential for the finite f64 range generated by Tweedie powers of f32
/// predictions. Range reduction is followed by a degree-12 polynomial on
/// [-ln(2)/2, ln(2)/2].
#[inline]
#[target_feature(enable = "neon")]
unsafe fn expq_f64(value: float64x2_t) -> float64x2_t {
    // SAFETY: the caller guarantees NEON support; all operations use registers.
    unsafe {
        let scaled = vmulq_n_f64(value, std::f64::consts::LOG2_E);
        let exponent = vcvtnq_s64_f64(scaled);
        let exponent_f64 = vcvtq_f64_s64(exponent);
        let mut reduced = vfmsq_n_f64(value, exponent_f64, 0.693_145_751_953_125);
        reduced = vfmsq_n_f64(reduced, exponent_f64, 1.428_606_820_309_417_3e-6);

        // Independent polynomial pairs shorten the FMA dependency chain.
        let squared = vmulq_f64(reduced, reduced);
        let fourth = vmulq_f64(squared, squared);
        let eighth = vmulq_f64(fourth, fourth);
        let pair_0 = vaddq_f64(vdupq_n_f64(1.0), reduced);
        let pair_1 = vfmaq_f64(vdupq_n_f64(0.5), vdupq_n_f64(1.0 / 6.0), reduced);
        let pair_2 = vfmaq_f64(vdupq_n_f64(1.0 / 24.0), vdupq_n_f64(1.0 / 120.0), reduced);
        let pair_3 = vfmaq_f64(
            vdupq_n_f64(1.0 / 720.0),
            vdupq_n_f64(1.0 / 5_040.0),
            reduced,
        );
        let pair_4 = vfmaq_f64(
            vdupq_n_f64(1.0 / 40_320.0),
            vdupq_n_f64(1.0 / 362_880.0),
            reduced,
        );
        let pair_5 = vfmaq_f64(
            vdupq_n_f64(1.0 / 3_628_800.0),
            vdupq_n_f64(1.0 / 39_916_800.0),
            reduced,
        );
        let low = vfmaq_f64(pair_0, pair_1, squared);
        let middle = vfmaq_f64(pair_2, pair_3, squared);
        let high = vfmaq_f64(pair_4, pair_5, squared);
        let low = vfmaq_f64(low, middle, fourth);
        let high = vfmaq_f64(high, vdupq_n_f64(1.0 / 479_001_600.0), fourth);
        let polynomial = vfmaq_f64(low, high, eighth);

        let exponent_bits = vshlq_n_s64::<52>(vaddq_s64(exponent, vdupq_n_s64(1023)));
        let multiply: unsafe fn(float64x2_t, float64x2_t) -> float64x2_t = vmulq_f64;
        multiply(polynomial, vreinterpretq_f64_s64(exponent_bits))
    }
}

/// Number of the 16 `cuts` that are `<= value`.
#[target_feature(enable = "neon")]
pub(super) unsafe fn count_le_16(cuts: &[f32], value: f32) -> usize {
    debug_assert_eq!(cuts.len(), 16);
    // SAFETY: the caller guarantees NEON support and exactly 16 readable
    // values; each load below covers one aligned quarter of them.
    unsafe {
        let value = vdupq_n_f32(value);
        let ptr = cuts.as_ptr();
        let mut count = vdupq_n_u32(0);
        for quarter in 0..4 {
            let mask = vcleq_f32(vld1q_f32(ptr.add(quarter * VECTOR_WIDTH)), value);
            count = vsubq_u32(count, mask);
        }
        vaddvq_u32(count) as usize
    }
}

/// `alpha * h[i] / fma(alpha, u[i], 1)` for eight lanes, four at a time.
#[target_feature(enable = "neon")]
pub(super) unsafe fn edge_terms_8(alpha: f32, h: &[f32; 8], u: &[f32; 8]) -> [f32; 8] {
    let mut out = [0f32; 8];
    // SAFETY: the caller guarantees NEON support; each access covers one
    // half of an eight-lane array.
    unsafe {
        let a = vdupq_n_f32(alpha);
        let one = vdupq_n_f32(1.0);
        for half in 0..2 {
            let offset = half * VECTOR_WIDTH;
            let hv = vld1q_f32(h.as_ptr().add(offset));
            let uv = vld1q_f32(u.as_ptr().add(offset));
            let t = vdivq_f32(vmulq_f32(a, hv), vfmaq_f32(one, a, uv));
            vst1q_f32(out.as_mut_ptr().add(offset), t);
        }
    }
    out
}

/// `c[i] * fma(alpha, u[i], 1)` for eight lanes, four at a time.
#[target_feature(enable = "neon")]
pub(super) unsafe fn scaled_basis_8(alpha: f32, c: &[f32; 8], u: &[f32; 8]) -> [f32; 8] {
    let mut out = [0f32; 8];
    // SAFETY: the caller guarantees NEON support; each access covers one
    // half of an eight-lane array.
    unsafe {
        let a = vdupq_n_f32(alpha);
        let one = vdupq_n_f32(1.0);
        for half in 0..2 {
            let offset = half * VECTOR_WIDTH;
            let cv = vld1q_f32(c.as_ptr().add(offset));
            let uv = vld1q_f32(u.as_ptr().add(offset));
            vst1q_f32(
                out.as_mut_ptr().add(offset),
                vmulq_f32(cv, vfmaq_f32(one, a, uv)),
            );
        }
    }
    out
}

/// `c[i] / fma(alpha, u[i], 1)` for eight lanes when every `c[i]` and
/// every denominator is finite, else `None`.
#[target_feature(enable = "neon")]
pub(super) unsafe fn divided_basis_8(alpha: f32, c: &[f32; 8], u: &[f32; 8]) -> Option<[f32; 8]> {
    let mut out = [0f32; 8];
    // SAFETY: the caller guarantees NEON support; each access covers one
    // half of an eight-lane array.
    unsafe {
        let a = vdupq_n_f32(alpha);
        let one = vdupq_n_f32(1.0);
        let inf = vdupq_n_f32(f32::INFINITY);
        let mut finite = vdupq_n_u32(u32::MAX);
        for half in 0..2 {
            let offset = half * VECTOR_WIDTH;
            let cv = vld1q_f32(c.as_ptr().add(offset));
            let uv = vld1q_f32(u.as_ptr().add(offset));
            let old = vfmaq_f32(one, a, uv);
            // `|x| < inf` is false exactly for infinities and NaN.
            finite = vandq_u32(finite, vcltq_f32(vabsq_f32(cv), inf));
            finite = vandq_u32(finite, vcltq_f32(vabsq_f32(old), inf));
            vst1q_f32(out.as_mut_ptr().add(offset), vdivq_f32(cv, old));
        }
        (vminvq_u32(finite) == u32::MAX).then_some(out)
    }
}

/// Vector-loop shell of a `&mut [f32]` unary inplace kernel: vector fast path
/// for regular lanes, scalar per-lane fallback otherwise. The kernel and
/// scalar formulas (intrinsics included) are passed in as expressions.
macro_rules! unary_inplace_kernel {
    ($name:ident, $kernel:expr, $scalar:expr) => {
        #[target_feature(enable = "neon")]
        pub(super) unsafe fn $name(values: &mut [f32]) {
            // SAFETY: the caller guarantees NEON support. Pointer bounds are
            // documented at each memory access below.
            unsafe {
                let mut index = 0;
                while index + VECTOR_WIDTH <= values.len() {
                    // SAFETY: the loop condition leaves four readable and writable values.
                    let input = vld1q_f32(values.as_ptr().add(index));
                    if regular_input(input) {
                        // SAFETY: the loop condition leaves four writable values.
                        vst1q_f32(values.as_mut_ptr().add(index), ($kernel)(input));
                    } else {
                        for value in &mut values[index..index + VECTOR_WIDTH] {
                            *value = ($scalar)(*value);
                        }
                    }
                    index += VECTOR_WIDTH;
                }
                for value in &mut values[index..] {
                    *value = ($scalar)(*value);
                }
            }
        }
    };
}

unary_inplace_kernel!(exp_inplace, expq_f32::<true>, f32::exp);
unary_inplace_kernel!(sigmoid_inplace, sigmoidq_f32, sigmoid_scalar);

#[target_feature(enable = "neon")]
pub(super) unsafe fn logistic_gradient(
    preds: &[f32],
    labels: &[f32],
    weights: Option<&[f32]>,
    scale_pos_weight: f32,
    min_hess: f32,
    out: &mut [GradPair],
) {
    // SAFETY: the caller guarantees NEON support. Pointer bounds are
    // documented at each memory access below.
    unsafe {
        let one = vdupq_n_f32(1.0);
        let scale = vdupq_n_f32(scale_pos_weight);
        let min_hess_vector = vdupq_n_f32(min_hess);
        let scalar_range = |out: &mut [GradPair], range| {
            scalar::logistic_gradient(
                preds,
                labels,
                weights,
                scale_pos_weight,
                min_hess,
                out,
                range,
            );
        };
        let mut index = 0;

        while index + VECTOR_WIDTH <= preds.len() {
            // SAFETY: the loop condition and objective length checks leave four
            // readable inputs and four writable GradPair outputs.
            let pred = vld1q_f32(preds.as_ptr().add(index));
            gradient_guard!(
                !regular_input(pred),
                index,
                scalar_range(out, index..index + VECTOR_WIDTH)
            );
            let label = vld1q_f32(labels.as_ptr().add(index));
            let probability = sigmoidq_f32(pred);
            let mut weight = match weights {
                Some(values) => vld1q_f32(values.as_ptr().add(index)),
                None => one,
            };
            weight = vmulq_f32(weight, vbslq_f32(vceqq_f32(label, one), scale, one));
            let grad = vmulq_f32(vsubq_f32(probability, label), weight);
            let hess = vmulq_f32(
                vmaxq_f32(
                    vmulq_f32(probability, vsubq_f32(one, probability)),
                    min_hess_vector,
                ),
                weight,
            );
            // SAFETY: the loop condition leaves room for four pairs; see `store_grad_pairs`.
            store_grad_pairs(out.as_mut_ptr(), index, grad, hess);
            index += VECTOR_WIDTH;
        }

        scalar_range(out, index..preds.len());
    }
}

#[target_feature(enable = "neon")]
pub(super) unsafe fn poisson_gradient(
    preds: &[f32],
    labels: &[f32],
    weights: Option<&[f32]>,
    max_delta_step: f32,
    out: &mut [GradPair],
) {
    // SAFETY: the caller guarantees NEON support. Pointer bounds are
    // documented at each memory access below.
    unsafe {
        let delta = vdupq_n_f32(max_delta_step);
        let mut index = 0;
        while index + VECTOR_WIDTH <= preds.len() {
            // SAFETY: the common-length contract leaves four readable inputs.
            let pred = vld1q_f32(preds.as_ptr().add(index));
            let shifted = vaddq_f32(pred, delta);
            gradient_guard!(
                !regular_input(pred) || !regular_input(shifted),
                index,
                scalar::poisson_gradient(
                    preds,
                    labels,
                    weights,
                    max_delta_step,
                    out,
                    index..index + VECTOR_WIDTH,
                )
            );
            let label = vld1q_f32(labels.as_ptr().add(index));
            let weight = match weights {
                Some(values) => vld1q_f32(values.as_ptr().add(index)),
                None => vdupq_n_f32(1.0),
            };
            let grad = vmulq_f32(vsubq_f32(expq_f32::<true>(pred), label), weight);
            let hess = vmulq_f32(expq_f32::<true>(shifted), weight);
            // SAFETY: the loop condition leaves room for four pairs; see `store_grad_pairs`.
            store_grad_pairs(out.as_mut_ptr(), index, grad, hess);
            index += VECTOR_WIDTH;
        }
        scalar::poisson_gradient(
            preds,
            labels,
            weights,
            max_delta_step,
            out,
            index..preds.len(),
        );
    }
}

#[target_feature(enable = "neon")]
pub(super) unsafe fn gamma_gradient(
    preds: &[f32],
    labels: &[f32],
    weights: Option<&[f32]>,
    scale_pos_weight: f32,
    out: &mut [GradPair],
) {
    // SAFETY: the caller guarantees NEON support. Pointer bounds are
    // documented at each memory access below.
    unsafe {
        let one = vdupq_n_f32(1.0);
        let scale = vdupq_n_f32(scale_pos_weight);
        let scalar_range = |out: &mut [GradPair], range| {
            scalar::gamma_gradient(preds, labels, weights, scale_pos_weight, out, range);
        };
        let mut index = 0;
        while index + VECTOR_WIDTH <= preds.len() {
            // SAFETY: the common-length contract leaves four readable inputs.
            let pred = vld1q_f32(preds.as_ptr().add(index));
            let negative = vnegq_f32(pred);
            gradient_guard!(
                !regular_input(negative),
                index,
                scalar_range(out, index..index + VECTOR_WIDTH)
            );
            let label = vld1q_f32(labels.as_ptr().add(index));
            let mut weight = match weights {
                Some(values) => vld1q_f32(values.as_ptr().add(index)),
                None => one,
            };
            weight = vmulq_f32(weight, vbslq_f32(vceqq_f32(label, one), scale, one));
            let scaled = vmulq_f32(label, expq_f32::<true>(negative));
            let grad = vmulq_f32(vsubq_f32(one, scaled), weight);
            let hess = vmulq_f32(scaled, weight);
            // SAFETY: the loop condition leaves room for four pairs; see `store_grad_pairs`.
            store_grad_pairs(out.as_mut_ptr(), index, grad, hess);
            index += VECTOR_WIDTH;
        }
        scalar_range(out, index..preds.len());
    }
}

#[target_feature(enable = "neon")]
pub(super) unsafe fn tweedie_gradient(
    preds: &[f32],
    labels: &[f32],
    weights: Option<&[f32]>,
    rho: f32,
    out: &mut [GradPair],
) {
    // SAFETY: the caller guarantees NEON support. Pointer bounds are
    // documented at each memory access below.
    unsafe {
        let one_minus_rho = vdupq_n_f32(1.0 - rho);
        let two_minus_rho = vdupq_n_f32(2.0 - rho);
        let mut index = 0;
        while index + VECTOR_WIDTH <= preds.len() {
            // SAFETY: the common-length contract leaves four readable inputs.
            let pred = vld1q_f32(preds.as_ptr().add(index));
            let input_1 = vmulq_f32(one_minus_rho, pred);
            let input_2 = vmulq_f32(two_minus_rho, pred);
            gradient_guard!(
                !regular_input(input_1) || !regular_input(input_2),
                index,
                scalar::tweedie_gradient(
                    preds,
                    labels,
                    weights,
                    rho,
                    out,
                    index..index + VECTOR_WIDTH,
                )
            );
            let label = vld1q_f32(labels.as_ptr().add(index));
            let weight = match weights {
                Some(values) => vld1q_f32(values.as_ptr().add(index)),
                None => vdupq_n_f32(1.0),
            };
            let exp_1 = expq_f32::<true>(input_1);
            let exp_2 = expq_f32::<true>(input_2);
            let label_exp_1 = vmulq_f32(label, exp_1);
            let grad = vmulq_f32(vsubq_f32(exp_2, label_exp_1), weight);
            let hess = vmulq_f32(
                vsubq_f32(
                    vmulq_f32(two_minus_rho, exp_2),
                    vmulq_f32(one_minus_rho, label_exp_1),
                ),
                weight,
            );
            // SAFETY: the loop condition leaves room for four pairs; see `store_grad_pairs`.
            store_grad_pairs(out.as_mut_ptr(), index, grad, hess);
            index += VECTOR_WIDTH;
        }
        scalar::tweedie_gradient(preds, labels, weights, rho, out, index..preds.len());
    }
}

#[target_feature(enable = "neon")]
pub(super) unsafe fn softmax_inplace(values: &mut [f32]) {
    // SAFETY: the caller guarantees NEON support. Pointer bounds are
    // documented at each memory access below.
    unsafe {
        let Some((min, max)) = finite_min_max(values) else {
            super::softmax_scalar(values);
            return;
        };
        if max - min > MAX_FAST_EXP_INPUT {
            super::softmax_scalar(values);
            return;
        }

        let max_vector = vdupq_n_f32(max);
        let mut sum_vector = vdupq_n_f32(0.0);
        let mut index = 0;
        while index + VECTOR_WIDTH <= values.len() {
            // SAFETY: the loop condition leaves four readable and writable values.
            let input = vld1q_f32(values.as_ptr().add(index));
            let exp = expq_f32::<false>(vsubq_f32(input, max_vector));
            sum_vector = vaddq_f32(sum_vector, exp);
            // SAFETY: the loop condition leaves four writable values.
            vst1q_f32(values.as_mut_ptr().add(index), exp);
            index += VECTOR_WIDTH;
        }
        let mut sum = vaddvq_f32(sum_vector);
        for value in &mut values[index..] {
            *value = (*value - max).exp();
            sum += *value;
        }

        let inverse = 1.0 / sum;
        let inverse_vector = vdupq_n_f32(inverse);
        index = 0;
        while index + VECTOR_WIDTH <= values.len() {
            // SAFETY: the loop condition leaves four readable and writable values.

            let probability = vld1q_f32(values.as_ptr().add(index));
            vst1q_f32(
                values.as_mut_ptr().add(index),
                vmulq_f32(probability, inverse_vector),
            );

            index += VECTOR_WIDTH;
        }
        for value in &mut values[index..] {
            *value *= inverse;
        }
    }
}

/// Process four independent rows in the lanes, so short rows need neither a
/// horizontal reduction nor a scratch pass through the output matrix.
///
/// `GRADIENT` selects the shift of `SoftmaxMultiClassObj::GetGradient`,
/// `max(f32::MIN_POSITIVE, row...)`, instead of the plain row maximum of
/// `common::Softmax`; rows whose maximum is at least `MIN_POSITIVE` are
/// unaffected.
#[inline]
#[target_feature(enable = "neon")]
unsafe fn short_softmax_batch<const K: usize, const GRADIENT: bool>(
    preds: &[f32],
) -> Option<[float32x4_t; K]> {
    // SAFETY: callers provide four complete rows, K is 2, 3, or 4, and NEON
    // is available. Interleaved loads transpose row-major inputs into classes.
    unsafe {
        let mut values = [vdupq_n_f32(0.0); K];
        match K {
            2 => {
                let input = vld2q_f32(preds.as_ptr());
                values[0] = input.0;
                values[1] = input.1;
            }
            3 => {
                let input = vld3q_f32(preds.as_ptr());
                values[0] = input.0;
                values[1] = input.1;
                values[2] = input.2;
            }
            4 => {
                let input = vld4q_f32(preds.as_ptr());
                values[0] = input.0;
                values[1] = input.1;
                values[2] = input.2;
                values[3] = input.3;
            }
            _ => unreachable!(),
        }
        let mut minimum = values[0];
        let mut maximum = values[0];
        for &value in &values[1..] {
            minimum = vminq_f32(minimum, value);
            maximum = vmaxq_f32(maximum, value);
        }
        if GRADIENT {
            // `vmaxq_f32` propagates NaN, so the range guard below still
            // rejects non-finite rows.
            maximum = vmaxq_f32(maximum, vdupq_n_f32(f32::MIN_POSITIVE));
        }
        // NaNs propagate through min/max; infinities produce a non-finite
        // range. Either makes this ordered comparison fail for that row.
        if vminvq_u32(vcleq_f32(
            vsubq_f32(maximum, minimum),
            vdupq_n_f32(MAX_FAST_EXP_INPUT),
        )) != u32::MAX
        {
            return None;
        }
        let mut sum = vdupq_n_f32(0.0);
        for value in &mut values {
            *value = expq_f32::<true>(vsubq_f32(*value, maximum));
            sum = vaddq_f32(sum, *value);
        }
        let inverse = vdivq_f32(vdupq_n_f32(1.0), sum);
        for value in &mut values {
            *value = vmulq_f32(*value, inverse);
        }
        Some(values)
    }
}

#[target_feature(enable = "neon")]
pub(super) unsafe fn short_softmax_rows<const K: usize>(values: &mut [f32]) {
    // SAFETY: NEON is available, K is 2, 3, or 4, and chunks bound each
    // interleaved load/store to four complete rows. Remaining rows are scalar.
    unsafe {
        #[allow(
            clippy::chunks_exact_to_as_chunks,
            reason = "`as_chunks_mut::<{ 4 * K }>` needs generic_const_exprs"
        )]
        let mut batches = values.chunks_exact_mut(4 * K);
        for batch in &mut batches {
            if let Some(p) = short_softmax_batch::<K, false>(batch) {
                match K {
                    2 => vst2q_f32(batch.as_mut_ptr(), float32x4x2_t(p[0], p[1])),
                    3 => vst3q_f32(batch.as_mut_ptr(), float32x4x3_t(p[0], p[1], p[2])),
                    4 => vst4q_f32(batch.as_mut_ptr(), float32x4x4_t(p[0], p[1], p[2], p[3])),
                    _ => unreachable!(),
                }
            } else {
                for row in batch.chunks_mut(K) {
                    super::softmax_scalar(row);
                }
            }
        }
        for row in batches.into_remainder().chunks_mut(K) {
            super::softmax_scalar(row);
        }
    }
}

#[target_feature(enable = "neon")]
pub(super) unsafe fn short_softmax_gradient<const K: usize>(
    preds: &[f32],
    labels: &[f32],
    weights: Option<&[f32]>,
    min_hess: f32,
    out: &mut [GradPair],
) {
    // SAFETY: dispatch checks complete matrices and NEON support. Each batch
    // covers four labels/weights and 4*K predictions/output pairs; K is 2..=4.
    unsafe {
        let one = vdupq_n_f32(1.0);
        let zero = vdupq_n_f32(0.0);
        let minimum = vdupq_n_f32(min_hess);
        let mut row = 0;
        while row + 4 <= labels.len() {
            let base = row * K;
            if let Some(probabilities) = short_softmax_batch::<K, true>(&preds[base..base + 4 * K])
            {
                // Saturating conversion matches Rust's float-to-usize cast
                // when comparing with class IDs 0..K, including NaN/negatives.
                let target = vcvtq_u32_f32(vld1q_f32(labels.as_ptr().add(row)));
                let weight = match weights {
                    Some(weights) => vld1q_f32(weights.as_ptr().add(row)),
                    None => one,
                };
                let mut low = [vdupq_n_u64(0); K];
                let mut high = low;
                for class in 0..K {
                    let probability = probabilities[class];
                    let indicator =
                        vbslq_f32(vceqq_u32(target, vdupq_n_u32(class as u32)), one, zero);
                    let gradient = vmulq_f32(vsubq_f32(probability, indicator), weight);
                    let hessian = vmaxnmq_f32(
                        vmulq_f32(
                            vmulq_f32(vmulq_n_f32(probability, 2.0), vsubq_f32(one, probability)),
                            weight,
                        ),
                        minimum,
                    );
                    low[class] = vreinterpretq_u64_f32(vzip1q_f32(gradient, hessian));
                    high[class] = vreinterpretq_u64_f32(vzip2q_f32(gradient, hessian));
                }
                // Each u64 lane contains one repr(C) GradPair. Interleaving
                // classes restores row-major order, two complete rows per store.
                for (offset, pairs) in [(0, low), (2 * K, high)] {
                    #[allow(
                        clippy::cast_ptr_alignment,
                        reason = "NEON st2/st4 stores accept any alignment"
                    )]
                    let dest = out.as_mut_ptr().add(base + offset).cast::<u64>();
                    match K {
                        2 => vst2q_u64(dest, uint64x2x2_t(pairs[0], pairs[1])),
                        3 => {
                            // GradPair guarantees only f32 alignment. Avoid
                            // vst3q_u64: stdarch can implement it using a typed
                            // copy that requires an eight-byte-aligned pointer.
                            // Assemble [a0,b0,c0,a1,b1,c1] in three vectors.
                            let dest = dest.cast::<f32>();
                            vst1q_f32(dest, vreinterpretq_f32_u64(vzip1q_u64(pairs[0], pairs[1])));
                            vst1q_f32(
                                dest.add(4),
                                vreinterpretq_f32_u64(vzip1q_u64(
                                    pairs[2],
                                    vextq_u64::<1>(pairs[0], pairs[0]),
                                )),
                            );
                            vst1q_f32(
                                dest.add(8),
                                vreinterpretq_f32_u64(vzip2q_u64(pairs[1], pairs[2])),
                            );
                        }
                        4 => vst4q_u64(dest, uint64x2x4_t(pairs[0], pairs[1], pairs[2], pairs[3])),
                        _ => unreachable!(),
                    }
                }
            } else {
                super::softmax_gradient_rows_scalar(
                    preds,
                    labels,
                    weights,
                    min_hess,
                    out,
                    row..row + 4,
                    K,
                );
            }
            row += 4;
        }
        super::softmax_gradient_rows_scalar(
            preds,
            labels,
            weights,
            min_hess,
            out,
            row..labels.len(),
            K,
        );
    }
}

#[target_feature(enable = "neon")]
pub(super) unsafe fn softmax_rows_inplace(values: &mut [f32], num_class: usize) {
    // SAFETY: the caller guarantees NEON support. Pointer bounds are
    // documented at each memory access below.
    unsafe {
        for row in values.chunks_mut(num_class) {
            // SAFETY: this function may only be entered after NEON detection and
            // `chunks_mut` bounds every class row.
            softmax_inplace(row);
        }
    }
}

#[target_feature(enable = "neon")]
pub(super) unsafe fn softmax_gradient(
    preds: &[f32],
    labels: &[f32],
    weights: Option<&[f32]>,
    num_class: usize,
    min_hess: f32,
    out: &mut [GradPair],
) {
    // SAFETY: the caller guarantees NEON support. Pointer bounds are
    // documented at each memory access below.
    unsafe {
        for row in 0..labels.len() {
            let base = row * num_class;
            // SAFETY: the dispatcher's complete-matrix check ensures these row
            // slices exist, and NEON was detected before this function was called.

            softmax_gradient_row(
                &preds[base..base + num_class],
                labels[row] as usize,
                weights.map_or(1.0, |values| values[row]),
                min_hess,
                &mut out[base..base + num_class],
            );
        }
    }
}

#[inline]
#[target_feature(enable = "neon")]
unsafe fn softmax_gradient_row(
    preds: &[f32],
    label: usize,
    weight: f32,
    min_hess: f32,
    out: &mut [GradPair],
) {
    // SAFETY: the caller guarantees NEON support. Pointer bounds are
    // documented at each memory access below.
    unsafe {
        let Some((min, max)) = finite_min_max(preds) else {
            super::softmax_gradient_row_scalar(preds, label, weight, min_hess, out);
            return;
        };
        // `SoftmaxMultiClassObj::GetGradient` seeds `wmax` with
        // `numeric_limits<float>::min()`; a row entirely below it (all large
        // negative margins) then spans more than the fast-exp range and takes
        // the scalar path, which underflows to `0/0` exactly as XGBoost does.
        let max = max.max(f32::MIN_POSITIVE);
        if max - min > MAX_FAST_EXP_INPUT {
            super::softmax_gradient_row_scalar(preds, label, weight, min_hess, out);
            return;
        }

        let zero = vdupq_n_f32(0.0);
        let one = vdupq_n_f32(1.0);
        let max_vector = vdupq_n_f32(max);
        let mut sum_vector = zero;
        let mut index = 0;
        while index + VECTOR_WIDTH <= preds.len() {
            // SAFETY: the loop condition leaves four inputs and four output pairs.
            let input = vld1q_f32(preds.as_ptr().add(index));
            let exp = expq_f32::<true>(vsubq_f32(input, max_vector));
            sum_vector = vaddq_f32(sum_vector, exp);
            // Store the exponent in the gradient field as scratch space. The second
            // pass overwrites both fields with their final values.

            vst2q_f32(
                out.as_mut_ptr().add(index).cast::<f32>(),
                float32x4x2_t(exp, zero),
            );
            index += VECTOR_WIDTH;
        }
        let vector_end = index;
        let mut sum = vaddvq_f32(sum_vector);
        for index in vector_end..preds.len() {
            let exp = (preds[index] - max).exp();
            out[index].grad = exp;
            sum += exp;
        }

        let inverse = vdupq_n_f32(1.0 / sum);
        let row_weight = vdupq_n_f32(weight);
        let min_hessian = vdupq_n_f32(min_hess);
        let class_offsets_data = [0_u32, 1, 2, 3];
        // SAFETY: the local array contains exactly four u32 lanes.
        let class_offsets = vld1q_u32(class_offsets_data.as_ptr());
        let target_class = vdupq_n_u32(label as u32);
        index = 0;
        while index < vector_end {
            // SAFETY: the first pass initialized four GradPairs at this index.
            let scratch = vld2q_f32(out.as_ptr().add(index).cast::<f32>());
            let probability = vmulq_f32(scratch.0, inverse);
            let classes = vaddq_u32(vdupq_n_u32(index as u32), class_offsets);
            let target = vbslq_f32(vceqq_u32(classes, target_class), one, zero);
            let gradient = vmulq_f32(vsubq_f32(probability, target), row_weight);
            let hessian = vmaxq_f32(
                vmulq_f32(
                    vmulq_n_f32(vmulq_f32(probability, vsubq_f32(one, probability)), 2.0),
                    row_weight,
                ),
                min_hessian,
            );
            // SAFETY: the loop bounds leave four writable output pairs.

            vst2q_f32(
                out.as_mut_ptr().add(index).cast::<f32>(),
                float32x4x2_t(gradient, hessian),
            );
            index += VECTOR_WIDTH;
        }
        let inverse = 1.0 / sum;
        for (class, output) in out.iter_mut().enumerate().skip(vector_end) {
            let probability = output.grad * inverse;
            let target = if class == label { 1.0 } else { 0.0 };
            *output = GradPair::new(
                (probability - target) * weight,
                (2.0 * probability * (1.0 - probability) * weight).max(min_hess),
            );
        }
    }
}

#[target_feature(enable = "neon")]
pub(super) unsafe fn distance_sum<const SQUARED: bool>(
    preds: &[f32],
    labels: &[f32],
    weights: Option<RowWeights<'_>>,
) -> (f64, f64) {
    // SAFETY: the caller guarantees NEON support. Pointer bounds are
    // documented at each memory access below.
    unsafe {
        let mut sum_low = vdupq_n_f64(0.0);
        let mut sum_high = vdupq_n_f64(0.0);
        let mut weight_low = vdupq_n_f64(0.0);
        let mut weight_high = vdupq_n_f64(0.0);
        let mut index = 0;

        while index + VECTOR_WIDTH <= preds.len() {
            // SAFETY: the common-length contract leaves four readable values.
            let pred = vld1q_f32(preds.as_ptr().add(index));
            let label = vld1q_f32(labels.as_ptr().add(index));
            let difference_low = vsubq_f64(
                vcvt_f64_f32(vget_low_f32(pred)),
                vcvt_f64_f32(vget_low_f32(label)),
            );
            let difference_high = vsubq_f64(vcvt_high_f64_f32(pred), vcvt_high_f64_f32(label));
            let distance_low = if SQUARED {
                vmulq_f64(difference_low, difference_low)
            } else {
                vabsq_f64(difference_low)
            };
            let distance_high = if SQUARED {
                vmulq_f64(difference_high, difference_high)
            } else {
                vabsq_f64(difference_high)
            };
            accumulate_metric_sum!(
                weights,
                index,
                distance_low,
                distance_high,
                sum_low,
                sum_high,
                weight_low,
                weight_high
            );
            index += VECTOR_WIDTH;
        }

        let tail = scalar::distance_sum::<SQUARED>(preds, labels, weights, index..preds.len());
        let sum = vaddvq_f64(vaddq_f64(sum_low, sum_high)) + tail.0;
        let weight_sum = match weights {
            Some(_) => vaddvq_f64(vaddq_f64(weight_low, weight_high)) + tail.1,
            None => index as f64 + tail.1,
        };
        (sum, weight_sum)
    }
}

#[target_feature(enable = "neon")]
pub(super) unsafe fn classification_error_sum(
    preds: &[f32],
    labels: &[f32],
    weights: Option<RowWeights<'_>>,
) -> (f64, f64) {
    // SAFETY: the caller guarantees NEON support. Pointer bounds are
    // documented at each memory access below.
    unsafe {
        let threshold = vdupq_n_f32(0.5);
        let mut index = 0;
        if let Some(weights) = weights {
            let mut wrong_low = vdupq_n_f64(0.0);
            let mut wrong_high = vdupq_n_f64(0.0);
            let mut weight_low = vdupq_n_f64(0.0);
            let mut weight_high = vdupq_n_f64(0.0);
            while index + VECTOR_WIDTH <= preds.len() {
                // SAFETY: the common-length contract leaves four values in
                // each input slice.
                let pred = vld1q_f32(preds.as_ptr().add(index));
                let label = vld1q_f32(labels.as_ptr().add(index));
                let weight = lane_weights(weights, index);
                let mismatch = veorq_u32(vcgtq_f32(pred, threshold), vcgtq_f32(label, threshold));
                let wrong_weight = vbslq_f32(mismatch, weight, vdupq_n_f32(0.0));
                let current_wrong_low = vcvt_f64_f32(vget_low_f32(wrong_weight));
                let current_wrong_high = vcvt_high_f64_f32(wrong_weight);
                let current_weight_low = vcvt_f64_f32(vget_low_f32(weight));
                let current_weight_high = vcvt_high_f64_f32(weight);
                wrong_low = vaddq_f64(wrong_low, current_wrong_low);
                wrong_high = vaddq_f64(wrong_high, current_wrong_high);
                weight_low = vaddq_f64(weight_low, current_weight_low);
                weight_high = vaddq_f64(weight_high, current_weight_high);
                index += VECTOR_WIDTH;
            }
            let tail =
                scalar::classification_error_sum(preds, labels, Some(weights), index..preds.len());
            let wrong = vaddvq_f64(vaddq_f64(wrong_low, wrong_high)) + tail.0;
            let weight_sum = vaddvq_f64(vaddq_f64(weight_low, weight_high)) + tail.1;
            (wrong, weight_sum)
        } else {
            let one = vdupq_n_u32(1);
            let mut wrong_low = vdupq_n_u64(0);
            let mut wrong_high = vdupq_n_u64(0);
            while index + VECTOR_WIDTH <= preds.len() {
                // SAFETY: the common-length contract leaves four values in
                // each input slice.
                let pred = vld1q_f32(preds.as_ptr().add(index));
                let label = vld1q_f32(labels.as_ptr().add(index));
                let mismatch = vandq_u32(
                    veorq_u32(vcgtq_f32(pred, threshold), vcgtq_f32(label, threshold)),
                    one,
                );
                wrong_low = vaddq_u64(wrong_low, vmovl_u32(vget_low_u32(mismatch)));
                wrong_high = vaddq_u64(wrong_high, vmovl_high_u32(mismatch));
                index += VECTOR_WIDTH;
            }
            let tail = scalar::classification_error_sum(preds, labels, None, index..preds.len());
            let wrong = vaddvq_u64(vaddq_u64(wrong_low, wrong_high)) as f64 + tail.0;
            (wrong, preds.len() as f64)
        }
    }
}

#[target_feature(enable = "neon")]
pub(super) unsafe fn log_loss_sum(
    preds: &[f32],
    labels: &[f32],
    weights: Option<RowWeights<'_>>,
) -> (f64, f64) {
    // SAFETY: the caller guarantees NEON support. Pointer bounds are
    // documented at each memory access below.
    unsafe {
        let one = vdupq_n_f64(1.0);
        let floor = vdupq_n_f64(BINARY_LOG_LOSS_EPSILON);
        let mut loss_low = vdupq_n_f64(0.0);
        let mut loss_high = vdupq_n_f64(0.0);
        let mut weight_low = vdupq_n_f64(0.0);
        let mut weight_high = vdupq_n_f64(0.0);
        let mut fallback = (0.0, 0.0);
        let mut index = 0;

        while index + VECTOR_WIDTH <= preds.len() {
            // SAFETY: the common-length contract leaves four readable values.
            let pred = vld1q_f32(preds.as_ptr().add(index));
            metric_finite_guard!(
                pred,
                index,
                fallback,
                scalar::log_loss(preds, labels, weights, index..index + VECTOR_WIDTH)
            );
            let label = vld1q_f32(labels.as_ptr().add(index));
            // XGBoost floors each log argument at ε separately and does not
            // clamp the prediction into [0, 1]; the finite guard above makes
            // both logs finite, so a zero label coefficient contributes 0
            // exactly as upstream's `xlogy` branch does.
            let probability_low = vcvt_f64_f32(vget_low_f32(pred));
            let probability_high = vcvt_high_f64_f32(pred);
            let label_low = vcvt_f64_f32(vget_low_f32(label));
            let label_high = vcvt_high_f64_f32(label);
            let value_low = vnegq_f64(vaddq_f64(
                vmulq_f64(label_low, logq_f64(vmaxq_f64(probability_low, floor))),
                vmulq_f64(
                    vsubq_f64(one, label_low),
                    logq_f64(vmaxq_f64(vsubq_f64(one, probability_low), floor)),
                ),
            ));
            let value_high = vnegq_f64(vaddq_f64(
                vmulq_f64(label_high, logq_f64(vmaxq_f64(probability_high, floor))),
                vmulq_f64(
                    vsubq_f64(one, label_high),
                    logq_f64(vmaxq_f64(vsubq_f64(one, probability_high), floor)),
                ),
            ));
            accumulate_metric_sum!(
                weights,
                index,
                value_low,
                value_high,
                loss_low,
                loss_high,
                weight_low,
                weight_high
            );
            index += VECTOR_WIDTH;
        }

        let tail = scalar::log_loss(preds, labels, weights, index..preds.len());
        finish_metric_sum!(
            loss_low,
            loss_high,
            weight_low,
            weight_high,
            fallback,
            tail,
            weights,
            preds.len()
        )
    }
}

#[target_feature(enable = "neon")]
pub(super) unsafe fn positive_nloglik_sum<const GAMMA: bool>(
    preds: &[f32],
    labels: &[f32],
    weights: Option<RowWeights<'_>>,
) -> (f64, f64) {
    // SAFETY: the caller guarantees NEON support. Pointer bounds are
    // documented at each memory access below.
    unsafe {
        let minimum = vdupq_n_f64(MIN_POSITIVE_PREDICTION);
        let mut loss_low = vdupq_n_f64(0.0);
        let mut loss_high = vdupq_n_f64(0.0);
        let mut weight_low = vdupq_n_f64(0.0);
        let mut weight_high = vdupq_n_f64(0.0);
        let mut fallback = (0.0, 0.0);
        let mut index = 0;

        while index + VECTOR_WIDTH <= preds.len() {
            // SAFETY: the common-length contract leaves four readable values.
            let pred = vld1q_f32(preds.as_ptr().add(index));
            metric_finite_guard!(
                pred,
                index,
                fallback,
                scalar::positive_nloglik::<GAMMA>(
                    preds,
                    labels,
                    weights,
                    index..index + VECTOR_WIDTH
                )
            );
            let label = vld1q_f32(labels.as_ptr().add(index));
            let prediction_low = vmaxq_f64(vcvt_f64_f32(vget_low_f32(pred)), minimum);
            let prediction_high = vmaxq_f64(vcvt_high_f64_f32(pred), minimum);
            let label_low = vcvt_f64_f32(vget_low_f32(label));
            let label_high = vcvt_high_f64_f32(label);
            let log_low = logq_f64(prediction_low);
            let log_high = logq_f64(prediction_high);
            let value_low = if GAMMA {
                vaddq_f64(vdivq_f64(label_low, prediction_low), log_low)
            } else {
                vsubq_f64(prediction_low, vmulq_f64(label_low, log_low))
            };
            let value_high = if GAMMA {
                vaddq_f64(vdivq_f64(label_high, prediction_high), log_high)
            } else {
                vsubq_f64(prediction_high, vmulq_f64(label_high, log_high))
            };
            accumulate_metric_sum!(
                weights,
                index,
                value_low,
                value_high,
                loss_low,
                loss_high,
                weight_low,
                weight_high
            );
            index += VECTOR_WIDTH;
        }

        let tail = scalar::positive_nloglik::<GAMMA>(preds, labels, weights, index..preds.len());
        finish_metric_sum!(
            loss_low,
            loss_high,
            weight_low,
            weight_high,
            fallback,
            tail,
            weights,
            preds.len()
        )
    }
}

#[target_feature(enable = "neon")]
pub(super) unsafe fn tweedie_nloglik_sum(
    preds: &[f32],
    labels: &[f32],
    weights: Option<RowWeights<'_>>,
    rho: f64,
) -> (f64, f64) {
    // SAFETY: the caller guarantees NEON support. Pointer bounds are
    // documented at each memory access below.
    unsafe {
        let minimum = vdupq_n_f64(MIN_POSITIVE_PREDICTION);
        let first_power = vdupq_n_f64(1.0 - rho);
        let second_power = vdupq_n_f64(2.0 - rho);
        let mut loss_low = vdupq_n_f64(0.0);
        let mut loss_high = vdupq_n_f64(0.0);
        let mut weight_low = vdupq_n_f64(0.0);
        let mut weight_high = vdupq_n_f64(0.0);
        let mut fallback = (0.0, 0.0);
        let mut index = 0;

        while index + VECTOR_WIDTH <= preds.len() {
            // SAFETY: the common-length contract leaves four readable values.
            let pred = vld1q_f32(preds.as_ptr().add(index));
            metric_finite_guard!(
                pred,
                index,
                fallback,
                scalar::tweedie_nloglik(preds, labels, weights, rho, index..index + VECTOR_WIDTH)
            );
            let label = vld1q_f32(labels.as_ptr().add(index));
            let prediction_low = vmaxq_f64(vcvt_f64_f32(vget_low_f32(pred)), minimum);
            let prediction_high = vmaxq_f64(vcvt_high_f64_f32(pred), minimum);
            let label_low = vcvt_f64_f32(vget_low_f32(label));
            let label_high = vcvt_high_f64_f32(label);
            let log_low = logq_f64(prediction_low);
            let log_high = logq_f64(prediction_high);
            // `first_power` and `second_power` are also the denominators.
            let first_low = vdivq_f64(
                vmulq_f64(label_low, expq_f64(vmulq_f64(first_power, log_low))),
                first_power,
            );
            let first_high = vdivq_f64(
                vmulq_f64(label_high, expq_f64(vmulq_f64(first_power, log_high))),
                first_power,
            );
            let second_low = vdivq_f64(expq_f64(vmulq_f64(second_power, log_low)), second_power);
            let second_high = vdivq_f64(expq_f64(vmulq_f64(second_power, log_high)), second_power);
            let value_low = vsubq_f64(second_low, first_low);
            let value_high = vsubq_f64(second_high, first_high);
            accumulate_metric_sum!(
                weights,
                index,
                value_low,
                value_high,
                loss_low,
                loss_high,
                weight_low,
                weight_high
            );
            index += VECTOR_WIDTH;
        }

        let tail = scalar::tweedie_nloglik(preds, labels, weights, rho, index..preds.len());
        finish_metric_sum!(
            loss_low,
            loss_high,
            weight_low,
            weight_high,
            fallback,
            tail,
            weights,
            preds.len()
        )
    }
}

#[target_feature(enable = "neon")]
pub(super) unsafe fn multiclass_log_loss_sum(
    preds: &[f32],
    labels: &[f32],
    weights: Option<&[f32]>,
    num_class: usize,
) -> (f64, f64) {
    // SAFETY: the caller guarantees NEON support. Pointer bounds are
    // documented at each memory access below.
    unsafe {
        let lower = vdupq_n_f64(LOG_LOSS_EPSILON);
        let upper = vdupq_n_f64(1.0);
        let mut loss_low = vdupq_n_f64(0.0);
        let mut loss_high = vdupq_n_f64(0.0);
        let mut weight_low = vdupq_n_f64(0.0);
        let mut weight_high = vdupq_n_f64(0.0);
        let mut fallback = (0.0, 0.0);
        let mut index = 0;

        while index + VECTOR_WIDTH <= labels.len() {
            let selected = [
                preds[index * num_class + labels[index] as usize],
                preds[(index + 1) * num_class + labels[index + 1] as usize],
                preds[(index + 2) * num_class + labels[index + 2] as usize],
                preds[(index + 3) * num_class + labels[index + 3] as usize],
            ];
            // SAFETY: `selected` contains exactly four f32 lanes.
            let probability = vld1q_f32(selected.as_ptr());
            metric_finite_guard!(
                probability,
                index,
                fallback,
                scalar::multiclass_log_loss(
                    preds,
                    labels,
                    weights,
                    num_class,
                    index..index + VECTOR_WIDTH
                )
            );

            let probability_low = vminq_f64(
                vmaxq_f64(vcvt_f64_f32(vget_low_f32(probability)), lower),
                upper,
            );
            let probability_high =
                vminq_f64(vmaxq_f64(vcvt_high_f64_f32(probability), lower), upper);
            let value_low = vnegq_f64(logq_f64(probability_low));
            let value_high = vnegq_f64(logq_f64(probability_high));
            // SAFETY: the dispatcher verified one weight per label.
            accumulate_metric_sum!(
                weights,
                index,
                value_low,
                value_high,
                loss_low,
                loss_high,
                weight_low,
                weight_high
            );
            index += VECTOR_WIDTH;
        }

        let tail =
            scalar::multiclass_log_loss(preds, labels, weights, num_class, index..labels.len());
        finish_metric_sum!(
            loss_low,
            loss_high,
            weight_low,
            weight_high,
            fallback,
            tail,
            weights,
            labels.len()
        )
    }
}

#[target_feature(enable = "neon")]
pub(super) unsafe fn multiclass_error_sum(
    preds: &[f32],
    labels: &[f32],
    weights: Option<&[f32]>,
    num_class: usize,
) -> (f64, f64) {
    super::multiclass_error_sum_rows(preds, labels, weights, num_class, |row| {
        // SAFETY: the dispatcher verified NEON support, a complete prediction
        // matrix, and a u32-fitting class count.
        unsafe { argmax(row) }
    })
}

#[inline]
#[target_feature(enable = "neon")]
unsafe fn argmax(values: &[f32]) -> usize {
    // SAFETY: the caller guarantees NEON support. Pointer bounds are
    // documented at each memory access below.
    unsafe {
        let offsets_data = [0_u32, 1, 2, 3];
        // SAFETY: the local array contains four u32 lanes.
        let offsets = vld1q_u32(offsets_data.as_ptr());
        let mut maxima = vdupq_n_f32(f32::NEG_INFINITY);
        let mut best_indices = vdupq_n_u32(0);
        let mut index = 0;
        while index + VECTOR_WIDTH <= values.len() {
            // SAFETY: the loop condition leaves four readable values.
            let value = vld1q_f32(values.as_ptr().add(index));
            if !finite_input(value) {
                return super::argmax_scalar(values);
            }
            let better = vcgtq_f32(value, maxima);
            maxima = vbslq_f32(better, value, maxima);
            let indices = vaddq_u32(vdupq_n_u32(index as u32), offsets);
            best_indices = vbslq_u32(better, indices, best_indices);
            index += VECTOR_WIDTH;
        }

        let mut lane_maxima = [0.0_f32; VECTOR_WIDTH];
        let mut lane_indices = [0_u32; VECTOR_WIDTH];
        // SAFETY: each output array contains four writable lanes.

        vst1q_f32(lane_maxima.as_mut_ptr(), maxima);
        vst1q_u32(lane_indices.as_mut_ptr(), best_indices);

        let mut max = lane_maxima[0];
        let mut best = lane_indices[0] as usize;
        for lane in 1..VECTOR_WIDTH {
            if lane_maxima[lane] > max
                || (lane_maxima[lane] == max && (lane_indices[lane] as usize) < best)
            {
                max = lane_maxima[lane];
                best = lane_indices[lane] as usize;
            }
        }
        for (tail_index, &value) in values[index..].iter().enumerate() {
            if !value.is_finite() {
                return super::argmax_scalar(values);
            }
            let class = index + tail_index;
            if value > max {
                max = value;
                best = class;
            }
        }
        best
    }
}

#[cfg(test)]
mod tests;