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_xw_minmax_ukernel_4x4c2__xop(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_xw_params params[restrict XNN_MIN_ELEMENTS (1)])23 void xnn_qs8_gemm_xw_minmax_ukernel_4x4c2__xop(
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_xw_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 vxb0 = _mm_load_si128((const __m128i*) w);
89 
90       vacc0x0123 = _mm_maddd_epi16(
91         _mm_shuffle_epi32(vxa0, _MM_SHUFFLE(0, 0, 0, 0)), vxb0, vacc0x0123);
92       vacc1x0123 = _mm_maddd_epi16(
93         _mm_shuffle_epi32(vxa1, _MM_SHUFFLE(0, 0, 0, 0)), vxb0, vacc1x0123);
94       vacc2x0123 = _mm_maddd_epi16(
95         _mm_shuffle_epi32(vxa2, _MM_SHUFFLE(0, 0, 0, 0)), vxb0, vacc2x0123);
96       vacc3x0123 = _mm_maddd_epi16(
97         _mm_shuffle_epi32(vxa3, _MM_SHUFFLE(0, 0, 0, 0)), vxb0, vacc3x0123);
98       const __m128i vxb1 = _mm_load_si128((const __m128i*) ((uintptr_t) w + 8 * sizeof(int16_t)));
99 
100       vacc0x0123 = _mm_maddd_epi16(
101         _mm_shuffle_epi32(vxa0, _MM_SHUFFLE(1, 1, 1, 1)), vxb1, vacc0x0123);
102       vacc1x0123 = _mm_maddd_epi16(
103         _mm_shuffle_epi32(vxa1, _MM_SHUFFLE(1, 1, 1, 1)), vxb1, vacc1x0123);
104       vacc2x0123 = _mm_maddd_epi16(
105         _mm_shuffle_epi32(vxa2, _MM_SHUFFLE(1, 1, 1, 1)), vxb1, vacc2x0123);
106       vacc3x0123 = _mm_maddd_epi16(
107         _mm_shuffle_epi32(vxa3, _MM_SHUFFLE(1, 1, 1, 1)), vxb1, vacc3x0123);
108       const __m128i vxb2 = _mm_load_si128((const __m128i*) ((uintptr_t) w + 16 * sizeof(int16_t)));
109 
110       vacc0x0123 = _mm_maddd_epi16(
111         _mm_shuffle_epi32(vxa0, _MM_SHUFFLE(2, 2, 2, 2)), vxb2, vacc0x0123);
112       vacc1x0123 = _mm_maddd_epi16(
113         _mm_shuffle_epi32(vxa1, _MM_SHUFFLE(2, 2, 2, 2)), vxb2, vacc1x0123);
114       vacc2x0123 = _mm_maddd_epi16(
115         _mm_shuffle_epi32(vxa2, _MM_SHUFFLE(2, 2, 2, 2)), vxb2, vacc2x0123);
116       vacc3x0123 = _mm_maddd_epi16(
117         _mm_shuffle_epi32(vxa3, _MM_SHUFFLE(2, 2, 2, 2)), vxb2, vacc3x0123);
118       const __m128i vxb3 = _mm_load_si128((const __m128i*) ((uintptr_t) w + 24 * sizeof(int16_t)));
119 
120       vacc0x0123 = _mm_maddd_epi16(
121         _mm_shuffle_epi32(vxa0, _MM_SHUFFLE(3, 3, 3, 3)), vxb3, vacc0x0123);
122       vacc1x0123 = _mm_maddd_epi16(
123         _mm_shuffle_epi32(vxa1, _MM_SHUFFLE(3, 3, 3, 3)), vxb3, vacc1x0123);
124       vacc2x0123 = _mm_maddd_epi16(
125         _mm_shuffle_epi32(vxa2, _MM_SHUFFLE(3, 3, 3, 3)), vxb3, vacc2x0123);
126       vacc3x0123 = _mm_maddd_epi16(
127         _mm_shuffle_epi32(vxa3, _MM_SHUFFLE(3, 3, 3, 3)), vxb3, vacc3x0123);
128 
129       w = (const void*) ((uintptr_t) w + 32 * sizeof(int16_t));
130       k -= 8 * sizeof(int8_t);
131     }
132     if (k != 0) {
133       const __m128i va0 = _mm_loadl_epi64((const __m128i*) a0);
134       const __m128i vxa0 = _mm_cvtepi8_epi16(va0);
135       a0 = (const int8_t*) ((uintptr_t) a0 + k);
136       const __m128i va1 = _mm_loadl_epi64((const __m128i*) a1);
137       const __m128i vxa1 = _mm_cvtepi8_epi16(va1);
138       a1 = (const int8_t*) ((uintptr_t) a1 + k);
139       const __m128i va2 = _mm_loadl_epi64((const __m128i*) a2);
140       const __m128i vxa2 = _mm_cvtepi8_epi16(va2);
141       a2 = (const int8_t*) ((uintptr_t) a2 + k);
142       const __m128i va3 = _mm_loadl_epi64((const __m128i*) a3);
143       const __m128i vxa3 = _mm_cvtepi8_epi16(va3);
144       a3 = (const int8_t*) ((uintptr_t) a3 + k);
145 
146       const __m128i vxb0 = _mm_load_si128((const __m128i*) w);
147       w = (const void*) ((uintptr_t) w + 8 * sizeof(int16_t));
148 
149       vacc0x0123 = _mm_maddd_epi16(
150         _mm_shuffle_epi32(vxa0, _MM_SHUFFLE(0, 0, 0, 0)), vxb0, vacc0x0123);
151       vacc1x0123 = _mm_maddd_epi16(
152         _mm_shuffle_epi32(vxa1, _MM_SHUFFLE(0, 0, 0, 0)), vxb0, vacc1x0123);
153       vacc2x0123 = _mm_maddd_epi16(
154         _mm_shuffle_epi32(vxa2, _MM_SHUFFLE(0, 0, 0, 0)), vxb0, vacc2x0123);
155       vacc3x0123 = _mm_maddd_epi16(
156         _mm_shuffle_epi32(vxa3, _MM_SHUFFLE(0, 0, 0, 0)), vxb0, vacc3x0123);
157 
158       if (k > 2 * sizeof(int8_t)) {
159         const __m128i vxb1 = _mm_load_si128((const __m128i*) w);
160         w = (const void*) ((uintptr_t) w + 8 * sizeof(int16_t));
161 
162         vacc0x0123 = _mm_maddd_epi16(
163           _mm_shuffle_epi32(vxa0, _MM_SHUFFLE(1, 1, 1, 1)), vxb1, vacc0x0123);
164         vacc1x0123 = _mm_maddd_epi16(
165           _mm_shuffle_epi32(vxa1, _MM_SHUFFLE(1, 1, 1, 1)), vxb1, vacc1x0123);
166         vacc2x0123 = _mm_maddd_epi16(
167           _mm_shuffle_epi32(vxa2, _MM_SHUFFLE(1, 1, 1, 1)), vxb1, vacc2x0123);
168         vacc3x0123 = _mm_maddd_epi16(
169           _mm_shuffle_epi32(vxa3, _MM_SHUFFLE(1, 1, 1, 1)), vxb1, vacc3x0123);
170 
171         if (k > 4 * sizeof(int8_t)) {
172           const __m128i vxb2 = _mm_load_si128((const __m128i*) w);
173           w = (const void*) ((uintptr_t) w + 8 * sizeof(int16_t));
174 
175           vacc0x0123 = _mm_maddd_epi16(
176             _mm_shuffle_epi32(vxa0, _MM_SHUFFLE(2, 2, 2, 2)), vxb2, vacc0x0123);
177           vacc1x0123 = _mm_maddd_epi16(
178             _mm_shuffle_epi32(vxa1, _MM_SHUFFLE(2, 2, 2, 2)), vxb2, vacc1x0123);
179           vacc2x0123 = _mm_maddd_epi16(
180             _mm_shuffle_epi32(vxa2, _MM_SHUFFLE(2, 2, 2, 2)), vxb2, vacc2x0123);
181           vacc3x0123 = _mm_maddd_epi16(
182             _mm_shuffle_epi32(vxa3, _MM_SHUFFLE(2, 2, 2, 2)), vxb2, vacc3x0123);
183         }
184       }
185     }
186 
187     const __m128i vmultiplier = _mm_load_si128((const __m128i*) params->sse2.multiplier);
188     const __m128i vrounding = _mm_load_si128((const __m128i*) params->sse2.rounding);
189 
190     const __m128i vacc0x1133 = _mm_shuffle_epi32(vacc0x0123, _MM_SHUFFLE(3, 3, 1, 1));
191     const __m128i vacc1x1133 = _mm_shuffle_epi32(vacc1x0123, _MM_SHUFFLE(3, 3, 1, 1));
192     const __m128i vacc2x1133 = _mm_shuffle_epi32(vacc2x0123, _MM_SHUFFLE(3, 3, 1, 1));
193     const __m128i vacc3x1133 = _mm_shuffle_epi32(vacc3x0123, _MM_SHUFFLE(3, 3, 1, 1));
194 
195     const __m128i vprod0x02 = _mm_add_epi64(_mm_mul_epi32(vacc0x0123, vmultiplier), vrounding);
196     const __m128i vprod1x02 = _mm_add_epi64(_mm_mul_epi32(vacc1x0123, vmultiplier), vrounding);
197     const __m128i vprod2x02 = _mm_add_epi64(_mm_mul_epi32(vacc2x0123, vmultiplier), vrounding);
198     const __m128i vprod3x02 = _mm_add_epi64(_mm_mul_epi32(vacc3x0123, vmultiplier), vrounding);
199 
200     const __m128i vprod0x13 = _mm_add_epi64(_mm_mul_epi32(vacc0x1133, vmultiplier), vrounding);
201     const __m128i vprod1x13 = _mm_add_epi64(_mm_mul_epi32(vacc1x1133, vmultiplier), vrounding);
202     const __m128i vprod2x13 = _mm_add_epi64(_mm_mul_epi32(vacc2x1133, vmultiplier), vrounding);
203     const __m128i vprod3x13 = _mm_add_epi64(_mm_mul_epi32(vacc3x1133, vmultiplier), vrounding);
204 
205     const __m128i vq31prod0x02 = _mm_srli_epi64(vprod0x02, 31);
206     const __m128i vq31prod0x13 = _mm_add_epi64(vprod0x13, vprod0x13);
207     const __m128i vq31prod1x02 = _mm_srli_epi64(vprod1x02, 31);
208     const __m128i vq31prod1x13 = _mm_add_epi64(vprod1x13, vprod1x13);
209     const __m128i vq31prod2x02 = _mm_srli_epi64(vprod2x02, 31);
210     const __m128i vq31prod2x13 = _mm_add_epi64(vprod2x13, vprod2x13);
211     const __m128i vq31prod3x02 = _mm_srli_epi64(vprod3x02, 31);
212     const __m128i vq31prod3x13 = _mm_add_epi64(vprod3x13, vprod3x13);
213 
214     const __m128i vq31prod0x0123 = _mm_blend_epi16(vq31prod0x02, vq31prod0x13, 0xCC);
215     const __m128i vq31prod1x0123 = _mm_blend_epi16(vq31prod1x02, vq31prod1x13, 0xCC);
216     const __m128i vq31prod2x0123 = _mm_blend_epi16(vq31prod2x02, vq31prod2x13, 0xCC);
217     const __m128i vq31prod3x0123 = _mm_blend_epi16(vq31prod3x02, vq31prod3x13, 0xCC);
218 
219     const __m128i vremainder_mask = _mm_load_si128((const __m128i*) params->sse2.remainder_mask);
220     const __m128i vrem0x0123 =
221       _mm_add_epi32(_mm_and_si128(vq31prod0x0123, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vq31prod0x0123));
222     const __m128i vrem1x0123 =
223       _mm_add_epi32(_mm_and_si128(vq31prod1x0123, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vq31prod1x0123));
224     const __m128i vrem2x0123 =
225       _mm_add_epi32(_mm_and_si128(vq31prod2x0123, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vq31prod2x0123));
226     const __m128i vrem3x0123 =
227       _mm_add_epi32(_mm_and_si128(vq31prod3x0123, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vq31prod3x0123));
228 
229     const __m128i vremainder_threshold = _mm_load_si128((const __m128i*) params->sse2.remainder_threshold);
230     const __m128i vshift = _mm_load_si128((const __m128i*) params->sse2.shift);
231     vacc0x0123 =
232       _mm_sub_epi32(_mm_sra_epi32(vq31prod0x0123, vshift), _mm_cmpgt_epi32(vrem0x0123, vremainder_threshold));
233     vacc1x0123 =
234       _mm_sub_epi32(_mm_sra_epi32(vq31prod1x0123, vshift), _mm_cmpgt_epi32(vrem1x0123, vremainder_threshold));
235     vacc2x0123 =
236       _mm_sub_epi32(_mm_sra_epi32(vq31prod2x0123, vshift), _mm_cmpgt_epi32(vrem2x0123, vremainder_threshold));
237     vacc3x0123 =
238       _mm_sub_epi32(_mm_sra_epi32(vq31prod3x0123, vshift), _mm_cmpgt_epi32(vrem3x0123, vremainder_threshold));
239 
240     const __m128i voutput_zero_point = _mm_load_si128((const __m128i*) params->sse2.output_zero_point);
241     __m128i vacc01x0123 = _mm_adds_epi16(_mm_packs_epi32(vacc0x0123, vacc1x0123), voutput_zero_point);
242     __m128i vacc23x0123 = _mm_adds_epi16(_mm_packs_epi32(vacc2x0123, vacc3x0123), voutput_zero_point);
243 
244     const __m128i voutput_min = _mm_load_si128((const __m128i*) params->sse2.output_min);
245     const __m128i voutput_max = _mm_load_si128((const __m128i*) params->sse2.output_max);
246     vacc01x0123 = _mm_min_epi16(_mm_max_epi16(vacc01x0123, voutput_min), voutput_max);
247     vacc23x0123 = _mm_min_epi16(_mm_max_epi16(vacc23x0123, voutput_min), voutput_max);
248 
249     __m128i vout = _mm_packs_epi16(vacc01x0123, vacc23x0123);
250 
251     if (nc >= 4) {
252       *((uint32_t*) c0) = (uint32_t) _mm_cvtsi128_si32(vout);
253       *((uint32_t*) c1) = (uint32_t) _mm_extract_epi32(vout, 1);
254       *((uint32_t*) c2) = (uint32_t) _mm_extract_epi32(vout, 2);
255       *((uint32_t*) c3) = (uint32_t) _mm_extract_epi32(vout, 3);
256 
257       c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
258       c1 = (int8_t*) ((uintptr_t) c1 + cn_stride);
259       c2 = (int8_t*) ((uintptr_t) c2 + cn_stride);
260       c3 = (int8_t*) ((uintptr_t) c3 + cn_stride);
261 
262       a0 = (const int8_t*) ((uintptr_t) a0 - kc);
263       a1 = (const int8_t*) ((uintptr_t) a1 - kc);
264       a2 = (const int8_t*) ((uintptr_t) a2 - kc);
265       a3 = (const int8_t*) ((uintptr_t) a3 - kc);
266 
267       nc -= 4;
268     } else {
269       if (nc & 2) {
270         *((uint16_t*) c0) = (uint16_t) _mm_extract_epi16(vout, 0);
271         c0 += 2;
272         *((uint16_t*) c1) = (uint16_t) _mm_extract_epi16(vout, 2);
273         c1 += 2;
274         *((uint16_t*) c2) = (uint16_t) _mm_extract_epi16(vout, 4);
275         c2 += 2;
276         *((uint16_t*) c3) = (uint16_t) _mm_extract_epi16(vout, 6);
277         c3 += 2;
278         vout = _mm_srli_epi32(vout, 16);
279       }
280       if (nc & 1) {
281         *((int8_t*) c0) = (int8_t) _mm_extract_epi8(vout, 0);
282         *((int8_t*) c1) = (int8_t) _mm_extract_epi8(vout, 4);
283         *((int8_t*) c2) = (int8_t) _mm_extract_epi8(vout, 8);
284         *((int8_t*) c3) = (int8_t) _mm_extract_epi8(vout, 12);
285       }
286 
287       nc = 0;
288     }
289   } while (nc != 0);
290 }
291