Skip to content

Commit 346ec9c

Browse files
nmoinvazDead2
authored andcommitted
Merge crc32_little and crc32_big with preprocessor macros for each endian. Removed crc32_generic since it is not being used.
1 parent 42eefd4 commit 346ec9c

File tree

4 files changed

+31
-97
lines changed

4 files changed

+31
-97
lines changed

arch/s390/crc32-vx.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,12 @@ uint32_t Z_INTERNAL s390_crc32_vx(uint32_t crc, const unsigned char *buf, uint64
202202
uint64_t prealign, aligned, remaining;
203203

204204
if (len < VX_MIN_LEN + VX_ALIGN_MASK)
205-
return crc32_big(crc, buf, len);
205+
return crc32_byfour(crc, buf, len);
206206

207207
if ((uintptr_t)buf & VX_ALIGN_MASK) {
208208
prealign = VX_ALIGNMENT - ((uintptr_t)buf & VX_ALIGN_MASK);
209209
len -= prealign;
210-
crc = crc32_big(crc, buf, prealign);
210+
crc = crc32_byfour(crc, buf, prealign);
211211
buf += prealign;
212212
}
213213
aligned = len & ~VX_ALIGN_MASK;
@@ -216,7 +216,7 @@ uint32_t Z_INTERNAL s390_crc32_vx(uint32_t crc, const unsigned char *buf, uint64
216216
crc = crc32_le_vgfm_16(crc ^ 0xffffffff, buf, (size_t)aligned) ^ 0xffffffff;
217217

218218
if (remaining)
219-
crc = crc32_big(crc, buf + aligned, remaining);
219+
crc = crc32_byfour(crc, buf + aligned, remaining);
220220

221221
return crc;
222222
}

crc32.c

Lines changed: 26 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
#include "functable.h"
1717
#include "crc32_tbl.h"
1818

19-
/* =========================================================================
20-
* This function can be used by asm versions of crc32()
21-
*/
19+
/* ========================================================================= */
2220
const uint32_t * Z_EXPORT PREFIX(get_crc_table)(void) {
2321
return (const uint32_t *)crc_table;
2422
}
@@ -35,34 +33,8 @@ uint32_t Z_EXPORT PREFIX(crc32_z)(uint32_t crc, const unsigned char *buf, size_t
3533

3634
return functable.crc32(crc, buf, len);
3735
}
38-
#endif
39-
/* ========================================================================= */
40-
#define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8)
41-
#define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1
42-
#define DO4 DO1; DO1; DO1; DO1
43-
44-
/* ========================================================================= */
45-
Z_INTERNAL uint32_t crc32_generic(uint32_t crc, const unsigned char *buf, uint64_t len) {
46-
crc = crc ^ 0xffffffff;
47-
48-
#ifdef UNROLL_MORE
49-
while (len >= 8) {
50-
DO8;
51-
len -= 8;
52-
}
53-
#else
54-
while (len >= 4) {
55-
DO4;
56-
len -= 4;
57-
}
5836
#endif
5937

60-
if (len) do {
61-
DO1;
62-
} while (--len);
63-
return crc ^ 0xffffffff;
64-
}
65-
6638
#ifdef ZLIB_COMPAT
6739
unsigned long Z_EXPORT PREFIX(crc32)(unsigned long crc, const unsigned char *buf, unsigned int len) {
6840
return (unsigned long)PREFIX(crc32_z)((uint32_t)crc, buf, len);
@@ -73,6 +45,8 @@ uint32_t Z_EXPORT PREFIX(crc32)(uint32_t crc, const unsigned char *buf, uint32_t
7345
}
7446
#endif
7547

48+
/* ========================================================================= */
49+
7650
/*
7751
This BYFOUR code accesses the passed unsigned char * buffer with a 32-bit
7852
integer pointer type. This violates the strict aliasing rule, where a
@@ -87,84 +61,54 @@ uint32_t Z_EXPORT PREFIX(crc32)(uint32_t crc, const unsigned char *buf, uint32_t
8761

8862
/* ========================================================================= */
8963
#if BYTE_ORDER == LITTLE_ENDIAN
90-
#define DOLIT4 c ^= *buf4++; \
91-
c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \
92-
crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24]
93-
#define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4
94-
95-
/* ========================================================================= */
96-
Z_INTERNAL uint32_t crc32_little(uint32_t crc, const unsigned char *buf, uint64_t len) {
97-
Z_REGISTER uint32_t c;
98-
Z_REGISTER const uint32_t *buf4;
99-
100-
c = crc;
101-
c = ~c;
102-
while (len && ((ptrdiff_t)buf & 3)) {
103-
c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
104-
len--;
105-
}
106-
107-
buf4 = (const uint32_t *)(const void *)buf;
108-
109-
#ifdef UNROLL_MORE
110-
while (len >= 32) {
111-
DOLIT32;
112-
len -= 32;
113-
}
64+
#define DOSWAP(crc) (crc)
65+
#define DO1 \
66+
c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8)
67+
#define DO4 c ^= *buf4++; \
68+
c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \
69+
crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24]
70+
#elif BYTE_ORDER == BIG_ENDIAN
71+
#define DOSWAP(crc) ZSWAP32(crc)
72+
#define DO1 \
73+
c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8)
74+
#define DO4 c ^= *buf4++; \
75+
c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \
76+
crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24]
77+
#else
78+
# error "No endian defined"
11479
#endif
115-
116-
while (len >= 4) {
117-
DOLIT4;
118-
len -= 4;
119-
}
120-
buf = (const unsigned char *)buf4;
121-
122-
if (len) do {
123-
c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
124-
} while (--len);
125-
c = ~c;
126-
return c;
127-
}
128-
#endif /* BYTE_ORDER == LITTLE_ENDIAN */
129-
130-
/* ========================================================================= */
131-
#if BYTE_ORDER == BIG_ENDIAN
132-
#define DOBIG4 c ^= *buf4++; \
133-
c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \
134-
crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24]
135-
#define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4
80+
#define DO32 DO4; DO4; DO4; DO4; DO4; DO4; DO4; DO4
13681

