[libc-commits] [libc] [llvm] [libc][math] Refactor log10, log1p, log2 implementation to header-only in src/__support/math folder. (PR #176089)

Muhammad Bassiouni via libc-commits libc-commits at lists.llvm.org
Thu Jan 15 22:58:19 PST 2026


================
@@ -0,0 +1,1068 @@
+//===-- Double-precision log1p(x) function --------------------------------===//
+//
+// 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_MATH_LOG1P_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_LOG1P_H
+
+#include "common_constants.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/double_double.h"
+#include "src/__support/FPUtil/dyadic_float.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/common.h"
+#include "src/__support/integer_literals.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+namespace log1p_internal {
+// 128-bit precision dyadic floating point numbers.
+using Float128 = typename fputil::DyadicFloat<128>;
+
+using LIBC_NAMESPACE::operator""_u128;
+
+using namespace common_constants_internal;
+
+// R1[i] = 2^-8 * nearestint( 2^8 / (1 + i * 2^-7) )
+constexpr double R1[129] = {
+    0x1p0,     0x1.fcp-1, 0x1.f8p-1, 0x1.f4p-1, 0x1.fp-1,  0x1.ecp-1, 0x1.eap-1,
+    0x1.e6p-1, 0x1.e2p-1, 0x1.dep-1, 0x1.dap-1, 0x1.d8p-1, 0x1.d4p-1, 0x1.dp-1,
+    0x1.cep-1, 0x1.cap-1, 0x1.c8p-1, 0x1.c4p-1, 0x1.cp-1,  0x1.bep-1, 0x1.bap-1,
+    0x1.b8p-1, 0x1.b4p-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-1,
+    0x1.0ap-1, 0x1.08p-1, 0x1.08p-1, 0x1.06p-1, 0x1.06p-1, 0x1.04p-1, 0x1.04p-1,
+    0x1.02p-1, 0x1.02p-1, 0x1p-1,
+};
+
+// Extra constants for exact range reduction when FMA instructions are not
+// available:
+// r * c - 1 for r = 2^-8 * nearestint( 2^8 / (1 + i * 2^-7))
+//           and c = 1 + i * 2^-7
+//           with i = 0..128.
+[[maybe_unused]] constexpr double RCM1[129] = {
----------------
bassiounix wrote:

```suggestion
[[maybe_unused]] LIBC_INLINE_VAR constexpr double RCM1[129] = {
```

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


More information about the libc-commits mailing list