Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #30412
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!news2.arglkargh.de!news.swapon.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Joshua Cranmer 🐧 <Pidgeot18@verizon.invalid> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: just for fun: JSL: "q*100" hand-optimized to "(q<<6)+(q<<5)+(q<<2)" |
| Date | Tue, 25 Nov 2014 18:30:41 -0600 |
| Organization | A noiseless patient Spider |
| Lines | 47 |
| Message-ID | <m536v4$2bo$1@dont-email.me> (permalink) |
| References | <slrnm7770k.uoi.avl@login.logic.tuwien.ac.at> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| Injection-Date | Wed, 26 Nov 2014 00:30:28 +0000 (UTC) |
| Injection-Info | mx02.eternal-september.org; posting-host="8cedddb29338592f734c41b16a68fadc"; logging-data="2424"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19Ynf8q/OW0UfrhC0JPBUTLBHhvnVBqtmw=" |
| User-Agent | Mozilla/5.0 (Windows NT 6.3; WOW64; rv:34.0) Gecko/20100101 Thunderbird/34.0 |
| In-Reply-To | <slrnm7770k.uoi.avl@login.logic.tuwien.ac.at> |
| Cancel-Lock | sha1:wLEn4Dr6luMP6KIxXo74/lR3y3w= |
| Xref | csiph.com comp.lang.java.programmer:30412 |
Show key headers only | View raw
On 11/24/2014 2:58 PM, Andreas Leitgeb wrote: > Out of idle curiosity I stepped into StringBuilder.append(int), > and reached non-public Integer.getChars(int,int,char[]). > > " // really: r = i - (q * 100); > " r = i - ((q << 6) + (q << 5) + (q << 2)); > > Is this some relict from early early days, or are 5 operations > (3 "<<" and 2 "+") still faster than one multiplication on > modern cpus? Or did just no one ever care to revise it? According to Agner's timing tables, a Nehalem processor has latency 3 for MUL, and it can only execute in one of the superscalar lanes. Sandy Bridge, Ivy Bridge, and Haswell don't do better here. Adds and shifts both have latency 1, and can execute in any of the two or three lanes for integer operations. The numbers for AMD chips seem comparable, but I understand their internal microarchitecture less well. On the Atom and Silvermont (mobile) architectures, it looks like shifts only get one port. MUL is 5 cycle latency on Silvermont but 3 (both for 32-bit), adds and shifts are of course 1. I don't have timings for ARM, but my recollection was that multiply times sucked there. In summary: replacing a multiply with a single shift is always better; replacing it with two shifts is also always better (it uses fewer ports, even if it takes as long as a multiply); and replacing it with three shifts is no overall improvement, probably, but it may allow for better instruction scheduling in a big block, since the instructions are finer granularity and can be spread out more. Note that all of these assume word-sized multiplies. If you're doing a 64-bit multiply on 32-bit hardware, shifts are much faster since a 64-bit shift is 4 operations with a critical path of 2 whereas a 64-bit multiply is 3 multiplies and two adds with a critical path length of 3. And multiplies generally cannot be done in parallel. > PS: even if it *is* still faster, I'd expect the JIT/hotspot > to do that optimization on its own... Quite frankly, yes. Optimizing constant multiplies/divisions is one of the more fundamental optimizations in a toolkit. -- Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
just for fun: JSL: "q*100" hand-optimized to "(q<<6)+(q<<5)+(q<<2)" Andreas Leitgeb <avl@auth.logic.tuwien.ac.at> - 2014-11-24 20:58 +0000
Re: just for fun: JSL: "q*100" hand-optimized to "(q<<6)+(q<<5)+(q<<2)" Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-11-24 16:31 -0500
Re: just for fun: JSL: "q*100" hand-optimized to "(q<<6)+(q<<5)+(q<<2)" Andreas Leitgeb <avl@auth.logic.tuwien.ac.at> - 2014-11-26 12:39 +0000
Re: just for fun: JSL: "q*100" hand-optimized to "(q<<6)+(q<<5)+(q<<2)" Marcel Mueller <news.5.maazl@spamgourmet.org> - 2014-11-25 23:11 +0100
Re: just for fun: JSL: "q*100" hand-optimized to "(q<<6)+(q<<5)+(q<<2)" Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-11-25 17:45 -0500
Re: just for fun: JSL: "q*100" hand-optimized to "(q<<6)+(q<<5)+(q<<2)" Joshua Cranmer 🐧 <Pidgeot18@verizon.invalid> - 2014-11-25 18:39 -0600
Re: just for fun: JSL: "q*100" hand-optimized to "(q<<6)+(q<<5)+(q<<2)" Joshua Cranmer 🐧 <Pidgeot18@verizon.invalid> - 2014-11-25 18:30 -0600
Re: just for fun: JSL: "q*100" hand-optimized to "(q<<6)+(q<<5)+(q<<2)" Andreas Leitgeb <avl@auth.logic.tuwien.ac.at> - 2014-11-26 08:53 +0000
Re: just for fun: JSL: "q*100" hand-optimized to "(q<<6)+(q<<5)+(q<<2)" Joshua Cranmer 🐧 <Pidgeot18@verizon.invalid> - 2014-11-26 08:10 -0600
Re: just for fun: JSL: "q*100" hand-optimized to "(q<<6)+(q<<5)+(q<<2)" Joerg Meier <joergmmeier@arcor.de> - 2014-11-26 17:30 +0100
Re: just for fun: JSL: "q*100" hand-optimized to "(q<<6)+(q<<5)+(q<<2)" Joerg Meier <joergmmeier@arcor.de> - 2014-11-26 17:25 +0100
Re: just for fun: JSL: "q*100" hand-optimized to "(q<<6)+(q<<5)+(q<<2)" Joshua Cranmer 🐧 <Pidgeot18@verizon.invalid> - 2014-11-27 12:17 -0600
Re: just for fun: JSL: "q*100" hand-optimized to "(q<<6)+(q<<5)+(q<<2)" Kevin McMurtrie <mcmurtrie@pixelmemory.us> - 2014-11-27 16:39 -0800
Re: just for fun: JSL: "q*100" hand-optimized to "(q<<6)+(q<<5)+(q<<2)" Andreas Leitgeb <avl@auth.logic.tuwien.ac.at> - 2014-11-29 15:17 +0000
csiph-web