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

via libc-commits libc-commits at lists.llvm.org
Sun Jun 7 02:27:11 PDT 2026


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

>From d7db782e4e77ab8884457cf57dae027f66b0d7d3 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Wed, 4 Mar 2026 00:32:31 +0530
Subject: [PATCH 01/13] 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 623f2a83053dbe7774b96c2a17bf870e8eab08f4 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Wed, 4 Mar 2026 11:56:34 +0530
Subject: [PATCH 02/13] 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 e75c4b2d229ba..49d167afe4ba9 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 b6b397c88a256..d43d78aa495e9 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 730c0c06073e1..3ed9501d60a5b 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 4c3eac4c3356c..17c3c0443ce4a 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3853,6 +3853,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 7a5c3a369c2fc..4b34361f0ba43 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -19,6 +19,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 571861ecbdfae..776cad754cce6 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -752,8 +752,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 bb462afb71b91..f19cc916a1298 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 46da65b791918..7858e7e206ab5 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3558,6 +3558,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"],
@@ -9622,6 +9637,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 a47557f517b96e255bc49f3ecb20cc1a6ee7d02a Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Wed, 4 Mar 2026 11:58:49 +0530
Subject: [PATCH 03/13] 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 746bdc0a64ee7..737e02142e134 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -794,6 +794,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 a43bbb865bafd..56f2a370ce7fe 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -805,6 +805,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 fd5d92321a746..812bbd1a2dc14 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -802,6 +802,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 01ae40c898734..8a593569736a3 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -615,6 +615,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 b941c968255e8..d35700d341d1a 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -236,6 +236,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 bbd15a131ca24..0e57c46051751 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/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/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index ade635e095994..5b2a402e7c806 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -645,6 +645,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 4c6b43a3dba8e..fd78ae685ceef 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -909,6 +909,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 49b30ef1830f3..f2820e4dcd947 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -484,6 +484,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 e4116fd4c25ae..4852b9d515177 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -930,6 +930,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 616159737b3f2..c5659cce3207f 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -995,6 +995,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 94d1d00e676d9..d7f66dc58e4db 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -326,6 +326,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 d43d78aa495e9..bc909fb3944df 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 63c0f1d799073d819e1559e0856c8e5700aeeba8 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Wed, 4 Mar 2026 18:02:44 +0530
Subject: [PATCH 04/13] 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 a2465e1de2cc2..a2b7f51da99a1 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -257,7 +257,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 f19cc916a1298..1de4ae31773e1 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
@@ -4888,6 +4875,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 7858e7e206ab5..187b09627e3e1 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3562,12 +3562,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 3c9f67f39016b2f40e472db196ed351cc7bf7ea2 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Wed, 4 Mar 2026 21:07:29 +0530
Subject: [PATCH 05/13] 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 1de4ae31773e1..3f785809b7b7f 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -4877,6 +4877,7 @@ add_fp_unittest(
 
 add_fp_unittest(
   acosbf16_test
+  NEED_MPFR
   SUITE
     libc-math-smoke-tests
   SRCS

>From f41aa81668c0e825644c55ee3ce01b59ecfbb19a Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Wed, 4 Mar 2026 22:09:35 +0530
Subject: [PATCH 06/13] 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 bc909fb3944df..cce07c31d2d29 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 c1ab220a27a3ed8de7ab658e78cf4fc4bb5e21cc Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Thu, 19 Mar 2026 08:00:52 +0530
Subject: [PATCH 07/13] 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 e99007731bc34807017ea7317fe24380ff0ba312 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Mon, 30 Mar 2026 13:11:36 +0530
Subject: [PATCH 08/13] 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 17c3c0443ce4a..677ad87dbc72f 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3863,7 +3863,6 @@ add_entrypoint_object(
     libc.src.__support.math.acosbf16
 )
 
-
 add_entrypoint_object(
   acosf
   SRCS

>From 3143e95511157047cc8ea9b107f1f7e470fbc0c4 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Mon, 30 Mar 2026 13:38:51 +0530
Subject: [PATCH 09/13] 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

>From 849338522fdc1d4cc7f8327ececd9c4c9ef682b1 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Thu, 2 Apr 2026 19:58:50 +0530
Subject: [PATCH 10/13] revert wrong change

---
 libc/src/math/generic/asinbf16.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/math/generic/asinbf16.cpp b/libc/src/math/generic/asinbf16.cpp
index 85c41f9ee5b9c..acaeef0c6ef55 100644
--- a/libc/src/math/generic/asinbf16.cpp
+++ b/libc/src/math/generic/asinbf16.cpp
@@ -6,8 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "src/math/asinbf16.h"
 #include "src/__support/math/asinbf16.h"
-#include "src/math/asinfb16.h"
 
 namespace LIBC_NAMESPACE_DECL {
 

>From 6d1a6b14abb9561e2ca3263222a3af82f3cdd139 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sun, 7 Jun 2026 01:00:18 +0530
Subject: [PATCH 11/13] nits

---
 libc/src/__support/math/acosbf16.h | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/libc/src/__support/math/acosbf16.h b/libc/src/__support/math/acosbf16.h
index 38ce867de1f3c..2763998301731 100644
--- a/libc/src/__support/math/acosbf16.h
+++ b/libc/src/__support/math/acosbf16.h
@@ -27,7 +27,7 @@ LIBC_INLINE bfloat16 acosbf16(bfloat16 x) {
   // > round(pi/2, SG, RN);
   constexpr float PI_2 = 0x1.921fb6p0f;
   // > round(pi, SG, RN);
-  constexpr float PI = 0x1.921fb6p1;
+  constexpr float PI = 0x1.921fb6p1f;
 
   using FPBits = fputil::FPBits<bfloat16>;
   FPBits xbits(x);
@@ -58,7 +58,7 @@ LIBC_INLINE bfloat16 acosbf16(bfloat16 x) {
       if (sign)
         return fputil::cast<bfloat16>(PI);
       else
-        return fputil::cast<bfloat16>(0.0f);
+        return FPBits::zero().get_val();
     }
 
     // using reduction for acos:
@@ -66,14 +66,16 @@ LIBC_INLINE bfloat16 acosbf16(bfloat16 x) {
     // 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);
+    // TODO: Use bfloat16 version for inv_trigf_utils_internals after they are available
+    // Tracking issue : https://github.com/llvm/llvm-project/issues/202079
     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));
 
     return fputil::cast<bfloat16>(
         (sign) ? fputil::multiply_add(asin_sqrt_t, -2.0f, PI)
-               : 2 * asin_sqrt_t);
+               : 2.0f * asin_sqrt_t);
   }
-  // case 3: NaN or Inf
+  // case 3: NaN or |x| > 1
   // NaN
   if (xbits.is_nan()) {
     if (xbits.is_signaling_nan()) {
@@ -82,7 +84,7 @@ LIBC_INLINE bfloat16 acosbf16(bfloat16 x) {
     }
     return x; // quiet NaN
   }
-  // inf
+  // |x| > 1
   fputil::raise_except_if_required(FE_INVALID);
   fputil::set_errno_if_required(EDOM); // Domain is bounded
   return FPBits::quiet_nan().get_val();

>From 5a94517550a0df38128969f8c4e88eef2e40245d Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sun, 7 Jun 2026 01:26:21 +0530
Subject: [PATCH 12/13] clang-format

---
 libc/src/__support/math/acosbf16.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/libc/src/__support/math/acosbf16.h b/libc/src/__support/math/acosbf16.h
index 2763998301731..93c5ebc1e1bf8 100644
--- a/libc/src/__support/math/acosbf16.h
+++ b/libc/src/__support/math/acosbf16.h
@@ -66,8 +66,9 @@ LIBC_INLINE bfloat16 acosbf16(bfloat16 x) {
     // 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);
-    // TODO: Use bfloat16 version for inv_trigf_utils_internals after they are available
-    // Tracking issue : https://github.com/llvm/llvm-project/issues/202079
+    // TODO: Use bfloat16 version for inv_trigf_utils_internals after they are
+    // available Tracking issue :
+    // https://github.com/llvm/llvm-project/issues/202079
     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));
 

>From 33f545574234c46c849c21c3540e08340500bf95 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sun, 7 Jun 2026 14:56:41 +0530
Subject: [PATCH 13/13] add freebsd entrypoint

---
 libc/config/freebsd/x86_64/entrypoints.txt | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/libc/config/freebsd/x86_64/entrypoints.txt b/libc/config/freebsd/x86_64/entrypoints.txt
index 31d421555624d..b4745d9400335 100644
--- a/libc/config/freebsd/x86_64/entrypoints.txt
+++ b/libc/config/freebsd/x86_64/entrypoints.txt
@@ -557,8 +557,10 @@ if(LIBC_TYPES_HAS_FLOAT128)
 endif()
 
 list(APPEND TARGET_LIBM_ENTRYPOINTS
-  libc.src.math.atanbf16
+  # bfloat16 entrypoints
+  libc.src.math.acosbf16
   libc.src.math.asinbf16
+  libc.src.math.atanbf16
   libc.src.math.bf16add
   libc.src.math.bf16addf
   libc.src.math.bf16addl



More information about the libc-commits mailing list