1 // Copyright (c) Facebook, Inc. and its affiliates.
2 // All rights reserved.
3 //
4 // Copyright 2019 Google LLC
5 //
6 // This source code is licensed under the BSD-style license found in the
7 // LICENSE file in the root directory of this source tree.
8
9 #include <assert.h>
10
11 #include <emmintrin.h>
12
13 #include <xnnpack/avgpool.h>
14
15
xnn_qu8_avgpool_minmax_ukernel_9x__sse2_c8(size_t output_pixels,size_t kernel_elements,size_t channels,const uint8_t ** input,size_t input_offset,const uint8_t * zero,uint8_t * output,size_t input_increment,size_t output_increment,const union xnn_qu8_avgpool_params params[restrict XNN_MIN_ELEMENTS (1)])16 void xnn_qu8_avgpool_minmax_ukernel_9x__sse2_c8(
17 size_t output_pixels,
18 size_t kernel_elements,
19 size_t channels,
20 const uint8_t** input,
21 size_t input_offset,
22 const uint8_t* zero,
23 uint8_t* output,
24 size_t input_increment,
25 size_t output_increment,
26 const union xnn_qu8_avgpool_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN
27 {
28 assert(output_pixels != 0);
29 assert(kernel_elements != 0);
30 assert(kernel_elements <= 9);
31 assert(channels != 0);
32
33 const __m128i vbias = _mm_load_si128((const __m128i*) ¶ms->sse2.bias);
34 const __m128i vzero = _mm_setzero_si128();
35 const __m128i vmultiplier = _mm_load_si128((const __m128i*) params->sse2.multiplier);
36 const __m128i vrounding = _mm_load_si128((const __m128i*) params->sse2.rounding);
37 const __m128i vright_shift = _mm_loadl_epi64((const __m128i*) params->sse2.right_shift);
38
39 do {
40 const uint8_t* i0 = input[0];
41 assert(i0 != NULL);
42 const uint8_t* i1 = input[1];
43 const uint8_t* i2 = input[2];
44 const uint8_t* i3 = input[3];
45 const uint8_t* i4 = input[4];
46 const uint8_t* i5 = input[5];
47 const uint8_t* i6 = input[6];
48 const uint8_t* i7 = input[7];
49 const uint8_t* i8 = input[8];
50 input = (const uint8_t**) ((uintptr_t) input + input_increment);
51 if (kernel_elements < 2) {
52 i1 = zero;
53 }
54 assert(i1 != NULL);
55 if (kernel_elements <= 2) {
56 i2 = zero;
57 }
58 assert(i2 != NULL);
59 if (kernel_elements < 4) {
60 i3 = zero;
61 }
62 assert(i3 != NULL);
63 if (kernel_elements <= 4) {
64 i4 = zero;
65 }
66 assert(i4 != NULL);
67 if (kernel_elements < 6) {
68 i5 = zero;
69 }
70 assert(i5 != NULL);
71 if (kernel_elements <= 6) {
72 i6 = zero;
73 }
74 assert(i6 != NULL);
75 if (kernel_elements < 8) {
76 i7 = zero;
77 }
78 assert(i7 != NULL);
79 if (kernel_elements <= 8) {
80 i8 = zero;
81 }
82 assert(i8 != NULL);
83 if XNN_UNPREDICTABLE(i0 != zero) {
84 i0 = (const uint8_t*) ((uintptr_t) i0 + input_offset);
85 }
86 if XNN_UNPREDICTABLE(i1 != zero) {
87 i1 = (const uint8_t*) ((uintptr_t) i1 + input_offset);
88 }
89 if XNN_UNPREDICTABLE(i2 != zero) {
90 i2 = (const uint8_t*) ((uintptr_t) i2 + input_offset);
91 }
92 if XNN_UNPREDICTABLE(i3 != zero) {
93 i3 = (const uint8_t*) ((uintptr_t) i3 + input_offset);
94 }
95 if XNN_UNPREDICTABLE(i4 != zero) {
96 i4 = (const uint8_t*) ((uintptr_t) i4 + input_offset);
97 }
98 if XNN_UNPREDICTABLE(i5 != zero) {
99 i5 = (const uint8_t*) ((uintptr_t) i5 + input_offset);
100 }
101 if XNN_UNPREDICTABLE(i6 != zero) {
102 i6 = (const uint8_t*) ((uintptr_t) i6 + input_offset);
103 }
104 if XNN_UNPREDICTABLE(i7 != zero) {
105 i7 = (const uint8_t*) ((uintptr_t) i7 + input_offset);
106 }
107 if XNN_UNPREDICTABLE(i8 != zero) {
108 i8 = (const uint8_t*) ((uintptr_t) i8 + input_offset);
109 }
110
111 size_t c = channels;
112 while (c >= 8) {
113 const __m128i vi0 = _mm_loadl_epi64((const __m128i*) i0); i0 += 8;
114 const __m128i vi1 = _mm_loadl_epi64((const __m128i*) i1); i1 += 8;
115 const __m128i vi2 = _mm_loadl_epi64((const __m128i*) i2); i2 += 8;
116 const __m128i vi3 = _mm_loadl_epi64((const __m128i*) i3); i3 += 8;
117 const __m128i vi4 = _mm_loadl_epi64((const __m128i*) i4); i4 += 8;
118 const __m128i vi5 = _mm_loadl_epi64((const __m128i*) i5); i5 += 8;
119 const __m128i vi6 = _mm_loadl_epi64((const __m128i*) i6); i6 += 8;
120 const __m128i vi7 = _mm_loadl_epi64((const __m128i*) i7); i7 += 8;
121 const __m128i vi8 = _mm_loadl_epi64((const __m128i*) i8); i8 += 8;
122
123 const __m128i vxi0 = _mm_unpacklo_epi8(vi0, vzero);
124 const __m128i vxi1 = _mm_unpacklo_epi8(vi1, vzero);
125 const __m128i vxi2 = _mm_unpacklo_epi8(vi2, vzero);
126 const __m128i vxi3 = _mm_unpacklo_epi8(vi3, vzero);
127 const __m128i vxi4 = _mm_unpacklo_epi8(vi4, vzero);
128 const __m128i vxi5 = _mm_unpacklo_epi8(vi5, vzero);
129 const __m128i vxi6 = _mm_unpacklo_epi8(vi6, vzero);
130 const __m128i vxi7 = _mm_unpacklo_epi8(vi7, vzero);
131 const __m128i vxi8 = _mm_unpacklo_epi8(vi8, vzero);
132
133 const __m128i vsum018 = _mm_add_epi16(_mm_add_epi16(vxi0, vxi1), vxi8);
134 const __m128i vsum23 = _mm_add_epi16(vxi2, vxi3);
135 const __m128i vsum45 = _mm_add_epi16(vxi4, vxi5);
136 const __m128i vsum67 = _mm_add_epi16(vxi6, vxi7);
137
138 const __m128i vsum2345 = _mm_add_epi16(vsum23, vsum45);
139 const __m128i vsum01678 = _mm_add_epi16(vsum018, vsum67);
140 const __m128i vsum = _mm_add_epi16(vsum2345, vsum01678);
141
142 const __m128i vacc_lo = _mm_add_epi32(vbias, _mm_unpacklo_epi16(vsum, vzero));
143 const __m128i vacc_hi = _mm_add_epi32(vbias, _mm_unpackhi_epi16(vsum, vzero));
144
145 const __m128i vneg_mask_lo = _mm_cmpgt_epi32(_mm_setzero_si128(), vacc_lo);
146 const __m128i vneg_mask_hi = _mm_cmpgt_epi32(_mm_setzero_si128(), vacc_hi);
147
148 const __m128i vabs_lo0123 = _mm_sub_epi32(_mm_xor_si128(vacc_lo, vneg_mask_lo), vneg_mask_lo);
149 const __m128i vabs_hi0123 = _mm_sub_epi32(_mm_xor_si128(vacc_hi, vneg_mask_hi), vneg_mask_hi);
150
151 const __m128i vabs_lo1032 = _mm_shuffle_epi32(vabs_lo0123, _MM_SHUFFLE(2, 3, 0, 1));
152 const __m128i vabs_hi1032 = _mm_shuffle_epi32(vabs_hi0123, _MM_SHUFFLE(2, 3, 0, 1));
153
154 const __m128i vabsmul_lo02 = _mm_mul_epu32(vabs_lo0123, vmultiplier);
155 const __m128i vabsmul_hi02 = _mm_mul_epu32(vabs_hi0123, vmultiplier);
156
157 const __m128i vabsmul_lo13 = _mm_mul_epu32(vabs_lo1032, vmultiplier);
158 const __m128i vabsmul_hi13 = _mm_mul_epu32(vabs_hi1032, vmultiplier);
159
160 const __m128i vabs_scaled_lo02 = _mm_srl_epi64(_mm_add_epi64(vabsmul_lo02, vrounding), vright_shift);
161 const __m128i vabs_scaled_lo13 = _mm_srl_epi64(_mm_add_epi64(vabsmul_lo13, vrounding), vright_shift);
162 const __m128i vabs_scaled_hi02 = _mm_srl_epi64(_mm_add_epi64(vabsmul_hi02, vrounding), vright_shift);
163 const __m128i vabs_scaled_hi13 = _mm_srl_epi64(_mm_add_epi64(vabsmul_hi13, vrounding), vright_shift);
164
165 const __m128i vabs_scaled_lo0213 = _mm_castps_si128(
166 _mm_shuffle_ps(_mm_castsi128_ps(vabs_scaled_lo02), _mm_castsi128_ps(vabs_scaled_lo13), _MM_SHUFFLE(2, 0, 2, 0)));
167 const __m128i vabs_scaled_hi0213 = _mm_castps_si128(
168 _mm_shuffle_ps(_mm_castsi128_ps(vabs_scaled_hi02), _mm_castsi128_ps(vabs_scaled_hi13), _MM_SHUFFLE(2, 0, 2, 0)));
169
170 const __m128i vabs_scaled_lo = _mm_shuffle_epi32(vabs_scaled_lo0213, _MM_SHUFFLE(3, 1, 2, 0));
171 const __m128i vabs_scaled_hi = _mm_shuffle_epi32(vabs_scaled_hi0213, _MM_SHUFFLE(3, 1, 2, 0));
172
173 const __m128i vscaled_lo = _mm_sub_epi32(_mm_xor_si128(vabs_scaled_lo, vneg_mask_lo), vneg_mask_lo);
174 const __m128i vscaled_hi = _mm_sub_epi32(_mm_xor_si128(vabs_scaled_hi, vneg_mask_hi), vneg_mask_hi);
175
176 __m128i vout = _mm_packs_epi32(vscaled_lo, vscaled_hi);
177 vout = _mm_adds_epi16(vout, _mm_load_si128((const __m128i*) ¶ms->sse2.output_zero_point));
178 vout = _mm_packus_epi16(vout, vout);
179 vout = _mm_min_epu8(vout, _mm_load_si128((const __m128i*) ¶ms->sse2.output_max));
180 vout = _mm_max_epu8(vout, _mm_load_si128((const __m128i*) ¶ms->sse2.output_min));
181
182 _mm_storel_epi64((__m128i*) output, vout);
183 output += 8;
184
185 c -= 8;
186 }
187 if (c != 0) {
188 const __m128i vi0 = _mm_loadl_epi64((const __m128i*) i0);
189 const __m128i vi1 = _mm_loadl_epi64((const __m128i*) i1);
190 const __m128i vi2 = _mm_loadl_epi64((const __m128i*) i2);
191 const __m128i vi3 = _mm_loadl_epi64((const __m128i*) i3);
192 const __m128i vi4 = _mm_loadl_epi64((const __m128i*) i4);
193 const __m128i vi5 = _mm_loadl_epi64((const __m128i*) i5);
194 const __m128i vi6 = _mm_loadl_epi64((const __m128i*) i6);
195 const __m128i vi7 = _mm_loadl_epi64((const __m128i*) i7);
196 const __m128i vi8 = _mm_loadl_epi64((const __m128i*) i8);
197
198 const __m128i vxi0 = _mm_unpacklo_epi8(vi0, vzero);
199 const __m128i vxi1 = _mm_unpacklo_epi8(vi1, vzero);
200 const __m128i vxi2 = _mm_unpacklo_epi8(vi2, vzero);
201 const __m128i vxi3 = _mm_unpacklo_epi8(vi3, vzero);
202 const __m128i vxi4 = _mm_unpacklo_epi8(vi4, vzero);
203 const __m128i vxi5 = _mm_unpacklo_epi8(vi5, vzero);
204 const __m128i vxi6 = _mm_unpacklo_epi8(vi6, vzero);
205 const __m128i vxi7 = _mm_unpacklo_epi8(vi7, vzero);
206 const __m128i vxi8 = _mm_unpacklo_epi8(vi8, vzero);
207
208 const __m128i vsum018 = _mm_add_epi16(_mm_add_epi16(vxi0, vxi1), vxi8);
209 const __m128i vsum23 = _mm_add_epi16(vxi2, vxi3);
210 const __m128i vsum45 = _mm_add_epi16(vxi4, vxi5);
211 const __m128i vsum67 = _mm_add_epi16(vxi6, vxi7);
212
213 const __m128i vsum2345 = _mm_add_epi16(vsum23, vsum45);
214 const __m128i vsum01678 = _mm_add_epi16(vsum018, vsum67);
215 const __m128i vsum = _mm_add_epi16(vsum2345, vsum01678);
216
217 const __m128i vacc_lo = _mm_add_epi32(vbias, _mm_unpacklo_epi16(vsum, vzero));
218 const __m128i vacc_hi = _mm_add_epi32(vbias, _mm_unpackhi_epi16(vsum, vzero));
219
220 const __m128i vneg_mask_lo = _mm_cmpgt_epi32(_mm_setzero_si128(), vacc_lo);
221 const __m128i vneg_mask_hi = _mm_cmpgt_epi32(_mm_setzero_si128(), vacc_hi);
222
223 const __m128i vabs_lo0123 = _mm_sub_epi32(_mm_xor_si128(vacc_lo, vneg_mask_lo), vneg_mask_lo);
224 const __m128i vabs_hi0123 = _mm_sub_epi32(_mm_xor_si128(vacc_hi, vneg_mask_hi), vneg_mask_hi);
225
226 const __m128i vabs_lo1032 = _mm_shuffle_epi32(vabs_lo0123, _MM_SHUFFLE(2, 3, 0, 1));
227 const __m128i vabs_hi1032 = _mm_shuffle_epi32(vabs_hi0123, _MM_SHUFFLE(2, 3, 0, 1));
228
229 const __m128i vabsmul_lo02 = _mm_mul_epu32(vabs_lo0123, vmultiplier);
230 const __m128i vabsmul_hi02 = _mm_mul_epu32(vabs_hi0123, vmultiplier);
231
232 const __m128i vabsmul_lo13 = _mm_mul_epu32(vabs_lo1032, vmultiplier);
233 const __m128i vabsmul_hi13 = _mm_mul_epu32(vabs_hi1032, vmultiplier);
234
235 const __m128i vabs_scaled_lo02 = _mm_srl_epi64(_mm_add_epi64(vabsmul_lo02, vrounding), vright_shift);
236 const __m128i vabs_scaled_lo13 = _mm_srl_epi64(_mm_add_epi64(vabsmul_lo13, vrounding), vright_shift);
237 const __m128i vabs_scaled_hi02 = _mm_srl_epi64(_mm_add_epi64(vabsmul_hi02, vrounding), vright_shift);
238 const __m128i vabs_scaled_hi13 = _mm_srl_epi64(_mm_add_epi64(vabsmul_hi13, vrounding), vright_shift);
239
240 const __m128i vabs_scaled_lo0213 = _mm_castps_si128(
241 _mm_shuffle_ps(_mm_castsi128_ps(vabs_scaled_lo02), _mm_castsi128_ps(vabs_scaled_lo13), _MM_SHUFFLE(2, 0, 2, 0)));
242 const __m128i vabs_scaled_hi0213 = _mm_castps_si128(
243 _mm_shuffle_ps(_mm_castsi128_ps(vabs_scaled_hi02), _mm_castsi128_ps(vabs_scaled_hi13), _MM_SHUFFLE(2, 0, 2, 0)));
244
245 const __m128i vabs_scaled_lo = _mm_shuffle_epi32(vabs_scaled_lo0213, _MM_SHUFFLE(3, 1, 2, 0));
246 const __m128i vabs_scaled_hi = _mm_shuffle_epi32(vabs_scaled_hi0213, _MM_SHUFFLE(3, 1, 2, 0));
247
248 const __m128i vscaled_lo = _mm_sub_epi32(_mm_xor_si128(vabs_scaled_lo, vneg_mask_lo), vneg_mask_lo);
249 const __m128i vscaled_hi = _mm_sub_epi32(_mm_xor_si128(vabs_scaled_hi, vneg_mask_hi), vneg_mask_hi);
250
251 __m128i vout = _mm_packs_epi32(vscaled_lo, vscaled_hi);
252 vout = _mm_adds_epi16(vout, _mm_load_si128((const __m128i*) ¶ms->sse2.output_zero_point));
253 vout = _mm_packus_epi16(vout, vout);
254 vout = _mm_min_epu8(vout, _mm_load_si128((const __m128i*) ¶ms->sse2.output_max));
255 vout = _mm_max_epu8(vout, _mm_load_si128((const __m128i*) ¶ms->sse2.output_min));
256
257 if (c & 4) {
258 *((uint32_t*) output) = (uint32_t) _mm_cvtsi128_si32(vout);
259 output += 4;
260 vout = _mm_srli_epi64(vout, 32);
261 }
262 if (c & 2) {
263 *((uint16_t*) output) = (uint16_t) _mm_extract_epi16(vout, 0);
264 output += 2;
265 vout = _mm_srli_epi32(vout, 16);
266 }
267 if (c & 1) {
268 *((uint8_t*) output) = (uint8_t) _mm_cvtsi128_si32(vout);
269 output += 1;
270 }
271 }
272 output = (uint8_t*) ((uintptr_t) output + output_increment);
273 } while (--output_pixels != 0);
274 }
275