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