price_tablegen.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file price_tablegen.c
  4. /// \brief Probability price table generator
  5. ///
  6. /// Compiling: gcc -std=c99 -o price_tablegen price_tablegen.c
  7. ///
  8. // Authors: Igor Pavlov
  9. // Lasse Collin
  10. //
  11. // This file has been put into the public domain.
  12. // You can do whatever you want with this file.
  13. //
  14. ///////////////////////////////////////////////////////////////////////////////
  15. #include <inttypes.h>
  16. #include <stdio.h>
  17. #include "range_common.h"
  18. #include "price.h"
  19. static uint32_t rc_prices[RC_PRICE_TABLE_SIZE];
  20. static void
  21. init_price_table(void)
  22. {
  23. for (uint32_t i = (UINT32_C(1) << RC_MOVE_REDUCING_BITS) / 2;
  24. i < RC_BIT_MODEL_TOTAL;
  25. i += (UINT32_C(1) << RC_MOVE_REDUCING_BITS)) {
  26. const uint32_t cycles_bits = RC_BIT_PRICE_SHIFT_BITS;
  27. uint32_t w = i;
  28. uint32_t bit_count = 0;
  29. for (uint32_t j = 0; j < cycles_bits; ++j) {
  30. w *= w;
  31. bit_count <<= 1;
  32. while (w >= (UINT32_C(1) << 16)) {
  33. w >>= 1;
  34. ++bit_count;
  35. }
  36. }
  37. rc_prices[i >> RC_MOVE_REDUCING_BITS]
  38. = (RC_BIT_MODEL_TOTAL_BITS << cycles_bits)
  39. - 15 - bit_count;
  40. }
  41. return;
  42. }
  43. static void
  44. print_price_table(void)
  45. {
  46. printf("/* This file has been automatically generated by "
  47. "price_tablegen.c. */\n\n"
  48. "#include \"range_encoder.h\"\n\n"
  49. "const uint8_t lzma_rc_prices["
  50. "RC_PRICE_TABLE_SIZE] = {");
  51. const size_t array_size = sizeof(lzma_rc_prices)
  52. / sizeof(lzma_rc_prices[0]);
  53. for (size_t i = 0; i < array_size; ++i) {
  54. if (i % 8 == 0)
  55. printf("\n\t");
  56. printf("%4" PRIu32, rc_prices[i]);
  57. if (i != array_size - 1)
  58. printf(",");
  59. }
  60. printf("\n};\n");
  61. return;
  62. }
  63. int
  64. main(void)
  65. {
  66. init_price_table();
  67. print_price_table();
  68. return 0;
  69. }