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