|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <cstdint> |
|
typedef uint16_t npy_uint16; |
|
typedef uint32_t npy_uint32; |
|
typedef uint64_t npy_uint64; |
|
|
|
|
|
|
|
|
|
#define NPY_HALF_ROUND_TIES_TO_EVEN 1 |
|
|
|
|
|
|
|
|
|
#define NPY_HALF_GENERATE_OVERFLOW 0 |
|
#define NPY_HALF_GENERATE_UNDERFLOW 0 |
|
#define NPY_HALF_GENERATE_INVALID 0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static inline npy_uint16 npy_floatbits_to_halfbits(npy_uint32 f) |
|
{ |
|
npy_uint32 f_exp, f_sig; |
|
npy_uint16 h_sgn, h_exp, h_sig; |
|
|
|
h_sgn = (npy_uint16) ((f&0x80000000u) >> 16); |
|
f_exp = (f&0x7f800000u); |
|
|
|
|
|
if (f_exp >= 0x47800000u) { |
|
if (f_exp == 0x7f800000u) { |
|
|
|
f_sig = (f&0x007fffffu); |
|
if (f_sig != 0) { |
|
|
|
npy_uint16 ret = (npy_uint16) (0x7c00u + (f_sig >> 13)); |
|
|
|
if (ret == 0x7c00u) { |
|
ret++; |
|
} |
|
return h_sgn + ret; |
|
} else { |
|
|
|
return (npy_uint16) (h_sgn + 0x7c00u); |
|
} |
|
} else { |
|
|
|
#if NPY_HALF_GENERATE_OVERFLOW |
|
npy_set_floatstatus_overflow(); |
|
#endif |
|
return (npy_uint16) (h_sgn + 0x7c00u); |
|
} |
|
} |
|
|
|
|
|
if (f_exp <= 0x38000000u) { |
|
|
|
|
|
|
|
|
|
if (f_exp < 0x33000000u) { |
|
#if NPY_HALF_GENERATE_UNDERFLOW |
|
|
|
if ((f&0x7fffffff) != 0) { |
|
npy_set_floatstatus_underflow(); |
|
} |
|
#endif |
|
return h_sgn; |
|
} |
|
|
|
f_exp >>= 23; |
|
f_sig = (0x00800000u + (f&0x007fffffu)); |
|
#if NPY_HALF_GENERATE_UNDERFLOW |
|
|
|
if ((f_sig&(((npy_uint32)1 << (126 - f_exp)) - 1)) != 0) { |
|
npy_set_floatstatus_underflow(); |
|
} |
|
#endif |
|
f_sig >>= (113 - f_exp); |
|
|
|
#if NPY_HALF_ROUND_TIES_TO_EVEN |
|
|
|
|
|
|
|
|
|
|
|
if ((f_sig&0x00003fffu) != 0x00001000u) { |
|
f_sig += 0x00001000u; |
|
} |
|
#else |
|
f_sig += 0x00001000u; |
|
#endif |
|
h_sig = (npy_uint16) (f_sig >> 13); |
|
|
|
|
|
|
|
|
|
|
|
return (npy_uint16) (h_sgn + h_sig); |
|
} |
|
|
|
|
|
h_exp = (npy_uint16) ((f_exp - 0x38000000u) >> 13); |
|
|
|
f_sig = (f&0x007fffffu); |
|
#if NPY_HALF_ROUND_TIES_TO_EVEN |
|
|
|
|
|
|
|
|
|
|
|
if ((f_sig&0x00003fffu) != 0x00001000u) { |
|
f_sig += 0x00001000u; |
|
} |
|
#else |
|
f_sig += 0x00001000u; |
|
#endif |
|
h_sig = (npy_uint16) (f_sig >> 13); |
|
|
|
|
|
|
|
|
|
|
|
|
|
#if NPY_HALF_GENERATE_OVERFLOW |
|
h_sig += h_exp; |
|
if (h_sig == 0x7c00u) { |
|
npy_set_floatstatus_overflow(); |
|
} |
|
return h_sgn + h_sig; |
|
#else |
|
return h_sgn + h_exp + h_sig; |
|
#endif |
|
} |
|
|
|
static inline npy_uint16 npy_doublebits_to_halfbits(npy_uint64 d) |
|
{ |
|
npy_uint64 d_exp, d_sig; |
|
npy_uint16 h_sgn, h_exp, h_sig; |
|
|
|
h_sgn = (d&0x8000000000000000ULL) >> 48; |
|
d_exp = (d&0x7ff0000000000000ULL); |
|
|
|
|
|
if (d_exp >= 0x40f0000000000000ULL) { |
|
if (d_exp == 0x7ff0000000000000ULL) { |
|
|
|
d_sig = (d&0x000fffffffffffffULL); |
|
if (d_sig != 0) { |
|
|
|
npy_uint16 ret = (npy_uint16) (0x7c00u + (d_sig >> 42)); |
|
|
|
if (ret == 0x7c00u) { |
|
ret++; |
|
} |
|
return h_sgn + ret; |
|
} else { |
|
|
|
return h_sgn + 0x7c00u; |
|
} |
|
} else { |
|
|
|
#if NPY_HALF_GENERATE_OVERFLOW |
|
npy_set_floatstatus_overflow(); |
|
#endif |
|
return h_sgn + 0x7c00u; |
|
} |
|
} |
|
|
|
|
|
if (d_exp <= 0x3f00000000000000ULL) { |
|
|
|
|
|
|
|
|
|
if (d_exp < 0x3e60000000000000ULL) { |
|
#if NPY_HALF_GENERATE_UNDERFLOW |
|
|
|
if ((d&0x7fffffffffffffffULL) != 0) { |
|
npy_set_floatstatus_underflow(); |
|
} |
|
#endif |
|
return h_sgn; |
|
} |
|
|
|
d_exp >>= 52; |
|
d_sig = (0x0010000000000000ULL + (d&0x000fffffffffffffULL)); |
|
#if NPY_HALF_GENERATE_UNDERFLOW |
|
|
|
if ((d_sig&(((npy_uint64)1 << (1051 - d_exp)) - 1)) != 0) { |
|
npy_set_floatstatus_underflow(); |
|
} |
|
#endif |
|
d_sig >>= (1009 - d_exp); |
|
|
|
#if NPY_HALF_ROUND_TIES_TO_EVEN |
|
|
|
|
|
|
|
|
|
|
|
if ((d_sig&0x000007ffffffffffULL) != 0x0000020000000000ULL) { |
|
d_sig += 0x0000020000000000ULL; |
|
} |
|
#else |
|
d_sig += 0x0000020000000000ULL; |
|
#endif |
|
h_sig = (npy_uint16) (d_sig >> 42); |
|
|
|
|
|
|
|
|
|
|
|
return h_sgn + h_sig; |
|
} |
|
|
|
|
|
h_exp = (npy_uint16) ((d_exp - 0x3f00000000000000ULL) >> 42); |
|
|
|
d_sig = (d&0x000fffffffffffffULL); |
|
#if NPY_HALF_ROUND_TIES_TO_EVEN |
|
|
|
|
|
|
|
|
|
|
|
if ((d_sig&0x000007ffffffffffULL) != 0x0000020000000000ULL) { |
|
d_sig += 0x0000020000000000ULL; |
|
} |
|
#else |
|
d_sig += 0x0000020000000000ULL; |
|
#endif |
|
h_sig = (npy_uint16) (d_sig >> 42); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if NPY_HALF_GENERATE_OVERFLOW |
|
h_sig += h_exp; |
|
if (h_sig == 0x7c00u) { |
|
npy_set_floatstatus_overflow(); |
|
} |
|
return h_sgn + h_sig; |
|
#else |
|
return h_sgn + h_exp + h_sig; |
|
#endif |
|
} |
|
|
|
static inline npy_uint32 npy_halfbits_to_floatbits(npy_uint16 h) |
|
{ |
|
npy_uint16 h_exp, h_sig; |
|
npy_uint32 f_sgn, f_exp, f_sig; |
|
|
|
h_exp = (h&0x7c00u); |
|
f_sgn = ((npy_uint32)h&0x8000u) << 16; |
|
switch (h_exp) { |
|
case 0x0000u: |
|
h_sig = (h&0x03ffu); |
|
|
|
if (h_sig == 0) { |
|
return f_sgn; |
|
} |
|
|
|
h_sig <<= 1; |
|
while ((h_sig&0x0400u) == 0) { |
|
h_sig <<= 1; |
|
h_exp++; |
|
} |
|
f_exp = ((npy_uint32)(127 - 15 - h_exp)) << 23; |
|
f_sig = ((npy_uint32)(h_sig&0x03ffu)) << 13; |
|
return f_sgn + f_exp + f_sig; |
|
case 0x7c00u: |
|
|
|
return f_sgn + 0x7f800000u + (((npy_uint32)(h&0x03ffu)) << 13); |
|
default: |
|
|
|
return f_sgn + (((npy_uint32)(h&0x7fffu) + 0x1c000u) << 13); |
|
} |
|
} |
|
|
|
static inline npy_uint64 npy_halfbits_to_doublebits(npy_uint16 h) |
|
{ |
|
npy_uint16 h_exp, h_sig; |
|
npy_uint64 d_sgn, d_exp, d_sig; |
|
|
|
h_exp = (h&0x7c00u); |
|
d_sgn = ((npy_uint64)h&0x8000u) << 48; |
|
switch (h_exp) { |
|
case 0x0000u: |
|
h_sig = (h&0x03ffu); |
|
|
|
if (h_sig == 0) { |
|
return d_sgn; |
|
} |
|
|
|
h_sig <<= 1; |
|
while ((h_sig&0x0400u) == 0) { |
|
h_sig <<= 1; |
|
h_exp++; |
|
} |
|
d_exp = ((npy_uint64)(1023 - 15 - h_exp)) << 52; |
|
d_sig = ((npy_uint64)(h_sig&0x03ffu)) << 42; |
|
return d_sgn + d_exp + d_sig; |
|
case 0x7c00u: |
|
|
|
return d_sgn + 0x7ff0000000000000ULL + |
|
(((npy_uint64)(h&0x03ffu)) << 42); |
|
default: |
|
|
|
return d_sgn + (((npy_uint64)(h&0x7fffu) + 0xfc000u) << 42); |
|
} |
|
} |
|
|