|
|
|
|
|
by jart
601 days ago
|
|
If you don't want a hard coded table, you could always do this. unsigned kCastagnoli[256];
void InitializeCrc32(unsigned table[256], unsigned polynomial) {
unsigned d, i, r;
for (d = 0; d < 256; ++d) {
r = d;
for (i = 0; i < 8; ++i)
r = r >> 1 ^ (r & 1 ? polynomial : 0);
table[d] = r;
}
}
unsigned Castagnoli(unsigned h, unsigned long w, long n) {
long i;
static int once;
if (!once) {
InitializeCrc32(kCastagnoli, 0x82f63b78);
once = 1;
}
for (i = 0; i < n; ++i) {
h = h >> 8 ^ kCastagnoli[(h & 255) ^ (w & 255)];
w >>= 8;
}
return h;
}
That does the same thing as the crc32 instructions in x86. |
|
This version has the caveat that it is technically not thread safe.