1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-spmm/wasmsimd.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/spmm.h>
15 
16 
xnn_f32_spmm_minmax_ukernel_32x1__wasmsimd_arm_x2(size_t mc,size_t nc,const float * restrict input,const float * restrict weights,const int32_t * restrict widx_dmap,const uint32_t * restrict nidx_nnzmap,float * restrict output,size_t output_stride,const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])17 void xnn_f32_spmm_minmax_ukernel_32x1__wasmsimd_arm_x2(
18     size_t mc,
19     size_t nc,
20     const float*restrict input,
21     const float*restrict weights,
22     const int32_t*restrict widx_dmap,
23     const uint32_t*restrict nidx_nnzmap,
24     float*restrict output,
25     size_t output_stride,
26     const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS(1)])
27 {
28   assert(mc != 0);
29   assert(mc % sizeof(float) == 0);
30   assert(nc != 0);
31 
32   const v128_t vmin = wasm_v32x4_load_splat(&params->scalar.min);
33   const v128_t vmax = wasm_v32x4_load_splat(&params->scalar.max);
34   const v128_t vzero = wasm_f64x2_splat(0.0);
35   size_t output_decrement = output_stride * nc - 32 * sizeof(float);
36   while XNN_LIKELY(mc >= 32 * sizeof(float)) {
37     const float*restrict w = weights;
38     const int32_t* dmap = widx_dmap;
39     const uint32_t* nnzmap = nidx_nnzmap;
40     size_t n = nc;
41     do {
42       uint32_t nnz = *nnzmap++;
43       v128_t vacc0123x0 = wasm_v32x4_load_splat(w);
44       w += 1;
45       v128_t vacc0123x1 = vzero;
46       v128_t vacc4567x0 = vacc0123x0;
47       v128_t vacc4567x1 = vzero;
48       v128_t vacc89ABx0 = vacc0123x0;
49       v128_t vacc89ABx1 = vzero;
50       v128_t vaccCDEFx0 = vacc0123x0;
51       v128_t vaccCDEFx1 = vzero;
52       v128_t vaccGHIJx0 = vacc0123x0;
53       v128_t vaccGHIJx1 = vzero;
54       v128_t vaccKLMNx0 = vacc0123x0;
55       v128_t vaccKLMNx1 = vzero;
56       v128_t vaccOPQRx0 = vacc0123x0;
57       v128_t vaccOPQRx1 = vzero;
58       v128_t vaccSTUVx0 = vacc0123x0;
59       v128_t vaccSTUVx1 = vzero;
60       for (; nnz >= 2; nnz -= 2) {
61         const intptr_t diff0 = dmap[0];
62         const intptr_t diff1 = dmap[1];
63         dmap += 2;
64         const v128_t vi0123x0 = wasm_v128_load(input);
65         const v128_t vi4567x0 = wasm_v128_load(input + 4);
66         const v128_t vi89ABx0 = wasm_v128_load(input + 8);
67         const v128_t viCDEFx0 = wasm_v128_load(input + 12);
68         const v128_t viGHIJx0 = wasm_v128_load(input + 16);
69         const v128_t viKLMNx0 = wasm_v128_load(input + 20);
70         const v128_t viOPQRx0 = wasm_v128_load(input + 24);
71         const v128_t viSTUVx0 = wasm_v128_load(input + 28);
72         input = (const float*restrict) ((uintptr_t) input + (uintptr_t) diff0);
73         const v128_t vw0 = wasm_v32x4_load_splat(w);
74         w += 1;
75         vacc0123x0 = wasm_f32x4_add(vacc0123x0, wasm_f32x4_mul(vi0123x0, vw0));
76         vacc4567x0 = wasm_f32x4_add(vacc4567x0, wasm_f32x4_mul(vi4567x0, vw0));
77         vacc89ABx0 = wasm_f32x4_add(vacc89ABx0, wasm_f32x4_mul(vi89ABx0, vw0));
78         vaccCDEFx0 = wasm_f32x4_add(vaccCDEFx0, wasm_f32x4_mul(viCDEFx0, vw0));
79         vaccGHIJx0 = wasm_f32x4_add(vaccGHIJx0, wasm_f32x4_mul(viGHIJx0, vw0));
80         vaccKLMNx0 = wasm_f32x4_add(vaccKLMNx0, wasm_f32x4_mul(viKLMNx0, vw0));
81         vaccOPQRx0 = wasm_f32x4_add(vaccOPQRx0, wasm_f32x4_mul(viOPQRx0, vw0));
82         vaccSTUVx0 = wasm_f32x4_add(vaccSTUVx0, wasm_f32x4_mul(viSTUVx0, vw0));
83         const v128_t vi0123x1 = wasm_v128_load(input);
84         const v128_t vi4567x1 = wasm_v128_load(input + 4);
85         const v128_t vi89ABx1 = wasm_v128_load(input + 8);
86         const v128_t viCDEFx1 = wasm_v128_load(input + 12);
87         const v128_t viGHIJx1 = wasm_v128_load(input + 16);
88         const v128_t viKLMNx1 = wasm_v128_load(input + 20);
89         const v128_t viOPQRx1 = wasm_v128_load(input + 24);
90         const v128_t viSTUVx1 = wasm_v128_load(input + 28);
91         input = (const float*restrict) ((uintptr_t) input + (uintptr_t) diff1);
92         const v128_t vw1 = wasm_v32x4_load_splat(w);
93         w += 1;
94         vacc0123x1 = wasm_f32x4_add(vacc0123x1, wasm_f32x4_mul(vi0123x1, vw1));
95         vacc4567x1 = wasm_f32x4_add(vacc4567x1, wasm_f32x4_mul(vi4567x1, vw1));
96         vacc89ABx1 = wasm_f32x4_add(vacc89ABx1, wasm_f32x4_mul(vi89ABx1, vw1));
97         vaccCDEFx1 = wasm_f32x4_add(vaccCDEFx1, wasm_f32x4_mul(viCDEFx1, vw1));
98         vaccGHIJx1 = wasm_f32x4_add(vaccGHIJx1, wasm_f32x4_mul(viGHIJx1, vw1));
99         vaccKLMNx1 = wasm_f32x4_add(vaccKLMNx1, wasm_f32x4_mul(viKLMNx1, vw1));
100         vaccOPQRx1 = wasm_f32x4_add(vaccOPQRx1, wasm_f32x4_mul(viOPQRx1, vw1));
101         vaccSTUVx1 = wasm_f32x4_add(vaccSTUVx1, wasm_f32x4_mul(viSTUVx1, vw1));
102       }
103       v128_t vacc0123 = vacc0123x0;
104       v128_t vacc4567 = vacc4567x0;
105       v128_t vacc89AB = vacc89ABx0;
106       v128_t vaccCDEF = vaccCDEFx0;
107       v128_t vaccGHIJ = vaccGHIJx0;
108       v128_t vaccKLMN = vaccKLMNx0;
109       v128_t vaccOPQR = vaccOPQRx0;
110       v128_t vaccSTUV = vaccSTUVx0;
111       vacc0123 = wasm_f32x4_add(vacc0123, vacc0123x1);
112       vacc4567 = wasm_f32x4_add(vacc4567, vacc4567x1);
113       vacc89AB = wasm_f32x4_add(vacc89AB, vacc89ABx1);
114       vaccCDEF = wasm_f32x4_add(vaccCDEF, vaccCDEFx1);
115       vaccGHIJ = wasm_f32x4_add(vaccGHIJ, vaccGHIJx1);
116       vaccKLMN = wasm_f32x4_add(vaccKLMN, vaccKLMNx1);
117       vaccOPQR = wasm_f32x4_add(vaccOPQR, vaccOPQRx1);
118       vaccSTUV = wasm_f32x4_add(vaccSTUV, vaccSTUVx1);
119       if XNN_LIKELY(nnz != 0) {
120         do {
121           const intptr_t diff = *dmap++;
122           const v128_t vi0123 = wasm_v128_load(input);
123           const v128_t vi4567 = wasm_v128_load(input + 4);
124           const v128_t vi89AB = wasm_v128_load(input + 8);
125           const v128_t viCDEF = wasm_v128_load(input + 12);
126           const v128_t viGHIJ = wasm_v128_load(input + 16);
127           const v128_t viKLMN = wasm_v128_load(input + 20);
128           const v128_t viOPQR = wasm_v128_load(input + 24);
129           const v128_t viSTUV = wasm_v128_load(input + 28);
130           input = (const float*restrict) ((uintptr_t) input + (uintptr_t) diff);
131           const v128_t vw = wasm_v32x4_load_splat(w); w += 1;
132           vacc0123 = wasm_f32x4_add(vacc0123, wasm_f32x4_mul(vi0123, vw));
133           vacc4567 = wasm_f32x4_add(vacc4567, wasm_f32x4_mul(vi4567, vw));
134           vacc89AB = wasm_f32x4_add(vacc89AB, wasm_f32x4_mul(vi89AB, vw));
135           vaccCDEF = wasm_f32x4_add(vaccCDEF, wasm_f32x4_mul(viCDEF, vw));
136           vaccGHIJ = wasm_f32x4_add(vaccGHIJ, wasm_f32x4_mul(viGHIJ, vw));
137           vaccKLMN = wasm_f32x4_add(vaccKLMN, wasm_f32x4_mul(viKLMN, vw));
138           vaccOPQR = wasm_f32x4_add(vaccOPQR, wasm_f32x4_mul(viOPQR, vw));
139           vaccSTUV = wasm_f32x4_add(vaccSTUV, wasm_f32x4_mul(viSTUV, vw));
140         } while (--nnz != 0);
141       }
142       v128_t vout0123 = wasm_f32x4_min(vacc0123, vmax);
143       v128_t vout4567 = wasm_f32x4_min(vacc4567, vmax);
144       v128_t vout89AB = wasm_f32x4_min(vacc89AB, vmax);
145       v128_t voutCDEF = wasm_f32x4_min(vaccCDEF, vmax);
146       v128_t voutGHIJ = wasm_f32x4_min(vaccGHIJ, vmax);
147       v128_t voutKLMN = wasm_f32x4_min(vaccKLMN, vmax);
148       v128_t voutOPQR = wasm_f32x4_min(vaccOPQR, vmax);
149       v128_t voutSTUV = wasm_f32x4_min(vaccSTUV, vmax);
150       vout0123 = wasm_f32x4_max(vout0123, vmin);
151       vout4567 = wasm_f32x4_max(vout4567, vmin);
152       vout89AB = wasm_f32x4_max(vout89AB, vmin);
153       voutCDEF = wasm_f32x4_max(voutCDEF, vmin);
154       voutGHIJ = wasm_f32x4_max(voutGHIJ, vmin);
155       voutKLMN = wasm_f32x4_max(voutKLMN, vmin);
156       voutOPQR = wasm_f32x4_max(voutOPQR, vmin);
157       voutSTUV = wasm_f32x4_max(voutSTUV, vmin);
158       wasm_v128_store(output, vout0123);
159       wasm_v128_store(output + 4, vout4567);
160       wasm_v128_store(output + 8, vout89AB);
161       wasm_v128_store(output + 12, voutCDEF);
162       wasm_v128_store(output + 16, voutGHIJ);
163       wasm_v128_store(output + 20, voutKLMN);
164       wasm_v128_store(output + 24, voutOPQR);
165       wasm_v128_store(output + 28, voutSTUV);
166       output = (float*restrict) ((uintptr_t) output + output_stride);
167     } while (--n != 0);
168     output = (float*restrict) ((uintptr_t) output - output_decrement);
169     input += 32;
170     mc -= 32 * sizeof(float);
171   }
172   if XNN_UNLIKELY(mc != 0) {
173     output_decrement += 16 * sizeof(float);
174     if (mc & (16 * sizeof(float))) {
175       const float*restrict w = weights;
176       const int32_t* dmap = widx_dmap;
177       const uint32_t* nnzmap = nidx_nnzmap;
178       size_t n = nc;
179       do {
180         uint32_t nnz = *nnzmap++;
181         v128_t vacc0123 = wasm_v32x4_load_splat(w); w += 1;
182         v128_t vacc4567 = vacc0123;
183         v128_t vacc89AB = vacc0123;
184         v128_t vaccCDEF = vacc0123;
185         if XNN_LIKELY(nnz != 0) {
186           do {
187             const intptr_t diff = *dmap++;
188             const v128_t vi0123 = wasm_v128_load(input);
189             const v128_t vi4567 = wasm_v128_load(input + 4);
190             const v128_t vi89AB = wasm_v128_load(input + 8);
191             const v128_t viCDEF = wasm_v128_load(input + 12);
192             input = (const float*restrict) ((uintptr_t) input + (uintptr_t) diff);
193             const v128_t vw = wasm_v32x4_load_splat(w); w += 1;
194             vacc0123 = wasm_f32x4_add(vacc0123, wasm_f32x4_mul(vi0123, vw));
195             vacc4567 = wasm_f32x4_add(vacc4567, wasm_f32x4_mul(vi4567, vw));
196             vacc89AB = wasm_f32x4_add(vacc89AB, wasm_f32x4_mul(vi89AB, vw));
197             vaccCDEF = wasm_f32x4_add(vaccCDEF, wasm_f32x4_mul(viCDEF, vw));
198           } while (--nnz != 0);
199         }
200         v128_t vout0123 = wasm_f32x4_min(vacc0123, vmax);
201         v128_t vout4567 = wasm_f32x4_min(vacc4567, vmax);
202         v128_t vout89AB = wasm_f32x4_min(vacc89AB, vmax);
203         v128_t voutCDEF = wasm_f32x4_min(vaccCDEF, vmax);
204         vout0123 = wasm_f32x4_max(vout0123, vmin);
205         vout4567 = wasm_f32x4_max(vout4567, vmin);
206         vout89AB = wasm_f32x4_max(vout89AB, vmin);
207         voutCDEF = wasm_f32x4_max(voutCDEF, vmin);
208         wasm_v128_store(output, vout0123);
209 
210         wasm_v128_store(output + 4, vout4567);
211         wasm_v128_store(output + 8, vout89AB);
212         wasm_v128_store(output + 12, voutCDEF);
213         output = (float*restrict) ((uintptr_t) output + output_stride);
214       } while (--n != 0);
215       output = (float*restrict) ((uintptr_t) output - output_decrement);
216       input += 16;
217     }
218     output_decrement += 8 * sizeof(float);
219     if (mc & (8 * sizeof(float))) {
220       const float*restrict w = weights;
221       const int32_t* dmap = widx_dmap;
222       const uint32_t* nnzmap = nidx_nnzmap;
223       size_t n = nc;
224       do {
225         uint32_t nnz = *nnzmap++;
226         v128_t vacc0123 = wasm_v32x4_load_splat(w); w += 1;
227         v128_t vacc4567 = vacc0123;
228         if XNN_LIKELY(nnz != 0) {
229           do {
230             const intptr_t diff = *dmap++;
231             const v128_t vi0123 = wasm_v128_load(input);
232             const v128_t vi4567 = wasm_v128_load(input + 4);
233             input = (const float*restrict) ((uintptr_t) input + (uintptr_t) diff);
234             const v128_t vw = wasm_v32x4_load_splat(w); w += 1;
235             vacc0123 = wasm_f32x4_add(vacc0123, wasm_f32x4_mul(vi0123, vw));
236             vacc4567 = wasm_f32x4_add(vacc4567, wasm_f32x4_mul(vi4567, vw));
237           } while (--nnz != 0);
238         }
239         v128_t vout0123 = wasm_f32x4_min(vacc0123, vmax);
240         v128_t vout4567 = wasm_f32x4_min(vacc4567, vmax);
241         vout0123 = wasm_f32x4_max(vout0123, vmin);
242         vout4567 = wasm_f32x4_max(vout4567, vmin);
243         wasm_v128_store(output, vout0123);
244 
245         wasm_v128_store(output + 4, vout4567);
246         output = (float*restrict) ((uintptr_t) output + output_stride);
247       } while (--n != 0);
248       output = (float*restrict) ((uintptr_t) output - output_decrement);
249       input += 8;
250     }
251     output_decrement += 4 * sizeof(float);
252     if (mc & (4 * sizeof(float))) {
253       const float*restrict w = weights;
254       const int32_t* dmap = widx_dmap;
255       const uint32_t* nnzmap = nidx_nnzmap;
256       size_t n = nc;
257       do {
258         uint32_t nnz = *nnzmap++;
259         v128_t vacc0123 = wasm_v32x4_load_splat(w); w += 1;
260         if XNN_LIKELY(nnz != 0) {
261           do {
262             const intptr_t diff = *dmap++;
263             const v128_t vi0123 = wasm_v128_load(input);
264             input = (const float*restrict) ((uintptr_t) input + (uintptr_t) diff);
265             const v128_t vw = wasm_v32x4_load_splat(w); w += 1;
266             vacc0123 = wasm_f32x4_add(vacc0123, wasm_f32x4_mul(vi0123, vw));
267           } while (--nnz != 0);
268         }
269         v128_t vout0123 = wasm_f32x4_min(vacc0123, vmax);
270         vout0123 = wasm_f32x4_max(vout0123, vmin);
271         wasm_v128_store(output, vout0123);
272 
273         output = (float*restrict) ((uintptr_t) output + output_stride);
274       } while (--n != 0);
275       output = (float*restrict) ((uintptr_t) output - output_decrement);
276       input += 4;
277     }
278     output_decrement += 2 * sizeof(float);
279     if (mc & (2 * sizeof(float))) {
280       const float*restrict w = weights;
281       const int32_t* dmap = widx_dmap;
282       const uint32_t* nnzmap = nidx_nnzmap;
283       size_t n = nc;
284       do {
285         uint32_t nnz = *nnzmap++;
286         v128_t vacc01 = wasm_v32x4_load_splat(w); w += 1;
287         if XNN_LIKELY(nnz != 0) {
288           do {
289             const intptr_t diff = *dmap++;
290             const v128_t vi01 = wasm_v64x2_load_splat(input);
291             input = (const float*restrict) ((uintptr_t) input + (uintptr_t) diff);
292             const v128_t vw = wasm_v32x4_load_splat(w); w += 1;
293             vacc01 = wasm_f32x4_add(vacc01, wasm_f32x4_mul(vi01, vw));
294           } while (--nnz != 0);
295         }
296         v128_t vout01 = wasm_f32x4_min(vacc01, vmax);
297         vout01 = wasm_f32x4_max(vout01, vmin);
298         *((double*) output) = wasm_f64x2_extract_lane(vout01, 0);
299 
300         output = (float*restrict) ((uintptr_t) output + output_stride);
301       } while (--n != 0);
302       output = (float*restrict) ((uintptr_t) output - output_decrement);
303       input += 2;
304     }
305     output_decrement += 1 * sizeof(float);
306     if (mc & (1 * sizeof(float))) {
307       const float*restrict w = weights;
308       const int32_t* dmap = widx_dmap;
309       const uint32_t* nnzmap = nidx_nnzmap;
310       size_t n = nc;
311       do {
312         uint32_t nnz = *nnzmap++;
313         v128_t vacc0 = wasm_v32x4_load_splat(w); w += 1;
314         if XNN_LIKELY(nnz != 0) {
315           do {
316             const intptr_t diff = *dmap++;
317             const v128_t vi0 = wasm_v32x4_load_splat(input);
318             input = (const float*restrict) ((uintptr_t) input + (uintptr_t) diff);
319             const v128_t vw = wasm_v32x4_load_splat(w); w += 1;
320             vacc0 = wasm_f32x4_add(vacc0, wasm_f32x4_mul(vi0, vw));
321           } while (--nnz != 0);
322         }
323         v128_t vout0 = wasm_f32x4_min(vacc0, vmax);
324         vout0 = wasm_f32x4_max(vout0, vmin);
325         *output = wasm_f32x4_extract_lane(vout0, 0);
326 
327         output = (float*restrict) ((uintptr_t) output + output_stride);
328       } while (--n != 0);
329       output = (float*restrict) ((uintptr_t) output - output_decrement);
330       input += 1;
331     }
332   }
333 }
334