xref: /aosp_15_r20/external/XNNPACK/src/qs8-gemm/gen/6x8c4-minmax-rndnu-neondot.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Auto-generated file. Do not edit!
2 //   Template: src/qs8-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_qs8_gemm_minmax_rndnu_ukernel_6x8c4__neondot(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_6x8c4__neondot(
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 <= 6);
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   kc = round_up_po2(kc, 4 * sizeof(int8_t));
40   const int8_t* a0 = a;
41   int8_t* c0 = c;
42   const int8_t* a1 = (const int8_t*) ((uintptr_t) a0 + a_stride);
43   int8_t* c1 = (int8_t*) ((uintptr_t) c0 + cm_stride);
44   if XNN_UNPREDICTABLE(mr < 2) {
45     a1 = a0;
46     c1 = c0;
47   }
48   const int8_t* a2 = (const int8_t*) ((uintptr_t) a1 + a_stride);
49   int8_t* c2 = (int8_t*) ((uintptr_t) c1 + cm_stride);
50   if XNN_UNPREDICTABLE(mr <= 2) {
51     a2 = a1;
52     c2 = c1;
53   }
54   const int8_t* a3 = (const int8_t*) ((uintptr_t) a2 + a_stride);
55   int8_t* c3 = (int8_t*) ((uintptr_t) c2 + cm_stride);
56   if XNN_UNPREDICTABLE(mr < 4) {
57     a3 = a2;
58     c3 = c2;
59   }
60   const int8_t* a4 = (const int8_t*) ((uintptr_t) a3 + a_stride);
61   int8_t* c4 = (int8_t*) ((uintptr_t) c3 + cm_stride);
62   if XNN_UNPREDICTABLE(mr <= 4) {
63     a4 = a3;
64     c4 = c3;
65   }
66   const int8_t* a5 = (const int8_t*) ((uintptr_t) a4 + a_stride);
67   int8_t* c5 = (int8_t*) ((uintptr_t) c4 + cm_stride);
68   if XNN_UNPREDICTABLE(mr != 6) {
69     a5 = a4;
70     c5 = c4;
71   }
72 
73   // Loop over groups of 8 columns.
74   do {
75     // Initialize accumulators with bias. 8 bias values are loaded from the
76     // weight matrix, at the start of the group of 8 columns.
77     int32x4_t vacc0x0123 = vld1q_s32(w); w = (const void*) ((const int32_t*) w + 4);
78     int32x4_t vacc0x4567 = vld1q_s32(w); w = (const void*) ((const int32_t*) w + 4);
79     int32x4_t vacc1x0123 = vacc0x0123;
80     int32x4_t vacc1x4567 = vacc0x4567;
81     int32x4_t vacc2x0123 = vacc0x0123;
82     int32x4_t vacc2x4567 = vacc0x4567;
83     int32x4_t vacc3x0123 = vacc0x0123;
84     int32x4_t vacc3x4567 = vacc0x4567;
85     int32x4_t vacc4x0123 = vacc0x0123;
86     int32x4_t vacc4x4567 = vacc0x4567;
87     int32x4_t vacc5x0123 = vacc0x0123;
88     int32x4_t vacc5x4567 = vacc0x4567;
89 
90     // Inner accumulation loop along the 8 columns.
91     size_t k = kc;
92     // 2x partial unrolled loop to load 8 bytes at a time.
93     while (k >= 8 * sizeof(int8_t)) {
94       // Load a 6x8 block of activations.
95       const int8x8_t va0x01234567 = vld1_s8(a0); a0 += 8;
96       const int8x8_t va1x01234567 = vld1_s8(a1); a1 += 8;
97       const int8x8_t va2x01234567 = vld1_s8(a2); a2 += 8;
98       const int8x8_t va3x01234567 = vld1_s8(a3); a3 += 8;
99       const int8x8_t va4x01234567 = vld1_s8(a4); a4 += 8;
100       const int8x8_t va5x01234567 = vld1_s8(a5); a5 += 8;
101 
102       // Load a 8x8 block of weights.
103       const int8x16_t vb0123x0123 = vld1q_s8(w); w = (const void*) ((const int8_t*) w + 16);
104       const int8x16_t vb0123x4567 = vld1q_s8(w); w = (const void*) ((const int8_t*) w + 16);
105       const int8x16_t vb4567x0123 = vld1q_s8(w); w = (const void*) ((const int8_t*) w + 16);
106       const int8x16_t vb4567x4567 = vld1q_s8(w); w = (const void*) ((const int8_t*) w + 16);
107 
108       // Multiply-accumulate: 6x8 * 8x8 --> 6x8.
109       vacc0x0123 = vdotq_lane_s32(vacc0x0123, vb0123x0123, va0x01234567, 0);
110       vacc0x4567 = vdotq_lane_s32(vacc0x4567, vb0123x4567, va0x01234567, 0);
111       vacc1x0123 = vdotq_lane_s32(vacc1x0123, vb0123x0123, va1x01234567, 0);
112       vacc1x4567 = vdotq_lane_s32(vacc1x4567, vb0123x4567, va1x01234567, 0);
113       vacc2x0123 = vdotq_lane_s32(vacc2x0123, vb0123x0123, va2x01234567, 0);
114       vacc2x4567 = vdotq_lane_s32(vacc2x4567, vb0123x4567, va2x01234567, 0);
115       vacc3x0123 = vdotq_lane_s32(vacc3x0123, vb0123x0123, va3x01234567, 0);
116       vacc3x4567 = vdotq_lane_s32(vacc3x4567, vb0123x4567, va3x01234567, 0);
117       vacc4x0123 = vdotq_lane_s32(vacc4x0123, vb0123x0123, va4x01234567, 0);
118       vacc4x4567 = vdotq_lane_s32(vacc4x4567, vb0123x4567, va4x01234567, 0);
119       vacc5x0123 = vdotq_lane_s32(vacc5x0123, vb0123x0123, va5x01234567, 0);
120       vacc5x4567 = vdotq_lane_s32(vacc5x4567, vb0123x4567, va5x01234567, 0);
121       vacc0x0123 = vdotq_lane_s32(vacc0x0123, vb4567x0123, va0x01234567, 1);
122       vacc0x4567 = vdotq_lane_s32(vacc0x4567, vb4567x4567, va0x01234567, 1);
123       vacc1x0123 = vdotq_lane_s32(vacc1x0123, vb4567x0123, va1x01234567, 1);
124       vacc1x4567 = vdotq_lane_s32(vacc1x4567, vb4567x4567, va1x01234567, 1);
125       vacc2x0123 = vdotq_lane_s32(vacc2x0123, vb4567x0123, va2x01234567, 1);
126       vacc2x4567 = vdotq_lane_s32(vacc2x4567, vb4567x4567, va2x01234567, 1);
127       vacc3x0123 = vdotq_lane_s32(vacc3x0123, vb4567x0123, va3x01234567, 1);
128       vacc3x4567 = vdotq_lane_s32(vacc3x4567, vb4567x4567, va3x01234567, 1);
129       vacc4x0123 = vdotq_lane_s32(vacc4x0123, vb4567x0123, va4x01234567, 1);
130       vacc4x4567 = vdotq_lane_s32(vacc4x4567, vb4567x4567, va4x01234567, 1);
131       vacc5x0123 = vdotq_lane_s32(vacc5x0123, vb4567x0123, va5x01234567, 1);
132       vacc5x4567 = vdotq_lane_s32(vacc5x4567, vb4567x4567, va5x01234567, 1);
133 
134       k -= 8 * sizeof(int8_t);
135     }
136     // Handle up to 4 final positions of `k`
137     if XNN_UNLIKELY(k != 0) {
138       // Load a 6x4 block of activations.
139       const int8x8_t va0x01234567 = vld1_s8(a0); a0 += 4;
140       const int8x8_t va1x01234567 = vld1_s8(a1); a1 += 4;
141       const int8x8_t va2x01234567 = vld1_s8(a2); a2 += 4;
142       const int8x8_t va3x01234567 = vld1_s8(a3); a3 += 4;
143       const int8x8_t va4x01234567 = vld1_s8(a4); a4 += 4;
144       const int8x8_t va5x01234567 = vld1_s8(a5); a5 += 4;
145 
146       // Load a 4x8 block of weights.
147       const int8x16_t vb0123x0123 = vld1q_s8(w); w = (const void*) ((const int8_t*) w + 16);
148       const int8x16_t vb0123x4567 = vld1q_s8(w); w = (const void*) ((const int8_t*) w + 16);
149 
150       // Multiply-accumulate: 6x4 * 4x8 --> 6x8.
151       vacc0x0123 = vdotq_lane_s32(vacc0x0123, vb0123x0123, va0x01234567, 0);
152       vacc0x4567 = vdotq_lane_s32(vacc0x4567, vb0123x4567, va0x01234567, 0);
153       vacc1x0123 = vdotq_lane_s32(vacc1x0123, vb0123x0123, va1x01234567, 0);
154       vacc1x4567 = vdotq_lane_s32(vacc1x4567, vb0123x4567, va1x01234567, 0);
155       vacc2x0123 = vdotq_lane_s32(vacc2x0123, vb0123x0123, va2x01234567, 0);
156       vacc2x4567 = vdotq_lane_s32(vacc2x4567, vb0123x4567, va2x01234567, 0);
157       vacc3x0123 = vdotq_lane_s32(vacc3x0123, vb0123x0123, va3x01234567, 0);
158       vacc3x4567 = vdotq_lane_s32(vacc3x4567, vb0123x4567, va3x01234567, 0);
159       vacc4x0123 = vdotq_lane_s32(vacc4x0123, vb0123x0123, va4x01234567, 0);
160       vacc4x4567 = vdotq_lane_s32(vacc4x4567, vb0123x4567, va4x01234567, 0);
161       vacc5x0123 = vdotq_lane_s32(vacc5x0123, vb0123x0123, va5x01234567, 0);
162       vacc5x4567 = vdotq_lane_s32(vacc5x4567, vb0123x4567, va5x01234567, 0);
163     }
164 
165     const int32x4_t vright_pre_shift = vld1q_dup_s32(&params->rndnu_neon.right_pre_shift);
166     const int32x4_t vmultiplier = vld1q_dup_s32(&params->rndnu_neon.multiplier);
167     const int32x4_t vright_post_shift = vld1q_dup_s32(&params->rndnu_neon.right_post_shift);
168 
169     vacc0x0123 = vqshlq_s32(vacc0x0123, vright_pre_shift);
170     vacc0x4567 = vqshlq_s32(vacc0x4567, vright_pre_shift);
171     vacc1x0123 = vqshlq_s32(vacc1x0123, vright_pre_shift);
172     vacc1x4567 = vqshlq_s32(vacc1x4567, vright_pre_shift);
173     vacc2x0123 = vqshlq_s32(vacc2x0123, vright_pre_shift);
174     vacc2x4567 = vqshlq_s32(vacc2x4567, vright_pre_shift);
175     vacc3x0123 = vqshlq_s32(vacc3x0123, vright_pre_shift);
176     vacc3x4567 = vqshlq_s32(vacc3x4567, vright_pre_shift);
177     vacc4x0123 = vqshlq_s32(vacc4x0123, vright_pre_shift);
178     vacc4x4567 = vqshlq_s32(vacc4x4567, vright_pre_shift);
179     vacc5x0123 = vqshlq_s32(vacc5x0123, vright_pre_shift);
180     vacc5x4567 = vqshlq_s32(vacc5x4567, vright_pre_shift);
181 
182     vacc0x0123 = vqdmulhq_s32(vacc0x0123, vmultiplier);
183     vacc0x4567 = vqdmulhq_s32(vacc0x4567, vmultiplier);
184     vacc1x0123 = vqdmulhq_s32(vacc1x0123, vmultiplier);
185     vacc1x4567 = vqdmulhq_s32(vacc1x4567, vmultiplier);
186     vacc2x0123 = vqdmulhq_s32(vacc2x0123, vmultiplier);
187     vacc2x4567 = vqdmulhq_s32(vacc2x4567, vmultiplier);
188     vacc3x0123 = vqdmulhq_s32(vacc3x0123, vmultiplier);
189     vacc3x4567 = vqdmulhq_s32(vacc3x4567, vmultiplier);
190     vacc4x0123 = vqdmulhq_s32(vacc4x0123, vmultiplier);
191     vacc4x4567 = vqdmulhq_s32(vacc4x4567, vmultiplier);
192     vacc5x0123 = vqdmulhq_s32(vacc5x0123, vmultiplier);
193     vacc5x4567 = vqdmulhq_s32(vacc5x4567, vmultiplier);
194 
195     vacc0x0123 = vrshlq_s32(vacc0x0123, vright_post_shift);
196     vacc0x4567 = vrshlq_s32(vacc0x4567, vright_post_shift);
197     vacc1x0123 = vrshlq_s32(vacc1x0123, vright_post_shift);
198     vacc1x4567 = vrshlq_s32(vacc1x4567, vright_post_shift);
199     vacc2x0123 = vrshlq_s32(vacc2x0123, vright_post_shift);
200     vacc2x4567 = vrshlq_s32(vacc2x4567, vright_post_shift);
201     vacc3x0123 = vrshlq_s32(vacc3x0123, vright_post_shift);
202     vacc3x4567 = vrshlq_s32(vacc3x4567, vright_post_shift);
203     vacc4x0123 = vrshlq_s32(vacc4x0123, vright_post_shift);
204     vacc4x4567 = vrshlq_s32(vacc4x4567, vright_post_shift);
205     vacc5x0123 = vrshlq_s32(vacc5x0123, vright_post_shift);
206     vacc5x4567 = vrshlq_s32(vacc5x4567, vright_post_shift);
207 
208     const int16x8_t voutput_zero_point = vld1q_dup_s16(&params->rndnu_neon.output_zero_point);
209 #if XNN_ARCH_ARM64
210     const int16x8_t vacc0x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc0x0123), vacc0x4567), voutput_zero_point);
211     const int16x8_t vacc1x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc1x0123), vacc1x4567), voutput_zero_point);
212     const int16x8_t vacc2x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc2x0123), vacc2x4567), voutput_zero_point);
213     const int16x8_t vacc3x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc3x0123), vacc3x4567), voutput_zero_point);
214     const int16x8_t vacc4x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc4x0123), vacc4x4567), voutput_zero_point);
215     const int16x8_t vacc5x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc5x0123), vacc5x4567), voutput_zero_point);
216 
217     int8x16_t vout0x01234567_1x01234567 = vqmovn_high_s16(vqmovn_s16(vacc0x01234567), vacc1x01234567);
218     int8x16_t vout2x01234567_3x01234567 = vqmovn_high_s16(vqmovn_s16(vacc2x01234567), vacc3x01234567);
219     int8x16_t vout4x01234567_5x01234567 = vqmovn_high_s16(vqmovn_s16(vacc4x01234567), vacc5x01234567);
220 #else
221     const int16x8_t vacc0x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc0x0123), vqmovn_s32(vacc0x4567)), voutput_zero_point);
222     const int16x8_t vacc1x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc1x0123), vqmovn_s32(vacc1x4567)), voutput_zero_point);
223     const int16x8_t vacc2x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc2x0123), vqmovn_s32(vacc2x4567)), voutput_zero_point);
224     const int16x8_t vacc3x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc3x0123), vqmovn_s32(vacc3x4567)), voutput_zero_point);
225     const int16x8_t vacc4x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc4x0123), vqmovn_s32(vacc4x4567)), voutput_zero_point);
226     const int16x8_t vacc5x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc5x0123), vqmovn_s32(vacc5x4567)), voutput_zero_point);
227 
228     int8x16_t vout0x01234567_1x01234567 = vcombine_s8(vqmovn_s16(vacc0x01234567), vqmovn_s16(vacc1x01234567));
229     int8x16_t vout2x01234567_3x01234567 = vcombine_s8(vqmovn_s16(vacc2x01234567), vqmovn_s16(vacc3x01234567));
230     int8x16_t vout4x01234567_5x01234567 = vcombine_s8(vqmovn_s16(vacc4x01234567), vqmovn_s16(vacc5x01234567));
231 #endif
232     const int8x16_t voutput_min = vld1q_dup_s8(&params->rndnu_neon.output_min);
233     const int8x16_t voutput_max = vld1q_dup_s8(&params->rndnu_neon.output_max);
234 
235     vout0x01234567_1x01234567 = vmaxq_s8(vout0x01234567_1x01234567, voutput_min);
236     vout2x01234567_3x01234567 = vmaxq_s8(vout2x01234567_3x01234567, voutput_min);
237     vout4x01234567_5x01234567 = vmaxq_s8(vout4x01234567_5x01234567, voutput_min);
238 
239     vout0x01234567_1x01234567 = vminq_s8(vout0x01234567_1x01234567, voutput_max);
240     vout2x01234567_3x01234567 = vminq_s8(vout2x01234567_3x01234567, voutput_max);
241     vout4x01234567_5x01234567 = vminq_s8(vout4x01234567_5x01234567, voutput_max);
242 
243     if (nc >= 8) {
244       // Main case where there the 8 columns fit in the destination.
245       vst1_s8(c0 + 0, vget_low_s8(vout0x01234567_1x01234567));
246       vst1_s8(c1 + 0, vget_high_s8(vout0x01234567_1x01234567));
247       vst1_s8(c2 + 0, vget_low_s8(vout2x01234567_3x01234567));
248       vst1_s8(c3 + 0, vget_high_s8(vout2x01234567_3x01234567));
249       vst1_s8(c4 + 0, vget_low_s8(vout4x01234567_5x01234567));
250       vst1_s8(c5 + 0, vget_high_s8(vout4x01234567_5x01234567));
251 
252       // Advance to the next 8 columns.
253       c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
254       c1 = (int8_t*) ((uintptr_t) c1 + cn_stride);
255       c2 = (int8_t*) ((uintptr_t) c2 + cn_stride);
256       c3 = (int8_t*) ((uintptr_t) c3 + cn_stride);
257       c4 = (int8_t*) ((uintptr_t) c4 + cn_stride);
258       c5 = (int8_t*) ((uintptr_t) c5 + cn_stride);
259 
260       a0 = (const int8_t*) ((uintptr_t) a0 - kc);
261       a1 = (const int8_t*) ((uintptr_t) a1 - kc);
262       a2 = (const int8_t*) ((uintptr_t) a2 - kc);
263       a3 = (const int8_t*) ((uintptr_t) a3 - kc);
264       a4 = (const int8_t*) ((uintptr_t) a4 - kc);
265       a5 = (const int8_t*) ((uintptr_t) a5 - kc);
266 
267       nc -= 8;
268     } else {
269       // Final case where not all of the 8 columns fit in the destination.
270       if (nc & 4) {
271         vst1q_lane_u32((void*) c0, vreinterpretq_u32_s8(vout0x01234567_1x01234567), 0); c0 += 4;
272         vst1q_lane_u32((void*) c1, vreinterpretq_u32_s8(vout0x01234567_1x01234567), 2); c1 += 4;
273         vst1q_lane_u32((void*) c2, vreinterpretq_u32_s8(vout2x01234567_3x01234567), 0); c2 += 4;
274         vst1q_lane_u32((void*) c3, vreinterpretq_u32_s8(vout2x01234567_3x01234567), 2); c3 += 4;
275         vst1q_lane_u32((void*) c4, vreinterpretq_u32_s8(vout4x01234567_5x01234567), 0); c4 += 4;
276         vst1q_lane_u32((void*) c5, vreinterpretq_u32_s8(vout4x01234567_5x01234567), 2); c5 += 4;
277         vout0x01234567_1x01234567 = vextq_s8(vout0x01234567_1x01234567, vout0x01234567_1x01234567, 4);
278         vout2x01234567_3x01234567 = vextq_s8(vout2x01234567_3x01234567, vout2x01234567_3x01234567, 4);
279         vout4x01234567_5x01234567 = vextq_s8(vout4x01234567_5x01234567, vout4x01234567_5x01234567, 4);
280       }
281       if (nc & 2) {
282         vst1q_lane_u16((void*) c0, vreinterpretq_u16_s8(vout0x01234567_1x01234567), 0); c0 += 2;
283         vst1q_lane_u16((void*) c1, vreinterpretq_u16_s8(vout0x01234567_1x01234567), 4); c1 += 2;
284         vst1q_lane_u16((void*) c2, vreinterpretq_u16_s8(vout2x01234567_3x01234567), 0); c2 += 2;
285         vst1q_lane_u16((void*) c3, vreinterpretq_u16_s8(vout2x01234567_3x01234567), 4); c3 += 2;
286         vst1q_lane_u16((void*) c4, vreinterpretq_u16_s8(vout4x01234567_5x01234567), 0); c4 += 2;
287         vst1q_lane_u16((void*) c5, vreinterpretq_u16_s8(vout4x01234567_5x01234567), 4); c5 += 2;
288         vout0x01234567_1x01234567 = vextq_s8(vout0x01234567_1x01234567, vout0x01234567_1x01234567, 2);
289         vout2x01234567_3x01234567 = vextq_s8(vout2x01234567_3x01234567, vout2x01234567_3x01234567, 2);
290         vout4x01234567_5x01234567 = vextq_s8(vout4x01234567_5x01234567, vout4x01234567_5x01234567, 2);
291       }
292       if (nc & 1) {
293         vst1q_lane_s8(c0, vout0x01234567_1x01234567, 0);
294         vst1q_lane_s8(c1, vout0x01234567_1x01234567, 8);
295         vst1q_lane_s8(c2, vout2x01234567_3x01234567, 0);
296         vst1q_lane_s8(c3, vout2x01234567_3x01234567, 8);
297         vst1q_lane_s8(c4, vout4x01234567_5x01234567, 0);
298         vst1q_lane_s8(c5, vout4x01234567_5x01234567, 8);
299       }
300 
301       nc = 0;
302     }
303   } while (nc != 0);
304 }
305