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 <emmintrin.h>
13
14 #include <xnnpack/gavgpool.h>
15
16
xnn_qs8_gavgpool_minmax_ukernel_7x__sse2_c8_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__sse2_c8_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 >= 8) {
61 const __m128i vi0x01234567 = _mm_loadl_epi64((const __m128i*) i0);
62 i0 += 8;
63 const __m128i vi1x01234567 = _mm_loadl_epi64((const __m128i*) i1);
64 i1 += 8;
65 const __m128i vi2x01234567 = _mm_loadl_epi64((const __m128i*) i2);
66 i2 += 8;
67 const __m128i vi3x01234567 = _mm_loadl_epi64((const __m128i*) i3);
68 i3 += 8;
69 const __m128i vi4x01234567 = _mm_loadl_epi64((const __m128i*) i4);
70 i4 += 8;
71 const __m128i vi5x01234567 = _mm_loadl_epi64((const __m128i*) i5);
72 i5 += 8;
73 const __m128i vi6x01234567 = _mm_loadl_epi64((const __m128i*) i6);
74 i6 += 8;
75
76 const __m128i vxi0x01234567 = _mm_unpacklo_epi8(vi0x01234567, _mm_cmpgt_epi8(_mm_setzero_si128(), vi0x01234567));
77 const __m128i vxi1x01234567 = _mm_unpacklo_epi8(vi1x01234567, _mm_cmpgt_epi8(_mm_setzero_si128(), vi1x01234567));
78 const __m128i vxi2x01234567 = _mm_unpacklo_epi8(vi2x01234567, _mm_cmpgt_epi8(_mm_setzero_si128(), vi2x01234567));
79 const __m128i vxi3x01234567 = _mm_unpacklo_epi8(vi3x01234567, _mm_cmpgt_epi8(_mm_setzero_si128(), vi3x01234567));
80 const __m128i vxi4x01234567 = _mm_unpacklo_epi8(vi4x01234567, _mm_cmpgt_epi8(_mm_setzero_si128(), vi4x01234567));
81 const __m128i vxi5x01234567 = _mm_unpacklo_epi8(vi5x01234567, _mm_cmpgt_epi8(_mm_setzero_si128(), vi5x01234567));
82 const __m128i vxi6x01234567 = _mm_unpacklo_epi8(vi6x01234567, _mm_cmpgt_epi8(_mm_setzero_si128(), vi6x01234567));
83
84 __m128i vacc0x01234567 = _mm_add_epi16(vxi0x01234567, vxi1x01234567);
85 __m128i vacc1x01234567 = _mm_add_epi16(vxi2x01234567, vxi3x01234567);
86
87 vacc0x01234567 = _mm_add_epi16(vacc0x01234567, vxi4x01234567);
88 vacc1x01234567 = _mm_add_epi16(vacc1x01234567, vxi5x01234567);
89 vacc0x01234567 = _mm_add_epi16(vacc0x01234567, vxi6x01234567);
90
91 // Add up all accumulators to vacc0x01234567
92 vacc0x01234567 = _mm_add_epi16(vacc0x01234567, vacc1x01234567);
93
94 const __m128i vsgnacc0x01234567 = _mm_cmpgt_epi16(_mm_setzero_si128(), vacc0x01234567);
95 const __m128i vacc0123 = _mm_add_epi32(vbias, _mm_unpacklo_epi16(vacc0x01234567, vsgnacc0x01234567));
96 const __m128i vacc4567 = _mm_add_epi32(vbias, _mm_unpackhi_epi16(vacc0x01234567, vsgnacc0x01234567));
97
98 const __m128i vsgnacc0123 = _mm_cmpgt_epi32(_mm_setzero_si128(), vacc0123);
99 const __m128i vsgnacc4567 = _mm_cmpgt_epi32(_mm_setzero_si128(), vacc4567);
100
101 const __m128i vabsacc0123 = _mm_sub_epi32(_mm_xor_si128(vacc0123, vsgnacc0123), vsgnacc0123);
102 const __m128i vabsacc4567 = _mm_sub_epi32(_mm_xor_si128(vacc4567, vsgnacc4567), vsgnacc4567);
103
104 const __m128i vabsacc13 = _mm_shuffle_epi32(vabsacc0123, _MM_SHUFFLE(3, 3, 1, 1));
105 const __m128i vabsacc57 = _mm_shuffle_epi32(vabsacc4567, _MM_SHUFFLE(3, 3, 1, 1));
106
107 const __m128i vabsprod02 = _mm_mul_epu32(vabsacc0123, vmultiplier);
108 const __m128i vabsprod13 = _mm_mul_epu32(vabsacc13, vmultiplier);
109 const __m128i vabsprod46 = _mm_mul_epu32(vabsacc4567, vmultiplier);
110 const __m128i vabsprod57 = _mm_mul_epu32(vabsacc57, vmultiplier);
111
112 const __m128i vabsout02 = _mm_srl_epi64(_mm_add_epi64(vabsprod02, vrounding), vshift);
113 const __m128i vabsout13 = _mm_srl_epi64(_mm_add_epi64(vabsprod13, vrounding), vshift);
114 const __m128i vabsout46 = _mm_srl_epi64(_mm_add_epi64(vabsprod46, vrounding), vshift);
115 const __m128i vabsout57 = _mm_srl_epi64(_mm_add_epi64(vabsprod57, vrounding), vshift);
116
117 const __m128i vabsout0213 = _mm_castps_si128(
118 _mm_shuffle_ps(_mm_castsi128_ps(vabsout02), _mm_castsi128_ps(vabsout13), _MM_SHUFFLE(2, 0, 2, 0)));
119 const __m128i vabsout4657 = _mm_castps_si128(
120 _mm_shuffle_ps(_mm_castsi128_ps(vabsout46), _mm_castsi128_ps(vabsout57), _MM_SHUFFLE(2, 0, 2, 0)));
121
122 const __m128i vabsout0123 = _mm_shuffle_epi32(vabsout0213, _MM_SHUFFLE(3, 1, 2, 0));
123 const __m128i vabsout4567 = _mm_shuffle_epi32(vabsout4657, _MM_SHUFFLE(3, 1, 2, 0));
124
125 const __m128i vout0123 = _mm_sub_epi32(_mm_xor_si128(vabsout0123, vsgnacc0123), vsgnacc0123);
126 const __m128i vout4567 = _mm_sub_epi32(_mm_xor_si128(vabsout4567, vsgnacc4567), vsgnacc4567);
127
128 const __m128i voutput_zero_point = _mm_load_si128((const __m128i*) params->sse2.output_zero_point);
129 __m128i vout01234567 = _mm_adds_epi16(_mm_packs_epi32(vout0123, vout4567), voutput_zero_point);
130
131 const __m128i voutput_min = _mm_load_si128((const __m128i*) params->sse2.output_min);
132 const __m128i voutput_max = _mm_load_si128((const __m128i*) params->sse2.output_max);
133 vout01234567 = _mm_min_epi16(_mm_max_epi16(vout01234567, voutput_min), voutput_max);
134
135 __m128i vout0123456701234567 = _mm_packs_epi16(vout01234567, vout01234567);
136
137 _mm_storel_epi64((__m128i*) output, vout0123456701234567);
138 output += 8;
139
140 channels -= 8;
141 }
142 if XNN_UNLIKELY(channels != 0) {
143 {
144 const __m128i vi0x01234567 = _mm_loadl_epi64((const __m128i*) i0);
145 i0 += 8;
146 const __m128i vi1x01234567 = _mm_loadl_epi64((const __m128i*) i1);
147 i1 += 8;
148 const __m128i vi2x01234567 = _mm_loadl_epi64((const __m128i*) i2);
149 i2 += 8;
150 const __m128i vi3x01234567 = _mm_loadl_epi64((const __m128i*) i3);
151 i3 += 8;
152 const __m128i vi4x01234567 = _mm_loadl_epi64((const __m128i*) i4);
153 i4 += 8;
154 const __m128i vi5x01234567 = _mm_loadl_epi64((const __m128i*) i5);
155 i5 += 8;
156 const __m128i vi6x01234567 = _mm_loadl_epi64((const __m128i*) i6);
157 i6 += 8;
158
159 const __m128i vxi0x01234567 = _mm_unpacklo_epi8(vi0x01234567, _mm_cmpgt_epi8(_mm_setzero_si128(), vi0x01234567));
160 const __m128i vxi1x01234567 = _mm_unpacklo_epi8(vi1x01234567, _mm_cmpgt_epi8(_mm_setzero_si128(), vi1x01234567));
161 const __m128i vxi2x01234567 = _mm_unpacklo_epi8(vi2x01234567, _mm_cmpgt_epi8(_mm_setzero_si128(), vi2x01234567));
162 const __m128i vxi3x01234567 = _mm_unpacklo_epi8(vi3x01234567, _mm_cmpgt_epi8(_mm_setzero_si128(), vi3x01234567));
163 const __m128i vxi4x01234567 = _mm_unpacklo_epi8(vi4x01234567, _mm_cmpgt_epi8(_mm_setzero_si128(), vi4x01234567));
164 const __m128i vxi5x01234567 = _mm_unpacklo_epi8(vi5x01234567, _mm_cmpgt_epi8(_mm_setzero_si128(), vi5x01234567));
165 const __m128i vxi6x01234567 = _mm_unpacklo_epi8(vi6x01234567, _mm_cmpgt_epi8(_mm_setzero_si128(), vi6x01234567));
166
167 __m128i vacc0x01234567 = _mm_add_epi16(vxi0x01234567, vxi1x01234567);
168 __m128i vacc1x01234567 = _mm_add_epi16(vxi2x01234567, vxi3x01234567);
169
170 vacc0x01234567 = _mm_add_epi16(vacc0x01234567, vxi4x01234567);
171 vacc1x01234567 = _mm_add_epi16(vacc1x01234567, vxi5x01234567);
172 vacc0x01234567 = _mm_add_epi16(vacc0x01234567, vxi6x01234567);
173
174 // Add up all accumulators to vacc0x01234567
175 vacc0x01234567 = _mm_add_epi16(vacc0x01234567, vacc1x01234567);
176
177 const __m128i vsgnacc0x01234567 = _mm_cmpgt_epi16(_mm_setzero_si128(), vacc0x01234567);
178 const __m128i vacc0123 = _mm_add_epi32(vbias, _mm_unpacklo_epi16(vacc0x01234567, vsgnacc0x01234567));
179 const __m128i vacc4567 = _mm_add_epi32(vbias, _mm_unpackhi_epi16(vacc0x01234567, vsgnacc0x01234567));
180
181 const __m128i vsgnacc0123 = _mm_cmpgt_epi32(_mm_setzero_si128(), vacc0123);
182 const __m128i vsgnacc4567 = _mm_cmpgt_epi32(_mm_setzero_si128(), vacc4567);
183
184 const __m128i vabsacc0123 = _mm_sub_epi32(_mm_xor_si128(vacc0123, vsgnacc0123), vsgnacc0123);
185 const __m128i vabsacc4567 = _mm_sub_epi32(_mm_xor_si128(vacc4567, vsgnacc4567), vsgnacc4567);
186
187 const __m128i vabsacc13 = _mm_shuffle_epi32(vabsacc0123, _MM_SHUFFLE(3, 3, 1, 1));
188 const __m128i vabsacc57 = _mm_shuffle_epi32(vabsacc4567, _MM_SHUFFLE(3, 3, 1, 1));
189
190 const __m128i vabsprod02 = _mm_mul_epu32(vabsacc0123, vmultiplier);
191 const __m128i vabsprod13 = _mm_mul_epu32(vabsacc13, vmultiplier);
192 const __m128i vabsprod46 = _mm_mul_epu32(vabsacc4567, vmultiplier);
193 const __m128i vabsprod57 = _mm_mul_epu32(vabsacc57, vmultiplier);
194
195 const __m128i vabsout02 = _mm_srl_epi64(_mm_add_epi64(vabsprod02, vrounding), vshift);
196 const __m128i vabsout13 = _mm_srl_epi64(_mm_add_epi64(vabsprod13, vrounding), vshift);
197 const __m128i vabsout46 = _mm_srl_epi64(_mm_add_epi64(vabsprod46, vrounding), vshift);
198 const __m128i vabsout57 = _mm_srl_epi64(_mm_add_epi64(vabsprod57, vrounding), vshift);
199
200 const __m128i vabsout0213 = _mm_castps_si128(
201 _mm_shuffle_ps(_mm_castsi128_ps(vabsout02), _mm_castsi128_ps(vabsout13), _MM_SHUFFLE(2, 0, 2, 0)));
202 const __m128i vabsout4657 = _mm_castps_si128(
203 _mm_shuffle_ps(_mm_castsi128_ps(vabsout46), _mm_castsi128_ps(vabsout57), _MM_SHUFFLE(2, 0, 2, 0)));
204
205 const __m128i vabsout0123 = _mm_shuffle_epi32(vabsout0213, _MM_SHUFFLE(3, 1, 2, 0));
206 const __m128i vabsout4567 = _mm_shuffle_epi32(vabsout4657, _MM_SHUFFLE(3, 1, 2, 0));
207
208 const __m128i vout0123 = _mm_sub_epi32(_mm_xor_si128(vabsout0123, vsgnacc0123), vsgnacc0123);
209 const __m128i vout4567 = _mm_sub_epi32(_mm_xor_si128(vabsout4567, vsgnacc4567), vsgnacc4567);
210
211 const __m128i voutput_zero_point = _mm_load_si128((const __m128i*) params->sse2.output_zero_point);
212 __m128i vout01234567 = _mm_adds_epi16(_mm_packs_epi32(vout0123, vout4567), voutput_zero_point);
213
214 const __m128i voutput_min = _mm_load_si128((const __m128i*) params->sse2.output_min);
215 const __m128i voutput_max = _mm_load_si128((const __m128i*) params->sse2.output_max);
216 vout01234567 = _mm_min_epi16(_mm_max_epi16(vout01234567, voutput_min), voutput_max);
217
218 __m128i vout0123456701234567 = _mm_packs_epi16(vout01234567, vout01234567);
219
220 if (channels & 4) {
221 *((uint32_t*) output) = (uint32_t) _mm_cvtsi128_si32(vout0123456701234567);
222 vout0123456701234567 = _mm_srli_epi64(vout0123456701234567, 32);
223 output += 4;
224 }
225 if (channels & 2) {
226 *((uint16_t*) output) = (uint16_t) _mm_extract_epi16(vout0123456701234567, 0);
227 vout0123456701234567 = _mm_srli_epi32(vout0123456701234567, 16);
228 output += 2;
229 }
230 if (channels & 1) {
231 *output = (int32_t) _mm_cvtsi128_si32(vout0123456701234567);
232 }
233 }
234 }
235 }
236