186229Stmm///////////////////////////////////////////////////////////////////////////////
286229Stmm//
386229Stmm/// \file       range_common.h
486229Stmm/// \brief      Common things for range encoder and decoder
586229Stmm///
686229Stmm//  Authors:    Igor Pavlov
786229Stmm//              Lasse Collin
886229Stmm//
986229Stmm//  This file has been put into the public domain.
1086229Stmm//  You can do whatever you want with this file.
1186229Stmm//
1286229Stmm///////////////////////////////////////////////////////////////////////////////
1386229Stmm
1486229Stmm#ifndef LZMA_RANGE_COMMON_H
1586229Stmm#define LZMA_RANGE_COMMON_H
1686229Stmm
1786229Stmm#ifdef HAVE_CONFIG_H
1886229Stmm#	include "common.h"
1986229Stmm#endif
2086229Stmm
2186229Stmm
2286229Stmm///////////////
2386229Stmm// Constants //
2486229Stmm///////////////
2586229Stmm
2686229Stmm#define RC_SHIFT_BITS 8
2786229Stmm#define RC_TOP_BITS 24
2886229Stmm#define RC_TOP_VALUE (UINT32_C(1) << RC_TOP_BITS)
2986229Stmm#define RC_BIT_MODEL_TOTAL_BITS 11
3086229Stmm#define RC_BIT_MODEL_TOTAL (UINT32_C(1) << RC_BIT_MODEL_TOTAL_BITS)
3186229Stmm#define RC_MOVE_BITS 5
32186347Snwhitehorn
33186347Snwhitehorn
3486229Stmm////////////
35186347Snwhitehorn// Macros //
36186347Snwhitehorn////////////
37141753Smarius
38119697Smarcel// Resets the probability so that both 0 and 1 have probability of 50 %
39207243Smarius#define bit_reset(prob) \
40230632Smarius	prob = RC_BIT_MODEL_TOTAL >> 1
41230632Smarius
42186347Snwhitehorn// This does the same for a complete bit tree.
43230632Smarius// (A tree represented as an array.)
4486229Stmm#define bittree_reset(probs, bit_levels) \
4586229Stmm	for (uint32_t bt_i = 0; bt_i < (1 << (bit_levels)); ++bt_i) \
46		bit_reset((probs)[bt_i])
47
48
49//////////////////////
50// Type definitions //
51//////////////////////
52
53/// \brief      Type of probabilities used with range coder
54///
55/// This needs to be at least 12-bit integer, so uint16_t is a logical choice.
56/// However, on some architecture and compiler combinations, a bigger type
57/// may give better speed, because the probability variables are accessed
58/// a lot. On the other hand, bigger probability type increases cache
59/// footprint, since there are 2 to 14 thousand probability variables in
60/// LZMA (assuming the limit of lc + lp <= 4; with lc + lp <= 12 there
61/// would be about 1.5 million variables).
62///
63/// With malicious files, the initialization speed of the LZMA decoder can
64/// become important. In that case, smaller probability variables mean that
65/// there is less bytes to write to RAM, which makes initialization faster.
66/// With big probability type, the initialization can become so slow that it
67/// can be a problem e.g. for email servers doing virus scanning.
68///
69/// I will be sticking to uint16_t unless some specific architectures
70/// are *much* faster (20-50 %) with uint32_t.
71typedef uint16_t probability;
72
73#endif
74