Article View: comp.arch
Article #1024Re: reassociation and undefined behaviour
From: MitchAlsup
Date: Wed, 02 Dec 2026 17:50
Date: Wed, 02 Dec 2026 17:50
54 lines
2809 bytes
2809 bytes
anton@mips.complang.tuwien.ac.at (Anton Ertl) posted: > 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. This is an argument that HW (not SW) should be the one to reassociate. > - anton
Technical Details
Message-ID:
<1796233819-5857@newsgrouper.org>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!newsgrouper.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> <2026Dec2.084013@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!newsgrouper.org!.POSTED!not-for-mail From: MitchAlsup <user5857@newsgrouper.org.invalid> Newsgroups: comp.arch Subject: Re: reassociation and undefined behaviour 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> <2026Dec2.084013@mips.complang.tuwien.ac.at> Date: Wed, 02 Dec 2026 17:50:19 GMT Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Message-ID: <1796233819-5857@newsgrouper.org> Injection-Info: newsgrouper.org; mail-complaints-to="newsgrouper@yahoo.com"; posting-account=user5857 Injection-Date: Wed, 02 Dec 2026 17:50:19 GMT User-Agent: Newsgrouper/0.9.0