13782
/* ========================================================================= */
138-
Z_INTERNAL uint32_t crc32_big(uint32_t crc, const unsigned char *buf, uint64_t len) {
83+
Z_INTERNAL uint32_t crc32_byfour(uint32_t crc, const unsigned char *buf, uint64_t len) {
13984
Z_REGISTER uint32_t c;
14085
Z_REGISTER const uint32_t *buf4;
14186

142-
c = ZSWAP32(crc);
87+
c = DOSWAP(crc);
14388
c = ~c;
14489
while (len && ((ptrdiff_t)buf & 3)) {
145-
c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
90+
DO1;
14691
len--;
14792
}
14893

14994
buf4 = (const uint32_t *)(const void *)buf;
15095

15196
#ifdef UNROLL_MORE
15297
while (len >= 32) {
153-
DOBIG32;
98+
DO32;
15499
len -= 32;
155100
}
156101
#endif
157102

158103
while (len >= 4) {
159-
DOBIG4;
104+
DO4;
160105
len -= 4;
161106
}
162107
buf = (const unsigned char *)buf4;
163108

164109
if (len) do {
165-
c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
110+
DO1;
166111
} while (--len);
167112
c = ~c;
168-
return ZSWAP32(c);
113+
return DOSWAP(c);
169114
}
170-
#endif /* BYTE_ORDER == BIG_ENDIAN */

crc32_p.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ static inline uint32_t gf2_matrix_times(const uint32_t *mat, uint32_t vec) {
1919
}
2020

2121

22-
#if BYTE_ORDER == LITTLE_ENDIAN
23-
extern uint32_t crc32_little(uint32_t, const unsigned char *, uint64_t);
24-
#elif BYTE_ORDER == BIG_ENDIAN
25-
extern uint32_t crc32_big(uint32_t, const unsigned char *, uint64_t);
26-
#endif
22+
extern uint32_t crc32_byfour(uint32_t, const unsigned char *, uint64_t);
2723

2824
#endif /* CRC32_P_H_ */

functable.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -528,13 +528,7 @@ Z_INTERNAL uint32_t crc32_stub(uint32_t crc, const unsigned char *buf, uint64_t
528528
Assert(sizeof(uint64_t) >= sizeof(size_t),
529529
"crc32_z takes size_t but internally we have a uint64_t len");
530530

531-
#if BYTE_ORDER == LITTLE_ENDIAN
532-
functable.crc32 = &crc32_little;
533-
#elif BYTE_ORDER == BIG_ENDIAN
534-
functable.crc32 = &crc32_big;
535-
#else
536-
functable.crc32 = &crc32_generic;
537-
#endif
531+
functable.crc32 = &crc32_byfour;
538532
cpu_check_features();
539533
#ifdef ARM_ACLE_CRC_HASH
540534
if (arm_cpu_has_crc32)

0 commit comments

Comments
 (0)