Article View: comp.arch
Article #1115Re: reassociation and undefined behaviour
From: Terje Mathisen
Date: Sat, 05 Dec 2026 22:09
Date: Sat, 05 Dec 2026 22:09
45 lines
3128 bytes
3128 bytes
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 is the result on all x86 cpus since the 386: SHR/SHL x,n are defined to first mask the shift count n by 31, so a shift by 32 is exactly identical to a shift by 0. From other posts here, it seems that ARM also does masking but by 255, ignoring any higher byte parts of the count. Terje -- - <Terje.Mathisen at tmsw.no> "almost all programming can be viewed as an exercise in caching"
Technical Details
Message-ID:
<11f223l$3cpaq$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> <2026Dec3.122036@mips.complang.tuwien.ac.at> <1796326998-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: Terje Mathisen <terje.mathisen@tmsw.no> Newsgroups: comp.arch Subject: Re: reassociation and undefined behaviour Date: Sat, 5 Dec 2026 23:09:58 +0100 Organization: A noiseless patient Spider Lines: 45 Message-ID: <11f223l$3cpaq$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: Sat, 05 Dec 2026 22:09:57 +0000 (UTC) Injection-Info: dont-email.me; logging-data="3564890"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19GUlOmfBp6Wo2Yqpusptq5G2B43lo+G5Z1f62K4t1pPw=="; posting-host="8b09f6055099d41691f39a31b0710612" User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101 Firefox/140.0 SeaMonkey/2.53.24 Cancel-Lock: sha1:DONLoPFrfKhMMJ8ErMQnnE8gF/8= sha256:b5AettwdFsaINaQpSoDj3F3gJQS3mEE6UlMCyBcg810= sha1:n0dSRErKe/VSBEcnMUsUxX+VMhE= sha256:Qp9W2bO1XkRZINWCFHyHoksCwaUiqUZFPjAm/At6b0Y= In-Reply-To: <1796326998-5857@newsgrouper.org>