[libc][math] Implement log1pf16 (C23) #224152
<!--LLVM NEW CONTRIBUTOR COMMENT-->
Hello @usefahmed07 :wave:
Thank you for submitting a Pull Request (PR) to the LLVM Project. Since this is your first PR, here are a few useful links covering our main contribution policies and review practices.
All contributions to LLVM must follow our LLVM AI Tool Use Policy. In particular, if you used AI while working on this PR, remember to add a note to the PR description.
The LLVM Code-Review Policy and Practices document contains practical information about the PR process, including how patches are reviewed and accepted, and who can review a PR.
Please reply to this message to confirm that you have read these policies, especially the LLVM AI Tool Use Policy, and that any AI tool usage has been noted in the PR description.
---
Frequently asked questions
How do I add reviewers?
This PR will be automatically labeled, and the relevant teams will be notified. For some parts of the project, reviewers may also be added automatically.
You can also add reviewers manually using the Reviewers section on this page. If you cannot use that section, it is probably because you do not have write permissions for the repository. In that case, you can request a review by tagging reviewers in a comment using @ followed by their GitHub username.
What if there are no comments?
If you have not received any comments on your PR after a week, you can request a review by pinging the PR with a comment such as “Ping”. The common courtesy ping rate is once a week. Please remember that you are asking for volunteer time from other developers.
Are any special GitHub settings required to contribute to LLVM?
We only require contributors to have a public email address associated with their GitHub commits, see this section of LLVM Developer Policy for details.
---
If you have questions, feel free to leave a comment on this PR, or ask on LLVM Discord or LLVM Discourse.
Thank you,
The LLVM Community
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-amdgpu
@llvm/pr-subscribers-libc
Author: Youssef Ahmed (usefahmed07)
<details>
<summary>Changes</summary>
Implements log1pf16 (C23) per #<!-- -->189543 — small-range minimax polynomial, general-range reduction reusing the logf16 pattern, plus an ExceptValues table for 6 hard-to-round cases found via the MPFR exhaustive test. Exhaustive and smoke tests both pass.
Fixes #<!-- -->189543
---
Full diff: https://github.com/llvm/llvm-project/pull/224152.diff
6 Files Affected:
````````````diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 3f4e563e54359..81b4ba218458f 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -977,6 +977,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.llroundf16
libc.src.math.log10f16
libc.src.math.log10p1f16
+ libc.src.math.log1pf16
libc.src.math.log2f16
libc.src.math.log2p1f16
libc.src.math.logbf16
diff --git a/libc/src/__support/math/log1pf16.h b/libc/src/__support/math/log1pf16.h
new file mode 100644
index 0000000000000..14b1a227a2aae
--- /dev/null
+++ b/libc/src/__support/math/log1pf16.h
@@ -0,0 +1,141 @@
+//===-- Implementation header for log1pf16 -----------------------*- 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_LOG1PF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_LOG1PF16_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 log1pf16(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;
+
+ constexpr size_t N_LOG1PF16_EXCEPTS = 6;
+ constexpr fputil::ExceptValues<float16, N_LOG1PF16_EXCEPTS>
+ LOG1PF16_EXCEPTS = {{
+ // (input, RZ output, RU offset, RD offset, RN offset)
+ // x = 0x1.f24p-7, log1pf16(x) = 0x1.ee4p-7 (RZ)
+ {0x23C9U, 0x23B9U, 1U, 0U, 1U},
+ // x = 0x1.988p0, log1pf16(x) = 0x1.e84p-1 (RZ)
+ {0x3E62U, 0x3BA1U, 1U, 0U, 1U},
+ // x = -0x1.e5p-7, log1pf16(x) = -0x1.e88p-7 (RZ)
+ {0xA394U, 0xA3A2U, 0U, 1U, 1U},
+ // x = -0x1.ed4p-7, log1pf16(x) = -0x1.f1p-7 (RZ)
+ {0xA3B5U, 0xA3C4U, 0U, 1U, 0U},
+ // x = -0x1.ae4p-6, log1pf16(x) = -0x1.b4p-6 (RZ)
+ {0xA6B9U, 0xA6D0U, 0U, 1U, 0U},
+ // x = -0x1.f6cp-6, log1pf16(x) = -0x1.fe8p-6 (RZ)
+ {0xA7DBU, 0xA7FAU, 0U, 1U, 1U},
+ }};
+
+ if (auto r = LOG1PF16_EXCEPTS.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
+ return r.value();
+
+ // If x is NaN, +/-inf, or |x| <= 2^-3.
+ if (LIBC_UNLIKELY(x_abs <= 0x3000U || x_abs >= 0x7c00U)) {
+ 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;
+ }
+
+ if (x_abs <= 0x3000U) {
+ // log1p(+/-0) = +/-0
+ if (x_abs == 0U)
+ return x;
+
+ // Degree-5 minimax polynomial generated by Sollya with:
+ // > display = hexadecimal;
+ // > P = fpminimax(log(1 + x)/x, [|0, 1, 2, 3, 4|], [|SG...|],
+ // [-2^-3, 2^-3]);
+ // > x * P;
+ // > dirtyinfnorm((log(1 + x) - x*P) / log(1 + x), [-2^-3, 2^-3]);
+ // 0x1.65afe505af70502b729765651fe9ef9d2071f0f89p-22
+ float xf = x;
+ return fputil::cast<float16>(
+ xf * fputil::polyeval(xf, 0x1p+0f, -0x1.fffc9cp-2f, 0x1.555174p-2f,
+ -0x1.03601ap-2f, 0x1.9ff8eep-3f));
+ }
+
+ if (x_u == 0x7c00U)
+ return FPBits::inf().get_val();
+
+ fputil::set_errno_if_required(EDOM);
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+
+ // log1p(-1) = -inf
+ if (LIBC_UNLIKELY(x_u == 0xbc00U)) {
+ fputil::raise_except_if_required(FE_DIVBYZERO);
+ return FPBits::inf(Sign::NEG).get_val();
+ }
+
+ // log1p(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();
+ }
+
+ float xf = x;
+ float y = 1.0f + xf;
+
+ using FPBitsFloat = fputil::FPBits<float>;
+ FPBitsFloat y_bits(y);
+
+ 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();
+ int f = y_bits.get_mantissa() >> (FPBitsFloat::FRACTION_LEN - 5);
+
+ float v = fputil::multiply_add(mant_f, ONE_OVER_F_F[f], -1.0f);
+
+ // Reusing the same log(1+v)/v minimax poly already used in logf16.h.
+ float log1p_d_over_f =
+ v * fputil::polyeval(v, 0x1p+0f, -0x1.001804p-1f, 0x1.557ef6p-2f);
+ float log_1_mant = LOGF_F[f] + log1p_d_over_f;
+ return fputil::cast<float16>(
+ fputil::multiply_add(static_cast<float>(m), LOGF_2, log_1_mant));
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_LOG1PF16_H
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 2c7568f0b6361..1cbcc282f6948 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -405,6 +405,7 @@ add_math_entrypoint_object(log10p1f16)
add_math_entrypoint_object(log1p)
add_math_entrypoint_object(log1pf)
+add_math_entrypoint_object(log1pf16)
add_math_entrypoint_object(log2)
add_math_entrypoint_object(log2f)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 28b8e954530dd..03a6106959b81 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -1791,6 +1791,17 @@ add_entrypoint_object(
libc.src.errno.errno
)
+add_entrypoint_object(
+ log1pf16
+ SRCS
+ log1pf16.cpp
+ HDRS
+ ../log1pf16.h
+ DEPENDS
+ libc.src.__support.math.log1pf16
+ libc.src.errno.errno
+)
+
add_entrypoint_object(
log2p1f16
SRCS
diff --git a/libc/src/math/generic/log1pf16.cpp b/libc/src/math/generic/log1pf16.cpp
new file mode 100644
index 0000000000000..123426202d7dd
--- /dev/null
+++ b/libc/src/math/generic/log1pf16.cpp
@@ -0,0 +1,18 @@
+//===-- Half-precision log(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/log1pf16.h"
+#include "src/__support/math/log1pf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(float16, log1pf16, (float16 x)) {
+ return math::log1pf16(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/log1pf16.h b/libc/src/math/log1pf16.h
new file mode 100644
index 0000000000000..2fdfe19bf7da9
--- /dev/null
+++ b/libc/src/math/log1pf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for log1pf16 -----------------------*- 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_LOG1PF16_H
+#define LLVM_LIBC_SRC_MATH_LOG1PF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+float16 log1pf16(float16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_LOG1PF16_H
</details>
Hi @Sukumarsawant, saw you were fine with someone else picking this up, so I gave it a go here. Tests are passing, would love a review whenever you're free!
Check
once and other merged PRs in the meta-issue
Thanks @Sukumarsawant! Went through the checklist and added the missing
pieces: math.yaml entry, entrypoints on the other platforms, and shared
math library wiring. Tests are passing. Let me know if anything else
is needed!
Please edit the title of the PR, also have you used AI in this ( please read https://llvm.org/docs/AIToolPolicy.html)
Please edit the title of the PR, also have you used AI in this ( please read https://llvm.org/docs/AIToolPolicy.html)
Yup, I used Claude for part of this. I reviewed
and tested everything myself. Added an Assisted-by note and updated
the title.
The title doesn't match what is done in this pr. Please change it. Also, link it to https://github.com/llvm/llvm-project/issues/189543 and close it
at the end of pr description, add:
Close #189543
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:white_check_mark: With the latest revision this PR passed the C/C++ code formatter.
The title doesn't match what is done in this pr. Please change it. Also, link it to #189543 and close it at the end of pr description, add: Close #189543
Done updated both. Let me know if you'd like any changes.
The accuracy tests are failing. You can take a look at the logs.
The accuracy tests are failing. You can take a look at the logs.
Done
Can you update the header comments to match the new LLVM style?
Can you update the header comments to match the new LLVM style?
Use include path relative to libc/ folder.
exclude exceptional checking when LIBC_MATH_HAS_SKIP_ACCURATE_PATH defined (check the exact spelling inside optimization.h)
D
D
exclude exceptional checking whenLIBC_MATH_HAS_SKIP_ACCURATE_PATHdefined (check the exact spelling insideoptimization.h)
Fixed, guarded it with LIBC_MATH_HAS_SKIP_ACCURATE_PASS.
I'm confused because you have the same generated profile icon as me
I'm confused because you have the same generated profile icon as me
Haha yeah, we somehow got the same one 🤣 You’re making me think about changing mine again btw.
Try to debug the CI failures.
Apart from this some comments are marked as resolved even though they aren't yet, please have a look at it.
it seems the CI failure is not related to your changes. please rebase the branch
it seems the CI failure is not related to your changes. please rebase the branch
Done, Thanks!
@hulxv is blocking the PR so I will leave it to him to unblock it.
not blocking but the header comments aren't updated
@usefahmed07 this one's unresolved
seems unnecesary since we are already inside math namespace
@Sukumarsawant , @lntue Looks Good Now!
is this needed ?
smoke test can too be added to bazel
Not needed, removed it.
just a request to addresss the previous comments properly before re-requesting review
>https://llvm.org/docs/CodingStandards.html#file-headers
refer this
This was in context with the headers not updated properly as said in https://github.com/llvm/llvm-project/pull/224152#discussion_r4105611143 and https://github.com/llvm/llvm-project/pull/224152#discussion_r4362412009
just a request to addresss the previous comments properly before re-requesting review
> > https://llvm.org/docs/CodingStandards.html#file-headers
> refer this
> This was in context with the headers not updated properly as said in #224152 (comment) and #224152 (comment)
Sorry, I had only updated the first line before. Added the \file description block to all the log1pf16 file headers, matching the exp2bf16 files.
> just a request to addresss the previous comments properly before re-requesting review
> > https://llvm.org/docs/CodingStandards.html#file-headers
> > refer this
>
>
> This was in context with the headers not updated properly as said in #224152 (comment) and #224152 (comment)
> Sorry, I had only updated the first line before. Added the \file description block to all the log1pf16 file headers, matching the exp2bf16 files.
Can you take a look at the CI failures?
> > just a request to addresss the previous comments properly before re-requesting review
> > > https://llvm.org/docs/CodingStandards.html#file-headers
> > > refer this
> >
> >
> > This was in context with the headers not updated properly as said in #224152 (comment) and #224152 (comment)
>
>
> Sorry, I had only updated the first line before. Added the \file description block to all the log1pf16 file headers, matching the exp2bf16 files.
> Can you take a look at the CI failures?
Done, Thanks!
@usefahmed07 Do mind syncing the PR to resolve merge conflicts?
@Intue Rebased onto latest main and resolved the conflicts. PTAL.
hulxv
usefahmed07
Implements the C23 log1pf16 (log(1+x) for _Float16) math function.
Assisted-by: Claude (Anthropic), human reviewed.
Close #189543