]> Cypherpunks repositories - gostls13.git/commitdiff
math: change float64 bias constant from 1022 to 1023
authorEoghan Sherry <ejsherry@gmail.com>
Wed, 15 Dec 2010 18:20:52 +0000 (13:20 -0500)
committerRuss Cox <rsc@golang.org>
Wed, 15 Dec 2010 18:20:52 +0000 (13:20 -0500)
This makes some subtle code easier to understand.

R=rsc
CC=golang-dev
https://golang.org/cl/3444043

src/pkg/math/bits.go
src/pkg/math/exp_amd64.s
src/pkg/math/frexp.go
src/pkg/math/logb.go
src/pkg/math/modf.go
src/pkg/math/sqrt_port.go

index d36cd18d76b8a22f781a4d79d73dbf469eec2e58..1a97e76799ad594332e07f47796a35a00732480f 100644 (file)
@@ -10,7 +10,7 @@ const (
        uvneginf = 0xFFF0000000000000
        mask     = 0x7FF
        shift    = 64 - 11 - 1
-       bias     = 1022
+       bias     = 1023
 )
 
 // Inf returns positive infinity if sign >= 0, negative infinity if sign < 0.
index 28064f5f132c711e30c0029d255acafd7eaf125d..74c9c876affd988af47ff64f09c4df713059b866 100644 (file)
@@ -84,7 +84,7 @@ TEXT ·Exp(SB),7,$0
        MULSD   X1, X0
        ADDSD   $1.0, X0
        // return fr * 2**exponent
-       MOVL    $0x3FF, AX // bias + 1
+       MOVL    $0x3FF, AX // bias
        ADDL    AX, BX
        JLE     underflow
        CMPL    BX, $0x7FF
index b63b508e6002ccf4931fda11a786d367904cf568..203219c0dce6a58943f6cf50e1e94d7599e3f091 100644 (file)
@@ -19,9 +19,9 @@ func Frexp(f float64) (frac float64, exp int) {
                return f, 0
        }
        x := Float64bits(f)
-       exp = int((x>>shift)&mask) - bias
+       exp = int((x>>shift)&mask) - bias + 1
        x &^= mask << shift
-       x |= bias << shift
+       x |= (-1 + bias) << shift
        frac = Float64frombits(x)
        return
 }
index 22ec06325d596afc2e962b61b95e0506c2d72f4b..9e465151711d83061bb0a9004eae184bbf428bb6 100644 (file)
@@ -22,7 +22,7 @@ func Logb(x float64) float64 {
        case x != x: // IsNaN(x):
                return x
        }
-       return float64(int((Float64bits(x)>>shift)&mask) - (bias + 1))
+       return float64(int((Float64bits(x)>>shift)&mask) - bias)
 }
 
 // Ilogb(x) returns the binary exponent of non-zero x as an integer.
@@ -43,5 +43,5 @@ func Ilogb(x float64) int {
        case x < -MaxFloat64 || x > MaxFloat64: // IsInf(x, 0):
                return MaxInt32
        }
-       return int((Float64bits(x)>>shift)&mask) - (bias + 1)
+       return int((Float64bits(x)>>shift)&mask) - bias
 }
index ae0c7c8879058783d6fcaf5c320910733deabf5a..315174b70144ab31494dbf6c8e585835b11722b9 100644 (file)
@@ -23,9 +23,9 @@ func Modf(f float64) (int float64, frac float64) {
        x := Float64bits(f)
        e := uint(x>>shift)&mask - bias
 
-       // Keep the top 11+e bits, the integer part; clear the rest.
-       if e < 64-11 {
-               x &^= 1<<(64-11-e) - 1
+       // Keep the top 12+e bits, the integer part; clear the rest.
+       if e < 64-12 {
+               x &^= 1<<(64-12-e) - 1
        }
        int = Float64frombits(x)
        frac = f - int
index 8d821b559b64f095e22501e2ca896fece48ae3b3..6f35a383d11f98db3e42073236cd48359e29fe6a 100644 (file)
@@ -113,7 +113,7 @@ func sqrtGo(x float64) float64 {
                }
                exp++
        }
-       exp -= bias + 1 // unbias exponent
+       exp -= bias // unbias exponent
        ix &^= mask << shift
        ix |= 1 << shift
        if exp&1 == 1 { // odd exp, double x to make it even
@@ -138,6 +138,6 @@ func sqrtGo(x float64) float64 {
        if ix != 0 { // remainder, result not exact
                q += q & 1 // round according to extra bit
        }
-       ix = q>>1 + uint64(exp+bias)<<shift // significand + biased exponent
+       ix = q>>1 + uint64(exp-1+bias)<<shift // significand + biased exponent
        return Float64frombits(ix)
 }