xref: /aosp_15_r20/external/XNNPACK/src/qu8-gemm/gen/3x32c4-minmax-rndnu-neondot.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Auto-generated file. Do not edit!
2 //   Template: src/qu8-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_qu8_gemm_minmax_rndnu_ukernel_3x32c4__neondot(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_conv_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])18 void xnn_qu8_gemm_minmax_rndnu_ukernel_3x32c4__neondot(
19     size_t mr,
20     size_t nc,
21     size_t kc,
22     const uint8_t* restrict a,
23     size_t a_stride,
24     const void* restrict w,
25     uint8_t* restrict c,
26     size_t cm_stride,
27     size_t cn_stride,
28     const union xnn_qu8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
29 {
30   assert(mr != 0);
31   assert(mr <= 3);
32   assert(nc != 0);
33   assert(kc != 0);
34   assert(kc % sizeof(uint8_t) == 0);
35   assert(a != NULL);
36   assert(w != NULL);
37   assert(c != NULL);
38 
39   kc = round_up_po2(kc, 4 * sizeof(uint8_t));
40   const uint8_t* a0 = a;
41   uint8_t* c0 = c;
42   const uint8_t* a1 = (const uint8_t*) ((uintptr_t) a0 + a_stride);
43   uint8_t* c1 = (uint8_t*) ((uintptr_t) c0 + cm_stride);
44   if XNN_UNPREDICTABLE(mr < 2) {
45     a1 = a0;
46     c1 = c0;
47   }
48   const uint8_t* a2 = (const uint8_t*) ((uintptr_t) a1 + a_stride);
49   uint8_t* c2 = (uint8_t*) ((uintptr_t) c1 + cm_stride);
50   if XNN_UNPREDICTABLE(mr <= 2) {
51     a2 = a1;
52     c2 = c1;
53   }
54 
55   const uint8x8_t va_zero_point = vld1_dup_u8(&params->rndnu_neon.kernel_zero_point[0]);
56 
57   // Loop over groups of 32 columns.
58   do {
59     // Initialize accumulators with bias. 32 bias values are loaded from the
60     // weight matrix, at the start of the group of 32 columns.
61     uint32x4_t vpacc0x0123 = vld1q_u32(w); w = (const void*) ((const uint32_t*) w + 4);
62     uint32x4_t vpacc0x4567 = vld1q_u32(w); w = (const void*) ((const uint32_t*) w + 4);
63     uint32x4_t vpacc0x89AB = vld1q_u32(w); w = (const void*) ((const uint32_t*) w + 4);
64     uint32x4_t vpacc0xCDEF = vld1q_u32(w); w = (const void*) ((const uint32_t*) w + 4);
65     uint32x4_t vpacc0xGHIJ = vld1q_u32(w); w = (const void*) ((const uint32_t*) w + 4);
66     uint32x4_t vpacc0xKLMN = vld1q_u32(w); w = (const void*) ((const uint32_t*) w + 4);
67     uint32x4_t vpacc0xOPQR = vld1q_u32(w); w = (const void*) ((const uint32_t*) w + 4);
68     uint32x4_t vpacc0xSTUV = vld1q_u32(w); w = (const void*) ((const uint32_t*) w + 4);
69     uint32x4_t vpacc1x0123 = vpacc0x0123;
70     uint32x4_t vpacc1x4567 = vpacc0x4567;
71     uint32x4_t vpacc1x89AB = vpacc0x89AB;
72     uint32x4_t vpacc1xCDEF = vpacc0xCDEF;
73     uint32x4_t vpacc1xGHIJ = vpacc0xGHIJ;
74     uint32x4_t vpacc1xKLMN = vpacc0xKLMN;
75     uint32x4_t vpacc1xOPQR = vpacc0xOPQR;
76     uint32x4_t vpacc1xSTUV = vpacc0xSTUV;
77     uint32x4_t vpacc2x0123 = vpacc0x0123;
78     uint32x4_t vpacc2x4567 = vpacc0x4567;
79     uint32x4_t vpacc2x89AB = vpacc0x89AB;
80     uint32x4_t vpacc2xCDEF = vpacc0xCDEF;
81     uint32x4_t vpacc2xGHIJ = vpacc0xGHIJ;
82     uint32x4_t vpacc2xKLMN = vpacc0xKLMN;
83     uint32x4_t vpacc2xOPQR = vpacc0xOPQR;
84     uint32x4_t vpacc2xSTUV = vpacc0xSTUV;
85     uint32x2_t vnacc0 = vmov_n_u32(0);
86     uint32x2_t vnacc1 = vmov_n_u32(0);
87     uint32x2_t vnacc2 = vmov_n_u32(0);
88 
89     // Inner accumulation loop along the 32 columns.
90     size_t k = kc;
91     // 2x partial unrolled loop to load 8 bytes at a time.
92     while (k >= 8 * sizeof(uint8_t)) {
93       // Load a 3x8 block of activations.
94       const uint8x8_t va0x01234567 = vld1_u8(a0); a0 += 8;
95       const uint8x8_t va1x01234567 = vld1_u8(a1); a1 += 8;
96       const uint8x8_t va2x01234567 = vld1_u8(a2); a2 += 8;
97 
98       // Load a 8x32 block of weights.
99       const uint8x16_t vb0123x0123 = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
100       const uint8x16_t vb0123x4567 = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
101       const uint8x16_t vb0123x89AB = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
102       const uint8x16_t vb0123xCDEF = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
103       const uint8x16_t vb0123xGHIJ = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
104       const uint8x16_t vb0123xKLMN = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
105       const uint8x16_t vb0123xOPQR = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
106       const uint8x16_t vb0123xSTUV = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
107       const uint8x16_t vb4567x0123 = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
108       const uint8x16_t vb4567x4567 = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
109       const uint8x16_t vb4567x89AB = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
110       const uint8x16_t vb4567xCDEF = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
111       const uint8x16_t vb4567xGHIJ = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
112       const uint8x16_t vb4567xKLMN = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
113       const uint8x16_t vb4567xOPQR = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
114       const uint8x16_t vb4567xSTUV = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
115 
116       // Multiply-accumulate: 3x8 * 8x32 --> 3x32.
117       vnacc0 = vdot_u32(vnacc0, va_zero_point, va0x01234567);
118       vpacc0x0123 = vdotq_lane_u32(vpacc0x0123, vb0123x0123, va0x01234567, 0);
119       vpacc0x4567 = vdotq_lane_u32(vpacc0x4567, vb0123x4567, va0x01234567, 0);
120       vpacc0x89AB = vdotq_lane_u32(vpacc0x89AB, vb0123x89AB, va0x01234567, 0);
121       vpacc0xCDEF = vdotq_lane_u32(vpacc0xCDEF, vb0123xCDEF, va0x01234567, 0);
122       vpacc0xGHIJ = vdotq_lane_u32(vpacc0xGHIJ, vb0123xGHIJ, va0x01234567, 0);
123       vpacc0xKLMN = vdotq_lane_u32(vpacc0xKLMN, vb0123xKLMN, va0x01234567, 0);
124       vpacc0xOPQR = vdotq_lane_u32(vpacc0xOPQR, vb0123xOPQR, va0x01234567, 0);
125       vpacc0xSTUV = vdotq_lane_u32(vpacc0xSTUV, vb0123xSTUV, va0x01234567, 0);
126       vpacc0x0123 = vdotq_lane_u32(vpacc0x0123, vb4567x0123, va0x01234567, 1);
127       vpacc0x4567 = vdotq_lane_u32(vpacc0x4567, vb4567x4567, va0x01234567, 1);
128       vpacc0x89AB = vdotq_lane_u32(vpacc0x89AB, vb4567x89AB, va0x01234567, 1);
129       vpacc0xCDEF = vdotq_lane_u32(vpacc0xCDEF, vb4567xCDEF, va0x01234567, 1);
130       vpacc0xGHIJ = vdotq_lane_u32(vpacc0xGHIJ, vb4567xGHIJ, va0x01234567, 1);
131       vpacc0xKLMN = vdotq_lane_u32(vpacc0xKLMN, vb4567xKLMN, va0x01234567, 1);
132       vpacc0xOPQR = vdotq_lane_u32(vpacc0xOPQR, vb4567xOPQR, va0x01234567, 1);
133       vpacc0xSTUV = vdotq_lane_u32(vpacc0xSTUV, vb4567xSTUV, va0x01234567, 1);
134       vnacc1 = vdot_u32(vnacc1, va_zero_point, va1x01234567);
135       vpacc1x0123 = vdotq_lane_u32(vpacc1x0123, vb0123x0123, va1x01234567, 0);
136       vpacc1x4567 = vdotq_lane_u32(vpacc1x4567, vb0123x4567, va1x01234567, 0);
137       vpacc1x89AB = vdotq_lane_u32(vpacc1x89AB, vb0123x89AB, va1x01234567, 0);
138       vpacc1xCDEF = vdotq_lane_u32(vpacc1xCDEF, vb0123xCDEF, va1x01234567, 0);
139       vpacc1xGHIJ = vdotq_lane_u32(vpacc1xGHIJ, vb0123xGHIJ, va1x01234567, 0);
140       vpacc1xKLMN = vdotq_lane_u32(vpacc1xKLMN, vb0123xKLMN, va1x01234567, 0);
141       vpacc1xOPQR = vdotq_lane_u32(vpacc1xOPQR, vb0123xOPQR, va1x01234567, 0);
142       vpacc1xSTUV = vdotq_lane_u32(vpacc1xSTUV, vb0123xSTUV, va1x01234567, 0);
143       vpacc1x0123 = vdotq_lane_u32(vpacc1x0123, vb4567x0123, va1x01234567, 1);
144       vpacc1x4567 = vdotq_lane_u32(vpacc1x4567, vb4567x4567, va1x01234567, 1);
145       vpacc1x89AB = vdotq_lane_u32(vpacc1x89AB, vb4567x89AB, va1x01234567, 1);
146       vpacc1xCDEF = vdotq_lane_u32(vpacc1xCDEF, vb4567xCDEF, va1x01234567, 1);
147       vpacc1xGHIJ = vdotq_lane_u32(vpacc1xGHIJ, vb4567xGHIJ, va1x01234567, 1);
148       vpacc1xKLMN = vdotq_lane_u32(vpacc1xKLMN, vb4567xKLMN, va1x01234567, 1);
149       vpacc1xOPQR = vdotq_lane_u32(vpacc1xOPQR, vb4567xOPQR, va1x01234567, 1);
150       vpacc1xSTUV = vdotq_lane_u32(vpacc1xSTUV, vb4567xSTUV, va1x01234567, 1);
151       vnacc2 = vdot_u32(vnacc2, va_zero_point, va2x01234567);
152       vpacc2x0123 = vdotq_lane_u32(vpacc2x0123, vb0123x0123, va2x01234567, 0);
153       vpacc2x4567 = vdotq_lane_u32(vpacc2x4567, vb0123x4567, va2x01234567, 0);
154       vpacc2x89AB = vdotq_lane_u32(vpacc2x89AB, vb0123x89AB, va2x01234567, 0);
155       vpacc2xCDEF = vdotq_lane_u32(vpacc2xCDEF, vb0123xCDEF, va2x01234567, 0);
156       vpacc2xGHIJ = vdotq_lane_u32(vpacc2xGHIJ, vb0123xGHIJ, va2x01234567, 0);
157       vpacc2xKLMN = vdotq_lane_u32(vpacc2xKLMN, vb0123xKLMN, va2x01234567, 0);
158       vpacc2xOPQR = vdotq_lane_u32(vpacc2xOPQR, vb0123xOPQR, va2x01234567, 0);
159       vpacc2xSTUV = vdotq_lane_u32(vpacc2xSTUV, vb0123xSTUV, va2x01234567, 0);
160       vpacc2x0123 = vdotq_lane_u32(vpacc2x0123, vb4567x0123, va2x01234567, 1);
161       vpacc2x4567 = vdotq_lane_u32(vpacc2x4567, vb4567x4567, va2x01234567, 1);
162       vpacc2x89AB = vdotq_lane_u32(vpacc2x89AB, vb4567x89AB, va2x01234567, 1);
163       vpacc2xCDEF = vdotq_lane_u32(vpacc2xCDEF, vb4567xCDEF, va2x01234567, 1);
164       vpacc2xGHIJ = vdotq_lane_u32(vpacc2xGHIJ, vb4567xGHIJ, va2x01234567, 1);
165       vpacc2xKLMN = vdotq_lane_u32(vpacc2xKLMN, vb4567xKLMN, va2x01234567, 1);
166       vpacc2xOPQR = vdotq_lane_u32(vpacc2xOPQR, vb4567xOPQR, va2x01234567, 1);
167       vpacc2xSTUV = vdotq_lane_u32(vpacc2xSTUV, vb4567xSTUV, va2x01234567, 1);
168 
169       k -= 8 * sizeof(uint8_t);
170     }
171     // Handle up to 4 final positions of `k`
172     if XNN_UNLIKELY(k != 0) {
173       // Load a 3x4 block of activations.
174       const uint8x8_t va0x01234567 = vreinterpret_u8_u32(vld1_lane_u32((const void*) a0, vmov_n_u32(0), 0)); a0 += 4;
175       const uint8x8_t va1x01234567 = vreinterpret_u8_u32(vld1_lane_u32((const void*) a1, vmov_n_u32(0), 0)); a1 += 4;
176       const uint8x8_t va2x01234567 = vreinterpret_u8_u32(vld1_lane_u32((const void*) a2, vmov_n_u32(0), 0)); a2 += 4;
177 
178       // Load a 4x32 block of weights.
179       const uint8x16_t vb0123x0123 = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
180       const uint8x16_t vb0123x4567 = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
181       const uint8x16_t vb0123x89AB = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
182       const uint8x16_t vb0123xCDEF = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
183       const uint8x16_t vb0123xGHIJ = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
184       const uint8x16_t vb0123xKLMN = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
185       const uint8x16_t vb0123xOPQR = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
186       const uint8x16_t vb0123xSTUV = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
187 
188       // Multiply-accumulate: 3x4 * 4x32 --> 3x32.
189       vnacc0 = vdot_u32(vnacc0, va_zero_point, va0x01234567);
190       vpacc0x0123 = vdotq_lane_u32(vpacc0x0123, vb0123x0123, va0x01234567, 0);
191       vpacc0x4567 = vdotq_lane_u32(vpacc0x4567, vb0123x4567, va0x01234567, 0);
192       vpacc0x89AB = vdotq_lane_u32(vpacc0x89AB, vb0123x89AB, va0x01234567, 0);
193       vpacc0xCDEF = vdotq_lane_u32(vpacc0xCDEF, vb0123xCDEF, va0x01234567, 0);
194       vpacc0xGHIJ = vdotq_lane_u32(vpacc0xGHIJ, vb0123xGHIJ, va0x01234567, 0);
195       vpacc0xKLMN = vdotq_lane_u32(vpacc0xKLMN, vb0123xKLMN, va0x01234567, 0);
196       vpacc0xOPQR = vdotq_lane_u32(vpacc0xOPQR, vb0123xOPQR, va0x01234567, 0);
197       vpacc0xSTUV = vdotq_lane_u32(vpacc0xSTUV, vb0123xSTUV, va0x01234567, 0);
198       vnacc1 = vdot_u32(vnacc1, va_zero_point, va1x01234567);
199       vpacc1x0123 = vdotq_lane_u32(vpacc1x0123, vb0123x0123, va1x01234567, 0);
200       vpacc1x4567 = vdotq_lane_u32(vpacc1x4567, vb0123x4567, va1x01234567, 0);
201       vpacc1x89AB = vdotq_lane_u32(vpacc1x89AB, vb0123x89AB, va1x01234567, 0);
202       vpacc1xCDEF = vdotq_lane_u32(vpacc1xCDEF, vb0123xCDEF, va1x01234567, 0);
203       vpacc1xGHIJ = vdotq_lane_u32(vpacc1xGHIJ, vb0123xGHIJ, va1x01234567, 0);
204       vpacc1xKLMN = vdotq_lane_u32(vpacc1xKLMN, vb0123xKLMN, va1x01234567, 0);
205       vpacc1xOPQR = vdotq_lane_u32(vpacc1xOPQR, vb0123xOPQR, va1x01234567, 0);
206       vpacc1xSTUV = vdotq_lane_u32(vpacc1xSTUV, vb0123xSTUV, va1x01234567, 0);
207       vnacc2 = vdot_u32(vnacc2, va_zero_point, va2x01234567);
208       vpacc2x0123 = vdotq_lane_u32(vpacc2x0123, vb0123x0123, va2x01234567, 0);
209       vpacc2x4567 = vdotq_lane_u32(vpacc2x4567, vb0123x4567, va2x01234567, 0);
210       vpacc2x89AB = vdotq_lane_u32(vpacc2x89AB, vb0123x89AB, va2x01234567, 0);
211       vpacc2xCDEF = vdotq_lane_u32(vpacc2xCDEF, vb0123xCDEF, va2x01234567, 0);
212       vpacc2xGHIJ = vdotq_lane_u32(vpacc2xGHIJ, vb0123xGHIJ, va2x01234567, 0);
213       vpacc2xKLMN = vdotq_lane_u32(vpacc2xKLMN, vb0123xKLMN, va2x01234567, 0);
214       vpacc2xOPQR = vdotq_lane_u32(vpacc2xOPQR, vb0123xOPQR, va2x01234567, 0);
215       vpacc2xSTUV = vdotq_lane_u32(vpacc2xSTUV, vb0123xSTUV, va2x01234567, 0);
216     }
217 
218     // Subtract zero point from accumulators.
219     vnacc0 = vpadd_u32(vnacc0, vnacc0);
220     const uint32x4_t vnacc0x0123 = vcombine_u32(vnacc0, vnacc0);
221     int32x4_t vacc0x0123 = vreinterpretq_s32_u32(vsubq_u32(vpacc0x0123, vnacc0x0123));
222     int32x4_t vacc0x4567 = vreinterpretq_s32_u32(vsubq_u32(vpacc0x4567, vnacc0x0123));
223     int32x4_t vacc0x89AB = vreinterpretq_s32_u32(vsubq_u32(vpacc0x89AB, vnacc0x0123));
224     int32x4_t vacc0xCDEF = vreinterpretq_s32_u32(vsubq_u32(vpacc0xCDEF, vnacc0x0123));
225     int32x4_t vacc0xGHIJ = vreinterpretq_s32_u32(vsubq_u32(vpacc0xGHIJ, vnacc0x0123));
226     int32x4_t vacc0xKLMN = vreinterpretq_s32_u32(vsubq_u32(vpacc0xKLMN, vnacc0x0123));
227     int32x4_t vacc0xOPQR = vreinterpretq_s32_u32(vsubq_u32(vpacc0xOPQR, vnacc0x0123));
228     int32x4_t vacc0xSTUV = vreinterpretq_s32_u32(vsubq_u32(vpacc0xSTUV, vnacc0x0123));
229     vnacc1 = vpadd_u32(vnacc1, vnacc1);
230     const uint32x4_t vnacc1x0123 = vcombine_u32(vnacc1, vnacc1);
231     int32x4_t vacc1x0123 = vreinterpretq_s32_u32(vsubq_u32(vpacc1x0123, vnacc1x0123));
232     int32x4_t vacc1x4567 = vreinterpretq_s32_u32(vsubq_u32(vpacc1x4567, vnacc1x0123));
233     int32x4_t vacc1x89AB = vreinterpretq_s32_u32(vsubq_u32(vpacc1x89AB, vnacc1x0123));
234     int32x4_t vacc1xCDEF = vreinterpretq_s32_u32(vsubq_u32(vpacc1xCDEF, vnacc1x0123));
235     int32x4_t vacc1xGHIJ = vreinterpretq_s32_u32(vsubq_u32(vpacc1xGHIJ, vnacc1x0123));
236     int32x4_t vacc1xKLMN = vreinterpretq_s32_u32(vsubq_u32(vpacc1xKLMN, vnacc1x0123));
237     int32x4_t vacc1xOPQR = vreinterpretq_s32_u32(vsubq_u32(vpacc1xOPQR, vnacc1x0123));
238     int32x4_t vacc1xSTUV = vreinterpretq_s32_u32(vsubq_u32(vpacc1xSTUV, vnacc1x0123));
239     vnacc2 = vpadd_u32(vnacc2, vnacc2);
240     const uint32x4_t vnacc2x0123 = vcombine_u32(vnacc2, vnacc2);
241     int32x4_t vacc2x0123 = vreinterpretq_s32_u32(vsubq_u32(vpacc2x0123, vnacc2x0123));
242     int32x4_t vacc2x4567 = vreinterpretq_s32_u32(vsubq_u32(vpacc2x4567, vnacc2x0123));
243     int32x4_t vacc2x89AB = vreinterpretq_s32_u32(vsubq_u32(vpacc2x89AB, vnacc2x0123));
244     int32x4_t vacc2xCDEF = vreinterpretq_s32_u32(vsubq_u32(vpacc2xCDEF, vnacc2x0123));
245     int32x4_t vacc2xGHIJ = vreinterpretq_s32_u32(vsubq_u32(vpacc2xGHIJ, vnacc2x0123));
246     int32x4_t vacc2xKLMN = vreinterpretq_s32_u32(vsubq_u32(vpacc2xKLMN, vnacc2x0123));
247     int32x4_t vacc2xOPQR = vreinterpretq_s32_u32(vsubq_u32(vpacc2xOPQR, vnacc2x0123));
248     int32x4_t vacc2xSTUV = vreinterpretq_s32_u32(vsubq_u32(vpacc2xSTUV, vnacc2x0123));
249 
250     const int32x4_t vright_pre_shift = vld1q_dup_s32(&params->rndnu_neon.right_pre_shift);
251     const int32x4_t vmultiplier = vld1q_dup_s32(&params->rndnu_neon.multiplier);
252     const int32x4_t vright_post_shift = vld1q_dup_s32(&params->rndnu_neon.right_post_shift);
253 
254     vacc0x0123 = vshlq_s32(vacc0x0123, vright_pre_shift);
255     vacc0x4567 = vshlq_s32(vacc0x4567, vright_pre_shift);
256     vacc0x89AB = vshlq_s32(vacc0x89AB, vright_pre_shift);
257     vacc0xCDEF = vshlq_s32(vacc0xCDEF, vright_pre_shift);
258     vacc0xGHIJ = vshlq_s32(vacc0xGHIJ, vright_pre_shift);
259     vacc0xKLMN = vshlq_s32(vacc0xKLMN, vright_pre_shift);
260     vacc0xOPQR = vshlq_s32(vacc0xOPQR, vright_pre_shift);
261     vacc0xSTUV = vshlq_s32(vacc0xSTUV, vright_pre_shift);
262     vacc1x0123 = vshlq_s32(vacc1x0123, vright_pre_shift);
263     vacc1x4567 = vshlq_s32(vacc1x4567, vright_pre_shift);
264     vacc1x89AB = vshlq_s32(vacc1x89AB, vright_pre_shift);
265     vacc1xCDEF = vshlq_s32(vacc1xCDEF, vright_pre_shift);
266     vacc1xGHIJ = vshlq_s32(vacc1xGHIJ, vright_pre_shift);
267     vacc1xKLMN = vshlq_s32(vacc1xKLMN, vright_pre_shift);
268     vacc1xOPQR = vshlq_s32(vacc1xOPQR, vright_pre_shift);
269     vacc1xSTUV = vshlq_s32(vacc1xSTUV, vright_pre_shift);
270     vacc2x0123 = vshlq_s32(vacc2x0123, vright_pre_shift);
271     vacc2x4567 = vshlq_s32(vacc2x4567, vright_pre_shift);
272     vacc2x89AB = vshlq_s32(vacc2x89AB, vright_pre_shift);
273     vacc2xCDEF = vshlq_s32(vacc2xCDEF, vright_pre_shift);
274     vacc2xGHIJ = vshlq_s32(vacc2xGHIJ, vright_pre_shift);
275     vacc2xKLMN = vshlq_s32(vacc2xKLMN, vright_pre_shift);
276     vacc2xOPQR = vshlq_s32(vacc2xOPQR, vright_pre_shift);
277     vacc2xSTUV = vshlq_s32(vacc2xSTUV, vright_pre_shift);
278 
279     vacc0x0123 = vqdmulhq_s32(vacc0x0123, vmultiplier);
280     vacc0x4567 = vqdmulhq_s32(vacc0x4567, vmultiplier);
281     vacc0x89AB = vqdmulhq_s32(vacc0x89AB, vmultiplier);
282     vacc0xCDEF = vqdmulhq_s32(vacc0xCDEF, vmultiplier);
283     vacc0xGHIJ = vqdmulhq_s32(vacc0xGHIJ, vmultiplier);
284     vacc0xKLMN = vqdmulhq_s32(vacc0xKLMN, vmultiplier);
285     vacc0xOPQR = vqdmulhq_s32(vacc0xOPQR, vmultiplier);
286     vacc0xSTUV = vqdmulhq_s32(vacc0xSTUV, vmultiplier);
287     vacc1x0123 = vqdmulhq_s32(vacc1x0123, vmultiplier);
288     vacc1x4567 = vqdmulhq_s32(vacc1x4567, vmultiplier);
289     vacc1x89AB = vqdmulhq_s32(vacc1x89AB, vmultiplier);
290     vacc1xCDEF = vqdmulhq_s32(vacc1xCDEF, vmultiplier);
291     vacc1xGHIJ = vqdmulhq_s32(vacc1xGHIJ, vmultiplier);
292     vacc1xKLMN = vqdmulhq_s32(vacc1xKLMN, vmultiplier);
293     vacc1xOPQR = vqdmulhq_s32(vacc1xOPQR, vmultiplier);
294     vacc1xSTUV = vqdmulhq_s32(vacc1xSTUV, vmultiplier);
295     vacc2x0123 = vqdmulhq_s32(vacc2x0123, vmultiplier);
296     vacc2x4567 = vqdmulhq_s32(vacc2x4567, vmultiplier);
297     vacc2x89AB = vqdmulhq_s32(vacc2x89AB, vmultiplier);
298     vacc2xCDEF = vqdmulhq_s32(vacc2xCDEF, vmultiplier);
299     vacc2xGHIJ = vqdmulhq_s32(vacc2xGHIJ, vmultiplier);
300     vacc2xKLMN = vqdmulhq_s32(vacc2xKLMN, vmultiplier);
301     vacc2xOPQR = vqdmulhq_s32(vacc2xOPQR, vmultiplier);
302     vacc2xSTUV = vqdmulhq_s32(vacc2xSTUV, vmultiplier);
303 
304     vacc0x0123 = vrshlq_s32(vacc0x0123, vright_post_shift);
305     vacc0x4567 = vrshlq_s32(vacc0x4567, vright_post_shift);
306     vacc0x89AB = vrshlq_s32(vacc0x89AB, vright_post_shift);
307     vacc0xCDEF = vrshlq_s32(vacc0xCDEF, vright_post_shift);
308     vacc0xGHIJ = vrshlq_s32(vacc0xGHIJ, vright_post_shift);
309     vacc0xKLMN = vrshlq_s32(vacc0xKLMN, vright_post_shift);
310     vacc0xOPQR = vrshlq_s32(vacc0xOPQR, vright_post_shift);
311     vacc0xSTUV = vrshlq_s32(vacc0xSTUV, vright_post_shift);
312     vacc1x0123 = vrshlq_s32(vacc1x0123, vright_post_shift);
313     vacc1x4567 = vrshlq_s32(vacc1x4567, vright_post_shift);
314     vacc1x89AB = vrshlq_s32(vacc1x89AB, vright_post_shift);
315     vacc1xCDEF = vrshlq_s32(vacc1xCDEF, vright_post_shift);
316     vacc1xGHIJ = vrshlq_s32(vacc1xGHIJ, vright_post_shift);
317     vacc1xKLMN = vrshlq_s32(vacc1xKLMN, vright_post_shift);
318     vacc1xOPQR = vrshlq_s32(vacc1xOPQR, vright_post_shift);
319     vacc1xSTUV = vrshlq_s32(vacc1xSTUV, vright_post_shift);
320     vacc2x0123 = vrshlq_s32(vacc2x0123, vright_post_shift);
321     vacc2x4567 = vrshlq_s32(vacc2x4567, vright_post_shift);
322     vacc2x89AB = vrshlq_s32(vacc2x89AB, vright_post_shift);
323     vacc2xCDEF = vrshlq_s32(vacc2xCDEF, vright_post_shift);
324     vacc2xGHIJ = vrshlq_s32(vacc2xGHIJ, vright_post_shift);
325     vacc2xKLMN = vrshlq_s32(vacc2xKLMN, vright_post_shift);
326     vacc2xOPQR = vrshlq_s32(vacc2xOPQR, vright_post_shift);
327     vacc2xSTUV = vrshlq_s32(vacc2xSTUV, vright_post_shift);
328 
329     const int16x8_t voutput_zero_point = vld1q_dup_s16(&params->rndnu_neon.output_zero_point);
330 #if XNN_ARCH_ARM64
331     const int16x8_t vacc0x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc0x0123), vacc0x4567), voutput_zero_point);
332     const int16x8_t vacc0x89ABCDEF = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc0x89AB), vacc0xCDEF), voutput_zero_point);
333     const int16x8_t vacc0xGHIJKLMN = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc0xGHIJ), vacc0xKLMN), voutput_zero_point);
334     const int16x8_t vacc0xOPQRSTUV = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc0xOPQR), vacc0xSTUV), voutput_zero_point);
335     const int16x8_t vacc1x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc1x0123), vacc1x4567), voutput_zero_point);
336     const int16x8_t vacc1x89ABCDEF = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc1x89AB), vacc1xCDEF), voutput_zero_point);
337     const int16x8_t vacc1xGHIJKLMN = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc1xGHIJ), vacc1xKLMN), voutput_zero_point);
338     const int16x8_t vacc1xOPQRSTUV = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc1xOPQR), vacc1xSTUV), voutput_zero_point);
339     const int16x8_t vacc2x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc2x0123), vacc2x4567), voutput_zero_point);
340     const int16x8_t vacc2x89ABCDEF = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc2x89AB), vacc2xCDEF), voutput_zero_point);
341     const int16x8_t vacc2xGHIJKLMN = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc2xGHIJ), vacc2xKLMN), voutput_zero_point);
342     const int16x8_t vacc2xOPQRSTUV = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc2xOPQR), vacc2xSTUV), voutput_zero_point);
343 
344     uint8x16_t vout0x0123456789ABCDEF = vqmovun_high_s16(vqmovun_s16(vacc0x01234567), vacc0x89ABCDEF);
345     uint8x16_t vout0xGHIJKLMNOPQRSTUV = vqmovun_high_s16(vqmovun_s16(vacc0xGHIJKLMN), vacc0xOPQRSTUV);
346     uint8x16_t vout1x0123456789ABCDEF = vqmovun_high_s16(vqmovun_s16(vacc1x01234567), vacc1x89ABCDEF);
347     uint8x16_t vout1xGHIJKLMNOPQRSTUV = vqmovun_high_s16(vqmovun_s16(vacc1xGHIJKLMN), vacc1xOPQRSTUV);
348     uint8x16_t vout2x0123456789ABCDEF = vqmovun_high_s16(vqmovun_s16(vacc2x01234567), vacc2x89ABCDEF);
349     uint8x16_t vout2xGHIJKLMNOPQRSTUV = vqmovun_high_s16(vqmovun_s16(vacc2xGHIJKLMN), vacc2xOPQRSTUV);
350 #else
351     const int16x8_t vacc0x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc0x0123), vqmovn_s32(vacc0x4567)), voutput_zero_point);
352     const int16x8_t vacc0x89ABCDEF = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc0x89AB), vqmovn_s32(vacc0xCDEF)), voutput_zero_point);
353     const int16x8_t vacc0xGHIJKLMN = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc0xGHIJ), vqmovn_s32(vacc0xKLMN)), voutput_zero_point);
354     const int16x8_t vacc0xOPQRSTUV = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc0xOPQR), vqmovn_s32(vacc0xSTUV)), voutput_zero_point);
355     const int16x8_t vacc1x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc1x0123), vqmovn_s32(vacc1x4567)), voutput_zero_point);
356     const int16x8_t vacc1x89ABCDEF = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc1x89AB), vqmovn_s32(vacc1xCDEF)), voutput_zero_point);
357     const int16x8_t vacc1xGHIJKLMN = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc1xGHIJ), vqmovn_s32(vacc1xKLMN)), voutput_zero_point);
358     const int16x8_t vacc1xOPQRSTUV = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc1xOPQR), vqmovn_s32(vacc1xSTUV)), voutput_zero_point);
359     const int16x8_t vacc2x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc2x0123), vqmovn_s32(vacc2x4567)), voutput_zero_point);
360     const int16x8_t vacc2x89ABCDEF = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc2x89AB), vqmovn_s32(vacc2xCDEF)), voutput_zero_point);
361     const int16x8_t vacc2xGHIJKLMN = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc2xGHIJ), vqmovn_s32(vacc2xKLMN)), voutput_zero_point);
362     const int16x8_t vacc2xOPQRSTUV = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc2xOPQR), vqmovn_s32(vacc2xSTUV)), voutput_zero_point);
363 
364     uint8x16_t vout0x0123456789ABCDEF = vcombine_u8(vqmovun_s16(vacc0x01234567), vqmovun_s16(vacc0x89ABCDEF));
365     uint8x16_t vout0xGHIJKLMNOPQRSTUV = vcombine_u8(vqmovun_s16(vacc0xGHIJKLMN), vqmovun_s16(vacc0xOPQRSTUV));
366     uint8x16_t vout1x0123456789ABCDEF = vcombine_u8(vqmovun_s16(vacc1x01234567), vqmovun_s16(vacc1x89ABCDEF));
367     uint8x16_t vout1xGHIJKLMNOPQRSTUV = vcombine_u8(vqmovun_s16(vacc1xGHIJKLMN), vqmovun_s16(vacc1xOPQRSTUV));
368     uint8x16_t vout2x0123456789ABCDEF = vcombine_u8(vqmovun_s16(vacc2x01234567), vqmovun_s16(vacc2x89ABCDEF));
369     uint8x16_t vout2xGHIJKLMNOPQRSTUV = vcombine_u8(vqmovun_s16(vacc2xGHIJKLMN), vqmovun_s16(vacc2xOPQRSTUV));
370 #endif
371     const uint8x16_t voutput_min = vld1q_dup_u8(&params->rndnu_neon.output_min);
372     const uint8x16_t voutput_max = vld1q_dup_u8(&params->rndnu_neon.output_max);
373 
374     vout0x0123456789ABCDEF = vmaxq_u8(vout0x0123456789ABCDEF, voutput_min);
375     vout0xGHIJKLMNOPQRSTUV = vmaxq_u8(vout0xGHIJKLMNOPQRSTUV, voutput_min);
376     vout1x0123456789ABCDEF = vmaxq_u8(vout1x0123456789ABCDEF, voutput_min);
377     vout1xGHIJKLMNOPQRSTUV = vmaxq_u8(vout1xGHIJKLMNOPQRSTUV, voutput_min);
378     vout2x0123456789ABCDEF = vmaxq_u8(vout2x0123456789ABCDEF, voutput_min);
379     vout2xGHIJKLMNOPQRSTUV = vmaxq_u8(vout2xGHIJKLMNOPQRSTUV, voutput_min);
380 
381     vout0x0123456789ABCDEF = vminq_u8(vout0x0123456789ABCDEF, voutput_max);
382     vout0xGHIJKLMNOPQRSTUV = vminq_u8(vout0xGHIJKLMNOPQRSTUV, voutput_max);
383     vout1x0123456789ABCDEF = vminq_u8(vout1x0123456789ABCDEF, voutput_max);
384     vout1xGHIJKLMNOPQRSTUV = vminq_u8(vout1xGHIJKLMNOPQRSTUV, voutput_max);
385     vout2x0123456789ABCDEF = vminq_u8(vout2x0123456789ABCDEF, voutput_max);
386     vout2xGHIJKLMNOPQRSTUV = vminq_u8(vout2xGHIJKLMNOPQRSTUV, voutput_max);
387 
388     if (nc >= 32) {
389       vst1q_u8(c0 + 0, vout0x0123456789ABCDEF);
390       vst1q_u8(c0 + 16, vout0xGHIJKLMNOPQRSTUV);
391       vst1q_u8(c1 + 0, vout1x0123456789ABCDEF);
392       vst1q_u8(c1 + 16, vout1xGHIJKLMNOPQRSTUV);
393       vst1q_u8(c2 + 0, vout2x0123456789ABCDEF);
394       vst1q_u8(c2 + 16, vout2xGHIJKLMNOPQRSTUV);
395 
396       c0 = (uint8_t*) ((uintptr_t) c0 + cn_stride);
397       c1 = (uint8_t*) ((uintptr_t) c1 + cn_stride);
398       c2 = (uint8_t*) ((uintptr_t) c2 + cn_stride);
399 
400       a0 = (const uint8_t*) ((uintptr_t) a0 - kc);
401       a1 = (const uint8_t*) ((uintptr_t) a1 - kc);
402       a2 = (const uint8_t*) ((uintptr_t) a2 - kc);
403 
404       nc -= 32;
405     } else {
406       if (nc & 16) {
407         vst1q_u8(c0, vout0x0123456789ABCDEF);  c0 += 16;
408         vst1q_u8(c1, vout1x0123456789ABCDEF);  c1 += 16;
409         vst1q_u8(c2, vout2x0123456789ABCDEF);  c2 += 16;
410 
411         vout0x0123456789ABCDEF = vout0xGHIJKLMNOPQRSTUV;
412         vout1x0123456789ABCDEF = vout1xGHIJKLMNOPQRSTUV;
413         vout2x0123456789ABCDEF = vout2xGHIJKLMNOPQRSTUV;
414       }
415       uint8x16_t vout0x01234567_1x01234567 = vcombine_u8(vget_low_u8(vout0x0123456789ABCDEF), vget_low_u8(vout1x0123456789ABCDEF));
416       uint8x8_t vout2x01234567 = vget_low_u8(vout2x0123456789ABCDEF);
417       if (nc & 8) {
418         vst1_u8(c0, vget_low_u8(vout0x01234567_1x01234567)); c0 += 8;
419         vst1_u8(c1, vget_high_u8(vout0x01234567_1x01234567)); c1 += 8;
420         vst1_u8(c2, vout2x01234567); c2 += 8;
421         vout0x01234567_1x01234567 = vcombine_u8(vget_high_u8(vout0x0123456789ABCDEF), vget_high_u8(vout1x0123456789ABCDEF));
422         vout2x01234567 = vget_high_u8(vout2x0123456789ABCDEF);
423       }
424       if (nc & 4) {
425         vst1q_lane_u32((void*) c0, vreinterpretq_u32_u8(vout0x01234567_1x01234567), 0); c0 += 4;
426         vst1q_lane_u32((void*) c1, vreinterpretq_u32_u8(vout0x01234567_1x01234567), 2); c1 += 4;
427         vst1_lane_u32((void*) c2, vreinterpret_u32_u8(vout2x01234567), 0); c2 += 4;
428         vout0x01234567_1x01234567 = vextq_u8(vout0x01234567_1x01234567, vout0x01234567_1x01234567, 4);
429         vout2x01234567 = vext_u8(vout2x01234567, vout2x01234567, 4);
430       }
431       if (nc & 2) {
432         vst1q_lane_u16((void*) c0, vreinterpretq_u16_u8(vout0x01234567_1x01234567), 0); c0 += 2;
433         vst1q_lane_u16((void*) c1, vreinterpretq_u16_u8(vout0x01234567_1x01234567), 4); c1 += 2;
434         vst1_lane_u16((void*) c2, vreinterpret_u16_u8(vout2x01234567), 0); c2 += 2;
435         vout0x01234567_1x01234567 = vextq_u8(vout0x01234567_1x01234567, vout0x01234567_1x01234567, 2);
436         vout2x01234567 = vext_u8(vout2x01234567, vout2x01234567, 2);
437       }
438       if (nc & 1) {
439         vst1q_lane_u8(c0, vout0x01234567_1x01234567, 0);
440         vst1q_lane_u8(c1, vout0x01234567_1x01234567, 8);
441         vst1_lane_u8(c2, vout2x01234567, 0);
442       }
443 
444       nc = 0;
445     }
446   } while (nc != 0);
447 }
448