Article View: comp.arch
Article #1086Re: reassociation and undefined behaviour
From: Stefan Monnier
Date: Fri, 04 Dec 2026 22:29
Date: Fri, 04 Dec 2026 22:29
22 lines
2920 bytes
2920 bytes
David Brown [2026-12-04 10:00:10] wrote: > 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". Agreed in general, but in the case of "right shift by too many bits", there is a very sensible choice. And programmers who *really* want to recover exactly current semantics could use something like `y > 31 ? *(int*)NULL : x >> y` or some other way to tell the compiler that y > 31 means UB. It may occasionally incur an additional cost, but using `x >> (y & 0x1f)` seems cheap enough: in the worst case you pay a single extra instruction and extra cycle. It's still a lot closer to the "speed of light" than what you need to do in C to implement something like "arithmetic with overflow trap", even though it's usually supported much more efficiently in ISAs. === Stefan
Technical Details
Message-ID:
<jwvtsksvgj0.fsf-monnier+comp.arch@gnu.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!.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> <11etveq$1b93u$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!.POSTED!not-for-mail From: Stefan Monnier <monnier@iro.umontreal.ca> Newsgroups: comp.arch Subject: Re: reassociation and undefined behaviour Date: Fri, 04 Dec 2026 17:29:22 -0500 Organization: A noiseless patient Spider Lines: 22 Message-ID: <jwvtsksvgj0.fsf-monnier+comp.arch@gnu.org> 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> <11etveq$1b93u$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Sat, 05 Dec 2026 00:26:31 +0000 (UTC) Injection-Info: dont-email.me; logging-data="2341131"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19MFP09DAYo3z2AlPFiQi6byDPvABDVLyI="; posting-host="e3b09c4b254b81d0e4021aaf72659161" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:sO3YDrXX5cb8+N4hCVBBnoYWfJk= sha1:VGAQmorzigwgOemPj70kBvwBSts= sha256:hGE+IZcSaH7Wpy1aI9+OXPgD4V9EGIA90JrUer8cWuo= sha1:+wiiC5YR0bDKhNKfxpxb9RZIlfo= sha256:URzF33CL8ncUClGZgYY3O2SYJOnBqUXkivibiUVlJ4E= In-Reply-To: <11etveq$1b93u$1@dont-email.me> (message from David Brown <david.brown@hesbynett.no> on Fri, 4 Dec 2026 10:00:10 +0100)