[libc-commits] [libc] [llvm] [libc][math][c23] Add cosbf16 math function (PR #185801)

via libc-commits libc-commits at lists.llvm.org
Thu Jun 25 16:48:53 PDT 2026


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

>From cb270eda358268092891c8da5b4533f36aff5224 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Tue, 10 Mar 2026 19:30:18 +0530
Subject: [PATCH 1/5] feat: added entrypoints

---
 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 73f4c1f937388..51ae6c3652471 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -818,6 +818,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.canonicalizebf16
   libc.src.math.ceilbf16
   libc.src.math.copysignbf16
+  libc.src.math.cosbf16
   libc.src.math.fabsbf16
   libc.src.math.fdimbf16
   libc.src.math.floorbf16
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index a5c1b20a7a434..7bb3f1e24690d 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -827,6 +827,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.bf16subf
   libc.src.math.bf16subl
   libc.src.math.canonicalizebf16
+  libc.src.math.cosbf16
   libc.src.math.ceilbf16
   libc.src.math.copysignbf16
   libc.src.math.fabsbf16
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index 60bc924993fc8..a186fa4ddb5b1 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -824,6 +824,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.bf16subf
   libc.src.math.bf16subl
   libc.src.math.canonicalizebf16
+  libc.src.math.cosbf16
   libc.src.math.ceilbf16
   libc.src.math.copysignbf16
   libc.src.math.fabsbf16
diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index 6f4916b203de5..3f596e88aabef 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -637,6 +637,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.bf16subf
   libc.src.math.bf16subl
   libc.src.math.canonicalizebf16
+  libc.src.math.cosbf16
   libc.src.math.ceilbf16
   libc.src.math.copysignbf16
   libc.src.math.fabsbf16
diff --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index c09ccadbb8c4c..d08f024467d5d 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -254,6 +254,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.bf16subf
   libc.src.math.bf16subl
   libc.src.math.canonicalizebf16
+  libc.src.math.cosbf16
   libc.src.math.ceilbf16
   libc.src.math.copysignbf16
   libc.src.math.fabsbf16
diff --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt
index 60a98cf5c773f..7a7b4a4ccbe62 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -662,6 +662,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.bf16subf
   libc.src.math.bf16subl
   libc.src.math.canonicalizebf16
+  libc.src.math.cosbf16
   libc.src.math.ceilbf16
   libc.src.math.copysignbf16
   libc.src.math.fabsbf16
diff --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index 70bdbde97e868..1c6a5bb19214b 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -664,6 +664,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.bf16subf
   libc.src.math.bf16subl
   libc.src.math.canonicalizebf16
+  libc.src.math.cosbf16
   libc.src.math.ceilbf16
   libc.src.math.copysignbf16
   libc.src.math.fabsbf16
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 570f7666ba67b..d10a95c223162 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -937,6 +937,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.bf16subf
   libc.src.math.bf16subl
   libc.src.math.canonicalizebf16
+  libc.src.math.cosbf16
   libc.src.math.ceilbf16
   libc.src.math.copysignbf16
   libc.src.math.fabsbf16
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index e6aa6091ed550..188845cb6c140 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -508,6 +508,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.bf16subf
   libc.src.math.bf16subl
   libc.src.math.canonicalizebf16
+  libc.src.math.cosbf16
   libc.src.math.ceilbf16
   libc.src.math.copysignbf16
   libc.src.math.fabsbf16
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 2508de5cfe5a4..7ad35415c9259 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -958,6 +958,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.bf16subf
   libc.src.math.bf16subl
   libc.src.math.canonicalizebf16
+  libc.src.math.cosbf16
   libc.src.math.ceilbf16
   libc.src.math.copysignbf16
   libc.src.math.fabsbf16
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 5ace968a739c8..48119762feb4c 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1029,6 +1029,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.bf16subf
   libc.src.math.bf16subl
   libc.src.math.canonicalizebf16
+  libc.src.math.cosbf16
   libc.src.math.ceilbf16
   libc.src.math.copysignbf16
   libc.src.math.fabsbf16
diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index 3c68290675b11..bb3182d40740f 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -344,6 +344,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.bf16subf
   libc.src.math.bf16subl
   libc.src.math.canonicalizebf16
+  libc.src.math.cosbf16
   libc.src.math.ceilbf16
   libc.src.math.copysignbf16
   libc.src.math.fabsbf16

>From c25cea4738beeadf468b707112b12d07080c92d5 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Tue, 10 Mar 2026 23:09:24 +0530
Subject: [PATCH 2/5] feat: final Implementation for cosbf16

temp

feat: Implementation for cosbf16

nit
---
 libc/shared/math.h                            |  1 +
 libc/shared/math/cosbf16.h                    | 23 ++++++
 libc/src/__support/math/CMakeLists.txt        | 16 ++++
 libc/src/__support/math/cosbf16.h             | 76 +++++++++++++++++++
 libc/src/math/CMakeLists.txt                  |  1 +
 libc/src/math/cosbf16.h                       | 21 +++++
 libc/src/math/generic/CMakeLists.txt          | 10 +++
 libc/src/math/generic/cosbf16.cpp             | 17 +++++
 libc/test/shared/CMakeLists.txt               |  1 +
 libc/test/shared/shared_math_test.cpp         |  5 ++
 libc/test/src/math/CMakeLists.txt             | 13 ++++
 libc/test/src/math/cosbf16_test.cpp           | 45 +++++++++++
 libc/test/src/math/smoke/CMakeLists.txt       | 13 ++++
 libc/test/src/math/smoke/cosbf16_test.cpp     | 40 ++++++++++
 .../llvm-project-overlay/libc/BUILD.bazel     | 23 ++++++
 15 files changed, 305 insertions(+)
 create mode 100644 libc/shared/math/cosbf16.h
 create mode 100644 libc/src/__support/math/cosbf16.h
 create mode 100644 libc/src/math/cosbf16.h
 create mode 100644 libc/src/math/generic/cosbf16.cpp
 create mode 100644 libc/test/src/math/cosbf16_test.cpp
 create mode 100644 libc/test/src/math/smoke/cosbf16_test.cpp

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 2e8a574e4d042..a17a84ed2c341 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -82,6 +82,7 @@
 #include "math/copysignf16.h"
 #include "math/copysignl.h"
 #include "math/cos.h"
+#include "math/cosbf16.h"
 #include "math/cosf.h"
 #include "math/cosf16.h"
 #include "math/coshf.h"
diff --git a/libc/shared/math/cosbf16.h b/libc/shared/math/cosbf16.h
new file mode 100644
index 0000000000000..5ff8ce8dea8dd
--- /dev/null
+++ b/libc/shared/math/cosbf16.h
@@ -0,0 +1,23 @@
+//===-- Shared cosbf16 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_COSBF16_H
+#define LLVM_LIBC_SHARED_MATH_COSBF16_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/cosbf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::cosbf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_COSBF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index a57086a31dc03..01c5b91cf2e54 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -2829,6 +2829,22 @@ add_header_library(
     libc.src.__support.macros.optimization
 )
 
