jxl-encoder-simd 0.3.0

SIMD-accelerated primitives for jxl-encoder
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
// Copyright (c) Imazen LLC and the JPEG XL Project Authors.
// Algorithms and constants derived from libjxl (BSD-3-Clause).
// Licensed under AGPL-3.0-or-later. Commercial licenses at https://www.imazen.io/pricing

//! SIMD-accelerated chroma-from-luma (CfL) dot product computation.
//!
//! `find_best_multiplier`: least-squares fitting of integer CfL coefficient.
//! Inner loop is a dual dot product (sum_aa, sum_ab) over up to 4096 elements.

const K_INV_COLOR_FACTOR: f32 = 1.0 / 84.0;

/// Bias towards zero and quantize to i8 (libjxl enc_chroma_from_luma.cc:176-183).
///
/// Small CfL factors (within ±2.6 of zero) are snapped to zero to reduce
/// oscillations in the CfL map. Larger factors are shifted towards zero by 2.6.
#[inline(always)]
fn bias_and_quantize(x: f32) -> i8 {
    const TOWARDS_ZERO: f32 = 2.6;
    let biased = if x >= TOWARDS_ZERO {
        x - TOWARDS_ZERO
    } else if x <= -TOWARDS_ZERO {
        x + TOWARDS_ZERO
    } else {
        0.0
    };
    biased.round().clamp(-128.0, 127.0) as i8
}

/// Newton's method constants.
///
/// eps=1 (not 100) gives accurate local second derivatives, enabling
/// convergence. libjxl uses eps=100 which causes oscillation on most tiles
/// (see CFL_NEWTON_CONVERGENCE_BUG.md in the libjxl repo).
///
/// `NEWTON_EPS` and `NEWTON_MAX_ITERS` are defaults; callers can override
/// via function parameters to tune CfL fitting precision vs. convergence.
pub const NEWTON_EPS_DEFAULT: f32 = 1.0;
pub const NEWTON_MAX_ITERS_DEFAULT: usize = 10;
const NEWTON_CLAMP: f32 = 20.0;
const NEWTON_COEFF: f32 = 1.0 / 3.0;
const NEWTON_THRES: f32 = 100.0;
const NEWTON_STABILIZER: f32 = 0.85;
const NEWTON_CONVERGENCE: f32 = 3e-3;

/// Find the best integer CfL multiplier via regularized least-squares.
///
/// Computes: `x = -sum_ab / (sum_aa + num * distance_mul * 0.5)`
/// where `sum_aa = sum(a_i^2)`, `sum_ab = sum(a_i * b_i)`,
/// `a_i = values_m[i] / 84`, `b_i = base * values_m[i] - values_s[i]`.
pub fn find_best_multiplier(
    values_m: &[f32],
    values_s: &[f32],
    num: usize,
    base: f32,
    distance_mul: f32,
) -> i8 {
    #[cfg(target_arch = "x86_64")]
    {
        use archmage::SimdToken;
        if let Some(token) = archmage::X64V3Token::summon() {
            return find_best_multiplier_avx2(token, values_m, values_s, num, base, distance_mul);
        }
    }

    #[cfg(target_arch = "aarch64")]
    {
        use archmage::SimdToken;
        if let Some(token) = archmage::NeonToken::summon() {
            return find_best_multiplier_neon(token, values_m, values_s, num, base, distance_mul);
        }
    }

    #[cfg(target_arch = "wasm32")]
    {
        use archmage::SimdToken;
        if let Some(token) = archmage::Wasm128Token::summon() {
            return find_best_multiplier_wasm128(
                token,
                values_m,
                values_s,
                num,
                base,
                distance_mul,
            );
        }
    }

    find_best_multiplier_scalar(values_m, values_s, num, base, distance_mul)
}

