shithub: mc

ref: 96641430b332facfacc55f6060d0425886f35697
dir: /lib/math/round-impl.myr/

View raw version
use std

use "util"

pkg math =
	const rn64 : (x : flt64 -> int64)
	const rn32 : (x : flt32 -> int32)
;;

const rn64 = {x : flt64
	var n : bool, e : int64, s : uint64

	(n, e, s) = std.flt64explode(x)

	if e >= 63
		-> -9223372036854775808
	elif e >= 52
		var shifted : int64 = (( s << (e - 52 : uint64)) : int64)
		if n
			-> -1 * shifted
		else
			-> shifted
		;;
	elif e < -1
		-> 0
	;;

	var integral_s = (s >> (52 - e : uint64) : int64)

	if need_round_away(0, s, 52 - e)
		integral_s++
	;;

	if n
		-> -integral_s
	else
		-> integral_s
	;;
}

const rn32 = {x : flt32
	var n : bool, e : int32, s : uint32

	(n, e, s) = std.flt32explode(x)

	if e >= 31
		-> -2147483648
	elif e >= 23
		var shifted : int32 = (( s << (e - 23 : uint32)) : int32)
		if n
			->  -1 * shifted
		else
			->  shifted
		;;
	elif e < -1
		-> 0
	;;

	var integral_s = (s >> (23 - e : uint32) : int32)

	if need_round_away(0, (s : uint64), (23 - e : int64))
		integral_s++
	;;

	if n
		-> -integral_s
	else
		-> integral_s
	;;
}