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}__wasmsimd${ARCH_SUFFIX}_splat(
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        const v128_t va${M} = wasm_v128_load(a${M});
86        a${M} += 4;
87
88      $for L in range(4):
89        $for M in range(MR):
90          const v128_t va${M}c${L} = wasm_v32x4_shuffle(va${M}, va${M}, ${L}, ${L}, ${L}, ${L});
91
92        $for N in range(0, NR, 4):
93          const v128_t vb${ABC[N:N+4]}c${L} = wasm_v128_load(w + ${L * NR + N});
94
95        $for N in range(0, NR, 4):
96          $for M in range(MR):
97            vacc${M}x${ABC[N:N+4]} = wasm_f32x4_add(vacc${M}x${ABC[N:N+4]}, wasm_f32x4_mul(va${M}c${L}, vb${ABC[N:N+4]}c${L}));
98
99      w += ${4 * NR};
100      k -= 4 * sizeof(float);
101    }
102    if XNN_UNLIKELY(k != 0) {
103      do {
104        $for M in range(MR):
105          const v128_t va${M} = wasm_v32x4_load_splat(a${M});
106          a${M} += 1;
107
108        const v128_t vb${ABC[0:4]} = wasm_v128_load(w);
109        $for N in range(4, NR, 4):
110          const v128_t vb${ABC[N:N+4]} = wasm_v128_load(w + ${N});
111        w += ${NR};
112
113        $for N in range(0, NR, 4):
114          $for M in range(MR):
115            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]}));
116
117        k -= sizeof(float);
118      } while (k != 0);
119    }
120
121    $if ACTIVATION == "MINMAX":
122      $if X86:
123        const v128_t vmin = wasm_v32x4_load_splat(&params->scalar.min);
124        $for N in range(0, NR, 4):
125          $for M in range(MR):
126            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));
127
128        const v128_t vmax = wasm_v32x4_load_splat(&params->scalar.max);
129        $for N in range(0, NR, 4):
130          $for M in range(MR):
131            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));
132      $else:
133        $for N in range(0, NR, 4):
134          $for M in range(MR):
135            vacc${M}x${ABC[N:N+4]} = wasm_f32x4_max(vacc${M}x${ABC[N:N+4]}, vmin);
136
137        $for N in range(0, NR, 4):
138          $for M in range(MR):
139            vacc${M}x${ABC[N:N+4]} = wasm_f32x4_min(vacc${M}x${ABC[N:N+4]}, vmax);
140    $elif ACTIVATION == "RELU":
141      const v128_t vzero = wasm_f32x4_splat(0.0f);
142      $for N in range(0, NR, 4):
143        $for M in range(MR):
144          vacc${M}x${ABC[N:N+4]} = wasm_i32x4_max(vacc${M}x${ABC[N:N+4]}, vzero);
145
146    if XNN_LIKELY(nc >= ${NR}) {
147      $for M in reversed(range(MR)):
148        wasm_v128_store(c${M}, vacc${M}x${ABC[0:4]});
149        $for N in range(4, NR, 4):
150          wasm_v128_store(c${M} + ${N}, vacc${M}x${ABC[N:N+4]});
151        c${M} = (float*) ((uintptr_t) c${M} + cn_stride);
152
153      $for M in reversed(range(MR)):
154        a${M} = (const float*) ((uintptr_t) a${M} - kc);
155
156      nc -= ${NR};
157    } else {
158      $for LOG2N in reversed(range(NR.bit_length())):
159        $if NR != 1 << LOG2N:
160          if (nc & ${1 << LOG2N}) {
161            $if LOG2N >= 2:
162              $for M in reversed(range(MR)):
163                wasm_v128_store(c${M}, vacc${M}x${ABC[0:4]});
164                $for N in range(4, 1 << LOG2N, 4):
165                  wasm_v128_store(c${M} + ${N}, vacc${M}x${ABC[N:N+4]});
166
167              $for M in reversed(range(MR)):
168                $for N in range(0, 1 << (LOG2N - 1), 4):
169                  vacc${M}x${ABC[N:N+4]} = vacc${M}x${ABC[N + (1 << LOG2N):N + (1 << LOG2N)+4]};
170
171              $for M in reversed(range(MR)):
172                c${M} += ${1 << LOG2N};
173            $elif LOG2N == 1:
174              $for M in reversed(range(MR)):
175                *((double*) c${M}) = wasm_f64x2_extract_lane(vacc${M}x${ABC[0:4]}, 0);
176
177              $for M in reversed(range(MR)):
178                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);
179
180              $for M in reversed(range(MR)):
181                c${M} += 2;
182            $elif LOG2N == 0:
183              $for M in reversed(range(MR)):
184                *c${M} = wasm_f32x4_extract_lane(vacc${M}x${ABC[0:4]}, 0);
185          }
186
187      nc = 0;
188    }
189  } while (nc != 0);
190}
191