]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/rangecoder/range_common.h
Switch to uint16_t as the type of range coder probabilities.
[icculus/xz.git] / src / liblzma / rangecoder / range_common.h
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       range_common.h
4 /// \brief      Common things for range encoder and decoder
5 //
6 //  Copyright (C) 1999-2006 Igor Pavlov
7 //  Copyright (C) 2006 Lasse Collin
8 //
9 //  This library is free software; you can redistribute it and/or
10 //  modify it under the terms of the GNU Lesser General Public
11 //  License as published by the Free Software Foundation; either
12 //  version 2.1 of the License, or (at your option) any later version.
13 //
14 //  This library is distributed in the hope that it will be useful,
15 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 //  Lesser General Public License for more details.
18 //
19 ///////////////////////////////////////////////////////////////////////////////
20
21 #ifndef LZMA_RANGE_COMMON_H
22 #define LZMA_RANGE_COMMON_H
23
24 #ifdef HAVE_CONFIG_H
25 #       include "common.h"
26 #endif
27
28
29 ///////////////
30 // Constants //
31 ///////////////
32
33 #define SHIFT_BITS 8
34 #define TOP_BITS 24
35 #define TOP_VALUE (UINT32_C(1) << TOP_BITS)
36 #define BIT_MODEL_TOTAL_BITS 11
37 #define BIT_MODEL_TOTAL (UINT32_C(1) << BIT_MODEL_TOTAL_BITS)
38 #define MOVE_BITS 5
39
40 #define MOVE_REDUCING_BITS 2
41 #define BIT_PRICE_SHIFT_BITS 6
42
43
44 ////////////
45 // Macros //
46 ////////////
47
48 // Resets the probability so that both 0 and 1 have probability of 50 %
49 #define bit_reset(prob) \
50         prob = BIT_MODEL_TOTAL >> 1
51
52 // This does the same for a complete bit tree.
53 // (A tree represented as an array.)
54 #define bittree_reset(probs, bit_levels) \
55         for (uint32_t bt_i = 0; bt_i < (1 << (bit_levels)); ++bt_i) \
56                 bit_reset((probs)[bt_i])
57
58
59 //////////////////////
60 // Type definitions //
61 //////////////////////
62
63 /// \brief      Type of probabilities used with range coder
64 ///
65 /// This needs to be at least 12-bit integer, so uint16_t is a logical choice.
66 /// However, on some architecture and compiler combinations, a bigger type
67 /// may give better speed, because the probability variables are accessed
68 /// a lot. On the other hand, bigger probability type increases cache
69 /// footprint, since there are 2 to 14 thousand probability variables in
70 /// LZMA (assuming the limit of lc + lp <= 4; with lc + lp <= 12 there
71 /// would be about 1.5 million variables).
72 ///
73 /// With malicious files, the initialization speed of the LZMA decoder can
74 /// become important. In that case, smaller probability variables mean that
75 /// there is less bytes to write to RAM, which makes initialization faster.
76 /// With big probability type, the initialization can become so slow that it
77 /// can be a problem e.g. for email servers doing virus scanning.
78 ///
79 /// I will be sticking to uint16_t unless some specific architectures
80 /// are *much* faster (20-50 %) with uint32_t.
81 typedef uint16_t probability;
82
83 #endif