From d8f412571f8768df2d3239e72392dfeabbad1559 Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Wed, 26 Oct 2011 23:57:58 +0000 Subject: Update Go library to last weekly. From-SVN: r180552 --- libgo/go/math/abs.go | 21 +++++++++ libgo/go/math/abs_decl.go | 7 +++ libgo/go/math/all_test.go | 112 +++++++++++++++++++++++++++++---------------- libgo/go/math/bits.go | 2 +- libgo/go/math/dim.go | 29 ++++++++++++ libgo/go/math/dim_decl.go | 9 ++++ libgo/go/math/fabs.go | 21 --------- libgo/go/math/fdim.go | 29 ------------ libgo/go/math/fmod.go | 47 ------------------- libgo/go/math/gamma.go | 4 +- libgo/go/math/jn.go | 2 +- libgo/go/math/lgamma.go | 4 +- libgo/go/math/mod.go | 47 +++++++++++++++++++ libgo/go/math/mod_decl.go | 7 +++ libgo/go/math/pow.go | 2 +- libgo/go/math/pow10.go | 6 +++ libgo/go/math/remainder.go | 4 +- 17 files changed, 208 insertions(+), 145 deletions(-) create mode 100644 libgo/go/math/abs.go create mode 100644 libgo/go/math/abs_decl.go create mode 100644 libgo/go/math/dim.go create mode 100644 libgo/go/math/dim_decl.go delete mode 100644 libgo/go/math/fabs.go delete mode 100644 libgo/go/math/fdim.go delete mode 100644 libgo/go/math/fmod.go create mode 100644 libgo/go/math/mod.go create mode 100644 libgo/go/math/mod_decl.go (limited to 'libgo/go/math') diff --git a/libgo/go/math/abs.go b/libgo/go/math/abs.go new file mode 100644 index 0000000..eb3e4c7 --- /dev/null +++ b/libgo/go/math/abs.go @@ -0,0 +1,21 @@ +// Copyright 2009 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 math + +// Abs returns the absolute value of x. +// +// Special cases are: +// Abs(+Inf) = +Inf +// Abs(-Inf) = +Inf +// Abs(NaN) = NaN +func Abs(x float64) float64 { + switch { + case x < 0: + return -x + case x == 0: + return 0 // return correctly abs(-0) + } + return x +} diff --git a/libgo/go/math/abs_decl.go b/libgo/go/math/abs_decl.go new file mode 100644 index 0000000..6be9305 --- /dev/null +++ b/libgo/go/math/abs_decl.go @@ -0,0 +1,7 @@ +// Copyright 2010 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 math + +func Abs(x float64) float64 diff --git a/libgo/go/math/all_test.go b/libgo/go/math/all_test.go index d2a7d41..94ddea2 100644 --- a/libgo/go/math/all_test.go +++ b/libgo/go/math/all_test.go @@ -1359,6 +1359,20 @@ var powSC = []float64{ NaN(), // pow(NaN, NaN) } +var vfpow10SC = []int{ + MinInt32, + MaxInt32, + -325, + 309, +} + +var pow10SC = []float64{ + 0, // pow10(MinInt32) + Inf(1), // pow10(MaxInt32) + 0, // pow10(-325) + Inf(1), // pow10(309) +} + var vfsignbitSC = []float64{ Inf(-1), Copysign(0, -1), @@ -1570,7 +1584,7 @@ func TestAcos(t *testing.T) { func TestAcosh(t *testing.T) { for i := 0; i < len(vf); i++ { - a := 1 + Fabs(vf[i]) + a := 1 + Abs(vf[i]) if f := Acosh(a); !veryclose(acosh[i], f) { t.Errorf("Acosh(%g) = %g, want %g", a, f, acosh[i]) } @@ -1804,23 +1818,23 @@ func testExp2(t *testing.T, Exp2 func(float64) float64, name string) { } } -func TestFabs(t *testing.T) { +func TestAbs(t *testing.T) { for i := 0; i < len(vf); i++ { - if f := Fabs(vf[i]); fabs[i] != f { - t.Errorf("Fabs(%g) = %g, want %g", vf[i], f, fabs[i]) + if f := Abs(vf[i]); fabs[i] != f { + t.Errorf("Abs(%g) = %g, want %g", vf[i], f, fabs[i]) } } for i := 0; i < len(vffabsSC); i++ { - if f := Fabs(vffabsSC[i]); !alike(fabsSC[i], f) { - t.Errorf("Fabs(%g) = %g, want %g", vffabsSC[i], f, fabsSC[i]) + if f := Abs(vffabsSC[i]); !alike(fabsSC[i], f) { + t.Errorf("Abs(%g) = %g, want %g", vffabsSC[i], f, fabsSC[i]) } } } -func TestFdim(t *testing.T) { +func TestDim(t *testing.T) { for i := 0; i < len(vf); i++ { - if f := Fdim(vf[i], 0); fdim[i] != f { - t.Errorf("Fdim(%g, %g) = %g, want %g", vf[i], 0.0, f, fdim[i]) + if f := Dim(vf[i], 0); fdim[i] != f { + t.Errorf("Dim(%g, %g) = %g, want %g", vf[i], 0.0, f, fdim[i]) } } } @@ -1838,31 +1852,31 @@ func TestFloor(t *testing.T) { } } -func TestFmax(t *testing.T) { +func TestMax(t *testing.T) { for i := 0; i < len(vf); i++ { - if f := Fmax(vf[i], ceil[i]); ceil[i] != f { - t.Errorf("Fmax(%g, %g) = %g, want %g", vf[i], ceil[i], f, ceil[i]) + if f := Max(vf[i], ceil[i]); ceil[i] != f { + t.Errorf("Max(%g, %g) = %g, want %g", vf[i], ceil[i], f, ceil[i]) } } } -func TestFmin(t *testing.T) { +func TestMin(t *testing.T) { for i := 0; i < len(vf); i++ { - if f := Fmin(vf[i], floor[i]); floor[i] != f { - t.Errorf("Fmin(%g, %g) = %g, want %g", vf[i], floor[i], f, floor[i]) + if f := Min(vf[i], floor[i]); floor[i] != f { + t.Errorf("Min(%g, %g) = %g, want %g", vf[i], floor[i], f, floor[i]) } } } -func TestFmod(t *testing.T) { +func TestMod(t *testing.T) { for i := 0; i < len(vf); i++ { - if f := Fmod(10, vf[i]); fmod[i] != f { - t.Errorf("Fmod(10, %g) = %g, want %g", vf[i], f, fmod[i]) + if f := Mod(10, vf[i]); fmod[i] != f { + t.Errorf("Mod(10, %g) = %g, want %g", vf[i], f, fmod[i]) } } for i := 0; i < len(vffmodSC); i++ { - if f := Fmod(vffmodSC[i][0], vffmodSC[i][1]); !alike(fmodSC[i], f) { - t.Errorf("Fmod(%g, %g) = %g, want %g", vffmodSC[i][0], vffmodSC[i][1], f, fmodSC[i]) + if f := Mod(vffmodSC[i][0], vffmodSC[i][1]); !alike(fmodSC[i], f) { + t.Errorf("Mod(%g, %g) = %g, want %g", vffmodSC[i][0], vffmodSC[i][1], f, fmodSC[i]) } } } @@ -1900,7 +1914,7 @@ func TestGamma(t *testing.T) { func TestHypot(t *testing.T) { for i := 0; i < len(vf); i++ { - a := Fabs(1e200 * tanh[i] * Sqrt(2)) + a := Abs(1e200 * tanh[i] * Sqrt(2)) if f := Hypot(1e200*tanh[i], 1e200*tanh[i]); !veryclose(a, f) { t.Errorf("Hypot(%g, %g) = %g, want %g", 1e200*tanh[i], 1e200*tanh[i], f, a) } @@ -2019,7 +2033,7 @@ func TestLgamma(t *testing.T) { func TestLog(t *testing.T) { for i := 0; i < len(vf); i++ { - a := Fabs(vf[i]) + a := Abs(vf[i]) if f := Log(a); log[i] != f { t.Errorf("Log(%g) = %g, want %g", a, f, log[i]) } @@ -2054,7 +2068,7 @@ func TestLogb(t *testing.T) { func TestLog10(t *testing.T) { for i := 0; i < len(vf); i++ { - a := Fabs(vf[i]) + a := Abs(vf[i]) if f := Log10(a); !veryclose(log10[i], f) { t.Errorf("Log10(%g) = %g, want %g", a, f, log10[i]) } @@ -2089,7 +2103,7 @@ func TestLog1p(t *testing.T) { func TestLog2(t *testing.T) { for i := 0; i < len(vf); i++ { - a := Fabs(vf[i]) + a := Abs(vf[i]) if f := Log2(a); !veryclose(log2[i], f) { t.Errorf("Log2(%g) = %g, want %g", a, f, log2[i]) } @@ -2143,6 +2157,14 @@ func TestPow(t *testing.T) { } } +func TestPow10(t *testing.T) { + for i := 0; i < len(vfpow10SC); i++ { + if f := Pow10(vfpow10SC[i]); !alike(pow10SC[i], f) { + t.Errorf("Pow10(%d) = %g, want %g", vfpow10SC[i], f, pow10SC[i]) + } + } +} + func TestRemainder(t *testing.T) { for i := 0; i < len(vf); i++ { if f := Remainder(10, vf[i]); remainder[i] != f { @@ -2204,11 +2226,11 @@ func TestSinh(t *testing.T) { func TestSqrt(t *testing.T) { for i := 0; i < len(vf); i++ { - a := Fabs(vf[i]) + a := Abs(vf[i]) if f := SqrtGo(a); sqrt[i] != f { t.Errorf("SqrtGo(%g) = %g, want %g", a, f, sqrt[i]) } - a = Fabs(vf[i]) + a = Abs(vf[i]) if f := Sqrt(a); sqrt[i] != f { t.Errorf("Sqrt(%g) = %g, want %g", a, f, sqrt[i]) } @@ -2275,7 +2297,7 @@ func TestTrunc(t *testing.T) { func TestY0(t *testing.T) { for i := 0; i < len(vf); i++ { - a := Fabs(vf[i]) + a := Abs(vf[i]) if f := Y0(a); !close(y0[i], f) { t.Errorf("Y0(%g) = %g, want %g", a, f, y0[i]) } @@ -2289,7 +2311,7 @@ func TestY0(t *testing.T) { func TestY1(t *testing.T) { for i := 0; i < len(vf); i++ { - a := Fabs(vf[i]) + a := Abs(vf[i]) if f := Y1(a); !soclose(y1[i], f, 2e-14) { t.Errorf("Y1(%g) = %g, want %g", a, f, y1[i]) } @@ -2303,7 +2325,7 @@ func TestY1(t *testing.T) { func TestYn(t *testing.T) { for i := 0; i < len(vf); i++ { - a := Fabs(vf[i]) + a := Abs(vf[i]) if f := Yn(2, a); !close(y2[i], f) { t.Errorf("Yn(2, %g) = %g, want %g", a, f, y2[i]) } @@ -2509,15 +2531,15 @@ func BenchmarkExp2Go(b *testing.B) { } } -func BenchmarkFabs(b *testing.B) { +func BenchmarkAbs(b *testing.B) { for i := 0; i < b.N; i++ { - Fabs(.5) + Abs(.5) } } -func BenchmarkFdim(b *testing.B) { +func BenchmarkDim(b *testing.B) { for i := 0; i < b.N; i++ { - Fdim(10, 3) + Dim(10, 3) } } @@ -2527,21 +2549,21 @@ func BenchmarkFloor(b *testing.B) { } } -func BenchmarkFmax(b *testing.B) { +func BenchmarkMax(b *testing.B) { for i := 0; i < b.N; i++ { - Fmax(10, 3) + Max(10, 3) } } -func BenchmarkFmin(b *testing.B) { +func BenchmarkMin(b *testing.B) { for i := 0; i < b.N; i++ { - Fmin(10, 3) + Min(10, 3) } } -func BenchmarkFmod(b *testing.B) { +func BenchmarkMod(b *testing.B) { for i := 0; i < b.N; i++ { - Fmod(10, 3) + Mod(10, 3) } } @@ -2659,6 +2681,18 @@ func BenchmarkPowFrac(b *testing.B) { } } +func BenchmarkPow10Pos(b *testing.B) { + for i := 0; i < b.N; i++ { + Pow10(300) + } +} + +func BenchmarkPow10Neg(b *testing.B) { + for i := 0; i < b.N; i++ { + Pow10(-300) + } +} + func BenchmarkRemainder(b *testing.B) { for i := 0; i < b.N; i++ { Remainder(10, 3) diff --git a/libgo/go/math/bits.go b/libgo/go/math/bits.go index a1dca3e..1cf60ce 100644 --- a/libgo/go/math/bits.go +++ b/libgo/go/math/bits.go @@ -52,7 +52,7 @@ func IsInf(f float64, sign int) bool { // satisfying x == y × 2**exp. It assumes x is finite and non-zero. func normalize(x float64) (y float64, exp int) { const SmallestNormal = 2.2250738585072014e-308 // 2**-1022 - if Fabs(x) < SmallestNormal { + if Abs(x) < SmallestNormal { return x * (1 << 52), -52 } return x, 0 diff --git a/libgo/go/math/dim.go b/libgo/go/math/dim.go new file mode 100644 index 0000000..d2eb52f --- /dev/null +++ b/libgo/go/math/dim.go @@ -0,0 +1,29 @@ +// Copyright 2010 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 math + +// Dim returns the maximum of x-y or 0. +func Dim(x, y float64) float64 { + if x > y { + return x - y + } + return 0 +} + +// Max returns the larger of x or y. +func Max(x, y float64) float64 { + if x > y { + return x + } + return y +} + +// Min returns the smaller of x or y. +func Min(x, y float64) float64 { + if x < y { + return x + } + return y +} diff --git a/libgo/go/math/dim_decl.go b/libgo/go/math/dim_decl.go new file mode 100644 index 0000000..196f84f --- /dev/null +++ b/libgo/go/math/dim_decl.go @@ -0,0 +1,9 @@ +// Copyright 2010 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 math + +func Dim(x, y float64) float64 +func Max(x, y float64) float64 +func Min(x, y float64) float64 diff --git a/libgo/go/math/fabs.go b/libgo/go/math/fabs.go deleted file mode 100644 index 3431231..0000000 --- a/libgo/go/math/fabs.go +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright 2009 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 math - -// Fabs returns the absolute value of x. -// -// Special cases are: -// Fabs(+Inf) = +Inf -// Fabs(-Inf) = +Inf -// Fabs(NaN) = NaN -func Fabs(x float64) float64 { - switch { - case x < 0: - return -x - case x == 0: - return 0 // return correctly fabs(-0) - } - return x -} diff --git a/libgo/go/math/fdim.go b/libgo/go/math/fdim.go deleted file mode 100644 index 1899313..0000000 --- a/libgo/go/math/fdim.go +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright 2010 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 math - -// Fdim returns the maximum of x-y or 0. -func Fdim(x, y float64) float64 { - if x > y { - return x - y - } - return 0 -} - -// Fmax returns the larger of x or y. -func Fmax(x, y float64) float64 { - if x > y { - return x - } - return y -} - -// Fmin returns the smaller of x or y. -func Fmin(x, y float64) float64 { - if x < y { - return x - } - return y -} diff --git a/libgo/go/math/fmod.go b/libgo/go/math/fmod.go deleted file mode 100644 index 75c6146..0000000 --- a/libgo/go/math/fmod.go +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright 2009-2010 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 math - -/* - Floating-point mod function. -*/ - -// Fmod returns the floating-point remainder of x/y. -// The magnitude of the result is less than y and its -// sign agrees with that of x. -// -// Special cases are: -// if x is not finite, Fmod returns NaN -// if y is 0 or NaN, Fmod returns NaN -func Fmod(x, y float64) float64 { - // TODO(rsc): Remove manual inlining of IsNaN, IsInf - // when compiler does it for us. - if y == 0 || x > MaxFloat64 || x < -MaxFloat64 || x != x || y != y { // y == 0 || IsInf(x, 0) || IsNaN(x) || IsNan(y) - return NaN() - } - if y < 0 { - y = -y - } - - yfr, yexp := Frexp(y) - sign := false - r := x - if x < 0 { - r = -x - sign = true - } - - for r >= y { - rfr, rexp := Frexp(r) - if rfr < yfr { - rexp = rexp - 1 - } - r = r - Ldexp(y, rexp-yexp) - } - if sign { - r = -r - } - return r -} diff --git a/libgo/go/math/gamma.go b/libgo/go/math/gamma.go index 73ca0e5..0136507 100644 --- a/libgo/go/math/gamma.go +++ b/libgo/go/math/gamma.go @@ -127,7 +127,7 @@ func Gamma(x float64) float64 { case x < -170.5674972726612 || x > 171.61447887182298: return Inf(1) } - q := Fabs(x) + q := Abs(x) p := Floor(q) if q > 33 { if x >= 0 { @@ -146,7 +146,7 @@ func Gamma(x float64) float64 { if z == 0 { return Inf(signgam) } - z = Pi / (Fabs(z) * stirling(q)) + z = Pi / (Abs(z) * stirling(q)) return float64(signgam) * z } diff --git a/libgo/go/math/jn.go b/libgo/go/math/jn.go index 9024af3..1878df5 100644 --- a/libgo/go/math/jn.go +++ b/libgo/go/math/jn.go @@ -197,7 +197,7 @@ func Jn(n int, x float64) float64 { tmp := float64(n) v := 2 / x - tmp = tmp * Log(Fabs(v*tmp)) + tmp = tmp * Log(Abs(v*tmp)) if tmp < 7.09782712893383973096e+02 { for i := n - 1; i > 0; i-- { di := float64(i + i) diff --git a/libgo/go/math/lgamma.go b/libgo/go/math/lgamma.go index dc30f46..8f6d7b9 100644 --- a/libgo/go/math/lgamma.go +++ b/libgo/go/math/lgamma.go @@ -206,7 +206,7 @@ func Lgamma(x float64) (lgamma float64, sign int) { lgamma = Inf(1) // -integer return } - nadj = Log(Pi / Fabs(t*x)) + nadj = Log(Pi / Abs(t*x)) if t < 0 { sign = -1 } @@ -319,7 +319,7 @@ func sinPi(x float64) float64 { z := Floor(x) var n int if z != x { // inexact - x = Fmod(x, 2) + x = Mod(x, 2) n = int(x * 4) } else { if x >= Two53 { // x must be even diff --git a/libgo/go/math/mod.go b/libgo/go/math/mod.go new file mode 100644 index 0000000..6b16abe --- /dev/null +++ b/libgo/go/math/mod.go @@ -0,0 +1,47 @@ +// Copyright 2009-2010 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 math + +/* + Floating-point mod function. +*/ + +// Mod returns the floating-point remainder of x/y. +// The magnitude of the result is less than y and its +// sign agrees with that of x. +// +// Special cases are: +// if x is not finite, Mod returns NaN +// if y is 0 or NaN, Mod returns NaN +func Mod(x, y float64) float64 { + // TODO(rsc): Remove manual inlining of IsNaN, IsInf + // when compiler does it for us. + if y == 0 || x > MaxFloat64 || x < -MaxFloat64 || x != x || y != y { // y == 0 || IsInf(x, 0) || IsNaN(x) || IsNan(y) + return NaN() + } + if y < 0 { + y = -y + } + + yfr, yexp := Frexp(y) + sign := false + r := x + if x < 0 { + r = -x + sign = true + } + + for r >= y { + rfr, rexp := Frexp(r) + if rfr < yfr { + rexp = rexp - 1 + } + r = r - Ldexp(y, rexp-yexp) + } + if sign { + r = -r + } + return r +} diff --git a/libgo/go/math/mod_decl.go b/libgo/go/math/mod_decl.go new file mode 100644 index 0000000..d5047a7 --- /dev/null +++ b/libgo/go/math/mod_decl.go @@ -0,0 +1,7 @@ +// Copyright 2010 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 math + +func Mod(x, y float64) float64 diff --git a/libgo/go/math/pow.go b/libgo/go/math/pow.go index 06b1074..f0f52c5 100644 --- a/libgo/go/math/pow.go +++ b/libgo/go/math/pow.go @@ -66,7 +66,7 @@ func Pow(x, y float64) float64 { switch { case x == -1: return 1 - case (Fabs(x) < 1) == IsInf(y, 1): + case (Abs(x) < 1) == IsInf(y, 1): return 0 default: return Inf(1) diff --git a/libgo/go/math/pow10.go b/libgo/go/math/pow10.go index bda2e82..20f91bc 100644 --- a/libgo/go/math/pow10.go +++ b/libgo/go/math/pow10.go @@ -10,6 +10,12 @@ var pow10tab [70]float64 // Pow10 returns 10**e, the base-10 exponential of e. func Pow10(e int) float64 { + if e <= -325 { + return 0 + } else if e > 309 { + return Inf(1) + } + if e < 0 { return 1 / Pow10(-e) } diff --git a/libgo/go/math/remainder.go b/libgo/go/math/remainder.go index be8724c..7fb8a12 100644 --- a/libgo/go/math/remainder.go +++ b/libgo/go/math/remainder.go @@ -24,7 +24,7 @@ package math // precision arithmetic, where [x/y] is the (infinite bit) // integer nearest x/y (in half way cases, choose the even one). // Method : -// Based on fmod() returning x - [x/y]chopped * y exactly. +// Based on Mod() returning x - [x/y]chopped * y exactly. // Remainder returns the IEEE 754 floating-point remainder of x/y. // @@ -60,7 +60,7 @@ func Remainder(x, y float64) float64 { return 0 } if y <= HalfMax { - x = Fmod(x, y+y) // now x < 2y + x = Mod(x, y+y) // now x < 2y } if y < Tiny { if x+x > y { -- cgit v1.1