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

The binary packing of Lemire and Boytsov, SIMD-BP128, for integers of 32 bits. More...

Files

file  ADS/compression/BinaryPacking.h
 The binary packing of Lemire and Boytsov, SIMD-BP128, for integers of 32 bits: the sequence is cut into blocks of 128 integers, and a block is written with \( b \) bits per integer, \( b \) the bits of its largest one, so that it takes \( 16 b \) bytes; the widths of 16 blocks are 16 bytes before them.

Functions

std::vector< std::uint8_t > ADS::binaryPack (std::span< const std::uint32_t > x, bool sorted=false)
 the binary packing of \( x \) in blocks of 128 integers, of their differences at a distance of 4 if sorted
std::vector< std::uint32_t > ADS::binaryUnpack (std::span< const std::uint8_t > in, std::size_t n, bool sorted=false)
 the \( n \) integers of the code in, packed with the same sorted
std::vector< std::uint8_t > ADS::patchedPack (std::span< const std::uint32_t > x, bool sorted=false)
 the patched binary packing (PFor) of \( x \) in blocks of 128 integers, of their differences at a distance of 4 if sorted
std::vector< std::uint32_t > ADS::patchedUnpack (std::span< const std::uint8_t > in, std::size_t n, bool sorted=false)
 the \( n \) integers of the patched code in, packed with the same sorted

Detailed description

The binary packing of Lemire and Boytsov, SIMD-BP128, for integers of 32 bits.

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

Function documentation

◆ binaryPack()

std::vector< std::uint8_t > ADS::binaryPack ( std::span< const std::uint32_t > x,
bool sorted = false )
inlinenodiscard

the binary packing of \( x \) in blocks of 128 integers, of their differences at a distance of 4 if sorted

In pseudocode (SIMD-BP128 of Lemire and Boytsov, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{binaryPack}( x , \mathit{sorted} ) \cr 2 & \quad \textbf{if}\ \mathit{sorted}\ \textbf{then foreach}\ i \ \textbf{do}\ v_i \gets x_i - x_{i-4}\ \textbf{else}\ v \gets x \cr 3 & \quad \text{fill the last block of } v \text{ with } 0 \text{s} \cr 4 & \quad \textbf{foreach}\ \text{block } k \text{ of 128 integers of } v\ \textbf{do} \cr 5 & \qquad \textbf{if}\ k \bmod 16 = 0\ \textbf{then}\ \text{write the} \text{ widths of the blocks } k , \ldots , k + 15 \cr 6 & \qquad b_k \gets \mathrm{bitwidth}( \text{the or of the block} ); \ \text{pack the block in } 16 b_k \text{ bytes, in 4 lanes} \end{array}} \]

Precondition
if sorted, \( x \) is non-decreasing (audit)
Postcondition
binaryUnpack( result , x.size() , sorted ) is \( x \) (audit)

◆ binaryUnpack()

std::vector< std::uint32_t > ADS::binaryUnpack ( std::span< const std::uint8_t > in,
std::size_t n,
bool sorted = false )
inlinenodiscard

the \( n \) integers of the code in, packed with the same sorted

In pseudocode (SIMD-BP128, beyond the book), \( p \) the last 4 integers decoded:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{binaryUnpack}( y , n , \mathit{sorted} ) \cr 2 & \quad p \gets ( 0 , 0 , 0 , 0 ) \cr 3 & \quad \textbf{foreach}\ \text{block } k \text{ of the } \lceil n / 128 \rceil\ \textbf{do} \cr 4 & \qquad \textbf{foreach}\ \text{register } r \text{ of 4 integers} \text{ unpacked from its } 16 b_k \text{ bytes}\ \textbf{do} \cr 5 & \qquad\quad \textbf{if}\ \mathit{sorted}\ \textbf{then}\ r \gets r + p;\ p \gets r \cr 6 & \qquad\quad \text{append } r \cr 7 & \quad \textbf{return}\ \text{the first } n \text{ integers} \end{array}} \]

Precondition
in is the code of \( n \) integers

◆ patchedPack()

std::vector< std::uint8_t > ADS::patchedPack ( std::span< const std::uint32_t > x,
bool sorted = false )
inlinenodiscard

the patched binary packing (PFor) of \( x \) in blocks of 128 integers, of their differences at a distance of 4 if sorted

In pseudocode (the FastPFor of Lemire and Boytsov, beyond the book), \( c( t ) \) the integers of the block of \( 2^t \) or more:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{patchedPack}( x , \mathit{sorted} ) \cr 2 & \quad \textbf{foreach}\ \text{block } v \text{ of 128 integers} \text{ (differences if sorted, filled with 0s)}\ \textbf{do} \cr 3 & \qquad \beta \gets \max_i \mathrm{bitwidth}( v_i ) \cr 4 & \qquad b \gets \text{the } t \le \beta \text{ of least } 128 t + c( t ) ( 8 + \beta - t ) + 8 [ t < \beta ] \cr 5 & \qquad \text{write } b \text{ and } c( b ) \text{, and } \beta \text{ if } c( b ) > 0 \cr 6 & \qquad \text{pack the low } b \text{ bits of each } v_i \cr 7 & \qquad \text{write the places of the } v_i \ge 2^b \text{, then their} \text{ high } \beta - b \text{ bits packed} \cr 8 & \quad \text{write 8 bytes of } 0 \end{array}} \]

Precondition
if sorted, \( x \) is non-decreasing (audit)
Postcondition
patchedUnpack( result , x.size() , sorted ) is \( x \) (audit)

◆ patchedUnpack()

std::vector< std::uint32_t > ADS::patchedUnpack ( std::span< const std::uint8_t > in,
std::size_t n,
bool sorted = false )
inlinenodiscard

the \( n \) integers of the patched code in, packed with the same sorted

In pseudocode (the FastPFor, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{patchedUnpack}( y , n , \mathit{sorted} ) \cr 2 & \quad \textbf{foreach}\ \text{block } k\ \textbf{do} \cr 3 & \qquad \text{read } b \text{, } c \text{ and } \beta;\ \text{unpack} \text{ the 128 integers of } b \text{ bits} \cr 4 & \qquad \textbf{for}\ e \gets 0\ \textbf{to}\ c - 1\ \textbf{do} \ v_{\mathit{pos}[ e ]} \gets v_{\mathit{pos}[ e ]} + 2^b \cdot \text{the high bits of } e \cr 5 & \quad \textbf{if}\ \mathit{sorted}\ \textbf{then foreach}\ i \ge 4 \ \textbf{do}\ v_i \gets v_i + v_{i-4} \cr 6 & \quad \textbf{return}\ \text{the first } n \text{ integers} \end{array}} \]

Precondition
in is the patched code of \( n \) integers