Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(76)

Side by Side Diff: third_party/WebKit/Source/wtf/HashFunctions.h

Issue 2469893005: Correct truncation behaviour in WTF::hashInts() (Closed)
Patch Set: remove test logic Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2005, 2006, 2008 Apple Inc. All rights reserved. 2 * Copyright (C) 2005, 2006, 2008 Apple Inc. All rights reserved.
3 * 3 *
4 * This library is free software; you can redistribute it and/or 4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public 5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either 6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version. 7 * version 2 of the License, or (at your option) any later version.
8 * 8 *
9 * This library is distributed in the hope that it will be useful, 9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 key += ~(key << 27); 104 key += ~(key << 27);
105 key ^= (key >> 31); 105 key ^= (key >> 31);
106 return static_cast<unsigned>(key); 106 return static_cast<unsigned>(key);
107 } 107 }
108 108
109 // Compound integer hash method: 109 // Compound integer hash method:
110 // http://opendatastructures.org/versions/edition-0.1d/ods-java/node33.html#SECT ION00832000000000000000 110 // http://opendatastructures.org/versions/edition-0.1d/ods-java/node33.html#SECT ION00832000000000000000
111 inline unsigned hashInts(unsigned key1, unsigned key2) { 111 inline unsigned hashInts(unsigned key1, unsigned key2) {
112 unsigned shortRandom1 = 277951225; // A random 32-bit value. 112 unsigned shortRandom1 = 277951225; // A random 32-bit value.
113 unsigned shortRandom2 = 95187966; // A random 32-bit value. 113 unsigned shortRandom2 = 95187966; // A random 32-bit value.
114 uint64_t longRandom = 19248658165952622LL; // A random 64-bit value. 114 uint64_t longRandom = 19248658165952623LL; // A random, odd 64-bit value.
115 115
116 uint64_t product = longRandom * (shortRandom1 * key1 + shortRandom2 * key2); 116 uint64_t product =
117 unsigned highBits = 117 longRandom * shortRandom1 * key1 + longRandom * shortRandom2 * key2;
118 static_cast<unsigned>(product >> (sizeof(uint64_t) - sizeof(unsigned))); 118 unsigned highBits = static_cast<unsigned>(
119 product >> (8 * (sizeof(uint64_t) - sizeof(unsigned))));
119 return highBits; 120 return highBits;
120 } 121 }
121 122
122 template <typename T> 123 template <typename T>
123 struct IntHash { 124 struct IntHash {
124 static unsigned hash(T key) { 125 static unsigned hash(T key) {
125 return hashInt( 126 return hashInt(
126 static_cast<typename IntTypes<sizeof(T)>::UnsignedType>(key)); 127 static_cast<typename IntTypes<sizeof(T)>::UnsignedType>(key));
127 } 128 }
128 static bool equal(T a, T b) { return a == b; } 129 static bool equal(T a, T b) { return a == b; }
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 using Hash = PairHash<T, U>; 282 using Hash = PairHash<T, U>;
282 }; 283 };
283 284
284 } // namespace WTF 285 } // namespace WTF
285 286
286 using WTF::DefaultHash; 287 using WTF::DefaultHash;
287 using WTF::IntHash; 288 using WTF::IntHash;
288 using WTF::PtrHash; 289 using WTF::PtrHash;
289 290
290 #endif // WTF_HashFunctions_h 291 #endif // WTF_HashFunctions_h
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698