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: The bounds check can be removed in u%uint(x) for all x != 0 && x <= len(slice). #15079

Closed
OneOfOne opened this issue Apr 3, 2016 · 6 comments

Comments

@OneOfOne
Copy link
Contributor

OneOfOne commented Apr 3, 2016

Please answer these questions before submitting your issue. Thanks!

  1. What version of Go are you using (go version)?
    go version devel +29267c2 2016-04-03 01:50:04 +0000 linux/amd64
  2. What operating system and processor architecture are you using (go env)?
  3. What did you do?
    http://play.golang.org/p/gWCa1r06CI
  4. What did you expect to see?
    bb (and maybe aa)'s bounds checking removed
  5. What did you see instead?
        0x0013 00019 (blah.go:21)       MOVQ    $0, "".autotmp_6+6(SP)
        0x001c 00028 (blah.go:21)       MOVQ    $0, "".autotmp_6+8(SP)
        0x0025 00037 (blah.go:22)       MOVQ    $-3689348814741910323, AX
        0x002f 00047 (blah.go:22)       MOVQ    $32768, CX
        0x0036 00054 (blah.go:22)       MULQ    CX
        0x0039 00057 (blah.go:22)       SHRQ    $3, DX
        0x003d 00061 (blah.go:22)       LEAQ    (DX)(DX*4), AX
        0x0041 00065 (blah.go:22)       SHLQ    $1, AX
        0x0044 00068 (blah.go:22)       SUBQ    $32768, AX
        0x004a 00074 (blah.go:22)       NEGQ    AX
        0x004d 00077 (blah.go:22)       CMPQ    AX, $10
        0x0051 00081 (blah.go:22)       JCC     $0, 101
        0x0053 00083 (blah.go:21)       LEAQ    "".autotmp_6+6(SP), CX
        0x0058 00088 (blah.go:22)       MOVBLZX (CX)(AX*1), CX
        0x005c 00092 (blah.go:22)       MOVB    CL, "".~r0+24(FP)
        0x0060 00096 (blah.go:22)       ADDQ    $16, SP
        0x0064 00100 (blah.go:22)       RET
        0x0065 00101 (blah.go:22)       PCDATA  $0, $0
        0x0065 00101 (blah.go:22)       CALL    runtime.panicindex(SB)

I could've sworn there was a bug about this, but I can't find it

@randall77
Copy link
Contributor

I would assert that this bug is about all this arithmetic not constant-folding away to nothing.

@cznic
Copy link
Contributor

cznic commented Apr 3, 2016

Note: Wrt issue title: The bounds check can be removed in u%uint(x) for all x != 0 && x <= len(slice).

@OneOfOne OneOfOne changed the title cmd/compile: remove bounds checking for u%uint(len(slice)) cmd/compile: The bounds check can be removed in u%uint(x) for all x != 0 && x <= len(slice). Apr 3, 2016
@minux
Copy link
Member

minux commented Apr 4, 2016 via email

@brtzsnr
Copy link
Contributor

brtzsnr commented Apr 4, 2016

@brtzsnr

Also, len(slice) can be 0 which means division by zero.

@gopherbot
Copy link

CL https://golang.org/cl/21501 mentions this issue.

@gopherbot
Copy link

CL https://golang.org/cl/21502 mentions this issue.

gopherbot pushed a commit that referenced this issue Apr 4, 2016
In b we only need the division by 0 check.

func b(i uint, v []byte) byte {
    return v[i%uint(len(v))]
}

Updates #15079.

Change-Id: Ic7491e677dd57cd6ba577efbce576dbb6e023cbd
Reviewed-on: https://go-review.googlesource.com/21502
Run-TryBot: Alexandru Moșoi <alexandru@mosoi.ro>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Ahmed Waheed <oneofone@gmail.com>
@golang golang locked and limited conversation to collaborators Apr 5, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants