Re:Undefined behavior in C is a reading error
by anarazel on Thursday May 20, 2021 @01:36PM
(#27224335)
That's precisely my point? Because the overflow case is undefined, the compiler can assume it doesn't happen and optimize based on the fixed loop count.
[ Parent
| on HN ]
Re:Undefined behavior in C is a reading error
by rurban on Thursday May 20, 2021 @04:07PM
(#27226465)
The overflow case is not UB. param can be unsigned, of fwrapv may be declared. Or the compiler chooses to declare fwrapv by default. In no case is the compiler allowed to declare the overflow away, unless it knows from before that param can not overflow. The optimization on loop count 16 can still happen with a runtime guard.
Re:Undefined behavior in C is a reading error
by OskarS on Thursday May 20, 2021 @04:30PM
(#27226793)
The loop counter is signed even if param is not, so i++ could overflow. fwrapv is a compiler flag, it is not part of the standard: it is a flag that mandates a certain behaviour in this case, but in standard C, the loop variable overflowing is definitely UB. No runtime guard needed, C compilers are just allowed to assume a fixed length. This is the whole reason signed overflow is UB in C, for exactly cases like this.