[libc][math][c23] Add log2p1f16 C23 math function (#186754)

Signed-off-by: Shikhar Soni <shikharish05@gmail.com>
This commit is contained in:
Shikhar Soni 2026-03-28 21:04:42 +05:30 committed by GitHub
parent 15940b130a
commit f0ce26d06d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
24 changed files with 433 additions and 1 deletions

View File

@ -659,6 +659,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.log10f16
libc.src.math.log10p1f16
libc.src.math.log2f16
libc.src.math.log2p1f16
libc.src.math.logbf16
libc.src.math.logf16
libc.src.math.lrintf16

View File

@ -665,6 +665,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.log10f16
libc.src.math.log10p1f16
libc.src.math.log2f16
libc.src.math.log2p1f16
libc.src.math.logbf16
libc.src.math.logf16
libc.src.math.lrintf16

View File

@ -478,6 +478,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.log10f16
libc.src.math.log10p1f16
libc.src.math.log2f16
libc.src.math.log2p1f16
libc.src.math.logbf16
libc.src.math.logf16
libc.src.math.lrintf16

View File

@ -587,6 +587,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.log10f16
libc.src.math.log10p1f16
libc.src.math.log2f16
libc.src.math.log2p1f16
libc.src.math.logbf16
libc.src.math.logf16
libc.src.math.lrintf16

View File

@ -589,6 +589,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.log10f16
libc.src.math.log10p1f16
libc.src.math.log2f16
libc.src.math.log2p1f16
libc.src.math.logbf16
libc.src.math.logf16
libc.src.math.lrintf16

View File

@ -742,6 +742,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.llrintf16
libc.src.math.llroundf16
libc.src.math.log10p1f16
libc.src.math.log2p1f16
libc.src.math.logbf16
libc.src.math.lrintf16
libc.src.math.lroundf16

View File

@ -760,6 +760,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.log10f16
libc.src.math.log10p1f16
libc.src.math.log2f16
libc.src.math.log2p1f16
libc.src.math.logbf16
libc.src.math.logf16
libc.src.math.lrintf16

View File

@ -814,6 +814,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.log10f16
libc.src.math.log10p1f16
libc.src.math.log2f16
libc.src.math.log2p1f16
libc.src.math.logbf16
libc.src.math.logf16
libc.src.math.lrintf16

View File

@ -329,7 +329,7 @@ Higher Math Functions
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
| log2 | |check| | |check| | | |check| | | | 7.12.6.15 | F.10.3.15 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
| log2p1 | | | | | | | 7.12.6.16 | F.10.3.16 |
| log2p1 | | | | |check| | | | 7.12.6.16 | F.10.3.16 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
| logp1 | | | | | | | 7.12.6.14 | F.10.3.14 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+

View File

@ -1798,6 +1798,13 @@ functions:
arguments:
- type: _Float16
guard: LIBC_TYPES_HAS_FLOAT16
- name: log2p1f16
standards:
- stdc
return_type: _Float16
arguments:
- type: _Float16
guard: LIBC_TYPES_HAS_FLOAT16
- name: log1p
standards:
- stdc

View File

@ -2792,6 +2792,24 @@ add_header_library(
libc.src.__support.macros.properties.cpu_features
)
add_header_library(
log2p1f16
HDRS
log2p1f16.h
DEPENDS
.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 log2p1f16 ---------------------*- 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_LOG2P1F16_H
#define LLVM_LIBC_SRC___SUPPORT_MATH_LOG2P1F16_H
#include "include/llvm-libc-macros/float16-macros.h"
#ifdef LIBC_TYPES_HAS_FLOAT16
#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 log2p1f16(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 NaN, +/-inf, or |x| <= 2^-3.
if (LIBC_UNLIKELY(x_abs <= 0x3000U || x_abs >= 0x7c00U)) {
// log2p1(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;
}
// 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 + log2(f) nearly cancel). Computing
// log2(1+x) directly via polynomial avoids this.
//
// Generated by Sollya with:
// > display = hexadecimal;
// > Q = fpminimax(log2(1 + x), [|1, 2, 3, 4, 5|], [|SG...|],
// [-2^-3, 2^-3]);
// > Q;
// > dirtyinfnorm((log2(1 + x) - Q) / log2(1 + x), [-2^-3, 2^-3]);
// 0x1.6ed1f4728dcb6f1d6a651a3c728937f7468f8eedfp-22
if (x_abs <= 0x3000U) {
// log2p1(+/-0) = +/-0
if (x_abs == 0U)
return x;
#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
constexpr size_t N_LOG2P1F16_EXCEPTS = 11;
constexpr fputil::ExceptValues<float16, N_LOG2P1F16_EXCEPTS>
LOG2P1F16_EXCEPTS = {{
// (input, RZ output, RU offset, RD offset, RN offset)
// x = 0x1.ec4p-12
{0x0FB1U, 0x118BU, 1U, 0U, 1U},
// x = 0x1.c04p-6
{0x2701U, 0x28FBU, 1U, 0U, 1U},
// x = -0x1.824p-15
{0x8309U, 0x8461U, 0U, 1U, 0U},
// x = -0x1.414p-10
{0x9505U, 0x973EU, 0U, 1U, 1U},
// x = -0x1.cb8p-10
{0x972EU, 0x992FU, 0U, 1U, 0U},
// x = -0x1.99cp-8
{0x9E67U, 0xA0A2U, 0U, 1U, 0U},
// x = -0x1.ce8p-7
{0xA33AU, 0xA540U, 0U, 1U, 0U},
// x = -0x1.73cp-6
{0xA5CFU, 0xA83DU, 0U, 1U, 0U},
// x = -0x1.87p-5
{0xAA1CU, 0xAC84U, 0U, 1U, 0U},
// x = -0x1.d48p-5
{0xAB52U, 0xAD70U, 0U, 1U, 0U},
// x = -0x1.da0p-4
{0xAF68U, 0xB1ADU, 0U, 1U, 0U},
}};
if (auto r = LOG2P1F16_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.715476p+0f, -0x1.715204p-1f,
0x1.ec6c5p-2f, -0x1.763338p-2f,
0x1.2bcd3cp-2f));
}
// log2p1(+inf) = +inf
if (x_u == 0x7c00U)
return FPBits::inf().get_val();
// log2p1(-inf) = NaN
fputil::set_errno_if_required(EDOM);
fputil::raise_except_if_required(FE_INVALID);
return FPBits::quiet_nan().get_val();
}
// log2p1(-1) = -inf
if (LIBC_UNLIKELY(x_u == 0xbc00U)) {
fputil::raise_except_if_required(FE_DIVBYZERO);
return FPBits::inf(Sign::NEG).get_val();
}
// log2p1(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
#ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT
constexpr size_t N_LOG2P1F16_EXCEPTS_HI = 2;
#else
constexpr size_t N_LOG2P1F16_EXCEPTS_HI = 6;
#endif
constexpr fputil::ExceptValues<float16, N_LOG2P1F16_EXCEPTS_HI>
LOG2P1F16_EXCEPTS_HI = {{
// (input, RZ output, RU offset, RD offset, RN offset)
#ifndef LIBC_TARGET_CPU_HAS_FMA_FLOAT
// x = 0x1.12p-3
{0x3048U, 0x31CBU, 1U, 0U, 1U},
// x = 0x1.598p-2
{0x3566U, 0x36B5U, 1U, 0U, 1U},
#endif
// x = 0x1.23cp-3
{0x308FU, 0x3226U, 1U, 0U, 0U},
// x = 0x1.accp+0
{0x3EB3U, 0x3DADU, 1U, 0U, 0U},
#ifndef LIBC_TARGET_CPU_HAS_FMA_FLOAT
// x = -0x1.534p-2
{0xB54DU, 0xB8A5U, 0U, 1U, 0U},
// x = -0x1.bb8p-2
{0xB6EEU, 0xBA8DU, 0U, 1U, 0U},
#endif
}};
if (auto r = LOG2P1F16_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);
// y = 1 + x should stay positive for x > -1, but keep this guard for
// completeness in case of unexpected intermediate behavior.
if (LIBC_UNLIKELY(y_bits.is_zero()))
return FPBits::inf(Sign::NEG).get_val();
int m = y_bits.get_exponent();
y_bits.set_biased_exponent(FPBitsFloat::EXP_BIAS);
float mant_f = y_bits.get_val();
// Leading 23 - 5 = 18, so the top 5 mantissa bits give the index 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(log2(1 + x)/x, 2, [|SG...|], [-2^-5, 2^-5]);
// > x * P;
// > dirtyinfnorm((log2(1 + x)/x - P) / (log2(1 + x)/x), [-2^-5, 2^-5]);
// 0x1.0087f3cad284d0a4464b1e27e83258b5e69a647f5p-19
float log2p1_d_over_f =
v * fputil::polyeval(v, 0x1.715476p+0f, -0x1.71771ap-1f, 0x1.ecb38ep-2f);
float log2_1_mant = LOG2F_F[f] + log2p1_d_over_f;
return fputil::cast<float16>(static_cast<float>(m) + log2_1_mant);
}
} // namespace math
} // namespace LIBC_NAMESPACE_DECL
#endif // LIBC_TYPES_HAS_FLOAT16
#endif // LLVM_LIBC_SRC___SUPPORT_MATH_LOG2P1F16_H

