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

via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 10 21:52:56 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-amdgpu

Author: Zorojuro (Sukumarsawant)

<details>
<summary>Changes</summary>

This PR intends to add `cosbf16` higher math function for bfloat16 type .

---

Patch is 21.29 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/185801.diff


28 Files Affected:

- (modified) libc/config/baremetal/aarch64/entrypoints.txt (+1) 
- (modified) libc/config/baremetal/arm/entrypoints.txt (+1) 
- (modified) libc/config/baremetal/riscv/entrypoints.txt (+1) 
- (modified) libc/config/darwin/aarch64/entrypoints.txt (+1) 
- (modified) libc/config/darwin/x86_64/entrypoints.txt (+1) 
- (modified) libc/config/gpu/amdgpu/entrypoints.txt (+1) 
- (modified) libc/config/gpu/nvptx/entrypoints.txt (+1) 
- (modified) libc/config/linux/aarch64/entrypoints.txt (+1) 
- (modified) libc/config/linux/arm/entrypoints.txt (+1) 
- (modified) libc/config/linux/riscv/entrypoints.txt (+1) 
- (modified) libc/config/linux/x86_64/entrypoints.txt (+1) 
- (modified) libc/config/windows/entrypoints.txt (+1) 
- (modified) libc/docs/headers/math/index.rst (+1-1) 
- (modified) libc/shared/math.h (+1) 
- (added) libc/shared/math/cosbf16.h (+23) 
- (modified) libc/src/__support/math/CMakeLists.txt (+16) 
- (added) libc/src/__support/math/cosbf16.h (+76) 
- (modified) libc/src/math/CMakeLists.txt (+1) 
- (added) libc/src/math/cosbf16.h (+21) 
- (modified) libc/src/math/generic/CMakeLists.txt (+10) 
- (added) libc/src/math/generic/cosbf16.cpp (+17) 
- (modified) libc/test/shared/CMakeLists.txt (+1) 
- (modified) libc/test/shared/shared_math_test.cpp (+1) 
- (modified) libc/test/src/math/CMakeLists.txt (+13) 
- (added) libc/test/src/math/cosbf16_test.cpp (+45) 
- (modified) libc/test/src/math/smoke/CMakeLists.txt (+13) 
- (added) libc/test/src/math/smoke/cosbf16_test.cpp (+40) 
- (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+23) 


``````````diff
diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index 278e1f5afde70..21ee5a1192573 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -797,6 +797,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 955dc643586e9..46c9738d022d0 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -799,6 +799,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 472792e48fa01..c9ff9312203ec 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -794,6 +794,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 051ab45f450fa..bc1675b886229 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -610,6 +610,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 e899bf97ea3f6..433108bc1e4e4 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -249,6 +249,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 a65b6f0274fd8..4c5f73c0140f2 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -634,6 +634,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 fee0038c88cc0..3f074d35016ec 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -636,6 +636,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 bdca0baf478c7..6d3f0ce214a48 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -866,6 +866,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 3d3e77d826b06..efc5ceb6fa075 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -479,6 +479,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 13343dea76fe3..97378d87f244c 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -887,6 +887,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 24df7e63e3ebf..14be9e28f4e03 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/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/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index f777fc6c94bc2..f5e42ddf799af 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -324,6 +324,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/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst
index e8b3fe63b2a6c..7d260f8aced65 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -285,7 +285,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                   |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/shared/math.h b/libc/shared/math.h
index ede0ebd5371ac..9972a34715143 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -64,6 +64,7 @@
 #include "math/ceilf16.h"
 #include "math/ceill.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 0df8262cfb5f2..686073d291bd9 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -720,6 +720,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 e617950368994..18c1cb85ef872 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -109,6 +109,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 418cf85b84a20..8b85e58f8b2d6 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -303,6 +303,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 c90e5687d8c33..a241285b21914 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -61,6 +61,7 @@ add_fp_unittest(
     libc.src.__support.math.ceilf16
     libc.src.__support.math.ceill
     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 17045ce5edfdb..3271067abc8ed 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -419,6 +419,7 @@ TEST(LlvmLibcSharedMathTest, AllBFloat16) {
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::canonicalizebf16(&canonicalizebf16_cx,
                                                         &canonicalizebf16_x));
   EXPECT_FP_EQ(bfloat16(0.0), canonicalizebf16_cx);
+  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),
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 73b5ebf5a856e..ae7f09d8267e0 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 3d52873c40bb8..936784c06d609 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_N...
[truncated]

``````````

</details>


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


More information about the llvm-commits mailing list