1 /**************************************************************************
3 * Copyright 2013-2014 RAD Game Tools and Valve Software
4 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 **************************************************************************/
30 #include "vogl_core.h"
34 extern uint64_t g_crc64_table[256];
36 const uint64_t CRC64_INIT = 0;
38 uint64_t calc_crc64(uint64_t crc, const uint8 *buf, size_t size);
40 uint64_t calc_sum64(const uint8 *buf, size_t size, uint shift_amount = 0);
42 uint32 fast_hash(const void *p, int len);
45 inline uint32 fast_hash_obj(const T &obj)
47 return fast_hash(&obj, sizeof(obj));
50 // 4-byte integer hash, full avalanche
51 inline uint32 bitmix32c(uint32 a)
53 a = (a + 0x7ed55d16) + (a << 12);
54 a = (a ^ 0xc761c23c) ^ (a >> 19);
55 a = (a + 0x165667b1) + (a << 5);
56 a = (a + 0xd3a2646c) ^ (a << 9);
57 a = (a + 0xfd7046c5) + (a << 3);
58 a = (a ^ 0xb55a4f09) ^ (a >> 16);
62 // 4-byte integer hash, full avalanche, no constants
63 inline uint32 bitmix32(uint32 a)
75 // Derived from: http://www.altdevblogaday.com/2011/10/27/quasi-compile-time-string-hashing/
76 // Also see: http://www.isthe.com/chongo/tech/comp/fnv/#xor-fold
84 inline string_hash(uint hash)
88 inline string_hash(const string_hash &other)
89 : m_hash(other.m_hash)
92 inline string_hash &operator=(const string_hash &rhs)
99 inline string_hash(const char (&str)[N])
100 : m_hash(fnv_hash<N, N>::hash(str))
105 inline bool operator==(const char (&str)[N]) const
107 return m_hash == fnv_hash<N, N>::hash(str);
111 inline bool operator!=(const char (&str)[N]) const
113 return m_hash != fnv_hash<N, N>::hash(str);
116 inline bool operator==(const string_hash &other) const
118 return m_hash == other.m_hash;
120 inline bool operator!=(const string_hash &other) const
122 return m_hash != other.m_hash;
125 struct const_char_ptr
127 inline const_char_ptr(const char *pStr)
133 inline string_hash(const const_char_ptr &str)
136 finalize(str.m_pStr);
138 inline string_hash(const const_char_ptr &str, size_t len)
141 finalize(str.m_pStr, len);
144 inline string_hash(char *pStr)
147 finalize((const char *)pStr);
149 inline string_hash(char *pStr, size_t len)
152 finalize((const char *)pStr, len);
155 inline uint get_hash() const
159 inline void set_hash(uint hash)
164 inline operator size_t() const
169 inline uint set(const char *pStr, size_t len)
172 return finalize(pStr, len);
175 inline uint set(const char *pStr)
178 return finalize(pStr);
183 m_hash = 2166136261U;
186 inline uint finalize(const char *pStr, size_t len)
193 inline uint finalize(const char *pStr)
195 return finalize(pStr, strlen(pStr));
198 inline uint update(const char *pStr, size_t len)
201 for (const char *pEnd = pStr + len; pStr != pEnd; ++pStr)
210 inline uint update(const char *pStr)
212 return update(pStr, strlen(pStr));
218 template <uint N, uint I>
221 inline static uint hash(const char (&str)[N])
223 return (fnv_hash<N, I - 1>::hash(str) ^ str[I - 1]) * 16777619U;
228 struct fnv_hash<N, 1>
230 inline static uint hash(const char (&str)[N])
232 return (2166136261U ^ str[0]) * 16777619U;
237 const char *find_well_known_string_hash(const string_hash &hash);