blob: 99ac744848fb49ffe7303c1046d0e3bcf7cfa383 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <stdio.h>
2#include "crc32defs.h"
3#include <inttypes.h>
4
5#define ENTRIES_PER_LINE 4
6
Bob Pearson9a1dbf62012-03-23 15:02:23 -07007#if CRC_LE_BITS <= 8
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#define LE_TABLE_SIZE (1 << CRC_LE_BITS)
Bob Pearson9a1dbf62012-03-23 15:02:23 -07009#else
10#define LE_TABLE_SIZE 256
11#endif
12
13#if CRC_BE_BITS <= 8
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#define BE_TABLE_SIZE (1 << CRC_BE_BITS)
Bob Pearson9a1dbf62012-03-23 15:02:23 -070015#else
16#define BE_TABLE_SIZE 256
17#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Bob Pearson60e58d52012-03-23 15:02:22 -070019static uint32_t crc32table_le[4][256];
20static uint32_t crc32table_be[4][256];
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22/**
23 * crc32init_le() - allocate and initialize LE table data
24 *
25 * crc is the crc of the byte i; other entries are filled in based on the
26 * fact that crctable[i^j] = crctable[i] ^ crctable[j].
27 *
28 */
29static void crc32init_le(void)
30{
31 unsigned i, j;
32 uint32_t crc = 1;
33
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070034 crc32table_le[0][0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Bob Pearson9a1dbf62012-03-23 15:02:23 -070036 for (i = LE_TABLE_SIZE >> 1; i; i >>= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
38 for (j = 0; j < LE_TABLE_SIZE; j += 2 * i)
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070039 crc32table_le[0][i + j] = crc ^ crc32table_le[0][j];
40 }
41 for (i = 0; i < LE_TABLE_SIZE; i++) {
42 crc = crc32table_le[0][i];
43 for (j = 1; j < 4; j++) {
44 crc = crc32table_le[0][crc & 0xff] ^ (crc >> 8);
45 crc32table_le[j][i] = crc;
46 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 }
48}
49
50/**
51 * crc32init_be() - allocate and initialize BE table data
52 */
53static void crc32init_be(void)
54{
55 unsigned i, j;
56 uint32_t crc = 0x80000000;
57
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070058 crc32table_be[0][0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 for (i = 1; i < BE_TABLE_SIZE; i <<= 1) {
61 crc = (crc << 1) ^ ((crc & 0x80000000) ? CRCPOLY_BE : 0);
62 for (j = 0; j < i; j++)
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070063 crc32table_be[0][i + j] = crc ^ crc32table_be[0][j];
64 }
65 for (i = 0; i < BE_TABLE_SIZE; i++) {
66 crc = crc32table_be[0][i];
67 for (j = 1; j < 4; j++) {
68 crc = crc32table_be[0][(crc >> 24) & 0xff] ^ (crc << 8);
69 crc32table_be[j][i] = crc;
70 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 }
72}
73
Bob Pearson60e58d52012-03-23 15:02:22 -070074static void output_table(uint32_t (*table)[256], int len, char *trans)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070076 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070078 for (j = 0 ; j < 4; j++) {
79 printf("{");
80 for (i = 0; i < len - 1; i++) {
81 if (i % ENTRIES_PER_LINE == 0)
82 printf("\n");
83 printf("%s(0x%8.8xL), ", trans, table[j][i]);
84 }
85 printf("%s(0x%8.8xL)},\n", trans, table[j][len - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
89int main(int argc, char** argv)
90{
91 printf("/* this file is generated - do not edit */\n\n");
92
93 if (CRC_LE_BITS > 1) {
94 crc32init_le();
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070095 printf("static const u32 crc32table_le[4][256] = {");
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 output_table(crc32table_le, LE_TABLE_SIZE, "tole");
97 printf("};\n");
98 }
99
100 if (CRC_BE_BITS > 1) {
101 crc32init_be();
Joakim Tjernlund836e2af2010-05-24 14:33:31 -0700102 printf("static const u32 crc32table_be[4][256] = {");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 output_table(crc32table_be, BE_TABLE_SIZE, "tobe");
104 printf("};\n");
105 }
106
107 return 0;
108}