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}__wasmsimd${ARCH_SUFFIX}_splat( 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(¶ms->scalar.min); 63 const v128_t vmax = wasm_v32x4_load_splat(¶ms->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 const v128_t va${M} = wasm_v128_load(a${M}); 87 a${M} += 4; 88 89 $for L in range(4): 90 $for M in range(MR): 91 const v128_t va${M}c${L} = wasm_v32x4_shuffle(va${M}, va${M}, ${L}, ${L}, ${L}, ${L}); 92 93 $for N in range(0, NR, 4): 94 const v128_t vb${ABC[N:N+4]}c${L} = wasm_v128_load(w + ${L * NR + N}); 95 96 $for N in range(0, NR, 4): 97 $for M in range(MR): 98 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})); 99 100 w += ${4 * NR}; 101 k -= 4 * sizeof(float); 102 } 103 if XNN_UNLIKELY(k != 0) { 104 do { 105 const v128_t vb${ABC[0:4]} = wasm_v128_load(w); 106 $for N in range(4, NR, 4): 107 const v128_t vb${ABC[N:N+4]} = wasm_v128_load(w + ${N}); 108 w += ${NR}; 109 110 $for M in range(MR): 111 const v128_t va${M} = wasm_v32x4_load_splat(a${M}); 112 a${M} += 1; 113 114 $for M in range(MR): 115 $for N in range(0, NR, 4): 116 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]})); 117 k -= sizeof(float); 118 } while (k != 0); 119 } 120 p -= ${MR} * sizeof(void*); 121 } while (p != 0); 122 123 $if ACTIVATION == "MINMAX": 124 $if X86: 125 const v128_t vmin = wasm_v32x4_load_splat(¶ms->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(¶ms->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 a = (const float**restrict) ((uintptr_t) a - ks); 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