1 // Copyright (c) Facebook, Inc. and its affiliates.
2 // All rights reserved.
3 //
4 // Copyright 2019 Google LLC
5 //
6 // This source code is licensed under the BSD-style license found in the
7 // LICENSE file in the root directory of this source tree.
8 
9 #include <assert.h>
10 
11 #include <immintrin.h>
12 
13 #include <xnnpack/gemm.h>
14 #include <xnnpack/math.h>
15 
16 
xnn_qu8_gemm_minmax_ukernel_4x4c2__sse2(size_t mr,size_t nc,size_t kc,const uint8_t * restrict a,size_t a_stride,const void * restrict w,uint8_t * restrict c,size_t cm_stride,size_t cn_stride,const union xnn_qu8_gemm_params params[restrict XNN_MIN_ELEMENTS (1)])17 void xnn_qu8_gemm_minmax_ukernel_4x4c2__sse2(
18     size_t mr,
19     size_t nc,
20     size_t kc,
21     const uint8_t* restrict a,
22     size_t a_stride,
23     const void* restrict w,
24     uint8_t* restrict c,
25     size_t cm_stride,
26     size_t cn_stride,
27     const union xnn_qu8_gemm_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN
28 {
29   assert(mr != 0);
30   assert(mr <= 4);
31   assert(nc != 0);
32   assert(kc != 0);
33   assert(kc % sizeof(int8_t) == 0);
34   assert(a != NULL);
35   assert(w != NULL);
36   assert(c != NULL);
37 
38   kc = round_up_po2(kc, 2);
39   const uint8_t* a0 = a;
40   uint8_t* c0 = c;
41   const uint8_t* a1 = (const uint8_t*) ((uintptr_t) a0 + a_stride);
42   uint8_t* c1 = (uint8_t*) ((uintptr_t) c0 + cm_stride);
43   if XNN_UNPREDICTABLE(mr < 2) {
44     a1 = a0;
45     c1 = c0;
46   }
47   const uint8_t* a2 = (const uint8_t*) ((uintptr_t) a1 + a_stride);
48   uint8_t* c2 = (uint8_t*) ((uintptr_t) c1 + cm_stride);
49   if XNN_UNPREDICTABLE(mr <= 2) {
50     a2 = a1;
51     c2 = c1;
52   }
53   const uint8_t* a3 = (const uint8_t*) ((uintptr_t) a2 + a_stride);
54   uint8_t* c3 = (uint8_t*) ((uintptr_t) c2 + cm_stride);
55   if (mr != 4) {
56     a3 = a2;
57     c3 = c2;
58   }
59 
60   const __m128i vb_zero_point = _mm_load_si128((const __m128i*) params->sse2.kernel_zero_point);
61 
62   do {
63     __m128i vacc0x0123 = _mm_loadu_si128((const __m128i*) w);
64     __m128i vacc1x0123 = vacc0x0123;
65     __m128i vacc2x0123 = vacc0x0123;
66     __m128i vacc3x0123 = vacc0x0123;
67     w = (const void*) ((uintptr_t) w + 16);
68 
69     const __m128i vzero = _mm_setzero_si128();
70     size_t k = kc;
71     while (k >= 8 * sizeof(uint8_t)) {
72       const __m128i va0 = _mm_loadl_epi64((const __m128i*) a0);
73       const __m128i vxa0 = _mm_unpacklo_epi8(va0, vzero);
74       a0 += 8;
75       const __m128i va1 = _mm_loadl_epi64((const __m128i*) a1);
76       const __m128i vxa1 = _mm_unpacklo_epi8(va1, vzero);
77       a1 += 8;
78       const __m128i va2 = _mm_loadl_epi64((const __m128i*) a2);
79       const __m128i vxa2 = _mm_unpacklo_epi8(va2, vzero);
80       a2 += 8;
81       const __m128i va3 = _mm_loadl_epi64((const __m128i*) a3);
82       const __m128i vxa3 = _mm_unpacklo_epi8(va3, vzero);
83       a3 += 8;
84 
85       const __m128i vb0 = _mm_loadl_epi64((const __m128i*) w);
86       const __m128i vxb0 = _mm_sub_epi16(_mm_unpacklo_epi8(vb0, vzero), vb_zero_point);
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       const __m128i vb1 = _mm_loadl_epi64((const __m128i*) ((uintptr_t) w + 8));
98       const __m128i vxb1 = _mm_sub_epi16(_mm_unpacklo_epi8(vb1, vzero), vb_zero_point);
99 
100       vacc0x0123 = _mm_add_epi32(vacc0x0123,
101         _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
102       vacc1x0123 = _mm_add_epi32(vacc1x0123,
103         _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
104       vacc2x0123 = _mm_add_epi32(vacc2x0123,
105         _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
106       vacc3x0123 = _mm_add_epi32(vacc3x0123,
107         _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
108 
109       const __m128i vb2 = _mm_loadl_epi64((const __m128i*) ((uintptr_t) w + 16));
110       const __m128i vxb2 = _mm_sub_epi16(_mm_unpacklo_epi8(vb2, vzero), vb_zero_point);
111 
112       vacc0x0123 = _mm_add_epi32(vacc0x0123,
113         _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
114       vacc1x0123 = _mm_add_epi32(vacc1x0123,
115         _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
116       vacc2x0123 = _mm_add_epi32(vacc2x0123,
117         _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
118       vacc3x0123 = _mm_add_epi32(vacc3x0123,
119         _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
120 
121       const __m128i vb3 = _mm_loadl_epi64((const __m128i*) ((uintptr_t) w + 24));
122       const __m128i vxb3 = _mm_sub_epi16(_mm_unpacklo_epi8(vb3, vzero), vb_zero_point);
123       w = (const void*) ((uintptr_t) w + 32);
124 
125       vacc0x0123 = _mm_add_epi32(vacc0x0123,
126         _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(3, 3, 3, 3)), vxb3));
127       vacc1x0123 = _mm_add_epi32(vacc1x0123,
128         _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(3, 3, 3, 3)), vxb3));
129       vacc2x0123 = _mm_add_epi32(vacc2x0123,
130         _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(3, 3, 3, 3)), vxb3));
131       vacc3x0123 = _mm_add_epi32(vacc3x0123,
132         _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(3, 3, 3, 3)), vxb3));
133 
134       k -= 8 * sizeof(uint8_t);
135     }
136     if (k != 0) {
137       const __m128i va0 = _mm_loadl_epi64((const __m128i*) a0);
138       a0 = (const uint8_t*) ((uintptr_t) a0 + k);
139       const __m128i vxa0 = _mm_unpacklo_epi8(va0, vzero);
140       const __m128i va1 = _mm_loadl_epi64((const __m128i*) a1);
141       a1 = (const uint8_t*) ((uintptr_t) a1 + k);
142       const __m128i vxa1 = _mm_unpacklo_epi8(va1, vzero);
143       const __m128i va2 = _mm_loadl_epi64((const __m128i*) a2);
144       a2 = (const uint8_t*) ((uintptr_t) a2 + k);
145       const __m128i vxa2 = _mm_unpacklo_epi8(va2, vzero);
146       const __m128i va3 = _mm_loadl_epi64((const __m128i*) a3);
147       a3 = (const uint8_t*) ((uintptr_t) a3 + k);
148       const __m128i vxa3 = _mm_unpacklo_epi8(va3, vzero);
149 
150       const __m128i vb0 = _mm_loadl_epi64((const __m128i*) w);
151       w = (const void*) ((uintptr_t) w + 8);
152       const __m128i vxb0 = _mm_sub_epi16(_mm_unpacklo_epi8(vb0, vzero), vb_zero_point);
153 
154       vacc0x0123 = _mm_add_epi32(vacc0x0123,
155         _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
156       vacc1x0123 = _mm_add_epi32(vacc1x0123,
157         _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
158       vacc2x0123 = _mm_add_epi32(vacc2x0123,
159         _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
160       vacc3x0123 = _mm_add_epi32(vacc3x0123,
161         _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
162 
163       if (k > 2 * sizeof(uint8_t)) {
164         const __m128i vb1 = _mm_loadl_epi64((const __m128i*) w);
165         w = (const void*) ((uintptr_t) w + 8);
166         const __m128i vxb1 = _mm_sub_epi16(_mm_unpacklo_epi8(vb1, vzero), vb_zero_point);
167 
168         vacc0x0123 = _mm_add_epi32(vacc0x0123,
169           _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
170         vacc1x0123 = _mm_add_epi32(vacc1x0123,
171           _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
172         vacc2x0123 = _mm_add_epi32(vacc2x0123,
173           _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
174         vacc3x0123 = _mm_add_epi32(vacc3x0123,
175           _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
176 
177         if (k > 4 * sizeof(uint8_t)) {
178           const __m128i vb2 = _mm_loadl_epi64((const __m128i*) w);
179           w = (const void*) ((uintptr_t) w + 8);
180           const __m128i vxb2 = _mm_sub_epi16(_mm_unpacklo_epi8(vb2, vzero), vb_zero_point);
181 
182           vacc0x0123 = _mm_add_epi32(vacc0x0123,
183             _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
184           vacc1x0123 = _mm_add_epi32(vacc1x0123,
185             _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
186           vacc2x0123 = _mm_add_epi32(vacc2x0123,
187             _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
188           vacc3x0123 = _mm_add_epi32(vacc3x0123,
189             _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
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 vnmask0x0123 = _mm_cmpgt_epi32(_mm_setzero_si128(), vacc0x0123);
198     const __m128i vnmask1x0123 = _mm_cmpgt_epi32(_mm_setzero_si128(), vacc1x0123);
199     const __m128i vnmask2x0123 = _mm_cmpgt_epi32(_mm_setzero_si128(), vacc2x0123);
200     const __m128i vnmask3x0123 = _mm_cmpgt_epi32(_mm_setzero_si128(), vacc3x0123);
201 
202     const __m128i vabsacc0x0123 = _mm_sub_epi32(_mm_xor_si128(vacc0x0123, vnmask0x0123), vnmask0x0123);
203     const __m128i vabsacc1x0123 = _mm_sub_epi32(_mm_xor_si128(vacc1x0123, vnmask1x0123), vnmask1x0123);
204     const __m128i vabsacc2x0123 = _mm_sub_epi32(_mm_xor_si128(vacc2x0123, vnmask2x0123), vnmask2x0123);
205     const __m128i vabsacc3x0123 = _mm_sub_epi32(_mm_xor_si128(vacc3x0123, vnmask3x0123), vnmask3x0123);
206 
207     const __m128i vabsacc0x1032 = _mm_shuffle_epi32(vabsacc0x0123, _MM_SHUFFLE(2, 3, 0, 1));
208     const __m128i vabsacc1x1032 = _mm_shuffle_epi32(vabsacc1x0123, _MM_SHUFFLE(2, 3, 0, 1));
209     const __m128i vabsacc2x1032 = _mm_shuffle_epi32(vabsacc2x0123, _MM_SHUFFLE(2, 3, 0, 1));
210     const __m128i vabsacc3x1032 = _mm_shuffle_epi32(vabsacc3x0123, _MM_SHUFFLE(2, 3, 0, 1));
211 
212     const __m128i vabsprod0x02 = _mm_mul_epu32(vabsacc0x0123, vmultiplier);
213     const __m128i vabsprod1x02 = _mm_mul_epu32(vabsacc1x0123, vmultiplier);
214     const __m128i vabsprod2x02 = _mm_mul_epu32(vabsacc2x0123, vmultiplier);
215     const __m128i vabsprod3x02 = _mm_mul_epu32(vabsacc3x0123, vmultiplier);
216 
217     const __m128i vnmask0x02 = _mm_shuffle_epi32(vnmask0x0123, _MM_SHUFFLE(2, 2, 0, 0));
218     const __m128i vnmask1x02 = _mm_shuffle_epi32(vnmask1x0123, _MM_SHUFFLE(2, 2, 0, 0));
219     const __m128i vnmask2x02 = _mm_shuffle_epi32(vnmask2x0123, _MM_SHUFFLE(2, 2, 0, 0));
220     const __m128i vnmask3x02 = _mm_shuffle_epi32(vnmask3x0123, _MM_SHUFFLE(2, 2, 0, 0));
221 
222     const __m128i vprod0x02 = _mm_sub_epi64(_mm_xor_si128(vabsprod0x02, vnmask0x02), vnmask0x02);
223     const __m128i vprod1x02 = _mm_sub_epi64(_mm_xor_si128(vabsprod1x02, vnmask1x02), vnmask1x02);
224     const __m128i vprod2x02 = _mm_sub_epi64(_mm_xor_si128(vabsprod2x02, vnmask2x02), vnmask2x02);
225     const __m128i vprod3x02 = _mm_sub_epi64(_mm_xor_si128(vabsprod3x02, vnmask3x02), vnmask3x02);
226 
227     const __m128i vq31prod0x02 = _mm_srli_epi64(_mm_add_epi64(vprod0x02, vrounding), 31);
228     const __m128i vq31prod1x02 = _mm_srli_epi64(_mm_add_epi64(vprod1x02, vrounding), 31);
229     const __m128i vq31prod2x02 = _mm_srli_epi64(_mm_add_epi64(vprod2x02, vrounding), 31);
230     const __m128i vq31prod3x02 = _mm_srli_epi64(_mm_add_epi64(vprod3x02, vrounding), 31);
231 
232     const __m128i vabsprod0x13 = _mm_mul_epu32(vabsacc0x1032, vmultiplier);
233     const __m128i vabsprod1x13 = _mm_mul_epu32(vabsacc1x1032, vmultiplier);
234     const __m128i vabsprod2x13 = _mm_mul_epu32(vabsacc2x1032, vmultiplier);
235     const __m128i vabsprod3x13 = _mm_mul_epu32(vabsacc3x1032, vmultiplier);
236 
237     const __m128i vnmask0x13 = _mm_shuffle_epi32(vnmask0x0123, _MM_SHUFFLE(3, 3, 1, 1));
238     const __m128i vnmask1x13 = _mm_shuffle_epi32(vnmask1x0123, _MM_SHUFFLE(3, 3, 1, 1));
239     const __m128i vnmask2x13 = _mm_shuffle_epi32(vnmask2x0123, _MM_SHUFFLE(3, 3, 1, 1));
240     const __m128i vnmask3x13 = _mm_shuffle_epi32(vnmask3x0123, _MM_SHUFFLE(3, 3, 1, 1));
241 
242     const __m128i vprod0x13 = _mm_sub_epi64(_mm_xor_si128(vabsprod0x13, vnmask0x13), vnmask0x13);
243     const __m128i vprod1x13 = _mm_sub_epi64(_mm_xor_si128(vabsprod1x13, vnmask1x13), vnmask1x13);
244     const __m128i vprod2x13 = _mm_sub_epi64(_mm_xor_si128(vabsprod2x13, vnmask2x13), vnmask2x13);
245     const __m128i vprod3x13 = _mm_sub_epi64(_mm_xor_si128(vabsprod3x13, vnmask3x13), vnmask3x13);
246 
247     const __m128i vq31prod0x13 = _mm_srli_epi64(_mm_add_epi64(vprod0x13, vrounding), 31);
248     const __m128i vq31prod1x13 = _mm_srli_epi64(_mm_add_epi64(vprod1x13, vrounding), 31);
249     const __m128i vq31prod2x13 = _mm_srli_epi64(_mm_add_epi64(vprod2x13, vrounding), 31);
250     const __m128i vq31prod3x13 = _mm_srli_epi64(_mm_add_epi64(vprod3x13, vrounding), 31);
251 
252     const __m128i vq31prod0x0213 = _mm_castps_si128(_mm_shuffle_ps(
253         _mm_castsi128_ps(vq31prod0x02), _mm_castsi128_ps(vq31prod0x13), _MM_SHUFFLE(2, 0, 2, 0)));
254     const __m128i vq31prod1x0213 = _mm_castps_si128(_mm_shuffle_ps(
255         _mm_castsi128_ps(vq31prod1x02), _mm_castsi128_ps(vq31prod1x13), _MM_SHUFFLE(2, 0, 2, 0)));
256     const __m128i vq31prod2x0213 = _mm_castps_si128(_mm_shuffle_ps(
257         _mm_castsi128_ps(vq31prod2x02), _mm_castsi128_ps(vq31prod2x13), _MM_SHUFFLE(2, 0, 2, 0)));
258     const __m128i vq31prod3x0213 = _mm_castps_si128(_mm_shuffle_ps(
259         _mm_castsi128_ps(vq31prod3x02), _mm_castsi128_ps(vq31prod3x13), _MM_SHUFFLE(2, 0, 2, 0)));
260 
261     const __m128i vq31prod0x0123 = _mm_shuffle_epi32(vq31prod0x0213, _MM_SHUFFLE(3, 1, 2, 0));
262     const __m128i vq31prod1x0123 = _mm_shuffle_epi32(vq31prod1x0213, _MM_SHUFFLE(3, 1, 2, 0));
263     const __m128i vq31prod2x0123 = _mm_shuffle_epi32(vq31prod2x0213, _MM_SHUFFLE(3, 1, 2, 0));
264     const __m128i vq31prod3x0123 = _mm_shuffle_epi32(vq31prod3x0213, _MM_SHUFFLE(3, 1, 2, 0));
265 
266     const __m128i vremainder_mask = _mm_load_si128((const __m128i*) params->sse2.remainder_mask);
267 
268     const __m128i vrem0x0123 =
269       _mm_add_epi32(_mm_and_si128(vq31prod0x0123, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vq31prod0x0123));
270     const __m128i vrem1x0123 =
271       _mm_add_epi32(_mm_and_si128(vq31prod1x0123, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vq31prod1x0123));
272     const __m128i vrem2x0123 =
273       _mm_add_epi32(_mm_and_si128(vq31prod2x0123, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vq31prod2x0123));
274     const __m128i vrem3x0123 =
275       _mm_add_epi32(_mm_and_si128(vq31prod3x0123, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vq31prod3x0123));
276 
277     const __m128i vremainder_threshold = _mm_load_si128((const __m128i*) params->sse2.remainder_threshold);
278     const __m128i vshift = _mm_load_si128((const __m128i*) params->sse2.shift);
279 
280     vacc0x0123 =
281       _mm_sub_epi32(_mm_sra_epi32(vq31prod0x0123, vshift), _mm_cmpgt_epi32(vrem0x0123, vremainder_threshold));
282     vacc1x0123 =
283       _mm_sub_epi32(_mm_sra_epi32(vq31prod1x0123, vshift), _mm_cmpgt_epi32(vrem1x0123, vremainder_threshold));
284     vacc2x0123 =
285       _mm_sub_epi32(_mm_sra_epi32(vq31prod2x0123, vshift), _mm_cmpgt_epi32(vrem2x0123, vremainder_threshold));
286     vacc3x0123 =
287       _mm_sub_epi32(_mm_sra_epi32(vq31prod3x0123, vshift), _mm_cmpgt_epi32(vrem3x0123, vremainder_threshold));
288 
289     const __m128i voutput_zero_point = _mm_load_si128((const __m128i*) params->sse2.output_zero_point);
290     const __m128i vacc01x0123 = _mm_adds_epi16(_mm_packs_epi32(vacc0x0123, vacc1x0123), voutput_zero_point);
291     const __m128i vacc23x0123 = _mm_adds_epi16(_mm_packs_epi32(vacc2x0123, vacc3x0123), voutput_zero_point);
292     __m128i vout = _mm_packus_epi16(vacc01x0123, vacc23x0123);
293     vout = _mm_min_epu8(vout, _mm_load_si128((const __m128i*) params->sse2.output_max));
294     vout = _mm_max_epu8(vout, _mm_load_si128((const __m128i*) params->sse2.output_min));
295 
296     if (nc >= 4) {
297       *((uint32_t*) c0) = (uint32_t) _mm_cvtsi128_si32(vout);
298       *((uint32_t*) c1) = (uint32_t) _mm_cvtsi128_si32(_mm_srli_epi64(vout, 32));
299       *((uint32_t*) c2) = (uint32_t) _mm_cvtsi128_si32(_mm_unpackhi_epi32(vout, vout));
300       *((uint32_t*) c3) = (uint32_t) _mm_cvtsi128_si32(_mm_srli_si128(vout, 12));
301 
302       a0 = (const uint8_t*) ((uintptr_t) a0 - kc);
303       a1 = (const uint8_t*) ((uintptr_t) a1 - kc);
304       a2 = (const uint8_t*) ((uintptr_t) a2 - kc);
305       a3 = (const uint8_t*) ((uintptr_t) a3 - kc);
306 
307       c0 = (uint8_t*) ((uintptr_t) c0 + cn_stride);
308       c1 = (uint8_t*) ((uintptr_t) c1 + cn_stride);
309       c2 = (uint8_t*) ((uintptr_t) c2 + cn_stride);
310       c3 = (uint8_t*) ((uintptr_t) c3 + cn_stride);
311 
312       nc -= 4;
313     } else {
314       if (nc & 2) {
315         *((uint16_t*) c0) = (uint16_t) _mm_extract_epi16(vout, 0);
316         c0 += 2;
317         *((uint16_t*) c1) = (uint16_t) _mm_extract_epi16(vout, 2);
318         c1 += 2;
319         *((uint16_t*) c2) = (uint16_t) _mm_extract_epi16(vout, 4);
320         c2 += 2;
321         *((uint16_t*) c3) = (uint16_t) _mm_extract_epi16(vout, 6);
322         c3 += 2;
323         vout = _mm_srli_epi32(vout, 16);
324       }
325       if (nc & 1) {
326         *((uint8_t*) c0) = (uint8_t) _mm_cvtsi128_si32(vout);
327         *((uint8_t*) c1) = (uint8_t) _mm_extract_epi16(vout, 2);
328         *((uint8_t*) c2) = (uint8_t) _mm_extract_epi16(vout, 4);
329         *((uint8_t*) c3) = (uint8_t) _mm_extract_epi16(vout, 6);
330       }
331 
332       nc = 0;
333     }
334   } while (nc != 0);
335 }
336