shithub: mc

ref: 51d5a41b3f09a15fef2f427a129f877365d10bf6
dir: /libstd/floatbits.myr/

View raw version
pkg std =
	const float64bits	: (flt : float64 -> uint64)
	const float32bits	: (flt : float32 -> uint32)
	const float64frombits	: (bits : uint64 -> float64)
	const float32frombits	: (bits : uint32 -> float32)
	const float64explode	: (flt : float64 -> (bool, uint64, int32))
	const float32explode	: (flt : float64 -> (bool, uint64, int32))
;;

const float64bits	= {flt;	-> (&flt castto(uint64#))#}
const float32bits	= {flt;	-> (&flt castto(uint32#))#}
const float64frombits	= {bits;	-> (&bits castto(float64#))#}
const float32frombits	= {bits;	-> (&bits castto(float32#))#}

const float64explode = {flt
	var bits, isneg, mant, exp

	bits = float64bits(flt)
	isneg = (bits >> 63) == 0  	/* msb is sign bit */
	exp = (bits >> 52) & 0x7ff 	/* exp is in bits [52..63] */
	mant = bits & ((1ul << 52) - 1) /* msb is in bits [..51] */

	/* add back the implicit bit if this is not a denormal */
	if exp != 0
		mant |= 1ul << 52
	else
		exp = 1
	;;
	/*
	   adjust for exponent bias. nb: because we are
	   treating the mantissa as m.0 instead of 0.m,
	   our exponent bias needs to be offset by the
	   size of m
	*/
	-> (isneg, mant, (exp castto(int32)) - 1075)
}

const float32explode = {flt
	var bits, isneg, mant, exp

	bits = float64bits(flt) castto(uint64)
	isneg = (bits >> 31) == 0  	/* msb is sign bit */
	exp = (bits >> 22) & 0xff 	/* exp is in bits [23..30] */
	mant = bits & ((1ul << 52) - 1) /* msb is in bits [0..22] */

	/* add back the implicit bit if this is not a denormal */
	if exp != 0
		mant |= 1ul << 22
	else
		exp = 1
	;;
	/*
	   adjust for exponent bias. nb: because we are
	   treating the mantissa as m.0 instead of 0.m,
	   our exponent bias needs to be offset by the
	   size of m
	*/
	-> (isneg, mant, (exp castto(int32)) - 149)
}