[libcxx-commits] [PATCH] D134124: [libc++] Fix wrong implementation of CityHash

Tommy Chiang via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Mon Sep 19 11:48:38 PDT 2022


oToToT updated this revision to Diff 461290.
oToToT added a comment.

Add basic test cases.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D134124/new/

https://reviews.llvm.org/D134124

Files:
  libcxx/include/__functional/hash.h
  libcxx/test/libcxx/utilities/utility/__murmur2_or_cityhash.pass.cpp


Index: libcxx/test/libcxx/utilities/utility/__murmur2_or_cityhash.pass.cpp
===================================================================
--- /dev/null
+++ libcxx/test/libcxx/utilities/utility/__murmur2_or_cityhash.pass.cpp
@@ -0,0 +1,30 @@
+//===----------------------------------------------------------------------===//
+//
+// 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>
+
+int main(int, char**) {
+  const std::pair<std::string, uint64_t> TestCases[] = {
+      {"abcdefgh", 0x4382a8d0fe8edb17ULL},
+      {"abcDefgh", 0xecefb080a6854061ULL},
+      {"CityHash", 0x169ea3aebf908d6dULL},
+      {"CitYHash", 0xe18298a2760f09faULL},
+  };
+
+  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;
+}
Index: libcxx/include/__functional/hash.h
===================================================================
--- libcxx/include/__functional/hash.h
+++ libcxx/include/__functional/hash.h
@@ -135,7 +135,7 @@
     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]);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D134124.461290.patch
Type: text/x-patch
Size: 1804 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20220919/43d57e01/attachment.bin>


More information about the libcxx-commits mailing list