+add_header_library(
+  cosbf16
+  HDRS
+    cosbf16.h
+  DEPENDS
+    .sincosf_utils
+    libc.hdr.errno_macros
+    libc.hdr.fenv_macros
+    libc.src.__support.FPUtil.cast
+    libc.src.__support.FPUtil.fenv_impl
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.multiply_add
+    libc.src.__support.macros.optimization
+    libc.src.__support.FPUtil.bfloat16
+)
+
 add_header_library(
   cosf16
   HDRS
diff --git a/libc/src/__support/math/cosbf16.h b/libc/src/__support/math/cosbf16.h
new file mode 100644
index 0000000000000..7f7eead2d760b
--- /dev/null
+++ b/libc/src/__support/math/cosbf16.h
@@ -0,0 +1,76 @@
+//===-- Implementation header for cosbf16 -----------------------*- 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_COSBF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_COSBF16_H
+
+#include "hdr/errno_macros.h"
+#include "hdr/fenv_macros.h"
+#include "sincosf_utils.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/macros/optimization.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE bfloat16 cosbf16(bfloat16 x) {
+  using namespace sincosf_utils_internal;
+
+  using FPBits = fputil::FPBits<bfloat16>;
+  FPBits xbits(x);
+
+  uint16_t x_u = xbits.uintval();
+  uint16_t x_abs = x_u & 0x7fff;
+  float xf = x;
+
+  if (x_abs < 0x7f80) {
+
+    // cos(+/-0) = 1
+    if (LIBC_UNLIKELY(x_abs == 0U))
+      return fputil::cast<bfloat16>(1.0f);
+
+    double xd = static_cast<double>(xf);
+    uint32_t x_abs_d = fputil::FPBits<float>(xf).uintval() & 0x7fffffff;
+    double sin_k, cos_k, sin_y, cosm1_y;
+
+    sincosf_eval(xd, x_abs_d, sin_k, cos_k, sin_y, cosm1_y);
+    // using cos(a + b) = cos(a)*cos(b) - sin(a)*sin(b)
+    //  cos(x) = cos_k*cos_y - sin_k*sin_y
+    //  but cosm1_y = cos_y - 1 --> cos_y = cosm1_y + 1
+    //  cos(x) = cos_k*cosm1_y + cos_k - sin_k * sin_y
+
+    return fputil::cast<bfloat16>(fputil::multiply_add(
+        cosm1_y, cos_k, fputil::multiply_add(-sin_y, sin_k, cos_k)));
+  }
+
+  // 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;
+  }
+
+  // +/- inf
+  fputil::set_errno_if_required(EDOM);
+  fputil::raise_except_if_required(FE_INVALID);
+
+  return x + FPBits::quiet_nan().get_val();
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_COSBF16_H
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 4823ceefd7475..0e5a4cb50f8ad 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -116,6 +116,7 @@ add_math_entrypoint_object(copysignf128)
 add_math_entrypoint_object(copysignbf16)
 
 add_math_entrypoint_object(cos)