pub fn find_best_multiplier_scalar(
    values_m: &[f32],
    values_s: &[f32],
    num: usize,
    base: f32,
    distance_mul: f32,
) -> i8 {
    if num == 0 {
        return 0;
    }
    let mut sum_aa = 0.0_f32;
    let mut sum_ab = 0.0_f32;
    for i in 0..num {
        let a = K_INV_COLOR_FACTOR * values_m[i];
        let b = base * values_m[i] - values_s[i];
        sum_aa += a * a;
        sum_ab += a * b;
    }
    let x = -sum_ab / (sum_aa + num as f32 * distance_mul * 0.5);
    bias_and_quantize(x)
}

#[cfg(target_arch = "x86_64")]
#[archmage::arcane]
pub fn find_best_multiplier_avx2(
    token: archmage::X64V3Token,
    values_m: &[f32],
    values_s: &[f32],
    num: usize,
    base: f32,
    distance_mul: f32,
) -> i8 {
    use magetypes::simd::f32x8;

    if num == 0 {
        return 0;
    }

    let inv_cf = f32x8::splat(token, K_INV_COLOR_FACTOR);
    let base_v = f32x8::splat(token, base);
    let mut acc_aa = f32x8::splat(token, 0.0);
    let mut acc_ab = f32x8::splat(token, 0.0);

    let simd_end = num & !7;
    let mut i = 0;
    while i < simd_end {
        let m = crate::load_f32x8(token, values_m, i);
        let s = crate::load_f32x8(token, values_s, i);
        let a = inv_cf * m;
        let b = base_v * m - s;
        acc_aa = a.mul_add(a, acc_aa);
        acc_ab = a.mul_add(b, acc_ab);
        i += 8;
    }

    // Horizontal reduction
    let aa_arr: [f32; 8] = acc_aa.into();
    let ab_arr: [f32; 8] = acc_ab.into();
    let mut sum_aa: f32 = aa_arr.iter().sum();
    let mut sum_ab: f32 = ab_arr.iter().sum();

    // Scalar remainder
    while i < num {
        let a = K_INV_COLOR_FACTOR * values_m[i];
        let b = base * values_m[i] - values_s[i];
        sum_aa += a * a;
        sum_ab += a * b;
        i += 1;
    }

    let x = -sum_ab / (sum_aa + num as f32 * distance_mul * 0.5);
    bias_and_quantize(x)
}

#[cfg(target_arch = "aarch64")]
#[archmage::arcane]
pub fn find_best_multiplier_neon(
    token: archmage::NeonToken,
    values_m: &[f32],
    values_s: &[f32],
    num: usize,
    base: f32,
    distance_mul: f32,
) -> i8 {
    use magetypes::simd::f32x4;

    if num == 0 {
        return 0;
    }

    let inv_cf = f32x4::splat(token, K_INV_COLOR_FACTOR);
    let base_v = f32x4::splat(token, base);
    let mut acc_aa = f32x4::splat(token, 0.0);
    let mut acc_ab = f32x4::splat(token, 0.0);

    let simd_end = num & !3;
    let mut i = 0;
    while i < simd_end {
        let m = f32x4::from_slice(token, &values_m[i..]);
        let s = f32x4::from_slice(token, &values_s[i..]);
        let a = inv_cf * m;
        let b = base_v * m - s;
        acc_aa = a.mul_add(a, acc_aa);
        acc_ab = a.mul_add(b, acc_ab);
        i += 4;
    }

    let aa_arr: [f32; 4] = acc_aa.into();
    let ab_arr: [f32; 4] = acc_ab.into();
    let mut sum_aa: f32 = aa_arr.iter().sum();
    let mut sum_ab: f32 = ab_arr.iter().sum();

    while i < num {
        let a = K_INV_COLOR_FACTOR * values_m[i];
        let b = base * values_m[i] - values_s[i];
        sum_aa += a * a;
        sum_ab += a * b;
        i += 1;
    }

    let x = -sum_ab / (sum_aa + num as f32 * distance_mul * 0.5);
    bias_and_quantize(x)
}

