GCC undefined behaviors are getting wild
This has nothing to do with undefined behavior. Switch the code to unsigned integers, where overflow is perfectly defined as wrapping, and the result is exactly the same.<p>The compiler, to avoid the division, compares x * 0x1ff with 512 * 0xffff instead of x * 0x1ff &#x2F; 0xffff with 512. This comparison is obviously bunk, since it doesn&#x27;t take the uppermost bits of the multiplication into account. But so is the original comparison!<p>You see the same thing happen in Rust -- <a href="https:&#x2F;&#x2F;rust.godbolt.org&#x2F;z&#x2F;xf67rM77T" rel="nofollow">https:&#x2F;&#x2F;rust.godbolt.org&#x2F;z&#x2F;xf67rM77T</a> -- the difference being that there&#x27;s a second language-level bounds-check inserted at the lookup site.