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]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * crc32init_le() - allocate and initialize LE table data |
| 29 | * |
| 30 | * crc is the crc of the byte i; other entries are filled in based on the |
| 31 | * fact that crctable[i^j] = crctable[i] ^ crctable[j]. |
| 32 | * |
| 33 | */ |
| 34 | static void crc32init_le(void) |
| 35 | { |
| 36 | unsigned i, j; |
| 37 | uint32_t crc = 1; |
| 38 | |
Joakim Tjernlund | 836e2af | 2010-05-24 14:33:31 -0700 | [diff] [blame] | 39 | crc32table_le[0][0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Bob Pearson | 9a1dbf6 | 2012-03-23 15:02:23 -0700 | [diff] [blame] | 41 | for (i = LE_TABLE_SIZE >> 1; i; i >>= 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0); |
| 43 | for (j = 0; j < LE_TABLE_SIZE; j += 2 * i) |
Joakim Tjernlund | 836e2af | 2010-05-24 14:33:31 -0700 | [diff] [blame] | 44 | crc32table_le[0][i + j] = crc ^ crc32table_le[0][j]; |
| 45 | } |
| 46 | for (i = 0; i < LE_TABLE_SIZE; i++) { |
| 47 | crc = crc32table_le[0][i]; |
Bob Pearson | 324eb0f | 2012-03-23 15:02:24 -0700 | [diff] [blame] | 48 | for (j = 1; j < LE_TABLE_ROWS; j++) { |
Joakim Tjernlund | 836e2af | 2010-05-24 14:33:31 -0700 | [diff] [blame] | 49 | crc = crc32table_le[0][crc & 0xff] ^ (crc >> 8); |
| 50 | crc32table_le[j][i] = crc; |
| 51 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * crc32init_be() - allocate and initialize BE table data |
| 57 | */ |
| 58 | static void crc32init_be(void) |
| 59 | { |
| 60 | unsigned i, j; |
| 61 | uint32_t crc = 0x80000000; |
| 62 | |
Joakim Tjernlund | 836e2af | 2010-05-24 14:33:31 -0700 | [diff] [blame] | 63 | crc32table_be[0][0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
| 65 | for (i = 1; i < BE_TABLE_SIZE; i <<= 1) { |
| 66 | crc = (crc << 1) ^ ((crc & 0x80000000) ? CRCPOLY_BE : 0); |
| 67 | for (j = 0; j < i; j++) |
Joakim Tjernlund | 836e2af | 2010-05-24 14:33:31 -0700 | [diff] [blame] | 68 | crc32table_be[0][i + j] = crc ^ crc32table_be[0][j]; |
| 69 | } |
| 70 | for (i = 0; i < BE_TABLE_SIZE; i++) { |
| 71 | crc = crc32table_be[0][i]; |
Bob Pearson | 324eb0f | 2012-03-23 15:02:24 -0700 | [diff] [blame] | 72 | for (j = 1; j < BE_TABLE_ROWS; j++) { |
Joakim Tjernlund | 836e2af | 2010-05-24 14:33:31 -0700 | [diff] [blame] | 73 | crc = crc32table_be[0][(crc >> 24) & 0xff] ^ (crc << 8); |
| 74 | crc32table_be[j][i] = crc; |
| 75 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
Bob Pearson | 324eb0f | 2012-03-23 15:02:24 -0700 | [diff] [blame] | 79 | 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] | 80 | { |
Joakim Tjernlund | 836e2af | 2010-05-24 14:33:31 -0700 | [diff] [blame] | 81 | int i, j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
Bob Pearson | 324eb0f | 2012-03-23 15:02:24 -0700 | [diff] [blame] | 83 | for (j = 0 ; j < rows; j++) { |
Joakim Tjernlund | 836e2af | 2010-05-24 14:33:31 -0700 | [diff] [blame] | 84 | printf("{"); |
| 85 | for (i = 0; i < len - 1; i++) { |
| 86 | if (i % ENTRIES_PER_LINE == 0) |
| 87 | printf("\n"); |
| 88 | printf("%s(0x%8.8xL), ", trans, table[j][i]); |
| 89 | } |
| 90 | printf("%s(0x%8.8xL)},\n", trans, table[j][len - 1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | int main(int argc, char** argv) |
| 95 | { |
| 96 | printf("/* this file is generated - do not edit */\n\n"); |
| 97 | |
| 98 | if (CRC_LE_BITS > 1) { |
| 99 | crc32init_le(); |
Bob Pearson | 324eb0f | 2012-03-23 15:02:24 -0700 | [diff] [blame] | 100 | printf("static const u32 __cacheline_aligned " |
| 101 | "crc32table_le[%d][%d] = {", |
| 102 | LE_TABLE_ROWS, LE_TABLE_SIZE); |
| 103 | output_table(crc32table_le, LE_TABLE_ROWS, |
| 104 | LE_TABLE_SIZE, "tole"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | printf("};\n"); |
| 106 | } |
| 107 | |
| 108 | if (CRC_BE_BITS > 1) { |
| 109 | crc32init_be(); |
Bob Pearson | 324eb0f | 2012-03-23 15:02:24 -0700 | [diff] [blame] | 110 | printf("static const u32 __cacheline_aligned " |
| 111 | "crc32table_be[%d][%d] = {", |
| 112 | BE_TABLE_ROWS, BE_TABLE_SIZE); |
| 113 | output_table(crc32table_be, LE_TABLE_ROWS, |
| 114 | BE_TABLE_SIZE, "tobe"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | printf("};\n"); |
| 116 | } |
| 117 | |
| 118 | return 0; |
| 119 | } |