[libc][math][c23] Add log10p1f16 C23 math function (#184739)

Closes #133202

---------

Signed-off-by: Shikhar Soni <shikharish05@gmail.com>
This commit is contained in:
Shikhar Soni 2026-03-13 19:29:26 +05:30 committed by GitHub
parent 1b9a4a0f72
commit a7d1a87b30
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
25 changed files with 435 additions and 1 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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);
}
}

View File

@ -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

View File

@ -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);
}

View File

@ -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);

View File

@ -214,6 +214,7 @@ public:
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 &quotient);

View File

@ -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:

View File

@ -50,6 +50,7 @@ enum class Operation : int {
Log,
Log2,
Log10,
Log10p1,
Log1p,
Mod2PI,
ModPIOver2,