#[cfg(target_arch = "wasm32")]
#[archmage::arcane]
pub fn find_best_multiplier_wasm128(
    token: archmage::Wasm128Token,
    values_m: &[f32],
    values_s: &[f32],
    num: usize,
    base: f32,
    distance_mul: f32,
) -> i8 {
    use magetypes::simd::f32x4;

    if num == 0 {
        return 0;
    }

    let inv_cf = f32x4::splat(token, K_INV_COLOR_FACTOR);
    let base_v = f32x4::splat(token, base);
    let mut acc_aa = f32x4::splat(token, 0.0);
    let mut acc_ab = f32x4::splat(token, 0.0);

    let simd_end = num & !3;
    let mut i = 0;
    while i < simd_end {
        let m = f32x4::from_slice(token, &values_m[i..]);
        let s = f32x4::from_slice(token, &values_s[i..]);
        let a = inv_cf * m;
        let b = base_v * m - s;
        acc_aa = a.mul_add(a, acc_aa);
        acc_ab = a.mul_add(b, acc_ab);
        i += 4;
    }

    let aa_arr: [f32; 4] = acc_aa.into();
    let ab_arr: [f32; 4] = acc_ab.into();
    let mut sum_aa: f32 = aa_arr.iter().sum();
    let mut sum_ab: f32 = ab_arr.iter().sum();

    while i < num {
        let a = K_INV_COLOR_FACTOR * values_m[i];
        let b = base * values_m[i] - values_s[i];
        sum_aa += a * a;
        sum_ab += a * b;
        i += 1;
    }

    let x = -sum_ab / (sum_aa + num as f32 * distance_mul * 0.5);
    bias_and_quantize(x)
}

/// Find the best integer CfL multiplier via Newton's method with perceptual cost.
///
/// Uses the cost function: `1/3 * sum((|ax+b| + 1)^2 - 1) + distance_mul * x^2 * num`
/// where `a = values_m[i] / 84`, `b = base * values_m[i] - values_s[i]`.
///
/// Newton iterations use central finite differences for the second derivative.
/// Large residuals (|ax+b| >= 100) are clipped (ignored) for robustness.
///
/// libjxl uses this at effort >= 7 (speed_tier <= kSquirrel).
/// At effort 5-6, the fast (least-squares) path is used instead.
pub fn find_best_multiplier_newton(
    values_m: &[f32],
    values_s: &[f32],
    num: usize,
    base: f32,
    distance_mul: f32,
    eps: f32,
    max_iters: usize,
) -> i8 {
    #[cfg(target_arch = "x86_64")]
    {
        use archmage::SimdToken;
        if let Some(token) = archmage::X64V3Token::summon() {
            return find_best_multiplier_newton_avx2(
                token,
                values_m,
                values_s,
                num,
                base,
                distance_mul,
                eps,
                max_iters,
            );
        }
    }

    #[cfg(target_arch = "aarch64")]
    {
        use archmage::SimdToken;
        if let Some(token) = archmage::NeonToken::summon() {
            return find_best_multiplier_newton_neon(
                token,
                values_m,
                values_s,
                num,
                base,
                distance_mul,
                eps,
                max_iters,
            );
        }
    }

    find_best_multiplier_newton_scalar(values_m, values_s, num, base, distance_mul, eps, max_iters)
}

