From: Terje Mathisen <terje.mathisen@hda.hydro.com>
Subject: Re: Can POWER4, 5 etc continue where Alpha was stopped?
Date: Wed, 29 Aug 2001 21:32:45 +0200 [thread overview]
Message-ID: <3B8D435C.D5E432A4@hda.hydro.com> (raw)
In-Reply-To: 3B8D26B8.5467626B@hda.hydro.com
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
--
- <Terje.Mathisen@hda.hydro.com>
Using self-discipline, see http://www.eiffel.com/discipline
"almost all programming can be viewed as an exercise in caching"
next prev parent reply other threads:[~2001-08-29 19:32 UTC|newest]
Thread overview: 49+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-08-26 17:14 Can POWER4, 5 etc continue where Alpha was stopped? Nova
2001-08-26 22:30 ` del cecchi
2001-08-28 17:39 ` Niels Jørgen Kruse
2001-08-28 18:47 ` Christer Palm
2001-08-28 20:00 ` Del Cecchi
2001-08-27 1:38 ` Robert Deininger
2001-08-27 17:12 ` Richard Tomkins
2001-08-28 19:02 ` McCalpin
2001-08-28 10:14 ` Bruce Hoult
2001-08-29 0:03 ` McCalpin
2001-08-28 15:27 ` Bruce Hoult
2001-08-29 6:25 ` Terje Mathisen
2001-08-29 11:48 ` George Coulouris
2001-08-29 17:30 ` Terje Mathisen
2001-08-29 19:32 ` Terje Mathisen [this message]
2001-08-29 21:45 ` Maynard Handley
2001-08-31 10:09 ` Terje Mathisen
2001-08-31 16:09 ` Serguei Patchkovskii
2001-09-01 19:51 ` Terje Mathisen
2001-08-28 20:38 ` Andi Kleen
2001-08-28 10:16 ` Bruce Hoult
2001-08-28 20:51 ` Nick Maclaren
2001-08-28 23:04 ` Andi Kleen
2001-08-29 7:59 ` Nick Maclaren
2001-08-30 20:18 ` Wow
2001-08-31 7:47 ` Terry C. Shannon
2001-08-31 9:09 ` Ketil Z Malde
2001-08-31 12:52 ` Terry C. Shannon
2001-08-31 13:14 ` McCalpin
2001-09-03 12:23 ` Alexis Cousein
2001-09-05 1:27 ` del cecchi
2001-09-05 2:07 ` Chris Morgan
2001-09-05 18:13 ` Del Cecchi
2001-09-05 18:57 ` Larry Kilgallen
2001-09-05 12:49 ` Sander Vesik
2001-09-05 14:17 ` McCalpin
2001-09-05 14:59 ` Michael Kraemer
2001-09-05 16:32 ` Pentyern
2001-09-06 0:32 ` del cecchi
2001-09-05 18:08 ` Terry C. Shannon
2001-09-06 13:42 ` Stefaan A Eeckels
2001-09-06 15:08 ` Alexis Cousein
2001-09-06 16:40 ` Mark Smotherman
2001-09-06 21:57 ` Stefaan A Eeckels
2001-09-07 16:22 ` Peter da Silva
2001-08-31 12:20 ` Nick Maclaren
2001-09-03 15:07 ` bad bob
2001-08-28 21:16 ` Greg Lindahl
2001-08-29 0:06 ` McCalpin
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox