hash/adler: convert to object style accumulator

This commit is contained in:
Danny Robson 2016-06-20 16:37:43 +10:00
parent f02ea00f0a
commit 51e7995c63
5 changed files with 150 additions and 41 deletions

View File

@ -115,6 +115,7 @@ UTIL_FILES = \
hash/crc.hpp \ hash/crc.hpp \
hash/fasthash.cpp \ hash/fasthash.cpp \
hash/fasthash.hpp \ hash/fasthash.hpp \
hash/fletcher.cpp \
hash/fletcher.hpp \ hash/fletcher.hpp \
hash/fnv1a.cpp \ hash/fnv1a.cpp \
hash/fnv1a.hpp \ hash/fnv1a.hpp \

View File

@ -19,23 +19,31 @@
#include "./fletcher.hpp" #include "./fletcher.hpp"
#include "../debug.hpp" #include "../debug.hpp"
static const unsigned MODULUS = 65521; static constexpr unsigned MODULUS = 65521;
using util::hash::adler32;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
uint32_t adler32::adler32 ():
util::hash::adler32 (const void* restrict _data, size_t _size) noexcept fletcher (MODULUS, 1, 0)
{ { ; }
return adler32 (
static_cast<const uint8_t*> (_data),
static_cast<const uint8_t*> (_data) + _size
);
}
//----------------------------------------------------------------------------- /////////////////////////////////////////////////////////////////////////////////
uint32_t //uint32_t
util::hash::adler32 (const uint8_t *restrict first, const uint8_t *restrict last) noexcept //util::hash::adler32 (const void* restrict _data, size_t _size) noexcept
{ //{
return fletcher<32, MODULUS, 1, 0> (first, last - first); // return adler32 (
} // static_cast<const uint8_t*> (_data),
// static_cast<const uint8_t*> (_data) + _size
// );
//}
//
//
////-----------------------------------------------------------------------------
//uint32_t
//util::hash::adler32 (const uint8_t *restrict first, const uint8_t *restrict last) noexcept
//{
// return fletcher<32, MODULUS, 1, 0> (first, last - first);
//}

View File

@ -14,8 +14,10 @@
* Copyright 2010-2014 Danny Robson <danny@nerdcruft.net> * Copyright 2010-2014 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef __UTIL_ADLER_HPP #ifndef __UTIL_HASH_ADLER_HPP
#define __UTIL_ADLER_HPP #define __UTIL_HASH_ADLER_HPP
#include "./fletcher.hpp"
#include <cstdint> #include <cstdint>
#include <cstdlib> #include <cstdlib>
@ -23,8 +25,10 @@
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
namespace util { namespace hash { namespace util { namespace hash {
uint32_t adler32 (const void* restrict, size_t) noexcept; class adler32 : public fletcher<uint32_t> {
uint32_t adler32 (const uint8_t *restrict first, const uint8_t *restrict last) noexcept; public:
adler32 ();
};
} } } }
#endif #endif

89
hash/fletcher.cpp Normal file
View File

@ -0,0 +1,89 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2010 Danny Robson <danny@nerdcruft.net>
*/
#include "./fletcher.hpp"
#include "../debug.hpp"
using util::hash::fletcher;
///////////////////////////////////////////////////////////////////////////////
template <typename T>
fletcher<T>::fletcher (part_t _modulus, part_t _a, part_t _b):
m_modulus { _modulus },
m_initial { _a, _b },
m_state { m_initial }
{ ; }
//-----------------------------------------------------------------------------
template <typename T>
void
fletcher<T>::reset (void)
{
m_state = m_initial;
}
///////////////////////////////////////////////////////////////////////////////
template <typename T>
void
fletcher<T>::update (const void *restrict data, size_t size) noexcept
{
CHECK (data);
auto first = static_cast<const uint8_t *restrict> (data);
update (first, first + size);
}
//-----------------------------------------------------------------------------
template <typename T>
void
fletcher<T>::update (const uint8_t *restrict first, const uint8_t *restrict last) noexcept
{
CHECK (first);
CHECK (last);
CHECK_LE (first, last);
for (auto cursor = first; cursor < last; ++cursor) {
m_state.a = (m_state.a + *cursor ) % m_modulus;
m_state.b = (m_state.a + m_state.b) % m_modulus;
}
}
//-----------------------------------------------------------------------------
template <typename T>
void
fletcher<T>::finish (void)
{ ; }
///////////////////////////////////////////////////////////////////////////////
template <typename T>
typename fletcher<T>::digest_t
fletcher<T>::digest (void) const
{
return (m_state.b << (sizeof (part_t) * 8)) + m_state.a;
}
///////////////////////////////////////////////////////////////////////////////
template class util::hash::fletcher<uint32_t>;

View File

@ -11,11 +11,11 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* Copyright 2010 Danny Robson <danny@nerdcruft.net> * Copyright 2010-2016 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef __UTIL_FLETCHER_HPP #ifndef __UTIL_HASH_FLETCHER_HPP
#define __UTIL_FLETCHER_HPP #define __UTIL_HASH_FLETCHER_HPP
#include "../types/bits.hpp" #include "../types/bits.hpp"
@ -24,28 +24,35 @@
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
template < namespace util { namespace hash {
unsigned OUTPUT, template <typename DIGEST>
unsigned MODULUS, class fletcher {
unsigned INITIAL_A, public:
unsigned INITIAL_B using digest_t = DIGEST;
> using part_t = typename bytes_type<sizeof (digest_t) / 2>::uint;
typename bits_type<OUTPUT>::uint
fletcher (const void *restrict _data,
size_t size)
{
typedef typename bits_type<OUTPUT / 2>::uint temp_t;
auto data = static_cast<const uint8_t*> (_data); fletcher (part_t modulus, part_t a, part_t b);
temp_t A = INITIAL_A, B = INITIAL_B;
for (size_t i = 0; i < size; ++i) { void update (const void *restrict, size_t) noexcept;
A = (A + data[i]) % MODULUS; void update (const uint8_t *restrict first, const uint8_t *restrict last) noexcept;
B = (A + B) % MODULUS;
} void finish (void);
digest_t digest (void) const;
void reset (void);
private:
const digest_t m_modulus;
struct state_t {
part_t a, b;
};
const state_t m_initial;
state_t m_state;
};
} }
return (B << (OUTPUT / 2u)) + A;
}
#endif #endif