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