openMSX
xxhash.hh
Go to the documentation of this file.
1 /*
2  This code is a based on xxhash, the main modifications are:
3  - allow to produce case-insensitive (for ascii) hash values
4  - tweak interface to fit openMSX (e.g. directly work on string_view)
5 
6 - - - - - - - - - - - - - - - - - - - -
7 
8  original header:
9 
10  xxHash - Fast Hash algorithm
11  Header File
12  Copyright (C) 2012-2014, Yann Collet.
13  BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
14 
15  Redistribution and use in source and binary forms, with or without
16  modification, are permitted provided that the following conditions are
17  met:
18 
19  * Redistributions of source code must retain the above copyright
20  notice, this list of conditions and the following disclaimer.
21  * Redistributions in binary form must reproduce the above
22  copyright notice, this list of conditions and the following disclaimer
23  in the documentation and/or other materials provided with the
24  distribution.
25 
26  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 
38  You can contact the author at :
39  - xxHash source repository : http://code.google.com/p/xxhash/
40 */
41 
42 #ifndef XXHASH_HH
43 #define XXHASH_HH
44 
45 #include <bit>
46 #include <cstdint>
47 #include <cstring>
48 #include <string_view>
49 
50 constexpr uint32_t PRIME32_1 = 2654435761;
51 constexpr uint32_t PRIME32_2 = 2246822519;
52 constexpr uint32_t PRIME32_3 = 3266489917;
53 constexpr uint32_t PRIME32_4 = 668265263;
54 constexpr uint32_t PRIME32_5 = 374761393;
55 
56 
57 template<bool ALIGNED>
58 [[nodiscard]] inline uint32_t read32(const uint8_t* ptr)
59 {
60  if constexpr (ALIGNED) {
61 #ifdef DEBUG
62  assert((reinterpret_cast<intptr_t>(ptr) & 3) == 0);
63 #endif
64  return *reinterpret_cast<const uint32_t*>(ptr);
65  } else {
66  uint32_t result;
67  memcpy(&result, ptr, sizeof(result));
68  return result;
69  }
70 }
71 
72 
73 template<bool ALIGNED, uint8_t MASK8 = 0xFF, uint32_t SEED = 0>
74 [[nodiscard]] inline uint32_t xxhash_impl(const uint8_t* p, size_t size)
75 {
76  constexpr uint32_t MASK32 = MASK8 * 0x01010101U;
77 
78  const uint8_t* const bEnd = p + size;
79  uint32_t h32;
80 
81  if (size >= 16) [[unlikely]] {
82  const uint8_t* const limit = bEnd - 16;
83 
84  // casts to avoid: warning C4307: '+': integral constant overflow
85  uint32_t v1 = uint32_t(SEED + PRIME32_1 + uint64_t(PRIME32_2));
86  uint32_t v2 = SEED + PRIME32_2;
87  uint32_t v3 = SEED + 0;
88  uint32_t v4 = SEED - PRIME32_1;
89 
90  do {
91  uint32_t r1 = (read32<ALIGNED>(p + 0) & MASK32) * PRIME32_2;
92  uint32_t r2 = (read32<ALIGNED>(p + 4) & MASK32) * PRIME32_2;
93  uint32_t r3 = (read32<ALIGNED>(p + 8) & MASK32) * PRIME32_2;
94  uint32_t r4 = (read32<ALIGNED>(p + 12) & MASK32) * PRIME32_2;
95  p += 16;
96  v1 = std::rotl(v1 + r1, 13) * PRIME32_1;
97  v2 = std::rotl(v2 + r2, 13) * PRIME32_1;
98  v3 = std::rotl(v3 + r3, 13) * PRIME32_1;
99  v4 = std::rotl(v4 + r4, 13) * PRIME32_1;
100  } while (p <= limit);
101 
102  h32 = std::rotl(v1, 1) + std::rotl(v2, 7) + std::rotl(v3, 12) + std::rotl(v4, 18);
103  } else {
104  h32 = SEED + PRIME32_5;
105  }
106 
107  h32 += uint32_t(size);
108 
109  while ((p + 4) <= bEnd) {
110  uint32_t r = (read32<ALIGNED>(p) & MASK32) * PRIME32_3;
111  h32 = std::rotl(h32 + r, 17) * PRIME32_4;
112  p += 4;
113  }
114 
115  while (p < bEnd) {
116  uint32_t r = (*p & MASK8) * PRIME32_5;
117  h32 = std::rotl(h32 + r, 11) * PRIME32_1;
118  p += 1;
119  }
120 
121  h32 = (h32 ^ (h32 >> 15)) * PRIME32_2;
122  h32 = (h32 ^ (h32 >> 13)) * PRIME32_3;
123  return h32 ^ (h32 >> 16);
124 }
125 
126 template<uint8_t MASK8> [[nodiscard]] inline uint32_t xxhash_impl(std::string_view key)
127 {
128  const auto* data = reinterpret_cast<const uint8_t*>(key.data());
129  auto size = key.size();
130  if (reinterpret_cast<intptr_t>(data) & 3) {
131  return xxhash_impl<false, MASK8>(data, size);
132  } else {
133  // Input is aligned, let's leverage the speed advantage
134  return xxhash_impl<true, MASK8>(data, size);
135  }
136 }
137 
138 [[nodiscard]] inline uint32_t xxhash(std::string_view key)
139 {
140  return xxhash_impl<0xFF>(key);
141 }
142 [[nodiscard]] inline uint32_t xxhash_case(std::string_view key)
143 {
144  return xxhash_impl<static_cast<uint8_t>(~('a' - 'A'))>(key);
145 }
146 
147 struct XXHasher {
148  [[nodiscard]] uint32_t operator()(std::string_view key) const {
149  return xxhash(key);
150  }
151 };
152 
154  [[nodiscard]] uint32_t operator()(std::string_view key) const {
155  return xxhash_case(key);
156  }
157 };
158 
159 #endif
vec4 v4(2, 1, -4, -3)
size_t size(std::string_view utf8)
uint32_t operator()(std::string_view key) const
Definition: xxhash.hh:154
uint32_t operator()(std::string_view key) const
Definition: xxhash.hh:148
uint32_t read32(const uint8_t *ptr)
Definition: xxhash.hh:58
uint32_t xxhash_impl(const uint8_t *p, size_t size)
Definition: xxhash.hh:74
constexpr uint32_t PRIME32_1
Definition: xxhash.hh:50
constexpr uint32_t PRIME32_2
Definition: xxhash.hh:51
uint32_t xxhash(std::string_view key)
Definition: xxhash.hh:138
constexpr uint32_t PRIME32_3
Definition: xxhash.hh:52
uint32_t xxhash_case(std::string_view key)
Definition: xxhash.hh:142
constexpr uint32_t PRIME32_5
Definition: xxhash.hh:54
constexpr uint32_t PRIME32_4
Definition: xxhash.hh:53