range_common.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // SPDX-License-Identifier: 0BSD
  2. ///////////////////////////////////////////////////////////////////////////////
  3. //
  4. /// \file range_common.h
  5. /// \brief Common things for range encoder and decoder
  6. ///
  7. // Authors: Igor Pavlov
  8. // Lasse Collin
  9. //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #ifndef LZMA_RANGE_COMMON_H
  12. #define LZMA_RANGE_COMMON_H
  13. // Skip common.h if building price_tablegen.c.
  14. #ifndef BUILDING_PRICE_TABLEGEN
  15. # include "common.h"
  16. #endif
  17. ///////////////
  18. // Constants //
  19. ///////////////
  20. #define RC_SHIFT_BITS 8
  21. #define RC_TOP_BITS 24
  22. #define RC_TOP_VALUE (UINT32_C(1) << RC_TOP_BITS)
  23. #define RC_BIT_MODEL_TOTAL_BITS 11
  24. #define RC_BIT_MODEL_TOTAL (UINT32_C(1) << RC_BIT_MODEL_TOTAL_BITS)
  25. #define RC_MOVE_BITS 5
  26. ////////////
  27. // Macros //
  28. ////////////
  29. // Resets the probability so that both 0 and 1 have probability of 50 %
  30. #define bit_reset(prob) \
  31. prob = RC_BIT_MODEL_TOTAL >> 1
  32. // This does the same for a complete bit tree.
  33. // (A tree represented as an array.)
  34. #define bittree_reset(probs, bit_levels) \
  35. for (uint32_t bt_i = 0; bt_i < (1 << (bit_levels)); ++bt_i) \
  36. bit_reset((probs)[bt_i])
  37. //////////////////////
  38. // Type definitions //
  39. //////////////////////
  40. /// \brief Type of probabilities used with range coder
  41. ///
  42. /// This needs to be at least 12-bit integer, so uint16_t is a logical choice.
  43. /// However, on some architecture and compiler combinations, a bigger type
  44. /// may give better speed, because the probability variables are accessed
  45. /// a lot. On the other hand, bigger probability type increases cache
  46. /// footprint, since there are 2 to 14 thousand probability variables in
  47. /// LZMA (assuming the limit of lc + lp <= 4; with lc + lp <= 12 there
  48. /// would be about 1.5 million variables).
  49. ///
  50. /// With malicious files, the initialization speed of the LZMA decoder can
  51. /// become important. In that case, smaller probability variables mean that
  52. /// there is less bytes to write to RAM, which makes initialization faster.
  53. /// With big probability type, the initialization can become so slow that it
  54. /// can be a problem e.g. for email servers doing virus scanning.
  55. ///
  56. /// I will be sticking to uint16_t unless some specific architectures
  57. /// are *much* faster (20-50 %) with uint32_t.
  58. ///
  59. /// Update in 2024: The branchless C and x86-64 assembly was written so that
  60. /// probability is assumed to be uint16_t. (In contrast, LZMA SDK 23.01
  61. /// assembly supports both types.)
  62. typedef uint16_t probability;
  63. #endif