Skip to content

Commit a205daf

Browse files
committed
[Core] Cleanup CRC32 code
1 parent a6fe117 commit a205daf

File tree

1 file changed

+14
-165
lines changed

1 file changed

+14
-165
lines changed

src/core/lib_functions.cpp

Lines changed: 14 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ CurrentTask: Returns the active Task object.
8888
8989
This function returns the @Task object of the active process.
9090
91-
If there is a legitimate circumstance where there is no current task (e.g. if the function is called during
91+
If there is a legitimate circumstance where there is no current task (e.g. if this function is called during
9292
Core initialisation) then the "system task" may be returned, which has ownership of Core resources.
9393
9494
-RESULT-
@@ -147,8 +147,6 @@ uint: Returns the computed 32 bit CRC value for the given data.
147147
148148
*********************************************************************************************************************/
149149

150-
#if 1
151-
152150
static const ULONG crc_table[256] = {
153151
0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,
154152
0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,
@@ -204,182 +202,33 @@ static const ULONG crc_table[256] = {
204202
0x2d02ef8dL
205203
};
206204

207-
#define DO1(buf) crc = crc_table[((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8);
208-
#define DO2(buf) DO1(buf); DO1(buf);
209-
#define DO4(buf) DO2(buf); DO2(buf);
210-
#define DO8(buf) DO4(buf); DO4(buf);
211-
212205
ULONG GenCRC32(ULONG crc, APTR Data, ULONG len)
213206
{
214207
if (!Data) return 0;
215208

216-
BYTE *buf = (BYTE *)Data;
209+
auto buf = (BYTE *)Data;
217210
crc = crc ^ 0xffffffff;
218211
while (len >= 8) {
219-
DO8(buf);
212+
crc = crc_table[((LONG)crc ^ (buf[0])) & 0xff] ^ (crc >> 8);
213+
crc = crc_table[((LONG)crc ^ (buf[1])) & 0xff] ^ (crc >> 8);
214+
crc = crc_table[((LONG)crc ^ (buf[2])) & 0xff] ^ (crc >> 8);
215+
crc = crc_table[((LONG)crc ^ (buf[3])) & 0xff] ^ (crc >> 8);
216+
crc = crc_table[((LONG)crc ^ (buf[4])) & 0xff] ^ (crc >> 8);
217+
crc = crc_table[((LONG)crc ^ (buf[5])) & 0xff] ^ (crc >> 8);
218+
crc = crc_table[((LONG)crc ^ (buf[6])) & 0xff] ^ (crc >> 8);
219+
crc = crc_table[((LONG)crc ^ (buf[7])) & 0xff] ^ (crc >> 8);
220+
buf += 8;
220221
len -= 8;
221222
}
222223

223-
if (len) do {
224-
DO1(buf);
225-
} while (--len);
226-
227-
return crc ^ 0xffffffff;
228-
}
229-
230-
#else
231-
232-
// CRC calculation routine from Zlib, written by Rodney Brown <rbrown64@csc.com.au>
233-
234-
typedef ULONG u4;
235-
typedef int ptrdiff_t;
236-
237-
#define REV(w) (((w)>>24)+(((w)>>8)&0xff00)+(((w)&0xff00)<<8)+(((w)&0xff)<<24))
238-
static ULONG crc32_little(ULONG, const UBYTE *, unsigned);
239-
static ULONG crc32_big(ULONG, const UBYTE *, unsigned);
240-
static volatile int crc_table_empty = 1;
241-
static ULONG crc_table[8][256];
242-
243-
#define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8)
244-
#define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1
245-
#define DOLIT4 c ^= *buf4++; \
246-
c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \
247-
crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24]
248-
#define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4
249-
#define DOBIG4 c ^= *++buf4; \
250-
c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \
251-
crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24]
252-
#define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4
253-
254-
ULONG GenCRC32(ULONG crc, APTR Data, ULONG len)
255-
{
256-
if (!Data) return 0;
257-
258-
UBYTE *buf = Data;
259-
if (crc_table_empty) {
260-
ULONG c;
261-
LONG n, k;
262-
ULONG poly;
263-
// terms of polynomial defining this crc (except x^32):
264-
static const UBYTE p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
265-
266-
// make exclusive-or pattern from polynomial (0xedb88320UL)
267-
268-
poly = 0;
269-
for (n = 0; (size_t)n < sizeof(p)/sizeof(UBYTE); n++) {
270-
poly |= 1 << (31 - p[n]);
271-
}
272-
273-
// generate a crc for every 8-bit value
274-
275-
for (n = 0; n < 256; n++) {
276-
c = (ULONG)n;
277-
for (k = 0; k < 8; k++) {
278-
c = c & 1 ? poly ^ (c >> 1) : c >> 1;
279-
}
280-
crc_table[0][n] = c;
281-
}
282-
283-
// Generate CRC for each value followed by one, two, and three zeros, and then the byte reversal of those as well
284-
// as the first table
285-
286-
for (n = 0; n < 256; n++) {
287-
c = crc_table[0][n];
288-
crc_table[4][n] = REV(c);
289-
for (k = 1; k < 4; k++) {
290-
c = crc_table[0][c & 0xff] ^ (c >> 8);
291-
crc_table[k][n] = c;
292-
crc_table[k + 4][n] = REV(c);
293-
}
294-
}
295-
296-
crc_table_empty = 0;
297-
}
298-
299-
if (sizeof(void *) == sizeof(ptrdiff_t)) {
300-
if constexpr (std::endian::native == std::endian::little) {
301-
return crc32_little(crc, buf, len);
302-
}
303-
else return crc32_big(crc, buf, len);
304-
}
305-
306-
crc = crc ^ 0xffffffff;
307-
while (len >= 8) {
308-
DO8;
309-
len -= 8;
310-
}
311-
312-
if (len) do {
313-
DO1;
314-
} while (--len);
315-
316-
return crc ^ 0xffffffff;
317-
}
318-
319-
static ULONG crc32_little(ULONG crc, const UBYTE *buf, unsigned len)
320-
{
321-
register u4 c;
322-
register const u4 *buf4;
323-
324-
c = (u4)crc;
325-
c = ~c;
326-
while (len && ((ptrdiff_t)buf & 3)) {
327-
c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
224+
while (len > 0) {
225+
crc = crc_table[((LONG)crc ^ *buf++) & 0xff] ^ (crc >> 8);
328226
len--;
329227
}
330228

331-
buf4 = (const u4 *)(const void *)buf;
332-
while (len >= 32) {
333-
DOLIT32;
334-
len -= 32;
335-
}
336-
while (len >= 4) {
337-
DOLIT4;
338-
len -= 4;
339-
}
340-
buf = (const UBYTE *)buf4;
341-
342-
if (len) do {
343-
c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
344-
} while (--len);
345-
c = ~c;
346-
return (ULONG)c;
347-
}
348-
349-
static ULONG crc32_big(ULONG crc, const UBYTE *buf, unsigned len)
350-
{
351-
register u4 c;
352-
register const u4 *buf4;
353-
354-
c = REV((u4)crc);
355-
c = ~c;
356-
while (len && ((ptrdiff_t)buf & 3)) {
357-
c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
358-
len--;
359-
}
360-
361-
buf4 = (const u4 *)(const void *)buf;
362-
buf4--;
363-
while (len >= 32) {
364-
DOBIG32;
365-
len -= 32;
366-
}
367-
while (len >= 4) {
368-
DOBIG4;
369-
len -= 4;
370-
}
371-
buf4++;
372-
buf = (const UBYTE *)buf4;
373-
374-
if (len) do {
375-
c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
376-
} while (--len);
377-
c = ~c;
378-
return (ULONG)(REV(c));
229+
return crc ^ 0xffffffff;
379230
}
380231

381-
#endif
382-
383232
/*********************************************************************************************************************
384233
385234
-FUNCTION-

0 commit comments

Comments
 (0)