1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief Probability price calculation
6 // Copyright (C) 1999-2008 Igor Pavlov
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.
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.
18 ///////////////////////////////////////////////////////////////////////////////
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)
28 #define RC_INFINITY_PRICE (UINT32_C(1) << 30)
31 /// Lookup table for the inline functions defined in this file.
32 extern const uint8_t lzma_rc_prices[RC_PRICE_TABLE_SIZE];
35 static inline uint32_t
36 rc_bit_price(const probability prob, const uint32_t bit)
38 return lzma_rc_prices[(prob ^ ((UINT32_C(0) - bit)
39 & (RC_BIT_MODEL_TOTAL - 1))) >> RC_MOVE_REDUCING_BITS];
43 static inline uint32_t
44 rc_bit_0_price(const probability prob)
46 return lzma_rc_prices[prob >> RC_MOVE_REDUCING_BITS];
50 static inline uint32_t
51 rc_bit_1_price(const probability prob)
53 return lzma_rc_prices[(prob ^ (RC_BIT_MODEL_TOTAL - 1))
54 >> RC_MOVE_REDUCING_BITS];
58 static inline uint32_t
59 rc_bittree_price(const probability *const probs,
60 const uint32_t bit_levels, uint32_t symbol)
63 symbol += UINT32_C(1) << bit_levels;
66 const uint32_t bit = symbol & 1;
68 price += rc_bit_price(probs[symbol], bit);
69 } while (symbol != 1);
75 static inline uint32_t
76 rc_bittree_reverse_price(const probability *const probs,
77 uint32_t bit_levels, uint32_t symbol)
80 uint32_t model_index = 1;
83 const uint32_t bit = symbol & 1;
85 price += rc_bit_price(probs[model_index], bit);
86 model_index = (model_index << 1) + bit;
87 } while (--bit_levels != 0);
93 static inline uint32_t
94 rc_direct_price(const uint32_t bits)
96 return bits << RC_BIT_PRICE_SHIFT_BITS;