golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
124.26k stars 17.7k forks source link

cmd/compile: incorrect math reduction #70481

Closed randall77 closed 3 days ago

randall77 commented 4 days ago

Go version

tip

Output of go env in your module/workspace:

AR='ar'
CC='clang'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='clang++'
GCCGO='gccgo'
GO111MODULE=''
GOARCH='arm64'
GOARM64='v8.0'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/Users/khr/Library/Caches/go-build'
GODEBUG=''
GOENV='/Users/khr/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/z9/dty110711l9cr9w3ktv1_2380000gn/T/go-build1754495342=/tmp/go-build -gno-record-gcc-switches -fno-common'
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMOD='/Users/khr/sandbox/swissbase/src/go.mod'
GOMODCACHE='/Users/khr/gopath/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/khr/gopath'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/Users/khr/sandbox/swissbase'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/khr/Library/Application Support/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/Users/khr/sandbox/swissbase/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='devel go1.24-c483fdbfcf Wed Nov 20 16:57:48 2024 +0000'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

First seen as a failure on x/exp. The test rand.TestModulo is failing.

Reproducer:

package main

const maxUint64 = (1 << 64) - 1

//go:noinline
func f(n uint64) uint64 {
    return maxUint64 - maxUint64%n
}

func main() {
    for i := uint64(1); i < 20; i++ {
        println(i, maxUint64-f(i))
    }
}

This prints different things than go1.23 prints.

It was caused by me in CL 629858. Subtracting a negative should make a positive, it seems.

What did you see happen?

test failure. It prints

1 2
2 3
3 2
4 5
5 2
6 5
7 3
8 9
9 8
10 7
11 6
12 5
13 4
14 3
15 2
16 17
17 2
18 17
19 18

What did you expect to see?

It should print

1 0
2 1
3 0
4 3
5 0
6 3
7 1
8 7
9 6
10 5
11 4
12 3
13 2
14 1
15 0
16 15
17 0
18 15
19 16
gabyhelp commented 4 days ago

Related Issues

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

randall77 commented 4 days ago

@dmitshur

gopherbot commented 4 days ago

Change https://go.dev/cl/630397 mentions this issue: cmd/compile: fix rewrite rules for multiply/add