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