[libc-commits] [libc] a7d1a87 - [libc][math][c23] Add log10p1f16 C23 math function (#184739)
via libc-commits
libc-commits at lists.llvm.org
Fri Mar 13 06:59:31 PDT 2026
Author: Shikhar Soni
Date: 2026-03-13T09:59:26-04:00
New Revision: a7d1a87b30ce626678d33fe1c12e647f7ce4fb20
URL: https://github.com/llvm/llvm-project/commit/a7d1a87b30ce626678d33fe1c12e647f7ce4fb20
DIFF: https://github.com/llvm/llvm-project/commit/a7d1a87b30ce626678d33fe1c12e647f7ce4fb20.diff
LOG: [libc][math][c23] Add log10p1f16 C23 math function (#184739)
Closes #133202
---------
Signed-off-by: Shikhar Soni <shikharish05 at gmail.com>
Added:
libc/src/__support/math/log10p1f16.h
libc/src/math/generic/log10p1f16.cpp
libc/src/math/log10p1f16.h
libc/test/src/math/log10p1f16_test.cpp
libc/test/src/math/smoke/log10p1f16_test.cpp
Modified:
libc/config/baremetal/aarch64/entrypoints.txt
libc/config/baremetal/arm/entrypoints.txt
libc/config/baremetal/riscv/entrypoints.txt
libc/config/darwin/aarch64/entrypoints.txt
libc/config/gpu/amdgpu/entrypoints.txt
libc/config/gpu/nvptx/entrypoints.txt
libc/config/linux/aarch64/entrypoints.txt
libc/config/linux/riscv/entrypoints.txt
libc/config/linux/x86_64/entrypoints.txt
libc/docs/headers/math/index.rst
libc/include/math.yaml
libc/src/__support/math/CMakeLists.txt
libc/src/math/CMakeLists.txt
libc/src/math/generic/CMakeLists.txt
libc/test/src/math/CMakeLists.txt
libc/test/src/math/smoke/CMakeLists.txt
libc/utils/MPFRWrapper/MPCommon.cpp
libc/utils/MPFRWrapper/MPCommon.h
libc/utils/MPFRWrapper/MPFRUtils.cpp
libc/utils/MPFRWrapper/MPFRUtils.h
Removed:
################################################################################
diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index a6e4923274514..4c98f8037f5e4 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -650,6 +650,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 48fc612358e92..40e3f8b8000d1 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -661,6 +661,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 2697cb1b53765..e23159b7a85ce 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/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/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index 234ea3f42d6d0..385c5bf13a4e3 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -472,6 +472,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 c6960d31907aa..286f003e92967 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -582,6 +582,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 c1927d8dc3c9d..8307e6426a7ad 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -584,6 +584,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/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 1c0abae76dbcd..5c5d63e48bbe2 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -731,6 +731,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/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 6c8ccb15e74b4..38b441551316a 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -747,6 +747,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 0602955dc1cb1..51343ea03c072 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -797,6 +797,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 6b987671546e6..296c2537adbd9 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 646fd5a93e2a5..07a204c67a365 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -2597,6 +2597,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..54533c994df81
--- /dev/null
+++ b/libc/src/__support/math/log10p1f16.h
@@ -0,0 +1,207 @@
+//===-- 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 {
+
+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, 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()) {
+ 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(-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 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.
+ //
+ // 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
+ // 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.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
+ // 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
+
+ // 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 418cf75dfa17a..23dc02304ba10 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -385,6 +385,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 8bc7525e4f520..046ccbfd2d823 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..8f8d133d94edd
--- /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 c8460ee030db1..4e71ed81fc4e6 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -2191,6 +2191,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..4fc4034002040
--- /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 ce419a3226113..647626e69d7d4 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -4456,6 +4456,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..b4afd89546264
--- /dev/null
+++ b/libc/test/src/math/smoke/log10p1f16_test.cpp
@@ -0,0 +1,48 @@
+//===-- 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 4c5c891bcfe9a..d9f444cfa8a76 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,
More information about the libc-commits
mailing list