golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
121.22k stars 17.37k forks source link

cmd/compile: math.Trunc produces incorrect result on riscv64 in Go 1.23rc1 #68322

Closed mathetake closed 1 hour ago

mathetake commented 4 days ago

Go version

go version go1.23rc1 darwin/arm64

Output of go env in your module/workspace:

GO111MODULE='on'
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/mathetake/Library/Caches/go-build'
GOENV='/Users/mathetake/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/mathetake/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/mathetake/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/Users/mathetake/sdk/go1.23rc1'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/Users/mathetake/sdk/go1.23rc1/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.23rc1'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/mathetake/Library/Application Support/go/telemetry'
GCCGO='gccgo'
GOARM64='v8.0'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/Users/mathetake/wazero/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/zv/cj1015lx5bndpw3n_qsx9mh40000gn/T/go-build971855478=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

I created the following reproducer

func TestRISCVTruncIncorrect(t *testing.T) {
    if math.Trunc(18446744073709549568.0) != 18446744073709549568.0 {
        t.Fatalf("math.Trunc(18446744073709549568.0) = %f, want 18446744073709549568.0", math.Trunc(18446744073709549568.0))
    }
}

What did you see happen?

I cross compiled the test with GOOS=linux GOARCH=riscv64, and run it in the qemu environment. Then the test passed with Go 1.22 but was failing with Go 1.23rc1.

What did you expect to see?

The test passes.

gabyhelp commented 4 days ago

Related Issues

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

randall77 commented 4 days ago

@golang/riscv64

Jorropo commented 4 days ago

Very probably ad377e906a8ee6f27545d83de280206dacec1e58 cc @mengzhuo

randall77 commented 4 days ago

Yes, that riscv64 assembly looks bad. It converts float64->int64->float64, which doesn't work if the input float is bigger than 2^63-1. (For your example it is 2^64-2^11, I think, but generally it is a problem for very large floats.) We could probably compare |input| to a large value and if it is bigger than 2^53 (or whatever the last fractional float is), return the input. (And maybe we could do that instead of the Inf comparison?)

gopherbot commented 4 days ago

Change https://go.dev/cl/596516 mentions this issue: math: remove riscv64 assembly implementations of rounding