[libc-commits] [libc] [libc][math] Adds rsqrtbf16 higher math function for bfloat16 type (PR #235422)
via libc-commits
libc-commits at lists.llvm.org
Thu Nov 26 10:12:09 PST 2026
https://github.com/albertbolt1 updated https://github.com/llvm/llvm-project/pull/235422
>From a91f0f06f0b26d3e639ff662e0ed58b2e10f8f95 Mon Sep 17 00:00:00 2001
From: albertbolt <albertboltinfinity at gmail.com>
Date: Sat, 24 Oct 2026 17:52:12 +0530
Subject: [PATCH 01/13] checkpoint 1, added the needed code, need to check and
run
---
libc/config/baremetal/aarch64/entrypoints.txt | 1 +
libc/config/baremetal/arm/entrypoints.txt | 1 +
libc/config/baremetal/hexagon/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/freebsd/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/src/__support/math/rsqrtbf16.h | 30 +++++++++++
libc/src/math/CMakeLists.txt | 1 +
libc/src/math/generic/CMakeLists.txt | 11 ++++
libc/src/math/generic/rsqrtbf16.cpp | 18 +++++++
libc/src/math/rsqrtbf16.h | 21 ++++++++
libc/test/src/math/smoke/CMakeLists.txt | 13 +++++
libc/test/src/math/smoke/RsqrtTest.h | 50 +++++++++++++++++++
libc/test/src/math/smoke/rsqrtbf16_test.cpp | 14 ++++++
22 files changed, 172 insertions(+)
create mode 100644 libc/src/__support/math/rsqrtbf16.h
create mode 100644 libc/src/math/generic/rsqrtbf16.cpp
create mode 100644 libc/src/math/rsqrtbf16.h
create mode 100644 libc/test/src/math/smoke/RsqrtTest.h
create mode 100644 libc/test/src/math/smoke/rsqrtbf16_test.cpp
diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index e3d5c8c0a68399..aaba2206c6e34a 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -905,6 +905,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.sinbf16
libc.src.math.sinhbf16
libc.src.math.sqrtbf16
+ libc.src.math.rsqrtbf16
libc.src.math.tanbf16
libc.src.math.truncbf16
libc.src.math.totalorderbf16
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index fc937dcc7670c0..6db703b1fc3f9e 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -916,6 +916,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.sinbf16
libc.src.math.sinhbf16
libc.src.math.sqrtbf16
+ libc.src.math.rsqrtbf16
libc.src.math.tanbf16
libc.src.math.truncbf16
libc.src.math.totalorderbf16
diff --git a/libc/config/baremetal/hexagon/entrypoints.txt b/libc/config/baremetal/hexagon/entrypoints.txt
index b83f4d11379c4d..93a4ffc3c3078e 100644
--- a/libc/config/baremetal/hexagon/entrypoints.txt
+++ b/libc/config/baremetal/hexagon/entrypoints.txt
@@ -905,6 +905,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.sinbf16
libc.src.math.sinhbf16
libc.src.math.sqrtbf16
+ libc.src.math.rsqrtbf16
libc.src.math.tanbf16
libc.src.math.truncbf16
libc.src.math.totalorderbf16
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index ae01bdda48a8ed..605d94a50aec28 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -908,6 +908,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.sinbf16
libc.src.math.sinhbf16
libc.src.math.sqrtbf16
+ libc.src.math.rsqrtbf16
libc.src.math.tanbf16
libc.src.math.truncbf16
libc.src.math.totalorderbf16
diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index b0fa44ccbd6df0..d8b66bb152faf0 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -726,6 +726,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.sinbf16
libc.src.math.sinhbf16
libc.src.math.sqrtbf16
+ libc.src.math.rsqrtbf16
libc.src.math.tanbf16
libc.src.math.truncbf16
libc.src.math.totalorderbf16
diff --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index a85ea90f123e79..78a085c6fc76db 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -371,6 +371,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.sinbf16
libc.src.math.sinhbf16
libc.src.math.sqrtbf16
+ libc.src.math.rsqrtbf16
libc.src.math.tanbf16
libc.src.math.truncbf16
libc.src.math.totalorderbf16
diff --git a/libc/config/freebsd/x86_64/entrypoints.txt b/libc/config/freebsd/x86_64/entrypoints.txt
index 862a421c473d36..6092c77b76d1b2 100644
--- a/libc/config/freebsd/x86_64/entrypoints.txt
+++ b/libc/config/freebsd/x86_64/entrypoints.txt
@@ -658,6 +658,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.sinbf16
libc.src.math.sinhbf16
libc.src.math.sqrtbf16
+ libc.src.math.rsqrtbf16
libc.src.math.tanbf16
libc.src.math.truncbf16
libc.src.math.totalorderbf16
diff --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt
index d2b1050abf66b6..4fa90b06873e2f 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -795,6 +795,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.sinbf16
libc.src.math.sinhbf16
libc.src.math.sqrtbf16
+ libc.src.math.rsqrtbf16
libc.src.math.tanbf16
libc.src.math.truncbf16
libc.src.math.totalorderbf16
diff --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index 8dba78c330a5f5..708db6462777c9 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -811,6 +811,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.sinbf16
libc.src.math.sinhbf16
libc.src.math.sqrtbf16
+ libc.src.math.rsqrtbf16
libc.src.math.tanbf16
libc.src.math.truncbf16
libc.src.math.totalorderbf16
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 99678f528cda26..a0330bd6a7b3ce 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1187,6 +1187,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.sinbf16
libc.src.math.sinhbf16
libc.src.math.sqrtbf16
+ libc.src.math.rsqrtbf16
libc.src.math.tanbf16
libc.src.math.truncbf16
libc.src.math.totalorderbf16
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 7a9e287839f9a7..477799f1e73988 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -790,6 +790,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.sinbf16
libc.src.math.sinhbf16
libc.src.math.sqrtbf16
+ libc.src.math.rsqrtbf16
libc.src.math.tanbf16
libc.src.math.truncbf16
libc.src.math.totalorderbf16
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index d1232ae7d11013..5eed64b707d52c 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1289,6 +1289,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.sinbf16
libc.src.math.sinhbf16
libc.src.math.sqrtbf16
+ libc.src.math.rsqrtbf16
libc.src.math.tanbf16
libc.src.math.truncbf16
libc.src.math.totalorderbf16
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 28d2d4881dbefd..c8156465765a81 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1289,6 +1289,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.sinbf16
libc.src.math.sinhbf16
libc.src.math.sqrtbf16
+ libc.src.math.rsqrtbf16
libc.src.math.tanbf16
libc.src.math.truncbf16
libc.src.math.totalorderbf16
diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index 7046934c029380..805ea646d4683b 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -472,6 +472,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.sinbf16
libc.src.math.sinhbf16
libc.src.math.sqrtbf16
+ libc.src.math.rsqrtbf16
libc.src.math.tanbf16
libc.src.math.truncbf16
libc.src.math.totalorderbf16
diff --git a/libc/src/__support/math/rsqrtbf16.h b/libc/src/__support/math/rsqrtbf16.h
new file mode 100644
index 00000000000000..f7cbac8fe5fd2f
--- /dev/null
+++ b/libc/src/__support/math/rsqrtbf16.h
@@ -0,0 +1,30 @@
+//===-- Implementation header for sqrtbf16 ----------------------*- 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_RSQRTBF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_RSQRTBF16_H
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/FPUtil/sqrt.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/FPUtil/cast.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+LIBC_INLINE bfloat16 rsqrtbf16(bfloat16 x) {
+
+ float xf = fputil::cast<float>(x);
+ return fputil::cast<bfloat16>(1.0f/fputil::sqrt<float>(xf));
+ }
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_RSQRTBF16_H
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index aa2c720f47df16..72928bc85b5be8 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -612,6 +612,7 @@ add_math_entrypoint_object(sqrtl)
add_math_entrypoint_object(sqrtf16)
add_math_entrypoint_object(sqrtf128)
add_math_entrypoint_object(sqrtbf16)
+add_math_entrypoint_object(rsqrtbf16)
add_math_entrypoint_object(tan)
add_math_entrypoint_object(tanbf16)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 0c495701e97886..65a145717ef359 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -2911,6 +2911,17 @@ add_entrypoint_object(
libc.src.__support.math.sqrtbf16
)
+add_entrypoint_object(
+ rsqrtbf16
+ SRCS
+ rsqrtbf16.cpp
+ HDRS
+ ../rsqrtbf16.h
+ DEPENDS
+ libc.src.__support.math.rsqrtbf16
+)
+
+
add_entrypoint_object(
remquof
SRCS
diff --git a/libc/src/math/generic/rsqrtbf16.cpp b/libc/src/math/generic/rsqrtbf16.cpp
new file mode 100644
index 00000000000000..14675843a226c7
--- /dev/null
+++ b/libc/src/math/generic/rsqrtbf16.cpp
@@ -0,0 +1,18 @@
+//===-- Implementation of sqrtbf16 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/rsqrtbf16.h"
+#include "src/__support/math/rsqrtbf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, rsqrtbf16, (bfloat16 x)) {
+ return math::rsqrtbf16(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/rsqrtbf16.h b/libc/src/math/rsqrtbf16.h
new file mode 100644
index 00000000000000..4efa6a91cc1c5f
--- /dev/null
+++ b/libc/src/math/rsqrtbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for rsqrtbf16 ----------------------*- 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_RSQRTBF16_H
+#define LLVM_LIBC_SRC_MATH_RSQRTBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 rsqrtbf16(bfloat16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_RSQRTBF16_H
\ No newline at end of file
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 754430326df0b8..710ed4de13ab1b 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -3952,6 +3952,19 @@ add_fp_unittest(
libc.src.__support.FPUtil.bfloat16
)
+add_fp_unittest(
+ rsqrtbf16_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ rsqrtbf16_test.cpp
+ HDRS
+ SqrtTest.h
+ DEPENDS
+ libc.src.math.rsqrtbf16
+ libc.src.__support.FPUtil.bfloat16
+)
+
add_fp_unittest(
generic_sqrtf_test
SUITE
diff --git a/libc/test/src/math/smoke/RsqrtTest.h b/libc/test/src/math/smoke/RsqrtTest.h
new file mode 100644
index 00000000000000..e8dda8a603931c
--- /dev/null
+++ b/libc/test/src/math/smoke/RsqrtTest.h
@@ -0,0 +1,50 @@
+//===-- Utility class to test rsqrt[f|l] ------------------------*- 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_TEST_SRC_MATH_SMOKE_RSQRTTEST_H
+#define LLVM_LIBC_TEST_SRC_MATH_SMOKE_RSQRTTEST_H
+
+#include "test/UnitTest/FEnvSafeTest.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+template <typename OutType, typename InType>
+class RsqrtTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+
+ DECLARE_SPECIAL_CONSTANTS(OutType)
+
+ struct InConstants {
+ DECLARE_SPECIAL_CONSTANTS(InType)
+ };
+
+ InConstants in;
+
+public:
+ typedef OutType (*RsqrtFunc)(InType);
+
+ void test_special_numbers(RsqrtFunc func) {
+ ASSERT_FP_EQ(in.aNaN, func(in.aNaN));
+ ASSERT_FP_EQ(inf, func(in.zero));
+ ASSERT_FP_EQ(neg_inf, func(in.neg_zero));
+ ASSERT_FP_EQ(zero, func(in.inf));
+ ASSERT_FP_EQ(aNaN, func(in.neg_inf));
+ ASSERT_FP_EQ(aNaN, func(InType(-1.0)));
+ ASSERT_FP_EQ(OutType(1.0), func(InType(1.0)));
+ ASSERT_FP_EQ(OutType(0.5), func(InType(4.0)));
+ ASSERT_FP_EQ(OutType(0.70710677), func(InType(2.0)));
+ }
+};
+
+#define LIST_RSQRT_TESTS(Name, T, func) \
+ using LlvmLibc##Name##Test = RsqrtTest<T, T>; \
+ TEST_F(LlvmLibc##Name##Test, SpecialNumbers) { \
+ test_special_numbers(&func); \
+ } \
+ static_assert(true, "Require semicolon.")
+
+#endif // LLVM_LIBC_TEST_SRC_MATH_SMOKE_RSQRTTEST_H
\ No newline at end of file
diff --git a/libc/test/src/math/smoke/rsqrtbf16_test.cpp b/libc/test/src/math/smoke/rsqrtbf16_test.cpp
new file mode 100644
index 00000000000000..7912ccf7682a0b
--- /dev/null
+++ b/libc/test/src/math/smoke/rsqrtbf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for sqrtbf16 --------------------------------------------===//
+//
+// 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 "RsqrtTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/rsqrtbf16.h"
+
+LIST_RSQRT_TESTS(Rsqrtbf16, bfloat16, LIBC_NAMESPACE::rsqrtbf16);
>From d93e989432f0f9f2969398f3418ded4b4d3b3755 Mon Sep 17 00:00:00 2001
From: albertbolt <albertboltinfinity at gmail.com>
Date: Sun, 25 Oct 2026 09:31:06 +0530
Subject: [PATCH 02/13] checkpoint 1, added the needed code, need to check and
run
---
libc/src/__support/math/CMakeLists.txt | 12 ++++++++++++
libc/src/math/rsqrtbf16.h | 2 +-
2 files changed, 13 insertions(+), 1 deletion(-)
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 9a312dd0fded88..171bfe823ee3d6 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -6244,6 +6244,18 @@ add_header_library(
libc.src.__support.macros.config
)
+add_header_library(
+ rsqrtbf16
+ HDRS
+ rsqrtbf16.h
+ DEPENDS
+ libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.FPUtil.sqrt
+ libc.src.__support.common
+ libc.src.__support.macros.config
+ libc.src.__support.FPUtil.cast.h
+)
+
add_header_library(
sqrtf
HDRS
diff --git a/libc/src/math/rsqrtbf16.h b/libc/src/math/rsqrtbf16.h
index 4efa6a91cc1c5f..f25a10343078fa 100644
--- a/libc/src/math/rsqrtbf16.h
+++ b/libc/src/math/rsqrtbf16.h
@@ -18,4 +18,4 @@ bfloat16 rsqrtbf16(bfloat16 x);
} // namespace LIBC_NAMESPACE_DECL
-#endif // LLVM_LIBC_SRC_MATH_RSQRTBF16_H
\ No newline at end of file
+#endif // LLVM_LIBC_SRC_MATH_RSQRTBF16_H
>From c1bc8e60939e45073b26e0268a63f035b554f4b6 Mon Sep 17 00:00:00 2001
From: albertbolt <albertboltinfinity at gmail.com>
Date: Sun, 25 Oct 2026 17:21:48 +0530
Subject: [PATCH 03/13] cleaning up
---
libc/src/__support/math/rsqrtbf16.h | 10 +++++-----
libc/src/math/rsqrtbf16.h | 3 ++-
2 files changed, 7 insertions(+), 6 deletions(-)
diff --git a/libc/src/__support/math/rsqrtbf16.h b/libc/src/__support/math/rsqrtbf16.h
index f7cbac8fe5fd2f..1b5e581588de10 100644
--- a/libc/src/__support/math/rsqrtbf16.h
+++ b/libc/src/__support/math/rsqrtbf16.h
@@ -10,19 +10,19 @@
#define LLVM_LIBC_SRC___SUPPORT_MATH_RSQRTBF16_H
#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/FPUtil/cast.h"
#include "src/__support/FPUtil/sqrt.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
-#include "src/__support/FPUtil/cast.h"
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE bfloat16 rsqrtbf16(bfloat16 x) {
+LIBC_INLINE bfloat16 rsqrtbf16(bfloat16 x) {
- float xf = fputil::cast<float>(x);
- return fputil::cast<bfloat16>(1.0f/fputil::sqrt<float>(xf));
- }
+ float xf = fputil::cast<float>(x);
+ return fputil::cast<bfloat16>(1.0f / fputil::sqrt<float>(xf));
+}
} // namespace math
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/rsqrtbf16.h b/libc/src/math/rsqrtbf16.h
index f25a10343078fa..e6dfc28e5d549c 100644
--- a/libc/src/math/rsqrtbf16.h
+++ b/libc/src/math/rsqrtbf16.h
@@ -1,4 +1,5 @@
-//===-- Implementation header for rsqrtbf16 ----------------------*- C++ -*-===//
+//===-- Implementation header for rsqrtbf16 ----------------------*- C++
+//-*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
>From 6d15d146d13bcb3444afdc3805292c1fe3dc4154 Mon Sep 17 00:00:00 2001
From: albertbolt1 <albertboltinfinity at gmail.com>
Date: Sun, 25 Oct 2026 20:47:58 +0530
Subject: [PATCH 04/13] Update libc/src/__support/math/rsqrtbf16.h
Co-authored-by: Kazu Hirata <kazu at google.com>
---
libc/src/__support/math/rsqrtbf16.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/src/__support/math/rsqrtbf16.h b/libc/src/__support/math/rsqrtbf16.h
index 1b5e581588de10..12d32063015086 100644
--- a/libc/src/__support/math/rsqrtbf16.h
+++ b/libc/src/__support/math/rsqrtbf16.h
@@ -1,4 +1,4 @@
-//===-- Implementation header for sqrtbf16 ----------------------*- C++ -*-===//
+//===-- Implementation header for rsqrtbf16 ---------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
>From 2fa7523436a4d45c97cb49630f7c584bb6fe759f Mon Sep 17 00:00:00 2001
From: albertbolt1 <albertboltinfinity at gmail.com>
Date: Sun, 25 Oct 2026 20:48:09 +0530
Subject: [PATCH 05/13] Update libc/src/math/generic/rsqrtbf16.cpp
Co-authored-by: Kazu Hirata <kazu at google.com>
---
libc/src/math/generic/rsqrtbf16.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/src/math/generic/rsqrtbf16.cpp b/libc/src/math/generic/rsqrtbf16.cpp
index 14675843a226c7..163188cede798b 100644
--- a/libc/src/math/generic/rsqrtbf16.cpp
+++ b/libc/src/math/generic/rsqrtbf16.cpp
@@ -1,4 +1,4 @@
-//===-- Implementation of sqrtbf16 function -------------------------------===//
+//===-- Implementation of rsqrtbf16 function ------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
>From 63099b86552a6b02daa49e72e9212653551f1736 Mon Sep 17 00:00:00 2001
From: albertbolt1 <albertboltinfinity at gmail.com>
Date: Sun, 25 Oct 2026 20:48:17 +0530
Subject: [PATCH 06/13] Update libc/src/math/rsqrtbf16.h
Co-authored-by: Kazu Hirata <kazu at google.com>
---
libc/src/math/rsqrtbf16.h | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/libc/src/math/rsqrtbf16.h b/libc/src/math/rsqrtbf16.h
index e6dfc28e5d549c..8bd5a02704f921 100644
--- a/libc/src/math/rsqrtbf16.h
+++ b/libc/src/math/rsqrtbf16.h
@@ -1,5 +1,4 @@
-//===-- Implementation header for rsqrtbf16 ----------------------*- C++
-//-*-===//
+//===-- Implementation header for rsqrtbf16 ---------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
>From 5ac7c57bdb89fdba0592c14237cd0f1b2d6aa8a1 Mon Sep 17 00:00:00 2001
From: albertbolt1 <albertboltinfinity at gmail.com>
Date: Sun, 25 Oct 2026 20:48:25 +0530
Subject: [PATCH 07/13] Update libc/test/src/math/smoke/rsqrtbf16_test.cpp
Co-authored-by: Kazu Hirata <kazu at google.com>
---
libc/test/src/math/smoke/rsqrtbf16_test.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/test/src/math/smoke/rsqrtbf16_test.cpp b/libc/test/src/math/smoke/rsqrtbf16_test.cpp
index 7912ccf7682a0b..31c94ca72c3027 100644
--- a/libc/test/src/math/smoke/rsqrtbf16_test.cpp
+++ b/libc/test/src/math/smoke/rsqrtbf16_test.cpp
@@ -1,4 +1,4 @@
-//===-- Unittests for sqrtbf16 --------------------------------------------===//
+//===-- Unittests for rsqrtbf16 -------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
>From 005d47548d413d218083f04edf3b4c4fe2139e23 Mon Sep 17 00:00:00 2001
From: albertbolt1 <albertboltinfinity at gmail.com>
Date: Sun, 25 Oct 2026 20:50:51 +0530
Subject: [PATCH 08/13] Update libc/src/__support/math/CMakeLists.txt
Co-authored-by: Kazu Hirata <kazu at google.com>
---
libc/src/__support/math/CMakeLists.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 171bfe823ee3d6..bb7fb20a8b17c3 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -6253,7 +6253,7 @@ add_header_library(
libc.src.__support.FPUtil.sqrt
libc.src.__support.common
libc.src.__support.macros.config
- libc.src.__support.FPUtil.cast.h
+ libc.src.__support.FPUtil.cast
)
add_header_library(
>From bd76e687408845f41f41941663e35377b11f4198 Mon Sep 17 00:00:00 2001
From: albertbolt1 <albertboltinfinity at gmail.com>
Date: Sun, 25 Oct 2026 20:51:17 +0530
Subject: [PATCH 09/13] Update libc/test/src/math/smoke/CMakeLists.txt
Co-authored-by: Kazu Hirata <kazu at google.com>
---
libc/test/src/math/smoke/CMakeLists.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 710ed4de13ab1b..1346f99b471d69 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -3959,7 +3959,7 @@ add_fp_unittest(
SRCS
rsqrtbf16_test.cpp
HDRS
- SqrtTest.h
+ RsqrtTest.h
DEPENDS
libc.src.math.rsqrtbf16
libc.src.__support.FPUtil.bfloat16
>From c331b128a954b8b6d1a8cbf80328b040829077b9 Mon Sep 17 00:00:00 2001
From: albertbolt <albertboltinfinity at gmail.com>
Date: Wed, 11 Nov 2026 07:23:00 +0530
Subject: [PATCH 10/13] fix: added new line to format for rsqrttest
---
libc/test/src/math/smoke/RsqrtTest.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/test/src/math/smoke/RsqrtTest.h b/libc/test/src/math/smoke/RsqrtTest.h
index e8dda8a603931c..ea7b123aa6a608 100644
--- a/libc/test/src/math/smoke/RsqrtTest.h
+++ b/libc/test/src/math/smoke/RsqrtTest.h
@@ -47,4 +47,4 @@ class RsqrtTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
} \
static_assert(true, "Require semicolon.")
-#endif // LLVM_LIBC_TEST_SRC_MATH_SMOKE_RSQRTTEST_H
\ No newline at end of file
+#endif // LLVM_LIBC_TEST_SRC_MATH_SMOKE_RSQRTTEST_H
>From c4e307198014a6789d1444b8a605b92b1b4d84dc Mon Sep 17 00:00:00 2001
From: albertbolt <albertboltinfinity at gmail.com>
Date: Wed, 11 Nov 2026 07:27:54 +0530
Subject: [PATCH 11/13] fix: formatting differences sorted
---
libc/src/math/generic/CMakeLists.txt | 1 -
1 file changed, 1 deletion(-)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 65a145717ef359..f84701c6e92794 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -2921,7 +2921,6 @@ add_entrypoint_object(
libc.src.__support.math.rsqrtbf16
)
-
add_entrypoint_object(
remquof
SRCS
>From 9db3a091fdf6c1c1c8d050201cc15b426dfc7026 Mon Sep 17 00:00:00 2001
From: albertbolt <albertboltinfinity at gmail.com>
Date: Sat, 21 Nov 2026 00:34:48 +0530
Subject: [PATCH 12/13] changed text in the headers
---
libc/src/__support/math/rsqrtbf16.h | 7 ++++++-
libc/src/math/generic/rsqrtbf16.cpp | 7 ++++++-
libc/src/math/rsqrtbf16.h | 7 ++++++-
3 files changed, 18 insertions(+), 3 deletions(-)
diff --git a/libc/src/__support/math/rsqrtbf16.h b/libc/src/__support/math/rsqrtbf16.h
index 12d32063015086..4119b3464fc041 100644
--- a/libc/src/__support/math/rsqrtbf16.h
+++ b/libc/src/__support/math/rsqrtbf16.h
@@ -1,10 +1,15 @@
-//===-- Implementation header for rsqrtbf16 ---------------------*- 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
//
//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file contains the implementation of rsqrtbf16.
+///
+//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_RSQRTBF16_H
#define LLVM_LIBC_SRC___SUPPORT_MATH_RSQRTBF16_H
diff --git a/libc/src/math/generic/rsqrtbf16.cpp b/libc/src/math/generic/rsqrtbf16.cpp
index 163188cede798b..5c83f77dc149ff 100644
--- a/libc/src/math/generic/rsqrtbf16.cpp
+++ b/libc/src/math/generic/rsqrtbf16.cpp
@@ -1,10 +1,15 @@
-//===-- Implementation of rsqrtbf16 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
//
//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file implements the rsqrtbf16 entrypoint.
+///
+//===----------------------------------------------------------------------===//
#include "src/math/rsqrtbf16.h"
#include "src/__support/math/rsqrtbf16.h"
diff --git a/libc/src/math/rsqrtbf16.h b/libc/src/math/rsqrtbf16.h
index 8bd5a02704f921..d4b2249fcedd1c 100644
--- a/libc/src/math/rsqrtbf16.h
+++ b/libc/src/math/rsqrtbf16.h
@@ -1,10 +1,15 @@
-//===-- Implementation header for rsqrtbf16 ---------------------*- 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
//
//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file declares the rsqrtbf16 entrypoint.
+///
+//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_SRC_MATH_RSQRTBF16_H
#define LLVM_LIBC_SRC_MATH_RSQRTBF16_H
>From 3922875cd702557116c806317993b2945709ed2e Mon Sep 17 00:00:00 2001
From: albertbolt <albertboltinfinity at gmail.com>
Date: Thu, 26 Nov 2026 01:26:15 +0530
Subject: [PATCH 13/13] added code for special scenarios and mpfr test added
---
libc/src/__support/math/CMakeLists.txt | 7 +++-
libc/src/__support/math/rsqrtbf16.h | 35 ++++++++++++++++++++
libc/test/src/math/CMakeLists.txt | 12 +++++++
libc/test/src/math/rsqrtbf16_test.cpp | 44 ++++++++++++++++++++++++++
libc/test/src/math/smoke/RsqrtTest.h | 32 ++++++++-----------
5 files changed, 110 insertions(+), 20 deletions(-)
create mode 100644 libc/test/src/math/rsqrtbf16_test.cpp
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index bb7fb20a8b17c3..d29eac70aed5d3 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -6249,11 +6249,16 @@ add_header_library(
HDRS
rsqrtbf16.h
DEPENDS
+ libc.hdr.errno_macros
+ libc.hdr.fenv_macros
libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.FPUtil.cast
+ libc.src.__support.FPUtil.fenv_impl
+ libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.sqrt
libc.src.__support.common
libc.src.__support.macros.config
- libc.src.__support.FPUtil.cast
+ libc.src.__support.macros.optimization
)
add_header_library(
diff --git a/libc/src/__support/math/rsqrtbf16.h b/libc/src/__support/math/rsqrtbf16.h
index 4119b3464fc041..3c454f0869d2cf 100644
--- a/libc/src/__support/math/rsqrtbf16.h
+++ b/libc/src/__support/math/rsqrtbf16.h
@@ -14,16 +14,51 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_RSQRTBF16_H
#define LLVM_LIBC_SRC___SUPPORT_MATH_RSQRTBF16_H
+#include "hdr/errno_macros.h"
+#include "hdr/fenv_macros.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/sqrt.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"
namespace LIBC_NAMESPACE_DECL {
namespace math {
LIBC_INLINE bfloat16 rsqrtbf16(bfloat16 x) {
+ using FPBits = fputil::FPBits<bfloat16>;
+ FPBits bits(x);
+
+ if (LIBC_UNLIKELY(bits.is_zero() || bits.is_inf_or_nan() || bits.is_neg())) {
+ // x is NaN
+ if (bits.is_nan()) {
+ if (bits.is_signaling_nan()) {
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+ return x;
+ }
+
+ // |x| = 0
+ if (bits.is_zero()) {
+ fputil::raise_except_if_required(FE_DIVBYZERO);
+ fputil::set_errno_if_required(ERANGE);
+ return FPBits::inf(bits.sign()).get_val();
+ }
+
+ // -inf <= x < 0
+ if (bits.is_neg()) {
+ fputil::raise_except_if_required(FE_INVALID);
+ fputil::set_errno_if_required(EDOM);
+ return FPBits::quiet_nan().get_val();
+ }
+
+ // x = +inf
+ return FPBits::zero().get_val();
+ }
float xf = fputil::cast<float>(x);
return fputil::cast<bfloat16>(1.0f / fputil::sqrt<float>(xf));
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 2a7ef8e0e186d8..e193b34b13d8bd 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -2094,6 +2094,18 @@ add_fp_unittest(
libc.src.math.rsqrtf16
)
+add_fp_unittest(
+ rsqrtbf16_test
+ NEED_MPFR
+ SUITE
+ libc-math-tests
+ SRCS
+ rsqrtbf16_test.cpp
+ DEPENDS
+ libc.src.math.rsqrtbf16
+ libc.src.__support.FPUtil.bfloat16
+)
+
add_fp_unittest(
sqrtf16_test
NEED_MPFR
diff --git a/libc/test/src/math/rsqrtbf16_test.cpp b/libc/test/src/math/rsqrtbf16_test.cpp
new file mode 100644
index 00000000000000..732a7b35c74ebb
--- /dev/null
+++ b/libc/test/src/math/rsqrtbf16_test.cpp
@@ -0,0 +1,44 @@
+//===-- Exhaustive test for rsqrtbf16 -------------------------------------===//
+//
+// 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/rsqrtbf16.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+using LlvmLibcRsqrtbf16Test = 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)
+// rsqrt(-0.0) is -inf, not the same for mpfr.
+static constexpr uint16_t NEG_START = 0x8001U;
+static constexpr uint16_t NEG_STOP = 0xff80U;
+
+TEST_F(LlvmLibcRsqrtbf16Test, PositiveRange) {
+ for (uint16_t v = POS_START; v <= POS_STOP; ++v) {
+ bfloat16 x = FPBits(v).get_val();
+
+ EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Rsqrt, x,
+ LIBC_NAMESPACE::rsqrtbf16(x), 0.5);
+ }
+}
+
+TEST_F(LlvmLibcRsqrtbf16Test, NegativeRange) {
+ for (uint16_t v = NEG_START; v <= NEG_STOP; ++v) {
+ bfloat16 x = FPBits(v).get_val();
+
+ EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Rsqrt, x,
+ LIBC_NAMESPACE::rsqrtbf16(x), 0.5);
+ }
+}
diff --git a/libc/test/src/math/smoke/RsqrtTest.h b/libc/test/src/math/smoke/RsqrtTest.h
index ea7b123aa6a608..a612191eab5785 100644
--- a/libc/test/src/math/smoke/RsqrtTest.h
+++ b/libc/test/src/math/smoke/RsqrtTest.h
@@ -13,35 +13,29 @@
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"
-template <typename OutType, typename InType>
+template <typename T>
class RsqrtTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
- DECLARE_SPECIAL_CONSTANTS(OutType)
-
- struct InConstants {
- DECLARE_SPECIAL_CONSTANTS(InType)
- };
-
- InConstants in;
+ DECLARE_SPECIAL_CONSTANTS(T)
public:
- typedef OutType (*RsqrtFunc)(InType);
+ typedef T (*RsqrtFunc)(T);
void test_special_numbers(RsqrtFunc func) {
- ASSERT_FP_EQ(in.aNaN, func(in.aNaN));
- ASSERT_FP_EQ(inf, func(in.zero));
- ASSERT_FP_EQ(neg_inf, func(in.neg_zero));
- ASSERT_FP_EQ(zero, func(in.inf));
- ASSERT_FP_EQ(aNaN, func(in.neg_inf));
- ASSERT_FP_EQ(aNaN, func(InType(-1.0)));
- ASSERT_FP_EQ(OutType(1.0), func(InType(1.0)));
- ASSERT_FP_EQ(OutType(0.5), func(InType(4.0)));
- ASSERT_FP_EQ(OutType(0.70710677), func(InType(2.0)));
+ ASSERT_FP_EQ(aNaN, func(aNaN));
+ ASSERT_FP_EQ(inf, func(zero));
+ ASSERT_FP_EQ(neg_inf, func(neg_zero));
+ ASSERT_FP_EQ(zero, func(inf));
+ ASSERT_FP_EQ(aNaN, func(neg_inf));
+ ASSERT_FP_EQ(aNaN, func(T(-1.0)));
+ ASSERT_FP_EQ(T(1.0), func(T(1.0)));
+ ASSERT_FP_EQ(T(0.5), func(T(4.0)));
+ ASSERT_FP_EQ(T(0.70710677), func(T(2.0)));
}
};
#define LIST_RSQRT_TESTS(Name, T, func) \
- using LlvmLibc##Name##Test = RsqrtTest<T, T>; \
+ using LlvmLibc##Name##Test = RsqrtTest<T>; \
TEST_F(LlvmLibc##Name##Test, SpecialNumbers) { \
test_special_numbers(&func); \
} \
More information about the libc-commits
mailing list