blob: 8f26660ea10a4cd47255a46c2cd5ec9113d5d420 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#include <stdio.h>
Bob Pearson324eb0f2012-03-23 15:02:24 -07003#include "../include/generated/autoconf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#include "crc32defs.h"
5#include <inttypes.h>
6
7#define ENTRIES_PER_LINE 4
8
Bob Pearson324eb0f2012-03-23 15:02:24 -07009#if CRC_LE_BITS > 8
10# define LE_TABLE_ROWS (CRC_LE_BITS/8)
11# define LE_TABLE_SIZE 256
Bob Pearson9a1dbf62012-03-23 15:02:23 -070012#else
Bob Pearson324eb0f2012-03-23 15:02:24 -070013# define LE_TABLE_ROWS 1
14# define LE_TABLE_SIZE (1 << CRC_LE_BITS)
Bob Pearson9a1dbf62012-03-23 15:02:23 -070015#endif
16
Bob Pearson324eb0f2012-03-23 15:02:24 -070017#if CRC_BE_BITS > 8
18# define BE_TABLE_ROWS (CRC_BE_BITS/8)
19# define BE_TABLE_SIZE 256
Bob Pearson9a1dbf62012-03-23 15:02:23 -070020#else
Bob Pearson324eb0f2012-03-23 15:02:24 -070021# define BE_TABLE_ROWS 1
22# define BE_TABLE_SIZE (1 << CRC_BE_BITS)
Bob Pearson9a1dbf62012-03-23 15:02:23 -070023#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Bob Pearson324eb0f2012-03-23 15:02:24 -070025static uint32_t crc32table_le[LE_TABLE_ROWS][256];
26static uint32_t crc32table_be[BE_TABLE_ROWS][256];
Darrick J. Wong46c58012012-03-23 15:02:25 -070027static uint32_t crc32ctable_le[LE_TABLE_ROWS][256];
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29/**
30 * crc32init_le() - allocate and initialize LE table data
31 *
32 * crc is the crc of the byte i; other entries are filled in based on the
33 * fact that crctable[i^j] = crctable[i] ^ crctable[j].
34 *
35 */
Darrick J. Wong46c58012012-03-23 15:02:25 -070036static void crc32init_le_generic(const uint32_t polynomial,
37 uint32_t (*tab)[256])
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
39 unsigned i, j;
40 uint32_t crc = 1;
41
Darrick J. Wong46c58012012-03-23 15:02:25 -070042 tab[0][0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Bob Pearson9a1dbf62012-03-23 15:02:23 -070044 for (i = LE_TABLE_SIZE >> 1; i; i >>= 1) {
Darrick J. Wong46c58012012-03-23 15:02:25 -070045 crc = (crc >> 1) ^ ((crc & 1) ? polynomial : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 for (j = 0; j < LE_TABLE_SIZE; j += 2 * i)
Darrick J. Wong46c58012012-03-23 15:02:25 -070047 tab[0][i + j] = crc ^ tab[0][j];
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070048 }
49 for (i = 0; i < LE_TABLE_SIZE; i++) {
Darrick J. Wong46c58012012-03-23 15:02:25 -070050 crc = tab[0][i];
Bob Pearson324eb0f2012-03-23 15:02:24 -070051 for (j = 1; j < LE_TABLE_ROWS; j++) {
Darrick J. Wong46c58012012-03-23 15:02:25 -070052 crc = tab[0][crc & 0xff] ^ (crc >> 8);
53 tab[j][i] = crc;
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 }
56}
57
Darrick J. Wong46c58012012-03-23 15:02:25 -070058static void crc32init_le(void)
59{
60 crc32init_le_generic(CRCPOLY_LE, crc32table_le);
61}
62
63static void crc32cinit_le(void)
64{
65 crc32init_le_generic(CRC32C_POLY_LE, crc32ctable_le);
66}
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/**
69 * crc32init_be() - allocate and initialize BE table data
70 */
71static void crc32init_be(void)
72{
73 unsigned i, j;
74 uint32_t crc = 0x80000000;
75
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070076 crc32table_be[0][0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 for (i = 1; i < BE_TABLE_SIZE; i <<= 1) {
79 crc = (crc << 1) ^ ((crc & 0x80000000) ? CRCPOLY_BE : 0);
80 for (j = 0; j < i; j++)
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070081 crc32table_be[0][i + j] = crc ^ crc32table_be[0][j];
82 }
83 for (i = 0; i < BE_TABLE_SIZE; i++) {
84 crc = crc32table_be[0][i];
Bob Pearson324eb0f2012-03-23 15:02:24 -070085 for (j = 1; j < BE_TABLE_ROWS; j++) {
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070086 crc = crc32table_be[0][(crc >> 24) & 0xff] ^ (crc << 8);
87 crc32table_be[j][i] = crc;
88 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 }
90}
91
Bob Pearson324eb0f2012-03-23 15:02:24 -070092static void output_table(uint32_t (*table)[256], int rows, int len, char *trans)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070094 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Bob Pearson324eb0f2012-03-23 15:02:24 -070096 for (j = 0 ; j < rows; j++) {
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070097 printf("{");
98 for (i = 0; i < len - 1; i++) {
99 if (i % ENTRIES_PER_LINE == 0)
100 printf("\n");
101 printf("%s(0x%8.8xL), ", trans, table[j][i]);
102 }
103 printf("%s(0x%8.8xL)},\n", trans, table[j][len - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
107int main(int argc, char** argv)
108{
109 printf("/* this file is generated - do not edit */\n\n");
110
111 if (CRC_LE_BITS > 1) {
112 crc32init_le();
Daniel Borkmannf5e38b92015-02-13 14:36:21 -0800113 printf("static const u32 ____cacheline_aligned "
Bob Pearson324eb0f2012-03-23 15:02:24 -0700114 "crc32table_le[%d][%d] = {",
115 LE_TABLE_ROWS, LE_TABLE_SIZE);
116 output_table(crc32table_le, LE_TABLE_ROWS,
117 LE_TABLE_SIZE, "tole");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 printf("};\n");
119 }
120
121 if (CRC_BE_BITS > 1) {
122 crc32init_be();
Daniel Borkmannf5e38b92015-02-13 14:36:21 -0800123 printf("static const u32 ____cacheline_aligned "
Bob Pearson324eb0f2012-03-23 15:02:24 -0700124 "crc32table_be[%d][%d] = {",
125 BE_TABLE_ROWS, BE_TABLE_SIZE);
126 output_table(crc32table_be, LE_TABLE_ROWS,
127 BE_TABLE_SIZE, "tobe");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 printf("};\n");
129 }
Darrick J. Wong46c58012012-03-23 15:02:25 -0700130 if (CRC_LE_BITS > 1) {
131 crc32cinit_le();
Daniel Borkmannf5e38b92015-02-13 14:36:21 -0800132 printf("static const u32 ____cacheline_aligned "
Darrick J. Wong46c58012012-03-23 15:02:25 -0700133 "crc32ctable_le[%d][%d] = {",
134 LE_TABLE_ROWS, LE_TABLE_SIZE);
135 output_table(crc32ctable_le, LE_TABLE_ROWS,
136 LE_TABLE_SIZE, "tole");
137 printf("};\n");
138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 return 0;
141}