[libc-commits] [libc] [llvm] [libc][math][c23] Add rsqrtf() function (PR #159615)

Anton Shepelev via libc-commits libc-commits at lists.llvm.org
Thu Sep 18 10:46:08 PDT 2025


https://github.com/amemov created https://github.com/llvm/llvm-project/pull/159615

Addresses #159614

**Changes:**
- Initial implementation of rsqrt for single precision float

**Some small unrelated style changes to this PR (that I missed in my rsqrtf16 PR):**
- Added extra - to the top comments to make it look nicer in libc/shared/math/rsqrtf16.h
- Put rsqrtf16 inside of libc/src/__support/math/CMakeLists.txt in sorted order
- Rearanged libc_math_function rsqrtf16 in Bazel to match alphabetical order

>From e84a1e6fe073929c6bc97bbb673ba90e7b9c79b4 Mon Sep 17 00:00:00 2001
From: Anton Shepelev <shepelev777 at gmail.com>
Date: Thu, 18 Sep 2025 10:42:53 -0700
Subject: [PATCH] - Initial implementation of rsqrt for single precision float

Some small unrelated changes to this PR:
- Added extra - to the top comments to make it look nicer in libc/shared/math/rsqrtf16.h
- Put rsqrtf16 inside of libc/src/__support/math/CMakeLists.txt in sorted order
- Rearanged libc_math_function rsqrtf16 in Bazel to match alphabetical order
---
 libc/config/linux/aarch64/entrypoints.txt     |  1 +
 libc/config/linux/riscv/entrypoints.txt       |  1 +
 libc/config/linux/x86_64/entrypoints.txt      |  1 +
 libc/docs/headers/math/index.rst              |  2 +-
 libc/include/math.yaml                        |  6 +++
 libc/shared/math.h                            |  2 +-
 libc/shared/math/rsqrtf.h                     | 23 ++++++++++
 libc/shared/math/rsqrtf16.h                   |  2 +-
 libc/src/__support/math/CMakeLists.txt        | 46 ++++++++++++-------
 libc/src/math/CMakeLists.txt                  |  1 +
 libc/src/math/generic/CMakeLists.txt          | 11 +++++
 libc/src/math/generic/rsqrtf.cpp              | 15 ++++++
 libc/src/math/rsqrtf.h                        | 20 ++++++++
 libc/test/shared/CMakeLists.txt               |  2 +-
 libc/test/shared/shared_math_test.cpp         |  1 +
 libc/test/src/math/CMakeLists.txt             | 11 +++++
 libc/test/src/math/rsqrtf_test.cpp            | 41 +++++++++++++++++
 libc/test/src/math/smoke/CMakeLists.txt       | 11 +++++
 libc/test/src/math/smoke/rsqrtf_test.cpp      | 42 +++++++++++++++++
 .../llvm-project-overlay/libc/BUILD.bazel     | 38 +++++++++++----
 20 files changed, 249 insertions(+), 28 deletions(-)
 create mode 100644 libc/shared/math/rsqrtf.h
 create mode 100644 libc/src/math/generic/rsqrtf.cpp
 create mode 100644 libc/src/math/rsqrtf.h
 create mode 100644 libc/test/src/math/rsqrtf_test.cpp
 create mode 100644 libc/test/src/math/smoke/rsqrtf_test.cpp

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 00c4b2ec0f828..9e04eedd16162 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -735,6 +735,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
     libc.src.math.rintf16
     libc.src.math.roundevenf16
     libc.src.math.roundf16
+    libc.src.math.rsqrtf
     libc.src.math.rsqrtf16
     libc.src.math.scalblnf16
     libc.src.math.scalbnf16
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 89e3653186d13..f5742faf5355e 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -749,6 +749,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
     libc.src.math.rintf16
     libc.src.math.roundevenf16
     libc.src.math.roundf16
+    libc.src.math.rsqrtf
     libc.src.math.rsqrtf16
     libc.src.math.scalblnf16
     libc.src.math.scalbnf16
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 0bb8a683c5b01..0b861c537df9e 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -784,6 +784,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
     libc.src.math.rintf16
     libc.src.math.roundevenf16
     libc.src.math.roundf16
+    libc.src.math.rsqrtf
     libc.src.math.rsqrtf16
     libc.src.math.scalblnf16
     libc.src.math.scalbnf16
diff --git a/libc/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst
index 7d5b341ba674a..392b419b2d949 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -343,7 +343,7 @@ Higher Math Functions
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | rootn     |                  |                 |                        |                      |                        |                        | 7.12.7.8               | F.10.4.8                   |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| rsqrt     |                  |                 |                        | |check|              |                        |                        | 7.12.7.9               | F.10.4.9                   |
+| rsqrt     | |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/include/math.yaml b/libc/include/math.yaml
index 6c800a0e2aa28..afd3ae33305c1 100644
--- a/libc/include/math.yaml
+++ b/libc/include/math.yaml
@@ -2349,6 +2349,12 @@ functions:
     return_type: long double
     arguments:
       - type: long double
+  - name: rsqrtf
+    standards:
+      - stdc
+    return_type: float
+    arguments:
+      - type: float
   - name: rsqrtf16
     standards:
       - stdc
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 4f20095912bf1..abcea786a28b5 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -52,7 +52,7 @@
 #include "math/ldexpf.h"
 #include "math/ldexpf128.h"
 #include "math/ldexpf16.h"
-
+#include "math/rsqrtf.h"
 #include "math/rsqrtf16.h"
 
 #endif // LLVM_LIBC_SHARED_MATH_H
diff --git a/libc/shared/math/rsqrtf.h b/libc/shared/math/rsqrtf.h
new file mode 100644
index 0000000000000..32d0a61f7c21b
--- /dev/null
+++ b/libc/shared/math/rsqrtf.h
@@ -0,0 +1,23 @@
+//===-- Shared rsqrtf 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_RSQRTF_H
+#define LLVM_LIBC_SHARED_MATH_RSQRTF_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/rsqrtf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::rsqrtf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_RSQRTF_H
diff --git a/libc/shared/math/rsqrtf16.h b/libc/shared/math/rsqrtf16.h
index 54c7499214636..8dbf065802f49 100644
--- a/libc/shared/math/rsqrtf16.h
+++ b/libc/shared/math/rsqrtf16.h
@@ -1,4 +1,4 @@
-//===-- Shared rsqrtf16 function -------------------------------*- C++ -*-===//
+//===-- Shared rsqrtf16 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.
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index ed5f314b0a9b5..26ad5a1d8901a 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -109,22 +109,6 @@ add_header_library(
     libc.src.__support.macros.properties.types
 )
 
-
-add_header_library(
-  rsqrtf16
-  HDRS
-    rsqrtf16.h
-  DEPENDS
-    libc.src.__support.FPUtil.cast
-    libc.src.__support.FPUtil.fenv_impl
-    libc.src.__support.FPUtil.fp_bits
-    libc.src.__support.FPUtil.multiply_add
-    libc.src.__support.FPUtil.polyeval
-    libc.src.__support.FPUtil.manipulation_functions
-    libc.src.__support.macros.optimization
-    libc.src.__support.macros.properties.types
-)
-
 add_header_library(
   asin_utils
   HDRS
@@ -775,6 +759,36 @@ add_header_library(
     libc.src.__support.common
 )
 
+add_header_library(
+  rsqrtf16
+  HDRS
+    rsqrtf16.h
+  DEPENDS
+    libc.src.__support.FPUtil.cast
+    libc.src.__support.FPUtil.fenv_impl
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.multiply_add
+    libc.src.__support.FPUtil.polyeval
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.macros.optimization
+    libc.src.__support.macros.properties.types
+)
+
+add_header_library(
+  rsqrtf
+  HDRS
+    rsqrtf.h
+  DEPENDS
+    libc.src.__support.FPUtil.cast
+    libc.src.__support.FPUtil.fenv_impl
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.multiply_add
+    libc.src.__support.FPUtil.polyeval
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.macros.optimization
+    libc.src.__support.macros.properties.types
+)
+
 add_header_library(
   sincos_eval
   HDRS
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index a6f400c873b7e..271c1c0ae1045 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -516,6 +516,7 @@ add_math_entrypoint_object(roundevenf16)
 add_math_entrypoint_object(roundevenf128)
 add_math_entrypoint_object(roundevenbf16)
 
+add_math_entrypoint_object(rsqrtf)
 add_math_entrypoint_object(rsqrtf16)
 
 add_math_entrypoint_object(scalbln)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index ca7baeccae01a..2d92d7342ec96 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -988,6 +988,17 @@ add_entrypoint_object(
     ROUND_OPT
 )
 
+add_entrypoint_object(
+  rsqrtf
+  SRCS
+    rsqrtf.cpp
+  HDRS
+    ../rsqrtf.h
+  DEPENDS
+    libc.src.__support.math.rsqrtf
+    libc.src.errno.errno
+)
+
 add_entrypoint_object(
   rsqrtf16
   SRCS
diff --git a/libc/src/math/generic/rsqrtf.cpp b/libc/src/math/generic/rsqrtf.cpp
new file mode 100644
index 0000000000000..1c37979b35ed5
--- /dev/null
+++ b/libc/src/math/generic/rsqrtf.cpp
@@ -0,0 +1,15 @@
+//===-- Single-precision rsqrt 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/rsqrtf.h"
+#include "src/__support/math/rsqrtf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(float, rsqrtf, (float x)) { return math::rsqrtf(x); }
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/rsqrtf.h b/libc/src/math/rsqrtf.h
new file mode 100644
index 0000000000000..bc559043be828
--- /dev/null
+++ b/libc/src/math/rsqrtf.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for rsqrtf ------------------------*- 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_RSQRTF_H
+#define LLVM_LIBC_SRC_MATH_RSQRTF_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+float rsqrtf(float x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_RSQRTF_H
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 495d6f0a81a4c..0fa89e3ae6b62 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -48,6 +48,6 @@ add_fp_unittest(
     libc.src.__support.math.ldexpf
     libc.src.__support.math.ldexpf128
     libc.src.__support.math.ldexpf16
+    libc.src.__support.math.rsqrtf
     libc.src.__support.math.rsqrtf16
-
 )
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index aa459f88c29f5..46bd112549cbd 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -67,6 +67,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat) {
 
   ASSERT_FP_EQ(float(8 << 5), LIBC_NAMESPACE::shared::ldexpf(8.0f, 5));
   ASSERT_FP_EQ(float(-1 * (8 << 5)), LIBC_NAMESPACE::shared::ldexpf(-8.0f, 5));
+  EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::rsqrtf(1.0f));
 }
 
 TEST(LlvmLibcSharedMathTest, AllDouble) {
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 9d644703a61ae..f728eb83ebcf4 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -1678,6 +1678,17 @@ add_fp_unittest(
     libc.src.math.sqrtl
 )
 
+add_fp_unittest(
+  rsqrtf_test
+  NEED_MPFR
+  SUITE
+    libc-math-unittests
+  SRCS
+    rsqrtf_test.cpp
+  DEPENDS
+    libc.src.math.rsqrtf
+)
+
 add_fp_unittest(
   rsqrtf16_test
   NEED_MPFR
diff --git a/libc/test/src/math/rsqrtf_test.cpp b/libc/test/src/math/rsqrtf_test.cpp
new file mode 100644
index 0000000000000..cedafb63a31de
--- /dev/null
+++ b/libc/test/src/math/rsqrtf_test.cpp
@@ -0,0 +1,41 @@
+//===-- Exhaustive test for rsqrtf ----------------------------------------===//
+//
+// 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/rsqrtf.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+using LlvmLibcRsqrtfTest = LIBC_NAMESPACE::testing::FPTest<float>;
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+// Range: [0, Inf]
+static constexpr uint32_t POS_START = 0x00000000u; 
+static constexpr uint32_t POS_STOP = 0x7F800000u;
+
+// Range: [-Inf, 0)
+// rsqrt(-0.0) is -inf, not the same for mpfr.
+static constexpr uint32_t NEG_START = 0x80000001u;
+static constexpr uint32_t NEG_STOP = 0xFF800000u;
+
+TEST_F(LlvmLibcRsqrtfTest, PositiveRange) {
+  for (uint32_t v = POS_START; v <= POS_STOP; ++v) {
+    float x = FPBits(v).get_val();
+    EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Rsqrt, x,
+                                   LIBC_NAMESPACE::rsqrtf(x), 0.5);
+  }
+}
+
+TEST_F(LlvmLibcRsqrtfTest, NegativeRange) {
+  for (uint32_t v = NEG_START; v <= NEG_STOP; ++v) {
+    float x = FPBits(v).get_val();
+    EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Rsqrt, x,
+                                   LIBC_NAMESPACE::rsqrtf(x), 0.5);
+  }
+}
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index eadd5f0970722..644fa90a412fa 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -3502,6 +3502,17 @@ add_fp_unittest(
     libc.src.math.sqrtl
 )
 
+add_fp_unittest(
+  rsqrtf_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    rsqrtf_test.cpp
+  DEPENDS
+    libc.src.math.rsqrtf
+    libc.hdr.errno_macros
+)
+
 add_fp_unittest(
   rsqrtf16_test
   SUITE
diff --git a/libc/test/src/math/smoke/rsqrtf_test.cpp b/libc/test/src/math/smoke/rsqrtf_test.cpp
new file mode 100644
index 0000000000000..9ffde79e5e9a7
--- /dev/null
+++ b/libc/test/src/math/smoke/rsqrtf_test.cpp
@@ -0,0 +1,42 @@
+//===-- Unittests for rsqrtf --------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception.
+//
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "src/math/rsqrtf.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcRsqrtfTest = LIBC_NAMESPACE::testing::FPTest<float>;
+
+TEST_F(LlvmLibcRsqrtfTest, SpecialNumbers) {
+  EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::rsqrtf(aNaN));
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_IS_NAN_WITH_EXCEPTION(LIBC_NAMESPACE::rsqrtf(sNaN), FE_INVALID);
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ(inf, LIBC_NAMESPACE::rsqrtf(zero));
+  EXPECT_MATH_ERRNO(ERANGE);
+
+  EXPECT_FP_EQ(neg_inf, LIBC_NAMESPACE::rsqrtf(neg_zero));
+  EXPECT_MATH_ERRNO(ERANGE);
+
+  EXPECT_FP_EQ(
+      1.0f,
+      LIBC_NAMESPACE::rsqrtf(1.0f));
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ(zero, LIBC_NAMESPACE::rsqrtf(inf));
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::rsqrtf(neg_inf));
+  EXPECT_MATH_ERRNO(EDOM);
+
+  EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::rsqrtf(-2.0f));
+  EXPECT_MATH_ERRNO(EDOM);
+}
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 921163a413067..87ad0926bfee6 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2296,6 +2296,20 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_rsqrtf",
+    hdrs = ["src/__support/math/rsqrtf.h"],
+    deps = [
+        ":__support_fputil_fenv_impl",
+        ":__support_fputil_fp_bits",
+        ":__support_fputil_manipulation_functions",
+        ":__support_fputil_multiply_add",
+        ":__support_fputil_polyeval",
+        ":__support_macros_optimization",
+        ":__support_macros_properties_types",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_rsqrtf16",
     hdrs = ["src/__support/math/rsqrtf16.h"],
@@ -3259,14 +3273,6 @@ libc_math_function(
     ],
 )
 
-libc_math_function(
-    name = "rsqrtf16",
-    additional_deps = [
-        ":__support_math_rsqrtf16",
-        ":errno",
-    ],
-)
-
 libc_math_function(
     name = "acoshf16",
     additional_deps = [
@@ -4497,6 +4503,22 @@ libc_math_function(name = "roundevenf128")
 
 libc_math_function(name = "roundevenf16")
 
+libc_math_function(
+    name = "rsqrtf",
+    additional_deps = [
+        ":__support_math_rsqrtf",
+        ":errno",
+    ],
+)
+
+libc_math_function(
+    name = "rsqrtf16",
+    additional_deps = [
+        ":__support_math_rsqrtf16",
+        ":errno",
+    ],
+)
+
 libc_math_function(name = "scalbln")
 
 libc_math_function(name = "scalblnf")



More information about the libc-commits mailing list