price.h 1.9 KB

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