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

runtime: floating point number conversion to int different on silicon #47387

Closed
asdine opened this issue Jul 25, 2021 · 2 comments
Closed

runtime: floating point number conversion to int different on silicon #47387

asdine opened this issue Jul 25, 2021 · 2 comments

Comments

@asdine
Copy link

asdine commented Jul 25, 2021

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

$ go version
go version go1.16.6 darwin/arm64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/asdine/Library/Caches/go-build"
GOENV="/Users/asdine/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/asdine/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/asdine/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/opt/homebrew/Cellar/go/1.16.6/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/opt/homebrew/Cellar/go/1.16.6/libexec/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.16.6"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/asdine/code/github.com/genjidb/genji/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/z3/f6p4jtl563s0z7gth5c_ydlc0000gn/T/go-build1754120130=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Calling math.Abs(math.MinInt64) then converting to an int64 doesn't overflow on mac silicon.

package main

import (
	"fmt"
	"math"
)

func main() {
	a := int64(math.MinInt64)

	b := math.Abs(math.MinInt64)

	c := math.Float64bits(b)

	d := int64(b)

	fmt.Println(a, b, c, d)
	// on x86 cpus:
	// -9223372036854775808 9.223372036854776e+18 4890909195324358656 -9223372036854775808

	// on silicon:
	// -9223372036854775808 9.223372036854776e+18 4890909195324358656 9223372036854775807
}

https://play.golang.org/p/xAYEJonoDAW

I'm not sure if this is a bug or something specific to the Silicon architecture.

What did you expect to see?

I was expecting the int64 to overflow and be negative. This is important to detect overflows and return a proper error when that happens.

What did you see instead?

The number doesn't overflow and stays positive

@randall77
Copy link
Contributor

From the Go spec:

In all non-constant conversions involving floating-point or complex values, if the result type cannot represent the value the conversion succeeds but the result value is implementation-dependent.

That's the case here. You're converting 1<<63 to int64 and it doesn't quite fit.

@ianlancetaylor
Copy link
Contributor

Closing, as this is not a bug.

To detect a representation failure when converting from float64 to in64, convert to int64 and then back again, and see if you get the same number.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants