16
16
#include "functable.h"
17
17
#include "crc32_tbl.h"
18
18
19
- /* =========================================================================
20
- * This function can be used by asm versions of crc32()
21
- */
19
+ /* ========================================================================= */
22
20
const uint32_t * Z_EXPORT PREFIX (get_crc_table )(void ) {
23
21
return (const uint32_t * )crc_table ;
24
22
}
@@ -35,34 +33,8 @@ uint32_t Z_EXPORT PREFIX(crc32_z)(uint32_t crc, const unsigned char *buf, size_t
35
33
36
34
return functable .crc32 (crc , buf , len );
37
35
}
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
- }
58
36
#endif
59
37
60
- if (len ) do {
61
- DO1 ;
62
- } while (-- len );
63
- return crc ^ 0xffffffff ;
64
- }
65
-
66
38
#ifdef ZLIB_COMPAT
67
39
unsigned long Z_EXPORT PREFIX (crc32 )(unsigned long crc , const unsigned char * buf , unsigned int len ) {
68
40
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
73
45
}
74
46
#endif
75
47
48
+ /* ========================================================================= */
49
+
76
50
/*
77
51
This BYFOUR code accesses the passed unsigned char * buffer with a 32-bit
78
52
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
87
61
88
62
/* ========================================================================= */
89
63
#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"
114
79
#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
136
81
137
82
/* ========================================================================= */
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 ) {
139
84
Z_REGISTER uint32_t c ;
140
85
Z_REGISTER const uint32_t * buf4 ;
141
86
142
- c = ZSWAP32 (crc );
87
+ c = DOSWAP (crc );
143
88
c = ~c ;
144
89
while (len && ((ptrdiff_t )buf & 3 )) {
145
- c = crc_table [ 4 ][( c >> 24 ) ^ * buf ++ ] ^ ( c << 8 ) ;
90
+ DO1 ;
146
91
len -- ;
147
92
}
148
93
149
94
buf4 = (const uint32_t * )(const void * )buf ;
150
95
151
96
#ifdef UNROLL_MORE
152
97
while (len >= 32 ) {
153
- DOBIG32 ;
98
+ DO32 ;
154
99
len -= 32 ;
155
100
}
156
101
#endif
157
102
158
103
while (len >= 4 ) {
159
- DOBIG4 ;
104
+ DO4 ;
160
105
len -= 4 ;
161
106
}
162
107
buf = (const unsigned char * )buf4 ;
163
108
164
109
if (len ) do {
165
- c = crc_table [ 4 ][( c >> 24 ) ^ * buf ++ ] ^ ( c << 8 ) ;
110
+ DO1 ;
166
111
} while (-- len );
167
112
c = ~c ;
168
- return ZSWAP32 (c );
113
+ return DOSWAP (c );
169
114
}
170
- #endif /* BYTE_ORDER == BIG_ENDIAN */
0 commit comments