2 * gen_crc32table.c --- Generate CRC32 tables.
4 * Copyright (C) 2008 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
13 #include "crc32defs.h"
16 #define ENTRIES_PER_LINE 4
18 #define LE_TABLE_SIZE (1 << CRC_LE_BITS)
19 #define BE_TABLE_SIZE (1 << CRC_BE_BITS)
21 static uint32_t crc32table_le[LE_TABLE_SIZE];
22 static uint32_t crc32table_be[BE_TABLE_SIZE];
25 * crc32init_le() - allocate and initialize LE table data
27 * crc is the crc of the byte i; other entries are filled in based on the
28 * fact that crctable[i^j] = crctable[i] ^ crctable[j].
31 static void crc32init_le(void)
38 for (i = 1 << (CRC_LE_BITS - 1); i; i >>= 1) {
39 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
40 for (j = 0; j < LE_TABLE_SIZE; j += 2 * i)
41 crc32table_le[i + j] = crc ^ crc32table_le[j];
46 * crc32init_be() - allocate and initialize BE table data
48 static void crc32init_be(void)
51 uint32_t crc = 0x80000000;
55 for (i = 1; i < BE_TABLE_SIZE; i <<= 1) {
56 crc = (crc << 1) ^ ((crc & 0x80000000) ? CRCPOLY_BE : 0);
57 for (j = 0; j < i; j++)
58 crc32table_be[i + j] = crc ^ crc32table_be[j];
62 static void output_table(uint32_t table[], int len, const char *trans)
66 for (i = 0; i < len - 1; i++) {
67 if (i % ENTRIES_PER_LINE == 0)
69 printf("%s(0x%8.8xL), ", trans, table[i]);
71 printf("%s(0x%8.8xL)\n", trans, table[len - 1]);
75 #define ATTR(x) __attribute__(x)
80 int main(int argc ATTR((unused)), char** argv ATTR((unused)))
82 printf("/* this file is generated - do not edit */\n\n");
84 printf("#ifdef UNITTEST\n");
85 if (CRC_LE_BITS > 1) {
87 printf("static const __u32 crc32table_le[] = {");
88 output_table(crc32table_le, LE_TABLE_SIZE, "tole");
91 printf("#endif /* UNITTEST */\n");
93 if (CRC_BE_BITS > 1) {
95 printf("static const __u32 crc32table_be[] = {");
96 output_table(crc32table_be, BE_TABLE_SIZE, "tobe");