Debian Codegraph @

src/qs8-qc8w-igemm/gen/qs8-qc8w-igemm-4x4-minmax-fp32-scalar-imagic.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/qs8-igemm/scalar.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 "xnnpack/math.h"
13 #include "xnnpack/gemm.h"
14
15
16 void xnn_qs8_qc8w_igemm_minmax_fp32_ukernel_4x4__scalar_imagic(
17 size_t mr,
18 size_t nc,
19 size_t kc,
20 size_t ks,
21 const int8_t** restrict a,
22 const void* restrict w,
23 int8_t* restrict c,
24 size_t cm_stride,
25 size_t cn_stride,
26 size_t a_offset,
27 const int8_t* zero,
28 const union xnn_qs8_qc8w_conv_minmax_params params[restrict XNN_MIN_ELEMENTS(1)])
29 {
30 assert(mr != 0);
31 assert(mr <= 4);
32 assert(nc != 0);
33 assert(kc != 0);
34 assert(ks != 0);
35 assert(ks % (4 * sizeof(void*)) == 0);
36 assert(a != NULL);
37 assert(w != NULL);
38 assert(c != NULL);
39
40 int8_t* c0 = c;
41 int8_t* c1 = (int8_t*) ((uintptr_t) c0 + cm_stride);
42 if XNN_UNPREDICTABLE(mr < 2) {
43 c1 = c0;
44 }
45 int8_t* c2 = (int8_t*) ((uintptr_t) c1 + cm_stride);
46 if XNN_UNPREDICTABLE(mr <= 2) {
47 c2 = c1;
48 }
49 int8_t* c3 = (int8_t*) ((uintptr_t) c2 + cm_stride);
50 if XNN_UNPREDICTABLE(mr != 4) {
51 c3 = c2;
52 }
53
54 const int32_t output_min_less_zero_point = (int32_t) params->fp32_scalar.output_min - (int32_t) params->fp32_scalar.output_zero_point;
55 const int32_t output_max_less_zero_point = (int32_t) params->fp32_scalar.output_max - (int32_t) params->fp32_scalar.output_zero_point;
56 const float vmagic_bias = 12582912.0f;
57 const int32_t vmagic_min = (int32_t) float_as_uint32(12582912.0f + output_min_less_zero_point);
58 const int32_t vmagic_max = (int32_t) float_as_uint32(12582912.0f + output_max_less_zero_point);
59 const int32_t vmagic_bias_less_zero_point = INT32_C(0x4B400000) - (int32_t) params->fp32_scalar.output_zero_point;
60
61 do {
62 int32_t vacc0x0 = ((const int32_t*) w)[0];
63 int32_t vacc0x1 = ((const int32_t*) w)[1];
64 int32_t vacc0x2 = ((const int32_t*) w)[2];
65 int32_t vacc0x3 = ((const int32_t*) w)[3];
66 int32_t vacc1x0 = vacc0x0;
67 int32_t vacc1x1 = vacc0x1;
68 int32_t vacc1x2 = vacc0x2;
69 int32_t vacc1x3 = vacc0x3;
70 int32_t vacc2x0 = vacc0x0;
71 int32_t vacc2x1 = vacc0x1;
72 int32_t vacc2x2 = vacc0x2;
73 int32_t vacc2x3 = vacc0x3;
74 int32_t vacc3x0 = vacc0x0;
75 int32_t vacc3x1 = vacc0x1;
76 int32_t vacc3x2 = vacc0x2;
77 int32_t vacc3x3 = vacc0x3;
78 w = (const void*) ((const int32_t*) w + 4);
79
80 size_t p = ks;
81 do {
82 const int8_t* restrict a0 = a[0];
83 assert(a0 != NULL);
84 if XNN_UNPREDICTABLE(a0 != zero) {
85 a0 = (const int8_t*) ((uintptr_t) a0 + a_offset);
86 }
87 const int8_t* restrict a1 = a[1];
88 assert(a1 != NULL);
89 if XNN_UNPREDICTABLE(a1 != zero) {
90 a1 = (const int8_t*) ((uintptr_t) a1 + a_offset);
91 }
92 const int8_t* restrict a2 = a[2];
93 assert(a2 != NULL);
94 if XNN_UNPREDICTABLE(a2 != zero) {
95 a2 = (const int8_t*) ((uintptr_t) a2 + a_offset);
96 }
97 const int8_t* restrict a3 = a[3];
98 assert(a3 != NULL);
99 if XNN_UNPREDICTABLE(a3 != zero) {
100 a3 = (const int8_t*) ((uintptr_t) a3 + a_offset);
101 }
102 a += 4;
103
104 size_t k = kc;
105 do {
106 const int32_t va0 = (int32_t) *a0++;
107 const int32_t va1 = (int32_t) *a1++;
108 const int32_t va2 = (int32_t) *a2++;
109 const int32_t va3 = (int32_t) *a3++;
110
111 const int32_t vb0 = (int32_t) ((const int8_t*) w)[0];
112 const int32_t vb1 = (int32_t) ((const int8_t*) w)[1];
113 const int32_t vb2 = (int32_t) ((const int8_t*) w)[2];
114 const int32_t vb3 = (int32_t) ((const int8_t*) w)[3];
115 w = (const void*) ((const int8_t*) w + 4);
116
117 vacc0x0 += va0 * vb0;
118 vacc0x1 += va0 * vb1;
119 vacc0x2 += va0 * vb2;
120 vacc0x3 += va0 * vb3;
121 vacc1x0 += va1 * vb0;
122 vacc1x1 += va1 * vb1;
123 vacc1x2 += va1 * vb2;
124 vacc1x3 += va1 * vb3;
125 vacc2x0 += va2 * vb0;
126 vacc2x1 += va2 * vb1;
127 vacc2x2 += va2 * vb2;
128 vacc2x3 += va2 * vb3;
129 vacc3x0 += va3 * vb0;
130 vacc3x1 += va3 * vb1;
131 vacc3x2 += va3 * vb2;
132 vacc3x3 += va3 * vb3;
133
134 k -= sizeof(int8_t);
135 } while (k != 0);
136 p -= 4 * sizeof(void*);
137 } while (p != 0);
138
139 float vfpacc0x0 = (float) vacc0x0;
140 float vfpacc0x1 = (float) vacc0x1;
141 float vfpacc0x2 = (float) vacc0x2;
142 float vfpacc0x3 = (float) vacc0x3;
143 float vfpacc1x0 = (float) vacc1x0;
144 float vfpacc1x1 = (float) vacc1x1;
145 float vfpacc1x2 = (float) vacc1x2;
146 float vfpacc1x3 = (float) vacc1x3;
147 float vfpacc2x0 = (float) vacc2x0;
148 float vfpacc2x1 = (float) vacc2x1;
149 float vfpacc2x2 = (float) vacc2x2;
150 float vfpacc2x3 = (float) vacc2x3;
151 float vfpacc3x0 = (float) vacc3x0;
152 float vfpacc3x1 = (float) vacc3x1;
153 float vfpacc3x2 = (float) vacc3x2;
154 float vfpacc3x3 = (float) vacc3x3;
155
156 const float vscale0 = ((const float*) w)[0];
157 vfpacc0x0 *= vscale0;
158 vfpacc1x0 *= vscale0;
159 vfpacc2x0 *= vscale0;
160 vfpacc3x0 *= vscale0;
161 const float vscale1 = ((const float*) w)[1];
162 vfpacc0x1 *= vscale1;
163 vfpacc1x1 *= vscale1;
164 vfpacc2x1 *= vscale1;
165 vfpacc3x1 *= vscale1;
166 const float vscale2 = ((const float*) w)[2];
167 vfpacc0x2 *= vscale2;
168 vfpacc1x2 *= vscale2;
169 vfpacc2x2 *= vscale2;
170 vfpacc3x2 *= vscale2;
171 const float vscale3 = ((const float*) w)[3];
172 vfpacc0x3 *= vscale3;
173 vfpacc1x3 *= vscale3;
174 vfpacc2x3 *= vscale3;
175 vfpacc3x3 *= vscale3;
176 w = (const void*) ((const float*) w + 4);
177
178 vfpacc0x0 += vmagic_bias;
179 vfpacc0x1 += vmagic_bias;
180 vfpacc0x2 += vmagic_bias;
181 vfpacc0x3 += vmagic_bias;
182 vfpacc1x0 += vmagic_bias;
183 vfpacc1x1 += vmagic_bias;
184 vfpacc1x2 += vmagic_bias;
185 vfpacc1x3 += vmagic_bias;
186 vfpacc2x0 += vmagic_bias;
187 vfpacc2x1 += vmagic_bias;
188 vfpacc2x2 += vmagic_bias;
189 vfpacc2x3 += vmagic_bias;
190 vfpacc3x0 += vmagic_bias;
191 vfpacc3x1 += vmagic_bias;
192 vfpacc3x2 += vmagic_bias;
193 vfpacc3x3 += vmagic_bias;
194
195 int32_t vout0x0 = (int32_t) float_as_uint32(vfpacc0x0);
196 int32_t vout0x1 = (int32_t) float_as_uint32(vfpacc0x1);
197 int32_t vout0x2 = (int32_t) float_as_uint32(vfpacc0x2);
198 int32_t vout0x3 = (int32_t) float_as_uint32(vfpacc0x3);
199 int32_t vout1x0 = (int32_t) float_as_uint32(vfpacc1x0);
200 int32_t vout1x1 = (int32_t) float_as_uint32(vfpacc1x1);
201 int32_t vout1x2 = (int32_t) float_as_uint32(vfpacc1x2);
202 int32_t vout1x3 = (int32_t) float_as_uint32(vfpacc1x3);
203 int32_t vout2x0 = (int32_t) float_as_uint32(vfpacc2x0);
204 int32_t vout2x1 = (int32_t) float_as_uint32(vfpacc2x1);
205 int32_t vout2x2 = (int32_t) float_as_uint32(vfpacc2x2);
206 int32_t vout2x3 = (int32_t) float_as_uint32(vfpacc2x3);
207 int32_t vout3x0 = (int32_t) float_as_uint32(vfpacc3x0);
208 int32_t vout3x1 = (int32_t) float_as_uint32(vfpacc3x1);
209 int32_t vout3x2 = (int32_t) float_as_uint32(vfpacc3x2);
210 int32_t vout3x3 = (int32_t) float_as_uint32(vfpacc3x3);
211
212 vout0x0 = math_max_s32(vout0x0, vmagic_min);
213 vout0x1 = math_max_s32(vout0x1, vmagic_min);
214 vout0x2 = math_max_s32(vout0x2, vmagic_min);
215 vout0x3 = math_max_s32(vout0x3, vmagic_min);
216 vout1x0 = math_max_s32(vout1x0, vmagic_min);
217 vout1x1 = math_max_s32(vout1x1, vmagic_min);
218 vout1x2 = math_max_s32(vout1x2, vmagic_min);
219 vout1x3 = math_max_s32(vout1x3, vmagic_min);
220 vout2x0 = math_max_s32(vout2x0, vmagic_min);
221 vout2x1 = math_max_s32(vout2x1, vmagic_min);
222 vout2x2 = math_max_s32(vout2x2, vmagic_min);
223 vout2x3 = math_max_s32(vout2x3, vmagic_min);
224 vout3x0 = math_max_s32(vout3x0, vmagic_min);
225 vout3x1 = math_max_s32(vout3x1, vmagic_min);
226 vout3x2 = math_max_s32(vout3x2, vmagic_min);
227 vout3x3 = math_max_s32(vout3x3, vmagic_min);
228
229 vout0x0 = math_min_s32(vout0x0, vmagic_max);
230 vout0x1 = math_min_s32(vout0x1, vmagic_max);
231 vout0x2 = math_min_s32(vout0x2, vmagic_max);
232 vout0x3 = math_min_s32(vout0x3, vmagic_max);
233 vout1x0 = math_min_s32(vout1x0, vmagic_max);
234 vout1x1 = math_min_s32(vout1x1, vmagic_max);
235 vout1x2 = math_min_s32(vout1x2, vmagic_max);
236 vout1x3 = math_min_s32(vout1x3, vmagic_max);
237 vout2x0 = math_min_s32(vout2x0, vmagic_max);
238 vout2x1 = math_min_s32(vout2x1, vmagic_max);
239 vout2x2 = math_min_s32(vout2x2, vmagic_max);
240 vout2x3 = math_min_s32(vout2x3, vmagic_max);
241 vout3x0 = math_min_s32(vout3x0, vmagic_max);
242 vout3x1 = math_min_s32(vout3x1, vmagic_max);
243 vout3x2 = math_min_s32(vout3x2, vmagic_max);
244 vout3x3 = math_min_s32(vout3x3, vmagic_max);
245
246 vout0x0 -= vmagic_bias_less_zero_point;
247 vout0x1 -= vmagic_bias_less_zero_point;
248 vout0x2 -= vmagic_bias_less_zero_point;
249 vout0x3 -= vmagic_bias_less_zero_point;
250 vout1x0 -= vmagic_bias_less_zero_point;
251 vout1x1 -= vmagic_bias_less_zero_point;
252 vout1x2 -= vmagic_bias_less_zero_point;
253 vout1x3 -= vmagic_bias_less_zero_point;
254 vout2x0 -= vmagic_bias_less_zero_point;
255 vout2x1 -= vmagic_bias_less_zero_point;
256 vout2x2 -= vmagic_bias_less_zero_point;
257 vout2x3 -= vmagic_bias_less_zero_point;
258 vout3x0 -= vmagic_bias_less_zero_point;
259 vout3x1 -= vmagic_bias_less_zero_point;
260 vout3x2 -= vmagic_bias_less_zero_point;
261 vout3x3 -= vmagic_bias_less_zero_point;
262
263 if XNN_LIKELY(nc >= 4) {
264 c3[0] = (int8_t) vout3x0;
265 c3[1] = (int8_t) vout3x1;
266 c3[2] = (int8_t) vout3x2;
267 c3[3] = (int8_t) vout3x3;
268 c2[0] = (int8_t) vout2x0;
269 c2[1] = (int8_t) vout2x1;
270 c2[2] = (int8_t) vout2x2;
271 c2[3] = (int8_t) vout2x3;
272 c1[0] = (int8_t) vout1x0;
273 c1[1] = (int8_t) vout1x1;
274 c1[2] = (int8_t) vout1x2;
275 c1[3] = (int8_t) vout1x3;
276 c0[0] = (int8_t) vout0x0;
277 c0[1] = (int8_t) vout0x1;
278 c0[2] = (int8_t) vout0x2;
279 c0[3] = (int8_t) vout0x3;
280
281 c3 = (int8_t*) ((uintptr_t) c3 + cn_stride);
282 c2 = (int8_t*) ((uintptr_t) c2 + cn_stride);
283 c1 = (int8_t*) ((uintptr_t) c1 + cn_stride);
284 c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
285
286 a = (const int8_t**restrict) ((uintptr_t) a - ks);
287 nc -= 4;
288 } else {
289 if (nc & 2) {
290 c3[0] = (int8_t) vout3x0;
291 c3[1] = (int8_t) vout3x1;
292 vout3x0 = vout3x2;
293 c3 += 2;
294 c2[0] = (int8_t) vout2x0;
295 c2[1] = (int8_t) vout2x1;
296 vout2x0 = vout2x2;
297 c2 += 2;
298 c1[0] = (int8_t) vout1x0;
299 c1[1] = (int8_t) vout1x1;
300 vout1x0 = vout1x2;
301 c1 += 2;
302 c0[0] = (int8_t) vout0x0;
303 c0[1] = (int8_t) vout0x1;
304 vout0x0 = vout0x2;
305 c0 += 2;
306 }
307 if (nc & 1) {
308 c3[0] = (int8_t) vout3x0;
309 c2[0] = (int8_t) vout2x0;
310 c1[0] = (int8_t) vout1x0;
311 c0[0] = (int8_t) vout0x0;
312 }
313
314 nc = 0;
315 }
316 } while (nc != 0);
317 }