Example #1
0
func tc64(f float64) float64 {
	if runtime.GOOS == "nacl" {
		return hx.CallFloat("", "Go_haxegoruntime_FFloat64frombits.hx", 1,
			hx.CallDynamic("", "Go_haxegoruntime_FFloat64bits.hx", 1, f))
	}
	return f
}
Example #2
0
// Float64bits returns the IEEE 754 binary representation of f.
//func Float64bits(f float64) uint64 { return *(*uint64)(unsafe.Pointer(&f)) }
func Float64bits(f float64) uint64 {

	if hx.GetBool("", "Object.nativeFloats") {
		var t float64 = f
		return *(*uint64)(unsafe.Pointer(&t))
	}

	switch runtime.GOARCH {
	case "cs":
		return uint64(hx.Int64(hx.CallDynamic("", "Force.Float64bits", 1, f)))
		// TODO js/cpp/neko short-cut - using Force.f64byts
		//case "js", "cpp", "neko":
		//	return uint64(hx.Int64(hx.CallDynamic("", "Force.Float64bits", 1, f)))
	}

	// below from math.IsInf
	if f > MaxFloat64 {
		return uvinf
	}
	if f < -MaxFloat64 {
		return uvneginf
	}
	if f != f { // NaN
		if f < 0 { // -NaN ?
			return uvnan | uint64(1)<<63
		}
		//return 9221120237041090561
		return uvnan
	}
	if f == 0 {
		if 1/f < -MaxFloat64 { // dividing by -0 gives -Inf
			return uint64(1) << 63
		}
		return 0
	}

	s := uint64(0)
	if f < hx.GetFloat("", "0") {
		s = 1 << 63
		f = -f
	}

	e := uint32(1023 + 52)
	for f >= float64(int64(1<<53)) {
		f /= hx.GetFloat("", "2")
		e++
		if e == uint32((1<<11)-1) {
			break
		}
	}
	for f < float64(int64(1<<52)) {
		e--
		if e == 0 {
			break
		}
		f *= hx.GetFloat("", "2")
	}

	return s | uint64(e)<<52 | (uint64(f) &^ (1 << 52))
}