• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-gemm/wasmsimd-splat.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_relu_ukernel_5x8__wasmrelaxedsimd_fma_splat(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_relu_params params[restrict XNN_MIN_ELEMENTS (1)])17 void xnn_f32_gemm_relu_ukernel_5x8__wasmrelaxedsimd_fma_splat(
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_relu_params params[restrict XNN_MIN_ELEMENTS(1)])
28 {
29   assert(mr != 0);
30   assert(mr <= 5);
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   const float* a4 = (const float*) ((uintptr_t) a3 + a_stride);
59   float* c4 = (float*) ((uintptr_t) c3 + cm_stride);
60   if XNN_UNPREDICTABLE(mr <= 4) {
61     a4 = a3;
62     c4 = c3;
63   }
64 
65   do {
66     v128_t vacc0x0123 = wasm_v128_load(w + 0);
67     v128_t vacc0x4567 = wasm_v128_load(w + 4);
68     v128_t vacc1x0123 = vacc0x0123;
69     v128_t vacc1x4567 = vacc0x4567;
70     v128_t vacc2x0123 = vacc0x0123;
71     v128_t vacc2x4567 = vacc0x4567;
72     v128_t vacc3x0123 = vacc0x0123;
73     v128_t vacc3x4567 = vacc0x4567;
74     v128_t vacc4x0123 = vacc0x0123;
75     v128_t vacc4x4567 = vacc0x4567;
76     w += 8;
77 
78     size_t k = kc;
79     while (k >= 4 * sizeof(float)) {
80       const v128_t va0 = wasm_v128_load(a0);
81       a0 += 4;
82       const v128_t va1 = wasm_v128_load(a1);
83       a1 += 4;
84       const v128_t va2 = wasm_v128_load(a2);
85       a2 += 4;
86       const v128_t va3 = wasm_v128_load(a3);
87       a3 += 4;
88       const v128_t va4 = wasm_v128_load(a4);
89       a4 += 4;
90 
91       const v128_t va0c0 = wasm_v32x4_shuffle(va0, va0, 0, 0, 0, 0);
92       const v128_t va1c0 = wasm_v32x4_shuffle(va1, va1, 0, 0, 0, 0);
93       const v128_t va2c0 = wasm_v32x4_shuffle(va2, va2, 0, 0, 0, 0);
94       const v128_t va3c0 = wasm_v32x4_shuffle(va3, va3, 0, 0, 0, 0);
95       const v128_t va4c0 = wasm_v32x4_shuffle(va4, va4, 0, 0, 0, 0);
96 
97       const v128_t vb0123c0 = wasm_v128_load(w + 0);
98       const v128_t vb4567c0 = wasm_v128_load(w + 4);
99 
100       vacc0x0123 = wasm_f32x4_add(vacc0x0123, wasm_f32x4_mul(va0c0, vb0123c0));
101       vacc1x0123 = wasm_f32x4_add(vacc1x0123, wasm_f32x4_mul(va1c0, vb0123c0));
102       vacc2x0123 = wasm_f32x4_add(vacc2x0123, wasm_f32x4_mul(va2c0, vb0123c0));
103       vacc3x0123 = wasm_f32x4_add(vacc3x0123, wasm_f32x4_mul(va3c0, vb0123c0));
104       vacc4x0123 = wasm_f32x4_add(vacc4x0123, wasm_f32x4_mul(va4c0, vb0123c0));
105       vacc0x4567 = wasm_f32x4_add(vacc0x4567, wasm_f32x4_mul(va0c0, vb4567c0));
106       vacc1x4567 = wasm_f32x4_add(vacc1x4567, wasm_f32x4_mul(va1c0, vb4567c0));
107       vacc2x4567 = wasm_f32x4_add(vacc2x4567, wasm_f32x4_mul(va2c0, vb4567c0));
108       vacc3x4567 = wasm_f32x4_add(vacc3x4567, wasm_f32x4_mul(va3c0, vb4567c0));
109       vacc4x4567 = wasm_f32x4_add(vacc4x4567, wasm_f32x4_mul(va4c0, vb4567c0));
110       const v128_t va0c1 = wasm_v32x4_shuffle(va0, va0, 1, 1, 1, 1);
111       const v128_t va1c1 = wasm_v32x4_shuffle(va1, va1, 1, 1, 1, 1);
112       const v128_t va2c1 = wasm_v32x4_shuffle(va2, va2, 1, 1, 1, 1);
113       const v128_t va3c1 = wasm_v32x4_shuffle(va3, va3, 1, 1, 1, 1);
114       const v128_t va4c1 = wasm_v32x4_shuffle(va4, va4, 1, 1, 1, 1);
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(va0c1, vb0123c1));
120       vacc1x0123 = wasm_f32x4_add(vacc1x0123, wasm_f32x4_mul(va1c1, vb0123c1));
121       vacc2x0123 = wasm_f32x4_add(vacc2x0123, wasm_f32x4_mul(va2c1, vb0123c1));
122       vacc3x0123 = wasm_f32x4_add(vacc3x0123, wasm_f32x4_mul(va3c1, vb0123c1));
123       vacc4x0123 = wasm_f32x4_add(vacc4x0123, wasm_f32x4_mul(va4c1, vb0123c1));
124       vacc0x4567 = wasm_f32x4_add(vacc0x4567, wasm_f32x4_mul(va0c1, vb4567c1));
125       vacc1x4567 = wasm_f32x4_add(vacc1x4567, wasm_f32x4_mul(va1c1, vb4567c1));
126       vacc2x4567 = wasm_f32x4_add(vacc2x4567, wasm_f32x4_mul(va2c1, vb4567c1));
127       vacc3x4567 = wasm_f32x4_add(vacc3x4567, wasm_f32x4_mul(va3c1, vb4567c1));
128       vacc4x4567 = wasm_f32x4_add(vacc4x4567, wasm_f32x4_mul(va4c1, vb4567c1));
129       const v128_t va0c2 = wasm_v32x4_shuffle(va0, va0, 2, 2, 2, 2);
130       const v128_t va1c2 = wasm_v32x4_shuffle(va1, va1, 2, 2, 2, 2);
131       const v128_t va2c2 = wasm_v32x4_shuffle(va2, va2, 2, 2, 2, 2);
132       const v128_t va3c2 = wasm_v32x4_shuffle(va3, va3, 2, 2, 2, 2);
133       const v128_t va4c2 = wasm_v32x4_shuffle(va4, va4, 2, 2, 2, 2);
134 
135       const v128_t vb0123c2 = wasm_v128_load(w + 16);
136       const v128_t vb4567c2 = wasm_v128_load(w + 20);
137 
138       vacc0x0123 = wasm_f32x4_add(vacc0x0123, wasm_f32x4_mul(va0c2, vb0123c2));
139       vacc1x0123 = wasm_f32x4_add(vacc1x0123, wasm_f32x4_mul(va1c2, vb0123c2));
140       vacc2x0123 = wasm_f32x4_add(vacc2x0123, wasm_f32x4_mul(va2c2, vb0123c2));
141       vacc3x0123 = wasm_f32x4_add(vacc3x0123, wasm_f32x4_mul(va3c2, vb0123c2));
142       vacc4x0123 = wasm_f32x4_add(vacc4x0123, wasm_f32x4_mul(va4c2, vb0123c2));
143       vacc0x4567 = wasm_f32x4_add(vacc0x4567, wasm_f32x4_mul(va0c2, vb4567c2));
144       vacc1x4567 = wasm_f32x4_add(vacc1x4567, wasm_f32x4_mul(va1c2, vb4567c2));
145       vacc2x4567 = wasm_f32x4_add(vacc2x4567, wasm_f32x4_mul(va2c2, vb4567c2));
146       vacc3x4567 = wasm_f32x4_add(vacc3x4567, wasm_f32x4_mul(va3c2, vb4567c2));
147       vacc4x4567 = wasm_f32x4_add(vacc4x4567, wasm_f32x4_mul(va4c2, vb4567c2));
148       const v128_t va0c3 = wasm_v32x4_shuffle(va0, va0, 3, 3, 3, 3);
149       const v128_t va1c3 = wasm_v32x4_shuffle(va1, va1, 3, 3, 3, 3);
150       const v128_t va2c3 = wasm_v32x4_shuffle(va2, va2, 3, 3, 3, 3);
151       const v128_t va3c3 = wasm_v32x4_shuffle(va3, va3, 3, 3, 3, 3);
152       const v128_t va4c3 = wasm_v32x4_shuffle(va4, va4, 3, 3, 3, 3);
153 
154       const v128_t vb0123c3 = wasm_v128_load(w + 24);
155       const v128_t vb4567c3 = wasm_v128_load(w + 28);
156 
157       vacc0x0123 = wasm_f32x4_add(vacc0x0123, wasm_f32x4_mul(va0c3, vb0123c3));
158       vacc1x0123 = wasm_f32x4_add(vacc1x0123, wasm_f32x4_mul(va1c3, vb0123c3));
159       vacc2x0123 = wasm_f32x4_add(vacc2x0123, wasm_f32x4_mul(va2c3, vb0123c3));
160       vacc3x0123 = wasm_f32x4_add(vacc3x0123, wasm_f32x4_mul(va3c3, vb0123c3));
161       vacc4x0123 = wasm_f32x4_add(vacc4x0123, wasm_f32x4_mul(va4c3, vb0123c3));
162       vacc0x4567 = wasm_f32x4_add(vacc0x4567, wasm_f32x4_mul(va0c3, vb4567c3));
163       vacc1x4567 = wasm_f32x4_add(vacc1x4567, wasm_f32x4_mul(va1c3, vb4567c3));
164       vacc2x4567 = wasm_f32x4_add(vacc2x4567, wasm_f32x4_mul(va2c3, vb4567c3));
165       vacc3x4567 = wasm_f32x4_add(vacc3x4567, wasm_f32x4_mul(va3c3, vb4567c3));
166       vacc4x4567 = wasm_f32x4_add(vacc4x4567, wasm_f32x4_mul(va4c3, vb4567c3));
167 
168       w += 32;
169       k -= 4 * sizeof(float);
170     }
171     if XNN_UNLIKELY(k != 0) {
172       do {
173         const v128_t va0 = wasm_v128_load32_splat(a0);
174         a0 += 1;
175         const v128_t va1 = wasm_v128_load32_splat(a1);
176         a1 += 1;
177         const v128_t va2 = wasm_v128_load32_splat(a2);
178         a2 += 1;
179         const v128_t va3 = wasm_v128_load32_splat(a3);
180         a3 += 1;
181         const v128_t va4 = wasm_v128_load32_splat(a4);
182         a4 += 1;
183 
184         const v128_t vb0123 = wasm_v128_load(w);
185         const v128_t vb4567 = wasm_v128_load(w + 4);
186         w += 8;
187 
188         vacc0x0123 = __builtin_wasm_fma_f32x4(vacc0x0123, va0, vb0123);
189         vacc1x0123 = __builtin_wasm_fma_f32x4(vacc1x0123, va1, vb0123);
190         vacc2x0123 = __builtin_wasm_fma_f32x4(vacc2x0123, va2, vb0123);
191         vacc3x0123 = __builtin_wasm_fma_f32x4(vacc3x0123, va3, vb0123);
192         vacc4x0123 = __builtin_wasm_fma_f32x4(vacc4x0123, va4, vb0123);
193         vacc0x4567 = __builtin_wasm_fma_f32x4(vacc0x4567, va0, vb4567);
194         vacc1x4567 = __builtin_wasm_fma_f32x4(vacc1x4567, va1, vb4567);
195         vacc2x4567 = __builtin_wasm_fma_f32x4(vacc2x4567, va2, vb4567);
196         vacc3x4567 = __builtin_wasm_fma_f32x4(vacc3x4567, va3, vb4567);
197         vacc4x4567 = __builtin_wasm_fma_f32x4(vacc4x4567, va4, vb4567);
198 
199         k -= sizeof(float);
200       } while (k != 0);
201     }
202 
203     const v128_t vzero = wasm_i32x4_const_splat(0);
204     vacc0x0123 = wasm_i32x4_max(vacc0x0123, vzero);
205     vacc1x0123 = wasm_i32x4_max(vacc1x0123, vzero);
206     vacc2x0123 = wasm_i32x4_max(vacc2x0123, vzero);
207     vacc3x0123 = wasm_i32x4_max(vacc3x0123, vzero);
208     vacc4x0123 = wasm_i32x4_max(vacc4x0123, vzero);
209     vacc0x4567 = wasm_i32x4_max(vacc0x4567, vzero);
210     vacc1x4567 = wasm_i32x4_max(vacc1x4567, vzero);
211     vacc2x4567 = wasm_i32x4_max(vacc2x4567, vzero);
212     vacc3x4567 = wasm_i32x4_max(vacc3x4567, vzero);
213     vacc4x4567 = wasm_i32x4_max(vacc4x4567, vzero);
214 
215     if XNN_LIKELY(nc >= 8) {
216       wasm_v128_store(c4, vacc4x0123);
217       wasm_v128_store(c4 + 4, vacc4x4567);
218       c4 = (float*) ((uintptr_t) c4 + cn_stride);
219       wasm_v128_store(c3, vacc3x0123);
220       wasm_v128_store(c3 + 4, vacc3x4567);
221       c3 = (float*) ((uintptr_t) c3 + cn_stride);
222       wasm_v128_store(c2, vacc2x0123);
223       wasm_v128_store(c2 + 4, vacc2x4567);
224       c2 = (float*) ((uintptr_t) c2 + cn_stride);
225       wasm_v128_store(c1, vacc1x0123);
226       wasm_v128_store(c1 + 4, vacc1x4567);
227       c1 = (float*) ((uintptr_t) c1 + cn_stride);
228       wasm_v128_store(c0, vacc0x0123);
229       wasm_v128_store(c0 + 4, vacc0x4567);
230       c0 = (float*) ((uintptr_t) c0 + cn_stride);
231 
232       a4 = (const float*) ((uintptr_t) a4 - kc);
233       a3 = (const float*) ((uintptr_t) a3 - kc);
234       a2 = (const float*) ((uintptr_t) a2 - kc);
235       a1 = (const float*) ((uintptr_t) a1 - kc);
236       a0 = (const float*) ((uintptr_t) a0 - kc);
237 
238       nc -= 8;
239     } else {
240       if (nc & 4) {
241         wasm_v128_store(c4, vacc4x0123);
242         wasm_v128_store(c3, vacc3x0123);
243         wasm_v128_store(c2, vacc2x0123);
244         wasm_v128_store(c1, vacc1x0123);
245         wasm_v128_store(c0, vacc0x0123);
246 
247         vacc4x0123 = vacc4x4567;
248         vacc3x0123 = vacc3x4567;
249         vacc2x0123 = vacc2x4567;
250         vacc1x0123 = vacc1x4567;
251         vacc0x0123 = vacc0x4567;
252 
253         c4 += 4;
254         c3 += 4;
255         c2 += 4;
256         c1 += 4;
257         c0 += 4;
258       }
259       if (nc & 2) {
260         *((double*) c4) = wasm_f64x2_extract_lane(vacc4x0123, 0);
261         *((double*) c3) = wasm_f64x2_extract_lane(vacc3x0123, 0);
262         *((double*) c2) = wasm_f64x2_extract_lane(vacc2x0123, 0);
263         *((double*) c1) = wasm_f64x2_extract_lane(vacc1x0123, 0);
264         *((double*) c0) = wasm_f64x2_extract_lane(vacc0x0123, 0);
265 
266         vacc4x0123 = wasm_v32x4_shuffle(vacc4x0123, vacc4x0123, 2, 3, 2, 3);
267         vacc3x0123 = wasm_v32x4_shuffle(vacc3x0123, vacc3x0123, 2, 3, 2, 3);
268         vacc2x0123 = wasm_v32x4_shuffle(vacc2x0123, vacc2x0123, 2, 3, 2, 3);
269         vacc1x0123 = wasm_v32x4_shuffle(vacc1x0123, vacc1x0123, 2, 3, 2, 3);
270         vacc0x0123 = wasm_v32x4_shuffle(vacc0x0123, vacc0x0123, 2, 3, 2, 3);
271 
272         c4 += 2;
273         c3 += 2;
274         c2 += 2;
275         c1 += 2;
276         c0 += 2;
277       }
278       if (nc & 1) {
279         *c4 = wasm_f32x4_extract_lane(vacc4x0123, 0);
280         *c3 = wasm_f32x4_extract_lane(vacc3x0123, 0);
281         *c2 = wasm_f32x4_extract_lane(vacc2x0123, 0);
282         *c1 = wasm_f32x4_extract_lane(vacc1x0123, 0);
283         *c0 = wasm_f32x4_extract_lane(vacc0x0123, 0);
284       }
285 
286       nc = 0;
287     }
288   } while (nc != 0);
289 }
290