diff --git a/sys/libkern/crc32.c b/sys/libkern/crc32.c index a6a162b718bf..4c937d29a568 100644 --- a/sys/libkern/crc32.c +++ b/sys/libkern/crc32.c @@ -94,18 +94,21 @@ uint32_t crc32_tab[] = { 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d }; -uint32_t -crc32(const void *buf, size_t size) -{ - const uint8_t *p; - uint32_t crc; - - p = buf; - crc = ~0U; - - while (size--) - crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8); - - return crc ^ ~0U; -} - +/* + * A function that calculates the CRC-32 based on the table above is + * given below for documentation purposes. An equivalent implementation + * of this function that's actually used in the kernel can be found + * in sys/systm.h, where it can be inlined. + * + * uint32_t + * crc32(const void *buf, size_t size) + * { + * const uint8_t *p = buf; + * uint32_t crc; + * + * crc = ~0U; + * while (size--) + * crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8); + * return crc ^ ~0U; + * } + */ diff --git a/sys/sys/systm.h b/sys/sys/systm.h index 0c114b3f7362..64545f7ec766 100644 --- a/sys/sys/systm.h +++ b/sys/sys/systm.h @@ -143,8 +143,28 @@ void panic(const char *, ...) __dead2 __printflike(1, 2); void cpu_boot(int); void cpu_rootconf(void); + extern uint32_t crc32_tab[]; -uint32_t crc32(const void *buf, size_t size); + +static __inline uint32_t +crc32_raw(const void *buf, size_t size, uint32_t crc) +{ + const uint8_t *p = buf; + + while (size--) + crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8); + return (crc); +} + +static __inline uint32_t +crc32(const void *buf, size_t size) +{ + uint32_t crc; + + crc = crc32_raw(buf, size, ~0U); + return (crc ^ ~0U); +} + void critical_enter(void); void critical_exit(void); void init_param1(void);