[libc-commits] [libc] [llvm] [libc][math][c23] Add acosbf16 math function (PR #184633)

via libc-commits libc-commits at lists.llvm.org
Thu Apr 2 07:26:49 PDT 2026


https://github.com/Sukumarsawant updated https://github.com/llvm/llvm-project/pull/184633

>From 8bc74e80569bc7c1aa8eb5e22ddc14915a1dff25 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/9] 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 87c3c2dfa226f108aa65511486e6a5e0996f9baa 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/9] 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            |  6 ++-
 libc/test/shared/CMakeLists.txt               |  1 +
 libc/test/shared/shared_math_test.cpp         |  4 +-
 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, 239 insertions(+), 4 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/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 13ed0ba9518a5..38973c3a873a1 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 6d752b06bcd8e..23e2b23e26771 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 b53817e2a1729..288b1e27d0b1f 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 899c32216e987..c7d050056a868 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -4138,6 +4138,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
index acaeef0c6ef55..306d76af4f6a1 100644
--- a/libc/src/math/generic/asinbf16.cpp
+++ b/libc/src/math/generic/asinbf16.cpp
@@ -1,4 +1,8 @@
+<<<<<<< HEAD
 //===-- Implementation for asinbf16(x) function ---------------------------===//
+=======
+//===-- Half-precision asinfb16(x) function -------------------------------===//
+>>>>>>> e1999663252e (feat: added smoke and exahaustive tests &  improved acosbf16(x))
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,8 +10,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "src/math/asinbf16.h"
 #include "src/__support/math/asinbf16.h"
+#include "src/math/asinfb16.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 8710b0513d23b..44b1ee87e4b2d 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 5dfa7851f6ab6..296bc775d66d5 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -444,8 +444,10 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) {
 
 TEST(LlvmLibcSharedMathTest, AllBFloat16) {
   using FPBits = LIBC_NAMESPACE::fputil::FPBits<bfloat16>;
-  EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::atanbf16(bfloat16(0.0)));
+  EXPECT_FP_EQ(bfloat16(0x1.921fb6p0f),
+               LIBC_NAMESPACE::shared::acosbf16(bfloat16(0.0f)));
   EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::asinbf16(bfloat16(0.0)));
