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_3x8s4__wasmsimd_x86(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_3x8s4__wasmsimd_x86(
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 <= 3);
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
53 const v128_t vmin = wasm_v128_load64_splat(params->wasmsimd.min);
54 const v128_t vmax = wasm_v128_load64_splat(params->wasmsimd.max);
55 do {
56 v128_t vacc0x0123 = wasm_v128_load(w + 0);
57 v128_t vacc0x4567 = wasm_v128_load(w + 4);
58 v128_t vacc1x0123 = vacc0x0123;
59 v128_t vacc1x4567 = vacc0x4567;
60 v128_t vacc2x0123 = vacc0x0123;
61 v128_t vacc2x4567 = vacc0x4567;
62 w += 8;
63
64 size_t k = kc;
65 while (k >= 4 * sizeof(float)) {
66 v128_t va0 = wasm_v128_load(a0);
67 a0 += 4;
68 v128_t va1 = wasm_v128_load(a1);
69 a1 += 4;
70 v128_t va2 = wasm_v128_load(a2);
71 a2 += 4;
72
73
74 const v128_t vb0123c0 = wasm_v128_load(w + 0);
75 const v128_t vb4567c0 = wasm_v128_load(w + 4);
76
77 vacc0x0123 = wasm_f32x4_add(vacc0x0123, wasm_f32x4_mul(va0, vb0123c0));
78 vacc1x0123 = wasm_f32x4_add(vacc1x0123, wasm_f32x4_mul(va1, vb0123c0));
79 vacc2x0123 = wasm_f32x4_add(vacc2x0123, wasm_f32x4_mul(va2, vb0123c0));
80 vacc0x4567 = wasm_f32x4_add(vacc0x4567, wasm_f32x4_mul(va0, vb4567c0));
81 vacc1x4567 = wasm_f32x4_add(vacc1x4567, wasm_f32x4_mul(va1, vb4567c0));
82 vacc2x4567 = wasm_f32x4_add(vacc2x4567, wasm_f32x4_mul(va2, vb4567c0));
83
84 va0 = wasm_v32x4_shuffle(va0, va0, 1, 2, 3, 0);
85 va1 = wasm_v32x4_shuffle(va1, va1, 1, 2, 3, 0);
86 va2 = wasm_v32x4_shuffle(va2, va2, 1, 2, 3, 0);
87
88 const v128_t vb0123c1 = wasm_v128_load(w + 8);
89 const v128_t vb4567c1 = wasm_v128_load(w + 12);
90
91 vacc0x0123 = wasm_f32x4_add(vacc0x0123, wasm_f32x4_mul(va0, vb0123c1));
92 vacc1x0123 = wasm_f32x4_add(vacc1x0123, wasm_f32x4_mul(va1, vb0123c1));
93 vacc2x0123 = wasm_f32x4_add(vacc2x0123, wasm_f32x4_mul(va2, vb0123c1));
94 vacc0x4567 = wasm_f32x4_add(vacc0x4567, wasm_f32x4_mul(va0, vb4567c1));
95 vacc1x4567 = wasm_f32x4_add(vacc1x4567, wasm_f32x4_mul(va1, vb4567c1));
96 vacc2x4567 = wasm_f32x4_add(vacc2x4567, wasm_f32x4_mul(va2, vb4567c1));
97
98 va0 = wasm_v32x4_shuffle(va0, va0, 1, 2, 3, 0);
99 va1 = wasm_v32x4_shuffle(va1, va1, 1, 2, 3, 0);
100 va2 = wasm_v32x4_shuffle(va2, va2, 1, 2, 3, 0);
101
102 const v128_t vb0123c2 = wasm_v128_load(w + 16);
103 const v128_t vb4567c2 = wasm_v128_load(w + 20);
104
105 vacc0x0123 = wasm_f32x4_add(vacc0x0123, wasm_f32x4_mul(va0, vb0123c2));
106 vacc1x0123 = wasm_f32x4_add(vacc1x0123, wasm_f32x4_mul(va1, vb0123c2));
107 vacc2x0123 = wasm_f32x4_add(vacc2x0123, wasm_f32x4_mul(va2, vb0123c2));
108 vacc0x4567 = wasm_f32x4_add(vacc0x4567, wasm_f32x4_mul(va0, vb4567c2));
109 vacc1x4567 = wasm_f32x4_add(vacc1x4567, wasm_f32x4_mul(va1, vb4567c2));
110 vacc2x4567 = wasm_f32x4_add(vacc2x4567, wasm_f32x4_mul(va2, vb4567c2));
111
112 va0 = wasm_v32x4_shuffle(va0, va0, 1, 2, 3, 0);
113 va1 = wasm_v32x4_shuffle(va1, va1, 1, 2, 3, 0);
114 va2 = wasm_v32x4_shuffle(va2, va2, 1, 2, 3, 0);
115
116 const v128_t vb0123c3 = wasm_v128_load(w + 24);
117 const v128_t vb4567c3 = wasm_v128_load(w + 28);
118
119 vacc0x0123 = wasm_f32x4_add(vacc0x0123, wasm_f32x4_mul(va0, vb0123c3));
120 vacc1x0123 = wasm_f32x4_add(vacc1x0123, wasm_f32x4_mul(va1, vb0123c3));
121 vacc2x0123 = wasm_f32x4_add(vacc2x0123, wasm_f32x4_mul(va2, vb0123c3));
122 vacc0x4567 = wasm_f32x4_add(vacc0x4567, wasm_f32x4_mul(va0, vb4567c3));
123 vacc1x4567 = wasm_f32x4_add(vacc1x4567, wasm_f32x4_mul(va1, vb4567c3));
124 vacc2x4567 = wasm_f32x4_add(vacc2x4567, wasm_f32x4_mul(va2, vb4567c3));
125
126
127 w += 32;
128 k -= 4 * sizeof(float);
129 }
130 if XNN_UNLIKELY(k != 0) {
131 v128_t va0 = wasm_v128_load(a0);
132 a0 = (const float*) ((uintptr_t) a0 + k);
133 v128_t va1 = wasm_v128_load(a1);
134 a1 = (const float*) ((uintptr_t) a1 + k);
135 v128_t va2 = wasm_v128_load(a2);
136 a2 = (const float*) ((uintptr_t) a2 + k);
137
138 const v128_t vzero = wasm_f32x4_const_splat(0.0f);
139
140 const v128_t vb0123c0 = wasm_v128_load(w + 0);
141 const v128_t vb4567c0 = wasm_v128_load(w + 4);
142
143 vacc0x0123 = wasm_f32x4_add(vacc0x0123, wasm_f32x4_mul(wasm_v128_andnot(va0, wasm_f32x4_eq(vb0123c0, vzero)), vb0123c0));
144 vacc1x0123 = wasm_f32x4_add(vacc1x0123, wasm_f32x4_mul(wasm_v128_andnot(va1, wasm_f32x4_eq(vb0123c0, vzero)), vb0123c0));
145 vacc2x0123 = wasm_f32x4_add(vacc2x0123, wasm_f32x4_mul(wasm_v128_andnot(va2, wasm_f32x4_eq(vb0123c0, vzero)), vb0123c0));
146 vacc0x4567 = wasm_f32x4_add(vacc0x4567, wasm_f32x4_mul(wasm_v128_andnot(va0, wasm_f32x4_eq(vb4567c0, vzero)), vb4567c0));
147 vacc1x4567 = wasm_f32x4_add(vacc1x4567, wasm_f32x4_mul(wasm_v128_andnot(va1, wasm_f32x4_eq(vb4567c0, vzero)), vb4567c0));
148 vacc2x4567 = wasm_f32x4_add(vacc2x4567, wasm_f32x4_mul(wasm_v128_andnot(va2, wasm_f32x4_eq(vb4567c0, vzero)), vb4567c0));
149
150 va0 = wasm_v32x4_shuffle(va0, va0, 1, 2, 3, 0);
151 va1 = wasm_v32x4_shuffle(va1, va1, 1, 2, 3, 0);
152 va2 = wasm_v32x4_shuffle(va2, va2, 1, 2, 3, 0);
153
154 const v128_t vb0123c1 = wasm_v128_load(w + 8);
155 const v128_t vb4567c1 = wasm_v128_load(w + 12);
156
157 vacc0x0123 = wasm_f32x4_add(vacc0x0123, wasm_f32x4_mul(wasm_v128_andnot(va0, wasm_f32x4_eq(vb0123c1, vzero)), vb0123c1));
158 vacc1x0123 = wasm_f32x4_add(vacc1x0123, wasm_f32x4_mul(wasm_v128_andnot(va1, wasm_f32x4_eq(vb0123c1, vzero)), vb0123c1));
159 vacc2x0123 = wasm_f32x4_add(vacc2x0123, wasm_f32x4_mul(wasm_v128_andnot(va2, wasm_f32x4_eq(vb0123c1, vzero)), vb0123c1));
160 vacc0x4567 = wasm_f32x4_add(vacc0x4567, wasm_f32x4_mul(wasm_v128_andnot(va0, wasm_f32x4_eq(vb4567c1, vzero)), vb4567c1));
161 vacc1x4567 = wasm_f32x4_add(vacc1x4567, wasm_f32x4_mul(wasm_v128_andnot(va1, wasm_f32x4_eq(vb4567c1, vzero)), vb4567c1));
162 vacc2x4567 = wasm_f32x4_add(vacc2x4567, wasm_f32x4_mul(wasm_v128_andnot(va2, wasm_f32x4_eq(vb4567c1, vzero)), vb4567c1));
163
164 va0 = wasm_v32x4_shuffle(va0, va0, 1, 2, 3, 0);
165 va1 = wasm_v32x4_shuffle(va1, va1, 1, 2, 3, 0);
166 va2 = wasm_v32x4_shuffle(va2, va2, 1, 2, 3, 0);
167
168 const v128_t vb0123c2 = wasm_v128_load(w + 16);
169 const v128_t vb4567c2 = wasm_v128_load(w + 20);
170
171 vacc0x0123 = wasm_f32x4_add(vacc0x0123, wasm_f32x4_mul(wasm_v128_andnot(va0, wasm_f32x4_eq(vb0123c2, vzero)), vb0123c2));
172 vacc1x0123 = wasm_f32x4_add(vacc1x0123, wasm_f32x4_mul(wasm_v128_andnot(va1, wasm_f32x4_eq(vb0123c2, vzero)), vb0123c2));
173 vacc2x0123 = wasm_f32x4_add(vacc2x0123, wasm_f32x4_mul(wasm_v128_andnot(va2, wasm_f32x4_eq(vb0123c2, vzero)), vb0123c2));
174 vacc0x4567 = wasm_f32x4_add(vacc0x4567, wasm_f32x4_mul(wasm_v128_andnot(va0, wasm_f32x4_eq(vb4567c2, vzero)), vb4567c2));
175 vacc1x4567 = wasm_f32x4_add(vacc1x4567, wasm_f32x4_mul(wasm_v128_andnot(va1, wasm_f32x4_eq(vb4567c2, vzero)), vb4567c2));
176 vacc2x4567 = wasm_f32x4_add(vacc2x4567, wasm_f32x4_mul(wasm_v128_andnot(va2, wasm_f32x4_eq(vb4567c2, vzero)), vb4567c2));
177
178 va0 = wasm_v32x4_shuffle(va0, va0, 1, 2, 3, 0);
179 va1 = wasm_v32x4_shuffle(va1, va1, 1, 2, 3, 0);
180 va2 = wasm_v32x4_shuffle(va2, va2, 1, 2, 3, 0);
181
182 const v128_t vb0123c3 = wasm_v128_load(w + 24);
183 const v128_t vb4567c3 = wasm_v128_load(w + 28);
184
185 vacc0x0123 = wasm_f32x4_add(vacc0x0123, wasm_f32x4_mul(wasm_v128_andnot(va0, wasm_f32x4_eq(vb0123c3, vzero)), vb0123c3));
186 vacc1x0123 = wasm_f32x4_add(vacc1x0123, wasm_f32x4_mul(wasm_v128_andnot(va1, wasm_f32x4_eq(vb0123c3, vzero)), vb0123c3));
187 vacc2x0123 = wasm_f32x4_add(vacc2x0123, wasm_f32x4_mul(wasm_v128_andnot(va2, wasm_f32x4_eq(vb0123c3, vzero)), vb0123c3));
188 vacc0x4567 = wasm_f32x4_add(vacc0x4567, wasm_f32x4_mul(wasm_v128_andnot(va0, wasm_f32x4_eq(vb4567c3, vzero)), vb4567c3));
189 vacc1x4567 = wasm_f32x4_add(vacc1x4567, wasm_f32x4_mul(wasm_v128_andnot(va1, wasm_f32x4_eq(vb4567c3, vzero)), vb4567c3));
190 vacc2x4567 = wasm_f32x4_add(vacc2x4567, wasm_f32x4_mul(wasm_v128_andnot(va2, wasm_f32x4_eq(vb4567c3, vzero)), vb4567c3));
191
192
193 w += 32;
194 }
195
196 vacc0x0123 = wasm_f32x4_pmax(vmin, vacc0x0123);
197 vacc1x0123 = wasm_f32x4_pmax(vmin, vacc1x0123);
198 vacc2x0123 = wasm_f32x4_pmax(vmin, vacc2x0123);
199 vacc0x4567 = wasm_f32x4_pmax(vmin, vacc0x4567);
200 vacc1x4567 = wasm_f32x4_pmax(vmin, vacc1x4567);
201 vacc2x4567 = wasm_f32x4_pmax(vmin, vacc2x4567);
202
203 vacc0x0123 = wasm_f32x4_pmin(vmax, vacc0x0123);
204 vacc1x0123 = wasm_f32x4_pmin(vmax, vacc1x0123);
205 vacc2x0123 = wasm_f32x4_pmin(vmax, vacc2x0123);
206 vacc0x4567 = wasm_f32x4_pmin(vmax, vacc0x4567);
207 vacc1x4567 = wasm_f32x4_pmin(vmax, vacc1x4567);
208 vacc2x4567 = wasm_f32x4_pmin(vmax, vacc2x4567);
209
210 if XNN_LIKELY(nc >= 8) {
211 wasm_v128_store(c2, vacc2x0123);
212 wasm_v128_store(c2 + 4, vacc2x4567);
213 c2 = (float*) ((uintptr_t) c2 + cn_stride);
214 wasm_v128_store(c1, vacc1x0123);
215 wasm_v128_store(c1 + 4, vacc1x4567);
216 c1 = (float*) ((uintptr_t) c1 + cn_stride);
217 wasm_v128_store(c0, vacc0x0123);
218 wasm_v128_store(c0 + 4, vacc0x4567);
219 c0 = (float*) ((uintptr_t) c0 + cn_stride);
220
221 a2 = (const float*) ((uintptr_t) a2 - kc);
222 a1 = (const float*) ((uintptr_t) a1 - kc);
223 a0 = (const float*) ((uintptr_t) a0 - kc);
224
225 nc -= 8;
226 } else {
227 if (nc & 4) {
228 wasm_v128_store(c2, vacc2x0123);
229 wasm_v128_store(c1, vacc1x0123);
230 wasm_v128_store(c0, vacc0x0123);
231
232 vacc2x0123 = vacc2x4567;
233 vacc1x0123 = vacc1x4567;
234 vacc0x0123 = vacc0x4567;
235
236 c2 += 4;
237 c1 += 4;
238 c0 += 4;
239 }
240 if (nc & 2) {
241 *((double*) c2) = wasm_f64x2_extract_lane(vacc2x0123, 0);
242 *((double*) c1) = wasm_f64x2_extract_lane(vacc1x0123, 0);
243 *((double*) c0) = wasm_f64x2_extract_lane(vacc0x0123, 0);
244
245 vacc2x0123 = wasm_v32x4_shuffle(vacc2x0123, vacc2x0123, 2, 3, 2, 3);
246 vacc1x0123 = wasm_v32x4_shuffle(vacc1x0123, vacc1x0123, 2, 3, 2, 3);
247 vacc0x0123 = wasm_v32x4_shuffle(vacc0x0123, vacc0x0123, 2, 3, 2, 3);
248
249 c2 += 2;
250 c1 += 2;
251 c0 += 2;
252 }
253 if (nc & 1) {
254 *c2 = wasm_f32x4_extract_lane(vacc2x0123, 0);
255 *c1 = wasm_f32x4_extract_lane(vacc1x0123, 0);
256 *c0 = wasm_f32x4_extract_lane(vacc0x0123, 0);
257 }
258
259 nc = 0;
260 }
261 } while (nc != 0);
262 }
263