Mercurial > crc32
comparison crc32-table.c @ 0:422835bc1aca
*: checkin
| author | Paper <paper@tflc.us> |
|---|---|
| date | Mon, 09 Feb 2026 01:15:00 -0500 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:422835bc1aca |
|---|---|
| 1 #include "crc32i.h" | |
| 2 | |
| 3 /* This builds our CRC table at compile-time rather than runtime. | |
| 4 * Note: We should have a way to change the polynomial at runtime too. */ | |
| 5 | |
| 6 #define CRC32_MASK(crc) (-((crc) & 1)) | |
| 7 | |
| 8 /* Does one iteration of the 8-time loop to generate one byte of the CRC table. */ | |
| 9 #define CRC32_PRECALC_EX(crc) (((crc) >> 1) ^ ((CRC32_POLYNOMIAL) & CRC32_MASK(crc))) | |
| 10 | |
| 11 /* Does all eight iterations of the loop to generate one byte. */ | |
| 12 #define CRC32_PRECALC_E(byte) \ | |
| 13 (CRC32_PRECALC_EX(CRC32_PRECALC_EX(CRC32_PRECALC_EX(CRC32_PRECALC_EX(CRC32_PRECALC_EX(CRC32_PRECALC_EX(CRC32_PRECALC_EX(CRC32_PRECALC_EX(byte))))))))) | |
| 14 | |
| 15 /* Simple wrapper of CRC32_PRECALC_E that converts everything to uint32_t */ | |
| 16 #define CRC32_PRECALC(byte) \ | |
| 17 CRC32_PRECALC_E((uint32_t)(byte)) | |
| 18 | |
| 19 #define CRC32_PRECALC_0(byte) \ | |
| 20 CRC32_PRECALC(byte), CRC32_PRECALC((byte) | 0x01) | |
| 21 | |
| 22 #define CRC32_PRECALC_1(byte) \ | |
| 23 CRC32_PRECALC_0(byte), CRC32_PRECALC_0((byte) | UINT32_C(0x02)) | |
| 24 | |
| 25 #define CRC32_PRECALC_2(byte) \ | |
| 26 CRC32_PRECALC_1(byte), CRC32_PRECALC_1((byte) | UINT32_C(0x04)) | |
| 27 | |
| 28 #define CRC32_PRECALC_3(byte) \ | |
| 29 CRC32_PRECALC_2(byte), CRC32_PRECALC_2((byte) | UINT32_C(0x08)) | |
| 30 | |
| 31 #define CRC32_PRECALC_4(byte) \ | |
| 32 CRC32_PRECALC_3(byte), CRC32_PRECALC_3((byte) | UINT32_C(0x10)) | |
| 33 | |
| 34 #define CRC32_PRECALC_5(byte) \ | |
| 35 CRC32_PRECALC_4(byte), CRC32_PRECALC_4((byte) | UINT32_C(0x20)) | |
| 36 | |
| 37 #define CRC32_PRECALC_6(byte) \ | |
| 38 CRC32_PRECALC_5(byte), CRC32_PRECALC_5((byte) | UINT32_C(0x40)) | |
| 39 | |
| 40 #define CRC32_PRECALC_7(byte) \ | |
| 41 CRC32_PRECALC_6(byte), CRC32_PRECALC_6((byte) | UINT32_C(0x80)) | |
| 42 | |
| 43 const uint32_t crc32_tab[256] = { | |
| 44 CRC32_PRECALC_7(0) | |
| 45 }; | |
| 46 | |
| 47 #undef CRC32_MASK | |
| 48 #undef CRC32_PRECALC_EX | |
| 49 #undef CRC32_PRECALC_E | |
| 50 #undef CRC32_PRECALC | |
| 51 #undef CRC32_PRECALC_0 | |
| 52 #undef CRC32_PRECALC_1 | |
| 53 #undef CRC32_PRECALC_2 | |
| 54 #undef CRC32_PRECALC_3 | |
| 55 #undef CRC32_PRECALC_4 | |
| 56 #undef CRC32_PRECALC_5 | |
| 57 #undef CRC32_PRECALC_6 | |
| 58 #undef CRC32_PRECALC_7 |
