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/igemm.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_igemm${ACTIVATION_SUFFIX}_ukernel_${MR}x${NR}s4__wasmsimd${ARCH_SUFFIX}(
20    size_t mr,
21    size_t nc,
22    size_t kc,
23    size_t ks,
24    const float**restrict a,
25    const float*restrict w,
26    float*restrict c,
27    size_t cm_stride,
28    size_t cn_stride,
29    size_t a_offset,
30    const float* zero,
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(ks != 0);
39  assert(ks % (${MR} * sizeof(void*)) == 0);
40  assert(a_offset % sizeof(float) == 0);
41  assert(a != NULL);
42  assert(w != NULL);
43  assert(c != NULL);
44
45  float* c0 = c;
46  $for M in range(1, MR):
47    float* c${M} = (float*) ((uintptr_t) c${M-1} + cm_stride);
48    $if M % 2 == 0:
49      if XNN_UNPREDICTABLE(mr <= ${M}) {
50        c${M} = c${M-1};
51      }
52    $elif M + 1 == MR:
53      if XNN_UNPREDICTABLE(mr != ${M+1}) {
54        c${M} = c${M-1};
55      }
56    $else:
57      if XNN_UNPREDICTABLE(mr < ${M+1}) {
58        c${M} = c${M-1};
59      }
60
61  $if ACTIVATION == "MINMAX" and not X86:
62    const v128_t vmin = wasm_v32x4_load_splat(&params->scalar.min);
63    const v128_t vmax = wasm_v32x4_load_splat(&params->scalar.max);
64  do {
65    v128_t vacc0x${ABC[0:4]} = wasm_v128_load(w);
66    $for N in range(4, NR, 4):
67      v128_t vacc0x${ABC[N:N+4]} = wasm_v128_load(w + ${N});
68    $for M in range(1, MR):
69      $for N in range(0, NR, 4):
70        v128_t vacc${M}x${ABC[N:N+4]} = vacc0x${ABC[N:N+4]};
71    w += ${NR};
72
73    size_t p = ks;
74    do {
75      $for M in range(MR):
76        const float* restrict a${M} = a[${M}];
77        assert(a${M} != NULL);
78        if XNN_UNPREDICTABLE(a${M} != zero) {
79          a${M} = (const float*) ((uintptr_t) a${M} + a_offset);
80        }
81      a += ${MR};
82
83      size_t k = kc;
84      while (k >= 4 * sizeof(float)) {
85        $for M in range(MR):
86          v128_t va${M} = wasm_v128_load(a${M});
87          a${M} += 4;
88
89        $for L in range(4):
90
91          $for N in range(0, NR, 4):
92            const v128_t vb${ABC[N:N+4]}c${L} = wasm_v128_load(w + ${L * NR + N});
93
94          $for N in range(0, NR, 4):
95            $for M in range(MR):
96              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}));
97
98          $if L + 1 != 4:
99            $for M in range(MR):
100              va${M} = wasm_v32x4_shuffle(va${M}, va${M}, 1, 2, 3, 0);
101
102        w += ${4 * NR};
103        k -= 4 * sizeof(float);
104      }
105      if XNN_UNLIKELY(k != 0) {
106        do {
107          const v128_t vb${ABC[0:4]} = wasm_v128_load(w);
108          $for N in range(4, NR, 4):
109            const v128_t vb${ABC[N:N+4]} = wasm_v128_load(w + ${N});
110          w += ${NR};
111
112          $for M in range(MR):
113            const v128_t va${M} = wasm_v32x4_load_splat(a${M});
114            a${M} += 1;
115
116          $for M in range(MR):
117            $for N in range(0, NR, 4):
118              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]}));
119          k -= sizeof(float);
120        } while (k != 0);
121      }
122      p -= ${MR} * sizeof(void*);
123    } while (p != 0);
124
125    $if ACTIVATION == "MINMAX":
126      $if X86:
127        const v128_t vmin = wasm_v32x4_load_splat(&params->scalar.min);
128        $for N in range(0, NR, 4):
129          $for M in range(MR):
130            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));
131      $else:
132        $for N in range(0, NR, 4):
133          $for M in range(MR):
134            vacc${M}x${ABC[N:N+4]} = wasm_f32x4_max(vacc${M}x${ABC[N:N+4]}, vmin);
135
136      $if X86:
137        const v128_t vmax = wasm_v32x4_load_splat(&params->scalar.max);
138        $for N in range(0, NR, 4):
139          $for M in range(MR):
140            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));
141      $else:
142        $for N in range(0, NR, 4):
143          $for M in range(MR):
144            vacc${M}x${ABC[N:N+4]} = wasm_f32x4_min(vacc${M}x${ABC[N:N+4]}, vmax);
145    $elif ACTIVATION == "RELU":
146      const v128_t vzero = wasm_f32x4_splat(0.0f);
147      $for N in range(0, NR, 4):
148        $for M in range(MR):
149          vacc${M}x${ABC[N:N+4]} = wasm_i32x4_max(vacc${M}x${ABC[N:N+4]}, vzero);
150
151    if XNN_LIKELY(nc >= ${NR}) {
152      $for M in reversed(range(MR)):
153        wasm_v128_store(c${M}, vacc${M}x${ABC[0:4]});
154        $for N in range(4, NR, 4):
155          wasm_v128_store(c${M} + ${N}, vacc${M}x${ABC[N:N+4]});
156        c${M} = (float*) ((uintptr_t) c${M} + cn_stride);
157
158      a = (const float**restrict) ((uintptr_t) a - ks);
159      nc -= ${NR};
160    } else {
161      $for LOG2N in reversed(range(NR.bit_length())):
162        $if NR != 1 << LOG2N:
163          if (nc & ${1 << LOG2N}) {
164            $if LOG2N >= 2:
165              $for M in reversed(range(MR)):
166                wasm_v128_store(c${M}, vacc${M}x${ABC[0:4]});
167                $for N in range(4, 1 << LOG2N, 4):
168                  wasm_v128_store(c${M} + ${N}, vacc${M}x${ABC[N:N+4]});
169
170              $for M in reversed(range(MR)):
171                $for N in range(0, 1 << (LOG2N - 1), 4):
172                  vacc${M}x${ABC[N:N+4]} = vacc${M}x${ABC[N + (1 << LOG2N):N + (1 << LOG2N)+4]};
173
174              $for M in reversed(range(MR)):
175                c${M} += ${1 << LOG2N};
176            $elif LOG2N == 1:
177              $for M in reversed(range(MR)):
178                *((double*) c${M}) = wasm_f64x2_extract_lane(vacc${M}x${ABC[0:4]}, 0);
179
180              $for M in reversed(range(MR)):
181                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);
182
183              $for M in reversed(range(MR)):
184                c${M} += 2;
185            $elif LOG2N == 0:
186              $for M in reversed(range(MR)):
187                *c${M} = wasm_f32x4_extract_lane(vacc${M}x${ABC[0:4]}, 0);
188          }
189
190      nc = 0;
191    }
192  } while (nc != 0);
193}
194