Skip to content

Commit 60e58d5

Browse files
rpears0ntorvalds
authored andcommittedMar 23, 2012
crc32: miscellaneous cleanups
Misc cleanup of lib/crc32.c and related files. - remove unnecessary header files. - straighten out some convoluted ifdef's - rewrite some references to 2 dimensional arrays as 1 dimensional arrays to make them correct. I.e. replace tab[i] with tab[0][i]. - a few trivial whitespace changes - fix a warning in gen_crc32tables.c caused by a mismatch in the type of the pointer passed to output table. Since the table is only used at kernel compile time, it is simpler to make the table big enough to hold the largest column size used. One cannot make the column size smaller in output_table because it has to be used by both the le and be tables and they can have different column sizes. [[email protected]: Minor changelog tweaks] Signed-off-by: Bob Pearson <[email protected]> Signed-off-by: Darrick J. Wong <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 3863ef3 commit 60e58d5

File tree

2 files changed

+39
-71
lines changed

2 files changed

+39
-71
lines changed
 

‎lib/crc32.c

+36-68
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,10 @@
2323
/* see: Documentation/crc32.txt for a description of algorithms */
2424

2525
#include <linux/crc32.h>
26-
#include <linux/kernel.h>
2726
#include <linux/module.h>
28-
#include <linux/compiler.h>
2927
#include <linux/types.h>
30-
#include <linux/init.h>
31-
#include <linux/atomic.h>
3228
#include "crc32defs.h"
29+
3330
#if CRC_LE_BITS == 8
3431
# define tole(x) __constant_cpu_to_le32(x)
3532
#else
@@ -41,6 +38,7 @@
4138
#else
4239
# define tobe(x) (x)
4340
#endif
41+
4442
#include "crc32table.h"
4543

4644
MODULE_AUTHOR("Matt Domsch <Matt_Domsch@dell.com>");
@@ -96,60 +94,47 @@ crc32_body(u32 crc, unsigned char const *buf, size_t len, const u32 (*tab)[256])
9694
#undef DO_CRC4
9795
}
9896
#endif
97+
9998
/**
10099
* crc32_le() - Calculate bitwise little-endian Ethernet AUTODIN II CRC32
101100
* @crc: seed value for computation. ~0 for Ethernet, sometimes 0 for
102101
* other uses, or the previous crc32 value if computing incrementally.
103102
* @p: pointer to buffer over which CRC is run
104103
* @len: length of buffer @p
105104
*/
106-
u32 __pure crc32_le(u32 crc, unsigned char const *p, size_t len);
107-
108-
#if CRC_LE_BITS == 1
109-
/*
110-
* In fact, the table-based code will work in this case, but it can be
111-
* simplified by inlining the table in ?: form.
112-
*/
113-
114105
u32 __pure crc32_le(u32 crc, unsigned char const *p, size_t len)
115106
{
107+
#if CRC_LE_BITS == 1
116108
int i;
117109
while (len--) {
118110
crc ^= *p++;
119111
for (i = 0; i < 8; i++)
120112
crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
121113
}
122-
return crc;
123-
}
124-
#else /* Table-based approach */
125-
126-
u32 __pure crc32_le(u32 crc, unsigned char const *p, size_t len)
127-
{
128-
# if CRC_LE_BITS == 8
129-
const u32 (*tab)[] = crc32table_le;
130-
131-
crc = __cpu_to_le32(crc);
132-
crc = crc32_body(crc, p, len, tab);
133-
return __le32_to_cpu(crc);
134-
# elif CRC_LE_BITS == 4
114+
# elif CRC_LE_BITS == 2
135115
while (len--) {
136116
crc ^= *p++;
137-
crc = (crc >> 4) ^ crc32table_le[crc & 15];
138-
crc = (crc >> 4) ^ crc32table_le[crc & 15];
117+
crc = (crc >> 2) ^ crc32table_le[0][crc & 3];
118+
crc = (crc >> 2) ^ crc32table_le[0][crc & 3];
119+
crc = (crc >> 2) ^ crc32table_le[0][crc & 3];
120+
crc = (crc >> 2) ^ crc32table_le[0][crc & 3];
139121
}
140-
return crc;
141-
# elif CRC_LE_BITS == 2
122+
# elif CRC_LE_BITS == 4
142123
while (len--) {
143124
crc ^= *p++;
144-
crc = (crc >> 2) ^ crc32table_le[crc & 3];
145-
crc = (crc >> 2) ^ crc32table_le[crc & 3];
146-
crc = (crc >> 2) ^ crc32table_le[crc & 3];
147-
crc = (crc >> 2) ^ crc32table_le[crc & 3];
125+
crc = (crc >> 4) ^ crc32table_le[0][crc & 15];
126+
crc = (crc >> 4) ^ crc32table_le[0][crc & 15];
148127
}
128+
# elif CRC_LE_BITS == 8
129+
const u32 (*tab)[] = crc32table_le;
130+
131+
crc = __cpu_to_le32(crc);
132+
crc = crc32_body(crc, p, len, tab);
133+
crc = __le32_to_cpu(crc);
134+
#endif
149135
return crc;
150-
# endif
151136
}
152-
#endif
137+
EXPORT_SYMBOL(crc32_le);
153138

