2 ms·
It would be extremely useful e.g. because you could perform overflow checks as easily as: if (x + 1 < x) // overflow as opposed to the much more complicated
by int_19h 2y ago
It would be extremely useful e.g. because you could perform overflow checks as easily as:
if (x + 1 < x) // overflow
as opposed to the much more complicated dance that you have to do today to be conformant.
I don't disagree that people want compilers to optimize in general. But can you give an example of optimization that is 1) relies on compiler treating signed overflow as UB, and 2) is actually useful (i.e. makes conforming code run faster)?
As far as modeling, the very fact that C ints are bounded - and lest we forget, unless you use `long long` everywhere, even `long` doesn't guarantee you more than 32 bits per the Standard, which is not all that large! - they are not a good abstraction for mathematical integers. C developers use it as such in practice because it is convenient, but consequently tend to ignore the overflow behavior, not the least because dealing with signed overflow is so inconvenient. Even today, I suspect that the vast majority of production C code out there is not actually safe wrt to signed overflow-induced UB due to overly large inputs (e.g. how many CLI tools do such validation for values that come out of argv?).
- atiedebee 2y agoif( x > INT_MAX - n ) // overflow for x + n Your definition of a far more complicated dance is absurd. Signed integer being undefined makes a lot of integer math faster actually: https://kristerw.blogspot.com/2016/02/how-undefined-signed-overflow-enables.html?m=1 https://kristerw.blogspot.com/2016/02/how-undefined-signed-o... There's a reason zig made unsigned overflow undefined: it allows for more optimisations (Zig can also afford it thanks to their wrapping addition (+%) operator).