xref: /aosp_15_r20/external/XNNPACK/src/qu8-igemm/gen/4x4-minmax-fp32-scalar-lrintf.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 #include <math.h>
12 
13 #include <xnnpack/math.h>
14 #include <xnnpack/gemm.h>
15 
16 
xnn_qu8_igemm_minmax_fp32_ukernel_4x4__scalar_lrintf(size_t mr,size_t nc,size_t kc,size_t ks,const uint8_t ** restrict a,const void * restrict w,uint8_t * restrict c,size_t cm_stride,size_t cn_stride,size_t a_offset,const uint8_t * zero,const union xnn_qu8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])17 void xnn_qu8_igemm_minmax_fp32_ukernel_4x4__scalar_lrintf(
18     size_t mr,
19     size_t nc,
20     size_t kc,
21     size_t ks,
22     const uint8_t**restrict a,
23     const void*restrict w,
24     uint8_t*restrict c,
25     size_t cm_stride,
26     size_t cn_stride,
27     size_t a_offset,
28     const uint8_t* zero,
29     const union xnn_qu8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS(1)])
30 {
31   assert(mr != 0);
32   assert(mr <= 4);
33   assert(nc != 0);
34   assert(kc != 0);
35   assert(ks != 0);
36   assert(ks % (4 * sizeof(void*)) == 0);
37   assert(a != NULL);
38   assert(w != NULL);
39   assert(c != NULL);
40 
41   uint8_t* c0 = c;
42   uint8_t* c1 = (uint8_t*) ((uintptr_t) c0 + cm_stride);
43   if XNN_UNPREDICTABLE(mr < 2) {
44     c1 = c0;
45   }
46   uint8_t* c2 = (uint8_t*) ((uintptr_t) c1 + cm_stride);
47   if XNN_UNPREDICTABLE(mr <= 2) {
48     c2 = c1;
49   }
50   uint8_t* c3 = (uint8_t*) ((uintptr_t) c2 + cm_stride);
51   if XNN_UNPREDICTABLE(mr != 4) {
52     c3 = c2;
53   }
54 
55   const int32_t vb_zero_point = params->fp32_scalar_lrintf.kernel_zero_point;
56   do {
57     int32_t vacc0x0 = ((const int32_t*) w)[0];
58     int32_t vacc0x1 = ((const int32_t*) w)[1];
59     int32_t vacc0x2 = ((const int32_t*) w)[2];
60     int32_t vacc0x3 = ((const int32_t*) w)[3];
61     int32_t vacc1x0 = vacc0x0;
62     int32_t vacc1x1 = vacc0x1;
63     int32_t vacc1x2 = vacc0x2;
64     int32_t vacc1x3 = vacc0x3;
65     int32_t vacc2x0 = vacc0x0;
66     int32_t vacc2x1 = vacc0x1;
67     int32_t vacc2x2 = vacc0x2;
68     int32_t vacc2x3 = vacc0x3;
69     int32_t vacc3x0 = vacc0x0;
70     int32_t vacc3x1 = vacc0x1;
71     int32_t vacc3x2 = vacc0x2;
72     int32_t vacc3x3 = vacc0x3;
73     w = (const void*) ((const int32_t*) w + 4);
74 
75     size_t p = ks;
76     do {
77       const uint8_t* restrict a0 = a[0];
78       assert(a0 != NULL);
79       if XNN_UNPREDICTABLE(a0 != zero) {
80         a0 = (const uint8_t*) ((uintptr_t) a0 + a_offset);
81       }
82       const uint8_t* restrict a1 = a[1];
83       assert(a1 != NULL);
84       if XNN_UNPREDICTABLE(a1 != zero) {
85         a1 = (const uint8_t*) ((uintptr_t) a1 + a_offset);
86       }
87       const uint8_t* restrict a2 = a[2];
88       assert(a2 != NULL);
89       if XNN_UNPREDICTABLE(a2 != zero) {
90         a2 = (const uint8_t*) ((uintptr_t) a2 + a_offset);
91       }
92       const uint8_t* restrict a3 = a[3];
93       assert(a3 != NULL);
94       if XNN_UNPREDICTABLE(a3 != zero) {
95         a3 = (const uint8_t*) ((uintptr_t) a3 + a_offset);
96       }
97       a += 4;
98 
99       size_t k = kc;
100       do {
101         const int32_t va0 = (int32_t) (uint32_t) *a0++;
102         const int32_t va1 = (int32_t) (uint32_t) *a1++;
103         const int32_t va2 = (int32_t) (uint32_t) *a2++;
104         const int32_t va3 = (int32_t) (uint32_t) *a3++;
105 
106         const int32_t vb0 = (int32_t) (uint32_t) ((const uint8_t*) w)[0] - vb_zero_point;
107         const int32_t vb1 = (int32_t) (uint32_t) ((const uint8_t*) w)[1] - vb_zero_point;
108         const int32_t vb2 = (int32_t) (uint32_t) ((const uint8_t*) w)[2] - vb_zero_point;
109         const int32_t vb3 = (int32_t) (uint32_t) ((const uint8_t*) w)[3] - vb_zero_point;
110         w = (const void*) ((const uint8_t*) w + 4);
111 
112         vacc0x0 += va0 * vb0;
113         vacc0x1 += va0 * vb1;
114         vacc0x2 += va0 * vb2;
115         vacc0x3 += va0 * vb3;
116         vacc1x0 += va1 * vb0;
117         vacc1x1 += va1 * vb1;
118         vacc1x2 += va1 * vb2;
119         vacc1x3 += va1 * vb3;
120         vacc2x0 += va2 * vb0;
121         vacc2x1 += va2 * vb1;
122         vacc2x2 += va2 * vb2;
123         vacc2x3 += va2 * vb3;
124         vacc3x0 += va3 * vb0;
125         vacc3x1 += va3 * vb1;
126         vacc3x2 += va3 * vb2;
127         vacc3x3 += va3 * vb3;
128 
129         k -= sizeof(uint8_t);
130       } while (k != 0);
131       p -= 4 * sizeof(void*);
132     } while (p != 0);
133 
134     float vfpacc0x0 = (float) vacc0x0;
135     float vfpacc0x1 = (float) vacc0x1;
136     float vfpacc0x2 = (float) vacc0x2;
137     float vfpacc0x3 = (float) vacc0x3;
138     float vfpacc1x0 = (float) vacc1x0;
139     float vfpacc1x1 = (float) vacc1x1;
140     float vfpacc1x2 = (float) vacc1x2;
141     float vfpacc1x3 = (float) vacc1x3;
142     float vfpacc2x0 = (float) vacc2x0;
143     float vfpacc2x1 = (float) vacc2x1;
144     float vfpacc2x2 = (float) vacc2x2;
145     float vfpacc2x3 = (float) vacc2x3;
146     float vfpacc3x0 = (float) vacc3x0;
147     float vfpacc3x1 = (float) vacc3x1;
148     float vfpacc3x2 = (float) vacc3x2;
149     float vfpacc3x3 = (float) vacc3x3;
150 
151     const float vscale = params->fp32_scalar_lrintf.scale;
152     vfpacc0x0 *= vscale;
153     vfpacc0x1 *= vscale;
154     vfpacc0x2 *= vscale;
155     vfpacc0x3 *= vscale;
156     vfpacc1x0 *= vscale;
157     vfpacc1x1 *= vscale;
158     vfpacc1x2 *= vscale;
159     vfpacc1x3 *= vscale;
160     vfpacc2x0 *= vscale;
161     vfpacc2x1 *= vscale;
162     vfpacc2x2 *= vscale;
163     vfpacc2x3 *= vscale;
164     vfpacc3x0 *= vscale;
165     vfpacc3x1 *= vscale;
166     vfpacc3x2 *= vscale;
167     vfpacc3x3 *= vscale;
168 
169     const float voutput_min_less_zero_point = params->fp32_scalar_lrintf.output_min_less_zero_point;
170     vfpacc0x0 = math_max_f32(vfpacc0x0, voutput_min_less_zero_point);
171     vfpacc0x1 = math_max_f32(vfpacc0x1, voutput_min_less_zero_point);
172     vfpacc0x2 = math_max_f32(vfpacc0x2, voutput_min_less_zero_point);
173     vfpacc0x3 = math_max_f32(vfpacc0x3, voutput_min_less_zero_point);
174     vfpacc1x0 = math_max_f32(vfpacc1x0, voutput_min_less_zero_point);
175     vfpacc1x1 = math_max_f32(vfpacc1x1, voutput_min_less_zero_point);
176     vfpacc1x2 = math_max_f32(vfpacc1x2, voutput_min_less_zero_point);
177     vfpacc1x3 = math_max_f32(vfpacc1x3, voutput_min_less_zero_point);
178     vfpacc2x0 = math_max_f32(vfpacc2x0, voutput_min_less_zero_point);
179     vfpacc2x1 = math_max_f32(vfpacc2x1, voutput_min_less_zero_point);
180     vfpacc2x2 = math_max_f32(vfpacc2x2, voutput_min_less_zero_point);
181     vfpacc2x3 = math_max_f32(vfpacc2x3, voutput_min_less_zero_point);
182     vfpacc3x0 = math_max_f32(vfpacc3x0, voutput_min_less_zero_point);
183     vfpacc3x1 = math_max_f32(vfpacc3x1, voutput_min_less_zero_point);
184     vfpacc3x2 = math_max_f32(vfpacc3x2, voutput_min_less_zero_point);
185     vfpacc3x3 = math_max_f32(vfpacc3x3, voutput_min_less_zero_point);
186 
187     const float voutput_max_less_zero_point = params->fp32_scalar_lrintf.output_max_less_zero_point;
188     vfpacc0x0 = math_min_f32(vfpacc0x0, voutput_max_less_zero_point);
189     vfpacc0x1 = math_min_f32(vfpacc0x1, voutput_max_less_zero_point);
190     vfpacc0x2 = math_min_f32(vfpacc0x2, voutput_max_less_zero_point);
191     vfpacc0x3 = math_min_f32(vfpacc0x3, voutput_max_less_zero_point);
192     vfpacc1x0 = math_min_f32(vfpacc1x0, voutput_max_less_zero_point);
193     vfpacc1x1 = math_min_f32(vfpacc1x1, voutput_max_less_zero_point);
194     vfpacc1x2 = math_min_f32(vfpacc1x2, voutput_max_less_zero_point);
195     vfpacc1x3 = math_min_f32(vfpacc1x3, voutput_max_less_zero_point);
196     vfpacc2x0 = math_min_f32(vfpacc2x0, voutput_max_less_zero_point);
197     vfpacc2x1 = math_min_f32(vfpacc2x1, voutput_max_less_zero_point);
198     vfpacc2x2 = math_min_f32(vfpacc2x2, voutput_max_less_zero_point);
199     vfpacc2x3 = math_min_f32(vfpacc2x3, voutput_max_less_zero_point);
200     vfpacc3x0 = math_min_f32(vfpacc3x0, voutput_max_less_zero_point);
201     vfpacc3x1 = math_min_f32(vfpacc3x1, voutput_max_less_zero_point);
202     vfpacc3x2 = math_min_f32(vfpacc3x2, voutput_max_less_zero_point);
203     vfpacc3x3 = math_min_f32(vfpacc3x3, voutput_max_less_zero_point);
204 
205     const int32_t vrndacc0x0 = (int32_t) lrintf(vfpacc0x0);
206     const int32_t vrndacc0x1 = (int32_t) lrintf(vfpacc0x1);
207     const int32_t vrndacc0x2 = (int32_t) lrintf(vfpacc0x2);
208     const int32_t vrndacc0x3 = (int32_t) lrintf(vfpacc0x3);
209     const int32_t vrndacc1x0 = (int32_t) lrintf(vfpacc1x0);
210     const int32_t vrndacc1x1 = (int32_t) lrintf(vfpacc1x1);
211     const int32_t vrndacc1x2 = (int32_t) lrintf(vfpacc1x2);
212     const int32_t vrndacc1x3 = (int32_t) lrintf(vfpacc1x3);
213     const int32_t vrndacc2x0 = (int32_t) lrintf(vfpacc2x0);
214     const int32_t vrndacc2x1 = (int32_t) lrintf(vfpacc2x1);
215     const int32_t vrndacc2x2 = (int32_t) lrintf(vfpacc2x2);
216     const int32_t vrndacc2x3 = (int32_t) lrintf(vfpacc2x3);
217     const int32_t vrndacc3x0 = (int32_t) lrintf(vfpacc3x0);
218     const int32_t vrndacc3x1 = (int32_t) lrintf(vfpacc3x1);
219     const int32_t vrndacc3x2 = (int32_t) lrintf(vfpacc3x2);
220     const int32_t vrndacc3x3 = (int32_t) lrintf(vfpacc3x3);
221 
222     const int32_t voutput_zero_point = params->fp32_scalar_lrintf.output_zero_point;
223     int32_t vout0x0 = vrndacc0x0 + voutput_zero_point;
224     int32_t vout0x1 = vrndacc0x1 + voutput_zero_point;
225     int32_t vout0x2 = vrndacc0x2 + voutput_zero_point;
226     int32_t vout0x3 = vrndacc0x3 + voutput_zero_point;
227     int32_t vout1x0 = vrndacc1x0 + voutput_zero_point;
228     int32_t vout1x1 = vrndacc1x1 + voutput_zero_point;
229     int32_t vout1x2 = vrndacc1x2 + voutput_zero_point;
230     int32_t vout1x3 = vrndacc1x3 + voutput_zero_point;
231     int32_t vout2x0 = vrndacc2x0 + voutput_zero_point;
232     int32_t vout2x1 = vrndacc2x1 + voutput_zero_point;
233     int32_t vout2x2 = vrndacc2x2 + voutput_zero_point;
234     int32_t vout2x3 = vrndacc2x3 + voutput_zero_point;
235     int32_t vout3x0 = vrndacc3x0 + voutput_zero_point;
236     int32_t vout3x1 = vrndacc3x1 + voutput_zero_point;
237     int32_t vout3x2 = vrndacc3x2 + voutput_zero_point;
238     int32_t vout3x3 = vrndacc3x3 + voutput_zero_point;
239 
240     if XNN_LIKELY(nc >= 4) {
241       c3[0] = (uint8_t) vout3x0;
242       c3[1] = (uint8_t) vout3x1;
243       c3[2] = (uint8_t) vout3x2;
244       c3[3] = (uint8_t) vout3x3;
245       c2[0] = (uint8_t) vout2x0;
246       c2[1] = (uint8_t) vout2x1;
247       c2[2] = (uint8_t) vout2x2;
248       c2[3] = (uint8_t) vout2x3;
249       c1[0] = (uint8_t) vout1x0;
250       c1[1] = (uint8_t) vout1x1;
251       c1[2] = (uint8_t) vout1x2;
252       c1[3] = (uint8_t) vout1x3;
253       c0[0] = (uint8_t) vout0x0;
254       c0[1] = (uint8_t) vout0x1;
255       c0[2] = (uint8_t) vout0x2;
256       c0[3] = (uint8_t) vout0x3;
257 
258       c3 = (uint8_t*) ((uintptr_t) c3 + cn_stride);
259       c2 = (uint8_t*) ((uintptr_t) c2 + cn_stride);
260       c1 = (uint8_t*) ((uintptr_t) c1 + cn_stride);
261       c0 = (uint8_t*) ((uintptr_t) c0 + cn_stride);
262 
263       a = (const uint8_t**restrict) ((uintptr_t) a - ks);
264       nc -= 4;
265     } else {
266       if (nc & 2) {
267         c3[0] = (uint8_t) vout3x0;
268         c3[1] = (uint8_t) vout3x1;
269         vout3x0 = vout3x2;
270         c3 += 2;
271         c2[0] = (uint8_t) vout2x0;
272         c2[1] = (uint8_t) vout2x1;
273         vout2x0 = vout2x2;
274         c2 += 2;
275         c1[0] = (uint8_t) vout1x0;
276         c1[1] = (uint8_t) vout1x1;
277         vout1x0 = vout1x2;
278         c1 += 2;
279         c0[0] = (uint8_t) vout0x0;
280         c0[1] = (uint8_t) vout0x1;
281         vout0x0 = vout0x2;
282         c0 += 2;
283       }
284       if (nc & 1) {
285         c3[0] = (uint8_t) vout3x0;
286         c2[0] = (uint8_t) vout2x0;
287         c1[0] = (uint8_t) vout1x0;
288         c0[0] = (uint8_t) vout0x0;
289       }
290 
291       nc = 0;
292     }
293   } while (nc != 0);
294 }
295