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_ref)
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 "string_ref.hh"
46 #include "likely.hh"
47 #include "build-info.hh"
48 #include <cstdint>
49 #include <cstring>
50 
51 static const uint32_t PRIME32_1 = 2654435761;
52 static const uint32_t PRIME32_2 = 2246822519;
53 static const uint32_t PRIME32_3 = 3266489917;
54 static const uint32_t PRIME32_4 = 668265263;
55 static const uint32_t PRIME32_5 = 374761393;
56 
57 
58 template<int R>
59 static inline uint32_t rotl(uint32_t x)
60 {
61  return (x << R) | (x >> (32 - R));
62 }
63 
64 template<bool ALIGNED>
65 static inline uint32_t read32(const uint8_t* ptr)
66 {
67  if (ALIGNED) {
68  return *reinterpret_cast<const uint32_t*>(ptr);
69  } else {
70  uint32_t result;
71  memcpy(&result, ptr, sizeof(result));
72  return result;
73  }
74 }
75 
76 
77 template<bool ALIGNED, uint8_t MASK8 = 0xFF, uint32_t SEED = 0>
78 static inline uint32_t xxhash_impl(const uint8_t* p, size_t size)
79 {
80  static const uint32_t MASK32 = MASK8 * 0x01010101U;
81 
82  const uint8_t* const bEnd = p + size;
83  uint32_t h32;
84 
85  if (unlikely(size >= 16)) {
86  const uint8_t* const limit = bEnd - 16;
87 
88  // casts to avoid: warning C4307: '+': integral constant overflow
89  uint32_t v1 = uint32_t(SEED + PRIME32_1 + uint64_t(PRIME32_2));
90  uint32_t v2 = SEED + PRIME32_2;
91  uint32_t v3 = SEED + 0;
92  uint32_t v4 = SEED - PRIME32_1;
93 
94  do {
95  uint32_t r1 = (read32<ALIGNED>(p + 0) & MASK32) * PRIME32_2;
96  uint32_t r2 = (read32<ALIGNED>(p + 4) & MASK32) * PRIME32_2;
97  uint32_t r3 = (read32<ALIGNED>(p + 8) & MASK32) * PRIME32_2;
98  uint32_t r4 = (read32<ALIGNED>(p + 12) & MASK32) * PRIME32_2;
99  p += 16;
100  v1 = rotl<13>(v1 + r1) * PRIME32_1;
101  v2 = rotl<13>(v2 + r2) * PRIME32_1;
102  v3 = rotl<13>(v3 + r3) * PRIME32_1;
103  v4 = rotl<13>(v4 + r4) * PRIME32_1;
104  } while (p <= limit);
105 
106  h32 = rotl<1>(v1) + rotl<7>(v2) + rotl<12>(v3) + rotl<18>(v4);
107  } else {
108  h32 = SEED + PRIME32_5;
109  }
110 
111  h32 += uint32_t(size);
112 
113  while ((p + 4) <= bEnd) {
114  uint32_t r = (read32<ALIGNED>(p) & MASK32) * PRIME32_3;
115  h32 = rotl<17>(h32 + r) * PRIME32_4;
116  p += 4;
117  }
118 
119  while (p < bEnd) {
120  uint32_t r = (*p & MASK8) * PRIME32_5;
121  h32 = rotl<11>(h32 + r) * PRIME32_1;
122  p += 1;
123  }
124 
125  h32 = (h32 ^ (h32 >> 15)) * PRIME32_2;
126  h32 = (h32 ^ (h32 >> 13)) * PRIME32_3;
127  return h32 ^ (h32 >> 16);
128 }
129 
130 template<uint8_t MASK8> static inline uint32_t xxhash_impl(string_ref key)
131 {
132  auto* data = reinterpret_cast<const uint8_t*>(key.data());
133  auto size = key.size();
134  if (!openmsx::OPENMSX_UNALIGNED_MEMORY_ACCESS &&
135  (reinterpret_cast<intptr_t>(data) & 3)) {
136  return xxhash_impl<false, MASK8>(data, size);
137  } else {
138  // Input is aligned, let's leverage the speed advantage
139  return xxhash_impl<true, MASK8>(data, size);
140  }
141 }
142 
143 inline uint32_t xxhash(string_ref key)
144 {
145  return xxhash_impl<0xFF>(key);
146 }
147 inline uint32_t xxhash_case(string_ref key)
148 {
149  return xxhash_impl<static_cast<uint8_t>(~('a' - 'A'))>(key);
150 }
151 
152 struct XXHasher {
153  uint32_t operator()(string_ref key) const {
154  return xxhash(key);
155  }
156 };
157 
159  uint32_t operator()(string_ref key) const {
160  return xxhash_case(key);
161  }
162 };
163 
164 #endif
uint32_t xxhash_case(string_ref key)
Definition: xxhash.hh:147
#define ALIGNED(EXPRESSION, ALIGNMENT)
Definition: aligned.hh:12
#define unlikely(x)
Definition: likely.hh:15
uint32_t operator()(string_ref key) const
Definition: xxhash.hh:153
This class implements a subset of the proposal for std::string_ref (proposed for the next c++ standar...
Definition: string_ref.hh:18
const char * data() const
Definition: string_ref.hh:68
size_t size() const
uint32_t xxhash(string_ref key)
Definition: xxhash.hh:143
size_type size() const
Definition: string_ref.hh:55
uint8_t * data()
uint32_t operator()(string_ref key) const
Definition: xxhash.hh:159