View File

@ -399,6 +399,7 @@ add_math_entrypoint_object(log1pf)
add_math_entrypoint_object(log2)
add_math_entrypoint_object(log2f)
add_math_entrypoint_object(log2f16)
add_math_entrypoint_object(log2p1f16)
add_math_entrypoint_object(log)
add_math_entrypoint_object(logf)

View File

@ -1867,6 +1867,17 @@ add_entrypoint_object(
libc.src.errno.errno
)
add_entrypoint_object(
log2p1f16
SRCS
log2p1f16.cpp
HDRS
../log2p1f16.h
DEPENDS
libc.src.__support.math.log2p1f16
libc.src.errno.errno
)
add_entrypoint_object(
log1p
SRCS

View File

@ -0,0 +1,18 @@
//===-- Half-precision log2(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/log2p1f16.h"
#include "src/__support/math/log2p1f16.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(float16, log2p1f16, (float16 x)) {
return math::log2p1f16(x);
}
} // namespace LIBC_NAMESPACE_DECL

21
libc/src/math/log2p1f16.h Normal file
View File

@ -0,0 +1,21 @@
//===-- Implementation header for log2p1f16 ---------------------*- 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_LOG2P1F16_H
#define LLVM_LIBC_SRC_MATH_LOG2P1F16_H
#include "src/__support/macros/config.h"
#include "src/__support/macros/properties/types.h"
namespace LIBC_NAMESPACE_DECL {
float16 log2p1f16(float16 x);
} // namespace LIBC_NAMESPACE_DECL
#endif // LLVM_LIBC_SRC_MATH_LOG2P1F16_H

