mirror of
https://github.com/golang/go
synced 2024-11-18 13:04:46 -07:00
88672de7af
As necessary, math functions were structured to use stubs, so that they can be accelerated with assembly on any platform. Technique used was minimax polynomial approximation using tables of polynomial coefficients, with argument range reduction. Benchmark New Old Speedup BenchmarkAcos 12.2 47.5 3.89 BenchmarkAcosh 18.5 56.2 3.04 BenchmarkAsin 13.1 40.6 3.10 BenchmarkAsinh 19.4 62.8 3.24 BenchmarkAtan 10.1 23 2.28 BenchmarkAtanh 19.1 53.2 2.79 BenchmarkAtan2 16.5 33.9 2.05 BenchmarkCbrt 14.8 58 3.92 BenchmarkErf 10.8 20.1 1.86 BenchmarkErfc 11.2 23.5 2.10 BenchmarkExp 8.77 53.8 6.13 BenchmarkExpm1 10.1 38.3 3.79 BenchmarkLog 13.1 40.1 3.06 BenchmarkLog1p 12.7 38.3 3.02 BenchmarkPowInt 31.7 40.5 1.28 BenchmarkPowFrac 33.1 141 4.26 BenchmarkTan 11.5 30 2.61 Accuracy was tested against a high precision reference function to determine maximum error. Note: ulperr is error in "units in the last place" max ulperr Acos 1.15 Acosh 1.07 Asin 2.22 Asinh 1.72 Atan 1.41 Atanh 3.00 Atan2 1.45 Cbrt 1.18 Erf 1.29 Erfc 4.82 Exp 1.00 Expm1 2.26 Log 0.94 Log1p 2.39 Tan 3.14 Pow will have 99.99% correctly rounded results with reasonable inputs producing numeric (non Inf or NaN) results Change-Id: I850e8cf7b70426e8b54ec49d74acd4cddc8c6cb2 Reviewed-on: https://go-review.googlesource.com/38585 Reviewed-by: Michael Munday <munday@ca.ibm.com> Run-TryBot: Michael Munday <munday@ca.ibm.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
140 lines
2.6 KiB
Go
140 lines
2.6 KiB
Go
// 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
|
|
|
|
func isOddInt(x float64) bool {
|
|
xi, xf := Modf(x)
|
|
return xf == 0 && int64(xi)&1 == 1
|
|
}
|
|
|
|
// Special cases taken from FreeBSD's /usr/src/lib/msun/src/e_pow.c
|
|
// updated by IEEE Std. 754-2008 "Section 9.2.1 Special values".
|
|
|
|
// Pow returns x**y, the base-x exponential of y.
|
|
//
|
|
// Special cases are (in order):
|
|
// Pow(x, ±0) = 1 for any x
|
|
// Pow(1, y) = 1 for any y
|
|
// Pow(x, 1) = x for any x
|
|
// Pow(NaN, y) = NaN
|
|
// Pow(x, NaN) = NaN
|
|
// Pow(±0, y) = ±Inf for y an odd integer < 0
|
|
// Pow(±0, -Inf) = +Inf
|
|
// Pow(±0, +Inf) = +0
|
|
// Pow(±0, y) = +Inf for finite y < 0 and not an odd integer
|
|
// Pow(±0, y) = ±0 for y an odd integer > 0
|
|
// Pow(±0, y) = +0 for finite y > 0 and not an odd integer
|
|
// Pow(-1, ±Inf) = 1
|
|
// Pow(x, +Inf) = +Inf for |x| > 1
|
|
// Pow(x, -Inf) = +0 for |x| > 1
|
|
// Pow(x, +Inf) = +0 for |x| < 1
|
|
// Pow(x, -Inf) = +Inf for |x| < 1
|
|
// Pow(+Inf, y) = +Inf for y > 0
|
|
// Pow(+Inf, y) = +0 for y < 0
|
|
// Pow(-Inf, y) = Pow(-0, -y)
|
|
// Pow(x, y) = NaN for finite x < 0 and finite non-integer y
|
|
func Pow(x, y float64) float64
|
|
|
|
func pow(x, y float64) float64 {
|
|
switch {
|
|
case y == 0 || x == 1:
|
|
return 1
|
|
case y == 1:
|
|
return x
|
|
case y == 0.5:
|
|
return Sqrt(x)
|
|
case y == -0.5:
|
|
return 1 / Sqrt(x)
|
|
case IsNaN(x) || IsNaN(y):
|
|
return NaN()
|
|
case x == 0:
|
|
switch {
|
|
case y < 0:
|
|
if isOddInt(y) {
|
|
return Copysign(Inf(1), x)
|
|
}
|
|
return Inf(1)
|
|
case y > 0:
|
|
if isOddInt(y) {
|
|
return x
|
|
}
|
|
return 0
|
|
}
|
|
case IsInf(y, 0):
|
|
switch {
|
|
case x == -1:
|
|
return 1
|
|
case (Abs(x) < 1) == IsInf(y, 1):
|
|
return 0
|
|
default:
|
|
return Inf(1)
|
|
}
|
|
case IsInf(x, 0):
|
|
if IsInf(x, -1) {
|
|
return Pow(1/x, -y) // Pow(-0, -y)
|
|
}
|
|
switch {
|
|
case y < 0:
|
|
return 0
|
|
case y > 0:
|
|
return Inf(1)
|
|
}
|
|
}
|
|
|
|
absy := y
|
|
flip := false
|
|
if absy < 0 {
|
|
absy = -absy
|
|
flip = true
|
|
}
|
|
yi, yf := Modf(absy)
|
|
if yf != 0 && x < 0 {
|
|
return NaN()
|
|
}
|
|
if yi >= 1<<63 {
|
|
return Exp(y * Log(x))
|
|
}
|
|
|
|
// ans = a1 * 2**ae (= 1 for now).
|
|
a1 := 1.0
|
|
ae := 0
|
|
|
|
// ans *= x**yf
|
|
if yf != 0 {
|
|
if yf > 0.5 {
|
|
yf--
|
|
yi++
|
|
}
|
|
a1 = Exp(yf * Log(x))
|
|
}
|
|
|
|
// ans *= x**yi
|
|
// by multiplying in successive squarings
|
|
// of x according to bits of yi.
|
|
// accumulate powers of two into exp.
|
|
x1, xe := Frexp(x)
|
|
for i := int64(yi); i != 0; i >>= 1 {
|
|
if i&1 == 1 {
|
|
a1 *= x1
|
|
ae += xe
|
|
}
|
|
x1 *= x1
|
|
xe <<= 1
|
|
if x1 < .5 {
|
|
x1 += x1
|
|
xe--
|
|
}
|
|
}
|
|
|
|
// ans = a1*2**ae
|
|
// if flip { ans = 1 / ans }
|
|
// but in the opposite order
|
|
if flip {
|
|
a1 = 1 / a1
|
|
ae = -ae
|
|
}
|
|
return Ldexp(a1, ae)
|
|
}
|