ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches

The broadword tricks of Knuth (section 7.1.3 of The Art of Computer Programming), beyond the book. More...

Files

file  ADS/combinatorial/Bitwise.h
 The broadword tricks of Knuth (section 7.1.3 of The Art of Computer Programming), beyond the book: operations on the 64 bits of a word as on a vector of bits, with a few instructions and no loop over the bits.

Functions

constexpr int ADS::sidewaysSum (Word x)
 the number of 1 bits of \( x \), \( \nu( x ) \)
constexpr int ADS::ruler (Word x)
 the number of trailing zeros of \( x \), \( \rho( x ) \)
constexpr int ADS::lambda (Word x)
 the place of the leading 1 of \( x \), \( \lambda( x ) = \lfloor \log_2 x \rfloor \)
constexpr Word ADS::rightmostOne (Word x)
 the rightmost 1 of \( x \) alone, 0 if \( x = 0 \)
constexpr Word ADS::smearRight (Word x)
 \( x \) with the 0 bits below its rightmost 1 set, 0 if \( x = 0 \)
constexpr Word ADS::reverseBits (Word x)
 the bits of \( x \) in reverse order
constexpr Word ADS::gray (Word x)
 the Gray code of \( x \)
constexpr Word ADS::grayInverse (Word g)
 the \( x \) whose Gray code is \( g \)
constexpr Word ADS::nextSameWeight (Word x)
 the next larger word with as many 1 bits as \( x \)
