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_8x1__wasmsimd_arm_x4(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_8x1__wasmsimd_arm_x4(
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 - 8 * sizeof(float);
36   while XNN_LIKELY(mc >= 8 * 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 vacc0123x2 = vzero;
47       v128_t vacc0123x3 = vzero;
48       v128_t vacc4567x0 = vacc0123x0;
49       v128_t vacc4567x1 = vzero;
50       v128_t vacc4567x2 = vzero;
51       v128_t vacc4567x3 = vzero;
52       for (; nnz >= 4; nnz -= 4) {
53         const intptr_t diff0 = dmap[0];
54         const intptr_t diff1 = dmap[1];
55         const intptr_t diff2 = dmap[2];
56         const intptr_t diff3 = dmap[3];
57         dmap += 4;
58         const v128_t vi0123x0 = wasm_v128_load(input);
59         const v128_t vi4567x0 = wasm_v128_load(input + 4);
60         input = (const float*restrict) ((uintptr_t) input + (uintptr_t) diff0);
61         const v128_t vw0 = wasm_v32x4_load_splat(w);
62         w += 1;
63         vacc0123x0 = wasm_f32x4_add(vacc0123x0, wasm_f32x4_mul(vi0123x0, vw0));
64         vacc4567x0 = wasm_f32x4_add(vacc4567x0, wasm_f32x4_mul(vi4567x0, vw0));
65         const v128_t vi0123x1 = wasm_v128_load(input);
66         const v128_t vi4567x1 = wasm_v128_load(input + 4);
67         input = (const float*restrict) ((uintptr_t) input + (uintptr_t) diff1);
68         const v128_t vw1 = wasm_v32x4_load_splat(w);
69         w += 1;
70         vacc0123x1 = wasm_f32x4_add(vacc0123x1, wasm_f32x4_mul(vi0123x1, vw1));
71         vacc4567x1 = wasm_f32x4_add(vacc4567x1, wasm_f32x4_mul(vi4567x1, vw1));
72         const v128_t vi0123x2 = wasm_v128_load(input);
73         const v128_t vi4567x2 = wasm_v128_load(input + 4);
74         input = (const float*restrict) ((uintptr_t) input + (uintptr_t) diff2);
75         const v128_t vw2 = wasm_v32x4_load_splat(w);
76         w += 1;
77         vacc0123x2 = wasm_f32x4_add(vacc0123x2, wasm_f32x4_mul(vi0123x2, vw2));
78         vacc4567x2 = wasm_f32x4_add(vacc4567x2, wasm_f32x4_mul(vi4567x2, vw2));
79         const v128_t vi0123x3 = wasm_v128_load(input);
80         const v128_t vi4567x3 = wasm_v128_load(input + 4);
81         input = (const float*restrict) ((uintptr_t) input + (uintptr_t) diff3);
82         const v128_t vw3 = wasm_v32x4_load_splat(w);
83         w += 1;
84         vacc0123x3 = wasm_f32x4_add(vacc0123x3, wasm_f32x4_mul(vi0123x3, vw3));
85         vacc4567x3 = wasm_f32x4_add(vacc4567x3, wasm_f32x4_mul(vi4567x3, vw3));
86       }
87       v128_t vacc0123 = vacc0123x0;
88       v128_t vacc4567 = vacc4567x0;
89       vacc0123 = wasm_f32x4_add(vacc0123, vacc0123x1);
90       vacc4567 = wasm_f32x4_add(vacc4567, vacc4567x1);
91       vacc0123 = wasm_f32x4_add(vacc0123, vacc0123x2);
92       vacc4567 = wasm_f32x4_add(vacc4567, vacc4567x2);
93       vacc0123 = wasm_f32x4_add(vacc0123, vacc0123x3);
94       vacc4567 = wasm_f32x4_add(vacc4567, vacc4567x3);
95       if XNN_LIKELY(nnz != 0) {
96         do {
97           const intptr_t diff = *dmap++;
98           const v128_t vi0123 = wasm_v128_load(input);
99           const v128_t vi4567 = wasm_v128_load(input + 4);
100           input = (const float*restrict) ((uintptr_t) input + (uintptr_t) diff);
101           const v128_t vw = wasm_v32x4_load_splat(w); w += 1;
102           vacc0123 = wasm_f32x4_add(vacc0123, wasm_f32x4_mul(vi0123, vw));
103           vacc4567 = wasm_f32x4_add(vacc4567, wasm_f32x4_mul(vi4567, vw));
104         } while (--nnz != 0);
105       }
106       v128_t vout0123 = wasm_f32x4_min(vacc0123, vmax);
107       v128_t vout4567 = wasm_f32x4_min(vacc4567, vmax);
108       vout0123 = wasm_f32x4_max(vout0123, vmin);
109       vout4567 = wasm_f32x4_max(vout4567, vmin);
110       wasm_v128_store(output, vout0123);
111       wasm_v128_store(output + 4, vout4567);
112       output = (float*restrict) ((uintptr_t) output + output_stride);
113     } while (--n != 0);
114     output = (float*restrict) ((uintptr_t) output - output_decrement);
115     input += 8;
116     mc -= 8 * sizeof(float);
117   }
118   if XNN_UNLIKELY(mc != 0) {
119     output_decrement += 4 * sizeof(float);
120     if (mc & (4 * sizeof(float))) {
121       const float*restrict w = weights;
122       const int32_t* dmap = widx_dmap;
123       const uint32_t* nnzmap = nidx_nnzmap;
124       size_t n = nc;
125       do {
126         uint32_t nnz = *nnzmap++;
127         v128_t vacc0123 = wasm_v32x4_load_splat(w); w += 1;
128         if XNN_LIKELY(nnz != 0) {
129           do {
130             const intptr_t diff = *dmap++;
131             const v128_t vi0123 = wasm_v128_load(input);
132             input = (const float*restrict) ((uintptr_t) input + (uintptr_t) diff);
133             const v128_t vw = wasm_v32x4_load_splat(w); w += 1;
134             vacc0123 = wasm_f32x4_add(vacc0123, wasm_f32x4_mul(vi0123, vw));
135           } while (--nnz != 0);
136         }
137         v128_t vout0123 = wasm_f32x4_min(vacc0123, vmax);
138         vout0123 = wasm_f32x4_max(vout0123, vmin);
139         wasm_v128_store(output, vout0123);
140 
141         output = (float*restrict) ((uintptr_t) output + output_stride);
142       } while (--n != 0);
143       output = (float*restrict) ((uintptr_t) output - output_decrement);
144       input += 4;
145     }
146     output_decrement += 2 * sizeof(float);
147     if (mc & (2 * sizeof(float))) {
148       const float*restrict w = weights;
149       const int32_t* dmap = widx_dmap;
150       const uint32_t* nnzmap = nidx_nnzmap;
151       size_t n = nc;
152       do {
153         uint32_t nnz = *nnzmap++;
154         v128_t vacc01 = wasm_v32x4_load_splat(w); w += 1;
155         if XNN_LIKELY(nnz != 0) {
156           do {
157             const intptr_t diff = *dmap++;
158             const v128_t vi01 = wasm_v64x2_load_splat(input);
159             input = (const float*restrict) ((uintptr_t) input + (uintptr_t) diff);
160             const v128_t vw = wasm_v32x4_load_splat(w); w += 1;
161             vacc01 = wasm_f32x4_add(vacc01, wasm_f32x4_mul(vi01, vw));
162           } while (--nnz != 0);
163         }
164         v128_t vout01 = wasm_f32x4_min(vacc01, vmax);
165         vout01 = wasm_f32x4_max(vout01, vmin);
166         *((double*) output) = wasm_f64x2_extract_lane(vout01, 0);
167 
168         output = (float*restrict) ((uintptr_t) output + output_stride);
169       } while (--n != 0);
170       output = (float*restrict) ((uintptr_t) output - output_decrement);
171       input += 2;
172     }
173     output_decrement += 1 * sizeof(float);
174     if (mc & (1 * 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 vacc0 = wasm_v32x4_load_splat(w); w += 1;
182         if XNN_LIKELY(nnz != 0) {
183           do {
184             const intptr_t diff = *dmap++;
185             const v128_t vi0 = wasm_v32x4_load_splat(input);
186             input = (const float*restrict) ((uintptr_t) input + (uintptr_t) diff);
187             const v128_t vw = wasm_v32x4_load_splat(w); w += 1;
188             vacc0 = wasm_f32x4_add(vacc0, wasm_f32x4_mul(vi0, vw));
189           } while (--nnz != 0);
190         }
191         v128_t vout0 = wasm_f32x4_min(vacc0, vmax);
192         vout0 = wasm_f32x4_max(vout0, vmin);
193         *output = wasm_f32x4_extract_lane(vout0, 0);
194 
195         output = (float*restrict) ((uintptr_t) output + output_stride);
196       } while (--n != 0);
197       output = (float*restrict) ((uintptr_t) output - output_decrement);
198       input += 1;
199     }
200   }
201 }
202