+add_math_entrypoint_object(cosbf16)
 add_math_entrypoint_object(cosf)
 add_math_entrypoint_object(cosf16)
 
diff --git a/libc/src/math/cosbf16.h b/libc/src/math/cosbf16.h
new file mode 100644
index 0000000000000..75306265cb35d
--- /dev/null
+++ b/libc/src/math/cosbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for cosbf16 -----------------------*- 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_COSBF16_H
+#define LLVM_LIBC_SRC_MATH_COSBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 cosbf16(bfloat16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_COSBF16_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 5a5bd84213a55..0a9166aa8e7be 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -297,6 +297,16 @@ add_entrypoint_object(
     libc.src.__support.math.cosf
 )
 
+add_entrypoint_object(
+  cosbf16
+  SRCS
+    cosbf16.cpp
+  HDRS
+    ../cosbf16.h
+  DEPENDS
+    libc.src.__support.math.cosbf16
+)
+
 add_entrypoint_object(
   cosf16
   SRCS
diff --git a/libc/src/math/generic/cosbf16.cpp b/libc/src/math/generic/cosbf16.cpp
new file mode 100644
index 0000000000000..95cb90f37c3f9
--- /dev/null
+++ b/libc/src/math/generic/cosbf16.cpp
@@ -0,0 +1,17 @@
+//===-- Implementation for cosbf16 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/cosbf16.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/math/cosbf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, cosbf16, (bfloat16 x)) { return math::cosbf16(x); }
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 9b990dd5a5c84..f950e7666213e 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -89,6 +89,7 @@ add_fp_unittest(
     libc.src.__support.math.copysignf16
     libc.src.__support.math.copysignl
     libc.src.__support.math.cos
+    libc.src.__support.math.cosbf16
     libc.src.__support.math.cosf
     libc.src.__support.math.cosf16
     libc.src.__support.math.coshf
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 5877729ae7931..2260c75f142c3 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -768,6 +768,11 @@ TEST(LlvmLibcSharedMathTest, AllBFloat16) {
                                                         &canonicalizebf16_x));
   EXPECT_FP_EQ(bfloat16(0.0), canonicalizebf16_cx);
   EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::cbrtbf16(bfloat16(0.0)));
+  EXPECT_FP_EQ(bfloat16(1.0), LIBC_NAMESPACE::shared::cosbf16(bfloat16(0.0)));
+  EXPECT_FP_EQ(bfloat16(5.0), LIBC_NAMESPACE::shared::bf16addf(2.0f, 3.0f));
+  EXPECT_FP_EQ(bfloat16(5.0), LIBC_NAMESPACE::shared::bf16addl(2L, 3L));
+  EXPECT_FP_EQ(bfloat16(10.0),
+               LIBC_NAMESPACE::shared::bf16fmaf(2.0f, 3.0f, 4.0f));
   EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::ceilbf16(bfloat16(0.0)));
   EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::copysignbf16(
                                   bfloat16(0.0), bfloat16(0.0)));
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 31f6468e01110..9b9105438fbd1 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -42,6 +42,19 @@ add_fp_unittest(
     libc.src.__support.FPUtil.fp_bits
 )
 
