Skip to content

Commit 5ae9a3e

Browse files
committed
[NFC][sanitizer] Add MurMur2Hash64Builder
Depends on D111176. Differential Revision: https://reviews.llvm.org/D111177
1 parent dc603b0 commit 5ae9a3e

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_hash.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,30 @@ class MurMur2HashBuilder {
3838
return x;
3939
}
4040
};
41+
42+
class MurMur2Hash64Builder {
43+
static const u64 m = 0xc6a4a7935bd1e995ull;
44+
static const u64 seed = 0x9747b28c9747b28cull;
45+
static const u64 r = 47;
46+
u64 h;
47+
48+
public:
49+
explicit MurMur2Hash64Builder(u64 init = 0) { h = seed ^ (init * m); }
50+
void add(u64 k) {
51+
k *= m;
52+
k ^= k >> r;
53+
k *= m;
54+
h ^= k;
55+
h *= m;
56+
}
57+
u64 get() {
58+
u64 x = h;
59+
x ^= x >> r;
60+
x *= m;
61+
x ^= x >> r;
62+
return x;
63+
}
64+
};
4165
} //namespace __sanitizer
4266

4367
#endif // SANITIZER_HASH_H

compiler-rt/lib/sanitizer_common/tests/sanitizer_hash_test.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,18 @@ TEST(SanitizerCommon, Hash32Add) {
3131
EXPECT_EQ(h.get(), 2640061027u);
3232
}
3333

34+
TEST(SanitizerCommon, Hash64Seed) {
35+
EXPECT_EQ(MurMur2Hash64Builder(0).get(), 4469829599815726255ull);
36+
EXPECT_EQ(MurMur2Hash64Builder(1).get(), 14121968454562043709ull);
37+
EXPECT_EQ(MurMur2Hash64Builder(3).get(), 8040757559320203998ull);
38+
}
39+
40+
TEST(SanitizerCommon, Hash64Add) {
41+
MurMur2Hash64Builder h(123 * sizeof(u64));
42+
for (u32 i = 0; i < 123; ++i) h.add(i);
43+
EXPECT_EQ(h.get(), 11366430808886012537ull);
44+
for (u32 i = 0; i < 123; ++i) h.add(-i);
45+
EXPECT_EQ(h.get(), 10843188204560467446ull);
46+
}
47+
3448
} // namespace __sanitizer

0 commit comments

Comments
 (0)