Debian Codegraph @

src/qs8-qc8w-igemm/gen/qs8-qc8w-igemm-3x8c8-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_3x8c8__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 <= 3);
35 assert(nc != 0);
36 assert(kc != 0);
37 assert(ks != 0);
38 assert(ks % (3 * 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
55 do {
56 const uint64x2x2_t vbias01x0123 = vld2q_dup_u64(w); w = (const int32_t*) w + 4;
57 const uint64x2x2_t vbias01x4567 = vld2q_dup_u64(w); w = (const int32_t*) w + 4;
58 int32x4_t vacc01x01 = vreinterpretq_s32_u64(vbias01x0123.val[0]);
59 int32x4_t vacc01x23 = vreinterpretq_s32_u64(vbias01x0123.val[1]);
60 int32x4_t vacc01x45 = vreinterpretq_s32_u64(vbias01x4567.val[0]);
61 int32x4_t vacc01x67 = vreinterpretq_s32_u64(vbias01x4567.val[1]);
62 int32x4_t vacc23x01 = vreinterpretq_s32_u64(vbias01x0123.val[0]);
63 int32x4_t vacc23x23 = vreinterpretq_s32_u64(vbias01x0123.val[1]);
64 int32x4_t vacc23x45 = vreinterpretq_s32_u64(vbias01x4567.val[0]);
65 int32x4_t vacc23x67 = vreinterpretq_s32_u64(vbias01x4567.val[1]);
66
67 size_t p = ks;
68 do {
69 const int8_t* restrict a0 = a[0];
70 if XNN_UNPREDICTABLE(a0 != zero) {
71 a0 = (const int8_t*) ((uintptr_t) a0 + a_offset);
72 }
73 const int8_t* restrict a1 = a[1];
74 if XNN_UNPREDICTABLE(a1 != zero) {
75 a1 = (const int8_t*) ((uintptr_t) a1 + a_offset);
76 }
77 const int8_t* restrict a2 = a[2];
78 if XNN_UNPREDICTABLE(a2 != zero) {
79 a2 = (const int8_t*) ((uintptr_t) a2 + a_offset);
80 }
81 a += 3;
82
83 uint64x2x2_t va01x0123456789ABCDEF;
84 va01x0123456789ABCDEF.val[0] = vdupq_n_u64(0);
85 va01x0123456789ABCDEF.val[1] = vdupq_n_u64(0);
86 uint64x2x2_t va23x0123456789ABCDEF;
87 va23x0123456789ABCDEF.val[0] = vdupq_n_u64(0);
88 va23x0123456789ABCDEF.val[1] = vdupq_n_u64(0);
89
90 // Inner accumulation loop along the 8 columns.
91 size_t k = kc;
92 // 2x partial unrolled loop to load 8 bytes at a time.
93 while (k >= 16 * sizeof(int8_t)) {
94 // Load a 3x16 block of activations.
95 va01x0123456789ABCDEF = vld2q_lane_u64((const void*) a0, va01x0123456789ABCDEF, 0); a0 += 16;
96 va23x0123456789ABCDEF = vld2q_lane_u64((const void*) a2, va23x0123456789ABCDEF, 0); a2 += 16;
97 va01x0123456789ABCDEF = vld2q_lane_u64((const void*) a1, va01x0123456789ABCDEF, 1); a1 += 16;
98
99 // Load a 16x8 block of weights.
100 const int8x16_t vb01x01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
101 const int8x16_t vb23x01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
102 const int8x16_t vb45x01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
103 const int8x16_t vb67x01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
104 const int8x16_t vb01x89ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
105 const int8x16_t vb23x89ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
106 const int8x16_t vb45x89ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
107 const int8x16_t vb67x89ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
108
109 // Multiply-accumulate: 3x16 * 16x8 --> 3x8.
110 vacc01x01 = vmmlaq_s32(vacc01x01, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[0]), vb01x01234567);
111 vacc01x23 = vmmlaq_s32(vacc01x23, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[0]), vb23x01234567);
112 vacc01x45 = vmmlaq_s32(vacc01x45, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[0]), vb45x01234567);
113 vacc01x67 = vmmlaq_s32(vacc01x67, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[0]), vb67x01234567);
114 vacc23x01 = vmmlaq_s32(vacc23x01, vreinterpretq_s8_u64(va23x0123456789ABCDEF.val[0]), vb01x01234567);
115 vacc23x23 = vmmlaq_s32(vacc23x23, vreinterpretq_s8_u64(va23x0123456789ABCDEF.val[0]), vb23x01234567);
116 vacc23x45 = vmmlaq_s32(vacc23x45, vreinterpretq_s8_u64(va23x0123456789ABCDEF.val[0]), vb45x01234567);
117 vacc23x67 = vmmlaq_s32(vacc23x67, vreinterpretq_s8_u64(va23x0123456789ABCDEF.val[0]), vb67x01234567);
118 vacc01x01 = vmmlaq_s32(vacc01x01, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[1]), vb01x89ABCDEF);
119 vacc01x23 = vmmlaq_s32(vacc01x23, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[1]), vb23x89ABCDEF);
120 vacc01x45 = vmmlaq_s32(vacc01x45, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[1]), vb45x89ABCDEF);
121 vacc01x67 = vmmlaq_s32(vacc01x67, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[1]), vb67x89ABCDEF);
122 vacc23x01 = vmmlaq_s32(vacc23x01, vreinterpretq_s8_u64(va23x0123456789ABCDEF.val[1]), vb01x89ABCDEF);
123 vacc23x23 = vmmlaq_s32(vacc23x23, vreinterpretq_s8_u64(va23x0123456789ABCDEF.val[1]), vb23x89ABCDEF);
124 vacc23x45 = vmmlaq_s32(vacc23x45, vreinterpretq_s8_u64(va23x0123456789ABCDEF.val[1]), vb45x89ABCDEF);
125 vacc23x67 = vmmlaq_s32(vacc23x67, vreinterpretq_s8_u64(va23x0123456789ABCDEF.val[1]), vb67x89ABCDEF);
126
127 k -= 16 * sizeof(int8_t);
128 }
129 // Handle up to 8 final positions of `k`
130 if XNN_UNLIKELY(k != 0) {
131 // Load a 3x8 block of activations.
132 uint64x2_t va01x01234567 = vld1q_dup_u64((const void*) a0); a0 += 8;
133 uint64x2_t va23x01234567 = vld1q_dup_u64((const void*) a2); a2 += 8;
134 va01x01234567 = vld1q_lane_u64((const void*) a1, va01x01234567, 1); a1 += 8;
135
136 // Load a 16x8 block of weights.
137 const int8x16_t vb01x01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
138 const int8x16_t vb23x01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
139 const int8x16_t vb45x01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
140 const int8x16_t vb67x01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
141
142 // Multiply-accumulate: 3x4 * 4x8 --> 3x8.
143 vacc01x01 = vmmlaq_s32(vacc01x01, vreinterpretq_s8_u64(va01x01234567), vb01x01234567);
144 vacc01x23 = vmmlaq_s32(vacc01x23, vreinterpretq_s8_u64(va01x01234567), vb23x01234567);
145 vacc01x45 = vmmlaq_s32(vacc01x45, vreinterpretq_s8_u64(va01x01234567), vb45x01234567);
146 vacc01x67 = vmmlaq_s32(vacc01x67, vreinterpretq_s8_u64(va01x01234567), vb67x01234567);
147 vacc23x01 = vmmlaq_s32(vacc23x01, vreinterpretq_s8_u64(va23x01234567), vb01x01234567);
148 vacc23x23 = vmmlaq_s32(vacc23x23, vreinterpretq_s8_u64(va23x01234567), vb23x01234567);
149 vacc23x45 = vmmlaq_s32(vacc23x45, vreinterpretq_s8_u64(va23x01234567), vb45x01234567);
150 vacc23x67 = vmmlaq_s32(vacc23x67, vreinterpretq_s8_u64(va23x01234567), vb67x01234567);
151 }
152
153 p -= 3 * sizeof(void*);
154 } while (p != 0);
155
156 int32x4_t vacc0x0123 = vcombine_s32(vget_low_s32(vacc01x01), vget_low_s32(vacc01x23));
157 int32x4_t vacc1x0123 = vcombine_s32(vget_high_s32(vacc01x01), vget_high_s32(vacc01x23));
158 int32x4_t vacc0x4567 = vcombine_s32(vget_low_s32(vacc01x45), vget_low_s32(vacc01x67));
159 int32x4_t vacc1x4567 = vcombine_s32(vget_high_s32(vacc01x45), vget_high_s32(vacc01x67));
160 int32x4_t vacc2x0123 = vcombine_s32(vget_low_s32(vacc23x01), vget_low_s32(vacc23x23));
161 int32x4_t vacc2x4567 = vcombine_s32(vget_low_s32(vacc23x45), vget_low_s32(vacc23x67));
162
163 float32x4_t vfpacc0x0123 = vcvtq_f32_s32(vacc0x0123);
164 float32x4_t vfpacc0x4567 = vcvtq_f32_s32(vacc0x4567);
165 float32x4_t vfpacc1x0123 = vcvtq_f32_s32(vacc1x0123);
166 float32x4_t vfpacc1x4567 = vcvtq_f32_s32(vacc1x4567);
167 float32x4_t vfpacc2x0123 = vcvtq_f32_s32(vacc2x0123);
168 float32x4_t vfpacc2x4567 = vcvtq_f32_s32(vacc2x4567);
169
170 const float32x4_t vscale0123 = vld1q_f32((const float*) w); w = (const float*) w + 4;
171 vfpacc0x0123 = vmulq_f32(vfpacc0x0123, vscale0123);
172 vfpacc1x0123 = vmulq_f32(vfpacc1x0123, vscale0123);
173 vfpacc2x0123 = vmulq_f32(vfpacc2x0123, vscale0123);
174 const float32x4_t vscale4567 = vld1q_f32((const float*) w); w = (const float*) w + 4;
175 vfpacc0x4567 = vmulq_f32(vfpacc0x4567, vscale4567);
176 vfpacc1x4567 = vmulq_f32(vfpacc1x4567, vscale4567);
177 vfpacc2x4567 = vmulq_f32(vfpacc2x4567, vscale4567);
178
179 vacc0x0123 = vcvtnq_s32_f32(vfpacc0x0123);
180 vacc0x4567 = vcvtnq_s32_f32(vfpacc0x4567);
181 vacc1x0123 = vcvtnq_s32_f32(vfpacc1x0123);
182 vacc1x4567 = vcvtnq_s32_f32(vfpacc1x4567);
183 vacc2x0123 = vcvtnq_s32_f32(vfpacc2x0123);
184 vacc2x4567 = vcvtnq_s32_f32(vfpacc2x4567);
185
186 const int16x8_t voutput_zero_point = vld1q_dup_s16(&params->fp32_neonv8.output_zero_point);
187 #if XNN_ARCH_ARM64
188 const int16x8_t vacc0x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc0x0123), vacc0x4567), voutput_zero_point);
189 const int16x8_t vacc1x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc1x0123), vacc1x4567), voutput_zero_point);
190 const int16x8_t vacc2x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc2x0123), vacc2x4567), voutput_zero_point);
191
192 int8x16_t vout0x01234567_1x01234567 = vqmovn_high_s16(vqmovn_s16(vacc0x01234567), vacc1x01234567);
193 int8x8_t vout2x01234567 = vqmovn_s16(vacc2x01234567);
194 #else
195 const int16x8_t vacc0x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc0x0123), vqmovn_s32(vacc0x4567)), voutput_zero_point);
196 const int16x8_t vacc1x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc1x0123), vqmovn_s32(vacc1x4567)), voutput_zero_point);
197 const int16x8_t vacc2x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc2x0123), vqmovn_s32(vacc2x4567)), voutput_zero_point);
198
199 int8x16_t vout0x01234567_1x01234567 = vcombine_s8(vqmovn_s16(vacc0x01234567), vqmovn_s16(vacc1x01234567));
200 int8x8_t vout2x01234567 = vqmovn_s16(vacc2x01234567);
201 #endif
202 const int8x16_t voutput_min = vld1q_dup_s8(&params->fp32_neonv8.output_min);
203 const int8x16_t voutput_max = vld1q_dup_s8(&params->fp32_neonv8.output_max);
204
205 vout2x01234567 = vmax_s8(vout2x01234567, vget_low_s8(voutput_min));
206 vout0x01234567_1x01234567 = vmaxq_s8(vout0x01234567_1x01234567, voutput_min);
207
208 vout2x01234567 = vmin_s8(vout2x01234567, vget_low_s8(voutput_max));
209 vout0x01234567_1x01234567 = vminq_s8(vout0x01234567_1x01234567, voutput_max);
210
211 if (nc >= 8) {
212 vst1_s8(c2, vout2x01234567);
213 vst1_s8(c1, vget_high_s8(vout0x01234567_1x01234567));
214 vst1_s8(c0, vget_low_s8(vout0x01234567_1x01234567));
215
216 c2 = (int8_t*) ((uintptr_t) c2 + cn_stride);
217 c1 = (int8_t*) ((uintptr_t) c1 + cn_stride);
218 c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
219
220 a = (const int8_t**restrict) ((uintptr_t) a - ks);
221
222 nc -= 8;
223 } else {
224 if (nc & 4) {
225 vst1_lane_u32((void*) c2, vreinterpret_u32_s8(vout2x01234567), 0); c2 += 4;
226 vst1q_lane_u32((void*) c1, vreinterpretq_u32_s8(vout0x01234567_1x01234567), 2); c1 += 4;
227 vst1q_lane_u32((void*) c0, vreinterpretq_u32_s8(vout0x01234567_1x01234567), 0); c0 += 4;
228 vout2x01234567 = vext_s8(vout2x01234567, vout2x01234567, 4);
229 vout0x01234567_1x01234567 = vextq_s8(vout0x01234567_1x01234567, vout0x01234567_1x01234567, 4);
230 }
231 if (nc & 2) {
232 vst1_lane_u16((void*) c2, vreinterpret_u16_s8(vout2x01234567), 0); c2 += 2;
233 vst1q_lane_u16((void*) c1, vreinterpretq_u16_s8(vout0x01234567_1x01234567), 4); c1 += 2;
234 vst1q_lane_u16((void*) c0, vreinterpretq_u16_s8(vout0x01234567_1x01234567), 0); c0 += 2;
235 vout2x01234567 = vext_s8(vout2x01234567, vout2x01234567, 2);
236 vout0x01234567_1x01234567 = vextq_s8(vout0x01234567_1x01234567, vout0x01234567_1x01234567, 2);
237 }
238 if (nc & 1) {
239 vst1_lane_s8(c2, vout2x01234567, 0);
240 vst1q_lane_s8(c1, vout0x01234567_1x01234567, 8);
241 vst1q_lane_s8(c0, vout0x01234567_1x01234567, 0);
242 }
243
244 nc = 0;
245 }
246 } while (nc != 0);
247 }