xref: /aosp_15_r20/external/XNNPACK/src/qs8-gemm/gen/2x16c2s4-minmax-rndnu-neon-mull.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_2x16c2s4__neon_mull(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_2x16c2s4__neon_mull(
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 <= 2);
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   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 
48   kc = round_up_po2(kc, 8 * sizeof(int8_t));
49   do {
50     int32x4_t vacc0x0123 = vld1q_s32(w); w = (const int32_t*) w + 4;
51     int32x4_t vacc0x4567 = vld1q_s32(w); w = (const int32_t*) w + 4;
52     int32x4_t vacc0x89AB = vld1q_s32(w); w = (const int32_t*) w + 4;
53     int32x4_t vacc0xCDEF = vld1q_s32(w); w = (const int32_t*) w + 4;
54     int32x4_t vacc1x0123 = vacc0x0123;
55     int32x4_t vacc1x4567 = vacc0x4567;
56     int32x4_t vacc1x89AB = vacc0x89AB;
57     int32x4_t vacc1xCDEF = vacc0xCDEF;
58 
59     size_t k = kc;
60     do {
61       int8x8_t va0x0 = vld1_s8(a0); a0 += 8;
62       int8x8_t va1x0 = vld1_s8(a1); a1 += 8;
63 
64       const int8x8_t vb0123c0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
65       const int8x8_t vb4567c0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
66       const int8x8_t vb89ABc0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
67       const int8x8_t vbCDEFc0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
68       const int8x8_t vb0123c1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
69       const int8x8_t vb4567c1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
70       const int8x8_t vb89ABc1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
71       const int8x8_t vbCDEFc1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
72       const int8x8_t vb0123c2x0 = vld1_s8(w); w = (const int8_t*) w + 8;
73       const int8x8_t vb4567c2x0 = vld1_s8(w); w = (const int8_t*) w + 8;
74       const int8x8_t vb89ABc2x0 = vld1_s8(w); w = (const int8_t*) w + 8;
75       const int8x8_t vbCDEFc2x0 = vld1_s8(w); w = (const int8_t*) w + 8;
76       const int8x8_t vb0123c3x0 = vld1_s8(w); w = (const int8_t*) w + 8;
77       const int8x8_t vb4567c3x0 = vld1_s8(w); w = (const int8_t*) w + 8;
78       const int8x8_t vb89ABc3x0 = vld1_s8(w); w = (const int8_t*) w + 8;
79       const int8x8_t vbCDEFc3x0 = vld1_s8(w); w = (const int8_t*) w + 8;
80 
81       int16x8_t vprod0x0123c0 = vmull_s8(vb0123c0x0, va0x0);
82       int16x8_t vprod1x0123c0 = vmull_s8(vb0123c0x0, va1x0);
83       vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c0);
84       vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c0);
85       int16x8_t vprod0x4567c0 = vmull_s8(vb4567c0x0, va0x0);
86       int16x8_t vprod1x4567c0 = vmull_s8(vb4567c0x0, va1x0);
87       vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c0);
88       vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c0);
89       int16x8_t vprod0x89ABc0 = vmull_s8(vb89ABc0x0, va0x0);
90       int16x8_t vprod1x89ABc0 = vmull_s8(vb89ABc0x0, va1x0);
91       vacc0x89AB = vpadalq_s16(vacc0x89AB, vprod0x89ABc0);
92       vacc1x89AB = vpadalq_s16(vacc1x89AB, vprod1x89ABc0);
93       int16x8_t vprod0xCDEFc0 = vmull_s8(vbCDEFc0x0, va0x0);
94       int16x8_t vprod1xCDEFc0 = vmull_s8(vbCDEFc0x0, va1x0);
95       vacc0xCDEF = vpadalq_s16(vacc0xCDEF, vprod0xCDEFc0);
96       vacc1xCDEF = vpadalq_s16(vacc1xCDEF, vprod1xCDEFc0);
97       va0x0 = vext_s8(va0x0, va0x0, 2);
98       va1x0 = vext_s8(va1x0, va1x0, 2);
99       int16x8_t vprod0x0123c1 = vmull_s8(vb0123c1x0, va0x0);
100       int16x8_t vprod1x0123c1 = vmull_s8(vb0123c1x0, va1x0);
101       vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c1);
102       vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c1);
103       int16x8_t vprod0x4567c1 = vmull_s8(vb4567c1x0, va0x0);
104       int16x8_t vprod1x4567c1 = vmull_s8(vb4567c1x0, va1x0);
105       vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c1);
106       vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c1);
107       int16x8_t vprod0x89ABc1 = vmull_s8(vb89ABc1x0, va0x0);
108       int16x8_t vprod1x89ABc1 = vmull_s8(vb89ABc1x0, va1x0);
109       vacc0x89AB = vpadalq_s16(vacc0x89AB, vprod0x89ABc1);
110       vacc1x89AB = vpadalq_s16(vacc1x89AB, vprod1x89ABc1);
111       int16x8_t vprod0xCDEFc1 = vmull_s8(vbCDEFc1x0, va0x0);
112       int16x8_t vprod1xCDEFc1 = vmull_s8(vbCDEFc1x0, va1x0);
113       vacc0xCDEF = vpadalq_s16(vacc0xCDEF, vprod0xCDEFc1);
114       vacc1xCDEF = vpadalq_s16(vacc1xCDEF, vprod1xCDEFc1);
115       va0x0 = vext_s8(va0x0, va0x0, 2);
116       va1x0 = vext_s8(va1x0, va1x0, 2);
117       int16x8_t vprod0x0123c2 = vmull_s8(vb0123c2x0, va0x0);
118       int16x8_t vprod1x0123c2 = vmull_s8(vb0123c2x0, va1x0);
119       vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c2);
120       vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c2);
121       int16x8_t vprod0x4567c2 = vmull_s8(vb4567c2x0, va0x0);
122       int16x8_t vprod1x4567c2 = vmull_s8(vb4567c2x0, va1x0);
123       vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c2);
124       vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c2);
125       int16x8_t vprod0x89ABc2 = vmull_s8(vb89ABc2x0, va0x0);
126       int16x8_t vprod1x89ABc2 = vmull_s8(vb89ABc2x0, va1x0);
127       vacc0x89AB = vpadalq_s16(vacc0x89AB, vprod0x89ABc2);
128       vacc1x89AB = vpadalq_s16(vacc1x89AB, vprod1x89ABc2);
129       int16x8_t vprod0xCDEFc2 = vmull_s8(vbCDEFc2x0, va0x0);
130       int16x8_t vprod1xCDEFc2 = vmull_s8(vbCDEFc2x0, va1x0);
131       vacc0xCDEF = vpadalq_s16(vacc0xCDEF, vprod0xCDEFc2);
132       vacc1xCDEF = vpadalq_s16(vacc1xCDEF, vprod1xCDEFc2);
133       va0x0 = vext_s8(va0x0, va0x0, 2);
134       va1x0 = vext_s8(va1x0, va1x0, 2);
135       int16x8_t vprod0x0123c3 = vmull_s8(vb0123c3x0, va0x0);
136       int16x8_t vprod1x0123c3 = vmull_s8(vb0123c3x0, va1x0);
137       vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c3);
138       vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c3);
139       int16x8_t vprod0x4567c3 = vmull_s8(vb4567c3x0, va0x0);
140       int16x8_t vprod1x4567c3 = vmull_s8(vb4567c3x0, va1x0);
141       vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c3);
142       vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c3);
143       int16x8_t vprod0x89ABc3 = vmull_s8(vb89ABc3x0, va0x0);
144       int16x8_t vprod1x89ABc3 = vmull_s8(vb89ABc3x0, va1x0);
145       vacc0x89AB = vpadalq_s16(vacc0x89AB, vprod0x89ABc3);
146       vacc1x89AB = vpadalq_s16(vacc1x89AB, vprod1x89ABc3);
147       int16x8_t vprod0xCDEFc3 = vmull_s8(vbCDEFc3x0, va0x0);
148       int16x8_t vprod1xCDEFc3 = vmull_s8(vbCDEFc3x0, va1x0);
149       vacc0xCDEF = vpadalq_s16(vacc0xCDEF, vprod0xCDEFc3);
150       vacc1xCDEF = vpadalq_s16(vacc1xCDEF, vprod1xCDEFc3);
151 
152       k -= 8 * sizeof(int8_t);
153     } while (k != 0);
154 
155     const int32x4_t vright_pre_shift = vld1q_dup_s32(&params->rndnu_neon.right_pre_shift);
156     const int32x4_t vmultiplier = vld1q_dup_s32(&params->rndnu_neon.multiplier);
157     const int32x4_t vright_post_shift = vld1q_dup_s32(&params->rndnu_neon.right_post_shift);
158 
159     vacc0x0123 = vqshlq_s32(vacc0x0123, vright_pre_shift);
160     vacc0x4567 = vqshlq_s32(vacc0x4567, vright_pre_shift);
161     vacc0x89AB = vqshlq_s32(vacc0x89AB, vright_pre_shift);
162     vacc0xCDEF = vqshlq_s32(vacc0xCDEF, vright_pre_shift);
163     vacc1x0123 = vqshlq_s32(vacc1x0123, vright_pre_shift);
164     vacc1x4567 = vqshlq_s32(vacc1x4567, vright_pre_shift);
165     vacc1x89AB = vqshlq_s32(vacc1x89AB, vright_pre_shift);
166     vacc1xCDEF = vqshlq_s32(vacc1xCDEF, vright_pre_shift);
167 
168     vacc0x0123 = vqdmulhq_s32(vacc0x0123, vmultiplier);
169     vacc0x4567 = vqdmulhq_s32(vacc0x4567, vmultiplier);
170     vacc0x89AB = vqdmulhq_s32(vacc0x89AB, vmultiplier);
171     vacc0xCDEF = vqdmulhq_s32(vacc0xCDEF, vmultiplier);
172     vacc1x0123 = vqdmulhq_s32(vacc1x0123, vmultiplier);
173     vacc1x4567 = vqdmulhq_s32(vacc1x4567, vmultiplier);
174     vacc1x89AB = vqdmulhq_s32(vacc1x89AB, vmultiplier);
175     vacc1xCDEF = vqdmulhq_s32(vacc1xCDEF, vmultiplier);
176 
177     vacc0x0123 = vrshlq_s32(vacc0x0123, vright_post_shift);
178     vacc0x4567 = vrshlq_s32(vacc0x4567, vright_post_shift);
179     vacc0x89AB = vrshlq_s32(vacc0x89AB, vright_post_shift);
180     vacc0xCDEF = vrshlq_s32(vacc0xCDEF, vright_post_shift);
181     vacc1x0123 = vrshlq_s32(vacc1x0123, vright_post_shift);
182     vacc1x4567 = vrshlq_s32(vacc1x4567, vright_post_shift);
183     vacc1x89AB = vrshlq_s32(vacc1x89AB, vright_post_shift);
184     vacc1xCDEF = vrshlq_s32(vacc1xCDEF, vright_post_shift);
185 
186     const int16x8_t voutput_zero_point = vld1q_dup_s16(&params->rndnu_neon.output_zero_point);
187 #if XNN_ARCH_ARM64
188     int16x8_t vacc0x01234567 = vqmovn_high_s32(vqmovn_s32(vacc0x0123), vacc0x4567);
189     int16x8_t vacc0x89ABCDEF = vqmovn_high_s32(vqmovn_s32(vacc0x89AB), vacc0xCDEF);
190     int16x8_t vacc1x01234567 = vqmovn_high_s32(vqmovn_s32(vacc1x0123), vacc1x4567);
191     int16x8_t vacc1x89ABCDEF = vqmovn_high_s32(vqmovn_s32(vacc1x89AB), vacc1xCDEF);
192 
193     vacc0x01234567 = vqaddq_s16(vacc0x01234567, voutput_zero_point);
194     vacc0x89ABCDEF = vqaddq_s16(vacc0x89ABCDEF, voutput_zero_point);
195     vacc1x01234567 = vqaddq_s16(vacc1x01234567, voutput_zero_point);
196     vacc1x89ABCDEF = vqaddq_s16(vacc1x89ABCDEF, voutput_zero_point);
197 
198     int8x16_t vout0x0123456789ABCDEF = vqmovn_high_s16(vqmovn_s16(vacc0x01234567), vacc0x89ABCDEF);
199     int8x16_t vout1x0123456789ABCDEF = vqmovn_high_s16(vqmovn_s16(vacc1x01234567), vacc1x89ABCDEF);
200 #else
201     int16x8_t vacc0x01234567 = vcombine_s16(vqmovn_s32(vacc0x0123), vqmovn_s32(vacc0x4567));
202     int16x8_t vacc0x89ABCDEF = vcombine_s16(vqmovn_s32(vacc0x89AB), vqmovn_s32(vacc0xCDEF));
203     int16x8_t vacc1x01234567 = vcombine_s16(vqmovn_s32(vacc1x0123), vqmovn_s32(vacc1x4567));
204     int16x8_t vacc1x89ABCDEF = vcombine_s16(vqmovn_s32(vacc1x89AB), vqmovn_s32(vacc1xCDEF));
205 
206     vacc0x01234567 = vqaddq_s16(vacc0x01234567, voutput_zero_point);
207     vacc0x89ABCDEF = vqaddq_s16(vacc0x89ABCDEF, voutput_zero_point);
208     vacc1x01234567 = vqaddq_s16(vacc1x01234567, voutput_zero_point);
209     vacc1x89ABCDEF = vqaddq_s16(vacc1x89ABCDEF, voutput_zero_point);
210 
211     int8x16_t vout0x0123456789ABCDEF = vcombine_s8(vqmovn_s16(vacc0x01234567), vqmovn_s16(vacc0x89ABCDEF));
212     int8x16_t vout1x0123456789ABCDEF = vcombine_s8(vqmovn_s16(vacc1x01234567), vqmovn_s16(vacc1x89ABCDEF));
213 #endif
214 
215     const int8x16_t voutput_min = vld1q_dup_s8(&params->rndnu_neon.output_min);
216     vout0x0123456789ABCDEF = vmaxq_s8(vout0x0123456789ABCDEF, voutput_min);
217     vout1x0123456789ABCDEF = vmaxq_s8(vout1x0123456789ABCDEF, voutput_min);
218 
219     const int8x16_t voutput_max = vld1q_dup_s8(&params->rndnu_neon.output_max);
220     vout0x0123456789ABCDEF = vminq_s8(vout0x0123456789ABCDEF, voutput_max);
221     vout1x0123456789ABCDEF = vminq_s8(vout1x0123456789ABCDEF, voutput_max);
222 
223     if (nc >= 16) {
224       vst1q_s8(c0 + 0, vout0x0123456789ABCDEF);
225       vst1q_s8(c1 + 0, vout1x0123456789ABCDEF);
226 
227       c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
228       c1 = (int8_t*) ((uintptr_t) c1 + cn_stride);
229 
230       a0 = (const int8_t*) ((uintptr_t) a0 - kc);
231       a1 = (const int8_t*) ((uintptr_t) a1 - kc);
232 
233       nc -= 16;
234     } else {
235       // Final case where not all of the 16 columns fit in the destination.
236       int8x16_t vout0x01234567_1x01234567 = vcombine_s8(vget_low_s8(vout0x0123456789ABCDEF), vget_low_s8(vout1x0123456789ABCDEF));
237       if (nc & 8) {
238         vst1_s8(c0, vget_low_s8(vout0x01234567_1x01234567)); c0 += 8;
239         vst1_s8(c1, vget_high_s8(vout0x01234567_1x01234567)); c1 += 8;
240         vout0x01234567_1x01234567 = vcombine_s8(vget_high_s8(vout0x0123456789ABCDEF), vget_high_s8(vout1x0123456789ABCDEF));
241       }
242       if (nc & 4) {
243         vst1q_lane_u32((void*) c0, vreinterpretq_u32_s8(vout0x01234567_1x01234567), 0); c0 += 4;
244         vst1q_lane_u32((void*) c1, vreinterpretq_u32_s8(vout0x01234567_1x01234567), 2); c1 += 4;
245         vout0x01234567_1x01234567 = vextq_s8(vout0x01234567_1x01234567, vout0x01234567_1x01234567, 4);
246       }
247       if (nc & 2) {
248         vst1q_lane_u16((void*) c0, vreinterpretq_u16_s8(vout0x01234567_1x01234567), 0); c0 += 2;
249         vst1q_lane_u16((void*) c1, vreinterpretq_u16_s8(vout0x01234567_1x01234567), 4); c1 += 2;
250         vout0x01234567_1x01234567 = vextq_s8(vout0x01234567_1x01234567, vout0x01234567_1x01234567, 2);
251       }
252       if (nc & 1) {
253         vst1q_lane_s8(c0, vout0x01234567_1x01234567, 0);
254         vst1q_lane_s8(c1, vout0x01234567_1x01234567, 8);
255       }
256 
257       nc = 0;
258     }
259   } while (nc != 0);
260 }
261