Added Adler32 and Crc32 hash functions.

This commit is contained in:
Branimir Karadžić
2017-09-16 20:55:22 -07:00
parent 5ba4d9f6d9
commit 5cf40c175d
5 changed files with 321 additions and 0 deletions

View File

@@ -62,6 +62,57 @@ namespace bx
///
uint32_t hashMurmur2A(const char* _data);
///
class HashAdler32
{
public:
///
void begin();
///
void add(const void* _data, int _len);
///
template<typename Ty>
void add(Ty _value);
///
uint32_t end();
private:
uint32_t m_a;
uint32_t m_b;
};
///
class HashCrc32
{
public:
enum Enum
{
Ieee, //!< 0xedb88320
Castagnoli, //!< 0x82f63b78
Koopman, //!< 0xeb31d82e
};
///
void begin(Enum _type = Ieee);
///
void add(const void* _data, int _len);
///
template<typename Ty>
void add(Ty _value);
///
uint32_t end();
private:
const uint32_t* m_table;
uint32_t m_hash;
};
} // namespace bx
#include "inline/hash.inl"

View File

@@ -161,4 +161,44 @@ namespace bx
return hashMurmur2A(StringView(_data) );
}
inline void HashAdler32::begin()
{
m_a = 1;
m_b = 0;
}
inline void HashAdler32::add(const void* _data, int _len)
{
const uint32_t kModAdler = 65521;
const uint8_t* data = (const uint8_t*)_data;
for (; _len != 0; --_len)
{
m_a = (m_a + *data++) % kModAdler;
m_b = (m_b + m_a ) % kModAdler;
}
}
template<typename Ty>
inline void HashAdler32::add(Ty _value)
{
add(&_value, sizeof(Ty) );
}
inline uint32_t HashAdler32::end()
{
return m_a | (m_b<<16);
}
template<typename Ty>
inline void HashCrc32::add(Ty _value)
{
add(&_value, sizeof(Ty) );
}
inline uint32_t HashCrc32::end()
{
m_hash ^= UINT32_MAX;
return m_hash;
}
} // namespace bx