1 // Auto-generated file. Do not edit!
2 // Template: src/f32-gemm/wasmsimd-s4.c.in
3 // Generator: tools/xngen
4 //
5 // Copyright 2020 Google LLC
6 //
7 // This source code is licensed under the BSD-style license found in the
8 // LICENSE file in the root directory of this source tree.
9
10 #include <assert.h>
11
12 #include <wasm_simd128.h>
13
14 #include <xnnpack/gemm.h>
15
16
xnn_f32_gemm_minmax_ukernel_4x8s4__wasmsimd_arm(size_t mr,size_t nc,size_t kc,const float * restrict a,size_t a_stride,const float * restrict w,float * restrict c,size_t cm_stride,size_t cn_stride,const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])17 void xnn_f32_gemm_minmax_ukernel_4x8s4__wasmsimd_arm(
18 size_t mr,
19 size_t nc,
20 size_t kc,
21 const float*restrict a,
22 size_t a_stride,
23 const float*restrict w,
24 float*restrict c,
25 size_t cm_stride,
26 size_t cn_stride,
27 const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS(1)])
28 {
29 assert(mr != 0);
30 assert(mr <= 4);
31 assert(nc != 0);
32 assert(kc != 0);
33 assert(kc % sizeof(float) == 0);
34 assert(a != NULL);
35 assert(w != NULL);
36 assert(c != NULL);
37
38 const float* a0 = a;
39 float* c0 = c;
40 const float* a1 = (const float*) ((uintptr_t) a0 + a_stride);
41 float* c1 = (float*) ((uintptr_t) c0 + cm_stride);
42 if XNN_UNPREDICTABLE(mr < 2) {
43 a1 = a0;
44 c1 = c0;
45 }
46 const float* a2 = (const float*) ((uintptr_t) a1 + a_stride);
47 float* c2 = (float*) ((uintptr_t) c1 + cm_stride);
48 if XNN_UNPREDICTABLE(mr <= 2) {
49 a2 = a1;
50 c2 = c1;
51 }
52 const float* a3 = (const float*) ((uintptr_t) a2 + a_stride);
53 float* c3 = (float*) ((uintptr_t) c2 + cm_stride);
54 if XNN_UNPREDICTABLE(mr != 4) {
55 a3 = a2;
56 c3 = c2;
57 }
58
59 const v128_t vmin = wasm_v32x4_load_splat(¶ms->scalar.min);
60 const v128_t vmax = wasm_v32x4_load_splat(¶ms->scalar.max);
61 do {
62 v128_t vacc0x0123 = wasm_v128_load(w + 0);
63 v128_t vacc0x4567 = wasm_v128_load(w + 4);
64 v128_t vacc1x0123 = vacc0x0123;
65 v128_t vacc1x4567 = vacc0x4567;
66 v128_t vacc2x0123 = vacc0x0123;
67 v128_t vacc2x4567 = vacc0x4567;
68 v128_t vacc3x0123 = vacc0x0123;
69 v128_t vacc3x4567 = vacc0x4567;
70 w += 8;
71
72 size_t k = kc;
73 while (k >= 4 * sizeof(float)) {
74 v128_t va0 = wasm_v128_load(a0);
75 a0 += 4;
76 v128_t va1 = wasm_v128_load(a1);
77 a1 += 4;
78 v128_t va2 = wasm_v128_load(a2);
79 a2 += 4;
80 v128_t va3 = wasm_v128_load(a3);
81 a3 += 4;
82
83
84 const v128_t vb0123c0 = wasm_v128_load(w + 0);
85 const v128_t vb4567c0 = wasm_v128_load(w + 4);
86
87 vacc0x0123 = wasm_f32x4_add(vacc0x0123, wasm_f32x4_mul(va0, vb0123c0));
88 vacc1x0123 = wasm_f32x4_add(vacc1x0123, wasm_f32x4_mul(va1, vb0123c0));
89 vacc2x0123 = wasm_f32x4_add(vacc2x0123, wasm_f32x4_mul(va2, vb0123c0));
90 vacc3x0123 = wasm_f32x4_add(vacc3x0123, wasm_f32x4_mul(va3, vb0123c0));
91 vacc0x4567 = wasm_f32x4_add(vacc0x4567, wasm_f32x4_mul(va0, vb4567c0));
92 vacc1x4567 = wasm_f32x4_add(vacc1x4567, wasm_f32x4_mul(va1, vb4567c0));
93 vacc2x4567 = wasm_f32x4_add(vacc2x4567, wasm_f32x4_mul(va2, vb4567c0));
94 vacc3x4567 = wasm_f32x4_add(vacc3x4567, wasm_f32x4_mul(va3, vb4567c0));
95
96 va0 = wasm_v32x4_shuffle(va0, va0, 1, 2, 3, 0);
97 va1 = wasm_v32x4_shuffle(va1, va1, 1, 2, 3, 0);
98 va2 = wasm_v32x4_shuffle(va2, va2, 1, 2, 3, 0);
99 va3 = wasm_v32x4_shuffle(va3, va3, 1, 2, 3, 0);
100
101 const v128_t vb0123c1 = wasm_v128_load(w + 8);
102 const v128_t vb4567c1 = wasm_v128_load(w + 12);
103
104 vacc0x0123 = wasm_f32x4_add(vacc0x0123, wasm_f32x4_mul(va0, vb0123c1));
105 vacc1x0123 = wasm_f32x4_add(vacc1x0123, wasm_f32x4_mul(va1, vb0123c1));
106 vacc2x0123 = wasm_f32x4_add(vacc2x0123, wasm_f32x4_mul(va2, vb0123c1));
107 vacc3x0123 = wasm_f32x4_add(vacc3x0123, wasm_f32x4_mul(va3, vb0123c1));
108 vacc0x4567 = wasm_f32x4_add(vacc0x4567, wasm_f32x4_mul(va0, vb4567c1));
109 vacc1x4567 = wasm_f32x4_add(vacc1x4567, wasm_f32x4_mul(va1, vb4567c1));
110 vacc2x4567 = wasm_f32x4_add(vacc2x4567, wasm_f32x4_mul(va2, vb4567c1));
111 vacc3x4567 = wasm_f32x4_add(vacc3x4567, wasm_f32x4_mul(va3, vb4567c1));
112
113 va0 = wasm_v32x4_shuffle(va0, va0, 1, 2, 3, 0);
114 va1 = wasm_v32x4_shuffle(va1, va1, 1, 2, 3, 0);
115 va2 = wasm_v32x4_shuffle(va2, va2, 1, 2, 3, 0);
116 va3 = wasm_v32x4_shuffle(va3, va3, 1, 2, 3, 0);
117
118 const v128_t vb0123c2 = wasm_v128_load(w + 16);
119 const v128_t vb4567c2 = wasm_v128_load(w + 20);
120
121 vacc0x0123 = wasm_f32x4_add(vacc0x0123, wasm_f32x4_mul(va0, vb0123c2));
122 vacc1x0123 = wasm_f32x4_add(vacc1x0123, wasm_f32x4_mul(va1, vb0123c2));
123 vacc2x0123 = wasm_f32x4_add(vacc2x0123, wasm_f32x4_mul(va2, vb0123c2));
124 vacc3x0123 = wasm_f32x4_add(vacc3x0123, wasm_f32x4_mul(va3, vb0123c2));
125 vacc0x4567 = wasm_f32x4_add(vacc0x4567, wasm_f32x4_mul(va0, vb4567c2));
126 vacc1x4567 = wasm_f32x4_add(vacc1x4567, wasm_f32x4_mul(va1, vb4567c2));
127 vacc2x4567 = wasm_f32x4_add(vacc2x4567, wasm_f32x4_mul(va2, vb4567c2));
128 vacc3x4567 = wasm_f32x4_add(vacc3x4567, wasm_f32x4_mul(va3, vb4567c2));
129
130 va0 = wasm_v32x4_shuffle(va0, va0, 1, 2, 3, 0);
131 va1 = wasm_v32x4_shuffle(va1, va1, 1, 2, 3, 0);
132 va2 = wasm_v32x4_shuffle(va2, va2, 1, 2, 3, 0);
133 va3 = wasm_v32x4_shuffle(va3, va3, 1, 2, 3, 0);
134
135 const v128_t vb0123c3 = wasm_v128_load(w + 24);
136 const v128_t vb4567c3 = wasm_v128_load(w + 28);
137
138 vacc0x0123 = wasm_f32x4_add(vacc0x0123, wasm_f32x4_mul(va0, vb0123c3));
139 vacc1x0123 = wasm_f32x4_add(vacc1x0123, wasm_f32x4_mul(va1, vb0123c3));
140 vacc2x0123 = wasm_f32x4_add(vacc2x0123, wasm_f32x4_mul(va2, vb0123c3));
141 vacc3x0123 = wasm_f32x4_add(vacc3x0123, wasm_f32x4_mul(va3, vb0123c3));
142 vacc0x4567 = wasm_f32x4_add(vacc0x4567, wasm_f32x4_mul(va0, vb4567c3));
143 vacc1x4567 = wasm_f32x4_add(vacc1x4567, wasm_f32x4_mul(va1, vb4567c3));
144 vacc2x4567 = wasm_f32x4_add(vacc2x4567, wasm_f32x4_mul(va2, vb4567c3));
145 vacc3x4567 = wasm_f32x4_add(vacc3x4567, wasm_f32x4_mul(va3, vb4567c3));
146
147
148 w += 32;
149 k -= 4 * sizeof(float);
150 }
151 if XNN_UNLIKELY(k != 0) {
152 do {
153 const v128_t va0 = wasm_v32x4_load_splat(a0);
154 a0 += 1;
155 const v128_t va1 = wasm_v32x4_load_splat(a1);
156 a1 += 1;
157 const v128_t va2 = wasm_v32x4_load_splat(a2);
158 a2 += 1;
159 const v128_t va3 = wasm_v32x4_load_splat(a3);
160 a3 += 1;
161
162 const v128_t vb0123 = wasm_v128_load(w);
163 const v128_t vb4567 = wasm_v128_load(w + 4);
164 w += 8;
165
166 vacc0x0123 = wasm_f32x4_add(vacc0x0123, wasm_f32x4_mul(va0, vb0123));
167 vacc1x0123 = wasm_f32x4_add(vacc1x0123, wasm_f32x4_mul(va1, vb0123));
168 vacc2x0123 = wasm_f32x4_add(vacc2x0123, wasm_f32x4_mul(va2, vb0123));
169 vacc3x0123 = wasm_f32x4_add(vacc3x0123, wasm_f32x4_mul(va3, vb0123));
170 vacc0x4567 = wasm_f32x4_add(vacc0x4567, wasm_f32x4_mul(va0, vb4567));
171 vacc1x4567 = wasm_f32x4_add(vacc1x4567, wasm_f32x4_mul(va1, vb4567));
172 vacc2x4567 = wasm_f32x4_add(vacc2x4567, wasm_f32x4_mul(va2, vb4567));
173 vacc3x4567 = wasm_f32x4_add(vacc3x4567, wasm_f32x4_mul(va3, vb4567));
174
175 k -= sizeof(float);
176 } while (k != 0);
177 }
178
179 vacc0x0123 = wasm_f32x4_max(vacc0x0123, vmin);
180 vacc1x0123 = wasm_f32x4_max(vacc1x0123, vmin);
181 vacc2x0123 = wasm_f32x4_max(vacc2x0123, vmin);
182 vacc3x0123 = wasm_f32x4_max(vacc3x0123, vmin);
183 vacc0x4567 = wasm_f32x4_max(vacc0x4567, vmin);
184 vacc1x4567 = wasm_f32x4_max(vacc1x4567, vmin);
185 vacc2x4567 = wasm_f32x4_max(vacc2x4567, vmin);
186 vacc3x4567 = wasm_f32x4_max(vacc3x4567, vmin);
187
188 vacc0x0123 = wasm_f32x4_min(vacc0x0123, vmax);
189 vacc1x0123 = wasm_f32x4_min(vacc1x0123, vmax);
190 vacc2x0123 = wasm_f32x4_min(vacc2x0123, vmax);
191 vacc3x0123 = wasm_f32x4_min(vacc3x0123, vmax);
192 vacc0x4567 = wasm_f32x4_min(vacc0x4567, vmax);
193 vacc1x4567 = wasm_f32x4_min(vacc1x4567, vmax);
194 vacc2x4567 = wasm_f32x4_min(vacc2x4567, vmax);
195 vacc3x4567 = wasm_f32x4_min(vacc3x4567, vmax);
196
197 if XNN_LIKELY(nc >= 8) {
198 wasm_v128_store(c3, vacc3x0123);
199 wasm_v128_store(c3 + 4, vacc3x4567);
200 c3 = (float*) ((uintptr_t) c3 + cn_stride);
201 wasm_v128_store(c2, vacc2x0123);
202 wasm_v128_store(c2 + 4, vacc2x4567);
203 c2 = (float*) ((uintptr_t) c2 + cn_stride);
204 wasm_v128_store(c1, vacc1x0123);
205 wasm_v128_store(c1 + 4, vacc1x4567);
206 c1 = (float*) ((uintptr_t) c1 + cn_stride);
207 wasm_v128_store(c0, vacc0x0123);
208 wasm_v128_store(c0 + 4, vacc0x4567);
209 c0 = (float*) ((uintptr_t) c0 + cn_stride);
210
211 a3 = (const float*) ((uintptr_t) a3 - kc);
212 a2 = (const float*) ((uintptr_t) a2 - kc);
213 a1 = (const float*) ((uintptr_t) a1 - kc);
214 a0 = (const float*) ((uintptr_t) a0 - kc);
215
216 nc -= 8;
217 } else {
218 if (nc & 4) {
219 wasm_v128_store(c3, vacc3x0123);
220 wasm_v128_store(c2, vacc2x0123);
221 wasm_v128_store(c1, vacc1x0123);
222 wasm_v128_store(c0, vacc0x0123);
223
224 vacc3x0123 = vacc3x4567;
225 vacc2x0123 = vacc2x4567;
226 vacc1x0123 = vacc1x4567;
227 vacc0x0123 = vacc0x4567;
228
229 c3 += 4;
230 c2 += 4;
231 c1 += 4;
232 c0 += 4;
233 }
234 if (nc & 2) {
235 *((double*) c3) = wasm_f64x2_extract_lane(vacc3x0123, 0);
236 *((double*) c2) = wasm_f64x2_extract_lane(vacc2x0123, 0);
237 *((double*) c1) = wasm_f64x2_extract_lane(vacc1x0123, 0);
238 *((double*) c0) = wasm_f64x2_extract_lane(vacc0x0123, 0);
239
240 vacc3x0123 = wasm_v32x4_shuffle(vacc3x0123, vacc3x0123, 2, 3, 2, 3);
241 vacc2x0123 = wasm_v32x4_shuffle(vacc2x0123, vacc2x0123, 2, 3, 2, 3);
242 vacc1x0123 = wasm_v32x4_shuffle(vacc1x0123, vacc1x0123, 2, 3, 2, 3);
243 vacc0x0123 = wasm_v32x4_shuffle(vacc0x0123, vacc0x0123, 2, 3, 2, 3);
244
245 c3 += 2;
246 c2 += 2;
247 c1 += 2;
248 c0 += 2;
249 }
250 if (nc & 1) {
251 *c3 = wasm_f32x4_extract_lane(vacc3x0123, 0);
252 *c2 = wasm_f32x4_extract_lane(vacc2x0123, 0);
253 *c1 = wasm_f32x4_extract_lane(vacc1x0123, 0);
254 *c0 = wasm_f32x4_extract_lane(vacc0x0123, 0);
255 }
256
257 nc = 0;
258 }
259 } while (nc != 0);
260 }
261