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