Add signalSyncPoint to the WebGraphicsContext3D command buffer impls.
[chromium-blink-merge.git] / cc / base / hash_pair.h
blob5e0aa0826080ede766638d3b4cde7881d944eaeb
1 // 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.
7 #ifndef CC_BASE_HASH_PAIR_H_
8 #define CC_BASE_HASH_PAIR_H_
10 #include <utility>
12 #include "base/basictypes.h"
13 #include "base/hash_tables.h"
15 #if defined(COMPILER_MSVC)
17 #define DEFINE_PAIR_HASH_FUNCTION_START(type1, type2) \
18 template<> \
19 inline std::size_t hash_value<std::pair<type1, type2> >( \
20 const std::pair<type1, type2>& value)
22 #define DEFINE_PAIR_HASH_FUNCTION_END()
24 #elif defined(COMPILER_GCC)
26 #define DEFINE_PAIR_HASH_FUNCTION_START(type1, type2) \
27 template<> \
28 struct hash<std::pair<type1, type2> > { \
29 std::size_t operator()(std::pair<type1, type2> value) const
31 #define DEFINE_PAIR_HASH_FUNCTION_END() \
34 #else
35 #error define DEFINE_PAIR_HASH_FUNCTION_START for your compiler
36 #endif // COMPILER
38 namespace BASE_HASH_NAMESPACE {
40 // Implement hashing for pairs of at-most 32 bit integer values.
41 // When size_t is 32 bits, we turn the 64-bit hash code into 32 bits by using
42 // multiply-add hashing. This algorithm, as described in
43 // Theorem 4.3.3 of the thesis "Über die Komplexität der Multiplikation in
44 // eingeschränkten Branchingprogrammmodellen" by Woelfel, is:
46 // h32(x32, y32) = (h64(x32, y32) * rand_odd64 + rand16 * 2^16) % 2^64 / 2^32
48 #define DEFINE_32BIT_PAIR_HASH(type1, type2) \
49 DEFINE_PAIR_HASH_FUNCTION_START(type1, type2) { \
50 uint64 first = value.first; \
51 uint32 second = value.second; \
52 uint64 hash64 = (first << 32) | second; \
54 if (sizeof(std::size_t) >= sizeof(uint64)) \
55 return static_cast<std::size_t>(hash64); \
57 uint64 odd_random = 481046412LL << 32 | 1025306954LL; \
58 uint32 shift_random = 10121U << 16; \
60 hash64 = hash64 * odd_random + shift_random; \
61 std::size_t high_bits = static_cast<std::size_t>( \
62 hash64 >> (sizeof(uint64) - sizeof(std::size_t))); \
63 return high_bits; \
64 } \
65 DEFINE_PAIR_HASH_FUNCTION_END();
67 DEFINE_32BIT_PAIR_HASH(int16, int16);
68 DEFINE_32BIT_PAIR_HASH(int16, uint16);
69 DEFINE_32BIT_PAIR_HASH(int16, int32);
70 DEFINE_32BIT_PAIR_HASH(int16, uint32);
71 DEFINE_32BIT_PAIR_HASH(uint16, int16);
72 DEFINE_32BIT_PAIR_HASH(uint16, uint16);
73 DEFINE_32BIT_PAIR_HASH(uint16, int32);
74 DEFINE_32BIT_PAIR_HASH(uint16, uint32);
75 DEFINE_32BIT_PAIR_HASH(int32, int16);
76 DEFINE_32BIT_PAIR_HASH(int32, uint16);
77 DEFINE_32BIT_PAIR_HASH(int32, int32);
78 DEFINE_32BIT_PAIR_HASH(int32, uint32);
79 DEFINE_32BIT_PAIR_HASH(uint32, int16);
80 DEFINE_32BIT_PAIR_HASH(uint32, uint16);
81 DEFINE_32BIT_PAIR_HASH(uint32, int32);
82 DEFINE_32BIT_PAIR_HASH(uint32, uint32);
84 #undef DEFINE_32BIT_PAIR_HASH
86 // Implement hashing for pairs of up-to 64-bit integer values.
87 // We use the compound integer hash method to produce a 64-bit hash code, by
88 // breaking the two 64-bit inputs into 4 32-bit values:
89 // http://opendatastructures.org/versions/edition-0.1d/ods-java/node33.html#SECTION00832000000000000000
90 // Then we reduce our result to 32 bits if required, similar to above.
92 #define DEFINE_64BIT_PAIR_HASH(type1, type2) \
93 DEFINE_PAIR_HASH_FUNCTION_START(type1, type2) { \
94 uint32 short_random1 = 842304669U; \
95 uint32 short_random2 = 619063811U; \
96 uint32 short_random3 = 937041849U; \
97 uint32 short_random4 = 3309708029U; \
99 uint64 value1 = value.first; \
100 uint64 value2 = value.second; \
101 uint32 value1a = static_cast<uint32>(value1 & 0xffffffff); \
102 uint32 value1b = static_cast<uint32>((value1 >> 32) & 0xffffffff); \
103 uint32 value2a = static_cast<uint32>(value2 & 0xffffffff); \
104 uint32 value2b = static_cast<uint32>((value2 >> 32) & 0xffffffff); \
106 uint64 product1 = static_cast<uint64>(value1a) * short_random1; \
107 uint64 product2 = static_cast<uint64>(value1b) * short_random2; \
108 uint64 product3 = static_cast<uint64>(value2a) * short_random3; \
109 uint64 product4 = static_cast<uint64>(value2b) * short_random4; \
111 uint64 hash64 = product1 + product2 + product3 + product4; \
113 if (sizeof(std::size_t) >= sizeof(uint64)) \
114 return static_cast<std::size_t>(hash64); \
116 uint64 odd_random = 1578233944LL << 32 | 194370989LL; \
117 uint32 shift_random = 20591U << 16; \
119 hash64 = hash64 * odd_random + shift_random; \
120 std::size_t high_bits = static_cast<std::size_t>( \
121 hash64 >> (sizeof(uint64) - sizeof(std::size_t))); \
122 return high_bits; \
124 DEFINE_PAIR_HASH_FUNCTION_END();
126 DEFINE_64BIT_PAIR_HASH(int16, int64);
127 DEFINE_64BIT_PAIR_HASH(int16, uint64);
128 DEFINE_64BIT_PAIR_HASH(uint16, int64);
129 DEFINE_64BIT_PAIR_HASH(uint16, uint64);
130 DEFINE_64BIT_PAIR_HASH(int32, int64);
131 DEFINE_64BIT_PAIR_HASH(int32, uint64);
132 DEFINE_64BIT_PAIR_HASH(uint32, int64);
133 DEFINE_64BIT_PAIR_HASH(uint32, uint64);
134 DEFINE_64BIT_PAIR_HASH(int64, int16);
135 DEFINE_64BIT_PAIR_HASH(int64, uint16);
136 DEFINE_64BIT_PAIR_HASH(int64, int32);
137 DEFINE_64BIT_PAIR_HASH(int64, uint32);
138 DEFINE_64BIT_PAIR_HASH(int64, int64);
139 DEFINE_64BIT_PAIR_HASH(int64, uint64);
140 DEFINE_64BIT_PAIR_HASH(uint64, int16);
141 DEFINE_64BIT_PAIR_HASH(uint64, uint16);
142 DEFINE_64BIT_PAIR_HASH(uint64, int32);
143 DEFINE_64BIT_PAIR_HASH(uint64, uint32);
144 DEFINE_64BIT_PAIR_HASH(uint64, int64);
145 DEFINE_64BIT_PAIR_HASH(uint64, uint64);
147 #undef DEFINE_64BIT_PAIR_HASH
150 #undef DEFINE_PAIR_HASH_FUNCTION_START
151 #undef DEFINE_PAIR_HASH_FUNCTION_END
153 #endif // CC_BASE_HASH_PAIR_H_