Am 31.10.2024 um 17:25 schrieb Takashi Yano:
On Thu, 31 Oct 2024 11:15:59 +0100
Corinna Vinschen wrote:
Yes, I will, but this is still puzzeling. While negative shift values
are undefined in C, there's this:

[...]

I guess the compiler ommitted the undefined calculation.

Precisely. Using negative arguments on the right hand side of shift operators is undefined behaviour.  The compiler can do whatever tickles its fancy with that code.

For about a decade now, compilers like GCC have raised quite some ruckus by what some people view as an abuse of this "poetic license": they completely remove any provably undefined behaviour they discover, and from that point on may assume whichever outcome makes the following code the easiest to compile.  Their reasoning goes something like this: writing code with undefined behaviour means the user has relinquished any rights they ever had to get reasonable output. So instead the compiler writers decided to make life easy for the people who still mattered at that point: themselves.

What that means for us compiler users, even as we're implementing the runtime library, i.e. a substantial part of the C compiler toolchain itself, here, is relatively clear: we must not write such code.

Oh, and BTW, __builtin_clzl() is the wrong function to use on signed inputs, anyway :-)

Reply via email to