// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package runtime

import 

const (
	float64Mask  = 0x7FF
	float64Shift = 64 - 11 - 1
	float64Bias  = 1023
)

var inf = float64frombits(0x7FF0000000000000)

// isNaN reports whether f is an IEEE 754 “not-a-number” value.
func isNaN( float64) ( bool) {
	// IEEE 754 says that only NaNs satisfy f != f.
	return  != 
}

// isFinite reports whether f is neither NaN nor an infinity.
func isFinite( float64) bool {
	return !isNaN( - )
}

// isInf reports whether f is an infinity.
func isInf( float64) bool {
	return !isNaN() && !isFinite()
}

// abs returns the absolute value of x.
//
// Special cases are:
//
//	abs(±Inf) = +Inf
//	abs(NaN) = NaN
func abs( float64) float64 {
	const  = 1 << 63
	return float64frombits(float64bits() &^ )
}

// copysign returns a value with the magnitude
// of x and the sign of y.
func copysign(,  float64) float64 {
	const  = 1 << 63
	return float64frombits(float64bits()&^ | float64bits()&)
}

// float64bits returns the IEEE 754 binary representation of f.
func float64bits( float64) uint64 {
	return *(*uint64)(unsafe.Pointer(&))
}

// float64frombits returns the floating point number corresponding
// the IEEE 754 binary representation b.
func float64frombits( uint64) float64 {
	return *(*float64)(unsafe.Pointer(&))
}

// floor returns the greatest integer value less than or equal to x.
//
// Special cases are:
//
//	floor(±0) = ±0
//	floor(±Inf) = ±Inf
//	floor(NaN) = NaN
//
// N.B. Portable floor copied from math. math also has optimized arch-specific
// implementations.
func floor( float64) float64 {
	if  == 0 || isNaN() || isInf() {
		return 
	}
	if  < 0 {
		,  := modf(-)
		if  != 0.0 {
			 =  + 1
		}
		return -
	}
	,  := modf()
	return 
}

// ceil returns the least integer value greater than or equal to x.
//
// Special cases are:
//
//	Ceil(±0) = ±0
//	Ceil(±Inf) = ±Inf
//	Ceil(NaN) = NaN
//
// N.B. Portable ceil copied from math. math also has optimized arch-specific
// implementations.
func ceil( float64) float64 {
	return -floor(-)
}

// modf returns integer and fractional floating-point numbers
// that sum to f. Both values have the same sign as f.
//
// Special cases are:
//
//	Modf(±Inf) = ±Inf, NaN
//	Modf(NaN) = NaN, NaN
//
// N.B. Portable modf copied from math. math also has optimized arch-specific
// implementations.
func modf( float64) ( float64,  float64) {
	if  < 1 {
		switch {
		case  < 0:
			,  = (-)
			return -, -
		case  == 0:
			return ,  // Return -0, -0 when f == -0
		}
		return 0, 
	}

	 := float64bits()
	 := uint(>>float64Shift)&float64Mask - float64Bias

	// Keep the top 12+e bits, the integer part; clear the rest.
	if  < 64-12 {
		 &^= 1<<(64-12-) - 1
	}
	 = float64frombits()
	 =  - 
	return
}