Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 19 Jan 2018 15:50:21 -0800 (PST)
From:      Don Lewis <truckman@FreeBSD.org>
To:        David Chisnall <theraven@FreeBSD.org>
Cc:        cem@freebsd.org, Benjamin Kaduk <bjkfbsd@gmail.com>, src-committers <src-committers@freebsd.org>, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   Re: svn commit: r328159 - head/sys/modules
Message-ID:  <tkrat.b370da8cb029971e@FreeBSD.org>
In-Reply-To: <244014BB-C492-48A9-9F28-0A5E713DE5EB@FreeBSD.org>
References:  <201801190434.w0J4Y6i7086677@repo.freebsd.org> <CAJ5_RoAhGgkQTLXgHpYYOiupA=c6wK%2BpeAdT0Zjaw0n-ggAd1g@mail.gmail.com> <CAG6CVpVhLcY5ohnHW32raKic7MT5Q%2B7C9EA9dzwpQvbUXVH%2Big@mail.gmail.com> <CAJ5_RoDQqBkPoDh5RH452zswKTX34O5eYeimGUwBps50FHE3Hw@mail.gmail.com> <CAG6CVpWVstTHW2OkNTu9Yj7eZgcFNUvyZhB-P6EOB1gwLOcuBQ@mail.gmail.com> <244014BB-C492-48A9-9F28-0A5E713DE5EB@FreeBSD.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On 19 Jan, David Chisnall wrote:
> On 19 Jan 2018, at 05:07, Conrad Meyer <cem@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.




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?tkrat.b370da8cb029971e>