/// Scalar Newton's method for CfL multiplier.
///
/// Seeds Newton from the least-squares solution (warm start) so it begins
/// near the optimum. With eps=1, Newton refines the LS solution toward the
/// smoothed-L1 optimum in a few iterations.
///
/// Falls back to LS if Newton doesn't converge (rare with eps=1 + warm start).
pub fn find_best_multiplier_newton_scalar(
    values_m: &[f32],
    values_s: &[f32],
    num: usize,
    base: f32,
    distance_mul: f32,
    eps: f32,
    max_iters: usize,
) -> i8 {
    if num == 0 {
        return 0;
    }

    // Compute LS solution as starting point for Newton.
    let mut sum_aa = 0.0_f32;
    let mut sum_ab = 0.0_f32;
    for i in 0..num {
        let a = K_INV_COLOR_FACTOR * values_m[i];
        let b = base * values_m[i] - values_s[i];
        sum_aa += a * a;
        sum_ab += a * b;
    }
    let ls_x = -sum_ab / (sum_aa + num as f32 * distance_mul * 0.5);

    let coeffx2 = NEWTON_COEFF * 2.0;
    let mut x = ls_x;
    let mut converged = false;

    for _ in 0..max_iters {
        let mut fd = 2.0 * distance_mul * num as f32 * x;
        let mut fd_pe = 2.0 * distance_mul * num as f32 * (x + eps);
        let mut fd_me = 2.0 * distance_mul * num as f32 * (x - eps);

        for i in 0..num {
            let a = K_INV_COLOR_FACTOR * values_m[i];
            let b = base * values_m[i] - values_s[i];

            let v = a * x + b;
            let vpe = a * (x + eps) + b;
            let vme = a * (x - eps) + b;

            let av = v.abs();
            let avpe = vpe.abs();
            let avme = vme.abs();

            let acoeffx2 = coeffx2 * a;

            let mut d = acoeffx2 * (av + 1.0);
            let mut dpe = acoeffx2 * (avpe + 1.0);
            let mut dme = acoeffx2 * (avme + 1.0);

            // Sign flip for negative residuals
            if v < 0.0 {
                d = -d;
            }
            if vpe < 0.0 {
                dpe = -dpe;
            }
            if vme < 0.0 {
                dme = -dme;
            }

            // Threshold clipping: ignore large residuals
            if av < NEWTON_THRES {
                fd += d;
            }
            if avpe < NEWTON_THRES {
                fd_pe += dpe;
            }
            if avme < NEWTON_THRES {
                fd_me += dme;
            }
        }

        // Second derivative via central difference
        let ddf = (fd_pe - fd_me) / (2.0 * eps);
        let step = fd / (ddf + NEWTON_STABILIZER);
        x -= step.clamp(-NEWTON_CLAMP, NEWTON_CLAMP);

        if step.abs() < NEWTON_CONVERGENCE {
            converged = true;
            break;
        }
    }

    if converged {
        bias_and_quantize(x)
    } else {
        // Newton didn't converge — fall back to LS
        bias_and_quantize(ls_x)
    }
}

