[libc-commits] [libc] [llvm] [libc][math][c23] Add acospibf16 math function (PR #190477)

via libc-commits libc-commits at lists.llvm.org
Sat Apr 4 11:22:20 PDT 2026


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

>From f9594e9fde44faf16cdb8a4a47e9aa246798ac18 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Mon, 30 Mar 2026 15:19:00 +0530
Subject: [PATCH 1/3] entrypoints added

---
 libc/config/baremetal/aarch64/entrypoints.txt | 1 +
 libc/config/baremetal/arm/entrypoints.txt     | 1 +
 libc/config/baremetal/riscv/entrypoints.txt   | 1 +
 libc/config/darwin/aarch64/entrypoints.txt    | 1 +
 libc/config/darwin/x86_64/entrypoints.txt     | 1 +
 libc/config/gpu/amdgpu/entrypoints.txt        | 1 +
 libc/config/gpu/nvptx/entrypoints.txt         | 1 +
 libc/config/linux/aarch64/entrypoints.txt     | 1 +
 libc/config/linux/arm/entrypoints.txt         | 1 +
 libc/config/linux/riscv/entrypoints.txt       | 1 +
 libc/config/linux/x86_64/entrypoints.txt      | 1 +
 libc/config/windows/entrypoints.txt           | 1 +
 12 files changed, 12 insertions(+)

diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index ed84bdff8ce1e..42bf3de5ee643 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -791,6 +791,7 @@ endif()
 
 list(APPEND TARGET_LIBM_ENTRYPOINTS
   # bfloat16 entrypoints
+  libc.src.math.acospibf16
   libc.src.math.atanbf16
   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 43ba426b8ca09..1b08d5fdbb221 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -800,6 +800,7 @@ endif()
 
 list(APPEND TARGET_LIBM_ENTRYPOINTS
   # bfloat16 entrypoints
+  libc.src.math.acospibf16
   libc.src.math.atanbf16
   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 79fbd1e4b0de2..1d183eaa2f351 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -797,6 +797,7 @@ endif()
 
 list(APPEND TARGET_LIBM_ENTRYPOINTS
   # bfloat16 entrypoints
+  libc.src.math.acospibf16
   libc.src.math.atanbf16
   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 71881d23c7a06..64fe76614b1bc 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -610,6 +610,7 @@ endif()
 
 list(APPEND TARGET_LIBM_ENTRYPOINTS
   # bfloat16 entrypoints
+  libc.src.math.acospibf16
   libc.src.math.atanbf16
   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 cad973f1ac069..73ea35fe8a874 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -234,6 +234,7 @@ set(TARGET_LIBM_ENTRYPOINTS
 
 list(APPEND TARGET_LIBM_ENTRYPOINTS
   # bfloat16 entrypoints
+  libc.src.math.acospibf16
   libc.src.math.atanbf16
   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 e327d9ac6f3d3..f2f8b4c318e17 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -626,6 +626,7 @@ endif()
 
 list(APPEND TARGET_LIBM_ENTRYPOINTS
   # bfloat16 entrypoints
+  libc.src.math.acospibf16
   libc.src.math.atanbf16
   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 efc784d23e301..d5b45752e123f 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -628,6 +628,7 @@ endif()
 
 list(APPEND TARGET_LIBM_ENTRYPOINTS
   # bfloat16 entrypoints
+  libc.src.math.acospibf16
   libc.src.math.atanbf16
   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 4311ba624e6d8..4fbabb17ae532 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -873,6 +873,7 @@ endif()
 
 list(APPEND TARGET_LIBM_ENTRYPOINTS
   # bfloat16 entrypoints
+  libc.src.math.acospibf16
   libc.src.math.atanbf16
   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 a10dc0c82e36f..087e0d8ab12c6 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -476,6 +476,7 @@ set(TARGET_LIBM_ENTRYPOINTS
 
 list(APPEND TARGET_LIBM_ENTRYPOINTS
   # bfloat16 entrypoints
+  libc.src.math.acospibf16
   libc.src.math.atanbf16
   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 17efa2e368ede..7061881a7b4e0 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -894,6 +894,7 @@ endif()
 
 list(APPEND TARGET_LIBM_ENTRYPOINTS
   # bfloat16 entrypoints
+  libc.src.math.acospibf16
   libc.src.math.atanbf16
   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 eeaccc863dbc3..0135445585584 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -949,6 +949,7 @@ endif()
 
 list(APPEND TARGET_LIBM_ENTRYPOINTS
   # bfloat16 entrypoints
+  libc.src.math.acospibf16
   libc.src.math.atanbf16
   libc.src.math.bf16add
   libc.src.math.bf16addf
diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index e594f6bbb1fd5..590d5105624d1 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -322,6 +322,7 @@ set(TARGET_LIBM_ENTRYPOINTS
 
 list(APPEND TARGET_LIBM_ENTRYPOINTS
   # bfloat16 entrypoints
+  libc.src.math.acospibf16
   libc.src.math.atanbf16
   libc.src.math.bf16add
   libc.src.math.bf16addf

>From 65806deac927816f09ece764a19ab15350661887 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Mon, 30 Mar 2026 16:09:54 +0530
Subject: [PATCH 2/3] Implementation for acospibf16

pre-exec

final

tested

verified
---
 libc/docs/headers/math/index.rst              |   2 +-
 libc/shared/math.h                            |   1 +
 libc/shared/math/acospibf16.h                 |  23 ++++
 libc/src/__support/math/CMakeLists.txt        |  15 +++
 libc/src/__support/math/acospibf16.h          | 106 ++++++++++++++++++
 libc/src/math/CMakeLists.txt                  |   1 +
 libc/src/math/acospibf16.h                    |  21 ++++
 libc/src/math/generic/CMakeLists.txt          |  10 ++
 libc/src/math/generic/acospibf16.cpp          |  18 +++
 libc/test/shared/CMakeLists.txt               |   1 +
 libc/test/shared/shared_math_test.cpp         |   1 +
 libc/test/src/math/CMakeLists.txt             |  13 +++
 libc/test/src/math/acospibf16_test.cpp        |  45 ++++++++
 libc/test/src/math/smoke/CMakeLists.txt       |  14 +++
 libc/test/src/math/smoke/acospibf16_test.cpp  |  46 ++++++++
 .../llvm-project-overlay/libc/BUILD.bazel     |  20 ++++
 16 files changed, 336 insertions(+), 1 deletion(-)
 create mode 100644 libc/shared/math/acospibf16.h
 create mode 100644 libc/src/__support/math/acospibf16.h
 create mode 100644 libc/src/math/acospibf16.h
 create mode 100644 libc/src/math/generic/acospibf16.cpp
 create mode 100644 libc/test/src/math/acospibf16_test.cpp
 create mode 100644 libc/test/src/math/smoke/acospibf16_test.cpp

diff --git a/libc/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst
index f9614c6d19d22..10e9f45004cda 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -263,7 +263,7 @@ Higher Math Functions
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | acosh     | |check|          |                 |                        | |check|              |                        |                        | 7.12.5.1               | F.10.2.1                   |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| acospi    |                  |                 |                        | |check|              |                        |                        | 7.12.4.8               | F.10.1.8                   |
+| acospi    |                  |                 |                        | |check|              |                        | |check|                | 7.12.4.8               | F.10.1.8                   |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | asin      | |check|          | |check|         |                        | |check|              |                        |                        | 7.12.4.2               | F.10.1.2                   |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 08a4c994283c5..90b7131be6e98 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -16,6 +16,7 @@
 #include "math/acosf16.h"
 #include "math/acoshf.h"
 #include "math/acoshf16.h"
+#include "math/acospibf16.h"
 #include "math/acospif.h"
 #include "math/acospif16.h"
 #include "math/asin.h"
diff --git a/libc/shared/math/acospibf16.h b/libc/shared/math/acospibf16.h
new file mode 100644
index 0000000000000..55746577673f4
--- /dev/null
+++ b/libc/shared/math/acospibf16.h
@@ -0,0 +1,23 @@
+//===-- Shared acospibf16 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_ACOSPIBF16_H
+#define LLVM_LIBC_SHARED_MATH_ACOSPIBF16_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/acospibf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::acospibf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ACOSPIBF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 10329830a004b..2c49e930db5ac 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -96,6 +96,21 @@ add_header_library(
     libc.src.__support.macros.optimization
 )
 
+add_header_library(
+  acospibf16
+  HDRS
+    acospibf16.h
+  DEPENDS
+    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
+    libc.src.__support.FPUtil.sqrt
+    libc.src.__support.macros.optimization
+    libc.src.__support.math.inv_trigf_utils
+)
+
 add_header_library(
   acospif
   HDRS
diff --git a/libc/src/__support/math/acospibf16.h b/libc/src/__support/math/acospibf16.h
new file mode 100644
index 0000000000000..9d480897f2765
--- /dev/null
+++ b/libc/src/__support/math/acospibf16.h
@@ -0,0 +1,106 @@
+//===-- Implementation header for acospibf16 --------------------*- 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_ACOSPIBF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ACOSPIBF16_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 bfloat16 acospibf16(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 xf = x;
+
+  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>(0.5f);
+
+    float xp = fputil::cast<float>(inv_trigf_utils_internal::asinpi_eval(x_sq));
+    float result =
+        xf *
+        fputil::multiply_add(
+            x_sq, xp,
+            fputil::cast<float>(inv_trigf_utils_internal::ASINPI_COEFFS[0]));
+    return fputil::cast<bfloat16>(0.5 - result);
+  }
+
+  // case 2: 0.5< |x|<= 1.0
+  if (x_abs <= 0x3F80) {
+    // |x| = {1}
+    if (x_abs == 0x3F80) {
+      if (sign)
+        return fputil::cast<bfloat16>(1.0f);
+      else
+        return fputil::cast<bfloat16>(0.0f);
+    }
+
+    // using reduction for acos:
+    // f(x) = acos(|x|) = 2*asin(sqrt((1 - |x|)/2)),
+    // then for
+    // using same for acospi
+    // x>=0: f(x) = acos(x)/pi  = acospi(x)
+    // x<0 : f(x) = (pi - acos(x))/pi = 1 - acospi(x)
+
+    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::asinpi_eval(t));
+    float asin_sqrt_t =
+        t_sqrt *
+        (fputil::multiply_add(
+            t, tp,
+            fputil::cast<float>(inv_trigf_utils_internal::ASINPI_COEFFS[0])));
+
+    return fputil::cast<bfloat16>(
+        (sign) ? fputil::multiply_add(asin_sqrt_t, -2.0f, 1.0f)
+               : 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
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ACOSPIBF16_H
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index ebba3f04b8d56..c69425921fb0a 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -48,6 +48,7 @@ add_math_entrypoint_object(acosh)
 add_math_entrypoint_object(acoshf)
 add_math_entrypoint_object(acoshf16)
 
+add_math_entrypoint_object(acospibf16)
 add_math_entrypoint_object(acospif)
 add_math_entrypoint_object(acospif16)
  
diff --git a/libc/src/math/acospibf16.h b/libc/src/math/acospibf16.h
new file mode 100644
index 0000000000000..224e2cb0cae31
--- /dev/null
+++ b/libc/src/math/acospibf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for acospibf16 --------------------*- 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_ACOSPIBF16_H
+#define LLVM_LIBC_SRC_MATH_ACOSPIBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 acospibf16(bfloat16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_ACOSPIBF16_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 30f33749d165e..f1041f8256ca3 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -4159,6 +4159,16 @@ add_entrypoint_object(
     libc.src.__support.math.acos
 )
 
+add_entrypoint_object(
+  acospibf16
+  SRCS
+    acospibf16.cpp
+  HDRS
+    ../acospibf16.h
+  DEPENDS
+    libc.src.__support.math.acospibf16
+)
+
 add_entrypoint_object(
   acospif16
   SRCS
diff --git a/libc/src/math/generic/acospibf16.cpp b/libc/src/math/generic/acospibf16.cpp
new file mode 100644
index 0000000000000..cdfa599d544e0
--- /dev/null
+++ b/libc/src/math/generic/acospibf16.cpp
@@ -0,0 +1,18 @@
+//===-- Bfloat16 acospi 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/acospibf16.h"
+#include "src/__support/math/acospibf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, acospibf16, (bfloat16 x)) {
+  return math::acospibf16(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index c3795d366a588..37ff31fd82c88 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -13,6 +13,7 @@ add_fp_unittest(
     libc.src.__support.math.acosf16
     libc.src.__support.math.acoshf
     libc.src.__support.math.acoshf16
+    libc.src.__support.math.acospibf16
     libc.src.__support.math.acospif
     libc.src.__support.math.acospif16
     libc.src.__support.math.asin
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index b52abc6d8e990..022c600fb8c31 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -424,6 +424,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) {
 
 TEST(LlvmLibcSharedMathTest, AllBFloat16) {
   using FPBits = LIBC_NAMESPACE::fputil::FPBits<bfloat16>;
+  EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::acospibf16(bfloat16(1.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));
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 2fd77f964e4d6..b1ff0feb3192c 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -2591,6 +2591,19 @@ add_fp_unittest(
     libc.src.math.acosf16  
 )
 
+add_fp_unittest(
+  acospibf16_test
+  NEED_MPFR
+  SUITE
+    libc-math-unittests
+  SRCS
+    acospibf16_test.cpp
+  DEPENDS
+    libc.src.math.acospibf16
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.bfloat16
+)
+
 add_fp_unittest(
   acospif_test
   NEED_MPFR
diff --git a/libc/test/src/math/acospibf16_test.cpp b/libc/test/src/math/acospibf16_test.cpp
new file mode 100644
index 0000000000000..38a9e4387a198
--- /dev/null
+++ b/libc/test/src/math/acospibf16_test.cpp
@@ -0,0 +1,45 @@
+//===-- Exhaustive test for acospibf16 ------------------------------------===//
+//
+// 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/acospibf16.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+using LlvmLibcAcospiBf16Test = LIBC_NAMESPACE::testing::FPTest<bfloat16>;
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+// range: [+0, +inf]
+static constexpr uint16_t POS_START = 0x0000U;
+static constexpr uint16_t POS_STOP = 0x7f80U;
+
+// range: [-0, -inf]
+static constexpr uint16_t NEG_START = 0x8000U;
+static constexpr uint16_t NEG_STOP = 0xff80U;
+
+TEST_F(LlvmLibcAcospiBf16Test, PositiveRange) {
+  for (uint16_t v1 = POS_START; v1 <= POS_STOP; v1++) {
+
+    bfloat16 x = FPBits(v1).get_val();
+
+    EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Acospi, x,
+                                   LIBC_NAMESPACE::acospibf16(x), 0.5);
+  }
+}
+
+TEST_F(LlvmLibcAcospiBf16Test, NegativeRange) {
+  for (uint16_t v1 = NEG_START; v1 <= NEG_STOP; v1++) {
+
+    bfloat16 x = FPBits(v1).get_val();
+
+    EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Acospi, x,
+                                   LIBC_NAMESPACE::acospibf16(x), 0.5);
+  }
+}
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 79f2ea4806377..8e1ac0c7fd934 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -4885,6 +4885,20 @@ add_fp_unittest(
     libc.src.math.acosf16
 )
 
+add_fp_unittest(
+  acospibf16_test
+  NEED_MPFR
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    acospibf16_test.cpp
+  DEPENDS
+    libc.src.math.acospibf16
+    libc.hdr.errno_macros
+    libc.hdr.fenv_macros
+    libc.src.__support.FPUtil.bfloat16
+)
+
 add_fp_unittest(
   acospif_test
   SUITE
diff --git a/libc/test/src/math/smoke/acospibf16_test.cpp b/libc/test/src/math/smoke/acospibf16_test.cpp
new file mode 100644
index 0000000000000..1aaa653e4f464
--- /dev/null
+++ b/libc/test/src/math/smoke/acospibf16_test.cpp
@@ -0,0 +1,46 @@
+//===-- Unittests for acospibf16 ------------------------------------------===//
+//
+// 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/acospibf16.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 LlvmLibcAcospiBf16Test : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+  DECLARE_SPECIAL_CONSTANTS(bfloat16)
+public:
+  void test_special_numbers() {
+    EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::acospibf16(aNaN));
+    EXPECT_MATH_ERRNO(0);
+
+    EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(
+        aNaN, LIBC_NAMESPACE::acospibf16(sNaN), FE_INVALID);
+    EXPECT_MATH_ERRNO(0);
+
+    EXPECT_FP_EQ_ALL_ROUNDING(bfloat16(0.5),
+                              LIBC_NAMESPACE::acospibf16(zero));
+    EXPECT_MATH_ERRNO(0);
+
+    EXPECT_FP_EQ_ALL_ROUNDING(bfloat16(0.5),
+                              LIBC_NAMESPACE::acospibf16(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::Acospi, x,
+                                     LIBC_NAMESPACE::acospibf16(x), 0.5);
+    }
+  }
+};
+TEST_F(LlvmLibcAcospiBf16Test, 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 a69ebcdb63fa7..f8cc17e016c8c 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2936,6 +2936,21 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_acospibf16",
+    hdrs = ["src/__support/math/acospibf16.h"],
+    deps = [
+        ":__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",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_acospif",
     hdrs = ["src/__support/math/acospif.h"],
@@ -6541,6 +6556,11 @@ libc_math_function(
     ],
 )
 
+libc_math_function(
+    name = "acospibf16",
+    additional_deps = [":__support_math_acosbf16"],
+)
+
 libc_math_function(
     name = "acospif",
     additional_deps = [

>From 7fa6b39417bd129e70b69baa67c718995ea90634 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sat, 4 Apr 2026 23:51:59 +0530
Subject: [PATCH 3/3] nits

---
 libc/src/__support/math/acospibf16.h              | 8 +-------
 utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 2 +-
 2 files changed, 2 insertions(+), 8 deletions(-)

diff --git a/libc/src/__support/math/acospibf16.h b/libc/src/__support/math/acospibf16.h
index 9d480897f2765..eca1d58bb93a1 100644
--- a/libc/src/__support/math/acospibf16.h
+++ b/libc/src/__support/math/acospibf16.h
@@ -22,12 +22,6 @@ namespace LIBC_NAMESPACE_DECL {
 namespace math {
 
 LIBC_INLINE bfloat16 acospibf16(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);
@@ -40,7 +34,7 @@ LIBC_INLINE bfloat16 acospibf16(bfloat16 x) {
   float xf_abs = (xf < 0 ? -xf : xf);
   float x_sq = xf_abs * xf_abs;
 
-  // case 1: x <= 0.5
+  // case 1: |x| <= 0.5
   if (x_abs <= 0x3F00) {
     // |x| = {0}
     if (LIBC_UNLIKELY(x_abs == 0))
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index f8cc17e016c8c..0f098dd729ea3 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -6558,7 +6558,7 @@ libc_math_function(
 
 libc_math_function(
     name = "acospibf16",
-    additional_deps = [":__support_math_acosbf16"],
+    additional_deps = [":__support_math_acospibf16"],
 )
 
 libc_math_function(



More information about the libc-commits mailing list