1 // Auto-generated file. Do not edit!
2 // Template: src/qs8-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/math.h>
16
17
xnn_qs8_igemm_minmax_rndnu_ukernel_6x8c4__neondot(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)])18 void xnn_qs8_igemm_minmax_rndnu_ukernel_6x8c4__neondot(
19 size_t mr,
20 size_t nc,
21 size_t kc,
22 size_t ks,
23 const int8_t** restrict a,
24 const void* restrict w,
25 int8_t* restrict c,
26 size_t cm_stride,
27 size_t cn_stride,
28 size_t a_offset,
29 const int8_t* zero,
30 const union xnn_qs8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
31 {
32 assert(mr != 0);
33 assert(mr <= 6);
34 assert(nc != 0);
35 assert(kc != 0);
36 assert(ks != 0);
37 assert(ks % (6 * sizeof(void*)) == 0);
38 assert(a_offset % sizeof(int8_t) == 0);
39 assert(a != NULL);
40 assert(w != NULL);
41 assert(c != NULL);
42
43 kc = round_up_po2(kc, 4 * sizeof(int8_t));
44 int8_t* c0 = c;
45 int8_t* c1 = (int8_t*) ((uintptr_t) c0 + cm_stride);
46 if XNN_UNPREDICTABLE(mr < 2) {
47 c1 = c0;
48 }
49 int8_t* c2 = (int8_t*) ((uintptr_t) c1 + cm_stride);
50 if XNN_UNPREDICTABLE(mr <= 2) {
51 c2 = c1;
52 }
53 int8_t* c3 = (int8_t*) ((uintptr_t) c2 + cm_stride);
54 if XNN_UNPREDICTABLE(mr < 4) {
55 c3 = c2;
56 }
57 int8_t* c4 = (int8_t*) ((uintptr_t) c3 + cm_stride);
58 if XNN_UNPREDICTABLE(mr <= 4) {
59 c4 = c3;
60 }
61 int8_t* c5 = (int8_t*) ((uintptr_t) c4 + cm_stride);
62 if XNN_UNPREDICTABLE(mr != 6) {
63 c5 = c4;
64 }
65
66 do {
67 int32x4_t vacc0x0123 = vld1q_s32(w); w = (const void*) ((const int32_t*) w + 4);
68 int32x4_t vacc0x4567 = vld1q_s32(w); w = (const void*) ((const int32_t*) w + 4);
69 int32x4_t vacc1x0123 = vacc0x0123;
70 int32x4_t vacc1x4567 = vacc0x4567;
71 int32x4_t vacc2x0123 = vacc0x0123;
72 int32x4_t vacc2x4567 = vacc0x4567;
73 int32x4_t vacc3x0123 = vacc0x0123;
74 int32x4_t vacc3x4567 = vacc0x4567;
75 int32x4_t vacc4x0123 = vacc0x0123;
76 int32x4_t vacc4x4567 = vacc0x4567;
77 int32x4_t vacc5x0123 = vacc0x0123;
78 int32x4_t vacc5x4567 = vacc0x4567;
79
80 size_t p = ks;
81 do {
82 const int8_t* restrict a0 = a[0];
83 if XNN_UNPREDICTABLE(a0 != zero) {
84 a0 = (const int8_t*) ((uintptr_t) a0 + a_offset);
85 }
86 const int8_t* restrict a1 = a[1];
87 if XNN_UNPREDICTABLE(a1 != zero) {
88 a1 = (const int8_t*) ((uintptr_t) a1 + a_offset);
89 }
90 const int8_t* restrict a2 = a[2];
91 if XNN_UNPREDICTABLE(a2 != zero) {
92 a2 = (const int8_t*) ((uintptr_t) a2 + a_offset);
93 }
94 const int8_t* restrict a3 = a[3];
95 if XNN_UNPREDICTABLE(a3 != zero) {
96 a3 = (const int8_t*) ((uintptr_t) a3 + a_offset);
97 }
98 const int8_t* restrict a4 = a[4];
99 if XNN_UNPREDICTABLE(a4 != zero) {
100 a4 = (const int8_t*) ((uintptr_t) a4 + a_offset);
101 }
102 const int8_t* restrict a5 = a[5];
103 if XNN_UNPREDICTABLE(a5 != zero) {
104 a5 = (const int8_t*) ((uintptr_t) a5 + a_offset);
105 }
106 a += 6;
107
108 // Inner accumulation loop along the 8 columns.
109 size_t k = kc;
110 // 2x partial unrolled loop to load 8 bytes at a time.
111 while (k >= 8 * sizeof(int8_t)) {
112 // Load a 6x8 block of activations.
113 const int8x8_t va0x01234567 = vld1_s8(a0); a0 += 8;
114 const int8x8_t va1x01234567 = vld1_s8(a1); a1 += 8;
115 const int8x8_t va2x01234567 = vld1_s8(a2); a2 += 8;
116 const int8x8_t va3x01234567 = vld1_s8(a3); a3 += 8;
117 const int8x8_t va4x01234567 = vld1_s8(a4); a4 += 8;
118 const int8x8_t va5x01234567 = vld1_s8(a5); a5 += 8;
119
120 // Load a 8x8 block of weights.
121 const int8x16_t vb0123x0123 = vld1q_s8(w); w = (const void*) ((const int8_t*) w + 16);
122 const int8x16_t vb0123x4567 = vld1q_s8(w); w = (const void*) ((const int8_t*) w + 16);
123 const int8x16_t vb4567x0123 = vld1q_s8(w); w = (const void*) ((const int8_t*) w + 16);
124 const int8x16_t vb4567x4567 = vld1q_s8(w); w = (const void*) ((const int8_t*) w + 16);
125
126 // Multiply-accumulate: 6x8 * 8x8 --> 6x8.
127 vacc0x0123 = vdotq_lane_s32(vacc0x0123, vb0123x0123, va0x01234567, 0);
128 vacc0x4567 = vdotq_lane_s32(vacc0x4567, vb0123x4567, va0x01234567, 0);
129 vacc1x0123 = vdotq_lane_s32(vacc1x0123, vb0123x0123, va1x01234567, 0);
130 vacc1x4567 = vdotq_lane_s32(vacc1x4567, vb0123x4567, va1x01234567, 0);
131 vacc2x0123 = vdotq_lane_s32(vacc2x0123, vb0123x0123, va2x01234567, 0);
132 vacc2x4567 = vdotq_lane_s32(vacc2x4567, vb0123x4567, va2x01234567, 0);
133 vacc3x0123 = vdotq_lane_s32(vacc3x0123, vb0123x0123, va3x01234567, 0);
134 vacc3x4567 = vdotq_lane_s32(vacc3x4567, vb0123x4567, va3x01234567, 0);
135 vacc4x0123 = vdotq_lane_s32(vacc4x0123, vb0123x0123, va4x01234567, 0);
136 vacc4x4567 = vdotq_lane_s32(vacc4x4567, vb0123x4567, va4x01234567, 0);
137 vacc5x0123 = vdotq_lane_s32(vacc5x0123, vb0123x0123, va5x01234567, 0);
138 vacc5x4567 = vdotq_lane_s32(vacc5x4567, vb0123x4567, va5x01234567, 0);
139 vacc0x0123 = vdotq_lane_s32(vacc0x0123, vb4567x0123, va0x01234567, 1);
140 vacc0x4567 = vdotq_lane_s32(vacc0x4567, vb4567x4567, va0x01234567, 1);
141 vacc1x0123 = vdotq_lane_s32(vacc1x0123, vb4567x0123, va1x01234567, 1);
142 vacc1x4567 = vdotq_lane_s32(vacc1x4567, vb4567x4567, va1x01234567, 1);
143 vacc2x0123 = vdotq_lane_s32(vacc2x0123, vb4567x0123, va2x01234567, 1);
144 vacc2x4567 = vdotq_lane_s32(vacc2x4567, vb4567x4567, va2x01234567, 1);
145 vacc3x0123 = vdotq_lane_s32(vacc3x0123, vb4567x0123, va3x01234567, 1);
146 vacc3x4567 = vdotq_lane_s32(vacc3x4567, vb4567x4567, va3x01234567, 1);
147 vacc4x0123 = vdotq_lane_s32(vacc4x0123, vb4567x0123, va4x01234567, 1);
148 vacc4x4567 = vdotq_lane_s32(vacc4x4567, vb4567x4567, va4x01234567, 1);
149 vacc5x0123 = vdotq_lane_s32(vacc5x0123, vb4567x0123, va5x01234567, 1);
150 vacc5x4567 = vdotq_lane_s32(vacc5x4567, vb4567x4567, va5x01234567, 1);
151
152 k -= 8 * sizeof(int8_t);
153 }
154 // Handle up to 4 final positions of `k`
155 if XNN_UNLIKELY(k != 0) {
156 // Load a 6x4 block of activations.
157 const int8x8_t va0x01234567 = vld1_s8(a0);
158 const int8x8_t va1x01234567 = vld1_s8(a1);
159 const int8x8_t va2x01234567 = vld1_s8(a2);
160 const int8x8_t va3x01234567 = vld1_s8(a3);
161 const int8x8_t va4x01234567 = vld1_s8(a4);
162 const int8x8_t va5x01234567 = vld1_s8(a5);
163
164 // Load a 4x8 block of weights.
165 const int8x16_t vb0123x0123 = vld1q_s8(w); w = (const void*) ((const int8_t*) w + 16);
166 const int8x16_t vb0123x4567 = vld1q_s8(w); w = (const void*) ((const int8_t*) w + 16);
167
168 // Multiply-accumulate: 6x4 * 4x8 --> 6x8.
169 vacc0x0123 = vdotq_lane_s32(vacc0x0123, vb0123x0123, va0x01234567, 0);
170 vacc0x4567 = vdotq_lane_s32(vacc0x4567, vb0123x4567, va0x01234567, 0);
171 vacc1x0123 = vdotq_lane_s32(vacc1x0123, vb0123x0123, va1x01234567, 0);
172 vacc1x4567 = vdotq_lane_s32(vacc1x4567, vb0123x4567, va1x01234567, 0);
173 vacc2x0123 = vdotq_lane_s32(vacc2x0123, vb0123x0123, va2x01234567, 0);
174 vacc2x4567 = vdotq_lane_s32(vacc2x4567, vb0123x4567, va2x01234567, 0);
175 vacc3x0123 = vdotq_lane_s32(vacc3x0123, vb0123x0123, va3x01234567, 0);
176 vacc3x4567 = vdotq_lane_s32(vacc3x4567, vb0123x4567, va3x01234567, 0);
177 vacc4x0123 = vdotq_lane_s32(vacc4x0123, vb0123x0123, va4x01234567, 0);
178 vacc4x4567 = vdotq_lane_s32(vacc4x4567, vb0123x4567, va4x01234567, 0);
179 vacc5x0123 = vdotq_lane_s32(vacc5x0123, vb0123x0123, va5x01234567, 0);
180 vacc5x4567 = vdotq_lane_s32(vacc5x4567, vb0123x4567, va5x01234567, 0);
181 }
182 p -= 6 * sizeof(void*);
183 } while (p != 0);
184
185 const int32x4_t vright_pre_shift = vld1q_dup_s32(¶ms->rndnu_neon.right_pre_shift);
186 const int32x4_t vmultiplier = vld1q_dup_s32(¶ms->rndnu_neon.multiplier);
187 const int32x4_t vright_post_shift = vld1q_dup_s32(¶ms->rndnu_neon.right_post_shift);
188
189 vacc0x0123 = vshlq_s32(vacc0x0123, vright_pre_shift);
190 vacc0x4567 = vshlq_s32(vacc0x4567, vright_pre_shift);
191 vacc1x0123 = vshlq_s32(vacc1x0123, vright_pre_shift);
192 vacc1x4567 = vshlq_s32(vacc1x4567, vright_pre_shift);
193 vacc2x0123 = vshlq_s32(vacc2x0123, vright_pre_shift);
194 vacc2x4567 = vshlq_s32(vacc2x4567, vright_pre_shift);
195 vacc3x0123 = vshlq_s32(vacc3x0123, vright_pre_shift);
196 vacc3x4567 = vshlq_s32(vacc3x4567, vright_pre_shift);
197 vacc4x0123 = vshlq_s32(vacc4x0123, vright_pre_shift);
198 vacc4x4567 = vshlq_s32(vacc4x4567, vright_pre_shift);
199 vacc5x0123 = vshlq_s32(vacc5x0123, vright_pre_shift);
200 vacc5x4567 = vshlq_s32(vacc5x4567, vright_pre_shift);
201
202 vacc0x0123 = vqdmulhq_s32(vacc0x0123, vmultiplier);
203 vacc0x4567 = vqdmulhq_s32(vacc0x4567, vmultiplier);
204 vacc1x0123 = vqdmulhq_s32(vacc1x0123, vmultiplier);
205 vacc1x4567 = vqdmulhq_s32(vacc1x4567, vmultiplier);
206 vacc2x0123 = vqdmulhq_s32(vacc2x0123, vmultiplier);
207 vacc2x4567 = vqdmulhq_s32(vacc2x4567, vmultiplier);
208 vacc3x0123 = vqdmulhq_s32(vacc3x0123, vmultiplier);
209 vacc3x4567 = vqdmulhq_s32(vacc3x4567, vmultiplier);
210 vacc4x0123 = vqdmulhq_s32(vacc4x0123, vmultiplier);
211 vacc4x4567 = vqdmulhq_s32(vacc4x4567, vmultiplier);
212 vacc5x0123 = vqdmulhq_s32(vacc5x0123, vmultiplier);
213 vacc5x4567 = vqdmulhq_s32(vacc5x4567, vmultiplier);
214
215 vacc0x0123 = vrshlq_s32(vacc0x0123, vright_post_shift);
216 vacc0x4567 = vrshlq_s32(vacc0x4567, vright_post_shift);
217 vacc1x0123 = vrshlq_s32(vacc1x0123, vright_post_shift);
218 vacc1x4567 = vrshlq_s32(vacc1x4567, vright_post_shift);
219 vacc2x0123 = vrshlq_s32(vacc2x0123, vright_post_shift);
220 vacc2x4567 = vrshlq_s32(vacc2x4567, vright_post_shift);
221 vacc3x0123 = vrshlq_s32(vacc3x0123, vright_post_shift);
222 vacc3x4567 = vrshlq_s32(vacc3x4567, vright_post_shift);
223 vacc4x0123 = vrshlq_s32(vacc4x0123, vright_post_shift);
224 vacc4x4567 = vrshlq_s32(vacc4x4567, vright_post_shift);
225 vacc5x0123 = vrshlq_s32(vacc5x0123, vright_post_shift);
226 vacc5x4567 = vrshlq_s32(vacc5x4567, vright_post_shift);
227
228 const int16x8_t voutput_zero_point = vld1q_dup_s16(¶ms->rndnu_neon.output_zero_point);
229 #if XNN_ARCH_ARM64
230 const int16x8_t vacc0x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc0x0123), vacc0x4567), voutput_zero_point);
231 const int16x8_t vacc1x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc1x0123), vacc1x4567), voutput_zero_point);
232 const int16x8_t vacc2x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc2x0123), vacc2x4567), voutput_zero_point);
233 const int16x8_t vacc3x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc3x0123), vacc3x4567), voutput_zero_point);
234 const int16x8_t vacc4x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc4x0123), vacc4x4567), voutput_zero_point);
235 const int16x8_t vacc5x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc5x0123), vacc5x4567), voutput_zero_point);
236
237 int8x16_t vout0x01234567_1x01234567 = vqmovn_high_s16(vqmovn_s16(vacc0x01234567), vacc1x01234567);
238 int8x16_t vout2x01234567_3x01234567 = vqmovn_high_s16(vqmovn_s16(vacc2x01234567), vacc3x01234567);
239 int8x16_t vout4x01234567_5x01234567 = vqmovn_high_s16(vqmovn_s16(vacc4x01234567), vacc5x01234567);
240 #else
241 const int16x8_t vacc0x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc0x0123), vqmovn_s32(vacc0x4567)), voutput_zero_point);
242 const int16x8_t vacc1x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc1x0123), vqmovn_s32(vacc1x4567)), voutput_zero_point);
243 const int16x8_t vacc2x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc2x0123), vqmovn_s32(vacc2x4567)), voutput_zero_point);
244 const int16x8_t vacc3x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc3x0123), vqmovn_s32(vacc3x4567)), voutput_zero_point);
245 const int16x8_t vacc4x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc4x0123), vqmovn_s32(vacc4x4567)), voutput_zero_point);
246 const int16x8_t vacc5x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc5x0123), vqmovn_s32(vacc5x4567)), voutput_zero_point);
247
248 int8x16_t vout0x01234567_1x01234567 = vcombine_s8(vqmovn_s16(vacc0x01234567), vqmovn_s16(vacc1x01234567));
249 int8x16_t vout2x01234567_3x01234567 = vcombine_s8(vqmovn_s16(vacc2x01234567), vqmovn_s16(vacc3x01234567));
250 int8x16_t vout4x01234567_5x01234567 = vcombine_s8(vqmovn_s16(vacc4x01234567), vqmovn_s16(vacc5x01234567));
251 #endif
252 const int8x16_t voutput_min = vld1q_dup_s8(¶ms->rndnu_neon.output_min);
253 const int8x16_t voutput_max = vld1q_dup_s8(¶ms->rndnu_neon.output_max);
254
255 vout4x01234567_5x01234567 = vmaxq_s8(vout4x01234567_5x01234567, voutput_min);
256 vout2x01234567_3x01234567 = vmaxq_s8(vout2x01234567_3x01234567, voutput_min);
257 vout0x01234567_1x01234567 = vmaxq_s8(vout0x01234567_1x01234567, voutput_min);
258
259 vout4x01234567_5x01234567 = vminq_s8(vout4x01234567_5x01234567, voutput_max);
260 vout2x01234567_3x01234567 = vminq_s8(vout2x01234567_3x01234567, voutput_max);
261 vout0x01234567_1x01234567 = vminq_s8(vout0x01234567_1x01234567, voutput_max);
262
263 if (nc >= 8) {
264 vst1_s8(c5 + 0, vget_high_s8(vout4x01234567_5x01234567));
265 vst1_s8(c4 + 0, vget_low_s8(vout4x01234567_5x01234567));
266 vst1_s8(c3 + 0, vget_high_s8(vout2x01234567_3x01234567));
267 vst1_s8(c2 + 0, vget_low_s8(vout2x01234567_3x01234567));
268 vst1_s8(c1 + 0, vget_high_s8(vout0x01234567_1x01234567));
269 vst1_s8(c0 + 0, vget_low_s8(vout0x01234567_1x01234567));
270
271 c5 = (int8_t*) ((uintptr_t) c5 + cn_stride);
272 c4 = (int8_t*) ((uintptr_t) c4 + cn_stride);
273 c3 = (int8_t*) ((uintptr_t) c3 + cn_stride);
274 c2 = (int8_t*) ((uintptr_t) c2 + cn_stride);
275 c1 = (int8_t*) ((uintptr_t) c1 + cn_stride);
276 c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
277
278 a = (const int8_t**restrict) ((uintptr_t) a - ks);
279
280 nc -= 8;
281 } else {
282 if (nc & 4) {
283 vst1q_lane_u32((void*) c5, vreinterpretq_u32_s8(vout4x01234567_5x01234567), 2); c5 += 4;
284 vst1q_lane_u32((void*) c4, vreinterpretq_u32_s8(vout4x01234567_5x01234567), 0); c4 += 4;
285 vst1q_lane_u32((void*) c3, vreinterpretq_u32_s8(vout2x01234567_3x01234567), 2); c3 += 4;
286 vst1q_lane_u32((void*) c2, vreinterpretq_u32_s8(vout2x01234567_3x01234567), 0); c2 += 4;
287 vst1q_lane_u32((void*) c1, vreinterpretq_u32_s8(vout0x01234567_1x01234567), 2); c1 += 4;
288 vst1q_lane_u32((void*) c0, vreinterpretq_u32_s8(vout0x01234567_1x01234567), 0); c0 += 4;
289 vout4x01234567_5x01234567 = vextq_s8(vout4x01234567_5x01234567, vout4x01234567_5x01234567, 4);
290 vout2x01234567_3x01234567 = vextq_s8(vout2x01234567_3x01234567, vout2x01234567_3x01234567, 4);
291 vout0x01234567_1x01234567 = vextq_s8(vout0x01234567_1x01234567, vout0x01234567_1x01234567, 4);
292 }
293 if (nc & 2) {
294 vst1q_lane_u16((void*) c5, vreinterpretq_u16_s8(vout4x01234567_5x01234567), 4); c5 += 2;
295 vst1q_lane_u16((void*) c4, vreinterpretq_u16_s8(vout4x01234567_5x01234567), 0); c4 += 2;
296 vst1q_lane_u16((void*) c3, vreinterpretq_u16_s8(vout2x01234567_3x01234567), 4); c3 += 2;
297 vst1q_lane_u16((void*) c2, vreinterpretq_u16_s8(vout2x01234567_3x01234567), 0); c2 += 2;
298 vst1q_lane_u16((void*) c1, vreinterpretq_u16_s8(vout0x01234567_1x01234567), 4); c1 += 2;
299 vst1q_lane_u16((void*) c0, vreinterpretq_u16_s8(vout0x01234567_1x01234567), 0); c0 += 2;
300 vout4x01234567_5x01234567 = vextq_s8(vout4x01234567_5x01234567, vout4x01234567_5x01234567, 2);
301 vout2x01234567_3x01234567 = vextq_s8(vout2x01234567_3x01234567, vout2x01234567_3x01234567, 2);
302 vout0x01234567_1x01234567 = vextq_s8(vout0x01234567_1x01234567, vout0x01234567_1x01234567, 2);
303 }
304 if (nc & 1) {
305 vst1q_lane_s8(c5, vout4x01234567_5x01234567, 8);
306 vst1q_lane_s8(c4, vout4x01234567_5x01234567, 0);
307 vst1q_lane_s8(c3, vout2x01234567_3x01234567, 8);
308 vst1q_lane_s8(c2, vout2x01234567_3x01234567, 0);
309 vst1q_lane_s8(c1, vout0x01234567_1x01234567, 8);
310 vst1q_lane_s8(c0, vout0x01234567_1x01234567, 0);
311 }
312
313 nc = 0;
314 }
315 } while (nc != 0);
316 }
317