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

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

#include "ADS/Contract.h"
#include "ADS/Simd.h"
#include "ADS/combinatorial/Bitwise.h"
#include "ADS/succinct/BitVector.h"
#include <bit>
#include <cstddef>
#include <cstdint>
#include <utility>
#include "ADS/combinatorial/impl/FastBitwise.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

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 \)

Detailed description

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

  • popcount( w , n ) counts the 1 bits of \( n \) words by the method of Harley and Seal: 16 vectors at a time are added by a tree of carry-save adders, each a full adder of three vectors of bits in five logical operations, and only the vector of the sixteens is counted, so that a block of 16 vectors costs one count instead of 16; a vector is counted by the count of the lanes of Google Highway, one instruction where the processor has it (AVX-512 VPOPCNTDQ, NEON) and otherwise the table of the 16 nibbles in a shuffle of bytes (Mula, Kurz and Lemire). The kernel is that of ADS_WITH_HIGHWAY [see Simd.h], which runs the vectors of the processor found when the program runs; the arrays of fewer than 16 words, and every array without Highway, are counted by the population count of <bit> in four independent sums;
  • popcountAnd( a , b , n ) the 1 bits of the intersection of two arrays in the same way, the size of the intersection of two sets of bits;
  • pdep() and pext() scatter the low bits of \( x \) to the 1 bits of a mask and gather them back, by a loop over the 1 bits of the mask;
  • select( x , r ), the place of the 1 of rank \( r \) of \( x \), is selectInWord() [see BitVector.h];
  • mortonEncode() and mortonDecode() are interleave() and deinterleave() of Bitwise.h, by masks and shifts.

The contracts [see Contract.h]: select( x , r ) needs \( r \) smaller than the number of 1 bits of \( x \). The functions are defined in FastBitwise.tpp.

See also
W. Mula, N. Kurz, D. Lemire, Faster population counts using AVX2 instructions, The Computer Journal 61(1), 111-120, 2018, doi:10.1093/comjnl/bxx046
S. Vigna, Broadword implementation of rank/select queries, WEA 2008, LNCS 5038, 154-168, doi:10.1007/978-3-540-68552-4_12
P. Pandey, M. A. Bender, R. Johnson, A fast x86 implementation of select, arXiv:1706.00990, 2017
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa