]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/rangecoder/price.h
Remove lzma_init() and other init functions from liblzma API.
[icculus/xz.git] / src / liblzma / rangecoder / price.h
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       price.h
4 /// \brief      Probability price calculation
5 //
6 //  Copyright (C) 1999-2008 Igor Pavlov
7 //
8 //  This library is free software; you can redistribute it and/or
9 //  modify it under the terms of the GNU Lesser General Public
10 //  License as published by the Free Software Foundation; either
11 //  version 2.1 of the License, or (at your option) any later version.
12 //
13 //  This library is distributed in the hope that it will be useful,
14 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 //  Lesser General Public License for more details.
17 //
18 ///////////////////////////////////////////////////////////////////////////////
19
20 #ifndef LZMA_PRICE_H
21 #define LZMA_PRICE_H
22
23
24 #define RC_MOVE_REDUCING_BITS 4
25 #define RC_BIT_PRICE_SHIFT_BITS 4
26 #define RC_PRICE_TABLE_SIZE (RC_BIT_MODEL_TOTAL >> RC_MOVE_REDUCING_BITS)
27
28 #define RC_INFINITY_PRICE (UINT32_C(1) << 30)
29
30
31 /// Lookup table for the inline functions defined in this file.
32 extern const uint8_t lzma_rc_prices[RC_PRICE_TABLE_SIZE];
33
34
35 static inline uint32_t
36 rc_bit_price(const probability prob, const uint32_t bit)
37 {
38         return lzma_rc_prices[(prob ^ ((UINT32_C(0) - bit)
39                         & (RC_BIT_MODEL_TOTAL - 1))) >> RC_MOVE_REDUCING_BITS];
40 }
41
42
43 static inline uint32_t
44 rc_bit_0_price(const probability prob)
45 {
46         return lzma_rc_prices[prob >> RC_MOVE_REDUCING_BITS];
47 }
48
49
50 static inline uint32_t
51 rc_bit_1_price(const probability prob)
52 {
53         return lzma_rc_prices[(prob ^ (RC_BIT_MODEL_TOTAL - 1))
54                         >> RC_MOVE_REDUCING_BITS];
55 }
56
57
58 static inline uint32_t
59 rc_bittree_price(const probability *const probs,
60                 const uint32_t bit_levels, uint32_t symbol)
61 {
62         uint32_t price = 0;
63         symbol += UINT32_C(1) << bit_levels;
64
65         do {
66                 const uint32_t bit = symbol & 1;
67                 symbol >>= 1;
68                 price += rc_bit_price(probs[symbol], bit);
69         } while (symbol != 1);
70
71         return price;
72 }
73
74
75 static inline uint32_t
76 rc_bittree_reverse_price(const probability *const probs,
77                 uint32_t bit_levels, uint32_t symbol)
78 {
79         uint32_t price = 0;
80         uint32_t model_index = 1;
81
82         do {
83                 const uint32_t bit = symbol & 1;
84                 symbol >>= 1;
85                 price += rc_bit_price(probs[model_index], bit);
86                 model_index = (model_index << 1) + bit;
87         } while (--bit_levels != 0);
88
89         return price;
90 }
91
92
93 static inline uint32_t
94 rc_direct_price(const uint32_t bits)
95 {
96          return bits << RC_BIT_PRICE_SHIFT_BITS;
97 }
98
99 #endif