Skip to content

Commit

Permalink
approach 1
Browse files Browse the repository at this point in the history
  • Loading branch information
grisumbras committed Jun 15, 2023
1 parent 8957955 commit 9d848ed
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions include/boost/json/detail/digest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
#ifndef BOOST_JSON_DETAIL_DIGEST_HPP
#define BOOST_JSON_DETAIL_DIGEST_HPP

#include <algorithm>
#include <array>
#include <cstring>

namespace boost {
namespace json {
namespace detail {
Expand All @@ -23,15 +27,41 @@ digest(
std::size_t salt) noexcept
{
#if BOOST_JSON_ARCH == 64
std::uint64_t const prime = 0x100000001B3ULL;
std::uint64_t hash = 0xcbf29ce484222325ULL;
using hash_t = std::uint64_t;
hash_t const prime = 0x100000001B3ULL;
hash_t hash = 0xcbf29ce484222325ULL;
#else
std::uint32_t const prime = 0x01000193UL;
std::uint32_t hash = 0x811C9DC5UL;
using hash_t = std::uint32_t;
hash_t const prime = 0x01000193UL;
hash_t hash = 0x811C9DC5UL;
#endif
hash += salt;
for(; b != e; ++b)
hash = (*b ^ hash) * prime;

constexpr std::size_t step = sizeof(hash_t);
std::size_t n = std::distance(b, e);
std::size_t const m = n % step;

char temp[step];
while( n > m )
{
std::copy_n(b, step, temp);

hash_t batch;
std::memcpy(&batch, temp, step);
hash = (batch ^ hash) * prime;

std::advance(b, step);
n -= step;
}

std::memset(temp, 0, step);
std::copy_n(b, n, temp);
BOOST_ASSERT( std::next(b, n) == e );

hash_t batch;
std::memcpy(&batch, temp, step);
hash = (batch ^ hash) * prime;

return hash;
}

Expand Down

0 comments on commit 9d848ed

Please sign in to comment.