b24413180f560 (Greg Kroah-Hartman 2017-11-01 15:07:57 +0100 1) // SPDX-License-Identifier: GPL-2.0
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2) #include <stdio.h>
1fb2e3f276dda (Krzysztof Kozlowski 2018-07-17 18:05:36 +0200 3) #include "../include/linux/crc32poly.h"
324eb0f17d9dc (Bob Pearson 2012-03-23 15:02:24 -0700 4) #include "../include/generated/autoconf.h"
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 5) #include "crc32defs.h"
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 6) #include <inttypes.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 7)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 8) #define ENTRIES_PER_LINE 4
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 9)
324eb0f17d9dc (Bob Pearson 2012-03-23 15:02:24 -0700 10) #if CRC_LE_BITS > 8
324eb0f17d9dc (Bob Pearson 2012-03-23 15:02:24 -0700 11) # define LE_TABLE_ROWS (CRC_LE_BITS/8)
324eb0f17d9dc (Bob Pearson 2012-03-23 15:02:24 -0700 12) # define LE_TABLE_SIZE 256
9a1dbf6a29694 (Bob Pearson 2012-03-23 15:02:23 -0700 13) #else
324eb0f17d9dc (Bob Pearson 2012-03-23 15:02:24 -0700 14) # define LE_TABLE_ROWS 1
324eb0f17d9dc (Bob Pearson 2012-03-23 15:02:24 -0700 15) # define LE_TABLE_SIZE (1 << CRC_LE_BITS)
9a1dbf6a29694 (Bob Pearson 2012-03-23 15:02:23 -0700 16) #endif
9a1dbf6a29694 (Bob Pearson 2012-03-23 15:02:23 -0700 17)
324eb0f17d9dc (Bob Pearson 2012-03-23 15:02:24 -0700 18) #if CRC_BE_BITS > 8
324eb0f17d9dc (Bob Pearson 2012-03-23 15:02:24 -0700 19) # define BE_TABLE_ROWS (CRC_BE_BITS/8)
324eb0f17d9dc (Bob Pearson 2012-03-23 15:02:24 -0700 20) # define BE_TABLE_SIZE 256
9a1dbf6a29694 (Bob Pearson 2012-03-23 15:02:23 -0700 21) #else
324eb0f17d9dc (Bob Pearson 2012-03-23 15:02:24 -0700 22) # define BE_TABLE_ROWS 1
324eb0f17d9dc (Bob Pearson 2012-03-23 15:02:24 -0700 23) # define BE_TABLE_SIZE (1 << CRC_BE_BITS)
9a1dbf6a29694 (Bob Pearson 2012-03-23 15:02:23 -0700 24) #endif
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 25)
324eb0f17d9dc (Bob Pearson 2012-03-23 15:02:24 -0700 26) static uint32_t crc32table_le[LE_TABLE_ROWS][256];
324eb0f17d9dc (Bob Pearson 2012-03-23 15:02:24 -0700 27) static uint32_t crc32table_be[BE_TABLE_ROWS][256];
46c5801eaf86e (Darrick J. Wong 2012-03-23 15:02:25 -0700 28) static uint32_t crc32ctable_le[LE_TABLE_ROWS][256];
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 29)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 30) /**
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 31) * crc32init_le() - allocate and initialize LE table data
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 32) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 33) * crc is the crc of the byte i; other entries are filled in based on the
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 34) * fact that crctable[i^j] = crctable[i] ^ crctable[j].
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 35) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 36) */
46c5801eaf86e (Darrick J. Wong 2012-03-23 15:02:25 -0700 37) static void crc32init_le_generic(const uint32_t polynomial,
46c5801eaf86e (Darrick J. Wong 2012-03-23 15:02:25 -0700 38) uint32_t (*tab)[256])
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 39) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 40) unsigned i, j;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 41) uint32_t crc = 1;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 42)
46c5801eaf86e (Darrick J. Wong 2012-03-23 15:02:25 -0700 43) tab[0][0] = 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 44)
9a1dbf6a29694 (Bob Pearson 2012-03-23 15:02:23 -0700 45) for (i = LE_TABLE_SIZE >> 1; i; i >>= 1) {
46c5801eaf86e (Darrick J. Wong 2012-03-23 15:02:25 -0700 46) crc = (crc >> 1) ^ ((crc & 1) ? polynomial : 0);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 47) for (j = 0; j < LE_TABLE_SIZE; j += 2 * i)
46c5801eaf86e (Darrick J. Wong 2012-03-23 15:02:25 -0700 48) tab[0][i + j] = crc ^ tab[0][j];
836e2af92503f (Joakim Tjernlund 2010-05-24 14:33:31 -0700 49) }
836e2af92503f (Joakim Tjernlund 2010-05-24 14:33:31 -0700 50) for (i = 0; i < LE_TABLE_SIZE; i++) {
46c5801eaf86e (Darrick J. Wong 2012-03-23 15:02:25 -0700 51) crc = tab[0][i];
324eb0f17d9dc (Bob Pearson 2012-03-23 15:02:24 -0700 52) for (j = 1; j < LE_TABLE_ROWS; j++) {
46c5801eaf86e (Darrick J. Wong 2012-03-23 15:02:25 -0700 53) crc = tab[0][crc & 0xff] ^ (crc >> 8);
46c5801eaf86e (Darrick J. Wong 2012-03-23 15:02:25 -0700 54) tab[j][i] = crc;
836e2af92503f (Joakim Tjernlund 2010-05-24 14:33:31 -0700 55) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 56) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 57) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 58)
46c5801eaf86e (Darrick J. Wong 2012-03-23 15:02:25 -0700 59) static void crc32init_le(void)
46c5801eaf86e (Darrick J. Wong 2012-03-23 15:02:25 -0700 60) {
e37f2f93afe59 (Krzysztof Kozlowski 2018-07-17 18:05:37 +0200 61) crc32init_le_generic(CRC32_POLY_LE, crc32table_le);
46c5801eaf86e (Darrick J. Wong 2012-03-23 15:02:25 -0700 62) }
46c5801eaf86e (Darrick J. Wong 2012-03-23 15:02:25 -0700 63)
46c5801eaf86e (Darrick J. Wong 2012-03-23 15:02:25 -0700 64) static void crc32cinit_le(void)
46c5801eaf86e (Darrick J. Wong 2012-03-23 15:02:25 -0700 65) {
46c5801eaf86e (Darrick J. Wong 2012-03-23 15:02:25 -0700 66) crc32init_le_generic(CRC32C_POLY_LE, crc32ctable_le);
46c5801eaf86e (Darrick J. Wong 2012-03-23 15:02:25 -0700 67) }
46c5801eaf86e (Darrick J. Wong 2012-03-23 15:02:25 -0700 68)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 69) /**
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 70) * crc32init_be() - allocate and initialize BE table data
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 71) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 72) static void crc32init_be(void)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 73) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 74) unsigned i, j;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 75) uint32_t crc = 0x80000000;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 76)
836e2af92503f (Joakim Tjernlund 2010-05-24 14:33:31 -0700 77) crc32table_be[0][0] = 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 78)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 79) for (i = 1; i < BE_TABLE_SIZE; i <<= 1) {
e37f2f93afe59 (Krzysztof Kozlowski 2018-07-17 18:05:37 +0200 80) crc = (crc << 1) ^ ((crc & 0x80000000) ? CRC32_POLY_BE : 0);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 81) for (j = 0; j < i; j++)
836e2af92503f (Joakim Tjernlund 2010-05-24 14:33:31 -0700 82) crc32table_be[0][i + j] = crc ^ crc32table_be[0][j];
836e2af92503f (Joakim Tjernlund 2010-05-24 14:33:31 -0700 83) }
836e2af92503f (Joakim Tjernlund 2010-05-24 14:33:31 -0700 84) for (i = 0; i < BE_TABLE_SIZE; i++) {
836e2af92503f (Joakim Tjernlund 2010-05-24 14:33:31 -0700 85) crc = crc32table_be[0][i];
324eb0f17d9dc (Bob Pearson 2012-03-23 15:02:24 -0700 86) for (j = 1; j < BE_TABLE_ROWS; j++) {
836e2af92503f (Joakim Tjernlund 2010-05-24 14:33:31 -0700 87) crc = crc32table_be[0][(crc >> 24) & 0xff] ^ (crc << 8);
836e2af92503f (Joakim Tjernlund 2010-05-24 14:33:31 -0700 88) crc32table_be[j][i] = crc;
836e2af92503f (Joakim Tjernlund 2010-05-24 14:33:31 -0700 89) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 90) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 91) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 92)
324eb0f17d9dc (Bob Pearson 2012-03-23 15:02:24 -0700 93) static void output_table(uint32_t (*table)[256], int rows, int len, char *trans)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 94) {
836e2af92503f (Joakim Tjernlund 2010-05-24 14:33:31 -0700 95) int i, j;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 96)
324eb0f17d9dc (Bob Pearson 2012-03-23 15:02:24 -0700 97) for (j = 0 ; j < rows; j++) {
836e2af92503f (Joakim Tjernlund 2010-05-24 14:33:31 -0700 98) printf("{");
836e2af92503f (Joakim Tjernlund 2010-05-24 14:33:31 -0700 99) for (i = 0; i < len - 1; i++) {
836e2af92503f (Joakim Tjernlund 2010-05-24 14:33:31 -0700 100) if (i % ENTRIES_PER_LINE == 0)
836e2af92503f (Joakim Tjernlund 2010-05-24 14:33:31 -0700 101) printf("\n");
836e2af92503f (Joakim Tjernlund 2010-05-24 14:33:31 -0700 102) printf("%s(0x%8.8xL), ", trans, table[j][i]);
836e2af92503f (Joakim Tjernlund 2010-05-24 14:33:31 -0700 103) }
836e2af92503f (Joakim Tjernlund 2010-05-24 14:33:31 -0700 104) printf("%s(0x%8.8xL)},\n", trans, table[j][len - 1]);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 105) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 106) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 107)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 108) int main(int argc, char** argv)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 109) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 110) printf("/* this file is generated - do not edit */\n\n");
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 111)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 112) if (CRC_LE_BITS > 1) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 113) crc32init_le();
f5e38b9284e13 (Daniel Borkmann 2015-02-13 14:36:21 -0800 114) printf("static const u32 ____cacheline_aligned "
324eb0f17d9dc (Bob Pearson 2012-03-23 15:02:24 -0700 115) "crc32table_le[%d][%d] = {",
324eb0f17d9dc (Bob Pearson 2012-03-23 15:02:24 -0700 116) LE_TABLE_ROWS, LE_TABLE_SIZE);
324eb0f17d9dc (Bob Pearson 2012-03-23 15:02:24 -0700 117) output_table(crc32table_le, LE_TABLE_ROWS,
324eb0f17d9dc (Bob Pearson 2012-03-23 15:02:24 -0700 118) LE_TABLE_SIZE, "tole");
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 119) printf("};\n");
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 120) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 121)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 122) if (CRC_BE_BITS > 1) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 123) crc32init_be();
f5e38b9284e13 (Daniel Borkmann 2015-02-13 14:36:21 -0800 124) printf("static const u32 ____cacheline_aligned "
324eb0f17d9dc (Bob Pearson 2012-03-23 15:02:24 -0700 125) "crc32table_be[%d][%d] = {",
324eb0f17d9dc (Bob Pearson 2012-03-23 15:02:24 -0700 126) BE_TABLE_ROWS, BE_TABLE_SIZE);
324eb0f17d9dc (Bob Pearson 2012-03-23 15:02:24 -0700 127) output_table(crc32table_be, LE_TABLE_ROWS,
324eb0f17d9dc (Bob Pearson 2012-03-23 15:02:24 -0700 128) BE_TABLE_SIZE, "tobe");
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 129) printf("};\n");
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 130) }
46c5801eaf86e (Darrick J. Wong 2012-03-23 15:02:25 -0700 131) if (CRC_LE_BITS > 1) {
46c5801eaf86e (Darrick J. Wong 2012-03-23 15:02:25 -0700 132) crc32cinit_le();
f5e38b9284e13 (Daniel Borkmann 2015-02-13 14:36:21 -0800 133) printf("static const u32 ____cacheline_aligned "
46c5801eaf86e (Darrick J. Wong 2012-03-23 15:02:25 -0700 134) "crc32ctable_le[%d][%d] = {",
46c5801eaf86e (Darrick J. Wong 2012-03-23 15:02:25 -0700 135) LE_TABLE_ROWS, LE_TABLE_SIZE);
46c5801eaf86e (Darrick J. Wong 2012-03-23 15:02:25 -0700 136) output_table(crc32ctable_le, LE_TABLE_ROWS,
46c5801eaf86e (Darrick J. Wong 2012-03-23 15:02:25 -0700 137) LE_TABLE_SIZE, "tole");
46c5801eaf86e (Darrick J. Wong 2012-03-23 15:02:25 -0700 138) printf("};\n");
46c5801eaf86e (Darrick J. Wong 2012-03-23 15:02:25 -0700 139) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 140)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 141) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 142) }