getHalfPrecisionDouble function

double getHalfPrecisionDouble(
  1. int val
)

Numeric handling support functions. Float handling support functions. Gets a half precision float from its int value.

Implementation

/// Float handling support functions.

/// Gets a half precision float from its int
/// value.
double getHalfPrecisionDouble(int val) {
  var t1 = val & 0x7fff; // Non-sign bits
  var t2 = val & 0x8000; // Sign bit
  final t3 = val & 0x7c00; // Exponent
  t1 <<= 13; // Align mantissa on MSB
  t2 <<= 16; // Shift sign bit into position
  t1 += 0x38000000; // Adjust bias
  t1 = t3 == 0 ? 0 : t1; // Denormalise as zero
  t1 |= t2; // re-insert sign bit
  final tmp = <int>[];
  tmp.add((t1 >> 24) & 0xff);
  tmp.add((t1 >> 16) & 0xff);
  tmp.add((t1 >> 8) & 0xff);
  tmp.add(t1 & 0xff);
  final buff = typed.Uint8Buffer();
  buff.addAll(tmp);
  final bdata = ByteData.view(buff.buffer);
  final ret = bdata.getFloat32(0);
  return ret;
}