blob: d4b0c16a087e6894b0b5cca6f151a1ec561ece10 [file] [log] [blame]
[email protected]e7a765c2012-09-25 01:05:001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4//
5// Defines methods for hashing a pair of integers.
6
7#ifndef CC_HASH_PAIR_H_
8#define CC_HASH_PAIR_H_
9
10#include "base/hash_tables.h"
11
12#if defined(COMPILER_MSVC)
13
14#define DEFINE_PAIR_HASH_FUNCTION_START(type1, type2) \
15 template<> \
16 inline std::size_t hash_value<std::pair<type1, type2> >( \
17 const std::pair<type1, type2>& value)
18
19#define DEFINE_PAIR_HASH_FUNCTION_END()
20
21#elif defined(COMPILER_GCC)
22
23#define DEFINE_PAIR_HASH_FUNCTION_START(type1, type2) \
24 template<> \
25 struct hash<std::pair<type1, type2> > { \
26 std::size_t operator()(std::pair<type1, type2> value) const
27
28#define DEFINE_PAIR_HASH_FUNCTION_END() \
29 };
30
31#else
32#error define DEFINE_PAIR_HASH_FUNCTION_START for your compiler
33#endif // COMPILER
34
35namespace BASE_HASH_NAMESPACE {
36
37// Implement hashing for pairs of at-most 32 bit integer values.
38// When size_t is 32 bits, we turn the 64-bit hash code into 32 bits by using
39// multiply-add hashing. This algorithm, as described in
40// Theorem 4.3.3 of the thesis "Über die Komplexität der Multiplikation in
41// eingeschränkten Branchingprogrammmodellen" by Woelfel, is:
42//
43// h32(x32, y32) = (h64(x32, y32) * randOdd64 + rand16 * 2^16) % 2^64 / 2^32
44
45#define DEFINE_32BIT_PAIR_HASH(type1, type2) \
46 DEFINE_PAIR_HASH_FUNCTION_START(type1, type2) { \
47 uint64 first = value.first; \
48 uint32 second = value.second; \
49 uint64 hash64 = (first << 32) | second; \
50 \
51 if (sizeof(std::size_t) >= sizeof(uint64)) \
52 return static_cast<std::size_t>(hash64); \
53 \
54 uint64 oddRandom = 481046412LL << 32 | 1025306954LL; \
55 uint32 shiftRandom = 10121U << 16; \
56 \
57 hash64 = hash64 * oddRandom + shiftRandom; \
58 std::size_t highBits = static_cast<std::size_t>( \
59 hash64 >> (sizeof(uint64) - sizeof(std::size_t))); \
60 return highBits; \
61 } \
62 DEFINE_PAIR_HASH_FUNCTION_END();
63
64DEFINE_32BIT_PAIR_HASH(short, short);
65DEFINE_32BIT_PAIR_HASH(short, unsigned short);
66DEFINE_32BIT_PAIR_HASH(short, int);
67DEFINE_32BIT_PAIR_HASH(short, unsigned);
68DEFINE_32BIT_PAIR_HASH(unsigned short, short);
69DEFINE_32BIT_PAIR_HASH(unsigned short, unsigned short);
70DEFINE_32BIT_PAIR_HASH(unsigned short, int);
71DEFINE_32BIT_PAIR_HASH(unsigned short, unsigned);
72DEFINE_32BIT_PAIR_HASH(int, short);
73DEFINE_32BIT_PAIR_HASH(int, unsigned short);
74DEFINE_32BIT_PAIR_HASH(int, int);
75DEFINE_32BIT_PAIR_HASH(int, unsigned);
76DEFINE_32BIT_PAIR_HASH(unsigned, short);
77DEFINE_32BIT_PAIR_HASH(unsigned, unsigned short);
78DEFINE_32BIT_PAIR_HASH(unsigned, int);
79DEFINE_32BIT_PAIR_HASH(unsigned, unsigned);
80
81#undef DEFINE_32BIT_PAIR_HASH
82
83// Implement hashing for pairs of up-to 64-bit integer values.
84// We use the compound integer hash method to produce a 64-bit hash code, by
85// breaking the two 64-bit inputs into 4 32-bit values:
86// http://opendatastructures.org/versions/edition-0.1d/ods-java/node33.html#SECTION00832000000000000000
87// Then we reduce our result to 32 bits if required, similar to above.
88
89#define DEFINE_64BIT_PAIR_HASH(type1, type2) \
90 DEFINE_PAIR_HASH_FUNCTION_START(type1, type2) { \
91 uint32 shortRandom1 = 842304669U; \
92 uint32 shortRandom2 = 619063811U; \
93 uint32 shortRandom3 = 937041849U; \
94 uint32 shortRandom4 = 3309708029U; \
95 \
96 uint64 value1 = value.first; \
97 uint64 value2 = value.second; \
98 uint32 value1a = static_cast<uint32>(value1 & 0xffffffff); \
99 uint32 value1b = static_cast<uint32>((value1 >> 32) & 0xffffffff); \
100 uint32 value2a = static_cast<uint32>(value2 & 0xffffffff); \
101 uint32 value2b = static_cast<uint32>((value2 >> 32) & 0xffffffff); \
102 \
103 uint64 product1 = static_cast<uint64>(value1a) * shortRandom1; \
104 uint64 product2 = static_cast<uint64>(value1b) * shortRandom2; \
105 uint64 product3 = static_cast<uint64>(value2a) * shortRandom3; \
106 uint64 product4 = static_cast<uint64>(value2b) * shortRandom4; \
107 \
108 uint64 hash64 = product1 + product2 + product3 + product4; \
109 \
110 if (sizeof(std::size_t) >= sizeof(uint64)) \
111 return static_cast<std::size_t>(hash64); \
112 \
113 uint64 oddRandom = 1578233944LL << 32 | 194370989LL; \
114 uint32 shiftRandom = 20591U << 16; \
115 \
116 hash64 = hash64 * oddRandom + shiftRandom; \
117 std::size_t highBits = static_cast<std::size_t>( \
118 hash64 >> (sizeof(uint64) - sizeof(std::size_t))); \
119 return highBits; \
120 } \
121 DEFINE_PAIR_HASH_FUNCTION_END();
122
123DEFINE_64BIT_PAIR_HASH(short, int64);
124DEFINE_64BIT_PAIR_HASH(short, uint64);
125DEFINE_64BIT_PAIR_HASH(unsigned short, int64);
126DEFINE_64BIT_PAIR_HASH(unsigned short, uint64);
127DEFINE_64BIT_PAIR_HASH(int, int64);
128DEFINE_64BIT_PAIR_HASH(int, uint64);
129DEFINE_64BIT_PAIR_HASH(unsigned, int64);
130DEFINE_64BIT_PAIR_HASH(unsigned, uint64);
131DEFINE_64BIT_PAIR_HASH(int64, short);
132DEFINE_64BIT_PAIR_HASH(int64, unsigned short);
133DEFINE_64BIT_PAIR_HASH(int64, int);
134DEFINE_64BIT_PAIR_HASH(int64, unsigned);
135DEFINE_64BIT_PAIR_HASH(int64, int64);
136DEFINE_64BIT_PAIR_HASH(int64, uint64);
137DEFINE_64BIT_PAIR_HASH(uint64, short);
138DEFINE_64BIT_PAIR_HASH(uint64, unsigned short);
139DEFINE_64BIT_PAIR_HASH(uint64, int);
140DEFINE_64BIT_PAIR_HASH(uint64, unsigned);
141DEFINE_64BIT_PAIR_HASH(uint64, int64);
142DEFINE_64BIT_PAIR_HASH(uint64, uint64);
143
144#undef DEFINE_64BIT_PAIR_HASH
145}
146
147#undef DEFINE_PAIR_HASH_FUNCTION_START
148#undef DEFINE_PAIR_HASH_FUNCTION_END
149
150#endif // CC_HASH_PAIR_H_