[libc-commits] [libc] [llvm] [libc][math] Add coshbf16 math function (PR #240859)
via libc-commits
libc-commits at lists.llvm.org
Sat Nov 14 03:03:43 PST 2026
https://github.com/un-pixelated updated https://github.com/llvm/llvm-project/pull/240859
>From 087dae414d11fc73754ba82c2bf49d7998984e51 Mon Sep 17 00:00:00 2001
From: pixl <planner.chisels.7j at icloud.com>
Date: Wed, 4 Nov 2026 13:14:36 +0530
Subject: [PATCH 01/15] [libc][math] Add coshbf16 math function
---
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/docs/headers/math/index.md | 2 +-
libc/shared/math.h | 1 +
libc/shared/math/coshbf16.h | 28 ++++++++++++++++
libc/src/__support/math/CMakeLists.txt | 10 ++++++
libc/src/__support/math/coshbf16.h | 33 +++++++++++++++++++
libc/src/math/CMakeLists.txt | 1 +
libc/src/math/coshbf16.h | 26 +++++++++++++++
libc/src/math/generic/CMakeLists.txt | 10 ++++++
libc/src/math/generic/coshbf16.cpp | 23 +++++++++++++
libc/test/src/math/CMakeLists.txt | 12 +++++++
libc/test/src/math/coshbf16_test.cpp | 26 +++++++++++++++
libc/test/src/math/smoke/CMakeLists.txt | 11 +++++++
libc/test/src/math/smoke/coshbf16_test.cpp | 31 +++++++++++++++++
25 files changed, 225 insertions(+), 1 deletion(-)
create mode 100644 libc/shared/math/coshbf16.h
create mode 100644 libc/src/__support/math/coshbf16.h
create mode 100644 libc/src/math/coshbf16.h
create mode 100644 libc/src/math/generic/coshbf16.cpp
create mode 100644 libc/test/src/math/coshbf16_test.cpp
create mode 100644 libc/test/src/math/smoke/coshbf16_test.cpp
diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index bd46d678013fd0..9462505fb11fcd 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -851,6 +851,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.canonicalizebf16
libc.src.math.ceilbf16
libc.src.math.copysignbf16
+ libc.src.math.coshbf16
libc.src.math.exp2bf16
libc.src.math.expbf16
libc.src.math.fabsbf16
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index d51028d08bd068..fdde17666eda4b 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -862,6 +862,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.canonicalizebf16
libc.src.math.ceilbf16
libc.src.math.copysignbf16
+ libc.src.math.coshbf16
libc.src.math.exp2bf16
libc.src.math.expbf16
libc.src.math.fabsbf16
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index 6f2d3510716f9c..fa24a3e6cbec7e 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -854,6 +854,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.canonicalizebf16
libc.src.math.ceilbf16
libc.src.math.copysignbf16
+ libc.src.math.coshbf16
libc.src.math.exp2bf16
libc.src.math.expbf16
libc.src.math.fabsbf16
diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index 78cffd1f6b4fee..f7980f0a6849e1 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -673,6 +673,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.canonicalizebf16
libc.src.math.ceilbf16
libc.src.math.copysignbf16
+ libc.src.math.coshbf16
libc.src.math.exp2bf16
libc.src.math.expbf16
libc.src.math.fabsbf16
diff --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index a5f5cacb0f8638..10ceabc6828601 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -310,6 +310,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.canonicalizef80
libc.src.math.ceilbf16
libc.src.math.copysignbf16
+ libc.src.math.coshbf16
libc.src.math.exp2bf16
libc.src.math.expbf16
libc.src.math.fabsbf16
diff --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt
index ffa65e07f6e558..c7c1581239a951 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -735,6 +735,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.canonicalizebf16
libc.src.math.ceilbf16
libc.src.math.copysignbf16
+ libc.src.math.coshbf16
libc.src.math.exp2bf16
libc.src.math.expbf16
libc.src.math.fabsbf16
diff --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index 7d35aecf54e44a..c7be0b06cbfdb8 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -751,6 +751,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.canonicalizebf16
libc.src.math.ceilbf16
libc.src.math.copysignbf16
+ libc.src.math.coshbf16
libc.src.math.exp2bf16
libc.src.math.expbf16
libc.src.math.fabsbf16
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index f302835cb0aa8d..a798f26ca895dc 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1092,6 +1092,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.canonicalizebf16
libc.src.math.ceilbf16
libc.src.math.copysignbf16
+ libc.src.math.coshbf16
libc.src.math.exp2bf16
libc.src.math.expbf16
libc.src.math.fabsbf16
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 49162a88046b97..04bd7b35d4a312 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -703,6 +703,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.canonicalizef80
libc.src.math.ceilbf16
libc.src.math.copysignbf16
+ libc.src.math.coshbf16
libc.src.math.exp2bf16
libc.src.math.expbf16
libc.src.math.fabsbf16
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 13fe82809a2a93..6192a7b5845625 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1172,6 +1172,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.canonicalizebf16
libc.src.math.ceilbf16
libc.src.math.copysignbf16
+ libc.src.math.coshbf16
libc.src.math.exp2bf16
libc.src.math.expbf16
libc.src.math.fabsbf16
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index e22b50142d8a2d..a911820c72b171 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1194,6 +1194,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.canonicalizebf16
libc.src.math.ceilbf16
libc.src.math.copysignbf16
+ libc.src.math.coshbf16
libc.src.math.exp2bf16
libc.src.math.expbf16
libc.src.math.fabsbf16
diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index cebd511414bcb6..1de05fab5d5e41 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -409,6 +409,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.canonicalizef80
libc.src.math.ceilbf16
libc.src.math.copysignbf16
+ libc.src.math.coshbf16
libc.src.math.exp2bf16
libc.src.math.expbf16
libc.src.math.fabsbf16
diff --git a/libc/docs/headers/math/index.md b/libc/docs/headers/math/index.md
index e82e308351dce4..56f741760c84cc 100644
--- a/libc/docs/headers/math/index.md
+++ b/libc/docs/headers/math/index.md
@@ -180,7 +180,7 @@ log
| cbrt | {{ check }} | {{ check }} | | | {{ check }} | | {{ check }} | 7.12.7.1 | F.10.4.1 |
| compoundn | | | | | | | | 7.12.7.2 | F.10.4.2 |
| cos | {{ check }} | {{ check }} | | | {{ check }} | | | 7.12.4.5 | F.10.1.5 |
-| cosh | {{ check }} | {{ check }} | | | {{ check }} | | | 7.12.5.4 | F.10.2.4 |
+| cosh | {{ check }} | {{ check }} | | | {{ check }} | | {{ check }} | 7.12.5.4 | F.10.2.4 |
| cospi | {{ check }} | | | | {{ check }} | | | 7.12.4.12 | F.10.1.12 |
| dsqrt | N/A | N/A | | | N/A | {{ check }}\* | | 7.12.14.6 | F.10.11 |
| erf | {{ check }} | | | | | | | 7.12.8.1 | F.10.5.1 |
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 348ef11f9f655a..f670cd18a71622 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -88,6 +88,7 @@
#include "math/cosf.h"
#include "math/cosf16.h"
#include "math/cosh.h"
+#include "math/coshbf16.h"
#include "math/coshf.h"
#include "math/coshf16.h"
#include "math/cospif.h"
diff --git a/libc/shared/math/coshbf16.h b/libc/shared/math/coshbf16.h
new file mode 100644
index 00000000000000..3e4c53a52c9941
--- /dev/null
+++ b/libc/shared/math/coshbf16.h
@@ -0,0 +1,28 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Shared header for the coshbf16 function.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_MATH_COSHBF16_H
+#define LLVM_LIBC_SHARED_MATH_COSHBF16_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/coshbf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::coshbf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_COSHBF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index efde77253fbbde..5e05968dd7ea63 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -4602,6 +4602,16 @@ add_header_library(
libc.src.__support.macros.properties.cpu_features
)
+add_header_library(
+ coshbf16
+ HDRS
+ coshbf16.h
+ DEPENDS
+ libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.macros.config
+ libc.src.__support.macros.properties.types
+)
+
add_header_library(
exp2bf16
HDRS
diff --git a/libc/src/__support/math/coshbf16.h b/libc/src/__support/math/coshbf16.h
new file mode 100644
index 00000000000000..44225bf0baa6be
--- /dev/null
+++ b/libc/src/__support/math/coshbf16.h
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation of the coshbf16 function, which computes the hyperbolic
+/// cosine of a bfloat16 value.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_COSHBF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_COSHBF16_H
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+LIBC_INLINE bfloat16 coshbf16(bfloat16 x) {
+ // TODO: Implement coshbf16.
+ return x;
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_COSHBF16_H
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 6119fdb817f050..684c877448c54b 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -124,6 +124,7 @@ add_math_entrypoint_object(cosf)
add_math_entrypoint_object(cosf16)
add_math_entrypoint_object(cosh)
+add_math_entrypoint_object(coshbf16)
add_math_entrypoint_object(coshf)
add_math_entrypoint_object(coshf16)
diff --git a/libc/src/math/coshbf16.h b/libc/src/math/coshbf16.h
new file mode 100644
index 00000000000000..8c335ceb039cef
--- /dev/null
+++ b/libc/src/math/coshbf16.h
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Public declaration of the coshbf16 entrypoint.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_MATH_COSHBF16_H
+#define LLVM_LIBC_SRC_MATH_COSHBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 coshbf16(bfloat16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_COSHBF16_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index e79e69f9bfc0b7..60dc82e4e16bf8 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -1305,6 +1305,16 @@ add_entrypoint_object(
libc.src.__support.math.exp2f
)
+add_entrypoint_object(
+ coshbf16
+ SRCS
+ coshbf16.cpp
+ HDRS
+ ../coshbf16.h
+ DEPENDS
+ libc.src.__support.math.coshbf16
+)
+
add_entrypoint_object(
exp2bf16
SRCS
diff --git a/libc/src/math/generic/coshbf16.cpp b/libc/src/math/generic/coshbf16.cpp
new file mode 100644
index 00000000000000..65feb713a7de6c
--- /dev/null
+++ b/libc/src/math/generic/coshbf16.cpp
@@ -0,0 +1,23 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Entrypoint definition for the coshbf16 function.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/math/coshbf16.h"
+#include "src/math/coshbf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, coshbf16, (bfloat16 x)) {
+ return math::coshbf16(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 3ae032b82fea8d..7e7f660cb62fef 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -1399,6 +1399,18 @@ add_fp_unittest(
libc.src.__support.FPUtil.fp_bits
)
+add_fp_unittest(
+ coshbf16_test
+ NEED_MPFR
+ SUITE
+ libc-math-unittests
+ SRCS
+ coshbf16_test.cpp
+ DEPENDS
+ libc.src.math.coshbf16
+ libc.src.__support.FPUtil.bfloat16
+)
+
add_fp_unittest(
exp2bf16_test
NEED_MPFR
diff --git a/libc/test/src/math/coshbf16_test.cpp b/libc/test/src/math/coshbf16_test.cpp
new file mode 100644
index 00000000000000..7eada0421ae5d3
--- /dev/null
+++ b/libc/test/src/math/coshbf16_test.cpp
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Exhaustive test for the coshbf16 function.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/coshbf16.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+using LlvmLibcCoshbf16Test = LIBC_NAMESPACE::testing::FPTest<bfloat16>;
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+// TODO: Add exhaustive PositiveRange / NegativeRange tests against MPFR once
+// coshbf16 is implemented.
+TEST_F(LlvmLibcCoshbf16Test, Placeholder) {}
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index b00f01371bedc0..de8560952d6c29 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -1537,6 +1537,17 @@ add_fp_unittest(
libc.src.__support.FPUtil.fp_bits
)
+add_fp_unittest(
+ coshbf16_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ coshbf16_test.cpp
+ DEPENDS
+ libc.src.math.coshbf16
+ libc.src.__support.FPUtil.bfloat16
+)
+
add_fp_unittest(
exp2bf16_test
SUITE
diff --git a/libc/test/src/math/smoke/coshbf16_test.cpp b/libc/test/src/math/smoke/coshbf16_test.cpp
new file mode 100644
index 00000000000000..0282da1ce333d2
--- /dev/null
+++ b/libc/test/src/math/smoke/coshbf16_test.cpp
@@ -0,0 +1,31 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Smoke test for the coshbf16 function.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/coshbf16.h"
+#include "test/UnitTest/FEnvSafeTest.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+class LlvmLibcCoshbf16Test : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+ DECLARE_SPECIAL_CONSTANTS(bfloat16)
+
+public:
+ void test_special_numbers() {
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::coshbf16(aNaN));
+ // TODO: Add the remaining special values (sNaN, +-0, +-inf, overflow)
+ // once coshbf16 is implemented.
+ }
+};
+
+TEST_F(LlvmLibcCoshbf16Test, SpecialNumbers) { test_special_numbers(); }
>From 27f4dfe20d76c27644e9dc23bdad57d166c6749d Mon Sep 17 00:00:00 2001
From: pixl <planner.chisels.7j at icloud.com>
Date: Tue, 10 Nov 2026 20:16:49 +0530
Subject: [PATCH 02/15] [libc][math] Implement coshbf16
---
libc/src/__support/math/CMakeLists.txt | 11 ++++-
libc/src/__support/math/coshbf16.h | 64 ++++++++++++++++++++++++--
2 files changed, 71 insertions(+), 4 deletions(-)
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 5e05968dd7ea63..146c858f85a1a8 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -4607,9 +4607,18 @@ add_header_library(
HDRS
coshbf16.h
DEPENDS
+ .sinhcoshbf16_utils
+ libc.hdr.fenv_macros
+ libc.hdr.stdint_proxy
libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.FPUtil.cast
+ libc.src.__support.FPUtil.except_value_utils
+ libc.src.__support.FPUtil.fenv_impl
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.FPUtil.rounding_mode
+ libc.src.__support.macros.attributes
libc.src.__support.macros.config
- libc.src.__support.macros.properties.types
+ libc.src.__support.macros.optimization
)
add_header_library(
diff --git a/libc/src/__support/math/coshbf16.h b/libc/src/__support/math/coshbf16.h
index 44225bf0baa6be..6cfd668fd53164 100644
--- a/libc/src/__support/math/coshbf16.h
+++ b/libc/src/__support/math/coshbf16.h
@@ -15,16 +15,74 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_COSHBF16_H
#define LLVM_LIBC_SRC___SUPPORT_MATH_COSHBF16_H
+#include "hdr/fenv_macros.h"
+#include "hdr/stdint_proxy.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/except_value_utils.h"
+#include "src/__support/FPUtil/rounding_mode.h"
+#include "src/__support/macros/attributes.h"
#include "src/__support/macros/config.h"
-#include "src/__support/macros/properties/types.h"
+#include "src/__support/macros/optimization.h"
+#include "src/__support/math/sinhcoshbf16_utils.h"
namespace LIBC_NAMESPACE_DECL {
+
namespace math {
LIBC_INLINE bfloat16 coshbf16(bfloat16 x) {
- // TODO: Implement coshbf16.
- return x;
+ using FPBits = fputil::FPBits<bfloat16>;
+ FPBits x_bits(x);
+ // cosh is an even function, so only |x| matters.
+ uint16_t x_abs = x_bits.uintval() & 0x7fff;
+
+ // |x| <= 0x1.68p-4 (~0.0879)
+ // cosh(x) = 1 + x^2/2 + ..., and for these inputs the excess over 1 is less
+ // than half an ulp of 1 in bfloat16 (2^-8), so the result rounds to 1 (or to
+ // the next value above 1 when rounding upward, since cosh(x) > 1 for x != 0).
+ if (x_abs <= 0x3db4) {
+ if (x_abs == 0U)
+ return FPBits::one().get_val();
+
+#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+ if (fputil::fenv_is_round_up())
+ return FPBits(static_cast<uint16_t>(FPBits::one().uintval() + 1))
+ .get_val();
+#endif
+ return FPBits::one().get_val();
+ }
+
+ // |x| >= 89.5 (or inf / NaN)
+ if (LIBC_UNLIKELY(x_abs >= 0x42b3)) {
+ // cosh(+-inf) = +inf
+ if (x_bits.is_inf())
+ return FPBits::inf().get_val();
+
+ // cosh(NaN) = NaN
+ if (x_bits.is_nan()) {
+ if (x_bits.is_signaling_nan()) {
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+ return x;
+ }
+
+ // cosh(x) is positive for all x, so the overflow is always to +inf.
+ return fputil::set_overflow<bfloat16>();
+ }
+
+ // |x| >= 6.875
+ // e^(-|x|) is negligible, so cosh(x) ~ e^|x| / 2.
+ if (x_abs >= 0x40dc)
+ return fputil::cast<bfloat16>(
+ math::sinhcoshbf16_internal::exp_half(x_bits.abs().get_val()));
+
+ // cosh(x) = (e^x + e^(-x)) / 2.
+ return fputil::cast<bfloat16>(
+ math::sinhcoshbf16_internal::eval_sinh_or_cosh</*IsSinh=*/false>(
+ static_cast<float>(x)));
}
} // namespace math
>From 7c9ee0701856b7532a6975a65a1b69c795d01b22 Mon Sep 17 00:00:00 2001
From: pixl <planner.chisels.7j at icloud.com>
Date: Tue, 10 Nov 2026 20:17:24 +0530
Subject: [PATCH 03/15] [libc][math] Add coshbf16 tests
---
libc/test/src/math/CMakeLists.txt | 2 +-
libc/test/src/math/coshbf16_test.cpp | 30 +++++++--
libc/test/src/math/smoke/CMakeLists.txt | 5 +-
libc/test/src/math/smoke/coshbf16_test.cpp | 74 ++++++++++++++++++++--
4 files changed, 98 insertions(+), 13 deletions(-)
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 7e7f660cb62fef..94ad3cf22b9f87 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -1403,7 +1403,7 @@ add_fp_unittest(
coshbf16_test
NEED_MPFR
SUITE
- libc-math-unittests
+ libc-math-tests
SRCS
coshbf16_test.cpp
DEPENDS
diff --git a/libc/test/src/math/coshbf16_test.cpp b/libc/test/src/math/coshbf16_test.cpp
index 7eada0421ae5d3..e9cf1fcf385e76 100644
--- a/libc/test/src/math/coshbf16_test.cpp
+++ b/libc/test/src/math/coshbf16_test.cpp
@@ -7,7 +7,7 @@
//===----------------------------------------------------------------------===//
///
/// \file
-/// Exhaustive test for the coshbf16 function.
+/// This file contains exhaustive tests for bfloat16 cosh.
///
//===----------------------------------------------------------------------===//
@@ -17,10 +17,30 @@
#include "test/UnitTest/Test.h"
#include "utils/MPFRWrapper/MPFRUtils.h"
-using LlvmLibcCoshbf16Test = LIBC_NAMESPACE::testing::FPTest<bfloat16>;
+using LlvmLibcCoshBf16Test = LIBC_NAMESPACE::testing::FPTest<bfloat16>;
namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
-// TODO: Add exhaustive PositiveRange / NegativeRange tests against MPFR once
-// coshbf16 is implemented.
-TEST_F(LlvmLibcCoshbf16Test, Placeholder) {}
+// 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(LlvmLibcCoshBf16Test, PositiveRange) {
+ for (uint16_t v = POS_START; v <= POS_STOP; ++v) {
+ bfloat16 x = FPBits(v).get_val();
+ EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Cosh, x,
+ LIBC_NAMESPACE::coshbf16(x), 0.5);
+ }
+}
+
+TEST_F(LlvmLibcCoshBf16Test, NegativeRange) {
+ for (uint16_t v = NEG_START; v <= NEG_STOP; ++v) {
+ bfloat16 x = FPBits(v).get_val();
+ EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Cosh, x,
+ LIBC_NAMESPACE::coshbf16(x), 0.5);
+ }
+}
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index de8560952d6c29..ea6fc5baeeb479 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -1544,8 +1544,11 @@ add_fp_unittest(
SRCS
coshbf16_test.cpp
DEPENDS
- libc.src.math.coshbf16
+ libc.hdr.errno_macros
+ libc.hdr.fenv_macros
+ libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.bfloat16
+ libc.src.math.coshbf16
)
add_fp_unittest(
diff --git a/libc/test/src/math/smoke/coshbf16_test.cpp b/libc/test/src/math/smoke/coshbf16_test.cpp
index 0282da1ce333d2..0c3693d58eef08 100644
--- a/libc/test/src/math/smoke/coshbf16_test.cpp
+++ b/libc/test/src/math/smoke/coshbf16_test.cpp
@@ -7,25 +7,87 @@
//===----------------------------------------------------------------------===//
///
/// \file
-/// Smoke test for the coshbf16 function.
+/// This file contains unit tests for bfloat16 cosh.
///
//===----------------------------------------------------------------------===//
+#include "hdr/errno_macros.h"
+#include "hdr/fenv_macros.h"
+#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/FPUtil/bfloat16.h"
#include "src/math/coshbf16.h"
#include "test/UnitTest/FEnvSafeTest.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"
-class LlvmLibcCoshbf16Test : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+class LlvmLibcCoshBf16Test : public LIBC_NAMESPACE::testing::FEnvSafeTest {
DECLARE_SPECIAL_CONSTANTS(bfloat16)
-
public:
void test_special_numbers() {
EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::coshbf16(aNaN));
- // TODO: Add the remaining special values (sNaN, +-0, +-inf, overflow)
- // once coshbf16 is implemented.
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::coshbf16(sNaN),
+ FE_INVALID);
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(inf, LIBC_NAMESPACE::coshbf16(inf));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(inf, LIBC_NAMESPACE::coshbf16(neg_inf));
+ EXPECT_MATH_ERRNO(0);
+
+ // cosh(+-0) = 1 exactly.
+ bfloat16 one = FPBits::one().get_val();
+ EXPECT_FP_EQ_ALL_ROUNDING(one, LIBC_NAMESPACE::coshbf16(zero));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(one, LIBC_NAMESPACE::coshbf16(neg_zero));
+ EXPECT_MATH_ERRNO(0);
+ }
+
+ // For small values, cosh(x) rounds to 1 (or to the next value above 1 when
+ // rounding upward, since cosh(x) > 1 for x != 0).
+ void test_small_values() {
+ bfloat16 one = FPBits::one().get_val();
+ bfloat16 one_plus_ulp = FPBits(uint16_t(0x3f81)).get_val();
+
+ for (uint16_t u : {uint16_t(0x0001), uint16_t(0x3db4)}) {
+ for (uint16_t sign : {uint16_t(0x0000), uint16_t(0x8000)}) {
+ bfloat16 x = FPBits(static_cast<uint16_t>(u | sign)).get_val();
+ EXPECT_FP_EQ(one, LIBC_NAMESPACE::coshbf16(x));
+ }
+ }
+
+ // First input whose result no longer rounds to 1 in round-to-nearest.
+ EXPECT_FP_EQ(one_plus_ulp,
+ LIBC_NAMESPACE::coshbf16(FPBits(uint16_t(0x3db5)).get_val()));
+ EXPECT_FP_EQ(one_plus_ulp,
+ LIBC_NAMESPACE::coshbf16(FPBits(uint16_t(0xbdb5)).get_val()));
+ }
+
+ void test_overflow() {
+ EXPECT_FP_EQ_WITH_EXCEPTION(
+ inf, LIBC_NAMESPACE::coshbf16(FPBits(uint16_t(0x42b3)).get_val()),
+ FE_OVERFLOW);
+ EXPECT_MATH_ERRNO(ERANGE);
+
+ EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::coshbf16(max_normal),
+ FE_OVERFLOW);
+ EXPECT_MATH_ERRNO(ERANGE);
+
+ // cosh is even: large negative inputs overflow to +inf as well.
+ EXPECT_FP_EQ_WITH_EXCEPTION(
+ inf, LIBC_NAMESPACE::coshbf16(FPBits(uint16_t(0xc2b3)).get_val()),
+ FE_OVERFLOW);
+ EXPECT_MATH_ERRNO(ERANGE);
+
+ EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::coshbf16(neg_max_normal),
+ FE_OVERFLOW);
+ EXPECT_MATH_ERRNO(ERANGE);
}
};
-TEST_F(LlvmLibcCoshbf16Test, SpecialNumbers) { test_special_numbers(); }
+TEST_F(LlvmLibcCoshBf16Test, SpecialNumbers) { test_special_numbers(); }
+TEST_F(LlvmLibcCoshBf16Test, SmallValues) { test_small_values(); }
+TEST_F(LlvmLibcCoshBf16Test, Overflow) { test_overflow(); }
>From be1ee5bdbb262dc0f13c20ec0d4e5427d8638aa3 Mon Sep 17 00:00:00 2001
From: pixl <planner.chisels.7j at icloud.com>
Date: Tue, 10 Nov 2026 20:17:39 +0530
Subject: [PATCH 04/15] [libc][math] Enable coshbf16 on hexagon and freebsd
---
libc/config/baremetal/hexagon/entrypoints.txt | 1 +
libc/config/freebsd/x86_64/entrypoints.txt | 1 +
2 files changed, 2 insertions(+)
diff --git a/libc/config/baremetal/hexagon/entrypoints.txt b/libc/config/baremetal/hexagon/entrypoints.txt
index b6603523d478a7..4e06f5d6b356ae 100644
--- a/libc/config/baremetal/hexagon/entrypoints.txt
+++ b/libc/config/baremetal/hexagon/entrypoints.txt
@@ -852,6 +852,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.canonicalizebf16
libc.src.math.ceilbf16
libc.src.math.copysignbf16
+ libc.src.math.coshbf16
libc.src.math.expbf16
libc.src.math.fabsbf16
libc.src.math.fdimbf16
diff --git a/libc/config/freebsd/x86_64/entrypoints.txt b/libc/config/freebsd/x86_64/entrypoints.txt
index 7ee9f58c04f72e..d50f56c9819ca9 100644
--- a/libc/config/freebsd/x86_64/entrypoints.txt
+++ b/libc/config/freebsd/x86_64/entrypoints.txt
@@ -606,6 +606,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.canonicalizebf16
libc.src.math.ceilbf16
libc.src.math.copysignbf16
+ libc.src.math.coshbf16
libc.src.math.expbf16
libc.src.math.fabsbf16
libc.src.math.fdimbf16
>From 5a3e099842d845bfaf2a4a0e980c72f6b0d593ba Mon Sep 17 00:00:00 2001
From: pixl <planner.chisels.7j at icloud.com>
Date: Tue, 10 Nov 2026 20:18:00 +0530
Subject: [PATCH 05/15] [libc][math] Add coshbf16 to shared math tests
---
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
2 files changed, 2 insertions(+)
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 18638523256cf3..a75eb1ac6ecb28 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -104,6 +104,7 @@ else()
libc.src.__support.math.cosf
libc.src.__support.math.cosf16
libc.src.__support.math.cosh
+ libc.src.__support.math.coshbf16
libc.src.__support.math.coshf
libc.src.__support.math.coshf16
libc.src.__support.math.cospif
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 9ddce72220c8dd..4d0147b6f584b5 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -870,6 +870,7 @@ TEST(LlvmLibcSharedMathTest, AllBFloat16) {
EXPECT_FP_EQ(bfloat16(1.0), LIBC_NAMESPACE::shared::expbf16(bfloat16(0.0)));
EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::log_bf16(bfloat16(1.0)));
EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::sinhbf16(bfloat16(0.0)));
+ EXPECT_FP_EQ(bfloat16(1.0), LIBC_NAMESPACE::shared::coshbf16(bfloat16(0.0)));
bfloat16 neg_min_denormal = FPBits::min_subnormal(Sign::NEG).get_val();
EXPECT_FP_EQ(neg_min_denormal,
>From a977b688388d233bcfbf9e6edbfcfd66304eea0d Mon Sep 17 00:00:00 2001
From: pixl <planner.chisels.7j at icloud.com>
Date: Tue, 10 Nov 2026 20:18:16 +0530
Subject: [PATCH 06/15] [libc][math][bazel] Add coshbf16 Bazel targets
---
.../llvm-project-overlay/libc/BUILD.bazel | 26 +++++++++++++++++++
.../libc/test/src/math/BUILD.bazel | 7 +++++
.../libc/test/src/math/smoke/BUILD.bazel | 10 +++++++
3 files changed, 43 insertions(+)
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 42e3ae6fffaf9c..766faa02dbf454 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -9373,6 +9373,25 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_math_coshbf16",
+ hdrs = ["src/__support/math/coshbf16.h"],
+ deps = [
+ ":__support_fputil_bfloat16",
+ ":__support_fputil_cast",
+ ":__support_fputil_except_value_utils",
+ ":__support_fputil_fenv_impl",
+ ":__support_fputil_fp_bits",
+ ":__support_fputil_rounding_mode",
+ ":__support_macros_attributes",
+ ":__support_macros_config",
+ ":__support_macros_optimization",
+ ":__support_math_sinhcoshbf16_utils",
+ ":hdr_fenv_macros",
+ ":hdr_stdint_proxy",
+ ],
+)
+
libc_support_library(
name = "__support_math_coshf",
hdrs = ["src/__support/math/coshf.h"],
@@ -13890,6 +13909,13 @@ libc_math_function(
additional_deps = [":__support_math_cosh"],
)
+libc_math_function(
+ name = "coshbf16",
+ additional_deps = [
+ ":__support_math_coshbf16",
+ ],
+)
+
libc_math_function(
name = "coshf",
additional_deps = [":__support_math_coshf"],
diff --git a/utils/bazel/llvm-project-overlay/libc/test/src/math/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/src/math/BUILD.bazel
index 3c216b8b091300..342a8dcedfc4f4 100644
--- a/utils/bazel/llvm-project-overlay/libc/test/src/math/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/test/src/math/BUILD.bazel
@@ -197,6 +197,13 @@ math_mpfr_test(
],
)
+math_mpfr_test(
+ name = "coshbf16",
+ deps = [
+ "//libc:__support_fputil_bfloat16",
+ ],
+)
+
math_mpfr_test(
name = "coshf",
deps = [
diff --git a/utils/bazel/llvm-project-overlay/libc/test/src/math/smoke/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/src/math/smoke/BUILD.bazel
index ae8d44ff7ab433..69ac643e81f355 100644
--- a/utils/bazel/llvm-project-overlay/libc/test/src/math/smoke/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/test/src/math/smoke/BUILD.bazel
@@ -339,6 +339,16 @@ math_test(
],
)
+math_test(
+ name = "coshbf16",
+ deps = [
+ "//libc:__support_fputil_bfloat16",
+ "//libc:__support_fputil_fp_bits",
+ "//libc:hdr_errno_macros",
+ "//libc:hdr_fenv_macros",
+ ],
+)
+
math_test(
name = "coshf",
deps = [
>From 5d1d2a3a11f4b04b2b6cc284fbc116545f608ea6 Mon Sep 17 00:00:00 2001
From: pixl <planner.chisels.7j at icloud.com>
Date: Tue, 10 Nov 2026 20:19:08 +0530
Subject: [PATCH 07/15] [libc][math] Fix coshbf16 smoke test to avoid
initializer_list
---
libc/test/src/math/smoke/coshbf16_test.cpp | 16 ++++++++++------
1 file changed, 10 insertions(+), 6 deletions(-)
diff --git a/libc/test/src/math/smoke/coshbf16_test.cpp b/libc/test/src/math/smoke/coshbf16_test.cpp
index 0c3693d58eef08..d60008c6013833 100644
--- a/libc/test/src/math/smoke/coshbf16_test.cpp
+++ b/libc/test/src/math/smoke/coshbf16_test.cpp
@@ -52,12 +52,16 @@ class LlvmLibcCoshBf16Test : public LIBC_NAMESPACE::testing::FEnvSafeTest {
bfloat16 one = FPBits::one().get_val();
bfloat16 one_plus_ulp = FPBits(uint16_t(0x3f81)).get_val();
- for (uint16_t u : {uint16_t(0x0001), uint16_t(0x3db4)}) {
- for (uint16_t sign : {uint16_t(0x0000), uint16_t(0x8000)}) {
- bfloat16 x = FPBits(static_cast<uint16_t>(u | sign)).get_val();
- EXPECT_FP_EQ(one, LIBC_NAMESPACE::coshbf16(x));
- }
- }
+ // Largest inputs (in magnitude) that still round to 1, and the smallest
+ // subnormal.
+ EXPECT_FP_EQ(one,
+ LIBC_NAMESPACE::coshbf16(FPBits(uint16_t(0x0001)).get_val()));
+ EXPECT_FP_EQ(one,
+ LIBC_NAMESPACE::coshbf16(FPBits(uint16_t(0x8001)).get_val()));
+ EXPECT_FP_EQ(one,
+ LIBC_NAMESPACE::coshbf16(FPBits(uint16_t(0x3db4)).get_val()));
+ EXPECT_FP_EQ(one,
+ LIBC_NAMESPACE::coshbf16(FPBits(uint16_t(0xbdb4)).get_val()));
// First input whose result no longer rounds to 1 in round-to-nearest.
EXPECT_FP_EQ(one_plus_ulp,
>From 4a8cb702d7214a4bc656aeb4badd2bd1fead2111 Mon Sep 17 00:00:00 2001
From: pixl <planner.chisels.7j at icloud.com>
Date: Tue, 10 Nov 2026 20:21:38 +0530
Subject: [PATCH 08/15] [libc][math] Restructure coshbf16 comments and checks
in exp2bf16 style
---
libc/src/__support/math/coshbf16.h | 66 ++++++++++++++++++------------
1 file changed, 39 insertions(+), 27 deletions(-)
diff --git a/libc/src/__support/math/coshbf16.h b/libc/src/__support/math/coshbf16.h
index 6cfd668fd53164..7c7d8e0c3a0e7b 100644
--- a/libc/src/__support/math/coshbf16.h
+++ b/libc/src/__support/math/coshbf16.h
@@ -35,14 +35,29 @@ namespace math {
LIBC_INLINE bfloat16 coshbf16(bfloat16 x) {
using FPBits = fputil::FPBits<bfloat16>;
FPBits x_bits(x);
- // cosh is an even function, so only |x| matters.
- uint16_t x_abs = x_bits.uintval() & 0x7fff;
- // |x| <= 0x1.68p-4 (~0.0879)
- // cosh(x) = 1 + x^2/2 + ..., and for these inputs the excess over 1 is less
- // than half an ulp of 1 in bfloat16 (2^-8), so the result rounds to 1 (or to
- // the next value above 1 when rounding upward, since cosh(x) > 1 for x != 0).
- if (x_abs <= 0x3db4) {
+ // We first check for NaN
+ if (LIBC_UNLIKELY(x_bits.is_nan())) {
+ // If it is a signaling NaN, then we raise an FE_INVALID exception
+ // and return a quiet NaN
+ if (x_bits.is_signaling_nan()) {
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+ // If not, just return the NaN
+ return x;
+ }
+
+ // cosh is an even function, cosh(-x) = cosh(x), so we only need to look at
+ // |x|, and we can compare it as an integer.
+ uint16_t x_abs = x_bits.abs().uintval();
+
+ // For small |x|, cosh(x) = 1 + x^2/2 + ... lies strictly between 1 and
+ // the next bfloat16 above 1, and is closer to 1. So the result is 1 in all
+ // rounding modes except for round-up, where it is the next value above 1.
+ // x = 0 is the exception, since cosh(0) = 1 exactly.
+ constexpr uint16_t SMALL_ABS_BOUND = 0x3DB4; // 0x1.68p-4 = 0.0879
+ if (LIBC_UNLIKELY(x_abs <= SMALL_ABS_BOUND)) {
if (x_abs == 0U)
return FPBits::one().get_val();
@@ -54,35 +69,32 @@ LIBC_INLINE bfloat16 coshbf16(bfloat16 x) {
return FPBits::one().get_val();
}
- // |x| >= 89.5 (or inf / NaN)
- if (LIBC_UNLIKELY(x_abs >= 0x42b3)) {
- // cosh(+-inf) = +inf
+ // cosh(x) overflows bfloat16 for |x| >= 89.5. cosh(x) > 0 for all x, so the
+ // overflow is always to +Inf, regardless of the sign of x.
+ constexpr uint16_t OVERFLOW_ABS_BOUND = 0x42B3; // 0x1.66p6 = 89.5
+ if (LIBC_UNLIKELY(x_abs >= OVERFLOW_ABS_BOUND)) {
+ // cosh(+-Inf) = +Inf
if (x_bits.is_inf())
return FPBits::inf().get_val();
- // cosh(NaN) = NaN
- if (x_bits.is_nan()) {
- if (x_bits.is_signaling_nan()) {
- fputil::raise_except_if_required(FE_INVALID);
- return FPBits::quiet_nan().get_val();
- }
- return x;
- }
-
- // cosh(x) is positive for all x, so the overflow is always to +inf.
return fputil::set_overflow<bfloat16>();
}
- // |x| >= 6.875
- // e^(-|x|) is negligible, so cosh(x) ~ e^|x| / 2.
- if (x_abs >= 0x40dc)
- return fputil::cast<bfloat16>(
- math::sinhcoshbf16_internal::exp_half(x_bits.abs().get_val()));
+ // For |x| >= 6.875, e^(-|x|) / e^|x| = e^(-2|x|) < 2^-19 is far below the
+ // precision of bfloat16, so we can use
+ // cosh(x) = (e^|x| + e^(-|x|)) / 2 ~ e^|x| / 2.
+ constexpr uint16_t EXP_HALF_ABS_BOUND = 0x40DC; // 0x1.b8p2 = 6.875
+ if (x_abs >= EXP_HALF_ABS_BOUND)
+ return fputil::cast<bfloat16>(math::sinhcoshbf16_internal::exp_half(
+ fputil::cast<float>(x_bits.abs().get_val())));
- // cosh(x) = (e^x + e^(-x)) / 2.
+ // For the remaining inputs, we compute
+ // cosh(x) = (e^x + e^(-x)) / 2
+ // with the range reduction and the shared polynomial in
+ // sinhcoshbf16_utils.h.
return fputil::cast<bfloat16>(
math::sinhcoshbf16_internal::eval_sinh_or_cosh</*IsSinh=*/false>(
- static_cast<float>(x)));
+ fputil::cast<float>(x)));
}
} // namespace math
>From df34a4df01fd312ed51efe93ea034b50790dfb21 Mon Sep 17 00:00:00 2001
From: pixl <planner.chisels.7j at icloud.com>
Date: Tue, 10 Nov 2026 20:21:47 +0530
Subject: [PATCH 09/15] [libc][math] Test coshbf16 small inputs in all rounding
modes
---
libc/test/src/math/smoke/coshbf16_test.cpp | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/libc/test/src/math/smoke/coshbf16_test.cpp b/libc/test/src/math/smoke/coshbf16_test.cpp
index d60008c6013833..82ee8b95dac9c5 100644
--- a/libc/test/src/math/smoke/coshbf16_test.cpp
+++ b/libc/test/src/math/smoke/coshbf16_test.cpp
@@ -63,6 +63,13 @@ class LlvmLibcCoshBf16Test : public LIBC_NAMESPACE::testing::FEnvSafeTest {
EXPECT_FP_EQ(one,
LIBC_NAMESPACE::coshbf16(FPBits(uint16_t(0xbdb4)).get_val()));
+ // cosh(x) > 1 for x != 0, so only round-up moves the result above 1.
+ bfloat16 small = FPBits(uint16_t(0x0001)).get_val();
+ EXPECT_FP_EQ_ROUNDING_NEAREST(one, LIBC_NAMESPACE::coshbf16(small));
+ EXPECT_FP_EQ_ROUNDING_UPWARD(one_plus_ulp, LIBC_NAMESPACE::coshbf16(small));
+ EXPECT_FP_EQ_ROUNDING_DOWNWARD(one, LIBC_NAMESPACE::coshbf16(small));
+ EXPECT_FP_EQ_ROUNDING_TOWARD_ZERO(one, LIBC_NAMESPACE::coshbf16(small));
+
// First input whose result no longer rounds to 1 in round-to-nearest.
EXPECT_FP_EQ(one_plus_ulp,
LIBC_NAMESPACE::coshbf16(FPBits(uint16_t(0x3db5)).get_val()));
>From 77705df5d7c79975e285787e6f92f7e58cd17538 Mon Sep 17 00:00:00 2001
From: pixl <planner.chisels.7j at icloud.com>
Date: Tue, 10 Nov 2026 20:30:26 +0530
Subject: [PATCH 10/15] [libc][math] Move coshbf16 CMake targets next to the
other cosh targets
---
libc/src/__support/math/CMakeLists.txt | 38 ++++++++++++-------------
libc/src/math/generic/CMakeLists.txt | 20 ++++++-------
libc/test/src/math/CMakeLists.txt | 24 ++++++++--------
libc/test/src/math/smoke/CMakeLists.txt | 28 +++++++++---------
4 files changed, 55 insertions(+), 55 deletions(-)
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 146c858f85a1a8..a5fb91eec4de5f 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -3060,6 +3060,25 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ coshbf16
+ HDRS
+ coshbf16.h
+ DEPENDS
+ .sinhcoshbf16_utils
+ libc.hdr.fenv_macros
+ libc.hdr.stdint_proxy
+ libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.FPUtil.cast
+ libc.src.__support.FPUtil.except_value_utils
+ libc.src.__support.FPUtil.fenv_impl
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.FPUtil.rounding_mode
+ libc.src.__support.macros.attributes
+ libc.src.__support.macros.config
+ libc.src.__support.macros.optimization
+)
+
add_header_library(
coshf16
HDRS
@@ -4602,25 +4621,6 @@ add_header_library(
libc.src.__support.macros.properties.cpu_features
)
-add_header_library(
- coshbf16
- HDRS
- coshbf16.h
- DEPENDS
- .sinhcoshbf16_utils
- libc.hdr.fenv_macros
- libc.hdr.stdint_proxy
- libc.src.__support.FPUtil.bfloat16
- libc.src.__support.FPUtil.cast
- libc.src.__support.FPUtil.except_value_utils
- libc.src.__support.FPUtil.fenv_impl
- libc.src.__support.FPUtil.fp_bits
- libc.src.__support.FPUtil.rounding_mode
- libc.src.__support.macros.attributes
- libc.src.__support.macros.config
- libc.src.__support.macros.optimization
-)
-
add_header_library(
exp2bf16
HDRS
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 60dc82e4e16bf8..383b45fc772d83 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -1305,16 +1305,6 @@ add_entrypoint_object(
libc.src.__support.math.exp2f
)
-add_entrypoint_object(
- coshbf16
- SRCS
- coshbf16.cpp
- HDRS
- ../coshbf16.h
- DEPENDS
- libc.src.__support.math.coshbf16
-)
-
add_entrypoint_object(
exp2bf16
SRCS
@@ -3928,6 +3918,16 @@ add_entrypoint_object(
libc.src.__support.math.coshf
)
+add_entrypoint_object(
+ coshbf16
+ SRCS
+ coshbf16.cpp
+ HDRS
+ ../coshbf16.h
+ DEPENDS
+ libc.src.__support.math.coshbf16
+)
+
add_entrypoint_object(
coshf16
SRCS
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 94ad3cf22b9f87..08c45f4d6169f6 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -1399,18 +1399,6 @@ add_fp_unittest(
libc.src.__support.FPUtil.fp_bits
)
-add_fp_unittest(
- coshbf16_test
- NEED_MPFR
- SUITE
- libc-math-tests
- SRCS
- coshbf16_test.cpp
- DEPENDS
- libc.src.math.coshbf16
- libc.src.__support.FPUtil.bfloat16
-)
-
add_fp_unittest(
exp2bf16_test
NEED_MPFR
@@ -2656,6 +2644,18 @@ add_fp_unittest(
libc.src.__support.FPUtil.fp_bits
)
+add_fp_unittest(
+ coshbf16_test
+ NEED_MPFR
+ SUITE
+ libc-math-tests
+ SRCS
+ coshbf16_test.cpp
+ DEPENDS
+ libc.src.math.coshbf16
+ libc.src.__support.FPUtil.bfloat16
+)
+
add_fp_unittest(
coshf16_test
NEED_MPFR
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index ea6fc5baeeb479..d7bb8bf8dc2dfa 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -1537,20 +1537,6 @@ add_fp_unittest(
libc.src.__support.FPUtil.fp_bits
)
-add_fp_unittest(
- coshbf16_test
- SUITE
- libc-math-smoke-tests
- SRCS
- coshbf16_test.cpp
- DEPENDS
- libc.hdr.errno_macros
- libc.hdr.fenv_macros
- libc.src.__support.FPUtil.fp_bits
- libc.src.__support.FPUtil.bfloat16
- libc.src.math.coshbf16
-)
-
add_fp_unittest(
exp2bf16_test
SUITE
@@ -5006,6 +4992,20 @@ add_fp_unittest(
libc.src.__support.FPUtil.fp_bits
)
+add_fp_unittest(
+ coshbf16_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ coshbf16_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.hdr.fenv_macros
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.FPUtil.bfloat16
+ libc.src.math.coshbf16
+)
+
add_fp_unittest(
coshf16_test
SUITE
>From 3332ea651c5bf1e164224ae056de0eea6ff7a9c9 Mon Sep 17 00:00:00 2001
From: pixl <planner.chisels.7j at icloud.com>
Date: Tue, 10 Nov 2026 20:30:26 +0530
Subject: [PATCH 11/15] [libc][math] Clarify coshbf16 variable names and range
comments
---
libc/src/__support/math/coshbf16.h | 49 ++++++++++++++++--------------
1 file changed, 26 insertions(+), 23 deletions(-)
diff --git a/libc/src/__support/math/coshbf16.h b/libc/src/__support/math/coshbf16.h
index 7c7d8e0c3a0e7b..e0cee0c61b45ab 100644
--- a/libc/src/__support/math/coshbf16.h
+++ b/libc/src/__support/math/coshbf16.h
@@ -48,31 +48,33 @@ LIBC_INLINE bfloat16 coshbf16(bfloat16 x) {
return x;
}
- // cosh is an even function, cosh(-x) = cosh(x), so we only need to look at
- // |x|, and we can compare it as an integer.
- uint16_t x_abs = x_bits.abs().uintval();
+ // cosh is an even function, cosh(-x) = cosh(x), so we only need |x|.
+ // We bound the cases below by comparing the bits of |x| as an integer.
+ uint16_t x_abs_uint = x_bits.abs().uintval();
- // For small |x|, cosh(x) = 1 + x^2/2 + ... lies strictly between 1 and
- // the next bfloat16 above 1, and is closer to 1. So the result is 1 in all
- // rounding modes except for round-up, where it is the next value above 1.
+ constexpr uint16_t ONE_UINT = 0x3F80; // 0x1p0 = 1.0
+ constexpr uint16_t ONE_NEXT_UP_UINT = 0x3F81; // 0x1.02p0 = 1.0078125
+
+ // For 0 < |x| <= 0.0879, cosh(x) = 1 + x^2/2 + ... lies strictly between 1
+ // and the next bfloat16 above 1, and is closer to 1. So the result is 1 in
+ // all rounding modes except for RU, where it is the next value above 1.
// x = 0 is the exception, since cosh(0) = 1 exactly.
- constexpr uint16_t SMALL_ABS_BOUND = 0x3DB4; // 0x1.68p-4 = 0.0879
- if (LIBC_UNLIKELY(x_abs <= SMALL_ABS_BOUND)) {
- if (x_abs == 0U)
- return FPBits::one().get_val();
+ constexpr uint16_t ROUNDS_TO_ONE_ABS_BOUND = 0x3DB4; // 0x1.68p-4 = 0.0879
+ if (x_abs_uint <= ROUNDS_TO_ONE_ABS_BOUND) {
+ if (x_abs_uint == 0U)
+ return FPBits(ONE_UINT).get_val();
#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
if (fputil::fenv_is_round_up())
- return FPBits(static_cast<uint16_t>(FPBits::one().uintval() + 1))
- .get_val();
+ return FPBits(ONE_NEXT_UP_UINT).get_val();
#endif
- return FPBits::one().get_val();
+ return FPBits(ONE_UINT).get_val();
}
// cosh(x) overflows bfloat16 for |x| >= 89.5. cosh(x) > 0 for all x, so the
// overflow is always to +Inf, regardless of the sign of x.
constexpr uint16_t OVERFLOW_ABS_BOUND = 0x42B3; // 0x1.66p6 = 89.5
- if (LIBC_UNLIKELY(x_abs >= OVERFLOW_ABS_BOUND)) {
+ if (LIBC_UNLIKELY(x_abs_uint >= OVERFLOW_ABS_BOUND)) {
// cosh(+-Inf) = +Inf
if (x_bits.is_inf())
return FPBits::inf().get_val();
@@ -80,21 +82,22 @@ LIBC_INLINE bfloat16 coshbf16(bfloat16 x) {
return fputil::set_overflow<bfloat16>();
}
- // For |x| >= 6.875, e^(-|x|) / e^|x| = e^(-2|x|) < 2^-19 is far below the
- // precision of bfloat16, so we can use
+ // For 6.875 <= |x| < 89.5, e^(-|x|) / e^|x| = e^(-2|x|) < 2^-19 is far below
+ // the precision of bfloat16, so we can use
// cosh(x) = (e^|x| + e^(-|x|)) / 2 ~ e^|x| / 2.
constexpr uint16_t EXP_HALF_ABS_BOUND = 0x40DC; // 0x1.b8p2 = 6.875
- if (x_abs >= EXP_HALF_ABS_BOUND)
- return fputil::cast<bfloat16>(math::sinhcoshbf16_internal::exp_half(
- fputil::cast<float>(x_bits.abs().get_val())));
+ if (x_abs_uint >= EXP_HALF_ABS_BOUND) {
+ float x_abs_float = fputil::cast<float>(x_bits.abs().get_val());
+ return fputil::cast<bfloat16>(sinhcoshbf16_internal::exp_half(x_abs_float));
+ }
- // For the remaining inputs, we compute
+ // For the remaining inputs, 0.0879 < |x| < 6.875, we compute
// cosh(x) = (e^x + e^(-x)) / 2
- // with the range reduction and the shared polynomial in
+ // using the range reduction and the polynomial shared with sinhbf16 in
// sinhcoshbf16_utils.h.
+ float x_float = fputil::cast<float>(x);
return fputil::cast<bfloat16>(
- math::sinhcoshbf16_internal::eval_sinh_or_cosh</*IsSinh=*/false>(
- fputil::cast<float>(x)));
+ sinhcoshbf16_internal::eval_sinh_or_cosh</*IsSinh=*/false>(x_float));
}
} // namespace math
>From c7d6d2b59f2412626d4ff87fdb348dee6ffc0543 Mon Sep 17 00:00:00 2001
From: pixl <planner.chisels.7j at icloud.com>
Date: Tue, 10 Nov 2026 20:30:26 +0530
Subject: [PATCH 12/15] [libc][math] Name coshbf16 smoke test inputs and cover
the round-to-one bound
---
libc/test/src/math/smoke/coshbf16_test.cpp | 82 ++++++++++++----------
1 file changed, 44 insertions(+), 38 deletions(-)
diff --git a/libc/test/src/math/smoke/coshbf16_test.cpp b/libc/test/src/math/smoke/coshbf16_test.cpp
index 82ee8b95dac9c5..0b0ce647e54c18 100644
--- a/libc/test/src/math/smoke/coshbf16_test.cpp
+++ b/libc/test/src/math/smoke/coshbf16_test.cpp
@@ -37,8 +37,8 @@ class LlvmLibcCoshBf16Test : public LIBC_NAMESPACE::testing::FEnvSafeTest {
EXPECT_FP_EQ_ALL_ROUNDING(inf, LIBC_NAMESPACE::coshbf16(neg_inf));
EXPECT_MATH_ERRNO(0);
- // cosh(+-0) = 1 exactly.
- bfloat16 one = FPBits::one().get_val();
+ // cosh(+-0) = 1 exactly
+ bfloat16 one = FPBits(uint16_t(0x3F80)).get_val(); // 0x1p0
EXPECT_FP_EQ_ALL_ROUNDING(one, LIBC_NAMESPACE::coshbf16(zero));
EXPECT_MATH_ERRNO(0);
@@ -46,51 +46,57 @@ class LlvmLibcCoshBf16Test : public LIBC_NAMESPACE::testing::FEnvSafeTest {
EXPECT_MATH_ERRNO(0);
}
- // For small values, cosh(x) rounds to 1 (or to the next value above 1 when
- // rounding upward, since cosh(x) > 1 for x != 0).
+ // For 0 < |x| <= 0.0879, cosh(x) is strictly between 1 and the next bfloat16
+ // above 1, and is closer to 1. So the result is 1 in all rounding modes
+ // except for RU, where it is the next value above 1.
void test_small_values() {
- bfloat16 one = FPBits::one().get_val();
- bfloat16 one_plus_ulp = FPBits(uint16_t(0x3f81)).get_val();
-
- // Largest inputs (in magnitude) that still round to 1, and the smallest
- // subnormal.
- EXPECT_FP_EQ(one,
- LIBC_NAMESPACE::coshbf16(FPBits(uint16_t(0x0001)).get_val()));
- EXPECT_FP_EQ(one,
- LIBC_NAMESPACE::coshbf16(FPBits(uint16_t(0x8001)).get_val()));
- EXPECT_FP_EQ(one,
- LIBC_NAMESPACE::coshbf16(FPBits(uint16_t(0x3db4)).get_val()));
- EXPECT_FP_EQ(one,
- LIBC_NAMESPACE::coshbf16(FPBits(uint16_t(0xbdb4)).get_val()));
-
- // cosh(x) > 1 for x != 0, so only round-up moves the result above 1.
- bfloat16 small = FPBits(uint16_t(0x0001)).get_val();
- EXPECT_FP_EQ_ROUNDING_NEAREST(one, LIBC_NAMESPACE::coshbf16(small));
- EXPECT_FP_EQ_ROUNDING_UPWARD(one_plus_ulp, LIBC_NAMESPACE::coshbf16(small));
- EXPECT_FP_EQ_ROUNDING_DOWNWARD(one, LIBC_NAMESPACE::coshbf16(small));
- EXPECT_FP_EQ_ROUNDING_TOWARD_ZERO(one, LIBC_NAMESPACE::coshbf16(small));
-
- // First input whose result no longer rounds to 1 in round-to-nearest.
- EXPECT_FP_EQ(one_plus_ulp,
- LIBC_NAMESPACE::coshbf16(FPBits(uint16_t(0x3db5)).get_val()));
- EXPECT_FP_EQ(one_plus_ulp,
- LIBC_NAMESPACE::coshbf16(FPBits(uint16_t(0xbdb5)).get_val()));
+ bfloat16 one = FPBits(uint16_t(0x3F80)).get_val(); // 0x1p0
+ bfloat16 one_next_up = FPBits(uint16_t(0x3F81)).get_val(); // 0x1.02p0
+
+ // Largest |x| for which cosh(x) still rounds to 1 in RN.
+ bfloat16 rounds_to_one_bound =
+ FPBits(uint16_t(0x3DB4)).get_val(); // 0x1.68p-4
+ // Smallest |x| for which cosh(x) rounds to the next value above 1 in RN.
+ bfloat16 rounds_above_one_bound =
+ FPBits(uint16_t(0x3DB5)).get_val(); // 0x1.6ap-4
+
+ EXPECT_FP_EQ_ROUNDING_NEAREST(one, LIBC_NAMESPACE::coshbf16(min_denormal));
+ EXPECT_FP_EQ_ROUNDING_UPWARD(one_next_up,
+ LIBC_NAMESPACE::coshbf16(min_denormal));
+ EXPECT_FP_EQ_ROUNDING_DOWNWARD(one, LIBC_NAMESPACE::coshbf16(min_denormal));
+ EXPECT_FP_EQ_ROUNDING_TOWARD_ZERO(one,
+ LIBC_NAMESPACE::coshbf16(min_denormal));
+
+ EXPECT_FP_EQ_ROUNDING_NEAREST(one,
+ LIBC_NAMESPACE::coshbf16(neg_min_denormal));
+ EXPECT_FP_EQ_ROUNDING_NEAREST(
+ one, LIBC_NAMESPACE::coshbf16(rounds_to_one_bound));
+ EXPECT_FP_EQ_ROUNDING_NEAREST(
+ one, LIBC_NAMESPACE::coshbf16(-rounds_to_one_bound));
+ EXPECT_FP_EQ_ROUNDING_UPWARD(one_next_up,
+ LIBC_NAMESPACE::coshbf16(rounds_to_one_bound));
+
+ EXPECT_FP_EQ_ROUNDING_NEAREST(
+ one_next_up, LIBC_NAMESPACE::coshbf16(rounds_above_one_bound));
+ EXPECT_FP_EQ_ROUNDING_NEAREST(
+ one_next_up, LIBC_NAMESPACE::coshbf16(-rounds_above_one_bound));
}
+ // cosh(x) overflows bfloat16 for |x| >= 89.5. cosh(x) > 0 for all x, so the
+ // overflow is always to +Inf, regardless of the sign of x.
void test_overflow() {
- EXPECT_FP_EQ_WITH_EXCEPTION(
- inf, LIBC_NAMESPACE::coshbf16(FPBits(uint16_t(0x42b3)).get_val()),
- FE_OVERFLOW);
+ bfloat16 overflow_bound = FPBits(uint16_t(0x42B3)).get_val(); // 0x1.66p6
+
+ EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::coshbf16(overflow_bound),
+ FE_OVERFLOW);
EXPECT_MATH_ERRNO(ERANGE);
- EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::coshbf16(max_normal),
+ EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::coshbf16(-overflow_bound),
FE_OVERFLOW);
EXPECT_MATH_ERRNO(ERANGE);
- // cosh is even: large negative inputs overflow to +inf as well.
- EXPECT_FP_EQ_WITH_EXCEPTION(
- inf, LIBC_NAMESPACE::coshbf16(FPBits(uint16_t(0xc2b3)).get_val()),
- FE_OVERFLOW);
+ EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::coshbf16(max_normal),
+ FE_OVERFLOW);
EXPECT_MATH_ERRNO(ERANGE);
EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::coshbf16(neg_max_normal),
>From bd984deed203d29a546a57b0e3c18d4804850675 Mon Sep 17 00:00:00 2001
From: pixl <planner.chisels.7j at icloud.com>
Date: Tue, 10 Nov 2026 20:48:14 +0530
Subject: [PATCH 13/15] [libc][math] Fix unused coshbf16 constant in
round-nearest-only builds
---
libc/src/__support/math/coshbf16.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libc/src/__support/math/coshbf16.h b/libc/src/__support/math/coshbf16.h
index e0cee0c61b45ab..45c44b11dbfc38 100644
--- a/libc/src/__support/math/coshbf16.h
+++ b/libc/src/__support/math/coshbf16.h
@@ -52,8 +52,7 @@ LIBC_INLINE bfloat16 coshbf16(bfloat16 x) {
// We bound the cases below by comparing the bits of |x| as an integer.
uint16_t x_abs_uint = x_bits.abs().uintval();
- constexpr uint16_t ONE_UINT = 0x3F80; // 0x1p0 = 1.0
- constexpr uint16_t ONE_NEXT_UP_UINT = 0x3F81; // 0x1.02p0 = 1.0078125
+ constexpr uint16_t ONE_UINT = 0x3F80; // 0x1p0 = 1.0
// For 0 < |x| <= 0.0879, cosh(x) = 1 + x^2/2 + ... lies strictly between 1
// and the next bfloat16 above 1, and is closer to 1. So the result is 1 in
@@ -65,6 +64,7 @@ LIBC_INLINE bfloat16 coshbf16(bfloat16 x) {
return FPBits(ONE_UINT).get_val();
#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+ constexpr uint16_t ONE_NEXT_UP_UINT = 0x3F81; // 0x1.02p0 = 1.0078125
if (fputil::fenv_is_round_up())
return FPBits(ONE_NEXT_UP_UINT).get_val();
#endif
>From af97f7af999ebc02125c32e821a1b661bb76a0f4 Mon Sep 17 00:00:00 2001
From: pixl <planner.chisels.7j at icloud.com>
Date: Tue, 10 Nov 2026 20:54:21 +0530
Subject: [PATCH 14/15] [libc][bazel] Drop coshbf16 smoke test deps already
added by math_test
---
.../llvm-project-overlay/libc/test/src/math/smoke/BUILD.bazel | 3 ---
1 file changed, 3 deletions(-)
diff --git a/utils/bazel/llvm-project-overlay/libc/test/src/math/smoke/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/src/math/smoke/BUILD.bazel
index 69ac643e81f355..71e4db03f40145 100644
--- a/utils/bazel/llvm-project-overlay/libc/test/src/math/smoke/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/test/src/math/smoke/BUILD.bazel
@@ -343,9 +343,6 @@ math_test(
name = "coshbf16",
deps = [
"//libc:__support_fputil_bfloat16",
- "//libc:__support_fputil_fp_bits",
- "//libc:hdr_errno_macros",
- "//libc:hdr_fenv_macros",
],
)
>From f9236ac13c2758ddf2dcac8400b576af1c382ccb Mon Sep 17 00:00:00 2001
From: pixl <planner.chisels.7j at icloud.com>
Date: Tue, 10 Nov 2026 21:24:16 +0530
Subject: [PATCH 15/15] [libc][math] Fold coshbf16 NaN check into the overflow
branch
---
libc/src/__support/math/coshbf16.h | 25 +++++++++++++------------
1 file changed, 13 insertions(+), 12 deletions(-)
diff --git a/libc/src/__support/math/coshbf16.h b/libc/src/__support/math/coshbf16.h
index 45c44b11dbfc38..e3eeef97b73caa 100644
--- a/libc/src/__support/math/coshbf16.h
+++ b/libc/src/__support/math/coshbf16.h
@@ -36,20 +36,10 @@ LIBC_INLINE bfloat16 coshbf16(bfloat16 x) {
using FPBits = fputil::FPBits<bfloat16>;
FPBits x_bits(x);
- // We first check for NaN
- if (LIBC_UNLIKELY(x_bits.is_nan())) {
- // If it is a signaling NaN, then we raise an FE_INVALID exception
- // and return a quiet NaN
- if (x_bits.is_signaling_nan()) {
- fputil::raise_except_if_required(FE_INVALID);
- return FPBits::quiet_nan().get_val();
- }
- // If not, just return the NaN
- return x;
- }
-
// cosh is an even function, cosh(-x) = cosh(x), so we only need |x|.
// We bound the cases below by comparing the bits of |x| as an integer.
+ // NaN and Inf have the largest bits of |x|, so they are handled together with
+ // the overflow case below.
uint16_t x_abs_uint = x_bits.abs().uintval();
constexpr uint16_t ONE_UINT = 0x3F80; // 0x1p0 = 1.0
@@ -75,6 +65,17 @@ LIBC_INLINE bfloat16 coshbf16(bfloat16 x) {
// overflow is always to +Inf, regardless of the sign of x.
constexpr uint16_t OVERFLOW_ABS_BOUND = 0x42B3; // 0x1.66p6 = 89.5
if (LIBC_UNLIKELY(x_abs_uint >= OVERFLOW_ABS_BOUND)) {
+ if (x_bits.is_nan()) {
+ // If it is a signaling NaN, then we raise an FE_INVALID exception
+ // and return a quiet NaN
+ if (x_bits.is_signaling_nan()) {
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+ // If not, just return the NaN
+ return x;
+ }
+
// cosh(+-Inf) = +Inf
if (x_bits.is_inf())
return FPBits::inf().get_val();
More information about the libc-commits
mailing list