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: same float32 calculations, but different results on M1 (darwin/arm64), depending on inlining #53134

Closed
fzipp opened this issue May 29, 2022 · 4 comments

Comments

@fzipp
Copy link
Contributor

fzipp commented May 29, 2022

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

$ go version
go version devel go1.19-1247354a08 Sat May 28 00:28:55 2022 +0000 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/frederik/Library/Caches/go-build"
GOENV="/Users/frederik/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/frederik/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/frederik/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/frederik/go/src/go.googlesource.com/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/frederik/go/src/go.googlesource.com/go/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="devel go1.19-1247354a08 Sat May 28 00:28:55 2022 +0000"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
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/9g/vgkswhbn2tn8r6h6f6ry7wl80000gn/T/go-build3036370468=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Run the following program on a Mac M1 (darwin/arm64):

package main

import (
	"fmt"
	"math"
)

func main() {
	v := Vec3{20, 80, 15}

	x1 := Vec3{-0.037400786, 0, 0.06233464}.Norm().Dot(v)
	x2 := f(v)

	fmt.Println(x1)
	fmt.Println(x2)
}

func f(v Vec3) float32 {
	// same calculation as for x1 above, but different result
	return Vec3{-0.037400786, 0, 0.06233464}.Norm().Dot(v)
}

type Vec3 struct {
	X, Y, Z float32
}

// Norm returns the normalized vector of v.
func (v Vec3) Norm() Vec3 {
	l := float32(math.Sqrt(float64(v.Dot(v))))
	return Vec3{v.X / l, v.Y / l, v.Z / l}
}

// Dot returns the dot product of v and w.
func (v Vec3) Dot(w Vec3) float32 {
	return v.X*w.X + v.Y*w.Y + v.Z*w.Z
}

What did you expect to see?

These are the expected results I see on an Intel Mac machine:

2.5724783
2.5724783

Same results, due to same calculation for x1 and x2.

What did you see instead?

These are the deviating results I see on an M1 Mac machine:

2.5724783
2.5724788
@seankhliao
Copy link
Member

seankhliao commented May 29, 2022

Seems to be allowed by spec: https://go.dev/ref/spec#Floating_point_operators

An implementation may combine multiple floating-point operations into a single fused operation, possibly across statements, and produce a result that differs from the value obtained by executing and rounding the instructions individually

@fzipp
Copy link
Contributor Author

fzipp commented May 29, 2022

@seankhliao Interesting, that's probably it.

@randall77
Copy link
Contributor

randall77 commented May 29, 2022

Yes, arm64 has a fused multiply-add operation which does only one rounding.
I suspect if you made the body of Dot

float32(v.x*w.X)+float32(v.Y*w.Y)+float32(v.Z*w.Z)

then the answer would be the same.

@fzipp
Copy link
Contributor Author

fzipp commented May 29, 2022

@randall77 Yes, this makes it indeed the same. Thanks.

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

No branches or pull requests

3 participants