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