|
ADS
Algorithms and Data Structures
|
The tabled variant of the asymmetric numeral systems of Duda (tANS), the entropy coder of order 0 of Zstandard (RFC 8878), beside the range variant [see Rans.h]: the transitions of the state are read from tables, so that a symbol costs a lookup, a shift and an addition, and no multiplication. More...
#include "ADS/Contract.h"#include "ADS/compression/Rans.h"#include <array>#include <bit>#include <cstddef>#include <cstdint>#include <span>#include <vector>#include "ADS/compression/impl/Tans.tpp"Go to the source code of this file.
Namespaces | |
| namespace | ADS |
Functions | |
| std::vector< std::uint8_t > | ADS::tansCompress (std::span< const std::uint8_t > in) |
| the tANS code of order 0 of the bytes of in, with the frequencies and the size | |
| std::vector< std::uint8_t > | ADS::tansDecompress (std::span< const std::uint8_t > in) |
| the bytes that tansCompress() coded in in | |
The tabled variant of the asymmetric numeral systems of Duda (tANS), the entropy coder of order 0 of Zstandard (RFC 8878), beside the range variant [see Rans.h]: the transitions of the state are read from tables, so that a symbol costs a lookup, a shift and an addition, and no multiplication.
With \( L = 2^R \) states, \( R = 11 \), and the frequencies \( f_s \) of the bytes scaled to sum to \( L \) (each byte of the string kept at 1 at least [see scaleFrequencies() of Rans.h]), the \( L \) states are dealt to the symbols, \( f_s \) each, by the step \( L / 2 + L / 8 + 3 \) modulo \( L \), which visits them all (it is odd) and spreads the states of a symbol over the table. The decoder in the state \( x \in [ 0 , L ) \) of the symbol \( s \), its \( i \)-th one, emits \( s \) and moves to
\[ x' = ( ( f_s + i ) 2^{b} - L ) + r , \qquad b = R - \lfloor \log_2( f_s + i ) \rfloor , \]
\( r \) the next \( b \) bits of the stream; the encoder in the state \( X \in [ L , 2 L ) \) emits the low \( k \) bits of \( X \), \( k \) the number that brings \( \lfloor X / 2^k \rfloor \) into \( [ f_s , 2 f_s ) \) (one of two values, found by one addition and a shift), and moves to \( L \) plus the state of the \( ( \lfloor X / 2^k \rfloor - f_s ) \)-th place of \( s \). A symbol costs about \( \log_2( L / f_s ) \) bits, and the code comes within a fraction of a percent of \( n H_0 \) [see Entropy.h] for \( L \) a few times the alphabet (Duda; Dube and Yokoo for the analysis of the spread).
The string is cut in two halves, each with a stream of bits of its own and four states that take its symbols in turn; a stream is written forward from the low bit of a word and read backward from its end, 4 symbols of 11 bits at most between two loads of the reader, and the two halves are decoded together, so that their chains of dependences overlap. The encoder works from the last symbol to the first, so that the decoder, which reads the stream from its end, meets the bits in the order of the symbols.
The format: the size \( n \) in 8 bytes, the 256 scaled frequencies in 2 bytes each, the bits of the two streams in 8 bytes each, the streams (the 4 final states of the encoder at the end of each, \( R \) bits each) and 8 bytes of 0; the integers are little-endian.
The contracts [see Contract.h]: tansDecompress() gets the output of tansCompress(), whose header (the frequencies sum to \( L \), the bits of the stream fit in the input) it checks; the audit of tansCompress() is that the output decompresses to the input. The functions are defined in Tans.tpp.