Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/lib/crc/gen_crc32table.c
26285 views
1
// SPDX-License-Identifier: GPL-2.0
2
#include <stdio.h>
3
#include "../../include/linux/crc32poly.h"
4
#include "../../include/generated/autoconf.h"
5
#include <inttypes.h>
6
7
static uint32_t crc32table_le[256];
8
static uint32_t crc32table_be[256];
9
static uint32_t crc32ctable_le[256];
10
11
/**
12
* crc32init_le() - allocate and initialize LE table data
13
*
14
* crc is the crc of the byte i; other entries are filled in based on the
15
* fact that crctable[i^j] = crctable[i] ^ crctable[j].
16
*
17
*/
18
static void crc32init_le_generic(const uint32_t polynomial, uint32_t tab[256])
19
{
20
unsigned i, j;
21
uint32_t crc = 1;
22
23
tab[0] = 0;
24
25
for (i = 128; i; i >>= 1) {
26
crc = (crc >> 1) ^ ((crc & 1) ? polynomial : 0);
27
for (j = 0; j < 256; j += 2 * i)
28
tab[i + j] = crc ^ tab[j];
29
}
30
}
31
32
static void crc32init_le(void)
33
{
34
crc32init_le_generic(CRC32_POLY_LE, crc32table_le);
35
}
36
37
static void crc32cinit_le(void)
38
{
39
crc32init_le_generic(CRC32C_POLY_LE, crc32ctable_le);
40
}
41
42
/**
43
* crc32init_be() - allocate and initialize BE table data
44
*/
45
static void crc32init_be(void)
46
{
47
unsigned i, j;
48
uint32_t crc = 0x80000000;
49
50
crc32table_be[0] = 0;
51
52
for (i = 1; i < 256; i <<= 1) {
53
crc = (crc << 1) ^ ((crc & 0x80000000) ? CRC32_POLY_BE : 0);
54
for (j = 0; j < i; j++)
55
crc32table_be[i + j] = crc ^ crc32table_be[j];
56
}
57
}
58
59
static void output_table(const uint32_t table[256])
60
{
61
int i;
62
63
for (i = 0; i < 256; i += 4) {
64
printf("\t0x%08x, 0x%08x, 0x%08x, 0x%08x,\n",
65
table[i], table[i + 1], table[i + 2], table[i + 3]);
66
}
67
}
68
69
int main(int argc, char** argv)
70
{
71
printf("/* this file is generated - do not edit */\n\n");
72
73
crc32init_le();
74
printf("static const u32 ____cacheline_aligned crc32table_le[256] = {\n");
75
output_table(crc32table_le);
76
printf("};\n");
77
78
crc32init_be();
79
printf("static const u32 ____cacheline_aligned crc32table_be[256] = {\n");
80
output_table(crc32table_be);
81
printf("};\n");
82
83
crc32cinit_le();
84
printf("static const u32 ____cacheline_aligned crc32ctable_le[256] = {\n");
85
output_table(crc32ctable_le);
86
printf("};\n");
87
88
return 0;
89
}
90
91