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
use core::slice::from_raw_parts_mut;

use num_traits::{One, Zero};
use seq_macro::seq;

use crate::simd::{Boilerplate, MixedSimd, Simd};

#[inline(always)]
pub unsafe fn gemv<
    T: Copy
        + Zero
        + One
        + Send
        + Sync
        + core::ops::Add<Output = T>
        + core::ops::Mul<Output = T>
        + core::cmp::PartialEq,
    S: Simd,
>(
    _simd: S,
    m: usize,
    n: usize,
    k: usize,
    dst: *mut T,
    dst_cs: isize,
    dst_rs: isize,
    lhs: *const T,
    lhs_cs: isize,
    lhs_rs: isize,
    rhs: *const T,
    rhs_cs: isize,
    rhs_rs: isize,
    alpha: T,
    beta: T,
    mul_add: impl Fn(T, T, T) -> T,
) {
    if !alpha.is_zero() {
        for col in 0..n {
            for row in 0..m {
                let dst = dst
                    .wrapping_offset(row as isize * dst_rs)
                    .wrapping_offset(col as isize * dst_cs);

                *dst = alpha * *dst;
            }
        }
    } else {
        for col in 0..n {
            for row in 0..m {
                let dst = dst
                    .wrapping_offset(row as isize * dst_rs)
                    .wrapping_offset(col as isize * dst_cs);

                *dst = T::zero();
            }
        }
    }

    macro_rules! do_work {
        ($n: tt) => {
            for depth in 0..k {
                seq!(COL in 0..$n {
                    let rhs~COL = beta * *rhs
                        .wrapping_offset(COL as isize * rhs_cs)
                        .wrapping_offset(depth as isize * rhs_rs);
                });
                for row in 0..m {
                    let lhs = *lhs
                        .wrapping_offset(depth as isize * lhs_cs)
                        .wrapping_offset(row as isize * lhs_rs);

                    seq!(COL in 0..$n {
                        {
                            let dst = dst
                                .wrapping_offset(COL as isize * dst_cs)
                                .wrapping_offset(row as isize * dst_rs);
                            *dst = mul_add(rhs~COL, lhs, *dst);
                        }
                    });
                }
            }
        }
    }
    match n {
        1 => do_work!(1),
        _ => unreachable!(),
    }
}

// dst, lhs are colmajor
// n is small
#[inline(always)]
pub unsafe fn mixed_gemv_colmajor<
    Lhs: Boilerplate + One + Zero,
    Rhs: Boilerplate + One + Zero,
    Dst: Boilerplate + One + Zero,
    Acc: Boilerplate + One + Zero,
    S: MixedSimd<Lhs, Rhs, Dst, Acc>,
