From f0ce26d06d822fd6985d227dc1be9d218977e334 Mon Sep 17 00:00:00 2001 From: Shikhar Soni <139053348+shikharish@users.noreply.github.com> Date: Sat, 28 Mar 2026 21:04:42 +0530 Subject: [PATCH] [libc][math][c23] Add log2p1f16 C23 math function (#186754) Signed-off-by: Shikhar Soni --- libc/config/baremetal/aarch64/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/aarch64/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 | 18 ++ libc/src/__support/math/log2p1f16.h | 207 ++++++++++++++++++ libc/src/math/CMakeLists.txt | 1 + libc/src/math/generic/CMakeLists.txt | 11 + libc/src/math/generic/log2p1f16.cpp | 18 ++ libc/src/math/log2p1f16.h | 21 ++ libc/test/src/math/CMakeLists.txt | 11 + libc/test/src/math/log2p1f16_test.cpp | 49 +++++ libc/test/src/math/smoke/CMakeLists.txt | 13 ++ libc/test/src/math/smoke/log2p1f16_test.cpp | 48 ++++ 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, 433 insertions(+), 1 deletion(-) create mode 100644 libc/src/__support/math/log2p1f16.h create mode 100644 libc/src/math/generic/log2p1f16.cpp create mode 100644 libc/src/math/log2p1f16.h create mode 100644 libc/test/src/math/log2p1f16_test.cpp create mode 100644 libc/test/src/math/smoke/log2p1f16_test.cpp diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt index 84600efc5e0d..19f3fb189de7 100644 --- a/libc/config/baremetal/aarch64/entrypoints.txt +++ b/libc/config/baremetal/aarch64/entrypoints.txt @@ -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 diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt index 30f9bccd2221..5c2b5c24e158 100644 --- a/libc/config/baremetal/riscv/entrypoints.txt +++ b/libc/config/baremetal/riscv/entrypoints.txt @@ -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 diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt index 4d27e994323f..8e0d2fb01966 100644 --- a/libc/config/darwin/aarch64/entrypoints.txt +++ b/libc/config/darwin/aarch64/entrypoints.txt @@ -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 diff --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt index de05977d89ee..028d57b52116 100644 --- a/libc/config/gpu/amdgpu/entrypoints.txt +++ b/libc/config/gpu/amdgpu/entrypoints.txt @@ -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 diff --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt index a0ce378c5b34..feae60547bed 100644 --- a/libc/config/gpu/nvptx/entrypoints.txt +++ b/libc/config/gpu/nvptx/entrypoints.txt @@ -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 diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt index a37c27607db6..49288ce0699a 100644 --- a/libc/config/linux/aarch64/entrypoints.txt +++ b/libc/config/linux/aarch64/entrypoints.txt @@ -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 diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt index 66fb01e198ec..2f5a742a272d 100644 --- a/libc/config/linux/riscv/entrypoints.txt +++ b/libc/config/linux/riscv/entrypoints.txt @@ -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 diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt index 83c494932fec..2d7de065d811 100644 --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -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 diff --git a/libc/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst index 202ef034b8ac..88b284d1e63f 100644 --- a/libc/docs/headers/math/index.rst +++ b/libc/docs/headers/math/index.rst @@ -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 | +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+ diff --git a/libc/include/math.yaml b/libc/include/math.yaml index 055149a6a4e3..dbb8479b1d0d 100644 --- a/libc/include/math.yaml +++ b/libc/include/math.yaml @@ -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 diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt index b607c351f3bc..187ff4f124b2 100644 --- a/libc/src/__support/math/CMakeLists.txt +++ b/libc/src/__support/math/CMakeLists.txt @@ -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 diff --git a/libc/src/__support/math/log2p1f16.h b/libc/src/__support/math/log2p1f16.h new file mode 100644 index 000000000000..4fe9de42d71b --- /dev/null +++ b/libc/src/__support/math/log2p1f16.h @@ -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; + 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 + 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( + 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 + 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; + 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(static_cast(m) + log2_1_mant); +} + +} // namespace math +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT16 + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_LOG2P1F16_H diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt index d15ed5256c3b..c7ded04aad7c 100644 --- a/libc/src/math/CMakeLists.txt +++ b/libc/src/math/CMakeLists.txt @@ -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) diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt index 624c3b4f6795..ad05fd83cadb 100644 --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -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 diff --git a/libc/src/math/generic/log2p1f16.cpp b/libc/src/math/generic/log2p1f16.cpp new file mode 100644 index 000000000000..a4382d9d1e2c --- /dev/null +++ b/libc/src/math/generic/log2p1f16.cpp @@ -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 diff --git a/libc/src/math/log2p1f16.h b/libc/src/math/log2p1f16.h new file mode 100644 index 000000000000..88b2510572c3 --- /dev/null +++ b/libc/src/math/log2p1f16.h @@ -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 diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt index 0e93760a4675..e157b0463bbd 100644 --- a/libc/test/src/math/CMakeLists.txt +++ b/libc/test/src/math/CMakeLists.txt @@ -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 diff --git a/libc/test/src/math/log2p1f16_test.cpp b/libc/test/src/math/log2p1f16_test.cpp new file mode 100644 index 000000000000..98cf1dfbf0ea --- /dev/null +++ b/libc/test/src/math/log2p1f16_test.cpp @@ -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; + +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); + } +} diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt index c2ac29622c32..544425a39d34 100644 --- a/libc/test/src/math/smoke/CMakeLists.txt +++ b/libc/test/src/math/smoke/CMakeLists.txt @@ -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 diff --git a/libc/test/src/math/smoke/log2p1f16_test.cpp b/libc/test/src/math/smoke/log2p1f16_test.cpp new file mode 100644 index 000000000000..d7b8826552e2 --- /dev/null +++ b/libc/test/src/math/smoke/log2p1f16_test.cpp @@ -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; + +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(-1.0)), + FE_DIVBYZERO); + EXPECT_MATH_ERRNO(0); + + EXPECT_FP_EQ_ALL_ROUNDING( + aNaN, + LIBC_NAMESPACE::log2p1f16(LIBC_NAMESPACE::fputil::cast(-2.0))); + EXPECT_MATH_ERRNO(EDOM); +} diff --git a/libc/utils/MPFRWrapper/MPCommon.cpp b/libc/utils/MPFRWrapper/MPCommon.cpp index 9fa50b9187df..bdd828614864 100644 --- a/libc/utils/MPFRWrapper/MPCommon.cpp +++ b/libc/utils/MPFRWrapper/MPCommon.cpp @@ -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); diff --git a/libc/utils/MPFRWrapper/MPCommon.h b/libc/utils/MPFRWrapper/MPCommon.h index b8d144e1582b..935a2614968a 100644 --- a/libc/utils/MPFRWrapper/MPCommon.h +++ b/libc/utils/MPFRWrapper/MPCommon.h @@ -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; diff --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp index 1f6396221664..356764302bda 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.log(); case Operation::Log2: return mpfrInput.log2(); + case Operation::Log2p1: + return mpfrInput.log2p1(); case Operation::Log10: return mpfrInput.log10(); case Operation::Log10p1: diff --git a/libc/utils/MPFRWrapper/MPFRUtils.h b/libc/utils/MPFRWrapper/MPFRUtils.h index 9bfc5be507a5..6c1b15598b0f 100644 --- a/libc/utils/MPFRWrapper/MPFRUtils.h +++ b/libc/utils/MPFRWrapper/MPFRUtils.h @@ -50,6 +50,7 @@ enum class Operation : int { Floor, Log, Log2, + Log2p1, Log10, Log10p1, Log1p,