11 Comments

The optimization can also be done for unsigned integers, it just has to be done slightly differently. https://ideone.com/EBwo8e

So undefined behavior isn't really enabling much more of an optimization here. It's just that the compiler misses this optimization for unsigned ints.

Expand full comment

so why the compiler cannot use the more optimized, no loop method? unsigned has a well defined wrap around outcome.

Expand full comment

so in the case of unsigned, what could be wrong if the compiler would be using the more optimized, no loop code gen? why exactly it says "I cant do it" ? in unsigned, you have a defined wrap around, right?

Expand full comment
Comment deleted
Expand full comment