DoubleToHalf static method

int DoubleToHalf(
  1. num n
)

Implementation

static int DoubleToHalf(num n) {
  if (_toFloatFloat32 == null) {
    _initialize();
  }

  final f = n.toDouble();
  final x_i = float32ToUint32(f);
  if (f == 0.0) {
    // Common special case - zero.
    // Preserve the zero's sign bit.
    return x_i >> 16;
  }

  // We extract the combined sign and exponent, e, from our
  // floating-point number, f. Then we convert e to the sign
  // and exponent of the half number via a table lookup.
  //
  // For the most common case, where a normalized half is produced,
  // the table lookup returns a non-zero value; in this case, all
  // we have to do is round f's significand to 10 bits and combine
  // the result with e.
  //
  // For all other cases (overflow, zeroes, denormalized numbers
  // resulting from underflow, infinities and NANs), the table
  // lookup returns zero, and we call a longer, non-inline function
  // to do the float-to-half conversion.
  var e = (x_i >> 23) & 0x000001ff;

  e = _eLut[e];

  if (e != 0) {
    // Simple case - round the significand, m, to 10
    // bits and combine it with the sign and exponent.
    final m = x_i & 0x007fffff;
    return e + ((m + 0x00000fff + ((m >> 13) & 1)) >> 13);
  }

  // Difficult case - call a function.
  return _convert(x_i);
}