]> oss.titaniummirror.com Git - tinyos-2.x.git/commitdiff
faster crc implementation
authorandreaskoepke <andreaskoepke>
Mon, 7 May 2007 15:43:59 +0000 (15:43 +0000)
committerandreaskoepke <andreaskoepke>
Mon, 7 May 2007 15:43:59 +0000 (15:43 +0000)
tos/system/crc.h

index 708c567b2b29ca87cb41487d9f2b3014c22b229c..b86ff7ed208a3250d85dcbe2378d6e92b3ae6843 100644 (file)
@@ -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 <koepke@tkn.tu-berlin.de> (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