3 ms·
> Um... what? That produces no exception on any architecture I know. Have you tried actually compiling and running Tavis's test program with the given argument
by haberman 15y ago
> Um... what? That produces no exception on any architecture I know.
Have you tried actually compiling and running Tavis's test program with the given arguments? I did and it crashed on my Core i7 running on OS X.
In gdb I get:
(gdb) run -2147483648 -1
Starting program: /private/tmp/test3 -2147483648 -1
Reading symbols for shared libraries +. done
Program received signal EXC_ARITHMETIC, Arithmetic exception.
0x0000000100000f23 in main ()
Hint: it won't work to just compile a program that does INT_MIN / -1 at compile-time, the compiler will do constant-folding that prevents an actual "idiv" instruction from being generated.
- ajross 15y agoOoh, my bad. Though I was actually smarter than that and made gcc (4.6.0, amd64) do the division. The problem is that "int a=0x80000000; printf("%d\n", a/-1);" when compiled without (!) optimization generates a NEG instruction, not an IDIV. That's not constant folding, that's strength reduction. And it's an optimization that I specifically told gcc not to do. Honestly I'd consider this a compiler bug. And of course NEG does what I expected and produces the 2's complementation negation of 0x8000000 (which is 0x80000000).