[llvm-branch-commits] [libc] [libc][wctype] Upstream fastrand from PtrHash-cc prototype to LLVM libc (PR #174851)
Schrodinger ZHU Yifan via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Tue Jan 13 16:46:38 PST 2026
================
@@ -0,0 +1,78 @@
+//===-- Fast random number gen method - wctype conversion -------*- 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_WCTYPE_CONVERSION_RANDOM_FASTRAND_H
+#define LLVM_LIBC_SRC___SUPPORT_WCTYPE_CONVERSION_RANDOM_FASTRAND_H
+
+#include "src/__support/wctype/conversion/utils/utils.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace wctype_internal {
+
+namespace random {
+
+namespace fastrand {
+
+// This seed value is very important for different inputs. Bad values are known
+// to cause compilation errors and/or incorrect computations in some cases.
+// Defaulted to `0xEF6F79ED30BA75A` at first, but this value is not sufficient.
+// Candidate seeds are taken directly from the resulted computation of the
+// original Rust code, this is important for reproduction until a parallel
+// generator is written for LLVM libc!
+// `0x64a727ea04c46a32` is another viable seed.
+LIBC_INLINE_VAR constexpr uint64_t DEFAULT_RNG_SEED = 0xeec13c9f1362aa74;
+
+class Rng {
+public:
+ LIBC_INLINE constexpr Rng() : Rng(DEFAULT_RNG_SEED) {}
+
+ LIBC_INLINE constexpr Rng(uint64_t seed) : seed(seed) {}
+ LIBC_INLINE constexpr Rng(const Rng &) = default;
+ LIBC_INLINE constexpr Rng(Rng &&) = default;
+
+ LIBC_INLINE constexpr Rng &operator=(const Rng &) = default;
+ LIBC_INLINE constexpr Rng &operator=(Rng &&) = default;
+
+ LIBC_INLINE constexpr uint64_t gen() const {
+ constexpr uint64_t WY_CONST_0 = 0x2d35'8dcc'aa6c'78a5;
----------------
SchrodingerZhu wrote:
Better explain the requirement for seeds and how they were initially generated/selected
https://github.com/llvm/llvm-project/pull/174851
More information about the llvm-branch-commits
mailing list