[libcxx-commits] [libcxx] 0da59bb - [libc++] Fix wrong implementation of CityHash

via libcxx-commits libcxx-commits at lists.llvm.org
Wed Oct 5 13:33:04 PDT 2022


Author: oToToT
Date: 2022-10-06T04:32:04+08:00
New Revision: 0da59bb8650a37dd6c45d5f7c204634e009c1fc9

URL: https://github.com/llvm/llvm-project/commit/0da59bb8650a37dd6c45d5f7c204634e009c1fc9
DIFF: https://github.com/llvm/llvm-project/commit/0da59bb8650a37dd6c45d5f7c204634e009c1fc9.diff

LOG: [libc++] Fix wrong implementation of CityHash

As PR56606 stated, the current implementation of CityHash in libc++
would drop some bits unintentionally. Cast the 32bit int to the 64bit
int to avoid this happened.

Reviewed By: ldionne, #libc

Differential Revision: https://reviews.llvm.org/D134124

Added: 
    libcxx/test/libcxx/utilities/utility/__murmur2_or_cityhash.pass.cpp

Modified: 
    libcxx/include/__functional/hash.h

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__functional/hash.h b/libcxx/include/__functional/hash.h
index c1212778868c4..041730dfb6b11 100644
--- a/libcxx/include/__functional/hash.h
+++ b/libcxx/include/__functional/hash.h
@@ -135,7 +135,7 @@ struct __murmur2_or_cityhash<_Size, 64>
     if (__len >= 4) {
       const uint32_t __a = __loadword<uint32_t>(__s);
       const uint32_t __b = __loadword<uint32_t>(__s + __len - 4);
-      return __hash_len_16(__len + (__a << 3), __b);
+      return __hash_len_16(__len + (static_cast<_Size>(__a) << 3), __b);
     }
     if (__len > 0) {
       const unsigned char __a = static_cast<unsigned char>(__s[0]);

diff  --git a/libcxx/test/libcxx/utilities/utility/__murmur2_or_cityhash.pass.cpp b/libcxx/test/libcxx/utilities/utility/__murmur2_or_cityhash.pass.cpp
new file mode 100644
index 0000000000000..d87ddc2a137f2
--- /dev/null
+++ b/libcxx/test/libcxx/utilities/utility/__murmur2_or_cityhash.pass.cpp
@@ -0,0 +1,36 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// Test the CityHash implementation is correct.
+
+// UNSUPPORTED: c++03
+
+#include <cassert>
+#include <string>
+#include <utility>
+
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+#  define CHOOSE_BY_ENDIANESS(little, big) (little)
+#else
+#  define CHOOSE_BY_ENDIANESS(little, big) (big)
+#endif
+
+int main(int, char**) {
+  const std::pair<std::string, uint64_t> TestCases[] = {
+      {"abcdefgh", CHOOSE_BY_ENDIANESS(0x4382a8d0fe8edb17ULL, 0xca84e809bef16fbcULL)},
+      {"abcDefgh", CHOOSE_BY_ENDIANESS(0xecefb080a6854061ULL, 0xd7feb824250272dcULL)},
+      {"CityHash", CHOOSE_BY_ENDIANESS(0x169ea3aebf908d6dULL, 0xea8cef3ca6f6e368ULL)},
+      {"CitYHash", CHOOSE_BY_ENDIANESS(0xe18298a2760f09faULL, 0xf33a7700bb7a94a8ULL)},
+  };
+
+  std::__murmur2_or_cityhash<uint64_t> h64;
+  for (const auto& test_case : TestCases) {
+    assert(h64(test_case.first.data(), test_case.first.size()) == test_case.second);
+  }
+  return 0;
+}


        


More information about the libcxx-commits mailing list