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