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

Unified Diff: src/base/functional.cc

Issue 632713002: Don't use identity as hash function for integers. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 2 months 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/base/functional.h ('k') | test/unittests/base/functional-unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/base/functional.cc
diff --git a/src/base/functional.cc b/src/base/functional.cc
index c4a61adda2452f9203b2b88cf986a58f7f1fea36..4c0f3c8b2af3bf3f16a0286402d895dd9d8c20f9 100644
--- a/src/base/functional.cc
+++ b/src/base/functional.cc
@@ -64,6 +64,19 @@ size_t hash_combine(size_t seed, size_t value) {
}
+// Thomas Wang, Integer Hash Functions.
+// http://www.concentric.net/~Ttwang/tech/inthash.htm
+size_t hash_value(unsigned int v) {
+ v = ~v + (v << 15); // v = (v << 15) - v - 1;
+ v = v ^ (v >> 12);
+ v = v + (v << 2);
+ v = v ^ (v >> 4);
+ v = v * 2057; // v = (v + (v << 3)) + (v << 11);
+ v = v ^ (v >> 16);
+ return v;
+}
+
+
size_t hash_value(unsigned long v) { // NOLINT(runtime/int)
return hash_value_unsigned(v);
}
@@ -76,13 +89,13 @@ size_t hash_value(unsigned long long v) { // NOLINT(runtime/int)
size_t hash_value(float v) {
// 0 and -0 both hash to zero.
- return v != 0.0f ? hash_value(bit_cast<uint32_t>(v)) : 0;
+ return v != 0.0f ? hash_value_unsigned(bit_cast<uint32_t>(v)) : 0;
}
size_t hash_value(double v) {
// 0 and -0 both hash to zero.
- return v != 0.0 ? hash_value(bit_cast<uint64_t>(v)) : 0;
+ return v != 0.0 ? hash_value_unsigned(bit_cast<uint64_t>(v)) : 0;
}
} // namespace base
« no previous file with comments | « src/base/functional.h ('k') | test/unittests/base/functional-unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698