+add_fp_unittest(
+  cosbf16_test
+  NEED_MPFR
+  SUITE
+    libc-math-unittests
+  SRCS
+    cosbf16_test.cpp
+  DEPENDS
+    libc.src.math.cosbf16
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.bfloat16
+)
+
 add_fp_unittest(
   cosf16_test
   NEED_MPFR
diff --git a/libc/test/src/math/cosbf16_test.cpp b/libc/test/src/math/cosbf16_test.cpp
new file mode 100644
index 0000000000000..dadae218f38c6
--- /dev/null
+++ b/libc/test/src/math/cosbf16_test.cpp
@@ -0,0 +1,45 @@
+//===-- Exhaustive test for cosbf16 ---------------------------------------===//
+//
+// 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/cosbf16.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+using LlvmLibcCosBf16Test = 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: [-Inf, 0]
+static constexpr uint16_t NEG_START = 0x8000U;
+static constexpr uint16_t NEG_STOP = 0xff80U;
+
+TEST_F(LlvmLibcCosBf16Test, PositiveRange) {
+
+  for (uint16_t v = POS_START; v <= POS_STOP; ++v) {
+    bfloat16 x = FPBits(v).get_val();
+
+    EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Cos, x,
+                                   LIBC_NAMESPACE::cosbf16(x), 0.5);
+  }
+}
+
+TEST_F(LlvmLibcCosBf16Test, NegativeRange) {
+
+  for (uint16_t v = NEG_START; v <= NEG_STOP; ++v) {
+    bfloat16 x = FPBits(v).get_val();
+
+    EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Cos, x,
+                                   LIBC_NAMESPACE::cosbf16(x), 0.5);
+  }
+}
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 73d2265322c4d..744541a8c7a0a 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -12,6 +12,19 @@ add_fp_unittest(
     libc.src.math.cosf
 )
 
