Parallel prefix algorithms compute all prefixes of a input sequence in logarithmic time, and are topic of various SIMD and SWAR techniques applied to bitboards^{[1]} . This page provides some basics on simple parallel prefix problems, like parity words and Gray code with some interesting properties, followed by some theoretical background on more complex parallel prefix problems, like Kogge-Stone by Steffan Westcott, and a comparison of three similar Kogge-Stone routines for different purpose.

Parity is often related to error detection and correction. The modulo 2 sum aka exclusive or (xor) of any number of inputs is sufficient to determine even or odd parity. On a bitboard for each bit 0..63 we like to know, whether the number of ones including all trailing bits is odd or even, xi is the bit in x with bit-index i:

p63 indicates whether the whole cardinality is odd or even. The parallel prefix solution looks that way:

x ^= x <<1;
x ^= x <<2;
x ^= x <<4;
x ^= x <<8;
x ^= x <<16;
x ^= x <<32;

and only need log2(64) == 6 steps to perform all the xor instructions.

Surprisingly, thanks to xor, we can restore the initial value by a final:

x ^= x <<1;

Gray Code

The reflected binary code, or Gray code has some similar properties ^{[3]}. The Gray code is defined by the modulo 2 sum or exclusive or of a word with its value shifted right by one.

gray = x ^(x >>1);

This ensures that consecutive Gray codes have only one bit changed, that is a Hamming distance of one. For instance for a 4-bit or nibble Gray code:

x decimal

x binary

x>>1

Gray bin

decimal

0

0000

0000

0000

0

1

0001

0000

0001

1

2

0010

0001

0011

3

3

0011

0001

0010

2

4

0100

0010

0110

6

5

0101

0010

0111

7

6

0110

0011

0101

5

7

0111

0011

0100

4

8

1000

0100

1100

12

9

1001

0100

1101

13

10

1010

0101

1111

15

11

1011

0101

1110

14

12

1100

0110

1010

10

13

1101

0110

1011

11

14

1110

0111

1001

9

15

1111

0111

1000

8

0

0000

0000

0000

0

Decoding Gray code is a parallel prefix problem similar to the ring-wise operations of the Parity words. For a nibble Gray code:

x = gray;
x ^= x >>2;
x ^= x >>1;

Or for 64-bit Gray-Codes:

x = gray;
x ^= x >>32;
x ^= x >>16;
x ^= x >>8;
x ^= x >>4;
x ^= x >>2;
x ^= x >>1;

U64 nortFill(U64 gen){
gen |=(gen <<8);
gen |=(gen <<16);
gen |=(gen <<32);return gen;}
U64 soutFill(U64 gen){
gen |=(gen >>8);
gen |=(gen >>16);
gen |=(gen >>32);return gen;}

This is actually a simplified Kogge-Stone algorithm, whose general form even considers a second bitboard to occlude the flood accordantly.

Given the terms x1, x2, x3, ... , xN and an associative operator #
find the values q1 = x1
q2 = x1 # x2
q3 = x1 # x2 # x3
.
.
.
qN = x1 # x2 # x3 # ... # xN

Associative expressions can be bracketed any way you like, the result is the same. To see why this is interesting for chess programming, let's define x1, x2, ... and # to be

xI = < gI, pI > (a two element tuple)
where gI = square aI has a rook on it
and pI = square aI is empty
for all I = 1, 2, 3, ... , 8
and xI # xJ = < gI, pI > # < gJ, pJ >
= < ((gI && pJ) || gJ), (pI && pJ) >

All this algebra looks very scary at first, so here's an example:

The tuples g and p are known as the "generator" and "propagator" terms in the literature of fast carry propagation circuits. But we are not dealing with carry bits in a carry propagate adder! Here, we are generating and propagating upward rook attacks along a file of a chessboard.

Why all this theory? Well, prefix computation is a heavily researched area, researched by many folk smarter than me :) Its of interest because it has many applications, such as VLSI design, pattern matching, and others. There are many different ways of going about it, with different implementation characteristics.

U64 FillUpOccluded(U64 g, U64 p){
g |= p &(g <<8);
p &=(p <<8);
g |= p &(g <<16);
p &=(p <<16);return g |= p &(g <<32);}
U64 RookAttacksUp(U64 rooks, U64 empty_squares){return ShiftUp(FillUpOccluded(rooks, empty_squares));}

