blob: 0d9edd17ee13bb35fa4f409a3c7bf73e1591f4c4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <stdio.h>
Bob Pearson324eb0f2012-03-23 15:02:24 -07002#include "../include/generated/autoconf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07003#include "crc32defs.h"
4#include <inttypes.h>
5
6#define ENTRIES_PER_LINE 4
7
Bob Pearson324eb0f2012-03-23 15:02:24 -07008#if CRC_LE_BITS > 8
9# define LE_TABLE_ROWS (CRC_LE_BITS/8)
10# define LE_TABLE_SIZE 256
Bob Pearson9a1dbf62012-03-23 15:02:23 -070011#else
Bob Pearson324eb0f2012-03-23 15:02:24 -070012# define LE_TABLE_ROWS 1
13# define LE_TABLE_SIZE (1 << CRC_LE_BITS)
Bob Pearson9a1dbf62012-03-23 15:02:23 -070014#endif
15
Bob Pearson324eb0f2012-03-23 15:02:24 -070016#if CRC_BE_BITS > 8
17# define BE_TABLE_ROWS (CRC_BE_BITS/8)
18# define BE_TABLE_SIZE 256
Bob Pearson9a1dbf62012-03-23 15:02:23 -070019#else
Bob Pearson324eb0f2012-03-23 15:02:24 -070020# define BE_TABLE_ROWS 1
21# define BE_TABLE_SIZE (1 << CRC_BE_BITS)
Bob Pearson9a1dbf62012-03-23 15:02:23 -070022#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Bob Pearson324eb0f2012-03-23 15:02:24 -070024static uint32_t crc32table_le[LE_TABLE_ROWS][256];
25static uint32_t crc32table_be[BE_TABLE_ROWS][256];
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27/**
28 * crc32init_le() - allocate and initialize LE table data
29 *
30 * crc is the crc of the byte i; other entries are filled in based on the
31 * fact that crctable[i^j] = crctable[i] ^ crctable[j].
32 *
33 */
34static void crc32init_le(void)
35{
36 unsigned i, j;
37 uint32_t crc = 1;
38
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070039 crc32table_le[0][0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Bob Pearson9a1dbf62012-03-23 15:02:23 -070041 for (i = LE_TABLE_SIZE >> 1; i; i >>= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
43 for (j = 0; j < LE_TABLE_SIZE; j += 2 * i)
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070044 crc32table_le[0][i + j] = crc ^ crc32table_le[0][j];
45 }
46 for (i = 0; i < LE_TABLE_SIZE; i++) {
47 crc = crc32table_le[0][i];
Bob Pearson324eb0f2012-03-23 15:02:24 -070048 for (j = 1; j < LE_TABLE_ROWS; j++) {
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070049 crc = crc32table_le[0][crc & 0xff] ^ (crc >> 8);
50 crc32table_le[j][i] = crc;
51 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 }
53}
54
55/**
56 * crc32init_be() - allocate and initialize BE table data
57 */
58static void crc32init_be(void)
59{
60 unsigned i, j;
61 uint32_t crc = 0x80000000;
62
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070063 crc32table_be[0][0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 for (i = 1; i < BE_TABLE_SIZE; i <<= 1) {
66 crc = (crc << 1) ^ ((crc & 0x80000000) ? CRCPOLY_BE : 0);
67 for (j = 0; j < i; j++)
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070068 crc32table_be[0][i + j] = crc ^ crc32table_be[0][j];
69 }
70 for (i = 0; i < BE_TABLE_SIZE; i++) {
71 crc = crc32table_be[0][i];
Bob Pearson324eb0f2012-03-23 15:02:24 -070072 for (j = 1; j < BE_TABLE_ROWS; j++) {
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070073 crc = crc32table_be[0][(crc >> 24) & 0xff] ^ (crc << 8);
74 crc32table_be[j][i] = crc;
75 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 }
77}
78
Bob Pearson324eb0f2012-03-23 15:02:24 -070079static void output_table(uint32_t (*table)[256], int rows, int len, char *trans)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070081 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Bob Pearson324eb0f2012-03-23 15:02:24 -070083 for (j = 0 ; j < rows; j++) {
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070084 printf("{");
85 for (i = 0; i < len - 1; i++) {
86 if (i % ENTRIES_PER_LINE == 0)
87 printf("\n");
88 printf("%s(0x%8.8xL), ", trans, table[j][i]);
89 }
90 printf("%s(0x%8.8xL)},\n", trans, table[j][len - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
94int main(int argc, char** argv)
95{
96 printf("/* this file is generated - do not edit */\n\n");
97
98 if (CRC_LE_BITS > 1) {
99 crc32init_le();
Bob Pearson324eb0f2012-03-23 15:02:24 -0700100 printf("static const u32 __cacheline_aligned "
101 "crc32table_le[%d][%d] = {",
102 LE_TABLE_ROWS, LE_TABLE_SIZE);
103 output_table(crc32table_le, LE_TABLE_ROWS,
104 LE_TABLE_SIZE, "tole");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 printf("};\n");
106 }
107
108 if (CRC_BE_BITS > 1) {
109 crc32init_be();
Bob Pearson324eb0f2012-03-23 15:02:24 -0700110 printf("static const u32 __cacheline_aligned "
111 "crc32table_be[%d][%d] = {",
112 BE_TABLE_ROWS, BE_TABLE_SIZE);
113 output_table(crc32table_be, LE_TABLE_ROWS,
114 BE_TABLE_SIZE, "tobe");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 printf("};\n");
116 }
117
118 return 0;
119}