🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.arch
Article #1028

Re: reassociation and undefined behaviour

#1028
From: David Brown
Date: Wed, 02 Dec 2026 18:39
52 lines
3707 bytes
On 02/12/2026 17:25, Anton Ertl wrote:
> David Brown <david.brown@hesbynett.no> writes:

>> And "undef" is associative and commutative in expressions, and does not
>> affect the associativity of signed integer addition.
> 
> Addition that overflows into "undef" is not associative.  Consider the
> example above with this kind of addition:
> 
> (-2 + INTMAX) + 1 = "INTMAX-2" + 1 = INTMAX-1
> -2 + (INTMAX + 1) = -2 + "undef" = undef
> 

Sure.  I meant that once you have an undef in the expression, addition 
is associative - undef + (a + b) == (undef + a) + b.

But you are correct that the compiler can't generate the undefs along 
the way in expressions like this if it re-arranges the expressions.  At 
this level, it would not make sense to create undefs partly through 
expressions anyway (unless the whole expression can be calculated at 
compile time) - it's much more efficient to do two's complement addition 
than to check for overflows.

>>
>> The most efficient code is generated from "add_v2_int" - signed
>> integers, with manual parentheses.  Enabling "-fwrapv" results in the
>> same sub-optimal code for "add_v2_int" as for "add_v1_int".
> 
> Yes, when you optimize manually, and have the compiler transformation
> disabled, you get the optimal result (for our definition of
> optimility, which is to minimize the maximum tree-height).

(Note that I am not disabling the compiler transform in any way - it's 
just the way gcc works here.)

> 
> (The normally relevant optimization criterion is execution time,
> space, or both, but let's ignore that for the moment).

Sure.

> 
> The question is why gcc associates addition the way it does when it
> has the choice.  My guess is that it minimizes the simultaneously-live
> values.
> 

That may also be a factor.  I don't see any reason to do so here, but 
it's much easier to criticise how the compiler handles a small sample 
snippet of code than to write an optimiser that works well on real code, 
so I am aware I don't have the whole picture.
Technical Details
Message-ID: <11epol0$3ceif$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-mail
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> <2026Dec2.172552@mips.complang.tuwien.ac.at>

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 19:39:28 +0100
Organization: A noiseless patient Spider
Lines: 52
Message-ID: <11epol0$3ceif$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>
 <2026Dec2.172552@mips.complang.tuwien.ac.at>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 02 Dec 2026 18:39:29 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3553871"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18THMs+nV5PRtORRxtaKKSPc5QgCYZQ12E="; posting-host="8f689f606fabca0b227a4c433cd7c78e"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:DDWd/l7If0E6mczuNFagm2/0Jbg= sha256:fLMN/buy4Oax1DzfyUFTiLYcP4fJtZ0fsXhtr8t/O2w=
	sha1:wT5PDSQuEY4zhNjuQ+cFkNpHWmE= sha256:UcJnYVViB0OXcqQPIjcZ0lZRvwK+zaDUoZ+FbNr7sRo=
Content-Language: en-GB
In-Reply-To: <2026Dec2.172552@mips.complang.tuwien.ac.at>