This client driver allows you to use a GPIO pin as a source for PPS
[linux-2.6/next.git] / lib / gen_crc32table.c
blobe1303d7c6b51d6e525ac403a4afad378c7887f69
1 #include <stdio.h>
2 #include "crc32defs.h"
3 #include <inttypes.h>
5 #define ENTRIES_PER_LINE 4
7 #if CRC_LE_BITS <= 8
8 #define LE_TABLE_SIZE (1 << CRC_LE_BITS)
9 #else
10 #define LE_TABLE_SIZE 256
11 #endif
13 #if CRC_BE_BITS <= 8
14 #define BE_TABLE_SIZE (1 << CRC_BE_BITS)
15 #else
16 #define BE_TABLE_SIZE 256
17 #endif
19 static uint32_t crc32table_le[8][256];
20 static uint32_t crc32table_be[8][256];
22 /**
23 * crc32init_le() - allocate and initialize LE table data
25 * crc is the crc of the byte i; other entries are filled in based on the
26 * fact that crctable[i^j] = crctable[i] ^ crctable[j].
29 static void crc32init_le(void)
31 unsigned i, j;
32 uint32_t crc = 1;
34 crc32table_le[0][0] = 0;
36 for (i = LE_TABLE_SIZE >> 1; i; i >>= 1) {
37 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
38 for (j = 0; j < LE_TABLE_SIZE; j += 2 * i)
39 crc32table_le[0][i + j] = crc ^ crc32table_le[0][j];
41 for (i = 0; i < LE_TABLE_SIZE; i++) {
42 crc = crc32table_le[0][i];
43 for (j = 1; j < 8; j++) {
44 crc = crc32table_le[0][crc & 0xff] ^ (crc >> 8);
45 crc32table_le[j][i] = crc;
50 /**
51 * crc32init_be() - allocate and initialize BE table data
53 static void crc32init_be(void)
55 unsigned i, j;
56 uint32_t crc = 0x80000000;
58 crc32table_be[0][0] = 0;
60 for (i = 1; i < BE_TABLE_SIZE; i <<= 1) {
61 crc = (crc << 1) ^ ((crc & 0x80000000) ? CRCPOLY_BE : 0);
62 for (j = 0; j < i; j++)
63 crc32table_be[0][i + j] = crc ^ crc32table_be[0][j];
65 for (i = 0; i < BE_TABLE_SIZE; i++) {
66 crc = crc32table_be[0][i];
67 for (j = 1; j < 8; j++) {
68 crc = crc32table_be[0][(crc >> 24) & 0xff] ^ (crc << 8);
69 crc32table_be[j][i] = crc;
74 static void output_table(uint32_t table[8][256], int len, char trans)
76 int i, j;
78 for (j = 0 ; j < 8; j++) {
79 printf("static const u32 t%d_%ce[] = {", j, trans);
80 for (i = 0; i < len - 1; i++) {
81 if ((i % ENTRIES_PER_LINE) == 0)
82 printf("\n");
83 printf("to%ce(0x%8.8xL),", trans, table[j][i]);
84 if ((i % ENTRIES_PER_LINE) != (ENTRIES_PER_LINE - 1))
85 printf(" ");
87 printf("to%ce(0x%8.8xL)};\n\n", trans, table[j][len - 1]);
89 if (trans == 'l') {
90 if ((j+1)*8 >= CRC_LE_BITS)
91 break;
92 } else {
93 if ((j+1)*8 >= CRC_BE_BITS)
94 break;
99 int main(int argc, char** argv)
101 printf("/*\n");
102 printf(" * crc32table.h - CRC32 tables\n");
103 printf(" * this file is generated - do not edit\n");
104 printf(" * # gen_crc32table > crc32table.h\n");
105 printf(" * with\n");
106 printf(" * CRC_LE_BITS = %d\n", CRC_LE_BITS);
107 printf(" * CRC_BE_BITS = %d\n", CRC_BE_BITS);
108 printf(" */\n");
109 printf("\n");
111 if (CRC_LE_BITS > 1) {
112 crc32init_le();
113 output_table(crc32table_le, LE_TABLE_SIZE, 'l');
116 if (CRC_BE_BITS > 1) {
117 crc32init_be();
118 output_table(crc32table_be, BE_TABLE_SIZE, 'b');
121 return 0;