🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.arch
Article #1066

Re: reassociation and undefined behaviour

#1066
From: David Brown
Date: Fri, 04 Dec 2026 08:35
59 lines
4024 bytes
On 03/12/2026 20:43, MitchAlsup wrote:
> 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 ???

It matches what many processors (but far from all!) give you if you do a 
32-bit shift of a register count, and that register holds 32.

Certainly it does not match the obvious interpretation of right shift as 
a division by a power of two.

So it is useful in the same way that two's complement wrapping for 
signed arithmetic is useful - it is absolutely absurd mathematically 
that you can add two positive numbers and get a negative one, there is 
almost certainly a bug in your program if this happens in general code, 
but it matches the hardware, is often harmless, and it is occasionally 
handy.

>   
>> If C specified that the result is 0 or x, then the result of the
>> expression above for n=0 would be x (because (x|x)==x and (x|0)==x).
>>
>> So I think there is a good reason to specify that x>>32 is 0 or x, but
>> not a good reason to make it undefined behaviour.
> 
> I, personally, do not care that C makes this UB.
> What I do care about is that no ISA should allow (x>>32) == x when
> x is not 0. It is arithmetically unsound.
Complain to the designers of most ISAs.  Masking the shift count 
register to the size of the shift operand is the most common choice, 
AFAICS.  ARM masks the shift count to 8 bits - so its hardware gives "x 
 >> 32" as 0, but "x >> 256" will give x.

Technical Details
Message-ID: <11etu0b$1aih9$1@dont-email.me>
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <11cpqih$acf5$1@dont-email.me> <86o6b6kaqf.fsf@linuxsc.com> <11ejr5e$nk8$1@reader2.panix.com> <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>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: David Brown <david.brown@hesbynett.no>
Newsgroups: comp.arch
Subject: Re: reassociation and undefined behaviour
Date: Fri, 4 Dec 2026 09:35:22 +0100
Organization: A noiseless patient Spider
Lines: 59
Message-ID: <11etu0b$1aih9$1@dont-email.me>
References: <11cpqih$acf5$1@dont-email.me> <86o6b6kaqf.fsf@linuxsc.com>
 <11ejr5e$nk8$1@reader2.panix.com> <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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 04 Dec 2026 08:35:24 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="1395241"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/n99+qcugoNbm97SclsUvSfnkU6wV0AQM="; posting-host="f8d429fc41b298b14a9203a87d5618c6"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:EBKKctVYM9LD3ApiLXioy1SAYBs= sha256:FhIWI8SQmFi68gAtN8upmbcnAY1M6/svaxVXJxKZzH4=
	sha1:odpPvqRlY6AdeYImPSN5DjwfPDY= sha256:XfmL7QxQerHj2hwnfITU8Y/SpPejyPclddLQ0iHZMUg=
In-Reply-To: <1796326998-5857@newsgrouper.org>
Content-Language: en-GB, nb-NO