• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/qu8-igemm/c4-neondot.c.in
3 //   Generator: tools/xngen
4 //
5 // Copyright 2020 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/igemm.h>
15 #include <xnnpack/intrinsics-polyfill.h>
16 #include <xnnpack/math.h>
17 
18 
xnn_qu8_igemm_minmax_fp32_ukernel_2x16c4__neondot(size_t mr,size_t nc,size_t kc,size_t ks,const uint8_t ** restrict a,const void * restrict w,uint8_t * restrict c,size_t cm_stride,size_t cn_stride,size_t a_offset,const uint8_t * zero,const union xnn_qu8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])19 void xnn_qu8_igemm_minmax_fp32_ukernel_2x16c4__neondot(
20     size_t mr,
21     size_t nc,
22     size_t kc,
23     size_t ks,
24     const uint8_t** restrict a,
25     const void* restrict w,
26     uint8_t* restrict c,
27     size_t cm_stride,
28     size_t cn_stride,
29     size_t a_offset,
30     const uint8_t* zero,
31     const union xnn_qu8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
32 {
33   assert(mr != 0);
34   assert(mr <= 2);
35   assert(nc != 0);
36   assert(kc != 0);
37   assert(ks != 0);
38   assert(ks % (2 * sizeof(void*)) == 0);
39   assert(a_offset % sizeof(uint8_t) == 0);
40   assert(a != NULL);
41   assert(w != NULL);
42   assert(c != NULL);
43 
44   kc = round_up_po2(kc, 4 * sizeof(uint8_t));
45   uint8_t* c0 = c;
46   uint8_t* c1 = (uint8_t*) ((uintptr_t) c0 + cm_stride);
47   if XNN_UNPREDICTABLE(mr != 2) {
48     c1 = c0;
49   }
50 
51   const uint8x8_t va_zero_point = vld1_dup_u8(&params->fp32_neonv8.kernel_zero_point[0]);
52 
53   do {
54     // Initialize accumulators with bias. 16 bias values are loaded from the
55     // weight matrix, at the start of the group of 16 columns.
56     uint32x4_t vpacc0x0123 = vld1q_u32(w); w = (const void*) ((const uint32_t*) w + 4);
57     uint32x4_t vpacc0x4567 = vld1q_u32(w); w = (const void*) ((const uint32_t*) w + 4);
58     uint32x4_t vpacc0x89AB = vld1q_u32(w); w = (const void*) ((const uint32_t*) w + 4);
59     uint32x4_t vpacc0xCDEF = vld1q_u32(w); w = (const void*) ((const uint32_t*) w + 4);
60     uint32x4_t vpacc1x0123 = vpacc0x0123;
61     uint32x4_t vpacc1x4567 = vpacc0x4567;
62     uint32x4_t vpacc1x89AB = vpacc0x89AB;
63     uint32x4_t vpacc1xCDEF = vpacc0xCDEF;
64     uint32x2_t vnacc0 = vmov_n_u32(0);
65     uint32x2_t vnacc1 = vmov_n_u32(0);
66 
67     size_t p = ks;
68     do {
69       const uint8_t* restrict a0 = a[0];
70       if XNN_UNPREDICTABLE(a0 != zero) {
71         a0 = (const uint8_t*) ((uintptr_t) a0 + a_offset);
72       }
73       const uint8_t* restrict a1 = a[1];
74       if XNN_UNPREDICTABLE(a1 != zero) {
75         a1 = (const uint8_t*) ((uintptr_t) a1 + a_offset);
76       }
77       a += 2;
78 
79       // Inner accumulation loop along the 16 columns.
80       size_t k = kc;
81       // 2x partial unrolled loop to load 8 bytes at a time.
82       while (k >= 8 * sizeof(uint8_t)) {
83         // Load a 2x8 block of activations.
84         const uint8x8_t va0x01234567 = vld1_u8(a0); a0 += 8;
85         const uint8x8_t va1x01234567 = vld1_u8(a1); a1 += 8;
86 
87         // Load a 8x16 block of weights.
88         const uint8x16_t vb0123x0123 = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
89         const uint8x16_t vb0123x4567 = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
90         const uint8x16_t vb0123x89AB = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
91         const uint8x16_t vb0123xCDEF = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
92         const uint8x16_t vb4567x0123 = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
93         const uint8x16_t vb4567x4567 = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
94         const uint8x16_t vb4567x89AB = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
95         const uint8x16_t vb4567xCDEF = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
96 
97         // Multiply-accumulate: 2x8 * 8x16 --> 2x16.
98         vnacc0 = vdot_u32(vnacc0, va_zero_point, va0x01234567);
99         vpacc0x0123 = vdotq_lane_u32(vpacc0x0123, vb0123x0123, va0x01234567, 0);
100         vpacc0x4567 = vdotq_lane_u32(vpacc0x4567, vb0123x4567, va0x01234567, 0);
101         vpacc0x89AB = vdotq_lane_u32(vpacc0x89AB, vb0123x89AB, va0x01234567, 0);
102         vpacc0xCDEF = vdotq_lane_u32(vpacc0xCDEF, vb0123xCDEF, va0x01234567, 0);
103         vpacc0x0123 = vdotq_lane_u32(vpacc0x0123, vb4567x0123, va0x01234567, 1);
104         vpacc0x4567 = vdotq_lane_u32(vpacc0x4567, vb4567x4567, va0x01234567, 1);
105         vpacc0x89AB = vdotq_lane_u32(vpacc0x89AB, vb4567x89AB, va0x01234567, 1);
106         vpacc0xCDEF = vdotq_lane_u32(vpacc0xCDEF, vb4567xCDEF, va0x01234567, 1);
107         vnacc1 = vdot_u32(vnacc1, va_zero_point, va1x01234567);
108         vpacc1x0123 = vdotq_lane_u32(vpacc1x0123, vb0123x0123, va1x01234567, 0);
109         vpacc1x4567 = vdotq_lane_u32(vpacc1x4567, vb0123x4567, va1x01234567, 0);
110         vpacc1x89AB = vdotq_lane_u32(vpacc1x89AB, vb0123x89AB, va1x01234567, 0);
111         vpacc1xCDEF = vdotq_lane_u32(vpacc1xCDEF, vb0123xCDEF, va1x01234567, 0);
112         vpacc1x0123 = vdotq_lane_u32(vpacc1x0123, vb4567x0123, va1x01234567, 1);
113         vpacc1x4567 = vdotq_lane_u32(vpacc1x4567, vb4567x4567, va1x01234567, 1);
114         vpacc1x89AB = vdotq_lane_u32(vpacc1x89AB, vb4567x89AB, va1x01234567, 1);
115         vpacc1xCDEF = vdotq_lane_u32(vpacc1xCDEF, vb4567xCDEF, va1x01234567, 1);
116 
117         k -= 8 * sizeof(uint8_t);
118       }
119       // Handle up to 4 final positions of `k`
120       if XNN_UNLIKELY(k != 0) {
121         // Load a 2x4 block of activations.
122         const uint8x8_t va0x01234567 = vreinterpret_u8_u32(vld1_lane_u32((const void*) a0, vmov_n_u32(0), 0)); a0 += 4;
123         const uint8x8_t va1x01234567 = vreinterpret_u8_u32(vld1_lane_u32((const void*) a1, vmov_n_u32(0), 0)); a1 += 4;
124 
125         // Load a 4x16 block of weights.
126         const uint8x16_t vb0123x0123 = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
127         const uint8x16_t vb0123x4567 = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
128         const uint8x16_t vb0123x89AB = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
129         const uint8x16_t vb0123xCDEF = vld1q_u8(w); w = (const void*) ((const uint8_t*) w + 16);
130 
131         // Multiply-accumulate: 2x4 * 4x16 --> 2x16.
132         vnacc0 = vdot_u32(vnacc0, va_zero_point, va0x01234567);
133         vpacc0x0123 = vdotq_lane_u32(vpacc0x0123, vb0123x0123, va0x01234567, 0);
134         vpacc0x4567 = vdotq_lane_u32(vpacc0x4567, vb0123x4567, va0x01234567, 0);
135         vpacc0x89AB = vdotq_lane_u32(vpacc0x89AB, vb0123x89AB, va0x01234567, 0);
136         vpacc0xCDEF = vdotq_lane_u32(vpacc0xCDEF, vb0123xCDEF, va0x01234567, 0);
137         vnacc1 = vdot_u32(vnacc1, va_zero_point, va1x01234567);
138         vpacc1x0123 = vdotq_lane_u32(vpacc1x0123, vb0123x0123, va1x01234567, 0);
139         vpacc1x4567 = vdotq_lane_u32(vpacc1x4567, vb0123x4567, va1x01234567, 0);
140         vpacc1x89AB = vdotq_lane_u32(vpacc1x89AB, vb0123x89AB, va1x01234567, 0);
141         vpacc1xCDEF = vdotq_lane_u32(vpacc1xCDEF, vb0123xCDEF, va1x01234567, 0);
142       }
143       p -= 2 * sizeof(void*);
144     } while (p != 0);
145 
146     // Subtract zero point from accumulators.
147     vnacc0 = vpadd_u32(vnacc0, vnacc0);
148     const uint32x4_t vnacc0x0123 = vcombine_u32(vnacc0, vnacc0);
149     int32x4_t vacc0x0123 = vreinterpretq_s32_u32(vsubq_u32(vpacc0x0123, vnacc0x0123));
150     int32x4_t vacc0x4567 = vreinterpretq_s32_u32(vsubq_u32(vpacc0x4567, vnacc0x0123));
151     int32x4_t vacc0x89AB = vreinterpretq_s32_u32(vsubq_u32(vpacc0x89AB, vnacc0x0123));
152     int32x4_t vacc0xCDEF = vreinterpretq_s32_u32(vsubq_u32(vpacc0xCDEF, vnacc0x0123));
153     vnacc1 = vpadd_u32(vnacc1, vnacc1);
154     const uint32x4_t vnacc1x0123 = vcombine_u32(vnacc1, vnacc1);
155     int32x4_t vacc1x0123 = vreinterpretq_s32_u32(vsubq_u32(vpacc1x0123, vnacc1x0123));
156     int32x4_t vacc1x4567 = vreinterpretq_s32_u32(vsubq_u32(vpacc1x4567, vnacc1x0123));
157     int32x4_t vacc1x89AB = vreinterpretq_s32_u32(vsubq_u32(vpacc1x89AB, vnacc1x0123));
158     int32x4_t vacc1xCDEF = vreinterpretq_s32_u32(vsubq_u32(vpacc1xCDEF, vnacc1x0123));
159 
160     float32x4_t vfpacc0x0123 = vcvtq_f32_s32(vacc0x0123);
161     float32x4_t vfpacc0x4567 = vcvtq_f32_s32(vacc0x4567);
162     float32x4_t vfpacc0x89AB = vcvtq_f32_s32(vacc0x89AB);
163     float32x4_t vfpacc0xCDEF = vcvtq_f32_s32(vacc0xCDEF);
164     float32x4_t vfpacc1x0123 = vcvtq_f32_s32(vacc1x0123);
165     float32x4_t vfpacc1x4567 = vcvtq_f32_s32(vacc1x4567);
166     float32x4_t vfpacc1x89AB = vcvtq_f32_s32(vacc1x89AB);
167     float32x4_t vfpacc1xCDEF = vcvtq_f32_s32(vacc1xCDEF);
168 
169     const float32x4_t vscale = vld1q_dup_f32(&params->fp32_neonv8.scale);
170     vfpacc0x0123 = vmulq_f32(vfpacc0x0123, vscale);
171     vfpacc0x4567 = vmulq_f32(vfpacc0x4567, vscale);
172     vfpacc0x89AB = vmulq_f32(vfpacc0x89AB, vscale);
173     vfpacc0xCDEF = vmulq_f32(vfpacc0xCDEF, vscale);
174     vfpacc1x0123 = vmulq_f32(vfpacc1x0123, vscale);
175     vfpacc1x4567 = vmulq_f32(vfpacc1x4567, vscale);
176     vfpacc1x89AB = vmulq_f32(vfpacc1x89AB, vscale);
177     vfpacc1xCDEF = vmulq_f32(vfpacc1xCDEF, vscale);
178 
179     vacc0x0123 = vcvtnq_s32_f32(vfpacc0x0123);
180     vacc0x4567 = vcvtnq_s32_f32(vfpacc0x4567);
181     vacc0x89AB = vcvtnq_s32_f32(vfpacc0x89AB);
182     vacc0xCDEF = vcvtnq_s32_f32(vfpacc0xCDEF);
183     vacc1x0123 = vcvtnq_s32_f32(vfpacc1x0123);
184     vacc1x4567 = vcvtnq_s32_f32(vfpacc1x4567);
185     vacc1x89AB = vcvtnq_s32_f32(vfpacc1x89AB);
186     vacc1xCDEF = vcvtnq_s32_f32(vfpacc1xCDEF);
187 
188     const int16x8_t voutput_zero_point = vld1q_dup_s16(&params->fp32_neonv8.output_zero_point);
189 #if XNN_ARCH_ARM64
190     const int16x8_t vacc0x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc0x0123), vacc0x4567), voutput_zero_point);
191     const int16x8_t vacc0x89ABCDEF = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc0x89AB), vacc0xCDEF), voutput_zero_point);
192     const int16x8_t vacc1x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc1x0123), vacc1x4567), voutput_zero_point);
193     const int16x8_t vacc1x89ABCDEF = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc1x89AB), vacc1xCDEF), voutput_zero_point);
194 
195     uint8x16_t vout0x0123456789ABCDEF = vqmovun_high_s16(vqmovun_s16(vacc0x01234567), vacc0x89ABCDEF);
196     uint8x16_t vout1x0123456789ABCDEF = vqmovun_high_s16(vqmovun_s16(vacc1x01234567), vacc1x89ABCDEF);
197 #else
198     const int16x8_t vacc0x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc0x0123), vqmovn_s32(vacc0x4567)), voutput_zero_point);
199     const int16x8_t vacc0x89ABCDEF = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc0x89AB), vqmovn_s32(vacc0xCDEF)), voutput_zero_point);
200     const int16x8_t vacc1x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc1x0123), vqmovn_s32(vacc1x4567)), voutput_zero_point);
201     const int16x8_t vacc1x89ABCDEF = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc1x89AB), vqmovn_s32(vacc1xCDEF)), voutput_zero_point);
202 
203     uint8x16_t vout0x0123456789ABCDEF = vcombine_u8(vqmovun_s16(vacc0x01234567), vqmovun_s16(vacc0x89ABCDEF));
204     uint8x16_t vout1x0123456789ABCDEF = vcombine_u8(vqmovun_s16(vacc1x01234567), vqmovun_s16(vacc1x89ABCDEF));
205 #endif
206     const uint8x16_t voutput_min = vld1q_dup_u8(&params->fp32_neonv8.output_min);
207     const uint8x16_t voutput_max = vld1q_dup_u8(&params->fp32_neonv8.output_max);
208 
209     vout0x0123456789ABCDEF = vmaxq_u8(vout0x0123456789ABCDEF, voutput_min);
210     vout1x0123456789ABCDEF = vmaxq_u8(vout1x0123456789ABCDEF, voutput_min);
211 
212     vout0x0123456789ABCDEF = vminq_u8(vout0x0123456789ABCDEF, voutput_max);
213     vout1x0123456789ABCDEF = vminq_u8(vout1x0123456789ABCDEF, voutput_max);
214 
215     if (nc >= 16) {
216       vst1q_u8(c1 + 0, vout1x0123456789ABCDEF);
217       vst1q_u8(c0 + 0, vout0x0123456789ABCDEF);
218 
219       c1 = (uint8_t*) ((uintptr_t) c1 + cn_stride);
220       c0 = (uint8_t*) ((uintptr_t) c0 + cn_stride);
221 
222       a = (const uint8_t**restrict) ((uintptr_t) a - ks);
223 
224       nc -= 16;
225     } else {
226       uint8x16_t vout0x01234567_1x01234567 = vcombine_u8(vget_low_u8(vout0x0123456789ABCDEF), vget_low_u8(vout1x0123456789ABCDEF));
227       if (nc & 8) {
228         vst1_u8(c1, vget_high_u8(vout0x01234567_1x01234567)); c1 += 8;
229         vst1_u8(c0, vget_low_u8(vout0x01234567_1x01234567)); c0 += 8;
230         vout0x01234567_1x01234567 = vcombine_u8(vget_high_u8(vout0x0123456789ABCDEF), vget_high_u8(vout1x0123456789ABCDEF));
231       }
232       if (nc & 4) {
233         vst1q_lane_u32((void*) c1, vreinterpretq_u32_u8(vout0x01234567_1x01234567), 2); c1 += 4;
234         vst1q_lane_u32((void*) c0, vreinterpretq_u32_u8(vout0x01234567_1x01234567), 0); c0 += 4;
235         vout0x01234567_1x01234567 = vextq_u8(vout0x01234567_1x01234567, vout0x01234567_1x01234567, 4);
236       }
237       if (nc & 2) {
238         vst1q_lane_u16((void*) c1, vreinterpretq_u16_u8(vout0x01234567_1x01234567), 4); c1 += 2;
239         vst1q_lane_u16((void*) c0, vreinterpretq_u16_u8(vout0x01234567_1x01234567), 0); c0 += 2;
240         vout0x01234567_1x01234567 = vextq_u8(vout0x01234567_1x01234567, vout0x01234567_1x01234567, 2);
241       }
242       if (nc & 1) {
243         vst1q_lane_u8(c1, vout0x01234567_1x01234567, 8);
244         vst1q_lane_u8(c0, vout0x01234567_1x01234567, 0);
245       }
246 
247       nc = 0;
248     }
249   } while (nc != 0);
250 }
251