blob: f755b997b9670d04135f5bd572bcce5eab951f6f [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>
Krzysztof Kozlowski1fb2e3f2018-07-17 18:05:36 +02003#include "../include/linux/crc32poly.h"
Bob Pearson324eb0f2012-03-23 15:02:24 -07004#include "../include/generated/autoconf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include "crc32defs.h"
6#include <inttypes.h>
7
8#define ENTRIES_PER_LINE 4
9
Bob Pearson324eb0f2012-03-23 15:02:24 -070010#if CRC_LE_BITS > 8
11# define LE_TABLE_ROWS (CRC_LE_BITS/8)
12# define LE_TABLE_SIZE 256
Bob Pearson9a1dbf62012-03-23 15:02:23 -070013#else
Bob Pearson324eb0f2012-03-23 15:02:24 -070014# define LE_TABLE_ROWS 1
15# define LE_TABLE_SIZE (1 << CRC_LE_BITS)
Bob Pearson9a1dbf62012-03-23 15:02:23 -070016#endif
17
Bob Pearson324eb0f2012-03-23 15:02:24 -070018#if CRC_BE_BITS > 8
19# define BE_TABLE_ROWS (CRC_BE_BITS/8)
20# define BE_TABLE_SIZE 256
Bob Pearson9a1dbf62012-03-23 15:02:23 -070021#else
Bob Pearson324eb0f2012-03-23 15:02:24 -070022# define BE_TABLE_ROWS 1
23# define BE_TABLE_SIZE (1 << CRC_BE_BITS)
Bob Pearson9a1dbf62012-03-23 15:02:23 -070024#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Bob Pearson324eb0f2012-03-23 15:02:24 -070026static uint32_t crc32table_le[LE_TABLE_ROWS][256];
27static uint32_t crc32table_be[BE_TABLE_ROWS][256];
Darrick J. Wong46c58012012-03-23 15:02:25 -070028static uint32_t crc32ctable_le[LE_TABLE_ROWS][256];
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30/**
31 * crc32init_le() - allocate and initialize LE table data
32 *
33 * crc is the crc of the byte i; other entries are filled in based on the
34 * fact that crctable[i^j] = crctable[i] ^ crctable[j].
35 *
36 */
Darrick J. Wong46c58012012-03-23 15:02:25 -070037static void crc32init_le_generic(const uint32_t polynomial,
38 uint32_t (*tab)[256])
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
40 unsigned i, j;
41 uint32_t crc = 1;
42
Darrick J. Wong46c58012012-03-23 15:02:25 -070043 tab[0][0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Bob Pearson9a1dbf62012-03-23 15:02:23 -070045 for (i = LE_TABLE_SIZE >> 1; i; i >>= 1) {
Darrick J. Wong46c58012012-03-23 15:02:25 -070046 crc = (crc >> 1) ^ ((crc & 1) ? polynomial : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 for (j = 0; j < LE_TABLE_SIZE; j += 2 * i)
Darrick J. Wong46c58012012-03-23 15:02:25 -070048 tab[0][i + j] = crc ^ tab[0][j];
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070049 }
50 for (i = 0; i < LE_TABLE_SIZE; i++) {
Darrick J. Wong46c58012012-03-23 15:02:25 -070051 crc = tab[0][i];
Bob Pearson324eb0f2012-03-23 15:02:24 -070052 for (j = 1; j < LE_TABLE_ROWS; j++) {
Darrick J. Wong46c58012012-03-23 15:02:25 -070053 crc = tab[0][crc & 0xff] ^ (crc >> 8);
54 tab[j][i] = crc;
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 }
57}
58
Darrick J. Wong46c58012012-03-23 15:02:25 -070059static void crc32init_le(void)
60{
Krzysztof Kozlowskie37f2f92018-07-17 18:05:37 +020061 crc32init_le_generic(CRC32_POLY_LE, crc32table_le);
Darrick J. Wong46c58012012-03-23 15:02:25 -070062}
63
64static void crc32cinit_le(void)
65{
66 crc32init_le_generic(CRC32C_POLY_LE, crc32ctable_le);
67}
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/**
70 * crc32init_be() - allocate and initialize BE table data
71 */
72static void crc32init_be(void)
73{
74 unsigned i, j;
75 uint32_t crc = 0x80000000;
76
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070077 crc32table_be[0][0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 for (i = 1; i < BE_TABLE_SIZE; i <<= 1) {
Krzysztof Kozlowskie37f2f92018-07-17 18:05:37 +020080 crc = (crc << 1) ^ ((crc & 0x80000000) ? CRC32_POLY_BE : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 for (j = 0; j < i; j++)
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070082 crc32table_be[0][i + j] = crc ^ crc32table_be[0][j];
83 }
84 for (i = 0; i < BE_TABLE_SIZE; i++) {
85 crc = crc32table_be[0][i];
Bob Pearson324eb0f2012-03-23 15:02:24 -070086 for (j = 1; j < BE_TABLE_ROWS; j++) {
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070087 crc = crc32table_be[0][(crc >> 24) & 0xff] ^ (crc << 8);
88 crc32table_be[j][i] = crc;
89 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
91}
92
Bob Pearson324eb0f2012-03-23 15:02:24 -070093static void output_table(uint32_t (*table)[256], int rows, int len, char *trans)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070095 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Bob Pearson324eb0f2012-03-23 15:02:24 -070097 for (j = 0 ; j < rows; j++) {
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070098 printf("{");
99 for (i = 0; i < len - 1; i++) {
100 if (i % ENTRIES_PER_LINE == 0)
101 printf("\n");
102 printf("%s(0x%8.8xL), ", trans, table[j][i]);
103 }
104 printf("%s(0x%8.8xL)},\n", trans, table[j][len - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
108int main(int argc, char** argv)
109{
110 printf("/* this file is generated - do not edit */\n\n");
111
112 if (CRC_LE_BITS > 1) {
113 crc32init_le();
Daniel Borkmannf5e38b92015-02-13 14:36:21 -0800114 printf("static const u32 ____cacheline_aligned "
Bob Pearson324eb0f2012-03-23 15:02:24 -0700115 "crc32table_le[%d][%d] = {",
116 LE_TABLE_ROWS, LE_TABLE_SIZE);
117 output_table(crc32table_le, LE_TABLE_ROWS,
118 LE_TABLE_SIZE, "tole");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 printf("};\n");
120 }
121
122 if (CRC_BE_BITS > 1) {
123 crc32init_be();
Daniel Borkmannf5e38b92015-02-13 14:36:21 -0800124 printf("static const u32 ____cacheline_aligned "
Bob Pearson324eb0f2012-03-23 15:02:24 -0700125 "crc32table_be[%d][%d] = {",
126 BE_TABLE_ROWS, BE_TABLE_SIZE);
127 output_table(crc32table_be, LE_TABLE_ROWS,
128 BE_TABLE_SIZE, "tobe");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 printf("};\n");
130 }
Darrick J. Wong46c58012012-03-23 15:02:25 -0700131 if (CRC_LE_BITS > 1) {
132 crc32cinit_le();
Daniel Borkmannf5e38b92015-02-13 14:36:21 -0800133 printf("static const u32 ____cacheline_aligned "
Darrick J. Wong46c58012012-03-23 15:02:25 -0700134 "crc32ctable_le[%d][%d] = {",
135 LE_TABLE_ROWS, LE_TABLE_SIZE);
136 output_table(crc32ctable_le, LE_TABLE_ROWS,
137 LE_TABLE_SIZE, "tole");
138 printf("};\n");
139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 return 0;
142}