tract-linalg 0.23.7

Tiny, no-nonsense, self contained, TensorFlow and ONNX inference
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
#[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
use crate::Scaler;
use crate::mmm::FusedKerSpec;

#[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
unsafe fn kernel_f32_4x4(mut pnl: *const FusedKerSpec<f32>) -> isize {
    use std::arch::wasm32::*;

    unsafe {
        // Each of these variables stores a row of the matrix,
        // consisting of four packed `f32` numbers.
        let mut ab0 = f32x4_splat(0.0);
        let mut ab1 = f32x4_splat(0.0);
        let mut ab2 = f32x4_splat(0.0);
        let mut ab3 = f32x4_splat(0.0);

        while !pnl.is_null() {
            match *pnl {
                FusedKerSpec::Done => break,
                FusedKerSpec::Clear => wasm_set!(f32x4_splat(0.0); ab0, ab1, ab2, ab3),
                FusedKerSpec::LoadTile(_cols, rows) => wasm_load_indexed!(rows; ab0, ab1, ab2, ab3),
                FusedKerSpec::ScalarMin(a) => {
                    wasm_bin_sv!(f32x4_min, f32x4_splat(a); ab0, ab1, ab2, ab3)
                }
                FusedKerSpec::ScalarMax(a) => {
                    wasm_bin_sv!(f32x4_max, f32x4_splat(a); ab0, ab1, ab2, ab3)
                }
                FusedKerSpec::ScalarAdd(a) => {
                    wasm_bin_sv!(f32x4_add, f32x4_splat(a); ab0, ab1, ab2, ab3)
                }
                FusedKerSpec::ScalarMul(a) => {
                    wasm_bin_sv!(f32x4_mul, f32x4_splat(a); ab0, ab1, ab2, ab3)
                }
                FusedKerSpec::ScalarSub(a) => {
                    wasm_bin_sv!(f32x4_sub, f32x4_splat(a); ab0, ab1, ab2, ab3)
                }
                FusedKerSpec::ScalarSubF(a) => {
                    wasm_bin_vs!(f32x4_sub, f32x4_splat(a); ab0, ab1, ab2, ab3)
                }
                FusedKerSpec::LeakyRelu(a) => wasm_leaky_relu!(a; ab0, ab1, ab2, ab3),
                FusedKerSpec::PerRowMin(row) => {
                    wasm_bin_splat_indexed!(f32x4_min, row; ab0, ab1, ab2, ab3)
                }
                FusedKerSpec::PerRowMax(row) => {
                    wasm_bin_splat_indexed!(f32x4_max, row; ab0, ab1, ab2, ab3)
                }
                FusedKerSpec::PerRowAdd(row) => {
                    wasm_bin_splat_indexed!(f32x4_add, row; ab0, ab1, ab2, ab3)
                }
                FusedKerSpec::PerRowMul(row) => {
                    wasm_bin_splat_indexed!(f32x4_mul, row; ab0, ab1, ab2, ab3)
                }
                FusedKerSpec::PerRowSub(row) => {
                    wasm_bin_splat_indexed!(f32x4_sub, row; ab0, ab1, ab2, ab3)
                }
                FusedKerSpec::PerRowSubF(row) => {
                    wasm_bin_splat_indexed_vs!(f32x4_sub, row; ab0, ab1, ab2, ab3)
                }
                FusedKerSpec::PerColMin(cols) => {
                    wasm_bin_sv!(f32x4_min, v128_load(cols as *const v128); ab0, ab1, ab2, ab3)
                }
                FusedKerSpec::PerColMax(cols) => {
                    wasm_bin_sv!(f32x4_max, v128_load(cols as *const v128); ab0, ab1, ab2, ab3)
                }
                FusedKerSpec::PerColAdd(cols) => {
                    wasm_bin_sv!(f32x4_add, v128_load(cols as *const v128); ab0, ab1, ab2, ab3)
                }
                FusedKerSpec::PerColMul(cols) => {
                    wasm_bin_sv!(f32x4_mul, v128_load(cols as *const v128); ab0, ab1, ab2, ab3)
                }
                FusedKerSpec::PerColSub(cols) => {
                    wasm_bin_sv!(f32x4_sub, v128_load(cols as *const v128); ab0, ab1, ab2, ab3)
                }
                FusedKerSpec::PerColSubF(cols) => {
                    wasm_bin_vs!(f32x4_sub, v128_load(cols as *const v128); ab0, ab1, ab2, ab3)
                }
                FusedKerSpec::QScale(shift, rp, mult) => {
                    wasm_bin_sv!(f32x4_mul, f32x4_splat(Scaler::from_fuse_params(shift, rp, mult).scale); ab0, ab1, ab2, ab3)
                }
                FusedKerSpec::RoundingShiftRight(shift, _rp) => {
                    wasm_bin_sv!(f32x4_mul, f32x4_splat(2f32.powi(-(shift as i32))); ab0, ab1, ab2, ab3)
                }
                FusedKerSpec::ShiftLeft(shift) => {
                    wasm_bin_sv!(f32x4_mul, f32x4_splat(2f32.powi(shift as i32)); ab0, ab1, ab2, ab3)
                }
                FusedKerSpec::AddUnicast(tile) => {
                    let mut ptr: *const u8 = tile.ptr;

                    let m0 = *(ptr as *const f32);
                    let m1 = *(ptr.offset(tile.col_byte_stride) as *const f32);
                    let m2 = *(ptr.offset(tile.col_byte_stride * 2) as *const f32);
                    let m3 = *(ptr.offset(tile.col_byte_stride * 3) as *const f32);
                    ab0 = f32x4_add(ab0, f32x4(m0, m1, m2, m3));
                    ptr = ptr.add(tile.row_byte_stride as usize);

                    let m0 = *(ptr as *const f32);
                    let m1 = *(ptr.offset(tile.col_byte_stride) as *const f32);
                    let m2 = *(ptr.offset(tile.col_byte_stride * 2) as *const f32);
                    let m3 = *(ptr.offset(tile.col_byte_stride * 3) as *const f32);
                    ab1 = f32x4_add(ab1, f32x4(m0, m1, m2, m3));
                    ptr = ptr.add(tile.row_byte_stride as usize);

                    let m0 = *(ptr as *const f32);
                    let m1 = *(ptr.offset(tile.col_byte_stride) as *const f32);
                    let m2 = *(ptr.offset(tile.col_byte_stride * 2) as *const f32);
                    let m3 = *(ptr.offset(tile.col_byte_stride * 3) as *const f32);
                    ab2 = f32x4_add(ab2, f32x4(m0, m1, m2, m3));
                    ptr = ptr.add(tile.row_byte_stride as usize);

                    let m0 = *(ptr as *const f32);
                    let m1 = *(ptr.offset(tile.col_byte_stride) as *const f32);
                    let m2 = *(ptr.offset(tile.col_byte_stride * 2) as *const f32);
                    let m3 = *(ptr.offset(tile.col_byte_stride * 3) as *const f32);
                    ab3 = f32x4_add(ab3, f32x4(m0, m1, m2, m3));
                }
                FusedKerSpec::AddRowColProducts(rows, cols) => {
                    let cols = v128_load(cols as *const v128);
                    ab0 = madd_f32x4_nofma!(ab0, f32x4_splat(*rows.add(0)), cols);
                    ab1 = madd_f32x4_nofma!(ab1, f32x4_splat(*rows.add(1)), cols);
                    ab2 = madd_f32x4_nofma!(ab2, f32x4_splat(*rows.add(2)), cols);
                    ab3 = madd_f32x4_nofma!(ab3, f32x4_splat(*rows.add(3)), cols);
                }
                FusedKerSpec::Store(tile) => {
                    let mut ptr: *mut u8 = tile.ptr;

                    *(ptr as *mut f32) = f32x4_extract_lane::<0>(ab0);
                    *(ptr.offset(tile.col_byte_stride) as *mut f32) = f32x4_extract_lane::<1>(ab0);
                    *(ptr.offset(tile.col_byte_stride * 2) as *mut f32) =
                        f32x4_extract_lane::<2>(ab0);
                    *(ptr.offset(tile.col_byte_stride * 3) as *mut f32) =
                        f32x4_extract_lane::<3>(ab0);
                    ptr = ptr.add(tile.row_byte_stride as usize);

                    *(ptr as *mut f32) = f32x4_extract_lane::<0>(ab1);
                    *(ptr.offset(tile.col_byte_stride) as *mut f32) = f32x4_extract_lane::<1>(ab1);
                    *(ptr.offset(tile.col_byte_stride * 2) as *mut f32) =
                        f32x4_extract_lane::<2>(ab1);
                    *(ptr.offset(tile.col_byte_stride * 3) as *mut f32) =
                        f32x4_extract_lane::<3>(ab1);
                    ptr = ptr.add(tile.row_byte_stride as usize);

                    *(ptr as *mut f32) = f32x4_extract_lane::<0>(ab2);
                    *(ptr.offset(tile.col_byte_stride) as *mut f32) = f32x4_extract_lane::<1>(ab2);
                    *(ptr.offset(tile.col_byte_stride * 2) as *mut f32) =
                        f32x4_extract_lane::<2>(ab2);
                    *(ptr.offset(tile.col_byte_stride * 3) as *mut f32) =
                        f32x4_extract_lane::<3>(ab2);
                    ptr = ptr.add(tile.row_byte_stride as usize);

                    *(ptr as *mut f32) = f32x4_extract_lane::<0>(ab3);
                    *(ptr.offset(tile.col_byte_stride) as *mut f32) = f32x4_extract_lane::<1>(ab3);
                    *(ptr.offset(tile.col_byte_stride * 2) as *mut f32) =
                        f32x4_extract_lane::<2>(ab3);
                    *(ptr.offset(tile.col_byte_stride * 3) as *mut f32) =
                        f32x4_extract_lane::<3>(ab3);
                }
                FusedKerSpec::AddMatMul { k, pa, pb, packing: _ } => {
                    let a = pa as *const f32;
                    let b = pb as *const v128;
                    for i in 0..k {
                        let a = std::slice::from_raw_parts(a.offset(4 * i as isize), 4);
                        let b = v128_load(b.offset(i as isize));
                        ab0 = madd_f32x4_nofma!(ab0, f32x4_splat(a[0]), b);
                        ab1 = madd_f32x4_nofma!(ab1, f32x4_splat(a[1]), b);
                        ab2 = madd_f32x4_nofma!(ab2, f32x4_splat(a[2]), b);
                        ab3 = madd_f32x4_nofma!(ab3, f32x4_splat(a[3]), b);
                    }
                }
            }
            pnl = pnl.add(1);
        }
        0
    }
}

