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_q8_avgpool_ukernel_up9__sse2(size_t n,size_t ks,size_t kc,const uint8_t ** input,const uint8_t * zero,uint8_t * output,size_t input_increment,size_t output_increment,const union xnn_q8_avgpool_params params[restrict static1])16 void xnn_q8_avgpool_ukernel_up9__sse2(
17 size_t n,
18 size_t ks,
19 size_t kc,
20 const uint8_t** input,
21 const uint8_t* zero,
22 uint8_t* output,
23 size_t input_increment,
24 size_t output_increment,
25 const union xnn_q8_avgpool_params params[restrict static 1])
26 {
27 assert(n != 0);
28 assert(ks != 0);
29 assert(ks <= 9);
30 assert(kc != 0);
31
32 const __m128i vbias = _mm_load_si128((const __m128i*) ¶ms->sse2.bias);
33 const __m128i vzero = _mm_setzero_si128();
34 const __m128i vmultiplier = _mm_load_si128((const __m128i*) params->sse2.multiplier);
35 const __m128i vrounding = _mm_load_si128((const __m128i*) params->sse2.rounding);
36 const __m128i vright_shift = _mm_loadl_epi64((const __m128i*) params->sse2.right_shift);
37
38 do {
39 const uint8_t* i0 = input[0];
40 const uint8_t* i1 = input[1];
41 const uint8_t* i2 = input[2];
42 const uint8_t* i3 = input[3];
43 const uint8_t* i4 = input[4];
44 const uint8_t* i5 = input[5];
45 const uint8_t* i6 = input[6];
46 const uint8_t* i7 = input[7];
47 const uint8_t* i8 = input[8];
48 input = (const uint8_t**) ((uintptr_t) input + input_increment);
49 if (ks < 2) {
50 i1 = zero;
51 }
52 if (ks <= 2) {
53 i2 = zero;
54 }
55 if (ks < 4) {
56 i3 = zero;
57 }
58 if (ks <= 4) {
59 i4 = zero;
60 }
61 if (ks < 6) {
62 i5 = zero;
63 }
64 if (ks <= 6) {
65 i6 = zero;
66 }
67 if (ks < 8) {
68 i7 = zero;
69 }
70 if (ks <= 8) {
71 i8 = zero;
72 }
73
74 size_t k = kc;
75 while (k >= 8) {
76 const __m128i vi0 = _mm_loadl_epi64((const __m128i*) i0); i0 += 8;
77 const __m128i vi1 = _mm_loadl_epi64((const __m128i*) i1); i1 += 8;
78 const __m128i vi2 = _mm_loadl_epi64((const __m128i*) i2); i2 += 8;
79 const __m128i vi3 = _mm_loadl_epi64((const __m128i*) i3); i3 += 8;
80 const __m128i vi4 = _mm_loadl_epi64((const __m128i*) i4); i4 += 8;
81 const __m128i vi5 = _mm_loadl_epi64((const __m128i*) i5); i5 += 8;
82 const __m128i vi6 = _mm_loadl_epi64((const __m128i*) i6); i6 += 8;
83 const __m128i vi7 = _mm_loadl_epi64((const __m128i*) i7); i7 += 8;
84 const __m128i vi8 = _mm_loadl_epi64((const __m128i*) i8); i8 += 8;
85
86 const __m128i vxi0 = _mm_unpacklo_epi8(vi0, vzero);
87 const __m128i vxi1 = _mm_unpacklo_epi8(vi1, vzero);
88 const __m128i vxi2 = _mm_unpacklo_epi8(vi2, vzero);
89 const __m128i vxi3 = _mm_unpacklo_epi8(vi3, vzero);
90 const __m128i vxi4 = _mm_unpacklo_epi8(vi4, vzero);
91 const __m128i vxi5 = _mm_unpacklo_epi8(vi5, vzero);
92 const __m128i vxi6 = _mm_unpacklo_epi8(vi6, vzero);
93 const __m128i vxi7 = _mm_unpacklo_epi8(vi7, vzero);
94 const __m128i vxi8 = _mm_unpacklo_epi8(vi8, vzero);
95
96 const __m128i vsum018 = _mm_add_epi16(_mm_add_epi16(vxi0, vxi1), vxi8);
97 const __m128i vsum23 = _mm_add_epi16(vxi2, vxi3);
98 const __m128i vsum45 = _mm_add_epi16(vxi4, vxi5);
99 const __m128i vsum67 = _mm_add_epi16(vxi6, vxi7);
100
101 const __m128i vsum2345 = _mm_add_epi16(vsum23, vsum45);
102 const __m128i vsum01678 = _mm_add_epi16(vsum018, vsum67);
103 const __m128i vsum = _mm_add_epi16(vsum2345, vsum01678);
104
105 const __m128i vacc_lo = _mm_add_epi32(vbias, _mm_unpacklo_epi16(vsum, vzero));
106 const __m128i vacc_hi = _mm_add_epi32(vbias, _mm_unpackhi_epi16(vsum, vzero));
107
108 const __m128i vneg_mask_lo = _mm_cmpgt_epi32(_mm_setzero_si128(), vacc_lo);
109 const __m128i vneg_mask_hi = _mm_cmpgt_epi32(_mm_setzero_si128(), vacc_hi);
110
111 const __m128i vabs_lo0123 = _mm_sub_epi32(_mm_xor_si128(vacc_lo, vneg_mask_lo), vneg_mask_lo);
112 const __m128i vabs_hi0123 = _mm_sub_epi32(_mm_xor_si128(vacc_hi, vneg_mask_hi), vneg_mask_hi);
113
114 const __m128i vabs_lo1032 = _mm_shuffle_epi32(vabs_lo0123, _MM_SHUFFLE(2, 3, 0, 1));
115 const __m128i vabs_hi1032 = _mm_shuffle_epi32(vabs_hi0123, _MM_SHUFFLE(2, 3, 0, 1));
116
117 const __m128i vabsmul_lo02 = _mm_mul_epu32(vabs_lo0123, vmultiplier);
118 const __m128i vabsmul_hi02 = _mm_mul_epu32(vabs_hi0123, vmultiplier);
119
120 const __m128i vabsmul_lo13 = _mm_mul_epu32(vabs_lo1032, vmultiplier);
121 const __m128i vabsmul_hi13 = _mm_mul_epu32(vabs_hi1032, vmultiplier);
122
123 const __m128i vabs_scaled_lo02 = _mm_srl_epi64(_mm_add_epi64(vabsmul_lo02, vrounding), vright_shift);
124 const __m128i vabs_scaled_lo13 = _mm_srl_epi64(_mm_add_epi64(vabsmul_lo13, vrounding), vright_shift);
125 const __m128i vabs_scaled_hi02 = _mm_srl_epi64(_mm_add_epi64(vabsmul_hi02, vrounding), vright_shift);
126 const __m128i vabs_scaled_hi13 = _mm_srl_epi64(_mm_add_epi64(vabsmul_hi13, vrounding), vright_shift);
127
128 const __m128i vabs_scaled_lo0213 = _mm_castps_si128(
129 _mm_shuffle_ps(_mm_castsi128_ps(vabs_scaled_lo02), _mm_castsi128_ps(vabs_scaled_lo13), _MM_SHUFFLE(2, 0, 2, 0)));
130 const __m128i vabs_scaled_hi0213 = _mm_castps_si128(
131 _mm_shuffle_ps(_mm_castsi128_ps(vabs_scaled_hi02), _mm_castsi128_ps(vabs_scaled_hi13), _MM_SHUFFLE(2, 0, 2, 0)));
132
133 const __m128i vabs_scaled_lo = _mm_shuffle_epi32(vabs_scaled_lo0213, _MM_SHUFFLE(3, 1, 2, 0));
134 const __m128i vabs_scaled_hi = _mm_shuffle_epi32(vabs_scaled_hi0213, _MM_SHUFFLE(3, 1, 2, 0));
135
136 const __m128i vscaled_lo = _mm_sub_epi32(_mm_xor_si128(vabs_scaled_lo, vneg_mask_lo), vneg_mask_lo);
137 const __m128i vscaled_hi = _mm_sub_epi32(_mm_xor_si128(vabs_scaled_hi, vneg_mask_hi), vneg_mask_hi);
138
139 __m128i vout = _mm_packs_epi32(vscaled_lo, vscaled_hi);
140 vout = _mm_adds_epi16(vout, _mm_load_si128((const __m128i*) ¶ms->sse2.output_zero_point));
141 vout = _mm_packus_epi16(vout, vout);
142 vout = _mm_min_epu8(vout, _mm_load_si128((const __m128i*) ¶ms->sse2.output_max));
143 vout = _mm_max_epu8(vout, _mm_load_si128((const __m128i*) ¶ms->sse2.output_min));
144
145 _mm_storel_epi64((__m128i*) output, vout);
146 output += 8;
147
148 k -= 8;
149 }
150 if (k != 0) {
151 const __m128i vi0 = _mm_loadl_epi64((const __m128i*) i0);
152 const __m128i vi1 = _mm_loadl_epi64((const __m128i*) i1);
153 const __m128i vi2 = _mm_loadl_epi64((const __m128i*) i2);
154 const __m128i vi3 = _mm_loadl_epi64((const __m128i*) i3);
155 const __m128i vi4 = _mm_loadl_epi64((const __m128i*) i4);
156 const __m128i vi5 = _mm_loadl_epi64((const __m128i*) i5);
157 const __m128i vi6 = _mm_loadl_epi64((const __m128i*) i6);
158 const __m128i vi7 = _mm_loadl_epi64((const __m128i*) i7);
159 const __m128i vi8 = _mm_loadl_epi64((const __m128i*) i8);
160
161 const __m128i vxi0 = _mm_unpacklo_epi8(vi0, vzero);
162 const __m128i vxi1 = _mm_unpacklo_epi8(vi1, vzero);
163 const __m128i vxi2 = _mm_unpacklo_epi8(vi2, vzero);
164 const __m128i vxi3 = _mm_unpacklo_epi8(vi3, vzero);
165 const __m128i vxi4 = _mm_unpacklo_epi8(vi4, vzero);
166 const __m128i vxi5 = _mm_unpacklo_epi8(vi5, vzero);
167 const __m128i vxi6 = _mm_unpacklo_epi8(vi6, vzero);
168 const __m128i vxi7 = _mm_unpacklo_epi8(vi7, vzero);
169 const __m128i vxi8 = _mm_unpacklo_epi8(vi8, vzero);
170
171 const __m128i vsum018 = _mm_add_epi16(_mm_add_epi16(vxi0, vxi1), vxi8);
172 const __m128i vsum23 = _mm_add_epi16(vxi2, vxi3);
173 const __m128i vsum45 = _mm_add_epi16(vxi4, vxi5);
174 const __m128i vsum67 = _mm_add_epi16(vxi6, vxi7);
175
176 const __m128i vsum2345 = _mm_add_epi16(vsum23, vsum45);
177 const __m128i vsum01678 = _mm_add_epi16(vsum018, vsum67);
178 const __m128i vsum = _mm_add_epi16(vsum2345, vsum01678);
179
180 const __m128i vacc_lo = _mm_add_epi32(vbias, _mm_unpacklo_epi16(vsum, vzero));
181 const __m128i vacc_hi = _mm_add_epi32(vbias, _mm_unpackhi_epi16(vsum, vzero));
182
183 const __m128i vneg_mask_lo = _mm_cmpgt_epi32(_mm_setzero_si128(), vacc_lo);
184 const __m128i vneg_mask_hi = _mm_cmpgt_epi32(_mm_setzero_si128(), vacc_hi);
185
186 const __m128i vabs_lo0123 = _mm_sub_epi32(_mm_xor_si128(vacc_lo, vneg_mask_lo), vneg_mask_lo);
187 const __m128i vabs_hi0123 = _mm_sub_epi32(_mm_xor_si128(vacc_hi, vneg_mask_hi), vneg_mask_hi);
188
189 const __m128i vabs_lo1032 = _mm_shuffle_epi32(vabs_lo0123, _MM_SHUFFLE(2, 3, 0, 1));
190 const __m128i vabs_hi1032 = _mm_shuffle_epi32(vabs_hi0123, _MM_SHUFFLE(2, 3, 0, 1));
191
192 const __m128i vabsmul_lo02 = _mm_mul_epu32(vabs_lo0123, vmultiplier);
193 const __m128i vabsmul_hi02 = _mm_mul_epu32(vabs_hi0123, vmultiplier);
194
195 const __m128i vabsmul_lo13 = _mm_mul_epu32(vabs_lo1032, vmultiplier);
196 const __m128i vabsmul_hi13 = _mm_mul_epu32(vabs_hi1032, vmultiplier);
197
198 const __m128i vabs_scaled_lo02 = _mm_srl_epi64(_mm_add_epi64(vabsmul_lo02, vrounding), vright_shift);
199 const __m128i vabs_scaled_lo13 = _mm_srl_epi64(_mm_add_epi64(vabsmul_lo13, vrounding), vright_shift);
200 const __m128i vabs_scaled_hi02 = _mm_srl_epi64(_mm_add_epi64(vabsmul_hi02, vrounding), vright_shift);
201 const __m128i vabs_scaled_hi13 = _mm_srl_epi64(_mm_add_epi64(vabsmul_hi13, vrounding), vright_shift);
202
203 const __m128i vabs_scaled_lo0213 = _mm_castps_si128(
204 _mm_shuffle_ps(_mm_castsi128_ps(vabs_scaled_lo02), _mm_castsi128_ps(vabs_scaled_lo13), _MM_SHUFFLE(2, 0, 2, 0)));
205 const __m128i vabs_scaled_hi0213 = _mm_castps_si128(
206 _mm_shuffle_ps(_mm_castsi128_ps(vabs_scaled_hi02), _mm_castsi128_ps(vabs_scaled_hi13), _MM_SHUFFLE(2, 0, 2, 0)));
207
208 const __m128i vabs_scaled_lo = _mm_shuffle_epi32(vabs_scaled_lo0213, _MM_SHUFFLE(3, 1, 2, 0));
209 const __m128i vabs_scaled_hi = _mm_shuffle_epi32(vabs_scaled_hi0213, _MM_SHUFFLE(3, 1, 2, 0));
210
211 const __m128i vscaled_lo = _mm_sub_epi32(_mm_xor_si128(vabs_scaled_lo, vneg_mask_lo), vneg_mask_lo);
212 const __m128i vscaled_hi = _mm_sub_epi32(_mm_xor_si128(vabs_scaled_hi, vneg_mask_hi), vneg_mask_hi);
213
214 __m128i vout = _mm_packs_epi32(vscaled_lo, vscaled_hi);
215 vout = _mm_adds_epi16(vout, _mm_load_si128((const __m128i*) ¶ms->sse2.output_zero_point));
216 vout = _mm_packus_epi16(vout, vout);
217 vout = _mm_min_epu8(vout, _mm_load_si128((const __m128i*) ¶ms->sse2.output_max));
218 vout = _mm_max_epu8(vout, _mm_load_si128((const __m128i*) ¶ms->sse2.output_min));
219
220 if (k & 4) {
221 *((uint32_t*) output) = (uint32_t) _mm_cvtsi128_si32(vout);
222 output += 4;
223 vout = _mm_srli_epi64(vout, 32);
224 }
225 if (k & 2) {
226 *((uint16_t*) output) = (uint16_t) _mm_extract_epi16(vout, 0);
227 output += 2;
228 vout = _mm_srli_epi32(vout, 16);
229 }
230 if (k & 1) {
231 *((uint8_t*) output) = (uint8_t) _mm_cvtsi128_si32(vout);
232 output += 1;
233 }
234 }
235 output = (uint8_t*) ((uintptr_t) output + output_increment);
236 } while (--n != 0);
237 }
238