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 "likely.hh"
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<int R>
58 [[nodiscard]] static inline uint32_t rotl(uint32_t x)
59 {
60  return (x << R) | (x >> (32 - R));
61 }
62 
63 template<bool ALIGNED>
64 [[nodiscard]] static inline uint32_t read32(const uint8_t* ptr)
65 {
66  if (ALIGNED) {
67 #ifdef DEBUG
68  assert((reinterpret_cast<intptr_t>(ptr) & 3) == 0);
69 #endif
70  return *reinterpret_cast<const uint32_t*>(ptr);
71  } else {
72  uint32_t result;
73  memcpy(&result, ptr, sizeof(result));
74  return result;
75  }
76 }
77 
78 
79 template<bool ALIGNED, uint8_t MASK8 = 0xFF, uint32_t SEED = 0>
80 [[nodiscard]] static inline uint32_t xxhash_impl(const uint8_t* p, size_t size)
81 {
82  constexpr uint32_t MASK32 = MASK8 * 0x01010101U;
83 
84  const uint8_t* const bEnd = p + size;
85  uint32_t h32;
86 
87  if (unlikely(size >= 16)) {
88  const uint8_t* const limit = bEnd - 16;
89 
90  // casts to avoid: warning C4307: '+': integral constant overflow
91  uint32_t v1 = uint32_t(SEED + PRIME32_1 + uint64_t(PRIME32_2));
92  uint32_t v2 = SEED + PRIME32_2;
93  uint32_t v3 = SEED + 0;
94  uint32_t v4 = SEED - PRIME32_1;
95 
96  do {
97  uint32_t r1 = (read32<ALIGNED>(p + 0) & MASK32) * PRIME32_2;
98  uint32_t r2 = (read32<ALIGNED>(p + 4) & MASK32) * PRIME32_2;
99  uint32_t r3 = (read32<ALIGNED>(p + 8) & MASK32) * PRIME32_2;
100  uint32_t r4 = (read32<ALIGNED>(p + 12) & MASK32) * PRIME32_2;
101  p += 16;
102  v1 = rotl<13>(v1 + r1) * PRIME32_1;
103  v2 = rotl<13>(v2 + r2) * PRIME32_1;
104  v3 = rotl<13>(v3 + r3) * PRIME32_1;
105  v4 = rotl<13>(v4 + r4) * PRIME32_1;
106  } while (p <= limit);
107 
108  h32 = rotl<1>(v1) + rotl<7>(v2) + rotl<12>(v3) + rotl<18>(v4);
109  } else {
110  h32 = SEED + PRIME32_5;
111  }
112 
113  h32 += uint32_t(size);
114 
115  while ((p + 4) <= bEnd) {
116  uint32_t r = (read32<ALIGNED>(p) & MASK32) * PRIME32_3;
117  h32 = rotl<17>(h32 + r) * PRIME32_4;
118  p += 4;
119  }
120 
121  while (p < bEnd) {
122  uint32_t r = (*p & MASK8) * PRIME32_5;
123  h32 = rotl<11>(h32 + r) * PRIME32_1;
124  p += 1;
125  }
126 
127  h32 = (h32 ^ (h32 >> 15)) * PRIME32_2;
128  h32 = (h32 ^ (h32 >> 13)) * PRIME32_3;
129  return h32 ^ (h32 >> 16);
130 }
131 
132 template<uint8_t MASK8> [[nodiscard]] static inline uint32_t xxhash_impl(std::string_view key)
133 {
134  auto* data = reinterpret_cast<const uint8_t*>(key.data());
135  auto size = key.size();
136  if (reinterpret_cast<intptr_t>(data) & 3) {
137  return xxhash_impl<false, MASK8>(data, size);
138  } else {
139  // Input is aligned, let's leverage the speed advantage
140  return xxhash_impl<true, MASK8>(data, size);
141  }
142 }
143 
144 [[nodiscard]] inline uint32_t xxhash(std::string_view key)
145 {
146  return xxhash_impl<0xFF>(key);
147 }
148 [[nodiscard]] inline uint32_t xxhash_case(std::string_view key)
149 {
150  return xxhash_impl<static_cast<uint8_t>(~('a' - 'A'))>(key);
151 }
152 
153 struct XXHasher {
154  [[nodiscard]] uint32_t operator()(std::string_view key) const {
155  return xxhash(key);
156  }
157 };
158 
160  [[nodiscard]] uint32_t operator()(std::string_view key) const {
161  return xxhash_case(key);
162  }
163 };
164 
165 #endif
unlikely
#define unlikely(x)
Definition: likely.hh:15
XXHasher_IgnoreCase::operator()
uint32_t operator()(std::string_view key) const
Definition: xxhash.hh:160
PRIME32_4
constexpr uint32_t PRIME32_4
Definition: xxhash.hh:53
utf8::unchecked::size
size_t size(std::string_view utf8)
Definition: utf8_unchecked.hh:227
openmsx::R
constexpr auto R
Definition: MSXMixer.cc:297
xxhash_case
uint32_t xxhash_case(std::string_view key)
Definition: xxhash.hh:148
XXHasher
Definition: xxhash.hh:153
v4
vec4 v4(2, 1, -4, -3)
xxhash
uint32_t xxhash(std::string_view key)
Definition: xxhash.hh:144
PRIME32_3
constexpr uint32_t PRIME32_3
Definition: xxhash.hh:52
likely.hh
PRIME32_5
constexpr uint32_t PRIME32_5
Definition: xxhash.hh:54
XXHasher_IgnoreCase
Definition: xxhash.hh:159
openmsx::x
constexpr KeyMatrixPosition x
Keyboard bindings.
Definition: Keyboard.cc:1419
XXHasher::operator()
uint32_t operator()(std::string_view key) const
Definition: xxhash.hh:154
PRIME32_1
constexpr uint32_t PRIME32_1
Definition: xxhash.hh:50
PRIME32_2
constexpr uint32_t PRIME32_2
Definition: xxhash.hh:51