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_gemminc_minmax_ukernel_5x8s4__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 float * restrict acc,const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])17 void xnn_f32_gemminc_minmax_ukernel_5x8s4__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 float*restrict acc,
28     const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS(1)])
29 {
30   assert(mr != 0);
31   assert(mr <= 5);
32   assert(nc != 0);
33   assert(kc != 0);
34   assert(kc % sizeof(float) == 0);
35   assert(a != NULL);
36   assert(w != NULL);
37   assert(c != NULL);
38   assert(acc != NULL);
39 
40   const float* a0 = a;
41   float* c0 = c;
42   const float* a1 = (const float*) ((uintptr_t) a0 + a_stride);
43   float* c1 = (float*) ((uintptr_t) c0 + cm_stride);
44   if XNN_UNPREDICTABLE(mr < 2) {
45     a1 = a0;
46     c1 = c0;
47   }
48   const float* a2 = (const float*) ((uintptr_t) a1 + a_stride);
49   float* c2 = (float*) ((uintptr_t) c1 + cm_stride);
50   if XNN_UNPREDICTABLE(mr <= 2) {
51     a2 = a1;
52     c2 = c1;
53   }
54   const float* a3 = (const float*) ((uintptr_t) a2 + a_stride);
55   float* c3 = (float*) ((uintptr_t) c2 + cm_stride);
56   if XNN_UNPREDICTABLE(mr < 4) {
57     a3 = a2;
58     c3 = c2;
59   }
60   const float* a4 = (const float*) ((uintptr_t) a3 + a_stride);
61   float* c4 = (float*) ((uintptr_t) c3 + cm_stride);
62   if XNN_UNPREDICTABLE(mr <= 4) {
63     a4 = a3;
64     c4 = c3;
65   }
66 
67   const v128_t vmin = wasm_v32x4_load_splat(&params->scalar.min);
68   const v128_t vmax = wasm_v32x4_load_splat(&params->scalar.max);
69   do {
70     v128_t vacc0x0123 = wasm_v128_load(acc + 0);
71     v128_t vacc0x4567 = wasm_v128_load(acc + 4);
72     v128_t vacc1x0123 = wasm_v128_load(acc + 8);
73     v128_t vacc1x4567 = wasm_v128_load(acc + 12);
74     v128_t vacc2x0123 = wasm_v128_load(acc + 16);
75     v128_t vacc2x4567 = wasm_v128_load(acc + 20);
76     v128_t vacc3x0123 = wasm_v128_load(acc + 24);
77     v128_t vacc3x4567 = wasm_v128_load(acc + 28);
78     v128_t vacc4x0123 = wasm_v128_load(acc + 32);
79     v128_t vacc4x4567 = wasm_v128_load(acc + 36);
80     acc += 40;
81 
82     size_t k = kc;
83     while (k >= 4 * sizeof(float)) {
84       v128_t va0 = wasm_v128_load(a0);
85       a0 += 4;
86       v128_t va1 = wasm_v128_load(a1);
87       a1 += 4;
88       v128_t va2 = wasm_v128_load(a2);
89       a2 += 4;
90       v128_t va3 = wasm_v128_load(a3);
91       a3 += 4;
92       v128_t va4 = wasm_v128_load(a4);
93       a4 += 4;
94 
95 
96       const v128_t vb0123c0 = wasm_v128_load(w + 0);
97       const v128_t vb4567c0 = wasm_v128_load(w + 4);
98 
99       vacc0x0123 = wasm_f32x4_add(vacc0x0123, wasm_f32x4_mul(va0, vb0123c0));
100       vacc1x0123 = wasm_f32x4_add(vacc1x0123, wasm_f32x4_mul(va1, vb0123c0));
101       vacc2x0123 = wasm_f32x4_add(vacc2x0123, wasm_f32x4_mul(va2, vb0123c0));
102       vacc3x0123 = wasm_f32x4_add(vacc3x0123, wasm_f32x4_mul(va3, vb0123c0));
103       vacc4x0123 = wasm_f32x4_add(vacc4x0123, wasm_f32x4_mul(va4, vb0123c0));
104       vacc0x4567 = wasm_f32x4_add(vacc0x4567, wasm_f32x4_mul(va0, vb4567c0));
105       vacc1x4567 = wasm_f32x4_add(vacc1x4567, wasm_f32x4_mul(va1, vb4567c0));
106       vacc2x4567 = wasm_f32x4_add(vacc2x4567, wasm_f32x4_mul(va2, vb4567c0));
107       vacc3x4567 = wasm_f32x4_add(vacc3x4567, wasm_f32x4_mul(va3, vb4567c0));
108       vacc4x4567 = wasm_f32x4_add(vacc4x4567, wasm_f32x4_mul(va4, vb4567c0));
109 
110       va0 = wasm_v32x4_shuffle(va0, va0, 1, 2, 3, 0);
111       va1 = wasm_v32x4_shuffle(va1, va1, 1, 2, 3, 0);
112       va2 = wasm_v32x4_shuffle(va2, va2, 1, 2, 3, 0);
113       va3 = wasm_v32x4_shuffle(va3, va3, 1, 2, 3, 0);
114       va4 = wasm_v32x4_shuffle(va4, va4, 1, 2, 3, 0);
115 
116       const v128_t vb0123c1 = wasm_v128_load(w + 8);
117       const v128_t vb4567c1 = wasm_v128_load(w + 12);
118 
119       vacc0x0123 = wasm_f32x4_add(vacc0x0123, wasm_f32x4_mul(va0, vb0123c1));
120       vacc1x0123 = wasm_f32x4_add(vacc1x0123, wasm_f32x4_mul(va1, vb0123c1));
121       vacc2x0123 = wasm_f32x4_add(vacc2x0123, wasm_f32x4_mul(va2, vb0123c1));
122       vacc3x0123 = wasm_f32x4_add(vacc3x0123, wasm_f32x4_mul(va3, vb0123c1));
123       vacc4x0123 = wasm_f32x4_add(vacc4x0123, wasm_f32x4_mul(va4, vb0123c1));
124       vacc0x4567 = wasm_f32x4_add(vacc0x4567, wasm_f32x4_mul(va0, vb4567c1));
125       vacc1x4567 = wasm_f32x4_add(vacc1x4567, wasm_f32x4_mul(va1, vb4567c1));
126       vacc2x4567 = wasm_f32x4_add(vacc2x4567, wasm_f32x4_mul(va2, vb4567c1));
127       vacc3x4567 = wasm_f32x4_add(vacc3x4567, wasm_f32x4_mul(va3, vb4567c1));
128       vacc4x4567 = wasm_f32x4_add(vacc4x4567, wasm_f32x4_mul(va4, vb4567c1));
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       va4 = wasm_v32x4_shuffle(va4, va4, 1, 2, 3, 0);
135 
136       const v128_t vb0123c2 = wasm_v128_load(w + 16);
137       const v128_t vb4567c2 = wasm_v128_load(w + 20);
138 
139       vacc0x0123 = wasm_f32x4_add(vacc0x0123, wasm_f32x4_mul(va0, vb0123c2));
140       vacc1x0123 = wasm_f32x4_add(vacc1x0123, wasm_f32x4_mul(va1, vb0123c2));
141       vacc2x0123 = wasm_f32x4_add(vacc2x0123, wasm_f32x4_mul(va2, vb0123c2));
142       vacc3x0123 = wasm_f32x4_add(vacc3x0123, wasm_f32x4_mul(va3, vb0123c2));
143       vacc4x0123 = wasm_f32x4_add(vacc4x0123, wasm_f32x4_mul(va4, vb0123c2));
144       vacc0x4567 = wasm_f32x4_add(vacc0x4567, wasm_f32x4_mul(va0, vb4567c2));
145       vacc1x4567 = wasm_f32x4_add(vacc1x4567, wasm_f32x4_mul(va1, vb4567c2));
146       vacc2x4567 = wasm_f32x4_add(vacc2x4567, wasm_f32x4_mul(va2, vb4567c2));
147       vacc3x4567 = wasm_f32x4_add(vacc3x4567, wasm_f32x4_mul(va3, vb4567c2));
148       vacc4x4567 = wasm_f32x4_add(vacc4x4567, wasm_f32x4_mul(va4, vb4567c2));
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       va3 = wasm_v32x4_shuffle(va3, va3, 1, 2, 3, 0);
154       va4 = wasm_v32x4_shuffle(va4, va4, 1, 2, 3, 0);
155 
156       const v128_t vb0123c3 = wasm_v128_load(w + 24);
157       const v128_t vb4567c3 = wasm_v128_load(w + 28);
158 
159       vacc0x0123 = wasm_f32x4_add(vacc0x0123, wasm_f32x4_mul(va0, vb0123c3));
160       vacc1x0123 = wasm_f32x4_add(vacc1x0123, wasm_f32x4_mul(va1, vb0123c3));
161       vacc2x0123 = wasm_f32x4_add(vacc2x0123, wasm_f32x4_mul(va2, vb0123c3));
162       vacc3x0123 = wasm_f32x4_add(vacc3x0123, wasm_f32x4_mul(va3, vb0123c3));
163       vacc4x0123 = wasm_f32x4_add(vacc4x0123, wasm_f32x4_mul(va4, vb0123c3));
164       vacc0x4567 = wasm_f32x4_add(vacc0x4567, wasm_f32x4_mul(va0, vb4567c3));
165       vacc1x4567 = wasm_f32x4_add(vacc1x4567, wasm_f32x4_mul(va1, vb4567c3));
166       vacc2x4567 = wasm_f32x4_add(vacc2x4567, wasm_f32x4_mul(va2, vb4567c3));
167       vacc3x4567 = wasm_f32x4_add(vacc3x4567, wasm_f32x4_mul(va3, vb4567c3));
168       vacc4x4567 = wasm_f32x4_add(vacc4x4567, wasm_f32x4_mul(va4, vb4567c3));
169 
170 
171       w += 32;
172       k -= 4 * sizeof(float);
173     }
174     if XNN_UNLIKELY(k != 0) {
175       do {
176         const v128_t va0 = wasm_v32x4_load_splat(a0);
177         a0 += 1;
178         const v128_t va1 = wasm_v32x4_load_splat(a1);
179         a1 += 1;
180         const v128_t va2 = wasm_v32x4_load_splat(a2);
181         a2 += 1;
182         const v128_t va3 = wasm_v32x4_load_splat(a3);
183         a3 += 1;
184         const v128_t va4 = wasm_v32x4_load_splat(a4);
185         a4 += 1;
186 
187         const v128_t vb0123 = wasm_v128_load(w);
188         const v128_t vb4567 = wasm_v128_load(w + 4);
189         w += 8;
190 
191         vacc0x0123 = wasm_f32x4_add(vacc0x0123, wasm_f32x4_mul(va0, vb0123));
192         vacc1x0123 = wasm_f32x4_add(vacc1x0123, wasm_f32x4_mul(va1, vb0123));
193         vacc2x0123 = wasm_f32x4_add(vacc2x0123, wasm_f32x4_mul(va2, vb0123));
194         vacc3x0123 = wasm_f32x4_add(vacc3x0123, wasm_f32x4_mul(va3, vb0123));
195         vacc4x0123 = wasm_f32x4_add(vacc4x0123, wasm_f32x4_mul(va4, vb0123));
196         vacc0x4567 = wasm_f32x4_add(vacc0x4567, wasm_f32x4_mul(va0, vb4567));
197         vacc1x4567 = wasm_f32x4_add(vacc1x4567, wasm_f32x4_mul(va1, vb4567));
198         vacc2x4567 = wasm_f32x4_add(vacc2x4567, wasm_f32x4_mul(va2, vb4567));
199         vacc3x4567 = wasm_f32x4_add(vacc3x4567, wasm_f32x4_mul(va3, vb4567));
200         vacc4x4567 = wasm_f32x4_add(vacc4x4567, wasm_f32x4_mul(va4, vb4567));
201 
202         k -= sizeof(float);
203       } while (k != 0);
204     }
205 
206     vacc0x0123 = wasm_f32x4_max(vacc0x0123, vmin);
207     vacc1x0123 = wasm_f32x4_max(vacc1x0123, vmin);
208     vacc2x0123 = wasm_f32x4_max(vacc2x0123, vmin);
209     vacc3x0123 = wasm_f32x4_max(vacc3x0123, vmin);
210     vacc4x0123 = wasm_f32x4_max(vacc4x0123, vmin);
211     vacc0x4567 = wasm_f32x4_max(vacc0x4567, vmin);
212     vacc1x4567 = wasm_f32x4_max(vacc1x4567, vmin);
213     vacc2x4567 = wasm_f32x4_max(vacc2x4567, vmin);
214     vacc3x4567 = wasm_f32x4_max(vacc3x4567, vmin);
215     vacc4x4567 = wasm_f32x4_max(vacc4x4567, vmin);
216 
217     vacc0x0123 = wasm_f32x4_min(vacc0x0123, vmax);
218     vacc1x0123 = wasm_f32x4_min(vacc1x0123, vmax);
219     vacc2x0123 = wasm_f32x4_min(vacc2x0123, vmax);
220     vacc3x0123 = wasm_f32x4_min(vacc3x0123, vmax);
221     vacc4x0123 = wasm_f32x4_min(vacc4x0123, vmax);
222     vacc0x4567 = wasm_f32x4_min(vacc0x4567, vmax);
223     vacc1x4567 = wasm_f32x4_min(vacc1x4567, vmax);
224     vacc2x4567 = wasm_f32x4_min(vacc2x4567, vmax);
225     vacc3x4567 = wasm_f32x4_min(vacc3x4567, vmax);
226     vacc4x4567 = wasm_f32x4_min(vacc4x4567, vmax);
227 
228     if XNN_LIKELY(nc >= 8) {
229       wasm_v128_store(c4, vacc4x0123);
230       wasm_v128_store(c4 + 4, vacc4x4567);
231       c4 = (float*) ((uintptr_t) c4 + cn_stride);
232       wasm_v128_store(c3, vacc3x0123);
233       wasm_v128_store(c3 + 4, vacc3x4567);
234       c3 = (float*) ((uintptr_t) c3 + cn_stride);
235       wasm_v128_store(c2, vacc2x0123);
236       wasm_v128_store(c2 + 4, vacc2x4567);
237       c2 = (float*) ((uintptr_t) c2 + cn_stride);
238       wasm_v128_store(c1, vacc1x0123);
239       wasm_v128_store(c1 + 4, vacc1x4567);
240       c1 = (float*) ((uintptr_t) c1 + cn_stride);
241       wasm_v128_store(c0, vacc0x0123);
242       wasm_v128_store(c0 + 4, vacc0x4567);
243       c0 = (float*) ((uintptr_t) c0 + cn_stride);
244 
245       a4 = (const float*) ((uintptr_t) a4 - kc);
246       a3 = (const float*) ((uintptr_t) a3 - kc);
247       a2 = (const float*) ((uintptr_t) a2 - kc);
248       a1 = (const float*) ((uintptr_t) a1 - kc);
249       a0 = (const float*) ((uintptr_t) a0 - kc);
250 
251       nc -= 8;
252     } else {
253       if (nc & 4) {
254         wasm_v128_store(c4, vacc4x0123);
255         wasm_v128_store(c3, vacc3x0123);
256         wasm_v128_store(c2, vacc2x0123);
257         wasm_v128_store(c1, vacc1x0123);
258         wasm_v128_store(c0, vacc0x0123);
259 
260         vacc4x0123 = vacc4x4567;
261         vacc3x0123 = vacc3x4567;
262         vacc2x0123 = vacc2x4567;
263         vacc1x0123 = vacc1x4567;
264         vacc0x0123 = vacc0x4567;
265 
266         c4 += 4;
267         c3 += 4;
268         c2 += 4;
269         c1 += 4;
270         c0 += 4;
271       }
272       if (nc & 2) {
273         *((double*) c4) = wasm_f64x2_extract_lane(vacc4x0123, 0);
274         *((double*) c3) = wasm_f64x2_extract_lane(vacc3x0123, 0);
275         *((double*) c2) = wasm_f64x2_extract_lane(vacc2x0123, 0);
276         *((double*) c1) = wasm_f64x2_extract_lane(vacc1x0123, 0);
277         *((double*) c0) = wasm_f64x2_extract_lane(vacc0x0123, 0);
278 
279         vacc4x0123 = wasm_v32x4_shuffle(vacc4x0123, vacc4x0123, 2, 3, 2, 3);
280         vacc3x0123 = wasm_v32x4_shuffle(vacc3x0123, vacc3x0123, 2, 3, 2, 3);
281         vacc2x0123 = wasm_v32x4_shuffle(vacc2x0123, vacc2x0123, 2, 3, 2, 3);
282         vacc1x0123 = wasm_v32x4_shuffle(vacc1x0123, vacc1x0123, 2, 3, 2, 3);
283         vacc0x0123 = wasm_v32x4_shuffle(vacc0x0123, vacc0x0123, 2, 3, 2, 3);
284 
285         c4 += 2;
286         c3 += 2;
287         c2 += 2;
288         c1 += 2;
289         c0 += 2;
290       }
291       if (nc & 1) {
292         *c4 = wasm_f32x4_extract_lane(vacc4x0123, 0);
293         *c3 = wasm_f32x4_extract_lane(vacc3x0123, 0);
294         *c2 = wasm_f32x4_extract_lane(vacc2x0123, 0);
295         *c1 = wasm_f32x4_extract_lane(vacc1x0123, 0);
296         *c0 = wasm_f32x4_extract_lane(vacc0x0123, 0);
297       }
298 
299       nc = 0;
300     }
301   } while (nc != 0);
302 }
303