Skip to content

cmd/compile: possibly incorrect fused-multiply-add bypassing floating-point to integer conversions #54533

@jmacd

Description

@jmacd

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

$ go version
go version go1.18.5 darwin/arm64

Does this issue reproduce with the latest release?

This is the latest 1.18.x release (can't use 1.19.0, waiting for 1.19.1 to release b/c #54302).

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

go env Output
$ go env
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/josh.macdonald/Library/Caches/go-build"
GOENV="/Users/josh.macdonald/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/josh.macdonald/src/lightstep/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/josh.macdonald/src/lightstep/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.18.5"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/josh.macdonald/src/lightstep/go/src/github.com/lightstep/go.mod"
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/hp/8_6w4qns7g79tww_56kst5nh0000gp/T/go-build3568237414=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Ran the following:

package main

import "fmt"

func main() {
	fmt.Println("Test shows", test(0.99))
}

func test(q float64) string {
	p := q * 100
	d := int(p)
	f := p - float64(d)

	if f < 0 {
		panic(fmt.Sprint("impossible", p, d, f))
	}

	return fmt.Sprint(f)
}

What did you expect to see?

This should not panic.

What did you see instead?

I expect this not to panic because https://go.dev/ref/spec#Conversions states that floating-point to integer conversion rounds toward zero. Thus, the expression should not be negative. It looks like a fused-multiply-add is happening here, when the rules for conversion would not allow it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions