Article View: comp.arch
Article #1018Re: reassociation and undefined behaviour
From: David Brown
Date: Wed, 02 Dec 2026 08:45
Date: Wed, 02 Dec 2026 08:45
50 lines
3461 bytes
3461 bytes
On 01/12/2026 20:01, Stefan Monnier wrote: > David Brown [2026-12-01 12:53:59] wrote: >> And in this particular case, in both C and Fortran, addition is fully >> commutative and associative for both signed and unsigned types in the >> standard language. > > Is it? AFAIK, in C > > (-2 + INTMAX) + 1 > > is not the same as > > -2 + (INTMAX + 1) > > because the second two bumps into a UB while the first doesn't. Sort of, but... > > Of course, UB allows the compiler to choose to give the same > semantics to both forms (e.g. using wraparound). > Exactly. If the generated code uses some kind of "trap on overflow" addition instruction, then the compiler may not change the first expression into the second. But if the compiler is generating two's complement wrapping instructions, it certainly can do so. And the compiler knows if and when it is using wrapping instructions. That's how it knows it can turn "(-2 + x) + 1" into "x - 1" that will give the correct result whenever there is no UB in the original expression. "UB" is something you might have in the original source code. Compilers can do any manipulation and re-arrangement of expressions as long as any input in the original expression that did not lead to UB, gives the same results in the generated code. (Sometimes you might consider a particular re-arrangement unhelpful, but that's a different matter from correctness.) Remember that the original source code is in "real C", while any re-arrangements the compiler makes are in "internal compiler language" that can have different semantics or details - as long as the final results match up on observable behaviour in the end. And the semantics of this "internal compiler language" don't have to be consistent - in one expression, the compiler might treat signed integer overflow as wrapping, and in another it might treat it as resulting in the number 42.
Technical Details
Message-ID:
<11eolqn$2peg5$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu.MISMATCH!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.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>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu.MISMATCH!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.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: Wed, 2 Dec 2026 09:45:11 +0100 Organization: A noiseless patient Spider Lines: 50 Message-ID: <11eolqn$2peg5$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> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 02 Dec 2026 08:45:11 +0000 (UTC) Injection-Info: dont-email.me; logging-data="2931205"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19mkHUDDUaw/XPJAlFemJrTm4iCZZJFsxM="; posting-host="8f689f606fabca0b227a4c433cd7c78e" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:X9PVlRAVXqWl4ZPmWfu1qdKIVF4= sha256:useuwxncxI6ckjJlfIicbn4ejdmYmdV6cmXxv4jYjDs= sha1:SnzByPy9JutmJrDL16JiRSvTcuQ= sha256:aYzN/e0mAhjzx6Pi5tuFJmcXjQqjy5FfO2RWEASEa4E= In-Reply-To: <jwvbj74bzm4.fsf-monnier+comp.arch@gnu.org> Content-Language: en-GB, nb-NO