[libc-commits] [libc] [libc][math][c23] Add log10p1f16 C23 math function (PR #184739)
Shikhar Soni via libc-commits
libc-commits at lists.llvm.org
Wed Mar 11 22:59:00 PDT 2026
https://github.com/shikharish updated https://github.com/llvm/llvm-project/pull/184739
>From d22981f73cd52a9436a646aed442ad3b383fc20e Mon Sep 17 00:00:00 2001
From: Shikhar Soni <shikharish05 at gmail.com>
Date: Thu, 5 Mar 2026 10:35:59 +0530
Subject: [PATCH 1/2] [libc][math][c23] Add log10p1f16 C23 math function
Signed-off-by: Shikhar Soni <shikharish05 at gmail.com>
---
libc/config/baremetal/aarch64/entrypoints.txt | 1 +
libc/config/baremetal/arm/entrypoints.txt | 1 +
libc/config/baremetal/riscv/entrypoints.txt | 1 +
libc/config/darwin/aarch64/entrypoints.txt | 1 +
libc/config/gpu/amdgpu/entrypoints.txt | 1 +
libc/config/gpu/nvptx/entrypoints.txt | 1 +
libc/config/linux/riscv/entrypoints.txt | 1 +
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/docs/headers/math/index.rst | 2 +-
libc/include/math.yaml | 7 +
libc/src/__support/math/CMakeLists.txt | 19 ++
libc/src/__support/math/log10p1f16.h | 190 ++++++++++++++++++
libc/src/math/CMakeLists.txt | 1 +
libc/src/math/generic/CMakeLists.txt | 11 +
libc/src/math/generic/log10p1f16.cpp | 18 ++
libc/src/math/log10p1f16.h | 21 ++
libc/test/src/math/CMakeLists.txt | 11 +
libc/test/src/math/log10p1f16_test.cpp | 49 +++++
libc/test/src/math/smoke/CMakeLists.txt | 13 ++
libc/test/src/math/smoke/log10p1f16_test.cpp | 50 +++++
libc/utils/MPFRWrapper/MPCommon.cpp | 16 ++
libc/utils/MPFRWrapper/MPCommon.h | 1 +
libc/utils/MPFRWrapper/MPFRUtils.cpp | 2 +
libc/utils/MPFRWrapper/MPFRUtils.h | 1 +
24 files changed, 419 insertions(+), 1 deletion(-)
create mode 100644 libc/src/__support/math/log10p1f16.h
create mode 100644 libc/src/math/generic/log10p1f16.cpp
create mode 100644 libc/src/math/log10p1f16.h
create mode 100644 libc/test/src/math/log10p1f16_test.cpp
create mode 100644 libc/test/src/math/smoke/log10p1f16_test.cpp
diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index 4e720a234d471..3e62cff3470bc 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -649,6 +649,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.llrintf16
libc.src.math.llroundf16
libc.src.math.log10f16
+ libc.src.math.log10p1f16
libc.src.math.log2f16
libc.src.math.logbf16
libc.src.math.logf16
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index f45ee7a9052f4..11dc4db0a49a8 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -656,6 +656,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.llrintf16
libc.src.math.llroundf16
libc.src.math.log10f16
+ libc.src.math.log10p1f16
libc.src.math.log2f16
libc.src.math.logbf16
libc.src.math.logf16
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index 76e10e7d26a0a..df01cbd854a47 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -651,6 +651,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.llrintf16
libc.src.math.llroundf16
libc.src.math.log10f16
+ libc.src.math.log10p1f16
libc.src.math.log2f16
libc.src.math.logbf16
libc.src.math.logf16
diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index 4f5fc6831dd3f..b1614a016e6e4 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -467,6 +467,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.llrintf16
libc.src.math.llroundf16
libc.src.math.log10f16
+ libc.src.math.log10p1f16
libc.src.math.log2f16
libc.src.math.logbf16
libc.src.math.logf16
diff --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt
index 0441207ace96b..6c0a011c97306 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -581,6 +581,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.llrintf16
libc.src.math.llroundf16
libc.src.math.log10f16
+ libc.src.math.log10p1f16
libc.src.math.log2f16
libc.src.math.logbf16
libc.src.math.logf16
diff --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index f127ba6358b43..865221b3146e4 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -583,6 +583,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.llrintf16
libc.src.math.llroundf16
libc.src.math.log10f16
+ libc.src.math.log10p1f16
libc.src.math.log2f16
libc.src.math.logbf16
libc.src.math.logf16
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 1a4e85292c517..983027b41ec1f 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -742,6 +742,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.llrintf16
libc.src.math.llroundf16
libc.src.math.log10f16
+ libc.src.math.log10p1f16
libc.src.math.log2f16
libc.src.math.logbf16
libc.src.math.logf16
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 57d482dba9271..68ad9c65bf737 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -791,6 +791,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.llrintf16
libc.src.math.llroundf16
libc.src.math.log10f16
+ libc.src.math.log10p1f16
libc.src.math.log2f16
libc.src.math.logbf16
libc.src.math.logf16
diff --git a/libc/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst
index ee2b6d9b7bd16..b1795e2a8d233 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -323,7 +323,7 @@ Higher Math Functions
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
| log10 | |check| | |check| | | |check| | | | 7.12.6.12 | F.10.3.12 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| log10p1 | | | | | | | 7.12.6.13 | F.10.3.13 |
+| log10p1 | | | | |check| | | | 7.12.6.13 | F.10.3.13 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
| log1p | |check| | |check| | | | | | 7.12.6.14 | F.10.3.14 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/include/math.yaml b/libc/include/math.yaml
index afd3ae33305c1..3a5c962020bc7 100644
--- a/libc/include/math.yaml
+++ b/libc/include/math.yaml
@@ -1777,6 +1777,13 @@ functions:
arguments:
- type: _Float16
guard: LIBC_TYPES_HAS_FLOAT16
+ - name: log10p1f16
+ standards:
+ - stdc
+ return_type: _Float16
+ arguments:
+ - type: _Float16
+ guard: LIBC_TYPES_HAS_FLOAT16
- name: log1p
standards:
- stdc
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 315cc1069c1d2..b198dc336170c 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -2586,6 +2586,25 @@ add_header_library(
libc.src.__support.macros.properties.cpu_features
)
+add_header_library(
+ log10p1f16
+ HDRS
+ log10p1f16.h
+ DEPENDS
+ .exp10_float16_constants
+ .expxf16_utils
+ libc.hdr.errno_macros
+ libc.hdr.fenv_macros
+ libc.src.__support.FPUtil.cast
+ libc.src.__support.FPUtil.except_value_utils
+ libc.src.__support.FPUtil.fenv_impl
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.FPUtil.multiply_add
+ libc.src.__support.FPUtil.polyeval
+ libc.src.__support.macros.optimization
+ libc.src.__support.macros.properties.cpu_features
+)
+
add_header_library(
log10f
HDRS
diff --git a/libc/src/__support/math/log10p1f16.h b/libc/src/__support/math/log10p1f16.h
new file mode 100644
index 0000000000000..e2b01a504975d
--- /dev/null
+++ b/libc/src/__support/math/log10p1f16.h
@@ -0,0 +1,190 @@
+//===-- Implementation header for log10p1f16 ---------------------*- 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_MATH_LOG10P1F16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_LOG10P1F16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "exp10_float16_constants.h"
+#include "expxf16_utils.h"
+#include "hdr/errno_macros.h"
+#include "hdr/fenv_macros.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/cast.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"
+#include "src/__support/macros/properties/cpu_features.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+namespace log10p1f16_internal {
+#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+
+// ExceptValues for the small-|x| polynomial path (|x| <= 2^-3).
+LIBC_INLINE_VAR constexpr size_t N_LOG10P1F16_EXCEPTS = 3;
+
+LIBC_INLINE_VAR constexpr fputil::ExceptValues<float16, N_LOG10P1F16_EXCEPTS>
+ LOG10P1F16_EXCEPTS = {{
+ // (input, RZ output, RU offset, RD offset, RN offset)
+ // x = 0x1.ep-13
+ {0x0B80U, 0x0683U, 1U, 0U, 0U},
+ // x = -0x1.b8p-13
+ {0x89DCU, 0x8516U, 0U, 1U, 1U},
+ // x = -0x1.42p-8
+ {0x9D08U, 0x9861U, 0U, 1U, 0U},
+ }};
+
+// ExceptValues for the large-|x| table-based path (|x| > 2^-3).
+LIBC_INLINE_VAR constexpr size_t N_LOG10P1F16_EXCEPTS_HI = 6;
+
+LIBC_INLINE_VAR constexpr fputil::ExceptValues<float16,
+ N_LOG10P1F16_EXCEPTS_HI>
+ LOG10P1F16_EXCEPTS_HI = {{
+ // (input, RZ output, RU offset, RD offset, RN offset)
+ // x = 0x1.4c4p-2, log10p1f16(x) = 0x1.f3cp-4 (RZ)
+ {0x3531U, 0x2FCFU, 1U, 0U, 0U},
+ // x = 0x1.2p+3, log10p1f16(x) = 0x1p+0 (RZ)
+ {0x4880U, 0x3C00U, 0U, 0U, 0U},
+ // x = 0x1.8cp+6, log10p1f16(x) = 0x1p+1 (RZ)
+ {0x5630U, 0x4000U, 0U, 0U, 0U},
+ // x = 0x1.f44p+6, log10p1f16(x) = 0x1.0ccp+1 (RZ)
+ {0x57D1U, 0x4033U, 1U, 0U, 0U},
+ // x = 0x1.f38p+9, log10p1f16(x) = 0x1.8p+1 (RZ)
+ {0x63CEU, 0x4200U, 0U, 0U, 0U},
+ // x = -0x1.808p-1, log10p1f16(x) = -0x1.354p-1 (RZ)
+ {0xBA02U, 0xB8D4U, 0U, 1U, 1U},
+ }};
+#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+} // namespace log10p1f16_internal
+
+LIBC_INLINE float16 log10p1f16(float16 x) {
+ using namespace math::expxf16_internal;
+ using FPBits = fputil::FPBits<float16>;
+ FPBits x_bits(x);
+
+ uint16_t x_u = x_bits.uintval();
+ uint16_t x_abs = x_u & 0x7fffU;
+
+ // If x is +-0, +inf, NaN, -1, x < -1, or |x| <= 2^-3.
+ if (LIBC_UNLIKELY(x_abs == 0U || x_abs <= 0x3000U || x_u == 0x7c00U ||
+ x_u >= 0xbc00U || x_bits.is_nan())) {
+ // log10p1(NaN) = NaN
+ if (x_bits.is_nan()) {
+ if (x_bits.is_signaling_nan()) {
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+
+ return x;
+ }
+
+ // log10p1(+/-0) = +/-0
+ if (x_abs == 0U)
+ return x;
+
+ // log10p1(+inf) = +inf
+ if (x_u == 0x7c00U)
+ return FPBits::inf().get_val();
+
+ // log10p1(-1) = -inf
+ if (x_u == 0xbc00U) {
+ fputil::raise_except_if_required(FE_DIVBYZERO);
+ return FPBits::inf(Sign::NEG).get_val();
+ }
+
+ // log10p1(x) = NaN for x < -1
+ if (x_u > 0xbc00U) {
+ fputil::set_errno_if_required(EDOM);
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+
+ // When |x| <= 2^-3, use a polynomial approximation directly on x.
+ // For y = 1+x near 1, the table-based range reduction suffers from
+ // catastrophic cancellation (m*log10(2) + log10(f) nearly cancel).
+ // Computing log10(1+x) directly via polynomial avoids this.
+ // log10(1+x) = x * (c0 + c1*x + c2*x^2 + c3*x^3 + c4*x^4 + c5*x^5 +
+ // c6*x^6)
+ // where ck = (-1)^k / ((k+1) * ln(10)), the Taylor coefficients of
+ // log10(1+x)/x.
+ if (x_abs <= 0x3000U) {
+#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+ if (auto r = log10p1f16_internal::LOG10P1F16_EXCEPTS.lookup(x_u);
+ LIBC_UNLIKELY(r.has_value()))
+ return r.value();
+#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+
+ float xf = x;
+ return fputil::cast<float16>(
+ xf * fputil::polyeval(xf, 0x1.bcb7b2p-2f, -0x1.bcb7b2p-3f,
+ 0x1.287a76p-3f, -0x1.bcb7b2p-4f,
+ 0x1.63c628p-4f, -0x1.287a76p-4f,
+ 0x1.fc3fa6p-5f));
+ }
+ }
+
+#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+ if (auto r = log10p1f16_internal::LOG10P1F16_EXCEPTS_HI.lookup(x_u);
+ LIBC_UNLIKELY(r.has_value()))
+ return r.value();
+#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+
+ // For the range reduction, we compute y = 1 + x in float. For |x| > 2^-3,
+ // the addition 1.0f + xf is exact in float, and the result y is bounded away
+ // from 1.0, avoiding catastrophic cancellation in the table decomposition.
+ float xf = x;
+ float y = 1.0f + xf;
+
+ using FPBitsFloat = fputil::FPBits<float>;
+ FPBitsFloat y_bits(y);
+
+ // When y is subnormal or zero, which shouldn't happen since y = 1 + x and
+ // x >= -1 + eps in f16, so y >= eps > 0. But handle y = 0 just in case.
+ if (LIBC_UNLIKELY(y_bits.is_zero()))
+ return FPBits::inf(Sign::NEG).get_val();
+
+ int m = y_bits.get_exponent();
+ // Set y_bits to 1.mant (biased exponent = 127).
+ y_bits.set_biased_exponent(FPBitsFloat::EXP_BIAS);
+ float mant_f = y_bits.get_val();
+
+ // Leading 23 - 5 = 18, so top 5 mantissa bits give index f in [0, 31].
+ int f = y_bits.get_mantissa() >> (FPBitsFloat::FRACTION_LEN - 5);
+
+ // v = 1.mant * 1/f - 1 = d/f
+ float v = fputil::multiply_add(mant_f, ONE_OVER_F_F[f], -1.0f);
+
+ // Degree-3 minimax polynomial generated by Sollya with the following
+ // commands:
+ // > display = hexadecimal;
+ // > P = fpminimax(log10(1 + x)/x, 2, [|SG...|], [-2^-5, 2^-5]);
+ // > x * P;
+ float log10p1_d_over_f =
+ v * fputil::polyeval(v, 0x1.bcb7bp-2f, -0x1.bce168p-3f, 0x1.28acb8p-3f);
+ // log10(1.mant) = log10(f) + log10(1 + d/f)
+ float log10_1_mant = LOG10F_F[f] + log10p1_d_over_f;
+ return fputil::cast<float16>(
+ fputil::multiply_add(static_cast<float>(m), LOG10F_2, log10_1_mant));
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_LOG10P1F16_H
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 766feb0606c64..04a5cf48fd7b0 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -384,6 +384,7 @@ add_math_entrypoint_object(ldexpbf16)
add_math_entrypoint_object(log10)
add_math_entrypoint_object(log10f)
add_math_entrypoint_object(log10f16)
+add_math_entrypoint_object(log10p1f16)
add_math_entrypoint_object(log1p)
add_math_entrypoint_object(log1pf)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index b9de548c8bedb..1d616c04fdf70 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -1837,6 +1837,17 @@ add_entrypoint_object(
libc.src.errno.errno
)
+add_entrypoint_object(
+ log10p1f16
+ SRCS
+ log10p1f16.cpp
+ HDRS
+ ../log10p1f16.h
+ DEPENDS
+ libc.src.__support.math.log10p1f16
+ libc.src.errno.errno
+)
+
add_entrypoint_object(
log1p
SRCS
diff --git a/libc/src/math/generic/log10p1f16.cpp b/libc/src/math/generic/log10p1f16.cpp
new file mode 100644
index 0000000000000..3e3eaa631f12f
--- /dev/null
+++ b/libc/src/math/generic/log10p1f16.cpp
@@ -0,0 +1,18 @@
+//===-- Half-precision log10(1+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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/log10p1f16.h"
+#include "src/__support/math/log10p1f16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(float16, log10p1f16, (float16 x)) {
+ return math::log10p1f16(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/log10p1f16.h b/libc/src/math/log10p1f16.h
new file mode 100644
index 0000000000000..849ea9ad6c2a4
--- /dev/null
+++ b/libc/src/math/log10p1f16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for log10p1f16 ---------------------*- 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_MATH_LOG10P1F16_H
+#define LLVM_LIBC_SRC_MATH_LOG10P1F16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+float16 log10p1f16(float16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_LOG10P1F16_H
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 90afe842e9dec..be37adb4c0709 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -2178,6 +2178,17 @@ add_fp_unittest(
libc.src.math.log10f16
)
+add_fp_unittest(
+ log10p1f16_test
+ NEED_MPFR
+ SUITE
+ libc-math-unittests
+ SRCS
+ log10p1f16_test.cpp
+ DEPENDS
+ libc.src.math.log10p1f16
+)
+
add_fp_unittest(
log1p_test
NEED_MPFR
diff --git a/libc/test/src/math/log10p1f16_test.cpp b/libc/test/src/math/log10p1f16_test.cpp
new file mode 100644
index 0000000000000..d584f637b4166
--- /dev/null
+++ b/libc/test/src/math/log10p1f16_test.cpp
@@ -0,0 +1,49 @@
+//===-- Exhaustive test for log10p1f16 -------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/macros/optimization.h"
+#include "src/math/log10p1f16.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+#define TOLERANCE 1
+#else
+#define TOLERANCE 0
+#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+
+using LlvmLibcLog10p1f16Test = LIBC_NAMESPACE::testing::FPTest<float16>;
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+// Range: [0, Inf];
+static constexpr uint16_t POS_START = 0x0000U;
+static constexpr uint16_t POS_STOP = 0x7c00U;
+
+// Range: [-1, 0];
+static constexpr uint16_t NEG_START = 0x8000U;
+static constexpr uint16_t NEG_STOP = 0xbc00U;
+
+TEST_F(LlvmLibcLog10p1f16Test, PositiveRange) {
+ for (uint16_t v = POS_START; v <= POS_STOP; ++v) {
+ float16 x = FPBits(v).get_val();
+ EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log10p1, x,
+ LIBC_NAMESPACE::log10p1f16(x),
+ TOLERANCE + 0.5);
+ }
+}
+
+TEST_F(LlvmLibcLog10p1f16Test, NegativeRange) {
+ for (uint16_t v = NEG_START; v <= NEG_STOP; ++v) {
+ float16 x = FPBits(v).get_val();
+ EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log10p1, x,
+ LIBC_NAMESPACE::log10p1f16(x),
+ TOLERANCE + 0.5);
+ }
+}
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 185f646e3aa9c..9d4fe1abcd076 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -4442,6 +4442,19 @@ add_fp_unittest(
libc.src.__support.FPUtil.cast
)
+add_fp_unittest(
+ log10p1f16_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ log10p1f16_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.hdr.fenv_macros
+ libc.src.math.log10p1f16
+ libc.src.__support.FPUtil.cast
+)
+
add_fp_unittest(
log1p_test
SUITE
diff --git a/libc/test/src/math/smoke/log10p1f16_test.cpp b/libc/test/src/math/smoke/log10p1f16_test.cpp
new file mode 100644
index 0000000000000..3a27089582d3b
--- /dev/null
+++ b/libc/test/src/math/smoke/log10p1f16_test.cpp
@@ -0,0 +1,50 @@
+//===-- Unittests for log10p1f16 -------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/fenv_macros.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/math/log10p1f16.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcLog10p1f16Test = LIBC_NAMESPACE::testing::FPTest<float16>;
+
+TEST_F(LlvmLibcLog10p1f16Test, SpecialNumbers) {
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::log10p1f16(aNaN));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::log10p1f16(sNaN),
+ FE_INVALID);
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(inf, LIBC_NAMESPACE::log10p1f16(inf));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::log10p1f16(neg_inf));
+ EXPECT_MATH_ERRNO(EDOM);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(zero, LIBC_NAMESPACE::log10p1f16(zero));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(neg_zero, LIBC_NAMESPACE::log10p1f16(neg_zero));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(
+ neg_inf,
+ LIBC_NAMESPACE::log10p1f16(
+ LIBC_NAMESPACE::fputil::cast<float16>(-1.0)),
+ FE_DIVBYZERO);
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(
+ aNaN,
+ LIBC_NAMESPACE::log10p1f16(
+ LIBC_NAMESPACE::fputil::cast<float16>(-2.0)));
+ EXPECT_MATH_ERRNO(EDOM);
+}
diff --git a/libc/utils/MPFRWrapper/MPCommon.cpp b/libc/utils/MPFRWrapper/MPCommon.cpp
index 6b78bee6e7cae..47d15b3981efe 100644
--- a/libc/utils/MPFRWrapper/MPCommon.cpp
+++ b/libc/utils/MPFRWrapper/MPCommon.cpp
@@ -335,6 +335,22 @@ MPFRNumber MPFRNumber::log10() const {
return result;
}
+MPFRNumber MPFRNumber::log10p1() const {
+ // TODO: Only use mpfr_log10p1 once CI and buildbots get MPFR >= 4.2.0.
+#if MPFR_VERSION >= MPFR_VERSION_NUM(4, 2, 0)
+ MPFRNumber result(*this);
+ mpfr_log10p1(result.value, value, mpfr_rounding);
+ return result;
+#else
+ unsigned int prec = mpfr_precision * 3;
+ MPFRNumber result(*this, prec);
+ MPFRNumber one(1.0f, prec);
+ mpfr_add(result.value, value, one.value, mpfr_rounding);
+ mpfr_log10(result.value, result.value, mpfr_rounding);
+ return result;
+#endif
+}
+
MPFRNumber MPFRNumber::log1p() const {
MPFRNumber result(*this);
mpfr_log1p(result.value, value, mpfr_rounding);
diff --git a/libc/utils/MPFRWrapper/MPCommon.h b/libc/utils/MPFRWrapper/MPCommon.h
index 9f4107a7961d2..cb39735ca0bc6 100644
--- a/libc/utils/MPFRWrapper/MPCommon.h
+++ b/libc/utils/MPFRWrapper/MPCommon.h
@@ -214,6 +214,7 @@ class MPFRNumber {
MPFRNumber log() const;
MPFRNumber log2() const;
MPFRNumber log10() const;
+ MPFRNumber log10p1() const;
MPFRNumber log1p() const;
MPFRNumber pow(const MPFRNumber &b);
MPFRNumber remquo(const MPFRNumber &divisor, int "ient);
diff --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp
index a7d307b47c3e8..f1b7175fbd03f 100644
--- a/libc/utils/MPFRWrapper/MPFRUtils.cpp
+++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp
@@ -79,6 +79,8 @@ unary_operation(Operation op, InputType input, unsigned int precision,
return mpfrInput.log2();
case Operation::Log10:
return mpfrInput.log10();
+ case Operation::Log10p1:
+ return mpfrInput.log10p1();
case Operation::Log1p:
return mpfrInput.log1p();
case Operation::Mod2PI:
diff --git a/libc/utils/MPFRWrapper/MPFRUtils.h b/libc/utils/MPFRWrapper/MPFRUtils.h
index 3a8f5343b3118..6c24b66324c1a 100644
--- a/libc/utils/MPFRWrapper/MPFRUtils.h
+++ b/libc/utils/MPFRWrapper/MPFRUtils.h
@@ -50,6 +50,7 @@ enum class Operation : int {
Log,
Log2,
Log10,
+ Log10p1,
Log1p,
Mod2PI,
ModPIOver2,
>From c54c17daf1018181a20c8bb2b2301191a5208a48 Mon Sep 17 00:00:00 2001
From: Shikhar Soni <shikharish05 at gmail.com>
Date: Sat, 7 Mar 2026 14:56:00 +0530
Subject: [PATCH 2/2] fix failing test, minor changes
Signed-off-by: Shikhar Soni <shikharish05 at gmail.com>
---
libc/config/linux/aarch64/entrypoints.txt | 1 +
libc/src/__support/math/log10p1f16.h | 145 ++++++++++++----------
2 files changed, 82 insertions(+), 64 deletions(-)
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 08b45ad190627..d02a73fe913b4 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -725,6 +725,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.llogbf16
libc.src.math.llrintf16
libc.src.math.llroundf16
+ libc.src.math.log10p1f16
libc.src.math.logbf16
libc.src.math.lrintf16
libc.src.math.lroundf16
diff --git a/libc/src/__support/math/log10p1f16.h b/libc/src/__support/math/log10p1f16.h
index e2b01a504975d..9459d270f9a0b 100644
--- a/libc/src/__support/math/log10p1f16.h
+++ b/libc/src/__support/math/log10p1f16.h
@@ -32,46 +32,6 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-namespace log10p1f16_internal {
-#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
-
-// ExceptValues for the small-|x| polynomial path (|x| <= 2^-3).
-LIBC_INLINE_VAR constexpr size_t N_LOG10P1F16_EXCEPTS = 3;
-
-LIBC_INLINE_VAR constexpr fputil::ExceptValues<float16, N_LOG10P1F16_EXCEPTS>
- LOG10P1F16_EXCEPTS = {{
- // (input, RZ output, RU offset, RD offset, RN offset)
- // x = 0x1.ep-13
- {0x0B80U, 0x0683U, 1U, 0U, 0U},
- // x = -0x1.b8p-13
- {0x89DCU, 0x8516U, 0U, 1U, 1U},
- // x = -0x1.42p-8
- {0x9D08U, 0x9861U, 0U, 1U, 0U},
- }};
-
-// ExceptValues for the large-|x| table-based path (|x| > 2^-3).
-LIBC_INLINE_VAR constexpr size_t N_LOG10P1F16_EXCEPTS_HI = 6;
-
-LIBC_INLINE_VAR constexpr fputil::ExceptValues<float16,
- N_LOG10P1F16_EXCEPTS_HI>
- LOG10P1F16_EXCEPTS_HI = {{
- // (input, RZ output, RU offset, RD offset, RN offset)
- // x = 0x1.4c4p-2, log10p1f16(x) = 0x1.f3cp-4 (RZ)
- {0x3531U, 0x2FCFU, 1U, 0U, 0U},
- // x = 0x1.2p+3, log10p1f16(x) = 0x1p+0 (RZ)
- {0x4880U, 0x3C00U, 0U, 0U, 0U},
- // x = 0x1.8cp+6, log10p1f16(x) = 0x1p+1 (RZ)
- {0x5630U, 0x4000U, 0U, 0U, 0U},
- // x = 0x1.f44p+6, log10p1f16(x) = 0x1.0ccp+1 (RZ)
- {0x57D1U, 0x4033U, 1U, 0U, 0U},
- // x = 0x1.f38p+9, log10p1f16(x) = 0x1.8p+1 (RZ)
- {0x63CEU, 0x4200U, 0U, 0U, 0U},
- // x = -0x1.808p-1, log10p1f16(x) = -0x1.354p-1 (RZ)
- {0xBA02U, 0xB8D4U, 0U, 1U, 1U},
- }};
-#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
-} // namespace log10p1f16_internal
-
LIBC_INLINE float16 log10p1f16(float16 x) {
using namespace math::expxf16_internal;
using FPBits = fputil::FPBits<float16>;
@@ -80,9 +40,8 @@ LIBC_INLINE float16 log10p1f16(float16 x) {
uint16_t x_u = x_bits.uintval();
uint16_t x_abs = x_u & 0x7fffU;
- // If x is +-0, +inf, NaN, -1, x < -1, or |x| <= 2^-3.
- if (LIBC_UNLIKELY(x_abs == 0U || x_abs <= 0x3000U || x_u == 0x7c00U ||
- x_u >= 0xbc00U || x_bits.is_nan())) {
+ // If x is +-0, NaN, +/-inf, or |x| <= 2^-3.
+ if (LIBC_UNLIKELY(x_abs <= 0x3000U || x_abs >= 0x7c00U)) {
// log10p1(NaN) = NaN
if (x_bits.is_nan()) {
if (x_bits.is_signaling_nan()) {
@@ -101,46 +60,104 @@ LIBC_INLINE float16 log10p1f16(float16 x) {
if (x_u == 0x7c00U)
return FPBits::inf().get_val();
- // log10p1(-1) = -inf
- if (x_u == 0xbc00U) {
- fputil::raise_except_if_required(FE_DIVBYZERO);
- return FPBits::inf(Sign::NEG).get_val();
- }
-
- // log10p1(x) = NaN for x < -1
- if (x_u > 0xbc00U) {
+ // log10p1(-inf) = NaN
+ if (x_abs >= 0x7c00U) {
fputil::set_errno_if_required(EDOM);
fputil::raise_except_if_required(FE_INVALID);
return FPBits::quiet_nan().get_val();
}
- // When |x| <= 2^-3, use a polynomial approximation directly on x.
+ // When |x| <= 2^-3, use a degree-5 minimax polynomial on x.
// For y = 1+x near 1, the table-based range reduction suffers from
// catastrophic cancellation (m*log10(2) + log10(f) nearly cancel).
// Computing log10(1+x) directly via polynomial avoids this.
- // log10(1+x) = x * (c0 + c1*x + c2*x^2 + c3*x^3 + c4*x^4 + c5*x^5 +
- // c6*x^6)
- // where ck = (-1)^k / ((k+1) * ln(10)), the Taylor coefficients of
- // log10(1+x)/x.
+ //
+ // Generated by Sollya with:
+ // > display = hexadecimal;
+ // > Q = fpminimax(log10(1 + x), [|1, 2, 3, 4, 5|], [|SG...|],
+ // [-2^-3, 2^-3]);
+ // > Q;
if (x_abs <= 0x3000U) {
#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
- if (auto r = log10p1f16_internal::LOG10P1F16_EXCEPTS.lookup(x_u);
- LIBC_UNLIKELY(r.has_value()))
+ // ExceptValues for the small-|x| polynomial path (|x| <= 2^-3).
+ constexpr size_t N_LOG10P1F16_EXCEPTS = 7;
+ constexpr fputil::ExceptValues<float16, N_LOG10P1F16_EXCEPTS>
+ LOG10P1F16_EXCEPTS = {{
+ // (input, RZ output, RU offset, RD offset, RN offset)
+ // x = 0x1.ep-13
+ {0x0B80U, 0x0683U, 1U, 0U, 0U},
+ // x = 0x1.4fp-6
+ {0x213CU, 0x1C85U, 1U, 0U, 1U},
+ // x = 0x1.82cp-4
+ {0x2E0BU, 0x2904U, 1U, 0U, 0U},
+ // x = 0x1.d24p-4
+ {0x2EC9U, 0x299AU, 1U, 0U, 0U},
+ // x = -0x1.b8p-13
+ {0x89DCU, 0x8516U, 0U, 1U, 1U},
+ // x = -0x1.49p-6
+ {0x9924U, 0x9478U, 0U, 1U, 1U},
+ // x = -0x1.42p-8
+ {0x9D08U, 0x9861U, 0U, 1U, 0U},
+ }};
+
+ if (auto r = LOG10P1F16_EXCEPTS.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
return r.value();
#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
float xf = x;
return fputil::cast<float16>(
- xf * fputil::polyeval(xf, 0x1.bcb7b2p-2f, -0x1.bcb7b2p-3f,
- 0x1.287a76p-3f, -0x1.bcb7b2p-4f,
- 0x1.63c628p-4f, -0x1.287a76p-4f,
- 0x1.fc3fa6p-5f));
+ xf * fputil::polyeval(xf, 0x1.bcb7b2p-2f, -0x1.bcb4cp-3f,
+ 0x1.2875bcp-3f, -0x1.c2946ep-4f,
+ 0x1.69da2p-4f));
}
}
+ // log10p1(-1) = -inf
+ if (LIBC_UNLIKELY(x_u == 0xbc00U)) {
+ fputil::raise_except_if_required(FE_DIVBYZERO);
+ return FPBits::inf(Sign::NEG).get_val();
+ }
+
+ // log10p1(x) = NaN for x < -1
+ if (LIBC_UNLIKELY(x_u > 0xbc00U)) {
+ fputil::set_errno_if_required(EDOM);
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+
#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
- if (auto r = log10p1f16_internal::LOG10P1F16_EXCEPTS_HI.lookup(x_u);
- LIBC_UNLIKELY(r.has_value()))
+ // ExceptValues for the large-|x| table-based path (|x| > 2^-3).
+ constexpr size_t N_LOG10P1F16_EXCEPTS_HI = 12;
+ constexpr fputil::ExceptValues<float16, N_LOG10P1F16_EXCEPTS_HI>
+ LOG10P1F16_EXCEPTS_HI = {{
+ // (input, RZ output, RU offset, RD offset, RN offset)
+ // x = 0x1.3bp-3
+ {0x30ECU, 0x2BF3U, 1U, 0U, 1U},
+ // x = 0x1.ba0p-3
+ {0x32E8U, 0x2D6EU, 1U, 0U, 1U},
+ // x = 0x1.4c4p-2
+ {0x3531U, 0x2FCFU, 1U, 0U, 0U},
+ // x = 0x1.744p+0
+ {0x3DD1U, 0x363CU, 1U, 0U, 1U},
+ // x = 0x1.7d8p+0
+ {0x3DF6U, 0x3656U, 1U, 0U, 1U},
+ // x = 0x1.2p+3
+ {0x4880U, 0x3C00U, 0U, 0U, 0U},
+ // x = 0x1.8cp+6
+ {0x5630U, 0x4000U, 0U, 0U, 0U},
+ // x = 0x1.f44p+6
+ {0x57D1U, 0x4033U, 1U, 0U, 0U},
+ // x = 0x1.f38p+9
+ {0x63CEU, 0x4200U, 0U, 0U, 0U},
+ // x = -0x1.558p-3
+ {0xB156U, 0xAD12U, 0U, 1U, 0U},
+ // x = -0x1.8d8p-2
+ {0xB636U, 0xB2D3U, 0U, 1U, 1U},
+ // x = -0x1.808p-1
+ {0xBA02U, 0xB8D4U, 0U, 1U, 1U},
+ }};
+
+ if (auto r = LOG10P1F16_EXCEPTS_HI.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
return r.value();
#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
More information about the libc-commits
mailing list