diff --git a/sys/checksum/fletcher16.c b/sys/checksum/fletcher16.c new file mode 100644 index 000000000..6b8a62777 --- /dev/null +++ b/sys/checksum/fletcher16.c @@ -0,0 +1,40 @@ +/* + * Copyright 2015 Eistec AB + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup sys_checksum_fletcher16 + * @{ + * + * @file + * @brief Fletcher16 implementation + * + * @author Joakim Nohlgård + * + * @} + */ + +#include "checksum/fletcher16.h" + +uint16_t fletcher16(const uint8_t *data, size_t bytes) +{ + uint16_t sum1 = 0xff, sum2 = 0xff; + + while (bytes) { + size_t tlen = bytes > 20 ? 20 : bytes; + bytes -= tlen; + do { + sum2 += sum1 += *data++; + } while (--tlen); + sum1 = (sum1 & 0xff) + (sum1 >> 8); + sum2 = (sum2 & 0xff) + (sum2 >> 8); + } + /* Second reduction step to reduce sums to 8 bits */ + sum1 = (sum1 & 0xff) + (sum1 >> 8); + sum2 = (sum2 & 0xff) + (sum2 >> 8); + return (sum2 << 8) | sum1; +} diff --git a/sys/checksum/fletcher32.c b/sys/checksum/fletcher32.c new file mode 100644 index 000000000..1bfb815cf --- /dev/null +++ b/sys/checksum/fletcher32.c @@ -0,0 +1,40 @@ +/* + * Copyright 2015 Eistec AB + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup sys_checksum_fletcher32 + * @{ + * + * @file + * @brief Fletcher32 implementation + * + * @author Joakim Nohlgård + * + * @} + */ + +#include "checksum/fletcher32.h" + +uint32_t fletcher32(const uint16_t *data, size_t words) +{ + uint32_t sum1 = 0xffff, sum2 = 0xffff; + + while (words) { + unsigned tlen = words > 359 ? 359 : words; + words -= tlen; + do { + sum2 += sum1 += *data++; + } while (--tlen); + sum1 = (sum1 & 0xffff) + (sum1 >> 16); + sum2 = (sum2 & 0xffff) + (sum2 >> 16); + } + /* Second reduction step to reduce sums to 16 bits */ + sum1 = (sum1 & 0xffff) + (sum1 >> 16); + sum2 = (sum2 & 0xffff) + (sum2 >> 16); + return (sum2 << 16) | sum1; +} diff --git a/sys/hashes/hashes.c b/sys/hashes/hashes.c index 8992c3915..5086130f6 100644 --- a/sys/hashes/hashes.c +++ b/sys/hashes/hashes.c @@ -12,7 +12,6 @@ * @file * @author Jason Linehan * @author Christian Mehlis - * @author Joakim Nohlgård */ #include "hashes.h" @@ -110,41 +109,3 @@ uint32_t one_at_a_time_hash(const uint8_t *buf, size_t len) hash += hash << 15; return hash; } - -uint16_t fletcher16(const uint8_t *data, size_t bytes) -{ - uint16_t sum1 = 0xff, sum2 = 0xff; - - while (bytes) { - size_t tlen = bytes > 20 ? 20 : bytes; - bytes -= tlen; - do { - sum2 += sum1 += *data++; - } while (--tlen); - sum1 = (sum1 & 0xff) + (sum1 >> 8); - sum2 = (sum2 & 0xff) + (sum2 >> 8); - } - /* Second reduction step to reduce sums to 8 bits */ - sum1 = (sum1 & 0xff) + (sum1 >> 8); - sum2 = (sum2 & 0xff) + (sum2 >> 8); - return (sum2 << 8) | sum1; -} - -uint32_t fletcher32(const uint16_t *data, size_t words) -{ - uint32_t sum1 = 0xffff, sum2 = 0xffff; - - while (words) { - unsigned tlen = words > 359 ? 359 : words; - words -= tlen; - do { - sum2 += sum1 += *data++; - } while (--tlen); - sum1 = (sum1 & 0xffff) + (sum1 >> 16); - sum2 = (sum2 & 0xffff) + (sum2 >> 16); - } - /* Second reduction step to reduce sums to 16 bits */ - sum1 = (sum1 & 0xffff) + (sum1 >> 16); - sum2 = (sum2 & 0xffff) + (sum2 >> 16); - return (sum2 << 16) | sum1; -} diff --git a/sys/include/checksum/fletcher16.h b/sys/include/checksum/fletcher16.h new file mode 100644 index 000000000..234b47db7 --- /dev/null +++ b/sys/include/checksum/fletcher16.h @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2015 Eistec AB + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @defgroup sys_checksum_fletcher16 Fletcher16 + * @ingroup sys_checksum + * + * @brief Fletcher16 checksum algorithm + * @{ + * + * @file + * @author Joakim Nohlgård + */ + +#ifndef FLETCHER16_H +#define FLETCHER16_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Fletcher's 16 bit checksum + * + * found on + * http://en.wikipedia.org/w/index.php?title=Fletcher%27s_checksum&oldid=661273016#Optimizations + * + * @note the returned sum is never 0 + * + * @param buf input buffer to hash + * @param bytes length of buffer, in bytes + * @return 16 bit sized hash in the interval [1..65535] + */ +uint16_t fletcher16(const uint8_t *buf, size_t bytes); + + +#ifdef __cplusplus +} +#endif + +#endif /* FLETCHER16_H */ + +/** @} */ diff --git a/sys/include/checksum/fletcher32.h b/sys/include/checksum/fletcher32.h new file mode 100644 index 000000000..67d000d4f --- /dev/null +++ b/sys/include/checksum/fletcher32.h @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2015 Eistec AB + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @defgroup sys_checksum_fletcher32 Fletcher32 + * @ingroup sys_checksum + * + * @brief Fletcher32 checksum algorithm + * @{ + * + * @file + * @author Joakim Nohlgård + */ + +#ifndef FLETCHER32_H +#define FLETCHER32_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Fletcher's 32 bit checksum + * + * found on + * http://en.wikipedia.org/w/index.php?title=Fletcher%27s_checksum&oldid=661273016#Optimizations + * + * @note the returned sum is never 0 + * @note pay attention to alignment issues since this operates on an input + * buffer containing 16 bit words, not bytes. + * + * @param buf input buffer to hash + * @param words length of buffer, in 16 bit words + * @return 32 bit sized hash in the interval [1..2^32] + */ +uint32_t fletcher32(const uint16_t *buf, size_t words); + +#ifdef __cplusplus +} +#endif + +#endif /* FLETCHER32_H */ + +/** @} */ diff --git a/sys/include/hashes.h b/sys/include/hashes.h index 095013285..d7224ead5 100644 --- a/sys/include/hashes.h +++ b/sys/include/hashes.h @@ -17,7 +17,6 @@ * * @author Jason Linehan * @author Christian Mehlis - * @author Joakim Nohlgård */ #ifndef HASHES_H_ @@ -158,36 +157,6 @@ uint32_t rotating_hash(const uint8_t *buf, size_t len); */ uint32_t one_at_a_time_hash(const uint8_t *buf, size_t len); -/** - * @brief Fletcher's 16 bit checksum - * - * found on - * http://en.wikipedia.org/w/index.php?title=Fletcher%27s_checksum&oldid=661273016#Optimizations - * - * @note the returned sum is never 0 - * - * @param buf input buffer to hash - * @param bytes length of buffer, in bytes - * @return 16 bit sized hash in the interval [1..65535] - */ -uint16_t fletcher16(const uint8_t *buf, size_t bytes); - -/** - * @brief Fletcher's 32 bit checksum - * - * found on - * http://en.wikipedia.org/w/index.php?title=Fletcher%27s_checksum&oldid=661273016#Optimizations - * - * @note the returned sum is never 0 - * @note pay attention to alignment issues since this operates on an input - * buffer containing 16 bit words, not bytes. - * - * @param buf input buffer to hash - * @param words length of buffer, in 16 bit words - * @return 32 bit sized hash in the interval [1..2^32] - */ -uint32_t fletcher32(const uint16_t *buf, size_t words); - #ifdef __cplusplus } #endif