#[cfg(target_arch = "x86_64")]
#[allow(clippy::too_many_arguments)]
#[archmage::arcane]
pub fn find_best_multiplier_newton_avx2(
    token: archmage::X64V3Token,
    values_m: &[f32],
    values_s: &[f32],
    num: usize,
    base: f32,
    distance_mul: f32,
    eps: f32,
    max_iters: usize,
) -> i8 {
    use magetypes::simd::f32x8;

    if num == 0 {
        return 0;
    }

    let inv_cf = f32x8::splat(token, K_INV_COLOR_FACTOR);
    let base_v = f32x8::splat(token, base);
    let coeffx2_v = f32x8::splat(token, NEWTON_COEFF * 2.0);
    let one = f32x8::splat(token, 1.0);
    let zero = f32x8::splat(token, 0.0);
    let thres_v = f32x8::splat(token, NEWTON_THRES);

    let simd_end = num & !7;

    // Compute LS solution as starting point for Newton (reuses SIMD loop).
    let mut acc_aa = f32x8::splat(token, 0.0);
    let mut acc_ab = f32x8::splat(token, 0.0);
    let mut i = 0;
    while i < simd_end {
        let m = crate::load_f32x8(token, values_m, i);
        let s = crate::load_f32x8(token, values_s, i);
        let a = inv_cf * m;
        let b = base_v * m - s;
        acc_aa = a.mul_add(a, acc_aa);
        acc_ab = a.mul_add(b, acc_ab);
        i += 8;
    }
    let mut sum_aa: f32 = acc_aa.reduce_add();
    let mut sum_ab: f32 = acc_ab.reduce_add();
    while i < num {
        let a = K_INV_COLOR_FACTOR * values_m[i];
        let b = base * values_m[i] - values_s[i];
        sum_aa += a * a;
        sum_ab += a * b;
        i += 1;
    }
    let ls_x = -sum_ab / (sum_aa + num as f32 * distance_mul * 0.5);

    let mut x = ls_x;
    let mut converged = false;

    for _ in 0..max_iters {
        let x_v = f32x8::splat(token, x);
        let xpe_v = f32x8::splat(token, x + eps);
        let xme_v = f32x8::splat(token, x - eps);

        let mut acc_fd = f32x8::splat(token, 0.0);
        let mut acc_fdpe = f32x8::splat(token, 0.0);
        let mut acc_fdme = f32x8::splat(token, 0.0);

        let mut i = 0;
        while i < simd_end {
            let m = f32x8::from_slice(token, &values_m[i..]);
            let s = f32x8::from_slice(token, &values_s[i..]);
            let a = inv_cf * m;
            let b = base_v * m - s;

            let v = a.mul_add(x_v, b);
            let vpe = a.mul_add(xpe_v, b);
            let vme = a.mul_add(xme_v, b);

            let av = v.abs();
            let avpe = vpe.abs();
            let avme = vme.abs();

            let acoeffx2 = coeffx2_v * a;
            let d_unsigned = acoeffx2 * (av + one);
            let dpe_unsigned = acoeffx2 * (avpe + one);
            let dme_unsigned = acoeffx2 * (avme + one);

            // Sign flip: if v < 0 then -d else d (matches libjxl IfThenElse)
            let neg_v = v.simd_lt(zero);
            let neg_vpe = vpe.simd_lt(zero);
            let neg_vme = vme.simd_lt(zero);
            let d = f32x8::blend(neg_v, zero - d_unsigned, d_unsigned);
            let dpe = f32x8::blend(neg_vpe, zero - dpe_unsigned, dpe_unsigned);
            let dme = f32x8::blend(neg_vme, zero - dme_unsigned, dme_unsigned);

            // Threshold: zero out when |v| >= THRES
            let above = av.simd_ge(thres_v);
            let above_pe = avpe.simd_ge(thres_v);
            let above_me = avme.simd_ge(thres_v);
            acc_fd += f32x8::blend(above, zero, d);
            acc_fdpe += f32x8::blend(above_pe, zero, dpe);
            acc_fdme += f32x8::blend(above_me, zero, dme);

            i += 8;
        }

        let mut fd: f32 = 2.0 * distance_mul * num as f32 * x + acc_fd.reduce_add();
        let mut fd_pe: f32 = 2.0 * distance_mul * num as f32 * (x + eps) + acc_fdpe.reduce_add();
        let mut fd_me: f32 = 2.0 * distance_mul * num as f32 * (x - eps) + acc_fdme.reduce_add();

        // Scalar tail
        while i < num {
            let a = K_INV_COLOR_FACTOR * values_m[i];
            let b = base * values_m[i] - values_s[i];
            let v = a * x + b;
            let vpe = a * (x + eps) + b;
            let vme = a * (x - eps) + b;
            let av = v.abs();
            let avpe = vpe.abs();
            let avme = vme.abs();
            let acoeffx2 = NEWTON_COEFF * 2.0 * a;
            let mut d = acoeffx2 * (av + 1.0);
            let mut dpe = acoeffx2 * (avpe + 1.0);
            let mut dme = acoeffx2 * (avme + 1.0);
            if v < 0.0 {
                d = -d;
            }
            if vpe < 0.0 {
                dpe = -dpe;
            }
            if vme < 0.0 {
                dme = -dme;
            }
            if av < NEWTON_THRES {
                fd += d;
            }
            if avpe < NEWTON_THRES {
                fd_pe += dpe;
            }
            if avme < NEWTON_THRES {
                fd_me += dme;
            }
            i += 1;
        }

        let ddf = (fd_pe - fd_me) / (2.0 * eps);
        let step = fd / (ddf + NEWTON_STABILIZER);
        x -= step.clamp(-NEWTON_CLAMP, NEWTON_CLAMP);

        if step.abs() < NEWTON_CONVERGENCE {
            converged = true;
            break;
        }
    }

    if converged {
        bias_and_quantize(x)
    } else {
        // Newton didn't converge — fall back to LS (already computed)
        bias_and_quantize(ls_x)
    }
}

