xref: /aosp_15_r20/external/XNNPACK/src/qc8-gemm/gen/1x8c4s2-minmax-fp32-neonv8-mlal.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Auto-generated file. Do not edit!
2 //   Template: src/qs8-gemm/c4-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/intrinsics-polyfill.h>
16 #include <xnnpack/math.h>
17 
18 
xnn_qc8_gemm_minmax_fp32_ukernel_1x8c4s2__neonv8_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_qc8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])19 void xnn_qc8_gemm_minmax_fp32_ukernel_1x8c4s2__neonv8_mlal(
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 <= 1);
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   const int8_t* a0 = a;
41   int8_t* c0 = c;
42 
43   kc = round_up_po2(kc, 8 * sizeof(int8_t));
44   do {
45     int32x4_t vacc0x01 = vreinterpretq_s32_u64(vmovl_u32(vld1_u32(w))); w = (const int32_t*) w + 2;
46     int32x4_t vacc0x23 = vreinterpretq_s32_u64(vmovl_u32(vld1_u32(w))); w = (const int32_t*) w + 2;
47     int32x4_t vacc0x45 = vreinterpretq_s32_u64(vmovl_u32(vld1_u32(w))); w = (const int32_t*) w + 2;
48     int32x4_t vacc0x67 = vreinterpretq_s32_u64(vmovl_u32(vld1_u32(w))); w = (const int32_t*) w + 2;
49 
50     size_t k = kc;
51     while (k >= 16 * sizeof(int8_t)) {
52       int8x8_t va0x0 = vld1_s8(a0); a0 += 8;
53       int8x8_t va0x1 = vld1_s8(a0); a0 += 8;
54 
55       const int8x8_t vb01c0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
56       const int8x8_t vb23c0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
57       const int8x8_t vb45c0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
58       const int8x8_t vb67c0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
59       const int8x8_t vb01c1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
60       const int8x8_t vb23c1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
61       const int8x8_t vb45c1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
62       const int8x8_t vb67c1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
63 
64       int16x8_t vprod0x01c0 = vmull_s8(vb01c0x0, va0x0);
65       const int8x8_t vb01c0x1 = vld1_s8(w); w = (const int8_t*) w + 8;
66       vprod0x01c0 = vmlal_s8(vprod0x01c0, vb01c0x1, va0x1);
67       vacc0x01 = vpadalq_s16(vacc0x01, vprod0x01c0);
68       int16x8_t vprod0x23c0 = vmull_s8(vb23c0x0, va0x0);
69       const int8x8_t vb23c0x1 = vld1_s8(w); w = (const int8_t*) w + 8;
70       vprod0x23c0 = vmlal_s8(vprod0x23c0, vb23c0x1, va0x1);
71       vacc0x23 = vpadalq_s16(vacc0x23, vprod0x23c0);
72       int16x8_t vprod0x45c0 = vmull_s8(vb45c0x0, va0x0);
73       const int8x8_t vb45c0x1 = vld1_s8(w); w = (const int8_t*) w + 8;
74       vprod0x45c0 = vmlal_s8(vprod0x45c0, vb45c0x1, va0x1);
75       vacc0x45 = vpadalq_s16(vacc0x45, vprod0x45c0);
76       int16x8_t vprod0x67c0 = vmull_s8(vb67c0x0, va0x0);
77       const int8x8_t vb67c0x1 = vld1_s8(w); w = (const int8_t*) w + 8;
78       vprod0x67c0 = vmlal_s8(vprod0x67c0, vb67c0x1, va0x1);
79       vacc0x67 = vpadalq_s16(vacc0x67, vprod0x67c0);
80       va0x0 = vext_s8(va0x0, va0x0, 4);
81       va0x1 = vext_s8(va0x1, va0x1, 4);
82       int16x8_t vprod0x01c1 = vmull_s8(vb01c1x0, va0x0);
83       const int8x8_t vb01c1x1 = vld1_s8(w); w = (const int8_t*) w + 8;
84       vprod0x01c1 = vmlal_s8(vprod0x01c1, vb01c1x1, va0x1);
85       vacc0x01 = vpadalq_s16(vacc0x01, vprod0x01c1);
86       int16x8_t vprod0x23c1 = vmull_s8(vb23c1x0, va0x0);
87       const int8x8_t vb23c1x1 = vld1_s8(w); w = (const int8_t*) w + 8;
88       vprod0x23c1 = vmlal_s8(vprod0x23c1, vb23c1x1, va0x1);
89       vacc0x23 = vpadalq_s16(vacc0x23, vprod0x23c1);
90       int16x8_t vprod0x45c1 = vmull_s8(vb45c1x0, va0x0);
91       const int8x8_t vb45c1x1 = vld1_s8(w); w = (const int8_t*) w + 8;
92       vprod0x45c1 = vmlal_s8(vprod0x45c1, vb45c1x1, va0x1);
93       vacc0x45 = vpadalq_s16(vacc0x45, vprod0x45c1);
94       int16x8_t vprod0x67c1 = vmull_s8(vb67c1x0, va0x0);
95       const int8x8_t vb67c1x1 = vld1_s8(w); w = (const int8_t*) w + 8;
96       vprod0x67c1 = vmlal_s8(vprod0x67c1, vb67c1x1, va0x1);
97       vacc0x67 = vpadalq_s16(vacc0x67, vprod0x67c1);
98 
99       k -= 16 * sizeof(int8_t);
100     }
101     if (k != 0) {
102       int8x8_t va0x0 = vld1_s8(a0); a0 += 8;
103 
104       const int8x8_t vb01c0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
105       const int8x8_t vb23c0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
106       const int8x8_t vb45c0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
107       const int8x8_t vb67c0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
108       const int8x8_t vb01c1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
109       const int8x8_t vb23c1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
110       const int8x8_t vb45c1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
111       const int8x8_t vb67c1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
112 
113       int16x8_t vprod0x01c0 = vmull_s8(vb01c0x0, va0x0);
114       vacc0x01 = vpadalq_s16(vacc0x01, vprod0x01c0);
115       int16x8_t vprod0x23c0 = vmull_s8(vb23c0x0, va0x0);
116       vacc0x23 = vpadalq_s16(vacc0x23, vprod0x23c0);
117       int16x8_t vprod0x45c0 = vmull_s8(vb45c0x0, va0x0);
118       vacc0x45 = vpadalq_s16(vacc0x45, vprod0x45c0);
119       int16x8_t vprod0x67c0 = vmull_s8(vb67c0x0, va0x0);
120       vacc0x67 = vpadalq_s16(vacc0x67, vprod0x67c0);
121       va0x0 = vext_s8(va0x0, va0x0, 4);
122       int16x8_t vprod0x01c1 = vmull_s8(vb01c1x0, va0x0);
123       vacc0x01 = vpadalq_s16(vacc0x01, vprod0x01c1);
124       int16x8_t vprod0x23c1 = vmull_s8(vb23c1x0, va0x0);
125       vacc0x23 = vpadalq_s16(vacc0x23, vprod0x23c1);
126       int16x8_t vprod0x45c1 = vmull_s8(vb45c1x0, va0x0);
127       vacc0x45 = vpadalq_s16(vacc0x45, vprod0x45c1);
128       int16x8_t vprod0x67c1 = vmull_s8(vb67c1x0, va0x0);
129       vacc0x67 = vpadalq_s16(vacc0x67, vprod0x67c1);
130 
131     }
132 
133 #if XNN_ARCH_ARM64
134     int32x4_t vacc0x0123 = vpaddq_s32(vacc0x01, vacc0x23);
135     int32x4_t vacc0x4567 = vpaddq_s32(vacc0x45, vacc0x67);
136 #else
137     const int32x2_t vsum0x01 = vpadd_s32(vget_low_s32(vacc0x01), vget_high_s32(vacc0x01));
138     const int32x2_t vsum0x23 = vpadd_s32(vget_low_s32(vacc0x23), vget_high_s32(vacc0x23));
139     int32x4_t vacc0x0123 = vcombine_s32(vsum0x01, vsum0x23);
140     const int32x2_t vsum0x45 = vpadd_s32(vget_low_s32(vacc0x45), vget_high_s32(vacc0x45));
141     const int32x2_t vsum0x67 = vpadd_s32(vget_low_s32(vacc0x67), vget_high_s32(vacc0x67));
142     int32x4_t vacc0x4567 = vcombine_s32(vsum0x45, vsum0x67);
143 #endif
144 
145     float32x4_t vfpacc0x0123 = vcvtq_f32_s32(vacc0x0123);
146     float32x4_t vfpacc0x4567 = vcvtq_f32_s32(vacc0x4567);
147 
148     const float32x4_t vscale0123 = vld1q_f32(w); w = (const float*) w + 4;
149     vfpacc0x0123 = vmulq_f32(vfpacc0x0123, vscale0123);
150     const float32x4_t vscale4567 = vld1q_f32(w); w = (const float*) w + 4;
151     vfpacc0x4567 = vmulq_f32(vfpacc0x4567, vscale4567);
152 
153     vacc0x0123 = vcvtnq_s32_f32(vfpacc0x0123);
154     vacc0x4567 = vcvtnq_s32_f32(vfpacc0x4567);
155 
156     const int16x8_t voutput_zero_point = vld1q_dup_s16(&params->fp32_neonv8.output_zero_point);
157 #if XNN_ARCH_ARM64
158     int16x8_t vacc0x01234567 = vqmovn_high_s32(vqmovn_s32(vacc0x0123), vacc0x4567);
159 
160     vacc0x01234567 = vqaddq_s16(vacc0x01234567, voutput_zero_point);
161 
162     int8x8_t vout0x01234567 = vqmovn_s16(vacc0x01234567);
163 #else
164     int16x8_t vacc0x01234567 = vcombine_s16(vqmovn_s32(vacc0x0123), vqmovn_s32(vacc0x4567));
165 
166     vacc0x01234567 = vqaddq_s16(vacc0x01234567, voutput_zero_point);
167 
168     int8x8_t vout0x01234567 = vqmovn_s16(vacc0x01234567);
169 #endif
170 
171     const int8x8_t voutput_min = vld1_dup_s8(&params->fp32_neonv8.output_min);
172     vout0x01234567 = vmax_s8(vout0x01234567, voutput_min);
173 
174     const int8x8_t voutput_max = vld1_dup_s8(&params->fp32_neonv8.output_max);
175     vout0x01234567 = vmin_s8(vout0x01234567, voutput_max);
176 
177     if (nc >= 8) {
178       vst1_s8(c0 + 0, vout0x01234567);
179 
180       c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
181 
182       a0 = (const int8_t*) ((uintptr_t) a0 - kc);
183 
184       nc -= 8;
185     } else {
186       // Final case where not all of the 8 columns fit in the destination.
187       if (nc & 4) {
188         vst1_lane_u32((void*) c0, vreinterpret_u32_s8(vout0x01234567), 0); c0 += 4;
189         vout0x01234567 = vext_s8(vout0x01234567, vout0x01234567, 4);
190       }
191       if (nc & 2) {
192         vst1_lane_u16((void*) c0, vreinterpret_u16_s8(vout0x01234567), 0); c0 += 2;
193         vout0x01234567 = vext_s8(vout0x01234567, vout0x01234567, 2);
194       }
195       if (nc & 1) {
196         vst1_lane_s8(c0, vout0x01234567, 0);
197       }
198 
199       nc = 0;
200     }
201   } while (nc != 0);
202 }
203