🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.arch
Article #1070

Re: reassociation and undefined behaviour

#1070
From: Scott Lurndal
Date: Fri, 04 Dec 2026 15:41
50 lines
3258 bytes
MitchAlsup <user5857@newsgrouper.org.invalid> writes:
>scott@slp53.sl.home (Scott Lurndal) posted:
>
>> MitchAlsup <user5857@newsgrouper.org.invalid> writes:
>> >anton@mips.complang.tuwien.ac.at (Anton Ertl) posted:
>> >
>> >> David Brown <david.brown@hesbynett.no> writes:
>> >> >On 03/12/2026 03:07, MitchAlsup wrote:
>> >> >> David Brown <david.brown@hesbynett.no> posted:
>> >> >>> In general, "(x<<n) | (x>>(32-n))" cannot be replaced with "undef"
>> >> >> 
>> >> >> It should be replaced with::
>> >> >> 
>> >> >>       ROTW     Rt,Rx,Rn
>> >> >> 
>> >> >
>> >> >Optimising compilers generally do implement such expressions with 
>> >> >rotation instructions, when "n" is a variable.
>> >> 
>> >> I looked at this in 2015, and found that clang-3.5 generates:
>> >> 
>> >> mov %sil,%cl
>> >> rol %cl,%edi
>> >> 
>> >> if it knows nothing about n, but code that produces 0 if it knows that
>> >> n=0.
>> >> 
>> >> >There are good reasons for such shifts being UB in C.  Processors are 
>> >> >not consistent in how out-of-range shifts are handled.  "x >> 32" might 
>> >> >give 0 or it might give x.
>> >
>> >How is x a useful result ?? EVER ???
>> 
>> If the programmer wants a zero, surely it is more efficient to
>> to just assign zero the the variable rather than right-shifting
>> some random 32-bit value by 32 bits.
>
>Not questioning 0--but questioning when (x>>32) == x when x !=0

My point was that there is no reason for a compiler to
emit a shift of 32-bits for a 32-bit datum, so the
result of the instruction in that case is irrelevent[*].

I assume that the implementation of the instruction did
nothing when the shift count was >= 32, which simplified
the logic design.

[*] Granted, it would be difficult for a C compiler to prevent
a programmer from shifting by 32 (or greater), which is probably
why the C standard makes the outcome undefined.
Technical Details
Message-ID: <JaBQS.1048930$jCzf.384774@fx24.iad>
Path: mbox-mo1.pugleaf.net!news-wdc.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!feeder.usenetexpress.com!tr1.iad1.usenetexpress.com!news-out.netnews.com!s1-3.netnews.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx24.iad.POSTED!not-for-mail
References: <11cpqih$acf5$1@dont-email.me> <11ejtoe$eoj7$1@dont-email.me> <11el2c1$q43$1@reader2.panix.com> <11emcgn$1k681$1@dont-email.me> <jwvbj74bzm4.fsf-monnier+comp.arch@gnu.org> <11eolqn$2peg5$1@dont-email.me> <2026Dec2.132411@mips.complang.tuwien.ac.at> <11epd07$362ph$1@dont-email.me> <1796263648-5857@newsgrouper.org> <11ergn7$6utu$1@dont-email.me> <2026Dec3.122036@mips.complang.tuwien.ac.at> <1796326998-5857@newsgrouper.org> <eCkQS.41981$ncv4.3699@fx06.iad> <1796355421-5857@newsgrouper.org>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-wdc.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!feeder.usenetexpress.com!tr1.iad1.usenetexpress.com!news-out.netnews.com!s1-3.netnews.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx24.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: scott@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: reassociation and undefined behaviour
Newsgroups: comp.arch
References: <11cpqih$acf5$1@dont-email.me> <11ejtoe$eoj7$1@dont-email.me> <11el2c1$q43$1@reader2.panix.com> <11emcgn$1k681$1@dont-email.me> <jwvbj74bzm4.fsf-monnier+comp.arch@gnu.org> <11eolqn$2peg5$1@dont-email.me> <2026Dec2.132411@mips.complang.tuwien.ac.at> <11epd07$362ph$1@dont-email.me> <1796263648-5857@newsgrouper.org> <11ergn7$6utu$1@dont-email.me> <2026Dec3.122036@mips.complang.tuwien.ac.at> <1796326998-5857@newsgrouper.org> <eCkQS.41981$ncv4.3699@fx06.iad> <1796355421-5857@newsgrouper.org>
Lines: 50
Message-ID: <JaBQS.1048930$jCzf.384774@fx24.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Fri, 04 Dec 2026 15:41:29 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Fri, 04 Dec 2026 15:41:29 GMT
X-Received-Bytes: 2947