1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-dwconv2d-chw/3x3s2p1-sse.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 <xmmintrin.h>
13 
14 #include <xnnpack/dwconv.h>
15 #include <xnnpack/math.h>
16 
17 
xnn_f32_dwconv2d_chw_ukernel_3x3s2p1__sse_2x4(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_3x3s2p1__sse_2x4(
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 >= 0);
32   assert(padding_top <= 1);
33 
34   const __m128 vmask_even = _mm_load_ps((const float*) params->sse.mask_even);
35   const __m128 vmask_odd  = _mm_load_ps((const float*) params->sse.mask_odd);
36   const __m128 vmax = _mm_load_ps(params->sse.max);
37   const __m128 vmin = _mm_load_ps(params->sse.min);
38 
39   const __m128 vbias = _mm_load1_ps(weights);
40   const __m128 vk00 = _mm_load1_ps(weights + 1);
41   const __m128 vk01 = _mm_load1_ps(weights + 2);
42   const __m128 vk02 = _mm_load1_ps(weights + 3);
43   const __m128 vk10 = _mm_load1_ps(weights + 4);
44   const __m128 vk11 = _mm_load1_ps(weights + 5);
45   const __m128 vk12 = _mm_load1_ps(weights + 6);
46   const __m128 vk20 = _mm_load1_ps(weights + 7);
47   const __m128 vk21 = _mm_load1_ps(weights + 8);
48   const __m128 vk22 = _mm_load1_ps(weights + 9);
49 
50   const size_t input_decrement = round_down_po2(input_width, 4 /* SIMD output width */ * 2 /* subsampling */ * sizeof(float));
51   const size_t output_width = round_down_po2((input_width + (2 /* padding */ - 3 /* kernel size */ + 2 /* subsampling */) * sizeof(float)) / 2, sizeof(float));
52 
53   const float* i0 = (const float*) ((uintptr_t) input - ((-padding_top) & input_width));
54   const float* i1 = (const float*) ((uintptr_t) i0 + input_width);
55   if XNN_UNPREDICTABLE(padding_top != 0) {
56     i0 = zero;
57   }
58   const float* i2 = (const float*) ((uintptr_t) i1 + input_width);
59   const float* i3 = (const float*) ((uintptr_t) i2 + input_width);
60   const float* i4 = (const float*) ((uintptr_t) i3 + input_width);
61 
62   float* o0 = output;
63   float* o1 = (float*) ((uintptr_t) o0 + output_width);
64 
65   size_t padded_input_height = input_height + padding_top + 1 /* padding bottom */;
66   size_t output_height = (padded_input_height - 3 /* kernel size */ + 2 /* subsampling */) / 2;
67   do {
68     if XNN_UNPREDICTABLE(padded_input_height < 4) {
69       i2 = zero;
70     }
71     if XNN_UNPREDICTABLE(padded_input_height < 5) {
72       i3 = zero;
73       o1 = o0;
74     }
75     if XNN_UNPREDICTABLE(padded_input_height < 6) {
76       i4 = zero;
77     }
78 
79     __m128 vi0x7531 = _mm_setzero_ps();
80     __m128 vi1x7531 = _mm_setzero_ps();
81     __m128 vi2x7531 = _mm_setzero_ps();
82     __m128 vi3x7531 = _mm_setzero_ps();
83     __m128 vi4x7531 = _mm_setzero_ps();
84 
85     size_t w = input_width;
86     for (; w >= 8 * sizeof(float); w -= 8 * sizeof(float)) {
87       const __m128 vi0x89AB = _mm_loadu_ps(i0);
88       const __m128 vi0xCDEF = _mm_loadu_ps(i0 + 4);
89       i0 += 8;
90       const __m128 vi1x89AB = _mm_loadu_ps(i1);
91       const __m128 vi1xCDEF = _mm_loadu_ps(i1 + 4);
92       i1 += 8;
93       const __m128 vi2x89AB = _mm_loadu_ps(i2);
94       const __m128 vi2xCDEF = _mm_loadu_ps(i2 + 4);
95       i2 += 8;
96       const __m128 vi3x89AB = _mm_loadu_ps(i3);
97       const __m128 vi3xCDEF = _mm_loadu_ps(i3 + 4);
98       i3 += 8;
99       const __m128 vi4x89AB = _mm_loadu_ps(i4);
100       const __m128 vi4xCDEF = _mm_loadu_ps(i4 + 4);
101       i4 += 8;
102 
103       const __m128 vi0x8ACE = _mm_shuffle_ps(vi0x89AB, vi0xCDEF, _MM_SHUFFLE(2, 0, 2, 0));
104       const __m128 vi0x9BDF = _mm_shuffle_ps(vi0x89AB, vi0xCDEF, _MM_SHUFFLE(3, 1, 3, 1));
105       const __m128 vi1x8ACE = _mm_shuffle_ps(vi1x89AB, vi1xCDEF, _MM_SHUFFLE(2, 0, 2, 0));
106       const __m128 vi1x9BDF = _mm_shuffle_ps(vi1x89AB, vi1xCDEF, _MM_SHUFFLE(3, 1, 3, 1));
107       const __m128 vi2x8ACE = _mm_shuffle_ps(vi2x89AB, vi2xCDEF, _MM_SHUFFLE(2, 0, 2, 0));
108       const __m128 vi2x9BDF = _mm_shuffle_ps(vi2x89AB, vi2xCDEF, _MM_SHUFFLE(3, 1, 3, 1));
109       const __m128 vi3x8ACE = _mm_shuffle_ps(vi3x89AB, vi3xCDEF, _MM_SHUFFLE(2, 0, 2, 0));
110       const __m128 vi3x9BDF = _mm_shuffle_ps(vi3x89AB, vi3xCDEF, _MM_SHUFFLE(3, 1, 3, 1));
111       const __m128 vi4x8ACE = _mm_shuffle_ps(vi4x89AB, vi4xCDEF, _MM_SHUFFLE(2, 0, 2, 0));
112       const __m128 vi4x9BDF = _mm_shuffle_ps(vi4x89AB, vi4xCDEF, _MM_SHUFFLE(3, 1, 3, 1));
113 
114       __m128 vo0p0 = _mm_add_ps(vbias, _mm_mul_ps(vi0x8ACE, vk01));
115       __m128 vo1p0 = _mm_add_ps(vbias, _mm_mul_ps(vi2x8ACE, vk01));
116       vo0p0 = _mm_add_ps(vo0p0, _mm_mul_ps(vi1x8ACE, vk11));
117       vo1p0 = _mm_add_ps(vo1p0, _mm_mul_ps(vi3x8ACE, vk11));
118       vo0p0 = _mm_add_ps(vo0p0, _mm_mul_ps(vi2x8ACE, vk21));
119       vo1p0 = _mm_add_ps(vo1p0, _mm_mul_ps(vi4x8ACE, vk21));
120 
121       const __m128 vi0xF9BD = _mm_shuffle_ps(vi0x9BDF, vi0x9BDF, _MM_SHUFFLE(2, 1, 0, 3));
122       const __m128 vi1xF9BD = _mm_shuffle_ps(vi1x9BDF, vi1x9BDF, _MM_SHUFFLE(2, 1, 0, 3));
123       const __m128 vi2xF9BD = _mm_shuffle_ps(vi2x9BDF, vi2x9BDF, _MM_SHUFFLE(2, 1, 0, 3));
124       const __m128 vi3xF9BD = _mm_shuffle_ps(vi3x9BDF, vi3x9BDF, _MM_SHUFFLE(2, 1, 0, 3));
125       const __m128 vi4xF9BD = _mm_shuffle_ps(vi4x9BDF, vi4x9BDF, _MM_SHUFFLE(2, 1, 0, 3));
126 
127       vo0p0 = _mm_add_ps(vo0p0, _mm_mul_ps(vi0x9BDF, vk02));
128       vo1p0 = _mm_add_ps(vo1p0, _mm_mul_ps(vi2x9BDF, vk02));
129       vo0p0 = _mm_add_ps(vo0p0, _mm_mul_ps(vi1x9BDF, vk12));
130       vo1p0 = _mm_add_ps(vo1p0, _mm_mul_ps(vi3x9BDF, vk12));
131       vo0p0 = _mm_add_ps(vo0p0, _mm_mul_ps(vi2x9BDF, vk22));
132       vo1p0 = _mm_add_ps(vo1p0, _mm_mul_ps(vi4x9BDF, vk22));
133 
134       const __m128 vi0x7BDF = _mm_move_ss(vi0xF9BD, vi0x7531);
135       const __m128 vi1x7BDF = _mm_move_ss(vi1xF9BD, vi1x7531);
136       const __m128 vi2x7BDF = _mm_move_ss(vi2xF9BD, vi2x7531);
137       const __m128 vi3x7BDF = _mm_move_ss(vi3xF9BD, vi3x7531);
138       const __m128 vi4x7BDF = _mm_move_ss(vi4xF9BD, vi4x7531);
139 
140       vi0x7531 = vi0xF9BD;
141       vi1x7531 = vi1xF9BD;
142       vi2x7531 = vi2xF9BD;
143       vi3x7531 = vi3xF9BD;
144       vi4x7531 = vi4xF9BD;
145 
146       vo0p0 = _mm_add_ps(vo0p0, _mm_mul_ps(vi0x7BDF, vk00));
147       vo1p0 = _mm_add_ps(vo1p0, _mm_mul_ps(vi2x7BDF, vk00));
148       vo0p0 = _mm_add_ps(vo0p0, _mm_mul_ps(vi1x7BDF, vk10));
149       vo1p0 = _mm_add_ps(vo1p0, _mm_mul_ps(vi3x7BDF, vk10));
150       vo0p0 = _mm_add_ps(vo0p0, _mm_mul_ps(vi2x7BDF, vk20));
151       vo1p0 = _mm_add_ps(vo1p0, _mm_mul_ps(vi4x7BDF, vk20));
152 
153 
154       __m128 vo0 = _mm_max_ps(vo0p0, vmin);
155       __m128 vo1 = _mm_max_ps(vo1p0, vmin);
156 
157       vo0 = _mm_min_ps(vo0, vmax);
158       vo1 = _mm_min_ps(vo1, vmax);
159 
160       _mm_storeu_ps(o1, vo1);
161       o1 += 4;
162       _mm_storeu_ps(o0, vo0);
163       o0 += 4;
164     }
165     // Potentially process the last block of 0..7 pixels.
166     assert(w < 8 * sizeof(float));
167     if XNN_LIKELY(w != 0) {
168       const __m128 vi0x89AB = _mm_loadu_ps(i0);
169       const __m128 vi0xCDEF = _mm_loadu_ps(i0 + 4);
170       const __m128 vi1x89AB = _mm_loadu_ps(i1);
171       const __m128 vi1xCDEF = _mm_loadu_ps(i1 + 4);
172       const __m128 vi2x89AB = _mm_loadu_ps(i2);
173       const __m128 vi2xCDEF = _mm_loadu_ps(i2 + 4);
174       const __m128 vi3x89AB = _mm_loadu_ps(i3);
175       const __m128 vi3xCDEF = _mm_loadu_ps(i3 + 4);
176       const __m128 vi4x89AB = _mm_loadu_ps(i4);
177       const __m128 vi4xCDEF = _mm_loadu_ps(i4 + 4);
178 
179       const __m128 vi0x8ACE = _mm_and_ps(vmask_even, _mm_shuffle_ps(vi0x89AB, vi0xCDEF, _MM_SHUFFLE(2, 0, 2, 0)));
180       const __m128 vi0x9BDF = _mm_and_ps(vmask_odd,  _mm_shuffle_ps(vi0x89AB, vi0xCDEF, _MM_SHUFFLE(3, 1, 3, 1)));
181       const __m128 vi1x8ACE = _mm_and_ps(vmask_even, _mm_shuffle_ps(vi1x89AB, vi1xCDEF, _MM_SHUFFLE(2, 0, 2, 0)));
182       const __m128 vi1x9BDF = _mm_and_ps(vmask_odd,  _mm_shuffle_ps(vi1x89AB, vi1xCDEF, _MM_SHUFFLE(3, 1, 3, 1)));
183       const __m128 vi2x8ACE = _mm_and_ps(vmask_even, _mm_shuffle_ps(vi2x89AB, vi2xCDEF, _MM_SHUFFLE(2, 0, 2, 0)));
184       const __m128 vi2x9BDF = _mm_and_ps(vmask_odd,  _mm_shuffle_ps(vi2x89AB, vi2xCDEF, _MM_SHUFFLE(3, 1, 3, 1)));
185       const __m128 vi3x8ACE = _mm_and_ps(vmask_even, _mm_shuffle_ps(vi3x89AB, vi3xCDEF, _MM_SHUFFLE(2, 0, 2, 0)));
186       const __m128 vi3x9BDF = _mm_and_ps(vmask_odd,  _mm_shuffle_ps(vi3x89AB, vi3xCDEF, _MM_SHUFFLE(3, 1, 3, 1)));
187       const __m128 vi4x8ACE = _mm_and_ps(vmask_even, _mm_shuffle_ps(vi4x89AB, vi4xCDEF, _MM_SHUFFLE(2, 0, 2, 0)));
188       const __m128 vi4x9BDF = _mm_and_ps(vmask_odd,  _mm_shuffle_ps(vi4x89AB, vi4xCDEF, _MM_SHUFFLE(3, 1, 3, 1)));
189 
190       __m128 vo0p0 = _mm_add_ps(vbias, _mm_mul_ps(vi0x8ACE, vk01));
191       __m128 vo1p0 = _mm_add_ps(vbias, _mm_mul_ps(vi2x8ACE, vk01));
192       vo0p0 = _mm_add_ps(vo0p0, _mm_mul_ps(vi1x8ACE, vk11));
193       vo1p0 = _mm_add_ps(vo1p0, _mm_mul_ps(vi3x8ACE, vk11));
194       vo0p0 = _mm_add_ps(vo0p0, _mm_mul_ps(vi2x8ACE, vk21));
195       vo1p0 = _mm_add_ps(vo1p0, _mm_mul_ps(vi4x8ACE, vk21));
196 
197       const __m128 vi0xF9BD = _mm_shuffle_ps(vi0x9BDF, vi0x9BDF, _MM_SHUFFLE(2, 1, 0, 3));
198       const __m128 vi1xF9BD = _mm_shuffle_ps(vi1x9BDF, vi1x9BDF, _MM_SHUFFLE(2, 1, 0, 3));
199       const __m128 vi2xF9BD = _mm_shuffle_ps(vi2x9BDF, vi2x9BDF, _MM_SHUFFLE(2, 1, 0, 3));
200       const __m128 vi3xF9BD = _mm_shuffle_ps(vi3x9BDF, vi3x9BDF, _MM_SHUFFLE(2, 1, 0, 3));
201       const __m128 vi4xF9BD = _mm_shuffle_ps(vi4x9BDF, vi4x9BDF, _MM_SHUFFLE(2, 1, 0, 3));
202 
203       vo0p0 = _mm_add_ps(vo0p0, _mm_mul_ps(vi0x9BDF, vk02));
204       vo1p0 = _mm_add_ps(vo1p0, _mm_mul_ps(vi2x9BDF, vk02));
205       vo0p0 = _mm_add_ps(vo0p0, _mm_mul_ps(vi1x9BDF, vk12));
206       vo1p0 = _mm_add_ps(vo1p0, _mm_mul_ps(vi3x9BDF, vk12));
207       vo0p0 = _mm_add_ps(vo0p0, _mm_mul_ps(vi2x9BDF, vk22));
208       vo1p0 = _mm_add_ps(vo1p0, _mm_mul_ps(vi4x9BDF, vk22));
209 
210       const __m128 vi0x7BDF = _mm_move_ss(vi0xF9BD, vi0x7531);
211       const __m128 vi1x7BDF = _mm_move_ss(vi1xF9BD, vi1x7531);
212       const __m128 vi2x7BDF = _mm_move_ss(vi2xF9BD, vi2x7531);
213       const __m128 vi3x7BDF = _mm_move_ss(vi3xF9BD, vi3x7531);
214       const __m128 vi4x7BDF = _mm_move_ss(vi4xF9BD, vi4x7531);
215 
216       vi0x7531 = vi0xF9BD;
217       vi1x7531 = vi1xF9BD;
218       vi2x7531 = vi2xF9BD;
219       vi3x7531 = vi3xF9BD;
220       vi4x7531 = vi4xF9BD;
221 
222       vo0p0 = _mm_add_ps(vo0p0, _mm_mul_ps(vi0x7BDF, vk00));
223       vo1p0 = _mm_add_ps(vo1p0, _mm_mul_ps(vi2x7BDF, vk00));
224       vo0p0 = _mm_add_ps(vo0p0, _mm_mul_ps(vi1x7BDF, vk10));
225       vo1p0 = _mm_add_ps(vo1p0, _mm_mul_ps(vi3x7BDF, vk10));
226       vo0p0 = _mm_add_ps(vo0p0, _mm_mul_ps(vi2x7BDF, vk20));
227       vo1p0 = _mm_add_ps(vo1p0, _mm_mul_ps(vi4x7BDF, vk20));
228 
229 
230       __m128 vo0 = _mm_max_ps(vo0p0, vmin);
231       __m128 vo1 = _mm_max_ps(vo1p0, vmin);
232 
233       vo0 = _mm_min_ps(vo0, vmax);
234       vo1 = _mm_min_ps(vo1, vmax);
235 
236       if (w == 7 * sizeof(float)) {
237         _mm_storeu_ps(o1, vo1);
238         o1 += 4;
239         _mm_storeu_ps(o0, vo0);
240         o0 += 4;
241       } else {
242         w += 1 * sizeof(float);
243         if (w & (4 * sizeof(float))) {
244           _mm_storel_pi((__m64*) o1, vo1);
245           o1 += 2;
246           _mm_storel_pi((__m64*) o0, vo0);
247           o0 += 2;
248 
249           vo0 = _mm_movehl_ps(vo0, vo0);
250           vo1 = _mm_movehl_ps(vo1, vo1);
251         }
252         if (w & (2 * sizeof(float))) {
253           _mm_store_ss(o1, vo1);
254           o1 += 1;
255           _mm_store_ss(o0, vo0);
256           o0 += 1;
257         }
258       }
259     }
260 
261     i0 = (const float*) ((uintptr_t) i4 - input_decrement);
262     i1 = (const float*) ((uintptr_t) i0 + input_width);
263     i2 = (const float*) ((uintptr_t) i1 + input_width);
264     i3 = (const float*) ((uintptr_t) i2 + input_width);
265     i4 = (const float*) ((uintptr_t) i3 + input_width);
266 
267     o0 = o1;
268     o1 = (float*) ((uintptr_t) o0 + output_width);
269 
270     output_height = doz(output_height, 2);
271     padded_input_height = doz(padded_input_height, 4);
272   } while (output_height != 0);
273 }
274