hash/bsdum: convert to object style accumulator
This commit is contained in:
parent
51e7995c63
commit
6338a85992
@ -18,33 +18,61 @@
|
||||
|
||||
#include "../debug.hpp"
|
||||
|
||||
using util::hash::bsdsum;
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
uint16_t
|
||||
util::hash::bsdsum (
|
||||
const uint8_t *const restrict first,
|
||||
const uint8_t *const restrict last
|
||||
) noexcept {
|
||||
CHECK_LE (first, last);
|
||||
|
||||
uint16_t accum = 0;
|
||||
|
||||
for (auto cursor = first; cursor != last; ++cursor) {
|
||||
accum = (accum >> 1u) | ((accum & 0x01u) << 15u);
|
||||
accum += *cursor;
|
||||
}
|
||||
|
||||
return accum;
|
||||
bsdsum::bsdsum ()
|
||||
{
|
||||
reset ();
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
uint16_t
|
||||
util::hash::bsdsum (const void *restrict data, size_t size) noexcept
|
||||
void
|
||||
bsdsum::reset (void)
|
||||
{
|
||||
return bsdsum (
|
||||
static_cast<const uint8_t*> (data),
|
||||
static_cast<const uint8_t*> (data) + size
|
||||
);
|
||||
m_accum = 0;
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
bsdsum::update (const void *restrict data, size_t size) noexcept
|
||||
{
|
||||
auto first = static_cast<const uint8_t *restrict> (data);
|
||||
|
||||
update (first, first + size);
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
bsdsum::update (
|
||||
const uint8_t *const restrict first,
|
||||
const uint8_t *const restrict last) noexcept
|
||||
{
|
||||
CHECK (first);
|
||||
CHECK (last);
|
||||
CHECK_LE (first, last);
|
||||
|
||||
for (auto cursor = first; cursor != last; ++cursor) {
|
||||
m_accum = (m_accum >> 1u) | ((m_accum & 0x01u) << 15u);
|
||||
m_accum += *cursor;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void
|
||||
bsdsum::finish (void)
|
||||
{ ; }
|
||||
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
typename bsdsum::digest_t
|
||||
bsdsum::digest (void) const
|
||||
{
|
||||
return m_accum;
|
||||
}
|
||||
|
@ -23,8 +23,23 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
namespace util { namespace hash {
|
||||
uint16_t bsdsum (const void *restrict data, size_t bytes) noexcept;
|
||||
uint16_t bsdsum (const uint8_t *restrict first, const uint8_t *restrict last) noexcept;
|
||||
class bsdsum {
|
||||
public:
|
||||
using digest_t = uint16_t;
|
||||
|
||||
bsdsum (void);
|
||||
void reset (void);
|
||||
|
||||
void update (const void *restrict data, size_t bytes) noexcept;
|
||||
void update (const uint8_t *restrict first, const uint8_t *restrict last) noexcept;
|
||||
|
||||
void finish (void);
|
||||
|
||||
digest_t digest (void) const;
|
||||
|
||||
private:
|
||||
digest_t m_accum;
|
||||
};
|
||||
} }
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user