Article View: comp.arch
Article #1017Re: reassociation and undefined behaviour
From: Anton Ertl
Date: Wed, 02 Dec 2026 07:40
Date: Wed, 02 Dec 2026 07:40
53 lines
2926 bytes
2926 bytes
Thomas Koenig <tkoenig@netcologne.de> writes: >Even with OoO, latency is king; reducing it is always good because >if it ends up on the critical path, it will matter. > >Just about the only exception where rearranging a+b+c+d into >(a+b)+(c+d) is not profitable are single-issue processors where >the result is available on the next cacle. There are lots of cases where (a+b)+(c+d) is not the fastest way to get the sum of a,b,c,d. E.g., if a is available by an addition latency (or more) after b, and c and d are available by two addition latencies or more after a, then a+(b+(c+d)) is better. An example where this is easily visible is with linked lists: If you have d = l->val; l = l->next; c = l->val; l = l->next; b = l->val; l = l->next; a = l->val; and the latency of a load is at least as long as the latency of the addition, the optimal order is r=c+d r=b+r r=a+r Of course, if you write that evaluation as usual: r=0; for (; l!=NULL; l = l->next) r += l->val; ... you get this evaluation order without any reassociation. OTOH, for r=0; for (size_t i=0; i<n; i+=stride) r += a[i]; unrolling the loop and reassociating the additions is beneficial on processors that support more than one load per cycle (i.e., modern high-end cores). In any case, the best association depends on the latencies of how the operands are computed. If I don't know that, I would go for (a+b)+(c+d), where the latencies are the same for all operands. - 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.084013@mips.complang.tuwien.ac.at>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu!feed-us-ny.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> <11emvbp$1sc68$3@dont-email.me>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!feed-us-ny.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 07:40:13 GMT Organization: Institut fuer Computersprachen, Technische Universitaet Wien Lines: 53 Message-ID: <2026Dec2.084013@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> <11emvbp$1sc68$3@dont-email.me> Injection-Date: Wed, 02 Dec 2026 08:03:00 +0000 (UTC) Injection-Info: dont-email.me; logging-data="2893249"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+FjdJQX1SZuCCvP2ap+nuV"; posting-host="9e58e1045521d54b1b4af042e3ed402f" Cancel-Lock: sha1:d5XY+j24u0eBdfIUl4LbvJRaAVc= sha256:E6aq/Uahrg1XVYXu49DYMQvKOGdgzVVc+6ytPxs4Oa4= sha1:YI1KdhJ4so5rGmCLhSRBK0IpKP0= sha256:T3NJLzCgVa8U2cpS/K5q9pY/vvBpeJJMVfewfpn7OCA= X-newsreader: xrn 10.11