1// Copyright 2019 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 = "0123456789ABCDEFGHIJKLMN" 8#include <assert.h> 9 10#include <psimd.h> 11 12#include <xnnpack/igemm.h> 13 14 15void xnn_f32_igemm_ukernel_${MR}x${NR}s4__psimd( 16 size_t mr, 17 size_t nc, 18 size_t kc, 19 size_t ks, 20 const float**restrict a, 21 const float*restrict w, 22 float*restrict c, 23 size_t cm_stride, 24 size_t cn_stride, 25 size_t a_offset, 26 const float* zero, 27 const union xnn_f32_output_params params[restrict static 1]) 28{ 29 assert(mr != 0); 30 assert(mr <= ${MR}); 31 assert(nc != 0); 32 assert(kc != 0); 33 assert(kc % sizeof(float) == 0); 34 assert(ks != 0); 35 assert(ks % (${MR} * sizeof(void*)) == 0); 36 assert(a_offset % sizeof(float) == 0); 37 assert(a != NULL); 38 assert(w != NULL); 39 assert(c != NULL); 40 41 float* c0 = c; 42 $for M in range(1, MR): 43 float* c${M} = (float*) ((uintptr_t) c${M-1} + cm_stride); 44 $if M % 2 == 0: 45 if XNN_UNPREDICTABLE(mr <= ${M}) { 46 c${M} = c${M-1}; 47 } 48 $elif M + 1 == MR: 49 if XNN_UNPREDICTABLE(mr != ${M+1}) { 50 c${M} = c${M-1}; 51 } 52 $else: 53 if XNN_UNPREDICTABLE(mr < ${M+1}) { 54 c${M} = c${M-1}; 55 } 56 57 do { 58 psimd_f32 vacc0x${ABC[0:4]} = psimd_load_f32(w); 59 $for N in range(4, NR, 4): 60 psimd_f32 vacc0x${ABC[N:N+4]} = psimd_load_f32(w + ${N}); 61 $for M in range(1, MR): 62 $for N in range(0, NR, 4): 63 psimd_f32 vacc${M}x${ABC[N:N+4]} = vacc0x${ABC[N:N+4]}; 64 w += ${NR}; 65 66 size_t p = ks; 67 do { 68 $for M in range(MR): 69 const float* restrict a${M} = a[${M}]; 70 assert(a${M} != NULL); 71 if XNN_UNPREDICTABLE(a${M} != zero) { 72 a${M} = (const float*) ((uintptr_t) a${M} + a_offset); 73 } 74 a += ${MR}; 75 76 size_t k = kc; 77 while (k >= 4 * sizeof(float)) { 78 $for M in range(MR): 79 psimd_f32 va${M} = psimd_load_f32(a${M}); 80 a${M} += 4; 81 82 $for L in range(4): 83 84 $for N in range(0, NR, 4): 85 const psimd_f32 vb${ABC[N:N+4]}c${L} = psimd_load_f32(w + ${L * NR + N}); 86 87 $for N in range(0, NR, 4): 88 $for M in range(MR): 89 vacc${M}x${ABC[N:N+4]} = psimd_qfma_f32(vacc${M}x${ABC[N:N+4]}, va${M}, vb${ABC[N:N+4]}c${L}); 90 91 $if L + 1 != 4: 92 #ifdef __clang__ 93 $for M in range(MR): 94 va${M} = __builtin_shufflevector(va${M}, va${M}, 1, 2, 3, 0); 95 #else 96 $for M in range(MR): 97 va${M} = __builtin_shuffle(va${M}, va${M}, (psimd_s32) { 1, 2, 3, 0 }); 98 #endif 99 100 w += ${4 * NR}; 101 k -= 4 * sizeof(float); 102 } 103 if XNN_UNLIKELY(k != 0) { 104 do { 105 const psimd_f32 vb${ABC[0:4]} = psimd_load_f32(w); 106 $for N in range(4, NR, 4): 107 const psimd_f32 vb${ABC[N:N+4]} = psimd_load_f32(w + ${N}); 108 w += ${NR}; 109 110 $for M in range(MR): 111 const psimd_f32 va${M} = psimd_load_splat_f32(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]} = psimd_qfma_f32(vacc${M}x${ABC[N:N+4]}, 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 const psimd_f32 vmax = psimd_load_splat_f32(¶ms->scalar.max); 124 $for N in range(0, NR, 4): 125 $for M in range(MR): 126 vacc${M}x${ABC[N:N+4]} = psimd_min_f32(vacc${M}x${ABC[N:N+4]}, vmax); 127 128 const psimd_f32 vmin = psimd_load_splat_f32(¶ms->scalar.min); 129 $for N in range(0, NR, 4): 130 $for M in range(MR): 131 vacc${M}x${ABC[N:N+4]} = psimd_max_f32(vacc${M}x${ABC[N:N+4]}, vmin); 132 133 if XNN_LIKELY(nc >= ${NR}) { 134 $for M in reversed(range(MR)): 135 psimd_store_f32(c${M}, vacc${M}x${ABC[0:4]}); 136 $for N in range(4, NR, 4): 137 psimd_store_f32(c${M} + ${N}, vacc${M}x${ABC[N:N+4]}); 138 c${M} = (float*) ((uintptr_t) c${M} + cn_stride); 139 140 a = (const float**restrict) ((uintptr_t) a - ks); 141 nc -= ${NR}; 142 } else { 143 $for LOG2N in reversed(range(NR.bit_length())): 144 $if NR != 1 << LOG2N: 145 if (nc & ${1 << LOG2N}) { 146 $if LOG2N >= 2: 147 $for M in reversed(range(MR)): 148 psimd_store_f32(c${M}, vacc${M}x${ABC[0:4]}); 149 $for N in range(4, 1 << LOG2N, 4): 150 psimd_store_f32(c${M} + ${N}, vacc${M}x${ABC[N:N+4]}); 151 152 $for M in reversed(range(MR)): 153 $for N in range(0, 1 << (LOG2N - 1), 4): 154 vacc${M}x${ABC[N:N+4]} = vacc${M}x${ABC[N + (1 << LOG2N):N + (1 << LOG2N)+4]}; 155 156 $for M in reversed(range(MR)): 157 c${M} += ${1 << LOG2N}; 158 $elif LOG2N == 1: 159 $for M in reversed(range(MR)): 160 psimd_store2_f32(c${M}, vacc${M}x${ABC[0:4]}); 161 162 $for M in reversed(range(MR)): 163 vacc${M}x${ABC[0:4]} = psimd_concat_hi_f32(vacc${M}x${ABC[0:4]}, vacc${M}x${ABC[0:4]}); 164 165 $for M in reversed(range(MR)): 166 c${M} += 2; 167 $elif LOG2N == 0: 168 $for M in reversed(range(MR)): 169 psimd_store1_f32(c${M}, vacc${M}x${ABC[0:4]}); 170 } 171 172 nc = 0; 173 } 174 } while (nc != 0); 175} 176