Article View: comp.arch
Article #1020Re: reassociation and undefined behaviour
From: Anton Ertl
Date: Wed, 02 Dec 2026 12:24
Date: Wed, 02 Dec 2026 12:24
47 lines
3267 bytes
3267 bytes
David Brown <david.brown@hesbynett.no> writes: >On 01/12/2026 20:01, Stefan Monnier wrote: >> 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. ... >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. Two other options are: * saturating arithmetics, used for signal processing. It's not associative. * turning the result of an operation with undefined behaviour into an "undef" value or somesuch. Compilers like gcc and clang tend to do this. When it is time to generate code, they can (and do) generate anything for "undef". E.g., I have seen one of these compilers produce 0 as the result of (x<<n)|(x>>(32-n)) when it knew that n==0. >And the compiler knows if and >when it is using wrapping instructions. There's the rub. When the reassociation check occurs, gcc could 1) change the type from an undefined-overflow addition to a wrapping-overflow addition and report that reassociation is possible. But instead, gcc 2) reports that the operation cannot be reassociated. My guess is that at the point of this check it is not easy to find out which of these two alternatives eventually leads to better code, and a gcc maintainer chose to bet on 2). It would be interesting to know on what information this decision is based, but in any case, here we have an example where the more-defined -fwrapv can lead to better code than the less-defined default. - anton -- 'Anyone trying for "industrial quality" ISA should avoid undefined behavior.' Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Technical Details
Message-ID:
<2026Dec2.132411@mips.complang.tuwien.ac.at>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> <11eolqn$2peg5$1@dont-email.me>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: anton@mips.complang.tuwien.ac.at (Anton Ertl) Newsgroups: comp.arch Subject: Re: reassociation and undefined behaviour Date: Wed, 02 Dec 2026 12:24:11 GMT Organization: Institut fuer Computersprachen, Technische Universitaet Wien Lines: 47 Message-ID: <2026Dec2.132411@mips.complang.tuwien.ac.at> 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> Injection-Date: Wed, 02 Dec 2026 12:59:55 +0000 (UTC) Injection-Info: dont-email.me; logging-data="3195716"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18VxXOR5Sabwt2G+pkHpKJY"; posting-host="9e58e1045521d54b1b4af042e3ed402f" Cancel-Lock: sha1:M+5WeJd55WpXCN2tHH+LRGTgIe4= sha256:JjetZW/VSjr88OWx9aus9cHl2G41gkEHxGr4kbUzlSc= sha1:/z8EvFGT3WXH0cjJKT8h2izhgOA= sha256:uOB/6T2sdvEcmu5e27PAh9l9cc1m0oyMqEZBOMMgPBo= X-newsreader: xrn 10.11