Debian Codegraph @

src/qd8-f32-qc4w-gemm/gen/qd8-f32-qc4w-gemm-2x4-minmax-scalar.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/qs8-gemm/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/gemm.h"
13 #include "xnnpack/math.h"
14
15
16 void xnn_qd8_f32_qc4w_gemm_minmax_ukernel_2x4__scalar(
17 size_t mr,
18 size_t nc,
19 size_t kc,
20 const int8_t* restrict a,
21 size_t a_stride,
22 const void* restrict w,
23 float* restrict c,
24 size_t cm_stride,
25 size_t cn_stride,
26 const struct xnn_f32_qc4w_minmax_params params[restrict XNN_MIN_ELEMENTS(1)],
27 const struct xnn_qd8_quantization_params quantization_params[restrict XNN_MIN_ELEMENTS(1)])
28 {
29 assert(mr != 0);
30 assert(mr <= 2);
31 assert(nc != 0);
32 assert(kc != 0);
33
34 const int8_t* a0 = a;
35 float* c0 = c;
36 const int8_t* a1 = (const int8_t*) ((uintptr_t) a0 + a_stride);
37 float* c1 = (float*) ((uintptr_t) c0 + cm_stride);
38 if XNN_UNPREDICTABLE(mr != 2) {
39 a1 = a0;
40 c1 = c0;
41 }
42
43
44 kc = round_up_po2(kc, 2);
45 do {
46 const int32_t vksum0 = ((const int32_t*) w)[0];
47 const int32_t vksum1 = ((const int32_t*) w)[1];
48 const int32_t vksum2 = ((const int32_t*) w)[2];
49 const int32_t vksum3 = ((const int32_t*) w)[3];
50 const int32_t vinput_zero_point0 = quantization_params[0].zero_point;
51 int32_t vacc0x0 = vksum0 * vinput_zero_point0;
52 int32_t vacc0x1 = vksum1 * vinput_zero_point0;
53 int32_t vacc0x2 = vksum2 * vinput_zero_point0;
54 int32_t vacc0x3 = vksum3 * vinput_zero_point0;
55 const int32_t vinput_zero_point1 = quantization_params[1].zero_point;
56 int32_t vacc1x0 = vksum0 * vinput_zero_point1;
57 int32_t vacc1x1 = vksum1 * vinput_zero_point1;
58 int32_t vacc1x2 = vksum2 * vinput_zero_point1;
59 int32_t vacc1x3 = vksum3 * vinput_zero_point1;
60 w = (const int32_t*) w + 4;
61
62 size_t k = kc;
63 for (; k >= 2 * sizeof(uint8_t); k -= 2 * sizeof(uint8_t)) {
64 const int32_t va0c0 = (int32_t) a0[0];
65 const int32_t va0c1 = (int32_t) a0[1];
66 a0 += 2;
67 const int32_t va1c0 = (int32_t) a1[0];
68 const int32_t va1c1 = (int32_t) a1[1];
69 a1 += 2;
70
71 const uint8_t vbi0 = ((const uint8_t*) w)[0];
72 const uint8_t vbi1 = ((const uint8_t*) w)[1];
73 const uint8_t vbi2 = ((const uint8_t*) w)[2];
74 const uint8_t vbi3 = ((const uint8_t*) w)[3];
75 w = (const uint8_t*) w + 4;
76 const int32_t vb0c0 = (int32_t) (int8_t) (vbi0 << 4);
77 const int32_t vb0c1 = (int32_t) (int8_t) (vbi0 & 0xF0);
78 const int32_t vb1c0 = (int32_t) (int8_t) (vbi1 << 4);
79 const int32_t vb1c1 = (int32_t) (int8_t) (vbi1 & 0xF0);
80 const int32_t vb2c0 = (int32_t) (int8_t) (vbi2 << 4);
81 const int32_t vb2c1 = (int32_t) (int8_t) (vbi2 & 0xF0);
82 const int32_t vb3c0 = (int32_t) (int8_t) (vbi3 << 4);
83 const int32_t vb3c1 = (int32_t) (int8_t) (vbi3 & 0xF0);
84
85 vacc0x0 += va0c0 * vb0c0;
86 vacc0x1 += va0c0 * vb1c0;
87 vacc0x2 += va0c0 * vb2c0;
88 vacc0x3 += va0c0 * vb3c0;
89 vacc1x0 += va1c0 * vb0c0;
90 vacc1x1 += va1c0 * vb1c0;
91 vacc1x2 += va1c0 * vb2c0;
92 vacc1x3 += va1c0 * vb3c0;
93 vacc0x0 += va0c1 * vb0c1;
94 vacc0x1 += va0c1 * vb1c1;
95 vacc0x2 += va0c1 * vb2c1;
96 vacc0x3 += va0c1 * vb3c1;
97 vacc1x0 += va1c1 * vb0c1;
98 vacc1x1 += va1c1 * vb1c1;
99 vacc1x2 += va1c1 * vb2c1;
100 vacc1x3 += va1c1 * vb3c1;
101 }
102
103 float vout0x0 = (float) math_asr_s32(vacc0x0, 4);
104 float vout0x1 = (float) math_asr_s32(vacc0x1, 4);
105 float vout0x2 = (float) math_asr_s32(vacc0x2, 4);
106 float vout0x3 = (float) math_asr_s32(vacc0x3, 4);
107 float vout1x0 = (float) math_asr_s32(vacc1x0, 4);
108 float vout1x1 = (float) math_asr_s32(vacc1x1, 4);
109 float vout1x2 = (float) math_asr_s32(vacc1x2, 4);
110 float vout1x3 = (float) math_asr_s32(vacc1x3, 4);
111
112 const float vinput_scale0 = quantization_params[0].inv_scale;
113 vout0x0 *= vinput_scale0;
114 vout0x1 *= vinput_scale0;
115 vout0x2 *= vinput_scale0;
116 vout0x3 *= vinput_scale0;
117 const float vinput_scale1 = quantization_params[1].inv_scale;
118 vout1x0 *= vinput_scale1;
119 vout1x1 *= vinput_scale1;
120 vout1x2 *= vinput_scale1;
121 vout1x3 *= vinput_scale1;
122
123 const float vfilter_output_scale0 = ((const float*) w)[0];
124 vout0x0 *= vfilter_output_scale0;
125 vout1x0 *= vfilter_output_scale0;
126 const float vfilter_output_scale1 = ((const float*) w)[1];
127 vout0x1 *= vfilter_output_scale1;
128 vout1x1 *= vfilter_output_scale1;
129 const float vfilter_output_scale2 = ((const float*) w)[2];
130 vout0x2 *= vfilter_output_scale2;
131 vout1x2 *= vfilter_output_scale2;
132 const float vfilter_output_scale3 = ((const float*) w)[3];
133 vout0x3 *= vfilter_output_scale3;
134 vout1x3 *= vfilter_output_scale3;
135
136 const float vbias0 = ((const float*) w)[4];
137 vout0x0 += vbias0;
138 vout1x0 += vbias0;
139 const float vbias1 = ((const float*) w)[5];
140 vout0x1 += vbias1;
141 vout1x1 += vbias1;
142 const float vbias2 = ((const float*) w)[6];
143 vout0x2 += vbias2;
144 vout1x2 += vbias2;
145 const float vbias3 = ((const float*) w)[7];
146 vout0x3 += vbias3;
147 vout1x3 += vbias3;
148
149 w = (const float*) w + 8;
150
151 const float voutput_min = params->scalar.min;
152 vout0x0 = math_max_f32(vout0x0, voutput_min);
153 vout1x0 = math_max_f32(vout1x0, voutput_min);
154 vout0x1 = math_max_f32(vout0x1, voutput_min);
155 vout1x1 = math_max_f32(vout1x1, voutput_min);
156 vout0x2 = math_max_f32(vout0x2, voutput_min);
157 vout1x2 = math_max_f32(vout1x2, voutput_min);
158 vout0x3 = math_max_f32(vout0x3, voutput_min);
159 vout1x3 = math_max_f32(vout1x3, voutput_min);
160
161 const float voutput_max = params->scalar.max;
162 vout0x0 = math_min_f32(vout0x0, voutput_max);
163 vout1x0 = math_min_f32(vout1x0, voutput_max);
164 vout0x1 = math_min_f32(vout0x1, voutput_max);
165 vout1x1 = math_min_f32(vout1x1, voutput_max);
166 vout0x2 = math_min_f32(vout0x2, voutput_max);
167 vout1x2 = math_min_f32(vout1x2, voutput_max);
168 vout0x3 = math_min_f32(vout0x3, voutput_max);
169 vout1x3 = math_min_f32(vout1x3, voutput_max);
170
171 if XNN_LIKELY(nc >= 4) {
172 c0[0] = vout0x0;
173 c0[1] = vout0x1;
174 c0[2] = vout0x2;
175 c0[3] = vout0x3;
176 c1[0] = vout1x0;
177 c1[1] = vout1x1;
178 c1[2] = vout1x2;
179 c1[3] = vout1x3;
180
181 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
182 a1 = (const int8_t*) ((uintptr_t) a1 - kc);
183
184 c0 = (float*) ((uintptr_t) c0 + cn_stride);
185 c1 = (float*) ((uintptr_t) c1 + cn_stride);
186
187 nc -= 4;
188 } else {
189 if (nc & 2) {
190 c0[0] = vout0x0;
191 c0[1] = vout0x1;
192 vout0x0 = vout0x2;
193 c0 += 2;
194 c1[0] = vout1x0;
195 c1[1] = vout1x1;
196 vout1x0 = vout1x2;
197 c1 += 2;
198 }
199 if (nc & 1) {
200 c0[0] = vout0x0;
201 c1[0] = vout1x0;
202 }
203
204 nc = 0;
205 }
206 } while (nc != 0);
207 }