• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/qs8-gemm/c2-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/intrinsics-polyfill.h>
16 #include <xnnpack/math.h>
17 
18 
xnn_qc8_gemm_minmax_fp32_ukernel_2x8c2s4__neonv8_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_qc8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])19 void xnn_qc8_gemm_minmax_fp32_ukernel_2x8c2s4__neonv8_mlal(
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_qc8_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(kc % sizeof(int8_t) == 0);
36   assert(a != NULL);
37   assert(w != NULL);
38   assert(c != NULL);
39 
40   const int8_t* a0 = a;
41   int8_t* c0 = c;
42   const int8_t* a1 = (const int8_t*) ((uintptr_t) a0 + a_stride);
43   int8_t* c1 = (int8_t*) ((uintptr_t) c0 + cm_stride);
44   if XNN_UNPREDICTABLE(mr != 2) {
45     a1 = a0;
46     c1 = c0;
47   }
48 
49   kc = round_up_po2(kc, 8 * sizeof(int8_t));
50   do {
51     int32x4_t vacc0x0123 = vld1q_s32(w); w = (const int32_t*) w + 4;
52     int32x4_t vacc0x4567 = vld1q_s32(w); w = (const int32_t*) w + 4;
53     int32x4_t vacc1x0123 = vacc0x0123;
54     int32x4_t vacc1x4567 = vacc0x4567;
55 
56     size_t k = kc;
57     while (k >= 16 * sizeof(int8_t)) {
58       int8x8_t va0x0 = vld1_s8(a0); a0 += 8;
59       int8x8_t va0x1 = vld1_s8(a0); a0 += 8;
60       int8x8_t va1x0 = vld1_s8(a1); a1 += 8;
61       int8x8_t va1x1 = vld1_s8(a1); a1 += 8;
62 
63       const int8x8_t vb0123c0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
64       const int8x8_t vb4567c0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
65       const int8x8_t vb0123c1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
66       const int8x8_t vb4567c1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
67       const int8x8_t vb0123c2x0 = vld1_s8(w); w = (const int8_t*) w + 8;
68       const int8x8_t vb4567c2x0 = vld1_s8(w); w = (const int8_t*) w + 8;
69       const int8x8_t vb0123c3x0 = vld1_s8(w); w = (const int8_t*) w + 8;
70       const int8x8_t vb4567c3x0 = vld1_s8(w); w = (const int8_t*) w + 8;
71 
72       int16x8_t vprod0x0123c0 = vmull_s8(vb0123c0x0, va0x0);
73       int16x8_t vprod1x0123c0 = vmull_s8(vb0123c0x0, va1x0);
74       const int8x8_t vb0123c0x1 = vld1_s8(w); w = (const int8_t*) w + 8;
75       vprod0x0123c0 = vmlal_s8(vprod0x0123c0, vb0123c0x1, va0x1);
76       vprod1x0123c0 = vmlal_s8(vprod1x0123c0, vb0123c0x1, va1x1);
77       vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c0);
78       vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c0);
79       int16x8_t vprod0x4567c0 = vmull_s8(vb4567c0x0, va0x0);
80       int16x8_t vprod1x4567c0 = vmull_s8(vb4567c0x0, va1x0);
81       const int8x8_t vb4567c0x1 = vld1_s8(w); w = (const int8_t*) w + 8;
82       vprod0x4567c0 = vmlal_s8(vprod0x4567c0, vb4567c0x1, va0x1);
83       vprod1x4567c0 = vmlal_s8(vprod1x4567c0, vb4567c0x1, va1x1);
84       vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c0);
85       vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c0);
86       va0x0 = vext_s8(va0x0, va0x0, 2);
87       va0x1 = vext_s8(va0x1, va0x1, 2);
88       va1x0 = vext_s8(va1x0, va1x0, 2);
89       va1x1 = vext_s8(va1x1, va1x1, 2);
90       int16x8_t vprod0x0123c1 = vmull_s8(vb0123c1x0, va0x0);
91       int16x8_t vprod1x0123c1 = vmull_s8(vb0123c1x0, va1x0);
92       const int8x8_t vb0123c1x1 = vld1_s8(w); w = (const int8_t*) w + 8;
93       vprod0x0123c1 = vmlal_s8(vprod0x0123c1, vb0123c1x1, va0x1);
94       vprod1x0123c1 = vmlal_s8(vprod1x0123c1, vb0123c1x1, va1x1);
95       vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c1);
96       vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c1);
97       int16x8_t vprod0x4567c1 = vmull_s8(vb4567c1x0, va0x0);
98       int16x8_t vprod1x4567c1 = vmull_s8(vb4567c1x0, va1x0);
99       const int8x8_t vb4567c1x1 = vld1_s8(w); w = (const int8_t*) w + 8;
100       vprod0x4567c1 = vmlal_s8(vprod0x4567c1, vb4567c1x1, va0x1);
101       vprod1x4567c1 = vmlal_s8(vprod1x4567c1, vb4567c1x1, va1x1);
102       vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c1);
103       vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c1);
104       va0x0 = vext_s8(va0x0, va0x0, 2);
105       va0x1 = vext_s8(va0x1, va0x1, 2);
106       va1x0 = vext_s8(va1x0, va1x0, 2);
107       va1x1 = vext_s8(va1x1, va1x1, 2);
108       int16x8_t vprod0x0123c2 = vmull_s8(vb0123c2x0, va0x0);
109       int16x8_t vprod1x0123c2 = vmull_s8(vb0123c2x0, va1x0);
110       const int8x8_t vb0123c2x1 = vld1_s8(w); w = (const int8_t*) w + 8;
111       vprod0x0123c2 = vmlal_s8(vprod0x0123c2, vb0123c2x1, va0x1);
112       vprod1x0123c2 = vmlal_s8(vprod1x0123c2, vb0123c2x1, va1x1);
113       vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c2);
114       vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c2);
115       int16x8_t vprod0x4567c2 = vmull_s8(vb4567c2x0, va0x0);
116       int16x8_t vprod1x4567c2 = vmull_s8(vb4567c2x0, va1x0);
117       const int8x8_t vb4567c2x1 = vld1_s8(w); w = (const int8_t*) w + 8;
118       vprod0x4567c2 = vmlal_s8(vprod0x4567c2, vb4567c2x1, va0x1);
119       vprod1x4567c2 = vmlal_s8(vprod1x4567c2, vb4567c2x1, va1x1);
120       vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c2);
121       vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c2);
122       va0x0 = vext_s8(va0x0, va0x0, 2);
123       va0x1 = vext_s8(va0x1, va0x1, 2);
124       va1x0 = vext_s8(va1x0, va1x0, 2);
125       va1x1 = vext_s8(va1x1, va1x1, 2);
126       int16x8_t vprod0x0123c3 = vmull_s8(vb0123c3x0, va0x0);
127       int16x8_t vprod1x0123c3 = vmull_s8(vb0123c3x0, va1x0);
128       const int8x8_t vb0123c3x1 = vld1_s8(w); w = (const int8_t*) w + 8;
129       vprod0x0123c3 = vmlal_s8(vprod0x0123c3, vb0123c3x1, va0x1);
130       vprod1x0123c3 = vmlal_s8(vprod1x0123c3, vb0123c3x1, va1x1);
131       vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c3);
132       vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c3);
133       int16x8_t vprod0x4567c3 = vmull_s8(vb4567c3x0, va0x0);
134       int16x8_t vprod1x4567c3 = vmull_s8(vb4567c3x0, va1x0);
135       const int8x8_t vb4567c3x1 = vld1_s8(w); w = (const int8_t*) w + 8;
136       vprod0x4567c3 = vmlal_s8(vprod0x4567c3, vb4567c3x1, va0x1);
137       vprod1x4567c3 = vmlal_s8(vprod1x4567c3, vb4567c3x1, va1x1);
138       vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c3);
139       vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c3);
140 
141       k -= 16 * sizeof(int8_t);
142     }
143     if (k != 0) {
144       int8x8_t va0x0 = vld1_s8(a0); a0 += 8;
145       int8x8_t va1x0 = vld1_s8(a1); a1 += 8;
146 
147       const int8x8_t vb0123c0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
148       const int8x8_t vb4567c0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
149       const int8x8_t vb0123c1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
150       const int8x8_t vb4567c1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
151       const int8x8_t vb0123c2x0 = vld1_s8(w); w = (const int8_t*) w + 8;
152       const int8x8_t vb4567c2x0 = vld1_s8(w); w = (const int8_t*) w + 8;
153       const int8x8_t vb0123c3x0 = vld1_s8(w); w = (const int8_t*) w + 8;
154       const int8x8_t vb4567c3x0 = vld1_s8(w); w = (const int8_t*) w + 8;
155 
156       int16x8_t vprod0x0123c0 = vmull_s8(vb0123c0x0, va0x0);
157       int16x8_t vprod1x0123c0 = vmull_s8(vb0123c0x0, va1x0);
158       vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c0);
159       vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c0);
160       int16x8_t vprod0x4567c0 = vmull_s8(vb4567c0x0, va0x0);
161       int16x8_t vprod1x4567c0 = vmull_s8(vb4567c0x0, va1x0);
162       vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c0);
163       vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c0);
164       va0x0 = vext_s8(va0x0, va0x0, 2);
165       va1x0 = vext_s8(va1x0, va1x0, 2);
166       int16x8_t vprod0x0123c1 = vmull_s8(vb0123c1x0, va0x0);
167       int16x8_t vprod1x0123c1 = vmull_s8(vb0123c1x0, va1x0);
168       vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c1);
169       vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c1);
170       int16x8_t vprod0x4567c1 = vmull_s8(vb4567c1x0, va0x0);
171       int16x8_t vprod1x4567c1 = vmull_s8(vb4567c1x0, va1x0);
172       vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c1);
173       vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c1);
174       va0x0 = vext_s8(va0x0, va0x0, 2);
175       va1x0 = vext_s8(va1x0, va1x0, 2);
176       int16x8_t vprod0x0123c2 = vmull_s8(vb0123c2x0, va0x0);
177       int16x8_t vprod1x0123c2 = vmull_s8(vb0123c2x0, va1x0);
178       vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c2);
179       vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c2);
180       int16x8_t vprod0x4567c2 = vmull_s8(vb4567c2x0, va0x0);
181       int16x8_t vprod1x4567c2 = vmull_s8(vb4567c2x0, va1x0);
182       vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c2);
183       vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c2);
184       va0x0 = vext_s8(va0x0, va0x0, 2);
185       va1x0 = vext_s8(va1x0, va1x0, 2);
186       int16x8_t vprod0x0123c3 = vmull_s8(vb0123c3x0, va0x0);
187       int16x8_t vprod1x0123c3 = vmull_s8(vb0123c3x0, va1x0);
188       vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c3);
189       vacc1x0123 = vpadalq_s16(vacc1x0123, vprod1x0123c3);
190       int16x8_t vprod0x4567c3 = vmull_s8(vb4567c3x0, va0x0);
191       int16x8_t vprod1x4567c3 = vmull_s8(vb4567c3x0, va1x0);
192       vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c3);
193       vacc1x4567 = vpadalq_s16(vacc1x4567, vprod1x4567c3);
194 
195     }
196 
197     float32x4_t vfpacc0x0123 = vcvtq_f32_s32(vacc0x0123);
198     float32x4_t vfpacc0x4567 = vcvtq_f32_s32(vacc0x4567);
199     float32x4_t vfpacc1x0123 = vcvtq_f32_s32(vacc1x0123);
200     float32x4_t vfpacc1x4567 = vcvtq_f32_s32(vacc1x4567);
201 
202     const float32x4_t vscale0123 = vld1q_f32(w); w = (const float*) w + 4;
203     vfpacc0x0123 = vmulq_f32(vfpacc0x0123, vscale0123);
204     vfpacc1x0123 = vmulq_f32(vfpacc1x0123, vscale0123);
205     const float32x4_t vscale4567 = vld1q_f32(w); w = (const float*) w + 4;
206     vfpacc0x4567 = vmulq_f32(vfpacc0x4567, vscale4567);
207     vfpacc1x4567 = vmulq_f32(vfpacc1x4567, vscale4567);
208 
209     vacc0x0123 = vcvtnq_s32_f32(vfpacc0x0123);
210     vacc0x4567 = vcvtnq_s32_f32(vfpacc0x4567);
211     vacc1x0123 = vcvtnq_s32_f32(vfpacc1x0123);
212     vacc1x4567 = vcvtnq_s32_f32(vfpacc1x4567);
213 
214     const int16x8_t voutput_zero_point = vld1q_dup_s16(&params->fp32_neonv8.output_zero_point);
215 #if XNN_ARCH_ARM64
216     int16x8_t vacc0x01234567 = vqmovn_high_s32(vqmovn_s32(vacc0x0123), vacc0x4567);
217     int16x8_t vacc1x01234567 = vqmovn_high_s32(vqmovn_s32(vacc1x0123), vacc1x4567);
218 
219     vacc0x01234567 = vqaddq_s16(vacc0x01234567, voutput_zero_point);
220     vacc1x01234567 = vqaddq_s16(vacc1x01234567, voutput_zero_point);
221 
222     int8x16_t vout0x01234567_1x01234567 = vqmovn_high_s16(vqmovn_s16(vacc0x01234567), vacc1x01234567);
223 #else
224     int16x8_t vacc0x01234567 = vcombine_s16(vqmovn_s32(vacc0x0123), vqmovn_s32(vacc0x4567));
225     int16x8_t vacc1x01234567 = vcombine_s16(vqmovn_s32(vacc1x0123), vqmovn_s32(vacc1x4567));
226 
227     vacc0x01234567 = vqaddq_s16(vacc0x01234567, voutput_zero_point);
228     vacc1x01234567 = vqaddq_s16(vacc1x01234567, voutput_zero_point);
229 
230     int8x16_t vout0x01234567_1x01234567 = vcombine_s8(vqmovn_s16(vacc0x01234567), vqmovn_s16(vacc1x01234567));
231 #endif
232 
233     const int8x16_t voutput_min = vld1q_dup_s8(&params->fp32_neonv8.output_min);
234     vout0x01234567_1x01234567 = vmaxq_s8(vout0x01234567_1x01234567, voutput_min);
235 
236     const int8x16_t voutput_max = vld1q_dup_s8(&params->fp32_neonv8.output_max);
237     vout0x01234567_1x01234567 = vminq_s8(vout0x01234567_1x01234567, voutput_max);
238 
239     if (nc >= 8) {
240       vst1_s8(c0 + 0, vget_low_s8(vout0x01234567_1x01234567));
241       vst1_s8(c1 + 0, vget_high_s8(vout0x01234567_1x01234567));
242 
243       c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
244       c1 = (int8_t*) ((uintptr_t) c1 + cn_stride);
245 
246       a0 = (const int8_t*) ((uintptr_t) a0 - kc);
247       a1 = (const int8_t*) ((uintptr_t) a1 - kc);
248 
249       nc -= 8;
250     } else {
251       // Final case where not all of the 8 columns fit in the destination.
252       if (nc & 4) {
253         vst1q_lane_u32((void*) c0, vreinterpretq_u32_s8(vout0x01234567_1x01234567), 0); c0 += 4;
254         vst1q_lane_u32((void*) c1, vreinterpretq_u32_s8(vout0x01234567_1x01234567), 2); c1 += 4;
255         vout0x01234567_1x01234567 = vextq_s8(vout0x01234567_1x01234567, vout0x01234567_1x01234567, 4);
256       }
257       if (nc & 2) {
258         vst1q_lane_u16((void*) c0, vreinterpretq_u16_s8(vout0x01234567_1x01234567), 0); c0 += 2;
259         vst1q_lane_u16((void*) c1, vreinterpretq_u16_s8(vout0x01234567_1x01234567), 4); c1 += 2;
260         vout0x01234567_1x01234567 = vextq_s8(vout0x01234567_1x01234567, vout0x01234567_1x01234567, 2);
261       }
262       if (nc & 1) {
263         vst1q_lane_s8(c0, vout0x01234567_1x01234567, 0);
264         vst1q_lane_s8(c1, vout0x01234567_1x01234567, 8);
265       }
266 
267       nc = 0;
268     }
269   } while (nc != 0);
270 }
271