Fix HashBuffer to include sizeof(T) in size passed to MurmurHash64A.

Updated callers in test code that weren't expecting this. (Callers in
existing code were expecting it but not getting it!)

Fixes #487.
This commit is contained in:
Matt Pharr 2025-10-22 13:41:35 -07:00
parent 85918d59e4
commit 0d6d9573e9
2 changed files with 4 additions and 4 deletions

View file

@ -77,8 +77,8 @@ inline uint64_t MixBits(uint64_t v) {
}
template <typename T>
PBRT_CPU_GPU inline uint64_t HashBuffer(const T *ptr, size_t size, uint64_t seed = 0) {
return MurmurHash64A((const unsigned char *)ptr, size, seed);
PBRT_CPU_GPU inline uint64_t HashBuffer(const T *ptr, size_t nElements, uint64_t seed = 0) {
return MurmurHash64A((const unsigned char *)ptr, nElements * sizeof(T), seed);
}
template <typename... Args>

View file

@ -13,7 +13,7 @@ using namespace pbrt;
TEST(Hash, VarArgs) {
int64_t buf[] = {1, -12511, 31415821, 37};
for (int i = 0; i < 4; ++i)
EXPECT_EQ(HashBuffer(buf + i, sizeof(int64_t)), Hash(buf[i]));
EXPECT_EQ(HashBuffer(buf + i, 1), Hash(buf[i]));
}
TEST(Hash, Collisions) {
@ -50,6 +50,6 @@ TEST(Hash, Unaligned) {
char cbuf[sizeof(buf) + 8];
for (int delta = 0; delta < 8; ++delta) {
memcpy(cbuf + delta, buf, sizeof(buf));
EXPECT_EQ(HashBuffer(buf, sizeof(buf)), HashBuffer(cbuf + delta, sizeof(buf)));
EXPECT_EQ(HashBuffer(buf, 3), HashBuffer(cbuf + delta, sizeof(buf)));
}
}