xref: /aosp_15_r20/external/XNNPACK/src/qs8-gemm/gen/6x16c4-minmax-rndnu-neondot.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Auto-generated file. Do not edit!
2 //   Template: src/qs8-gemm/c4-neondot.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 <arm_neon.h>
13 
14 #include <xnnpack/gemm.h>
15 #include <xnnpack/math.h>
16 
17 
xnn_qs8_gemm_minmax_rndnu_ukernel_6x16c4__neondot(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_conv_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])18 void xnn_qs8_gemm_minmax_rndnu_ukernel_6x16c4__neondot(
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_conv_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
29 {
30   assert(mr != 0);
31   assert(mr <= 6);
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, 4 * sizeof(int8_t));
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   const int8_t* a4 = (const int8_t*) ((uintptr_t) a3 + a_stride);
61   int8_t* c4 = (int8_t*) ((uintptr_t) c3 + cm_stride);
62   if XNN_UNPREDICTABLE(mr <= 4) {
63     a4 = a3;
64     c4 = c3;
65   }
66   const int8_t* a5 = (const int8_t*) ((uintptr_t) a4 + a_stride);
67   int8_t* c5 = (int8_t*) ((uintptr_t) c4 + cm_stride);
68   if XNN_UNPREDICTABLE(mr != 6) {
69     a5 = a4;
70     c5 = c4;
71   }
72 
73   // Loop over groups of 16 columns.
74   do {
75     // Initialize accumulators with bias. 16 bias values are loaded from the
76     // weight matrix, at the start of the group of 16 columns.
77     int32x4_t vacc0x0123 = vld1q_s32(w); w = (const void*) ((const int32_t*) w + 4);
78     int32x4_t vacc0x4567 = vld1q_s32(w); w = (const void*) ((const int32_t*) w + 4);
79     int32x4_t vacc0x89AB = vld1q_s32(w); w = (const void*) ((const int32_t*) w + 4);
80     int32x4_t vacc0xCDEF = vld1q_s32(w); w = (const void*) ((const int32_t*) w + 4);
81     int32x4_t vacc1x0123 = vacc0x0123;
82     int32x4_t vacc1x4567 = vacc0x4567;
83     int32x4_t vacc1x89AB = vacc0x89AB;
84     int32x4_t vacc1xCDEF = vacc0xCDEF;
85     int32x4_t vacc2x0123 = vacc0x0123;
86     int32x4_t vacc2x4567 = vacc0x4567;
87     int32x4_t vacc2x89AB = vacc0x89AB;
88     int32x4_t vacc2xCDEF = vacc0xCDEF;
89     int32x4_t vacc3x0123 = vacc0x0123;
90     int32x4_t vacc3x4567 = vacc0x4567;
91     int32x4_t vacc3x89AB = vacc0x89AB;
92     int32x4_t vacc3xCDEF = vacc0xCDEF;
93     int32x4_t vacc4x0123 = vacc0x0123;
94     int32x4_t vacc4x4567 = vacc0x4567;
95     int32x4_t vacc4x89AB = vacc0x89AB;
96     int32x4_t vacc4xCDEF = vacc0xCDEF;
97     int32x4_t vacc5x0123 = vacc0x0123;
98     int32x4_t vacc5x4567 = vacc0x4567;
99     int32x4_t vacc5x89AB = vacc0x89AB;
100     int32x4_t vacc5xCDEF = vacc0xCDEF;
101 
102     // Inner accumulation loop along the 16 columns.
103     size_t k = kc;
104     // 2x partial unrolled loop to load 8 bytes at a time.
105     while (k >= 8 * sizeof(int8_t)) {
106       // Load a 6x8 block of activations.
107       const int8x8_t va0x01234567 = vld1_s8(a0); a0 += 8;
108       const int8x8_t va1x01234567 = vld1_s8(a1); a1 += 8;
109       const int8x8_t va2x01234567 = vld1_s8(a2); a2 += 8;
110       const int8x8_t va3x01234567 = vld1_s8(a3); a3 += 8;
111       const int8x8_t va4x01234567 = vld1_s8(a4); a4 += 8;
112       const int8x8_t va5x01234567 = vld1_s8(a5); a5 += 8;
113 
114       // Load a 8x16 block of weights.
115       const int8x16_t vb0123x0123 = vld1q_s8(w); w = (const void*) ((const int8_t*) w + 16);
116       const int8x16_t vb0123x4567 = vld1q_s8(w); w = (const void*) ((const int8_t*) w + 16);
117       const int8x16_t vb0123x89AB = vld1q_s8(w); w = (const void*) ((const int8_t*) w + 16);
118       const int8x16_t vb0123xCDEF = vld1q_s8(w); w = (const void*) ((const int8_t*) w + 16);
119       const int8x16_t vb4567x0123 = vld1q_s8(w); w = (const void*) ((const int8_t*) w + 16);
120       const int8x16_t vb4567x4567 = vld1q_s8(w); w = (const void*) ((const int8_t*) w + 16);
121       const int8x16_t vb4567x89AB = vld1q_s8(w); w = (const void*) ((const int8_t*) w + 16);
122       const int8x16_t vb4567xCDEF = vld1q_s8(w); w = (const void*) ((const int8_t*) w + 16);
123 
124       // Multiply-accumulate: 6x8 * 8x16 --> 6x16.
125       vacc0x0123 = vdotq_lane_s32(vacc0x0123, vb0123x0123, va0x01234567, 0);
126       vacc0x4567 = vdotq_lane_s32(vacc0x4567, vb0123x4567, va0x01234567, 0);
127       vacc0x89AB = vdotq_lane_s32(vacc0x89AB, vb0123x89AB, va0x01234567, 0);
128       vacc0xCDEF = vdotq_lane_s32(vacc0xCDEF, vb0123xCDEF, va0x01234567, 0);
129       vacc1x0123 = vdotq_lane_s32(vacc1x0123, vb0123x0123, va1x01234567, 0);
130       vacc1x4567 = vdotq_lane_s32(vacc1x4567, vb0123x4567, va1x01234567, 0);
131       vacc1x89AB = vdotq_lane_s32(vacc1x89AB, vb0123x89AB, va1x01234567, 0);
132       vacc1xCDEF = vdotq_lane_s32(vacc1xCDEF, vb0123xCDEF, va1x01234567, 0);
133       vacc2x0123 = vdotq_lane_s32(vacc2x0123, vb0123x0123, va2x01234567, 0);
134       vacc2x4567 = vdotq_lane_s32(vacc2x4567, vb0123x4567, va2x01234567, 0);
135       vacc2x89AB = vdotq_lane_s32(vacc2x89AB, vb0123x89AB, va2x01234567, 0);
136       vacc2xCDEF = vdotq_lane_s32(vacc2xCDEF, vb0123xCDEF, va2x01234567, 0);
137       vacc3x0123 = vdotq_lane_s32(vacc3x0123, vb0123x0123, va3x01234567, 0);
138       vacc3x4567 = vdotq_lane_s32(vacc3x4567, vb0123x4567, va3x01234567, 0);
139       vacc3x89AB = vdotq_lane_s32(vacc3x89AB, vb0123x89AB, va3x01234567, 0);
140       vacc3xCDEF = vdotq_lane_s32(vacc3xCDEF, vb0123xCDEF, va3x01234567, 0);
141       vacc4x0123 = vdotq_lane_s32(vacc4x0123, vb0123x0123, va4x01234567, 0);
142       vacc4x4567 = vdotq_lane_s32(vacc4x4567, vb0123x4567, va4x01234567, 0);
143       vacc4x89AB = vdotq_lane_s32(vacc4x89AB, vb0123x89AB, va4x01234567, 0);
144       vacc4xCDEF = vdotq_lane_s32(vacc4xCDEF, vb0123xCDEF, va4x01234567, 0);
145       vacc5x0123 = vdotq_lane_s32(vacc5x0123, vb0123x0123, va5x01234567, 0);
146       vacc5x4567 = vdotq_lane_s32(vacc5x4567, vb0123x4567, va5x01234567, 0);
147       vacc5x89AB = vdotq_lane_s32(vacc5x89AB, vb0123x89AB, va5x01234567, 0);
148       vacc5xCDEF = vdotq_lane_s32(vacc5xCDEF, vb0123xCDEF, va5x01234567, 0);
149       vacc0x0123 = vdotq_lane_s32(vacc0x0123, vb4567x0123, va0x01234567, 1);
150       vacc0x4567 = vdotq_lane_s32(vacc0x4567, vb4567x4567, va0x01234567, 1);
151       vacc0x89AB = vdotq_lane_s32(vacc0x89AB, vb4567x89AB, va0x01234567, 1);
152       vacc0xCDEF = vdotq_lane_s32(vacc0xCDEF, vb4567xCDEF, va0x01234567, 1);
153       vacc1x0123 = vdotq_lane_s32(vacc1x0123, vb4567x0123, va1x01234567, 1);
154       vacc1x4567 = vdotq_lane_s32(vacc1x4567, vb4567x4567, va1x01234567, 1);
155       vacc1x89AB = vdotq_lane_s32(vacc1x89AB, vb4567x89AB, va1x01234567, 1);
156       vacc1xCDEF = vdotq_lane_s32(vacc1xCDEF, vb4567xCDEF, va1x01234567, 1);
157       vacc2x0123 = vdotq_lane_s32(vacc2x0123, vb4567x0123, va2x01234567, 1);
158       vacc2x4567 = vdotq_lane_s32(vacc2x4567, vb4567x4567, va2x01234567, 1);
159       vacc2x89AB = vdotq_lane_s32(vacc2x89AB, vb4567x89AB, va2x01234567, 1);
160       vacc2xCDEF = vdotq_lane_s32(vacc2xCDEF, vb4567xCDEF, va2x01234567, 1);
161       vacc3x0123 = vdotq_lane_s32(vacc3x0123, vb4567x0123, va3x01234567, 1);
162       vacc3x4567 = vdotq_lane_s32(vacc3x4567, vb4567x4567, va3x01234567, 1);
163       vacc3x89AB = vdotq_lane_s32(vacc3x89AB, vb4567x89AB, va3x01234567, 1);
164       vacc3xCDEF = vdotq_lane_s32(vacc3xCDEF, vb4567xCDEF, va3x01234567, 1);
165       vacc4x0123 = vdotq_lane_s32(vacc4x0123, vb4567x0123, va4x01234567, 1);
166       vacc4x4567 = vdotq_lane_s32(vacc4x4567, vb4567x4567, va4x01234567, 1);
167       vacc4x89AB = vdotq_lane_s32(vacc4x89AB, vb4567x89AB, va4x01234567, 1);
168       vacc4xCDEF = vdotq_lane_s32(vacc4xCDEF, vb4567xCDEF, va4x01234567, 1);
169       vacc5x0123 = vdotq_lane_s32(vacc5x0123, vb4567x0123, va5x01234567, 1);
170       vacc5x4567 = vdotq_lane_s32(vacc5x4567, vb4567x4567, va5x01234567, 1);
171       vacc5x89AB = vdotq_lane_s32(vacc5x89AB, vb4567x89AB, va5x01234567, 1);
172       vacc5xCDEF = vdotq_lane_s32(vacc5xCDEF, vb4567xCDEF, va5x01234567, 1);
173 
174       k -= 8 * sizeof(int8_t);
175     }
176     // Handle up to 4 final positions of `k`
177     if XNN_UNLIKELY(k != 0) {
178       // Load a 6x4 block of activations.
179       const int8x8_t va0x01234567 = vld1_s8(a0); a0 += 4;
180       const int8x8_t va1x01234567 = vld1_s8(a1); a1 += 4;
181       const int8x8_t va2x01234567 = vld1_s8(a2); a2 += 4;
182       const int8x8_t va3x01234567 = vld1_s8(a3); a3 += 4;
183       const int8x8_t va4x01234567 = vld1_s8(a4); a4 += 4;
184       const int8x8_t va5x01234567 = vld1_s8(a5); a5 += 4;
185 
186       // Load a 4x16 block of weights.
187       const int8x16_t vb0123x0123 = vld1q_s8(w); w = (const void*) ((const int8_t*) w + 16);
188       const int8x16_t vb0123x4567 = vld1q_s8(w); w = (const void*) ((const int8_t*) w + 16);
189       const int8x16_t vb0123x89AB = vld1q_s8(w); w = (const void*) ((const int8_t*) w + 16);
190       const int8x16_t vb0123xCDEF = vld1q_s8(w); w = (const void*) ((const int8_t*) w + 16);
191 
192       // Multiply-accumulate: 6x4 * 4x16 --> 6x16.
193       vacc0x0123 = vdotq_lane_s32(vacc0x0123, vb0123x0123, va0x01234567, 0);
194       vacc0x4567 = vdotq_lane_s32(vacc0x4567, vb0123x4567, va0x01234567, 0);
195       vacc0x89AB = vdotq_lane_s32(vacc0x89AB, vb0123x89AB, va0x01234567, 0);
196       vacc0xCDEF = vdotq_lane_s32(vacc0xCDEF, vb0123xCDEF, va0x01234567, 0);
197       vacc1x0123 = vdotq_lane_s32(vacc1x0123, vb0123x0123, va1x01234567, 0);
198       vacc1x4567 = vdotq_lane_s32(vacc1x4567, vb0123x4567, va1x01234567, 0);
199       vacc1x89AB = vdotq_lane_s32(vacc1x89AB, vb0123x89AB, va1x01234567, 0);
200       vacc1xCDEF = vdotq_lane_s32(vacc1xCDEF, vb0123xCDEF, va1x01234567, 0);
201       vacc2x0123 = vdotq_lane_s32(vacc2x0123, vb0123x0123, va2x01234567, 0);
202       vacc2x4567 = vdotq_lane_s32(vacc2x4567, vb0123x4567, va2x01234567, 0);
203       vacc2x89AB = vdotq_lane_s32(vacc2x89AB, vb0123x89AB, va2x01234567, 0);
204       vacc2xCDEF = vdotq_lane_s32(vacc2xCDEF, vb0123xCDEF, va2x01234567, 0);
205       vacc3x0123 = vdotq_lane_s32(vacc3x0123, vb0123x0123, va3x01234567, 0);
206       vacc3x4567 = vdotq_lane_s32(vacc3x4567, vb0123x4567, va3x01234567, 0);
207       vacc3x89AB = vdotq_lane_s32(vacc3x89AB, vb0123x89AB, va3x01234567, 0);
208       vacc3xCDEF = vdotq_lane_s32(vacc3xCDEF, vb0123xCDEF, va3x01234567, 0);
209       vacc4x0123 = vdotq_lane_s32(vacc4x0123, vb0123x0123, va4x01234567, 0);
210       vacc4x4567 = vdotq_lane_s32(vacc4x4567, vb0123x4567, va4x01234567, 0);
211       vacc4x89AB = vdotq_lane_s32(vacc4x89AB, vb0123x89AB, va4x01234567, 0);
212       vacc4xCDEF = vdotq_lane_s32(vacc4xCDEF, vb0123xCDEF, va4x01234567, 0);
213       vacc5x0123 = vdotq_lane_s32(vacc5x0123, vb0123x0123, va5x01234567, 0);
214       vacc5x4567 = vdotq_lane_s32(vacc5x4567, vb0123x4567, va5x01234567, 0);
215       vacc5x89AB = vdotq_lane_s32(vacc5x89AB, vb0123x89AB, va5x01234567, 0);
216       vacc5xCDEF = vdotq_lane_s32(vacc5xCDEF, vb0123xCDEF, va5x01234567, 0);
217     }
218 
219     const int32x4_t vright_pre_shift = vld1q_dup_s32(&params->rndnu_neon.right_pre_shift);
220     const int32x4_t vmultiplier = vld1q_dup_s32(&params->rndnu_neon.multiplier);
221     const int32x4_t vright_post_shift = vld1q_dup_s32(&params->rndnu_neon.right_post_shift);
222 
223     vacc0x0123 = vqshlq_s32(vacc0x0123, vright_pre_shift);
224     vacc0x4567 = vqshlq_s32(vacc0x4567, vright_pre_shift);
225     vacc0x89AB = vqshlq_s32(vacc0x89AB, vright_pre_shift);
226     vacc0xCDEF = vqshlq_s32(vacc0xCDEF, vright_pre_shift);
227     vacc1x0123 = vqshlq_s32(vacc1x0123, vright_pre_shift);
228     vacc1x4567 = vqshlq_s32(vacc1x4567, vright_pre_shift);
229     vacc1x89AB = vqshlq_s32(vacc1x89AB, vright_pre_shift);
230     vacc1xCDEF = vqshlq_s32(vacc1xCDEF, vright_pre_shift);
231     vacc2x0123 = vqshlq_s32(vacc2x0123, vright_pre_shift);
232     vacc2x4567 = vqshlq_s32(vacc2x4567, vright_pre_shift);
233     vacc2x89AB = vqshlq_s32(vacc2x89AB, vright_pre_shift);
234     vacc2xCDEF = vqshlq_s32(vacc2xCDEF, vright_pre_shift);
235     vacc3x0123 = vqshlq_s32(vacc3x0123, vright_pre_shift);
236     vacc3x4567 = vqshlq_s32(vacc3x4567, vright_pre_shift);
237     vacc3x89AB = vqshlq_s32(vacc3x89AB, vright_pre_shift);
238     vacc3xCDEF = vqshlq_s32(vacc3xCDEF, vright_pre_shift);
239     vacc4x0123 = vqshlq_s32(vacc4x0123, vright_pre_shift);
240     vacc4x4567 = vqshlq_s32(vacc4x4567, vright_pre_shift);
241     vacc4x89AB = vqshlq_s32(vacc4x89AB, vright_pre_shift);
242     vacc4xCDEF = vqshlq_s32(vacc4xCDEF, vright_pre_shift);
243     vacc5x0123 = vqshlq_s32(vacc5x0123, vright_pre_shift);
244     vacc5x4567 = vqshlq_s32(vacc5x4567, vright_pre_shift);
245     vacc5x89AB = vqshlq_s32(vacc5x89AB, vright_pre_shift);
246     vacc5xCDEF = vqshlq_s32(vacc5xCDEF, vright_pre_shift);
247 
248     vacc0x0123 = vqdmulhq_s32(vacc0x0123, vmultiplier);
249     vacc0x4567 = vqdmulhq_s32(vacc0x4567, vmultiplier);
250     vacc0x89AB = vqdmulhq_s32(vacc0x89AB, vmultiplier);
251     vacc0xCDEF = vqdmulhq_s32(vacc0xCDEF, vmultiplier);
252     vacc1x0123 = vqdmulhq_s32(vacc1x0123, vmultiplier);
253     vacc1x4567 = vqdmulhq_s32(vacc1x4567, vmultiplier);
254     vacc1x89AB = vqdmulhq_s32(vacc1x89AB, vmultiplier);
255     vacc1xCDEF = vqdmulhq_s32(vacc1xCDEF, vmultiplier);
256     vacc2x0123 = vqdmulhq_s32(vacc2x0123, vmultiplier);
257     vacc2x4567 = vqdmulhq_s32(vacc2x4567, vmultiplier);
258     vacc2x89AB = vqdmulhq_s32(vacc2x89AB, vmultiplier);
259     vacc2xCDEF = vqdmulhq_s32(vacc2xCDEF, vmultiplier);
260     vacc3x0123 = vqdmulhq_s32(vacc3x0123, vmultiplier);
261     vacc3x4567 = vqdmulhq_s32(vacc3x4567, vmultiplier);
262     vacc3x89AB = vqdmulhq_s32(vacc3x89AB, vmultiplier);
263     vacc3xCDEF = vqdmulhq_s32(vacc3xCDEF, vmultiplier);
264     vacc4x0123 = vqdmulhq_s32(vacc4x0123, vmultiplier);
265     vacc4x4567 = vqdmulhq_s32(vacc4x4567, vmultiplier);
266     vacc4x89AB = vqdmulhq_s32(vacc4x89AB, vmultiplier);
267     vacc4xCDEF = vqdmulhq_s32(vacc4xCDEF, vmultiplier);
268     vacc5x0123 = vqdmulhq_s32(vacc5x0123, vmultiplier);
269     vacc5x4567 = vqdmulhq_s32(vacc5x4567, vmultiplier);
270     vacc5x89AB = vqdmulhq_s32(vacc5x89AB, vmultiplier);
271     vacc5xCDEF = vqdmulhq_s32(vacc5xCDEF, vmultiplier);
272 
273     vacc0x0123 = vrshlq_s32(vacc0x0123, vright_post_shift);
274     vacc0x4567 = vrshlq_s32(vacc0x4567, vright_post_shift);
275     vacc0x89AB = vrshlq_s32(vacc0x89AB, vright_post_shift);
276     vacc0xCDEF = vrshlq_s32(vacc0xCDEF, vright_post_shift);
277     vacc1x0123 = vrshlq_s32(vacc1x0123, vright_post_shift);
278     vacc1x4567 = vrshlq_s32(vacc1x4567, vright_post_shift);
279     vacc1x89AB = vrshlq_s32(vacc1x89AB, vright_post_shift);
280     vacc1xCDEF = vrshlq_s32(vacc1xCDEF, vright_post_shift);
281     vacc2x0123 = vrshlq_s32(vacc2x0123, vright_post_shift);
282     vacc2x4567 = vrshlq_s32(vacc2x4567, vright_post_shift);
283     vacc2x89AB = vrshlq_s32(vacc2x89AB, vright_post_shift);
284     vacc2xCDEF = vrshlq_s32(vacc2xCDEF, vright_post_shift);
285     vacc3x0123 = vrshlq_s32(vacc3x0123, vright_post_shift);
286     vacc3x4567 = vrshlq_s32(vacc3x4567, vright_post_shift);
287     vacc3x89AB = vrshlq_s32(vacc3x89AB, vright_post_shift);
288     vacc3xCDEF = vrshlq_s32(vacc3xCDEF, vright_post_shift);
289     vacc4x0123 = vrshlq_s32(vacc4x0123, vright_post_shift);
290     vacc4x4567 = vrshlq_s32(vacc4x4567, vright_post_shift);
291     vacc4x89AB = vrshlq_s32(vacc4x89AB, vright_post_shift);
292     vacc4xCDEF = vrshlq_s32(vacc4xCDEF, vright_post_shift);
293     vacc5x0123 = vrshlq_s32(vacc5x0123, vright_post_shift);
294     vacc5x4567 = vrshlq_s32(vacc5x4567, vright_post_shift);
295     vacc5x89AB = vrshlq_s32(vacc5x89AB, vright_post_shift);
296     vacc5xCDEF = vrshlq_s32(vacc5xCDEF, vright_post_shift);
297 
298     const int16x8_t voutput_zero_point = vld1q_dup_s16(&params->rndnu_neon.output_zero_point);
299 #if XNN_ARCH_ARM64
300     const int16x8_t vacc0x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc0x0123), vacc0x4567), voutput_zero_point);
301     const int16x8_t vacc0x89ABCDEF = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc0x89AB), vacc0xCDEF), voutput_zero_point);
302     const int16x8_t vacc1x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc1x0123), vacc1x4567), voutput_zero_point);
303     const int16x8_t vacc1x89ABCDEF = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc1x89AB), vacc1xCDEF), voutput_zero_point);
304     const int16x8_t vacc2x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc2x0123), vacc2x4567), voutput_zero_point);
305     const int16x8_t vacc2x89ABCDEF = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc2x89AB), vacc2xCDEF), voutput_zero_point);
306     const int16x8_t vacc3x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc3x0123), vacc3x4567), voutput_zero_point);
307     const int16x8_t vacc3x89ABCDEF = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc3x89AB), vacc3xCDEF), voutput_zero_point);
308     const int16x8_t vacc4x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc4x0123), vacc4x4567), voutput_zero_point);
309     const int16x8_t vacc4x89ABCDEF = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc4x89AB), vacc4xCDEF), voutput_zero_point);
310     const int16x8_t vacc5x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc5x0123), vacc5x4567), voutput_zero_point);
311     const int16x8_t vacc5x89ABCDEF = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc5x89AB), vacc5xCDEF), voutput_zero_point);
312 
313     int8x16_t vout0x0123456789ABCDEF = vqmovn_high_s16(vqmovn_s16(vacc0x01234567), vacc0x89ABCDEF);
314     int8x16_t vout1x0123456789ABCDEF = vqmovn_high_s16(vqmovn_s16(vacc1x01234567), vacc1x89ABCDEF);
315     int8x16_t vout2x0123456789ABCDEF = vqmovn_high_s16(vqmovn_s16(vacc2x01234567), vacc2x89ABCDEF);
316     int8x16_t vout3x0123456789ABCDEF = vqmovn_high_s16(vqmovn_s16(vacc3x01234567), vacc3x89ABCDEF);
317     int8x16_t vout4x0123456789ABCDEF = vqmovn_high_s16(vqmovn_s16(vacc4x01234567), vacc4x89ABCDEF);
318     int8x16_t vout5x0123456789ABCDEF = vqmovn_high_s16(vqmovn_s16(vacc5x01234567), vacc5x89ABCDEF);
319 #else
320     const int16x8_t vacc0x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc0x0123), vqmovn_s32(vacc0x4567)), voutput_zero_point);
321     const int16x8_t vacc0x89ABCDEF = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc0x89AB), vqmovn_s32(vacc0xCDEF)), voutput_zero_point);
322     const int16x8_t vacc1x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc1x0123), vqmovn_s32(vacc1x4567)), voutput_zero_point);
323     const int16x8_t vacc1x89ABCDEF = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc1x89AB), vqmovn_s32(vacc1xCDEF)), voutput_zero_point);
324     const int16x8_t vacc2x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc2x0123), vqmovn_s32(vacc2x4567)), voutput_zero_point);
325     const int16x8_t vacc2x89ABCDEF = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc2x89AB), vqmovn_s32(vacc2xCDEF)), voutput_zero_point);
326     const int16x8_t vacc3x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc3x0123), vqmovn_s32(vacc3x4567)), voutput_zero_point);
327     const int16x8_t vacc3x89ABCDEF = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc3x89AB), vqmovn_s32(vacc3xCDEF)), voutput_zero_point);
328     const int16x8_t vacc4x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc4x0123), vqmovn_s32(vacc4x4567)), voutput_zero_point);
329     const int16x8_t vacc4x89ABCDEF = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc4x89AB), vqmovn_s32(vacc4xCDEF)), voutput_zero_point);
330     const int16x8_t vacc5x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc5x0123), vqmovn_s32(vacc5x4567)), voutput_zero_point);
331     const int16x8_t vacc5x89ABCDEF = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc5x89AB), vqmovn_s32(vacc5xCDEF)), voutput_zero_point);
332 
333     int8x16_t vout0x0123456789ABCDEF = vcombine_s8(vqmovn_s16(vacc0x01234567), vqmovn_s16(vacc0x89ABCDEF));
334     int8x16_t vout1x0123456789ABCDEF = vcombine_s8(vqmovn_s16(vacc1x01234567), vqmovn_s16(vacc1x89ABCDEF));
335     int8x16_t vout2x0123456789ABCDEF = vcombine_s8(vqmovn_s16(vacc2x01234567), vqmovn_s16(vacc2x89ABCDEF));
336     int8x16_t vout3x0123456789ABCDEF = vcombine_s8(vqmovn_s16(vacc3x01234567), vqmovn_s16(vacc3x89ABCDEF));
337     int8x16_t vout4x0123456789ABCDEF = vcombine_s8(vqmovn_s16(vacc4x01234567), vqmovn_s16(vacc4x89ABCDEF));
338     int8x16_t vout5x0123456789ABCDEF = vcombine_s8(vqmovn_s16(vacc5x01234567), vqmovn_s16(vacc5x89ABCDEF));
339 #endif
340     const int8x16_t voutput_min = vld1q_dup_s8(&params->rndnu_neon.output_min);
341     const int8x16_t voutput_max = vld1q_dup_s8(&params->rndnu_neon.output_max);
342 
343     vout0x0123456789ABCDEF = vmaxq_s8(vout0x0123456789ABCDEF, voutput_min);
344     vout1x0123456789ABCDEF = vmaxq_s8(vout1x0123456789ABCDEF, voutput_min);
345     vout2x0123456789ABCDEF = vmaxq_s8(vout2x0123456789ABCDEF, voutput_min);
346     vout3x0123456789ABCDEF = vmaxq_s8(vout3x0123456789ABCDEF, voutput_min);
347     vout4x0123456789ABCDEF = vmaxq_s8(vout4x0123456789ABCDEF, voutput_min);
348     vout5x0123456789ABCDEF = vmaxq_s8(vout5x0123456789ABCDEF, voutput_min);
349 
350     vout0x0123456789ABCDEF = vminq_s8(vout0x0123456789ABCDEF, voutput_max);
351     vout1x0123456789ABCDEF = vminq_s8(vout1x0123456789ABCDEF, voutput_max);
352     vout2x0123456789ABCDEF = vminq_s8(vout2x0123456789ABCDEF, voutput_max);
353     vout3x0123456789ABCDEF = vminq_s8(vout3x0123456789ABCDEF, voutput_max);
354     vout4x0123456789ABCDEF = vminq_s8(vout4x0123456789ABCDEF, voutput_max);
355     vout5x0123456789ABCDEF = vminq_s8(vout5x0123456789ABCDEF, voutput_max);
356 
357     if (nc >= 16) {
358       // Main case where there the 16 columns fit in the destination.
359       vst1q_s8(c0 + 0, vout0x0123456789ABCDEF);
360       vst1q_s8(c1 + 0, vout1x0123456789ABCDEF);
361       vst1q_s8(c2 + 0, vout2x0123456789ABCDEF);
362       vst1q_s8(c3 + 0, vout3x0123456789ABCDEF);
363       vst1q_s8(c4 + 0, vout4x0123456789ABCDEF);
364       vst1q_s8(c5 + 0, vout5x0123456789ABCDEF);
365 
366       // Advance to the next 16 columns.
367       c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
368       c1 = (int8_t*) ((uintptr_t) c1 + cn_stride);
369       c2 = (int8_t*) ((uintptr_t) c2 + cn_stride);
370       c3 = (int8_t*) ((uintptr_t) c3 + cn_stride);
371       c4 = (int8_t*) ((uintptr_t) c4 + cn_stride);
372       c5 = (int8_t*) ((uintptr_t) c5 + cn_stride);
373 
374       a0 = (const int8_t*) ((uintptr_t) a0 - kc);
375       a1 = (const int8_t*) ((uintptr_t) a1 - kc);
376       a2 = (const int8_t*) ((uintptr_t) a2 - kc);
377       a3 = (const int8_t*) ((uintptr_t) a3 - kc);
378       a4 = (const int8_t*) ((uintptr_t) a4 - kc);
379       a5 = (const int8_t*) ((uintptr_t) a5 - kc);
380 
381       nc -= 16;
382     } else {
383       // Final case where not all of the 16 columns fit in the destination.
384       int8x16_t vout0x01234567_1x01234567 = vcombine_s8(vget_low_s8(vout0x0123456789ABCDEF), vget_low_s8(vout1x0123456789ABCDEF));
385       int8x16_t vout2x01234567_3x01234567 = vcombine_s8(vget_low_s8(vout2x0123456789ABCDEF), vget_low_s8(vout3x0123456789ABCDEF));
386       int8x16_t vout4x01234567_5x01234567 = vcombine_s8(vget_low_s8(vout4x0123456789ABCDEF), vget_low_s8(vout5x0123456789ABCDEF));
387       if (nc & 8) {
388         vst1_s8(c0, vget_low_s8(vout0x01234567_1x01234567)); c0 += 8;
389         vst1_s8(c1, vget_high_s8(vout0x01234567_1x01234567)); c1 += 8;
390         vst1_s8(c2, vget_low_s8(vout2x01234567_3x01234567)); c2 += 8;
391         vst1_s8(c3, vget_high_s8(vout2x01234567_3x01234567)); c3 += 8;
392         vst1_s8(c4, vget_low_s8(vout4x01234567_5x01234567)); c4 += 8;
393         vst1_s8(c5, vget_high_s8(vout4x01234567_5x01234567)); c5 += 8;
394         vout0x01234567_1x01234567 = vcombine_s8(vget_high_s8(vout0x0123456789ABCDEF), vget_high_s8(vout1x0123456789ABCDEF));
395         vout2x01234567_3x01234567 = vcombine_s8(vget_high_s8(vout2x0123456789ABCDEF), vget_high_s8(vout3x0123456789ABCDEF));
396         vout4x01234567_5x01234567 = vcombine_s8(vget_high_s8(vout4x0123456789ABCDEF), vget_high_s8(vout5x0123456789ABCDEF));
397       }
398       if (nc & 4) {
399         vst1q_lane_u32((void*) c0, vreinterpretq_u32_s8(vout0x01234567_1x01234567), 0); c0 += 4;
400         vst1q_lane_u32((void*) c1, vreinterpretq_u32_s8(vout0x01234567_1x01234567), 2); c1 += 4;
401         vst1q_lane_u32((void*) c2, vreinterpretq_u32_s8(vout2x01234567_3x01234567), 0); c2 += 4;
402         vst1q_lane_u32((void*) c3, vreinterpretq_u32_s8(vout2x01234567_3x01234567), 2); c3 += 4;
403         vst1q_lane_u32((void*) c4, vreinterpretq_u32_s8(vout4x01234567_5x01234567), 0); c4 += 4;
404         vst1q_lane_u32((void*) c5, vreinterpretq_u32_s8(vout4x01234567_5x01234567), 2); c5 += 4;
405         vout0x01234567_1x01234567 = vextq_s8(vout0x01234567_1x01234567, vout0x01234567_1x01234567, 4);
406         vout2x01234567_3x01234567 = vextq_s8(vout2x01234567_3x01234567, vout2x01234567_3x01234567, 4);
407         vout4x01234567_5x01234567 = vextq_s8(vout4x01234567_5x01234567, vout4x01234567_5x01234567, 4);
408       }
409       if (nc & 2) {
410         vst1q_lane_u16((void*) c0, vreinterpretq_u16_s8(vout0x01234567_1x01234567), 0); c0 += 2;
411         vst1q_lane_u16((void*) c1, vreinterpretq_u16_s8(vout0x01234567_1x01234567), 4); c1 += 2;
412         vst1q_lane_u16((void*) c2, vreinterpretq_u16_s8(vout2x01234567_3x01234567), 0); c2 += 2;
413         vst1q_lane_u16((void*) c3, vreinterpretq_u16_s8(vout2x01234567_3x01234567), 4); c3 += 2;
414         vst1q_lane_u16((void*) c4, vreinterpretq_u16_s8(vout4x01234567_5x01234567), 0); c4 += 2;
415         vst1q_lane_u16((void*) c5, vreinterpretq_u16_s8(vout4x01234567_5x01234567), 4); c5 += 2;
416         vout0x01234567_1x01234567 = vextq_s8(vout0x01234567_1x01234567, vout0x01234567_1x01234567, 2);
417         vout2x01234567_3x01234567 = vextq_s8(vout2x01234567_3x01234567, vout2x01234567_3x01234567, 2);
418         vout4x01234567_5x01234567 = vextq_s8(vout4x01234567_5x01234567, vout4x01234567_5x01234567, 2);
419       }
420       if (nc & 1) {
421         vst1q_lane_s8(c0, vout0x01234567_1x01234567, 0);
422         vst1q_lane_s8(c1, vout0x01234567_1x01234567, 8);
423         vst1q_lane_s8(c2, vout2x01234567_3x01234567, 0);
424         vst1q_lane_s8(c3, vout2x01234567_3x01234567, 8);
425         vst1q_lane_s8(c4, vout4x01234567_5x01234567, 0);
426         vst1q_lane_s8(c5, vout4x01234567_5x01234567, 8);
427       }
428 
429       nc = 0;
430     }
431   } while (nc != 0);
432 }
433