• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/math.h>
16 
17 
xnn_qs8_gemm_minmax_fp32_ukernel_2x8c4s2__neon_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_qs8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])18 void xnn_qs8_gemm_minmax_fp32_ukernel_2x8c4s2__neon_mlal(
19     size_t mr,
20     size_t nc,
21     size_t kc,
22     const int8_t* restrict a,
23     size_t a_stride,
24     const void* restrict w,
25     int8_t* restrict c,
26     size_t cm_stride,
27     size_t cn_stride,
28     const union xnn_qs8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
29 {
30   assert(mr != 0);
31   assert(mr <= 2);
32   assert(nc != 0);
33   assert(kc != 0);
34   assert(kc % sizeof(int8_t) == 0);
35   assert(a != NULL);
36   assert(w != NULL);
37   assert(c != NULL);
38 
39   const int8_t* a0 = a;
40   int8_t* c0 = c;
41   const int8_t* a1 = (const int8_t*) ((uintptr_t) a0 + a_stride);
42   int8_t* c1 = (int8_t*) ((uintptr_t) c0 + cm_stride);
43   if XNN_UNPREDICTABLE(mr != 2) {
44     a1 = a0;
45     c1 = c0;
46   }
47 
48   kc = round_up_po2(kc, 8 * sizeof(int8_t));
49   do {
50     int32x4_t vacc0x01 = vreinterpretq_s32_u64(vmovl_u32(vld1_u32(w))); w = (const int32_t*) w + 2;
51     int32x4_t vacc0x23 = vreinterpretq_s32_u64(vmovl_u32(vld1_u32(w))); w = (const int32_t*) w + 2;
52     int32x4_t vacc0x45 = vreinterpretq_s32_u64(vmovl_u32(vld1_u32(w))); w = (const int32_t*) w + 2;
53     int32x4_t vacc0x67 = vreinterpretq_s32_u64(vmovl_u32(vld1_u32(w))); w = (const int32_t*) w + 2;
54     int32x4_t vacc1x01 = vacc0x01;
55     int32x4_t vacc1x23 = vacc0x23;
56     int32x4_t vacc1x45 = vacc0x45;
57     int32x4_t vacc1x67 = vacc0x67;
58 
59     size_t k = kc;
60     while (k >= 16 * sizeof(int8_t)) {
61       int8x8_t va0x0 = vld1_s8(a0); a0 += 8;
62       int8x8_t va0x1 = vld1_s8(a0); a0 += 8;
63       int8x8_t va1x0 = vld1_s8(a1); a1 += 8;
64       int8x8_t va1x1 = vld1_s8(a1); a1 += 8;
65 
66       const int8x8_t vb01c0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
67       const int8x8_t vb23c0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
68       const int8x8_t vb45c0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
69       const int8x8_t vb67c0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
70       const int8x8_t vb01c1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
71       const int8x8_t vb23c1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
72       const int8x8_t vb45c1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
73       const int8x8_t vb67c1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
74 
75       int16x8_t vprod0x01c0 = vmull_s8(vb01c0x0, va0x0);
76       int16x8_t vprod1x01c0 = vmull_s8(vb01c0x0, va1x0);
77       const int8x8_t vb01c0x1 = vld1_s8(w); w = (const int8_t*) w + 8;
78       vprod0x01c0 = vmlal_s8(vprod0x01c0, vb01c0x1, va0x1);
79       vprod1x01c0 = vmlal_s8(vprod1x01c0, vb01c0x1, va1x1);
80       vacc0x01 = vpadalq_s16(vacc0x01, vprod0x01c0);
81       vacc1x01 = vpadalq_s16(vacc1x01, vprod1x01c0);
82       int16x8_t vprod0x23c0 = vmull_s8(vb23c0x0, va0x0);
83       int16x8_t vprod1x23c0 = vmull_s8(vb23c0x0, va1x0);
84       const int8x8_t vb23c0x1 = vld1_s8(w); w = (const int8_t*) w + 8;
85       vprod0x23c0 = vmlal_s8(vprod0x23c0, vb23c0x1, va0x1);
86       vprod1x23c0 = vmlal_s8(vprod1x23c0, vb23c0x1, va1x1);
87       vacc0x23 = vpadalq_s16(vacc0x23, vprod0x23c0);
88       vacc1x23 = vpadalq_s16(vacc1x23, vprod1x23c0);
89       int16x8_t vprod0x45c0 = vmull_s8(vb45c0x0, va0x0);
90       int16x8_t vprod1x45c0 = vmull_s8(vb45c0x0, va1x0);
91       const int8x8_t vb45c0x1 = vld1_s8(w); w = (const int8_t*) w + 8;
92       vprod0x45c0 = vmlal_s8(vprod0x45c0, vb45c0x1, va0x1);
93       vprod1x45c0 = vmlal_s8(vprod1x45c0, vb45c0x1, va1x1);
94       vacc0x45 = vpadalq_s16(vacc0x45, vprod0x45c0);
95       vacc1x45 = vpadalq_s16(vacc1x45, vprod1x45c0);
96       int16x8_t vprod0x67c0 = vmull_s8(vb67c0x0, va0x0);
97       int16x8_t vprod1x67c0 = vmull_s8(vb67c0x0, va1x0);
98       const int8x8_t vb67c0x1 = vld1_s8(w); w = (const int8_t*) w + 8;
99       vprod0x67c0 = vmlal_s8(vprod0x67c0, vb67c0x1, va0x1);
100       vprod1x67c0 = vmlal_s8(vprod1x67c0, vb67c0x1, va1x1);
101       vacc0x67 = vpadalq_s16(vacc0x67, vprod0x67c0);
102       vacc1x67 = vpadalq_s16(vacc1x67, vprod1x67c0);
103       va0x0 = vext_s8(va0x0, va0x0, 4);
104       va0x1 = vext_s8(va0x1, va0x1, 4);
105       va1x0 = vext_s8(va1x0, va1x0, 4);
106       va1x1 = vext_s8(va1x1, va1x1, 4);
107       int16x8_t vprod0x01c1 = vmull_s8(vb01c1x0, va0x0);
108       int16x8_t vprod1x01c1 = vmull_s8(vb01c1x0, va1x0);
109       const int8x8_t vb01c1x1 = vld1_s8(w); w = (const int8_t*) w + 8;
110       vprod0x01c1 = vmlal_s8(vprod0x01c1, vb01c1x1, va0x1);
111       vprod1x01c1 = vmlal_s8(vprod1x01c1, vb01c1x1, va1x1);
112       vacc0x01 = vpadalq_s16(vacc0x01, vprod0x01c1);
113       vacc1x01 = vpadalq_s16(vacc1x01, vprod1x01c1);
114       int16x8_t vprod0x23c1 = vmull_s8(vb23c1x0, va0x0);
115       int16x8_t vprod1x23c1 = vmull_s8(vb23c1x0, va1x0);
116       const int8x8_t vb23c1x1 = vld1_s8(w); w = (const int8_t*) w + 8;
117       vprod0x23c1 = vmlal_s8(vprod0x23c1, vb23c1x1, va0x1);
118       vprod1x23c1 = vmlal_s8(vprod1x23c1, vb23c1x1, va1x1);
119       vacc0x23 = vpadalq_s16(vacc0x23, vprod0x23c1);
120       vacc1x23 = vpadalq_s16(vacc1x23, vprod1x23c1);
121       int16x8_t vprod0x45c1 = vmull_s8(vb45c1x0, va0x0);
122       int16x8_t vprod1x45c1 = vmull_s8(vb45c1x0, va1x0);
123       const int8x8_t vb45c1x1 = vld1_s8(w); w = (const int8_t*) w + 8;
124       vprod0x45c1 = vmlal_s8(vprod0x45c1, vb45c1x1, va0x1);
125       vprod1x45c1 = vmlal_s8(vprod1x45c1, vb45c1x1, va1x1);
126       vacc0x45 = vpadalq_s16(vacc0x45, vprod0x45c1);
127       vacc1x45 = vpadalq_s16(vacc1x45, vprod1x45c1);
128       int16x8_t vprod0x67c1 = vmull_s8(vb67c1x0, va0x0);
129       int16x8_t vprod1x67c1 = vmull_s8(vb67c1x0, va1x0);
130       const int8x8_t vb67c1x1 = vld1_s8(w); w = (const int8_t*) w + 8;
131       vprod0x67c1 = vmlal_s8(vprod0x67c1, vb67c1x1, va0x1);
132       vprod1x67c1 = vmlal_s8(vprod1x67c1, vb67c1x1, va1x1);
133       vacc0x67 = vpadalq_s16(vacc0x67, vprod0x67c1);
134       vacc1x67 = vpadalq_s16(vacc1x67, vprod1x67c1);
135 
136       k -= 16 * sizeof(int8_t);
137     }
138     if (k != 0) {
139       int8x8_t va0x0 = vld1_s8(a0); a0 += 8;
140       int8x8_t va1x0 = vld1_s8(a1); a1 += 8;
141 
142       const int8x8_t vb01c0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
143       const int8x8_t vb23c0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
144       const int8x8_t vb45c0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
145       const int8x8_t vb67c0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
146       const int8x8_t vb01c1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
147       const int8x8_t vb23c1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
148       const int8x8_t vb45c1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
149       const int8x8_t vb67c1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
150 
151       int16x8_t vprod0x01c0 = vmull_s8(vb01c0x0, va0x0);
152       int16x8_t vprod1x01c0 = vmull_s8(vb01c0x0, va1x0);
153       vacc0x01 = vpadalq_s16(vacc0x01, vprod0x01c0);
154       vacc1x01 = vpadalq_s16(vacc1x01, vprod1x01c0);
155       int16x8_t vprod0x23c0 = vmull_s8(vb23c0x0, va0x0);
156       int16x8_t vprod1x23c0 = vmull_s8(vb23c0x0, va1x0);
157       vacc0x23 = vpadalq_s16(vacc0x23, vprod0x23c0);
158       vacc1x23 = vpadalq_s16(vacc1x23, vprod1x23c0);
159       int16x8_t vprod0x45c0 = vmull_s8(vb45c0x0, va0x0);
160       int16x8_t vprod1x45c0 = vmull_s8(vb45c0x0, va1x0);
161       vacc0x45 = vpadalq_s16(vacc0x45, vprod0x45c0);
162       vacc1x45 = vpadalq_s16(vacc1x45, vprod1x45c0);
163       int16x8_t vprod0x67c0 = vmull_s8(vb67c0x0, va0x0);
164       int16x8_t vprod1x67c0 = vmull_s8(vb67c0x0, va1x0);
165       vacc0x67 = vpadalq_s16(vacc0x67, vprod0x67c0);
166       vacc1x67 = vpadalq_s16(vacc1x67, vprod1x67c0);
167       va0x0 = vext_s8(va0x0, va0x0, 4);
168       va1x0 = vext_s8(va1x0, va1x0, 4);
169       int16x8_t vprod0x01c1 = vmull_s8(vb01c1x0, va0x0);
170       int16x8_t vprod1x01c1 = vmull_s8(vb01c1x0, va1x0);
171       vacc0x01 = vpadalq_s16(vacc0x01, vprod0x01c1);
172       vacc1x01 = vpadalq_s16(vacc1x01, vprod1x01c1);
173       int16x8_t vprod0x23c1 = vmull_s8(vb23c1x0, va0x0);
174       int16x8_t vprod1x23c1 = vmull_s8(vb23c1x0, va1x0);
175       vacc0x23 = vpadalq_s16(vacc0x23, vprod0x23c1);
176       vacc1x23 = vpadalq_s16(vacc1x23, vprod1x23c1);
177       int16x8_t vprod0x45c1 = vmull_s8(vb45c1x0, va0x0);
178       int16x8_t vprod1x45c1 = vmull_s8(vb45c1x0, va1x0);
179       vacc0x45 = vpadalq_s16(vacc0x45, vprod0x45c1);
180       vacc1x45 = vpadalq_s16(vacc1x45, vprod1x45c1);
181       int16x8_t vprod0x67c1 = vmull_s8(vb67c1x0, va0x0);
182       int16x8_t vprod1x67c1 = vmull_s8(vb67c1x0, va1x0);
183       vacc0x67 = vpadalq_s16(vacc0x67, vprod0x67c1);
184       vacc1x67 = vpadalq_s16(vacc1x67, vprod1x67c1);
185 
186     }
187 
188 #if XNN_ARCH_ARM64
189     int32x4_t vacc0x0123 = vpaddq_s32(vacc0x01, vacc0x23);
190     int32x4_t vacc0x4567 = vpaddq_s32(vacc0x45, vacc0x67);
191     int32x4_t vacc1x0123 = vpaddq_s32(vacc1x01, vacc1x23);
192     int32x4_t vacc1x4567 = vpaddq_s32(vacc1x45, vacc1x67);
193 #else
194     const int32x2_t vsum0x01 = vpadd_s32(vget_low_s32(vacc0x01), vget_high_s32(vacc0x01));
195     const int32x2_t vsum0x23 = vpadd_s32(vget_low_s32(vacc0x23), vget_high_s32(vacc0x23));
196     int32x4_t vacc0x0123 = vcombine_s32(vsum0x01, vsum0x23);
197     const int32x2_t vsum0x45 = vpadd_s32(vget_low_s32(vacc0x45), vget_high_s32(vacc0x45));
198     const int32x2_t vsum0x67 = vpadd_s32(vget_low_s32(vacc0x67), vget_high_s32(vacc0x67));
199     int32x4_t vacc0x4567 = vcombine_s32(vsum0x45, vsum0x67);
200     const int32x2_t vsum1x01 = vpadd_s32(vget_low_s32(vacc1x01), vget_high_s32(vacc1x01));
201     const int32x2_t vsum1x23 = vpadd_s32(vget_low_s32(vacc1x23), vget_high_s32(vacc1x23));
202     int32x4_t vacc1x0123 = vcombine_s32(vsum1x01, vsum1x23);
203     const int32x2_t vsum1x45 = vpadd_s32(vget_low_s32(vacc1x45), vget_high_s32(vacc1x45));
204     const int32x2_t vsum1x67 = vpadd_s32(vget_low_s32(vacc1x67), vget_high_s32(vacc1x67));
205     int32x4_t vacc1x4567 = vcombine_s32(vsum1x45, vsum1x67);
206 #endif
207 
208     float32x4_t vfpacc0x0123 = vcvtq_f32_s32(vacc0x0123);
209     float32x4_t vfpacc0x4567 = vcvtq_f32_s32(vacc0x4567);
210     float32x4_t vfpacc1x0123 = vcvtq_f32_s32(vacc1x0123);
211     float32x4_t vfpacc1x4567 = vcvtq_f32_s32(vacc1x4567);
212 
213     const float32x4_t vscale = vld1q_dup_f32(&params->fp32_neon.scale);
214     vfpacc0x0123 = vmulq_f32(vfpacc0x0123, vscale);
215     vfpacc0x4567 = vmulq_f32(vfpacc0x4567, vscale);
216     vfpacc1x0123 = vmulq_f32(vfpacc1x0123, vscale);
217     vfpacc1x4567 = vmulq_f32(vfpacc1x4567, vscale);
218 
219     const float32x4_t vmagic_bias = vld1q_dup_f32(&params->fp32_neon.magic_bias);
220     vacc0x0123 = vreinterpretq_s32_f32(vaddq_f32(vfpacc0x0123, vmagic_bias));
221     vacc0x4567 = vreinterpretq_s32_f32(vaddq_f32(vfpacc0x4567, vmagic_bias));
222     vacc1x0123 = vreinterpretq_s32_f32(vaddq_f32(vfpacc1x0123, vmagic_bias));
223     vacc1x4567 = vreinterpretq_s32_f32(vaddq_f32(vfpacc1x4567, vmagic_bias));
224 
225     const int32x4_t vmagic_bias_less_output_zero_point = vld1q_dup_s32(&params->fp32_neon.magic_bias_less_output_zero_point);
226     vacc0x0123 = vqsubq_s32(vacc0x0123, vmagic_bias_less_output_zero_point);
227     vacc0x4567 = vqsubq_s32(vacc0x4567, vmagic_bias_less_output_zero_point);
228     vacc1x0123 = vqsubq_s32(vacc1x0123, vmagic_bias_less_output_zero_point);
229     vacc1x4567 = vqsubq_s32(vacc1x4567, vmagic_bias_less_output_zero_point);
230 
231 #if XNN_ARCH_ARM64
232     int16x8_t vacc0x01234567 = vqmovn_high_s32(vqmovn_s32(vacc0x0123), vacc0x4567);
233     int16x8_t vacc1x01234567 = vqmovn_high_s32(vqmovn_s32(vacc1x0123), vacc1x4567);
234 
235 
236     int8x16_t vout0x01234567_1x01234567 = vqmovn_high_s16(vqmovn_s16(vacc0x01234567), vacc1x01234567);
237 #else
238     int16x8_t vacc0x01234567 = vcombine_s16(vqmovn_s32(vacc0x0123), vqmovn_s32(vacc0x4567));
239     int16x8_t vacc1x01234567 = vcombine_s16(vqmovn_s32(vacc1x0123), vqmovn_s32(vacc1x4567));
240 
241 
242     int8x16_t vout0x01234567_1x01234567 = vcombine_s8(vqmovn_s16(vacc0x01234567), vqmovn_s16(vacc1x01234567));
243 #endif
244 
245     const int8x16_t voutput_min = vld1q_dup_s8(&params->fp32_neon.output_min);
246     vout0x01234567_1x01234567 = vmaxq_s8(vout0x01234567_1x01234567, voutput_min);
247 
248     const int8x16_t voutput_max = vld1q_dup_s8(&params->fp32_neon.output_max);
249     vout0x01234567_1x01234567 = vminq_s8(vout0x01234567_1x01234567, voutput_max);
250 
251     if (nc >= 8) {
252       vst1_s8(c0 + 0, vget_low_s8(vout0x01234567_1x01234567));
253       vst1_s8(c1 + 0, vget_high_s8(vout0x01234567_1x01234567));
254 
255       c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
256       c1 = (int8_t*) ((uintptr_t) c1 + cn_stride);
257 
258       a0 = (const int8_t*) ((uintptr_t) a0 - kc);
259       a1 = (const int8_t*) ((uintptr_t) a1 - kc);
260 
261       nc -= 8;
262     } else {
263       // Final case where not all of the 8 columns fit in the destination.
264       if (nc & 4) {
265         vst1q_lane_u32((void*) c0, vreinterpretq_u32_s8(vout0x01234567_1x01234567), 0); c0 += 4;
266         vst1q_lane_u32((void*) c1, vreinterpretq_u32_s8(vout0x01234567_1x01234567), 2); c1 += 4;
267         vout0x01234567_1x01234567 = vextq_s8(vout0x01234567_1x01234567, vout0x01234567_1x01234567, 4);
268       }
269       if (nc & 2) {
270         vst1q_lane_u16((void*) c0, vreinterpretq_u16_s8(vout0x01234567_1x01234567), 0); c0 += 2;
271         vst1q_lane_u16((void*) c1, vreinterpretq_u16_s8(vout0x01234567_1x01234567), 4); c1 += 2;
272         vout0x01234567_1x01234567 = vextq_s8(vout0x01234567_1x01234567, vout0x01234567_1x01234567, 2);
273       }
274       if (nc & 1) {
275         vst1q_lane_s8(c0, vout0x01234567_1x01234567, 0);
276         vst1q_lane_s8(c1, vout0x01234567_1x01234567, 8);
277       }
278 
279       nc = 0;
280     }
281   } while (nc != 0);
282 }
283