Article View: comp.arch
Article #1051Re: reassociation and undefined behaviour
From: MitchAlsup
Date: Thu, 03 Dec 2026 19:39
Date: Thu, 03 Dec 2026 19:39
80 lines
3986 bytes
3986 bytes
David Brown <david.brown@hesbynett.no> posted:
> 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
> >
>
--------snip----------
>
> There are good reasons for such shifts being UB in C.
But there are no good reasons left why they should be UB in your
<modern> ISA.
> So on ARM, "x >> n" will give 0 for n = 32, but x for n = 256.
That is a little weird.....
> 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.
Not in a well designed ISA.
> 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:
<1796326745-5857@newsgrouper.org>Path:
mbox-mo1.pugleaf.net!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!newsgrouper.org!.POSTED!not-for-mailReferences:
<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!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!newsgrouper.org!.POSTED!not-for-mail From: MitchAlsup <user5857@newsgrouper.org.invalid> Newsgroups: comp.arch Subject: Re: reassociation and undefined behaviour 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> Date: Thu, 03 Dec 2026 19:39:05 GMT Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Message-ID: <1796326745-5857@newsgrouper.org> Injection-Info: newsgrouper.org; mail-complaints-to="newsgrouper@yahoo.com"; posting-account=user5857 Injection-Date: Thu, 03 Dec 2026 19:39:05 GMT User-Agent: Newsgrouper/0.9.0