'Convert ieee754 half-precision bytes to double and vise versa in Flutter
I have a device that provides temperature data in ieee754 half-precision float format, i.e. [78, 100] = +25.5C
.
Now, Dart/Flutter doesn't support HP-Float conversions out of the box. After googling around, I have found several solutions that I was able to put together into one that seems to be working fine. Having not done this for many years I am asking pro's to look this over. Also, I'm sure this will save some time to folks like me who need this functionality. This has been tested in temperatures from -10C to +35C and seems to convert correctly both ways. Here _ieee754HpBytesToDouble
converts HPF bytes to 64-bit double and _ieee754HpBytesFromDouble
converts 64-bit double to half-precision bytes.
///
/// Double to Uint8List
///
Uint8List _ieee754HpBytesFromDouble(double fval) {
int result = _doubleToBits(fval);
Uint8List beef = _int32bytes(result);
return Uint8List.fromList(beef.reversed.skip(2).toList());
}
///
/// Double to hp-float bits
///
int _doubleToBits(double fval) {
ByteData bdata = ByteData(8);
bdata.setFloat32(0, fval);
int fbits = bdata.getInt32(0);
int sign = fbits >> 16 & 0x8000;
int val = (fbits & 0x7fffffff) + 0x1000;
if (val >= 0x47800000) {
if ((fbits & 0x7fffffff) >= 0x47800000) {
if (val < 0x7f800000) return sign | 0x7c00;
return sign | 0x7c00 | (fbits & 0x007fffff) >> 13;
}
return sign | 0x7bff;
}
if (val >= 0x38800000) return sign | val - 0x38000000 >> 13;
if (val < 0x33000000) return sign;
val = (fbits & 0x7fffffff) >> 23;
return sign |
((fbits & 0x7fffff | 0x800000) + (0x800000 >> val - 102) >> 126 - val);
}
///
///
///
Uint8List _int32bytes(int value) =>
Uint8List(4)..buffer.asInt32List()[0] = value;
///
///
///
double _bitsToDouble(int bits) {
Uint8List list = _int32bytes(bits);
ByteBuffer buffer = new Int8List.fromList(list.reversed.toList()).buffer;
ByteData byteData = new ByteData.view(buffer);
double result = byteData.getFloat32(0);
return result;
}
///
///
///
double _ieee754HpBytesToDouble(List<int> i) {
int hbits = i[0] * 256 + i[1];
int mant = hbits & 0x03ff;
int exp = hbits & 0x7c00;
if (exp == 0x7c00)
exp = 0x3fc00;
else if (exp != 0) {
exp += 0x1c000;
if (mant == 0 && exp > 0x1c400) {
return _bitsToDouble((hbits & 0x8000) << 16 | exp << 13 | 0x3ff);
}
} else if (mant != 0) {
exp = 0x1c400;
do {
mant <<= 1;
exp -= 0x400;
} while ((mant & 0x400) == 0);
mant &= 0x3ff;
}
return _bitsToDouble((hbits & 0x8000) << 16 | (exp | mant) << 13);
}
Solution 1:[1]
For FP64<->FP32 conversion, use standard casting and for FP32<->FP16 conversion use these ultra-efficient conversion algorithms:
double half_to_double(const ushort x) { // IEEE-754 16-bit floating-point format (without infinity): 1-5-10, exp-15, +-131008.0, +-6.1035156E-5, +-5.9604645E-8, 3.311 digits
return (double)half_to_float(x);
}
ushort double_to_half(const double x) { // IEEE-754 16-bit floating-point format (without infinity): 1-5-10, exp-15, +-131008.0, +-6.1035156E-5, +-5.9604645E-8, 3.311 digits
return float_to_half((float)x);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ProjectPhysX |