154139
/**
155140
* crc32_be() - Calculate bitwise big-endian Ethernet AUTODIN II CRC32
@@ -158,16 +143,9 @@ u32 __pure crc32_le(u32 crc, unsigned char const *p, size_t len)
158143
* @p: pointer to buffer over which CRC is run
159144
* @len: length of buffer @p
160145
*/
161-
u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len);
162-
163-
#if CRC_BE_BITS == 1
164-
/*
165-
* In fact, the table-based code will work in this case, but it can be
166-
* simplified by inlining the table in ?: form.
167-
*/
168-
169146
u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len)
170147
{
148+
#if CRC_BE_BITS == 1
171149
int i;
172150
while (len--) {
173151
crc ^= *p++ << 24;
@@ -176,39 +154,29 @@ u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len)
176154
(crc << 1) ^ ((crc & 0x80000000) ? CRCPOLY_BE :
177155
0);
178156
}
179-
return crc;
180-
}
181-
182-
#else /* Table-based approach */
183-
u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len)
184-
{
185-
# if CRC_BE_BITS == 8
186-
const u32 (*tab)[] = crc32table_be;
187-
188-
crc = __cpu_to_be32(crc);
189-
crc = crc32_body(crc, p, len, tab);
190-
return __be32_to_cpu(crc);
191-
# elif CRC_BE_BITS == 4
157+
# elif CRC_BE_BITS == 2
192158
while (len--) {
193159
crc ^= *p++ << 24;
194-
crc = (crc << 4) ^ crc32table_be[crc >> 28];
195-
crc = (crc << 4) ^ crc32table_be[crc >> 28];
160+
crc = (crc << 2) ^ crc32table_be[0][crc >> 30];
161+
crc = (crc << 2) ^ crc32table_be[0][crc >> 30];
162+
crc = (crc << 2) ^ crc32table_be[0][crc >> 30];
163+
crc = (crc << 2) ^ crc32table_be[0][crc >> 30];
196164
}
197-
return crc;
198-
# elif CRC_BE_BITS == 2
165+
# elif CRC_BE_BITS == 4
199166
while (len--) {
200167
crc ^= *p++ << 24;
201-
crc = (crc << 2) ^ crc32table_be[crc >> 30];
202-
crc = (crc << 2) ^ crc32table_be[crc >> 30];
203-
crc = (crc << 2) ^ crc32table_be[crc >> 30];
204-
crc = (crc << 2) ^ crc32table_be[crc >> 30];
168+
crc = (crc << 4) ^ crc32table_be[0][crc >> 28];
169+
crc = (crc << 4) ^ crc32table_be[0][crc >> 28];
205170
}
206-
return crc;
171+
# elif CRC_BE_BITS == 8
172+
const u32 (*tab)[] = crc32table_be;
173+
174+
crc = __cpu_to_be32(crc);
175+
crc = crc32_body(crc, p, len, tab);
176+
crc = __be32_to_cpu(crc);
207177
# endif
178+
return crc;
208179
}
209-
#endif
210-
211-
EXPORT_SYMBOL(crc32_le);
212180
EXPORT_SYMBOL(crc32_be);
213181

214182
#ifdef CONFIG_CRC32_SELFTEST

‎lib/gen_crc32table.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
#define LE_TABLE_SIZE (1 << CRC_LE_BITS)
88
#define BE_TABLE_SIZE (1 << CRC_BE_BITS)
99

10-
static uint32_t crc32table_le[4][LE_TABLE_SIZE];
11-
static uint32_t crc32table_be[4][BE_TABLE_SIZE];
10+
static uint32_t crc32table_le[4][256];
11+
static uint32_t crc32table_be[4][256];
1212

1313
/**
1414
* crc32init_le() - allocate and initialize LE table data
@@ -62,7 +62,7 @@ static void crc32init_be(void)
6262
}
6363
}
6464

65-
static void output_table(uint32_t table[4][256], int len, char *trans)
65+
static void output_table(uint32_t (*table)[256], int len, char *trans)
6666
{
6767
int i, j;
6868

0 commit comments

Comments
 (0)