From a4bdb5e0c915c65d71908a6ed73e2313166cf6fb Mon Sep 17 00:00:00 2001 From: andreaskoepke Date: Mon, 7 May 2007 15:43:59 +0000 Subject: [PATCH 1/1] faster crc implementation --- tos/system/crc.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) 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 -- 2.39.2