From f3aa54e30de2c2c71a8735c5f61c9c1d93f7cd9f Mon Sep 17 00:00:00 2001 From: "Charles L. Dorian" Date: Mon, 21 Nov 2011 09:56:07 -0500 Subject: [PATCH] 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 --- src/pkg/math/cbrt.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/pkg/math/cbrt.go b/src/pkg/math/cbrt.go index d2b7e910b8..09edc0eae8 100644 --- a/src/pkg/math/cbrt.go +++ b/src/pkg/math/cbrt.go @@ -45,22 +45,21 @@ func Cbrt(x float64) float64 { x = -x sign = true } - // Reduce argument - f, e := Frexp(x) + // Reduce argument and estimate cube root + f, e := Frexp(x) // 0.5 <= f < 1.0 m := e % 3 if m > 0 { m -= 3 e -= m // e is multiple of 3 } - f = Ldexp(f, m) // 0.125 <= f < 1.0 - - // Estimate cube root switch m { case 0: // 0.5 <= f < 1.0 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) - default: // 0.125 <= f < 0.25 + default: // m == -2 + f *= 0.25 // 0.125 <= f < 0.25 f = C1*f + C2 - C3/(C4+f) } y := Ldexp(f, e/3) // e/3 = exponent of cube root -- 2.50.0