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