1207753Smm///////////////////////////////////////////////////////////////////////////////
2207753Smm//
3207753Smm/// \file       crc64_tablegen.c
4207753Smm/// \brief      Generate crc64_table_le.h and crc64_table_be.h
5207753Smm///
6207753Smm/// Compiling: gcc -std=c99 -o crc64_tablegen crc64_tablegen.c
7207753Smm/// Add -DWORDS_BIGENDIAN to generate big endian table.
8207753Smm//
9207753Smm//  Author:     Lasse Collin
10207753Smm//
11207753Smm//  This file has been put into the public domain.
12207753Smm//  You can do whatever you want with this file.
13207753Smm//
14207753Smm///////////////////////////////////////////////////////////////////////////////
15207753Smm
16207753Smm#include <stdio.h>
17207753Smm#include "../../common/tuklib_integer.h"
18207753Smm
19207753Smm
20207753Smmstatic uint64_t crc64_table[4][256];
21207753Smm
22207753Smm
23207753Smmextern void
24207753Smminit_crc64_table(void)
25207753Smm{
26207753Smm	static const uint64_t poly64 = UINT64_C(0xC96C5795D7870F42);
27207753Smm
28207753Smm	for (size_t s = 0; s < 4; ++s) {
29207753Smm		for (size_t b = 0; b < 256; ++b) {
30207753Smm			uint64_t r = s == 0 ? b : crc64_table[s - 1][b];
31207753Smm
32207753Smm			for (size_t i = 0; i < 8; ++i) {
33207753Smm				if (r & 1)
34207753Smm					r = (r >> 1) ^ poly64;
35207753Smm				else
36207753Smm					r >>= 1;
37207753Smm			}
38207753Smm
39207753Smm			crc64_table[s][b] = r;
40207753Smm		}
41207753Smm	}
42207753Smm
43207753Smm#ifdef WORDS_BIGENDIAN
44207753Smm	for (size_t s = 0; s < 4; ++s)
45207753Smm		for (size_t b = 0; b < 256; ++b)
46207753Smm			crc64_table[s][b] = bswap64(crc64_table[s][b]);
47207753Smm#endif
48207753Smm
49207753Smm	return;
50207753Smm}
51207753Smm
52207753Smm
53207753Smmstatic void
54207753Smmprint_crc64_table(void)
55207753Smm{
56207753Smm	printf("/* This file has been automatically generated by "
57207753Smm			"crc64_tablegen.c. */\n\n"
58207753Smm			"const uint64_t lzma_crc64_table[4][256] = {\n\t{");
59207753Smm
60207753Smm	for (size_t s = 0; s < 4; ++s) {
61207753Smm		for (size_t b = 0; b < 256; ++b) {
62207753Smm			if ((b % 2) == 0)
63207753Smm				printf("\n\t\t");
64207753Smm
65207753Smm			printf("UINT64_C(0x%016" PRIX64 ")",
66207753Smm					crc64_table[s][b]);
67207753Smm
68207753Smm			if (b != 255)
69207753Smm				printf(",%s", (b+1) % 2 == 0 ? "" : " ");
70207753Smm		}
71207753Smm
72207753Smm		if (s == 3)
73207753Smm			printf("\n\t}\n};\n");
74207753Smm		else
75207753Smm			printf("\n\t}, {");
76207753Smm	}
77207753Smm
78207753Smm	return;
79207753Smm}
80207753Smm
81207753Smm
82207753Smmint
83207753Smmmain(void)
84207753Smm{
85207753Smm	init_crc64_table();
86207753Smm	print_crc64_table();
87207753Smm	return 0;
88207753Smm}
89