5 ms·
So... unsigned overflow is defined, while signed is straight up UB... and for you it's the former that's wrong?
by Jorengarenar 3y ago
So... unsigned overflow is defined, while signed is straight up UB... and for you it's the former that's wrong?
- account42 3y agoYes. Most calculations won't do the right thing with wrapping overflow and you'd be better of by them being undefined and tested via the relevant tooling (e.g. ubsan). Those that do should be written in a way to make that explicit.
- Conscat 3y agoCompilers can make commutative optimizations with signed integer overflow that is impossible with wrapping semantics. This is why Zig and Carbon both make unsigned integer overflow undefined as well. There is absolutely no advantage in most cases to wrapping semantics unless it is actually required for the algorithm (like some hashing or PRNG functions, for example).