]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - lib/gen_crc32table.c
crc32: add note about this patchset to crc32.c
[mirror_ubuntu-bionic-kernel.git] / lib / gen_crc32table.c
CommitLineData
1da177e4 1#include <stdio.h>
324eb0f1 2#include "../include/generated/autoconf.h"
1da177e4
LT
3#include "crc32defs.h"
4#include <inttypes.h>
5
6#define ENTRIES_PER_LINE 4
7
324eb0f1
BP
8#if CRC_LE_BITS > 8
9# define LE_TABLE_ROWS (CRC_LE_BITS/8)
10# define LE_TABLE_SIZE 256
9a1dbf6a 11#else
324eb0f1
BP
12# define LE_TABLE_ROWS 1
13# define LE_TABLE_SIZE (1 << CRC_LE_BITS)
9a1dbf6a
BP
14#endif
15
324eb0f1
BP
16#if CRC_BE_BITS > 8
17# define BE_TABLE_ROWS (CRC_BE_BITS/8)
18# define BE_TABLE_SIZE 256
9a1dbf6a 19#else
324eb0f1
BP
20# define BE_TABLE_ROWS 1
21# define BE_TABLE_SIZE (1 << CRC_BE_BITS)
9a1dbf6a 22#endif
1da177e4 23
324eb0f1
BP
24static uint32_t crc32table_le[LE_TABLE_ROWS][256];
25static uint32_t crc32table_be[BE_TABLE_ROWS][256];
1da177e4
LT
26
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
836e2af9 39 crc32table_le[0][0] = 0;
1da177e4 40
9a1dbf6a 41 for (i = LE_TABLE_SIZE >> 1; i; i >>= 1) {
1da177e4
LT
42 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
43 for (j = 0; j < LE_TABLE_SIZE; j += 2 * i)
836e2af9
JT
44 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];
324eb0f1 48 for (j = 1; j < LE_TABLE_ROWS; j++) {
836e2af9
JT
49 crc = crc32table_le[0][crc & 0xff] ^ (crc >> 8);
50 crc32table_le[j][i] = crc;
51 }
1da177e4
LT
52 }
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
836e2af9 63 crc32table_be[0][0] = 0;
1da177e4
LT
64
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++)
836e2af9
JT
68 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];
324eb0f1 72 for (j = 1; j < BE_TABLE_ROWS; j++) {
836e2af9
JT
73 crc = crc32table_be[0][(crc >> 24) & 0xff] ^ (crc << 8);
74 crc32table_be[j][i] = crc;
75 }
1da177e4
LT
76 }
77}
78
324eb0f1 79static void output_table(uint32_t (*table)[256], int rows, int len, char *trans)
1da177e4 80{
836e2af9 81 int i, j;
1da177e4 82
324eb0f1 83 for (j = 0 ; j < rows; j++) {
836e2af9
JT
84 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]);
1da177e4 91 }
1da177e4
LT
92}
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();
324eb0f1
BP
100 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");
1da177e4
LT
105 printf("};\n");
106 }
107
108 if (CRC_BE_BITS > 1) {
109 crc32init_be();
324eb0f1
BP
110 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");
1da177e4
LT
115 printf("};\n");
116 }
117
118 return 0;
119}