xref: /aosp_15_r20/external/XNNPACK/src/qs8-igemm/gen/3x8c2-minmax-rndnu-neon-mull-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/math.h>
16 
xnn_qs8_igemm_minmax_rndnu_ukernel_3x8c2__neon_mull_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_qs8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])17 void xnn_qs8_igemm_minmax_rndnu_ukernel_3x8c2__neon_mull_ld2r(
18     size_t mr,
19     size_t nc,
20     size_t kc,
21     size_t ks,
22     const int8_t** restrict a,
23     const void* restrict w,
24     int8_t* restrict c,
25     size_t cm_stride,
26     size_t cn_stride,
27     size_t a_offset,
28     const int8_t* zero,
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 <= 3);
33   assert(nc != 0);
34   assert(kc != 0);
35   assert(ks != 0);
36   assert(ks % (3 * sizeof(void*)) == 0);
37   assert(a_offset % sizeof(int8_t) == 0);
38   assert(a != NULL);
39   assert(w != NULL);
40   assert(c != NULL);
41 
42   kc = round_up_po2(kc, 2 * sizeof(int8_t));
43   int8_t* c0 = c;
44   int8_t* c1 = (int8_t*) ((uintptr_t) c0 + cm_stride);
45   if XNN_UNPREDICTABLE(mr < 2) {
46     c1 = c0;
47   }
48   int8_t* c2 = (int8_t*) ((uintptr_t) c1 + cm_stride);
49   if XNN_UNPREDICTABLE(mr <= 2) {
50     c2 = c1;
51   }
52 
53   do {
54     int32x4_t vacc0x0123 = vld1q_s32(w); w = (const void*) ((uintptr_t) w + 4 * sizeof(int32_t));
55     int32x4_t vacc0x4567 = vld1q_s32(w); w = (const void*) ((uintptr_t) w + 4 * sizeof(int32_t));
56     int32x4_t vacc1x0123 = vacc0x0123;
57     int32x4_t vacc1x4567 = vacc0x4567;
58     int32x4_t vacc2x0123 = vacc0x0123;
59     int32x4_t vacc2x4567 = vacc0x4567;
60 
61     size_t p = ks;
62     do {
63       const int8_t* restrict a0 = a[0];
64       if XNN_UNPREDICTABLE(a0 != zero) {
65         a0 = (const int8_t*) ((uintptr_t) a0 + a_offset);
66       }
67       const int8_t* restrict a1 = a[1];
68       if XNN_UNPREDICTABLE(a1 != zero) {
69         a1 = (const int8_t*) ((uintptr_t) a1 + a_offset);
70       }
71       const int8_t* restrict a2 = a[2];
72       if XNN_UNPREDICTABLE(a2 != zero) {
73         a2 = (const int8_t*) ((uintptr_t) a2 + a_offset);
74       }
75       a += 3;
76 
77       size_t k = kc;
78 
79 
80       while (k >= 8 * sizeof(int8_t)) {
81         const int16x4x2_t va00 = vld2_dup_s16((const void*)a0);
82         const int16x4x2_t va01 = vld2_dup_s16((const void*)(a0 + 4)); a0 += 8;
83         const int16x4x2_t va10 = vld2_dup_s16((const void*)a1);
84         const int16x4x2_t va11 = vld2_dup_s16((const void*)(a1 + 4)); a1 += 8;
85         const int16x4x2_t va20 = vld2_dup_s16((const void*)a2);
86         const int16x4x2_t va21 = vld2_dup_s16((const void*)(a2 + 4)); a2 += 8;
87 
88         const int8x8_t vb0123c0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
89         const int8x8_t vb4567c0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
90         const int8x8_t vb0123c1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
91         const int8x8_t vb4567c1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
92         const int8x8_t vb0123c2 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
93         const int8x8_t vb4567c2 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
94         const int8x8_t vb0123c3 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
95         const int8x8_t vb4567c3 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
96 
97         const int8x8_t va0c0 = vreinterpret_s8_s16(va00.val[0]);
98         const int8x8_t va1c0 = vreinterpret_s8_s16(va10.val[0]);
99         const int8x8_t va2c0 = vreinterpret_s8_s16(va20.val[0]);
100 
101         const int16x8_t vprod0x0123c0 = vmull_s8(vb0123c0, va0c0);
102         const int16x8_t vprod1x0123c0 = vmull_s8(vb0123c0, va1c0);
103         const int16x8_t vprod2x0123c0 = vmull_s8(vb0123c0, va2c0);
104         vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c0);
105         vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c0);
106         vacc2x0123 = vpadalq_s16(vacc2x0123, vprod2x0123c0);
107         const int16x8_t vprod0x4567c0 = vmull_s8(vb4567c0, va0c0);
108         const int16x8_t vprod1x4567c0 = vmull_s8(vb4567c0, va1c0);
109         const int16x8_t vprod2x4567c0 = vmull_s8(vb4567c0, va2c0);
110         vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c0);
111         vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c0);
112         vacc2x4567 = vpadalq_s16(vacc2x4567, vprod2x4567c0);
113         const int8x8_t va0c1 = vreinterpret_s8_s16(va00.val[1]);
114         const int8x8_t va1c1 = vreinterpret_s8_s16(va10.val[1]);
115         const int8x8_t va2c1 = vreinterpret_s8_s16(va20.val[1]);
116 
117         const int16x8_t vprod0x0123c1 = vmull_s8(vb0123c1, va0c1);
118         const int16x8_t vprod1x0123c1 = vmull_s8(vb0123c1, va1c1);
119         const int16x8_t vprod2x0123c1 = vmull_s8(vb0123c1, va2c1);
120         vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c1);
121         vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c1);
122         vacc2x0123 = vpadalq_s16(vacc2x0123, vprod2x0123c1);
123         const int16x8_t vprod0x4567c1 = vmull_s8(vb4567c1, va0c1);
124         const int16x8_t vprod1x4567c1 = vmull_s8(vb4567c1, va1c1);
125         const int16x8_t vprod2x4567c1 = vmull_s8(vb4567c1, va2c1);
126         vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c1);
127         vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c1);
128         vacc2x4567 = vpadalq_s16(vacc2x4567, vprod2x4567c1);
129         const int8x8_t va0c2 = vreinterpret_s8_s16(va01.val[0]);
130         const int8x8_t va1c2 = vreinterpret_s8_s16(va11.val[0]);
131         const int8x8_t va2c2 = vreinterpret_s8_s16(va21.val[0]);
132 
133         const int16x8_t vprod0x0123c2 = vmull_s8(vb0123c2, va0c2);
134         const int16x8_t vprod1x0123c2 = vmull_s8(vb0123c2, va1c2);
135         const int16x8_t vprod2x0123c2 = vmull_s8(vb0123c2, va2c2);
136         vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c2);
137         vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c2);
138         vacc2x0123 = vpadalq_s16(vacc2x0123, vprod2x0123c2);
139         const int16x8_t vprod0x4567c2 = vmull_s8(vb4567c2, va0c2);
140         const int16x8_t vprod1x4567c2 = vmull_s8(vb4567c2, va1c2);
141         const int16x8_t vprod2x4567c2 = vmull_s8(vb4567c2, va2c2);
142         vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c2);
143         vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c2);
144         vacc2x4567 = vpadalq_s16(vacc2x4567, vprod2x4567c2);
145         const int8x8_t va0c3 = vreinterpret_s8_s16(va01.val[1]);
146         const int8x8_t va1c3 = vreinterpret_s8_s16(va11.val[1]);
147         const int8x8_t va2c3 = vreinterpret_s8_s16(va21.val[1]);
148 
149         const int16x8_t vprod0x0123c3 = vmull_s8(vb0123c3, va0c3);
150         const int16x8_t vprod1x0123c3 = vmull_s8(vb0123c3, va1c3);
151         const int16x8_t vprod2x0123c3 = vmull_s8(vb0123c3, va2c3);
152         vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c3);
153         vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c3);
154         vacc2x0123 = vpadalq_s16(vacc2x0123, vprod2x0123c3);
155         const int16x8_t vprod0x4567c3 = vmull_s8(vb4567c3, va0c3);
156         const int16x8_t vprod1x4567c3 = vmull_s8(vb4567c3, va1c3);
157         const int16x8_t vprod2x4567c3 = vmull_s8(vb4567c3, va2c3);
158         vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c3);
159         vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c3);
160         vacc2x4567 = vpadalq_s16(vacc2x4567, vprod2x4567c3);
161 
162         k -= 8 * sizeof(int8_t);
163       }
164 
165       if XNN_UNLIKELY(k != 0) {
166         const int8x8_t va0 = vld1_s8(a0); a0 = (const int8_t*) ((uintptr_t) a0 + k);
167         const int8x8_t va1 = vld1_s8(a1); a1 = (const int8_t*) ((uintptr_t) a1 + k);
168         const int8x8_t va2 = vld1_s8(a2); a2 = (const int8_t*) ((uintptr_t) a2 + k);
169 
170         const int8x8_t vb0123c0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
171         const int8x8_t vb4567c0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
172 
173         const int8x8_t va0c0 = vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(va0), 0));
174         const int16x8_t vprod0x0123c0 = vmull_s8(vb0123c0, va0c0);
175         vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c0);
176         const int16x8_t vprod0x4567c0 = vmull_s8(vb4567c0, va0c0);
177         vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c0);
178         const int8x8_t va1c0 = vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(va1), 0));
179         const int16x8_t vprod1x0123c0 = vmull_s8(vb0123c0, va1c0);
180         vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c0);
181         const int16x8_t vprod1x4567c0 = vmull_s8(vb4567c0, va1c0);
182         vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c0);
183         const int8x8_t va2c0 = vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(va2), 0));
184         const int16x8_t vprod2x0123c0 = vmull_s8(vb0123c0, va2c0);
185         vacc2x0123 = vpadalq_s16(vacc2x0123, vprod2x0123c0);
186         const int16x8_t vprod2x4567c0 = vmull_s8(vb4567c0, va2c0);
187         vacc2x4567 = vpadalq_s16(vacc2x4567, vprod2x4567c0);
188 
189         if (k > 2 * sizeof(int8_t)) {
190           const int8x8_t vb0123c1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
191           const int8x8_t vb4567c1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
192 
193           const int8x8_t va0c1 = vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(va0), 1));
194           const int16x8_t vprod0x0123c1 = vmull_s8(vb0123c1, va0c1);
195           vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c1);
196           const int16x8_t vprod0x4567c1 = vmull_s8(vb4567c1, va0c1);
197           vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c1);
198           const int8x8_t va1c1 = vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(va1), 1));
199           const int16x8_t vprod1x0123c1 = vmull_s8(vb0123c1, va1c1);
200           vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c1);
201           const int16x8_t vprod1x4567c1 = vmull_s8(vb4567c1, va1c1);
202           vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c1);
203           const int8x8_t va2c1 = vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(va2), 1));
204           const int16x8_t vprod2x0123c1 = vmull_s8(vb0123c1, va2c1);
205           vacc2x0123 = vpadalq_s16(vacc2x0123, vprod2x0123c1);
206           const int16x8_t vprod2x4567c1 = vmull_s8(vb4567c1, va2c1);
207           vacc2x4567 = vpadalq_s16(vacc2x4567, vprod2x4567c1);
208 
209           if (k > 4 * sizeof(int8_t)) {
210             const int8x8_t vb0123c2 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
211             const int8x8_t vb4567c2 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
212 
213             const int8x8_t va0c2 = vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(va0), 2));
214             const int16x8_t vprod0x0123c2 = vmull_s8(vb0123c2, va0c2);
215             vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c2);
216             const int16x8_t vprod0x4567c2 = vmull_s8(vb4567c2, va0c2);
217             vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c2);
218             const int8x8_t va1c2 = vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(va1), 2));
219             const int16x8_t vprod1x0123c2 = vmull_s8(vb0123c2, va1c2);
220             vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c2);
221             const int16x8_t vprod1x4567c2 = vmull_s8(vb4567c2, va1c2);
222             vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c2);
223             const int8x8_t va2c2 = vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(va2), 2));
224             const int16x8_t vprod2x0123c2 = vmull_s8(vb0123c2, va2c2);
225             vacc2x0123 = vpadalq_s16(vacc2x0123, vprod2x0123c2);
226             const int16x8_t vprod2x4567c2 = vmull_s8(vb4567c2, va2c2);
227             vacc2x4567 = vpadalq_s16(vacc2x4567, vprod2x4567c2);
228           }
229         }
230       }
231       p -= 3 * sizeof(void*);
232     } while (p != 0);
233 
234     const int32x4_t vright_pre_shift = vld1q_dup_s32(&params->rndnu_neon.right_pre_shift);
235     const int32x4_t vmultiplier = vld1q_dup_s32(&params->rndnu_neon.multiplier);
236     const int32x4_t vright_post_shift = vld1q_dup_s32(&params->rndnu_neon.right_post_shift);
237 
238     vacc0x0123 = vqshlq_s32(vacc0x0123, vright_pre_shift);
239     vacc0x4567 = vqshlq_s32(vacc0x4567, vright_pre_shift);
240     vacc1x0123 = vqshlq_s32(vacc1x0123, vright_pre_shift);
241     vacc1x4567 = vqshlq_s32(vacc1x4567, vright_pre_shift);
242     vacc2x0123 = vqshlq_s32(vacc2x0123, vright_pre_shift);
243     vacc2x4567 = vqshlq_s32(vacc2x4567, vright_pre_shift);
244 
245     vacc0x0123 = vqdmulhq_s32(vacc0x0123, vmultiplier);
246     vacc0x4567 = vqdmulhq_s32(vacc0x4567, vmultiplier);
247     vacc1x0123 = vqdmulhq_s32(vacc1x0123, vmultiplier);
248     vacc1x4567 = vqdmulhq_s32(vacc1x4567, vmultiplier);
249     vacc2x0123 = vqdmulhq_s32(vacc2x0123, vmultiplier);
250     vacc2x4567 = vqdmulhq_s32(vacc2x4567, vmultiplier);
251 
252     vacc0x0123 = vrshlq_s32(vacc0x0123, vright_post_shift);
253     vacc0x4567 = vrshlq_s32(vacc0x4567, vright_post_shift);
254     vacc1x0123 = vrshlq_s32(vacc1x0123, vright_post_shift);
255     vacc1x4567 = vrshlq_s32(vacc1x4567, vright_post_shift);
256     vacc2x0123 = vrshlq_s32(vacc2x0123, vright_post_shift);
257     vacc2x4567 = vrshlq_s32(vacc2x4567, vright_post_shift);
258 
259     const int16x8_t voutput_zero_point = vld1q_dup_s16(&params->rndnu_neon.output_zero_point);
260 #if XNN_ARCH_ARM64
261     int16x8_t vacc0x01234567 = vqmovn_high_s32(vqmovn_s32(vacc0x0123), vacc0x4567);
262     int16x8_t vacc1x01234567 = vqmovn_high_s32(vqmovn_s32(vacc1x0123), vacc1x4567);
263     int16x8_t vacc2x01234567 = vqmovn_high_s32(vqmovn_s32(vacc2x0123), vacc2x4567);
264 
265     vacc0x01234567 = vqaddq_s16(vacc0x01234567, voutput_zero_point);
266     vacc1x01234567 = vqaddq_s16(vacc1x01234567, voutput_zero_point);
267     vacc2x01234567 = vqaddq_s16(vacc2x01234567, voutput_zero_point);
268 
269     int8x16_t vout0x01234567_1x01234567 = vqmovn_high_s16(vqmovn_s16(vacc0x01234567), vacc1x01234567);
270     int8x8_t vout2x01234567 = vqmovn_s16(vacc2x01234567);
271 #else
272     int16x8_t vacc0x01234567 = vcombine_s16(vqmovn_s32(vacc0x0123), vqmovn_s32(vacc0x4567));
273     int16x8_t vacc1x01234567 = vcombine_s16(vqmovn_s32(vacc1x0123), vqmovn_s32(vacc1x4567));
274     int16x8_t vacc2x01234567 = vcombine_s16(vqmovn_s32(vacc2x0123), vqmovn_s32(vacc2x4567));
275 
276     vacc0x01234567 = vqaddq_s16(vacc0x01234567, voutput_zero_point);
277     vacc1x01234567 = vqaddq_s16(vacc1x01234567, voutput_zero_point);
278     vacc2x01234567 = vqaddq_s16(vacc2x01234567, voutput_zero_point);
279 
280     int8x16_t vout0x01234567_1x01234567 = vcombine_s8(vqmovn_s16(vacc0x01234567), vqmovn_s16(vacc1x01234567));
281     int8x8_t vout2x01234567 = vqmovn_s16(vacc2x01234567);
282 #endif
283 
284     const int8x16_t voutput_min = vld1q_dup_s8(&params->rndnu_neon.output_min);
285     vout0x01234567_1x01234567 = vmaxq_s8(vout0x01234567_1x01234567, voutput_min);
286     vout2x01234567 = vmax_s8(vout2x01234567, vget_low_s8(voutput_min));
287 
288     const int8x16_t voutput_max = vld1q_dup_s8(&params->rndnu_neon.output_max);
289     vout0x01234567_1x01234567 = vminq_s8(vout0x01234567_1x01234567, voutput_max);
290     vout2x01234567 = vmin_s8(vout2x01234567, vget_low_s8(voutput_max));
291 
292     if (nc >= 8) {
293       vst1_s8(c2 + 0, vout2x01234567);
294       vst1_s8(c1 + 0, vget_high_s8(vout0x01234567_1x01234567));
295       vst1_s8(c0 + 0, vget_low_s8(vout0x01234567_1x01234567));
296 
297       c2 = (int8_t*) ((uintptr_t) c2 + cn_stride);
298       c1 = (int8_t*) ((uintptr_t) c1 + cn_stride);
299       c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
300 
301       a = (const int8_t**restrict) ((uintptr_t) a - ks);
302 
303       nc -= 8;
304     } else {
305       if (nc & 4) {
306         vst1_lane_u32((void*) c2, vreinterpret_u32_s8(vout2x01234567), 0); c2 += 4;
307         vst1q_lane_u32((void*) c1, vreinterpretq_u32_s8(vout0x01234567_1x01234567), 2); c1 += 4;
308         vst1q_lane_u32((void*) c0, vreinterpretq_u32_s8(vout0x01234567_1x01234567), 0); c0 += 4;
309         vout2x01234567 = vext_s8(vout2x01234567, vout2x01234567, 4);
310         vout0x01234567_1x01234567 = vextq_s8(vout0x01234567_1x01234567, vout0x01234567_1x01234567, 4);
311       }
312       if (nc & 2) {
313         vst1_lane_u16((void*) c2, vreinterpret_u16_s8(vout2x01234567), 0); c2 += 2;
314         vst1q_lane_u16((void*) c1, vreinterpretq_u16_s8(vout0x01234567_1x01234567), 4); c1 += 2;
315         vst1q_lane_u16((void*) c0, vreinterpretq_u16_s8(vout0x01234567_1x01234567), 0); c0 += 2;
316         vout2x01234567 = vext_s8(vout2x01234567, vout2x01234567, 2);
317         vout0x01234567_1x01234567 = vextq_s8(vout0x01234567_1x01234567, vout0x01234567_1x01234567, 2);
318       }
319       if (nc & 1) {
320         vst1_lane_s8(c2, vout2x01234567, 0);
321         vst1q_lane_s8(c1, vout0x01234567_1x01234567, 8);
322         vst1q_lane_s8(c0, vout0x01234567_1x01234567, 0);
323       }
324 
325       nc = 0;
326     }
327   } while (nc != 0);
328 }
329