summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--AK/HashFunctions.h14
-rw-r--r--Tests/AK/TestHashFunctions.cpp5
2 files changed, 12 insertions, 7 deletions
diff --git a/AK/HashFunctions.h b/AK/HashFunctions.h
index 951619c4a7..e869b44800 100644
--- a/AK/HashFunctions.h
+++ b/AK/HashFunctions.h
@@ -21,11 +21,15 @@ constexpr unsigned int_hash(u32 key)
constexpr unsigned double_hash(u32 key)
{
- key = ~key + (key >> 23);
- key ^= (key << 12);
- key ^= (key >> 7);
- key ^= (key << 2);
- key ^= (key >> 20);
+ const unsigned magic = 0xBA5EDB01;
+ if (key == magic)
+ return 0u;
+ if (key == 0u)
+ key = magic;
+
+ key ^= key << 13;
+ key ^= key >> 17;
+ key ^= key << 5;
return key;
}
diff --git a/Tests/AK/TestHashFunctions.cpp b/Tests/AK/TestHashFunctions.cpp
index fa8eafae9e..6fbb0ab6f7 100644
--- a/Tests/AK/TestHashFunctions.cpp
+++ b/Tests/AK/TestHashFunctions.cpp
@@ -17,8 +17,9 @@ TEST_CASE(int_hash)
TEST_CASE(double_hash)
{
- static_assert(double_hash(42) == 524450u);
- static_assert(double_hash(0) == 12384u);
+ static_assert(double_hash(666) == 171644115u);
+ static_assert(double_hash(0) == 1189591134u);
+ static_assert(double_hash(0xBA5EDB01) == 0u);
}
TEST_CASE(pair_int_hash)