xref: /dflybsd-src/contrib/xz/src/liblzma/rangecoder/range_common.h (revision 46a2189dd86b644c3a76ac281d84b4182fd66b95)
1*2940b44dSPeter Avalos ///////////////////////////////////////////////////////////////////////////////
2*2940b44dSPeter Avalos //
3*2940b44dSPeter Avalos /// \file       range_common.h
4*2940b44dSPeter Avalos /// \brief      Common things for range encoder and decoder
5*2940b44dSPeter Avalos ///
6*2940b44dSPeter Avalos //  Authors:    Igor Pavlov
7*2940b44dSPeter Avalos //              Lasse Collin
8*2940b44dSPeter Avalos //
9*2940b44dSPeter Avalos //  This file has been put into the public domain.
10*2940b44dSPeter Avalos //  You can do whatever you want with this file.
11*2940b44dSPeter Avalos //
12*2940b44dSPeter Avalos ///////////////////////////////////////////////////////////////////////////////
13*2940b44dSPeter Avalos 
14*2940b44dSPeter Avalos #ifndef LZMA_RANGE_COMMON_H
15*2940b44dSPeter Avalos #define LZMA_RANGE_COMMON_H
16*2940b44dSPeter Avalos 
17*2940b44dSPeter Avalos #include "common.h"
18*2940b44dSPeter Avalos 
19*2940b44dSPeter Avalos 
20*2940b44dSPeter Avalos ///////////////
21*2940b44dSPeter Avalos // Constants //
22*2940b44dSPeter Avalos ///////////////
23*2940b44dSPeter Avalos 
24*2940b44dSPeter Avalos #define RC_SHIFT_BITS 8
25*2940b44dSPeter Avalos #define RC_TOP_BITS 24
26*2940b44dSPeter Avalos #define RC_TOP_VALUE (UINT32_C(1) << RC_TOP_BITS)
27*2940b44dSPeter Avalos #define RC_BIT_MODEL_TOTAL_BITS 11
28*2940b44dSPeter Avalos #define RC_BIT_MODEL_TOTAL (UINT32_C(1) << RC_BIT_MODEL_TOTAL_BITS)
29*2940b44dSPeter Avalos #define RC_MOVE_BITS 5
30*2940b44dSPeter Avalos 
31*2940b44dSPeter Avalos 
32*2940b44dSPeter Avalos ////////////
33*2940b44dSPeter Avalos // Macros //
34*2940b44dSPeter Avalos ////////////
35*2940b44dSPeter Avalos 
36*2940b44dSPeter Avalos // Resets the probability so that both 0 and 1 have probability of 50 %
37*2940b44dSPeter Avalos #define bit_reset(prob) \
38*2940b44dSPeter Avalos 	prob = RC_BIT_MODEL_TOTAL >> 1
39*2940b44dSPeter Avalos 
40*2940b44dSPeter Avalos // This does the same for a complete bit tree.
41*2940b44dSPeter Avalos // (A tree represented as an array.)
42*2940b44dSPeter Avalos #define bittree_reset(probs, bit_levels) \
43*2940b44dSPeter Avalos 	for (uint32_t bt_i = 0; bt_i < (1 << (bit_levels)); ++bt_i) \
44*2940b44dSPeter Avalos 		bit_reset((probs)[bt_i])
45*2940b44dSPeter Avalos 
46*2940b44dSPeter Avalos 
47*2940b44dSPeter Avalos //////////////////////
48*2940b44dSPeter Avalos // Type definitions //
49*2940b44dSPeter Avalos //////////////////////
50*2940b44dSPeter Avalos 
51*2940b44dSPeter Avalos /// \brief      Type of probabilities used with range coder
52*2940b44dSPeter Avalos ///
53*2940b44dSPeter Avalos /// This needs to be at least 12-bit integer, so uint16_t is a logical choice.
54*2940b44dSPeter Avalos /// However, on some architecture and compiler combinations, a bigger type
55*2940b44dSPeter Avalos /// may give better speed, because the probability variables are accessed
56*2940b44dSPeter Avalos /// a lot. On the other hand, bigger probability type increases cache
57*2940b44dSPeter Avalos /// footprint, since there are 2 to 14 thousand probability variables in
58*2940b44dSPeter Avalos /// LZMA (assuming the limit of lc + lp <= 4; with lc + lp <= 12 there
59*2940b44dSPeter Avalos /// would be about 1.5 million variables).
60*2940b44dSPeter Avalos ///
61*2940b44dSPeter Avalos /// With malicious files, the initialization speed of the LZMA decoder can
62*2940b44dSPeter Avalos /// become important. In that case, smaller probability variables mean that
63*2940b44dSPeter Avalos /// there is less bytes to write to RAM, which makes initialization faster.
64*2940b44dSPeter Avalos /// With big probability type, the initialization can become so slow that it
65*2940b44dSPeter Avalos /// can be a problem e.g. for email servers doing virus scanning.
66*2940b44dSPeter Avalos ///
67*2940b44dSPeter Avalos /// I will be sticking to uint16_t unless some specific architectures
68*2940b44dSPeter Avalos /// are *much* faster (20-50 %) with uint32_t.
69*2940b44dSPeter Avalos typedef uint16_t probability;
70*2940b44dSPeter Avalos 
71*2940b44dSPeter Avalos #endif
72