[libc-commits] [libc] [libc][math][c++23] Add log_bf16 math function (PR #157811)
Krishna Pandey via libc-commits
libc-commits at lists.llvm.org
Wed Sep 10 10:46:31 PDT 2025
https://github.com/krishna2803 updated https://github.com/llvm/llvm-project/pull/157811
>From a3a5d6bef1c1c2df23dd9c6661634ec269c37398 Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Wed, 3 Sep 2025 17:34:11 +0530
Subject: [PATCH 01/14] feat: implement sqrtbf16 higher math function
Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
libc/src/math/CMakeLists.txt | 1 +
libc/src/math/generic/CMakeLists.txt | 16 +++++++++++++++-
libc/src/math/generic/sqrtbf16.cpp | 21 +++++++++++++++++++++
libc/src/math/sqrtbf16.h | 21 +++++++++++++++++++++
4 files changed, 58 insertions(+), 1 deletion(-)
create mode 100644 libc/src/math/generic/sqrtbf16.cpp
create mode 100644 libc/src/math/sqrtbf16.h
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 95e5ae781490f..a31e1a2288356 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -552,6 +552,7 @@ add_math_entrypoint_object(sqrtf)
add_math_entrypoint_object(sqrtl)
add_math_entrypoint_object(sqrtf16)
add_math_entrypoint_object(sqrtf128)
+add_math_entrypoint_object(sqrtbf16)
add_math_entrypoint_object(tan)
add_math_entrypoint_object(tanf)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index ba71e5f9e1260..236c5fc01a5da 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3211,7 +3211,21 @@ add_entrypoint_object(
libc.src.__support.macros.optimization
libc.src.__support.macros.properties.types
libc.src.__support.FPUtil.sqrt
- )
+)
+
+add_entrypoint_object(
+ sqrtbf16
+ SRCS
+ sqrtbf16.cpp
+ HDRS
+ ../sqrtbf16.h
+ DEPENDS
+ libc.src.__support.common
+ libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.FPUtil.sqrt
+ libc.src.__support.macros.config
+ libc.src.__support.macros.properties.types
+)
add_entrypoint_object(
remquof
diff --git a/libc/src/math/generic/sqrtbf16.cpp b/libc/src/math/generic/sqrtbf16.cpp
new file mode 100644
index 0000000000000..061e5522a4d8f
--- /dev/null
+++ b/libc/src/math/generic/sqrtbf16.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of sqrtbf16 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/sqrtbf16.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/FPUtil/sqrt.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, sqrtbf16, (bfloat16 x)) {
+ return fputil::sqrt<bfloat16>(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/sqrtbf16.h b/libc/src/math/sqrtbf16.h
new file mode 100644
index 0000000000000..4300e9cab6ad3
--- /dev/null
+++ b/libc/src/math/sqrtbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for sqrtbf16 ----------------------*- 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_SQRTBF16_H
+#define LLVM_LIBC_SRC_MATH_SQRTBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 sqrtbf16(bfloat16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_SQRTBF16_H
>From edb359dbb1c265f751a725bc7b8e1f4db8e24afe Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Wed, 3 Sep 2025 17:36:50 +0530
Subject: [PATCH 02/14] chore: add smoke and general tests for sqrtbf16 higher
math function
Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
libc/test/src/math/CMakeLists.txt | 12 +++++++++
libc/test/src/math/smoke/CMakeLists.txt | 13 ++++++++++
libc/test/src/math/smoke/sqrtbf16_test.cpp | 14 +++++++++++
libc/test/src/math/sqrtbf16_test.cpp | 29 ++++++++++++++++++++++
libc/utils/MPFRWrapper/MPFRUtils.cpp | 7 ++++++
5 files changed, 75 insertions(+)
create mode 100644 libc/test/src/math/smoke/sqrtbf16_test.cpp
create mode 100644 libc/test/src/math/sqrtbf16_test.cpp
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 8e24c5d5c2afd..11323ee161c2a 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -1705,6 +1705,18 @@ add_fp_unittest(
libc.src.math.sqrtf128
)
+add_fp_unittest(
+ sqrtbf16_test
+ NEED_MPFR
+ SUITE
+ libc-math-unittests
+ SRCS
+ sqrtbf16_test.cpp
+ DEPENDS
+ libc.src.math.sqrtbf16
+ libc.src.__support.FPUtil.bfloat16
+)
+
add_fp_unittest(
generic_sqrtf_test
NEED_MPFR
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index a5f856f05bb8c..3be617b732caa 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -3455,6 +3455,19 @@ add_fp_unittest(
libc.src.math.sqrtf128
)
+add_fp_unittest(
+ sqrtbf16_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ sqrtbf16_test.cpp
+ HDRS
+ SqrtTest.h
+ DEPENDS
+ libc.src.math.sqrtbf16
+ libc.src.__support.FPUtil.bfloat16
+)
+
add_fp_unittest(
generic_sqrtf_test
SUITE
diff --git a/libc/test/src/math/smoke/sqrtbf16_test.cpp b/libc/test/src/math/smoke/sqrtbf16_test.cpp
new file mode 100644
index 0000000000000..25629347fee3e
--- /dev/null
+++ b/libc/test/src/math/smoke/sqrtbf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for sqrtbf16 --------------------------------------------===//
+//
+// 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 "SqrtTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/sqrtbf16.h"
+
+LIST_SQRT_TESTS(bfloat16, LIBC_NAMESPACE::sqrtbf16);
diff --git a/libc/test/src/math/sqrtbf16_test.cpp b/libc/test/src/math/sqrtbf16_test.cpp
new file mode 100644
index 0000000000000..186ddd5487133
--- /dev/null
+++ b/libc/test/src/math/sqrtbf16_test.cpp
@@ -0,0 +1,29 @@
+//===-- Exhaustive test for sqrtbf16 --------------------------------------===//
+//
+// 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/FPUtil/bfloat16.h"
+#include "src/math/sqrtbf16.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+using LlvmLibcSqrtf16Test = LIBC_NAMESPACE::testing::FPTest<bfloat16>;
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+// range: [0, inf]
+static constexpr uint16_t POS_START = 0x0000U;
+static constexpr uint16_t POS_STOP = 0x7f80U;
+
+TEST_F(LlvmLibcSqrtf16Test, PositiveRange) {
+ for (uint16_t v = POS_START; v <= POS_STOP; ++v) {
+ bfloat16 x = FPBits(v).get_val();
+ EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sqrt, x,
+ LIBC_NAMESPACE::sqrtbf16(x), 0.5);
+ }
+}
diff --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp
index 2155f324aa8a5..42e505e2fccb7 100644
--- a/libc/utils/MPFRWrapper/MPFRUtils.cpp
+++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp
@@ -272,6 +272,10 @@ template void explain_unary_operation_single_output_error(Operation op,
double, RoundingMode);
#endif // LIBC_TYPES_FLOAT128_IS_NOT_LONG_DOUBLE
+template void explain_unary_operation_single_output_error(Operation op,
+ bfloat16, bfloat16,
+ double, RoundingMode);
+
template <typename T>
void explain_unary_operation_two_outputs_error(
Operation op, T input, const BinaryOutput<T> &libc_result,
@@ -559,6 +563,9 @@ template bool compare_unary_operation_single_output(Operation, float128,
long double, double,
RoundingMode);
#endif // LIBC_TYPES_FLOAT128_IS_NOT_LONG_DOUBLE
+template bool compare_unary_operation_single_output(Operation, bfloat16,
+ bfloat16, double,
+ RoundingMode);
template <typename T>
bool compare_unary_operation_two_outputs(Operation op, T input,
>From f7ff333d82f8ee079cd4b23265759bc9178cee0c Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Wed, 3 Sep 2025 17:39:37 +0530
Subject: [PATCH 03/14] chore: update entrypoints
Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
libc/config/baremetal/aarch64/entrypoints.txt | 1 +
libc/config/baremetal/arm/entrypoints.txt | 1 +
libc/config/baremetal/riscv/entrypoints.txt | 1 +
libc/config/darwin/aarch64/entrypoints.txt | 1 +
libc/config/darwin/x86_64/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/arm/entrypoints.txt | 1 +
libc/config/linux/riscv/entrypoints.txt | 1 +
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/config/windows/entrypoints.txt | 1 +
12 files changed, 12 insertions(+)
diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index c2e4c337f199a..df9f61ca6c108 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -811,6 +811,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.roundevenbf16
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
+ libc.src.math.sqrtbf16
libc.src.math.truncbf16
libc.src.math.ufromfpbf16
libc.src.math.ufromfpxbf16
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index c4f3a87659b23..779f37be1859c 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -814,6 +814,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.roundevenbf16
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
+ libc.src.math.sqrtbf16
libc.src.math.truncbf16
libc.src.math.ufromfpbf16
libc.src.math.ufromfpxbf16
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index 380ca57ea8aa9..d0ced09111f9f 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -814,6 +814,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.roundevenbf16
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
+ libc.src.math.sqrtbf16
libc.src.math.truncbf16
libc.src.math.ufromfpbf16
libc.src.math.ufromfpxbf16
diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index 1f4318fc88389..a9ae541f8ed44 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -644,6 +644,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.roundevenbf16
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
+ libc.src.math.sqrtbf16
libc.src.math.truncbf16
libc.src.math.ufromfpbf16
libc.src.math.ufromfpxbf16
diff --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index 0cba22016c960..120d98a4a6160 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -287,6 +287,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.roundevenbf16
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
+ libc.src.math.sqrtbf16
libc.src.math.truncbf16
libc.src.math.ufromfpbf16
libc.src.math.ufromfpxbf16
diff --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt
index e08b028865bfc..66d0b618fb7cb 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -670,6 +670,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.roundevenbf16
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
+ libc.src.math.sqrtbf16
libc.src.math.truncbf16
libc.src.math.ufromfpbf16
libc.src.math.ufromfpxbf16
diff --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index 88c8fc91ebb77..395c7985feee7 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -672,6 +672,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.roundevenbf16
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
+ libc.src.math.sqrtbf16
libc.src.math.truncbf16
libc.src.math.ufromfpbf16
libc.src.math.ufromfpxbf16
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 20924e9047c69..079466c3b8d81 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -899,6 +899,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.roundevenbf16
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
+ libc.src.math.sqrtbf16
libc.src.math.truncbf16
libc.src.math.ufromfpbf16
libc.src.math.ufromfpxbf16
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index f2e8ddfe8e91a..116f554a6374b 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -514,6 +514,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.roundevenbf16
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
+ libc.src.math.sqrtbf16
libc.src.math.truncbf16
libc.src.math.ufromfpbf16
libc.src.math.ufromfpxbf16
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 0ad36a667232a..e5665a7cd139b 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -917,6 +917,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.roundevenbf16
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
+ libc.src.math.sqrtbf16
libc.src.math.truncbf16
libc.src.math.ufromfpbf16
libc.src.math.ufromfpxbf16
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index c060e900472eb..73ad93d3fdf18 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -950,6 +950,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.roundevenbf16
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
+ libc.src.math.sqrtbf16
libc.src.math.truncbf16
libc.src.math.ufromfpbf16
libc.src.math.ufromfpxbf16
diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index 9e45b800b10a3..c742d88f8c90d 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -360,6 +360,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.roundevenbf16
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
+ libc.src.math.sqrtbf16
libc.src.math.truncbf16
libc.src.math.ufromfpbf16
libc.src.math.ufromfpxbf16
>From 83ebd0f30be2b8e16a3f468b837f50ff8cab1c8c Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Wed, 3 Sep 2025 17:47:02 +0530
Subject: [PATCH 04/14] docs: add sqrtbf16 higher math function
Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
libc/docs/headers/math/index.rst | 214 +++++++++++++++----------------
1 file changed, 107 insertions(+), 107 deletions(-)
diff --git a/libc/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst
index b329bf031312d..016d96ce69d4b 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -255,113 +255,113 @@ Basic Operations
Higher Math Functions
=====================
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| <Func> | <Func_f> (float) | <Func> (double) | <Func_l> (long double) | <Func_f16> (float16) | <Func_f128> (float128) | C23 Definition Section | C23 Error Handling Section |
-+===========+==================+=================+========================+======================+========================+========================+============================+
-| acos | |check| | |check| | | |check| | | 7.12.4.1 | F.10.1.1 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| acosh | |check| | | | |check| | | 7.12.5.1 | F.10.2.1 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| acospi | | | | |check| | | 7.12.4.8 | F.10.1.8 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| asin | |check| | |check| | | |check| | | 7.12.4.2 | F.10.1.2 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| asinh | |check| | | | |check| | | 7.12.5.2 | F.10.2.2 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| asinpi | | | | |check| | | 7.12.4.9 | F.10.1.9 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| atan | |check| | 1 ULP | | |check| | | 7.12.4.3 | F.10.1.3 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| atan2 | |check| | 1 ULP | | | 1 ULP | 7.12.4.4 | F.10.1.4 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| atan2pi | | | | | | 7.12.4.11 | F.10.1.11 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| atanh | |check| | | | |check| | | 7.12.5.3 | F.10.2.3 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| atanpi | | | | | | 7.12.4.10 | F.10.1.10 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| cbrt | |check| | |check| | | | | 7.12.7.1 | F.10.4.1 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| compoundn | | | | | | 7.12.7.2 | F.10.4.2 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| cos | |check| | |check| | | |check| | | 7.12.4.5 | F.10.1.5 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| cosh | |check| | | | |check| | | 7.12.5.4 | F.10.2.4 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| cospi | |check| | | | |check| | | 7.12.4.12 | F.10.1.12 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| dsqrt | N/A | N/A | |check| | N/A | |check|\* | 7.12.14.6 | F.10.11 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| erf | |check| | | | | | 7.12.8.1 | F.10.5.1 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| erfc | | | | | | 7.12.8.2 | F.10.5.2 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| exp | |check| | |check| | | |check| | | 7.12.6.1 | F.10.3.1 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| exp10 | |check| | |check| | | |check| | | 7.12.6.2 | F.10.3.2 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| exp10m1 | |check| | | | |check| | | 7.12.6.3 | F.10.3.3 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| exp2 | |check| | |check| | | |check| | | 7.12.6.4 | F.10.3.4 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| exp2m1 | |check| | | | |check| | | 7.12.6.5 | F.10.3.5 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| expm1 | |check| | |check| | | |check| | | 7.12.6.6 | F.10.3.6 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| fma | |check| | |check| | | |check| | | 7.12.13.1 | F.10.10.1 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| f16sqrt | |check|\* | |check|\* | |check|\* | N/A | |check| | 7.12.14.6 | F.10.11 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| fsqrt | N/A | |check| | |check| | N/A | |check|\* | 7.12.14.6 | F.10.11 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| hypot | |check| | |check| | | |check| | | 7.12.7.4 | F.10.4.4 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| lgamma | | | | | | 7.12.8.3 | F.10.5.3 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| log | |check| | |check| | | |check| | | 7.12.6.11 | F.10.3.11 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| log10 | |check| | |check| | | |check| | | 7.12.6.12 | F.10.3.12 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| log10p1 | | | | | | 7.12.6.13 | F.10.3.13 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| log1p | |check| | |check| | | | | 7.12.6.14 | F.10.3.14 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| log2 | |check| | |check| | | |check| | | 7.12.6.15 | F.10.3.15 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| log2p1 | | | | | | 7.12.6.16 | F.10.3.16 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| logp1 | | | | | | 7.12.6.14 | F.10.3.14 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| pow | |check| | 1 ULP | | | | 7.12.7.5 | F.10.4.5 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| powi\* | | | | | | | |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| pown | | | | | | 7.12.7.6 | F.10.4.6 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| powr | | | | | | 7.12.7.7 | F.10.4.7 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| rootn | | | | | | 7.12.7.8 | F.10.4.8 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| rsqrt | | | | | | 7.12.7.9 | F.10.4.9 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| sin | |check| | |check| | | |check| | | 7.12.4.6 | F.10.1.6 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| sincos | |check| | |check| | | | | | |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| sinh | |check| | | | |check| | | 7.12.5.5 | F.10.2.5 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| sinpi | |check| | | | |check| | | 7.12.4.13 | F.10.1.13 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| sqrt | |check| | |check| | |check| | |check| | |check| | 7.12.7.10 | F.10.4.10 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| tan | |check| | |check| | | |check| | | 7.12.4.7 | F.10.1.7 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| tanh | |check| | | | |check| | | 7.12.5.6 | F.10.2.6 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| tanpi | |check| | | | |check| | | 7.12.4.14 | F.10.1.14 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| tgamma | | | | | | 7.12.8.4 | F.10.5.4 |
-+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+----------++------------+------------------------+----------------------------+
+| <Func> | <Func_f> (float) | <Func> (double) | <Func_l> (long double) | <Func_f16> (float16) | <Func_f128> (float128) | <Func_bf16> (bfloat16) | C23 Definition Section | C23 Error Handling Section |
++===========+==================+=================+========================+======================+========================+========================+========================+============================+
+| acos | |check| | |check| | | |check| | | | 7.12.4.1 | F.10.1.1 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| acosh | |check| | | | |check| | | | 7.12.5.1 | F.10.2.1 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| acospi | | | | |check| | | | 7.12.4.8 | F.10.1.8 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| asin | |check| | |check| | | |check| | | | 7.12.4.2 | F.10.1.2 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| asinh | |check| | | | |check| | | | 7.12.5.2 | F.10.2.2 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| asinpi | | | | |check| | | | 7.12.4.9 | F.10.1.9 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| atan | |check| | 1 ULP | | |check| | | | 7.12.4.3 | F.10.1.3 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| atan2 | |check| | 1 ULP | | | 1 ULP | | 7.12.4.4 | F.10.1.4 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| atan2pi | | | | | | | 7.12.4.11 | F.10.1.11 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| atanh | |check| | | | |check| | | | 7.12.5.3 | F.10.2.3 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| atanpi | | | | | | | 7.12.4.10 | F.10.1.10 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| cbrt | |check| | |check| | | | | | 7.12.7.1 | F.10.4.1 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| compoundn | | | | | | | 7.12.7.2 | F.10.4.2 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| cos | |check| | |check| | | |check| | | | 7.12.4.5 | F.10.1.5 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| cosh | |check| | | | |check| | | | 7.12.5.4 | F.10.2.4 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| cospi | |check| | | | |check| | | | 7.12.4.12 | F.10.1.12 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| dsqrt | N/A | N/A | |check| | N/A | |check|\* | | 7.12.14.6 | F.10.11 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| erf | |check| | | | | | | 7.12.8.1 | F.10.5.1 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| erfc | | | | | | | 7.12.8.2 | F.10.5.2 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| exp | |check| | |check| | | |check| | | | 7.12.6.1 | F.10.3.1 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| exp10 | |check| | |check| | | |check| | | | 7.12.6.2 | F.10.3.2 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| exp10m1 | |check| | | | |check| | | | 7.12.6.3 | F.10.3.3 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| exp2 | |check| | |check| | | |check| | | | 7.12.6.4 | F.10.3.4 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| exp2m1 | |check| | | | |check| | | | 7.12.6.5 | F.10.3.5 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| expm1 | |check| | |check| | | |check| | | | 7.12.6.6 | F.10.3.6 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| fma | |check| | |check| | | |check| | | | 7.12.13.1 | F.10.10.1 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| f16sqrt | |check|\* | |check|\* | |check|\* | N/A | |check| | | 7.12.14.6 | F.10.11 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| fsqrt | N/A | |check| | |check| | N/A | |check|\* | | 7.12.14.6 | F.10.11 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| hypot | |check| | |check| | | |check| | | | 7.12.7.4 | F.10.4.4 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| lgamma | | | | | | | 7.12.8.3 | F.10.5.3 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| log | |check| | |check| | | |check| | | | 7.12.6.11 | F.10.3.11 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| log10 | |check| | |check| | | |check| | | | 7.12.6.12 | F.10.3.12 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| log10p1 | | | | | | | 7.12.6.13 | F.10.3.13 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| log1p | |check| | |check| | | | | | 7.12.6.14 | F.10.3.14 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| log2 | |check| | |check| | | |check| | | | 7.12.6.15 | F.10.3.15 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| log2p1 | | | | | | | 7.12.6.16 | F.10.3.16 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| logp1 | | | | | | | 7.12.6.14 | F.10.3.14 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| pow | |check| | 1 ULP | | | | | 7.12.7.5 | F.10.4.5 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| powi\* | | | | | | | | |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| pown | | | | | | | 7.12.7.6 | F.10.4.6 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| powr | | | | | | | 7.12.7.7 | F.10.4.7 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| rootn | | | | | | | 7.12.7.8 | F.10.4.8 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| rsqrt | | | | | | | 7.12.7.9 | F.10.4.9 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| sin | |check| | |check| | | |check| | | | 7.12.4.6 | F.10.1.6 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| sincos | |check| | |check| | | | | | | |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| sinh | |check| | | | |check| | | | 7.12.5.5 | F.10.2.5 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| sinpi | |check| | | | |check| | | | 7.12.4.13 | F.10.1.13 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| sqrt | |check| | |check| | |check| | |check| | |check| | |check| | 7.12.7.10 | F.10.4.10 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| tan | |check| | |check| | | |check| | | | 7.12.4.7 | F.10.1.7 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| tanh | |check| | | | |check| | | | 7.12.5.6 | F.10.2.6 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| tanpi | |check| | | | |check| | | | 7.12.4.14 | F.10.1.14 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
+| tgamma | | | | | | | 7.12.8.4 | F.10.5.4 |
++-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
Legends:
>From 67ef3b83d2d21852fcde50b91e50d7a290ccce0b Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Thu, 4 Sep 2025 00:58:21 +0530
Subject: [PATCH 05/14] docs: check atanpi
Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
libc/docs/headers/math/index.rst | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst
index f2110a0fdb660..2b0f0228ac661 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -278,7 +278,7 @@ Higher Math Functions
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
| atanh | |check| | | | |check| | | | 7.12.5.3 | F.10.2.3 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| atanpi | | | | | | | 7.12.4.10 | F.10.1.10 |
+| atanpi | | | | |check| | | | 7.12.4.10 | F.10.1.10 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
| cbrt | |check| | |check| | | | | | 7.12.7.1 | F.10.4.1 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
>From 9660c6dc2c12b9436b887f9a13bd075d69629c5a Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Wed, 10 Sep 2025 14:01:52 +0530
Subject: [PATCH 06/14] feat: implement log_bf16 higher math function
Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
libc/src/math/CMakeLists.txt | 1 +
libc/src/math/generic/CMakeLists.txt | 16 ++++
libc/src/math/generic/log_bf16.cpp | 129 +++++++++++++++++++++++++++
libc/src/math/log_bf16.h | 21 +++++
4 files changed, 167 insertions(+)
create mode 100644 libc/src/math/generic/log_bf16.cpp
create mode 100644 libc/src/math/log_bf16.h
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index e418a8b0e24b9..ba2fd428d3065 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -392,6 +392,7 @@ add_math_entrypoint_object(log2f16)
add_math_entrypoint_object(log)
add_math_entrypoint_object(logf)
add_math_entrypoint_object(logf16)
+add_math_entrypoint_object(log_bf16)
add_math_entrypoint_object(logb)
add_math_entrypoint_object(logbf)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 263c5dfd0832b..627009a400983 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -2272,6 +2272,22 @@ add_entrypoint_object(
libc.src.__support.math.expxf16_utils
)
+add_entrypoint_object(
+ log_bf16
+ SRCS
+ log_bf16.cpp
+ HDRS
+ ../log_bf16.h
+ DEPENDS
+ libc.src.__support.common
+ libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.FPUtil.cast
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.FPUtil.multiply_add
+ libc.src.__support.macros.config
+ libc.src.__support.macros.optimization
+)
+
add_entrypoint_object(
logb
SRCS
diff --git a/libc/src/math/generic/log_bf16.cpp b/libc/src/math/generic/log_bf16.cpp
new file mode 100644
index 0000000000000..a333fa412162f
--- /dev/null
+++ b/libc/src/math/generic/log_bf16.cpp
@@ -0,0 +1,129 @@
+//===-- BFloat16 log(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/log_bf16.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/FPUtil/cast.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"
+
+namespace LIBC_NAMESPACE_DECL {
+
+// Generated by Sollya with the following commands:
+// > display = hexadecimal;
+// > round(log(2), SG, RN);
+static constexpr float LOGF_2 = 0x1.62e43p-1f;
+
+// Generated by Sollya with the following commands:
+// > display = hexadecimal;
+// > for i from 0 to 127 do print(round(log(1 + i * 2^-7), SG, RN));
+static constexpr float LOG_1_PLUS_M[128] = {
+ 0x0f, 0x1.fe02a6p-8f, 0x1.fc0a8cp-7f, 0x1.7b91bp-6f,
+ 0x1.f829bp-6f, 0x1.39e87cp-5f, 0x1.77459p-5f, 0x1.b42dd8p-5f,
+ 0x1.f0a30cp-5f, 0x1.16536ep-4f, 0x1.341d7ap-4f, 0x1.51b074p-4f,
+ 0x1.6f0d28p-4f, 0x1.8c345ep-4f, 0x1.a926d4p-4f, 0x1.c5e548p-4f,
+ 0x1.e27076p-4f, 0x1.fec914p-4f, 0x1.0d77e8p-3f, 0x1.1b72aep-3f,
+ 0x1.29553p-3f, 0x1.371fc2p-3f, 0x1.44d2b6p-3f, 0x1.526e5ep-3f,
+ 0x1.5ff308p-3f, 0x1.6d60fep-3f, 0x1.7ab89p-3f, 0x1.87fa06p-3f,
+ 0x1.9525aap-3f, 0x1.a23bc2p-3f, 0x1.af3c94p-3f, 0x1.bc2868p-3f,
+ 0x1.c8ff7cp-3f, 0x1.d5c216p-3f, 0x1.e27076p-3f, 0x1.ef0adcp-3f,
+ 0x1.fb9186p-3f, 0x1.04025ap-2f, 0x1.0a324ep-2f, 0x1.1058cp-2f,
+ 0x1.1675cap-2f, 0x1.1c898cp-2f, 0x1.22942p-2f, 0x1.2895a2p-2f,
+ 0x1.2e8e2cp-2f, 0x1.347ddap-2f, 0x1.3a64c6p-2f, 0x1.404308p-2f,
+ 0x1.4618bcp-2f, 0x1.4be5fap-2f, 0x1.51aad8p-2f, 0x1.576772p-2f,
+ 0x1.5d1bdcp-2f, 0x1.62c83p-2f, 0x1.686c82p-2f, 0x1.6e08eap-2f,
+ 0x1.739d8p-2f, 0x1.792a56p-2f, 0x1.7eaf84p-2f, 0x1.842d1ep-2f,
+ 0x1.89a338p-2f, 0x1.8f11e8p-2f, 0x1.947942p-2f, 0x1.99d958p-2f,
+ 0x1.9f323ep-2f, 0x1.a4840ap-2f, 0x1.a9cecap-2f, 0x1.af1294p-2f,
+ 0x1.b44f78p-2f, 0x1.b9858ap-2f, 0x1.beb4dap-2f, 0x1.c3dd7ap-2f,
+ 0x1.c8ff7cp-2f, 0x1.ce1afp-2f, 0x1.d32fe8p-2f, 0x1.d83e72p-2f,
+ 0x1.dd46ap-2f, 0x1.e24882p-2f, 0x1.e74426p-2f, 0x1.ec399ep-2f,
+ 0x1.f128f6p-2f, 0x1.f6124p-2f, 0x1.faf588p-2f, 0x1.ffd2ep-2f,
+ 0x1.02552ap-1f, 0x1.04bdfap-1f, 0x1.0723e6p-1f, 0x1.0986f4p-1f,
+ 0x1.0be72ep-1f, 0x1.0e4498p-1f, 0x1.109f3ap-1f, 0x1.12f71ap-1f,
+ 0x1.154c3ep-1f, 0x1.179eacp-1f, 0x1.19ee6cp-1f, 0x1.1c3b82p-1f,
+ 0x1.1e85f6p-1f, 0x1.20cdcep-1f, 0x1.23130ep-1f, 0x1.2555bcp-1f,
+ 0x1.2795e2p-1f, 0x1.29d38p-1f, 0x1.2c0e9ep-1f, 0x1.2e4744p-1f,
+ 0x1.307d74p-1f, 0x1.32b134p-1f, 0x1.34e28ap-1f, 0x1.37117cp-1f,
+ 0x1.393e0ep-1f, 0x1.3b6844p-1f, 0x1.3d9026p-1f, 0x1.3fb5b8p-1f,
+ 0x1.41d8fep-1f, 0x1.43f9fep-1f, 0x1.4618bcp-1f, 0x1.48353ep-1f,
+ 0x1.4a4f86p-1f, 0x1.4c679ap-1f, 0x1.4e7d82p-1f, 0x1.50913cp-1f,
+ 0x1.52a2d2p-1f, 0x1.54b246p-1f, 0x1.56bf9ep-1f, 0x1.58cadcp-1f,
+ 0x1.5ad404p-1f, 0x1.5cdb1ep-1f, 0x1.5ee02ap-1f, 0x1.60e33p-1f,
+};
+
+LLVM_LIBC_FUNCTION(bfloat16, log_bf16, (bfloat16 x)) {
+ using FPBits = fputil::FPBits<bfloat16>;
+ FPBits x_bits(x);
+
+ uint16_t x_u = x_bits.uintval();
+
+ // If x <= 0, or x is 1, or x is +inf, or x is NaN.
+ if (LIBC_UNLIKELY(x_u == 0U || x_u == 0x3f80 || x_u >= 0x7f80)) {
+ // log(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;
+ }
+
+ // log(+/-0) = −inf
+ if ((x_u & 0x7fffU) == 0U) {
+ fputil::raise_except_if_required(FE_DIVBYZERO);
+ return FPBits::inf(Sign::NEG).get_val();
+ }
+
+ // log(1) = 0
+ if (x_u == 0x3f80)
+ return FPBits::zero().get_val();
+
+ // x < 0
+ if (x_u > 0x8000U) {
+ fputil::set_errno_if_required(EDOM);
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+
+ // log(+inf) = +inf
+ return FPBits::inf().get_val();
+ }
+
+ int e = -FPBits::EXP_BIAS;
+
+ // When x is subnormal, normalize it.
+ if ((x_u & FPBits::EXP_MASK) == 0U) {
+ // Can't pass an integer to fputil::cast directly.
+ constexpr float NORMALIZE_EXP = 1U << FPBits::FRACTION_LEN;
+ x_bits = FPBits(x_bits.get_val() * fputil::cast<bfloat16>(NORMALIZE_EXP));
+ x_u = x_bits.uintval();
+ e -= FPBits::FRACTION_LEN;
+ }
+
+ // To compute log(x), we perform the following range reduction:
+ // x = 2^e * (1 + m),
+ // log(x) = e * log(2) + log(1 + m).
+ // for BFloat16, mantissa is at most 7 explicit bits, so we lookup
+ // log(1 + m) in LOG_1_PLUS_M table using `m` as key.
+
+ // Get the 7-bit mantissa directly as the table index
+ uint16_t m = x_bits.get_mantissa();
+
+ // Get unbiased exponent
+ e += x_u >> FPBits::FRACTION_LEN;
+
+ return fputil::cast<bfloat16>(
+ fputil::multiply_add(static_cast<float>(e), LOGF_2, LOG_1_PLUS_M[m]));
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/log_bf16.h b/libc/src/math/log_bf16.h
new file mode 100644
index 0000000000000..6f6b8e4cc55fe
--- /dev/null
+++ b/libc/src/math/log_bf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for BFloat16 log(x) function ------*- 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_LOG_BF16_H
+#define LLVM_LIBC_SRC_MATH_LOG_BF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 log_bf16(bfloat16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_LOG_BF16_H
>From fe1fdfd929e0f7223dee8c15910383a54e7f7750 Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Wed, 10 Sep 2025 14:04:49 +0530
Subject: [PATCH 07/14] chore: add smoke tests for log_bf16 higher math
function
Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
libc/test/src/math/smoke/CMakeLists.txt | 14 +++++++
libc/test/src/math/smoke/log_bf16_test.cpp | 48 ++++++++++++++++++++++
2 files changed, 62 insertions(+)
create mode 100644 libc/test/src/math/smoke/log_bf16_test.cpp
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index c9cbd8b413967..a7954aeec88c0 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -4333,6 +4333,20 @@ add_fp_unittest(
libc.src.__support.FPUtil.cast
)
+add_fp_unittest(
+ log_bf16_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ log_bf16_test.cpp
+ DEPENDS
+ libc.hdr.fenv_macros
+ libc.src.errno.errno
+ libc.src.math.log_bf16
+ libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.FPUtil.cast
+)
+
add_fp_unittest(
log2_test
SUITE
diff --git a/libc/test/src/math/smoke/log_bf16_test.cpp b/libc/test/src/math/smoke/log_bf16_test.cpp
new file mode 100644
index 0000000000000..09337060b57d0
--- /dev/null
+++ b/libc/test/src/math/smoke/log_bf16_test.cpp
@@ -0,0 +1,48 @@
+//===-- Unittests for BFloat16 log(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 "hdr/fenv_macros.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/properties/types.h"
+#include "src/math/log_bf16.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcLogBf16Test = LIBC_NAMESPACE::testing::FPTest<bfloat16>;
+
+TEST_F(LlvmLibcLogBf16Test, SpecialNumbers) {
+ libc_errno = 0;
+
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::log_bf16(aNaN));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::log_bf16(sNaN), FE_INVALID);
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(inf, LIBC_NAMESPACE::log_bf16(inf));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::log_bf16(neg_inf));
+ EXPECT_MATH_ERRNO(EDOM);
+
+ EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(
+ neg_inf, LIBC_NAMESPACE::log_bf16(zero), FE_DIVBYZERO);
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(
+ neg_inf, LIBC_NAMESPACE::log_bf16(neg_zero), FE_DIVBYZERO);
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(zero, LIBC_NAMESPACE::log_bf16(bfloat16(1.0)));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::log_bf16(bfloat16(-1.0)));
+ EXPECT_MATH_ERRNO(EDOM);
+}
>From b63608424d09f35b141543e96cdd5c583f2b1f78 Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Wed, 10 Sep 2025 14:06:11 +0530
Subject: [PATCH 08/14] chore: add full range tests for log_bf16 higher math
function
Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
libc/test/src/math/CMakeLists.txt | 12 ++++++++
libc/test/src/math/log_bf16_test.cpp | 41 ++++++++++++++++++++++++++++
2 files changed, 53 insertions(+)
create mode 100644 libc/test/src/math/log_bf16_test.cpp
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 945bc259573c2..807663bf015ad 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -2036,6 +2036,18 @@ add_fp_unittest(
libc.src.math.logf16
)
+add_fp_unittest(
+ log_bf16_test
+ NEED_MPFR
+ SUITE
+ libc-math-unittests
+ SRCS
+ log_bf16_test.cpp
+ DEPENDS
+ libc.src.math.log_bf16
+ libc.src.__support.FPUtil.bfloat16
+)
+
add_fp_unittest(
log2_test
NEED_MPFR
diff --git a/libc/test/src/math/log_bf16_test.cpp b/libc/test/src/math/log_bf16_test.cpp
new file mode 100644
index 0000000000000..ab91ebef4342f
--- /dev/null
+++ b/libc/test/src/math/log_bf16_test.cpp
@@ -0,0 +1,41 @@
+//===-- Full range tests for BFloat16 log(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/__support/FPUtil/bfloat16.h"
+#include "src/math/log_bf16.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+using LlvmLibcLogBf16Test = LIBC_NAMESPACE::testing::FPTest<bfloat16>;
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+// range: [0, inf]
+static constexpr uint16_t POS_START = 0x0000U;
+static constexpr uint16_t POS_STOP = 0x7f80U;
+
+// range: [-0, -inf]
+static constexpr uint16_t NEG_START = 0x8000U;
+static constexpr uint16_t NEG_STOP = 0xff80U;
+
+TEST_F(LlvmLibcLogBf16Test, PositiveRange) {
+ for (uint16_t v = POS_START; v <= POS_STOP; ++v) {
+ bfloat16 x = FPBits(v).get_val();
+ EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log, x,
+ LIBC_NAMESPACE::log_bf16(x), 0.5);
+ }
+}
+
+TEST_F(LlvmLibcLogBf16Test, NegativeRange) {
+ for (uint16_t v = NEG_START; v <= NEG_STOP; ++v) {
+ bfloat16 x = FPBits(v).get_val();
+ EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log, x,
+ LIBC_NAMESPACE::log_bf16(x), 0.5);
+ }
+}
>From 5e797164604fd0857e4cdfd2cf85a5ad8d4c18a3 Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Wed, 10 Sep 2025 14:07:22 +0530
Subject: [PATCH 09/14] chore: update entrypoints
Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
libc/config/baremetal/aarch64/entrypoints.txt | 1 +
libc/config/baremetal/arm/entrypoints.txt | 1 +
libc/config/baremetal/riscv/entrypoints.txt | 1 +
libc/config/darwin/aarch64/entrypoints.txt | 1 +
libc/config/darwin/x86_64/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/arm/entrypoints.txt | 1 +
libc/config/linux/riscv/entrypoints.txt | 1 +
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/config/windows/entrypoints.txt | 1 +
12 files changed, 12 insertions(+)
diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index 31cc0a9d7248e..2576708458038 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -800,6 +800,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.llogbbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
+ libc.src.math.log_bf16
libc.src.math.logbbf16
libc.src.math.lrintbf16
libc.src.math.lroundbf16
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index 8ec972fc58411..82e257c1d2b0d 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -803,6 +803,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.llogbbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
+ libc.src.math.log_bf16
libc.src.math.logbbf16
libc.src.math.lrintbf16
libc.src.math.lroundbf16
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index 2f1d930497734..c10cc1162cc5a 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -803,6 +803,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.llogbbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
+ libc.src.math.log_bf16
libc.src.math.logbbf16
libc.src.math.lrintbf16
libc.src.math.lroundbf16
diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index 3af3f7ff66874..e3c6c2b30c415 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -633,6 +633,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.llogbbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
+ libc.src.math.log_bf16
libc.src.math.logbbf16
libc.src.math.lrintbf16
libc.src.math.lroundbf16
diff --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index a0881e5b02fe8..e899bf97ea3f6 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -276,6 +276,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.llogbbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
+ libc.src.math.log_bf16
libc.src.math.logbbf16
libc.src.math.lrintbf16
libc.src.math.lroundbf16
diff --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt
index 7e4b1ab6d253f..0dda7d5c683ec 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -659,6 +659,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.llogbbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
+ libc.src.math.log_bf16
libc.src.math.logbbf16
libc.src.math.lrintbf16
libc.src.math.lroundbf16
diff --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index 72a6257283475..6070fb5b17b3c 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -661,6 +661,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.llogbbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
+ libc.src.math.log_bf16
libc.src.math.logbbf16
libc.src.math.lrintbf16
libc.src.math.lroundbf16
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 840d4cec14bbf..e099ec94b19f6 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -888,6 +888,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.llogbbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
+ libc.src.math.log_bf16
libc.src.math.logbbf16
libc.src.math.lrintbf16
libc.src.math.lroundbf16
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 97857986d3874..f04ac40145d3a 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -503,6 +503,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.llogbbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
+ libc.src.math.log_bf16
libc.src.math.logbbf16
libc.src.math.lrintbf16
libc.src.math.lroundbf16
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 653282c7d3935..86b29bd00cc20 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -906,6 +906,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.llogbbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
+ libc.src.math.log_bf16
libc.src.math.logbbf16
libc.src.math.lrintbf16
libc.src.math.lroundbf16
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 758574f560f05..2f5da9653d69a 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -941,6 +941,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.llogbbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
+ libc.src.math.log_bf16
libc.src.math.logbbf16
libc.src.math.lrintbf16
libc.src.math.lroundbf16
diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index b7e6f7be128c4..3a76595b258e2 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -349,6 +349,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.llogbbf16
libc.src.math.llrintbf16
libc.src.math.llroundbf16
+ libc.src.math.log_bf16
libc.src.math.logbbf16
libc.src.math.lrintbf16
libc.src.math.lroundbf16
>From 50b6a8ddf2dda7400eaf3438fde30a1c0f9e6e4f Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Wed, 10 Sep 2025 14:11:07 +0530
Subject: [PATCH 10/14] docs: add log_bf16 higher math function
Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
libc/docs/headers/math/index.rst | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/libc/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst
index 2b0f0228ac661..45817296701cd 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -318,7 +318,7 @@ Higher Math Functions
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
| lgamma | | | | | | | 7.12.8.3 | F.10.5.3 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| log | |check| | |check| | | |check| | | | 7.12.6.11 | F.10.3.11 |
+| log | |check| | |check| | | |check| | | |check| ? | 7.12.6.11 | F.10.3.11 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
| log10 | |check| | |check| | | |check| | | | 7.12.6.12 | F.10.3.12 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
@@ -371,6 +371,7 @@ Legends:
* x ULPs: largest errors recorded.
* N/A: Not defined in the standard or will not be added.
* \*: LLVM libc extension.
+* ? Because of a conflict between float16 logb function and bfloat16 log function, the latter is implemented as `log_bf16`.
..
TODO(lntue): Add a new page to discuss about the algorithms used in the
>From 036887a4aa7496fc7b0e29011987aacb7769d44d Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Wed, 10 Sep 2025 14:11:53 +0530
Subject: [PATCH 11/14] style: add explicit U specifiers to constants
Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
libc/src/math/generic/log_bf16.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libc/src/math/generic/log_bf16.cpp b/libc/src/math/generic/log_bf16.cpp
index a333fa412162f..90672d96df7e5 100644
--- a/libc/src/math/generic/log_bf16.cpp
+++ b/libc/src/math/generic/log_bf16.cpp
@@ -67,7 +67,7 @@ LLVM_LIBC_FUNCTION(bfloat16, log_bf16, (bfloat16 x)) {
uint16_t x_u = x_bits.uintval();
// If x <= 0, or x is 1, or x is +inf, or x is NaN.
- if (LIBC_UNLIKELY(x_u == 0U || x_u == 0x3f80 || x_u >= 0x7f80)) {
+ if (LIBC_UNLIKELY(x_u == 0U || x_u == 0x3f80U || x_u >= 0x7f80U)) {
// log(NaN) = NaN
if (x_bits.is_nan()) {
if (x_bits.is_signaling_nan()) {
@@ -85,7 +85,7 @@ LLVM_LIBC_FUNCTION(bfloat16, log_bf16, (bfloat16 x)) {
}
// log(1) = 0
- if (x_u == 0x3f80)
+ if (x_u == 0x3f80U)
return FPBits::zero().get_val();
// x < 0
>From 0979c7a4e87779c509ec15dbf44f7f9839ba87ca Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Wed, 10 Sep 2025 19:12:28 +0530
Subject: [PATCH 12/14] fix: critical bug where 0x0f was used instead of
0x0.0p0f
Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
libc/src/math/generic/log_bf16.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/src/math/generic/log_bf16.cpp b/libc/src/math/generic/log_bf16.cpp
index 90672d96df7e5..6e90c1b38f056 100644
--- a/libc/src/math/generic/log_bf16.cpp
+++ b/libc/src/math/generic/log_bf16.cpp
@@ -26,7 +26,7 @@ static constexpr float LOGF_2 = 0x1.62e43p-1f;
// > display = hexadecimal;
// > for i from 0 to 127 do print(round(log(1 + i * 2^-7), SG, RN));
static constexpr float LOG_1_PLUS_M[128] = {
- 0x0f, 0x1.fe02a6p-8f, 0x1.fc0a8cp-7f, 0x1.7b91bp-6f,
+ 0x0.0p0f, 0x1.fe02a6p-8f, 0x1.fc0a8cp-7f, 0x1.7b91bp-6f,
0x1.f829bp-6f, 0x1.39e87cp-5f, 0x1.77459p-5f, 0x1.b42dd8p-5f,
0x1.f0a30cp-5f, 0x1.16536ep-4f, 0x1.341d7ap-4f, 0x1.51b074p-4f,
0x1.6f0d28p-4f, 0x1.8c345ep-4f, 0x1.a926d4p-4f, 0x1.c5e548p-4f,
>From 11cc8aa931ae26f41abda4e99def20467e71c367 Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Wed, 10 Sep 2025 22:36:50 +0530
Subject: [PATCH 13/14] fix: NO FMA build
Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
libc/src/math/generic/CMakeLists.txt | 1 +
libc/src/math/generic/log_bf16.cpp | 8 ++++++++
2 files changed, 9 insertions(+)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 627009a400983..7e6743047c00e 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -2250,6 +2250,7 @@ add_entrypoint_object(
libc.src.__support.FPUtil.multiply_add
libc.src.__support.FPUtil.polyeval
libc.src.__support.macros.optimization
+ libc.src.__support.macros.properties.cpu_features
)
add_entrypoint_object(
diff --git a/libc/src/math/generic/log_bf16.cpp b/libc/src/math/generic/log_bf16.cpp
index 6e90c1b38f056..213dccca0fb9e 100644
--- a/libc/src/math/generic/log_bf16.cpp
+++ b/libc/src/math/generic/log_bf16.cpp
@@ -14,6 +14,7 @@
#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 {
@@ -99,6 +100,13 @@ LLVM_LIBC_FUNCTION(bfloat16, log_bf16, (bfloat16 x)) {
return FPBits::inf().get_val();
}
+#ifndef LIBC_TARGET_CPU_HAS_FMA
+ // log(0.00000000000000171390679426508540927898138761520386)
+ // ~= -34.00000095
+ if (LIBC_UNLIKELY(x_u == 0x26F7U))
+ return bfloat16(-34.0000009);
+#endif // LIBC_TARGET_CPU_HAS_FMA
+
int e = -FPBits::EXP_BIAS;
// When x is subnormal, normalize it.
>From c814888aa7910c2661e0d6f18fda93b1ed013bcb Mon Sep 17 00:00:00 2001
From: Krishna Pandey <kpandey81930 at gmail.com>
Date: Wed, 10 Sep 2025 23:16:06 +0530
Subject: [PATCH 14/14] refactor: error headers and unused cast
Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>
---
libc/test/src/math/smoke/CMakeLists.txt | 3 +--
libc/test/src/math/smoke/log_bf16_test.cpp | 3 +--
2 files changed, 2 insertions(+), 4 deletions(-)
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 2b9342eaa4a72..ff60c861145e5 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -4338,11 +4338,10 @@ add_fp_unittest(
SRCS
log_bf16_test.cpp
DEPENDS
+ libc.hdr.errno_macros
libc.hdr.fenv_macros
- libc.src.errno.errno
libc.src.math.log_bf16
libc.src.__support.FPUtil.bfloat16
- libc.src.__support.FPUtil.cast
)
add_fp_unittest(
diff --git a/libc/test/src/math/smoke/log_bf16_test.cpp b/libc/test/src/math/smoke/log_bf16_test.cpp
index 09337060b57d0..4ff752b257cb0 100644
--- a/libc/test/src/math/smoke/log_bf16_test.cpp
+++ b/libc/test/src/math/smoke/log_bf16_test.cpp
@@ -6,10 +6,9 @@
//
//===----------------------------------------------------------------------===//
+#include "hdr/errno_macros.h"
#include "hdr/fenv_macros.h"
#include "src/__support/FPUtil/bfloat16.h"
-#include "src/__support/FPUtil/cast.h"
-#include "src/__support/libc_errno.h"
#include "src/__support/macros/properties/types.h"
#include "src/math/log_bf16.h"
#include "test/UnitTest/FPMatcher.h"
More information about the libc-commits
mailing list