1
0
mirror of https://github.com/golang/go synced 2024-11-11 17:51:49 -07:00

math: fix portable FMA when x*y < 0 and x*y == -z

When x*y == -z the portable implementation of FMA copied the sign
bit from x*y into the result. This meant that when x*y == -z and
x*y < 0 the result was -0 which is incorrect.

Fixes #61130.

Change-Id: Ib93a568b7bdb9031e2aedfa1bdfa9bddde90851d
Reviewed-on: https://go-review.googlesource.com/c/go/+/507376
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Run-TryBot: Michael Munday <mike.munday@lowrisc.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Joedian Reid <joedian@golang.org>
This commit is contained in:
Michael Munday 2023-07-01 00:01:26 +01:00 committed by Keith Randall
parent cd6676126b
commit c8dad424bf
2 changed files with 12 additions and 0 deletions

View File

@ -2059,6 +2059,9 @@ var fmaC = []struct{ x, y, z, want float64 }{
// Special
{0, 0, 0, 0},
{Copysign(0, -1), 0, 0, 0},
{0, 0, Copysign(0, -1), 0},
{Copysign(0, -1), 0, Copysign(0, -1), Copysign(0, -1)},
{-1.1754226043408471e-38, NaN(), Inf(0), NaN()},
{0, 0, 2.22507385643494e-308, 2.22507385643494e-308},
{-8.65697792e+09, NaN(), -7.516192799999999e+09, NaN()},
@ -2077,6 +2080,10 @@ var fmaC = []struct{ x, y, z, want float64 }{
{4.612811918325842e+18, 1.4901161193847641e-08, 2.6077032311277997e-08, 6.873625395187494e+10},
{-9.094947033611148e-13, 4.450691014249257e-308, 2.086006742350485e-308, 2.086006742346437e-308},
{-7.751454006381804e-05, 5.588653777189071e-308, -2.2207280111272877e-308, -2.2211612130544025e-308},
// Issue #61130
{-1, 1, 1, 0},
{1, 1, -1, 0},
}
var sqrt32 = []float32{

View File

@ -132,6 +132,11 @@ func FMA(x, y, z float64) float64 {
ps, pe, pm1, pm2, zs, ze, zm1, zm2 = zs, ze, zm1, zm2, ps, pe, pm1, pm2
}
// Special case: if p == -z the result is always +0 since neither operand is zero.
if ps != zs && pe == ze && pm1 == zm1 && pm2 == zm2 {
return 0
}
// Align significands
zm1, zm2 = shrcompress(zm1, zm2, uint(pe-ze))