[libc-commits] [libc] [llvm] [libc][math][NFC] Split log*f into *_double_eval.h and dispatch headers. (PR #240167)
Hans Wennborg via libc-commits
libc-commits at lists.llvm.org
Wed Nov 11 03:45:02 PST 2026
================
@@ -0,0 +1,197 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Double-precision implementation of logf.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_LOGF_DOUBLE_EVAL_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_LOGF_DOUBLE_EVAL_H
+
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+#include "src/__support/macros/properties/cpu_features.h"
+#include "src/__support/math/common_constants.h" // Lookup table for (1/f) and log(f)
+
+// This is an algorithm for log(x) in single precision which is correctly
+// rounded for all rounding modes, based on the implementation of log(x) from
+// the RLIBM project at:
+// https://people.cs.rutgers.edu/~sn349/rlibm
+
+// Step 1 - Range reduction:
+// For x = 2^m * 1.mant, log(x) = m * log(2) + log(1.m)
+// If x is denormal, we normalize it by multiplying x by 2^23 and subtracting
+// m by 23.
+
+// Step 2 - Another range reduction:
+// To compute log(1.mant), let f be the highest 8 bits including the hidden
+// bit, and d be the difference (1.mant - f), i.e. the remaining 16 bits of the
+// mantissa. Then we have the following approximation formula:
+// log(1.mant) = log(f) + log(1.mant / f)
+// = log(f) + log(1 + d/f)
+// ~ log(f) + P(d/f)
+// since d/f is sufficiently small.
+// log(f) and 1/f are then stored in two 2^7 = 128 entries look-up tables.
+
+// Step 3 - Polynomial approximation:
+// To compute P(d/f), we use a single degree-5 polynomial in double precision
+// which provides correct rounding for all but few exception values.
+// For more detail about how this polynomial is obtained, please refer to the
+// paper:
+// Lim, J. and Nagarakatte, S., "One Polynomial Approximation to Produce
+// Correctly Rounded Results of an Elementary Function for Multiple
+// Representations and Rounding Modes", Proceedings of the 49th ACM SIGPLAN
+// Symposium on Principles of Programming Languages (POPL-2022), Philadelphia,
+// USA, January 16-22, 2022.
+// https://people.cs.rutgers.edu/~sn349/papers/rlibmall-popl-2022.pdf
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+namespace double_eval {
+
+LIBC_INLINE float logf(float x) {
+ using namespace common_constants_internal;
+ constexpr double LOG_2 = 0x1.62e42fefa39efp-1;
----------------
zmodem wrote:
This now shadows the DoubleDouble `math::double_eval::LOG_2` variable, causing -Wshadow warnings for us:
```
In file included from ../../v8/src/base/ieee754.cc:28:
In file included from ../../third_party/llvm-libc/src/shared/math.h:371:
In file included from ../../third_party/llvm-libc/src/shared/math/logf.h:13:
In file included from ../../third_party/llvm-libc/src/src/__support/math/logf.h:26:
../../third_party/llvm-libc/src/src/__support/math/logf_double_eval.h:66:20: error: declaration shadows a variable in namespace '__llvm_libc_cr::math::double_eval' [-Werror,-Wshadow]
66 | constexpr double LOG_2 = 0x1.62e42fefa39efp-1;
| ^
../../third_party/llvm-libc/src/src/__support/math/exp_utils.h:95:40: note: previous declaration is here
95 | LIBC_INLINE_VAR constexpr DoubleDouble LOG_2 = {0x1.abc9e3b39803fp-56,
|
```
Perhaps one of them could be renamed to avoid the conflict?
https://github.com/llvm/llvm-project/pull/240167
More information about the libc-commits
mailing list