xref: /aosp_15_r20/external/XNNPACK/src/qc8-igemm/gen/1x8c2-minmax-fp32-neonv8-mlal-ld2r.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Auto-generated file. Do not edit!
2 //   Template: src/qs8-igemm/c2-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 
xnn_qc8_igemm_minmax_fp32_ukernel_1x8c2__neonv8_mlal_ld2r(size_t mr,size_t nc,size_t kc,size_t ks,const int8_t ** restrict a,const void * restrict w,int8_t * restrict c,size_t cm_stride,size_t cn_stride,size_t a_offset,const int8_t * zero,const union xnn_qc8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])18 void xnn_qc8_igemm_minmax_fp32_ukernel_1x8c2__neonv8_mlal_ld2r(
19     size_t mr,
20     size_t nc,
21     size_t kc,
22     size_t ks,
23     const int8_t** restrict a,
24     const void* restrict w,
25     int8_t* restrict c,
26     size_t cm_stride,
27     size_t cn_stride,
28     size_t a_offset,
29     const int8_t* zero,
30     const union xnn_qc8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
31 {
32   assert(mr != 0);
33   assert(mr <= 1);
34   assert(nc != 0);
35   assert(kc != 0);
36   assert(ks != 0);
37   assert(ks % (1 * sizeof(void*)) == 0);
38   assert(a_offset % sizeof(int8_t) == 0);
39   assert(a != NULL);
40   assert(w != NULL);
41   assert(c != NULL);
42 
43   kc = round_up_po2(kc, 2 * sizeof(int8_t));
44   int8_t* c0 = c;
45 
46   do {
47     int32x4_t vacc0x0123 = vld1q_s32(w); w = (const void*) ((uintptr_t) w + 4 * sizeof(int32_t));
48     int32x4_t vacc0x4567 = vld1q_s32(w); w = (const void*) ((uintptr_t) w + 4 * sizeof(int32_t));
49 
50     size_t p = ks;
51     do {
52       const int8_t* restrict a0 = a[0];
53       if XNN_UNPREDICTABLE(a0 != zero) {
54         a0 = (const int8_t*) ((uintptr_t) a0 + a_offset);
55       }
56       a += 1;
57 
58       size_t k = kc;
59 
60       while (k >= 16 * sizeof(int8_t)) {
61         const int16x4x2_t va00x0 = vld2_dup_s16((const void*)a0);
62         const int16x4x2_t va01x0 = vld2_dup_s16((const void*)(a0 + 4)); a0 += 8;
63         const int16x4x2_t va00x1 = vld2_dup_s16((const void*)a0);
64         const int16x4x2_t va01x1 = vld2_dup_s16((const void*)(a0 + 4)); a0 += 8;
65 
66         const int8x8_t vb0123c0x0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
67         const int8x8_t vb4567c0x0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
68         const int8x8_t vb0123c1x0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
69         const int8x8_t vb4567c1x0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
70         const int8x8_t vb0123c2x0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
71         const int8x8_t vb4567c2x0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
72         const int8x8_t vb0123c3x0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
73         const int8x8_t vb4567c3x0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
74 
75         const int8x8_t va0c0x0 = vreinterpret_s8_s16(va00x0.val[0]);
76         const int8x8_t va0c0x1 = vreinterpret_s8_s16(va00x1.val[0]);
77 
78         int16x8_t vprod0x0123c0 = vmull_s8(vb0123c0x0, va0c0x0);
79         const int8x8_t vb0123c0x1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
80         vprod0x0123c0 = vmlal_s8(vprod0x0123c0, vb0123c0x1, va0c0x1);
81         vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c0);
82         int16x8_t vprod0x4567c0 = vmull_s8(vb4567c0x0, va0c0x0);
83         const int8x8_t vb4567c0x1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
84         vprod0x4567c0 = vmlal_s8(vprod0x4567c0, vb4567c0x1, va0c0x1);
85         vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c0);
86         const int8x8_t va0c1x0 = vreinterpret_s8_s16(va00x0.val[1]);
87         const int8x8_t va0c1x1 = vreinterpret_s8_s16(va00x1.val[1]);
88 
89         int16x8_t vprod0x0123c1 = vmull_s8(vb0123c1x0, va0c1x0);
90         const int8x8_t vb0123c1x1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
91         vprod0x0123c1 = vmlal_s8(vprod0x0123c1, vb0123c1x1, va0c1x1);
92         vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c1);
93         int16x8_t vprod0x4567c1 = vmull_s8(vb4567c1x0, va0c1x0);
94         const int8x8_t vb4567c1x1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
95         vprod0x4567c1 = vmlal_s8(vprod0x4567c1, vb4567c1x1, va0c1x1);
96         vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c1);
97         const int8x8_t va0c2x0 = vreinterpret_s8_s16(va01x0.val[0]);
98         const int8x8_t va0c2x1 = vreinterpret_s8_s16(va01x1.val[0]);
99 
100         int16x8_t vprod0x0123c2 = vmull_s8(vb0123c2x0, va0c2x0);
101         const int8x8_t vb0123c2x1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
102         vprod0x0123c2 = vmlal_s8(vprod0x0123c2, vb0123c2x1, va0c2x1);
103         vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c2);
104         int16x8_t vprod0x4567c2 = vmull_s8(vb4567c2x0, va0c2x0);
105         const int8x8_t vb4567c2x1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
106         vprod0x4567c2 = vmlal_s8(vprod0x4567c2, vb4567c2x1, va0c2x1);
107         vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c2);
108         const int8x8_t va0c3x0 = vreinterpret_s8_s16(va01x0.val[1]);
109         const int8x8_t va0c3x1 = vreinterpret_s8_s16(va01x1.val[1]);
110 
111         int16x8_t vprod0x0123c3 = vmull_s8(vb0123c3x0, va0c3x0);
112         const int8x8_t vb0123c3x1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
113         vprod0x0123c3 = vmlal_s8(vprod0x0123c3, vb0123c3x1, va0c3x1);
114         vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c3);
115         int16x8_t vprod0x4567c3 = vmull_s8(vb4567c3x0, va0c3x0);
116         const int8x8_t vb4567c3x1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
117         vprod0x4567c3 = vmlal_s8(vprod0x4567c3, vb4567c3x1, va0c3x1);
118         vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c3);
119 
120         k -= 16 * sizeof(int8_t);
121       }
122 
123       if (k >= 8 * sizeof(int8_t)) {
124         const int16x4x2_t va00 = vld2_dup_s16((const void*)a0);
125         const int16x4x2_t va01 = vld2_dup_s16((const void*)(a0 + 4)); a0 += 8;
126 
127         const int8x8_t vb0123c0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
128         const int8x8_t vb4567c0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
129         const int8x8_t vb0123c1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
130         const int8x8_t vb4567c1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
131         const int8x8_t vb0123c2 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
132         const int8x8_t vb4567c2 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
133         const int8x8_t vb0123c3 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
134         const int8x8_t vb4567c3 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
135 
136         const int8x8_t va0c0 = vreinterpret_s8_s16(va00.val[0]);
137 
138         const int16x8_t vprod0x0123c0 = vmull_s8(vb0123c0, va0c0);
139         vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c0);
140         const int16x8_t vprod0x4567c0 = vmull_s8(vb4567c0, va0c0);
141         vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c0);
142         const int8x8_t va0c1 = vreinterpret_s8_s16(va00.val[1]);
143 
144         const int16x8_t vprod0x0123c1 = vmull_s8(vb0123c1, va0c1);
145         vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c1);
146         const int16x8_t vprod0x4567c1 = vmull_s8(vb4567c1, va0c1);
147         vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c1);
148         const int8x8_t va0c2 = vreinterpret_s8_s16(va01.val[0]);
149 
150         const int16x8_t vprod0x0123c2 = vmull_s8(vb0123c2, va0c2);
151         vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c2);
152         const int16x8_t vprod0x4567c2 = vmull_s8(vb4567c2, va0c2);
153         vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c2);
154         const int8x8_t va0c3 = vreinterpret_s8_s16(va01.val[1]);
155 
156         const int16x8_t vprod0x0123c3 = vmull_s8(vb0123c3, va0c3);
157         vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c3);
158         const int16x8_t vprod0x4567c3 = vmull_s8(vb4567c3, va0c3);
159         vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c3);
160 
161         k -= 8 * sizeof(int8_t);
162       }
163 
164       if XNN_UNLIKELY(k != 0) {
165         const int8x8_t va0 = vld1_s8(a0); a0 = (const int8_t*) ((uintptr_t) a0 + k);
166 
167         const int8x8_t vb0123c0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
168         const int8x8_t vb4567c0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
169 
170         const int8x8_t va0c0 = vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(va0), 0));
171         const int16x8_t vprod0x0123c0 = vmull_s8(vb0123c0, va0c0);
172         vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c0);
173         const int16x8_t vprod0x4567c0 = vmull_s8(vb4567c0, va0c0);
174         vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c0);
175 
176         if (k > 2 * sizeof(int8_t)) {
177           const int8x8_t vb0123c1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
178           const int8x8_t vb4567c1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
179 
180           const int8x8_t va0c1 = vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(va0), 1));
181           const int16x8_t vprod0x0123c1 = vmull_s8(vb0123c1, va0c1);
182           vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c1);
183           const int16x8_t vprod0x4567c1 = vmull_s8(vb4567c1, va0c1);
184           vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c1);
185 
186           if (k > 4 * sizeof(int8_t)) {
187             const int8x8_t vb0123c2 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
188             const int8x8_t vb4567c2 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
189 
190             const int8x8_t va0c2 = vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(va0), 2));
191             const int16x8_t vprod0x0123c2 = vmull_s8(vb0123c2, va0c2);
192             vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c2);
193             const int16x8_t vprod0x4567c2 = vmull_s8(vb4567c2, va0c2);
194             vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c2);
195           }
196         }
197       }
198       p -= 1 * sizeof(void*);
199     } while (p != 0);
200 
201     float32x4_t vfpacc0x0123 = vcvtq_f32_s32(vacc0x0123);
202     float32x4_t vfpacc0x4567 = vcvtq_f32_s32(vacc0x4567);
203 
204     const float32x4_t vscale0123 = vld1q_f32((const float*) w); w = (const void*) ((const float*) w + 4);
205     vfpacc0x0123 = vmulq_f32(vfpacc0x0123, vscale0123);
206     const float32x4_t vscale4567 = vld1q_f32((const float*) w); w = (const void*) ((const float*) w + 4);
207     vfpacc0x4567 = vmulq_f32(vfpacc0x4567, vscale4567);
208 
209     vacc0x0123 = vcvtnq_s32_f32(vfpacc0x0123);
210     vacc0x4567 = vcvtnq_s32_f32(vfpacc0x4567);
211 
212     const int16x8_t voutput_zero_point = vld1q_dup_s16(&params->fp32_neonv8.output_zero_point);
213 #if XNN_ARCH_ARM64
214     int16x8_t vacc0x01234567 = vqmovn_high_s32(vqmovn_s32(vacc0x0123), vacc0x4567);
215 
216     vacc0x01234567 = vqaddq_s16(vacc0x01234567, voutput_zero_point);
217 
218     int8x8_t vout0x01234567 = vqmovn_s16(vacc0x01234567);
219 #else
220     int16x8_t vacc0x01234567 = vcombine_s16(vqmovn_s32(vacc0x0123), vqmovn_s32(vacc0x4567));
221 
222     vacc0x01234567 = vqaddq_s16(vacc0x01234567, voutput_zero_point);
223 
224     int8x8_t vout0x01234567 = vqmovn_s16(vacc0x01234567);
225 #endif
226 
227     const int8x8_t voutput_min = vld1_dup_s8(&params->fp32_neonv8.output_min);
228     vout0x01234567 = vmax_s8(vout0x01234567, voutput_min);
229 
230     const int8x8_t voutput_max = vld1_dup_s8(&params->fp32_neonv8.output_max);
231     vout0x01234567 = vmin_s8(vout0x01234567, voutput_max);
232 
233     if (nc >= 8) {
234       vst1_s8(c0 + 0, vout0x01234567);
235 
236       c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
237 
238       a = (const int8_t**restrict) ((uintptr_t) a - ks);
239 
240       nc -= 8;
241     } else {
242       if (nc & 4) {
243         vst1_lane_u32((void*) c0, vreinterpret_u32_s8(vout0x01234567), 0); c0 += 4;
244         vout0x01234567 = vext_s8(vout0x01234567, vout0x01234567, 4);
245       }
246       if (nc & 2) {
247         vst1_lane_u16((void*) c0, vreinterpret_u16_s8(vout0x01234567), 0); c0 += 2;
248         vout0x01234567 = vext_s8(vout0x01234567, vout0x01234567, 2);
249       }
250       if (nc & 1) {
251         vst1_lane_s8(c0, vout0x01234567, 0);
252       }
253 
254       nc = 0;
255     }
256   } while (nc != 0);
257 }
258