From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1164f8,e489d6e4973d889f X-Google-Attributes: gid1164f8,public X-Google-Thread: 115614,e489d6e4973d889f X-Google-Attributes: gid115614,public X-Google-Thread: 102b75,e489d6e4973d889f X-Google-Attributes: gid102b75,public X-Google-ArrivalTime: 2001-08-29 12:40:41 PST Path: archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!news.tele.dk!small.news.tele.dk!148.122.208.68!news2.oke.nextra.no!nextra.com!hydro.com!not-for-mail From: Terje Mathisen Newsgroups: comp.arch,comp.sys.dec,comp.unix.aix Subject: Re: Can POWER4, 5 etc continue where Alpha was stopped? Date: Wed, 29 Aug 2001 21:32:45 +0200 Organization: Hydro Message-ID: <3B8D435C.D5E432A4@hda.hydro.com> References: <88525dde.0108260914.7c63d8c3@posting.google.com> <9mgps0$12ee$1@ausnews.austin.ibm.com> <9mhbh0$pbq$1@ausnews.austin.ibm.com> <3B8C8AC4.59ED22CB@hda.hydro.com> <3B8D26B8.5467626B@hda.hydro.com> NNTP-Posting-Host: iabrahmspc.nho.hydro.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Mozilla 4.77 [en] (Windows NT 5.0; U) X-Accept-Language: en Xref: archiver1.google.com comp.arch:15937 comp.sys.dec:6790 comp.unix.aix:22758 I've been asked in email to explain this algorithm, from the headers it looks a lot like that email was also posted, but just in case I'll keep the originator anonymous. Terje Mathisen wrote: > I believe it was Robert Harley who first showed me how you could use > bitslice operations to combine 15 64-bit words into just 4, and then do > the popcount() on each of those in paralell. > > The resulting code runs at about 1-2 cycles/word, depending upon how > many operations the cpu can handle per cycle. | Do you have detailed description of this algorithm handy? First, Mea Culpa! The speed should have been per byte of input, not per word. Ouch. :-( The algorithm is based on parallel evaluation of full adders. I.e. a full adder has 3 input bits (a, b, carry_in) and returns two result bits (sum, carry_out). This can be implemented with just a few basic logic operations: temp = a xor b sum = temp xor carry_in carry_out = (a and b) or (temp and carry_in) or in C: temp = a ^ b; sum = temp ^ carry_in; carry_out = (a & b) | (temp & carry_in); With a normal three-operand cpu, the code above translates into 5 opcodes, taking 2.5 cycles on a dual-issue machine. So the way to do this using 64 or 128-bit registers is to define macros to do these basic operations, and then combine them, i.e. to go from 7 inputs to 3 takes these operations: (x1, x0) = full_add(i0, i1, i2); (y1, y0) = full_add(i3, i4, i5); (z1, s0) = full_add(x0, y0, i6); (s2, s1) = full_add(x1, y1, z1); This is a total of 4*5=20 instructions, plus 7 load operations. At this point we have three result words (s2, s1, s0), where each set bit in s2 is worth two bits in s1 and four in s0. (Starting with 15 inputs would result in 4 result words, which makes the final bit counting a little easier to parallelize.) s0 = (s0 & MASK1) + ((s0 >> 1) & MASK1); // Max count = 2, MASK1 = 55555555... s1 = (s1 & MASK1) + ((s1 >> 1) & MASK1); s2 = (s2 & MASK1) + ((s2 >> 1) & MASK1); 12 ops s0 = (s0 & MASK2) + ((s0 >> 2) & MASK2); // Max count = 4, MASK2 = 33333333... s1 = (s1 & MASK2) + ((s1 >> 2) & MASK2); s2 = (s2 & MASK2) + ((s2 >> 2) & MASK2); 12 ops (There is another way to do these initial operations which is slightly faster but harder to understand, I believe it dates back to the original MIT hackers.) s0 = (s0 + (s0 >> 4)) & MASK4_8; // Max count = 8, MASK4_8 = 0f0f0f0f... s1 = (s1 + (s1 >> 4)) & MASK4_8; s1 = (s1 + (s1 >> 4)) & MASK4_8; 9 ops s0 = s0 + (s0 >> 8) + (s1 << 1) + (s1 >> 7) + (s2 << 2) + (s2 >> 6); // Max count = 16+32+64 = 112 10 ops s0 = (s0 + (s0 >> 16)) & MASK8_32; // Max count = 224, MASK = 000000ff000000ff 3 ops count = (s0 + (s0 >> 32)) & 511; // Max count = 448 (7 * 64) 3 ops. The total seems to be 76 instructions to count the bits set in 56 bytes, working with 120 input bytes gets even closer to 1 instruction/byte, but this is still 2-4 times slower than a single-cycle popcount() opcode. Terje -- - Using self-discipline, see http://www.eiffel.com/discipline "almost all programming can be viewed as an exercise in caching"