1
0
mirror of https://github.com/golang/go synced 2024-11-22 09:34:54 -07:00

math: faster Cbrt

For amd64, from 127 to 105 ns/op; for 386, from 208 to 169 ns/op.

R=rsc, golang-dev
CC=golang-dev
https://golang.org/cl/5412056
This commit is contained in:
Charles L. Dorian 2011-11-21 09:56:07 -05:00 committed by Russ Cox
parent 8ec32e8d84
commit f3aa54e30d

View File

@ -45,22 +45,21 @@ func Cbrt(x float64) float64 {
x = -x x = -x
sign = true sign = true
} }
// Reduce argument // Reduce argument and estimate cube root
f, e := Frexp(x) f, e := Frexp(x) // 0.5 <= f < 1.0
m := e % 3 m := e % 3
if m > 0 { if m > 0 {
m -= 3 m -= 3
e -= m // e is multiple of 3 e -= m // e is multiple of 3
} }
f = Ldexp(f, m) // 0.125 <= f < 1.0
// Estimate cube root
switch m { switch m {
case 0: // 0.5 <= f < 1.0 case 0: // 0.5 <= f < 1.0
f = A1*f + A2 - A3/(A4+f) f = A1*f + A2 - A3/(A4+f)
case -1: // 0.25 <= f < 0.5 case -1:
f *= 0.5 // 0.25 <= f < 0.5
f = B1*f + B2 - B3/(B4+f) f = B1*f + B2 - B3/(B4+f)
default: // 0.125 <= f < 0.25 default: // m == -2
f *= 0.25 // 0.125 <= f < 0.25
f = C1*f + C2 - C3/(C4+f) f = C1*f + C2 - C3/(C4+f)
} }
y := Ldexp(f, e/3) // e/3 = exponent of cube root y := Ldexp(f, e/3) // e/3 = exponent of cube root