• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <fp16.h>
17 
18 
xnn_f32_f16_vcvt_ukernel__scalar_bitcast_x3(size_t n,const float * input,void * output,const union xnn_f32_f16_cvt_params params[restrict XNN_MIN_ELEMENTS (1)])19 void xnn_f32_f16_vcvt_ukernel__scalar_bitcast_x3(
20     size_t n,
21     const float* input,
22     void* output,
23     const union xnn_f32_f16_cvt_params params[restrict XNN_MIN_ELEMENTS(1)])
24 {
25   assert(n != 0);
26   assert(n % sizeof(float) == 0);
27   assert(input != NULL);
28   assert(output != NULL);
29 
30   const uint32_t vnonsign_mask = params->scalar_bitcast.nonsign_mask;
31   const uint32_t vexp_bias = params->scalar_bitcast.exp_bias;
32   const float vscale_to_inf = params->scalar_bitcast.scale_to_inf;
33   const uint32_t vexpw_max = params->scalar_bitcast.expw_max;
34   const float vscale_to_zero = params->scalar_bitcast.scale_to_zero;
35   const uint32_t vbias_min = params->scalar_bitcast.bias_min;
36   const uint16_t vexph_mask = params->scalar_bitcast.exph_mask;
37   const uint16_t vmanth_mask = params->scalar_bitcast.manth_mask;
38   const uint16_t vnanh = params->scalar_bitcast.nanh;
39 
40   const uint32_t* i = (const uint32_t*) input;
41   uint16_t* o = (uint16_t*) output;
42   for (; n >= 3 * sizeof(float); n -= 3 * sizeof(float)) {
43     const uint32_t vw0 = i[0];
44     const uint32_t vw1 = i[1];
45     const uint32_t vw2 = i[2];
46     i += 3;
47 
48     const uint32_t vnonsignw0 = vw0 & vnonsign_mask;
49     const uint32_t vnonsignw1 = vw1 & vnonsign_mask;
50     const uint32_t vnonsignw2 = vw2 & vnonsign_mask;
51 
52     float vf0 = fp32_from_bits(vnonsignw0);
53     float vf1 = fp32_from_bits(vnonsignw1);
54     float vf2 = fp32_from_bits(vnonsignw2);
55     const uint32_t vsignw0 = vw0 ^ vnonsignw0;
56     const uint32_t vsignw1 = vw1 ^ vnonsignw1;
57     const uint32_t vsignw2 = vw2 ^ vnonsignw2;
58     uint32_t vbias0 = vnonsignw0 + vexp_bias;
59     uint32_t vbias1 = vnonsignw1 + vexp_bias;
60     uint32_t vbias2 = vnonsignw2 + vexp_bias;
61 
62     vf0 *= vscale_to_inf;
63     vf1 *= vscale_to_inf;
64     vf2 *= vscale_to_inf;
65     vbias0 &= vexpw_max;
66     vbias1 &= vexpw_max;
67     vbias2 &= vexpw_max;
68 
69     vf0 *= vscale_to_zero;
70     vf1 *= vscale_to_zero;
71     vf2 *= vscale_to_zero;
72     vbias0 = math_max_u32(vbias0, vbias_min);
73     vbias1 = math_max_u32(vbias1, vbias_min);
74     vbias2 = math_max_u32(vbias2, vbias_min);
75 
76     vf0 += fp32_from_bits(vbias0);
77     vf1 += fp32_from_bits(vbias1);
78     vf2 += fp32_from_bits(vbias2);
79 
80     const uint32_t vbits0 = fp32_to_bits(vf0);
81     const uint32_t vbits1 = fp32_to_bits(vf1);
82     const uint32_t vbits2 = fp32_to_bits(vf2);
83 
84     const uint16_t vexph0 = (uint16_t) (vbits0 >> 13) & vexph_mask;
85     const uint16_t vexph1 = (uint16_t) (vbits1 >> 13) & vexph_mask;
86     const uint16_t vexph2 = (uint16_t) (vbits2 >> 13) & vexph_mask;
87     const uint16_t vmanth0 = (uint16_t) vbits0 & vmanth_mask;
88     const uint16_t vmanth1 = (uint16_t) vbits1 & vmanth_mask;
89     const uint16_t vmanth2 = (uint16_t) vbits2 & vmanth_mask;
90     const uint16_t vsignh0 = (uint16_t) (vsignw0 >> 16);
91     const uint16_t vsignh1 = (uint16_t) (vsignw1 >> 16);
92     const uint16_t vsignh2 = (uint16_t) (vsignw2 >> 16);
93 
94     uint16_t vh0 = vexph0 + vmanth0;
95     uint16_t vh1 = vexph1 + vmanth1;
96     uint16_t vh2 = vexph2 + vmanth2;
97     if XNN_UNPREDICTABLE(vnonsignw0 > vexpw_max) {
98       vh0 = vnanh;
99     }
100     if XNN_UNPREDICTABLE(vnonsignw1 > vexpw_max) {
101       vh1 = vnanh;
102     }
103     if XNN_UNPREDICTABLE(vnonsignw2 > vexpw_max) {
104       vh2 = vnanh;
105     }
106     vh0 |= vsignh0;
107     vh1 |= vsignh1;
108     vh2 |= vsignh2;
109 
110     o[0] = vh0;
111     o[1] = vh1;
112     o[2] = vh2;
113     o += 3;
114   }
115   if XNN_UNLIKELY(n != 0) {
116     do {
117       const uint32_t vw = *i++;
118 
119       const uint32_t vnonsignw = vw & vnonsign_mask;
120 
121       float vf = fp32_from_bits(vnonsignw);
122       const uint32_t vsignw = vw ^ vnonsignw;
123       uint32_t vbias = vnonsignw + vexp_bias;
124 
125       vf *= vscale_to_inf;
126       vbias &= vexpw_max;
127 
128       vf *= vscale_to_zero;
129       vbias = math_max_u32(vbias, vbias_min);
130 
131       vf += fp32_from_bits(vbias);
132 
133       const uint32_t vbits = fp32_to_bits(vf);
134 
135       const uint16_t vexph = (uint16_t) (vbits >> 13) & vexph_mask;
136       const uint16_t vmanth = (uint16_t) vbits & vmanth_mask;
137       const uint16_t vsignh = (uint16_t) (vsignw >> 16);
138 
139       uint16_t vh = vexph + vmanth;
140       if XNN_UNPREDICTABLE(vnonsignw > vexpw_max) {
141         vh = vnanh;
142       }
143       vh |= vsignh;
144 
145       *o++ = vh;
146 
147       n -= sizeof(float);
148     } while (n != 0);
149   }
150 }
151