sys/hashes, sys/checksum: mv fletcher* checksum
This commit is contained in:
parent
1fedd456ce
commit
be4ac58e0c
40
sys/checksum/fletcher16.c
Normal file
40
sys/checksum/fletcher16.c
Normal file
@ -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 <joakim.nohlgard@eistec.se>
|
||||
*
|
||||
* @}
|
||||
*/
|
||||
|
||||
#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;
|
||||
}
|
||||
40
sys/checksum/fletcher32.c
Normal file
40
sys/checksum/fletcher32.c
Normal file
@ -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 <joakim.nohlgard@eistec.se>
|
||||
*
|
||||
* @}
|
||||
*/
|
||||
|
||||
#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;
|
||||
}
|
||||
@ -12,7 +12,6 @@
|
||||
* @file
|
||||
* @author Jason Linehan <patientulysses@gmail.com>
|
||||
* @author Christian Mehlis <mehlis@inf.fu-berlin.de>
|
||||
* @author Joakim Nohlgård <joakim.nohlgard@eistec.se>
|
||||
*/
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
51
sys/include/checksum/fletcher16.h
Normal file
51
sys/include/checksum/fletcher16.h
Normal file
@ -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 <joakim.nohlgard@eistec.se>
|
||||
*/
|
||||
|
||||
#ifndef FLETCHER16_H
|
||||
#define FLETCHER16_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#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 */
|
||||
|
||||
/** @} */
|
||||
52
sys/include/checksum/fletcher32.h
Normal file
52
sys/include/checksum/fletcher32.h
Normal file
@ -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 <joakim.nohlgard@eistec.se>
|
||||
*/
|
||||
|
||||
#ifndef FLETCHER32_H
|
||||
#define FLETCHER32_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#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 */
|
||||
|
||||
/** @} */
|
||||
@ -17,7 +17,6 @@
|
||||
*
|
||||
* @author Jason Linehan <patientulysses@gmail.com>
|
||||
* @author Christian Mehlis <mehlis@inf.fu-berlin.de>
|
||||
* @author Joakim Nohlgård <joakim.nohlgard@eistec.se>
|
||||
*/
|
||||
|
||||
#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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user