xref: /aosp_15_r20/external/XNNPACK/src/qs8-gemm/gen/3x16c2-minmax-rndnu-neon-mull-ld2r.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Auto-generated file. Do not edit!
2 //   Template: src/qs8-gemm/c2-neon-mull-dup.c.in
3 //   Generator: tools/xngen
4 //
5 // Copyright 2021 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 
xnn_qs8_gemm_minmax_rndnu_ukernel_3x16c2__neon_mull_ld2r(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)])17 void xnn_qs8_gemm_minmax_rndnu_ukernel_3x16c2__neon_mull_ld2r(
18     size_t mr,
19     size_t nc,
20     size_t kc,
21     const int8_t* restrict a,
22     size_t a_stride,
23     const void* restrict w,
24     int8_t* restrict c,
25     size_t cm_stride,
26     size_t cn_stride,
27     const union xnn_qs8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
28 {
29   assert(mr != 0);
30   assert(mr <= 3);
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 * sizeof(int8_t));
39   const int8_t* a0 = a;
40   int8_t* c0 = c;
41   const int8_t* a1 = (const int8_t*) ((uintptr_t) a0 + a_stride);
42   int8_t* c1 = (int8_t*) ((uintptr_t) c0 + cm_stride);
43   if XNN_UNPREDICTABLE(mr < 2) {
44     a1 = a0;
45     c1 = c0;
46   }
47   const int8_t* a2 = (const int8_t*) ((uintptr_t) a1 + a_stride);
48   int8_t* c2 = (int8_t*) ((uintptr_t) c1 + cm_stride);
49   if XNN_UNPREDICTABLE(mr <= 2) {
50     a2 = a1;
51     c2 = c1;
52   }
53 
54   do {
55     int32x4_t vacc0x0123 = vld1q_s32(w); w = (const void*) ((uintptr_t) w + 4 * sizeof(int32_t));
56     int32x4_t vacc0x4567 = vld1q_s32(w); w = (const void*) ((uintptr_t) w + 4 * sizeof(int32_t));
57     int32x4_t vacc0x89AB = vld1q_s32(w); w = (const void*) ((uintptr_t) w + 4 * sizeof(int32_t));
58     int32x4_t vacc0xCDEF = vld1q_s32(w); w = (const void*) ((uintptr_t) w + 4 * sizeof(int32_t));
59     int32x4_t vacc1x0123 = vacc0x0123;
60     int32x4_t vacc1x4567 = vacc0x4567;
61     int32x4_t vacc1x89AB = vacc0x89AB;
62     int32x4_t vacc1xCDEF = vacc0xCDEF;
63     int32x4_t vacc2x0123 = vacc0x0123;
64     int32x4_t vacc2x4567 = vacc0x4567;
65     int32x4_t vacc2x89AB = vacc0x89AB;
66     int32x4_t vacc2xCDEF = vacc0xCDEF;
67 
68     size_t k = kc;
69 
70 
71     while (k >= 8 * sizeof(int8_t)) {
72       const int16x4x2_t va00 = vld2_dup_s16((const void*)a0);
73       const int16x4x2_t va01 = vld2_dup_s16((const void*)(a0 + 4)); a0 += 8;
74       const int16x4x2_t va10 = vld2_dup_s16((const void*)a1);
75       const int16x4x2_t va11 = vld2_dup_s16((const void*)(a1 + 4)); a1 += 8;
76       const int16x4x2_t va20 = vld2_dup_s16((const void*)a2);
77       const int16x4x2_t va21 = vld2_dup_s16((const void*)(a2 + 4)); a2 += 8;
78 
79       const int8x8_t vb0123c0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
80       const int8x8_t vb4567c0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
81       const int8x8_t vb89ABc0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
82       const int8x8_t vbCDEFc0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
83       const int8x8_t vb0123c1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
84       const int8x8_t vb4567c1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
85       const int8x8_t vb89ABc1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
86       const int8x8_t vbCDEFc1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
87       const int8x8_t vb0123c2 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
88       const int8x8_t vb4567c2 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
89       const int8x8_t vb89ABc2 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
90       const int8x8_t vbCDEFc2 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
91       const int8x8_t vb0123c3 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
92       const int8x8_t vb4567c3 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
93       const int8x8_t vb89ABc3 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
94       const int8x8_t vbCDEFc3 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
95 
96       const int8x8_t va0c0 = vreinterpret_s8_s16(va00.val[0]);
97       const int8x8_t va1c0 = vreinterpret_s8_s16(va10.val[0]);
98       const int8x8_t va2c0 = vreinterpret_s8_s16(va20.val[0]);
99 
100       const int16x8_t vprod0x0123c0 = vmull_s8(vb0123c0, va0c0);
101       const int16x8_t vprod1x0123c0 = vmull_s8(vb0123c0, va1c0);
102       const int16x8_t vprod2x0123c0 = vmull_s8(vb0123c0, va2c0);
103       vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c0);
104       vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c0);
105       vacc2x0123 = vpadalq_s16(vacc2x0123, vprod2x0123c0);
106       const int16x8_t vprod0x4567c0 = vmull_s8(vb4567c0, va0c0);
107       const int16x8_t vprod1x4567c0 = vmull_s8(vb4567c0, va1c0);
108       const int16x8_t vprod2x4567c0 = vmull_s8(vb4567c0, va2c0);
109       vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c0);
110       vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c0);
111       vacc2x4567 = vpadalq_s16(vacc2x4567, vprod2x4567c0);
112       const int16x8_t vprod0x89ABc0 = vmull_s8(vb89ABc0, va0c0);
113       const int16x8_t vprod1x89ABc0 = vmull_s8(vb89ABc0, va1c0);
114       const int16x8_t vprod2x89ABc0 = vmull_s8(vb89ABc0, va2c0);
115       vacc0x89AB = vpadalq_s16(vacc0x89AB, vprod0x89ABc0);
116       vacc1x89AB = vpadalq_s16(vacc1x89AB, vprod1x89ABc0);
117       vacc2x89AB = vpadalq_s16(vacc2x89AB, vprod2x89ABc0);
118       const int16x8_t vprod0xCDEFc0 = vmull_s8(vbCDEFc0, va0c0);
119       const int16x8_t vprod1xCDEFc0 = vmull_s8(vbCDEFc0, va1c0);
120       const int16x8_t vprod2xCDEFc0 = vmull_s8(vbCDEFc0, va2c0);
121       vacc0xCDEF = vpadalq_s16(vacc0xCDEF, vprod0xCDEFc0);
122       vacc1xCDEF = vpadalq_s16(vacc1xCDEF, vprod1xCDEFc0);
123       vacc2xCDEF = vpadalq_s16(vacc2xCDEF, vprod2xCDEFc0);
124       const int8x8_t va0c1 = vreinterpret_s8_s16(va00.val[1]);
125       const int8x8_t va1c1 = vreinterpret_s8_s16(va10.val[1]);
126       const int8x8_t va2c1 = vreinterpret_s8_s16(va20.val[1]);
127 
128       const int16x8_t vprod0x0123c1 = vmull_s8(vb0123c1, va0c1);
129       const int16x8_t vprod1x0123c1 = vmull_s8(vb0123c1, va1c1);
130       const int16x8_t vprod2x0123c1 = vmull_s8(vb0123c1, va2c1);
131       vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c1);
132       vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c1);
133       vacc2x0123 = vpadalq_s16(vacc2x0123, vprod2x0123c1);
134       const int16x8_t vprod0x4567c1 = vmull_s8(vb4567c1, va0c1);
135       const int16x8_t vprod1x4567c1 = vmull_s8(vb4567c1, va1c1);
136       const int16x8_t vprod2x4567c1 = vmull_s8(vb4567c1, va2c1);
137       vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c1);
138       vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c1);
139       vacc2x4567 = vpadalq_s16(vacc2x4567, vprod2x4567c1);
140       const int16x8_t vprod0x89ABc1 = vmull_s8(vb89ABc1, va0c1);
141       const int16x8_t vprod1x89ABc1 = vmull_s8(vb89ABc1, va1c1);
142       const int16x8_t vprod2x89ABc1 = vmull_s8(vb89ABc1, va2c1);
143       vacc0x89AB = vpadalq_s16(vacc0x89AB, vprod0x89ABc1);
144       vacc1x89AB = vpadalq_s16(vacc1x89AB, vprod1x89ABc1);
145       vacc2x89AB = vpadalq_s16(vacc2x89AB, vprod2x89ABc1);
146       const int16x8_t vprod0xCDEFc1 = vmull_s8(vbCDEFc1, va0c1);
147       const int16x8_t vprod1xCDEFc1 = vmull_s8(vbCDEFc1, va1c1);
148       const int16x8_t vprod2xCDEFc1 = vmull_s8(vbCDEFc1, va2c1);
149       vacc0xCDEF = vpadalq_s16(vacc0xCDEF, vprod0xCDEFc1);
150       vacc1xCDEF = vpadalq_s16(vacc1xCDEF, vprod1xCDEFc1);
151       vacc2xCDEF = vpadalq_s16(vacc2xCDEF, vprod2xCDEFc1);
152       const int8x8_t va0c2 = vreinterpret_s8_s16(va01.val[0]);
153       const int8x8_t va1c2 = vreinterpret_s8_s16(va11.val[0]);
154       const int8x8_t va2c2 = vreinterpret_s8_s16(va21.val[0]);
155 
156       const int16x8_t vprod0x0123c2 = vmull_s8(vb0123c2, va0c2);
157       const int16x8_t vprod1x0123c2 = vmull_s8(vb0123c2, va1c2);
158       const int16x8_t vprod2x0123c2 = vmull_s8(vb0123c2, va2c2);
159       vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c2);
160       vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c2);
161       vacc2x0123 = vpadalq_s16(vacc2x0123, vprod2x0123c2);
162       const int16x8_t vprod0x4567c2 = vmull_s8(vb4567c2, va0c2);
163       const int16x8_t vprod1x4567c2 = vmull_s8(vb4567c2, va1c2);
164       const int16x8_t vprod2x4567c2 = vmull_s8(vb4567c2, va2c2);
165       vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c2);
166       vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c2);
167       vacc2x4567 = vpadalq_s16(vacc2x4567, vprod2x4567c2);
168       const int16x8_t vprod0x89ABc2 = vmull_s8(vb89ABc2, va0c2);
169       const int16x8_t vprod1x89ABc2 = vmull_s8(vb89ABc2, va1c2);
170       const int16x8_t vprod2x89ABc2 = vmull_s8(vb89ABc2, va2c2);
171       vacc0x89AB = vpadalq_s16(vacc0x89AB, vprod0x89ABc2);
172       vacc1x89AB = vpadalq_s16(vacc1x89AB, vprod1x89ABc2);
173       vacc2x89AB = vpadalq_s16(vacc2x89AB, vprod2x89ABc2);
174       const int16x8_t vprod0xCDEFc2 = vmull_s8(vbCDEFc2, va0c2);
175       const int16x8_t vprod1xCDEFc2 = vmull_s8(vbCDEFc2, va1c2);
176       const int16x8_t vprod2xCDEFc2 = vmull_s8(vbCDEFc2, va2c2);
177       vacc0xCDEF = vpadalq_s16(vacc0xCDEF, vprod0xCDEFc2);
178       vacc1xCDEF = vpadalq_s16(vacc1xCDEF, vprod1xCDEFc2);
179       vacc2xCDEF = vpadalq_s16(vacc2xCDEF, vprod2xCDEFc2);
180       const int8x8_t va0c3 = vreinterpret_s8_s16(va01.val[1]);
181       const int8x8_t va1c3 = vreinterpret_s8_s16(va11.val[1]);
182       const int8x8_t va2c3 = vreinterpret_s8_s16(va21.val[1]);
183 
184       const int16x8_t vprod0x0123c3 = vmull_s8(vb0123c3, va0c3);
185       const int16x8_t vprod1x0123c3 = vmull_s8(vb0123c3, va1c3);
186       const int16x8_t vprod2x0123c3 = vmull_s8(vb0123c3, va2c3);
187       vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c3);
188       vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c3);
189       vacc2x0123 = vpadalq_s16(vacc2x0123, vprod2x0123c3);
190       const int16x8_t vprod0x4567c3 = vmull_s8(vb4567c3, va0c3);
191       const int16x8_t vprod1x4567c3 = vmull_s8(vb4567c3, va1c3);
192       const int16x8_t vprod2x4567c3 = vmull_s8(vb4567c3, va2c3);
193       vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c3);
194       vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c3);
195       vacc2x4567 = vpadalq_s16(vacc2x4567, vprod2x4567c3);
196       const int16x8_t vprod0x89ABc3 = vmull_s8(vb89ABc3, va0c3);
197       const int16x8_t vprod1x89ABc3 = vmull_s8(vb89ABc3, va1c3);
198       const int16x8_t vprod2x89ABc3 = vmull_s8(vb89ABc3, va2c3);
199       vacc0x89AB = vpadalq_s16(vacc0x89AB, vprod0x89ABc3);
200       vacc1x89AB = vpadalq_s16(vacc1x89AB, vprod1x89ABc3);
201       vacc2x89AB = vpadalq_s16(vacc2x89AB, vprod2x89ABc3);
202       const int16x8_t vprod0xCDEFc3 = vmull_s8(vbCDEFc3, va0c3);
203       const int16x8_t vprod1xCDEFc3 = vmull_s8(vbCDEFc3, va1c3);
204       const int16x8_t vprod2xCDEFc3 = vmull_s8(vbCDEFc3, va2c3);
205       vacc0xCDEF = vpadalq_s16(vacc0xCDEF, vprod0xCDEFc3);
206       vacc1xCDEF = vpadalq_s16(vacc1xCDEF, vprod1xCDEFc3);
207       vacc2xCDEF = vpadalq_s16(vacc2xCDEF, vprod2xCDEFc3);
208 
209       k -= 8 * sizeof(int8_t);
210     }
211 
212     if XNN_UNLIKELY(k != 0) {
213       const int8x8_t va0 = vld1_s8(a0); a0 = (const int8_t*) ((uintptr_t) a0 + k);
214       const int8x8_t va1 = vld1_s8(a1); a1 = (const int8_t*) ((uintptr_t) a1 + k);
215       const int8x8_t va2 = vld1_s8(a2); a2 = (const int8_t*) ((uintptr_t) a2 + k);
216 
217       const int8x8_t vb0123c0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
218       const int8x8_t vb4567c0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
219       const int8x8_t vb89ABc0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
220       const int8x8_t vbCDEFc0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
221 
222       const int8x8_t va0c0 = vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(va0), 0));
223       const int16x8_t vprod0x0123c0 = vmull_s8(vb0123c0, va0c0);
224       vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c0);
225       const int16x8_t vprod0x4567c0 = vmull_s8(vb4567c0, va0c0);
226       vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c0);
227       const int16x8_t vprod0x89ABc0 = vmull_s8(vb89ABc0, va0c0);
228       vacc0x89AB = vpadalq_s16(vacc0x89AB, vprod0x89ABc0);
229       const int16x8_t vprod0xCDEFc0 = vmull_s8(vbCDEFc0, va0c0);
230       vacc0xCDEF = vpadalq_s16(vacc0xCDEF, vprod0xCDEFc0);
231       const int8x8_t va1c0 = vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(va1), 0));
232       const int16x8_t vprod1x0123c0 = vmull_s8(vb0123c0, va1c0);
233       vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c0);
234       const int16x8_t vprod1x4567c0 = vmull_s8(vb4567c0, va1c0);
235       vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c0);
236       const int16x8_t vprod1x89ABc0 = vmull_s8(vb89ABc0, va1c0);
237       vacc1x89AB = vpadalq_s16(vacc1x89AB, vprod1x89ABc0);
238       const int16x8_t vprod1xCDEFc0 = vmull_s8(vbCDEFc0, va1c0);
239       vacc1xCDEF = vpadalq_s16(vacc1xCDEF, vprod1xCDEFc0);
240       const int8x8_t va2c0 = vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(va2), 0));
241       const int16x8_t vprod2x0123c0 = vmull_s8(vb0123c0, va2c0);
242       vacc2x0123 = vpadalq_s16(vacc2x0123, vprod2x0123c0);
243       const int16x8_t vprod2x4567c0 = vmull_s8(vb4567c0, va2c0);
244       vacc2x4567 = vpadalq_s16(vacc2x4567, vprod2x4567c0);
245       const int16x8_t vprod2x89ABc0 = vmull_s8(vb89ABc0, va2c0);
246       vacc2x89AB = vpadalq_s16(vacc2x89AB, vprod2x89ABc0);
247       const int16x8_t vprod2xCDEFc0 = vmull_s8(vbCDEFc0, va2c0);
248       vacc2xCDEF = vpadalq_s16(vacc2xCDEF, vprod2xCDEFc0);
249 
250       if (k > 2 * sizeof(int8_t)) {
251         const int8x8_t vb0123c1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
252         const int8x8_t vb4567c1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
253         const int8x8_t vb89ABc1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
254         const int8x8_t vbCDEFc1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
255 
256         const int8x8_t va0c1 = vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(va0), 1));
257         const int16x8_t vprod0x0123c1 = vmull_s8(vb0123c1, va0c1);
258         vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c1);
259         const int16x8_t vprod0x4567c1 = vmull_s8(vb4567c1, va0c1);
260         vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c1);
261         const int16x8_t vprod0x89ABc1 = vmull_s8(vb89ABc1, va0c1);
262         vacc0x89AB = vpadalq_s16(vacc0x89AB, vprod0x89ABc1);
263         const int16x8_t vprod0xCDEFc1 = vmull_s8(vbCDEFc1, va0c1);
264         vacc0xCDEF = vpadalq_s16(vacc0xCDEF, vprod0xCDEFc1);
265         const int8x8_t va1c1 = vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(va1), 1));
266         const int16x8_t vprod1x0123c1 = vmull_s8(vb0123c1, va1c1);
267         vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c1);
268         const int16x8_t vprod1x4567c1 = vmull_s8(vb4567c1, va1c1);
269         vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c1);
270         const int16x8_t vprod1x89ABc1 = vmull_s8(vb89ABc1, va1c1);
271         vacc1x89AB = vpadalq_s16(vacc1x89AB, vprod1x89ABc1);
272         const int16x8_t vprod1xCDEFc1 = vmull_s8(vbCDEFc1, va1c1);
273         vacc1xCDEF = vpadalq_s16(vacc1xCDEF, vprod1xCDEFc1);
274         const int8x8_t va2c1 = vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(va2), 1));
275         const int16x8_t vprod2x0123c1 = vmull_s8(vb0123c1, va2c1);
276         vacc2x0123 = vpadalq_s16(vacc2x0123, vprod2x0123c1);
277         const int16x8_t vprod2x4567c1 = vmull_s8(vb4567c1, va2c1);
278         vacc2x4567 = vpadalq_s16(vacc2x4567, vprod2x4567c1);
279         const int16x8_t vprod2x89ABc1 = vmull_s8(vb89ABc1, va2c1);
280         vacc2x89AB = vpadalq_s16(vacc2x89AB, vprod2x89ABc1);
281         const int16x8_t vprod2xCDEFc1 = vmull_s8(vbCDEFc1, va2c1);
282         vacc2xCDEF = vpadalq_s16(vacc2xCDEF, vprod2xCDEFc1);
283 
284         if (k > 4 * sizeof(int8_t)) {
285           const int8x8_t vb0123c2 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
286           const int8x8_t vb4567c2 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
287           const int8x8_t vb89ABc2 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
288           const int8x8_t vbCDEFc2 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
289 
290           const int8x8_t va0c2 = vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(va0), 2));
291           const int16x8_t vprod0x0123c2 = vmull_s8(vb0123c2, va0c2);
292           vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c2);
293           const int16x8_t vprod0x4567c2 = vmull_s8(vb4567c2, va0c2);
294           vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c2);
295           const int16x8_t vprod0x89ABc2 = vmull_s8(vb89ABc2, va0c2);
296           vacc0x89AB = vpadalq_s16(vacc0x89AB, vprod0x89ABc2);
297           const int16x8_t vprod0xCDEFc2 = vmull_s8(vbCDEFc2, va0c2);
298           vacc0xCDEF = vpadalq_s16(vacc0xCDEF, vprod0xCDEFc2);
299           const int8x8_t va1c2 = vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(va1), 2));
300           const int16x8_t vprod1x0123c2 = vmull_s8(vb0123c2, va1c2);
301           vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c2);
302           const int16x8_t vprod1x4567c2 = vmull_s8(vb4567c2, va1c2);
303           vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c2);
304           const int16x8_t vprod1x89ABc2 = vmull_s8(vb89ABc2, va1c2);
305           vacc1x89AB = vpadalq_s16(vacc1x89AB, vprod1x89ABc2);
306           const int16x8_t vprod1xCDEFc2 = vmull_s8(vbCDEFc2, va1c2);
307           vacc1xCDEF = vpadalq_s16(vacc1xCDEF, vprod1xCDEFc2);
308           const int8x8_t va2c2 = vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(va2), 2));
309           const int16x8_t vprod2x0123c2 = vmull_s8(vb0123c2, va2c2);
310           vacc2x0123 = vpadalq_s16(vacc2x0123, vprod2x0123c2);
311           const int16x8_t vprod2x4567c2 = vmull_s8(vb4567c2, va2c2);
312           vacc2x4567 = vpadalq_s16(vacc2x4567, vprod2x4567c2);
313           const int16x8_t vprod2x89ABc2 = vmull_s8(vb89ABc2, va2c2);
314           vacc2x89AB = vpadalq_s16(vacc2x89AB, vprod2x89ABc2);
315           const int16x8_t vprod2xCDEFc2 = vmull_s8(vbCDEFc2, va2c2);
316           vacc2xCDEF = vpadalq_s16(vacc2xCDEF, vprod2xCDEFc2);
317         }
318       }
319     }
320 
321     const int32x4_t vright_pre_shift = vld1q_dup_s32(&params->rndnu_neon.right_pre_shift);
322     const int32x4_t vmultiplier = vld1q_dup_s32(&params->rndnu_neon.multiplier);
323     const int32x4_t vright_post_shift = vld1q_dup_s32(&params->rndnu_neon.right_post_shift);
324 
325     vacc0x0123 = vqshlq_s32(vacc0x0123, vright_pre_shift);
326     vacc0x4567 = vqshlq_s32(vacc0x4567, vright_pre_shift);
327     vacc0x89AB = vqshlq_s32(vacc0x89AB, vright_pre_shift);
328     vacc0xCDEF = vqshlq_s32(vacc0xCDEF, vright_pre_shift);
329     vacc1x0123 = vqshlq_s32(vacc1x0123, vright_pre_shift);
330     vacc1x4567 = vqshlq_s32(vacc1x4567, vright_pre_shift);
331     vacc1x89AB = vqshlq_s32(vacc1x89AB, vright_pre_shift);
332     vacc1xCDEF = vqshlq_s32(vacc1xCDEF, vright_pre_shift);
333     vacc2x0123 = vqshlq_s32(vacc2x0123, vright_pre_shift);
334     vacc2x4567 = vqshlq_s32(vacc2x4567, vright_pre_shift);
335     vacc2x89AB = vqshlq_s32(vacc2x89AB, vright_pre_shift);
336     vacc2xCDEF = vqshlq_s32(vacc2xCDEF, vright_pre_shift);
337 
338     vacc0x0123 = vqdmulhq_s32(vacc0x0123, vmultiplier);
339     vacc0x4567 = vqdmulhq_s32(vacc0x4567, vmultiplier);
340     vacc0x89AB = vqdmulhq_s32(vacc0x89AB, vmultiplier);
341     vacc0xCDEF = vqdmulhq_s32(vacc0xCDEF, vmultiplier);
342     vacc1x0123 = vqdmulhq_s32(vacc1x0123, vmultiplier);
343     vacc1x4567 = vqdmulhq_s32(vacc1x4567, vmultiplier);
344     vacc1x89AB = vqdmulhq_s32(vacc1x89AB, vmultiplier);
345     vacc1xCDEF = vqdmulhq_s32(vacc1xCDEF, vmultiplier);
346     vacc2x0123 = vqdmulhq_s32(vacc2x0123, vmultiplier);
347     vacc2x4567 = vqdmulhq_s32(vacc2x4567, vmultiplier);
348     vacc2x89AB = vqdmulhq_s32(vacc2x89AB, vmultiplier);
349     vacc2xCDEF = vqdmulhq_s32(vacc2xCDEF, vmultiplier);
350 
351     vacc0x0123 = vrshlq_s32(vacc0x0123, vright_post_shift);
352     vacc0x4567 = vrshlq_s32(vacc0x4567, vright_post_shift);
353     vacc0x89AB = vrshlq_s32(vacc0x89AB, vright_post_shift);
354     vacc0xCDEF = vrshlq_s32(vacc0xCDEF, vright_post_shift);
355     vacc1x0123 = vrshlq_s32(vacc1x0123, vright_post_shift);
356     vacc1x4567 = vrshlq_s32(vacc1x4567, vright_post_shift);
357     vacc1x89AB = vrshlq_s32(vacc1x89AB, vright_post_shift);
358     vacc1xCDEF = vrshlq_s32(vacc1xCDEF, vright_post_shift);
359     vacc2x0123 = vrshlq_s32(vacc2x0123, vright_post_shift);
360     vacc2x4567 = vrshlq_s32(vacc2x4567, vright_post_shift);
361     vacc2x89AB = vrshlq_s32(vacc2x89AB, vright_post_shift);
362     vacc2xCDEF = vrshlq_s32(vacc2xCDEF, vright_post_shift);
363 
364     const int16x8_t voutput_zero_point = vld1q_dup_s16(&params->rndnu_neon.output_zero_point);
365 #if XNN_ARCH_ARM64
366     int16x8_t vacc0x01234567 = vqmovn_high_s32(vqmovn_s32(vacc0x0123), vacc0x4567);
367     int16x8_t vacc0x89ABCDEF = vqmovn_high_s32(vqmovn_s32(vacc0x89AB), vacc0xCDEF);
368     int16x8_t vacc1x01234567 = vqmovn_high_s32(vqmovn_s32(vacc1x0123), vacc1x4567);
369     int16x8_t vacc1x89ABCDEF = vqmovn_high_s32(vqmovn_s32(vacc1x89AB), vacc1xCDEF);
370     int16x8_t vacc2x01234567 = vqmovn_high_s32(vqmovn_s32(vacc2x0123), vacc2x4567);
371     int16x8_t vacc2x89ABCDEF = vqmovn_high_s32(vqmovn_s32(vacc2x89AB), vacc2xCDEF);
372 
373     vacc0x01234567 = vqaddq_s16(vacc0x01234567, voutput_zero_point);
374     vacc0x89ABCDEF = vqaddq_s16(vacc0x89ABCDEF, voutput_zero_point);
375     vacc1x01234567 = vqaddq_s16(vacc1x01234567, voutput_zero_point);
376     vacc1x89ABCDEF = vqaddq_s16(vacc1x89ABCDEF, voutput_zero_point);
377     vacc2x01234567 = vqaddq_s16(vacc2x01234567, voutput_zero_point);
378     vacc2x89ABCDEF = vqaddq_s16(vacc2x89ABCDEF, voutput_zero_point);
379 
380     int8x16_t vout0x0123456789ABCDEF = vqmovn_high_s16(vqmovn_s16(vacc0x01234567), vacc0x89ABCDEF);
381     int8x16_t vout1x0123456789ABCDEF = vqmovn_high_s16(vqmovn_s16(vacc1x01234567), vacc1x89ABCDEF);
382     int8x16_t vout2x0123456789ABCDEF = vqmovn_high_s16(vqmovn_s16(vacc2x01234567), vacc2x89ABCDEF);
383 #else
384     int16x8_t vacc0x01234567 = vcombine_s16(vqmovn_s32(vacc0x0123), vqmovn_s32(vacc0x4567));
385     int16x8_t vacc0x89ABCDEF = vcombine_s16(vqmovn_s32(vacc0x89AB), vqmovn_s32(vacc0xCDEF));
386     int16x8_t vacc1x01234567 = vcombine_s16(vqmovn_s32(vacc1x0123), vqmovn_s32(vacc1x4567));
387     int16x8_t vacc1x89ABCDEF = vcombine_s16(vqmovn_s32(vacc1x89AB), vqmovn_s32(vacc1xCDEF));
388     int16x8_t vacc2x01234567 = vcombine_s16(vqmovn_s32(vacc2x0123), vqmovn_s32(vacc2x4567));
389     int16x8_t vacc2x89ABCDEF = vcombine_s16(vqmovn_s32(vacc2x89AB), vqmovn_s32(vacc2xCDEF));
390 
391     vacc0x01234567 = vqaddq_s16(vacc0x01234567, voutput_zero_point);
392     vacc0x89ABCDEF = vqaddq_s16(vacc0x89ABCDEF, voutput_zero_point);
393     vacc1x01234567 = vqaddq_s16(vacc1x01234567, voutput_zero_point);
394     vacc1x89ABCDEF = vqaddq_s16(vacc1x89ABCDEF, voutput_zero_point);
395     vacc2x01234567 = vqaddq_s16(vacc2x01234567, voutput_zero_point);
396     vacc2x89ABCDEF = vqaddq_s16(vacc2x89ABCDEF, voutput_zero_point);
397 
398     int8x16_t vout0x0123456789ABCDEF = vcombine_s8(vqmovn_s16(vacc0x01234567), vqmovn_s16(vacc0x89ABCDEF));
399     int8x16_t vout1x0123456789ABCDEF = vcombine_s8(vqmovn_s16(vacc1x01234567), vqmovn_s16(vacc1x89ABCDEF));
400     int8x16_t vout2x0123456789ABCDEF = vcombine_s8(vqmovn_s16(vacc2x01234567), vqmovn_s16(vacc2x89ABCDEF));
401 #endif
402 
403     const int8x16_t voutput_min = vld1q_dup_s8(&params->rndnu_neon.output_min);
404     vout0x0123456789ABCDEF = vmaxq_s8(vout0x0123456789ABCDEF, voutput_min);
405     vout1x0123456789ABCDEF = vmaxq_s8(vout1x0123456789ABCDEF, voutput_min);
406     vout2x0123456789ABCDEF = vmaxq_s8(vout2x0123456789ABCDEF, voutput_min);
407 
408     const int8x16_t voutput_max = vld1q_dup_s8(&params->rndnu_neon.output_max);
409     vout0x0123456789ABCDEF = vminq_s8(vout0x0123456789ABCDEF, voutput_max);
410     vout1x0123456789ABCDEF = vminq_s8(vout1x0123456789ABCDEF, voutput_max);
411     vout2x0123456789ABCDEF = vminq_s8(vout2x0123456789ABCDEF, voutput_max);
412 
413     if (nc >= 16) {
414       vst1q_s8(c0 + 0, vout0x0123456789ABCDEF);
415       vst1q_s8(c1 + 0, vout1x0123456789ABCDEF);
416       vst1q_s8(c2 + 0, vout2x0123456789ABCDEF);
417 
418       c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
419       c1 = (int8_t*) ((uintptr_t) c1 + cn_stride);
420       c2 = (int8_t*) ((uintptr_t) c2 + cn_stride);
421 
422       a0 = (const int8_t*) ((uintptr_t) a0 - kc);
423       a1 = (const int8_t*) ((uintptr_t) a1 - kc);
424       a2 = (const int8_t*) ((uintptr_t) a2 - kc);
425 
426       nc -= 16;
427     } else {
428       // Final case where not all of the 16 columns fit in the destination.
429       int8x16_t vout0x01234567_1x01234567 = vcombine_s8(vget_low_s8(vout0x0123456789ABCDEF), vget_low_s8(vout1x0123456789ABCDEF));
430       int8x8_t vout2x01234567 = vget_low_s8(vout2x0123456789ABCDEF);
431       if (nc & 8) {
432         vst1_s8(c0, vget_low_s8(vout0x01234567_1x01234567)); c0 += 8;
433         vst1_s8(c1, vget_high_s8(vout0x01234567_1x01234567)); c1 += 8;
434         vst1_s8(c2, vout2x01234567); c2 += 8;
435         vout0x01234567_1x01234567 = vcombine_s8(vget_high_s8(vout0x0123456789ABCDEF), vget_high_s8(vout1x0123456789ABCDEF));
436         vout2x01234567 = vget_high_s8(vout2x0123456789ABCDEF);
437       }
438       if (nc & 4) {
439         vst1q_lane_u32((void*) c0, vreinterpretq_u32_s8(vout0x01234567_1x01234567), 0); c0 += 4;
440         vst1q_lane_u32((void*) c1, vreinterpretq_u32_s8(vout0x01234567_1x01234567), 2); c1 += 4;
441         vst1_lane_u32((void*) c2, vreinterpret_u32_s8(vout2x01234567), 0); c2 += 4;
442         vout0x01234567_1x01234567 = vextq_s8(vout0x01234567_1x01234567, vout0x01234567_1x01234567, 4);
443         vout2x01234567 = vext_s8(vout2x01234567, vout2x01234567, 4);
444       }
445       if (nc & 2) {
446         vst1q_lane_u16((void*) c0, vreinterpretq_u16_s8(vout0x01234567_1x01234567), 0); c0 += 2;
447         vst1q_lane_u16((void*) c1, vreinterpretq_u16_s8(vout0x01234567_1x01234567), 4); c1 += 2;
448         vst1_lane_u16((void*) c2, vreinterpret_u16_s8(vout2x01234567), 0); c2 += 2;
449         vout0x01234567_1x01234567 = vextq_s8(vout0x01234567_1x01234567, vout0x01234567_1x01234567, 2);
450         vout2x01234567 = vext_s8(vout2x01234567, vout2x01234567, 2);
451       }
452       if (nc & 1) {
453         vst1q_lane_s8(c0, vout0x01234567_1x01234567, 0);
454         vst1q_lane_s8(c1, vout0x01234567_1x01234567, 8);
455         vst1_lane_s8(c2, vout2x01234567, 0);
456       }
457 
458       nc = 0;
459     }
460   } while (nc != 0);
461 }
462