xref: /freebsd/contrib/xz/src/liblzma/rangecoder/price_tablegen.c (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
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 
16 #include <inttypes.h>
17 #include <stdio.h>
18 #include "range_common.h"
19 #include "price.h"
20 
21 
22 static uint32_t rc_prices[RC_PRICE_TABLE_SIZE];
23 
24 
25 static void
26 init_price_table(void)
27 {
28 	for (uint32_t i = (UINT32_C(1) << RC_MOVE_REDUCING_BITS) / 2;
29 			i < RC_BIT_MODEL_TOTAL;
30 			i += (UINT32_C(1) << RC_MOVE_REDUCING_BITS)) {
31 		const uint32_t cycles_bits = RC_BIT_PRICE_SHIFT_BITS;
32 		uint32_t w = i;
33 		uint32_t bit_count = 0;
34 
35 		for (uint32_t j = 0; j < cycles_bits; ++j) {
36 			w *= w;
37 			bit_count <<= 1;
38 
39 			while (w >= (UINT32_C(1) << 16)) {
40 				w >>= 1;
41 				++bit_count;
42 			}
43 		}
44 
45 		rc_prices[i >> RC_MOVE_REDUCING_BITS]
46 				= (RC_BIT_MODEL_TOTAL_BITS << cycles_bits)
47 				- 15 - bit_count;
48 	}
49 
50 	return;
51 }
52 
53 
54 static void
55 print_price_table(void)
56 {
57 	printf("/* This file has been automatically generated by "
58 			"price_tablegen.c. */\n\n"
59 			"#include \"range_encoder.h\"\n\n"
60 			"const uint8_t lzma_rc_prices["
61 			"RC_PRICE_TABLE_SIZE] = {");
62 
63 	const size_t array_size = sizeof(lzma_rc_prices)
64 			/ sizeof(lzma_rc_prices[0]);
65 	for (size_t i = 0; i < array_size; ++i) {
66 		if (i % 8 == 0)
67 			printf("\n\t");
68 
69 		printf("%4" PRIu32, rc_prices[i]);
70 
71 		if (i != array_size - 1)
72 			printf(",");
73 	}
74 
75 	printf("\n};\n");
76 
77 	return;
78 }
79 
80 
81 int
82 main(void)
83 {
84 	init_price_table();
85 	print_price_table();
86 	return 0;
87 }
88