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: wrong float result in Apple Silicon #65804

Closed
breezewish opened this issue Feb 20, 2024 · 1 comment
Closed

runtime: wrong float result in Apple Silicon #65804

breezewish opened this issue Feb 20, 2024 · 1 comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime.

Comments

@breezewish
Copy link

breezewish commented Feb 20, 2024

Go version

go version go1.22.0 darwin/arm64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/breezewish/Library/Caches/go-build'
GOENV='/Users/breezewish/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/breezewish/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/breezewish/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/opt/homebrew/Cellar/go/1.22.0/libexec'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/opt/homebrew/Cellar/go/1.22.0/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.22.0'
GCCGO='gccgo'
AR='ar'
CC='cc'
CXX='c++'
CGO_ENABLED='1'
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/6m/phy2_frd0vd1hb7g1py32qz40000gn/T/go-build3863423000=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

package main

import (
	"fmt"
)

func main() {
	vb := []float32{4.7, 4.9, 2.6, 5.2, 7.4}

	var nb float32
	for _, v := range vb {
		nb += v * v
	}

	fmt.Println(nb)
}

What did you see happen?

In M1 Pro, the output is:

134.65999

In Go Playground (which is Go 1.22 as well), the output is:

134.66

What did you expect to see?

The output in two architectures should be the same.

Additionally my Clang outputs 134.66 for the C++ version:

#include <iostream>

int main() {
  float vb[] = {4.7, 4.9, 2.6, 5.2, 7.4};
  float nb = 0;

  for (int i = 0; i < 5; i++) {
    nb += vb[i] * vb[i];
  }

  std::cout << nb << std::endl;
}
❯ g++ --version
Apple clang version 15.0.0 (clang-1500.1.0.2.5)
Target: arm64-apple-darwin23.1.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Feb 20, 2024
@ianlancetaylor
Copy link
Contributor

This is most likely due to different use of floating-point multiply-add instructions. See the discussion in the "Floating-point operators" section of https://go.dev/ref/spec#Arithmetic_operators.

@ianlancetaylor ianlancetaylor closed this as not planned Won't fix, can't repro, duplicate, stale Feb 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime.
Projects
None yet
Development

No branches or pull requests

3 participants