ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Bitwise.h File Reference

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. More...

#include "ADS/Contract.h"
#include <cstdint>
#include <utility>
#include "ADS/combinatorial/impl/Bitwise.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

Typedefs

using ADS::Word = std::uint64_t
 a word of 64 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: operations on the 64 bits of a word as on a vector of bits, with a few instructions and no loop over the bits.

The constants are written in hexadecimal with their repeated byte, e.g., \( \mathtt{0x5555 \ldots 55} \) the word of 8 bytes \( \mathtt{0x55} \), and \( \& \), \( \mid \), \( \oplus \), \( \lnot \), \( \gg \) and \( \ll \) are the operations of C++ on the bits.

  • sidewaysSum() counts the 1 bits by adding them in parallel in fields of 2, 4 and 8 bits and then the bytes by one multiplication;
  • ruler() (\( \rho \), the trailing zeros) as the sideways sum of \( x \oplus ( x - 1 ) \) minus 1, lambda() (\( \lambda \), the place of the leading 1) by halving the width, and rightmostOne() and smearRight() by the identities \( x \,\&\, -x \) and \( x \mid ( x - 1 ) \);
  • reverseBits() swaps halves, then quarters, and so on;
  • gray() and grayInverse() convert between binary and Gray code;
  • nextSameWeight() gives the next larger word with as many 1 bits (the trick of Gosper), which enumerates the \( k \)-subsets of 64 items;
  • interleave() and deinterleave() give the Morton code of two coordinates of 32 bits by the perfect shuffle of their bits, whose order keeps nearby points of the plane nearby;
  • hasZeroByte() tells in \( O( 1 ) \) whether one of the 8 bytes of a word is 0, the test that makes strlen() fast.

The contracts [see Contract.h]: ruler() and lambda() need \( x \ne 0 \), and nextSameWeight() a word whose next one does not overflow. The functions are defined in Bitwise.tpp.

See also
D. E. Knuth, The Art of Computer Programming, vol. 4A, Addison-Wesley, 2011, section 7.1.3
H. S. Warren, Hacker's Delight, 2nd ed., Addison-Wesley, 2012
M. Beeler, R. W. Gosper, R. Schroeppel, HAKMEM, MIT AI Memo 239, 1972, item 175 (the next word with as many 1 bits)
F. Gray, Pulse code communication, U.S. Patent 2,632,058, 1953
G. M. Morton, A computer oriented geodetic data base and a new technique in file sequencing, IBM report, Ottawa, 1966
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa