PR middle-end/53411
[official-gcc.git] / libgo / go / math / bits.go
blob1cf60ce7df29c49e7a1c9cb21563917255faf12a
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 package math
7 const (
8 uvnan = 0x7FF0000000000001
9 uvinf = 0x7FF0000000000000
10 uvneginf = 0xFFF0000000000000
11 mask = 0x7FF
12 shift = 64 - 11 - 1
13 bias = 1023
16 // Inf returns positive infinity if sign >= 0, negative infinity if sign < 0.
17 func Inf(sign int) float64 {
18 var v uint64
19 if sign >= 0 {
20 v = uvinf
21 } else {
22 v = uvneginf
24 return Float64frombits(v)
27 // NaN returns an IEEE 754 ``not-a-number'' value.
28 func NaN() float64 { return Float64frombits(uvnan) }
30 // IsNaN returns whether f is an IEEE 754 ``not-a-number'' value.
31 func IsNaN(f float64) (is bool) {
32 // IEEE 754 says that only NaNs satisfy f != f.
33 // To avoid the floating-point hardware, could use:
34 // x := Float64bits(f);
35 // return uint32(x>>shift)&mask == mask && x != uvinf && x != uvneginf
36 return f != f
39 // IsInf returns whether f is an infinity, according to sign.
40 // If sign > 0, IsInf returns whether f is positive infinity.
41 // If sign < 0, IsInf returns whether f is negative infinity.
42 // If sign == 0, IsInf returns whether f is either infinity.
43 func IsInf(f float64, sign int) bool {
44 // Test for infinity by comparing against maximum float.
45 // To avoid the floating-point hardware, could use:
46 // x := Float64bits(f);
47 // return sign >= 0 && x == uvinf || sign <= 0 && x == uvneginf;
48 return sign >= 0 && f > MaxFloat64 || sign <= 0 && f < -MaxFloat64
51 // normalize returns a normal number y and exponent exp
52 // satisfying x == y × 2**exp. It assumes x is finite and non-zero.
53 func normalize(x float64) (y float64, exp int) {
54 const SmallestNormal = 2.2250738585072014e-308 // 2**-1022
55 if Abs(x) < SmallestNormal {
56 return x * (1 << 52), -52
58 return x, 0