|
ADS
Algorithms and Data Structures
|
The engineered operations on words and on arrays of words, beyond the book. More...
Files | |
| file | ADS/combinatorial/FastBitwise.h |
| The engineered operations on words and on arrays of words, beyond the book [see Bitwise.h for the broadword tricks of Knuth, which compute the same results by the identities of his section 7.1.3 alone]. | |
Functions | |
| std::uint64_t | ADS::popcount (const Word *w, std::size_t n) |
| the 1 bits of the \( n \) words from \( w \) | |
| std::uint64_t | ADS::popcountAnd (const Word *a, const Word *b, std::size_t n) |
| the 1 bits of the \( n \) words of \( a \,\&\, b \) | |
| Word | ADS::pdep (Word x, Word mask) |
| the low bits of \( x \) to the places of the 1 bits of mask, in order | |
| Word | ADS::pext (Word x, Word mask) |
| the bits of \( x \) at the places of the 1 bits of mask, packed low | |
| int | ADS::select (Word x, int r) |
| the place of the 1 of rank \( r \) of \( x \), from 0 | |
| Word | ADS::mortonEncode (std::uint32_t x, std::uint32_t y) |
| the Morton code of \( ( x , y ) \), the bits of \( x \) in the even places and those of \( y \) in the odd ones | |
| std::pair< std::uint32_t, std::uint32_t > | ADS::mortonDecode (Word z) |
| the \( ( x , y ) \) of the Morton code \( z \) | |
The engineered operations on words and on arrays of words, beyond the book.
The definitions, the invariants and the costs of the area are in Theory of combinatorial algorithms.
|
inlinenodiscard |
the \( ( x , y ) \) of the Morton code \( z \)
In pseudocode (beyond the book), the extraction that deinterleave() of Bitwise.h computes by masks and shifts:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{mortonDecode}( z ) \cr 2 & \quad \textbf{return}\ ( \mathrm{pext}( z , \mathtt{0x5555 \ldots 55} ) , \mathrm{pext}( z , \mathtt{0xAAAA \ldots AA} ) ) \end{array}} \]
|
inlinenodiscard |
the Morton code of \( ( x , y ) \), the bits of \( x \) in the even places and those of \( y \) in the odd ones
In pseudocode (beyond the book), the deposit that interleave() of Bitwise.h computes by masks and shifts:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{mortonEncode}( x , y ) \cr 2 & \quad \textbf{return}\ \mathrm{pdep}( x , \mathtt{0x5555 \ldots 55} ) \mid \mathrm{pdep}( y , \mathtt{0xAAAA \ldots AA} ) \end{array}} \]
the low bits of \( x \) to the places of the 1 bits of mask, in order
In pseudocode (beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{pdep}( x , m ) \cr 2 & \quad r \gets 0;\ b \gets 1 \cr 3 & \quad \textbf{while}\ m \ne 0\ \textbf{do} \cr 4 & \qquad \textbf{if}\ x \,\&\, b \ne 0\ \textbf{then}\ r \gets r \mid ( m \,\&\, -m )\quad \triangleright\ \text{the lowest 1 of } m \cr 5 & \qquad m \gets m \,\&\, ( m - 1 );\ b \gets 2 b \cr 6 & \quad \textbf{return}\ r \end{array}} \]
the bits of \( x \) at the places of the 1 bits of mask, packed low
In pseudocode (beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{pext}( x , m ) \cr 2 & \quad r \gets 0;\ b \gets 1 \cr 3 & \quad \textbf{while}\ m \ne 0\ \textbf{do} \cr 4 & \qquad \textbf{if}\ x \,\&\, m \,\&\, -m \ne 0\ \textbf{then}\ r \gets r \mid b \cr 5 & \qquad m \gets m \,\&\, ( m - 1 );\ b \gets 2 b \cr 6 & \quad \textbf{return}\ r \end{array}} \]
|
inlinenodiscard |
the 1 bits of the \( n \) words from \( w \)
In pseudocode (the method of Harley and Seal after Mula, Kurz and Lemire, beyond the book), \( \nu \) the number of 1 bits, \( V \) the words of a vector of the processor and \( \mathrm{csa}( a , b , c ) \) the full adder of three vectors, which gives the carries \( \mathrm{maj}( a , b , c ) \) and the sums \( a \oplus b \oplus c \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{popcount}( w , n ) \cr 2 & \quad \textbf{if}\ \text{there is no Highway, or } n < 16 \ \textbf{then return}\ \sum_i \nu( w_i ) \text{ in four sums} \cr 3 & \quad o , t , f , e , T \gets 0\quad \triangleright\ \text{the ones,} \text{ twos, fours, eights and the total, by vectors} \cr 4 & \quad \textbf{foreach}\ \text{block of 16 vectors } v_0 , \ldots , v_{15}\ \textbf{do} \cr 5 & \qquad ( t_A , o ) \gets \mathrm{csa}( o , v_0 , v_1 );\ ( t_B , o ) \gets \mathrm{csa}( o , v_2 , v_3 );\ ( f_A , t ) \gets \mathrm{csa}( t , t_A , t_B ) \cr 6 & \qquad ( f_B , t ) \gets \text{the same on } v_4 , \ldots , v_7;\ ( e_A , f ) \gets \mathrm{csa}( f , f_A , f_B ) \cr 7 & \qquad ( e_B , f ) \gets \text{the same on } v_8 , \ldots , v_{15}; \ ( s , e ) \gets \mathrm{csa}( e , e_A , e_B ) \cr 8 & \qquad T \gets T + \nu( s )\quad \triangleright\ \text{the sixteens,} \text{ the only count of the block} \cr 9 & \quad T \gets 16 T + 8 \nu( e ) + 4 \nu( f ) + 2 \nu( t ) + \nu( o ) + \sum \nu( v ) \text{ on the vectors left} \cr 10 & \quad \textbf{return}\ \text{the sum of the lanes of } T + \sum_i \nu( w_i ) \text{ on the words left} \end{array}} \]
the 1 bits of the \( n \) words of \( a \,\&\, b \)
In pseudocode (as popcount() above, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{popcountAnd}( a , b , n ) \cr 2 & \quad \textbf{return}\ \mathrm{popcount}( ( a_i \,\&\, b_i )_{i < n} , n )\quad \triangleright\ \text{each } a_i \,\&\, b_i \text{ formed} \text{ when loaded} \end{array}} \]
|
inlinenodiscard |
the place of the 1 of rank \( r \) of \( x \), from 0
In pseudocode (the compress of the bytes of Highway and the broadword select of Vigna of selectInWord(), beyond the book), \( \beta_j \) the bit \( j \) of \( x \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{select}( x , r ) \cr 2 & \quad \textbf{if}\ \text{the compress of 64 bytes is one} \text{ instruction}\ \textbf{then} \cr 3 & \qquad v \gets \text{the bytes } 0 , \ldots , 63 \text{ compressed} \text{ by the mask } ( \beta_0 , \ldots , \beta_{63} )\quad \triangleright\ \text{the places of the 1s in order} \cr 4 & \qquad \textbf{return}\ \text{the byte } r \text{ of } v \cr 5 & \quad \textbf{return}\ \mathrm{selectInWord}( x , r )\quad \triangleright\ \text{the broadword select} \end{array}} \]
The test of line 2 is on the target of Highway at compile time (AVX-512 VBMI2), and it fails without ADS_WITH_HIGHWAY and in a constant expression; both branches are those of selectInWord()