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