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: uint32(float32(-1)) returns 0 when crosscompiled for arm #57837

Closed
LongHairedHacker opened this issue Jan 17, 2023 · 2 comments
Closed
Labels
arch-arm Issues solely affecting the 32-bit arm architecture. compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Comments

@LongHairedHacker
Copy link

LongHairedHacker commented Jan 17, 2023

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

go version go1.19.5 linux/amd64

Does this issue reproduce with the latest release?

Yes the issue can be reproduced with 1.19.5 as well as go1.18.1 where I first encountered it.

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

My host system used for crosscompiling:

go env Output
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/sebastian/.cache/go-build"
GOENV="/home/sebastian/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/sebastian/go/pkg/mod"
GONOPROXY="gitlab.hpc.devnet.itwm.fhg.de"
GONOSUMDB="gitlab.hpc.devnet.itwm.fhg.de"
GOOS="linux"
GOPATH="/home/sebastian/go"
GOPRIVATE="gitlab.hpc.devnet.itwm.fhg.de"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="off"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.19.5"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
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 -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3492048443=/tmp/go-build -gno-record-gcc-switches"

I currently don't have go installed on the Raspi, so no go env output there.
It's a Pi 3, so BCM2837 (armv7l) running a 32bit linux.

What did you do?

I cross compiled the program below using GOOS=linux GOARCH=arm GOARM=7 go build -o f32test main.go and ran it on a raspberry pi.

package main

import "fmt"

func main() {
	var f float32
	f = -1.0

	fmt.Printf("float32 to uint32: %v\n", uint32(f))
	fmt.Printf("float32 to uint64: %v\n", uint64(f))
	fmt.Printf("float32 to int32: %v\n", int32(f))
	fmt.Printf("float32 to int64: %v\n", int64(f))

	fmt.Printf("float32 to int32 to uint32: %v\n", uint32(int32(f)))
	fmt.Printf("float32 to int64 to uint64: %v\n", uint64(int64(f)))
}

What did you expect to see?

I expected to see an output consistent with the one on my amd64 system:

float32 to uint32: 4294967295
float32 to uint64: 18446744073709551615
float32 to int32: -1
float32 to int64: -1
float32 to int32 to uint32: 4294967295
float32 to int64 to uint64: 18446744073709551615

What did you see instead?

The output on the raspberry pi is:

float32 to uint32: 0
float32 to uint64: 18446744073709551615
float32 to int32: -1
float32 to int64: -1
float32 to int32 to uint32: 4294967295
float32 to int64 to uint64: 18446744073709551615

Comments

I did also check the language specs.
The section on numeric types in the language spec states, that float32 and int32, and uint32 are predeclared architecture-independent numeric types, so I’d expect their behavior to be the same across different architectures.
Also the section on conversions does not mention any undefined or architecture specific behaviour.

Let me know if you need any further information.

@seankhliao seankhliao changed the title uint32(float32(-1)) returns 0 when crosscompiled for arm cmd/compile: uint32(float32(-1)) returns 0 when crosscompiled for arm Jan 17, 2023
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jan 17, 2023
@seankhliao seankhliao added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. arch-arm Issues solely affecting the 32-bit arm architecture. labels Jan 17, 2023
@randall77
Copy link
Contributor

From the 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.

So I don't think this is a bug. It's just how out of range float->int conversions work.

Similar to #56023

@LongHairedHacker
Copy link
Author

Ah thanks ... for reasean (probably lack of caffeine) my brain turned non-constant into constant in that sentence.

Sorry for the noise.

@golang golang locked and limited conversation to collaborators Jan 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
arch-arm Issues solely affecting the 32-bit arm architecture. compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants