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