Re:We Need Hardware Traps for Integer Overflow
by danbruc on Wednesday June 04, 2014 @06:56PM
(#7848969)
Looking at the GCC source [1] starting at line 150 this is almost what GCC actually does - perform the multiplication with twice the operand size and then check by shifting right but GCC compares the result of two shift operations differing by one in the shift amount.
[1] https://github.com/mirrors/gcc/blob/master/libgcc/libgcc2.c
Re:We Need Hardware Traps for Integer Overflow
by stormbrew on Wednesday June 04, 2014 @06:58PM
(#7848986)
For future reference, you can link to a specific line in github with #LNNN. Which you can get by clicking on the line number. Pretty handy.
https://github.com/mirrors/gcc/blob/master/libgcc/libgcc2.c#...
For the latter it looks like at least on recent versions of gcc, you can do something like (WARNING: Just a vague proof of concept, please don't use it. It might kill kittens):
Which compiles to this in gcc -Os and -O3: Which I'm pretty sure could be optimized further by using the overflow flag, but the compiler doesn't seem to want to ignore the upper word.edit: and it probably suffers the same problem as described here: https://news.ycombinator.com/item?id=7848771 -- and also probably has general issues with signedness that would need to be further checked.
edit2: changed it to uints to simplify and avoid a whole class of problem. I still don't think you should go use this, though.
(inspiration from SO: http://stackoverflow.com/a/13187798)