+  EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::atanbf16(bfloat16(0.0)));
   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::bf16div(4.0, 2.0));
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 4213c11eca515..b2dd913cdf864 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -2569,6 +2569,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 28b85b1a25bbd..392fdbd07a421 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 ecfb00d841712..15e586c7b7332 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2847,6 +2847,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"],
@@ -6532,6 +6547,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 e597835a1d2acba1ee48d0f5570fa4a2d9ce8ec4 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/9] 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 82910c320b48a..82a1c68ce4f9b 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -792,6 +792,7 @@ endif()
 list(APPEND TARGET_LIBM_ENTRYPOINTS
   # bfloat16 entrypoints
   libc.src.math.atanbf16
+  libc.src.math.acosbf16
   libc.src.math.asinbf16
   libc.src.math.bf16add
   libc.src.math.bf16addf
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index 4cf34764916f7..72af183aa064f 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -801,6 +801,7 @@ endif()
 list(APPEND TARGET_LIBM_ENTRYPOINTS
   # bfloat16 entrypoints
   libc.src.math.atanbf16
+  libc.src.math.acosbf16
   libc.src.math.asinbf16
   libc.src.math.bf16add
   libc.src.math.bf16addf
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index c4a11c6f87337..f84464c1c055c 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -798,6 +798,7 @@ endif()
 list(APPEND TARGET_LIBM_ENTRYPOINTS
   # bfloat16 entrypoints
   libc.src.math.atanbf16
+  libc.src.math.acosbf16
   libc.src.math.asinbf16
   libc.src.math.bf16add
   libc.src.math.bf16addf
diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index 0888f4b0d922b..b19cee9d5690e 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -611,6 +611,7 @@ endif()
 list(APPEND TARGET_LIBM_ENTRYPOINTS
   # bfloat16 entrypoints
   libc.src.math.atanbf16
+  libc.src.math.acosbf16
   libc.src.math.asinbf16
   libc.src.math.bf16add
   libc.src.math.bf16addf
diff --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index 046d1b409742f..07b932c511a87 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -235,6 +235,7 @@ set(TARGET_LIBM_ENTRYPOINTS
 list(APPEND TARGET_LIBM_ENTRYPOINTS
   # bfloat16 entrypoints
   libc.src.math.atanbf16
+  libc.src.math.acosbf16
   libc.src.math.asinbf16
   libc.src.math.bf16add
   libc.src.math.bf16addf
diff --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt
index 40566029c8a42..e9731fc9741ad 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -641,6 +641,7 @@ endif()
 list(APPEND TARGET_LIBM_ENTRYPOINTS
   # bfloat16 entrypoints
   libc.src.math.atanbf16
+  libc.src.math.acosbf16
   libc.src.math.asinbf16
   libc.src.math.bf16add
   libc.src.math.bf16addf
diff --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index 22f2bc849523e..92e5842e235f0 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -643,6 +643,7 @@ endif()
 list(APPEND TARGET_LIBM_ENTRYPOINTS
   # bfloat16 entrypoints
   libc.src.math.atanbf16
+  libc.src.math.acosbf16
   libc.src.math.asinbf16
   libc.src.math.bf16add
   libc.src.math.bf16addf
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index e683da7300e38..c56cfad0191b6 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -874,6 +874,7 @@ endif()
 list(APPEND TARGET_LIBM_ENTRYPOINTS
   # bfloat16 entrypoints
   libc.src.math.atanbf16
+  libc.src.math.acosbf16
   libc.src.math.asinbf16
   libc.src.math.bf16add
   libc.src.math.bf16addf
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 31dcb31c67c7a..07b488b15a6a7 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -477,6 +477,7 @@ set(TARGET_LIBM_ENTRYPOINTS
 list(APPEND TARGET_LIBM_ENTRYPOINTS
   # bfloat16 entrypoints
   libc.src.math.atanbf16
+  libc.src.math.acosbf16
   libc.src.math.asinbf16
   libc.src.math.bf16add
   libc.src.math.bf16addf
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 5782acb727ee2..cd92c930e3ffb 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -895,6 +895,7 @@ endif()
 list(APPEND TARGET_LIBM_ENTRYPOINTS
   # bfloat16 entrypoints
   libc.src.math.atanbf16
+  libc.src.math.acosbf16
   libc.src.math.asinbf16
   libc.src.math.bf16add
   libc.src.math.bf16addf
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 90a0dade5435a..849502f45dc9a 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -950,6 +950,7 @@ endif()
 list(APPEND TARGET_LIBM_ENTRYPOINTS
   # bfloat16 entrypoints
   libc.src.math.atanbf16
+  libc.src.math.acosbf16
   libc.src.math.asinbf16
   libc.src.math.bf16add
   libc.src.math.bf16addf
diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index e8080bc97c59a..722391d1586c0 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -323,6 +323,7 @@ set(TARGET_LIBM_ENTRYPOINTS
 list(APPEND TARGET_LIBM_ENTRYPOINTS
   # bfloat16 entrypoints
   libc.src.math.atanbf16
+  libc.src.math.acosbf16
   libc.src.math.asinbf16
   libc.src.math.bf16add
   libc.src.math.bf16addf
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 23e2b23e26771..f4179b4a8c9fe 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 e5eb190b90f416487c75466817e6ba614be46417 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/9] 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            |  4 ---
 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(+), 37 deletions(-)

diff --git a/libc/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst
index 14152ac62435d..6391550b78c5f 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -259,7 +259,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
index 306d76af4f6a1..85c41f9ee5b9c 100644
--- a/libc/src/math/generic/asinbf16.cpp
+++ b/libc/src/math/generic/asinbf16.cpp
@@ -1,8 +1,4 @@
-<<<<<<< HEAD
 //===-- Implementation for asinbf16(x) function ---------------------------===//
-=======
-//===-- Half-precision asinfb16(x) function -------------------------------===//
->>>>>>> e1999663252e (feat: added smoke and exahaustive tests &  improved acosbf16(x))
 //
 // 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/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 392fdbd07a421..dd0c0356a664c 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
@@ -4876,6 +4863,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 15e586c7b7332..1d1d130eac9b7 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2851,12 +2851,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 c10ce7c06238395dca3c44df8739ad8330928abf 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/9] 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 dd0c0356a664c..842c253a2e2f2 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -4865,6 +4865,7 @@ add_fp_unittest(
 
 add_fp_unittest(
   acosbf16_test
+  NEED_MPFR
   SUITE
     libc-math-smoke-tests
   SRCS

>From b1d82883edc63df80ef6e5cf521f7bbcc3ed7859 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Wed, 4 Mar 2026 22:09:35 +0530
Subject: [PATCH 6/9] chore: nit

---
 libc/src/__support/math/CMakeLists.txt | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index f4179b4a8c9fe..2c7a7bea58e27 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -21,8 +21,8 @@ add_header_library(
   HDRS
     acosbf16.h
   DEPENDS
-    libc.src.__support.FPUtil.FEnvImpl
-    libc.src.__support.FPUtil.FPBits
+    libc.src.__support.FPUtil.fenv_impl
+    libc.src.__support.FPUtil.fp_bits
     libc.src.__support.FPUtil.bfloat16
     libc.src.__support.FPUtil.cast
     libc.src.__support.FPUtil.multiply_add

>From 9c174008667a25989f40243f4927b0fcbb1ffbba Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Thu, 19 Mar 2026 08:00:52 +0530
Subject: [PATCH 7/9] chore: nits

---
 libc/src/__support/math/acosbf16.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/libc/src/__support/math/acosbf16.h b/libc/src/__support/math/acosbf16.h
index 7ad7a20355811..f00bce19670a9 100644
--- a/libc/src/__support/math/acosbf16.h
+++ b/libc/src/__support/math/acosbf16.h
@@ -19,10 +19,9 @@
 #include "src/__support/math/inv_trigf_utils.h"
 
 namespace LIBC_NAMESPACE_DECL {
-
 namespace math {
 
-LIBC_INLINE constexpr bfloat16 acosbf16(bfloat16 x) {
+LIBC_INLINE bfloat16 acosbf16(bfloat16 x) {
   // Generated by Sollya using the following command:
   // > display = hexadecimal;
   // > round(pi/2, SG, RN);
@@ -91,7 +90,6 @@ LIBC_INLINE constexpr bfloat16 acosbf16(bfloat16 x) {
 }
 
 } // namespace math
-
 } // namespace LIBC_NAMESPACE_DECL
 
 #endif // LLVM_LIBC_SRC___SUPPORT_MATH_ACOSBF16_H

>From 195b6878278238be41ad7c78e3e29e472918709a Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Mon, 30 Mar 2026 13:11:36 +0530
Subject: [PATCH 8/9] nit

---
 libc/src/math/generic/CMakeLists.txt | 1 -
 1 file changed, 1 deletion(-)

diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index c7d050056a868..899d7c832b0f9 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -4148,7 +4148,6 @@ add_entrypoint_object(
     libc.src.__support.math.acosbf16
 )
 
-
 add_entrypoint_object(
   acosf
   SRCS

>From 4b3dbd79ca49a5eb4e1038de8362bbea2f684f9d Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Mon, 30 Mar 2026 13:38:51 +0530
Subject: [PATCH 9/9] Double-->Float + branchings

---
 libc/src/__support/math/acosbf16.h | 81 +++++++++++++++---------------
 1 file changed, 40 insertions(+), 41 deletions(-)

diff --git a/libc/src/__support/math/acosbf16.h b/libc/src/__support/math/acosbf16.h
index f00bce19670a9..38ce867de1f3c 100644
--- a/libc/src/__support/math/acosbf16.h
+++ b/libc/src/__support/math/acosbf16.h
@@ -37,56 +37,55 @@ LIBC_INLINE bfloat16 acosbf16(bfloat16 x) {
   bool sign = (x_u >> 15);
   float xf = x;
 
-  // case 1: |x|>=1, NaN or Inf
-  if (LIBC_UNLIKELY(x_abs >= 0x3F80)) {
+  float xf_abs = (xf < 0 ? -xf : xf);
+  float x_sq = xf_abs * xf_abs;
+
+  // case 1: x <= 0.5
+  if (x_abs <= 0x3F00) {
+    // |x| = {0}
+    if (LIBC_UNLIKELY(x_abs == 0))
+      return fputil::cast<bfloat16>(PI_2);
+
+    float xp = fputil::cast<float>(inv_trigf_utils_internal::asin_eval(x_sq));
+    float result = xf * fputil::multiply_add(x_sq, xp, 1.0f);
+    return fputil::cast<bfloat16>(PI_2 - result);
+  }
+
+  // case 2: 0.5< |x|<= 1.0
+  if (x_abs <= 0x3F80) {
+    // |x| = {1}
     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);
-  }
+    // 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);
+    float tp = fputil::cast<float>(inv_trigf_utils_internal::asin_eval(t));
+    float asin_sqrt_t = t_sqrt * (fputil::multiply_add(t, tp, 1.0f));
 
-  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);
+    return fputil::cast<bfloat16>(
+        (sign) ? fputil::multiply_add(asin_sqrt_t, -2.0f, PI)
+               : 2 * asin_sqrt_t);
   }
-
-  // case 4: (0.5,1)
-  // 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));
-
-  return fputil::cast<bfloat16>(
-      (sign) ? fputil::multiply_add(asin_sqrt_t, -2.0f, PI) : 2 * asin_sqrt_t);
+  // case 3: NaN or Inf
+  // 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
+  }
+  // inf
+  fputil::raise_except_if_required(FE_INVALID);
+  fputil::set_errno_if_required(EDOM); // Domain is bounded
+  return FPBits::quiet_nan().get_val();
 }
 
 } // namespace math



More information about the libc-commits mailing list