[libc-commits] [libc] ad05482 - [libc][math][c23] Add hypotbf16 function (#183460)
via libc-commits
libc-commits at lists.llvm.org
Fri Mar 13 07:11:38 PDT 2026
Author: Zorojuro
Date: 2026-03-13T10:11:33-04:00
New Revision: ad054822eb3b47442c516ebefe6e6763f83a8987
URL: https://github.com/llvm/llvm-project/commit/ad054822eb3b47442c516ebefe6e6763f83a8987
DIFF: https://github.com/llvm/llvm-project/commit/ad054822eb3b47442c516ebefe6e6763f83a8987.diff
LOG: [libc][math][c23] Add hypotbf16 function (#183460)
This PR intends to add hypotbf16 higher math function for BFloat16 type
along with the tests.
Added:
libc/shared/math/hypotbf16.h
libc/src/__support/math/hypotbf16.h
libc/src/math/generic/hypotbf16.cpp
libc/src/math/hypotbf16.h
libc/test/src/math/exhaustive/hypotbf16_test.cpp
libc/test/src/math/hypotbf16_test.cpp
libc/test/src/math/smoke/hypotbf16_test.cpp
Modified:
libc/config/baremetal/aarch64/entrypoints.txt
libc/config/baremetal/arm/entrypoints.txt
libc/config/baremetal/riscv/entrypoints.txt
libc/config/darwin/aarch64/entrypoints.txt
libc/config/darwin/x86_64/entrypoints.txt
libc/config/gpu/amdgpu/entrypoints.txt
libc/config/gpu/nvptx/entrypoints.txt
libc/config/linux/aarch64/entrypoints.txt
libc/config/linux/arm/entrypoints.txt
libc/config/linux/riscv/entrypoints.txt
libc/config/linux/x86_64/entrypoints.txt
libc/config/windows/entrypoints.txt
libc/docs/headers/math/index.rst
libc/shared/math.h
libc/src/__support/math/CMakeLists.txt
libc/src/math/CMakeLists.txt
libc/src/math/generic/CMakeLists.txt
libc/test/shared/CMakeLists.txt
libc/test/shared/shared_math_test.cpp
libc/test/src/math/CMakeLists.txt
libc/test/src/math/exhaustive/CMakeLists.txt
libc/test/src/math/smoke/CMakeLists.txt
libc/test/src/math/smoke/HypotTest.h
utils/bazel/llvm-project-overlay/libc/BUILD.bazel
Removed:
################################################################################
diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index 4c98f8037f5e4..eebdd2e6bf0b1 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.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
+ libc.src.math.hypotbf16
libc.src.math.ilogbbf16
libc.src.math.iscanonicalbf16
libc.src.math.issignalingbf16
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index 40e3f8b8000d1..c411959df8b78 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -829,6 +829,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
+ libc.src.math.hypotbf16
libc.src.math.ilogbbf16
libc.src.math.iscanonicalbf16
libc.src.math.issignalingbf16
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index e23159b7a85ce..13905eb254f58 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.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
+ libc.src.math.hypotbf16
libc.src.math.ilogbbf16
libc.src.math.iscanonicalbf16
libc.src.math.issignalingbf16
diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index 385c5bf13a4e3..556381ce3c1e8 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -640,6 +640,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
+ libc.src.math.hypotbf16
libc.src.math.ilogbbf16
libc.src.math.iscanonicalbf16
libc.src.math.issignalingbf16
diff --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index fcd874eb5ed9b..b0c247464c850 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -270,6 +270,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
+ libc.src.math.hypotbf16
libc.src.math.ilogbbf16
libc.src.math.iscanonicalbf16
libc.src.math.issignalingbf16
diff --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt
index 286f003e92967..f71b7991ff160 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -657,6 +657,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
+ libc.src.math.hypotbf16
libc.src.math.ilogbbf16
libc.src.math.iscanonicalbf16
libc.src.math.issignalingbf16
diff --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index 8307e6426a7ad..097c626358c7f 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -659,6 +659,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
+ libc.src.math.hypotbf16
libc.src.math.ilogbbf16
libc.src.math.iscanonicalbf16
libc.src.math.issignalingbf16
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 5c5d63e48bbe2..b91a736f6853b 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -897,6 +897,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
+ libc.src.math.hypotbf16
libc.src.math.ilogbbf16
libc.src.math.iscanonicalbf16
libc.src.math.issignalingbf16
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 3ce816b9727e1..cfcb57f73940d 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.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
+ libc.src.math.hypotbf16
libc.src.math.ilogbbf16
libc.src.math.iscanonicalbf16
libc.src.math.issignalingbf16
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 36f13a3bf156b..a906d602a7aae 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -918,6 +918,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
+ libc.src.math.hypotbf16
libc.src.math.ilogbbf16
libc.src.math.iscanonicalbf16
libc.src.math.issignalingbf16
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 51343ea03c072..202ee74e73293 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -968,6 +968,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
+ libc.src.math.hypotbf16
libc.src.math.ilogbbf16
libc.src.math.iscanonicalbf16
libc.src.math.issignalingbf16
diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index 1ff79f8f8e89b..f6cd9c9c6b414 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -354,6 +354,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
+ libc.src.math.hypotbf16
libc.src.math.ilogbbf16
libc.src.math.iscanonicalbf16
libc.src.math.issignalingbf16
diff --git a/libc/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst
index 296c2537adbd9..1f63eecda6831 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -315,7 +315,7 @@ Higher Math Functions
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
| fsqrt | N/A | |check| | |check| | N/A | |check|\* | | 7.12.14.6 | F.10.11 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| hypot | |check| | |check| | | |check| | | | 7.12.7.4 | F.10.4.4 |
+| hypot | |check| | |check| | | |check| | | |check| | 7.12.7.4 | F.10.4.4 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
| lgamma | | | | | | | 7.12.8.3 | F.10.5.3 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/shared/math.h b/libc/shared/math.h
index bd70a8335a96f..3d6e1c1796856 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -151,6 +151,7 @@
#include "math/getpayloadf128.h"
#include "math/getpayloadf16.h"
#include "math/getpayloadl.h"
+#include "math/hypotbf16.h"
#include "math/hypotf.h"
#include "math/hypotf16.h"
#include "math/ilogb.h"
diff --git a/libc/shared/math/hypotbf16.h b/libc/shared/math/hypotbf16.h
new file mode 100644
index 0000000000000..d134d84a304aa
--- /dev/null
+++ b/libc/shared/math/hypotbf16.h
@@ -0,0 +1,26 @@
+//===-- Shared hypotbf16 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_HYPOTBF16_H
+#define LLVM_LIBC_SHARED_MATH_HYPOTBF16_H
+
+#include "shared/libc_common.h"
+
+#include "src/__support/math/hypotbf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace shared {
+
+using math::hypotbf16;
+
+} // namespace shared
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_HYPOTBF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 07a204c67a365..ca80655b4bd0c 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1986,6 +1986,17 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ hypotbf16
+ HDRS
+ hypotbf16.h
+ DEPENDS
+ libc.src.__support.FPUtil.hypot
+ libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.common
+ libc.src.__support.macros.config
+)
+
add_header_library(
hypotf16
HDRS
diff --git a/libc/src/__support/math/hypotbf16.h b/libc/src/__support/math/hypotbf16.h
new file mode 100644
index 0000000000000..e04013e4e3d68
--- /dev/null
+++ b/libc/src/__support/math/hypotbf16.h
@@ -0,0 +1,29 @@
+//===-- Implementation header for hypotbf16 ---------------------*- 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_HYPOTBF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_HYPOTBF16_H
+
+#include "src/__support/FPUtil/Hypot.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE bfloat16 hypotbf16(bfloat16 x, bfloat16 y) {
+ return fputil::hypot<bfloat16>(x, y);
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_HYPOTBF16_H
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 23dc02304ba10..db13fcad47c48 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -347,6 +347,7 @@ add_math_entrypoint_object(getpayloadf128)
add_math_entrypoint_object(getpayloadbf16)
add_math_entrypoint_object(hypot)
+add_math_entrypoint_object(hypotbf16)
add_math_entrypoint_object(hypotf)
add_math_entrypoint_object(hypotf16)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 046ccbfd2d823..405276b0801f1 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3031,6 +3031,16 @@ add_entrypoint_object(
libc.src.__support.macros.config
)
+add_entrypoint_object(
+ hypotbf16
+ SRCS
+ hypotbf16.cpp
+ HDRS
+ ../hypotbf16.h
+ DEPENDS
+ libc.src.__support.math.hypotbf16
+)
+
add_entrypoint_object(
hypotf
SRCS
diff --git a/libc/src/math/generic/hypotbf16.cpp b/libc/src/math/generic/hypotbf16.cpp
new file mode 100644
index 0000000000000..0c1a3ed84021b
--- /dev/null
+++ b/libc/src/math/generic/hypotbf16.cpp
@@ -0,0 +1,18 @@
+//===-- Implementation of hypotbf16 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/hypotbf16.h"
+#include "src/__support/math/hypotbf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, hypotbf16, (bfloat16 x, bfloat16 y)) {
+ return math::hypotbf16(x, y);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/hypotbf16.h b/libc/src/math/hypotbf16.h
new file mode 100644
index 0000000000000..db1365d791dba
--- /dev/null
+++ b/libc/src/math/hypotbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for hypotbf16 ---------------------*- 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_HYPOTBF16_H
+#define LLVM_LIBC_SRC_MATH_HYPOTBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 hypotbf16(bfloat16 x, bfloat16 y);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_HYPOTBF16_H
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 73da792b75f45..7bda654fd24de 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -149,6 +149,7 @@ add_fp_unittest(
libc.src.__support.math.getpayloadf16
libc.src.__support.math.getpayloadl
libc.src.__support.math.hypotf
+ libc.src.__support.math.hypotbf16
libc.src.__support.math.hypotf16
libc.src.__support.math.ilogb
libc.src.__support.math.ilogbf
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index c2fa5f1eadd57..758d8e583b0b3 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -437,6 +437,9 @@ TEST(LlvmLibcSharedMathTest, AllBFloat16) {
EXPECT_FP_EQ(bfloat16(-1.0),
LIBC_NAMESPACE::shared::getpayloadbf16(&getpayloadbf16_x));
+ EXPECT_FP_EQ(bfloat16(5.0),
+ LIBC_NAMESPACE::shared::hypotbf16(bfloat16(4.0), bfloat16(3.0)));
+
bfloat16 setpayloadbf16_res = bfloat16(0.0);
EXPECT_EQ(0, LIBC_NAMESPACE::shared::setpayloadbf16(&setpayloadbf16_res,
bfloat16(0.0)));
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 4e71ed81fc4e6..2802be73639bf 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -1896,6 +1896,19 @@ add_fp_unittest(
libc.src.__support.FPUtil.fp_bits
)
+add_fp_unittest(
+ hypotbf16_test
+ NEED_MPFR
+ SUITE
+ libc-math-unittests
+ SRCS
+ hypotbf16_test.cpp
+ DEPENDS
+ libc.src.math.hypotbf16
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.FPUtil.bfloat16
+)
+
add_fp_unittest(
hypotf16_test
NEED_MPFR
diff --git a/libc/test/src/math/exhaustive/CMakeLists.txt b/libc/test/src/math/exhaustive/CMakeLists.txt
index afeb02469dcd1..1776e61b87e3c 100644
--- a/libc/test/src/math/exhaustive/CMakeLists.txt
+++ b/libc/test/src/math/exhaustive/CMakeLists.txt
@@ -376,6 +376,24 @@ add_fp_unittest(
-lpthread
)
+add_fp_unittest(
+ hypotbf16_test
+ NO_RUN_POSTBUILD
+ NEED_MPFR
+ SUITE
+ libc_math_exhaustive_tests
+ SRCS
+ hypotbf16_test.cpp
+ COMPILE_OPTIONS
+ ${libc_opt_high_flag}
+ DEPENDS
+ .exhaustive_test
+ libc.src.math.hypotbf16
+ libc.src.__support.FPUtil.bfloat16
+ LINK_LIBRARIES
+ -lpthread
+)
+
add_fp_unittest(
hypotf_test
NO_RUN_POSTBUILD
diff --git a/libc/test/src/math/exhaustive/hypotbf16_test.cpp b/libc/test/src/math/exhaustive/hypotbf16_test.cpp
new file mode 100644
index 0000000000000..2b41d3d7de70e
--- /dev/null
+++ b/libc/test/src/math/exhaustive/hypotbf16_test.cpp
@@ -0,0 +1,72 @@
+//===-- Exhaustive test for hypotbf16 -------------------------------------===//
+//
+// 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 "exhaustive_test.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/Hypot.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/hypotbf16.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+struct Hypotbf16Checker : public virtual LIBC_NAMESPACE::testing::Test {
+ using FloatType = bfloat16;
+ using FPBits = LIBC_NAMESPACE::fputil::FPBits<bfloat16>;
+ using StorageType = typename FPBits::StorageType;
+
+ uint64_t check(uint16_t x_start, uint16_t x_stop, uint16_t y_start,
+ uint16_t y_stop, mpfr::RoundingMode rounding) {
+ mpfr::ForceRoundingMode r(rounding);
+ if (!r.success)
+ return true;
+ uint16_t xbits = x_start;
+ uint64_t failed = 0;
+ do {
+ bfloat16 x = FPBits(xbits).get_val();
+ uint16_t ybits = y_start;
+ do {
+ bfloat16 y = FPBits(ybits).get_val();
+ mpfr::BinaryInput<bfloat16> input{x, y};
+ bool correct = TEST_MPFR_MATCH_ROUNDING_SILENTLY(
+ mpfr::Operation::Hypot, input, LIBC_NAMESPACE::hypotbf16(x, y), 0.5,
+ rounding);
+ failed += (!correct);
+ } while (ybits++ < y_stop);
+ } while (xbits++ < x_stop);
+ return failed;
+ }
+};
+
+using LlvmLibcHypotBf16ExhaustiveTest =
+ LlvmLibcExhaustiveMathTest<Hypotbf16Checker, 1 << 2>;
+
+// 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(LlvmLibcHypotBf16ExhaustiveTest, PositiveRange) {
+ test_full_range_all_roundings(POS_START, POS_STOP, POS_START, POS_STOP);
+}
+
+TEST_F(LlvmLibcHypotBf16ExhaustiveTest, NegativeRange) {
+ test_full_range_all_roundings(NEG_START, NEG_STOP, NEG_START, NEG_STOP);
+}
+
+TEST_F(LlvmLibcHypotBf16ExhaustiveTest, PositiveNegativeRange) {
+ test_full_range_all_roundings(POS_START, POS_STOP, NEG_START, NEG_STOP);
+}
+
+TEST_F(LlvmLibcHypotBf16ExhaustiveTest, NegativePositiveRange) {
+ test_full_range_all_roundings(NEG_START, NEG_STOP, POS_START, POS_STOP);
+}
diff --git a/libc/test/src/math/hypotbf16_test.cpp b/libc/test/src/math/hypotbf16_test.cpp
new file mode 100644
index 0000000000000..a757ff751ba77
--- /dev/null
+++ b/libc/test/src/math/hypotbf16_test.cpp
@@ -0,0 +1,22 @@
+//===-- Unittests tests for hypotbf16 ------------------------------------===//
+//
+// 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 "HypotTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/hypotbf16.h"
+
+using LlvmLibcHypotbf16Test = HypotTestTemplate<bfloat16>;
+
+TEST_F(LlvmLibcHypotbf16Test, SubnormalRange) {
+ test_subnormal_range(&LIBC_NAMESPACE::hypotbf16);
+}
+
+TEST_F(LlvmLibcHypotbf16Test, NormalRange) {
+ test_normal_range(&LIBC_NAMESPACE::hypotbf16);
+}
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 647626e69d7d4..d735058c1affc 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -3743,6 +3743,20 @@ add_fp_unittest(
libc.src.__support.macros.properties.architectures
)
+add_fp_unittest(
+ hypotbf16_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ hypotbf16_test.cpp
+ HDRS
+ HypotTest.h
+ DEPENDS
+ libc.src.math.hypotbf16
+ libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.FPUtil.fp_bits
+)
+
add_fp_unittest(
nanf_test
SUITE
diff --git a/libc/test/src/math/smoke/HypotTest.h b/libc/test/src/math/smoke/HypotTest.h
index 30d57a4fe2a26..800b5c41daadb 100644
--- a/libc/test/src/math/smoke/HypotTest.h
+++ b/libc/test/src/math/smoke/HypotTest.h
@@ -22,7 +22,10 @@ struct HypotTestTemplate : public LIBC_NAMESPACE::testing::Test {
void test_special_numbers(Func func) {
constexpr int N = 4;
// Pythagorean triples.
- constexpr T PYT[N][3] = {{3, 4, 5}, {5, 12, 13}, {8, 15, 17}, {7, 24, 25}};
+ constexpr T PYT[N][3] = {{T(3), T(4), T(5)},
+ {T(5), T(12), T(13)},
+ {T(8), T(15), T(17)},
+ {T(7), T(24), T(25)}};
#ifndef LIBC_TARGET_ARCH_IS_NVPTX
// TODO: Investigate why sNaN tests are failing on nVidia.
diff --git a/libc/test/src/math/smoke/hypotbf16_test.cpp b/libc/test/src/math/smoke/hypotbf16_test.cpp
new file mode 100644
index 0000000000000..c39971ecdd512
--- /dev/null
+++ b/libc/test/src/math/smoke/hypotbf16_test.cpp
@@ -0,0 +1,18 @@
+//===-- Unittests for hypotbf16 -------------------------------------------===//
+//
+// 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 "HypotTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/hypotbf16.h"
+
+using LlvmLibcHypotBf16Test = HypotTestTemplate<bfloat16>;
+
+TEST_F(LlvmLibcHypotBf16Test, SpecialNumbers) {
+ test_special_numbers(&LIBC_NAMESPACE::hypotbf16);
+}
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 82708e0d1ba2c..e9e4601c56f30 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -5505,6 +5505,17 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_math_hypotbf16",
+ hdrs = ["src/__support/math/hypotbf16.h"],
+ deps = [
+ ":__support_common",
+ ":__support_fputil_bfloat16",
+ ":__support_fputil_hypot",
+ ":__support_macros_config",
+ ],
+)
+
libc_support_library(
name = "__support_math_hypotf16",
hdrs = ["src/__support/math/hypotf16.h"],
@@ -7191,6 +7202,13 @@ libc_math_function(
],
)
+libc_math_function(
+ name = "hypotbf16",
+ additional_deps = [
+ ":__support_math_hypotbf16",
+ ],
+)
+
libc_math_function(
name = "hypotf16",
additional_deps = [
More information about the libc-commits
mailing list