1 // Auto-generated file. Do not edit!
2 //   Template: src/qs8-gavgpool/unipass-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 <smmintrin.h>
13 
14 #include <xnnpack/gavgpool.h>
15 
16 
xnn_qs8_gavgpool_minmax_ukernel_7x__sse41_c16_acc2(size_t rows,size_t channels,const int8_t * input,size_t input_stride,const int8_t * zero,int8_t * output,const union xnn_qs8_avgpool_params params[restrict XNN_MIN_ELEMENTS (1)])17 void xnn_qs8_gavgpool_minmax_ukernel_7x__sse41_c16_acc2(
18     size_t rows,
19     size_t channels,
20     const int8_t* input,
21     size_t input_stride,
22     const int8_t* zero,
23     int8_t* output,
24     const union xnn_qs8_avgpool_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN
25 {
26   assert(rows != 0);
27   assert(rows <= 7);
28   assert(channels != 0);
29 
30   const int8_t* i0 = input;
31   const int8_t* i1 = (const int8_t*) ((uintptr_t) i0 + input_stride);
32   if XNN_UNPREDICTABLE(rows < 2) {
33     i1 = zero;
34   }
35   const int8_t* i2 = (const int8_t*) ((uintptr_t) i1 + input_stride);
36   if XNN_UNPREDICTABLE(rows <= 2) {
37     i2 = zero;
38   }
39   const int8_t* i3 = (const int8_t*) ((uintptr_t) i2 + input_stride);
40   if XNN_UNPREDICTABLE(rows < 4) {
41     i3 = zero;
42   }
43   const int8_t* i4 = (const int8_t*) ((uintptr_t) i3 + input_stride);
44   if XNN_UNPREDICTABLE(rows <= 4) {
45     i4 = zero;
46   }
47   const int8_t* i5 = (const int8_t*) ((uintptr_t) i4 + input_stride);
48   if XNN_UNPREDICTABLE(rows < 6) {
49     i5 = zero;
50   }
51   const int8_t* i6 = (const int8_t*) ((uintptr_t) i5 + input_stride);
52   if XNN_UNPREDICTABLE(rows <= 6) {
53     i6 = zero;
54   }
55 
56   const __m128i vbias = _mm_load_si128((const __m128i*) params->sse2.bias);
57   const __m128i vmultiplier = _mm_load_si128((const __m128i*) params->sse2.multiplier);
58   const __m128i vrounding = _mm_load_si128((const __m128i*) params->sse2.rounding);
59   const __m128i vshift = _mm_loadl_epi64((const __m128i*) params->sse2.shift);
60   while (channels >= 16) {
61     const __m128i vxi0x01234567 = _mm_cvtepi8_epi16(_mm_loadl_epi64((const __m128i*) i0));
62     const __m128i vxi0x89ABCDEF = _mm_cvtepi8_epi16(_mm_loadl_epi64((const __m128i*) (i0 + 8)));
63     i0 += 16;
64     const __m128i vxi1x01234567 = _mm_cvtepi8_epi16(_mm_loadl_epi64((const __m128i*) i1));
65     const __m128i vxi1x89ABCDEF = _mm_cvtepi8_epi16(_mm_loadl_epi64((const __m128i*) (i1 + 8)));
66     i1 += 16;
67     const __m128i vxi2x01234567 = _mm_cvtepi8_epi16(_mm_loadl_epi64((const __m128i*) i2));
68     const __m128i vxi2x89ABCDEF = _mm_cvtepi8_epi16(_mm_loadl_epi64((const __m128i*) (i2 + 8)));
69     i2 += 16;
70     const __m128i vxi3x01234567 = _mm_cvtepi8_epi16(_mm_loadl_epi64((const __m128i*) i3));
71     const __m128i vxi3x89ABCDEF = _mm_cvtepi8_epi16(_mm_loadl_epi64((const __m128i*) (i3 + 8)));
72     i3 += 16;
73     const __m128i vxi4x01234567 = _mm_cvtepi8_epi16(_mm_loadl_epi64((const __m128i*) i4));
74     const __m128i vxi4x89ABCDEF = _mm_cvtepi8_epi16(_mm_loadl_epi64((const __m128i*) (i4 + 8)));
75     i4 += 16;
76     const __m128i vxi5x01234567 = _mm_cvtepi8_epi16(_mm_loadl_epi64((const __m128i*) i5));
77     const __m128i vxi5x89ABCDEF = _mm_cvtepi8_epi16(_mm_loadl_epi64((const __m128i*) (i5 + 8)));
78     i5 += 16;
79     const __m128i vxi6x01234567 = _mm_cvtepi8_epi16(_mm_loadl_epi64((const __m128i*) i6));
80     const __m128i vxi6x89ABCDEF = _mm_cvtepi8_epi16(_mm_loadl_epi64((const __m128i*) (i6 + 8)));
81     i6 += 16;
82 
83 
84     __m128i vacc0x01234567 = _mm_add_epi16(vxi0x01234567, vxi1x01234567);
85     __m128i vacc0x89ABCDEF = _mm_add_epi16(vxi0x89ABCDEF, vxi1x89ABCDEF);
86     __m128i vacc1x01234567 = _mm_add_epi16(vxi2x01234567, vxi3x01234567);
87     __m128i vacc1x89ABCDEF = _mm_add_epi16(vxi2x89ABCDEF, vxi3x89ABCDEF);
88 
89     vacc0x01234567 = _mm_add_epi16(vacc0x01234567, vxi4x01234567);
90     vacc0x89ABCDEF = _mm_add_epi16(vacc0x89ABCDEF, vxi4x89ABCDEF);
91     vacc1x01234567 = _mm_add_epi16(vacc1x01234567, vxi5x01234567);
92     vacc1x89ABCDEF = _mm_add_epi16(vacc1x89ABCDEF, vxi5x89ABCDEF);
93     vacc0x01234567 = _mm_add_epi16(vacc0x01234567, vxi6x01234567);
94     vacc0x89ABCDEF = _mm_add_epi16(vacc0x89ABCDEF, vxi6x89ABCDEF);
95 
96     // Add up all accumulators to vacc0x0123456789ABCDEF
97     vacc0x01234567 = _mm_add_epi16(vacc0x01234567, vacc1x01234567);
98     vacc0x89ABCDEF = _mm_add_epi16(vacc0x89ABCDEF, vacc1x89ABCDEF);
99 
100     const __m128i vacc0123 = _mm_add_epi32(vbias, _mm_cvtepi16_epi32(vacc0x01234567));
101     const __m128i vacc4567 = _mm_add_epi32(vbias, _mm_unpackhi_epi16(vacc0x01234567, _mm_cmpgt_epi16(_mm_setzero_si128(), vacc0x01234567)));
102     const __m128i vacc89AB = _mm_add_epi32(vbias, _mm_cvtepi16_epi32(vacc0x89ABCDEF));
103     const __m128i vaccCDEF = _mm_add_epi32(vbias, _mm_unpackhi_epi16(vacc0x89ABCDEF, _mm_cmpgt_epi16(_mm_setzero_si128(), vacc0x89ABCDEF)));
104 
105     const __m128i vabsacc0123 = _mm_abs_epi32(vacc0123);
106     const __m128i vabsacc4567 = _mm_abs_epi32(vacc4567);
107     const __m128i vabsacc89AB = _mm_abs_epi32(vacc89AB);
108     const __m128i vabsaccCDEF = _mm_abs_epi32(vaccCDEF);
109 
110     const __m128i vabsacc13 = _mm_shuffle_epi32(vabsacc0123, _MM_SHUFFLE(3, 3, 1, 1));
111     const __m128i vabsacc57 = _mm_shuffle_epi32(vabsacc4567, _MM_SHUFFLE(3, 3, 1, 1));
112     const __m128i vabsacc9B = _mm_shuffle_epi32(vabsacc89AB, _MM_SHUFFLE(3, 3, 1, 1));
113     const __m128i vabsaccDF = _mm_shuffle_epi32(vabsaccCDEF, _MM_SHUFFLE(3, 3, 1, 1));
114 
115     const __m128i vabsprod02 = _mm_mul_epu32(vabsacc0123, vmultiplier);
116     const __m128i vabsprod13 = _mm_mul_epu32(vabsacc13, vmultiplier);
117     const __m128i vabsprod46 = _mm_mul_epu32(vabsacc4567, vmultiplier);
118     const __m128i vabsprod57 = _mm_mul_epu32(vabsacc57, vmultiplier);
119     const __m128i vabsprod8A = _mm_mul_epu32(vabsacc89AB, vmultiplier);
120     const __m128i vabsprod9B = _mm_mul_epu32(vabsacc9B, vmultiplier);
121     const __m128i vabsprodCE = _mm_mul_epu32(vabsaccCDEF, vmultiplier);
122     const __m128i vabsprodDF = _mm_mul_epu32(vabsaccDF, vmultiplier);
123 
124     const __m128i vabsout02 = _mm_srl_epi64(_mm_add_epi64(vabsprod02, vrounding), vshift);
125     const __m128i vabsout13 = _mm_srl_epi64(_mm_add_epi64(vabsprod13, vrounding), vshift);
126     const __m128i vabsout46 = _mm_srl_epi64(_mm_add_epi64(vabsprod46, vrounding), vshift);
127     const __m128i vabsout57 = _mm_srl_epi64(_mm_add_epi64(vabsprod57, vrounding), vshift);
128     const __m128i vabsout8A = _mm_srl_epi64(_mm_add_epi64(vabsprod8A, vrounding), vshift);
129     const __m128i vabsout9B = _mm_srl_epi64(_mm_add_epi64(vabsprod9B, vrounding), vshift);
130     const __m128i vabsoutCE = _mm_srl_epi64(_mm_add_epi64(vabsprodCE, vrounding), vshift);
131     const __m128i vabsoutDF = _mm_srl_epi64(_mm_add_epi64(vabsprodDF, vrounding), vshift);
132 
133     const __m128i vabsout0123 = _mm_blend_epi16(vabsout02, _mm_shuffle_epi32(vabsout13, _MM_SHUFFLE(2, 2, 0, 0)), 0xCC);
134     const __m128i vabsout4567 = _mm_blend_epi16(vabsout46, _mm_shuffle_epi32(vabsout57, _MM_SHUFFLE(2, 2, 0, 0)), 0xCC);
135     const __m128i vabsout89AB = _mm_blend_epi16(vabsout8A, _mm_shuffle_epi32(vabsout9B, _MM_SHUFFLE(2, 2, 0, 0)), 0xCC);
136     const __m128i vabsoutCDEF = _mm_blend_epi16(vabsoutCE, _mm_shuffle_epi32(vabsoutDF, _MM_SHUFFLE(2, 2, 0, 0)), 0xCC);
137 
138     const __m128i vout0123 = _mm_sign_epi32(vabsout0123, vacc0123);
139     const __m128i vout4567 = _mm_sign_epi32(vabsout4567, vacc4567);
140     const __m128i vout89AB = _mm_sign_epi32(vabsout89AB, vacc89AB);
141     const __m128i voutCDEF = _mm_sign_epi32(vabsoutCDEF, vaccCDEF);
142 
143     const __m128i voutput_zero_point = _mm_load_si128((const __m128i*) params->sse2.output_zero_point);
144     __m128i vout01234567 = _mm_adds_epi16(_mm_packs_epi32(vout0123, vout4567), voutput_zero_point);
145     __m128i vout89ABCDEF = _mm_adds_epi16(_mm_packs_epi32(vout89AB, voutCDEF), voutput_zero_point);
146 
147     const __m128i voutput_min = _mm_load_si128((const __m128i*) params->sse2.output_min);
148     const __m128i voutput_max = _mm_load_si128((const __m128i*) params->sse2.output_max);
149     vout01234567 = _mm_min_epi16(_mm_max_epi16(vout01234567, voutput_min), voutput_max);
150     vout89ABCDEF = _mm_min_epi16(_mm_max_epi16(vout89ABCDEF, voutput_min), voutput_max);
151 
152     __m128i vout0123456789ABCDEF = _mm_packs_epi16(vout01234567, vout89ABCDEF);
153 
154     _mm_storeu_si128((__m128i*) output, vout0123456789ABCDEF);
155     output += 16;
156 
157     channels -= 16;
158   }
159   if XNN_UNLIKELY(channels != 0) {
160     do {
161       const __m128i vxi0x01234567 = _mm_cvtepi8_epi16(_mm_loadl_epi64((const __m128i*) i0));
162       i0 += 8;
163       const __m128i vxi1x01234567 = _mm_cvtepi8_epi16(_mm_loadl_epi64((const __m128i*) i1));
164       i1 += 8;
165       const __m128i vxi2x01234567 = _mm_cvtepi8_epi16(_mm_loadl_epi64((const __m128i*) i2));
166       i2 += 8;
167       const __m128i vxi3x01234567 = _mm_cvtepi8_epi16(_mm_loadl_epi64((const __m128i*) i3));
168       i3 += 8;
169       const __m128i vxi4x01234567 = _mm_cvtepi8_epi16(_mm_loadl_epi64((const __m128i*) i4));
170       i4 += 8;
171       const __m128i vxi5x01234567 = _mm_cvtepi8_epi16(_mm_loadl_epi64((const __m128i*) i5));
172       i5 += 8;
173       const __m128i vxi6x01234567 = _mm_cvtepi8_epi16(_mm_loadl_epi64((const __m128i*) i6));
174       i6 += 8;
175 
176 
177       __m128i vacc0x01234567 = _mm_add_epi16(vxi0x01234567, vxi1x01234567);
178       __m128i vacc1x01234567 = _mm_add_epi16(vxi2x01234567, vxi3x01234567);
179 
180       vacc0x01234567 = _mm_add_epi16(vacc0x01234567, vxi4x01234567);
181       vacc1x01234567 = _mm_add_epi16(vacc1x01234567, vxi5x01234567);
182       vacc0x01234567 = _mm_add_epi16(vacc0x01234567, vxi6x01234567);
183 
184       // Add up all accumulators to vacc0x01234567
185       vacc0x01234567 = _mm_add_epi16(vacc0x01234567, vacc1x01234567);
186 
187       const __m128i vacc0123 = _mm_add_epi32(vbias, _mm_cvtepi16_epi32(vacc0x01234567));
188       const __m128i vacc4567 = _mm_add_epi32(vbias, _mm_unpackhi_epi16(vacc0x01234567, _mm_cmpgt_epi16(_mm_setzero_si128(), vacc0x01234567)));
189 
190       const __m128i vabsacc0123 = _mm_abs_epi32(vacc0123);
191       const __m128i vabsacc4567 = _mm_abs_epi32(vacc4567);
192 
193       const __m128i vabsacc13 = _mm_shuffle_epi32(vabsacc0123, _MM_SHUFFLE(3, 3, 1, 1));
194       const __m128i vabsacc57 = _mm_shuffle_epi32(vabsacc4567, _MM_SHUFFLE(3, 3, 1, 1));
195 
196       const __m128i vabsprod02 = _mm_mul_epu32(vabsacc0123, vmultiplier);
197       const __m128i vabsprod13 = _mm_mul_epu32(vabsacc13, vmultiplier);
198       const __m128i vabsprod46 = _mm_mul_epu32(vabsacc4567, vmultiplier);
199       const __m128i vabsprod57 = _mm_mul_epu32(vabsacc57, vmultiplier);
200 
201       const __m128i vabsout02 = _mm_srl_epi64(_mm_add_epi64(vabsprod02, vrounding), vshift);
202       const __m128i vabsout13 = _mm_srl_epi64(_mm_add_epi64(vabsprod13, vrounding), vshift);
203       const __m128i vabsout46 = _mm_srl_epi64(_mm_add_epi64(vabsprod46, vrounding), vshift);
204       const __m128i vabsout57 = _mm_srl_epi64(_mm_add_epi64(vabsprod57, vrounding), vshift);
205 
206       const __m128i vabsout0123 = _mm_blend_epi16(vabsout02, _mm_shuffle_epi32(vabsout13, _MM_SHUFFLE(2, 2, 0, 0)), 0xCC);
207       const __m128i vabsout4567 = _mm_blend_epi16(vabsout46, _mm_shuffle_epi32(vabsout57, _MM_SHUFFLE(2, 2, 0, 0)), 0xCC);
208 
209       const __m128i vout0123 = _mm_sign_epi32(vabsout0123, vacc0123);
210       const __m128i vout4567 = _mm_sign_epi32(vabsout4567, vacc4567);
211 
212       const __m128i voutput_zero_point = _mm_load_si128((const __m128i*) params->sse2.output_zero_point);
213       __m128i vout01234567 = _mm_adds_epi16(_mm_packs_epi32(vout0123, vout4567), voutput_zero_point);
214 
215       const __m128i voutput_min = _mm_load_si128((const __m128i*) params->sse2.output_min);
216       const __m128i voutput_max = _mm_load_si128((const __m128i*) params->sse2.output_max);
217       vout01234567 = _mm_min_epi16(_mm_max_epi16(vout01234567, voutput_min), voutput_max);
218 
219       __m128i vout0123456701234567 = _mm_packs_epi16(vout01234567, vout01234567);
220 
221       if XNN_LIKELY(channels >= 8) {
222         _mm_storel_epi64((__m128i*) output, vout0123456701234567);
223         output += 8;
224         channels -= 8;
225       } else {
226         if (channels & 4) {
227           *((uint32_t*) output) = (uint32_t) _mm_cvtsi128_si32(vout0123456701234567);
228           vout0123456701234567 = _mm_srli_epi64(vout0123456701234567, 32);
229           output += 4;
230         }
231         if (channels & 2) {
232           *((uint16_t*) output) = (uint16_t) _mm_extract_epi16(vout0123456701234567, 0);
233           vout0123456701234567 = _mm_srli_epi32(vout0123456701234567, 16);
234           output += 2;
235         }
236         if (channels & 1) {
237           *output = (int8_t) _mm_extract_epi8(vout0123456701234567, 0);
238           output += 1;
239         }
240         channels = 0;
241       }
242     } while (channels != 0);
243   }
244 }
245