price.h 2.0 KB

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