// Reachable only by name, never through dispatch: it declares NEVER_PREFERRED, so every
// other wasm f32 kernel outranks it. Kept because it is the only f32 kernel besides 8x8
// whose C tile is two-dimensional, so the generated store and packing tests cover that
// layout on a second shape. `dispatch_never_returns_wasm_f32_4x4` holds this in place.
bail_stub!(wasm32; unsafe fn kernel_f32_4x4(*const FusedKerSpec<f32>) -> isize);

MMMRustKernel!(wasm32; kernel_f32_4x4 => wasm_f32_4x4<f32>(4,4)@(4,4)
    boost(|| crate::isa::NEVER_PREFERRED));

/// WASM SIMD f32 8x8 kernel — wide MM tile (8 rows × 8 cols, 16 v128 accumulators).
/// Each row uses 2 v128: cols 0-3 in `_lo`, cols 4-7 in `_hi`. 16 accumulators
/// is at the limit of WASM's 16 logical SIMD register slots; this tests the
/// register-pressure boundary. For DFN3 ops, all M and N are multiples of 8,
/// so 8x8 fits cleanly with no padding waste.
#[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
unsafe fn kernel_f32_8x8(mut pnl: *const FusedKerSpec<f32>) -> isize {
    use std::arch::wasm32::*;

    unsafe {
        // 8 rows × 8 cols = 16 f32x4 accumulators (cols 0-3 in _lo, cols 4-7 in _hi)
        let mut a0lo = f32x4_splat(0.0);
        let mut a0hi = f32x4_splat(0.0);
        let mut a1lo = f32x4_splat(0.0);
        let mut a1hi = f32x4_splat(0.0);
        let mut a2lo = f32x4_splat(0.0);
        let mut a2hi = f32x4_splat(0.0);
        let mut a3lo = f32x4_splat(0.0);
        let mut a3hi = f32x4_splat(0.0);
        let mut a4lo = f32x4_splat(0.0);
        let mut a4hi = f32x4_splat(0.0);
        let mut a5lo = f32x4_splat(0.0);
        let mut a5hi = f32x4_splat(0.0);
        let mut a6lo = f32x4_splat(0.0);
        let mut a6hi = f32x4_splat(0.0);
        let mut a7lo = f32x4_splat(0.0);
        let mut a7hi = f32x4_splat(0.0);

        while !pnl.is_null() {
            match *pnl {
                FusedKerSpec::Done => break,
                FusedKerSpec::Clear => {
                    wasm_set!(f32x4_splat(0.0); a0lo, a0hi, a1lo, a1hi, a2lo, a2hi, a3lo, a3hi, a4lo, a4hi, a5lo, a5hi, a6lo, a6hi, a7lo, a7hi)
                }
                FusedKerSpec::LoadTile(_cols, rows) => {
                    wasm_load_indexed!(rows; a0lo, a0hi, a1lo, a1hi, a2lo, a2hi, a3lo, a3hi, a4lo, a4hi, a5lo, a5hi, a6lo, a6hi, a7lo, a7hi)
                }
                FusedKerSpec::ScalarMin(a) => {
                    wasm_bin_sv!(f32x4_min, f32x4_splat(a); a0lo, a0hi, a1lo, a1hi, a2lo, a2hi, a3lo, a3hi, a4lo, a4hi, a5lo, a5hi, a6lo, a6hi, a7lo, a7hi)
                }
                FusedKerSpec::ScalarMax(a) => {
                    wasm_bin_sv!(f32x4_max, f32x4_splat(a); a0lo, a0hi, a1lo, a1hi, a2lo, a2hi, a3lo, a3hi, a4lo, a4hi, a5lo, a5hi, a6lo, a6hi, a7lo, a7hi)
                }
                FusedKerSpec::ScalarAdd(a) => {
                    wasm_bin_sv!(f32x4_add, f32x4_splat(a); a0lo, a0hi, a1lo, a1hi, a2lo, a2hi, a3lo, a3hi, a4lo, a4hi, a5lo, a5hi, a6lo, a6hi, a7lo, a7hi)
                }
                FusedKerSpec::ScalarMul(a) => {
                    wasm_bin_sv!(f32x4_mul, f32x4_splat(a); a0lo, a0hi, a1lo, a1hi, a2lo, a2hi, a3lo, a3hi, a4lo, a4hi, a5lo, a5hi, a6lo, a6hi, a7lo, a7hi)
                }
                FusedKerSpec::ScalarSub(a) => {
                    wasm_bin_sv!(f32x4_sub, f32x4_splat(a); a0lo, a0hi, a1lo, a1hi, a2lo, a2hi, a3lo, a3hi, a4lo, a4hi, a5lo, a5hi, a6lo, a6hi, a7lo, a7hi)
                }
                FusedKerSpec::ScalarSubF(a) => {
                    wasm_bin_vs!(f32x4_sub, f32x4_splat(a); a0lo, a0hi, a1lo, a1hi, a2lo, a2hi, a3lo, a3hi, a4lo, a4hi, a5lo, a5hi, a6lo, a6hi, a7lo, a7hi)
                }
                FusedKerSpec::LeakyRelu(a) => {
                    wasm_leaky_relu!(a; a0lo, a0hi, a1lo, a1hi, a2lo, a2hi, a3lo, a3hi, a4lo, a4hi, a5lo, a5hi, a6lo, a6hi, a7lo, a7hi)
                }
                FusedKerSpec::PerRowMin(row) => {
                    wasm_bin_row_pairs!(f32x4_min, row; (a0lo, a0hi), (a1lo, a1hi), (a2lo, a2hi), (a3lo, a3hi), (a4lo, a4hi), (a5lo, a5hi), (a6lo, a6hi), (a7lo, a7hi))
                }
                FusedKerSpec::PerRowMax(row) => {
                    wasm_bin_row_pairs!(f32x4_max, row; (a0lo, a0hi), (a1lo, a1hi), (a2lo, a2hi), (a3lo, a3hi), (a4lo, a4hi), (a5lo, a5hi), (a6lo, a6hi), (a7lo, a7hi))
                }
                FusedKerSpec::PerRowAdd(row) => {
                    wasm_bin_row_pairs!(f32x4_add, row; (a0lo, a0hi), (a1lo, a1hi), (a2lo, a2hi), (a3lo, a3hi), (a4lo, a4hi), (a5lo, a5hi), (a6lo, a6hi), (a7lo, a7hi))
                }
                FusedKerSpec::PerRowMul(row) => {
                    wasm_bin_row_pairs!(f32x4_mul, row; (a0lo, a0hi), (a1lo, a1hi), (a2lo, a2hi), (a3lo, a3hi), (a4lo, a4hi), (a5lo, a5hi), (a6lo, a6hi), (a7lo, a7hi))
                }
                FusedKerSpec::PerRowSub(row) => {
                    wasm_bin_row_pairs!(f32x4_sub, row; (a0lo, a0hi), (a1lo, a1hi), (a2lo, a2hi), (a3lo, a3hi), (a4lo, a4hi), (a5lo, a5hi), (a6lo, a6hi), (a7lo, a7hi))
                }
                FusedKerSpec::PerRowSubF(row) => {
                    wasm_bin_row_pairs_vs!(f32x4_sub, row; (a0lo, a0hi), (a1lo, a1hi), (a2lo, a2hi), (a3lo, a3hi), (a4lo, a4hi), (a5lo, a5hi), (a6lo, a6hi), (a7lo, a7hi))
                }
                FusedKerSpec::PerColMin(cols) => {
                    wasm_bin_col_pairs!(f32x4_min, cols; (a0lo, a0hi), (a1lo, a1hi), (a2lo, a2hi), (a3lo, a3hi), (a4lo, a4hi), (a5lo, a5hi), (a6lo, a6hi), (a7lo, a7hi))
                }
                FusedKerSpec::PerColMax(cols) => {
                    wasm_bin_col_pairs!(f32x4_max, cols; (a0lo, a0hi), (a1lo, a1hi), (a2lo, a2hi), (a3lo, a3hi), (a4lo, a4hi), (a5lo, a5hi), (a6lo, a6hi), (a7lo, a7hi))
                }
                FusedKerSpec::PerColAdd(cols) => {
                    wasm_bin_col_pairs!(f32x4_add, cols; (a0lo, a0hi), (a1lo, a1hi), (a2lo, a2hi), (a3lo, a3hi), (a4lo, a4hi), (a5lo, a5hi), (a6lo, a6hi), (a7lo, a7hi))
                }
                FusedKerSpec::PerColMul(cols) => {
                    wasm_bin_col_pairs!(f32x4_mul, cols; (a0lo, a0hi), (a1lo, a1hi), (a2lo, a2hi), (a3lo, a3hi), (a4lo, a4hi), (a5lo, a5hi), (a6lo, a6hi), (a7lo, a7hi))
                }
                FusedKerSpec::PerColSub(cols) => {
                    wasm_bin_col_pairs!(f32x4_sub, cols; (a0lo, a0hi), (a1lo, a1hi), (a2lo, a2hi), (a3lo, a3hi), (a4lo, a4hi), (a5lo, a5hi), (a6lo, a6hi), (a7lo, a7hi))
                }
                FusedKerSpec::PerColSubF(cols) => {
                    wasm_bin_col_pairs_vs!(f32x4_sub, cols; (a0lo, a0hi), (a1lo, a1hi), (a2lo, a2hi), (a3lo, a3hi), (a4lo, a4hi), (a5lo, a5hi), (a6lo, a6hi), (a7lo, a7hi))
                }
                FusedKerSpec::QScale(shift, rp, mult) => {
                    wasm_bin_sv!(f32x4_mul, f32x4_splat(Scaler::from_fuse_params(shift, rp, mult).scale); a0lo, a0hi, a1lo, a1hi, a2lo, a2hi, a3lo, a3hi, a4lo, a4hi, a5lo, a5hi, a6lo, a6hi, a7lo, a7hi)
                }
                FusedKerSpec::RoundingShiftRight(shift, _rp) => {
                    wasm_bin_sv!(f32x4_mul, f32x4_splat(2f32.powi(-(shift as i32))); a0lo, a0hi, a1lo, a1hi, a2lo, a2hi, a3lo, a3hi, a4lo, a4hi, a5lo, a5hi, a6lo, a6hi, a7lo, a7hi)
                }
                FusedKerSpec::ShiftLeft(shift) => {
                    wasm_bin_sv!(f32x4_mul, f32x4_splat(2f32.powi(shift as i32)); a0lo, a0hi, a1lo, a1hi, a2lo, a2hi, a3lo, a3hi, a4lo, a4hi, a5lo, a5hi, a6lo, a6hi, a7lo, a7hi)
                }
                FusedKerSpec::AddUnicast(tile) => {
                    // 8 rows × 8 cols, each row laid out per col_byte_stride
                    let mut ptr: *const u8 = tile.ptr;
                    for ab_pair in [
                        (&mut a0lo, &mut a0hi),
                        (&mut a1lo, &mut a1hi),
                        (&mut a2lo, &mut a2hi),
                        (&mut a3lo, &mut a3hi),
                        (&mut a4lo, &mut a4hi),
                        (&mut a5lo, &mut a5hi),
                        (&mut a6lo, &mut a6hi),
                        (&mut a7lo, &mut a7hi),
                    ]
                    .iter_mut()
                    {
                        let m0 = *(ptr as *const f32);
                        let m1 = *(ptr.offset(tile.col_byte_stride) as *const f32);
                        let m2 = *(ptr.offset(tile.col_byte_stride * 2) as *const f32);
                        let m3 = *(ptr.offset(tile.col_byte_stride * 3) as *const f32);
                        let m4 = *(ptr.offset(tile.col_byte_stride * 4) as *const f32);
                        let m5 = *(ptr.offset(tile.col_byte_stride * 5) as *const f32);
                        let m6 = *(ptr.offset(tile.col_byte_stride * 6) as *const f32);
                        let m7 = *(ptr.offset(tile.col_byte_stride * 7) as *const f32);
                        let (lo, hi) = ab_pair;
                        **lo = f32x4_add(**lo, f32x4(m0, m1, m2, m3));
                        **hi = f32x4_add(**hi, f32x4(m4, m5, m6, m7));
                        ptr = ptr.add(tile.row_byte_stride as usize);
                    }
                }
                FusedKerSpec::AddRowColProducts(rows, cols) => {
                    let p = cols as *const v128;
                    let clo = v128_load(p);
                    let chi = v128_load(p.add(1));
                    let r0 = f32x4_splat(*rows.add(0));
                    a0lo = madd_f32x4!(a0lo, r0, clo);
                    a0hi = madd_f32x4!(a0hi, r0, chi);
                    let r1 = f32x4_splat(*rows.add(1));
                    a1lo = madd_f32x4!(a1lo, r1, clo);
                    a1hi = madd_f32x4!(a1hi, r1, chi);
                    let r2 = f32x4_splat(*rows.add(2));
                    a2lo = madd_f32x4!(a2lo, r2, clo);
                    a2hi = madd_f32x4!(a2hi, r2, chi);
                    let r3 = f32x4_splat(*rows.add(3));
                    a3lo = madd_f32x4!(a3lo, r3, clo);
                    a3hi = madd_f32x4!(a3hi, r3, chi);
                    let r4 = f32x4_splat(*rows.add(4));
                    a4lo = madd_f32x4!(a4lo, r4, clo);
                    a4hi = madd_f32x4!(a4hi, r4, chi);
                    let r5 = f32x4_splat(*rows.add(5));
                    a5lo = madd_f32x4!(a5lo, r5, clo);
                    a5hi = madd_f32x4!(a5hi, r5, chi);
                    let r6 = f32x4_splat(*rows.add(6));
                    a6lo = madd_f32x4!(a6lo, r6, clo);
                    a6hi = madd_f32x4!(a6hi, r6, chi);
                    let r7 = f32x4_splat(*rows.add(7));
                    a7lo = madd_f32x4!(a7lo, r7, clo);
                    a7hi = madd_f32x4!(a7hi, r7, chi);
                }
                FusedKerSpec::Store(tile) => {
                    // 8 rows × 8 cols stores
                    let mut ptr: *mut u8 = tile.ptr;
                    for (lo, hi) in [
                        (a0lo, a0hi),
                        (a1lo, a1hi),
                        (a2lo, a2hi),
                        (a3lo, a3hi),
                        (a4lo, a4hi),
                        (a5lo, a5hi),
                        (a6lo, a6hi),
                        (a7lo, a7hi),
                    ]
                    .iter()
                    {
                        *(ptr as *mut f32) = f32x4_extract_lane::<0>(*lo);
                        *(ptr.offset(tile.col_byte_stride) as *mut f32) =
                            f32x4_extract_lane::<1>(*lo);
                        *(ptr.offset(tile.col_byte_stride * 2) as *mut f32) =
                            f32x4_extract_lane::<2>(*lo);
                        *(ptr.offset(tile.col_byte_stride * 3) as *mut f32) =
                            f32x4_extract_lane::<3>(*lo);
                        *(ptr.offset(tile.col_byte_stride * 4) as *mut f32) =
                            f32x4_extract_lane::<0>(*hi);
                        *(ptr.offset(tile.col_byte_stride * 5) as *mut f32) =
                            f32x4_extract_lane::<1>(*hi);
                        *(ptr.offset(tile.col_byte_stride * 6) as *mut f32) =
                            f32x4_extract_lane::<2>(*hi);
                        *(ptr.offset(tile.col_byte_stride * 7) as *mut f32) =
                            f32x4_extract_lane::<3>(*hi);
                        ptr = ptr.add(tile.row_byte_stride as usize);
                    }
                }
                FusedKerSpec::AddMatMul { k, pa, pb, packing: _ } => {
                    // A: packed [k][MR=8] = each k iter loads 8 row values
                    // B: packed [k][NR=8] = each k iter loads 8 col values as 2 v128
                    let a = pa as *const f32;
                    let b = pb as *const v128;
                    for i in 0..k {
                        let arow = std::slice::from_raw_parts(a.offset(8 * i as isize), 8);
                        let blo = v128_load(b.offset((2 * i) as isize));
                        let bhi = v128_load(b.offset((2 * i + 1) as isize));
                        let s = f32x4_splat(arow[0]);
                        a0lo = madd_f32x4!(a0lo, s, blo);
                        a0hi = madd_f32x4!(a0hi, s, bhi);
                        let s = f32x4_splat(arow[1]);
                        a1lo = madd_f32x4!(a1lo, s, blo);
                        a1hi = madd_f32x4!(a1hi, s, bhi);
                        let s = f32x4_splat(arow[2]);
                        a2lo = madd_f32x4!(a2lo, s, blo);
                        a2hi = madd_f32x4!(a2hi, s, bhi);
                        let s = f32x4_splat(arow[3]);
                        a3lo = madd_f32x4!(a3lo, s, blo);
                        a3hi = madd_f32x4!(a3hi, s, bhi);
                        let s = f32x4_splat(arow[4]);
                        a4lo = madd_f32x4!(a4lo, s, blo);
                        a4hi = madd_f32x4!(a4hi, s, bhi);
                        let s = f32x4_splat(arow[5]);
                        a5lo = madd_f32x4!(a5lo, s, blo);
                        a5hi = madd_f32x4!(a5hi, s, bhi);
                        let s = f32x4_splat(arow[6]);
                        a6lo = madd_f32x4!(a6lo, s, blo);
                        a6hi = madd_f32x4!(a6hi, s, bhi);
                        let s = f32x4_splat(arow[7]);
                        a7lo = madd_f32x4!(a7lo, s, blo);
                        a7hi = madd_f32x4!(a7hi, s, bhi);
                    }
                }
            }
            pnl = pnl.add(1);
        }
        0
    }
}

