Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

math: portable FMA implementation incorrectly returns -0 in some situations #61130

Closed
mundaym opened this issue Jun 30, 2023 · 3 comments
Closed
Assignees
Labels
NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@mundaym
Copy link
Member

mundaym commented Jun 30, 2023

What version of Go are you using (go version)?

8b5fe59

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

linux-amd64

What did you do?

https://go.dev/play/p/KGZMg3ATQtx

package main

import (
	"fmt"
	"math"
)

var portableFMA = math.FMA

func main() {
	fmt.Printf("%g\n", math.FMA(-1, 1, 1))
	fmt.Printf("%g\n", portableFMA(-1, 1, 1))
}

What did you expect to see?

0
0

What did you see instead?

0
-0

FMA calculates x*y+z. In the portable implementation of FMA if |x*y| is equal to |z| the sign bit of the result is set to the sign bit of x*y. If x*y is negative and z is positive this results in the incorrect result -0.

FMA is an intrinsic on some platforms. This issue only affects the portable implementation (hence the different results when calling FMA directly vs. indirectly).

@gopherbot
Copy link

Change https://go.dev/cl/507376 mentions this issue: math: fix portable FMA when x*y < 0 and x*y == -z

@mundaym mundaym self-assigned this Jun 30, 2023
@mpx
Copy link
Contributor

mpx commented Jul 2, 2023

@Nasfame - Go follows IEEE-754 which defines -0 == +0. The portable implementation has a bug where -1 * 1 + 1 returns -0, it should return +0.

@dmitshur dmitshur added the NeedsFix The path to resolution is known, but the work has not been done. label Jul 4, 2023
@dmitshur dmitshur added this to the Go1.21 milestone Jul 4, 2023
bradfitz pushed a commit to tailscale/go that referenced this issue Jul 15, 2023
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 golang#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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

5 participants
@mpx @dmitshur @gopherbot @mundaym and others