This source file includes following definitions.
- halide_float16_bits_to_float
- halide_float16_bits_to_double
#include "HalideRuntime.h"
extern "C" {
WEAK float halide_float16_bits_to_float(uint16_t bits) {
uint32_t signMask = (bits & 0x8000) << 16;
union {
float asFloat;
uint32_t asUInt;
} result;
uint32_t significandBits = (bits & 0x3ff);
int exponent = ((bits & 0x7c00) >> 10);
exponent -= 15;
if (exponent == -15 && significandBits != 0) {
uint32_t newSignificand = significandBits;
int leadingZeros = __builtin_clz(significandBits);
int setMSB = 31 - leadingZeros;
newSignificand &= ~(1 << setMSB);
newSignificand <<= (23 - setMSB);
int newExponent = -24 + setMSB;
uint32_t reEncodedExponent = newExponent + 127;
result.asUInt = signMask | (reEncodedExponent << 23) | newSignificand;
} else {
significandBits <<= 13;
uint32_t reEncodedExponent;
if (exponent == -15) {
reEncodedExponent = 0x00;
} else if (exponent == 16) {
reEncodedExponent = 0xff;
} else {
reEncodedExponent = exponent + 127;
}
result.asUInt = signMask | (reEncodedExponent << 23) | significandBits;
}
return result.asFloat;
}
WEAK double halide_float16_bits_to_double(uint16_t bits) {
float valueAsFloat = halide_float16_bits_to_float(bits);
return (double) valueAsFloat;
}
}