MCPcopy Create free account
hub / github.com/numpy/numpy / FromFloatBits

Function FromFloatBits

numpy/core/src/common/half_private.hpp:14–138  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

12
13template<bool gen_overflow=true, bool gen_underflow=true, bool round_even=true>
14inline uint16_t FromFloatBits(uint32_t f)
15{
16 uint32_t f_exp, f_sig;
17 uint16_t h_sgn, h_exp, h_sig;
18
19 h_sgn = (uint16_t) ((f&0x80000000u) >> 16);
20 f_exp = (f&0x7f800000u);
21
22 /* Exponent overflow/NaN converts to signed inf/NaN */
23 if (f_exp >= 0x47800000u) {
24 if (f_exp == 0x7f800000u) {
25 /* Inf or NaN */
26 f_sig = (f&0x007fffffu);
27 if (f_sig != 0) {
28 /* NaN - propagate the flag in the significand... */
29 uint16_t ret = (uint16_t) (0x7c00u + (f_sig >> 13));
30 /* ...but make sure it stays a NaN */
31 if (ret == 0x7c00u) {
32 ret++;
33 }
34 return h_sgn + ret;
35 } else {
36 /* signed inf */
37 return (uint16_t) (h_sgn + 0x7c00u);
38 }
39 } else {
40 if constexpr (gen_overflow) {
41 /* overflow to signed inf */
42 FloatStatus::RaiseOverflow();
43 }
44 return (uint16_t) (h_sgn + 0x7c00u);
45 }
46 }
47
48 /* Exponent underflow converts to a subnormal half or signed zero */
49 if (f_exp <= 0x38000000u) {
50 /*
51 * Signed zeros, subnormal floats, and floats with small
52 * exponents all convert to signed zero half-floats.
53 */
54 if (f_exp < 0x33000000u) {
55 if constexpr (gen_underflow) {
56 /* If f != 0, it underflowed to 0 */
57 if ((f&0x7fffffff) != 0) {
58 FloatStatus::RaiseUnderflow();
59 }
60 }
61 return h_sgn;
62 }
63 /* Make the subnormal significand */
64 f_exp >>= 23;
65 f_sig = (0x00800000u + (f&0x007fffffu));
66 if constexpr (gen_underflow) {
67 /* If it's not exactly represented, it underflowed */
68 if ((f_sig&(((uint32_t)1 << (126 - f_exp)) - 1)) != 0) {
69 FloatStatus::RaiseUnderflow();
70 }
71 }

Callers 2

HalfMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected