xref: /aosp_15_r20/external/XNNPACK/src/f32-f16-vcvt/gen/vcvt-scalar-bitcast-x4.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-f16-vcvt/scalar-bitcast.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/common.h>
13 #include <xnnpack/math.h>
14 #include <xnnpack/vcvt.h>
15 
16 
xnn_f32_f16_vcvt_ukernel__scalar_bitcast_x4(size_t n,const float * input,void * output,const union xnn_f32_f16_cvt_params params[restrict XNN_MIN_ELEMENTS (1)])17 void xnn_f32_f16_vcvt_ukernel__scalar_bitcast_x4(
18     size_t n,
19     const float* input,
20     void* output,
21     const union xnn_f32_f16_cvt_params params[restrict XNN_MIN_ELEMENTS(1)])
22 {
23   assert(n != 0);
24   assert(n % sizeof(float) == 0);
25   assert(input != NULL);
26   assert(output != NULL);
27 
28   const uint32_t vnonsign_mask = params->scalar_bitcast.nonsign_mask;
29   const uint32_t vexp_bias = params->scalar_bitcast.exp_bias;
30   const float vscale_to_inf = params->scalar_bitcast.scale_to_inf;
31   const uint32_t vexpw_max = params->scalar_bitcast.expw_max;
32   const float vscale_to_zero = params->scalar_bitcast.scale_to_zero;
33   const uint32_t vbias_min = params->scalar_bitcast.bias_min;
34   const uint16_t vexph_mask = params->scalar_bitcast.exph_mask;
35   const uint16_t vmanth_mask = params->scalar_bitcast.manth_mask;
36   const uint16_t vnanh = params->scalar_bitcast.nanh;
37 
38   const uint32_t* i = (const uint32_t*) input;
39   uint16_t* o = (uint16_t*) output;
40   for (; n >= 4 * sizeof(float); n -= 4 * sizeof(float)) {
41     const uint32_t vw0 = i[0];
42     const uint32_t vw1 = i[1];
43     const uint32_t vw2 = i[2];
44     const uint32_t vw3 = i[3];
45     i += 4;
46 
47     const uint32_t vnonsignw0 = vw0 & vnonsign_mask;
48     const uint32_t vnonsignw1 = vw1 & vnonsign_mask;
49     const uint32_t vnonsignw2 = vw2 & vnonsign_mask;
50     const uint32_t vnonsignw3 = vw3 & vnonsign_mask;
51 
52     float vf0 = uint32_as_float(vnonsignw0);
53     float vf1 = uint32_as_float(vnonsignw1);
54     float vf2 = uint32_as_float(vnonsignw2);
55     float vf3 = uint32_as_float(vnonsignw3);
56     const uint32_t vsignw0 = vw0 ^ vnonsignw0;
57     const uint32_t vsignw1 = vw1 ^ vnonsignw1;
58     const uint32_t vsignw2 = vw2 ^ vnonsignw2;
59     const uint32_t vsignw3 = vw3 ^ vnonsignw3;
60     uint32_t vbias0 = vnonsignw0 + vexp_bias;
61     uint32_t vbias1 = vnonsignw1 + vexp_bias;
62     uint32_t vbias2 = vnonsignw2 + vexp_bias;
63     uint32_t vbias3 = vnonsignw3 + vexp_bias;
64 
65     vf0 *= vscale_to_inf;
66     vf1 *= vscale_to_inf;
67     vf2 *= vscale_to_inf;
68     vf3 *= vscale_to_inf;
69     vbias0 &= vexpw_max;
70     vbias1 &= vexpw_max;
71     vbias2 &= vexpw_max;
72     vbias3 &= vexpw_max;
73 
74     vf0 *= vscale_to_zero;
75     vf1 *= vscale_to_zero;
76     vf2 *= vscale_to_zero;
77     vf3 *= vscale_to_zero;
78     vbias0 = math_max_u32(vbias0, vbias_min);
79     vbias1 = math_max_u32(vbias1, vbias_min);
80     vbias2 = math_max_u32(vbias2, vbias_min);
81     vbias3 = math_max_u32(vbias3, vbias_min);
82 
83     vf0 += uint32_as_float(vbias0);
84     vf1 += uint32_as_float(vbias1);
85     vf2 += uint32_as_float(vbias2);
86     vf3 += uint32_as_float(vbias3);
87 
88     const uint32_t vbits0 = float_as_uint32(vf0);
89     const uint32_t vbits1 = float_as_uint32(vf1);
90     const uint32_t vbits2 = float_as_uint32(vf2);
91     const uint32_t vbits3 = float_as_uint32(vf3);
92 
93     const uint16_t vexph0 = (uint16_t) (vbits0 >> 13) & vexph_mask;
94     const uint16_t vexph1 = (uint16_t) (vbits1 >> 13) & vexph_mask;
95     const uint16_t vexph2 = (uint16_t) (vbits2 >> 13) & vexph_mask;
96     const uint16_t vexph3 = (uint16_t) (vbits3 >> 13) & vexph_mask;
97     const uint16_t vmanth0 = (uint16_t) vbits0 & vmanth_mask;
98     const uint16_t vmanth1 = (uint16_t) vbits1 & vmanth_mask;
99     const uint16_t vmanth2 = (uint16_t) vbits2 & vmanth_mask;
100     const uint16_t vmanth3 = (uint16_t) vbits3 & vmanth_mask;
101     const uint16_t vsignh0 = (uint16_t) (vsignw0 >> 16);
102     const uint16_t vsignh1 = (uint16_t) (vsignw1 >> 16);
103     const uint16_t vsignh2 = (uint16_t) (vsignw2 >> 16);
104     const uint16_t vsignh3 = (uint16_t) (vsignw3 >> 16);
105 
106     uint16_t vh0 = vexph0 + vmanth0;
107     uint16_t vh1 = vexph1 + vmanth1;
108     uint16_t vh2 = vexph2 + vmanth2;
109     uint16_t vh3 = vexph3 + vmanth3;
110     if XNN_UNPREDICTABLE(vnonsignw0 > vexpw_max) {
111       vh0 = vnanh;
112     }
113     if XNN_UNPREDICTABLE(vnonsignw1 > vexpw_max) {
114       vh1 = vnanh;
115     }
116     if XNN_UNPREDICTABLE(vnonsignw2 > vexpw_max) {
117       vh2 = vnanh;
118     }
119     if XNN_UNPREDICTABLE(vnonsignw3 > vexpw_max) {
120       vh3 = vnanh;
121     }
122     vh0 |= vsignh0;
123     vh1 |= vsignh1;
124     vh2 |= vsignh2;
125     vh3 |= vsignh3;
126 
127     o[0] = vh0;
128     o[1] = vh1;
129     o[2] = vh2;
130     o[3] = vh3;
131     o += 4;
132   }
133   if XNN_UNLIKELY(n != 0) {
134     do {
135       const uint32_t vw = *i++;
136 
137       const uint32_t vnonsignw = vw & vnonsign_mask;
138 
139       float vf = uint32_as_float(vnonsignw);
140       const uint32_t vsignw = vw ^ vnonsignw;
141       uint32_t vbias = vnonsignw + vexp_bias;
142 
143       vf *= vscale_to_inf;
144       vbias &= vexpw_max;
145 
146       vf *= vscale_to_zero;
147       vbias = math_max_u32(vbias, vbias_min);
148 
149       vf += uint32_as_float(vbias);
150 
151       const uint32_t vbits = float_as_uint32(vf);
152 
153       const uint16_t vexph = (uint16_t) (vbits >> 13) & vexph_mask;
154       const uint16_t vmanth = (uint16_t) vbits & vmanth_mask;
155       const uint16_t vsignh = (uint16_t) (vsignw >> 16);
156 
157       uint16_t vh = vexph + vmanth;
158       if XNN_UNPREDICTABLE(vnonsignw > vexpw_max) {
159         vh = vnanh;
160       }
161       vh |= vsignh;
162 
163       *o++ = vh;
164 
165       n -= sizeof(float);
166     } while (n != 0);
167   }
168 }
169