Skip to content

Commit f6627b0

Browse files
committed
natives/math: Use Go implementation for extreme Ldexp inputs.
In Go 1.11, there has been an improvement made to Ldexp for some extreme values. The previous Math.pow-using algorithm doesn't produce correct results. Use the Go implementation of ldexp for those values. Continue to use the current Math.pow approach for other inputs because it's much faster: $ gopherjs test -v --run=none --bench=Ldexp math BenchmarkLdexp 2000000000 0.44 ns/op Compared to using the Go implementation for all inputs, without the Math.pow fast path: $ gopherjs test -v --run=none --bench=Ldexp math BenchmarkLdexp 5000000 203 ns/op Follows golang/go@4b265fb. Updates golang/go#23407.
1 parent e29d711 commit f6627b0

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

compiler/natives/src/math/math.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,13 @@ func IsNaN(f float64) (is bool) {
119119
}
120120

121121
func Ldexp(frac float64, exp int) float64 {
122-
if frac == 0 {
123-
return frac
122+
if -1024 < exp && exp < 1024 { // Use Math.pow for small exp values where it's viable. For performance.
123+
if frac == 0 {
124+
return frac
125+
}
126+
return frac * math.Call("pow", 2, exp).Float()
124127
}
125-
if exp >= 1024 {
126-
return frac * math.Call("pow", 2, 1023).Float() * math.Call("pow", 2, exp-1023).Float()
127-
}
128-
if exp <= -1024 {
129-
return frac * math.Call("pow", 2, -1023).Float() * math.Call("pow", 2, exp+1023).Float()
130-
}
131-
return frac * math.Call("pow", 2, exp).Float()
128+
return ldexp(frac, exp)
132129
}
133130

134131
func Log(x float64) float64 {

0 commit comments

Comments
 (0)