We've established the signs of values before so we can carefully jiggle the expressions to be guaranteed overflow-free; the tests for <0 here were meant to check if the value was "still negative", i.e. if the sum did not underflow below INT_MIN, and we can rewrite that using algebra to be overflow-free. We need an extra case in the Euclidean dvision for INT_MIN / INT_MIN which is a bit annoying but trivial; finally, for two's complement platforms, note that abs(x) = (x > 0) ? x : -x does not work (since for x=INT_MIN, -x still gives INT_MIN), but the equivalent formulation for _negative_ absolute value (x < 0) ? x : -x is always in range and overflow-free, so rewrite the relevant expressions using that negative absolute value instead. Fixes issue #741.master
parent
dcb1116f13
commit
c38ec91d22
1 changed files with 10 additions and 8 deletions
Loading…
Reference in New Issue