bail_stub!(wasm32; unsafe fn kernel_f32_8x8(*const FusedKerSpec<f32>) -> isize);

MMMRustKernel!(wasm32; kernel_f32_8x8 => wasm_f32_8x8<f32>(8,8)@(8,8));

/// WASM SIMD f32 4x16 kernel — 4 rows x 16 cols, 16 v128 accumulators laid out
/// row-major as `acc[r * 4 + j]`, j indexing the four v128 chunks of a row.
///
/// Same accumulator count as `wasm_f32_8x8`, but the tile is wide rather than
/// square. Each k-step splats 4 A values and loads 4 B vectors, so one splat
/// feeds four multiply-adds instead of two: the per-splat overhead is halved
/// against the same arithmetic. Measured against `wasm_f32_8x8` on a full tiled
/// GEMM, this is 1.09-1.11x on the 64x64 shapes and 1.21x aggregate.
///
/// It is NOT a general replacement: at n=8 a 16-wide tile wastes half its
/// columns and this runs at 0.58x. Dispatch must keep 8x8 for narrow n.
#[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
unsafe fn kernel_f32_4x16(mut pnl: *const FusedKerSpec<f32>) -> isize {
    use std::arch::wasm32::*;

    unsafe {
        const MR: usize = 4;
        const NV: usize = 4; // v128 chunks per row (NR = 16)
        let mut acc = [f32x4_splat(0.0); MR * NV];

        loop {
            if pnl.is_null() {
                break;
            }
            match *pnl {
                FusedKerSpec::Done => break,
                FusedKerSpec::Clear => acc = [f32x4_splat(0.0); MR * NV],
                FusedKerSpec::LoadTile(_cols, rows) => {
                    let p = rows as *const v128;
                    for i in 0..MR * NV {
                        acc[i] = *p.add(i);
                    }
                }
                FusedKerSpec::ScalarMin(a) => {
                    let s = f32x4_splat(a);
                    for v in acc.iter_mut() {
                        *v = f32x4_min(s, *v);
                    }
                }
                FusedKerSpec::ScalarMax(a) => {
                    let s = f32x4_splat(a);
                    for v in acc.iter_mut() {
                        *v = f32x4_max(s, *v);
                    }
                }
                FusedKerSpec::ScalarAdd(a) => {
                    let s = f32x4_splat(a);
                    for v in acc.iter_mut() {
                        *v = f32x4_add(s, *v);
                    }
                }
                FusedKerSpec::ScalarMul(a) => {
                    let s = f32x4_splat(a);
                    for v in acc.iter_mut() {
                        *v = f32x4_mul(s, *v);
                    }
                }
                FusedKerSpec::ScalarSub(a) => {
                    let s = f32x4_splat(a);
                    for v in acc.iter_mut() {
                        *v = f32x4_sub(s, *v);
                    }
                }
                FusedKerSpec::ScalarSubF(a) => {
                    let s = f32x4_splat(a);
                    for v in acc.iter_mut() {
                        *v = f32x4_sub(*v, s);
                    }
                }
                FusedKerSpec::LeakyRelu(a) => {
                    let s = f32x4_splat(a);
                    let zero = f32x4_splat(0.0);
                    for v in acc.iter_mut() {
                        let m = f32x4_gt(*v, zero);
                        *v = v128_bitselect(*v, f32x4_mul(s, *v), m);
                    }
                }
                FusedKerSpec::PerRowMin(rows) => {
                    for r in 0..MR {
                        let s = f32x4_splat(*rows.add(r));
                        for j in 0..NV {
                            acc[r * NV + j] = f32x4_min(s, acc[r * NV + j]);
                        }
                    }
                }
                FusedKerSpec::PerRowMax(rows) => {
                    for r in 0..MR {
                        let s = f32x4_splat(*rows.add(r));
                        for j in 0..NV {
                            acc[r * NV + j] = f32x4_max(s, acc[r * NV + j]);
                        }
                    }
                }
                FusedKerSpec::PerRowAdd(rows) => {
                    for r in 0..MR {
                        let s = f32x4_splat(*rows.add(r));
                        for j in 0..NV {
                            acc[r * NV + j] = f32x4_add(s, acc[r * NV + j]);
                        }
                    }
                }
                FusedKerSpec::PerRowMul(rows) => {
                    for r in 0..MR {
                        let s = f32x4_splat(*rows.add(r));
                        for j in 0..NV {
                            acc[r * NV + j] = f32x4_mul(s, acc[r * NV + j]);
                        }
                    }
                }
                FusedKerSpec::PerRowSub(rows) => {
                    for r in 0..MR {
                        let s = f32x4_splat(*rows.add(r));
                        for j in 0..NV {
                            acc[r * NV + j] = f32x4_sub(s, acc[r * NV + j]);
                        }
                    }
                }
                FusedKerSpec::PerRowSubF(rows) => {
                    for r in 0..MR {
                        let s = f32x4_splat(*rows.add(r));
                        for j in 0..NV {
                            acc[r * NV + j] = f32x4_sub(acc[r * NV + j], s);
                        }
                    }
                }
                FusedKerSpec::PerColMin(cols) => {
                    let p = cols as *const v128;
                    for r in 0..MR {
                        for j in 0..NV {
                            acc[r * NV + j] = f32x4_min(v128_load(p.add(j)), acc[r * NV + j]);
                        }
                    }
                }
                FusedKerSpec::PerColMax(cols) => {
                    let p = cols as *const v128;
                    for r in 0..MR {
                        for j in 0..NV {
                            acc[r * NV + j] = f32x4_max(v128_load(p.add(j)), acc[r * NV + j]);
                        }
                    }
                }
                FusedKerSpec::PerColAdd(cols) => {
                    let p = cols as *const v128;
                    for r in 0..MR {
                        for j in 0..NV {
                            acc[r * NV + j] = f32x4_add(v128_load(p.add(j)), acc[r * NV + j]);
                        }
                    }
                }
                FusedKerSpec::PerColMul(cols) => {
                    let p = cols as *const v128;
                    for r in 0..MR {
                        for j in 0..NV {
                            acc[r * NV + j] = f32x4_mul(v128_load(p.add(j)), acc[r * NV + j]);
                        }
                    }
                }
                FusedKerSpec::PerColSub(cols) => {
                    let p = cols as *const v128;
                    for r in 0..MR {
                        for j in 0..NV {
                            acc[r * NV + j] = f32x4_sub(v128_load(p.add(j)), acc[r * NV + j]);
                        }
                    }
                }
                FusedKerSpec::PerColSubF(cols) => {
                    let p = cols as *const v128;
                    for r in 0..MR {
                        for j in 0..NV {
                            acc[r * NV + j] = f32x4_sub(acc[r * NV + j], v128_load(p.add(j)));
                        }
                    }
                }
                FusedKerSpec::QScale(shift, rp, mult) => {
                    let scaler = Scaler::from_fuse_params(shift, rp, mult);
                    let s = f32x4_splat(scaler.scale);
                    for v in acc.iter_mut() {
                        *v = f32x4_mul(s, *v);
                    }
                }
                FusedKerSpec::RoundingShiftRight(shift, _rp) => {
                    let s = f32x4_splat(2f32.powi(-(shift as i32)));
                    for v in acc.iter_mut() {
                        *v = f32x4_mul(s, *v);
                    }
                }
                FusedKerSpec::ShiftLeft(shift) => {
                    let s = f32x4_splat(2f32.powi(shift as i32));
                    for v in acc.iter_mut() {
                        *v = f32x4_mul(s, *v);
                    }
                }
                FusedKerSpec::AddRowColProducts(rows, cols) => {
                    let p = cols as *const v128;
                    for r in 0..MR {
                        let rv = f32x4_splat(*rows.add(r));
                        for j in 0..NV {
                            acc[r * NV + j] = madd_f32x4!(acc[r * NV + j], rv, v128_load(p.add(j)));
                        }
                    }
                }
                FusedKerSpec::AddUnicast(tile) => {
                    let mut ptr: *const u8 = tile.ptr;
                    for r in 0..MR {
                        for j in 0..NV {
                            let c0 = *(ptr.offset(tile.col_byte_stride * (j * 4) as isize)
                                as *const f32);
                            let c1 = *(ptr.offset(tile.col_byte_stride * (j * 4 + 1) as isize)
                                as *const f32);
                            let c2 = *(ptr.offset(tile.col_byte_stride * (j * 4 + 2) as isize)
                                as *const f32);
                            let c3 = *(ptr.offset(tile.col_byte_stride * (j * 4 + 3) as isize)
                                as *const f32);
                            acc[r * NV + j] = f32x4_add(f32x4(c0, c1, c2, c3), acc[r * NV + j]);
                        }
                        ptr = ptr.add(tile.row_byte_stride as usize);
                    }
                }
                FusedKerSpec::Store(tile) => {
                    let mut ptr: *mut u8 = tile.ptr;
                    for r in 0..MR {
                        for j in 0..NV {
                            let v = acc[r * NV + j];
                            let base = (j * 4) as isize;
                            *(ptr.offset(tile.col_byte_stride * base) as *mut f32) =
                                f32x4_extract_lane::<0>(v);
                            *(ptr.offset(tile.col_byte_stride * (base + 1)) as *mut f32) =
                                f32x4_extract_lane::<1>(v);
                            *(ptr.offset(tile.col_byte_stride * (base + 2)) as *mut f32) =
                                f32x4_extract_lane::<2>(v);
                            *(ptr.offset(tile.col_byte_stride * (base + 3)) as *mut f32) =
                                f32x4_extract_lane::<3>(v);
                        }
                        ptr = ptr.add(tile.row_byte_stride as usize);
                    }
                }
                FusedKerSpec::AddMatMul { k, pa, pb, packing: _ } => {
                    // A: packed [k][MR=4], B: packed [k][NR=16] = 4 v128 per k
                    let a = pa as *const f32;
                    let b = pb as *const v128;
                    for i in 0..k {
                        let arow = std::slice::from_raw_parts(a.offset(4 * i as isize), 4);
                        let bb = b.offset((4 * i) as isize);
                        let b0 = v128_load(bb);
                        let b1 = v128_load(bb.add(1));
                        let b2 = v128_load(bb.add(2));
                        let b3 = v128_load(bb.add(3));
                        for r in 0..MR {
                            let s = f32x4_splat(arow[r]);
                            acc[r * NV] = madd_f32x4!(acc[r * NV], s, b0);
                            acc[r * NV + 1] = madd_f32x4!(acc[r * NV + 1], s, b1);
                            acc[r * NV + 2] = madd_f32x4!(acc[r * NV + 2], s, b2);
                            acc[r * NV + 3] = madd_f32x4!(acc[r * NV + 3], s, b3);
                        }
                    }
                }
            }
            pnl = pnl.add(1);
        }
        0
    }
}

bail_stub!(wasm32; unsafe fn kernel_f32_4x16(*const FusedKerSpec<f32>) -> isize);

MMMRustKernel!(wasm32; kernel_f32_4x16 => wasm_f32_4x16<f32>(4,16)@(4,16));