| 76 | } |
| 77 | |
| 78 | FP32 half_to_float(FP16 h) |
| 79 | { |
| 80 | static const FP32 magic = { 113 << 23 }; |
| 81 | static const uint shifted_exp = 0x7c00 << 13; // exponent mask after shift |
| 82 | FP32 o; |
| 83 | |
| 84 | o.u = (h.u & 0x7fff) << 13; // exponent/mantissa bits |
| 85 | uint exp = shifted_exp & o.u; // just the exponent |
| 86 | o.u += (127 - 15) << 23; // exponent adjust |
| 87 | |
| 88 | // handle exponent special cases |
| 89 | if (exp == shifted_exp) // Inf/NaN? |
| 90 | o.u += (128 - 16) << 23; // extra exp adjust |
| 91 | else if (exp == 0) // Zero/Denormal? |
| 92 | { |
| 93 | o.u += 1 << 23; // extra exp adjust |
| 94 | o.f -= magic.f; // renormalize |
| 95 | } |
| 96 | |
| 97 | o.u |= (h.u & 0x8000) << 16; // sign bit |
| 98 | return o; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | float half_to_float(int16_t x) |