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