View File

@ -2214,6 +2214,17 @@ add_fp_unittest(
libc.src.math.log10p1f16
)
add_fp_unittest(
log2p1f16_test
NEED_MPFR
SUITE
libc-math-unittests
SRCS
log2p1f16_test.cpp
DEPENDS
libc.src.math.log2p1f16
)
add_fp_unittest(
log1p_test
NEED_MPFR

View File

@ -0,0 +1,49 @@
//===-- Exhaustive test for log2p1f16 -------------------------------------===//
//
// 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/log2p1f16.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 LlvmLibcLog2p1f16Test = 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(LlvmLibcLog2p1f16Test, PositiveRange) {
for (uint16_t v = POS_START; v <= POS_STOP; ++v) {
float16 x = FPBits(v).get_val();
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log2p1, x,
LIBC_NAMESPACE::log2p1f16(x),
TOLERANCE + 0.5);
}
}
TEST_F(LlvmLibcLog2p1f16Test, NegativeRange) {
for (uint16_t v = NEG_START; v <= NEG_STOP; ++v) {
float16 x = FPBits(v).get_val();
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log2p1, x,
LIBC_NAMESPACE::log2p1f16(x),
TOLERANCE + 0.5);
}
}

View File

@ -4483,6 +4483,19 @@ add_fp_unittest(
libc.src.__support.FPUtil.cast
)
add_fp_unittest(
log2p1f16_test
SUITE
libc-math-smoke-tests
SRCS
log2p1f16_test.cpp
DEPENDS
libc.hdr.errno_macros
libc.hdr.fenv_macros
libc.src.math.log2p1f16
libc.src.__support.FPUtil.cast
)
add_fp_unittest(
log1p_test
SUITE

View File

@ -0,0 +1,48 @@
//===-- Unittests for log2p1f16 -------------------------------------------===//
//
// 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/log2p1f16.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"
using LlvmLibcLog2p1f16Test = LIBC_NAMESPACE::testing::FPTest<float16>;
TEST_F(LlvmLibcLog2p1f16Test, SpecialNumbers) {
EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::log2p1f16(aNaN));
EXPECT_MATH_ERRNO(0);
EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::log2p1f16(sNaN),
FE_INVALID);
EXPECT_MATH_ERRNO(0);
EXPECT_FP_EQ_ALL_ROUNDING(inf, LIBC_NAMESPACE::log2p1f16(inf));
EXPECT_MATH_ERRNO(0);
EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::log2p1f16(neg_inf));
EXPECT_MATH_ERRNO(EDOM);
EXPECT_FP_EQ_ALL_ROUNDING(zero, LIBC_NAMESPACE::log2p1f16(zero));
EXPECT_MATH_ERRNO(0);
EXPECT_FP_EQ_ALL_ROUNDING(neg_zero, LIBC_NAMESPACE::log2p1f16(neg_zero));
EXPECT_MATH_ERRNO(0);
EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(
neg_inf,
LIBC_NAMESPACE::log2p1f16(LIBC_NAMESPACE::fputil::cast<float16>(-1.0)),
FE_DIVBYZERO);
EXPECT_MATH_ERRNO(0);
EXPECT_FP_EQ_ALL_ROUNDING(
aNaN,
LIBC_NAMESPACE::log2p1f16(LIBC_NAMESPACE::fputil::cast<float16>(-2.0)));
EXPECT_MATH_ERRNO(EDOM);
}