The method chosen in FillUpOccluded() is based on a Kogge-Stone parallel prefix network, because it can be implemented very easily in software. The diagram below (trust me, it really _is_ supposed to look like that) is an illustration of how it works. The corresponding lines of program code are shown on the right. The inputs are fed into the network at the top, pass along the connecting lines, are combined by the # operator at various points, and the outputs appear at the bottom.

To convince yourself this works, select any output qI and trace upwards from the bottom of the diagram. You'll see it leads back to x1, x2, ... , xI, so qI is formed by some computation of x1 # x2 # ... # xI.

[Implementor's note : The 2 program lines that assign to variable p are not strictly correct. By the end of the routine, p1 and p2 have been trashed (reset). However, they are trashed after they can affect the correctness of the routine result.]

Add/Sub versus Attacks

As a comparison three Kogge-Stone routines. A "classical" byte-wise SWAR adder, byte-wise subtraction, and byte-wise rook-attacks in east direction. Note that the inner parallel prefix instructions are the same in all three routines:

gen |= pro &(gen <<1);
pro &=(pro <<1);
gen |= pro &(gen <<2);
pro &=(pro <<2);
gen |= pro &(gen <<4);

The difference are the assignments of generator and propagator, and the final result.

Add

4-bit Kogge-Stone adder

The software approach of a Kogge-Stonehardware adder^{[5]} , requires the initial carry bits as generator, thus the intersection of both summands, while the propagator is the modulo 2 sum, the exclusive or of both summands. To avoid inter byte overflows, the propagator has the least significant bits of each byte cleared. However the final carries need one further masked shift and the modulo two sum with the initial propagator.

// SIMD bytewise add a + b
U64 koggeStoneByteAdd(U64 a, U64 b){
U64 gen, pro;
gen = a & b;
pro = a ^ b;
pro &= ~aFile;
gen |= pro &(gen <<1);
pro &=(pro <<1);
gen |= pro &(gen <<2);
pro &=(pro <<2);
gen |= pro &(gen <<4);
gen =~aFile &(gen <<1);return gen ^ a ^ b;}

Sub

Subtraction is based on the two's complement which is ones' complement plus one aka the A-File assuming little-endian rank-file mapping. Thus, it is actually the sum of {a, ~b, one}, which is considered in calculating propagator and generator, which is the majority of all three inputs. Note that

gen =~aFile &(gen <<1);return gen ^ a ^ ~b ^ aFile;

can be re-written like in following routine:

// SIMD bytewise sub a - b
U64 koggeStoneByteSub(U64 a, U64 b){
U64 gen, pro;
gen = a & ~b;// based on -b = ~b + 1
pro = a ^ ~b;
gen |= aFile & pro;// majority
gen |= pro &(gen <<1);
pro &=(pro <<1);
gen |= pro &(gen <<2);
pro &=(pro <<2);
gen |= pro &(gen <<4);
gen = aFile |(gen <<1);return gen ^ a ^ ~b;}

East Attacks

Here the Kogge-Stone algorithm generates the sliding piece attacks along the empty squares. Generator are the rooks or queens, propagator the empty squares without the A-file to avoid wraps.

U64 eastAttacks(U64 occ, U64 rooks){
U64 gen, pro;
pro = ~occ & ~aFile;
gen = rooks;
gen |= pro &(gen <<1);
pro &=(pro <<1);
gen |= pro &(gen <<2);
pro &=(pro <<2);
gen |= pro &(gen <<4);
gen =~aFile &(gen <<1);return gen;}

Peter M. Kogge, Harold S. Stone (1973). A Parallel Algorithm for the Efficient Solution of a General Class of Recurrence Equations. IEEE Transactions on Computers, 1973, C-22, 783-791, pdf

Home * Programming * Algorithms * Parallel Prefix AlgorithmsParallel prefix algorithmscompute all prefixes of a input sequence in logarithmic time, and are topic of various SIMD and SWAR techniques applied to bitboards^{[1]}. This page provides some basics on simple parallel prefix problems, like parity words and Gray code with some interesting properties, followed by some theoretical background on more complex parallel prefix problems, like Kogge-Stone by Steffan Westcott, and a comparison of three similar Kogge-Stone routines for different purpose.^{[2]}## Table of Contents

## Parity Words

Parity is often related to error detection and correction. The modulo 2 sum aka exclusive or (xor) of any number of inputs is sufficient to determine even or odd parity. On a bitboard for each bit 0..63 we like to know, whether the number of ones including all trailing bits is odd or even, xi is the bit in x with bit-index i:Or recursively

p63 indicates whether the whole cardinality is odd or even. The parallel prefix solution looks that way:

and only need log2(64) == 6 steps to perform all the xor instructions.

Surprisingly, thanks to xor, we can restore the initial value by a final:

## Gray Code

The reflected binary code, or Gray code has some similar properties^{[3]}. The Gray code is defined by the modulo 2 sum or exclusive or of a word with its value shifted right by one.This ensures that consecutive Gray codes have only one bit changed, that is a Hamming distance of one. For instance for a 4-bit or nibble Gray code:

Decoding Gray code is a parallel prefix problem similar to the ring-wise operations of the Parity words. For a nibble Gray code:

Or for 64-bit Gray-Codes:

## Fill Stuff

Parallel prefix front and rear-fills, for instance to determine pawn spans in bitboards, are quite simple to understand.This is actually a simplified Kogge-Stone algorithm, whose general form even considers a second bitboard to occlude the flood accordantly.

## Elaborations on Kogge-Stone

Elaborations on Kogge-Stone parallel prefix algorithm by Steffan Westcott^{[4]}:A

parallel prefixproblem is of the sort:Associative expressions can be bracketed any way you like, the result is the same. To see why this is interesting for chess programming, let's define x1, x2, ... and # to be

All this algebra looks very scary at first, so here's an example:

In general,

and so

The tuples g and p are known as the "generator" and "propagator" terms in the literature of fast carry propagation circuits. But we are not dealing with carry bits in a carry propagate adder! Here, we are generating and propagating upward rook attacks along a file of a chessboard.

Why all this theory? Well, prefix computation is a heavily researched area, researched by many folk smarter than me :) Its of interest because it has many applications, such as VLSI design, pattern matching, and others. There are many different ways of going about it, with different implementation characteristics.

