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