View File

@ -335,6 +335,22 @@ MPFRNumber MPFRNumber::log2() const {
return result;
}
MPFRNumber MPFRNumber::log2p1() const {
// TODO: Only use mpfr_log2p1 once CI and buildbots get MPFR >= 4.2.0.
#if MPFR_VERSION >= MPFR_VERSION_NUM(4, 2, 0)
MPFRNumber result(*this);
mpfr_log2p1(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_log2(result.value, result.value, mpfr_rounding);
return result;
#endif
}
MPFRNumber MPFRNumber::log10() const {
MPFRNumber result(*this);
mpfr_log10(result.value, value, mpfr_rounding);

View File

@ -214,6 +214,7 @@ public:
MPFRNumber hypot(const MPFRNumber &b);
MPFRNumber log() const;
MPFRNumber log2() const;
MPFRNumber log2p1() const;
MPFRNumber log10() const;
MPFRNumber log10p1() const;
MPFRNumber log1p() const;

View File

@ -79,6 +79,8 @@ unary_operation(Operation op, InputType input, unsigned int precision,
return mpfrInput.log();
case Operation::Log2:
return mpfrInput.log2();
case Operation::Log2p1:
return mpfrInput.log2p1();
case Operation::Log10:
return mpfrInput.log10();
case Operation::Log10p1:

View File

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