Debian Codegraph @

src/qs8-qc8w-igemm/gen/qs8-qc8w-igemm-4x8c8-minmax-fp32-neoni8mm.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/qs8-igemm/c8-neoni8mm.c.in
3 // Generator: tools/xngen
4 //
5 // Copyright 2023 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
19 void xnn_qs8_qc8w_igemm_minmax_fp32_ukernel_4x8c8__neoni8mm(
20 size_t mr,
21 size_t nc,
22 size_t kc,
23 size_t ks,
24 const int8_t** restrict a,
25 const void* restrict w,
26 int8_t* restrict c,
27 size_t cm_stride,
28 size_t cn_stride,
29 size_t a_offset,
30 const int8_t* zero,
31 const union xnn_qs8_qc8w_conv_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
32 {
33 assert(mr != 0);
34 assert(mr <= 4);
35 assert(nc != 0);
36 assert(kc != 0);
37 assert(ks != 0);
38 assert(ks % (4 * sizeof(void*)) == 0);
39 assert(a_offset % sizeof(int8_t) == 0);
40 assert(a != NULL);
41 assert(w != NULL);
42 assert(c != NULL);
43
44 kc = round_up_po2(kc, 8 * sizeof(int8_t));
45 int8_t* c0 = c;
46 int8_t* c1 = (int8_t*) ((uintptr_t) c0 + cm_stride);
47 if XNN_UNPREDICTABLE(mr < 2) {
48 c1 = c0;
49 }
50 int8_t* c2 = (int8_t*) ((uintptr_t) c1 + cm_stride);
51 if XNN_UNPREDICTABLE(mr <= 2) {
52 c2 = c1;
53 }
54 int8_t* c3 = (int8_t*) ((uintptr_t) c2 + cm_stride);
55 if XNN_UNPREDICTABLE(mr != 4) {
56 c3 = c2;
57 }
58
59 do {
60 const uint64x2x2_t vbias01x0123 = vld2q_dup_u64(w); w = (const int32_t*) w + 4;
61 const uint64x2x2_t vbias01x4567 = vld2q_dup_u64(w); w = (const int32_t*) w + 4;
62 int32x4_t vacc01x01 = vreinterpretq_s32_u64(vbias01x0123.val[0]);
63 int32x4_t vacc01x23 = vreinterpretq_s32_u64(vbias01x0123.val[1]);
64 int32x4_t vacc01x45 = vreinterpretq_s32_u64(vbias01x4567.val[0]);
65 int32x4_t vacc01x67 = vreinterpretq_s32_u64(vbias01x4567.val[1]);
66 int32x4_t vacc23x01 = vreinterpretq_s32_u64(vbias01x0123.val[0]);
67 int32x4_t vacc23x23 = vreinterpretq_s32_u64(vbias01x0123.val[1]);
68 int32x4_t vacc23x45 = vreinterpretq_s32_u64(vbias01x4567.val[0]);
69 int32x4_t vacc23x67 = vreinterpretq_s32_u64(vbias01x4567.val[1]);
70
71 size_t p = ks;
72 do {
73 const int8_t* restrict a0 = a[0];
74 if XNN_UNPREDICTABLE(a0 != zero) {
75 a0 = (const int8_t*) ((uintptr_t) a0 + a_offset);
76 }
77 const int8_t* restrict a1 = a[1];
78 if XNN_UNPREDICTABLE(a1 != zero) {
79 a1 = (const int8_t*) ((uintptr_t) a1 + a_offset);
80 }
81 const int8_t* restrict a2 = a[2];
82 if XNN_UNPREDICTABLE(a2 != zero) {
83 a2 = (const int8_t*) ((uintptr_t) a2 + a_offset);
84 }
85 const int8_t* restrict a3 = a[3];
86 if XNN_UNPREDICTABLE(a3 != zero) {
87 a3 = (const int8_t*) ((uintptr_t) a3 + a_offset);
88 }
89 a += 4;
90
91 uint64x2x2_t va01x0123456789ABCDEF;
92 va01x0123456789ABCDEF.val[0] = vdupq_n_u64(0);
93 va01x0123456789ABCDEF.val[1] = vdupq_n_u64(0);
94 uint64x2x2_t va23x0123456789ABCDEF;
95 va23x0123456789ABCDEF.val[0] = vdupq_n_u64(0);
96 va23x0123456789ABCDEF.val[1] = vdupq_n_u64(0);
97
98 // Inner accumulation loop along the 8 columns.
99 size_t k = kc;
100 // 2x partial unrolled loop to load 8 bytes at a time.
101 while (k >= 16 * sizeof(int8_t)) {
102 // Load a 4x16 block of activations.
103 va01x0123456789ABCDEF = vld2q_lane_u64((const void*) a0, va01x0123456789ABCDEF, 0); a0 += 16;
104 va23x0123456789ABCDEF = vld2q_lane_u64((const void*) a2, va23x0123456789ABCDEF, 0); a2 += 16;
105 va01x0123456789ABCDEF = vld2q_lane_u64((const void*) a1, va01x0123456789ABCDEF, 1); a1 += 16;
106 va23x0123456789ABCDEF = vld2q_lane_u64((const void*) a3, va23x0123456789ABCDEF, 1); a3 += 16;
107
108 // Load a 16x8 block of weights.
109 const int8x16_t vb01x01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
110 const int8x16_t vb23x01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
111 const int8x16_t vb45x01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
112 const int8x16_t vb67x01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
113 const int8x16_t vb01x89ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
114 const int8x16_t vb23x89ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
115 const int8x16_t vb45x89ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
116 const int8x16_t vb67x89ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
117
118 // Multiply-accumulate: 4x16 * 16x8 --> 4x8.
119 vacc01x01 = vmmlaq_s32(vacc01x01, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[0]), vb01x01234567);
120 vacc01x23 = vmmlaq_s32(vacc01x23, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[0]), vb23x01234567);
121 vacc01x45 = vmmlaq_s32(vacc01x45, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[0]), vb45x01234567);
122 vacc01x67 = vmmlaq_s32(vacc01x67, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[0]), vb67x01234567);
123 vacc23x01 = vmmlaq_s32(vacc23x01, vreinterpretq_s8_u64(va23x0123456789ABCDEF.val[0]), vb01x01234567);
124 vacc23x23 = vmmlaq_s32(vacc23x23, vreinterpretq_s8_u64(va23x0123456789ABCDEF.val[0]), vb23x01234567);
125 vacc23x45 = vmmlaq_s32(vacc23x45, vreinterpretq_s8_u64(va23x0123456789ABCDEF.val[0]), vb45x01234567);
126 vacc23x67 = vmmlaq_s32(vacc23x67, vreinterpretq_s8_u64(va23x0123456789ABCDEF.val[0]), vb67x01234567);
127 vacc01x01 = vmmlaq_s32(vacc01x01, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[1]), vb01x89ABCDEF);
128 vacc01x23 = vmmlaq_s32(vacc01x23, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[1]), vb23x89ABCDEF);
129 vacc01x45 = vmmlaq_s32(vacc01x45, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[1]), vb45x89ABCDEF);
130 vacc01x67 = vmmlaq_s32(vacc01x67, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[1]), vb67x89ABCDEF);
131 vacc23x01 = vmmlaq_s32(vacc23x01, vreinterpretq_s8_u64(va23x0123456789ABCDEF.val[1]), vb01x89ABCDEF);
132 vacc23x23 = vmmlaq_s32(vacc23x23, vreinterpretq_s8_u64(va23x0123456789ABCDEF.val[1]), vb23x89ABCDEF);
133 vacc23x45 = vmmlaq_s32(vacc23x45, vreinterpretq_s8_u64(va23x0123456789ABCDEF.val[1]), vb45x89ABCDEF);
134 vacc23x67 = vmmlaq_s32(vacc23x67, vreinterpretq_s8_u64(va23x0123456789ABCDEF.val[1]), vb67x89ABCDEF);
135
136 k -= 16 * sizeof(int8_t);
137 }
138 // Handle up to 8 final positions of `k`
139 if XNN_UNLIKELY(k != 0) {
140 // Load a 4x8 block of activations.
141 uint64x2_t va01x01234567 = vld1q_dup_u64((const void*) a0); a0 += 8;
142 uint64x2_t va23x01234567 = vld1q_dup_u64((const void*) a2); a2 += 8;
143 va01x01234567 = vld1q_lane_u64((const void*) a1, va01x01234567, 1); a1 += 8;
144 va23x01234567 = vld1q_lane_u64((const void*) a3, va23x01234567, 1); a3 += 8;
145
146 // Load a 16x8 block of weights.
147 const int8x16_t vb01x01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
148 const int8x16_t vb23x01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
149 const int8x16_t vb45x01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
150 const int8x16_t vb67x01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
151
152 // Multiply-accumulate: 4x4 * 4x8 --> 4x8.
153 vacc01x01 = vmmlaq_s32(vacc01x01, vreinterpretq_s8_u64(va01x01234567), vb01x01234567);
154 vacc01x23 = vmmlaq_s32(vacc01x23, vreinterpretq_s8_u64(va01x01234567), vb23x01234567);
155 vacc01x45 = vmmlaq_s32(vacc01x45, vreinterpretq_s8_u64(va01x01234567), vb45x01234567);
156 vacc01x67 = vmmlaq_s32(vacc01x67, vreinterpretq_s8_u64(va01x01234567), vb67x01234567);
157 vacc23x01 = vmmlaq_s32(vacc23x01, vreinterpretq_s8_u64(va23x01234567), vb01x01234567);
158 vacc23x23 = vmmlaq_s32(vacc23x23, vreinterpretq_s8_u64(va23x01234567), vb23x01234567);
159 vacc23x45 = vmmlaq_s32(vacc23x45, vreinterpretq_s8_u64(va23x01234567), vb45x01234567);
160 vacc23x67 = vmmlaq_s32(vacc23x67, vreinterpretq_s8_u64(va23x01234567), vb67x01234567);
161 }
162
163 p -= 4 * sizeof(void*);
164 } while (p != 0);
165
166 int32x4_t vacc0x0123 = vcombine_s32(vget_low_s32(vacc01x01), vget_low_s32(vacc01x23));
167 int32x4_t vacc1x0123 = vcombine_s32(vget_high_s32(vacc01x01), vget_high_s32(vacc01x23));
168 int32x4_t vacc0x4567 = vcombine_s32(vget_low_s32(vacc01x45), vget_low_s32(vacc01x67));
169 int32x4_t vacc1x4567 = vcombine_s32(vget_high_s32(vacc01x45), vget_high_s32(vacc01x67));
170 int32x4_t vacc2x0123 = vcombine_s32(vget_low_s32(vacc23x01), vget_low_s32(vacc23x23));
171 int32x4_t vacc3x0123 = vcombine_s32(vget_high_s32(vacc23x01), vget_high_s32(vacc23x23));
172 int32x4_t vacc2x4567 = vcombine_s32(vget_low_s32(vacc23x45), vget_low_s32(vacc23x67));
173 int32x4_t vacc3x4567 = vcombine_s32(vget_high_s32(vacc23x45), vget_high_s32(vacc23x67));
174
175 float32x4_t vfpacc0x0123 = vcvtq_f32_s32(vacc0x0123);
176 float32x4_t vfpacc0x4567 = vcvtq_f32_s32(vacc0x4567);
177 float32x4_t vfpacc1x0123 = vcvtq_f32_s32(vacc1x0123);
178 float32x4_t vfpacc1x4567 = vcvtq_f32_s32(vacc1x4567);
179 float32x4_t vfpacc2x0123 = vcvtq_f32_s32(vacc2x0123);
180 float32x4_t vfpacc2x4567 = vcvtq_f32_s32(vacc2x4567);
181 float32x4_t vfpacc3x0123 = vcvtq_f32_s32(vacc3x0123);
182 float32x4_t vfpacc3x4567 = vcvtq_f32_s32(vacc3x4567);
183
184 const float32x4_t vscale0123 = vld1q_f32((const float*) w); w = (const float*) w + 4;
185 vfpacc0x0123 = vmulq_f32(vfpacc0x0123, vscale0123);
186 vfpacc1x0123 = vmulq_f32(vfpacc1x0123, vscale0123);
187 vfpacc2x0123 = vmulq_f32(vfpacc2x0123, vscale0123);
188 vfpacc3x0123 = vmulq_f32(vfpacc3x0123, vscale0123);
189 const float32x4_t vscale4567 = vld1q_f32((const float*) w); w = (const float*) w + 4;
190 vfpacc0x4567 = vmulq_f32(vfpacc0x4567, vscale4567);
191 vfpacc1x4567 = vmulq_f32(vfpacc1x4567, vscale4567);
192 vfpacc2x4567 = vmulq_f32(vfpacc2x4567, vscale4567);
193 vfpacc3x4567 = vmulq_f32(vfpacc3x4567, vscale4567);
194
195 vacc0x0123 = vcvtnq_s32_f32(vfpacc0x0123);
196 vacc0x4567 = vcvtnq_s32_f32(vfpacc0x4567);
197 vacc1x0123 = vcvtnq_s32_f32(vfpacc1x0123);
198 vacc1x4567 = vcvtnq_s32_f32(vfpacc1x4567);
199 vacc2x0123 = vcvtnq_s32_f32(vfpacc2x0123);
200 vacc2x4567 = vcvtnq_s32_f32(vfpacc2x4567);
201 vacc3x0123 = vcvtnq_s32_f32(vfpacc3x0123);
202 vacc3x4567 = vcvtnq_s32_f32(vfpacc3x4567);
203
204 const int16x8_t voutput_zero_point = vld1q_dup_s16(&params->fp32_neonv8.output_zero_point);
205 #if XNN_ARCH_ARM64
206 const int16x8_t vacc0x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc0x0123), vacc0x4567), voutput_zero_point);
207 const int16x8_t vacc1x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc1x0123), vacc1x4567), voutput_zero_point);
208 const int16x8_t vacc2x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc2x0123), vacc2x4567), voutput_zero_point);
209 const int16x8_t vacc3x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc3x0123), vacc3x4567), voutput_zero_point);
210
211 int8x16_t vout0x01234567_1x01234567 = vqmovn_high_s16(vqmovn_s16(vacc0x01234567), vacc1x01234567);
212 int8x16_t vout2x01234567_3x01234567 = vqmovn_high_s16(vqmovn_s16(vacc2x01234567), vacc3x01234567);
213 #else
214 const int16x8_t vacc0x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc0x0123), vqmovn_s32(vacc0x4567)), voutput_zero_point);
215 const int16x8_t vacc1x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc1x0123), vqmovn_s32(vacc1x4567)), voutput_zero_point);
216 const int16x8_t vacc2x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc2x0123), vqmovn_s32(vacc2x4567)), voutput_zero_point);
217 const int16x8_t vacc3x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc3x0123), vqmovn_s32(vacc3x4567)), voutput_zero_point);
218
219 int8x16_t vout0x01234567_1x01234567 = vcombine_s8(vqmovn_s16(vacc0x01234567), vqmovn_s16(vacc1x01234567));
220 int8x16_t vout2x01234567_3x01234567 = vcombine_s8(vqmovn_s16(vacc2x01234567), vqmovn_s16(vacc3x01234567));
221 #endif
222 const int8x16_t voutput_min = vld1q_dup_s8(&params->fp32_neonv8.output_min);
223 const int8x16_t voutput_max = vld1q_dup_s8(&params->fp32_neonv8.output_max);
224
225 vout2x01234567_3x01234567 = vmaxq_s8(vout2x01234567_3x01234567, voutput_min);
226 vout0x01234567_1x01234567 = vmaxq_s8(vout0x01234567_1x01234567, voutput_min);
227
228 vout2x01234567_3x01234567 = vminq_s8(vout2x01234567_3x01234567, voutput_max);
229 vout0x01234567_1x01234567 = vminq_s8(vout0x01234567_1x01234567, voutput_max);
230
231 if (nc >= 8) {
232 vst1_s8(c3, vget_high_s8(vout2x01234567_3x01234567));
233 vst1_s8(c2, vget_low_s8(vout2x01234567_3x01234567));
234 vst1_s8(c1, vget_high_s8(vout0x01234567_1x01234567));
235 vst1_s8(c0, vget_low_s8(vout0x01234567_1x01234567));
236
237 c3 = (int8_t*) ((uintptr_t) c3 + cn_stride);
238 c2 = (int8_t*) ((uintptr_t) c2 + cn_stride);
239 c1 = (int8_t*) ((uintptr_t) c1 + cn_stride);
240 c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
241
242 a = (const int8_t**restrict) ((uintptr_t) a - ks);
243
244 nc -= 8;
245 } else {
246 if (nc & 4) {
247 vst1q_lane_u32((void*) c3, vreinterpretq_u32_s8(vout2x01234567_3x01234567), 2); c3 += 4;
248 vst1q_lane_u32((void*) c2, vreinterpretq_u32_s8(vout2x01234567_3x01234567), 0); c2 += 4;
249 vst1q_lane_u32((void*) c1, vreinterpretq_u32_s8(vout0x01234567_1x01234567), 2); c1 += 4;
250 vst1q_lane_u32((void*) c0, vreinterpretq_u32_s8(vout0x01234567_1x01234567), 0); c0 += 4;
251 vout2x01234567_3x01234567 = vextq_s8(vout2x01234567_3x01234567, vout2x01234567_3x01234567, 4);
252 vout0x01234567_1x01234567 = vextq_s8(vout0x01234567_1x01234567, vout0x01234567_1x01234567, 4);
253 }
254 if (nc & 2) {
255 vst1q_lane_u16((void*) c3, vreinterpretq_u16_s8(vout2x01234567_3x01234567), 4); c3 += 2;
256 vst1q_lane_u16((void*) c2, vreinterpretq_u16_s8(vout2x01234567_3x01234567), 0); c2 += 2;
257 vst1q_lane_u16((void*) c1, vreinterpretq_u16_s8(vout0x01234567_1x01234567), 4); c1 += 2;
258 vst1q_lane_u16((void*) c0, vreinterpretq_u16_s8(vout0x01234567_1x01234567), 0); c0 += 2;
259 vout2x01234567_3x01234567 = vextq_s8(vout2x01234567_3x01234567, vout2x01234567_3x01234567, 2);
260 vout0x01234567_1x01234567 = vextq_s8(vout0x01234567_1x01234567, vout0x01234567_1x01234567, 2);
261 }
262 if (nc & 1) {
263 vst1q_lane_s8(c3, vout2x01234567_3x01234567, 8);
264 vst1q_lane_s8(c2, vout2x01234567_3x01234567, 0);
265 vst1q_lane_s8(c1, vout0x01234567_1x01234567, 8);
266 vst1q_lane_s8(c0, vout0x01234567_1x01234567, 0);
267 }
268
269 nc = 0;
270 }
271 } while (nc != 0);
272 }