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_x3(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_x3(
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 >= 3 * sizeof(float); n -= 3 * sizeof(float)) {
41 const uint32_t vw0 = i[0];
42 const uint32_t vw1 = i[1];
43 const uint32_t vw2 = i[2];
44 i += 3;
45
46 const uint32_t vnonsignw0 = vw0 & vnonsign_mask;
47 const uint32_t vnonsignw1 = vw1 & vnonsign_mask;
48 const uint32_t vnonsignw2 = vw2 & vnonsign_mask;
49
50 float vf0 = uint32_as_float(vnonsignw0);
51 float vf1 = uint32_as_float(vnonsignw1);
52 float vf2 = uint32_as_float(vnonsignw2);
53 const uint32_t vsignw0 = vw0 ^ vnonsignw0;
54 const uint32_t vsignw1 = vw1 ^ vnonsignw1;
55 const uint32_t vsignw2 = vw2 ^ vnonsignw2;
56 uint32_t vbias0 = vnonsignw0 + vexp_bias;
57 uint32_t vbias1 = vnonsignw1 + vexp_bias;
58 uint32_t vbias2 = vnonsignw2 + vexp_bias;
59
60 vf0 *= vscale_to_inf;
61 vf1 *= vscale_to_inf;
62 vf2 *= vscale_to_inf;
63 vbias0 &= vexpw_max;
64 vbias1 &= vexpw_max;
65 vbias2 &= vexpw_max;
66
67 vf0 *= vscale_to_zero;
68 vf1 *= vscale_to_zero;
69 vf2 *= vscale_to_zero;
70 vbias0 = math_max_u32(vbias0, vbias_min);
71 vbias1 = math_max_u32(vbias1, vbias_min);
72 vbias2 = math_max_u32(vbias2, vbias_min);
73
74 vf0 += uint32_as_float(vbias0);
75 vf1 += uint32_as_float(vbias1);
76 vf2 += uint32_as_float(vbias2);
77
78 const uint32_t vbits0 = float_as_uint32(vf0);
79 const uint32_t vbits1 = float_as_uint32(vf1);
80 const uint32_t vbits2 = float_as_uint32(vf2);
81
82 const uint16_t vexph0 = (uint16_t) (vbits0 >> 13) & vexph_mask;
83 const uint16_t vexph1 = (uint16_t) (vbits1 >> 13) & vexph_mask;
84 const uint16_t vexph2 = (uint16_t) (vbits2 >> 13) & vexph_mask;
85 const uint16_t vmanth0 = (uint16_t) vbits0 & vmanth_mask;
86 const uint16_t vmanth1 = (uint16_t) vbits1 & vmanth_mask;
87 const uint16_t vmanth2 = (uint16_t) vbits2 & vmanth_mask;
88 const uint16_t vsignh0 = (uint16_t) (vsignw0 >> 16);
89 const uint16_t vsignh1 = (uint16_t) (vsignw1 >> 16);
90 const uint16_t vsignh2 = (uint16_t) (vsignw2 >> 16);
91
92 uint16_t vh0 = vexph0 + vmanth0;
93 uint16_t vh1 = vexph1 + vmanth1;
94 uint16_t vh2 = vexph2 + vmanth2;
95 if XNN_UNPREDICTABLE(vnonsignw0 > vexpw_max) {
96 vh0 = vnanh;
97 }
98 if XNN_UNPREDICTABLE(vnonsignw1 > vexpw_max) {
99 vh1 = vnanh;
100 }
101 if XNN_UNPREDICTABLE(vnonsignw2 > vexpw_max) {
102 vh2 = vnanh;
103 }
104 vh0 |= vsignh0;
105 vh1 |= vsignh1;
106 vh2 |= vsignh2;
107
108 o[0] = vh0;
109 o[1] = vh1;
110 o[2] = vh2;
111 o += 3;
112 }
113 if XNN_UNLIKELY(n != 0) {
114 do {
115 const uint32_t vw = *i++;
116
117 const uint32_t vnonsignw = vw & vnonsign_mask;
118
119 float vf = uint32_as_float(vnonsignw);
120 const uint32_t vsignw = vw ^ vnonsignw;
121 uint32_t vbias = vnonsignw + vexp_bias;
122
123 vf *= vscale_to_inf;
124 vbias &= vexpw_max;
125
126 vf *= vscale_to_zero;
127 vbias = math_max_u32(vbias, vbias_min);
128
129 vf += uint32_as_float(vbias);
130
131 const uint32_t vbits = float_as_uint32(vf);
132
133 const uint16_t vexph = (uint16_t) (vbits >> 13) & vexph_mask;
134 const uint16_t vmanth = (uint16_t) vbits & vmanth_mask;
135 const uint16_t vsignh = (uint16_t) (vsignw >> 16);
136
137 uint16_t vh = vexph + vmanth;
138 if XNN_UNPREDICTABLE(vnonsignw > vexpw_max) {
139 vh = vnanh;
140 }
141 vh |= vsignh;
142
143 *o++ = vh;
144
145 n -= sizeof(float);
146 } while (n != 0);
147 }
148 }
149