🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.arch
Article #1043

Re: reassociation and undefined behaviour

#1043
From: Anton Ertl
Date: Thu, 03 Dec 2026 11:20
114 lines
5085 bytes
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.

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.

>So this is not something that can be fully defined behaviour in C, nor 
>something that can even be implementation-defined behaviour - results 
>calculated at compile time may differ from results calculated at run 
>time.

If the language defines it as implementation-defined, a compiler that
does not do what it documents as the implementation-defined behaviour
would be buggy.  And of course a compiler can and should implement at
compile time the same behaviour that it implements at run-time,
whether the behaviour is defined or not; in the latter case it's not a
requirement from the standard, but a quality-of-implementation issue.

>A good solution is to use :
>
>inline uint32_t rol(uint32_t x, uint32_t n) {
>	return (n == 0) ? x : (x << n) | (x >> (32 - n));
>}
>
>A good compiler will generate a single ROL for that

I tried "n==0?x:((x<<n)|(x>>(32-n)))" with clang-3.5, and the code it
generated is

testl %esi, %esi
je    .LBB1_2   
movb  %sil, %cl 
roll  %cl, %edi 
.LBB1_2:        

I.e., clang-3.5

* produces the best possible code for the common idiom "(x<<n) |
  (x>>(32-n))" if n is unknown.

* produces worse code for the alternative you recommend (and that has
  been recommended in discussions I read in 2015 (that's why I threw
  this alternative at the compiler).

If the compiler knows that n=0, the smallest code would be to just
leave %edi unchanged (0 instructions), and it would have resulted in
the intended behaviour (i.e. it produces the same result as invoking
the code produced for the common idiom with n=0).  Instead, clang-3.5
invested extra effort to miscompile the common idiom into

mov $0, %edi

or somesuch.


An alternative workaround for the shortcomings of the C standard and
its implementation by gcc and clang is:

(x<<n)|(x>>((-n)&31))

However, gcc-4.8 compiles this to:

movl %esi, %ecx
negl %ecx      
shrl %cl, %eax 
movl %esi, %ecx
sall %cl, %edi 
orl  %edi, %eax

A version of that that would work for any n and is nicely symmetric is

(x<<(n&31))|(x>>((-n)&31))

but I have not checked which compilers produce which code for that.

>C2y will have a function "stdc_rotate_left" that is specified as though 
>it was (for 32-bit types) :
>
>uint32_t stdc_rotate_left(uint32_t x, uint32_t n) {
>	n &= 0x1f;
>	return (n == 0) ? x : (x << n) | (x >> (32 - n));
>}

Good.

- anton
-- 
'Anyone trying for "industrial quality" ISA should avoid undefined behavior.'
  Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Technical Details
Message-ID: <2026Dec3.122036@mips.complang.tuwien.ac.at>
Path: mbox-mo1.pugleaf.net!srl.newsdeef.eu.MISMATCH!feed-eu-txt.newsdeef.eu!feed-eu-ch.newsdeef.eu!srl.newsdeef.eu!news.hispagatos.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>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srl.newsdeef.eu.MISMATCH!feed-eu-txt.newsdeef.eu!feed-eu-ch.newsdeef.eu!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.arch
Subject: Re: reassociation and undefined behaviour
Date: Thu, 03 Dec 2026 11:20:36 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 114
Message-ID: <2026Dec3.122036@mips.complang.tuwien.ac.at>
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>
Injection-Date: Thu, 03 Dec 2026 12:21:30 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="334702"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX185ZLPOlO9VzSrRJPHpFPWN"; posting-host="b318e488349409aaa87abf1f338dcb0c"
Cancel-Lock: sha1:6VQjPFLlRH76jrqCeZKvfuxkD80= sha256:aec8oEg2tNEtH5/x50jqQcc0M9/Yr9lJY9/L+QQY10E=
	sha1:Yc76tT1k1JD+L1QpJtF1iQXlb34= sha256:zlKBjwszy26w2LuIY9LKZYgl6cRaG3T+yZnQ1pMp8Ro=
X-newsreader: xrn 10.11