xref: /aosp_15_r20/external/XNNPACK/src/qs8-igemm/gen/2x2c4-minmax-fp32-armsimd32.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Auto-generated file. Do not edit!
2 //   Template: src/qs8-igemm/c4-armsimd32.c.in
3 //   Generator: tools/xngen
4 //
5 // Copyright 2022 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 <arm_acle.h>
13 
14 #include <xnnpack/intrinsics-polyfill.h>
15 #include <xnnpack/math.h>
16 #include <xnnpack/gemm.h>
17 #include <xnnpack/unaligned.h>
18 
19 
xnn_qs8_igemm_minmax_fp32_ukernel_2x2c4__armsimd32(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_qs8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])20 void xnn_qs8_igemm_minmax_fp32_ukernel_2x2c4__armsimd32(
21     size_t mr,
22     size_t nc,
23     size_t kc,
24     size_t ks,
25     const int8_t**restrict a,
26     const void*restrict w,
27     int8_t*restrict c,
28     size_t cm_stride,
29     size_t cn_stride,
30     size_t a_offset,
31     const int8_t* zero,
32     const union xnn_qs8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS(1)])
33 {
34   assert(mr != 0);
35   assert(mr <= 2);
36   assert(nc != 0);
37   assert(kc != 0);
38   assert(ks != 0);
39   assert(ks % (2 * sizeof(void*)) == 0);
40   assert(a != NULL);
41   assert(w != NULL);
42   assert(c != NULL);
43 
44   kc = round_up_po2(kc, 4 * sizeof(int8_t));
45   int8_t* c0 = c;
46   int8_t* c1 = (int8_t*) ((uintptr_t) c0 + cm_stride);
47   if XNN_UNPREDICTABLE(mr != 2) {
48     c1 = c0;
49   }
50 
51   const float vscale = params->fp32_armsimd32.scale;
52   const float vmagic_bias = params->fp32_armsimd32.magic_bias;
53   do {
54     int32_t vacc0x0 = ((const int32_t*) w)[0];
55     int32_t vacc0x1 = ((const int32_t*) w)[1];
56     int32_t vacc1x0 = vacc0x0;
57     int32_t vacc1x1 = vacc0x1;
58     w = (const void*) ((const int32_t*) w + 2);
59 
60     size_t p = ks;
61     do {
62       const int8_t* restrict a0 = a[0];
63       assert(a0 != NULL);
64       if XNN_UNPREDICTABLE(a0 != zero) {
65         a0 = (const int8_t*) ((uintptr_t) a0 + a_offset);
66       }
67       const int8_t* restrict a1 = a[1];
68       assert(a1 != NULL);
69       if XNN_UNPREDICTABLE(a1 != zero) {
70         a1 = (const int8_t*) ((uintptr_t) a1 + a_offset);
71       }
72       a += 2;
73 
74       size_t k = kc;
75       do {
76         const int8x4_t va0 = (int8x4_t) unaligned_load_s32(a0); a0 += 4;
77         const int8x4_t va1 = (int8x4_t) unaligned_load_s32(a1); a1 += 4;
78 
79         const int16x2_t va0c02 = __sxtb16(va0);
80         const int16x2_t va0c13 = __sxtb16(__ror(va0, 8));
81         const int16x2_t va1c02 = __sxtb16(va1);
82         const int16x2_t va1c13 = __sxtb16(__ror(va1, 8));
83 
84         const int8x4_t vb0 = *((const int8x4_t*) w); w = (const int8_t*) w + 4;
85         const int16x2_t vb0c02 = __sxtb16(vb0);
86 
87         vacc0x0 = __smlad(va0c02, vb0c02, vacc0x0);
88         vacc1x0 = __smlad(va1c02, vb0c02, vacc1x0);
89 
90         const int16x2_t vb0c13 = __sxtb16(__ror(vb0, 8));
91         vacc0x0 = __smlad(va0c13, vb0c13, vacc0x0);
92         vacc1x0 = __smlad(va1c13, vb0c13, vacc1x0);
93         const int8x4_t vb1 = *((const int8x4_t*) w); w = (const int8_t*) w + 4;
94         const int16x2_t vb1c02 = __sxtb16(vb1);
95 
96         vacc0x1 = __smlad(va0c02, vb1c02, vacc0x1);
97         vacc1x1 = __smlad(va1c02, vb1c02, vacc1x1);
98 
99         const int16x2_t vb1c13 = __sxtb16(__ror(vb1, 8));
100         vacc0x1 = __smlad(va0c13, vb1c13, vacc0x1);
101         vacc1x1 = __smlad(va1c13, vb1c13, vacc1x1);
102 
103         k -= 4 * sizeof(int8_t);
104       } while (k != 0);
105       p -= 2 * sizeof(void*);
106     } while (p != 0);
107 
108     float vfpacc0x0 = (float) vacc0x0;
109     float vfpacc0x1 = (float) vacc0x1;
110     float vfpacc1x0 = (float) vacc1x0;
111     float vfpacc1x1 = (float) vacc1x1;
112 
113     vfpacc0x0 *= vscale;
114     vfpacc0x1 *= vscale;
115     vfpacc1x0 *= vscale;
116     vfpacc1x1 *= vscale;
117 
118     vfpacc0x0 += vmagic_bias;
119     vfpacc0x1 += vmagic_bias;
120     vfpacc1x0 += vmagic_bias;
121     vfpacc1x1 += vmagic_bias;
122 
123     int32_t vout0x0 = (int32_t) float_as_uint32(vfpacc0x0);
124     int32_t vout0x1 = (int32_t) float_as_uint32(vfpacc0x1);
125     int32_t vout1x0 = (int32_t) float_as_uint32(vfpacc1x0);
126     int32_t vout1x1 = (int32_t) float_as_uint32(vfpacc1x1);
127 
128     const int32_t vmagic_bias_less_zero_point = params->fp32_armsimd32.magic_bias_less_zero_point;
129     vout0x0 = __qsub(vout0x0, vmagic_bias_less_zero_point);
130     vout0x1 = __qsub(vout0x1, vmagic_bias_less_zero_point);
131     vout1x0 = __qsub(vout1x0, vmagic_bias_less_zero_point);
132     vout1x1 = __qsub(vout1x1, vmagic_bias_less_zero_point);
133 
134     vout0x0 = __ssat(vout0x0, 8);
135     vout0x1 = __ssat(vout0x1, 8);
136     vout1x0 = __ssat(vout1x0, 8);
137     vout1x1 = __ssat(vout1x1, 8);
138 
139     const uint32_t vout0 = (uint32_t) (uint8_t) vout0x0 | ((uint32_t) vout0x1 << 8);
140     const uint32_t vout1 = (uint32_t) (uint8_t) vout1x0 | ((uint32_t) vout1x1 << 8);
141 
142     uint32_t vout = (uint32_t) (uint16_t) vout1 | (vout0 << 16);
143 
144     const int8x4_t voutput_min = (int8x4_t) params->fp32_armsimd32.output_min;
145     __ssub8((int8x4_t) vout, voutput_min);
146     vout = (uint32_t) __sel((uint8x4_t) vout, (uint8x4_t) voutput_min);
147 
148     const int8x4_t voutput_max = (int8x4_t) params->fp32_armsimd32.output_max;
149     __ssub8((int8x4_t) vout, voutput_max);
150     vout = (uint32_t) __sel((uint8x4_t) voutput_max, (uint8x4_t) vout);
151 
152     if XNN_LIKELY(nc >= 2) {
153       unaligned_store_u16(c1, (uint16_t) vout);
154       vout >>= 16;
155       unaligned_store_u16(c0, (uint16_t) vout);
156 
157       c1 = (int8_t*) ((uintptr_t) c1 + cn_stride);
158       c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
159 
160       a = (const int8_t**restrict) ((uintptr_t) a - ks);
161       nc -= 2;
162     } else {
163       *c1 = (int8_t) vout;
164       vout >>= 16;
165       *c0 = (int8_t) vout;
166 
167       nc = 0;
168     }
169   } while (nc != 0);
170 }
171