From: andreaskoepke Date: Mon, 7 May 2007 15:43:59 +0000 (+0000) Subject: faster crc implementation X-Git-Tag: release_tools_1_2_4_1~192 X-Git-Url: https://oss.titaniummirror.com/gitweb/?p=tinyos-2.x.git;a=commitdiff_plain;h=a4bdb5e0c915c65d71908a6ed73e2313166cf6fb faster crc implementation --- diff --git a/tos/system/crc.h b/tos/system/crc.h index 708c567b..b86ff7ed 100644 --- a/tos/system/crc.h +++ b/tos/system/crc.h @@ -48,7 +48,10 @@ uint16_t crcByte(uint16_t oldCrc, uint8_t byte); * @param b Byte to "add" to the CRC * @return New CRC value * + * To understand how the CRC works and how it relates to the polynomial, read through this + * loop based implementation. */ +/* uint16_t crcByte(uint16_t crc, uint8_t b) { uint8_t i; @@ -64,6 +67,24 @@ uint16_t crcByte(uint16_t crc, uint8_t b) return crc; } +*/ +/** + * The following implementation computes the same polynomial. It should be + * (much) faster on any processor architecture, as it does not involve + * loops. Unfortunately, I can not yet give a reference to a derivation. + * + * @author Andreas Koepke (porting to tinyos) + * @author Paul Curtis (pointed out this implementation on the MSP430 yahoo mailing list) + */ + +uint16_t crcByte(uint16_t crc, uint8_t b) { + crc = (uint8_t)(crc >> 8) | (crc << 8); + crc ^= b; + crc ^= (uint8_t)(crc & 0xff) >> 4; + crc ^= crc << 12; + crc ^= (crc & 0xff) << 5; + return crc; +} #endif #endif