1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-dwconv2d-chw/3x3p1-neon.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 <arm_neon.h>
13 
14 #include <xnnpack/dwconv.h>
15 #include <xnnpack/math.h>
16 
17 
xnn_f32_dwconv2d_chw_ukernel_3x3p1__neon_4x4(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)])18 void xnn_f32_dwconv2d_chw_ukernel_3x3p1__neon_4x4(
19     size_t input_height,
20     size_t input_width,
21     const float* input,
22     const float* weights,
23     const float* zero,
24     float* output,
25     uint32_t padding_top,
26     const union xnn_f32_chw_params params[restrict XNN_MIN_ELEMENTS(1)])
27 {
28   assert(input_height != 0);
29   assert(input_width != 0);
30   assert(input_width % sizeof(float) == 0);
31   assert(padding_top == 1);
32 
33   const uint32x4_t vmask = vld1q_u32(params->neon.mask);
34   const float32x4_t vmax = vld1q_dup_f32(&params->neon.max);
35   const float32x4_t vmin = vld1q_dup_f32(&params->neon.min);
36 
37   const float32x4_t vw0123 = vld1q_f32(weights);
38   const float32x4_t vw4567 = vld1q_f32(weights + 4);
39   const float32x2_t vw89 = vld1_f32(weights + 8);
40 
41   const size_t input_decrement = round_up_po2(input_width, 4 * sizeof(float));
42 
43   const float* i0 = zero;
44   const float* i1 = input;
45   const float* i2 = (const float*) ((uintptr_t) i1 + input_width);
46   const float* i3 = (const float*) ((uintptr_t) i2 + input_width);
47   const float* i4 = (const float*) ((uintptr_t) i3 + input_width);
48   const float* i5 = (const float*) ((uintptr_t) i4 + input_width);
49 
50   float* o0 = output;
51   float* o1 = (float*) ((uintptr_t) o0 + input_width);
52   float* o2 = (float*) ((uintptr_t) o1 + input_width);
53   float* o3 = (float*) ((uintptr_t) o2 + input_width);
54 
55   size_t output_height = input_height;
56   do {
57     if XNN_UNPREDICTABLE(output_height < 2) {
58       i2 = zero;
59       o1 = o0;
60     }
61     if XNN_UNPREDICTABLE(output_height < 3) {
62       i3 = zero;
63       o2 = o1;
64     }
65     if XNN_UNPREDICTABLE(output_height < 4) {
66       i4 = zero;
67       o3 = o2;
68     }
69     if XNN_UNPREDICTABLE(output_height < 5) {
70       i5 = zero;
71     }
72 
73     float32x4_t vi0x0123 = vmovq_n_f32(0.0f);
74     float32x4_t vi1x0123 = vmovq_n_f32(0.0f);
75     float32x4_t vi2x0123 = vmovq_n_f32(0.0f);
76     float32x4_t vi3x0123 = vmovq_n_f32(0.0f);
77     float32x4_t vi4x0123 = vmovq_n_f32(0.0f);
78     float32x4_t vi5x0123 = vmovq_n_f32(0.0f);
79 
80     float32x4_t vi0x4567 = vld1q_f32(i0); i0 += 4;
81     float32x4_t vi1x4567 = vld1q_f32(i1); i1 += 4;
82     float32x4_t vi2x4567 = vld1q_f32(i2); i2 += 4;
83     float32x4_t vi3x4567 = vld1q_f32(i3); i3 += 4;
84     float32x4_t vi4x4567 = vld1q_f32(i4); i4 += 4;
85     float32x4_t vi5x4567 = vld1q_f32(i5); i5 += 4;
86 
87     size_t w = input_width;
88     for (; w > 4 * sizeof(float); w -= 4 * sizeof(float)) {
89       float32x4_t vo0p0 = vdupq_lane_f32(vget_low_f32(vw0123), 0);
90       float32x4_t vo1p0 = vdupq_lane_f32(vget_low_f32(vw0123), 0);
91       float32x4_t vo2p0 = vdupq_lane_f32(vget_low_f32(vw0123), 0);
92       float32x4_t vo3p0 = vdupq_lane_f32(vget_low_f32(vw0123), 0);
93 
94       const float32x4_t vi0x89AB = vld1q_f32(i0); i0 += 4;
95       const float32x4_t vi1x89AB = vld1q_f32(i1); i1 += 4;
96       const float32x4_t vi2x89AB = vld1q_f32(i2); i2 += 4;
97       const float32x4_t vi3x89AB = vld1q_f32(i3); i3 += 4;
98       const float32x4_t vi4x89AB = vld1q_f32(i4); i4 += 4;
99       const float32x4_t vi5x89AB = vld1q_f32(i5); i5 += 4;
100 
101       vo0p0 = vmlaq_lane_f32(vo0p0, vi0x4567, vget_high_f32(vw0123), 0);
102       vo1p0 = vmlaq_lane_f32(vo1p0, vi1x4567, vget_high_f32(vw0123), 0);
103       vo2p0 = vmlaq_lane_f32(vo2p0, vi2x4567, vget_high_f32(vw0123), 0);
104       vo3p0 = vmlaq_lane_f32(vo3p0, vi3x4567, vget_high_f32(vw0123), 0);
105 
106       vo0p0 = vmlaq_lane_f32(vo0p0, vi1x4567, vget_low_f32(vw4567), 1);
107       vo1p0 = vmlaq_lane_f32(vo1p0, vi2x4567, vget_low_f32(vw4567), 1);
108       vo2p0 = vmlaq_lane_f32(vo2p0, vi3x4567, vget_low_f32(vw4567), 1);
109       vo3p0 = vmlaq_lane_f32(vo3p0, vi4x4567, vget_low_f32(vw4567), 1);
110 
111       vo0p0 = vmlaq_lane_f32(vo0p0, vi2x4567, vw89, 0);
112       vo1p0 = vmlaq_lane_f32(vo1p0, vi3x4567, vw89, 0);
113       vo2p0 = vmlaq_lane_f32(vo2p0, vi4x4567, vw89, 0);
114       vo3p0 = vmlaq_lane_f32(vo3p0, vi5x4567, vw89, 0);
115 
116       const float32x4_t vi0x3456 = vextq_f32(vi0x0123, vi0x4567, 3);
117       const float32x4_t vi1x3456 = vextq_f32(vi1x0123, vi1x4567, 3);
118       const float32x4_t vi2x3456 = vextq_f32(vi2x0123, vi2x4567, 3);
119       const float32x4_t vi3x3456 = vextq_f32(vi3x0123, vi3x4567, 3);
120       const float32x4_t vi4x3456 = vextq_f32(vi4x0123, vi4x4567, 3);
121       const float32x4_t vi5x3456 = vextq_f32(vi5x0123, vi5x4567, 3);
122 
123       vo0p0 = vmlaq_lane_f32(vo0p0, vi0x3456, vget_low_f32(vw0123), 1);
124       vo1p0 = vmlaq_lane_f32(vo1p0, vi1x3456, vget_low_f32(vw0123), 1);
125       vo2p0 = vmlaq_lane_f32(vo2p0, vi2x3456, vget_low_f32(vw0123), 1);
126       vo3p0 = vmlaq_lane_f32(vo3p0, vi3x3456, vget_low_f32(vw0123), 1);
127 
128       vo0p0 = vmlaq_lane_f32(vo0p0, vi1x3456, vget_low_f32(vw4567), 0);
129       vo1p0 = vmlaq_lane_f32(vo1p0, vi2x3456, vget_low_f32(vw4567), 0);
130       vo2p0 = vmlaq_lane_f32(vo2p0, vi3x3456, vget_low_f32(vw4567), 0);
131       vo3p0 = vmlaq_lane_f32(vo3p0, vi4x3456, vget_low_f32(vw4567), 0);
132 
133       vo0p0 = vmlaq_lane_f32(vo0p0, vi2x3456, vget_high_f32(vw4567), 1);
134       vo1p0 = vmlaq_lane_f32(vo1p0, vi3x3456, vget_high_f32(vw4567), 1);
135       vo2p0 = vmlaq_lane_f32(vo2p0, vi4x3456, vget_high_f32(vw4567), 1);
136       vo3p0 = vmlaq_lane_f32(vo3p0, vi5x3456, vget_high_f32(vw4567), 1);
137 
138       vi0x0123 = vi0x4567;
139       vi1x0123 = vi1x4567;
140       vi2x0123 = vi2x4567;
141       vi3x0123 = vi3x4567;
142       vi4x0123 = vi4x4567;
143       vi5x0123 = vi5x4567;
144 
145       const float32x4_t vi0x5678 = vextq_f32(vi0x4567, vi0x89AB, 1);
146       const float32x4_t vi1x5678 = vextq_f32(vi1x4567, vi1x89AB, 1);
147       const float32x4_t vi2x5678 = vextq_f32(vi2x4567, vi2x89AB, 1);
148       const float32x4_t vi3x5678 = vextq_f32(vi3x4567, vi3x89AB, 1);
149       const float32x4_t vi4x5678 = vextq_f32(vi4x4567, vi4x89AB, 1);
150       const float32x4_t vi5x5678 = vextq_f32(vi5x4567, vi5x89AB, 1);
151 
152       vo0p0 = vmlaq_lane_f32(vo0p0, vi0x5678, vget_high_f32(vw0123), 1);
153       vo1p0 = vmlaq_lane_f32(vo1p0, vi1x5678, vget_high_f32(vw0123), 1);
154       vo2p0 = vmlaq_lane_f32(vo2p0, vi2x5678, vget_high_f32(vw0123), 1);
155       vo3p0 = vmlaq_lane_f32(vo3p0, vi3x5678, vget_high_f32(vw0123), 1);
156 
157       vo0p0 = vmlaq_lane_f32(vo0p0, vi1x5678, vget_high_f32(vw4567), 0);
158       vo1p0 = vmlaq_lane_f32(vo1p0, vi2x5678, vget_high_f32(vw4567), 0);
159       vo2p0 = vmlaq_lane_f32(vo2p0, vi3x5678, vget_high_f32(vw4567), 0);
160       vo3p0 = vmlaq_lane_f32(vo3p0, vi4x5678, vget_high_f32(vw4567), 0);
161 
162       vo0p0 = vmlaq_lane_f32(vo0p0, vi2x5678, vw89, 1);
163       vo1p0 = vmlaq_lane_f32(vo1p0, vi3x5678, vw89, 1);
164       vo2p0 = vmlaq_lane_f32(vo2p0, vi4x5678, vw89, 1);
165       vo3p0 = vmlaq_lane_f32(vo3p0, vi5x5678, vw89, 1);
166 
167       vi0x4567 = vi0x89AB;
168       vi1x4567 = vi1x89AB;
169       vi2x4567 = vi2x89AB;
170       vi3x4567 = vi3x89AB;
171       vi4x4567 = vi4x89AB;
172       vi5x4567 = vi5x89AB;
173 
174 
175       float32x4_t vo0 = vmaxq_f32(vo0p0, vmin);
176       float32x4_t vo1 = vmaxq_f32(vo1p0, vmin);
177       float32x4_t vo2 = vmaxq_f32(vo2p0, vmin);
178       float32x4_t vo3 = vmaxq_f32(vo3p0, vmin);
179 
180       vo0 = vminq_f32(vo0, vmax);
181       vo1 = vminq_f32(vo1, vmax);
182       vo2 = vminq_f32(vo2, vmax);
183       vo3 = vminq_f32(vo3, vmax);
184 
185       vst1q_f32(o3, vo3); o3 += 4;
186       vst1q_f32(o2, vo2); o2 += 4;
187       vst1q_f32(o1, vo1); o1 += 4;
188       vst1q_f32(o0, vo0); o0 += 4;
189     }
190     // Always process the last block of 1..4 pixels.
191     assert(w >= 1 * sizeof(float));
192     assert(w <= 4 * sizeof(float));
193     {
194       float32x4_t vo0p0 = vdupq_lane_f32(vget_low_f32(vw0123), 0);
195       float32x4_t vo1p0 = vdupq_lane_f32(vget_low_f32(vw0123), 0);
196       float32x4_t vo2p0 = vdupq_lane_f32(vget_low_f32(vw0123), 0);
197       float32x4_t vo3p0 = vdupq_lane_f32(vget_low_f32(vw0123), 0);
198 
199       vi0x4567 = vreinterpretq_f32_u32(vandq_u32(vmask, vreinterpretq_u32_f32(vi0x4567)));
200       vi1x4567 = vreinterpretq_f32_u32(vandq_u32(vmask, vreinterpretq_u32_f32(vi1x4567)));
201       vi2x4567 = vreinterpretq_f32_u32(vandq_u32(vmask, vreinterpretq_u32_f32(vi2x4567)));
202       vi3x4567 = vreinterpretq_f32_u32(vandq_u32(vmask, vreinterpretq_u32_f32(vi3x4567)));
203       vi4x4567 = vreinterpretq_f32_u32(vandq_u32(vmask, vreinterpretq_u32_f32(vi4x4567)));
204       vi5x4567 = vreinterpretq_f32_u32(vandq_u32(vmask, vreinterpretq_u32_f32(vi5x4567)));
205 
206       vo0p0 = vmlaq_lane_f32(vo0p0, vi0x4567, vget_high_f32(vw0123), 0);
207       vo1p0 = vmlaq_lane_f32(vo1p0, vi1x4567, vget_high_f32(vw0123), 0);
208       vo2p0 = vmlaq_lane_f32(vo2p0, vi2x4567, vget_high_f32(vw0123), 0);
209       vo3p0 = vmlaq_lane_f32(vo3p0, vi3x4567, vget_high_f32(vw0123), 0);
210 
211       vo0p0 = vmlaq_lane_f32(vo0p0, vi1x4567, vget_low_f32(vw4567), 1);
212       vo1p0 = vmlaq_lane_f32(vo1p0, vi2x4567, vget_low_f32(vw4567), 1);
213       vo2p0 = vmlaq_lane_f32(vo2p0, vi3x4567, vget_low_f32(vw4567), 1);
214       vo3p0 = vmlaq_lane_f32(vo3p0, vi4x4567, vget_low_f32(vw4567), 1);
215 
216       vo0p0 = vmlaq_lane_f32(vo0p0, vi2x4567, vw89, 0);
217       vo1p0 = vmlaq_lane_f32(vo1p0, vi3x4567, vw89, 0);
218       vo2p0 = vmlaq_lane_f32(vo2p0, vi4x4567, vw89, 0);
219       vo3p0 = vmlaq_lane_f32(vo3p0, vi5x4567, vw89, 0);
220 
221       const float32x4_t vi0x3456 = vextq_f32(vi0x0123, vi0x4567, 3);
222       const float32x4_t vi1x3456 = vextq_f32(vi1x0123, vi1x4567, 3);
223       const float32x4_t vi2x3456 = vextq_f32(vi2x0123, vi2x4567, 3);
224       const float32x4_t vi3x3456 = vextq_f32(vi3x0123, vi3x4567, 3);
225       const float32x4_t vi4x3456 = vextq_f32(vi4x0123, vi4x4567, 3);
226       const float32x4_t vi5x3456 = vextq_f32(vi5x0123, vi5x4567, 3);
227 
228       vo0p0 = vmlaq_lane_f32(vo0p0, vi0x3456, vget_low_f32(vw0123), 1);
229       vo1p0 = vmlaq_lane_f32(vo1p0, vi1x3456, vget_low_f32(vw0123), 1);
230       vo2p0 = vmlaq_lane_f32(vo2p0, vi2x3456, vget_low_f32(vw0123), 1);
231       vo3p0 = vmlaq_lane_f32(vo3p0, vi3x3456, vget_low_f32(vw0123), 1);
232 
233       vo0p0 = vmlaq_lane_f32(vo0p0, vi1x3456, vget_low_f32(vw4567), 0);
234       vo1p0 = vmlaq_lane_f32(vo1p0, vi2x3456, vget_low_f32(vw4567), 0);
235       vo2p0 = vmlaq_lane_f32(vo2p0, vi3x3456, vget_low_f32(vw4567), 0);
236       vo3p0 = vmlaq_lane_f32(vo3p0, vi4x3456, vget_low_f32(vw4567), 0);
237 
238       vo0p0 = vmlaq_lane_f32(vo0p0, vi2x3456, vget_high_f32(vw4567), 1);
239       vo1p0 = vmlaq_lane_f32(vo1p0, vi3x3456, vget_high_f32(vw4567), 1);
240       vo2p0 = vmlaq_lane_f32(vo2p0, vi4x3456, vget_high_f32(vw4567), 1);
241       vo3p0 = vmlaq_lane_f32(vo3p0, vi5x3456, vget_high_f32(vw4567), 1);
242 
243       const float32x4_t vzero = vmovq_n_f32(0.0f);
244       const float32x4_t vi0x5678 = vextq_f32(vi0x4567, vzero, 1);
245       const float32x4_t vi1x5678 = vextq_f32(vi1x4567, vzero, 1);
246       const float32x4_t vi2x5678 = vextq_f32(vi2x4567, vzero, 1);
247       const float32x4_t vi3x5678 = vextq_f32(vi3x4567, vzero, 1);
248       const float32x4_t vi4x5678 = vextq_f32(vi4x4567, vzero, 1);
249       const float32x4_t vi5x5678 = vextq_f32(vi5x4567, vzero, 1);
250 
251       vo0p0 = vmlaq_lane_f32(vo0p0, vi0x5678, vget_high_f32(vw0123), 1);
252       vo1p0 = vmlaq_lane_f32(vo1p0, vi1x5678, vget_high_f32(vw0123), 1);
253       vo2p0 = vmlaq_lane_f32(vo2p0, vi2x5678, vget_high_f32(vw0123), 1);
254       vo3p0 = vmlaq_lane_f32(vo3p0, vi3x5678, vget_high_f32(vw0123), 1);
255 
256       vo0p0 = vmlaq_lane_f32(vo0p0, vi1x5678, vget_high_f32(vw4567), 0);
257       vo1p0 = vmlaq_lane_f32(vo1p0, vi2x5678, vget_high_f32(vw4567), 0);
258       vo2p0 = vmlaq_lane_f32(vo2p0, vi3x5678, vget_high_f32(vw4567), 0);
259       vo3p0 = vmlaq_lane_f32(vo3p0, vi4x5678, vget_high_f32(vw4567), 0);
260 
261       vo0p0 = vmlaq_lane_f32(vo0p0, vi2x5678, vw89, 1);
262       vo1p0 = vmlaq_lane_f32(vo1p0, vi3x5678, vw89, 1);
263       vo2p0 = vmlaq_lane_f32(vo2p0, vi4x5678, vw89, 1);
264       vo3p0 = vmlaq_lane_f32(vo3p0, vi5x5678, vw89, 1);
265 
266 
267       float32x4_t vo0 = vmaxq_f32(vo0p0, vmin);
268       float32x4_t vo1 = vmaxq_f32(vo1p0, vmin);
269       float32x4_t vo2 = vmaxq_f32(vo2p0, vmin);
270       float32x4_t vo3 = vmaxq_f32(vo3p0, vmin);
271 
272       vo0 = vminq_f32(vo0, vmax);
273       vo1 = vminq_f32(vo1, vmax);
274       vo2 = vminq_f32(vo2, vmax);
275       vo3 = vminq_f32(vo3, vmax);
276 
277       if XNN_LIKELY(w == 4 * sizeof(float)) {
278         vst1q_f32(o3, vo3); o3 += 4;
279         vst1q_f32(o2, vo2); o2 += 4;
280         vst1q_f32(o1, vo1); o1 += 4;
281         vst1q_f32(o0, vo0); o0 += 4;
282       } else {
283         float32x2_t vo0_lo = vget_low_f32(vo0);
284         float32x2_t vo1_lo = vget_low_f32(vo1);
285         float32x2_t vo2_lo = vget_low_f32(vo2);
286         float32x2_t vo3_lo = vget_low_f32(vo3);
287         if (w & (2 * sizeof(float))) {
288           vst1_f32(o3, vo3_lo); o3 += 2;
289           vst1_f32(o2, vo2_lo); o2 += 2;
290           vst1_f32(o1, vo1_lo); o1 += 2;
291           vst1_f32(o0, vo0_lo); o0 += 2;
292 
293           vo0_lo = vget_high_f32(vo0);
294           vo1_lo = vget_high_f32(vo1);
295           vo2_lo = vget_high_f32(vo2);
296           vo3_lo = vget_high_f32(vo3);
297         }
298         if (w & (1 * sizeof(float))) {
299           vst1_lane_f32(o3, vo3_lo, 0); o3 += 1;
300           vst1_lane_f32(o2, vo2_lo, 0); o2 += 1;
301           vst1_lane_f32(o1, vo1_lo, 0); o1 += 1;
302           vst1_lane_f32(o0, vo0_lo, 0); o0 += 1;
303         }
304       }
305     }
306 
307     i0 = (const float*) ((uintptr_t) i4 - input_decrement);
308     i1 = (const float*) ((uintptr_t) i5 - input_decrement);
309     i2 = (const float*) ((uintptr_t) i1 + input_width);
310     i3 = (const float*) ((uintptr_t) i2 + input_width);
311     i4 = (const float*) ((uintptr_t) i3 + input_width);
312     i5 = (const float*) ((uintptr_t) i4 + input_width);
313 
314     o0 = o3;
315     o1 = (float*) ((uintptr_t) o0 + input_width);
316     o2 = (float*) ((uintptr_t) o1 + input_width);
317     o3 = (float*) ((uintptr_t) o2 + input_width);
318 
319     output_height = doz(output_height, 4);
320   } while (output_height != 0);
321 }
322