[libc-commits] [libc] [llvm] [libc][math][c23] Add acosbf16 math function (PR #184633)
via libc-commits
libc-commits at lists.llvm.org
Wed Mar 4 07:39:55 PST 2026
https://github.com/Sukumarsawant created https://github.com/llvm/llvm-project/pull/184633
This PR intends to add acosbf16 higher math function for bfloat16 type .
>From d4278a75f1c93261bc3e85cc77c837c4b8ad03fe Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Wed, 4 Mar 2026 00:32:31 +0530
Subject: [PATCH 1/5] feat: implementation (raw)
---
libc/src/__support/math/acosbf16.h | 94 ++++++++++++++++++++++++++++++
1 file changed, 94 insertions(+)
create mode 100644 libc/src/__support/math/acosbf16.h
diff --git a/libc/src/__support/math/acosbf16.h b/libc/src/__support/math/acosbf16.h
new file mode 100644
index 0000000000000..4c551b5ee8560
--- /dev/null
+++ b/libc/src/__support/math/acosbf16.h
@@ -0,0 +1,94 @@
+//===-- Implementation header for acosbf16 ----------------------*- 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_ACOSBF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ACOSBF16_H
+
+#include "src/__support/FPUtil/FEnvImpl.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/FPUtil/sqrt.h"
+#include "src/__support/macros/optimization.h"
+#include "src/__support/math/inv_trigf_utils.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE constexpr bfloat16 acosbf16(bfloat16 x) {
+ // Generated by Sollya using the following command:
+ // > display = hexadecimal;
+ // > round(pi/2, SG, RN);
+ constexpr float PI_2 = 0x1.921fb6p0f;
+ // > round(pi, SG, RN);
+ constexpr float PI = 0x1.921fb6p1;
+
+ using FPBits = fputil::FPBits<bfloat16>;
+ FPBits xbits(x);
+
+ uint16_t x_u = xbits.uintval();
+ uint16_t x_abs = x_u & 0x7fff;
+ bool sign = (x_u>>15);
+ float x_sign = (sign) ? -1 : 1;
+ float xf = x;
+
+ // case 1: |x|>=1, NaN or Inf
+ if (LIBC_UNLIKELY(x_abs >= 0x3F80)) {
+ if (x_abs == 0x3F80) {
+ if(sign) return fputil::cast<bfloat16>(PI);
+ else return fputil::cast<bfloat16>(0.0f);
+ }
+ // NaN
+ if (xbits.is_nan()) {
+ if (xbits.is_signaling_nan()) {
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+ return x; // quiet NaN
+ }
+ // |x|>1 & inf
+ fputil::raise_except_if_required(FE_INVALID);
+ fputil::set_errno_if_required(EDOM); // Domain is bounded
+ return FPBits::quiet_nan().get_val();
+ }
+
+ // case 2: |x| = {0}
+ if (LIBC_UNLIKELY(x_abs == 0)){
+ return fputil::cast<bfloat16>(PI_2);
+ }
+
+ float xf_abs = (xf < 0 ? -xf : xf);
+ float x_sq = xf_abs * xf_abs;
+
+ // case 3: (0,0.5]
+ if (x_abs <= 0x3F00) {
+ double xp = inv_trigf_utils_internal::asin_eval(x_sq);
+ float result =
+ xf * static_cast<float>(fputil::multiply_add<double>(x_sq, xp, 1.0));
+ return fputil::cast<bfloat16>(PI_2 - result);
+ }
+
+ // case 4: (0.5,1)
+ // using reduction: asin(x) = pi/2 - 2*asin(sqrt((1-x)/2))
+ float t = fputil::multiply_add<float>(xf_abs, -0.5f, 0.5f);
+ float t_sqrt = fputil::sqrt<float>(t);
+ double tp = inv_trigf_utils_internal::asin_eval(t);
+ float asin_sqrt_t =
+ t_sqrt * static_cast<float>(fputil::multiply_add<double>(t, tp, 1.0));
+
+ if(sign) return fputil::cast<bfloat16>(fputil::multiply_add(asin_sqrt_t,-2.0f,PI));
+ else return fputil::cast<bfloat16>(2*asin_sqrt_t);
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ACOSBF16_H
\ No newline at end of file
>From 7613a86fd1390dbacf0c9541aa96d2c6e8d0ec76 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Wed, 4 Mar 2026 11:56:34 +0530
Subject: [PATCH 2/5] feat: added smoke and exahaustive tests & improved
acosbf16(x)
---
libc/shared/math.h | 1 +
libc/shared/math/acosbf16.h | 23 ++++++++++
libc/src/__support/math/CMakeLists.txt | 15 ++++++
libc/src/__support/math/acosbf16.h | 3 +-
libc/src/math/CMakeLists.txt | 1 +
libc/src/math/acosbf16.h | 21 +++++++++
libc/src/math/generic/CMakeLists.txt | 11 +++++
libc/src/math/generic/acosbf16.cpp | 20 ++++++++
libc/src/math/generic/asinbf16.cpp | 18 ++++++++
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 2 +
libc/test/src/math/CMakeLists.txt | 13 ++++++
libc/test/src/math/acosbf16_test.cpp | 45 ++++++++++++++++++
libc/test/src/math/smoke/CMakeLists.txt | 13 ++++++
libc/test/src/math/smoke/acosbf16_test.cpp | 46 +++++++++++++++++++
.../llvm-project-overlay/libc/BUILD.bazel | 20 ++++++++
16 files changed, 251 insertions(+), 2 deletions(-)
create mode 100644 libc/shared/math/acosbf16.h
create mode 100644 libc/src/math/acosbf16.h
create mode 100644 libc/src/math/generic/acosbf16.cpp
create mode 100644 libc/src/math/generic/asinbf16.cpp
create mode 100644 libc/test/src/math/acosbf16_test.cpp
create mode 100644 libc/test/src/math/smoke/acosbf16_test.cpp
diff --git a/libc/shared/math.h b/libc/shared/math.h
index a7d735ffa1746..4ad9605202470 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -12,6 +12,7 @@
#include "libc_common.h"
#include "math/acos.h"
+#include "math/acosbf16.h"
#include "math/acosf.h"
#include "math/acosf16.h"
#include "math/acoshf.h"
diff --git a/libc/shared/math/acosbf16.h b/libc/shared/math/acosbf16.h
new file mode 100644
index 0000000000000..779a2b3cb4ac5
--- /dev/null
+++ b/libc/shared/math/acosbf16.h
@@ -0,0 +1,23 @@
+//===-- Shared acosbf16 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_SHARED_MATH_ACOSBF16_H
+#define LLVM_LIBC_SHARED_MATH_ACOSBF16_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/acosbf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::acosbf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ACOSBF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 3bb8e76a54bfb..2fd121fdab235 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -16,6 +16,21 @@ add_header_library(
libc.src.__support.macros.properties.cpu_features
)
+add_header_library(
+ acosbf16
+ HDRS
+ acosf16.h
+ DEPENDS
+ libc.src.__support.FPUtil.FEnvImpl"
+ libc.src.__support.FPUtil.FPBits"
+ libc.src.__support.FPUtil.bfloat16"
+ libc.src.__support.FPUtil.cast"
+ libc.src.__support.FPUtil.multiply_add"
+ libc.src.__support.FPUtil.sqrt"
+ libc.src.__support.macros.optimization"
+ libc.src.__support.math.inv_trigf_utils"
+)
+
add_header_library(
acosf
HDRS
diff --git a/libc/src/__support/math/acosbf16.h b/libc/src/__support/math/acosbf16.h
index 4c551b5ee8560..974acc6871f5e 100644
--- a/libc/src/__support/math/acosbf16.h
+++ b/libc/src/__support/math/acosbf16.h
@@ -35,8 +35,7 @@ LIBC_INLINE constexpr bfloat16 acosbf16(bfloat16 x) {
uint16_t x_u = xbits.uintval();
uint16_t x_abs = x_u & 0x7fff;
- bool sign = (x_u>>15);
- float x_sign = (sign) ? -1 : 1;
+ bool sign = (x_u >> 15);
float xf = x;
// case 1: |x|>=1, NaN or Inf
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index e617950368994..bd272d7f3a9b8 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -41,6 +41,7 @@ function(add_math_entrypoint_object name)
endfunction()
add_math_entrypoint_object(acos)
+add_math_entrypoint_object(acosbf16)
add_math_entrypoint_object(acosf)
add_math_entrypoint_object(acosf16)
diff --git a/libc/src/math/acosbf16.h b/libc/src/math/acosbf16.h
new file mode 100644
index 0000000000000..78597c9ea4cad
--- /dev/null
+++ b/libc/src/math/acosbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for acosbf16 ----------------------*- 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_ACOSBF16_H
+#define LLVM_LIBC_SRC_MATH_ACOSBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 acosbf16(bfloat16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_ACOSBF16_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index f8ec25be61d12..723a19d615873 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -4083,6 +4083,17 @@ add_entrypoint_object(
libc.src.__support.math.asin
)
+add_entrypoint_object(
+ acosbf16
+ SRCS
+ acosbf16.cpp
+ HDRS
+ ../acosbf16.h
+ DEPENDS
+ libc.src.__support.math.acosbf16
+)
+
+
add_entrypoint_object(
acosf
SRCS
diff --git a/libc/src/math/generic/acosbf16.cpp b/libc/src/math/generic/acosbf16.cpp
new file mode 100644
index 0000000000000..84a988cfc641f
--- /dev/null
+++ b/libc/src/math/generic/acosbf16.cpp
@@ -0,0 +1,20 @@
+//===-- Half-precision acosbf16(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/acosbf16.h"
+#include "src/__support/math/acosbf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, acosbf16, (bfloat16 x)) {
+ return math::acosbf16(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/asinbf16.cpp b/libc/src/math/generic/asinbf16.cpp
new file mode 100644
index 0000000000000..c02d62d71869f
--- /dev/null
+++ b/libc/src/math/generic/asinbf16.cpp
@@ -0,0 +1,18 @@
+//===-- Half-precision asinfb16(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/math/asinbf16.h"
+#include "src/math/asinfb16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, asinbf16, (bfloat16 x)) {
+ return math::asinbf16(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index a04a15cdabcb7..7866cbb966e95 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -9,6 +9,7 @@ add_fp_unittest(
DEPENDS
libc.src.__support.FPUtil.fp_bits
libc.src.__support.math.acos
+ libc.src.__support.math.acosbf16
libc.src.__support.math.acosf
libc.src.__support.math.acosf16
libc.src.__support.math.acoshf
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 460449e4fcb2e..b4d5698cade05 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -406,6 +406,8 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) {
TEST(LlvmLibcSharedMathTest, AllBFloat16) {
using FPBits = LIBC_NAMESPACE::fputil::FPBits<bfloat16>;
+ EXPECT_FP_EQ(bfloat16(0x1.921fb6p0f),
+ LIBC_NAMESPACE::shared::acosbf16(bfloat16(0.0f)));
EXPECT_FP_EQ(bfloat16(5.0), LIBC_NAMESPACE::shared::bf16add(2.0, 3.0));
EXPECT_FP_EQ(bfloat16(2.0f), LIBC_NAMESPACE::shared::bf16divf(4.0f, 2.0f));
EXPECT_FP_EQ(bfloat16(2.0), LIBC_NAMESPACE::shared::bf16divl(6.0L, 3.0L));
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 73b5ebf5a856e..728b6f982b93c 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -2498,6 +2498,19 @@ add_fp_unittest(
libc.src.math.asinpif16
)
+add_fp_unittest(
+ acosbf16_test
+ NEED_MPFR
+ SUITE
+ libc-math-unittests
+ SRCS
+ acosbf16_test.cpp
+ DEPENDS
+ libc.src.math.acosbf16
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.FPUtil.bfloat16
+)
+
add_fp_unittest(
acosf_test
NEED_MPFR
diff --git a/libc/test/src/math/acosbf16_test.cpp b/libc/test/src/math/acosbf16_test.cpp
new file mode 100644
index 0000000000000..1b861a4d73e24
--- /dev/null
+++ b/libc/test/src/math/acosbf16_test.cpp
@@ -0,0 +1,45 @@
+//===-- Unittests for acosbf16 --------------------------------------------===//
+//
+// 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/acosbf16.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+using LlvmLibcAcosBf16Test = LIBC_NAMESPACE::testing::FPTest<bfloat16>;
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+// Normal range: [+0, +int]
+static constexpr uint16_t POS_START = 0x0000U;
+static constexpr uint16_t POS_STOP = 0x7f80U;
+
+// Normal range: [-0, -int]
+static constexpr uint16_t NEG_START = 0x8000U;
+static constexpr uint16_t NEG_STOP = 0xff80U;
+
+TEST_F(LlvmLibcAcosBf16Test, NormalPositiveRange) {
+ for (uint16_t v1 = POS_START; v1 <= POS_STOP; v1++) {
+
+ bfloat16 x = FPBits(v1).get_val();
+
+ EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Acos, x,
+ LIBC_NAMESPACE::acosbf16(x), 0.5);
+ }
+}
+
+TEST_F(LlvmLibcAcosBf16Test, NormalNegativeRange) {
+ for (uint16_t v1 = NEG_START; v1 <= NEG_STOP; v1++) {
+
+ bfloat16 x = FPBits(v1).get_val();
+
+ EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Acos, x,
+ LIBC_NAMESPACE::acosbf16(x), 0.5);
+ }
+}
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 3d52873c40bb8..86c673209e1ff 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -48,6 +48,19 @@ add_fp_unittest(
libc.src.__support.FPUtil.cast
)
+add_fp_unittest(
+ sinbf16_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ sinbf16_test.cpp
+ DEPENDS
+ libc.src.math.sinbf16
+ libc.hdr.errno_macros
+ libc.hdr.fenv_macros
+ libc.src.__support.FPUtil.bfloat16
+)
+
add_fp_unittest(
sinf_test
SUITE
diff --git a/libc/test/src/math/smoke/acosbf16_test.cpp b/libc/test/src/math/smoke/acosbf16_test.cpp
new file mode 100644
index 0000000000000..9a6a76407dddc
--- /dev/null
+++ b/libc/test/src/math/smoke/acosbf16_test.cpp
@@ -0,0 +1,46 @@
+//===-- Unittests for acosbf16 --------------------------------------------===//
+//
+// 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/acosbf16.h"
+#include "test/UnitTest/FEnvSafeTest.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+class LlvmLibcAcosBf16Test : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+ DECLARE_SPECIAL_CONSTANTS(bfloat16)
+public:
+ void test_special_numbers() {
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::acosbf16(aNaN));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(
+ aNaN, LIBC_NAMESPACE::acosbf16(sNaN), FE_INVALID);
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(bfloat16(0x1.921fb6p0f),
+ LIBC_NAMESPACE::acosbf16(zero));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(bfloat16(0x1.921fb6p0f),
+ LIBC_NAMESPACE::acosbf16(neg_zero));
+ EXPECT_MATH_ERRNO(0);
+
+ bfloat16 VALUES[] = {inf, neg_inf, bfloat16(1.0f), bfloat16(-1.0)};
+ for (size_t i = 0; i < 4; ++i) {
+ bfloat16 x = VALUES[i];
+
+ EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Acos, x,
+ LIBC_NAMESPACE::acosbf16(x), 0.5);
+ }
+ }
+};
+TEST_F(LlvmLibcAcosBf16Test, SpecialNumbers) { test_special_numbers(); }
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 87d1d88e971bf..c565e9acb234c 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2511,6 +2511,21 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_math_acosbf16",
+ hdrs = ["src/__support/math/acosbf16.h"],
+ deps = [
+ ":__support_FPUtil_FEnvImpl",
+ ":__support_FPUtil_FPBits",
+ ":__support_FPUtil_bfloat16",
+ ":__support_FPUtil_cast",
+ ":__support_FPUtil_multiply_add",
+ ":__support_FPUtil_sqrt",
+ ":__support_macros_optimization",
+ ":__support_math_inv_trigf_utils",
+ ],
+)
+
libc_support_library(
name = "__support_math_acosf",
hdrs = ["src/__support/math/acosf.h"],
@@ -5886,6 +5901,11 @@ libc_math_function(
additional_deps = [":__support_math_acos"],
)
+libc_math_function(
+ name = "acosbf16",
+ additional_deps = [":__support_math_acosbf16"],
+)
+
libc_math_function(
name = "acosf",
additional_deps = [":__support_math_acosf"],
>From 12c860cfad7b5e441803b16d8dab9b5ebc9b44dd Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Wed, 4 Mar 2026 11:58:49 +0530
Subject: [PATCH 3/5] chore: entrypoints.txt added acc to support for bf16
chore: entrypoints.txt added acc to support for bf16
t1
t2
t2
t3
---
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 +
libc/src/__support/math/CMakeLists.txt | 18 +++++++++---------
libc/src/__support/math/acosbf16.h | 2 +-
14 files changed, 22 insertions(+), 10 deletions(-)
diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index 278e1f5afde70..f0776bdcc42ce 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -779,6 +779,7 @@ endif()
list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
+ libc.src.math.acosbf16
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index 7bc92e84b7fa4..df62e130943b3 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -782,6 +782,7 @@ endif()
list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
+ libc.src.math.acosbf16
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index 29be47dfbfd53..b569450782958 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -777,6 +777,7 @@ endif()
list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
+ libc.src.math.acosbf16
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index b4a1ee70764cb..b0d8ca1cb9882 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -593,6 +593,7 @@ endif()
list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
+ libc.src.math.acosbf16
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
diff --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index e899bf97ea3f6..44309e1380056 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -233,6 +233,7 @@ set(TARGET_LIBM_ENTRYPOINTS
list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
+ libc.src.math.acosbf16
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
diff --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt
index a65b6f0274fd8..44ef487fc9e26 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -618,6 +618,7 @@ endif()
list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
+ libc.src.math.acosbf16
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
diff --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index fee0038c88cc0..84d20f07c7f2a 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -620,6 +620,7 @@ endif()
list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
+ libc.src.math.acosbf16
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 960958b7cf90a..d933fbd5015c7 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -849,6 +849,7 @@ endif()
list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
+ libc.src.math.acosbf16
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index e76bf81670ce5..1f1bf2dc6f3e5 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -462,6 +462,7 @@ set(TARGET_LIBM_ENTRYPOINTS
list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
+ libc.src.math.acosbf16
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index a69d1a1b0a642..4721728829002 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -870,6 +870,7 @@ endif()
list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
+ libc.src.math.acosbf16
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index bb4973628d588..1ad6afadfd300 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -917,6 +917,7 @@ endif()
list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
+ libc.src.math.acosbf16
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index f777fc6c94bc2..a149690fc13d7 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -308,6 +308,7 @@ set(TARGET_LIBM_ENTRYPOINTS
list(APPEND TARGET_LIBM_ENTRYPOINTS
# bfloat16 entrypoints
+ libc.src.math.acosbf16
libc.src.math.bf16add
libc.src.math.bf16addf
libc.src.math.bf16addl
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 2fd121fdab235..d426379864edd 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -19,16 +19,16 @@ add_header_library(
add_header_library(
acosbf16
HDRS
- acosf16.h
+ acosbf16.h
DEPENDS
- libc.src.__support.FPUtil.FEnvImpl"
- libc.src.__support.FPUtil.FPBits"
- libc.src.__support.FPUtil.bfloat16"
- libc.src.__support.FPUtil.cast"
- libc.src.__support.FPUtil.multiply_add"
- libc.src.__support.FPUtil.sqrt"
- libc.src.__support.macros.optimization"
- libc.src.__support.math.inv_trigf_utils"
+ libc.src.__support.FPUtil.FEnvImpl
+ libc.src.__support.FPUtil.FPBits
+ libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.FPUtil.cast
+ libc.src.__support.FPUtil.multiply_add
+ libc.src.__support.FPUtil.sqrt
+ libc.src.__support.macros.optimization
+ libc.src.__support.math.inv_trigf_utils
)
add_header_library(
diff --git a/libc/src/__support/math/acosbf16.h b/libc/src/__support/math/acosbf16.h
index 974acc6871f5e..bec8522faf86b 100644
--- a/libc/src/__support/math/acosbf16.h
+++ b/libc/src/__support/math/acosbf16.h
@@ -90,4 +90,4 @@ LIBC_INLINE constexpr bfloat16 acosbf16(bfloat16 x) {
} // namespace LIBC_NAMESPACE_DECL
-#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ACOSBF16_H
\ No newline at end of file
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ACOSBF16_H
>From 7071c5fdba18164fa01bc50aa027e30e83eb4be8 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Wed, 4 Mar 2026 18:02:44 +0530
Subject: [PATCH 4/5] chore: added to index.rst
chore: added to index.rst
chore: nit
nit
---
libc/docs/headers/math/index.rst | 2 +-
libc/src/__support/math/acosbf16.h | 18 ++++++++-----
libc/src/math/acosbf16.h | 2 +-
libc/src/math/generic/acosbf16.cpp | 3 +--
libc/src/math/generic/asinbf16.cpp | 18 -------------
libc/test/src/math/acosbf16_test.cpp | 4 +--
libc/test/src/math/smoke/CMakeLists.txt | 26 +++++++++----------
libc/test/src/math/smoke/acosbf16_test.cpp | 2 +-
.../llvm-project-overlay/libc/BUILD.bazel | 12 ++++-----
9 files changed, 36 insertions(+), 51 deletions(-)
delete mode 100644 libc/src/math/generic/asinbf16.cpp
diff --git a/libc/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst
index 7ea54fb4d8263..11d644be24ba2 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -263,7 +263,7 @@ Higher Math Functions
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+----------++------------+------------------------+----------------------------+
| <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 |
+| acos | |check| | |check| | | |check| | | |check| | 7.12.4.1 | F.10.1.1 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
| acosh | |check| | | | |check| | | | 7.12.5.1 | F.10.2.1 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/src/__support/math/acosbf16.h b/libc/src/__support/math/acosbf16.h
index bec8522faf86b..7ad7a20355811 100644
--- a/libc/src/__support/math/acosbf16.h
+++ b/libc/src/__support/math/acosbf16.h
@@ -41,8 +41,10 @@ LIBC_INLINE constexpr bfloat16 acosbf16(bfloat16 x) {
// case 1: |x|>=1, NaN or Inf
if (LIBC_UNLIKELY(x_abs >= 0x3F80)) {
if (x_abs == 0x3F80) {
- if(sign) return fputil::cast<bfloat16>(PI);
- else return fputil::cast<bfloat16>(0.0f);
+ if (sign)
+ return fputil::cast<bfloat16>(PI);
+ else
+ return fputil::cast<bfloat16>(0.0f);
}
// NaN
if (xbits.is_nan()) {
@@ -59,7 +61,7 @@ LIBC_INLINE constexpr bfloat16 acosbf16(bfloat16 x) {
}
// case 2: |x| = {0}
- if (LIBC_UNLIKELY(x_abs == 0)){
+ if (LIBC_UNLIKELY(x_abs == 0)) {
return fputil::cast<bfloat16>(PI_2);
}
@@ -75,15 +77,17 @@ LIBC_INLINE constexpr bfloat16 acosbf16(bfloat16 x) {
}
// case 4: (0.5,1)
- // using reduction: asin(x) = pi/2 - 2*asin(sqrt((1-x)/2))
+ // using reduction for acos:
+ // acos(|x|) = 2*asin(sqrt((1 - |x|)/2)),
+ // and acos(x) = acos(|x|) for x >= 0, pi - acos(|x|) for x < 0
float t = fputil::multiply_add<float>(xf_abs, -0.5f, 0.5f);
float t_sqrt = fputil::sqrt<float>(t);
double tp = inv_trigf_utils_internal::asin_eval(t);
float asin_sqrt_t =
t_sqrt * static_cast<float>(fputil::multiply_add<double>(t, tp, 1.0));
-
- if(sign) return fputil::cast<bfloat16>(fputil::multiply_add(asin_sqrt_t,-2.0f,PI));
- else return fputil::cast<bfloat16>(2*asin_sqrt_t);
+
+ return fputil::cast<bfloat16>(
+ (sign) ? fputil::multiply_add(asin_sqrt_t, -2.0f, PI) : 2 * asin_sqrt_t);
}
} // namespace math
diff --git a/libc/src/math/acosbf16.h b/libc/src/math/acosbf16.h
index 78597c9ea4cad..0888a8ee987f6 100644
--- a/libc/src/math/acosbf16.h
+++ b/libc/src/math/acosbf16.h
@@ -2,7 +2,7 @@
//
// 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.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
diff --git a/libc/src/math/generic/acosbf16.cpp b/libc/src/math/generic/acosbf16.cpp
index 84a988cfc641f..f963d7c65b631 100644
--- a/libc/src/math/generic/acosbf16.cpp
+++ b/libc/src/math/generic/acosbf16.cpp
@@ -1,5 +1,4 @@
-//===-- Half-precision acosbf16(x) function
-//--------------------------------===//
+//===-- Implementation of acosbf16 function -------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
diff --git a/libc/src/math/generic/asinbf16.cpp b/libc/src/math/generic/asinbf16.cpp
deleted file mode 100644
index c02d62d71869f..0000000000000
--- a/libc/src/math/generic/asinbf16.cpp
+++ /dev/null
@@ -1,18 +0,0 @@
-//===-- Half-precision asinfb16(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/math/asinbf16.h"
-#include "src/math/asinfb16.h"
-
-namespace LIBC_NAMESPACE_DECL {
-
-LLVM_LIBC_FUNCTION(bfloat16, asinbf16, (bfloat16 x)) {
- return math::asinbf16(x);
-}
-
-} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/math/acosbf16_test.cpp b/libc/test/src/math/acosbf16_test.cpp
index 1b861a4d73e24..e666d3a919f1c 100644
--- a/libc/test/src/math/acosbf16_test.cpp
+++ b/libc/test/src/math/acosbf16_test.cpp
@@ -16,11 +16,11 @@ using LlvmLibcAcosBf16Test = LIBC_NAMESPACE::testing::FPTest<bfloat16>;
namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
-// Normal range: [+0, +int]
+// range: [+0, +inf]
static constexpr uint16_t POS_START = 0x0000U;
static constexpr uint16_t POS_STOP = 0x7f80U;
-// Normal range: [-0, -int]
+// range: [-0, -inf]
static constexpr uint16_t NEG_START = 0x8000U;
static constexpr uint16_t NEG_STOP = 0xff80U;
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 86c673209e1ff..2ade98d9cf20d 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -48,19 +48,6 @@ add_fp_unittest(
libc.src.__support.FPUtil.cast
)
-add_fp_unittest(
- sinbf16_test
- SUITE
- libc-math-smoke-tests
- SRCS
- sinbf16_test.cpp
- DEPENDS
- libc.src.math.sinbf16
- libc.hdr.errno_macros
- libc.hdr.fenv_macros
- libc.src.__support.FPUtil.bfloat16
-)
-
add_fp_unittest(
sinf_test
SUITE
@@ -4797,6 +4784,19 @@ add_fp_unittest(
libc.src.math.asinf16
)
+add_fp_unittest(
+ acosbf16_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ acosbf16_test.cpp
+ DEPENDS
+ libc.src.math.acosbf16
+ libc.hdr.errno_macros
+ libc.hdr.fenv_macros
+ libc.src.__support.FPUtil.bfloat16
+)
+
add_fp_unittest(
acosf_test
SUITE
diff --git a/libc/test/src/math/smoke/acosbf16_test.cpp b/libc/test/src/math/smoke/acosbf16_test.cpp
index 9a6a76407dddc..ec2f9e34f8fbd 100644
--- a/libc/test/src/math/smoke/acosbf16_test.cpp
+++ b/libc/test/src/math/smoke/acosbf16_test.cpp
@@ -2,7 +2,7 @@
//
// 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.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index c565e9acb234c..e6bfbd0e2d4e1 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2515,12 +2515,12 @@ libc_support_library(
name = "__support_math_acosbf16",
hdrs = ["src/__support/math/acosbf16.h"],
deps = [
- ":__support_FPUtil_FEnvImpl",
- ":__support_FPUtil_FPBits",
- ":__support_FPUtil_bfloat16",
- ":__support_FPUtil_cast",
- ":__support_FPUtil_multiply_add",
- ":__support_FPUtil_sqrt",
+ ":__support_fputil_bfloat16",
+ ":__support_fputil_cast",
+ ":__support_fputil_fenv_impl",
+ ":__support_fputil_fp_bits",
+ ":__support_fputil_multiply_add",
+ ":__support_fputil_sqrt",
":__support_macros_optimization",
":__support_math_inv_trigf_utils",
],
>From 0f30ebe4700701ede24de0f2b48fe1c0e7a03c6b Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Wed, 4 Mar 2026 21:07:29 +0530
Subject: [PATCH 5/5] fix: Cmake missing MPFR
---
libc/test/src/math/acosbf16_test.cpp | 4 ++--
libc/test/src/math/smoke/CMakeLists.txt | 1 +
2 files changed, 3 insertions(+), 2 deletions(-)
diff --git a/libc/test/src/math/acosbf16_test.cpp b/libc/test/src/math/acosbf16_test.cpp
index e666d3a919f1c..0bea9fc911b91 100644
--- a/libc/test/src/math/acosbf16_test.cpp
+++ b/libc/test/src/math/acosbf16_test.cpp
@@ -24,7 +24,7 @@ static constexpr uint16_t POS_STOP = 0x7f80U;
static constexpr uint16_t NEG_START = 0x8000U;
static constexpr uint16_t NEG_STOP = 0xff80U;
-TEST_F(LlvmLibcAcosBf16Test, NormalPositiveRange) {
+TEST_F(LlvmLibcAcosBf16Test, PositiveRange) {
for (uint16_t v1 = POS_START; v1 <= POS_STOP; v1++) {
bfloat16 x = FPBits(v1).get_val();
@@ -34,7 +34,7 @@ TEST_F(LlvmLibcAcosBf16Test, NormalPositiveRange) {
}
}
-TEST_F(LlvmLibcAcosBf16Test, NormalNegativeRange) {
+TEST_F(LlvmLibcAcosBf16Test, NegativeRange) {
for (uint16_t v1 = NEG_START; v1 <= NEG_STOP; v1++) {
bfloat16 x = FPBits(v1).get_val();
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 2ade98d9cf20d..08eaa69baf542 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -4786,6 +4786,7 @@ add_fp_unittest(
add_fp_unittest(
acosbf16_test
+ NEED_MPFR
SUITE
libc-math-smoke-tests
SRCS
More information about the libc-commits
mailing list