Repository navigation
Expand file tree
/
Copy pathsimd_ops.rs
More file actions
1427 lines (1318 loc) · 54.5 KB
/
Copy pathsimd_ops.rs
File metadata and controls
1427 lines (1318 loc) · 54.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Slice-level elementwise ops built on the polyfill SIMD types.
//!
//! Every function uses `crate::simd::F32x16` (or the appropriate type),
//! which is already dispatched: AVX-512 → AVX2 → NEON → scalar.
//! These ops inherit that dispatch — no platform-specific code here.
//!
//! Re-exported flat through `ndarray::simd::add_f32`, etc.
//!
//! # ⚠️ Foundation primitives — do not remove ⚠️
//!
//! `array_chunks`, `array_chunks_checked`, `array_windows`,
//! `array_windows_checked`, `add_mul_f32`, and `add_mul_f64` are the
//! stable-Rust building blocks the BLAS-graph GEMM hand-rolled kernels
//! lean on. Combined with the polyfilled `F32x16::mul_add` / `F64x8::mul_add`
//! (defined on the SIMD types in `src/simd_*.rs`), they reach within a few
//! percent of a Cranelift-JIT'd inner loop on the bgz17 path — the reason
//! the JIT-native option was deemed unnecessary and dropped. **Prior
//! sessions have removed these helpers under the impression they were
//! unused cruft; the truth is they are the polyfill foundation. Do not
//! delete them without verifying every BLAS-graph kernel still compiles
//! AND that the JIT alternative has been re-evaluated.**
use crate::simd::{bf16_to_f32_batch, F32x16, F64x8};
// ═══════════════════════════════════════════════════════════════════
// f32 binary ops (out-of-place)
// ═══════════════════════════════════════════════════════════════════
/// Elementwise add: `out[i] = a[i] + b[i]`.
pub fn add_f32(a: &[f32], b: &[f32]) -> Vec<f32> {
binary_f32(a, b, |x, y| x + y, |x, y| x + y)
}
/// Elementwise subtract: `out[i] = a[i] - b[i]`.
pub fn sub_f32(a: &[f32], b: &[f32]) -> Vec<f32> {
binary_f32(a, b, |x, y| x - y, |x, y| x - y)
}
/// Elementwise multiply: `out[i] = a[i] * b[i]`.
pub fn mul_f32(a: &[f32], b: &[f32]) -> Vec<f32> {
binary_f32(a, b, |x, y| x * y, |x, y| x * y)
}
/// Elementwise divide: `out[i] = a[i] / b[i]`.
pub fn div_f32(a: &[f32], b: &[f32]) -> Vec<f32> {
binary_f32(a, b, |x, y| x / y, |x, y| x / y)
}
// ═══════════════════════════════════════════════════════════════════
// f32 inplace ops
// ═══════════════════════════════════════════════════════════════════
/// Inplace add: `dst[i] += src[i]`.
pub fn add_f32_inplace(dst: &mut [f32], src: &[f32]) {
inplace_f32(dst, src, |d, s| d + s, |d, s| *d += s)
}
/// Inplace subtract: `dst[i] -= src[i]`.
pub fn sub_f32_inplace(dst: &mut [f32], src: &[f32]) {
inplace_f32(dst, src, |d, s| d - s, |d, s| *d -= s)
}
/// Inplace multiply: `dst[i] *= src[i]`.
pub fn mul_f32_inplace(dst: &mut [f32], src: &[f32]) {
inplace_f32(dst, src, |d, s| d * s, |d, s| *d *= s)
}
/// Inplace divide: `dst[i] /= src[i]`.
pub fn div_f32_inplace(dst: &mut [f32], src: &[f32]) {
inplace_f32(dst, src, |d, s| d / s, |d, s| *d /= s)
}
// ═══════════════════════════════════════════════════════════════════
// f32 scalar ops
// ═══════════════════════════════════════════════════════════════════
/// Scalar multiply: `out[i] = a[i] * scalar`.
pub fn scale_f32(a: &[f32], scalar: f32) -> Vec<f32> {
let s = F32x16::splat(scalar);
let n = a.len();
let mut out = vec![0.0f32; n];
let mut i = 0;
while i + 16 <= n {
(F32x16::from_slice(&a[i..]) * s).copy_to_slice(&mut out[i..]);
i += 16;
}
while i < n {
out[i] = a[i] * scalar;
i += 1;
}
out
}
/// Scalar add: `out[i] = a[i] + scalar`.
pub fn add_scalar_f32(a: &[f32], scalar: f32) -> Vec<f32> {
let s = F32x16::splat(scalar);
let n = a.len();
let mut out = vec![0.0f32; n];
let mut i = 0;
while i + 16 <= n {
(F32x16::from_slice(&a[i..]) + s).copy_to_slice(&mut out[i..]);
i += 16;
}
while i < n {
out[i] = a[i] + scalar;
i += 1;
}
out
}
/// Inplace scalar multiply: `a[i] *= scalar`.
pub fn scale_f32_inplace(a: &mut [f32], scalar: f32) {
let s = F32x16::splat(scalar);
let n = a.len();
let mut i = 0;
while i + 16 <= n {
(F32x16::from_slice(&a[i..]) * s).copy_to_slice(&mut a[i..]);
i += 16;
}
while i < n {
a[i] *= scalar;
i += 1;
}
}
// ═══════════════════════════════════════════════════════════════════
// Fused multiply-add into accumulator slice
// ═══════════════════════════════════════════════════════════════════
//
// `add_mul_*` is the slice-level FMA-into-accumulator: `acc[i] += a[i] * b[i]`.
// Built directly on `F32x16::mul_add` / `F64x8::mul_add`, the polyfilled
// FMA primitives on the SIMD types (AVX-512: vfmadd231ps; AVX2 + FMA:
// _mm256_fmadd_ps; NEON: vfmaq_f32; wasm simd128: exact emulation; scalar
// and targets without FMA: the exact scalar FMA), and on [`mul_add_f32`] /
// [`mul_add_f64`] for the tails. Single rounding step on every backend —
// same semantic as BLAS-1 `axpy` with a vector multiplier, and the dominant
// inner-loop shape in the BLAS-graph GEMM kernels.
/// Scalar fused multiply-add `a * b + c`, rounded once, on every backend.
///
/// The FMA instruction where the target has one at compile time
/// (`target_feature = "fma"` on x86, aarch64); otherwise an exact emulation
/// (no libm call, no run-time CPU dispatch). Use this instead of
/// `f32::mul_add` in code that must behave the same on every polyfill arm.
///
/// # Examples
///
/// ```
/// use ndarray::simd::mul_add_f32;
/// let x = 1.0f32 + f32::EPSILON * 2048.0; // x * x = 1 + 2^-11 + 2^-24 exactly
/// assert_eq!(mul_add_f32(x, x, -(1.0 + f32::EPSILON * 4096.0)), f32::EPSILON / 2.0);
/// ```
#[inline(always)]
pub fn mul_add_f32(a: f32, b: f32, c: f32) -> f32 {
crate::simd_fma::ExactFma::exact_fma(a, b, c)
}
/// f64 sibling of [`mul_add_f32`].
///
/// # Examples
///
/// ```
/// use ndarray::simd::mul_add_f64;
/// let x = 1.0f64 + f64::EPSILON * 2f64.powi(25); // x * x = 1 + 2^-26 + 2^-54
/// assert_eq!(mul_add_f64(x, x, -(1.0 + f64::EPSILON * 2f64.powi(26))), 2f64.powi(-54));
/// ```
#[inline(always)]
pub fn mul_add_f64(a: f64, b: f64, c: f64) -> f64 {
crate::simd_fma::ExactFma::exact_fma(a, b, c)
}
/// Fused multiply-add into accumulator: `acc[i] += a[i] * b[i]`.
///
/// Operates on the prefix `min(acc.len(), a.len(), b.len())` lanes.
/// Each lane is computed with a single rounding step via the polyfilled
/// `F32x16::mul_add` (hardware FMA where available), and [`mul_add_f32`]
/// for the tail — never less precise than the manual
/// `acc[i] += a[i] * b[i]` two-step.
///
/// # Examples
///
/// ```
/// use ndarray::simd::add_mul_f32;
/// let mut acc = vec![1.0f32; 17];
/// let a = vec![2.0f32; 17];
/// let b = vec![3.0f32; 17];
/// add_mul_f32(&mut acc, &a, &b);
/// assert!(acc.iter().all(|&v| (v - 7.0).abs() < 1e-6));
/// ```
#[inline]
pub fn add_mul_f32(acc: &mut [f32], a: &[f32], b: &[f32]) {
let n = acc.len().min(a.len()).min(b.len());
let mut i = 0;
while i + 16 <= n {
let va = F32x16::from_slice(&a[i..]);
let vb = F32x16::from_slice(&b[i..]);
let vacc = F32x16::from_slice(&acc[i..]);
va.mul_add(vb, vacc).copy_to_slice(&mut acc[i..]);
i += 16;
}
while i < n {
acc[i] = mul_add_f32(a[i], b[i], acc[i]);
i += 1;
}
}
/// Fused multiply-add into accumulator (f64): `acc[i] += a[i] * b[i]`.
///
/// f64 sibling of [`add_mul_f32`]. Uses `F64x8::mul_add` (8-wide on
/// AVX-512 / 4-wide on AVX2-FMA / 2-wide on NEON / [`mul_add_f64`] otherwise).
#[inline]
pub fn add_mul_f64(acc: &mut [f64], a: &[f64], b: &[f64]) {
let n = acc.len().min(a.len()).min(b.len());
let mut i = 0;
while i + 8 <= n {
let va = F64x8::from_slice(&a[i..]);
let vb = F64x8::from_slice(&b[i..]);
let vacc = F64x8::from_slice(&acc[i..]);
va.mul_add(vb, vacc).copy_to_slice(&mut acc[i..]);
i += 8;
}
while i < n {
acc[i] = mul_add_f64(a[i], b[i], acc[i]);
i += 1;
}
}
// ═══════════════════════════════════════════════════════════════════
// f64 binary ops
// ═══════════════════════════════════════════════════════════════════
/// Elementwise add f64: `out[i] = a[i] + b[i]`.
pub fn add_f64(a: &[f64], b: &[f64]) -> Vec<f64> {
binary_f64(a, b, |x, y| x + y, |x, y| x + y)
}
/// Elementwise multiply f64: `out[i] = a[i] * b[i]`.
pub fn mul_f64(a: &[f64], b: &[f64]) -> Vec<f64> {
binary_f64(a, b, |x, y| x * y, |x, y| x * y)
}
/// Inplace add f64: `dst[i] += src[i]`.
pub fn add_f64_inplace(dst: &mut [f64], src: &[f64]) {
inplace_f64(dst, src, |d, s| d + s, |d, s| *d += s)
}
// ═══════════════════════════════════════════════════════════════════
// Internal dispatch helpers
// ═══════════════════════════════════════════════════════════════════
#[inline]
fn binary_f32(
a: &[f32], b: &[f32], simd_op: impl Fn(F32x16, F32x16) -> F32x16, scalar_op: impl Fn(f32, f32) -> f32,
) -> Vec<f32> {
let n = a.len().min(b.len());
let mut out = vec![0.0f32; n];
let mut i = 0;
while i + 16 <= n {
simd_op(F32x16::from_slice(&a[i..]), F32x16::from_slice(&b[i..])).copy_to_slice(&mut out[i..]);
i += 16;
}
while i < n {
out[i] = scalar_op(a[i], b[i]);
i += 1;
}
out
}
#[inline]
fn inplace_f32(
dst: &mut [f32], src: &[f32], simd_op: impl Fn(F32x16, F32x16) -> F32x16, scalar_op: impl Fn(&mut f32, f32),
) {
let n = dst.len().min(src.len());
let mut i = 0;
while i + 16 <= n {
simd_op(F32x16::from_slice(&dst[i..]), F32x16::from_slice(&src[i..])).copy_to_slice(&mut dst[i..]);
i += 16;
}
while i < n {
scalar_op(&mut dst[i], src[i]);
i += 1;
}
}
#[inline]
fn binary_f64(
a: &[f64], b: &[f64], simd_op: impl Fn(F64x8, F64x8) -> F64x8, scalar_op: impl Fn(f64, f64) -> f64,
) -> Vec<f64> {
let n = a.len().min(b.len());
let mut out = vec![0.0f64; n];
let mut i = 0;
while i + 8 <= n {
simd_op(F64x8::from_slice(&a[i..]), F64x8::from_slice(&b[i..])).copy_to_slice(&mut out[i..]);
i += 8;
}
while i < n {
out[i] = scalar_op(a[i], b[i]);
i += 1;
}
out
}
#[inline]
fn inplace_f64(
dst: &mut [f64], src: &[f64], simd_op: impl Fn(F64x8, F64x8) -> F64x8, scalar_op: impl Fn(&mut f64, f64),
) {
let n = dst.len().min(src.len());
let mut i = 0;
while i + 8 <= n {
simd_op(F64x8::from_slice(&dst[i..]), F64x8::from_slice(&src[i..])).copy_to_slice(&mut dst[i..]);
i += 8;
}
while i < n {
scalar_op(&mut dst[i], src[i]);
i += 1;
}
}
// ═══════════════════════════════════════════════════════════════════
// Tests
// ═══════════════════════════════════════════════════════════════════
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn add_f32_aligned() {
let a = vec![1.0f32; 32];
let b = vec![2.0f32; 32];
let c = add_f32(&a, &b);
assert!(c.iter().all(|&v| (v - 3.0).abs() < 1e-6));
}
#[test]
fn add_f32_misaligned_tail() {
let a = vec![1.0f32; 33];
let b = vec![2.0f32; 33];
let c = add_f32(&a, &b);
assert_eq!(c.len(), 33);
assert!(c.iter().all(|&v| (v - 3.0).abs() < 1e-6));
}
#[test]
fn mul_f32_inplace_works() {
let mut dst = vec![2.0f32; 17];
let src = vec![3.0f32; 17];
mul_f32_inplace(&mut dst, &src);
assert!(dst.iter().all(|&v| (v - 6.0).abs() < 1e-6));
}
#[test]
fn scale_f32_works() {
let a = vec![4.0f32; 35];
let b = scale_f32(&a, 0.5);
assert!(b.iter().all(|&v| (v - 2.0).abs() < 1e-6));
}
#[test]
fn scale_f32_inplace_works() {
let mut a = vec![10.0f32; 19];
scale_f32_inplace(&mut a, 0.1);
assert!(a.iter().all(|&v| (v - 1.0).abs() < 1e-5));
}
#[test]
fn add_scalar_f32_works() {
let a = vec![1.0f32; 20];
let b = add_scalar_f32(&a, 99.0);
assert!(b.iter().all(|&v| (v - 100.0).abs() < 1e-6));
}
#[test]
fn sub_f32_works() {
let c = sub_f32(&[5.0; 3], &[2.0; 3]);
assert!(c.iter().all(|&v| (v - 3.0).abs() < 1e-6));
}
#[test]
fn div_f32_works() {
let c = div_f32(&[6.0; 4], &[3.0; 4]);
assert!(c.iter().all(|&v| (v - 2.0).abs() < 1e-6));
}
#[test]
fn add_f64_works() {
let c = add_f64(&[1.0f64; 17], &[2.0f64; 17]);
assert_eq!(c.len(), 17);
assert!(c.iter().all(|&v| (v - 3.0).abs() < 1e-12));
}
#[test]
fn empty_slices() {
assert!(add_f32(&[], &[]).is_empty());
assert!(mul_f32(&[], &[]).is_empty());
assert!(scale_f32(&[], 2.0).is_empty());
}
#[test]
fn mismatched_lengths_takes_min() {
let c = add_f32(&[1.0; 10], &[2.0; 5]);
assert_eq!(c.len(), 5);
}
}
// ════════════════════════════════════════════════════════════════════
// PR-X1 — Const-size slice window helpers (non-overlapping + overlapping)
// ════════════════════════════════════════════════════════════════════
//
// Two stable-Rust helpers for SIMD-staged inner loops:
//
// - `array_chunks::<N>` — **non-overlapping** `&[T; N]` iterator
// (matches `std::slice::ArrayChunks` / stable `slice::as_chunks`).
// - `array_windows::<N>` — **overlapping** `&[T; N]` iterator
// (matches nightly `std::slice::array_windows::<N>()`; this file
// ships the stable equivalent until the std API lands).
//
// Both surface compile-time-fixed window sizes so call sites can
// directly feed `F32x16::from_array` / `F32x8::from_array` etc. and
// let the compiler infer the lane count. The pair lets BLAS-graph
// GEMM-style kernels iterate over a row of `B` (overlapping windows
// of the inner K dimension) and a column of `A` (non-overlapping
// chunks of the M dimension) in one source — the polyfill type
// resolves both to native SIMD width per target.
/// Walk `data` as a sequence of non-overlapping const-size windows.
///
/// Returns an iterator over `&[T; N]` references into `data`. The tail
/// (`data.len() % N` items) is discarded; use [`array_chunks_checked`] to
/// fail-fast when the length is not a multiple of `N`.
///
/// Zero-cost: thin wrapper around [`slice::as_chunks`] that pins the
/// chunk size at the call site for type inference.
///
/// # Examples
///
/// ```
/// use ndarray::simd::array_chunks;
/// let data: Vec<u8> = (0..16).collect();
/// let windows: Vec<&[u8; 4]> = array_chunks::<u8, 4>(&data).collect();
/// assert_eq!(windows.len(), 4);
/// assert_eq!(windows[0], &[0, 1, 2, 3]);
/// assert_eq!(windows[3], &[12, 13, 14, 15]);
/// ```
///
/// # Examples — tail discarded
///
/// ```
/// use ndarray::simd::array_chunks;
/// let data: Vec<u8> = (0..7).collect();
/// let windows: Vec<&[u8; 4]> = array_chunks::<u8, 4>(&data).collect();
/// assert_eq!(windows.len(), 1);
/// ```
#[inline]
pub fn array_chunks<T, const N: usize>(data: &[T]) -> impl Iterator<Item = &[T; N]> + '_ {
data.as_chunks::<N>().0.iter()
}
/// Walk `data` as `&[T; N]` windows, returning `Err(())` if `data.len()`
/// is not a multiple of `N`.
///
/// Strict variant of [`array_chunks`]: the consumer asserts up front that
/// the buffer is lane-aligned and wants the error surfaced rather than
/// silently truncating.
///
/// # Edge case — `N == 0`
///
/// Returns `Err(())` when `N == 0`. The underlying `slice::as_chunks::<0>`
/// would panic on a zero divisor; the strict-fallible contract here folds
/// that into the existing error path so callers never see an unexpected
/// panic on the checked surface.
///
/// # Examples
///
/// ```
/// use ndarray::simd::array_chunks_checked;
/// let data: Vec<u8> = (0..16).collect();
/// let it = array_chunks_checked::<u8, 4>(&data).expect("16 is a multiple of 4");
/// assert_eq!(it.count(), 4);
///
/// let bad: Vec<u8> = (0..7).collect();
/// assert!(array_chunks_checked::<u8, 4>(&bad).is_err());
///
/// // N == 0 surfaces as Err rather than panicking.
/// assert!(array_chunks_checked::<u8, 0>(&[0u8; 8]).is_err());
/// ```
#[inline]
#[allow(clippy::result_unit_err)] // matches PR-X1 design § 3 `Result<_, ()>` contract; no error variants needed
pub fn array_chunks_checked<T, const N: usize>(data: &[T]) -> Result<impl Iterator<Item = &[T; N]> + '_, ()> {
if N == 0 {
// `data.len() % 0` would panic; the strict-fallible contract
// folds N==0 into Err. See P2 review on PR #167.
return Err(());
}
if !data.len().is_multiple_of(N) {
return Err(());
}
Ok(array_chunks::<T, N>(data))
}
/// Walk `data` as a sequence of **overlapping** const-size windows.
///
/// Stable-Rust equivalent of nightly `std::slice::array_windows::<N>()`.
/// For input length `L`, yields `L.saturating_sub(N - 1)` windows; each
/// window slides forward by exactly one element.
///
/// Zero-cost: wraps `slice::windows(N)` with a `TryInto` array-ref
/// conversion that the compiler proves infallible (the inner slice is
/// guaranteed length `N`).
///
/// # Edge case — `N == 0`
///
/// `slice::windows(0)` panics. To preserve a panic-free surface, this
/// helper returns an empty iterator when `N == 0`. Strict-fallible
/// callers should use [`array_windows_checked`].
///
/// # Examples
///
/// ```
/// use ndarray::simd::array_windows;
/// let data: Vec<u8> = (0..6).collect();
/// let ws: Vec<&[u8; 4]> = array_windows::<u8, 4>(&data).collect();
/// assert_eq!(ws.len(), 3); // 6 - 4 + 1 = 3 overlapping windows
/// assert_eq!(ws[0], &[0, 1, 2, 3]);
/// assert_eq!(ws[1], &[1, 2, 3, 4]);
/// assert_eq!(ws[2], &[2, 3, 4, 5]);
/// ```
///
/// # Examples — fewer elements than `N`
///
/// ```
/// use ndarray::simd::array_windows;
/// let data: Vec<u8> = (0..3).collect();
/// let ws: Vec<&[u8; 4]> = array_windows::<u8, 4>(&data).collect();
/// assert_eq!(ws.len(), 0);
/// ```
#[inline]
pub fn array_windows<T, const N: usize>(data: &[T]) -> impl Iterator<Item = &[T; N]> + '_ {
// Index-based iteration sidesteps `slice::windows(0)`'s panic — when
// N == 0 the count below evaluates to 0 and the iterator is empty.
let count = if N == 0 || data.len() < N {
0
} else {
data.len() - N + 1
};
(0..count).map(move |i| {
// `&data[i..i + N]` is exactly N elements by construction (bounds
// checked once when `count` was computed). `try_into` always
// succeeds on the inner conversion; the optimizer folds the
// length check out and lowers this to a pointer-cast.
<&[T; N]>::try_from(&data[i..i + N]).expect("computed window length == N")
})
}
/// Walk `data` as `&[T; N]` overlapping windows, returning `Err(())` if
/// the buffer is shorter than `N` or `N == 0`.
///
/// Strict variant of [`array_windows`]: the consumer asserts up front
/// that the slice is long enough for at least one window and wants the
/// error surfaced rather than receiving an empty iterator. Useful for
/// kernels that pre-pad to `K + N - 1` and want to fail loudly if the
/// pad got dropped.
///
/// # Edge case — `N == 0`
///
/// Returns `Err(())`. The unchecked variant would silently produce an
/// empty iterator; the checked surface makes the misuse explicit.
///
/// # Examples
///
/// ```
/// use ndarray::simd::array_windows_checked;
/// let data: Vec<u8> = (0..6).collect();
/// let it = array_windows_checked::<u8, 4>(&data).expect("6 >= 4");
/// assert_eq!(it.count(), 3);
///
/// let too_short: Vec<u8> = (0..3).collect();
/// assert!(array_windows_checked::<u8, 4>(&too_short).is_err());
///
/// // N == 0 surfaces as Err rather than yielding an empty iterator.
/// assert!(array_windows_checked::<u8, 0>(&[0u8; 8]).is_err());
/// ```
#[inline]
#[allow(clippy::result_unit_err)] // matches PR-X1 design § 3 `Result<_, ()>` contract; no error variants needed
pub fn array_windows_checked<T, const N: usize>(data: &[T]) -> Result<impl Iterator<Item = &[T; N]> + '_, ()> {
if N == 0 || data.len() < N {
return Err(());
}
Ok(array_windows::<T, N>(data))
}
/// BF16 16×16 tile GEMM: `C[16,16] += A[16,K] · B[K,16]` where `A`, `B` are
/// BF16 row-major (`u16` bit patterns) and `C` is f32 row-major. `K` must be a
/// multiple of 32.
///
/// Pure SIMD-polyfill kernel: decodes BF16→f32 and accumulates with
/// [`F32x16::mul_add`] (FMA), so it rides the polyfill's per-arch escalation —
/// AVX-512 `VFMADD231PS` where available, the emulated `(F32x8, F32x8)` pair on
/// AVX2, NEON on aarch64, scalar otherwise. The `F32x16` wrapper owns the
/// dispatch; there is no AMX intrinsic and no external BLAS backend.
///
/// Accumulates into `c` (`+=`), matching BLAS `C := A·B + C`. Inputs are
/// decoded BF16→f32, so the result matches an f32-accumulated scalar reference
/// up to BF16 input precision (~2⁻⁸ per multiply, O(√K) accumulated).
///
/// # Panics
/// Panics if `k % 32 != 0`, `a_bf16.len() != 16 * k`, `b_bf16.len() != k * 16`,
/// or `c.len() != 256`.
///
/// # Examples
/// ```
/// use ndarray::simd::{bf16_tile_gemm_16x16, f32_to_bf16_scalar};
/// let k = 32;
/// let a = vec![f32_to_bf16_scalar(1.0); 16 * k];
/// let b = vec![f32_to_bf16_scalar(1.0); k * 16];
/// let mut c = vec![0.0f32; 256];
/// bf16_tile_gemm_16x16(&a, &b, &mut c, k);
/// assert!((c[0] - k as f32).abs() < 1e-3); // Σ_{p<k} 1·1 = k
/// ```
pub fn bf16_tile_gemm_16x16(a_bf16: &[u16], b_bf16: &[u16], c: &mut [f32], k: usize) {
assert_eq!(k % 32, 0, "K must be a multiple of 32");
assert_eq!(a_bf16.len(), 16 * k, "A must be 16xK BF16 row-major");
assert_eq!(b_bf16.len(), k * 16, "B must be Kx16 BF16 row-major");
assert_eq!(c.len(), 16 * 16, "C must be 16x16 f32 row-major");
// Decode BF16 -> f32 once; the batch decode rides the polyfill too.
let mut a_f32 = vec![0.0f32; a_bf16.len()];
let mut b_f32 = vec![0.0f32; b_bf16.len()];
bf16_to_f32_batch(a_bf16, &mut a_f32);
bf16_to_f32_batch(b_bf16, &mut b_f32);
// C[i,j] += dot(A row i, B col j) via F32x16 + FMA on 16-wide chunks.
// K is a multiple of 32, so chunks_exact(16) tiles A's row / B's column
// with no remainder.
for i in 0..16 {
let a_row = &a_f32[i * k..i * k + k];
for j in 0..16 {
// Gather B column j (row-major B is strided by 16) into a buffer so
// the dot product hits the contiguous chunks_exact(16) fast path.
let mut col = vec![0.0f32; k];
for (kk, slot) in col.iter_mut().enumerate() {
*slot = b_f32[kk * 16 + j];
}
let mut acc = F32x16::splat(0.0);
for (ra, rb) in a_row
.as_chunks::<16>()
.0
.iter()
.zip(col.as_chunks::<16>().0)
{
acc = F32x16::from_slice(ra).mul_add(F32x16::from_slice(rb), acc);
}
c[i * 16 + j] += acc.reduce_sum();
}
}
}
#[cfg(test)]
mod bf16_tile_gemm_tests {
use super::*;
use crate::simd::{bf16_to_f32_batch, f32_to_bf16_batch, f32_to_bf16_scalar};
/// Scalar BF16 reference (f32-accumulated, `+=`) — the correctness anchor.
fn ref_gemm(a: &[f32], b: &[f32], c: &mut [f32], k: usize) {
for i in 0..16 {
for j in 0..16 {
let mut s = 0.0f32;
for kk in 0..k {
s += a[i * k + kk] * b[kk * 16 + j];
}
c[i * 16 + j] += s;
}
}
}
#[test]
fn matches_scalar_reference_k64() {
let k = 64;
let mut a_f32 = vec![0.0f32; 16 * k];
let mut b_f32 = vec![0.0f32; k * 16];
for (i, v) in a_f32.iter_mut().enumerate() {
*v =
(((i as i32).wrapping_mul(1103515245).wrapping_add(12345) >> 8) as f32 / 2147483648.0).clamp(-1.0, 1.0);
}
for (i, v) in b_f32.iter_mut().enumerate() {
*v = (((i as i32).wrapping_mul(69069).wrapping_add(1) >> 8) as f32 / 2147483648.0).clamp(-1.0, 1.0);
}
let mut a_bf16 = vec![0u16; a_f32.len()];
let mut b_bf16 = vec![0u16; b_f32.len()];
f32_to_bf16_batch(&a_f32, &mut a_bf16);
f32_to_bf16_batch(&b_f32, &mut b_bf16);
// Reference consumes the same BF16-truncated inputs the kernel sees.
let mut a_back = vec![0.0f32; a_f32.len()];
let mut b_back = vec![0.0f32; b_f32.len()];
bf16_to_f32_batch(&a_bf16, &mut a_back);
bf16_to_f32_batch(&b_bf16, &mut b_back);
let mut c_ref = vec![0.0f32; 256];
ref_gemm(&a_back, &b_back, &mut c_ref, k);
let mut c = vec![0.0f32; 256];
bf16_tile_gemm_16x16(&a_bf16, &b_bf16, &mut c, k);
let max_err = c
.iter()
.zip(&c_ref)
.map(|(x, y)| (x - y).abs())
.fold(0.0f32, f32::max);
assert!(max_err < 1e-3, "polyfill vs scalar ref max_err = {max_err}");
}
#[test]
fn accumulates_into_c() {
// C is preloaded with 5.0; the kernel must ADD, not overwrite.
let k = 32;
let a = vec![f32_to_bf16_scalar(1.0); 16 * k];
let b = vec![f32_to_bf16_scalar(1.0); k * 16];
let mut c = vec![5.0f32; 256];
bf16_tile_gemm_16x16(&a, &b, &mut c, k);
// 5 (preload) + Σ_{p<32} 1·1 = 37
for v in &c {
assert!((v - 37.0).abs() < 1e-3, "got {v}");
}
}
}
#[cfg(test)]
mod array_chunks_tests {
use super::*;
#[test]
fn array_chunks_4_over_16() {
let data: Vec<u8> = (0u8..16).collect();
let windows: Vec<&[u8; 4]> = array_chunks::<u8, 4>(&data).collect();
assert_eq!(windows.len(), 4);
assert_eq!(windows[0], &[0, 1, 2, 3]);
assert_eq!(windows[1], &[4, 5, 6, 7]);
assert_eq!(windows[2], &[8, 9, 10, 11]);
assert_eq!(windows[3], &[12, 13, 14, 15]);
}
#[test]
fn array_chunks_drops_tail() {
let data: Vec<u8> = (0u8..7).collect();
let windows: Vec<&[u8; 4]> = array_chunks::<u8, 4>(&data).collect();
assert_eq!(windows.len(), 1);
assert_eq!(windows[0], &[0, 1, 2, 3]);
}
#[test]
fn array_chunks_checked_rejects_mismatch() {
assert!(array_chunks_checked::<u8, 4>(&[0u8; 7]).is_err());
assert!(array_chunks_checked::<u8, 4>(&[0u8; 5]).is_err());
assert!(array_chunks_checked::<u8, 4>(&[0u8; 1]).is_err());
}
#[test]
fn array_chunks_checked_accepts_aligned() {
let data = [0u8; 16];
let it = array_chunks_checked::<u8, 4>(&data).expect("16 is a multiple of 4");
assert_eq!(it.count(), 4);
}
#[test]
fn array_chunks_empty_buffer() {
assert_eq!(array_chunks::<u8, 4>(&[]).count(), 0);
let it = array_chunks_checked::<u8, 4>(&[]).expect("0 % 4 == 0, should be Ok");
assert_eq!(it.count(), 0);
}
#[test]
fn array_chunks_checked_rejects_zero_n() {
// P2 review fix on PR #167: N == 0 must surface as Err, not panic
// via `data.len() % 0`.
assert!(array_chunks_checked::<u8, 0>(&[]).is_err());
assert!(array_chunks_checked::<u8, 0>(&[0u8; 8]).is_err());
assert!(array_chunks_checked::<u32, 0>(&[1u32, 2, 3]).is_err());
}
// ── array_windows (overlapping) ──────────────────────────────────
#[test]
fn array_windows_4_over_6() {
let data: Vec<u8> = (0u8..6).collect();
let ws: Vec<&[u8; 4]> = array_windows::<u8, 4>(&data).collect();
assert_eq!(ws.len(), 3);
assert_eq!(ws[0], &[0, 1, 2, 3]);
assert_eq!(ws[1], &[1, 2, 3, 4]);
assert_eq!(ws[2], &[2, 3, 4, 5]);
}
#[test]
fn array_windows_short_buffer_empty_iter() {
let data: Vec<u8> = (0u8..3).collect();
let ws: Vec<&[u8; 4]> = array_windows::<u8, 4>(&data).collect();
assert_eq!(ws.len(), 0);
}
#[test]
fn array_windows_exact_n_yields_one() {
let data: [u8; 4] = [10, 20, 30, 40];
let ws: Vec<&[u8; 4]> = array_windows::<u8, 4>(&data).collect();
assert_eq!(ws.len(), 1);
assert_eq!(ws[0], &[10, 20, 30, 40]);
}
#[test]
fn array_windows_empty_buffer() {
let data: [u8; 0] = [];
let ws: Vec<&[u8; 4]> = array_windows::<u8, 4>(&data).collect();
assert_eq!(ws.len(), 0);
}
#[test]
fn array_windows_n_zero_yields_empty() {
// N == 0 returns empty iter from the unchecked variant — matches
// slice::windows(0) panic-avoidance contract documented above.
let data: [u8; 8] = [1; 8];
let ws: Vec<&[u8; 0]> = array_windows::<u8, 0>(&data).collect();
assert_eq!(ws.len(), 0);
}
#[test]
fn array_windows_checked_accepts_long_enough() {
let data: [u8; 8] = [0, 1, 2, 3, 4, 5, 6, 7];
let it = array_windows_checked::<u8, 4>(&data).expect("8 >= 4");
let ws: Vec<&[u8; 4]> = it.collect();
assert_eq!(ws.len(), 5);
assert_eq!(ws[4], &[4, 5, 6, 7]);
}
#[test]
fn array_windows_checked_rejects_short_buffer() {
assert!(array_windows_checked::<u8, 4>(&[0u8; 3]).is_err());
assert!(array_windows_checked::<u8, 4>(&[0u8; 0]).is_err());
}
#[test]
fn array_windows_checked_rejects_zero_n() {
assert!(array_windows_checked::<u8, 0>(&[0u8; 8]).is_err());
assert!(array_windows_checked::<u8, 0>(&[]).is_err());
}
}
// ════════════════════════════════════════════════════════════════════
// FMA-into-accumulator tests
// ════════════════════════════════════════════════════════════════════
#[cfg(test)]
mod add_mul_tests {
use super::*;
fn ref_add_mul_f32(acc: &[f32], a: &[f32], b: &[f32]) -> Vec<f32> {
acc.iter()
.zip(a)
.zip(b)
.map(|((&c, &x), &y)| x.mul_add(y, c))
.collect()
}
fn ref_add_mul_f64(acc: &[f64], a: &[f64], b: &[f64]) -> Vec<f64> {
acc.iter()
.zip(a)
.zip(b)
.map(|((&c, &x), &y)| x.mul_add(y, c))
.collect()
}
#[test]
fn add_mul_f32_aligned() {
let mut acc = vec![1.0f32; 32];
let a = vec![2.0f32; 32];
let b = vec![3.0f32; 32];
let expected = ref_add_mul_f32(&acc, &a, &b);
add_mul_f32(&mut acc, &a, &b);
for (got, want) in acc.iter().zip(expected.iter()) {
assert!((got - want).abs() < 1e-5, "got={got}, want={want}");
}
}
#[test]
fn add_mul_f32_tail() {
// 17 elements — exercises 16-wide chunk + 1-element scalar tail.
for &len in &[0usize, 1, 15, 16, 17, 31, 32, 33, 100] {
let mut acc: Vec<f32> = (0..len).map(|i| i as f32 + 0.5).collect();
let a: Vec<f32> = (0..len).map(|i| (i as f32) * 0.1).collect();
let b: Vec<f32> = (0..len).map(|i| (i as f32) * 0.2 - 0.3).collect();
let expected = ref_add_mul_f32(&acc, &a, &b);
add_mul_f32(&mut acc, &a, &b);
for (i, (got, want)) in acc.iter().zip(expected.iter()).enumerate() {
assert!((got - want).abs() < 1e-4, "len={len} idx={i}: got={got} want={want}");
}
}
}
#[test]
fn add_mul_f32_mismatched_lengths_takes_min() {
let mut acc = vec![0.0f32; 5];
let a = vec![1.0f32; 10];
let b = vec![2.0f32; 3];
add_mul_f32(&mut acc, &a, &b);
// First 3 lanes accumulate; remaining 2 unchanged.
assert_eq!(&acc[..3], &[2.0, 2.0, 2.0]);
assert_eq!(&acc[3..], &[0.0, 0.0]);
}
#[test]
fn add_mul_f64_aligned_and_tail() {
for &len in &[0usize, 1, 7, 8, 9, 16, 17, 50] {
let mut acc: Vec<f64> = (0..len).map(|i| i as f64 + 0.25).collect();
let a: Vec<f64> = (0..len).map(|i| (i as f64) * 0.5).collect();
let b: Vec<f64> = (0..len).map(|i| (i as f64) * 0.7 - 1.0).collect();
let expected = ref_add_mul_f64(&acc, &a, &b);
add_mul_f64(&mut acc, &a, &b);
for (i, (got, want)) in acc.iter().zip(expected.iter()).enumerate() {
assert!((got - want).abs() < 1e-10, "len={len} idx={i}: got={got} want={want}");
}
}
}
}
// ═══════════════════════════════════════════════════════════════════
// Tiled f64 GEMM — the crate-native, bit-exact reference GEMM
// ═══════════════════════════════════════════════════════════════════
/// Tiled GEMM: `C := alpha·A·B + beta·C` (f64, row-major, leading dims).
///
/// The crate-native f64 GEMM: a TILE=64-blocked loop nest with the
/// innermost j-run vectorized on the dispatched [`F64x8`] (AVX-512 /
/// AVX2 / NEON / WASM-SIMD128 / scalar — one source, every backend).
/// Entirely in-crate with a bit-exactness contract, so probes can
/// certify other kernels against it as ground truth — and since it IS
/// the engine behind `backend::native::gemm_f64`, the native f64 BLAS
/// path carries the same auditable numerics (the f32 sibling still
/// delegates to the external `matrixmultiply` crate).
///
/// # Bit-exactness contract
///
/// Every `C[i,j]` receives the exact IEEE op sequence
/// `c = c + (alpha·A[i,p])·B[p,j]` in ascending-`p` order, with the
/// multiply and add **unfused** (the `*`/`+` operators on the SIMD
/// types lower to plain `mul`/`add` on every backend — never
/// FMA-contracted). The result is therefore **bit-identical across all
/// backends for non-NaN inputs** (NaN-*ness* propagates everywhere,
/// but NaN *payloads* are backend-defined — WASM in particular may
/// canonicalize), and for `alpha == 1.0, beta == 0.0` (the
/// certification shape) bit-identical to the naive triple-loop
/// reference `s = Σ_p A[i,p]·B[p,j]; C[i,j] = s`.
///
/// Edge semantics: `m == 0` or `n == 0` is a no-op (`c` untouched);
/// `k == 0` applies only the beta pass; `beta == 0.0` overwrites `c`
/// without reading it (BLAS convention — pre-existing NaN/Inf in `c`
/// do not propagate); `beta == 1.0` accumulates into `c` as-is;
/// non-finite values in `a`/`b` propagate per IEEE. Unlike BLAS
/// `xGEMM`, `alpha == 0.0` does **not** short-circuit the product
/// term: `a`/`b` are still read and `(±0.0·a)·b` terms are added, so
/// NaN/Inf in `a`/`b` propagate into `c` (`0·Inf = NaN`) and a `-0.0`
/// in `c` can flip to `+0.0`.
///
/// Free-function shape follows the simd-surface GEMM family precedent
/// ([`bf16_tile_gemm_16x16`], `matmul_i8_to_i32`) — an 11-argument
/// slice-level BLAS kernel has no typed-wrapper home (W1a).
///
/// # Panics
///
/// Panics if `lda < k`, `ldb < n`, `ldc < n`, or if `a`, `b`, or `c`
/// is shorter than `(rows − 1)·ld + cols` for its shape (computed
/// overflow-checked). All checks are skipped when `m == 0 || n == 0`
/// (the no-op path), and the `a`/`b` length checks are skipped when
/// `k == 0` (neither is read).
///
/// # Examples
///
/// ```
/// use ndarray::simd::gemm_f64_tiled;
///
/// let a = [1.0, 2.0, 3.0, 4.0]; // 2×2 row-major
/// let b = [5.0, 6.0, 7.0, 8.0]; // 2×2 row-major
/// let mut c = [0.0f64; 4];
/// gemm_f64_tiled(2, 2, 2, 1.0, &a, 2, &b, 2, 0.0, &mut c, 2);
/// assert_eq!(c, [19.0, 22.0, 43.0, 50.0]);
/// ```
// BLAS gemm argument list kept verbatim (drop-in for `backend::native::gemm_f64`).
#[allow(clippy::too_many_arguments)]
pub fn gemm_f64_tiled(
m: usize, n: usize, k: usize, alpha: f64, a: &[f64], lda: usize, b: &[f64], ldb: usize, beta: f64, c: &mut [f64],
ldc: usize,
) {
gemm_f64_tiled_impl::<false>(m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
}
/// Fast tier of [`gemm_f64_tiled`]: same tiling, same ascending-`p`
/// per-element order, but each step is a **fused** multiply-add
/// (`c = fma(alpha·A[i,p], B[p,j], c)` — one rounding per step via
/// `F64x8::mul_add`, and [`mul_add_f64`] for the scalar tail; fused
/// semantics on every backend, though the lowering varies: native
/// `vfmadd` on AVX-512 and AVX2 + FMA, `vfmaq_f64` on NEON, an exact
/// emulation on wasm and on targets built without the `fma` feature:
/// correct, but slower. Prefer the reference tier, or an FMA-capable
/// target pin, in that situation).