1 // Auto-generated file. Do not edit!
2 // Template: src/f32-dwconv2d-chw/3x3p1-wasmsimd-loadsplat.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
15 #include <xnnpack/dwconv.h>
16 #include <xnnpack/math.h>
17
18
19
xnn_f32_dwconv2d_chw_ukernel_3x3p1__wasmsimd_arm_loadsplat_3x4(size_t input_height,size_t input_width,const float * input,const float * weights,const float * zero,float * output,uint32_t padding_top,const union xnn_f32_chw_params params[restrict XNN_MIN_ELEMENTS (1)])20 void xnn_f32_dwconv2d_chw_ukernel_3x3p1__wasmsimd_arm_loadsplat_3x4(
21 size_t input_height,
22 size_t input_width,
23 const float* input,
24 const float* weights,
25 const float* zero,
26 float* output,
27 uint32_t padding_top,
28 const union xnn_f32_chw_params params[restrict XNN_MIN_ELEMENTS(1)])
29 {
30 assert(input_height != 0);
31 assert(input_width != 0);
32 assert(input_width % sizeof(float) == 0);
33 assert(padding_top == 1);
34
35 const v128_t vmask = wasm_v128_load(params->scalar.mask);
36 const v128_t vmax = wasm_v32x4_load_splat(¶ms->scalar.max);
37 const v128_t vmin = wasm_v32x4_load_splat(¶ms->scalar.min);
38
39 const v128_t vw0123 = wasm_v128_load(weights);
40 const v128_t vw4567 = wasm_v128_load(weights + 4);
41 const v128_t vw89 = wasm_v64x2_load_splat(weights + 8);
42 const v128_t vbias = wasm_v32x4_shuffle(vw0123, vw0123, 0, 0, 0, 0);
43 const v128_t vk00 = wasm_v32x4_shuffle(vw0123, vw0123, 1, 1, 1, 1);
44 const v128_t vk01 = wasm_v32x4_shuffle(vw0123, vw0123, 2, 2, 2, 2);
45 const v128_t vk02 = wasm_v32x4_shuffle(vw0123, vw0123, 3, 3, 3, 3);
46 const v128_t vk10 = wasm_v32x4_shuffle(vw4567, vw4567, 0, 0, 0, 0);
47 const v128_t vk11 = wasm_v32x4_shuffle(vw4567, vw4567, 1, 1, 1, 1);
48 const v128_t vk12 = wasm_v32x4_shuffle(vw4567, vw4567, 2, 2, 2, 2);
49 const v128_t vk20 = wasm_v32x4_shuffle(vw4567, vw4567, 3, 3, 3, 3);
50 const v128_t vk21 = wasm_v32x4_shuffle(vw89, vw89, 0, 0, 0, 0);
51 const v128_t vk22 = wasm_v32x4_shuffle(vw89, vw89, 1, 1, 1, 1);
52
53 const v128_t vzero = wasm_f32x4_splat(0.0f);
54
55 const size_t input_decrement = round_up_po2(input_width, 4 * sizeof(float));
56
57 const float* i0 = zero;
58 const float* i1 = input;
59 const float* i2 = (const float*) ((uintptr_t) i1 + input_width);
60 const float* i3 = (const float*) ((uintptr_t) i2 + input_width);
61 const float* i4 = (const float*) ((uintptr_t) i3 + input_width);
62
63 float* o0 = output;
64 float* o1 = (float*) ((uintptr_t) o0 + input_width);
65 float* o2 = (float*) ((uintptr_t) o1 + input_width);
66
67 size_t output_height = input_height;
68 do {
69 if XNN_UNPREDICTABLE(output_height < 2) {
70 i2 = zero;
71 o1 = o0;
72 }
73 if XNN_UNPREDICTABLE(output_height < 3) {
74 i3 = zero;
75 o2 = o1;
76 }
77 if XNN_UNPREDICTABLE(output_height < 4) {
78 i4 = zero;
79 }
80
81 v128_t vi0x0123 = vzero;
82 v128_t vi1x0123 = vzero;
83 v128_t vi2x0123 = vzero;
84 v128_t vi3x0123 = vzero;
85 v128_t vi4x0123 = vzero;
86
87 v128_t vi0x4567 = wasm_v128_load(i0);
88 i0 += 4;
89 v128_t vi1x4567 = wasm_v128_load(i1);
90 i1 += 4;
91 v128_t vi2x4567 = wasm_v128_load(i2);
92 i2 += 4;
93 v128_t vi3x4567 = wasm_v128_load(i3);
94 i3 += 4;
95 v128_t vi4x4567 = wasm_v128_load(i4);
96 i4 += 4;
97
98 size_t w = input_width;
99 for (; w > 4 * sizeof(float); w -= 4 * sizeof(float)) {
100 const v128_t vi0x89AB = wasm_v128_load(i0);
101 i0 += 4;
102 const v128_t vi1x89AB = wasm_v128_load(i1);
103 i1 += 4;
104 const v128_t vi2x89AB = wasm_v128_load(i2);
105 i2 += 4;
106 const v128_t vi3x89AB = wasm_v128_load(i3);
107 i3 += 4;
108 const v128_t vi4x89AB = wasm_v128_load(i4);
109 i4 += 4;
110
111 v128_t vo0p0 = wasm_f32x4_add(vbias, wasm_f32x4_mul(vi0x4567, vk01));
112 v128_t vo1p0 = wasm_f32x4_add(vbias, wasm_f32x4_mul(vi1x4567, vk01));
113 v128_t vo2p0 = wasm_f32x4_add(vbias, wasm_f32x4_mul(vi2x4567, vk01));
114 vo0p0 = wasm_f32x4_add(vo0p0, wasm_f32x4_mul(vi1x4567, vk11));
115 vo1p0 = wasm_f32x4_add(vo1p0, wasm_f32x4_mul(vi2x4567, vk11));
116 vo2p0 = wasm_f32x4_add(vo2p0, wasm_f32x4_mul(vi3x4567, vk11));
117 vo0p0 = wasm_f32x4_add(vo0p0, wasm_f32x4_mul(vi2x4567, vk21));
118 vo1p0 = wasm_f32x4_add(vo1p0, wasm_f32x4_mul(vi3x4567, vk21));
119 vo2p0 = wasm_f32x4_add(vo2p0, wasm_f32x4_mul(vi4x4567, vk21));
120
121 const v128_t vi0x3456 = wasm_v32x4_shuffle(vi0x0123, vi0x4567, 3, 4, 5, 6);
122 const v128_t vi1x3456 = wasm_v32x4_shuffle(vi1x0123, vi1x4567, 3, 4, 5, 6);
123 const v128_t vi2x3456 = wasm_v32x4_shuffle(vi2x0123, vi2x4567, 3, 4, 5, 6);
124 const v128_t vi3x3456 = wasm_v32x4_shuffle(vi3x0123, vi3x4567, 3, 4, 5, 6);
125 const v128_t vi4x3456 = wasm_v32x4_shuffle(vi4x0123, vi4x4567, 3, 4, 5, 6);
126
127 vo0p0 = wasm_f32x4_add(vo0p0, wasm_f32x4_mul(vi0x3456, vk00));
128 vo1p0 = wasm_f32x4_add(vo1p0, wasm_f32x4_mul(vi1x3456, vk00));
129 vo2p0 = wasm_f32x4_add(vo2p0, wasm_f32x4_mul(vi2x3456, vk00));
130 vo0p0 = wasm_f32x4_add(vo0p0, wasm_f32x4_mul(vi1x3456, vk10));
131 vo1p0 = wasm_f32x4_add(vo1p0, wasm_f32x4_mul(vi2x3456, vk10));
132 vo2p0 = wasm_f32x4_add(vo2p0, wasm_f32x4_mul(vi3x3456, vk10));
133 vo0p0 = wasm_f32x4_add(vo0p0, wasm_f32x4_mul(vi2x3456, vk20));
134 vo1p0 = wasm_f32x4_add(vo1p0, wasm_f32x4_mul(vi3x3456, vk20));
135 vo2p0 = wasm_f32x4_add(vo2p0, wasm_f32x4_mul(vi4x3456, vk20));
136
137 vi0x0123 = vi0x4567;
138 vi1x0123 = vi1x4567;
139 vi2x0123 = vi2x4567;
140 vi3x0123 = vi3x4567;
141 vi4x0123 = vi4x4567;
142
143 const v128_t vi0x5678 = wasm_v32x4_shuffle(vi0x4567, vi0x89AB, 1, 2, 3, 4);
144 const v128_t vi1x5678 = wasm_v32x4_shuffle(vi1x4567, vi1x89AB, 1, 2, 3, 4);
145 const v128_t vi2x5678 = wasm_v32x4_shuffle(vi2x4567, vi2x89AB, 1, 2, 3, 4);
146 const v128_t vi3x5678 = wasm_v32x4_shuffle(vi3x4567, vi3x89AB, 1, 2, 3, 4);
147 const v128_t vi4x5678 = wasm_v32x4_shuffle(vi4x4567, vi4x89AB, 1, 2, 3, 4);
148
149 vo0p0 = wasm_f32x4_add(vo0p0, wasm_f32x4_mul(vi0x5678, vk02));
150 vo1p0 = wasm_f32x4_add(vo1p0, wasm_f32x4_mul(vi1x5678, vk02));
151 vo2p0 = wasm_f32x4_add(vo2p0, wasm_f32x4_mul(vi2x5678, vk02));
152 vo0p0 = wasm_f32x4_add(vo0p0, wasm_f32x4_mul(vi1x5678, vk12));
153 vo1p0 = wasm_f32x4_add(vo1p0, wasm_f32x4_mul(vi2x5678, vk12));
154 vo2p0 = wasm_f32x4_add(vo2p0, wasm_f32x4_mul(vi3x5678, vk12));
155 vo0p0 = wasm_f32x4_add(vo0p0, wasm_f32x4_mul(vi2x5678, vk22));
156 vo1p0 = wasm_f32x4_add(vo1p0, wasm_f32x4_mul(vi3x5678, vk22));
157 vo2p0 = wasm_f32x4_add(vo2p0, wasm_f32x4_mul(vi4x5678, vk22));
158
159 vi0x4567 = vi0x89AB;
160 vi1x4567 = vi1x89AB;
161 vi2x4567 = vi2x89AB;
162 vi3x4567 = vi3x89AB;
163 vi4x4567 = vi4x89AB;
164
165
166 v128_t vo0 = wasm_f32x4_max(vo0p0, vmin);
167 v128_t vo1 = wasm_f32x4_max(vo1p0, vmin);
168 v128_t vo2 = wasm_f32x4_max(vo2p0, vmin);
169 vo0 = wasm_f32x4_min(vo0, vmax);
170 vo1 = wasm_f32x4_min(vo1, vmax);
171 vo2 = wasm_f32x4_min(vo2, vmax);
172
173 wasm_v128_store(o2, vo2);
174 o2 += 4;
175 wasm_v128_store(o1, vo1);
176 o1 += 4;
177 wasm_v128_store(o0, vo0);
178 o0 += 4;
179 }
180 // Always process the last block of 1..4 pixels.
181 assert(w >= 1 * sizeof(float));
182 assert(w <= 4 * sizeof(float));
183 {
184 vi0x4567 = wasm_v128_and(vmask, vi0x4567);
185 vi1x4567 = wasm_v128_and(vmask, vi1x4567);
186 vi2x4567 = wasm_v128_and(vmask, vi2x4567);
187 vi3x4567 = wasm_v128_and(vmask, vi3x4567);
188 vi4x4567 = wasm_v128_and(vmask, vi4x4567);
189
190 v128_t vo0p0 = wasm_f32x4_add(vbias, wasm_f32x4_mul(vi0x4567, vk01));
191 v128_t vo1p0 = wasm_f32x4_add(vbias, wasm_f32x4_mul(vi1x4567, vk01));
192 v128_t vo2p0 = wasm_f32x4_add(vbias, wasm_f32x4_mul(vi2x4567, vk01));
193 vo0p0 = wasm_f32x4_add(vo0p0, wasm_f32x4_mul(vi1x4567, vk11));
194 vo1p0 = wasm_f32x4_add(vo1p0, wasm_f32x4_mul(vi2x4567, vk11));
195 vo2p0 = wasm_f32x4_add(vo2p0, wasm_f32x4_mul(vi3x4567, vk11));
196 vo0p0 = wasm_f32x4_add(vo0p0, wasm_f32x4_mul(vi2x4567, vk21));
197 vo1p0 = wasm_f32x4_add(vo1p0, wasm_f32x4_mul(vi3x4567, vk21));
198 vo2p0 = wasm_f32x4_add(vo2p0, wasm_f32x4_mul(vi4x4567, vk21));
199
200 const v128_t vi0x3456 = wasm_v32x4_shuffle(vi0x0123, vi0x4567, 3, 4, 5, 6);
201 const v128_t vi1x3456 = wasm_v32x4_shuffle(vi1x0123, vi1x4567, 3, 4, 5, 6);
202 const v128_t vi2x3456 = wasm_v32x4_shuffle(vi2x0123, vi2x4567, 3, 4, 5, 6);
203 const v128_t vi3x3456 = wasm_v32x4_shuffle(vi3x0123, vi3x4567, 3, 4, 5, 6);
204 const v128_t vi4x3456 = wasm_v32x4_shuffle(vi4x0123, vi4x4567, 3, 4, 5, 6);
205
206 vo0p0 = wasm_f32x4_add(vo0p0, wasm_f32x4_mul(vi0x3456, vk00));
207 vo1p0 = wasm_f32x4_add(vo1p0, wasm_f32x4_mul(vi1x3456, vk00));
208 vo2p0 = wasm_f32x4_add(vo2p0, wasm_f32x4_mul(vi2x3456, vk00));
209 vo0p0 = wasm_f32x4_add(vo0p0, wasm_f32x4_mul(vi1x3456, vk10));
210 vo1p0 = wasm_f32x4_add(vo1p0, wasm_f32x4_mul(vi2x3456, vk10));
211 vo2p0 = wasm_f32x4_add(vo2p0, wasm_f32x4_mul(vi3x3456, vk10));
212 vo0p0 = wasm_f32x4_add(vo0p0, wasm_f32x4_mul(vi2x3456, vk20));
213 vo1p0 = wasm_f32x4_add(vo1p0, wasm_f32x4_mul(vi3x3456, vk20));
214 vo2p0 = wasm_f32x4_add(vo2p0, wasm_f32x4_mul(vi4x3456, vk20));
215
216 const v128_t vi0x5678 = wasm_v32x4_shuffle(vi0x4567, vzero, 1, 2, 3, 4);
217 const v128_t vi1x5678 = wasm_v32x4_shuffle(vi1x4567, vzero, 1, 2, 3, 4);
218 const v128_t vi2x5678 = wasm_v32x4_shuffle(vi2x4567, vzero, 1, 2, 3, 4);
219 const v128_t vi3x5678 = wasm_v32x4_shuffle(vi3x4567, vzero, 1, 2, 3, 4);
220 const v128_t vi4x5678 = wasm_v32x4_shuffle(vi4x4567, vzero, 1, 2, 3, 4);
221
222 vo0p0 = wasm_f32x4_add(vo0p0, wasm_f32x4_mul(vi0x5678, vk02));
223 vo1p0 = wasm_f32x4_add(vo1p0, wasm_f32x4_mul(vi1x5678, vk02));
224 vo2p0 = wasm_f32x4_add(vo2p0, wasm_f32x4_mul(vi2x5678, vk02));
225 vo0p0 = wasm_f32x4_add(vo0p0, wasm_f32x4_mul(vi1x5678, vk12));
226 vo1p0 = wasm_f32x4_add(vo1p0, wasm_f32x4_mul(vi2x5678, vk12));
227 vo2p0 = wasm_f32x4_add(vo2p0, wasm_f32x4_mul(vi3x5678, vk12));
228 vo0p0 = wasm_f32x4_add(vo0p0, wasm_f32x4_mul(vi2x5678, vk22));
229 vo1p0 = wasm_f32x4_add(vo1p0, wasm_f32x4_mul(vi3x5678, vk22));
230 vo2p0 = wasm_f32x4_add(vo2p0, wasm_f32x4_mul(vi4x5678, vk22));
231
232
233 v128_t vo0 = wasm_f32x4_max(vo0p0, vmin);
234 v128_t vo1 = wasm_f32x4_max(vo1p0, vmin);
235 v128_t vo2 = wasm_f32x4_max(vo2p0, vmin);
236 vo0 = wasm_f32x4_min(vo0, vmax);
237 vo1 = wasm_f32x4_min(vo1, vmax);
238 vo2 = wasm_f32x4_min(vo2, vmax);
239
240 if XNN_LIKELY(w == 4 * sizeof(float)) {
241 wasm_v128_store(o2, vo2);
242 o2 += 4;
243 wasm_v128_store(o1, vo1);
244 o1 += 4;
245 wasm_v128_store(o0, vo0);
246 o0 += 4;
247 } else {
248 if (w & (2 * sizeof(float))) {
249 *((double*) o2) = wasm_f64x2_extract_lane(vo2, 0);
250 o2 += 2;
251 *((double*) o1) = wasm_f64x2_extract_lane(vo1, 0);
252 o1 += 2;
253 *((double*) o0) = wasm_f64x2_extract_lane(vo0, 0);
254 o0 += 2;
255
256 vo0 = wasm_v32x4_shuffle(vo0, vo0, 2, 3, 0, 1);
257 vo1 = wasm_v32x4_shuffle(vo1, vo1, 2, 3, 0, 1);
258 vo2 = wasm_v32x4_shuffle(vo2, vo2, 2, 3, 0, 1);
259 }
260 if (w & (1 * sizeof(float))) {
261 *o2 = wasm_f32x4_extract_lane(vo2, 0);
262 o2 += 1;
263 *o1 = wasm_f32x4_extract_lane(vo1, 0);
264 o1 += 1;
265 *o0 = wasm_f32x4_extract_lane(vo0, 0);
266 o0 += 1;
267 }
268 }
269 }
270
271 i0 = (const float*) ((uintptr_t) i3 - input_decrement);
272 i1 = (const float*) ((uintptr_t) i4 - input_decrement);
273 i2 = (const float*) ((uintptr_t) i1 + input_width);
274 i3 = (const float*) ((uintptr_t) i2 + input_width);
275 i4 = (const float*) ((uintptr_t) i3 + input_width);
276
277 o0 = o2;
278 o1 = (float*) ((uintptr_t) o0 + input_width);
279 o2 = (float*) ((uintptr_t) o1 + input_width);
280
281 output_height = doz(output_height, 3);
282 } while (output_height != 0);
283 }
284