+add_fp_unittest(
+  cosbf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    cosbf16_test.cpp
+  DEPENDS
+    libc.hdr.errno_macros
+    libc.src.math.cosbf16
+    libc.hdr.fenv_macros
+    libc.src.__support.FPUtil.bfloat16
+)
+
 add_fp_unittest(
   cosf16_test
   SUITE
diff --git a/libc/test/src/math/smoke/cosbf16_test.cpp b/libc/test/src/math/smoke/cosbf16_test.cpp
new file mode 100644
index 0000000000000..ae69fddd569a7
--- /dev/null
+++ b/libc/test/src/math/smoke/cosbf16_test.cpp
@@ -0,0 +1,40 @@
+//===-- Unittests for cosbf16 ---------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/cosbf16.h"
+#include "test/UnitTest/FEnvSafeTest.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+class LlvmLibcCosBf16Test : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+  DECLARE_SPECIAL_CONSTANTS(bfloat16)
+public:
+  void test_special_numbers() {
+    EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::cosbf16(aNaN));
+    EXPECT_MATH_ERRNO(0);
+
+    EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(
+        aNaN, LIBC_NAMESPACE::cosbf16(sNaN), FE_INVALID);
+    EXPECT_MATH_ERRNO(0);
+
+    EXPECT_FP_EQ_ALL_ROUNDING(bfloat16(1.0), LIBC_NAMESPACE::cosbf16(zero));
+    EXPECT_MATH_ERRNO(0);
+
+    EXPECT_FP_EQ_ALL_ROUNDING(bfloat16(1.0), LIBC_NAMESPACE::cosbf16(neg_zero));
+    EXPECT_MATH_ERRNO(0);
+
+    EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::cosbf16(inf));
+    EXPECT_MATH_ERRNO(EDOM);
+
+    EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::cosbf16(neg_inf));
+    EXPECT_MATH_ERRNO(EDOM);
+  }
+};
+TEST_F(LlvmLibcCosBf16Test, 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 68bb1f1d60bf2..857e00922e60e 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -6650,6 +6650,22 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_cosbf16",
+    hdrs = ["src/__support/math/cosbf16.h"],
+    deps = [
+        ":__support_fputil_bfloat16",
+        ":__support_fputil_cast",
+        ":__support_fputil_fenv_impl",
+        ":__support_fputil_fp_bits",
+        ":__support_fputil_multiply_add",
+        ":__support_macros_optimization",
+        ":__support_sincosf_utils",
+        ":hdr_errno_macros",
+        ":hdr_fenv_macros",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_cosf16",
     hdrs = ["src/__support/math/cosf16.h"],
@@ -10464,6 +10480,13 @@ libc_math_function(
     additional_deps = [":__support_math_cosf"],
 )
 
+libc_math_function(
+    name = "cosbf16",
+    additional_deps = [
+        ":__support_math_cosbf16",
+    ],
+)
+
 libc_math_function(
     name = "cosf16",
     additional_deps = [

>From 0d5d5c5f092c091bee4bd989789b7a14b3226157 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Wed, 11 Mar 2026 10:19:56 +0530
Subject: [PATCH 3/5] checked index.rst

---
 libc/docs/headers/math/index.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst
index 8f8f381054002..e1c1a1a5f8f09 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -283,7 +283,7 @@ Higher Math Functions
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | compoundn |                  |                 |                        |                      |                        |                        | 7.12.7.2               | F.10.4.2                   |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| cos       | |check|          | |check|         |                        | |check|              |                        |                        | 7.12.4.5               | F.10.1.5                   |
+| cos       | |check|          | |check|         |                        | |check|              |                        | |check|                | 7.12.4.5               | F.10.1.5                   |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | cosh      | |check|          |                 |                        | |check|              |                        |                        | 7.12.5.4               | F.10.2.4                   |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+

>From 4e40bd40388756c4377ebd8d1771ad183f0468aa Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Fri, 26 Jun 2026 04:35:39 +0530
Subject: [PATCH 4/5] nits

---
 libc/src/__support/math/cosbf16.h | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/libc/src/__support/math/cosbf16.h b/libc/src/__support/math/cosbf16.h
index 7f7eead2d760b..ce161f0e5e071 100644
--- a/libc/src/__support/math/cosbf16.h
+++ b/libc/src/__support/math/cosbf16.h
@@ -34,16 +34,15 @@ LIBC_INLINE bfloat16 cosbf16(bfloat16 x) {
   float xf = x;
 
   if (x_abs < 0x7f80) {
-
     // cos(+/-0) = 1
-    if (LIBC_UNLIKELY(x_abs == 0U))
+    if (LIBC_UNLIKELY(x_abs == 0))
       return fputil::cast<bfloat16>(1.0f);
 
     double xd = static_cast<double>(xf);
-    uint32_t x_abs_d = fputil::FPBits<float>(xf).uintval() & 0x7fffffff;
+    uint32_t x_abs_f = fputil::FPBits<float>(xf).uintval() & 0x7fffffff;
     double sin_k, cos_k, sin_y, cosm1_y;
 
-    sincosf_eval(xd, x_abs_d, sin_k, cos_k, sin_y, cosm1_y);
+    sincosf_eval(xd, x_abs_f, sin_k, cos_k, sin_y, cosm1_y);
     // using cos(a + b) = cos(a)*cos(b) - sin(a)*sin(b)
     //  cos(x) = cos_k*cos_y - sin_k*sin_y
     //  but cosm1_y = cos_y - 1 --> cos_y = cosm1_y + 1
@@ -53,7 +52,7 @@ LIBC_INLINE bfloat16 cosbf16(bfloat16 x) {
         cosm1_y, cos_k, fputil::multiply_add(-sin_y, sin_k, cos_k)));
   }
 
-  // nan
+  // NaN
   if (xbits.is_nan()) {
     if (xbits.is_signaling_nan()) {
       fputil::raise_except_if_required(FE_INVALID);

>From 4fad66a3df006f731e5603a773ab1fb3ad0a3c17 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Fri, 26 Jun 2026 05:17:19 +0530
Subject: [PATCH 5/5] add freebsd entrypoint

---
 libc/config/freebsd/x86_64/entrypoints.txt | 1 +
 1 file changed, 1 insertion(+)

diff --git a/libc/config/freebsd/x86_64/entrypoints.txt b/libc/config/freebsd/x86_64/entrypoints.txt
index 7b8b8384e6dd8..7165b034a2dc6 100644
--- a/libc/config/freebsd/x86_64/entrypoints.txt
+++ b/libc/config/freebsd/x86_64/entrypoints.txt
@@ -580,6 +580,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.canonicalizebf16
   libc.src.math.ceilbf16
   libc.src.math.copysignbf16
+  libc.src.math.cosbf16
   libc.src.math.fabsbf16
   libc.src.math.fdimbf16
   libc.src.math.floorbf16



More information about the libc-commits mailing list