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