Re:Undefined behavior in C is a reading error
by bhk on Thursday May 20, 2021 @12:46PM
(#27223568)
Even on modern processors, an ADD instruction does not corrupt memory. The C standard, in declaring that an integer overflow results in all-bets-are-off UB, is not enabling compilers to provide valuable optimizations.
[ Parent
| on HN ]
Re:Undefined behavior in C is a reading error
by nullc on Thursday May 20, 2021 @04:48PM
(#27227024)
It would be nice if that were true, but it's not. The ability to assume incrementing a counter won't overflow makes range analysis possible in many cases where it otherwise wouldn't be. Because of this you can perform vectorization because the compiler can assume it knows how many times the loop will run.
The performance differences are not small.
You can also tell some compilers to treat signed integer overflow as wrapping-- but people don't usually do this because the optimizations you lose are valuable.
Re:Undefined behavior in C is a reading error
by vyodaiken on Thursday May 20, 2021 @06:37PM
(#27228311)
I bet the performance differences are minimal except perhaps on some contrived cases. The compiler should not assume things it doesn't know.
But show me a benchmark.
Re:Undefined behavior in C is a reading error
by saagarjha on Thursday May 20, 2021 @10:15PM
(#27229799)
There is no guarantee that an addition in C is even going to convert into an add instruction, though. Actually, on x86 it's more likely to turn into a lea because this optimizes port usage better.