Debian Codegraph @

src/x8-packw/gen/x8-packw-x16-gemm-goi-scalar-u4.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/x8-packw/scalar.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
11 #include <assert.h>
12 #include <stddef.h>
13 #include <stdint.h>
14
15 #include "xnnpack/math.h"
16 #include "xnnpack/packw.h"
17 #include "xnnpack/unaligned.h"
18
19 void xnn_x8_packw_gemm_goi_ukernel_x16__scalar_u4(
20 size_t g,
21 size_t nc,
22 size_t kc,
23 size_t nr,
24 size_t kr,
25 size_t sr,
26 const int8_t* weights,
27 const uint32_t* bias,
28 const void* scale,
29 int8_t* packed_weights,
30 size_t extra_bytes,
31 const void* params)
32 {
33 assert(g != 0);
34 assert(nc != 0);
35 assert(kc != 0);
36 assert(nr == 16); // This kernel is for NR=16
37 assert(kr == 1);
38 assert(sr == 1);
39 assert(weights != NULL);
40 assert(packed_weights != NULL);
41
42 int8_t* out = (int8_t*) packed_weights;
43 const uint32_t* b = (const uint32_t*) bias;
44
45 do {
46 // NC main loop multiple of 16
47 const int8_t* w0 = (const int8_t*) weights;
48 size_t n = nc;
49 for (;n >= 16; n -= 16) {
50 if XNN_LIKELY(b != NULL) {
51 unaligned_store_s32(out + 0 * sizeof(int32_t), b[0]);
52 unaligned_store_s32(out + 1 * sizeof(int32_t), b[1]);
53 unaligned_store_s32(out + 2 * sizeof(int32_t), b[2]);
54 unaligned_store_s32(out + 3 * sizeof(int32_t), b[3]);
55 unaligned_store_s32(out + 4 * sizeof(int32_t), b[4]);
56 unaligned_store_s32(out + 5 * sizeof(int32_t), b[5]);
57 unaligned_store_s32(out + 6 * sizeof(int32_t), b[6]);
58 unaligned_store_s32(out + 7 * sizeof(int32_t), b[7]);
59 unaligned_store_s32(out + 8 * sizeof(int32_t), b[8]);
60 unaligned_store_s32(out + 9 * sizeof(int32_t), b[9]);
61 unaligned_store_s32(out + 10 * sizeof(int32_t), b[10]);
62 unaligned_store_s32(out + 11 * sizeof(int32_t), b[11]);
63 unaligned_store_s32(out + 12 * sizeof(int32_t), b[12]);
64 unaligned_store_s32(out + 13 * sizeof(int32_t), b[13]);
65 unaligned_store_s32(out + 14 * sizeof(int32_t), b[14]);
66 unaligned_store_s32(out + 15 * sizeof(int32_t), b[15]);
67 b += 16;
68 } else {
69 unaligned_store_s32(out + 0 * sizeof(int32_t), 0);
70 unaligned_store_s32(out + 1 * sizeof(int32_t), 0);
71 unaligned_store_s32(out + 2 * sizeof(int32_t), 0);
72 unaligned_store_s32(out + 3 * sizeof(int32_t), 0);
73 unaligned_store_s32(out + 4 * sizeof(int32_t), 0);
74 unaligned_store_s32(out + 5 * sizeof(int32_t), 0);
75 unaligned_store_s32(out + 6 * sizeof(int32_t), 0);
76 unaligned_store_s32(out + 7 * sizeof(int32_t), 0);
77 unaligned_store_s32(out + 8 * sizeof(int32_t), 0);
78 unaligned_store_s32(out + 9 * sizeof(int32_t), 0);
79 unaligned_store_s32(out + 10 * sizeof(int32_t), 0);
80 unaligned_store_s32(out + 11 * sizeof(int32_t), 0);
81 unaligned_store_s32(out + 12 * sizeof(int32_t), 0);
82 unaligned_store_s32(out + 13 * sizeof(int32_t), 0);
83 unaligned_store_s32(out + 14 * sizeof(int32_t), 0);
84 unaligned_store_s32(out + 15 * sizeof(int32_t), 0);
85 }
86 out += 16 * sizeof(uint32_t);
87
88 const int8_t* w1 = w0 + kc;
89 const int8_t* w2 = w1 + kc;
90 const int8_t* w3 = w2 + kc;
91 const int8_t* w4 = w3 + kc;
92 const int8_t* w5 = w4 + kc;
93 const int8_t* w6 = w5 + kc;
94 const int8_t* w7 = w6 + kc;
95 const int8_t* w8 = w7 + kc;
96 const int8_t* w9 = w8 + kc;
97 const int8_t* w10 = w9 + kc;
98 const int8_t* w11 = w10 + kc;
99 const int8_t* w12 = w11 + kc;
100 const int8_t* w13 = w12 + kc;
101 const int8_t* w14 = w13 + kc;
102 const int8_t* w15 = w14 + kc;
103
104 // KC main loop multiple of 16x4
105 size_t k = kc;
106 for (; k >= 4; k -= 4) {
107 const int8_t v00 = w0[0];
108 const int8_t v01 = w0[1];
109 const int8_t v02 = w0[2];
110 const int8_t v03 = w0[3];
111 w0 += 4;
112 const int8_t v10 = w1[0];
113 const int8_t v11 = w1[1];
114 const int8_t v12 = w1[2];
115 const int8_t v13 = w1[3];
116 w1 += 4;
117 const int8_t v20 = w2[0];
118 const int8_t v21 = w2[1];
119 const int8_t v22 = w2[2];
120 const int8_t v23 = w2[3];
121 w2 += 4;
122 const int8_t v30 = w3[0];
123 const int8_t v31 = w3[1];
124 const int8_t v32 = w3[2];
125 const int8_t v33 = w3[3];
126 w3 += 4;
127 const int8_t v40 = w4[0];
128 const int8_t v41 = w4[1];
129 const int8_t v42 = w4[2];
130 const int8_t v43 = w4[3];
131 w4 += 4;
132 const int8_t v50 = w5[0];
133 const int8_t v51 = w5[1];
134 const int8_t v52 = w5[2];
135 const int8_t v53 = w5[3];
136 w5 += 4;
137 const int8_t v60 = w6[0];
138 const int8_t v61 = w6[1];
139 const int8_t v62 = w6[2];
140 const int8_t v63 = w6[3];
141 w6 += 4;
142 const int8_t v70 = w7[0];
143 const int8_t v71 = w7[1];
144 const int8_t v72 = w7[2];
145 const int8_t v73 = w7[3];
146 w7 += 4;
147 const int8_t v80 = w8[0];
148 const int8_t v81 = w8[1];
149 const int8_t v82 = w8[2];
150 const int8_t v83 = w8[3];
151 w8 += 4;
152 const int8_t v90 = w9[0];
153 const int8_t v91 = w9[1];
154 const int8_t v92 = w9[2];
155 const int8_t v93 = w9[3];
156 w9 += 4;
157 const int8_t v100 = w10[0];
158 const int8_t v101 = w10[1];
159 const int8_t v102 = w10[2];
160 const int8_t v103 = w10[3];
161 w10 += 4;
162 const int8_t v110 = w11[0];
163 const int8_t v111 = w11[1];
164 const int8_t v112 = w11[2];
165 const int8_t v113 = w11[3];
166 w11 += 4;
167 const int8_t v120 = w12[0];
168 const int8_t v121 = w12[1];
169 const int8_t v122 = w12[2];
170 const int8_t v123 = w12[3];
171 w12 += 4;
172 const int8_t v130 = w13[0];
173 const int8_t v131 = w13[1];
174 const int8_t v132 = w13[2];
175 const int8_t v133 = w13[3];
176 w13 += 4;
177 const int8_t v140 = w14[0];
178 const int8_t v141 = w14[1];
179 const int8_t v142 = w14[2];
180 const int8_t v143 = w14[3];
181 w14 += 4;
182 const int8_t v150 = w15[0];
183 const int8_t v151 = w15[1];
184 const int8_t v152 = w15[2];
185 const int8_t v153 = w15[3];
186 w15 += 4;
187 out[0] = v00;
188 out[1] = v10;
189 out[2] = v20;
190 out[3] = v30;
191 out[4] = v40;
192 out[5] = v50;
193 out[6] = v60;
194 out[7] = v70;
195 out[8] = v80;
196 out[9] = v90;
197 out[10] = v100;
198 out[11] = v110;
199 out[12] = v120;
200 out[13] = v130;
201 out[14] = v140;
202 out[15] = v150;
203 out[16] = v01;
204 out[17] = v11;
205 out[18] = v21;
206 out[19] = v31;
207 out[20] = v41;
208 out[21] = v51;
209 out[22] = v61;
210 out[23] = v71;
211 out[24] = v81;
212 out[25] = v91;
213 out[26] = v101;
214 out[27] = v111;
215 out[28] = v121;
216 out[29] = v131;
217 out[30] = v141;
218 out[31] = v151;
219 out[32] = v02;
220 out[33] = v12;
221 out[34] = v22;
222 out[35] = v32;
223 out[36] = v42;
224 out[37] = v52;
225 out[38] = v62;
226 out[39] = v72;
227 out[40] = v82;
228 out[41] = v92;
229 out[42] = v102;
230 out[43] = v112;
231 out[44] = v122;
232 out[45] = v132;
233 out[46] = v142;
234 out[47] = v152;
235 out[48] = v03;
236 out[49] = v13;
237 out[50] = v23;
238 out[51] = v33;
239 out[52] = v43;
240 out[53] = v53;
241 out[54] = v63;
242 out[55] = v73;
243 out[56] = v83;
244 out[57] = v93;
245 out[58] = v103;
246 out[59] = v113;
247 out[60] = v123;
248 out[61] = v133;
249 out[62] = v143;
250 out[63] = v153;
251 out += 64;
252 }
253
254 // KC remainder
255 for (; k != 0; --k) {
256 const int8_t v0 = *w0++;
257 out[0] = v0;
258 const int8_t v1 = *w1++;
259 out[1] = v1;
260 const int8_t v2 = *w2++;
261 out[2] = v2;
262 const int8_t v3 = *w3++;
263 out[3] = v3;
264 const int8_t v4 = *w4++;
265 out[4] = v4;
266 const int8_t v5 = *w5++;
267 out[5] = v5;
268 const int8_t v6 = *w6++;
269 out[6] = v6;
270 const int8_t v7 = *w7++;
271 out[7] = v7;
272 const int8_t v8 = *w8++;
273 out[8] = v8;
274 const int8_t v9 = *w9++;
275 out[9] = v9;
276 const int8_t v10 = *w10++;
277 out[10] = v10;
278 const int8_t v11 = *w11++;
279 out[11] = v11;
280 const int8_t v12 = *w12++;
281 out[12] = v12;
282 const int8_t v13 = *w13++;
283 out[13] = v13;
284 const int8_t v14 = *w14++;
285 out[14] = v14;
286 const int8_t v15 = *w15++;
287 out[15] = v15;
288 out += 16;
289 }
290 out = (int8_t*) ((uintptr_t) out + extra_bytes);
291 w0 = w15;
292 }
293
294 // NC remainder (1..15)
295 if XNN_UNLIKELY(n != 0) {
296 if XNN_LIKELY(b != NULL) {
297 size_t nb = n;
298 do {
299 unaligned_store_s32(out, *b++);
300 out += sizeof(uint32_t);
301 } while (--nb != 0);
302 } else {
303 size_t nb = n;
304 do {
305 unaligned_store_s32(out, 0);
306 out += sizeof(uint32_t);
307 } while (--nb != 0);
308 }
309 out += (16 - n) * sizeof(uint32_t);
310
311 // NR remainder has less than 16 rows so last row is not loaded
312 const int8_t* w1 = w0 + kc;
313 if XNN_UNPREDICTABLE(n < 2) {
314 w1 = w0;
315 }
316 const int8_t* w2 = w1 + kc;
317 if XNN_UNPREDICTABLE(n <= 2) {
318 w2 = w1;
319 }
320 const int8_t* w3 = w2 + kc;
321 if XNN_UNPREDICTABLE(n < 4) {
322 w3 = w2;
323 }
324 const int8_t* w4 = w3 + kc;
325 if XNN_UNPREDICTABLE(n <= 4) {
326 w4 = w3;
327 }
328 const int8_t* w5 = w4 + kc;
329 if XNN_UNPREDICTABLE(n < 6) {
330 w5 = w4;
331 }
332 const int8_t* w6 = w5 + kc;
333 if XNN_UNPREDICTABLE(n <= 6) {
334 w6 = w5;
335 }
336 const int8_t* w7 = w6 + kc;
337 if XNN_UNPREDICTABLE(n < 8) {
338 w7 = w6;
339 }
340 const int8_t* w8 = w7 + kc;
341 if XNN_UNPREDICTABLE(n <= 8) {
342 w8 = w7;
343 }
344 const int8_t* w9 = w8 + kc;
345 if XNN_UNPREDICTABLE(n < 10) {
346 w9 = w8;
347 }
348 const int8_t* w10 = w9 + kc;
349 if XNN_UNPREDICTABLE(n <= 10) {
350 w10 = w9;
351 }
352 const int8_t* w11 = w10 + kc;
353 if XNN_UNPREDICTABLE(n < 12) {
354 w11 = w10;
355 }
356 const int8_t* w12 = w11 + kc;
357 if XNN_UNPREDICTABLE(n <= 12) {
358 w12 = w11;
359 }
360 const int8_t* w13 = w12 + kc;
361 if XNN_UNPREDICTABLE(n < 14) {
362 w13 = w12;
363 }
364 const int8_t* w14 = w13 + kc;
365 if XNN_UNPREDICTABLE(n <= 14) {
366 w14 = w13;
367 }
368
369 // KC main loop multiple of 16x4
370 size_t k = kc;
371 for (; k >= 4; k -= 4) {
372 const int8_t v00 = w0[0];
373 const int8_t v01 = w0[1];
374 const int8_t v02 = w0[2];
375 const int8_t v03 = w0[3];
376 w0 += 4;
377 const int8_t v10 = w1[0];
378 const int8_t v11 = w1[1];
379 const int8_t v12 = w1[2];
380 const int8_t v13 = w1[3];
381 w1 += 4;
382 const int8_t v20 = w2[0];
383 const int8_t v21 = w2[1];
384 const int8_t v22 = w2[2];
385 const int8_t v23 = w2[3];
386 w2 += 4;
387 const int8_t v30 = w3[0];
388 const int8_t v31 = w3[1];
389 const int8_t v32 = w3[2];
390 const int8_t v33 = w3[3];
391 w3 += 4;
392 const int8_t v40 = w4[0];
393 const int8_t v41 = w4[1];
394 const int8_t v42 = w4[2];
395 const int8_t v43 = w4[3];
396 w4 += 4;
397 const int8_t v50 = w5[0];
398 const int8_t v51 = w5[1];
399 const int8_t v52 = w5[2];
400 const int8_t v53 = w5[3];
401 w5 += 4;
402 const int8_t v60 = w6[0];
403 const int8_t v61 = w6[1];
404 const int8_t v62 = w6[2];
405 const int8_t v63 = w6[3];
406 w6 += 4;
407 const int8_t v70 = w7[0];
408 const int8_t v71 = w7[1];
409 const int8_t v72 = w7[2];
410 const int8_t v73 = w7[3];
411 w7 += 4;
412 const int8_t v80 = w8[0];
413 const int8_t v81 = w8[1];
414 const int8_t v82 = w8[2];
415 const int8_t v83 = w8[3];
416 w8 += 4;
417 const int8_t v90 = w9[0];
418 const int8_t v91 = w9[1];
419 const int8_t v92 = w9[2];
420 const int8_t v93 = w9[3];
421 w9 += 4;
422 const int8_t v100 = w10[0];
423 const int8_t v101 = w10[1];
424 const int8_t v102 = w10[2];
425 const int8_t v103 = w10[3];
426 w10 += 4;
427 const int8_t v110 = w11[0];
428 const int8_t v111 = w11[1];
429 const int8_t v112 = w11[2];
430 const int8_t v113 = w11[3];
431 w11 += 4;
432 const int8_t v120 = w12[0];
433 const int8_t v121 = w12[1];
434 const int8_t v122 = w12[2];
435 const int8_t v123 = w12[3];
436 w12 += 4;
437 const int8_t v130 = w13[0];
438 const int8_t v131 = w13[1];
439 const int8_t v132 = w13[2];
440 const int8_t v133 = w13[3];
441 w13 += 4;
442 const int8_t v140 = w14[0];
443 const int8_t v141 = w14[1];
444 const int8_t v142 = w14[2];
445 const int8_t v143 = w14[3];
446 w14 += 4;
447 out[0] = v00;
448 out[1] = v10;
449 out[2] = v20;
450 out[3] = v30;
451 out[4] = v40;
452 out[5] = v50;
453 out[6] = v60;
454 out[7] = v70;
455 out[8] = v80;
456 out[9] = v90;
457 out[10] = v100;
458 out[11] = v110;
459 out[12] = v120;
460 out[13] = v130;
461 out[14] = v140;
462 out[16] = v01;
463 out[17] = v11;
464 out[18] = v21;
465 out[19] = v31;
466 out[20] = v41;
467 out[21] = v51;
468 out[22] = v61;
469 out[23] = v71;
470 out[24] = v81;
471 out[25] = v91;
472 out[26] = v101;
473 out[27] = v111;
474 out[28] = v121;
475 out[29] = v131;
476 out[30] = v141;
477 out[32] = v02;
478 out[33] = v12;
479 out[34] = v22;
480 out[35] = v32;
481 out[36] = v42;
482 out[37] = v52;
483 out[38] = v62;
484 out[39] = v72;
485 out[40] = v82;
486 out[41] = v92;
487 out[42] = v102;
488 out[43] = v112;
489 out[44] = v122;
490 out[45] = v132;
491 out[46] = v142;
492 out[48] = v03;
493 out[49] = v13;
494 out[50] = v23;
495 out[51] = v33;
496 out[52] = v43;
497 out[53] = v53;
498 out[54] = v63;
499 out[55] = v73;
500 out[56] = v83;
501 out[57] = v93;
502 out[58] = v103;
503 out[59] = v113;
504 out[60] = v123;
505 out[61] = v133;
506 out[62] = v143;
507 out += 64;
508 }
509
510 // KC remainder of 1..3
511 for (; k != 0; --k) {
512 const int8_t v0 = *w0++;
513 out[0] = v0;
514 const int8_t v1 = *w1++;
515 out[1] = v1;
516 const int8_t v2 = *w2++;
517 out[2] = v2;
518 const int8_t v3 = *w3++;
519 out[3] = v3;
520 const int8_t v4 = *w4++;
521 out[4] = v4;
522 const int8_t v5 = *w5++;
523 out[5] = v5;
524 const int8_t v6 = *w6++;
525 out[6] = v6;
526 const int8_t v7 = *w7++;
527 out[7] = v7;
528 const int8_t v8 = *w8++;
529 out[8] = v8;
530 const int8_t v9 = *w9++;
531 out[9] = v9;
532 const int8_t v10 = *w10++;
533 out[10] = v10;
534 const int8_t v11 = *w11++;
535 out[11] = v11;
536 const int8_t v12 = *w12++;
537 out[12] = v12;
538 const int8_t v13 = *w13++;
539 out[13] = v13;
540 const int8_t v14 = *w14++;
541 out[14] = v14;
542 out += 16;
543 }
544 out = (int8_t*) ((uintptr_t) out + extra_bytes);
545 }
546 weights += nc * kc;
547 } while (--g != 0);
548 }