xref: /aosp_15_r20/external/XNNPACK/src/qs8-gemm/gen/1x16c2s4-minmax-rndnu-neon-mlal.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Auto-generated file. Do not edit!
2 //   Template: src/qs8-gemm/c2-neon-mull-shuffle.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 
17 
xnn_qs8_gemm_minmax_rndnu_ukernel_1x16c2s4__neon_mlal(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_1x16c2s4__neon_mlal(
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 <= 1);
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   const int8_t* a0 = a;
40   int8_t* c0 = c;
41 
42   kc = round_up_po2(kc, 8 * sizeof(int8_t));
43   do {
44     int32x4_t vacc0x0123 = vld1q_s32(w); w = (const int32_t*) w + 4;
45     int32x4_t vacc0x4567 = vld1q_s32(w); w = (const int32_t*) w + 4;
46     int32x4_t vacc0x89AB = vld1q_s32(w); w = (const int32_t*) w + 4;
47     int32x4_t vacc0xCDEF = vld1q_s32(w); w = (const int32_t*) w + 4;
48 
49     size_t k = kc;
50     while (k >= 16 * sizeof(int8_t)) {
51       int8x8_t va0x0 = vld1_s8(a0); a0 += 8;
52       int8x8_t va0x1 = vld1_s8(a0); a0 += 8;
53 
54       const int8x8_t vb0123c0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
55       const int8x8_t vb4567c0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
56       const int8x8_t vb89ABc0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
57       const int8x8_t vbCDEFc0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
58       const int8x8_t vb0123c1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
59       const int8x8_t vb4567c1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
60       const int8x8_t vb89ABc1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
61       const int8x8_t vbCDEFc1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
62       const int8x8_t vb0123c2x0 = vld1_s8(w); w = (const int8_t*) w + 8;
63       const int8x8_t vb4567c2x0 = vld1_s8(w); w = (const int8_t*) w + 8;
64       const int8x8_t vb89ABc2x0 = vld1_s8(w); w = (const int8_t*) w + 8;
65       const int8x8_t vbCDEFc2x0 = vld1_s8(w); w = (const int8_t*) w + 8;
66       const int8x8_t vb0123c3x0 = vld1_s8(w); w = (const int8_t*) w + 8;
67       const int8x8_t vb4567c3x0 = vld1_s8(w); w = (const int8_t*) w + 8;
68       const int8x8_t vb89ABc3x0 = vld1_s8(w); w = (const int8_t*) w + 8;
69       const int8x8_t vbCDEFc3x0 = vld1_s8(w); w = (const int8_t*) w + 8;
70 
71       int16x8_t vprod0x0123c0 = vmull_s8(vb0123c0x0, va0x0);
72       const int8x8_t vb0123c0x1 = vld1_s8(w); w = (const int8_t*) w + 8;
73       vprod0x0123c0 = vmlal_s8(vprod0x0123c0, vb0123c0x1, va0x1);
74       vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c0);
75       int16x8_t vprod0x4567c0 = vmull_s8(vb4567c0x0, va0x0);
76       const int8x8_t vb4567c0x1 = vld1_s8(w); w = (const int8_t*) w + 8;
77       vprod0x4567c0 = vmlal_s8(vprod0x4567c0, vb4567c0x1, va0x1);
78       vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c0);
79       int16x8_t vprod0x89ABc0 = vmull_s8(vb89ABc0x0, va0x0);
80       const int8x8_t vb89ABc0x1 = vld1_s8(w); w = (const int8_t*) w + 8;
81       vprod0x89ABc0 = vmlal_s8(vprod0x89ABc0, vb89ABc0x1, va0x1);
82       vacc0x89AB = vpadalq_s16(vacc0x89AB, vprod0x89ABc0);
83       int16x8_t vprod0xCDEFc0 = vmull_s8(vbCDEFc0x0, va0x0);
84       const int8x8_t vbCDEFc0x1 = vld1_s8(w); w = (const int8_t*) w + 8;
85       vprod0xCDEFc0 = vmlal_s8(vprod0xCDEFc0, vbCDEFc0x1, va0x1);
86       vacc0xCDEF = vpadalq_s16(vacc0xCDEF, vprod0xCDEFc0);
87       va0x0 = vext_s8(va0x0, va0x0, 2);
88       va0x1 = vext_s8(va0x1, va0x1, 2);
89       int16x8_t vprod0x0123c1 = vmull_s8(vb0123c1x0, va0x0);
90       const int8x8_t vb0123c1x1 = vld1_s8(w); w = (const int8_t*) w + 8;
91       vprod0x0123c1 = vmlal_s8(vprod0x0123c1, vb0123c1x1, va0x1);
92       vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c1);
93       int16x8_t vprod0x4567c1 = vmull_s8(vb4567c1x0, va0x0);
94       const int8x8_t vb4567c1x1 = vld1_s8(w); w = (const int8_t*) w + 8;
95       vprod0x4567c1 = vmlal_s8(vprod0x4567c1, vb4567c1x1, va0x1);
96       vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c1);
97       int16x8_t vprod0x89ABc1 = vmull_s8(vb89ABc1x0, va0x0);
98       const int8x8_t vb89ABc1x1 = vld1_s8(w); w = (const int8_t*) w + 8;
99       vprod0x89ABc1 = vmlal_s8(vprod0x89ABc1, vb89ABc1x1, va0x1);
100       vacc0x89AB = vpadalq_s16(vacc0x89AB, vprod0x89ABc1);
101       int16x8_t vprod0xCDEFc1 = vmull_s8(vbCDEFc1x0, va0x0);
102       const int8x8_t vbCDEFc1x1 = vld1_s8(w); w = (const int8_t*) w + 8;
103       vprod0xCDEFc1 = vmlal_s8(vprod0xCDEFc1, vbCDEFc1x1, va0x1);
104       vacc0xCDEF = vpadalq_s16(vacc0xCDEF, vprod0xCDEFc1);
105       va0x0 = vext_s8(va0x0, va0x0, 2);
106       va0x1 = vext_s8(va0x1, va0x1, 2);
107       int16x8_t vprod0x0123c2 = vmull_s8(vb0123c2x0, va0x0);
108       const int8x8_t vb0123c2x1 = vld1_s8(w); w = (const int8_t*) w + 8;
109       vprod0x0123c2 = vmlal_s8(vprod0x0123c2, vb0123c2x1, va0x1);
110       vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c2);
111       int16x8_t vprod0x4567c2 = vmull_s8(vb4567c2x0, va0x0);
112       const int8x8_t vb4567c2x1 = vld1_s8(w); w = (const int8_t*) w + 8;
113       vprod0x4567c2 = vmlal_s8(vprod0x4567c2, vb4567c2x1, va0x1);
114       vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c2);
115       int16x8_t vprod0x89ABc2 = vmull_s8(vb89ABc2x0, va0x0);
116       const int8x8_t vb89ABc2x1 = vld1_s8(w); w = (const int8_t*) w + 8;
117       vprod0x89ABc2 = vmlal_s8(vprod0x89ABc2, vb89ABc2x1, va0x1);
118       vacc0x89AB = vpadalq_s16(vacc0x89AB, vprod0x89ABc2);
119       int16x8_t vprod0xCDEFc2 = vmull_s8(vbCDEFc2x0, va0x0);
120       const int8x8_t vbCDEFc2x1 = vld1_s8(w); w = (const int8_t*) w + 8;
121       vprod0xCDEFc2 = vmlal_s8(vprod0xCDEFc2, vbCDEFc2x1, va0x1);
122       vacc0xCDEF = vpadalq_s16(vacc0xCDEF, vprod0xCDEFc2);
123       va0x0 = vext_s8(va0x0, va0x0, 2);
124       va0x1 = vext_s8(va0x1, va0x1, 2);
125       int16x8_t vprod0x0123c3 = vmull_s8(vb0123c3x0, va0x0);
126       const int8x8_t vb0123c3x1 = vld1_s8(w); w = (const int8_t*) w + 8;
127       vprod0x0123c3 = vmlal_s8(vprod0x0123c3, vb0123c3x1, va0x1);
128       vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c3);
129       int16x8_t vprod0x4567c3 = vmull_s8(vb4567c3x0, va0x0);
130       const int8x8_t vb4567c3x1 = vld1_s8(w); w = (const int8_t*) w + 8;
131       vprod0x4567c3 = vmlal_s8(vprod0x4567c3, vb4567c3x1, va0x1);
132       vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c3);
133       int16x8_t vprod0x89ABc3 = vmull_s8(vb89ABc3x0, va0x0);
134       const int8x8_t vb89ABc3x1 = vld1_s8(w); w = (const int8_t*) w + 8;
135       vprod0x89ABc3 = vmlal_s8(vprod0x89ABc3, vb89ABc3x1, va0x1);
136       vacc0x89AB = vpadalq_s16(vacc0x89AB, vprod0x89ABc3);
137       int16x8_t vprod0xCDEFc3 = vmull_s8(vbCDEFc3x0, va0x0);
138       const int8x8_t vbCDEFc3x1 = vld1_s8(w); w = (const int8_t*) w + 8;
139       vprod0xCDEFc3 = vmlal_s8(vprod0xCDEFc3, vbCDEFc3x1, va0x1);
140       vacc0xCDEF = vpadalq_s16(vacc0xCDEF, vprod0xCDEFc3);
141 
142       k -= 16 * sizeof(int8_t);
143     }
144     if (k != 0) {
145       int8x8_t va0x0 = vld1_s8(a0); a0 += 8;
146 
147       const int8x8_t vb0123c0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
148       const int8x8_t vb4567c0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
149       const int8x8_t vb89ABc0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
150       const int8x8_t vbCDEFc0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
151       const int8x8_t vb0123c1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
152       const int8x8_t vb4567c1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
153       const int8x8_t vb89ABc1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
154       const int8x8_t vbCDEFc1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
155       const int8x8_t vb0123c2x0 = vld1_s8(w); w = (const int8_t*) w + 8;
156       const int8x8_t vb4567c2x0 = vld1_s8(w); w = (const int8_t*) w + 8;
157       const int8x8_t vb89ABc2x0 = vld1_s8(w); w = (const int8_t*) w + 8;
158       const int8x8_t vbCDEFc2x0 = vld1_s8(w); w = (const int8_t*) w + 8;
159       const int8x8_t vb0123c3x0 = vld1_s8(w); w = (const int8_t*) w + 8;
160       const int8x8_t vb4567c3x0 = vld1_s8(w); w = (const int8_t*) w + 8;
161       const int8x8_t vb89ABc3x0 = vld1_s8(w); w = (const int8_t*) w + 8;
162       const int8x8_t vbCDEFc3x0 = vld1_s8(w); w = (const int8_t*) w + 8;
163 
164       int16x8_t vprod0x0123c0 = vmull_s8(vb0123c0x0, va0x0);
165       vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c0);
166       int16x8_t vprod0x4567c0 = vmull_s8(vb4567c0x0, va0x0);
167       vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c0);
168       int16x8_t vprod0x89ABc0 = vmull_s8(vb89ABc0x0, va0x0);
169       vacc0x89AB = vpadalq_s16(vacc0x89AB, vprod0x89ABc0);
170       int16x8_t vprod0xCDEFc0 = vmull_s8(vbCDEFc0x0, va0x0);
171       vacc0xCDEF = vpadalq_s16(vacc0xCDEF, vprod0xCDEFc0);
172       va0x0 = vext_s8(va0x0, va0x0, 2);
173       int16x8_t vprod0x0123c1 = vmull_s8(vb0123c1x0, va0x0);
174       vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c1);
175       int16x8_t vprod0x4567c1 = vmull_s8(vb4567c1x0, va0x0);
176       vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c1);
177       int16x8_t vprod0x89ABc1 = vmull_s8(vb89ABc1x0, va0x0);
178       vacc0x89AB = vpadalq_s16(vacc0x89AB, vprod0x89ABc1);
179       int16x8_t vprod0xCDEFc1 = vmull_s8(vbCDEFc1x0, va0x0);
180       vacc0xCDEF = vpadalq_s16(vacc0xCDEF, vprod0xCDEFc1);
181       va0x0 = vext_s8(va0x0, va0x0, 2);
182       int16x8_t vprod0x0123c2 = vmull_s8(vb0123c2x0, va0x0);
183       vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c2);
184       int16x8_t vprod0x4567c2 = vmull_s8(vb4567c2x0, va0x0);
185       vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c2);
186       int16x8_t vprod0x89ABc2 = vmull_s8(vb89ABc2x0, va0x0);
187       vacc0x89AB = vpadalq_s16(vacc0x89AB, vprod0x89ABc2);
188       int16x8_t vprod0xCDEFc2 = vmull_s8(vbCDEFc2x0, va0x0);
189       vacc0xCDEF = vpadalq_s16(vacc0xCDEF, vprod0xCDEFc2);
190       va0x0 = vext_s8(va0x0, va0x0, 2);
191       int16x8_t vprod0x0123c3 = vmull_s8(vb0123c3x0, va0x0);
192       vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c3);
193       int16x8_t vprod0x4567c3 = vmull_s8(vb4567c3x0, va0x0);
194       vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c3);
195       int16x8_t vprod0x89ABc3 = vmull_s8(vb89ABc3x0, va0x0);
196       vacc0x89AB = vpadalq_s16(vacc0x89AB, vprod0x89ABc3);
197       int16x8_t vprod0xCDEFc3 = vmull_s8(vbCDEFc3x0, va0x0);
198       vacc0xCDEF = vpadalq_s16(vacc0xCDEF, vprod0xCDEFc3);
199 
200     }
201 
202     const int32x4_t vright_pre_shift = vld1q_dup_s32(&params->rndnu_neon.right_pre_shift);
203     const int32x4_t vmultiplier = vld1q_dup_s32(&params->rndnu_neon.multiplier);
204     const int32x4_t vright_post_shift = vld1q_dup_s32(&params->rndnu_neon.right_post_shift);
205 
206     vacc0x0123 = vqshlq_s32(vacc0x0123, vright_pre_shift);
207     vacc0x4567 = vqshlq_s32(vacc0x4567, vright_pre_shift);
208     vacc0x89AB = vqshlq_s32(vacc0x89AB, vright_pre_shift);
209     vacc0xCDEF = vqshlq_s32(vacc0xCDEF, vright_pre_shift);
210 
211     vacc0x0123 = vqdmulhq_s32(vacc0x0123, vmultiplier);
212     vacc0x4567 = vqdmulhq_s32(vacc0x4567, vmultiplier);
213     vacc0x89AB = vqdmulhq_s32(vacc0x89AB, vmultiplier);
214     vacc0xCDEF = vqdmulhq_s32(vacc0xCDEF, vmultiplier);
215 
216     vacc0x0123 = vrshlq_s32(vacc0x0123, vright_post_shift);
217     vacc0x4567 = vrshlq_s32(vacc0x4567, vright_post_shift);
218     vacc0x89AB = vrshlq_s32(vacc0x89AB, vright_post_shift);
219     vacc0xCDEF = vrshlq_s32(vacc0xCDEF, vright_post_shift);
220 
221     const int16x8_t voutput_zero_point = vld1q_dup_s16(&params->rndnu_neon.output_zero_point);
222 #if XNN_ARCH_ARM64
223     int16x8_t vacc0x01234567 = vqmovn_high_s32(vqmovn_s32(vacc0x0123), vacc0x4567);
224     int16x8_t vacc0x89ABCDEF = vqmovn_high_s32(vqmovn_s32(vacc0x89AB), vacc0xCDEF);
225 
226     vacc0x01234567 = vqaddq_s16(vacc0x01234567, voutput_zero_point);
227     vacc0x89ABCDEF = vqaddq_s16(vacc0x89ABCDEF, voutput_zero_point);
228 
229     int8x16_t vout0x0123456789ABCDEF = vqmovn_high_s16(vqmovn_s16(vacc0x01234567), vacc0x89ABCDEF);
230 #else
231     int16x8_t vacc0x01234567 = vcombine_s16(vqmovn_s32(vacc0x0123), vqmovn_s32(vacc0x4567));
232     int16x8_t vacc0x89ABCDEF = vcombine_s16(vqmovn_s32(vacc0x89AB), vqmovn_s32(vacc0xCDEF));
233 
234     vacc0x01234567 = vqaddq_s16(vacc0x01234567, voutput_zero_point);
235     vacc0x89ABCDEF = vqaddq_s16(vacc0x89ABCDEF, voutput_zero_point);
236 
237     int8x16_t vout0x0123456789ABCDEF = vcombine_s8(vqmovn_s16(vacc0x01234567), vqmovn_s16(vacc0x89ABCDEF));
238 #endif
239 
240     const int8x16_t voutput_min = vld1q_dup_s8(&params->rndnu_neon.output_min);
241     vout0x0123456789ABCDEF = vmaxq_s8(vout0x0123456789ABCDEF, voutput_min);
242 
243     const int8x16_t voutput_max = vld1q_dup_s8(&params->rndnu_neon.output_max);
244     vout0x0123456789ABCDEF = vminq_s8(vout0x0123456789ABCDEF, voutput_max);
245 
246     if (nc >= 16) {
247       vst1q_s8(c0 + 0, vout0x0123456789ABCDEF);
248 
249       c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
250 
251       a0 = (const int8_t*) ((uintptr_t) a0 - kc);
252 
253       nc -= 16;
254     } else {
255       // Final case where not all of the 16 columns fit in the destination.
256       int8x8_t vout0x01234567 = vget_low_s8(vout0x0123456789ABCDEF);
257       if (nc & 8) {
258         vst1_s8(c0, vout0x01234567); c0 += 8;
259         vout0x01234567 = vget_high_s8(vout0x0123456789ABCDEF);
260       }
261       if (nc & 4) {
262         vst1_lane_u32((void*) c0, vreinterpret_u32_s8(vout0x01234567), 0); c0 += 4;
263         vout0x01234567 = vext_s8(vout0x01234567, vout0x01234567, 4);
264       }
265       if (nc & 2) {
266         vst1_lane_u16((void*) c0, vreinterpret_u16_s8(vout0x01234567), 0); c0 += 2;
267         vout0x01234567 = vext_s8(vout0x01234567, vout0x01234567, 2);
268       }
269       if (nc & 1) {
270         vst1_lane_s8(c0, vout0x01234567, 0);
271       }
272 
273       nc = 0;
274     }
275   } while (nc != 0);
276 }
277