1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief Probability price calculation
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
17 #define RC_MOVE_REDUCING_BITS 4
18 #define RC_BIT_PRICE_SHIFT_BITS 4
19 #define RC_PRICE_TABLE_SIZE (RC_BIT_MODEL_TOTAL >> RC_MOVE_REDUCING_BITS)
21 #define RC_INFINITY_PRICE (UINT32_C(1) << 30)
24 /// Lookup table for the inline functions defined in this file.
25 extern const uint8_t lzma_rc_prices
[RC_PRICE_TABLE_SIZE
];
28 static inline uint32_t
29 rc_bit_price(const probability prob
, const uint32_t bit
)
31 return lzma_rc_prices
[(prob
^ ((UINT32_C(0) - bit
)
32 & (RC_BIT_MODEL_TOTAL
- 1))) >> RC_MOVE_REDUCING_BITS
];
36 static inline uint32_t
37 rc_bit_0_price(const probability prob
)
39 return lzma_rc_prices
[prob
>> RC_MOVE_REDUCING_BITS
];
43 static inline uint32_t
44 rc_bit_1_price(const probability prob
)
46 return lzma_rc_prices
[(prob
^ (RC_BIT_MODEL_TOTAL
- 1))
47 >> RC_MOVE_REDUCING_BITS
];
51 static inline uint32_t
52 rc_bittree_price(const probability
*const probs
,
53 const uint32_t bit_levels
, uint32_t symbol
)
56 symbol
+= UINT32_C(1) << bit_levels
;
59 const uint32_t bit
= symbol
& 1;
61 price
+= rc_bit_price(probs
[symbol
], bit
);
62 } while (symbol
!= 1);
68 static inline uint32_t
69 rc_bittree_reverse_price(const probability
*const probs
,
70 uint32_t bit_levels
, uint32_t symbol
)
73 uint32_t model_index
= 1;
76 const uint32_t bit
= symbol
& 1;
78 price
+= rc_bit_price(probs
[model_index
], bit
);
79 model_index
= (model_index
<< 1) + bit
;
80 } while (--bit_levels
!= 0);
86 static inline uint32_t
87 rc_direct_price(const uint32_t bits
)
89 return bits
<< RC_BIT_PRICE_SHIFT_BITS
;