hash/bsdum: convert to object style accumulator

This commit is contained in:
Danny Robson 2016-06-20 16:38:57 +10:00
parent 51e7995c63
commit 6338a85992
2 changed files with 66 additions and 23 deletions

View File

@ -18,33 +18,61 @@
#include "../debug.hpp" #include "../debug.hpp"
using util::hash::bsdsum;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
uint16_t bsdsum::bsdsum ()
util::hash::bsdsum ( {
const uint8_t *const restrict first, reset ();
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;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
uint16_t void
util::hash::bsdsum (const void *restrict data, size_t size) noexcept bsdsum::reset (void)
{ {
return bsdsum ( m_accum = 0;
static_cast<const uint8_t*> (data),
static_cast<const uint8_t*> (data) + size
);
} }
///////////////////////////////////////////////////////////////////////////////
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;
}

View File

@ -23,8 +23,23 @@
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
namespace util { namespace hash { namespace util { namespace hash {
uint16_t bsdsum (const void *restrict data, size_t bytes) noexcept; class bsdsum {
uint16_t bsdsum (const uint8_t *restrict first, const uint8_t *restrict last) noexcept; 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 #endif