Debian Codegraph @

src/qs8-qc8w-igemm/gen/qs8-qc8w-igemm-2x4-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_2x4__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 <= 2);
32 assert(nc != 0);
33 assert(kc != 0);
34 assert(ks != 0);
35 assert(ks % (2 * 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
46 const int32_t output_min_less_zero_point = (int32_t) params->fp32_scalar.output_min - (int32_t) params->fp32_scalar.output_zero_point;
47 const int32_t output_max_less_zero_point = (int32_t) params->fp32_scalar.output_max - (int32_t) params->fp32_scalar.output_zero_point;
48 const float vmagic_bias = 12582912.0f;
49 const int32_t vmagic_min = (int32_t) float_as_uint32(12582912.0f + output_min_less_zero_point);
50 const int32_t vmagic_max = (int32_t) float_as_uint32(12582912.0f + output_max_less_zero_point);
51 const int32_t vmagic_bias_less_zero_point = INT32_C(0x4B400000) - (int32_t) params->fp32_scalar.output_zero_point;
52
53 do {
54 int32_t vacc0x0 = ((const int32_t*) w)[0];
55 int32_t vacc0x1 = ((const int32_t*) w)[1];
56 int32_t vacc0x2 = ((const int32_t*) w)[2];
57 int32_t vacc0x3 = ((const int32_t*) w)[3];
58 int32_t vacc1x0 = vacc0x0;
59 int32_t vacc1x1 = vacc0x1;
60 int32_t vacc1x2 = vacc0x2;
61 int32_t vacc1x3 = vacc0x3;
62 w = (const void*) ((const int32_t*) w + 4);
63
64 size_t p = ks;
65 do {
66 const int8_t* restrict a0 = a[0];
67 assert(a0 != NULL);
68 if XNN_UNPREDICTABLE(a0 != zero) {
69 a0 = (const int8_t*) ((uintptr_t) a0 + a_offset);
70 }
71 const int8_t* restrict a1 = a[1];
72 assert(a1 != NULL);
73 if XNN_UNPREDICTABLE(a1 != zero) {
74 a1 = (const int8_t*) ((uintptr_t) a1 + a_offset);
75 }
76 a += 2;
77
78 size_t k = kc;
79 do {
80 const int32_t va0 = (int32_t) *a0++;
81 const int32_t va1 = (int32_t) *a1++;
82
83 const int32_t vb0 = (int32_t) ((const int8_t*) w)[0];
84 const int32_t vb1 = (int32_t) ((const int8_t*) w)[1];
85 const int32_t vb2 = (int32_t) ((const int8_t*) w)[2];
86 const int32_t vb3 = (int32_t) ((const int8_t*) w)[3];
87 w = (const void*) ((const int8_t*) w + 4);
88
89 vacc0x0 += va0 * vb0;
90 vacc0x1 += va0 * vb1;
91 vacc0x2 += va0 * vb2;
92 vacc0x3 += va0 * vb3;
93 vacc1x0 += va1 * vb0;
94 vacc1x1 += va1 * vb1;
95 vacc1x2 += va1 * vb2;
96 vacc1x3 += va1 * vb3;
97
98 k -= sizeof(int8_t);
99 } while (k != 0);
100 p -= 2 * sizeof(void*);
101 } while (p != 0);
102
103 float vfpacc0x0 = (float) vacc0x0;
104 float vfpacc0x1 = (float) vacc0x1;
105 float vfpacc0x2 = (float) vacc0x2;
106 float vfpacc0x3 = (float) vacc0x3;
107 float vfpacc1x0 = (float) vacc1x0;
108 float vfpacc1x1 = (float) vacc1x1;
109 float vfpacc1x2 = (float) vacc1x2;
110 float vfpacc1x3 = (float) vacc1x3;
111
112 const float vscale0 = ((const float*) w)[0];
113 vfpacc0x0 *= vscale0;
114 vfpacc1x0 *= vscale0;
115 const float vscale1 = ((const float*) w)[1];
116 vfpacc0x1 *= vscale1;
117 vfpacc1x1 *= vscale1;
118 const float vscale2 = ((const float*) w)[2];
119 vfpacc0x2 *= vscale2;
120 vfpacc1x2 *= vscale2;
121 const float vscale3 = ((const float*) w)[3];
122 vfpacc0x3 *= vscale3;
123 vfpacc1x3 *= vscale3;
124 w = (const void*) ((const float*) w + 4);
125
126 vfpacc0x0 += vmagic_bias;
127 vfpacc0x1 += vmagic_bias;
128 vfpacc0x2 += vmagic_bias;
129 vfpacc0x3 += vmagic_bias;
130 vfpacc1x0 += vmagic_bias;
131 vfpacc1x1 += vmagic_bias;
132 vfpacc1x2 += vmagic_bias;
133 vfpacc1x3 += vmagic_bias;
134
135 int32_t vout0x0 = (int32_t) float_as_uint32(vfpacc0x0);
136 int32_t vout0x1 = (int32_t) float_as_uint32(vfpacc0x1);
137 int32_t vout0x2 = (int32_t) float_as_uint32(vfpacc0x2);
138 int32_t vout0x3 = (int32_t) float_as_uint32(vfpacc0x3);
139 int32_t vout1x0 = (int32_t) float_as_uint32(vfpacc1x0);
140 int32_t vout1x1 = (int32_t) float_as_uint32(vfpacc1x1);
141 int32_t vout1x2 = (int32_t) float_as_uint32(vfpacc1x2);
142 int32_t vout1x3 = (int32_t) float_as_uint32(vfpacc1x3);
143
144 vout0x0 = math_max_s32(vout0x0, vmagic_min);
145 vout0x1 = math_max_s32(vout0x1, vmagic_min);
146 vout0x2 = math_max_s32(vout0x2, vmagic_min);
147 vout0x3 = math_max_s32(vout0x3, vmagic_min);
148 vout1x0 = math_max_s32(vout1x0, vmagic_min);
149 vout1x1 = math_max_s32(vout1x1, vmagic_min);
150 vout1x2 = math_max_s32(vout1x2, vmagic_min);
151 vout1x3 = math_max_s32(vout1x3, vmagic_min);
152
153 vout0x0 = math_min_s32(vout0x0, vmagic_max);
154 vout0x1 = math_min_s32(vout0x1, vmagic_max);
155 vout0x2 = math_min_s32(vout0x2, vmagic_max);
156 vout0x3 = math_min_s32(vout0x3, vmagic_max);
157 vout1x0 = math_min_s32(vout1x0, vmagic_max);
158 vout1x1 = math_min_s32(vout1x1, vmagic_max);
159 vout1x2 = math_min_s32(vout1x2, vmagic_max);
160 vout1x3 = math_min_s32(vout1x3, vmagic_max);
161
162 vout0x0 -= vmagic_bias_less_zero_point;
163 vout0x1 -= vmagic_bias_less_zero_point;
164 vout0x2 -= vmagic_bias_less_zero_point;
165 vout0x3 -= vmagic_bias_less_zero_point;
166 vout1x0 -= vmagic_bias_less_zero_point;
167 vout1x1 -= vmagic_bias_less_zero_point;
168 vout1x2 -= vmagic_bias_less_zero_point;
169 vout1x3 -= vmagic_bias_less_zero_point;
170
171 if XNN_LIKELY(nc >= 4) {
172 c1[0] = (int8_t) vout1x0;
173 c1[1] = (int8_t) vout1x1;
174 c1[2] = (int8_t) vout1x2;
175 c1[3] = (int8_t) vout1x3;
176 c0[0] = (int8_t) vout0x0;
177 c0[1] = (int8_t) vout0x1;
178 c0[2] = (int8_t) vout0x2;
179 c0[3] = (int8_t) vout0x3;
180
181 c1 = (int8_t*) ((uintptr_t) c1 + cn_stride);
182 c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
183
184 a = (const int8_t**restrict) ((uintptr_t) a - ks);
185 nc -= 4;
186 } else {
187 if (nc & 2) {
188 c1[0] = (int8_t) vout1x0;
189 c1[1] = (int8_t) vout1x1;
190 vout1x0 = vout1x2;
191 c1 += 2;
192 c0[0] = (int8_t) vout0x0;
193 c0[1] = (int8_t) vout0x1;
194 vout0x0 = vout0x2;
195 c0 += 2;
196 }
197 if (nc & 1) {
198 c1[0] = (int8_t) vout1x0;
199 c0[0] = (int8_t) vout0x0;
200 }
201
202 nc = 0;
203 }
204 } while (nc != 0);
205 }