🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.arch
Article #1042

Re: reassociation and undefined behaviour

#1042
From: David Brown
Date: Thu, 03 Dec 2026 10:36
88 lines
5297 bytes
On 03/12/2026 03:07, MitchAlsup wrote:
> David Brown <david.brown@hesbynett.no> posted:
> 
>> On 02/12/2026 13:24, Anton Ertl wrote:
>>> David Brown <david.brown@hesbynett.no> writes:
>>>> On 01/12/2026 20:01, Stefan Monnier wrote:
> ----------------------------------
> 
>> 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.

But if the compiler knows things about "n" and "x" at compile time, it 
might do something different.  If it knows "n" is 8 or 16, maybe some 
kind of byte permutation instruction is faster.  If it knows "n" is 1 
and "x" does not have its high bit set, and "x" is in a suitable 
register, perhaps some kind of "lea" instruction to do an addition using 
an address execution unit can be scheduled better when integer units are 
otherwise used.  Such optimisations may be beyond current compilers, but 
they are certainly allowed.

And if the compiler knows "n" is outside the range 1 to 31, the 
expression can be replaced with "undef".

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.  Compilers will typically use 0 for "x >> n" 
when n is known at compile-time to be 0, and that is a result 
programmers would often expect.  But if a "r0 = r1 >> r2" instruction is 
generated, some processors (like x86) will mask r2 to 5 bits and return 
x.  Others will return 0.  ARM masks r2 to 8 bits, then returns 0 for if 
the result is 32 or higher.  So on ARM, "x >> n" will give 0 for n = 32, 
but x for n = 256.

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.  It is UB.  So what happens when you evaluate "x >> n" for 
out-of-range n, including n == 32, is not guaranteed in any way.

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, and it is fully 
defined for n = 0 to 31.  You still should not try to use n > 31.

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));
}

On x86, this is a rotate instruction.  On ARM, it is a mask and then a 
rotate.  C++ has this as std::rotl.





> --------------
>> I suspect (but have not thought it through in detail) that some kinds of
>> strength reduction or other expression simplification could result in
>> code where some operations in the internal re-arranged expression would
>> have new UB if evaluated with "UB on overflow" semantics.
> 
> Older ISA would avoid UB by defining 2× MUL and 2× DIV to avoid Overflow
> 
>       MULD    {Rt1,Rt2},Ra,Rb
>       DIV     {Rrem,Rquo},{Rt1,Rt2},Rdiv
> 
That's not why ISA's often have doubled-up multiply and divide.  I've 
seen some ISA's documented with undefined behaviour, but not for signed 
integer overflows.  Processors will have instructions for 32x32 -> 64 
bit multiply, or whatever, because such instructions are useful to users 
and make handling of larger integer types much more efficient.  What is 
and is not defined behaviour in C has nothing to do with it.
Technical Details
Message-ID: <11ergn7$6utu$1@dont-email.me>
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>

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: David Brown <david.brown@hesbynett.no>
Newsgroups: comp.arch
Subject: Re: reassociation and undefined behaviour
Date: Thu, 3 Dec 2026 11:36:22 +0100
Organization: A noiseless patient Spider
Lines: 88
Message-ID: <11ergn7$6utu$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 03 Dec 2026 10:36:23 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="228286"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/sLsmpqgKNDUPvwzatlzr9YAPaSE7mI2Y="; posting-host="afe1cf6b446da7dc61b87fcc2868235d"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:iRZzaq8fIl+emEDP/iHP7dXL3Rc= sha256:k4YvRIjOiQvOg4vAroj3PO3VXQobk0b2YQqNN+ly4bU=
	sha1:l78QxXbGWcMH6vRRsFYqWCIQ4iA= sha256:s4rgu+YADQ6KUnzMubw4au5kyNncmFgB2FO7lJ4wu5c=
Content-Language: en-GB, nb-NO
In-Reply-To: <1796263648-5857@newsgrouper.org>