|
10 | 10 | #ifndef BOOST_JSON_DETAIL_DIGEST_HPP |
11 | 11 | #define BOOST_JSON_DETAIL_DIGEST_HPP |
12 | 12 |
|
| 13 | +#include <boost/json/detail/config.hpp> |
| 14 | + |
| 15 | +#include <algorithm> |
| 16 | +#include <iterator> |
| 17 | + |
13 | 18 | namespace boost { |
14 | 19 | namespace json { |
15 | 20 | namespace detail { |
16 | 21 |
|
17 | 22 | // Calculate salted digest of string |
18 | 23 | template<class ForwardIterator> |
19 | 24 | std::size_t |
20 | | -digest( |
21 | | - ForwardIterator b, |
22 | | - ForwardIterator e, |
23 | | - std::size_t salt) noexcept |
| 25 | +digest(ForwardIterator b, ForwardIterator e, std::size_t salt) noexcept |
24 | 26 | { |
| 27 | + std::size_t const len = std::distance(b, e); |
| 28 | + |
25 | 29 | #if BOOST_JSON_ARCH == 64 |
26 | | - std::uint64_t const prime = 0x100000001B3ULL; |
27 | | - std::uint64_t hash = 0xcbf29ce484222325ULL; |
| 30 | + |
| 31 | + using state_type = std::uint64_t; |
| 32 | + state_type const m = 0xc6a4a7935bd1e995ULL; |
| 33 | + int const r = 47; |
| 34 | + state_type hash = salt ^ (len * m); |
| 35 | + |
| 36 | + constexpr std::size_t N = sizeof(state_type); |
| 37 | + e = std::next( b, len & ~std::size_t(N-1) ); |
| 38 | + for( ; b != e; std::advance(b, N) ) |
| 39 | + { |
| 40 | + state_type num; |
| 41 | + std::copy_n( b, N, reinterpret_cast<unsigned char*>(&num) ); |
| 42 | + |
| 43 | + num *= m; |
| 44 | + num ^= num >> r; |
| 45 | + num *= m; |
| 46 | + hash ^= num; |
| 47 | + hash *= m; |
| 48 | + } |
| 49 | + |
| 50 | + switch( len & (N - 1) ) |
| 51 | + { |
| 52 | + case 7: hash ^= state_type( *std::next(b, 6) ) << 48; // fall through |
| 53 | + case 6: hash ^= state_type( *std::next(b, 5) ) << 40; // fall through |
| 54 | + case 5: hash ^= state_type( *std::next(b, 4) ) << 32; // fall through |
| 55 | + case 4: hash ^= state_type( *std::next(b, 3) ) << 24; // fall through |
| 56 | + case 3: hash ^= state_type( *std::next(b, 2) ) << 16; // fall through |
| 57 | + case 2: hash ^= state_type( *std::next(b, 1) ) << 8; // fall through |
| 58 | + case 1: hash ^= state_type( *std::next(b, 0) ); |
| 59 | + hash *= m; |
| 60 | + }; |
| 61 | + |
| 62 | + hash ^= hash >> r; |
| 63 | + hash *= m; |
| 64 | + hash ^= hash >> r; |
| 65 | + |
28 | 66 | #else |
29 | | - std::uint32_t const prime = 0x01000193UL; |
30 | | - std::uint32_t hash = 0x811C9DC5UL; |
| 67 | + |
| 68 | + using state_type = std::uint32_t; |
| 69 | + state_type const m = 0x5bd1e995; |
| 70 | + int const r = 24; |
| 71 | + state_type hash = salt ^ len; |
| 72 | + |
| 73 | + constexpr std::size_t N = sizeof(state_type); |
| 74 | + e = std::next( b, len & ~std::size_t(N-1) ); |
| 75 | + for( ; b != e; std::advance(b, N) ) |
| 76 | + { |
| 77 | + state_type num; |
| 78 | + std::copy_n( b, N, reinterpret_cast<unsigned char*>(&num) ); |
| 79 | + |
| 80 | + num *= m; |
| 81 | + num ^= num >> r; |
| 82 | + num *= m; |
| 83 | + hash *= m; |
| 84 | + hash ^= num; |
| 85 | + } |
| 86 | + |
| 87 | + switch( len & (N - 1) ) |
| 88 | + { |
| 89 | + case 3: hash ^= state_type( *std::next(b, 2) ) << 16; // fall through |
| 90 | + case 2: hash ^= state_type( *std::next(b, 1) ) << 8; // fall through |
| 91 | + case 1: hash ^= state_type( *std::next(b, 0) ); |
| 92 | + hash *= m; |
| 93 | + }; |
| 94 | + |
| 95 | + hash ^= hash >> 13; |
| 96 | + hash *= m; |
| 97 | + hash ^= hash >> 15; |
| 98 | + |
31 | 99 | #endif |
32 | | - hash += salt; |
33 | | - for(; b != e; ++b) |
34 | | - hash = (*b ^ hash) * prime; |
| 100 | + |
35 | 101 | return hash; |
36 | 102 | } |
37 | 103 |
|
|
0 commit comments