diff --git a/src/main/java/net/openhft/hashing/LongHashFunction.java b/src/main/java/net/openhft/hashing/LongHashFunction.java index 969efbb..85565de 100644 --- a/src/main/java/net/openhft/hashing/LongHashFunction.java +++ b/src/main/java/net/openhft/hashing/LongHashFunction.java @@ -240,9 +240,22 @@ public static LongHashFunction murmur_3() { * input on platforms with different {@link ByteOrder}, but is slower on big-endian platforms * than on little-endian. * - * @param seed the seed value to be used for hashing - * @return a {@code LongHashFunction} implementing the MurmurHash3 algorithm with the given seed value + *
The C++ {@code MurmurHash3_x64_128} function accepts a {@code uint32_t} seed. For the same + * input byte sequence, seed values from {@code 0L} through {@code 0xffffffffL} initialise the + * same state as the C++ reference implementation. This overload is a 64-bit-seed extension: it + * copies all 64 bits of the supplied {@code long} into both initial state words. Values outside + * that range have no corresponding C++ seed and initialise a different state from their + * low-32-bit value. + * + *
To interpret a Java {@code int} as the corresponding unsigned C++ seed, use + * {@link Integer#toUnsignedLong(int) Integer.toUnsignedLong(seed)}. For a seed in the C++ + * domain, this function returns the first 64-bit output word ({@code h1}) of the C++ 128-bit + * result. + * + * @param seed the seed value; all 64 bits initialise both internal state words + * @return a {@code LongHashFunction} returning the first word of the MurmurHash3 128-bit result * @see #murmur_3() + * @see LongTupleHashFunction#murmur_3(long) */ @SuppressWarnings("checkstyle:MethodName") public static LongHashFunction murmur_3(long seed) { diff --git a/src/main/java/net/openhft/hashing/LongTupleHashFunction.java b/src/main/java/net/openhft/hashing/LongTupleHashFunction.java index baa4ba9..c01a3b0 100644 --- a/src/main/java/net/openhft/hashing/LongTupleHashFunction.java +++ b/src/main/java/net/openhft/hashing/LongTupleHashFunction.java @@ -87,7 +87,21 @@ public static LongTupleHashFunction murmur_3() { * input on platforms with different {@link ByteOrder}, but is slower on big-endian platforms * than on little-endian. * + *
The C++ {@code MurmurHash3_x64_128} function accepts a {@code uint32_t} seed. For the same + * input byte sequence, seed values from {@code 0L} through {@code 0xffffffffL} initialise the + * same state as the C++ reference implementation. This overload is a 64-bit-seed extension: it + * copies all 64 bits of the supplied {@code long} into both initial state words. Values outside + * that range have no corresponding C++ seed and initialise a different state from their + * low-32-bit value. + * + *
To interpret a Java {@code int} as the corresponding unsigned C++ seed, use
+ * {@link Integer#toUnsignedLong(int) Integer.toUnsignedLong(seed)}. For a seed in the C++
+ * domain, this function returns both 64-bit output words of the C++ result.
+ *
+ * @param seed the seed value; all 64 bits initialise both internal state words
+ * @return a {@code LongTupleHashFunction} returning both words of the MurmurHash3 128-bit result
* @see #murmur_3()
+ * @see LongHashFunction#murmur_3(long)
*/
@NotNull
public static LongTupleHashFunction murmur_3(final long seed) {
diff --git a/src/test/java/net/openhft/hashing/Murmur3SeedCompatibilityTest.java b/src/test/java/net/openhft/hashing/Murmur3SeedCompatibilityTest.java
new file mode 100644
index 0000000..abe27d1
--- /dev/null
+++ b/src/test/java/net/openhft/hashing/Murmur3SeedCompatibilityTest.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright 2013-2025 chronicle.software; SPDX-License-Identifier: Apache-2.0
+ */
+package net.openhft.hashing;
+
+import org.junit.Test;
+
+import java.nio.charset.StandardCharsets;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+
+/**
+ * Known-answer tests for the seed boundary between the C++ {@code uint32_t} contract and this
+ * library's 64-bit extension.
+ */
+public class Murmur3SeedCompatibilityTest {
+
+ private static final byte[] DATA = "The quick brown fox jumps over the lazy dog"
+ .getBytes(StandardCharsets.UTF_8);
+
+ // Austin Appleby's smhasher, revision 07bb4de10a63e8cc2e1724865454eba635742383.
+ // See src/test/resources/murmur3-seed-vectors.cpp for the generator and compile command.
+ private static final long[] CPP_SEED_80000000 = {
+ 0xace3941990e3b4e7L,
+ 0xda0d7c42fa300cd4L
+ };
+
+ private static final long[] CPP_SEED_FFFFFFFF = {
+ 0x691c1d73a800a18aL,
+ 0x647d67096440b412L
+ };
+
+ private static final long[] EXTENDED_SEED_MINUS_ONE = {
+ 0x7ddc2cae3c8061ceL,
+ 0xb3aa26abb775cf00L
+ };
+
+ @Test
+ public void unsignedJavaIntMinimumMatchesCppReference() {
+ final int seed = Integer.MIN_VALUE;
+ assertBothApis(Integer.toUnsignedLong(seed), CPP_SEED_80000000);
+ }
+
+ @Test
+ public void unsignedJavaIntMinusOneMatchesCppReference() {
+ final int seed = -1;
+ assertBothApis(Integer.toUnsignedLong(seed), CPP_SEED_FFFFFFFF);
+ }
+
+ @Test
+ public void widenedNegativeIntUsesDocumented64BitExtension() {
+ final int seed = -1;
+ final long widenedSeed = seed;
+ assertBothApis(widenedSeed, EXTENDED_SEED_MINUS_ONE);
+ assertNotEquals(
+ LongHashFunction.murmur_3(Integer.toUnsignedLong(seed)).hashBytes(DATA),
+ LongHashFunction.murmur_3(widenedSeed).hashBytes(DATA));
+ }
+
+ @Test
+ public void negativeIntCallRetainsLongSeedSemantics() {
+ final int seed = -1;
+ assertEquals(EXTENDED_SEED_MINUS_ONE[0],
+ LongHashFunction.murmur_3(seed).hashBytes(DATA));
+ assertArrayEquals(EXTENDED_SEED_MINUS_ONE,
+ LongTupleHashFunction.murmur_3(seed).hashBytes(DATA));
+ }
+
+ private static void assertBothApis(final long seed, final long[] expected) {
+ assertArrayEquals(expected, LongTupleHashFunction.murmur_3(seed).hashBytes(DATA));
+ assertEquals(expected[0], LongHashFunction.murmur_3(seed).hashBytes(DATA));
+ }
+}
diff --git a/src/test/resources/murmur3-seed-vectors.cpp b/src/test/resources/murmur3-seed-vectors.cpp
new file mode 100644
index 0000000..7cfb474
--- /dev/null
+++ b/src/test/resources/murmur3-seed-vectors.cpp
@@ -0,0 +1,24 @@
+/*
+ * Copyright 2013-2025 chronicle.software; SPDX-License-Identifier: Apache-2.0
+ *
+ * From the repository root:
+ * git clone https://github.com/aappleby/smhasher.git /tmp/smhasher-seeds
+ * git -C /tmp/smhasher-seeds checkout 07bb4de10a63e8cc2e1724865454eba635742383
+ * c++ -std=c++11 -I/tmp/smhasher-seeds/src /tmp/smhasher-seeds/src/MurmurHash3.cpp \
+ * src/test/resources/murmur3-seed-vectors.cpp -o /tmp/murmur3-seed-vectors
+ * /tmp/murmur3-seed-vectors
+ */
+#include "MurmurHash3.h"
+#include