MCPcopy Create free account
hub / github.com/apache/arrow / FromBinary16

Method FromBinary16

cpp/src/arrow/util/float16.cc:157–199  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

155// Converts a IEEE binary16 into a binary32/64
156template <typename T>
157T BinaryConverter<T>::FromBinary16(uint16_t h_bits) {
158 // Sign mask for output
159 const T f_sign = T(h_bits & 0x8000u) << (kNumBits - 16);
160
161 // Exponent mask for input binary16
162 const uint16_t h_exp = h_bits & 0x7c00;
163 // Mantissa mask for input binary16
164 const uint16_t h_mant = h_bits & 0x3ffu;
165
166 switch (h_exp) {
167 // Handle Inf and NaN
168 case 0x7c00u:
169 return f_sign | kExpMask | (T(h_mant) << (kMantNumBits - 10));
170 // Handle zeros and subnormals
171 case 0x0000u: {
172 // Input is +/-0
173 if (h_mant == 0) {
174 return f_sign;
175 }
176 // Subnormal binary16 to normal binary32/64
177 //
178 // Start with an f32/64-biased exponent of 2^-15. We then decrement it until the
179 // most significant set bit is left-shifted out - as it doesn't get explicitly
180 // stored in normalized floating point values. Instead, its existence is implied by
181 // the new exponent.
182 T f_exp = kExpBias - 15;
183 T f_mant = T(h_mant) << 1;
184 while ((f_mant & 0x0400u) == 0) {
185 --f_exp;
186 f_mant <<= 1;
187 }
188 f_exp <<= kMantNumBits;
189 f_mant = (f_mant & 0x03ffu) << (kMantNumBits - 10);
190 return f_sign | f_exp | f_mant;
191 } break;
192 // Handle normals
193 default:
194 // Equivalent to rebiasing the exponent and shifting everything by the remaining
195 // mantissa bits.
196 return f_sign |
197 ((T(h_bits & 0x7fffu) + (T(kExpBias - 15) << 10)) << (kMantNumBits - 10));
198 }
199}
200
201} // namespace
202

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected