blob: cc53eaf407f2816a34ceec33491f1bb2c08c2326 [file] [log] [blame]
Marat Dukhancdbe9a32021-07-01 23:52:04 -07001// Auto-generated file. Do not edit!
2// Template: src/qs8-igemm/MRx4c2-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/igemm.h>
15#include <xnnpack/math.h>
16
17
18void xnn_qu8_igemm_minmax_gemmlowp_ukernel_4x4c2__sse41_ld64(
19 size_t mr,
20 size_t nc,
21 size_t kc,
22 size_t ks,
23 const uint8_t** restrict a,
24 const void* restrict w,
25 uint8_t* restrict c,
26 size_t cm_stride,
27 size_t cn_stride,
28 size_t a_offset,
29 const uint8_t* zero,
30 const union xnn_qu8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN XNN_DISABLE_MSAN
31{
32 assert(mr != 0);
33 assert(mr <= 4);
34 assert(nc != 0);
35 assert(kc != 0);
36 assert(ks != 0);
37 assert(ks % (4 * sizeof(void*)) == 0);
38 assert(a_offset % sizeof(uint8_t) == 0);
39 assert(a != NULL);
40 assert(w != NULL);
41 assert(c != NULL);
42
43 kc = round_up_po2(kc, 2);
44 uint8_t* c0 = c;
45 uint8_t* c1 = (uint8_t*) ((uintptr_t) c0 + cm_stride);
46 if XNN_UNPREDICTABLE(mr < 2) {
47 c1 = c0;
48 }
49 uint8_t* c2 = (uint8_t*) ((uintptr_t) c1 + cm_stride);
50 if XNN_UNPREDICTABLE(mr <= 2) {
51 c2 = c1;
52 }
53 uint8_t* c3 = (uint8_t*) ((uintptr_t) c2 + cm_stride);
54 if XNN_UNPREDICTABLE(mr != 4) {
55 c3 = c2;
56 }
57
58 do {
59 __m128i vacc0x0123 = _mm_loadu_si128((const __m128i*) w);
60 __m128i vacc1x0123 = vacc0x0123;
61 __m128i vacc2x0123 = vacc0x0123;
62 __m128i vacc3x0123 = vacc0x0123;
63 w = (const void*) ((const int32_t*) w + 4);
64
65 size_t p = ks;
66 do {
67 const uint8_t* restrict a0 = a[0];
68 if XNN_UNPREDICTABLE(a0 != zero) {
69 a0 = (const uint8_t*) ((uintptr_t) a0 + a_offset);
70 }
71 const uint8_t* restrict a1 = a[1];
72 if XNN_UNPREDICTABLE(a1 != zero) {
73 a1 = (const uint8_t*) ((uintptr_t) a1 + a_offset);
74 }
75 const uint8_t* restrict a2 = a[2];
76 if XNN_UNPREDICTABLE(a2 != zero) {
77 a2 = (const uint8_t*) ((uintptr_t) a2 + a_offset);
78 }
79 const uint8_t* restrict a3 = a[3];
80 if XNN_UNPREDICTABLE(a3 != zero) {
81 a3 = (const uint8_t*) ((uintptr_t) a3 + a_offset);
82 }
83 a += 4;
84
85 size_t k = kc;
86 const __m128i vb_zero_point = _mm_load_si128((const __m128i*) params->gemmlowp_sse2.kernel_zero_point);
87 while (k >= 8 * sizeof(uint8_t)) {
88 const __m128i va0 = _mm_loadl_epi64((const __m128i*) a0);
89 const __m128i vxa0 = _mm_cvtepu8_epi16(va0);
90 a0 += 8;
91 const __m128i va1 = _mm_loadl_epi64((const __m128i*) a1);
92 const __m128i vxa1 = _mm_cvtepu8_epi16(va1);
93 a1 += 8;
94 const __m128i va2 = _mm_loadl_epi64((const __m128i*) a2);
95 const __m128i vxa2 = _mm_cvtepu8_epi16(va2);
96 a2 += 8;
97 const __m128i va3 = _mm_loadl_epi64((const __m128i*) a3);
98 const __m128i vxa3 = _mm_cvtepu8_epi16(va3);
99 a3 += 8;
100
101 const __m128i vb0 = _mm_loadl_epi64((const __m128i*) w);
102 const __m128i vxb0 = _mm_sub_epi16(_mm_cvtepu8_epi16(vb0), vb_zero_point);
103
104 vacc0x0123 = _mm_add_epi32(vacc0x0123,
105 _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
106 vacc1x0123 = _mm_add_epi32(vacc1x0123,
107 _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
108 vacc2x0123 = _mm_add_epi32(vacc2x0123,
109 _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
110 vacc3x0123 = _mm_add_epi32(vacc3x0123,
111 _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
112 const __m128i vb1 = _mm_loadl_epi64((const __m128i*) ((const uint8_t*) w + 8));
113 const __m128i vxb1 = _mm_sub_epi16(_mm_cvtepu8_epi16(vb1), vb_zero_point);
114
115 vacc0x0123 = _mm_add_epi32(vacc0x0123,
116 _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
117 vacc1x0123 = _mm_add_epi32(vacc1x0123,
118 _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
119 vacc2x0123 = _mm_add_epi32(vacc2x0123,
120 _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
121 vacc3x0123 = _mm_add_epi32(vacc3x0123,
122 _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
123 const __m128i vb2 = _mm_loadl_epi64((const __m128i*) ((const uint8_t*) w + 16));
124 const __m128i vxb2 = _mm_sub_epi16(_mm_cvtepu8_epi16(vb2), vb_zero_point);
125
126 vacc0x0123 = _mm_add_epi32(vacc0x0123,
127 _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
128 vacc1x0123 = _mm_add_epi32(vacc1x0123,
129 _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
130 vacc2x0123 = _mm_add_epi32(vacc2x0123,
131 _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
132 vacc3x0123 = _mm_add_epi32(vacc3x0123,
133 _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
134 const __m128i vb3 = _mm_loadl_epi64((const __m128i*) ((const uint8_t*) w + 24));
135 const __m128i vxb3 = _mm_sub_epi16(_mm_cvtepu8_epi16(vb3), vb_zero_point);
136
137 vacc0x0123 = _mm_add_epi32(vacc0x0123,
138 _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(3, 3, 3, 3)), vxb3));
139 vacc1x0123 = _mm_add_epi32(vacc1x0123,
140 _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(3, 3, 3, 3)), vxb3));
141 vacc2x0123 = _mm_add_epi32(vacc2x0123,
142 _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(3, 3, 3, 3)), vxb3));
143 vacc3x0123 = _mm_add_epi32(vacc3x0123,
144 _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(3, 3, 3, 3)), vxb3));
145
146 w = (const void*) ((const uint8_t*) w + 32);
147 k -= 8 * sizeof(uint8_t);
148 }
149 if (k != 0) {
150 const __m128i va0 = _mm_loadl_epi64((const __m128i*) a0);
151 const __m128i vxa0 = _mm_cvtepu8_epi16(va0);
152 a0 = (const uint8_t*) ((uintptr_t) a0 + k);
153 const __m128i va1 = _mm_loadl_epi64((const __m128i*) a1);
154 const __m128i vxa1 = _mm_cvtepu8_epi16(va1);
155 a1 = (const uint8_t*) ((uintptr_t) a1 + k);
156 const __m128i va2 = _mm_loadl_epi64((const __m128i*) a2);
157 const __m128i vxa2 = _mm_cvtepu8_epi16(va2);
158 a2 = (const uint8_t*) ((uintptr_t) a2 + k);
159 const __m128i va3 = _mm_loadl_epi64((const __m128i*) a3);
160 const __m128i vxa3 = _mm_cvtepu8_epi16(va3);
161 a3 = (const uint8_t*) ((uintptr_t) a3 + k);
162
163 const __m128i vb0 = _mm_loadl_epi64((const __m128i*) w);
164 w = (const void*) ((const uint8_t*) w + 8);
165 const __m128i vxb0 = _mm_sub_epi16(_mm_cvtepu8_epi16(vb0), vb_zero_point);
166
167 vacc0x0123 = _mm_add_epi32(vacc0x0123,
168 _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
169 vacc1x0123 = _mm_add_epi32(vacc1x0123,
170 _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
171 vacc2x0123 = _mm_add_epi32(vacc2x0123,
172 _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
173 vacc3x0123 = _mm_add_epi32(vacc3x0123,
174 _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
175
176 if (k > 2 * sizeof(uint8_t)) {
177 const __m128i vb1 = _mm_loadl_epi64((const __m128i*) w);
178 w = (const void*) ((const uint8_t*) w + 8);
179 const __m128i vxb1 = _mm_sub_epi16(_mm_cvtepu8_epi16(vb1), vb_zero_point);
180
181 vacc0x0123 = _mm_add_epi32(vacc0x0123,
182 _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
183 vacc1x0123 = _mm_add_epi32(vacc1x0123,
184 _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
185 vacc2x0123 = _mm_add_epi32(vacc2x0123,
186 _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
187 vacc3x0123 = _mm_add_epi32(vacc3x0123,
188 _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
189
190 if (k > 4 * sizeof(uint8_t)) {
191 const __m128i vb2 = _mm_loadl_epi64((const __m128i*) w);
192 w = (const void*) ((const uint8_t*) w + 8);
193 const __m128i vxb2 = _mm_sub_epi16(_mm_cvtepu8_epi16(vb2), vb_zero_point);
194
195 vacc0x0123 = _mm_add_epi32(vacc0x0123,
196 _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
197 vacc1x0123 = _mm_add_epi32(vacc1x0123,
198 _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
199 vacc2x0123 = _mm_add_epi32(vacc2x0123,
200 _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
201 vacc3x0123 = _mm_add_epi32(vacc3x0123,
202 _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
203 }
204 }
205 }
206 p -= 4 * sizeof(void*);
207 } while (p != 0);
208
209 const __m128i vmultiplier = _mm_load_si128((const __m128i*) params->gemmlowp_sse2.multiplier);
210 const __m128i vrounding = _mm_load_si128((const __m128i*) params->gemmlowp_sse2.rounding);
211
212 const __m128i vacc0x1133 = _mm_shuffle_epi32(vacc0x0123, _MM_SHUFFLE(3, 3, 1, 1));
213 const __m128i vacc1x1133 = _mm_shuffle_epi32(vacc1x0123, _MM_SHUFFLE(3, 3, 1, 1));
214 const __m128i vacc2x1133 = _mm_shuffle_epi32(vacc2x0123, _MM_SHUFFLE(3, 3, 1, 1));
215 const __m128i vacc3x1133 = _mm_shuffle_epi32(vacc3x0123, _MM_SHUFFLE(3, 3, 1, 1));
216
217 const __m128i vprod0x02 = _mm_add_epi64(_mm_mul_epi32(vacc0x0123, vmultiplier), vrounding);
218 const __m128i vprod1x02 = _mm_add_epi64(_mm_mul_epi32(vacc1x0123, vmultiplier), vrounding);
219 const __m128i vprod2x02 = _mm_add_epi64(_mm_mul_epi32(vacc2x0123, vmultiplier), vrounding);
220 const __m128i vprod3x02 = _mm_add_epi64(_mm_mul_epi32(vacc3x0123, vmultiplier), vrounding);
221
222 const __m128i vprod0x13 = _mm_add_epi64(_mm_mul_epi32(vacc0x1133, vmultiplier), vrounding);
223 const __m128i vprod1x13 = _mm_add_epi64(_mm_mul_epi32(vacc1x1133, vmultiplier), vrounding);
224 const __m128i vprod2x13 = _mm_add_epi64(_mm_mul_epi32(vacc2x1133, vmultiplier), vrounding);
225 const __m128i vprod3x13 = _mm_add_epi64(_mm_mul_epi32(vacc3x1133, vmultiplier), vrounding);
226
227 const __m128i vq31prod0x02 = _mm_srli_epi64(vprod0x02, 31);
228 const __m128i vq31prod0x13 = _mm_add_epi64(vprod0x13, vprod0x13);
229 const __m128i vq31prod1x02 = _mm_srli_epi64(vprod1x02, 31);
230 const __m128i vq31prod1x13 = _mm_add_epi64(vprod1x13, vprod1x13);
231 const __m128i vq31prod2x02 = _mm_srli_epi64(vprod2x02, 31);
232 const __m128i vq31prod2x13 = _mm_add_epi64(vprod2x13, vprod2x13);
233 const __m128i vq31prod3x02 = _mm_srli_epi64(vprod3x02, 31);
234 const __m128i vq31prod3x13 = _mm_add_epi64(vprod3x13, vprod3x13);
235
236 const __m128i vq31prod0x0123 = _mm_blend_epi16(vq31prod0x02, vq31prod0x13, 0xCC);
237 const __m128i vq31prod1x0123 = _mm_blend_epi16(vq31prod1x02, vq31prod1x13, 0xCC);
238 const __m128i vq31prod2x0123 = _mm_blend_epi16(vq31prod2x02, vq31prod2x13, 0xCC);
239 const __m128i vq31prod3x0123 = _mm_blend_epi16(vq31prod3x02, vq31prod3x13, 0xCC);
240
241 const __m128i vremainder_mask = _mm_load_si128((const __m128i*) params->gemmlowp_sse2.remainder_mask);
242 const __m128i vrem0x0123 =
243 _mm_add_epi32(_mm_and_si128(vq31prod0x0123, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vq31prod0x0123));
244 const __m128i vrem1x0123 =
245 _mm_add_epi32(_mm_and_si128(vq31prod1x0123, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vq31prod1x0123));
246 const __m128i vrem2x0123 =
247 _mm_add_epi32(_mm_and_si128(vq31prod2x0123, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vq31prod2x0123));
248 const __m128i vrem3x0123 =
249 _mm_add_epi32(_mm_and_si128(vq31prod3x0123, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vq31prod3x0123));
250
251 const __m128i vremainder_threshold = _mm_load_si128((const __m128i*) params->gemmlowp_sse2.remainder_threshold);
252 const __m128i vshift = _mm_loadl_epi64((const __m128i*) params->gemmlowp_sse2.shift);
253 vacc0x0123 =
254 _mm_sub_epi32(_mm_sra_epi32(vq31prod0x0123, vshift), _mm_cmpgt_epi32(vrem0x0123, vremainder_threshold));
255 vacc1x0123 =
256 _mm_sub_epi32(_mm_sra_epi32(vq31prod1x0123, vshift), _mm_cmpgt_epi32(vrem1x0123, vremainder_threshold));
257 vacc2x0123 =
258 _mm_sub_epi32(_mm_sra_epi32(vq31prod2x0123, vshift), _mm_cmpgt_epi32(vrem2x0123, vremainder_threshold));
259 vacc3x0123 =
260 _mm_sub_epi32(_mm_sra_epi32(vq31prod3x0123, vshift), _mm_cmpgt_epi32(vrem3x0123, vremainder_threshold));
261
262 const __m128i voutput_zero_point = _mm_load_si128((const __m128i*) params->gemmlowp_sse2.output_zero_point);
263 __m128i vacc01x0123 = _mm_adds_epi16(_mm_packs_epi32(vacc0x0123, vacc1x0123), voutput_zero_point);
264 __m128i vacc23x0123 = _mm_adds_epi16(_mm_packs_epi32(vacc2x0123, vacc3x0123), voutput_zero_point);
265
266 __m128i vout = _mm_packus_epi16(vacc01x0123, vacc23x0123);
267
268 vout = _mm_max_epu8(vout, _mm_load_si128((const __m128i*) params->gemmlowp_sse2.output_min));
269 vout = _mm_min_epu8(vout, _mm_load_si128((const __m128i*) params->gemmlowp_sse2.output_max));
270
271 if (nc >= 4) {
272 *((uint32_t*) c3) = (uint32_t) _mm_extract_epi32(vout, 3);
273 c3 = (uint8_t*) ((uintptr_t) c3 + cn_stride);
274 *((uint32_t*) c2) = (uint32_t) _mm_extract_epi32(vout, 2);
275 c2 = (uint8_t*) ((uintptr_t) c2 + cn_stride);
276 *((uint32_t*) c1) = (uint32_t) _mm_extract_epi32(vout, 1);
277 c1 = (uint8_t*) ((uintptr_t) c1 + cn_stride);
278 *((uint32_t*) c0) = (uint32_t) _mm_cvtsi128_si32(vout);
279 c0 = (uint8_t*) ((uintptr_t) c0 + cn_stride);
280
281 a = (const uint8_t**restrict) ((uintptr_t) a - ks);
282
283 nc -= 4;
284 } else {
285 if (nc & 2) {
286 *((uint16_t*) c3) = (uint16_t) _mm_extract_epi16(vout, 6);
287 c3 += 2;
288 *((uint16_t*) c2) = (uint16_t) _mm_extract_epi16(vout, 4);
289 c2 += 2;
290 *((uint16_t*) c1) = (uint16_t) _mm_extract_epi16(vout, 2);
291 c1 += 2;
292 *((uint16_t*) c0) = (uint16_t) _mm_extract_epi16(vout, 0);
293 c0 += 2;
294 vout = _mm_srli_epi32(vout, 16);
295 }
296 if (nc & 1) {
297 *c3 = (uint8_t) _mm_extract_epi8(vout, 12);
298 *c2 = (uint8_t) _mm_extract_epi8(vout, 8);
299 *c1 = (uint8_t) _mm_extract_epi8(vout, 4);
300 *c0 = (uint8_t) _mm_extract_epi8(vout, 0);
301 }
302
303 nc = 0;
304 }
305 } while (nc != 0);
306}