aboutsummaryrefslogtreecommitdiffstats
path: root/lib/gen_crc32table.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/gen_crc32table.c')
-rw-r--r--lib/gen_crc32table.c81
1 files changed, 61 insertions, 20 deletions
diff --git a/lib/gen_crc32table.c b/lib/gen_crc32table.c
index 85d0e412a04f..8f8d5439e2d9 100644
--- a/lib/gen_crc32table.c
+++ b/lib/gen_crc32table.c
@@ -1,14 +1,29 @@
1#include <stdio.h> 1#include <stdio.h>
2#include "../include/generated/autoconf.h"
2#include "crc32defs.h" 3#include "crc32defs.h"
3#include <inttypes.h> 4#include <inttypes.h>
4 5
5#define ENTRIES_PER_LINE 4 6#define ENTRIES_PER_LINE 4
6 7
7#define LE_TABLE_SIZE (1 << CRC_LE_BITS) 8#if CRC_LE_BITS > 8
8#define BE_TABLE_SIZE (1 << CRC_BE_BITS) 9# define LE_TABLE_ROWS (CRC_LE_BITS/8)
10# define LE_TABLE_SIZE 256
11#else
12# define LE_TABLE_ROWS 1
13# define LE_TABLE_SIZE (1 << CRC_LE_BITS)
14#endif
9 15
10static uint32_t crc32table_le[4][LE_TABLE_SIZE]; 16#if CRC_BE_BITS > 8
11static uint32_t crc32table_be[4][BE_TABLE_SIZE]; 17# define BE_TABLE_ROWS (CRC_BE_BITS/8)
18# define BE_TABLE_SIZE 256
19#else
20# define BE_TABLE_ROWS 1
21# define BE_TABLE_SIZE (1 << CRC_BE_BITS)
22#endif
23
24static uint32_t crc32table_le[LE_TABLE_ROWS][256];
25static uint32_t crc32table_be[BE_TABLE_ROWS][256];
26static uint32_t crc32ctable_le[LE_TABLE_ROWS][256];
12 27
13/** 28/**
14 * crc32init_le() - allocate and initialize LE table data 29 * crc32init_le() - allocate and initialize LE table data
@@ -17,27 +32,38 @@ static uint32_t crc32table_be[4][BE_TABLE_SIZE];
17 * fact that crctable[i^j] = crctable[i] ^ crctable[j]. 32 * fact that crctable[i^j] = crctable[i] ^ crctable[j].
18 * 33 *
19 */ 34 */
20static void crc32init_le(void) 35static void crc32init_le_generic(const uint32_t polynomial,
36 uint32_t (*tab)[256])
21{ 37{
22 unsigned i, j; 38 unsigned i, j;
23 uint32_t crc = 1; 39 uint32_t crc = 1;
24 40
25 crc32table_le[0][0] = 0; 41 tab[0][0] = 0;
26 42
27 for (i = 1 << (CRC_LE_BITS - 1); i; i >>= 1) { 43 for (i = LE_TABLE_SIZE >> 1; i; i >>= 1) {
28 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0); 44 crc = (crc >> 1) ^ ((crc & 1) ? polynomial : 0);
29 for (j = 0; j < LE_TABLE_SIZE; j += 2 * i) 45 for (j = 0; j < LE_TABLE_SIZE; j += 2 * i)
30 crc32table_le[0][i + j] = crc ^ crc32table_le[0][j]; 46 tab[0][i + j] = crc ^ tab[0][j];
31 } 47 }
32 for (i = 0; i < LE_TABLE_SIZE; i++) { 48 for (i = 0; i < LE_TABLE_SIZE; i++) {
33 crc = crc32table_le[0][i]; 49 crc = tab[0][i];
34 for (j = 1; j < 4; j++) { 50 for (j = 1; j < LE_TABLE_ROWS; j++) {
35 crc = crc32table_le[0][crc & 0xff] ^ (crc >> 8); 51 crc = tab[0][crc & 0xff] ^ (crc >> 8);
36 crc32table_le[j][i] = crc; 52 tab[j][i] = crc;
37 } 53 }
38 } 54 }
39} 55}
40 56
57static void crc32init_le(void)
58{
59 crc32init_le_generic(CRCPOLY_LE, crc32table_le);
60}
61
62static void crc32cinit_le(void)
63{
64 crc32init_le_generic(CRC32C_POLY_LE, crc32ctable_le);
65}
66
41/** 67/**
42 * crc32init_be() - allocate and initialize BE table data 68 * crc32init_be() - allocate and initialize BE table data
43 */ 69 */
@@ -55,18 +81,18 @@ static void crc32init_be(void)
55 } 81 }
56 for (i = 0; i < BE_TABLE_SIZE; i++) { 82 for (i = 0; i < BE_TABLE_SIZE; i++) {
57 crc = crc32table_be[0][i]; 83 crc = crc32table_be[0][i];
58 for (j = 1; j < 4; j++) { 84 for (j = 1; j < BE_TABLE_ROWS; j++) {
59 crc = crc32table_be[0][(crc >> 24) & 0xff] ^ (crc << 8); 85 crc = crc32table_be[0][(crc >> 24) & 0xff] ^ (crc << 8);
60 crc32table_be[j][i] = crc; 86 crc32table_be[j][i] = crc;
61 } 87 }
62 } 88 }
63} 89}
64 90
65static void output_table(uint32_t table[4][256], int len, char *trans) 91static void output_table(uint32_t (*table)[256], int rows, int len, char *trans)
66{ 92{
67 int i, j; 93 int i, j;
68 94
69 for (j = 0 ; j < 4; j++) { 95 for (j = 0 ; j < rows; j++) {
70 printf("{"); 96 printf("{");
71 for (i = 0; i < len - 1; i++) { 97 for (i = 0; i < len - 1; i++) {
72 if (i % ENTRIES_PER_LINE == 0) 98 if (i % ENTRIES_PER_LINE == 0)
@@ -83,15 +109,30 @@ int main(int argc, char** argv)
83 109
84 if (CRC_LE_BITS > 1) { 110 if (CRC_LE_BITS > 1) {
85 crc32init_le(); 111 crc32init_le();
86 printf("static const u32 crc32table_le[4][256] = {"); 112 printf("static const u32 __cacheline_aligned "
87 output_table(crc32table_le, LE_TABLE_SIZE, "tole"); 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");
88 printf("};\n"); 117 printf("};\n");
89 } 118 }
90 119
91 if (CRC_BE_BITS > 1) { 120 if (CRC_BE_BITS > 1) {
92 crc32init_be(); 121 crc32init_be();
93 printf("static const u32 crc32table_be[4][256] = {"); 122 printf("static const u32 __cacheline_aligned "
94 output_table(crc32table_be, BE_TABLE_SIZE, "tobe"); 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");
127 printf("};\n");
128 }
129 if (CRC_LE_BITS > 1) {
130 crc32cinit_le();
131 printf("static const u32 __cacheline_aligned "
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");
95 printf("};\n"); 136 printf("};\n");
96 } 137 }
97 138