]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - tos/system/crc.h
Remove bogus 'tab:4' and 'tab:2' markers.
[tinyos-2.x.git] / tos / system / crc.h
index 708c567b2b29ca87cb41487d9f2b3014c22b229c..676fdcc62b335a681aa2c09040f0cab4f9e7a991 100644 (file)
@@ -1,6 +1,6 @@
 // $Id$
 
-/*                                                                     tab:4
+/*
  * "Copyright (c) 2000-2003 The Regents of the University  of California.  
  * All rights reserved.
  *
@@ -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