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