The method chosen in FillUpOccluded() is based on a Kogge-Stone parallel prefix network, because it can be implemented very easily in software. The diagram below (trust me, it really _is_ supposed to look like that) is an illustration of how it works. The corresponding lines of program code are shown on the right. The inputs are fed into the network at the top, pass along the connecting lines, are combined by the # operator at various points, and the outputs appear at the bottom.

To convince yourself this works, select any output qI and trace upwards from the bottom of the diagram. You'll see it leads back to x1, x2, ... , xI, so qI is formed by some computation of x1 # x2 # ... # xI.

[Implementor's note : The 2 program lines that assign to variable p are not strictly correct. By the end of the routine, p1 and p2 have been trashed (reset). However, they are trashed after they can affect the correctness of the routine result.]

## Add/Sub versus Attacks

As a comparison three Kogge-Stone routines. A "classical" byte-wise SWAR adder, byte-wise subtraction, and byte-wise rook-attacks in east direction. Note that the inner parallel prefix instructions are the same in all three routines:The difference are the assignments of generator and propagator, and the final result.

## Add

The software approach of a Kogge-Stone hardware adder

^{[5]}, requires the initial carry bits as generator, thus the intersection of both summands, while the propagator is the modulo 2 sum, the exclusive or of both summands. To avoid inter byte overflows, the propagator has the least significant bits of each byte cleared. However the final carries need one further masked shift and the modulo two sum with the initial propagator.## Sub

Subtraction is based on the two's complement which is ones' complement plus one aka the A-File assuming little-endian rank-file mapping. Thus, it is actually the sum of {a, ~b, one}, which is considered in calculating propagator and generator, which is the majority of all three inputs. Note thatcan be re-written like in following routine:

## East Attacks

Here the Kogge-Stone algorithm generates the sliding piece attacks along the empty squares. Generator are the rooks or queens, propagator the empty squares without the A-file to avoid wraps.The mentioned routines also demonstrate, that this east attacking direction may cheaper implemented with Fill by Subtraction based on SWAR-wise techniques and subtracting a rook from a blocking piece:

However, due to various left and right shifts, Kogge-Stone can deal with all other seven sliding directions as well.

## See also

## Publications

1973).A Parallel Algorithm for the Efficient Solution of a General Class of Recurrence Equations. IEEE Transactions on Computers, 1973, C-22, 783-791, pdf1980).Parallel Prefix Computation. pdf via CiteSeerX1985).The power of parallel prefix. IEEE Transactions on Computers, Vol. C-342006).Parallel Prefix (Scan) Algorithms for MPI. in EuroPVM/MPI 2006, LNCS, pdf2009).Introduction to parallel & distributed algorithms. On-line Book## Forum Posts

## External Links

## References

2009).Introduction to parallel & distributed algorithms. On-line Book## What links here?

Up one Level