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