15 float fp16_to_fp32(fp16_int_t h)
17 fp32 magic; magic.u = 113 << 23;
18 unsigned int shifted_exp = 0x7c00 << 13; // exponent mask after shift
22 unsigned int shifted = (h.val & 0x7fff) << 13;
23 unsigned int exponent = shifted & shifted_exp;
27 if (exponent == 0) { // Zero / Denormal
30 } else if (exponent == shifted_exp) { // Inf/NaN
31 o.u += (255 - 31) << 23;
33 o.u += (127 - 15) << 23;
36 o.u |= (h.val & 0x8000) << 16; // copy sign bit
40 fp16_int_t fp32_to_fp16(float x)
43 unsigned int f32infty = 255 << 23;
44 unsigned int f16max = (127 + 16) << 23;
45 fp32 denorm_magic; denorm_magic.u = ((127 - 15) + (23 - 10) + 1) << 23;
46 unsigned int sign_mask = 0x80000000u;
49 unsigned int sign = f.u & sign_mask;
52 // NOTE all the integer compares in this function can be safely
53 // compiled into signed compares since all operands are below
54 // 0x80000000. Important if you want fast straight SSE2 code
55 // (since there's no unsigned PCMPGTD).
57 if (f.u >= f16max) { // result is Inf or NaN (all exponent bits set)
58 o.val = (f.u > f32infty) ? 0x7e00 : 0x7c00; // NaN->qNaN and Inf->Inf
59 } else { // (De)normalized number or zero
60 if (f.u < (113 << 23)) { // resulting FP16 is subnormal or zero
61 // use a magic value to align our 10 mantissa bits at the bottom of
62 // the float. as long as FP addition is round-to-nearest-even this
64 f.f += denorm_magic.f;
66 // and one integer subtract of the bias later, we have our final float!
67 o.val = f.u - denorm_magic.u;
69 unsigned int mant_odd = (f.u >> 13) & 1; // resulting mantissa is odd
71 // update exponent, rounding bias part 1
72 f.u += ((15 - 127) << 23) + 0xfff;
73 // rounding bias part 2