xref: /minix3/external/public-domain/xz/dist/src/liblzma/rangecoder/price.h (revision 5a645f22a86f086849945a5dd6acbf59f38c913a)
1*5a645f22SBen Gras ///////////////////////////////////////////////////////////////////////////////
2*5a645f22SBen Gras //
3*5a645f22SBen Gras /// \file       price.h
4*5a645f22SBen Gras /// \brief      Probability price calculation
5*5a645f22SBen Gras //
6*5a645f22SBen Gras //  Author:     Igor Pavlov
7*5a645f22SBen Gras //
8*5a645f22SBen Gras //  This file has been put into the public domain.
9*5a645f22SBen Gras //  You can do whatever you want with this file.
10*5a645f22SBen Gras //
11*5a645f22SBen Gras ///////////////////////////////////////////////////////////////////////////////
12*5a645f22SBen Gras 
13*5a645f22SBen Gras #ifndef LZMA_PRICE_H
14*5a645f22SBen Gras #define LZMA_PRICE_H
15*5a645f22SBen Gras 
16*5a645f22SBen Gras 
17*5a645f22SBen Gras #define RC_MOVE_REDUCING_BITS 4
18*5a645f22SBen Gras #define RC_BIT_PRICE_SHIFT_BITS 4
19*5a645f22SBen Gras #define RC_PRICE_TABLE_SIZE (RC_BIT_MODEL_TOTAL >> RC_MOVE_REDUCING_BITS)
20*5a645f22SBen Gras 
21*5a645f22SBen Gras #define RC_INFINITY_PRICE (UINT32_C(1) << 30)
22*5a645f22SBen Gras 
23*5a645f22SBen Gras 
24*5a645f22SBen Gras /// Lookup table for the inline functions defined in this file.
25*5a645f22SBen Gras extern const uint8_t lzma_rc_prices[RC_PRICE_TABLE_SIZE];
26*5a645f22SBen Gras 
27*5a645f22SBen Gras 
28*5a645f22SBen Gras static inline uint32_t
rc_bit_price(const probability prob,const uint32_t bit)29*5a645f22SBen Gras rc_bit_price(const probability prob, const uint32_t bit)
30*5a645f22SBen Gras {
31*5a645f22SBen Gras 	return lzma_rc_prices[(prob ^ ((UINT32_C(0) - bit)
32*5a645f22SBen Gras 			& (RC_BIT_MODEL_TOTAL - 1))) >> RC_MOVE_REDUCING_BITS];
33*5a645f22SBen Gras }
34*5a645f22SBen Gras 
35*5a645f22SBen Gras 
36*5a645f22SBen Gras static inline uint32_t
rc_bit_0_price(const probability prob)37*5a645f22SBen Gras rc_bit_0_price(const probability prob)
38*5a645f22SBen Gras {
39*5a645f22SBen Gras 	return lzma_rc_prices[prob >> RC_MOVE_REDUCING_BITS];
40*5a645f22SBen Gras }
41*5a645f22SBen Gras 
42*5a645f22SBen Gras 
43*5a645f22SBen Gras static inline uint32_t
rc_bit_1_price(const probability prob)44*5a645f22SBen Gras rc_bit_1_price(const probability prob)
45*5a645f22SBen Gras {
46*5a645f22SBen Gras 	return lzma_rc_prices[(prob ^ (RC_BIT_MODEL_TOTAL - 1))
47*5a645f22SBen Gras 			>> RC_MOVE_REDUCING_BITS];
48*5a645f22SBen Gras }
49*5a645f22SBen Gras 
50*5a645f22SBen Gras 
51*5a645f22SBen Gras static inline uint32_t
rc_bittree_price(const probability * const probs,const uint32_t bit_levels,uint32_t symbol)52*5a645f22SBen Gras rc_bittree_price(const probability *const probs,
53*5a645f22SBen Gras 		const uint32_t bit_levels, uint32_t symbol)
54*5a645f22SBen Gras {
55*5a645f22SBen Gras 	uint32_t price = 0;
56*5a645f22SBen Gras 	symbol += UINT32_C(1) << bit_levels;
57*5a645f22SBen Gras 
58*5a645f22SBen Gras 	do {
59*5a645f22SBen Gras 		const uint32_t bit = symbol & 1;
60*5a645f22SBen Gras 		symbol >>= 1;
61*5a645f22SBen Gras 		price += rc_bit_price(probs[symbol], bit);
62*5a645f22SBen Gras 	} while (symbol != 1);
63*5a645f22SBen Gras 
64*5a645f22SBen Gras 	return price;
65*5a645f22SBen Gras }
66*5a645f22SBen Gras 
67*5a645f22SBen Gras 
68*5a645f22SBen Gras static inline uint32_t
rc_bittree_reverse_price(const probability * const probs,uint32_t bit_levels,uint32_t symbol)69*5a645f22SBen Gras rc_bittree_reverse_price(const probability *const probs,
70*5a645f22SBen Gras 		uint32_t bit_levels, uint32_t symbol)
71*5a645f22SBen Gras {
72*5a645f22SBen Gras 	uint32_t price = 0;
73*5a645f22SBen Gras 	uint32_t model_index = 1;
74*5a645f22SBen Gras 
75*5a645f22SBen Gras 	do {
76*5a645f22SBen Gras 		const uint32_t bit = symbol & 1;
77*5a645f22SBen Gras 		symbol >>= 1;
78*5a645f22SBen Gras 		price += rc_bit_price(probs[model_index], bit);
79*5a645f22SBen Gras 		model_index = (model_index << 1) + bit;
80*5a645f22SBen Gras 	} while (--bit_levels != 0);
81*5a645f22SBen Gras 
82*5a645f22SBen Gras 	return price;
83*5a645f22SBen Gras }
84*5a645f22SBen Gras 
85*5a645f22SBen Gras 
86*5a645f22SBen Gras static inline uint32_t
rc_direct_price(const uint32_t bits)87*5a645f22SBen Gras rc_direct_price(const uint32_t bits)
88*5a645f22SBen Gras {
89*5a645f22SBen Gras 	 return bits << RC_BIT_PRICE_SHIFT_BITS;
90*5a645f22SBen Gras }
91*5a645f22SBen Gras 
92*5a645f22SBen Gras #endif
93