>(
    simd: S,

    m: usize,
    n: usize,
    k: usize,

    dst: *mut Dst,
    dst_cs: isize,
    dst_rs: isize,

    lhs: *const Lhs,
    lhs_cs: isize,
    lhs_rs: isize,

    rhs: *const Rhs,
    rhs_cs: isize,
    rhs_rs: isize,

    alpha: Acc,
    beta: Acc,
) {
    #[inline(always)]
    unsafe fn implementation<
        'a,
        Lhs: Boilerplate + One + Zero,
        Rhs: Boilerplate + One + Zero,
        Dst: Boilerplate + One + Zero,
        Acc: Boilerplate + One + Zero,
        S: MixedSimd<Lhs, Rhs, Dst, Acc>,
    >(
        noalias_dst: (&'a mut [Dst],),
        simd: S,
        m: usize,
        k: usize,
        lhs: *const Lhs,
        lhs_cs: isize,
        rhs: *const Rhs,
        rhs_cs: isize,
        rhs_rs: isize,
        alpha: Acc,
        beta: Acc,
    ) {
        #[allow(dead_code)]
        struct Impl<'a, Lhs, Rhs, Dst, Acc, S> {
            simd: S,
            m: usize,
            k: usize,
            noalias_dst: (&'a mut [Dst],),
            lhs: *const Lhs,
            lhs_cs: isize,
            rhs: *const Rhs,
            rhs_cs: isize,
            rhs_rs: isize,
            alpha: Acc,
            beta: Acc,
        }
        impl<
                Lhs: Boilerplate + One + Zero,
                Rhs: Boilerplate + One + Zero,
                Dst: Boilerplate + One + Zero,
                Acc: Boilerplate + One + Zero,
                S: MixedSimd<Lhs, Rhs, Dst, Acc>,
            > pulp::NullaryFnOnce for Impl<'_, Lhs, Rhs, Dst, Acc, S>
        {
            type Output = ();

            #[inline(always)]
            fn call(self) -> Self::Output {
                unsafe {
                    let Self {
                        simd,
                        m,
                        k,
                        noalias_dst,
                        lhs,
                        lhs_cs,
                        rhs,
                        rhs_cs: _,
                        rhs_rs,
                        mut alpha,
                        beta,
                    } = self;

                    let lane = S::SIMD_WIDTH;
                    let dst = noalias_dst.0.as_mut_ptr();
                    let m_lane = m / lane * lane;
                    for col in 0..k {
                        let lhs = lhs.wrapping_offset(col as isize * lhs_cs);
                        let rhs = simd.from_rhs(*rhs.wrapping_offset(col as isize * rhs_rs));

                        let alpha_s = alpha;
                        let alpha_v = simd.simd_splat(alpha_s);

                        let rhs_scalar = simd.mult(beta, rhs);
                        let rhs = simd.simd_splat(rhs_scalar);

                        if alpha_s.is_zero() {
                            let mut row = 0usize;
                            while row < m_lane {
                                let dst_ptr = dst.wrapping_add(row) as *mut S::DstN;
                                let lhs =
                                    simd.simd_from_lhs(*(lhs.wrapping_add(row) as *const S::LhsN));
                                *dst_ptr = simd.simd_into_dst(simd.simd_mul(lhs, rhs));
                                row += lane;
                            }
                            while row < m {
                                let dst_ptr = dst.wrapping_add(row);
                                let lhs = simd.from_lhs(*lhs.wrapping_add(row));
                                *dst_ptr = simd.into_dst(simd.mult(lhs, rhs_scalar));
                                row += 1;
                            }
                        } else if alpha_s.is_one() {
                            let mut row = 0usize;
                            while row < m_lane {
                                let dst_ptr = dst.wrapping_add(row) as *mut S::DstN;
                                let dst = *dst_ptr;
                                let lhs =
                                    simd.simd_from_lhs(*(lhs.wrapping_add(row) as *const S::LhsN));
                                *dst_ptr = simd.simd_into_dst(simd.simd_mult_add(
                                    lhs,
                                    rhs,
                                    simd.simd_from_dst(dst),
                                ));
                                row += lane;
                            }
                            while row < m {
                                let dst_ptr = dst.wrapping_add(row);
                                let dst = *dst_ptr;
                                let lhs = simd.from_lhs(*lhs.wrapping_add(row));
                                *dst_ptr = simd.into_dst(simd.mult_add(
                                    lhs,
                                    rhs_scalar,
                                    simd.from_dst(dst),
                                ));
                                row += 1;
                            }
                        } else {
                            let mut row = 0usize;
                            while row < m_lane {
                                let dst_ptr = dst.wrapping_add(row) as *mut S::DstN;
                                let dst = *dst_ptr;
                                let lhs =
                                    simd.simd_from_lhs(*(lhs.wrapping_add(row) as *const S::LhsN));
                                *dst_ptr = simd.simd_into_dst(simd.simd_add(
                                    simd.simd_mul(lhs, rhs),
                                    simd.simd_mul(alpha_v, simd.simd_from_dst(dst)),
                                ));
                                row += lane;
                            }
                            while row < m {
                                let dst_ptr = dst.wrapping_add(row);
                                let dst = *dst_ptr;
                                let lhs = simd.from_lhs(*lhs.wrapping_add(row));
                                *dst_ptr = simd.into_dst(simd.add(
                                    simd.mult(lhs, rhs_scalar),
                                    simd.mult(alpha_s, simd.from_dst(dst)),
                                ));
                                row += 1;
                            }
                        }
                        alpha = Acc::one();
                    }
                }
            }
        }

        simd.vectorize(Impl {
            simd,
            m,
            k,
            noalias_dst,
            lhs,
            lhs_cs,
            rhs,
            rhs_cs,
            rhs_rs,
            alpha,
            beta,
        })
    }

    assert_eq!(lhs_rs, 1);
    assert_eq!(dst_rs, 1);

    if k == 0 {
        if alpha.is_one() {
            return;
        }
        if alpha.is_zero() {
            for j in 0..n {
                core::ptr::write_bytes(dst.wrapping_offset(j as isize * dst_cs), 0u8, m);
            }
            return;
        }

        for j in 0..n {
            let dst = dst.wrapping_offset(j as isize * dst_cs);
            for i in 0..m {
                let dst = dst.add(i);
                *dst = simd.into_dst(simd.mult(simd.from_dst(*dst), alpha));
            }
        }
    }

    for x in 0..n {
        implementation(
            (from_raw_parts_mut(
                dst.wrapping_offset(x as isize * dst_cs) as _,
                m,
            ),),
            simd,
            m,
            k,
            lhs,
            lhs_cs,
            rhs.wrapping_offset(rhs_cs * x as isize),
            rhs_cs,
            rhs_rs,
            alpha,
            beta,
        );
    }
}

// lhs is rowmajor
// rhs is colmajor
// n is small
#[inline(always)]
pub unsafe fn mixed_gemv_rowmajor<
    Lhs: Boilerplate + One + Zero,
    Rhs: Boilerplate + One + Zero,
    Dst: Boilerplate + One + Zero,
    Acc: Boilerplate + One + Zero,
    S: MixedSimd<Lhs, Rhs, Dst, Acc>,
>(
    simd: S,

    m: usize,
    n: usize,
    k: usize,

    dst: *mut Dst,
    dst_cs: isize,
    dst_rs: isize,

    lhs: *const Lhs,
    lhs_cs: isize,
    lhs_rs: isize,

    rhs: *const Rhs,
    rhs_cs: isize,
    rhs_rs: isize,

    alpha: Acc,
    beta: Acc,
) {
    #[inline(always)]
    unsafe fn implementation<
        'a,
        Lhs: Boilerplate + One + Zero,
        Rhs: Boilerplate + One + Zero,
        Dst: Boilerplate + One + Zero,
        Acc: Boilerplate + One + Zero,
        S: MixedSimd<Lhs, Rhs, Dst, Acc>,
    >(
        simd: S,
        dst: *mut Dst,
        dst_rs: isize,
        m: usize,
        k: usize,
        lhs: *const Lhs,
        lhs_rs: isize,
        rhs: *const Rhs,
        alpha: Acc,
        beta: Acc,
    ) {
        #[allow(dead_code)]
        struct Impl<Lhs, Rhs, Dst, Acc, S> {
            simd: S,
            dst: *mut Dst,
            dst_rs: isize,
            m: usize,
            k: usize,
            lhs: *const Lhs,
            lhs_rs: isize,
            rhs: *const Rhs,
            alpha: Acc,
            beta: Acc,
        }
        impl<
                Lhs: Boilerplate + One + Zero,
                Rhs: Boilerplate + One + Zero,
                Dst: Boilerplate + One + Zero,
                Acc: Boilerplate + One + Zero,
                S: MixedSimd<Lhs, Rhs, Dst, Acc>,
            > pulp::NullaryFnOnce for Impl<Lhs, Rhs, Dst, Acc, S>
        {
            type Output = ();

            #[inline(always)]
            fn call(self) -> Self::Output {
                unsafe {
                    let Self {
                        simd,
                        dst,
                        dst_rs,
                        m,
                        k,
                        lhs,
                        lhs_rs,
                        rhs,
                        alpha,
                        beta,
                    } = self;

                    let lane = S::SIMD_WIDTH;
                    let lane8 = 8 * S::SIMD_WIDTH;

                    let k_lane = k / lane * lane;
                    let k_lane8 = k / lane8 * lane8;

                    for row in 0..m {
                        let lhs = lhs.wrapping_offset(row as isize * lhs_rs);

                        let mut depth = 0;

                        let mut acc0 = simd.simd_splat(Acc::zero());
                        let mut acc1 = simd.simd_splat(Acc::zero());
                        let mut acc2 = simd.simd_splat(Acc::zero());
                        let mut acc3 = simd.simd_splat(Acc::zero());
                        let mut acc4 = simd.simd_splat(Acc::zero());
                        let mut acc5 = simd.simd_splat(Acc::zero());
                        let mut acc6 = simd.simd_splat(Acc::zero());
                        let mut acc7 = simd.simd_splat(Acc::zero());

                        while depth < k_lane8 {
                            let lhs0 = *(lhs.wrapping_add(depth + lane * 0) as *const S::LhsN);
                            let rhs0 = *(rhs.wrapping_add(depth + lane * 0) as *const S::RhsN);
                            acc0 = simd.simd_mult_add(
                                simd.simd_from_lhs(lhs0),
                                simd.simd_from_rhs(rhs0),
                                acc0,
                            );

                            let lhs1 = *(lhs.wrapping_add(depth + lane * 1) as *const S::LhsN);
                            let rhs1 = *(rhs.wrapping_add(depth + lane * 1) as *const S::RhsN);
                            acc1 = simd.simd_mult_add(
                                simd.simd_from_lhs(lhs1),
                                simd.simd_from_rhs(rhs1),
                                acc1,
                            );

                            let lhs2 = *(lhs.wrapping_add(depth + lane * 2) as *const S::LhsN);
                            let rhs2 = *(rhs.wrapping_add(depth + lane * 2) as *const S::RhsN);
                            acc2 = simd.simd_mult_add(
                                simd.simd_from_lhs(lhs2),
                                simd.simd_from_rhs(rhs2),
                                acc2,
                            );

                            let lhs3 = *(lhs.wrapping_add(depth + lane * 3) as *const S::LhsN);
                            let rhs3 = *(rhs.wrapping_add(depth + lane * 3) as *const S::RhsN);
                            acc3 = simd.simd_mult_add(
                                simd.simd_from_lhs(lhs3),
                                simd.simd_from_rhs(rhs3),
                                acc3,
                            );

                            let lhs4 = *(lhs.wrapping_add(depth + lane * 4) as *const S::LhsN);
                            let rhs4 = *(rhs.wrapping_add(depth + lane * 4) as *const S::RhsN);
                            acc4 = simd.simd_mult_add(
                                simd.simd_from_lhs(lhs4),
                                simd.simd_from_rhs(rhs4),
                                acc4,
                            );

                            let lhs5 = *(lhs.wrapping_add(depth + lane * 5) as *const S::LhsN);
                            let rhs5 = *(rhs.wrapping_add(depth + lane * 5) as *const S::RhsN);
                            acc5 = simd.simd_mult_add(
                                simd.simd_from_lhs(lhs5),
                                simd.simd_from_rhs(rhs5),
                                acc5,
                            );

                            let lhs6 = *(lhs.wrapping_add(depth + lane * 6) as *const S::LhsN);
                            let rhs6 = *(rhs.wrapping_add(depth + lane * 6) as *const S::RhsN);
                            acc6 = simd.simd_mult_add(
                                simd.simd_from_lhs(lhs6),
                                simd.simd_from_rhs(rhs6),
                                acc6,
                            );

                            let lhs7 = *(lhs.wrapping_add(depth + lane * 7) as *const S::LhsN);
                            let rhs7 = *(rhs.wrapping_add(depth + lane * 7) as *const S::RhsN);
                            acc7 = simd.simd_mult_add(
                                simd.simd_from_lhs(lhs7),
                                simd.simd_from_rhs(rhs7),
                                acc7,
                            );

                            depth += lane8;
                        }

                        let acc0 = simd.simd_add(acc0, acc1);
                        let acc2 = simd.simd_add(acc2, acc3);
                        let acc4 = simd.simd_add(acc4, acc5);
                        let acc6 = simd.simd_add(acc6, acc7);

                        let acc0 = simd.simd_add(acc0, acc2);
                        let acc4 = simd.simd_add(acc4, acc6);

                        let mut acc0 = simd.simd_add(acc0, acc4);

                        while depth < k_lane {
                            let lhs0 = *(lhs.wrapping_add(depth) as *const S::LhsN);
                            let rhs0 = *(rhs.wrapping_add(depth) as *const S::RhsN);
                            acc0 = simd.simd_mult_add(
                                simd.simd_from_lhs(lhs0),
                                simd.simd_from_rhs(rhs0),
                                acc0,
                            );

                            depth += lane;
                        }

                        let acc_ptr = &acc0 as *const _ as *const Acc;
                        let mut acc0 = *acc_ptr;
                        for x in 1..S::SIMD_WIDTH {
                            acc0 = simd.add(acc0, *acc_ptr.add(x));
                        }

                        while depth < k {
                            let lhs0 = *(lhs.wrapping_add(depth + 0));
                            let rhs0 = *(rhs.wrapping_add(depth + 0));

                            acc0 = simd.mult_add(simd.from_lhs(lhs0), simd.from_rhs(rhs0), acc0);

                            depth += 1;
                        }

                        if alpha.is_zero() {
                            let dst = dst.wrapping_offset(dst_rs * row as isize);
                            *dst = simd.into_dst(simd.mult(acc0, beta));
                        } else {
                            let dst = dst.wrapping_offset(dst_rs * row as isize);
                            *dst =
                                simd.into_dst(simd.add(
                                    simd.mult(acc0, beta),
                                    simd.mult(simd.from_dst(*dst), alpha),
                                ));
                        }
                    }
                }
            }
        }

        simd.vectorize(Impl {
            simd,
            dst,
            dst_rs,
            m,
            k,
            lhs,
            lhs_rs,
            rhs,
            alpha,
            beta,
        })
    }

    assert_eq!(lhs_cs, 1);
    assert_eq!(rhs_rs, 1);

    for x in 0..n {
        implementation(
            simd,
            dst.wrapping_offset(x as isize * dst_cs),
            dst_rs,
            m,
            k,
            lhs,
            lhs_rs,
            rhs.wrapping_offset(rhs_cs * x as isize),
            alpha,
            beta,
        );
    }
}