#[cfg(target_arch = "aarch64")]
#[allow(clippy::too_many_arguments)]
#[archmage::arcane]
pub fn find_best_multiplier_newton_neon(
    token: archmage::NeonToken,
    values_m: &[f32],
    values_s: &[f32],
    num: usize,
    base: f32,
    distance_mul: f32,
    eps: f32,
    max_iters: usize,
) -> i8 {
    use magetypes::simd::f32x4;

    if num == 0 {
        return 0;
    }

    let inv_cf = f32x4::splat(token, K_INV_COLOR_FACTOR);
    let base_v = f32x4::splat(token, base);
    let coeffx2_v = f32x4::splat(token, NEWTON_COEFF * 2.0);
    let one = f32x4::splat(token, 1.0);
    let zero = f32x4::splat(token, 0.0);
    let thres_v = f32x4::splat(token, NEWTON_THRES);

    let simd_end = num & !3;

    // Compute LS solution as starting point for Newton (reuses SIMD loop).
    let mut acc_aa = f32x4::splat(token, 0.0);
    let mut acc_ab = f32x4::splat(token, 0.0);
    let mut i = 0;
    while i < simd_end {
        let m = f32x4::from_slice(token, &values_m[i..]);
        let s = f32x4::from_slice(token, &values_s[i..]);
        let a = inv_cf * m;
        let b = base_v * m - s;
        acc_aa = a.mul_add(a, acc_aa);
        acc_ab = a.mul_add(b, acc_ab);
        i += 4;
    }
    let mut sum_aa: f32 = acc_aa.reduce_add();
    let mut sum_ab: f32 = acc_ab.reduce_add();
    while i < num {
        let a = K_INV_COLOR_FACTOR * values_m[i];
        let b = base * values_m[i] - values_s[i];
        sum_aa += a * a;
        sum_ab += a * b;
        i += 1;
    }
    let ls_x = -sum_ab / (sum_aa + num as f32 * distance_mul * 0.5);

    let mut x = ls_x;
    let mut converged = false;

    for _ in 0..max_iters {
        let x_v = f32x4::splat(token, x);
        let xpe_v = f32x4::splat(token, x + eps);
        let xme_v = f32x4::splat(token, x - eps);

        let mut acc_fd = f32x4::splat(token, 0.0);
        let mut acc_fdpe = f32x4::splat(token, 0.0);
        let mut acc_fdme = f32x4::splat(token, 0.0);

        let mut i = 0;
        while i < simd_end {
            let m = f32x4::from_slice(token, &values_m[i..]);
            let s = f32x4::from_slice(token, &values_s[i..]);
            let a = inv_cf * m;
            let b = base_v * m - s;

            let v = a.mul_add(x_v, b);
            let vpe = a.mul_add(xpe_v, b);
            let vme = a.mul_add(xme_v, b);

            let av = v.abs();
            let avpe = vpe.abs();
            let avme = vme.abs();

            let acoeffx2 = coeffx2_v * a;
            let d_unsigned = acoeffx2 * (av + one);
            let dpe_unsigned = acoeffx2 * (avpe + one);
            let dme_unsigned = acoeffx2 * (avme + one);

            let neg_v = v.simd_lt(zero);
            let neg_vpe = vpe.simd_lt(zero);
            let neg_vme = vme.simd_lt(zero);
            let d = f32x4::blend(neg_v, zero - d_unsigned, d_unsigned);
            let dpe = f32x4::blend(neg_vpe, zero - dpe_unsigned, dpe_unsigned);
            let dme = f32x4::blend(neg_vme, zero - dme_unsigned, dme_unsigned);

            let above = av.simd_ge(thres_v);
            let above_pe = avpe.simd_ge(thres_v);
            let above_me = avme.simd_ge(thres_v);
            acc_fd += f32x4::blend(above, zero, d);
            acc_fdpe += f32x4::blend(above_pe, zero, dpe);
            acc_fdme += f32x4::blend(above_me, zero, dme);

            i += 4;
        }

        let mut fd: f32 = 2.0 * distance_mul * num as f32 * x + acc_fd.reduce_add();
        let mut fd_pe: f32 = 2.0 * distance_mul * num as f32 * (x + eps) + acc_fdpe.reduce_add();
        let mut fd_me: f32 = 2.0 * distance_mul * num as f32 * (x - eps) + acc_fdme.reduce_add();

        while i < num {
            let a = K_INV_COLOR_FACTOR * values_m[i];
            let b = base * values_m[i] - values_s[i];
            let v = a * x + b;
            let vpe = a * (x + eps) + b;
            let vme = a * (x - eps) + b;
            let av = v.abs();
            let avpe = vpe.abs();
            let avme = vme.abs();
            let acoeffx2 = NEWTON_COEFF * 2.0 * a;
            let mut d = acoeffx2 * (av + 1.0);
            let mut dpe = acoeffx2 * (avpe + 1.0);
            let mut dme = acoeffx2 * (avme + 1.0);
            if v < 0.0 {
                d = -d;
            }
            if vpe < 0.0 {
                dpe = -dpe;
            }
            if vme < 0.0 {
                dme = -dme;
            }
            if av < NEWTON_THRES {
                fd += d;
            }
            if avpe < NEWTON_THRES {
                fd_pe += dpe;
            }
            if avme < NEWTON_THRES {
                fd_me += dme;
            }
            i += 1;
        }

        let ddf = (fd_pe - fd_me) / (2.0 * eps);
        let step = fd / (ddf + NEWTON_STABILIZER);
        x -= step.clamp(-NEWTON_CLAMP, NEWTON_CLAMP);

        if step.abs() < NEWTON_CONVERGENCE {
            converged = true;
            break;
        }
    }

    if converged {
        bias_and_quantize(x)
    } else {
        // Newton didn't converge — fall back to LS (already computed)
        bias_and_quantize(ls_x)
    }
}