constexpr Word ADS::interleave (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
constexpr std::pair< std::uint32_t, std::uint32_t > ADS::deinterleave (Word z)
 the \( ( x , y ) \) of the Morton code \( z \)
constexpr bool ADS::hasZeroByte (Word x)
 whether one of the 8 bytes of \( x \) is 0

Detailed description

The broadword tricks of Knuth (section 7.1.3 of The Art of Computer Programming), beyond the book.

The definitions, the invariants and the costs of the area are in Theory of combinatorial algorithms.

Function documentation

◆ deinterleave()

std::pair< std::uint32_t, std::uint32_t > ADS::deinterleave ( Word z)
nodiscardconstexpr

the \( ( x , y ) \) of the Morton code \( z \)

In pseudocode (Knuth's section 7.1.3, beyond the book), the steps of interleave() backwards:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{deinterleave}( z ) \cr 2 & \quad \textbf{return}\ ( \mathrm{gather}( z ) , \mathrm{gather}( z \gg 1 ) ) \cr 3 & \textbf{function}\ \mathrm{gather}( x ) \cr 4 & \quad x \gets x \,\&\, \mathtt{0x5555 \ldots 55} \cr 5 & \quad \textbf{foreach}\ ( s , m ) \in ( ( 1 , \mathtt{0x3333 \ldots 33} ) , ( 2 , \mathtt{0x0F0F \ldots 0F} ) , ( 4 , \mathtt{0x00FF \ldots 00FF} ) , \cr & \qquad ( 8 , \mathtt{0x0000FFFF0000FFFF} ) , ( 16 , \mathtt{0x00000000FFFFFFFF} ) )\ \textbf{do}\ x \gets ( x \mid ( x \gg s ) ) \,\&\, m \cr 6 & \quad \textbf{return}\ x \end{array}} \]

◆ gray()

Word ADS::gray ( Word x)
nodiscardconstexpr

the Gray code of \( x \)

In pseudocode (Knuth's section 7.1.3, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{gray}( x ) \cr 2 & \quad \textbf{return}\ x \oplus ( x \gg 1 ) \end{array}} \]

◆ grayInverse()

Word ADS::grayInverse ( Word g)
nodiscardconstexpr

the \( x \) whose Gray code is \( g \)

In pseudocode (Knuth's section 7.1.3, beyond the book): each bit of \( x \) is the exclusive or of the bits of \( g \) above it and in its place, a prefix computed by doubling.

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{grayInverse}( g ) \cr 2 & \quad \textbf{foreach}\ s \in ( 1 , 2 , 4 , 8 , 16 , 32 )\ \textbf{do} \ g \gets g \oplus ( g \gg s ) \cr 3 & \quad \textbf{return}\ g \end{array}} \]

◆ hasZeroByte()

bool ADS::hasZeroByte ( Word x)
nodiscardconstexpr

whether one of the 8 bytes of \( x \) is 0

In pseudocode (Knuth's section 7.1.3, beyond the book): the subtraction borrows into the top bit of a byte that is 0, and the top bits of the bytes of \( x \) that are set are masked out.

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{hasZeroByte}( x ) \cr 2 & \quad \textbf{return}\ ( ( x - \mathtt{0x0101 \ldots 01} ) \,\&\, \lnot x \,\&\, \mathtt{0x8080 \ldots 80} ) \ne 0 \end{array}} \]

◆ interleave()

Word ADS::interleave ( std::uint32_t x,
std::uint32_t y )
nodiscardconstexpr

the Morton code of \( ( x , y ) \), the bits of \( x \) in the even places and those of \( y \) in the odd ones

In pseudocode (Knuth's section 7.1.3, beyond the book), the 32 bits of a coordinate spread to the even places by halving the distance of their blocks:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{interleave}( x , y ) \cr 2 & \quad \textbf{return}\ \mathrm{spread}( x ) \mid ( \mathrm{spread}( y ) \ll 1 ) \cr 3 & \textbf{function}\ \mathrm{spread}( x ) \cr 4 & \quad \textbf{foreach}\ ( s , m ) \in ( ( 16 , \mathtt{0x0000FFFF0000FFFF} ) , ( 8 , \mathtt{0x00FF \ldots 00FF} ) , ( 4 , \mathtt{0x0F0F \ldots 0F} ) , \cr & \qquad ( 2 , \mathtt{0x3333 \ldots 33} ) , ( 1 , \mathtt{0x5555 \ldots 55} ) )\ \textbf{do}\ x \gets ( x \mid ( x \ll s ) ) \,\&\, m \cr 5 & \quad \textbf{return}\ x \end{array}} \]

◆ lambda()

int ADS::lambda ( Word x)
nodiscardconstexpr

the place of the leading 1 of \( x \), \( \lambda( x ) = \lfloor \log_2 x \rfloor \)

In pseudocode (Knuth's section 7.1.3, beyond the book), by halving the width of the search:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lambda}( x ) \cr 2 & \quad r \gets 0 \cr 3 & \quad \textbf{foreach}\ s \in ( 32 , 16 , 8 , 4 , 2 , 1 )\ \textbf{do} \cr 4 & \qquad \textbf{if}\ x \gg s \ne 0\ \textbf{then}\ x \gets x \gg s;\ r \gets r + s \cr 5 & \quad \textbf{return}\ r \end{array}} \]

Precondition
\( x \ne 0 \)

◆ nextSameWeight()

Word ADS::nextSameWeight ( Word x)
nodiscardconstexpr

the next larger word with as many 1 bits as \( x \)

In pseudocode (the trick of Gosper, beyond the book): adding the rightmost 1 carries the rightmost run of 1s one place up, and the 1s lost in the carry, but one, go back to the bottom.

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{nextSameWeight}( x ) \cr 2 & \quad u \gets x \,\&\, -x;\ v \gets x + u \cr 3 & \quad \textbf{return}\ v + ( ( ( v \oplus x ) / u ) \gg 2 ) \end{array}} \]

Precondition
\( x \ne 0 \) and the result fits in 64 bits

◆ reverseBits()

Word ADS::reverseBits ( Word x)
nodiscardconstexpr

the bits of \( x \) in reverse order

In pseudocode (Knuth's section 7.1.3, beyond the book), \( m_k \) the mask of the fields of \( 2^k \) bits in the even places (\( \mathtt{0x5555 \ldots 55} \), \( \mathtt{0x3333 \ldots 33} \), \( \mathtt{0x0F0F \ldots 0F} \), \( \mathtt{0x00FF \ldots 00FF} \), \( \mathtt{0x0000FFFF0000FFFF} \)):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{reverseBits}( x ) \cr 2 & \quad \textbf{for}\ k \gets 0\ \textbf{to}\ 4\ \textbf{do}\quad \triangleright\ \text{swap the fields of } 2^k \text{ bits} \cr 3 & \qquad x \gets ( ( x \gg 2^k ) \,\&\, m_k ) \mid ( ( x \,\&\, m_k ) \ll 2^k ) \cr 4 & \quad \textbf{return}\ ( x \gg 32 ) \mid ( x \ll 32 )\quad \triangleright\ \text{the two halves} \end{array}} \]

◆ rightmostOne()

Word ADS::rightmostOne ( Word x)
nodiscardconstexpr

the rightmost 1 of \( x \) alone, 0 if \( x = 0 \)

In pseudocode (Knuth's section 7.1.3, beyond the book), \( -x \) the complement of \( x - 1 \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{rightmostOne}( x ) \cr 2 & \quad \textbf{return}\ x \,\&\, ( \lnot x + 1 ) \end{array}} \]

◆ ruler()

int ADS::ruler ( Word x)
nodiscardconstexpr

the number of trailing zeros of \( x \), \( \rho( x ) \)

In pseudocode (Knuth's section 7.1.3, beyond the book): \( x \oplus ( x - 1 ) \) has a 1 in the place of the rightmost 1 of \( x \) and in each place below it.

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{ruler}( x ) \cr 2 & \quad \textbf{return}\ \mathrm{sidewaysSum}( x \oplus ( x - 1 ) ) - 1 \end{array}} \]

Precondition
\( x \ne 0 \)

◆ sidewaysSum()

int ADS::sidewaysSum ( Word x)
nodiscardconstexpr

the number of 1 bits of \( x \), \( \nu( x ) \)

In pseudocode (Knuth's section 7.1.3, beyond the book), the sums of the fields of 2, 4 and 8 bits formed in place:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{sidewaysSum}( x ) \cr 2 & \quad x \gets x - ( ( x \gg 1 ) \,\&\, \mathtt{0x5555 \ldots 55} )\quad \triangleright\ \text{the fields of 2 bits} \cr 3 & \quad x \gets ( x \,\&\, \mathtt{0x3333 \ldots 33} ) + ( ( x \gg 2 ) \,\&\, \mathtt{0x3333 \ldots 33} )\quad \triangleright\ \text{of 4} \cr 4 & \quad x \gets ( x + ( x \gg 4 ) ) \,\&\, \mathtt{0x0F0F \ldots 0F}\quad \triangleright\ \text{of 8, the bytes} \cr 5 & \quad \textbf{return}\ ( x \cdot \mathtt{0x0101 \ldots 01} ) \gg 56\quad \triangleright\ \text{the sum of the bytes, in the top one} \end{array}} \]

◆ smearRight()

Word ADS::smearRight ( Word x)
nodiscardconstexpr

\( x \) with the 0 bits below its rightmost 1 set, 0 if \( x = 0 \)

In pseudocode (Knuth's section 7.1.3, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{smearRight}( x ) \cr 2 & \quad \textbf{if}\ x = 0\ \textbf{then return}\ 0 \ \textbf{else return}\ x \mid ( x - 1 ) \end{array}} \]