Given something in the form
rem := len(foo) % m
_ = foo[:rem]
The compiler currently (1.14) inserts a bound check on foo[:rem], but 0 <= rem <= len(foo) is guaranteed since rem is the modulo of a positive integer.
It would be even nicer if the compiler can figure out the general form x > 0 ∧ r = x % m ⇒ 0 ≤ r < m, so that operations like
_ = foo[m]
rem := len(foo) % m
_ = foo[rem]
can have the bound check eliminated too.
This sort of expression usually props up when dealing with block-oriented algorithms, such as ciphers:
https://github.com/golang/crypto/blob/0848c9571904fcbcb24543358ca8b5a7dbfde875/chacha20/chacha_generic.go#L213
https://github.com/golang/crypto/blob/0848c9571904fcbcb24543358ca8b5a7dbfde875/poly1305/sum_amd64.go#L42
Given something in the form
The compiler currently (1.14) inserts a bound check on
foo[:rem], but0 <= rem <= len(foo)is guaranteed sinceremis the modulo of a positive integer.It would be even nicer if the compiler can figure out the general form
x > 0 ∧ r = x % m ⇒ 0 ≤ r < m, so that operations likecan have the bound check eliminated too.
This sort of expression usually props up when dealing with block-oriented algorithms, such as ciphers:
https://github.com/golang/crypto/blob/0848c9571904fcbcb24543358ca8b5a7dbfde875/chacha20/chacha_generic.go#L213
https://github.com/golang/crypto/blob/0848c9571904fcbcb24543358ca8b5a7dbfde875/poly1305/sum_amd64.go#L42