#[cfg(test)]
mod tests {
    extern crate std;
    use super::*;

    #[test]
    fn test_find_best_multiplier_scalar_vs_dispatch() {
        let num = 256;
        let values_m: alloc::vec::Vec<f32> = (0..num).map(|i| (i as f32 - 128.0) * 0.1).collect();
        let values_s: alloc::vec::Vec<f32> =
            (0..num).map(|i| (i as f32 - 128.0) * 0.05 + 0.3).collect();

        let ref0 = find_best_multiplier_scalar(&values_m, &values_s, num, 0.0, 1e-3);
        let ref1 = find_best_multiplier_scalar(&values_m, &values_s, num, 1.0, 1e-3);

        let report = archmage::testing::for_each_token_permutation(
            archmage::testing::CompileTimePolicy::Warn,
            |perm| {
                let test0 = find_best_multiplier(&values_m, &values_s, num, 0.0, 1e-3);
                assert_eq!(
                    ref0, test0,
                    "base=0.0: scalar={ref0} dispatch={test0} [{perm}]"
                );

                let test1 = find_best_multiplier(&values_m, &values_s, num, 1.0, 1e-3);
                assert_eq!(
                    ref1, test1,
                    "base=1.0: scalar={ref1} dispatch={test1} [{perm}]"
                );
            },
        );
        std::eprintln!("{report}");
    }

    #[test]
    fn test_find_best_multiplier_empty() {
        assert_eq!(find_best_multiplier(&[], &[], 0, 0.0, 1e-3), 0);
    }

    #[test]
    fn test_find_best_multiplier_correlated() {
        let factor = 42.0_f32;
        let m: alloc::vec::Vec<f32> = (0..64).map(|i| (i as f32 - 32.0) * 10.0).collect();
        let s: alloc::vec::Vec<f32> = m.iter().map(|&v| factor / 84.0 * v).collect();
        let result = find_best_multiplier(&m, &s, 64, 0.0, 1e-3);
        // Optimization produces ~42.0, towards_zero bias subtracts 2.6 → ~39.4 → rounds to 39
        let expected = (factor - 2.6).round() as i8;
        assert!(
            (result as f32 - expected as f32).abs() < 2.0,
            "Expected ~{expected} (factor {factor} - 2.6 bias), got {result}"
        );
    }

    #[test]
    fn test_newton_empty() {
        assert_eq!(
            find_best_multiplier_newton(
                &[],
                &[],
                0,
                0.0,
                1e-9,
                NEWTON_EPS_DEFAULT,
                NEWTON_MAX_ITERS_DEFAULT,
            ),
            0,
        );
    }

    #[test]
    fn test_newton_scalar_vs_dispatch() {
        let num = 256;
        let values_m: alloc::vec::Vec<f32> = (0..num).map(|i| (i as f32 - 128.0) * 0.1).collect();
        let values_s: alloc::vec::Vec<f32> =
            (0..num).map(|i| (i as f32 - 128.0) * 0.05 + 0.3).collect();

        let eps = NEWTON_EPS_DEFAULT;
        let iters = NEWTON_MAX_ITERS_DEFAULT;

        let ref0 =
            find_best_multiplier_newton_scalar(&values_m, &values_s, num, 0.0, 1e-9, eps, iters);
        let ref1 =
            find_best_multiplier_newton_scalar(&values_m, &values_s, num, 1.0, 1e-9, eps, iters);

        let report = archmage::testing::for_each_token_permutation(
            archmage::testing::CompileTimePolicy::Warn,
            |perm| {
                let test0 =
                    find_best_multiplier_newton(&values_m, &values_s, num, 0.0, 1e-9, eps, iters);
                assert_eq!(
                    ref0, test0,
                    "newton base=0.0: scalar={ref0} dispatch={test0} [{perm}]"
                );

                let test1 =
                    find_best_multiplier_newton(&values_m, &values_s, num, 1.0, 1e-9, eps, iters);
                assert_eq!(
                    ref1, test1,
                    "newton base=1.0: scalar={ref1} dispatch={test1} [{perm}]"
                );
            },
        );
        std::eprintln!("{report}");
    }

    #[test]
    fn test_newton_correlated() {
        // With a strong correlation, Newton should find a similar result to least-squares
        let factor = 42.0_f32;
        let m: alloc::vec::Vec<f32> = (0..64).map(|i| (i as f32 - 32.0) * 10.0).collect();
        let s: alloc::vec::Vec<f32> = m.iter().map(|&v| factor / 84.0 * v).collect();

        let ls_result = find_best_multiplier(&m, &s, 64, 0.0, 1e-9);
        let eps = NEWTON_EPS_DEFAULT;
        let iters = NEWTON_MAX_ITERS_DEFAULT;
        let newton_result = find_best_multiplier_newton(&m, &s, 64, 0.0, 1e-9, eps, iters);

        // Both should be in the right ballpark (Newton uses perceptual cost, not MSE)
        let expected = (factor - 2.6).round() as i8;
        assert!(
            (newton_result as f32 - expected as f32).abs() <= 3.0,
            "Newton expected ~{expected}, got {newton_result}"
        );
        // Newton and LS should agree within ±3 for well-conditioned data
        assert!(
            (newton_result as i16 - ls_result as i16).unsigned_abs() <= 3,
            "Newton={newton_result} vs LS={ls_result} differ by more than 3"
        );
    }
}