Closed rsc closed 9 years ago
The result should be -128 in this case and no overflow should occur (TBD in the spec). The spec should be clarified, but there appears to be a bug in the gc compiler tool chain. The result is as expected (and without panic) for x/-1, but not for x /= -1 (analogous for % operation): package main func main() { // / operations { var x int8 = -1<<(8-1) println(x, x/-1) } { var x int16 = -1<<(16-1) println(x, x/-1) } { var x int32 = -1<<(32-1) println(x, x/-1) } { var x int64 = -1<<(64-1) println(x, x/-1) } println() { var x int8 = -1<<(8-1) y := x/-1 println(x, y) } { var x int16 = -1<<(16-1) y := x/-1 println(x, y) } { var x int32 = -1<<(32-1) y := x/-1 println(x, y) } { var x int64 = -1<<(64-1) y := x/-1 println(x, y) } println() { var x int8 = -1<<(8-1) x /= -1 println(x) } // The following panic with 6g. { var x int16 = -1<<(16-1) x /= -1 println(x) } { var x int32 = -1<<(32-1) x /= -1 println(x) } { var x int64 = -1<<(64-1) x /= -1 println(x) } println() }
package main func main() { // / operations { var x int8 = -1<<(8-1) println(x, x/-1) } { var x int16 = -1<<(16-1) println(x, x/-1) } { var x int32 = -1<<(32-1) println(x, x/-1) } { var x int64 = -1<<(64-1) println(x, x/-1) } println() { var x int8 = -1<<(8-1) y := x/-1 println(x, y) } { var x int16 = -1<<(16-1) y := x/-1 println(x, y) } { var x int32 = -1<<(32-1) y := x/-1 println(x, y) } { var x int64 = -1<<(64-1) y := x/-1 println(x, y) } println() { var x int8 = -1<<(8-1) x /= -1 println(x) } { var x int16 = -1<<(16-1) x /= -1 println(x) } { var x int32 = -1<<(32-1) x /= -1 println(x) } { var x int64 = -1<<(64-1) x /= -1 println(x) } println() // % operations { var x int8 = -1<<(8-1) println(x, x%-1) } { var x int16 = -1<<(16-1) println(x, x%-1) } { var x int32 = -1<<(32-1) println(x, x%-1) } { var x int64 = -1<<(64-1) println(x, x%-1) } println() { var x int8 = -1<<(8-1) y := x%-1 println(x, y) } { var x int16 = -1<<(16-1) y := x%-1 println(x, y) } { var x int32 = -1<<(32-1) y := x%-1 println(x, y) } { var x int64 = -1<<(64-1) y := x%-1 println(x, y) } println() { var x int8 = -1<<(8-1) x %= -1 println(x) } { var x int16 = -1<<(16-1) x %= -1 println(x) } { var x int32 = -1<<(32-1) x %= -1 println(x) } { var x int64 = -1<<(64-1) x %= -1 println(x) } println() } -128 -128 -32768 -32768 -2147483648 -2147483648 -9223372036854775808 -9223372036854775808 -128 -128 -32768 -32768 -2147483648 -2147483648 -9223372036854775808 -9223372036854775808 -128 -32768 -2147483648 -9223372036854775808 gri@r45:~/go2/tmp$ gccgo test11.go gri@r45:~/go2/tmp$ a.out -128 -128 -32768 -32768 -2147483648 -2147483648 -9223372036854775808 -9223372036854775808 -128 -128 -32768 -32768 -2147483648 -2147483648 -9223372036854775808 -9223372036854775808 -128 -32768 -2147483648 -9223372036854775808 -128 0 -32768 0 -2147483648 0 -9223372036854775808 0 -128 0 -32768 0 -2147483648 0 -9223372036854775808 0