• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/qs8-igemm/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 <tmmintrin.h>
13 
14 #include <xnnpack/igemm.h>
15 #include <xnnpack/math.h>
16 
17 
xnn_qs8_igemm_minmax_ukernel_4x4c2__ssse3_ld128(size_t mr,size_t nc,size_t kc,size_t ks,const int8_t ** restrict a,const void * restrict w,int8_t * restrict c,size_t cm_stride,size_t cn_stride,size_t a_offset,const int8_t * zero,const union xnn_qs8_gemm_params params[restrict XNN_MIN_ELEMENTS (1)])18 void xnn_qs8_igemm_minmax_ukernel_4x4c2__ssse3_ld128(
19     size_t mr,
20     size_t nc,
21     size_t kc,
22     size_t ks,
23     const int8_t** restrict a,
24     const void* restrict w,
25     int8_t* restrict c,
26     size_t cm_stride,
27     size_t cn_stride,
28     size_t a_offset,
29     const int8_t* zero,
30     const union xnn_qs8_gemm_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN
31 {
32   assert(mr != 0);
33   assert(mr <= 4);
34   assert(nc != 0);
35   assert(kc != 0);
36   assert(ks != 0);
37   assert(ks % (4 * sizeof(void*)) == 0);
38   assert(a_offset % sizeof(int8_t) == 0);
39   assert(a != NULL);
40   assert(w != NULL);
41   assert(c != NULL);
42 
43   kc = round_up_po2(kc, 2);
44   int8_t* c0 = c;
45   int8_t* c1 = (int8_t*) ((uintptr_t) c0 + cm_stride);
46   if XNN_UNPREDICTABLE(mr < 2) {
47     c1 = c0;
48   }
49   int8_t* c2 = (int8_t*) ((uintptr_t) c1 + cm_stride);
50   if XNN_UNPREDICTABLE(mr <= 2) {
51     c2 = c1;
52   }
53   int8_t* c3 = (int8_t*) ((uintptr_t) c2 + cm_stride);
54   if XNN_UNPREDICTABLE(mr != 4) {
55     c3 = c2;
56   }
57 
58   do {
59     __m128i vacc0x0123 = _mm_loadu_si128((const __m128i*) w);
60     __m128i vacc1x0123 = vacc0x0123;
61     __m128i vacc2x0123 = vacc0x0123;
62     __m128i vacc3x0123 = vacc0x0123;
63     w = (const void*) ((uintptr_t) w + 4 * sizeof(int32_t));
64 
65     size_t p = ks;
66     do {
67       const int8_t* restrict a0 = a[0];
68       if XNN_UNPREDICTABLE(a0 != zero) {
69         a0 = (const int8_t*) ((uintptr_t) a0 + a_offset);
70       }
71       const int8_t* restrict a1 = a[1];
72       if XNN_UNPREDICTABLE(a1 != zero) {
73         a1 = (const int8_t*) ((uintptr_t) a1 + a_offset);
74       }
75       const int8_t* restrict a2 = a[2];
76       if XNN_UNPREDICTABLE(a2 != zero) {
77         a2 = (const int8_t*) ((uintptr_t) a2 + a_offset);
78       }
79       const int8_t* restrict a3 = a[3];
80       if XNN_UNPREDICTABLE(a3 != zero) {
81         a3 = (const int8_t*) ((uintptr_t) a3 + a_offset);
82       }
83       a += 4;
84 
85       size_t k = kc;
86       while (k >= 8 * sizeof(int8_t)) {
87         const __m128i va0 = _mm_loadl_epi64((const __m128i*) a0);
88         const __m128i vxa0 = _mm_unpacklo_epi8(va0, _mm_cmpgt_epi8(_mm_setzero_si128(), va0));
89         a0 += 8;
90         const __m128i va1 = _mm_loadl_epi64((const __m128i*) a1);
91         const __m128i vxa1 = _mm_unpacklo_epi8(va1, _mm_cmpgt_epi8(_mm_setzero_si128(), va1));
92         a1 += 8;
93         const __m128i va2 = _mm_loadl_epi64((const __m128i*) a2);
94         const __m128i vxa2 = _mm_unpacklo_epi8(va2, _mm_cmpgt_epi8(_mm_setzero_si128(), va2));
95         a2 += 8;
96         const __m128i va3 = _mm_loadl_epi64((const __m128i*) a3);
97         const __m128i vxa3 = _mm_unpacklo_epi8(va3, _mm_cmpgt_epi8(_mm_setzero_si128(), va3));
98         a3 += 8;
99 
100         const __m128i vb01 = _mm_loadu_si128((const __m128i*) w);
101         const __m128i vsb01 = _mm_cmpgt_epi8(_mm_setzero_si128(), vb01);
102         const __m128i vxb0 = _mm_unpacklo_epi8(vb01, vsb01);
103         const __m128i vxb1 = _mm_unpackhi_epi8(vb01, vsb01);
104 
105         vacc0x0123 = _mm_add_epi32(vacc0x0123,
106           _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
107         vacc1x0123 = _mm_add_epi32(vacc1x0123,
108           _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
109         vacc2x0123 = _mm_add_epi32(vacc2x0123,
110           _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
111         vacc3x0123 = _mm_add_epi32(vacc3x0123,
112           _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
113 
114         vacc0x0123 = _mm_add_epi32(vacc0x0123,
115           _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
116         vacc1x0123 = _mm_add_epi32(vacc1x0123,
117           _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
118         vacc2x0123 = _mm_add_epi32(vacc2x0123,
119           _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
120         vacc3x0123 = _mm_add_epi32(vacc3x0123,
121           _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
122         const __m128i vb23 = _mm_loadu_si128((const __m128i*) ((uintptr_t) w + 16));
123         const __m128i vsb23 = _mm_cmpgt_epi8(_mm_setzero_si128(), vb23);
124         const __m128i vxb2 = _mm_unpacklo_epi8(vb23, vsb23);
125         const __m128i vxb3 = _mm_unpackhi_epi8(vb23, vsb23);
126 
127         vacc0x0123 = _mm_add_epi32(vacc0x0123,
128           _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
129         vacc1x0123 = _mm_add_epi32(vacc1x0123,
130           _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
131         vacc2x0123 = _mm_add_epi32(vacc2x0123,
132           _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
133         vacc3x0123 = _mm_add_epi32(vacc3x0123,
134           _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
135 
136         vacc0x0123 = _mm_add_epi32(vacc0x0123,
137           _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(3, 3, 3, 3)), vxb3));
138         vacc1x0123 = _mm_add_epi32(vacc1x0123,
139           _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(3, 3, 3, 3)), vxb3));
140         vacc2x0123 = _mm_add_epi32(vacc2x0123,
141           _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(3, 3, 3, 3)), vxb3));
142         vacc3x0123 = _mm_add_epi32(vacc3x0123,
143           _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(3, 3, 3, 3)), vxb3));
144 
145         w = (const void*) ((uintptr_t) w + 32);
146         k -= 8 * sizeof(int8_t);
147       }
148       if (k != 0) {
149         const __m128i va0 = _mm_loadl_epi64((const __m128i*) a0);
150         const __m128i vxa0 = _mm_unpacklo_epi8(va0, _mm_cmpgt_epi8(_mm_setzero_si128(), va0));
151         a0 = (const int8_t*) ((uintptr_t) a0 + k);
152         const __m128i va1 = _mm_loadl_epi64((const __m128i*) a1);
153         const __m128i vxa1 = _mm_unpacklo_epi8(va1, _mm_cmpgt_epi8(_mm_setzero_si128(), va1));
154         a1 = (const int8_t*) ((uintptr_t) a1 + k);
155         const __m128i va2 = _mm_loadl_epi64((const __m128i*) a2);
156         const __m128i vxa2 = _mm_unpacklo_epi8(va2, _mm_cmpgt_epi8(_mm_setzero_si128(), va2));
157         a2 = (const int8_t*) ((uintptr_t) a2 + k);
158         const __m128i va3 = _mm_loadl_epi64((const __m128i*) a3);
159         const __m128i vxa3 = _mm_unpacklo_epi8(va3, _mm_cmpgt_epi8(_mm_setzero_si128(), va3));
160         a3 = (const int8_t*) ((uintptr_t) a3 + k);
161 
162         const __m128i vb0 = _mm_loadl_epi64((const __m128i*) w);
163         w = (const void*) ((uintptr_t) w + 8);
164         const __m128i vxb0 = _mm_unpacklo_epi8(vb0, _mm_cmpgt_epi8(_mm_setzero_si128(), vb0));
165 
166         vacc0x0123 = _mm_add_epi32(vacc0x0123,
167           _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
168         vacc1x0123 = _mm_add_epi32(vacc1x0123,
169           _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
170         vacc2x0123 = _mm_add_epi32(vacc2x0123,
171           _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
172         vacc3x0123 = _mm_add_epi32(vacc3x0123,
173           _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
174 
175         if (k > 2 * sizeof(int8_t)) {
176           const __m128i vb1 = _mm_loadl_epi64((const __m128i*) w);
177           w = (const void*) ((uintptr_t) w + 8);
178           const __m128i vxb1 = _mm_unpacklo_epi8(vb1, _mm_cmpgt_epi8(_mm_setzero_si128(), vb1));
179 
180           vacc0x0123 = _mm_add_epi32(vacc0x0123,
181             _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
182           vacc1x0123 = _mm_add_epi32(vacc1x0123,
183             _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
184           vacc2x0123 = _mm_add_epi32(vacc2x0123,
185             _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
186           vacc3x0123 = _mm_add_epi32(vacc3x0123,
187             _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
188 
189           if (k > 4 * sizeof(int8_t)) {
190             const __m128i vb2 = _mm_loadl_epi64((const __m128i*) w);
191             w = (const void*) ((uintptr_t) w + 8);
192             const __m128i vxb2 = _mm_unpacklo_epi8(vb2, _mm_cmpgt_epi8(_mm_setzero_si128(), vb2));
193 
194             vacc0x0123 = _mm_add_epi32(vacc0x0123,
195               _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
196             vacc1x0123 = _mm_add_epi32(vacc1x0123,
197               _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
198             vacc2x0123 = _mm_add_epi32(vacc2x0123,
199               _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
200             vacc3x0123 = _mm_add_epi32(vacc3x0123,
201               _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
202           }
203         }
204       }
205       p -= 4 * sizeof(void*);
206     } while (p != 0);
207 
208     const __m128i vmultiplier = _mm_load_si128((const __m128i*) params->sse2.multiplier);
209     const __m128i vrounding = _mm_load_si128((const __m128i*) params->sse2.rounding);
210 
211     const __m128i vnmask0x0123 = _mm_cmpgt_epi32(_mm_setzero_si128(), vacc0x0123);
212     const __m128i vnmask1x0123 = _mm_cmpgt_epi32(_mm_setzero_si128(), vacc1x0123);
213     const __m128i vnmask2x0123 = _mm_cmpgt_epi32(_mm_setzero_si128(), vacc2x0123);
214     const __m128i vnmask3x0123 = _mm_cmpgt_epi32(_mm_setzero_si128(), vacc3x0123);
215 
216     const __m128i vabsacc0x0123 = _mm_abs_epi32(vacc0x0123);
217     const __m128i vabsacc1x0123 = _mm_abs_epi32(vacc1x0123);
218     const __m128i vabsacc2x0123 = _mm_abs_epi32(vacc2x0123);
219     const __m128i vabsacc3x0123 = _mm_abs_epi32(vacc3x0123);
220 
221     const __m128i vabsacc0x1133 = _mm_shuffle_epi32(vabsacc0x0123, _MM_SHUFFLE(3, 3, 1, 1));
222     const __m128i vabsacc1x1133 = _mm_shuffle_epi32(vabsacc1x0123, _MM_SHUFFLE(3, 3, 1, 1));
223     const __m128i vabsacc2x1133 = _mm_shuffle_epi32(vabsacc2x0123, _MM_SHUFFLE(3, 3, 1, 1));
224     const __m128i vabsacc3x1133 = _mm_shuffle_epi32(vabsacc3x0123, _MM_SHUFFLE(3, 3, 1, 1));
225 
226     const __m128i vabsprod0x02 = _mm_mul_epu32(vabsacc0x0123, vmultiplier);
227     const __m128i vabsprod1x02 = _mm_mul_epu32(vabsacc1x0123, vmultiplier);
228     const __m128i vabsprod2x02 = _mm_mul_epu32(vabsacc2x0123, vmultiplier);
229     const __m128i vabsprod3x02 = _mm_mul_epu32(vabsacc3x0123, vmultiplier);
230 
231     const __m128i vnmask0x02 = _mm_shuffle_epi32(vnmask0x0123, _MM_SHUFFLE(2, 2, 0, 0));
232     const __m128i vnmask1x02 = _mm_shuffle_epi32(vnmask1x0123, _MM_SHUFFLE(2, 2, 0, 0));
233     const __m128i vnmask2x02 = _mm_shuffle_epi32(vnmask2x0123, _MM_SHUFFLE(2, 2, 0, 0));
234     const __m128i vnmask3x02 = _mm_shuffle_epi32(vnmask3x0123, _MM_SHUFFLE(2, 2, 0, 0));
235 
236     const __m128i vprod0x02 = _mm_sub_epi64(_mm_xor_si128(vabsprod0x02, vnmask0x02), vnmask0x02);
237     const __m128i vprod1x02 = _mm_sub_epi64(_mm_xor_si128(vabsprod1x02, vnmask1x02), vnmask1x02);
238     const __m128i vprod2x02 = _mm_sub_epi64(_mm_xor_si128(vabsprod2x02, vnmask2x02), vnmask2x02);
239     const __m128i vprod3x02 = _mm_sub_epi64(_mm_xor_si128(vabsprod3x02, vnmask3x02), vnmask3x02);
240 
241     const __m128i vq31prod0x02 = _mm_srli_epi64(_mm_add_epi64(vprod0x02, vrounding), 31);
242     const __m128i vq31prod1x02 = _mm_srli_epi64(_mm_add_epi64(vprod1x02, vrounding), 31);
243     const __m128i vq31prod2x02 = _mm_srli_epi64(_mm_add_epi64(vprod2x02, vrounding), 31);
244     const __m128i vq31prod3x02 = _mm_srli_epi64(_mm_add_epi64(vprod3x02, vrounding), 31);
245 
246     const __m128i vabsprod0x13 = _mm_mul_epu32(vabsacc0x1133, vmultiplier);
247     const __m128i vabsprod1x13 = _mm_mul_epu32(vabsacc1x1133, vmultiplier);
248     const __m128i vabsprod2x13 = _mm_mul_epu32(vabsacc2x1133, vmultiplier);
249     const __m128i vabsprod3x13 = _mm_mul_epu32(vabsacc3x1133, vmultiplier);
250 
251     const __m128i vnmask0x13 = _mm_shuffle_epi32(vnmask0x0123, _MM_SHUFFLE(3, 3, 1, 1));
252     const __m128i vnmask1x13 = _mm_shuffle_epi32(vnmask1x0123, _MM_SHUFFLE(3, 3, 1, 1));
253     const __m128i vnmask2x13 = _mm_shuffle_epi32(vnmask2x0123, _MM_SHUFFLE(3, 3, 1, 1));
254     const __m128i vnmask3x13 = _mm_shuffle_epi32(vnmask3x0123, _MM_SHUFFLE(3, 3, 1, 1));
255 
256     const __m128i vprod0x13 = _mm_sub_epi64(_mm_xor_si128(vabsprod0x13, vnmask0x13), vnmask0x13);
257     const __m128i vprod1x13 = _mm_sub_epi64(_mm_xor_si128(vabsprod1x13, vnmask1x13), vnmask1x13);
258     const __m128i vprod2x13 = _mm_sub_epi64(_mm_xor_si128(vabsprod2x13, vnmask2x13), vnmask2x13);
259     const __m128i vprod3x13 = _mm_sub_epi64(_mm_xor_si128(vabsprod3x13, vnmask3x13), vnmask3x13);
260 
261     const __m128i vq31prod0x13 = _mm_srli_epi64(_mm_add_epi64(vprod0x13, vrounding), 31);
262     const __m128i vq31prod1x13 = _mm_srli_epi64(_mm_add_epi64(vprod1x13, vrounding), 31);
263     const __m128i vq31prod2x13 = _mm_srli_epi64(_mm_add_epi64(vprod2x13, vrounding), 31);
264     const __m128i vq31prod3x13 = _mm_srli_epi64(_mm_add_epi64(vprod3x13, vrounding), 31);
265 
266     const __m128i vq31prod0x0213 = _mm_castps_si128(_mm_shuffle_ps(
267         _mm_castsi128_ps(vq31prod0x02), _mm_castsi128_ps(vq31prod0x13), _MM_SHUFFLE(2, 0, 2, 0)));
268     const __m128i vq31prod1x0213 = _mm_castps_si128(_mm_shuffle_ps(
269         _mm_castsi128_ps(vq31prod1x02), _mm_castsi128_ps(vq31prod1x13), _MM_SHUFFLE(2, 0, 2, 0)));
270     const __m128i vq31prod2x0213 = _mm_castps_si128(_mm_shuffle_ps(
271         _mm_castsi128_ps(vq31prod2x02), _mm_castsi128_ps(vq31prod2x13), _MM_SHUFFLE(2, 0, 2, 0)));
272     const __m128i vq31prod3x0213 = _mm_castps_si128(_mm_shuffle_ps(
273         _mm_castsi128_ps(vq31prod3x02), _mm_castsi128_ps(vq31prod3x13), _MM_SHUFFLE(2, 0, 2, 0)));
274 
275     const __m128i vq31prod0x0123 = _mm_shuffle_epi32(vq31prod0x0213, _MM_SHUFFLE(3, 1, 2, 0));
276     const __m128i vq31prod1x0123 = _mm_shuffle_epi32(vq31prod1x0213, _MM_SHUFFLE(3, 1, 2, 0));
277     const __m128i vq31prod2x0123 = _mm_shuffle_epi32(vq31prod2x0213, _MM_SHUFFLE(3, 1, 2, 0));
278     const __m128i vq31prod3x0123 = _mm_shuffle_epi32(vq31prod3x0213, _MM_SHUFFLE(3, 1, 2, 0));
279 
280     const __m128i vremainder_mask = _mm_load_si128((const __m128i*) params->sse2.remainder_mask);
281     const __m128i vrem0x0123 =
282       _mm_add_epi32(_mm_and_si128(vq31prod0x0123, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vq31prod0x0123));
283     const __m128i vrem1x0123 =
284       _mm_add_epi32(_mm_and_si128(vq31prod1x0123, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vq31prod1x0123));
285     const __m128i vrem2x0123 =
286       _mm_add_epi32(_mm_and_si128(vq31prod2x0123, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vq31prod2x0123));
287     const __m128i vrem3x0123 =
288       _mm_add_epi32(_mm_and_si128(vq31prod3x0123, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vq31prod3x0123));
289 
290     const __m128i vremainder_threshold = _mm_load_si128((const __m128i*) params->sse2.remainder_threshold);
291     const __m128i vshift = _mm_load_si128((const __m128i*) params->sse2.shift);
292     vacc0x0123 =
293       _mm_sub_epi32(_mm_sra_epi32(vq31prod0x0123, vshift), _mm_cmpgt_epi32(vrem0x0123, vremainder_threshold));
294     vacc1x0123 =
295       _mm_sub_epi32(_mm_sra_epi32(vq31prod1x0123, vshift), _mm_cmpgt_epi32(vrem1x0123, vremainder_threshold));
296     vacc2x0123 =
297       _mm_sub_epi32(_mm_sra_epi32(vq31prod2x0123, vshift), _mm_cmpgt_epi32(vrem2x0123, vremainder_threshold));
298     vacc3x0123 =
299       _mm_sub_epi32(_mm_sra_epi32(vq31prod3x0123, vshift), _mm_cmpgt_epi32(vrem3x0123, vremainder_threshold));
300 
301     const __m128i voutput_zero_point = _mm_load_si128((const __m128i*) params->sse2.output_zero_point);
302     __m128i vacc01x0123 = _mm_adds_epi16(_mm_packs_epi32(vacc0x0123, vacc1x0123), voutput_zero_point);
303     __m128i vacc23x0123 = _mm_adds_epi16(_mm_packs_epi32(vacc2x0123, vacc3x0123), voutput_zero_point);
304 
305     const __m128i voutput_min = _mm_load_si128((const __m128i*) params->sse2.output_min);
306     const __m128i voutput_max = _mm_load_si128((const __m128i*) params->sse2.output_max);
307     vacc01x0123 = _mm_min_epi16(_mm_max_epi16(vacc01x0123, voutput_min), voutput_max);
308     vacc23x0123 = _mm_min_epi16(_mm_max_epi16(vacc23x0123, voutput_min), voutput_max);
309 
310     __m128i vout = _mm_packs_epi16(vacc01x0123, vacc23x0123);
311 
312     if (nc >= 4) {
313       *((uint32_t*) c3) = (uint32_t) _mm_cvtsi128_si32(_mm_shuffle_epi32(vout, _MM_SHUFFLE(3, 3, 3, 3)));
314       c3 = (int8_t*) ((uintptr_t) c3 + cn_stride);
315       *((uint32_t*) c2) = (uint32_t) _mm_cvtsi128_si32(_mm_shuffle_epi32(vout, _MM_SHUFFLE(2, 2, 2, 2)));
316       c2 = (int8_t*) ((uintptr_t) c2 + cn_stride);
317       *((uint32_t*) c1) = (uint32_t) _mm_cvtsi128_si32(_mm_shuffle_epi32(vout, _MM_SHUFFLE(1, 1, 1, 1)));
318       c1 = (int8_t*) ((uintptr_t) c1 + cn_stride);
319       *((uint32_t*) c0) = (uint32_t) _mm_cvtsi128_si32(vout);
320       c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
321 
322       a = (const int8_t**restrict) ((uintptr_t) a - ks);
323 
324       nc -= 4;
325     } else {
326       if (nc & 2) {
327         *((uint16_t*) c3) = (uint16_t) _mm_extract_epi16(vout, 6);
328         c3 += 2;
329         *((uint16_t*) c2) = (uint16_t) _mm_extract_epi16(vout, 4);
330         c2 += 2;
331         *((uint16_t*) c1) = (uint16_t) _mm_extract_epi16(vout, 2);
332         c1 += 2;
333         *((uint16_t*) c0) = (uint16_t) _mm_extract_epi16(vout, 0);
334         c0 += 2;
335         vout = _mm_srli_epi32(vout, 16);
336       }
337       if (nc & 1) {
338         *((int8_t*) c3) = (int8_t) _mm_extract_epi16(vout, 6);
339         *((int8_t*) c2) = (int8_t) _mm_extract_epi16(vout, 4);
340         *((int8_t*) c1) = (int8_t) _mm_extract_epi16(vout, 2);
341         *((int8_t*) c0) = (int8_t) _mm_cvtsi128_si32(vout);
342       }
343 
344       nc = 0;
345     }
346   } while (nc != 0);
347 }
348