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

cmd/compile: incorrect double rounding for conversion from uint64 to float32 on 32-bit platforms #48807

Closed
ianlancetaylor opened this issue Oct 5, 2021 · 2 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@ianlancetaylor
Copy link
Contributor

Test program:

package main

import (
        "fmt"
)

func s(k uint64) float32 {
        var x float32
        x = float32(k)
        return x
}

func main() {
        k := uint64(0x8234508000000001)
	x2 := float64(k)
	fmt.Printf("%x\n", x2)
        x := float64(s(k))
	fmt.Printf("%x\n", x)
}

On amd64 this prints

0x1.0468a1p+63
0x1.0468a2p+63

On 386 it prints

0x1.0468a1p+63
0x1.0468ap+63

The second number is different. This is happening because on 386 we convert a uint64 to float32 by first converting to float64 (via runtime.uint64tofloat64) and then converting that to float32. This introduces a double-rounding. Specifically the uint64 value 0x1.0468a1p+63 converts to float32 0x1.0468ap+63, but the correct result of the original rounding is 0x1.0468a2p+63.

@ianlancetaylor ianlancetaylor added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Oct 5, 2021
@ianlancetaylor ianlancetaylor added this to the Backlog milestone Oct 5, 2021
@gopherbot
Copy link

Change https://golang.org/cl/354429 mentions this issue: cmd/compile,runtime: implement uint64->float32 correctly on 32-bit archs

@gopherbot
Copy link

Change https://golang.org/cl/354613 mentions this issue: runtime: fix uint64->float32 conversion for softfloat

gopherbot pushed a commit that referenced this issue Oct 8, 2021
The fix for #48807 in CL 354429 forgot that we also need to fix
the softfloat implementation.

Update #48807

Change-Id: I596fb4e14e78145d1ad43c130b2cc5122b73655c
Reviewed-on: https://go-review.googlesource.com/c/go/+/354613
Trust: Keith Randall <khr@golang.org>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: David Chase <drchase@google.com>
@golang golang locked and limited conversation to collaborators Oct 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

2 participants