xref: /aosp_15_r20/external/XNNPACK/src/qs8-igemm/gen/2x8c2-minmax-rndnu-neon-mull-ld1r.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_2x8c2__neon_mull_ld1r(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_2x8c2__neon_mull_ld1r(
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 <= 2);
33   assert(nc != 0);
34   assert(kc != 0);
35   assert(ks != 0);
36   assert(ks % (2 * 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 
49   do {
50     int32x4_t vacc0x0123 = vld1q_s32(w); w = (const void*) ((uintptr_t) w + 4 * sizeof(int32_t));
51     int32x4_t vacc0x4567 = vld1q_s32(w); w = (const void*) ((uintptr_t) w + 4 * sizeof(int32_t));
52     int32x4_t vacc1x0123 = vacc0x0123;
53     int32x4_t vacc1x4567 = vacc0x4567;
54 
55     size_t p = ks;
56     do {
57       const int8_t* restrict a0 = a[0];
58       if XNN_UNPREDICTABLE(a0 != zero) {
59         a0 = (const int8_t*) ((uintptr_t) a0 + a_offset);
60       }
61       const int8_t* restrict a1 = a[1];
62       if XNN_UNPREDICTABLE(a1 != zero) {
63         a1 = (const int8_t*) ((uintptr_t) a1 + a_offset);
64       }
65       a += 2;
66 
67       size_t k = kc;
68 
69 
70       while (k >= 8 * sizeof(int8_t)) {
71         const int16x4_t va00 = vld1_dup_s16((const void*)a0);
72         const int16x4_t va01 = vld1_dup_s16((const void*)(a0 + 2));
73         const int16x4_t va02 = vld1_dup_s16((const void*)(a0 + 4));
74         const int16x4_t va03 = vld1_dup_s16((const void*)(a0 + 6)); a0 += 8;
75         const int16x4_t va10 = vld1_dup_s16((const void*)a1);
76         const int16x4_t va11 = vld1_dup_s16((const void*)(a1 + 2));
77         const int16x4_t va12 = vld1_dup_s16((const void*)(a1 + 4));
78         const int16x4_t va13 = vld1_dup_s16((const void*)(a1 + 6)); a1 += 8;
79 
80         const int8x8_t vb0123c0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
81         const int8x8_t vb4567c0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
82         const int8x8_t vb0123c1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
83         const int8x8_t vb4567c1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
84         const int8x8_t vb0123c2 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
85         const int8x8_t vb4567c2 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
86         const int8x8_t vb0123c3 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
87         const int8x8_t vb4567c3 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
88 
89         const int8x8_t va0c0 = vreinterpret_s8_s16(va00);
90         const int8x8_t va1c0 = vreinterpret_s8_s16(va10);
91 
92         const int16x8_t vprod0x0123c0 = vmull_s8(vb0123c0, va0c0);
93         const int16x8_t vprod1x0123c0 = vmull_s8(vb0123c0, va1c0);
94         vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c0);
95         vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c0);
96         const int16x8_t vprod0x4567c0 = vmull_s8(vb4567c0, va0c0);
97         const int16x8_t vprod1x4567c0 = vmull_s8(vb4567c0, va1c0);
98         vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c0);
99         vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c0);
100         const int8x8_t va0c1 = vreinterpret_s8_s16(va01);
101         const int8x8_t va1c1 = vreinterpret_s8_s16(va11);
102 
103         const int16x8_t vprod0x0123c1 = vmull_s8(vb0123c1, va0c1);
104         const int16x8_t vprod1x0123c1 = vmull_s8(vb0123c1, va1c1);
105         vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c1);
106         vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c1);
107         const int16x8_t vprod0x4567c1 = vmull_s8(vb4567c1, va0c1);
108         const int16x8_t vprod1x4567c1 = vmull_s8(vb4567c1, va1c1);
109         vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c1);
110         vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c1);
111         const int8x8_t va0c2 = vreinterpret_s8_s16(va02);
112         const int8x8_t va1c2 = vreinterpret_s8_s16(va12);
113 
114         const int16x8_t vprod0x0123c2 = vmull_s8(vb0123c2, va0c2);
115         const int16x8_t vprod1x0123c2 = vmull_s8(vb0123c2, va1c2);
116         vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c2);
117         vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c2);
118         const int16x8_t vprod0x4567c2 = vmull_s8(vb4567c2, va0c2);
119         const int16x8_t vprod1x4567c2 = vmull_s8(vb4567c2, va1c2);
120         vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c2);
121         vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c2);
122         const int8x8_t va0c3 = vreinterpret_s8_s16(va03);
123         const int8x8_t va1c3 = vreinterpret_s8_s16(va13);
124 
125         const int16x8_t vprod0x0123c3 = vmull_s8(vb0123c3, va0c3);
126         const int16x8_t vprod1x0123c3 = vmull_s8(vb0123c3, va1c3);
127         vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c3);
128         vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c3);
129         const int16x8_t vprod0x4567c3 = vmull_s8(vb4567c3, va0c3);
130         const int16x8_t vprod1x4567c3 = vmull_s8(vb4567c3, va1c3);
131         vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c3);
132         vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c3);
133 
134         k -= 8 * sizeof(int8_t);
135       }
136 
137       if XNN_UNLIKELY(k != 0) {
138         const int8x8_t va0 = vld1_s8(a0); a0 = (const int8_t*) ((uintptr_t) a0 + k);
139         const int8x8_t va1 = vld1_s8(a1); a1 = (const int8_t*) ((uintptr_t) a1 + k);
140 
141         const int8x8_t vb0123c0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
142         const int8x8_t vb4567c0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
143 
144         const int8x8_t va0c0 = vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(va0), 0));
145         const int16x8_t vprod0x0123c0 = vmull_s8(vb0123c0, va0c0);
146         vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c0);
147         const int16x8_t vprod0x4567c0 = vmull_s8(vb4567c0, va0c0);
148         vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c0);
149         const int8x8_t va1c0 = vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(va1), 0));
150         const int16x8_t vprod1x0123c0 = vmull_s8(vb0123c0, va1c0);
151         vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c0);
152         const int16x8_t vprod1x4567c0 = vmull_s8(vb4567c0, va1c0);
153         vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c0);
154 
155         if (k > 2 * sizeof(int8_t)) {
156           const int8x8_t vb0123c1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
157           const int8x8_t vb4567c1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
158 
159           const int8x8_t va0c1 = vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(va0), 1));
160           const int16x8_t vprod0x0123c1 = vmull_s8(vb0123c1, va0c1);
161           vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c1);
162           const int16x8_t vprod0x4567c1 = vmull_s8(vb4567c1, va0c1);
163           vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c1);
164           const int8x8_t va1c1 = vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(va1), 1));
165           const int16x8_t vprod1x0123c1 = vmull_s8(vb0123c1, va1c1);
166           vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c1);
167           const int16x8_t vprod1x4567c1 = vmull_s8(vb4567c1, va1c1);
168           vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c1);
169 
170           if (k > 4 * sizeof(int8_t)) {
171             const int8x8_t vb0123c2 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
172             const int8x8_t vb4567c2 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
173 
174             const int8x8_t va0c2 = vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(va0), 2));
175             const int16x8_t vprod0x0123c2 = vmull_s8(vb0123c2, va0c2);
176             vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c2);
177             const int16x8_t vprod0x4567c2 = vmull_s8(vb4567c2, va0c2);
178             vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c2);
179             const int8x8_t va1c2 = vreinterpret_s8_s16(vdup_lane_s16(vreinterpret_s16_s8(va1), 2));
180             const int16x8_t vprod1x0123c2 = vmull_s8(vb0123c2, va1c2);
181             vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c2);
182             const int16x8_t vprod1x4567c2 = vmull_s8(vb4567c2, va1c2);
183             vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c2);
184           }
185         }
186       }
187       p -= 2 * sizeof(void*);
188     } while (p != 0);
189 
190     const int32x4_t vright_pre_shift = vld1q_dup_s32(&params->rndnu_neon.right_pre_shift);
191     const int32x4_t vmultiplier = vld1q_dup_s32(&params->rndnu_neon.multiplier);
192     const int32x4_t vright_post_shift = vld1q_dup_s32(&params->rndnu_neon.right_post_shift);
193 
194     vacc0x0123 = vqshlq_s32(vacc0x0123, vright_pre_shift);
195     vacc0x4567 = vqshlq_s32(vacc0x4567, vright_pre_shift);
196     vacc1x0123 = vqshlq_s32(vacc1x0123, vright_pre_shift);
197     vacc1x4567 = vqshlq_s32(vacc1x4567, vright_pre_shift);
198 
199     vacc0x0123 = vqdmulhq_s32(vacc0x0123, vmultiplier);
200     vacc0x4567 = vqdmulhq_s32(vacc0x4567, vmultiplier);
201     vacc1x0123 = vqdmulhq_s32(vacc1x0123, vmultiplier);
202     vacc1x4567 = vqdmulhq_s32(vacc1x4567, vmultiplier);
203 
204     vacc0x0123 = vrshlq_s32(vacc0x0123, vright_post_shift);
205     vacc0x4567 = vrshlq_s32(vacc0x4567, vright_post_shift);
206     vacc1x0123 = vrshlq_s32(vacc1x0123, vright_post_shift);
207     vacc1x4567 = vrshlq_s32(vacc1x4567, vright_post_shift);
208 
209     const int16x8_t voutput_zero_point = vld1q_dup_s16(&params->rndnu_neon.output_zero_point);
210 #if XNN_ARCH_ARM64
211     int16x8_t vacc0x01234567 = vqmovn_high_s32(vqmovn_s32(vacc0x0123), vacc0x4567);
212     int16x8_t vacc1x01234567 = vqmovn_high_s32(vqmovn_s32(vacc1x0123), vacc1x4567);
213 
214     vacc0x01234567 = vqaddq_s16(vacc0x01234567, voutput_zero_point);
215     vacc1x01234567 = vqaddq_s16(vacc1x01234567, voutput_zero_point);
216 
217     int8x16_t vout0x01234567_1x01234567 = vqmovn_high_s16(vqmovn_s16(vacc0x01234567), vacc1x01234567);
218 #else
219     int16x8_t vacc0x01234567 = vcombine_s16(vqmovn_s32(vacc0x0123), vqmovn_s32(vacc0x4567));
220     int16x8_t vacc1x01234567 = vcombine_s16(vqmovn_s32(vacc1x0123), vqmovn_s32(vacc1x4567));
221 
222     vacc0x01234567 = vqaddq_s16(vacc0x01234567, voutput_zero_point);
223     vacc1x01234567 = vqaddq_s16(vacc1x01234567, voutput_zero_point);
224 
225     int8x16_t vout0x01234567_1x01234567 = vcombine_s8(vqmovn_s16(vacc0x01234567), vqmovn_s16(vacc1x01234567));
226 #endif
227 
228     const int8x16_t voutput_min = vld1q_dup_s8(&params->rndnu_neon.output_min);
229     vout0x01234567_1x01234567 = vmaxq_s8(vout0x01234567_1x01234567, voutput_min);
230 
231     const int8x16_t voutput_max = vld1q_dup_s8(&params->rndnu_neon.output_max);
232     vout0x01234567_1x01234567 = vminq_s8(vout0x01234567_1x01234567, voutput_max);
233 
234     if (nc >= 8) {
235       vst1_s8(c1 + 0, vget_high_s8(vout0x01234567_1x01234567));
236       vst1_s8(c0 + 0, vget_low_s8(vout0x01234567_1x01234567));
237 
238       c1 = (int8_t*) ((uintptr_t) c1 + cn_stride);
239       c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
240 
241       a = (const int8_t**restrict) ((uintptr_t) a - ks);
242 
243       nc -= 8;
244     } else {
245       if (nc & 4) {
246         vst1q_lane_u32((void*) c1, vreinterpretq_u32_s8(vout0x01234567_1x01234567), 2); c1 += 4;
247         vst1q_lane_u32((void*) c0, vreinterpretq_u32_s8(vout0x01234567_1x01234567), 0); c0 += 4;
248         vout0x01234567_1x01234567 = vextq_s8(vout0x01234567_1x01234567, vout0x01234567_1x01234567, 4);
249       }
250       if (nc & 2) {
251         vst1q_lane_u16((void*) c1, vreinterpretq_u16_s8(vout0x01234567_1x01234567), 4); c1 += 2;
252         vst1q_lane_u16((void*) c0, vreinterpretq_u16_s8(vout0x01234567_1x01234567), 0); c0 += 2;
253         vout0x01234567_1x01234567 = vextq_s8(vout0x01234567_1x01234567, vout0x01234567_1x01234567, 2);
254       }
255       if (nc & 1) {
256         vst1q_lane_s8(c1, vout0x01234567_1x01234567, 8);
257         vst1q_lane_s8(c0, vout0x01234567_1x01234567, 0);
258       }
259 
260       nc = 0;
261     }
262   } while (nc != 0);
263 }
264