svn commit: r328159 - head/sys/modules
Don Lewis
truckman at FreeBSD.org
Fri Jan 19 23:50:30 UTC 2018
On 19 Jan, David Chisnall wrote:
> On 19 Jan 2018, at 05:07, Conrad Meyer <cem at FreeBSD.org> wrote:
>>
>> The spec says the behavior is undefined; not that the compiler has to
>> produce a warning or error message. The compiler *does* get to
>> arbitrarily decide what it wants to do when it encounters UB. It is
>> wholly free to implement this particular UB with the logical result
>> and no warning/error.
>
> First, you are not correct that the only logical outcome of a shift of
> greater than the width of a type is 0. In C, a right-shift of a
> signed type propagates the sign bit. Right shifting a negative 32-bit
> int by 16 and then again by 16 is not undefined behaviour (though
> doing the shift as a single operation is) and will give you a value of
> -1.
Propagating the sign when doing a right shift is the common behaviour,
but I believe this is actually implemenation defined. If the machine
doesn't have arithmetic shift instructions, then a logical shift which
fills in zeros on the left is also conformant. See the edit to the
first answer here:
https://stackoverflow.com/questions/1857928/right-shifting-negative-numbers-in-c
I think powerpc falls into this category.
More information about the svn-src-all
mailing list