[libc-commits] [libc] Ab 233205 (PR #235422)

via libc-commits libc-commits at lists.llvm.org
Sun Oct 25 04:02:55 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-nvptx

Author: albertbolt1

<details>
<summary>Changes</summary>



---
Full diff: https://github.com/llvm/llvm-project/pull/235422.diff


24 Files Affected:

- (modified) libc/config/baremetal/aarch64/entrypoints.txt (+1) 
- (modified) libc/config/baremetal/arm/entrypoints.txt (+1) 
- (modified) libc/config/baremetal/hexagon/entrypoints.txt (+1) 
- (modified) libc/config/baremetal/riscv/entrypoints.txt (+1) 
- (modified) libc/config/darwin/aarch64/entrypoints.txt (+1) 
- (modified) libc/config/darwin/x86_64/entrypoints.txt (+1) 
- (modified) libc/config/freebsd/x86_64/entrypoints.txt (+1) 
- (modified) libc/config/gpu/amdgpu/entrypoints.txt (+1) 
- (modified) libc/config/gpu/nvptx/entrypoints.txt (+1) 
- (modified) libc/config/linux/aarch64/entrypoints.txt (+1) 
- (modified) libc/config/linux/arm/entrypoints.txt (+1) 
- (modified) libc/config/linux/riscv/entrypoints.txt (+1) 
- (modified) libc/config/linux/x86_64/entrypoints.txt (+1) 
- (modified) libc/config/windows/entrypoints.txt (+1) 
- (modified) libc/docs/headers/math/index.rst (+1-1) 
- (modified) libc/src/__support/math/CMakeLists.txt (+12) 
- (added) libc/src/__support/math/rsqrtbf16.h (+30) 
- (modified) libc/src/math/CMakeLists.txt (+1) 
- (modified) libc/src/math/generic/CMakeLists.txt (+11) 
- (added) libc/src/math/generic/rsqrtbf16.cpp (+18) 
- (added) libc/src/math/rsqrtbf16.h (+21) 
- (modified) libc/test/src/math/smoke/CMakeLists.txt (+13) 
- (added) libc/test/src/math/smoke/RsqrtTest.h (+50) 
- (added) libc/test/src/math/smoke/rsqrtbf16_test.cpp (+14) 


``````````diff
diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index a85fa19a92fccf..9a91a6f9976233 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -877,6 +877,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   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 87fdcd04652303..4216125cf921b0 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -888,6 +888,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   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 cfee5f8240e60c..eeffe258127e9b 100644
--- a/libc/config/baremetal/hexagon/entrypoints.txt
+++ b/libc/config/baremetal/hexagon/entrypoints.txt
@@ -877,6 +877,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   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 19397087f3e92c..5df5b18432f536 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -880,6 +880,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   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 5600b82ad3410b..0b276c14f35aba 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -699,6 +699,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   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 e3fc845d2ed3af..b18206e83b6a4b 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -335,6 +335,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   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 8f6c178301c2f2..45b7941d36f9bd 100644
--- a/libc/config/freebsd/x86_64/entrypoints.txt
+++ b/libc/config/freebsd/x86_64/entrypoints.txt
@@ -631,6 +631,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   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 2eaef608e9a7ac..e75ff37f151286 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -748,6 +748,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   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 79d0c733ec3c01..0cd418b48c69ce 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -764,6 +764,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   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 b2c8d62a65f4e1..a3254d2c3b2cc5 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1112,6 +1112,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   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 023d298170c245..eaf5780afa3c50 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -713,6 +713,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   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 3bdc686c0e4ce4..ccf04178070044 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1186,6 +1186,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   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 23a588f65d7435..cfec263a971984 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1208,6 +1208,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   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 1712b2ad26fda4..ca664bd0bcb94c 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -425,6 +425,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.sqrtbf16
+  libc.src.math.rsqrtbf16
   libc.src.math.tanbf16
   libc.src.math.truncbf16
   libc.src.math.totalorderbf16
diff --git a/libc/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst
index 575c7bd1d1a10a..e691e12136c3e3 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -341,7 +341,7 @@ Higher Math Functions
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | rootn     |                  |                 |                        |                      |                        |                        | 7.12.7.8               | F.10.4.8                   |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| rsqrt     | |check|          |                 |                        | |check|              |                        |                        | 7.12.7.9               | F.10.4.9                   |
+| rsqrt     | |check|          |                 |                        | |check|              |                        | |check|                | 7.12.7.9               | F.10.4.9                   |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | sin       | |check|          | |check|         |                        | |check|              |                        |                        | 7.12.4.6               | F.10.1.6                   |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index baac96e76d5435..2cc9e39e01cff8 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -5735,6 +5735,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/__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 4f1dd95a6993da..68cf32c9bf21be 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -590,6 +590,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 17f586036396f1..d8b5fe243afd53 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -2757,6 +2757,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..f25a10343078fa
--- /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
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index a1f85df34e4c7d..e8c9aeec3603c8 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -3688,6 +3688,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);

``````````

</details>


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


More information about the libc-commits mailing list