]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/libsupc++/hash_bytes.cc
hash_bytes.cc: Move...
[thirdparty/gcc.git] / libstdc++-v3 / libsupc++ / hash_bytes.cc
1 // Definition of _Hash_bytes. -*- C++ -*-
2
3 // Copyright (C) 2010 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 // This file defines Hash_bytes, a primitive used for defining hash
26 // functions. Based on public domain MurmurHashUnaligned2, by Austin
27 // Appleby. http://murmurhash.googlepages.com/
28
29 // This file also defines _Fnv_hash_bytes, another primitive with
30 // exactly the same interface but using a different hash algorithm,
31 // Fowler / Noll / Vo (FNV) Hash (type FNV-1a). The Murmur hash
32 // function apears to be better in both speed and hash quality, and
33 // FNV is provided primarily for backward compatibility.
34
35 #include <bits/c++config.h>
36
37 namespace
38 {
39 inline std::size_t
40 unaligned_load(const char* p)
41 {
42 std::size_t result;
43 __builtin_memcpy(&result, p, sizeof(result));
44 return result;
45 }
46
47 #if __SIZEOF_SIZE_T__ == 8
48 // Loads n bytes, where 1 <= n < 8.
49 inline std::size_t
50 load_bytes(const char* p, int n)
51 {
52 std::size_t result = 0;
53 --n;
54 do
55 result = (result << 8) + static_cast<unsigned char>(p[n]);
56 while (--n >= 0);
57 return result;
58 }
59
60 inline std::size_t
61 shift_mix(std::size_t v)
62 { return v ^ (v >> 47);}
63 #endif
64 }
65
66 namespace std
67 {
68 #if __SIZEOF_SIZE_T__ == 4
69
70 // Implementation of Murmur hash for 32-bit size_t.
71 size_t
72 _Hash_bytes(const void* ptr, size_t len, size_t seed)
73 {
74 const size_t m = 0x5bd1e995;
75 size_t hash = seed ^ len;
76 const char* buf = static_cast<const char*>(ptr);
77
78 // Mix 4 bytes at a time into the hash.
79 while(len >= 4)
80 {
81 size_t k = unaligned_load(buf);
82 k *= m;
83 k ^= k >> 24;
84 k *= m;
85 hash *= m;
86 hash ^= k;
87 buf += 4;
88 len -= 4;
89 }
90
91 // Handle the last few bytes of the input array.
92 switch(len)
93 {
94 case 3:
95 hash ^= static_cast<unsigned char>(buf[2]) << 16;
96 case 2:
97 hash ^= static_cast<unsigned char>(buf[1]) << 8;
98 case 1:
99 hash ^= static_cast<unsigned char>(buf[0]);
100 hash *= m;
101 };
102
103 // Do a few final mixes of the hash.
104 hash ^= hash >> 13;
105 hash *= m;
106 hash ^= hash >> 15;
107 return hash;
108 }
109
110 // Implementation of FNV hash for 32-bit size_t.
111 size_t
112 _Fnv_hash_bytes(const void* ptr, size_t len, size_t hash)
113 {
114 const char* cptr = static_cast<const char*>(ptr);
115 for (; len; --len)
116 {
117 hash ^= static_cast<size_t>(*cptr++);
118 hash *= static_cast<size_t>(16777619UL);
119 }
120 return hash;
121 }
122
123 #elif __SIZEOF_SIZE_T__ == 8
124
125 // Implementation of Murmur hash for 64-bit size_t.
126 size_t
127 _Hash_bytes(const void* ptr, size_t len, size_t seed)
128 {
129 static const size_t mul = (0xc6a4a793UL << 32UL) + 0x5bd1e995UL;
130 const char* const buf = static_cast<const char*>(ptr);
131
132 // Remove the bytes not divisible by the sizeof(size_t). This
133 // allows the main loop to process the data as 64-bit integers.
134 const int len_aligned = len & ~0x7;
135 const char* const end = buf + len_aligned;
136 size_t hash = seed ^ (len * mul);
137 for (const char* p = buf; p != end; p += 8)
138 {
139 const size_t data = shift_mix(unaligned_load(p) * mul) * mul;
140 hash ^= data;
141 hash *= mul;
142 }
143 if ((len & 0x7) != 0)
144 {
145 const size_t data = load_bytes(end, len & 0x7);
146 hash ^= data;
147 hash *= mul;
148 }
149 hash = shift_mix(hash) * mul;
150 hash = shift_mix(hash);
151 return hash;
152 }
153
154 // Implementation of FNV hash for 64-bit size_t.
155 size_t
156 _Fnv_hash_bytes(const void* ptr, size_t len, size_t hash)
157 {
158 const char* cptr = static_cast<const char*>(ptr);
159 for (; len; --len)
160 {
161 hash ^= static_cast<size_t>(*cptr++);
162 hash *= static_cast<size_t>(1099511628211ULL);
163 }
164 return hash;
165 }
166
167 #else
168
169 // Dummy hash implementation for unusual sizeof(size_t).
170 size_t
171 _Hash_bytes(const void* ptr, size_t len, size_t seed)
172 {
173 size_t hash = seed;
174 const char* cptr = reinterpret_cast<const char*>(ptr);
175 for (; clength; --clength)
176 hash = (hash * 131) + *cptr++;
177 return hash;
178 }
179
180 size_t
181 _Fnv_hash_bytes(const void* ptr, size_t len, size_t seed)
182 { return _Hash_bytes(ptr, len, seed); }
183
184 #endif /* __SIZEOF_SIZE_T__ */
185 }