Article View: comp.arch
Article #1067Re: reassociation and undefined behaviour
From: David Brown
Date: Fri, 04 Dec 2026 09:00
Date: Fri, 04 Dec 2026 09:00
73 lines
5053 bytes
5053 bytes
On 03/12/2026 20:39, MitchAlsup wrote: > 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. What ISA's are "modern" ? They are all made compatible with older ones. I agree that UB should be found sparingly, if at all, in actual hardware ISAs. UB is a concept that makes sense in a programming language for a variety of reasons, such as "there is no sensible choice of correct answer or behaviour", "defining a behaviour would significantly limit efficiency of other useful correct code on some hardware", "this situation can only result from a bug in the program", "we know this is wrong, but it is impractical, impossible, or too costly to check it at runtime". People have differing opinions on what should or should not be UB in C, and how it should be avoided or handled - so let's not talk about the details! In an ISA, "UB" means the situation has no documented behaviour. A common use of UB in ISAs has been for opcodes that do not match any documented instruction. This has the big benefit that later expansions of the ISA with new instructions can use that opcode space with less fuss than if behaviour had been specified. Even a specification of "take an unimplemented instruction trap" limits later ISA versions or can cause compatibility issues for older code, though of course such a trap system can also help compatibility. ARM documents a number of instructions or combinations as "unpredictable", which I think means that /users/ should view it as entirely undefined behaviour, but the chip designers have made sure there are limits to what happens. (I.e., you might jump to an unspecified address, or get unspecified values in some registers - but you won't execute an HCF or jump from user mode to supervisor mode.) An example of "unpredictable" behaviour is trying to do a shift instruction on the program counter register. Of course for an ISA's shift instructions (on normal registers) the behaviour should be fully defined. But there is no universal agreement on what that behaviour should be, outside the range 0 to 31. Ensuring that a shift of 32 bits gives zero takes extra hardware - that means more space, more power, more latency. Ensuring that shifts of anything greater than 32 result in zero takes even more additional hardware. Maybe on a modern processor, this extra would be completely negligible, but in the past it was something worth avoiding. And now making a shift of 32-bit result in 0 for a new processor using an established ISA would mean an incompatibility with older devices - that is much more of an issue. > >> So on ARM, "x >> n" will give 0 for n = 32, but x for n = 256. > > That is a little weird..... >
Technical Details
Message-ID:
<11etveq$1b93u$1@dont-email.me>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!.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> <1796326745-5857@newsgrouper.org>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!.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 10:00:10 +0100 Organization: A noiseless patient Spider Lines: 73 Message-ID: <11etveq$1b93u$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> <1796326745-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 09:00:11 +0000 (UTC) Injection-Info: dont-email.me; logging-data="1418366"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+9xIuZohl3MsRaqoflgnVLqCmPUuuPJM0="; posting-host="f8d429fc41b298b14a9203a87d5618c6" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:Zfk6bYkTxwujp61qtyXsAldry/g= sha256:1nUELqTxaLlhO16E4QDU90BR327LY+sYGbTabOkO4hw= sha1:7nZ7U1gK+TzibSz1CVW6ORPkn40= sha256:lsBouVPQzNmtmwHiNkZtsJNz8U4BSjZMqyuYvRXMZy8= In-Reply-To: <1796326745-5857@newsgrouper.org> Content-Language: en-GB, nb-NO