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
use crate::simd::Simd;
use num_traits::{One, Zero};

#[inline(always)]
pub unsafe fn gevv<
    T: Copy
        + Zero
        + One
        + Send
        + Sync
        + core::fmt::Debug
        + 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,
) {
    macro_rules! do_work {
        () => {
            match k {
                0 => {
                    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();
                            }
                        }
                    }
                    return;
                }
                1 => {
                    if !alpha.is_zero() {
                        if alpha.is_one() {
                            for col in 0..n {
                                let rhs = beta * *rhs.wrapping_offset(col as isize * rhs_cs);
                                for row in 0..m {
                                    let lhs = *lhs.wrapping_offset(row as isize * lhs_rs);
                                    let dst = dst
                                        .wrapping_offset(row as isize * dst_rs)
                                        .wrapping_offset(col as isize * dst_cs);

                                    *dst = mul_add(lhs, rhs, *dst);
                                }
                            }
                        } else {
                            for col in 0..n {
                                let rhs = beta * *rhs.wrapping_offset(col as isize * rhs_cs);
                                for row in 0..m {
                                    let lhs = *lhs.wrapping_offset(row as isize * lhs_rs);
                                    let dst = dst
                                        .wrapping_offset(row as isize * dst_rs)
                                        .wrapping_offset(col as isize * dst_cs);

                                    *dst = mul_add(lhs, rhs, alpha * *dst);
                                }
                            }
                        }
                    } else {
                        for col in 0..n {
                            let rhs = beta * *rhs.wrapping_offset(col as isize * rhs_cs);
                            for row in 0..m {
                                let lhs = *lhs.wrapping_offset(row as isize * lhs_rs);
                                let dst = dst
                                    .wrapping_offset(row as isize * dst_rs)
                                    .wrapping_offset(col as isize * dst_cs);

                                *dst = lhs * rhs;
                            }
                        }
                    }
                    return;
                }
                2 => {
                    if !alpha.is_zero() {
                        if alpha.is_one() {
                            for col in 0..n {
                                let rhs0 =
                                    beta * *rhs.wrapping_offset(col as isize * rhs_cs + 0 * rhs_rs);
                                let rhs1 =
                                    beta * *rhs.wrapping_offset(col as isize * rhs_cs + 1 * rhs_rs);
                                for row in 0..m {
                                    let lhs0 =
                                        *lhs.wrapping_offset(row as isize * lhs_rs + 0 * lhs_cs);
                                    let lhs1 =
                                        *lhs.wrapping_offset(row as isize * lhs_rs + 1 * lhs_cs);
                                    let dst = dst
                                        .wrapping_offset(row as isize * dst_rs)
                                        .wrapping_offset(col as isize * dst_cs);

                                    *dst = mul_add(lhs1, rhs1, mul_add(lhs0, rhs0, *dst));
                                }
                            }
                        } else {
                            for col in 0..n {
                                let rhs0 =
                                    beta * *rhs.wrapping_offset(col as isize * rhs_cs + 0 * rhs_rs);
                                let rhs1 =
                                    beta * *rhs.wrapping_offset(col as isize * rhs_cs + 1 * rhs_rs);
                                for row in 0..m {
                                    let lhs0 =
                                        *lhs.wrapping_offset(row as isize * lhs_rs + 0 * lhs_cs);
                                    let lhs1 =
                                        *lhs.wrapping_offset(row as isize * lhs_rs + 1 * lhs_cs);
                                    let dst = dst
                                        .wrapping_offset(row as isize * dst_rs)
                                        .wrapping_offset(col as isize * dst_cs);

                                    *dst = mul_add(lhs1, rhs1, mul_add(lhs0, rhs0, alpha * *dst));
                                }
                            }
                        }
                    } else {
                        for col in 0..n {
                            let rhs0 =
                                beta * *rhs.wrapping_offset(col as isize * rhs_cs + 0 * rhs_rs);
                            let rhs1 =
                                beta * *rhs.wrapping_offset(col as isize * rhs_cs + 1 * rhs_rs);
                            for row in 0..m {
                                let lhs0 = *lhs.wrapping_offset(row as isize * lhs_rs + 0 * lhs_cs);
                                let lhs1 = *lhs.wrapping_offset(row as isize * lhs_rs + 1 * lhs_cs);
                                let dst = dst
                                    .wrapping_offset(row as isize * dst_rs)
                                    .wrapping_offset(col as isize * dst_cs);

                                *dst = mul_add(lhs1, rhs1, lhs0 * rhs0);
                            }
                        }
                    }
                    return;
                }
                _ => unreachable!(),
            }
        };
    }
    do_work!()
}