1// Copyright 2020 Google LLC
2//
3// This source code is licensed under the BSD-style license found in the
4// LICENSE file in the root directory of this source tree.
5
6$assert NR % 4 == 0
7$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
8#include <assert.h>
9
10#include <wasm_simd128.h>
11
12#include <xnnpack/gemm.h>
13
14
15$assert ACTIVATION in ["LINEAR", "RELU", "MINMAX"]
16$ACTIVATION_SUFFIX = {"LINEAR": ""}.get(ACTIVATION, "_" + ACTIVATION.lower())
17$ARCH_SUFFIX = "" if ACTIVATION in ["LINEAR", "RELU"] else "_x86" if X86 else "_arm"
18$PARAMS = {"LINEAR": "xnn_f32_default_params", "RELU": "xnn_f32_relu_params", "MINMAX": "xnn_f32_minmax_params"}[ACTIVATION]
19void xnn_f32_gemm${"inc" if INC else ""}${ACTIVATION_SUFFIX}_ukernel_${MR}x${NR}s4__wasmsimd${ARCH_SUFFIX}(
20    size_t mr,
21    size_t nc,
22    size_t kc,
23    const float*restrict a,
24    size_t a_stride,
25    const float*restrict w,
26    float*restrict c,
27    size_t cm_stride,
28    size_t cn_stride,
29    $if INC:
30      const float*restrict acc,
31    const union ${PARAMS} params[restrict XNN_MIN_ELEMENTS(1)])
32{
33  assert(mr != 0);
34  assert(mr <= ${MR});
35  assert(nc != 0);
36  assert(kc != 0);
37  assert(kc % sizeof(float) == 0);
38  assert(a != NULL);
39  assert(w != NULL);
40  assert(c != NULL);
41  $if INC:
42    assert(acc != NULL);
43
44  const float* a0 = a;
45  float* c0 = c;
46  $for M in range(1, MR):
47    const float* a${M} = (const float*) ((uintptr_t) a${M-1} + a_stride);
48    float* c${M} = (float*) ((uintptr_t) c${M-1} + cm_stride);
49    $if M % 2 == 0:
50      if XNN_UNPREDICTABLE(mr <= ${M}) {
51        a${M} = a${M-1};
52        c${M} = c${M-1};
53      }
54    $elif M + 1 == MR:
55      if XNN_UNPREDICTABLE(mr != ${M+1}) {
56        a${M} = a${M-1};
57        c${M} = c${M-1};
58      }
59    $else:
60      if XNN_UNPREDICTABLE(mr < ${M+1}) {
61        a${M} = a${M-1};
62        c${M} = c${M-1};
63      }
64
65  $if ACTIVATION == "MINMAX" and not X86:
66    const v128_t vmin = wasm_v32x4_load_splat(&params->scalar.min);
67    const v128_t vmax = wasm_v32x4_load_splat(&params->scalar.max);
68  do {
69    $if INC:
70      $for M in range(MR):
71        $for N in range(0, NR, 4):
72          v128_t vacc${M}x${ABC[N:N+4]} = wasm_v128_load(acc + ${M*NR+N});
73      acc += ${MR*NR};
74    $else:
75      $for N in range(0, NR, 4):
76        v128_t vacc0x${ABC[N:N+4]} = wasm_v128_load(w + ${N});
77      $for M in range(1, MR):
78        $for N in range(0, NR, 4):
79          v128_t vacc${M}x${ABC[N:N+4]} = vacc0x${ABC[N:N+4]};
80      w += ${NR};
81
82    size_t k = kc;
83    while (k >= 4 * sizeof(float)) {
84      $for M in range(MR):
85        v128_t va${M} = wasm_v128_load(a${M});
86        a${M} += 4;
87
88      $for L in range(4):
89
90        $for N in range(0, NR, 4):
91          const v128_t vb${ABC[N:N+4]}c${L} = wasm_v128_load(w + ${L * NR + N});
92
93        $for N in range(0, NR, 4):
94          $for M in range(MR):
95            vacc${M}x${ABC[N:N+4]} = wasm_f32x4_add(vacc${M}x${ABC[N:N+4]}, wasm_f32x4_mul(va${M}, vb${ABC[N:N+4]}c${L}));
96
97        $if L + 1 != 4:
98          $for M in range(MR):
99            va${M} = wasm_v32x4_shuffle(va${M}, va${M}, 1, 2, 3, 0);
100
101      w += ${4 * NR};
102      k -= 4 * sizeof(float);
103    }
104    if XNN_UNLIKELY(k != 0) {
105      do {
106        $for M in range(MR):
107          const v128_t va${M} = wasm_v32x4_load_splat(a${M});
108          a${M} += 1;
109
110        const v128_t vb${ABC[0:4]} = wasm_v128_load(w);
111        $for N in range(4, NR, 4):
112          const v128_t vb${ABC[N:N+4]} = wasm_v128_load(w + ${N});
113        w += ${NR};
114
115        $for N in range(0, NR, 4):
116          $for M in range(MR):
117            vacc${M}x${ABC[N:N+4]} = wasm_f32x4_add(vacc${M}x${ABC[N:N+4]}, wasm_f32x4_mul(va${M}, vb${ABC[N:N+4]}));
118
119        k -= sizeof(float);
120      } while (k != 0);
121    }
122
123    $if ACTIVATION == "MINMAX":
124      $if X86:
125        const v128_t vmin = wasm_v32x4_load_splat(&params->scalar.min);
126        $for N in range(0, NR, 4):
127          $for M in range(MR):
128            vacc${M}x${ABC[N:N+4]} = wasm_v128_bitselect(vmin, vacc${M}x${ABC[N:N+4]}, wasm_f32x4_lt(vacc${M}x${ABC[N:N+4]}, vmin));
129
130        const v128_t vmax = wasm_v32x4_load_splat(&params->scalar.max);
131        $for N in range(0, NR, 4):
132          $for M in range(MR):
133            vacc${M}x${ABC[N:N+4]} = wasm_v128_bitselect(vacc${M}x${ABC[N:N+4]}, vmax, wasm_f32x4_le(vacc${M}x${ABC[N:N+4]}, vmax));
134      $else:
135        $for N in range(0, NR, 4):
136          $for M in range(MR):
137            vacc${M}x${ABC[N:N+4]} = wasm_f32x4_max(vacc${M}x${ABC[N:N+4]}, vmin);
138
139        $for N in range(0, NR, 4):
140          $for M in range(MR):
141            vacc${M}x${ABC[N:N+4]} = wasm_f32x4_min(vacc${M}x${ABC[N:N+4]}, vmax);
142    $elif ACTIVATION == "RELU":
143      const v128_t vzero = wasm_f32x4_splat(0.0f);
144      $for N in range(0, NR, 4):
145        $for M in range(MR):
146          vacc${M}x${ABC[N:N+4]} = wasm_i32x4_max(vacc${M}x${ABC[N:N+4]}, vzero);
147
148    if XNN_LIKELY(nc >= ${NR}) {
149      $for M in reversed(range(MR)):
150        wasm_v128_store(c${M}, vacc${M}x${ABC[0:4]});
151        $for N in range(4, NR, 4):
152          wasm_v128_store(c${M} + ${N}, vacc${M}x${ABC[N:N+4]});
153        c${M} = (float*) ((uintptr_t) c${M} + cn_stride);
154
155      $for M in reversed(range(MR)):
156        a${M} = (const float*) ((uintptr_t) a${M} - kc);
157
158      nc -= ${NR};
159    } else {
160      $for LOG2N in reversed(range(NR.bit_length())):
161        $if NR != 1 << LOG2N:
162          if (nc & ${1 << LOG2N}) {
163            $if LOG2N >= 2:
164              $for M in reversed(range(MR)):
165                wasm_v128_store(c${M}, vacc${M}x${ABC[0:4]});
166                $for N in range(4, 1 << LOG2N, 4):
167                  wasm_v128_store(c${M} + ${N}, vacc${M}x${ABC[N:N+4]});
168
169              $for M in reversed(range(MR)):
170                $for N in range(0, 1 << (LOG2N - 1), 4):
171                  vacc${M}x${ABC[N:N+4]} = vacc${M}x${ABC[N + (1 << LOG2N):N + (1 << LOG2N)+4]};
172
173              $for M in reversed(range(MR)):
174                c${M} += ${1 << LOG2N};
175            $elif LOG2N == 1:
176              $for M in reversed(range(MR)):
177                *((double*) c${M}) = wasm_f64x2_extract_lane(vacc${M}x${ABC[0:4]}, 0);
178
179              $for M in reversed(range(MR)):
180                vacc${M}x${ABC[0:4]} = wasm_v32x4_shuffle(vacc${M}x${ABC[0:4]}, vacc${M}x${ABC[0:4]}, 2, 3, 2, 3);
181
182              $for M in reversed(range(MR)):
183                c${M} += 2;
184            $elif LOG2N == 0:
185              $for M in reversed(range(MR)):
186                *c${M} = wasm_f32x4_extract_lane(vacc${M}x${ABC[0:4]}, 0);
187          }
188
189      nc = 0;
190    }
191  } while (nc != 0);
192}
193