xref: /aosp_15_r20/external/XNNPACK/src/qs8-gemm/gen/3x8c2s4-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_3x8c2s4__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_3x8c2s4__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 <= 3);
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   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   kc = round_up_po2(kc, 8 * sizeof(int8_t));
55   do {
56     int32x4_t vacc0x0123 = vld1q_s32(w); w = (const int32_t*) w + 4;
57     int32x4_t vacc0x4567 = vld1q_s32(w); w = (const int32_t*) w + 4;
58     int32x4_t vacc1x0123 = vacc0x0123;
59     int32x4_t vacc1x4567 = vacc0x4567;
60     int32x4_t vacc2x0123 = vacc0x0123;
61     int32x4_t vacc2x4567 = vacc0x4567;
62 
63     size_t k = kc;
64     do {
65       int8x8_t va0x0 = vld1_s8(a0); a0 += 8;
66       int8x8_t va1x0 = vld1_s8(a1); a1 += 8;
67       int8x8_t va2x0 = vld1_s8(a2); a2 += 8;
68 
69       const int8x8_t vb0123c0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
70       const int8x8_t vb4567c0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
71       const int8x8_t vb0123c1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
72       const int8x8_t vb4567c1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
73       const int8x8_t vb0123c2x0 = vld1_s8(w); w = (const int8_t*) w + 8;
74       const int8x8_t vb4567c2x0 = vld1_s8(w); w = (const int8_t*) w + 8;
75       const int8x8_t vb0123c3x0 = vld1_s8(w); w = (const int8_t*) w + 8;
76       const int8x8_t vb4567c3x0 = vld1_s8(w); w = (const int8_t*) w + 8;
77 
78       int16x8_t vprod0x0123c0 = vmull_s8(vb0123c0x0, va0x0);
79       int16x8_t vprod1x0123c0 = vmull_s8(vb0123c0x0, va1x0);
80       int16x8_t vprod2x0123c0 = vmull_s8(vb0123c0x0, va2x0);
81       vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c0);
82       vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c0);
83       vacc2x0123 = vpadalq_s16(vacc2x0123, vprod2x0123c0);
84       int16x8_t vprod0x4567c0 = vmull_s8(vb4567c0x0, va0x0);
85       int16x8_t vprod1x4567c0 = vmull_s8(vb4567c0x0, va1x0);
86       int16x8_t vprod2x4567c0 = vmull_s8(vb4567c0x0, va2x0);
87       vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c0);
88       vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c0);
89       vacc2x4567 = vpadalq_s16(vacc2x4567, vprod2x4567c0);
90       va0x0 = vext_s8(va0x0, va0x0, 2);
91       va1x0 = vext_s8(va1x0, va1x0, 2);
92       va2x0 = vext_s8(va2x0, va2x0, 2);
93       int16x8_t vprod0x0123c1 = vmull_s8(vb0123c1x0, va0x0);
94       int16x8_t vprod1x0123c1 = vmull_s8(vb0123c1x0, va1x0);
95       int16x8_t vprod2x0123c1 = vmull_s8(vb0123c1x0, va2x0);
96       vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c1);
97       vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c1);
98       vacc2x0123 = vpadalq_s16(vacc2x0123, vprod2x0123c1);
99       int16x8_t vprod0x4567c1 = vmull_s8(vb4567c1x0, va0x0);
100       int16x8_t vprod1x4567c1 = vmull_s8(vb4567c1x0, va1x0);
101       int16x8_t vprod2x4567c1 = vmull_s8(vb4567c1x0, va2x0);
102       vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c1);
103       vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c1);
104       vacc2x4567 = vpadalq_s16(vacc2x4567, vprod2x4567c1);
105       va0x0 = vext_s8(va0x0, va0x0, 2);
106       va1x0 = vext_s8(va1x0, va1x0, 2);
107       va2x0 = vext_s8(va2x0, va2x0, 2);
108       int16x8_t vprod0x0123c2 = vmull_s8(vb0123c2x0, va0x0);
109       int16x8_t vprod1x0123c2 = vmull_s8(vb0123c2x0, va1x0);
110       int16x8_t vprod2x0123c2 = vmull_s8(vb0123c2x0, va2x0);
111       vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c2);
112       vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c2);
113       vacc2x0123 = vpadalq_s16(vacc2x0123, vprod2x0123c2);
114       int16x8_t vprod0x4567c2 = vmull_s8(vb4567c2x0, va0x0);
115       int16x8_t vprod1x4567c2 = vmull_s8(vb4567c2x0, va1x0);
116       int16x8_t vprod2x4567c2 = vmull_s8(vb4567c2x0, va2x0);
117       vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c2);
118       vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c2);
119       vacc2x4567 = vpadalq_s16(vacc2x4567, vprod2x4567c2);
120       va0x0 = vext_s8(va0x0, va0x0, 2);
121       va1x0 = vext_s8(va1x0, va1x0, 2);
122       va2x0 = vext_s8(va2x0, va2x0, 2);
123       int16x8_t vprod0x0123c3 = vmull_s8(vb0123c3x0, va0x0);
124       int16x8_t vprod1x0123c3 = vmull_s8(vb0123c3x0, va1x0);
125       int16x8_t vprod2x0123c3 = vmull_s8(vb0123c3x0, va2x0);
126       vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c3);
127       vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c3);
128       vacc2x0123 = vpadalq_s16(vacc2x0123, vprod2x0123c3);
129       int16x8_t vprod0x4567c3 = vmull_s8(vb4567c3x0, va0x0);
130       int16x8_t vprod1x4567c3 = vmull_s8(vb4567c3x0, va1x0);
131       int16x8_t vprod2x4567c3 = vmull_s8(vb4567c3x0, va2x0);
132       vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c3);
133       vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c3);
134       vacc2x4567 = vpadalq_s16(vacc2x4567, vprod2x4567c3);
135 
136       k -= 8 * sizeof(int8_t);
137     } while (k != 0);
138 
139     const int32x4_t vright_pre_shift = vld1q_dup_s32(&params->rndnu_neon.right_pre_shift);
140     const int32x4_t vmultiplier = vld1q_dup_s32(&params->rndnu_neon.multiplier);
141     const int32x4_t vright_post_shift = vld1q_dup_s32(&params->rndnu_neon.right_post_shift);
142 
143     vacc0x0123 = vqshlq_s32(vacc0x0123, vright_pre_shift);
144     vacc0x4567 = vqshlq_s32(vacc0x4567, vright_pre_shift);
145     vacc1x0123 = vqshlq_s32(vacc1x0123, vright_pre_shift);
146     vacc1x4567 = vqshlq_s32(vacc1x4567, vright_pre_shift);
147     vacc2x0123 = vqshlq_s32(vacc2x0123, vright_pre_shift);
148     vacc2x4567 = vqshlq_s32(vacc2x4567, vright_pre_shift);
149 
150     vacc0x0123 = vqdmulhq_s32(vacc0x0123, vmultiplier);
151     vacc0x4567 = vqdmulhq_s32(vacc0x4567, vmultiplier);
152     vacc1x0123 = vqdmulhq_s32(vacc1x0123, vmultiplier);
153     vacc1x4567 = vqdmulhq_s32(vacc1x4567, vmultiplier);
154     vacc2x0123 = vqdmulhq_s32(vacc2x0123, vmultiplier);
155     vacc2x4567 = vqdmulhq_s32(vacc2x4567, vmultiplier);
156 
157     vacc0x0123 = vrshlq_s32(vacc0x0123, vright_post_shift);
158     vacc0x4567 = vrshlq_s32(vacc0x4567, vright_post_shift);
159     vacc1x0123 = vrshlq_s32(vacc1x0123, vright_post_shift);
160     vacc1x4567 = vrshlq_s32(vacc1x4567, vright_post_shift);
161     vacc2x0123 = vrshlq_s32(vacc2x0123, vright_post_shift);
162     vacc2x4567 = vrshlq_s32(vacc2x4567, vright_post_shift);
163 
164     const int16x8_t voutput_zero_point = vld1q_dup_s16(&params->rndnu_neon.output_zero_point);
165 #if XNN_ARCH_ARM64
166     int16x8_t vacc0x01234567 = vqmovn_high_s32(vqmovn_s32(vacc0x0123), vacc0x4567);
167     int16x8_t vacc1x01234567 = vqmovn_high_s32(vqmovn_s32(vacc1x0123), vacc1x4567);
168     int16x8_t vacc2x01234567 = vqmovn_high_s32(vqmovn_s32(vacc2x0123), vacc2x4567);
169 
170     vacc0x01234567 = vqaddq_s16(vacc0x01234567, voutput_zero_point);
171     vacc1x01234567 = vqaddq_s16(vacc1x01234567, voutput_zero_point);
172     vacc2x01234567 = vqaddq_s16(vacc2x01234567, voutput_zero_point);
173 
174     int8x16_t vout0x01234567_1x01234567 = vqmovn_high_s16(vqmovn_s16(vacc0x01234567), vacc1x01234567);
175     int8x8_t vout2x01234567 = vqmovn_s16(vacc2x01234567);
176 #else
177     int16x8_t vacc0x01234567 = vcombine_s16(vqmovn_s32(vacc0x0123), vqmovn_s32(vacc0x4567));
178     int16x8_t vacc1x01234567 = vcombine_s16(vqmovn_s32(vacc1x0123), vqmovn_s32(vacc1x4567));
179     int16x8_t vacc2x01234567 = vcombine_s16(vqmovn_s32(vacc2x0123), vqmovn_s32(vacc2x4567));
180 
181     vacc0x01234567 = vqaddq_s16(vacc0x01234567, voutput_zero_point);
182     vacc1x01234567 = vqaddq_s16(vacc1x01234567, voutput_zero_point);
183     vacc2x01234567 = vqaddq_s16(vacc2x01234567, voutput_zero_point);
184 
185     int8x16_t vout0x01234567_1x01234567 = vcombine_s8(vqmovn_s16(vacc0x01234567), vqmovn_s16(vacc1x01234567));
186     int8x8_t vout2x01234567 = vqmovn_s16(vacc2x01234567);
187 #endif
188 
189     const int8x16_t voutput_min = vld1q_dup_s8(&params->rndnu_neon.output_min);
190     vout0x01234567_1x01234567 = vmaxq_s8(vout0x01234567_1x01234567, voutput_min);
191     vout2x01234567 = vmax_s8(vout2x01234567, vget_low_s8(voutput_min));
192 
193     const int8x16_t voutput_max = vld1q_dup_s8(&params->rndnu_neon.output_max);
194     vout0x01234567_1x01234567 = vminq_s8(vout0x01234567_1x01234567, voutput_max);
195     vout2x01234567 = vmin_s8(vout2x01234567, vget_low_s8(voutput_max));
196 
197     if (nc >= 8) {
198       vst1_s8(c0 + 0, vget_low_s8(vout0x01234567_1x01234567));
199       vst1_s8(c1 + 0, vget_high_s8(vout0x01234567_1x01234567));
200       vst1_s8(c2 + 0, vout2x01234567);
201 
202       c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
203       c1 = (int8_t*) ((uintptr_t) c1 + cn_stride);
204       c2 = (int8_t*) ((uintptr_t) c2 + cn_stride);
205 
206       a0 = (const int8_t*) ((uintptr_t) a0 - kc);
207       a1 = (const int8_t*) ((uintptr_t) a1 - kc);
208       a2 = (const int8_t*) ((uintptr_t) a2 - kc);
209 
210       nc -= 8;
211     } else {
212       // Final case where not all of the 8 columns fit in the destination.
213       if (nc & 4) {
214         vst1q_lane_u32((void*) c0, vreinterpretq_u32_s8(vout0x01234567_1x01234567), 0); c0 += 4;
215         vst1q_lane_u32((void*) c1, vreinterpretq_u32_s8(vout0x01234567_1x01234567), 2); c1 += 4;
216         vst1_lane_u32((void*) c2, vreinterpret_u32_s8(vout2x01234567), 0); c2 += 4;
217         vout0x01234567_1x01234567 = vextq_s8(vout0x01234567_1x01234567, vout0x01234567_1x01234567, 4);
218         vout2x01234567 = vext_s8(vout2x01234567, vout2x01234567, 4);
219       }
220       if (nc & 2) {
221         vst1q_lane_u16((void*) c0, vreinterpretq_u16_s8(vout0x01234567_1x01234567), 0); c0 += 2;
222         vst1q_lane_u16((void*) c1, vreinterpretq_u16_s8(vout0x01234567_1x01234567), 4); c1 += 2;
223         vst1_lane_u16((void*) c2, vreinterpret_u16_s8(vout2x01234567), 0); c2 += 2;
224         vout0x01234567_1x01234567 = vextq_s8(vout0x01234567_1x01234567, vout0x01234567_1x01234567, 2);
225         vout2x01234567 = vext_s8(vout2x01234567, vout2x01234567, 2);
226       }
227       if (nc & 1) {
228         vst1q_lane_s8(c0, vout0x01234567_1x01234567, 0);
229         vst1q_lane_s8(c1, vout0x01234567_1x01234567, 8);
230         vst1_lane_s8(c2, vout2x01234567, 0);
231       }
232 
233       nc = 0;
234     }
235   } while (nc != 0);
236 }
237