• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-dwconv2d-chw/3x3p1-ssse3.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 <tmmintrin.h>
13 
14 #include <xnnpack/dwconv.h>
15 #include <xnnpack/math.h>
16 
17 
xnn_f32_dwconv2d_chw_ukernel_3x3p1__ssse3_2x4_acc2(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__ssse3_2x4_acc2(
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 __m128 vmask = _mm_load_ps((const float*) params->sse.mask);
34   const __m128 vmax = _mm_load_ps(params->sse.max);
35   const __m128 vmin = _mm_load_ps(params->sse.min);
36 
37   const __m128 vbias = _mm_load1_ps(weights);
38   const __m128 vk00 = _mm_load1_ps(weights + 1);
39   const __m128 vk01 = _mm_load1_ps(weights + 2);
40   const __m128 vk02 = _mm_load1_ps(weights + 3);
41   const __m128 vk10 = _mm_load1_ps(weights + 4);
42   const __m128 vk11 = _mm_load1_ps(weights + 5);
43   const __m128 vk12 = _mm_load1_ps(weights + 6);
44   const __m128 vk20 = _mm_load1_ps(weights + 7);
45   const __m128 vk21 = _mm_load1_ps(weights + 8);
46   const __m128 vk22 = _mm_load1_ps(weights + 9);
47 
48   const size_t input_decrement = round_up_po2(input_width, 4 * sizeof(float));
49 
50   const float* i0 = zero;
51   const float* i1 = input;
52   const float* i2 = (const float*) ((uintptr_t) i1 + input_width);
53   const float* i3 = (const float*) ((uintptr_t) i2 + input_width);
54 
55   float* o0 = output;
56   float* o1 = (float*) ((uintptr_t) o0 + input_width);
57 
58   size_t output_height = input_height;
59   do {
60     if XNN_UNPREDICTABLE(output_height < 2) {
61       i2 = zero;
62       o1 = o0;
63     }
64     if XNN_UNPREDICTABLE(output_height < 3) {
65       i3 = zero;
66     }
67 
68     __m128 vi0x0123 = _mm_setzero_ps();
69     __m128 vi1x0123 = _mm_setzero_ps();
70     __m128 vi2x0123 = _mm_setzero_ps();
71     __m128 vi3x0123 = _mm_setzero_ps();
72 
73     __m128 vi0x4567 = _mm_loadu_ps(i0);
74     i0 += 4;
75     __m128 vi1x4567 = _mm_loadu_ps(i1);
76     i1 += 4;
77     __m128 vi2x4567 = _mm_loadu_ps(i2);
78     i2 += 4;
79     __m128 vi3x4567 = _mm_loadu_ps(i3);
80     i3 += 4;
81 
82     size_t w = input_width;
83     for (; w > 4 * sizeof(float); w -= 4 * sizeof(float)) {
84       const __m128 vi0x89AB = _mm_loadu_ps(i0);
85       i0 += 4;
86       const __m128 vi1x89AB = _mm_loadu_ps(i1);
87       i1 += 4;
88       const __m128 vi2x89AB = _mm_loadu_ps(i2);
89       i2 += 4;
90       const __m128 vi3x89AB = _mm_loadu_ps(i3);
91       i3 += 4;
92 
93       __m128 vo0p0 = _mm_add_ps(vbias, _mm_mul_ps(vi0x4567, vk01));
94       __m128 vo1p0 = _mm_add_ps(vbias, _mm_mul_ps(vi1x4567, vk01));
95       __m128 vo0p1 = _mm_mul_ps(vi1x4567, vk11);
96       __m128 vo1p1 = _mm_mul_ps(vi2x4567, vk11);
97       vo0p0 = _mm_add_ps(vo0p0, _mm_mul_ps(vi2x4567, vk21));
98       vo1p0 = _mm_add_ps(vo1p0, _mm_mul_ps(vi3x4567, vk21));
99 
100       const __m128 vi0x3456 = _mm_castsi128_ps(_mm_alignr_epi8(_mm_castps_si128(vi0x4567), _mm_castps_si128(vi0x0123), 12));
101       const __m128 vi1x3456 = _mm_castsi128_ps(_mm_alignr_epi8(_mm_castps_si128(vi1x4567), _mm_castps_si128(vi1x0123), 12));
102       const __m128 vi2x3456 = _mm_castsi128_ps(_mm_alignr_epi8(_mm_castps_si128(vi2x4567), _mm_castps_si128(vi2x0123), 12));
103       const __m128 vi3x3456 = _mm_castsi128_ps(_mm_alignr_epi8(_mm_castps_si128(vi3x4567), _mm_castps_si128(vi3x0123), 12));
104 
105       vo0p1 = _mm_add_ps(vo0p1, _mm_mul_ps(vi0x3456, vk00));
106       vo1p1 = _mm_add_ps(vo1p1, _mm_mul_ps(vi1x3456, vk00));
107       vo0p0 = _mm_add_ps(vo0p0, _mm_mul_ps(vi1x3456, vk10));
108       vo1p0 = _mm_add_ps(vo1p0, _mm_mul_ps(vi2x3456, vk10));
109       vo0p1 = _mm_add_ps(vo0p1, _mm_mul_ps(vi2x3456, vk20));
110       vo1p1 = _mm_add_ps(vo1p1, _mm_mul_ps(vi3x3456, vk20));
111 
112       vi0x0123 = vi0x4567;
113       vi1x0123 = vi1x4567;
114       vi2x0123 = vi2x4567;
115       vi3x0123 = vi3x4567;
116 
117       const __m128 vi0x5678 = _mm_castsi128_ps(_mm_alignr_epi8(_mm_castps_si128(vi0x89AB), _mm_castps_si128(vi0x4567), 4));
118       const __m128 vi1x5678 = _mm_castsi128_ps(_mm_alignr_epi8(_mm_castps_si128(vi1x89AB), _mm_castps_si128(vi1x4567), 4));
119       const __m128 vi2x5678 = _mm_castsi128_ps(_mm_alignr_epi8(_mm_castps_si128(vi2x89AB), _mm_castps_si128(vi2x4567), 4));
120       const __m128 vi3x5678 = _mm_castsi128_ps(_mm_alignr_epi8(_mm_castps_si128(vi3x89AB), _mm_castps_si128(vi3x4567), 4));
121 
122       vo0p0 = _mm_add_ps(vo0p0, _mm_mul_ps(vi0x5678, vk02));
123       vo1p0 = _mm_add_ps(vo1p0, _mm_mul_ps(vi1x5678, vk02));
124       vo0p1 = _mm_add_ps(vo0p1, _mm_mul_ps(vi1x5678, vk12));
125       vo1p1 = _mm_add_ps(vo1p1, _mm_mul_ps(vi2x5678, vk12));
126       vo0p0 = _mm_add_ps(vo0p0, _mm_mul_ps(vi2x5678, vk22));
127       vo1p0 = _mm_add_ps(vo1p0, _mm_mul_ps(vi3x5678, vk22));
128 
129       vi0x4567 = vi0x89AB;
130       vi1x4567 = vi1x89AB;
131       vi2x4567 = vi2x89AB;
132       vi3x4567 = vi3x89AB;
133 
134       vo0p0 = _mm_add_ps(vo0p0, vo0p1);
135       vo1p0 = _mm_add_ps(vo1p0, vo1p1);
136 
137       __m128 vo0 = _mm_max_ps(vo0p0, vmin);
138       __m128 vo1 = _mm_max_ps(vo1p0, vmin);
139 
140       vo0 = _mm_min_ps(vo0, vmax);
141       vo1 = _mm_min_ps(vo1, vmax);
142 
143       _mm_storeu_ps(o1, vo1);
144       o1 += 4;
145       _mm_storeu_ps(o0, vo0);
146       o0 += 4;
147     }
148     // Always process the last block of 1..4 pixels.
149     assert(w >= 1 * sizeof(float));
150     assert(w <= 4 * sizeof(float));
151     {
152       vi0x4567 = _mm_and_ps(vmask, vi0x4567);
153       vi1x4567 = _mm_and_ps(vmask, vi1x4567);
154       vi2x4567 = _mm_and_ps(vmask, vi2x4567);
155       vi3x4567 = _mm_and_ps(vmask, vi3x4567);
156 
157       __m128 vo0p0 = _mm_add_ps(vbias, _mm_mul_ps(vi0x4567, vk01));
158       __m128 vo1p0 = _mm_add_ps(vbias, _mm_mul_ps(vi1x4567, vk01));
159       __m128 vo0p1 = _mm_mul_ps(vi1x4567, vk11);
160       __m128 vo1p1 = _mm_mul_ps(vi2x4567, vk11);
161       vo0p0 = _mm_add_ps(vo0p0, _mm_mul_ps(vi2x4567, vk21));
162       vo1p0 = _mm_add_ps(vo1p0, _mm_mul_ps(vi3x4567, vk21));
163 
164       const __m128 vi0x3456 = _mm_castsi128_ps(_mm_alignr_epi8(_mm_castps_si128(vi0x4567), _mm_castps_si128(vi0x0123), 12));
165       const __m128 vi1x3456 = _mm_castsi128_ps(_mm_alignr_epi8(_mm_castps_si128(vi1x4567), _mm_castps_si128(vi1x0123), 12));
166       const __m128 vi2x3456 = _mm_castsi128_ps(_mm_alignr_epi8(_mm_castps_si128(vi2x4567), _mm_castps_si128(vi2x0123), 12));
167       const __m128 vi3x3456 = _mm_castsi128_ps(_mm_alignr_epi8(_mm_castps_si128(vi3x4567), _mm_castps_si128(vi3x0123), 12));
168 
169       vo0p1 = _mm_add_ps(vo0p1, _mm_mul_ps(vi0x3456, vk00));
170       vo1p1 = _mm_add_ps(vo1p1, _mm_mul_ps(vi1x3456, vk00));
171       vo0p0 = _mm_add_ps(vo0p0, _mm_mul_ps(vi1x3456, vk10));
172       vo1p0 = _mm_add_ps(vo1p0, _mm_mul_ps(vi2x3456, vk10));
173       vo0p1 = _mm_add_ps(vo0p1, _mm_mul_ps(vi2x3456, vk20));
174       vo1p1 = _mm_add_ps(vo1p1, _mm_mul_ps(vi3x3456, vk20));
175 
176       const __m128i vzero = _mm_setzero_si128();
177       const __m128 vi0x5678 = _mm_castsi128_ps(_mm_alignr_epi8(vzero, _mm_castps_si128(vi0x4567), 4));
178       const __m128 vi1x5678 = _mm_castsi128_ps(_mm_alignr_epi8(vzero, _mm_castps_si128(vi1x4567), 4));
179       const __m128 vi2x5678 = _mm_castsi128_ps(_mm_alignr_epi8(vzero, _mm_castps_si128(vi2x4567), 4));
180       const __m128 vi3x5678 = _mm_castsi128_ps(_mm_alignr_epi8(vzero, _mm_castps_si128(vi3x4567), 4));
181 
182       vo0p0 = _mm_add_ps(vo0p0, _mm_mul_ps(vi0x5678, vk02));
183       vo1p0 = _mm_add_ps(vo1p0, _mm_mul_ps(vi1x5678, vk02));
184       vo0p1 = _mm_add_ps(vo0p1, _mm_mul_ps(vi1x5678, vk12));
185       vo1p1 = _mm_add_ps(vo1p1, _mm_mul_ps(vi2x5678, vk12));
186       vo0p0 = _mm_add_ps(vo0p0, _mm_mul_ps(vi2x5678, vk22));
187       vo1p0 = _mm_add_ps(vo1p0, _mm_mul_ps(vi3x5678, vk22));
188 
189       vo0p0 = _mm_add_ps(vo0p0, vo0p1);
190       vo1p0 = _mm_add_ps(vo1p0, vo1p1);
191 
192       __m128 vo0 = _mm_max_ps(vo0p0, vmin);
193       __m128 vo1 = _mm_max_ps(vo1p0, vmin);
194 
195       vo0 = _mm_min_ps(vo0, vmax);
196       vo1 = _mm_min_ps(vo1, vmax);
197 
198       if XNN_LIKELY(w == 4 * sizeof(float)) {
199         _mm_storeu_ps(o1, vo1);
200         o1 += 4;
201         _mm_storeu_ps(o0, vo0);
202         o0 += 4;
203       } else {
204         if (w & (2 * sizeof(float))) {
205           _mm_storel_pi((__m64*) o1, vo1);
206           o1 += 2;
207           _mm_storel_pi((__m64*) o0, vo0);
208           o0 += 2;
209 
210           vo0 = _mm_movehl_ps(vo0, vo0);
211           vo1 = _mm_movehl_ps(vo1, vo1);
212         }
213         if (w & (1 * sizeof(float))) {
214           _mm_store_ss(o1, vo1);
215           o1 += 1;
216           _mm_store_ss(o0, vo0);
217           o0 += 1;
218         }
219       }
220     }
221 
222     i0 = (const float*) ((uintptr_t) i2 - input_decrement);
223     i1 = (const float*) ((uintptr_t) i3 - input_decrement);
224     i2 = (const float*) ((uintptr_t) i1 + input_width);
225     i3 = (const float*) ((uintptr_t) i2 + input_width);
226 
227     o0 = o1;
228     o1 = (float*) ((uintptr_t) o0 + input_width);
229 
230     output_height = doz(output_height, 2);
231   } while (output_height != 0);
232 }
233