Debian Codegraph @

src/x8-packw/gen/x8-packw-x4-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_x4__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 == 4); // This kernel is for NR=4
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 4
47 const int8_t* w0 = (const int8_t*) weights;
48 size_t n = nc;
49 for (;n >= 4; n -= 4) {
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 b += 4;
56 } else {
57 unaligned_store_s32(out + 0 * sizeof(int32_t), 0);
58 unaligned_store_s32(out + 1 * sizeof(int32_t), 0);
59 unaligned_store_s32(out + 2 * sizeof(int32_t), 0);
60 unaligned_store_s32(out + 3 * sizeof(int32_t), 0);
61 }
62 out += 4 * sizeof(uint32_t);
63
64 const int8_t* w1 = w0 + kc;
65 const int8_t* w2 = w1 + kc;
66 const int8_t* w3 = w2 + kc;
67
68 // KC main loop multiple of 4x4
69 size_t k = kc;
70 for (; k >= 4; k -= 4) {
71 const int8_t v00 = w0[0];
72 const int8_t v01 = w0[1];
73 const int8_t v02 = w0[2];
74 const int8_t v03 = w0[3];
75 w0 += 4;
76 const int8_t v10 = w1[0];
77 const int8_t v11 = w1[1];
78 const int8_t v12 = w1[2];
79 const int8_t v13 = w1[3];
80 w1 += 4;
81 const int8_t v20 = w2[0];
82 const int8_t v21 = w2[1];
83 const int8_t v22 = w2[2];
84 const int8_t v23 = w2[3];
85 w2 += 4;
86 const int8_t v30 = w3[0];
87 const int8_t v31 = w3[1];
88 const int8_t v32 = w3[2];
89 const int8_t v33 = w3[3];
90 w3 += 4;
91 out[0] = v00;
92 out[1] = v10;
93 out[2] = v20;
94 out[3] = v30;
95 out[4] = v01;
96 out[5] = v11;
97 out[6] = v21;
98 out[7] = v31;
99 out[8] = v02;
100 out[9] = v12;
101 out[10] = v22;
102 out[11] = v32;
103 out[12] = v03;
104 out[13] = v13;
105 out[14] = v23;
106 out[15] = v33;
107 out += 16;
108 }
109
110 // KC remainder
111 for (; k != 0; --k) {
112 const int8_t v0 = *w0++;
113 out[0] = v0;
114 const int8_t v1 = *w1++;
115 out[1] = v1;
116 const int8_t v2 = *w2++;
117 out[2] = v2;
118 const int8_t v3 = *w3++;
119 out[3] = v3;
120 out += 4;
121 }
122 out = (int8_t*) ((uintptr_t) out + extra_bytes);
123 w0 = w3;
124 }
125
126 // NC remainder (1..3)
127 if XNN_UNLIKELY(n != 0) {
128 if XNN_LIKELY(b != NULL) {
129 size_t nb = n;
130 do {
131 unaligned_store_s32(out, *b++);
132 out += sizeof(uint32_t);
133 } while (--nb != 0);
134 } else {
135 size_t nb = n;
136 do {
137 unaligned_store_s32(out, 0);
138 out += sizeof(uint32_t);
139 } while (--nb != 0);
140 }
141 out += (4 - n) * sizeof(uint32_t);
142
143 // NR remainder has less than 4 rows so last row is not loaded
144 const int8_t* w1 = w0 + kc;
145 if XNN_UNPREDICTABLE(n < 2) {
146 w1 = w0;
147 }
148 const int8_t* w2 = w1 + kc;
149 if XNN_UNPREDICTABLE(n <= 2) {
150 w2 = w1;
151 }
152
153 // KC main loop multiple of 4x4
154 size_t k = kc;
155 for (; k >= 4; k -= 4) {
156 const int8_t v00 = w0[0];
157 const int8_t v01 = w0[1];
158 const int8_t v02 = w0[2];
159 const int8_t v03 = w0[3];
160 w0 += 4;
161 const int8_t v10 = w1[0];
162 const int8_t v11 = w1[1];
163 const int8_t v12 = w1[2];
164 const int8_t v13 = w1[3];
165 w1 += 4;
166 const int8_t v20 = w2[0];
167 const int8_t v21 = w2[1];
168 const int8_t v22 = w2[2];
169 const int8_t v23 = w2[3];
170 w2 += 4;
171 out[0] = v00;
172 out[1] = v10;
173 out[2] = v20;
174 out[4] = v01;
175 out[5] = v11;
176 out[6] = v21;
177 out[8] = v02;
178 out[9] = v12;
179 out[10] = v22;
180 out[12] = v03;
181 out[13] = v13;
182 out[14] = v23;
183 out += 16;
184 }
185
186 // KC remainder of 1..3
187 for (; k != 0; --k) {
188 const int8_t v0 = *w0++;
189 out[0] = v0;
190 const int8_t v1 = *w1++;
191 out[1] = v1;
192 const int8_t v2 = *w2++;
193 out[2] = v2;
194 out += 4;
195 }
196 out = (int8_t*) ((uintptr_t) out + extra_bytes);
197 }
198 weights += nc * kc;
199 } while (--g != 0);
200 }