float32ToFloat16 function
Converts a float32 to float16 representation (as int).
Implementation
int float32ToFloat16(double value) {
final Float32List float32Buffer = Float32List(1);
final Uint32List int32Buffer = float32Buffer.buffer.asUint32List();
float32Buffer[0] = value;
int f = int32Buffer[0];
int sign = (f >> 16) & 0x8000;
int exponent = (f >> 23) & 0xFF;
int mantissa = f & 0x007FFFFF;
if (exponent == 0) return sign;
if (exponent == 255) return sign | 0x7C00;
exponent = exponent - 127 + 15;
if (exponent >= 31) return sign | 0x7C00;
if (exponent <= 0) return sign;
int roundMantissa = (mantissa >> 13) + ((mantissa >> 12) & 1);
return sign | (exponent << 10) | roundMantissa;
}