[libc-commits] [libc] [libc] [search] implement hcreate(_r)/hsearch(_r)/hdestroy(_r) (PR #73469)

Schrodinger ZHU Yifan via libc-commits libc-commits at lists.llvm.org
Mon Nov 27 16:19:58 PST 2023


================
@@ -0,0 +1,162 @@
+//===-- Portable string hash function ---------------------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_HASH_H
+#define LLVM_LIBC_SRC___SUPPORT_HASH_H
+
+#include "src/__support/UInt128.h" // UInt128
+#include "src/__support/macros/attributes.h"
+#include <stdint.h> // For uint64_t
+
+namespace LIBC_NAMESPACE {
+namespace internal {
+
+// Folded multiplication.
+// This function multiplies two 64-bit integers and xor the high and
+// low 64-bit parts of the result.
+LIBC_INLINE uint64_t folded_multiply(uint64_t x, uint64_t y) {
+  UInt128 mask = static_cast<UInt128>(0xffffffffffffffff);
+  UInt128 p = static_cast<UInt128>(x) * static_cast<UInt128>(y);
+  uint64_t low = static_cast<uint64_t>(p & mask);
+  uint64_t high = static_cast<uint64_t>(p >> 64);
+  return low ^ high;
+}
+
+// Read as little endian.
+// Shift-and-or implementation does not give a satisfactory code on aarch64.
+// Therefore, we use a union to read the value.
+template <typename T> LIBC_INLINE T read_little_endian(const void *ptr) {
+  const uint8_t *bytes = static_cast<const uint8_t *>(ptr);
+  union {
+    T value;
+    uint8_t buffer[sizeof(T)];
+  } data;
+#if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
+  // Compiler should able to optimize this as a load followed by a byte swap.
+  for (size_t i = 0; i < sizeof(T); ++i) {
+    data.buffer[i] = bytes[sizeof(T) - i - 1];
+  }
+#else
+  for (size_t i = 0; i < sizeof(T); ++i) {
+    data.buffer[i] = bytes[i];
+  }
+#endif
+  return data.value;
+}
+
+// Specialized read functions for small values. size must be <= 8.
+LIBC_INLINE void read_small_values(const void *ptr, size_t size, uint64_t &low,
+                                   uint64_t &high) {
+  const uint8_t *bytes = static_cast<const uint8_t *>(ptr);
+  if (size >= 2) {
+    if (size >= 4) {
+      low = static_cast<uint64_t>(read_little_endian<uint32_t>(&bytes[0]));
+      high =
+          static_cast<uint64_t>(read_little_endian<uint32_t>(&bytes[size - 4]));
+    } else {
+      low = static_cast<uint64_t>(read_little_endian<uint16_t>(&bytes[0]));
+      high = static_cast<uint64_t>(bytes[size - 1]);
+    }
+  } else {
+    if (size > 0) {
+      low = static_cast<uint64_t>(bytes[0]);
+      high = static_cast<uint64_t>(bytes[0]);
+    } else {
+      low = 0;
+      high = 0;
+    }
+  }
+}
+
+// This constant comes from Kunth's prng (it empirically works well).
+LIBC_INLINE_VAR constexpr uint64_t MULTIPLE = 6364136223846793005;
+// Rotation amount for mixing.
+LIBC_INLINE_VAR constexpr uint64_t ROTATE = 23;
+
+// Randomly generated values (for now, it uses the same values as in aHash).
+LIBC_INLINE_VAR constexpr uint64_t RANDOMNESS[2][4] = {
+    {0x243f6a8885a308d3, 0x13198a2e03707344, 0xa4093822299f31d0,
+     0x082efa98ec4e6c89},
+    {0x452821e638d01377, 0xbe5466cf34e90c6c, 0xc0ac29b7c97c50dd,
+     0x3f84d5b5b5470917},
+};
+
+LIBC_INLINE uint64_t rotate_left(uint64_t x, uint64_t y) {
+  return (x << y) | (x >> (64 - y));
----------------
SchrodingerZhu wrote:

Cited.
Updated to a more general version in `bit.h`

https://github.com/llvm/llvm-project/pull/73469


More information about the libc-commits mailing list