Skip to content

Commit 116a49d

Browse files
committed
Optimize software crc32c on 32bit machine
1 parent 57d5056 commit 116a49d

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/crc32c.c

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -400,14 +400,14 @@ JL_DLLEXPORT uint32_t jl_crc32c(uint32_t crc, const char *buf, size_t len)
400400
regardless of the endianness of the machine this is running on. */
401401
JL_DLLEXPORT uint32_t jl_crc32c_sw(uint32_t crci, const char *buf, size_t len)
402402
{
403-
const unsigned char *next = (const unsigned char *) buf;
404-
uint64_t crc = crci ^ 0xffffffff;
405-
while (len && ((uintptr_t)next & 7) != 0) {
406-
crc = crc32c_table[0][(crc ^ *next++) & 0xff] ^ (crc >> 8);
403+
uintptr_t crc = crci ^ 0xffffffff;
404+
while (len && ((uintptr_t)buf & 7) != 0) {
405+
crc = crc32c_table[0][(crc ^ *buf++) & 0xff] ^ (crc >> 8);
407406
len--;
408407
}
409408
while (len >= 8) {
410-
crc ^= *(uint64_t*)next;
409+
#ifdef _P64
410+
crc ^= *(uint64_t*)buf;
411411
crc = crc32c_table[7][crc & 0xff] ^
412412
crc32c_table[6][(crc >> 8) & 0xff] ^
413413
crc32c_table[5][(crc >> 16) & 0xff] ^
@@ -416,11 +416,24 @@ JL_DLLEXPORT uint32_t jl_crc32c_sw(uint32_t crci, const char *buf, size_t len)
416416
crc32c_table[2][(crc >> 40) & 0xff] ^
417417
crc32c_table[1][(crc >> 48) & 0xff] ^
418418
crc32c_table[0][crc >> 56];
419-
next += 8;
419+
#else
420+
uint32_t *p = (uint32_t*)buf;
421+
crc ^= p[0];
422+
uint32_t hi = p[1];
423+
crc = crc32c_table[7][crc & 0xff] ^
424+
crc32c_table[6][(crc >> 8) & 0xff] ^
425+
crc32c_table[5][(crc >> 16) & 0xff] ^
426+
crc32c_table[4][(crc >> 24) & 0xff] ^
427+
crc32c_table[3][hi & 0xff] ^
428+
crc32c_table[2][(hi >> 8) & 0xff] ^
429+
crc32c_table[1][(hi >> 16) & 0xff] ^
430+
crc32c_table[0][hi >> 24];
431+
#endif
432+
buf += 8;
420433
len -= 8;
421434
}
422435
while (len) {
423-
crc = crc32c_table[0][(crc ^ *next++) & 0xff] ^ (crc >> 8);
436+
crc = crc32c_table[0][(crc ^ *buf++) & 0xff] ^ (crc >> 8);
424437
len--;
425438
}
426439
return (uint32_t)crc ^ 0xffffffff;

0 commit comments

Comments
 (0)