Commit 47e71f3b authored by Plekhanov Maxim's avatar Plekhanov Maxim Committed by Brad Fitzpatrick

math: use Abs in Pow rather than if x < 0 { x = -x }

name     old time/op  new time/op  delta
PowInt   55.7ns ± 1%  53.4ns ± 2%  -4.15%  (p=0.000 n=9+9)
PowFrac   133ns ± 1%   133ns ± 2%    ~     (p=0.587 n=8+9)

Change-Id: Ica0f4c2cbd554f2195c6d1762ed26742ff8e3924
Reviewed-on: https://go-review.googlesource.com/c/85375Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 497d2417
...@@ -83,13 +83,7 @@ func pow(x, y float64) float64 { ...@@ -83,13 +83,7 @@ func pow(x, y float64) float64 {
return 1 / Sqrt(x) return 1 / Sqrt(x)
} }
absy := y yi, yf := Modf(Abs(y))
flip := false
if absy < 0 {
absy = -absy
flip = true
}
yi, yf := Modf(absy)
if yf != 0 && x < 0 { if yf != 0 && x < 0 {
return NaN() return NaN()
} }
...@@ -147,9 +141,9 @@ func pow(x, y float64) float64 { ...@@ -147,9 +141,9 @@ func pow(x, y float64) float64 {
} }
// ans = a1*2**ae // ans = a1*2**ae
// if flip { ans = 1 / ans } // if y < 0 { ans = 1 / ans }
// but in the opposite order // but in the opposite order
if flip { if y < 0 {
a1 = 1 / a1 a1 = 1 / a1
ae = -ae ae = -ae
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment