aboutsummaryrefslogtreecommitdiff
path: root/src/liblzma/rangecoder/price_tablegen.c
blob: bf08ce39d7e5922a898e5909eca33c9ecca6acf2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
///////////////////////////////////////////////////////////////////////////////
//
/// \file       price_tablegen.c
/// \brief      Probability price table generator
///
/// Compiling: gcc -std=c99 -o price_tablegen price_tablegen.c
///
//  Authors:    Igor Pavlov
//              Lasse Collin
//
//  This file has been put into the public domain.
//  You can do whatever you want with this file.
//
///////////////////////////////////////////////////////////////////////////////

#include <inttypes.h>
#include <stdio.h>
#include "range_common.h"
#include "price.h"


static uint32_t rc_prices[RC_PRICE_TABLE_SIZE];


static void
init_price_table(void)
{
	for (uint32_t i = (UINT32_C(1) << RC_MOVE_REDUCING_BITS) / 2;
			i < RC_BIT_MODEL_TOTAL;
			i += (UINT32_C(1) << RC_MOVE_REDUCING_BITS)) {
		const uint32_t cycles_bits = RC_BIT_PRICE_SHIFT_BITS;
		uint32_t w = i;
		uint32_t bit_count = 0;

		for (uint32_t j = 0; j < cycles_bits; ++j) {
			w *= w;
			bit_count <<= 1;

			while (w >= (UINT32_C(1) << 16)) {
				w >>= 1;
				++bit_count;
			}
		}

		rc_prices[i >> RC_MOVE_REDUCING_BITS]
				= (RC_BIT_MODEL_TOTAL_BITS << cycles_bits)
				- 15 - bit_count;
	}

	return;
}


static void
print_price_table(void)
{
	printf("/* This file has been automatically generated by "
			"price_tablegen.c. */\n\n"
			"#include \"range_encoder.h\"\n\n"
			"const uint8_t lzma_rc_prices["
			"RC_PRICE_TABLE_SIZE] = {");

	const size_t array_size = sizeof(lzma_rc_prices)
			/ sizeof(lzma_rc_prices[0]);
	for (size_t i = 0; i < array_size; ++i) {
		if (i % 8 == 0)
			printf("\n\t");

		printf("%4" PRIu32, rc_prices[i]);

		if (i != array_size - 1)
			printf(",");
	}

	printf("\n};\n");

	return;
}


int
main(void)
{
	init_price_table();
	print_price_table();
	return 0;
}