[libc-commits] [libc] [llvm] [libc][math] Refactor fsqrtf128 to Header Only. (PR #175686)

via libc-commits libc-commits at lists.llvm.org
Fri Jan 16 16:18:35 PST 2026


https://github.com/AnonMiraj updated https://github.com/llvm/llvm-project/pull/175686

>From 660619b1e920ea7f7a5ab8c79f2eebd9ae8f8128 Mon Sep 17 00:00:00 2001
From: anonmiraj <nabilmalek48 at gmail.com>
Date: Thu, 15 Jan 2026 16:16:56 +0200
Subject: [PATCH 1/2] [libc][math] Refactor fsqrtf128 to Header Only.

---
 libc/shared/math.h                            |  1 +
 libc/shared/math/fsqrtf128.h                  | 28 ++++++++++++++++
 libc/src/__support/math/CMakeLists.txt        | 10 ++++++
 libc/src/__support/math/fsqrtf128.h           | 33 +++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt          |  4 +--
 libc/src/math/generic/fsqrtf128.cpp           |  7 ++--
 libc/test/shared/CMakeLists.txt               |  1 +
 libc/test/shared/shared_math_test.cpp         |  1 +
 .../llvm-project-overlay/libc/BUILD.bazel     | 13 +++++++-
 9 files changed, 90 insertions(+), 8 deletions(-)
 create mode 100644 libc/shared/math/fsqrtf128.h
 create mode 100644 libc/src/__support/math/fsqrtf128.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 51fd4006feb4d..24b518543485c 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -62,6 +62,7 @@
 #include "math/frexpf128.h"
 #include "math/frexpf16.h"
 #include "math/fsqrt.h"
+#include "math/fsqrtf128.h"
 #include "math/hypotf.h"
 #include "math/ilogbf16.h"
 #include "math/ldexpf.h"
diff --git a/libc/shared/math/fsqrtf128.h b/libc/shared/math/fsqrtf128.h
new file mode 100644
index 0000000000000..22f39725695dc
--- /dev/null
+++ b/libc/shared/math/fsqrtf128.h
@@ -0,0 +1,28 @@
+//===-- Shared fsqrtf128 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_FSQRTF128_H
+#define LLVM_LIBC_SHARED_MATH_FSQRTF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "src/__support/math/fsqrtf128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::fsqrtf128;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+       //
+#endif // LLVM_LIBC_SHARED_MATH_FSQRTF128_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 0f3b17d467579..b8e18bbbec450 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -943,6 +943,15 @@ add_header_library(
     libc.src.__support.macros.optimization
 )
 
+add_header_library(
+  fsqrtf128
+  HDRS
+    fsqrtf128.h
+  DEPENDS
+    libc.src.__support.FPUtil.generic.sqrt
+    libc.src.__support.macros.properties.types
+)
+
 add_header_library(
   hypotf
   HDRS
@@ -1124,6 +1133,7 @@ add_header_library(
     libc.src.__support.math.range_reduction_double
     libc.src.__support.math.sincos_eval
     libc.src.__support.macros.optimization
+
 )
 
 add_header_library(
diff --git a/libc/src/__support/math/fsqrtf128.h b/libc/src/__support/math/fsqrtf128.h
new file mode 100644
index 0000000000000..8ca274eff6888
--- /dev/null
+++ b/libc/src/__support/math/fsqrtf128.h
@@ -0,0 +1,33 @@
+//===-- Implementation header for fsqrtf128 ---------------------*- 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_FSQRTF128_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FSQRTF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "src/__support/FPUtil/generic/sqrt.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static constexpr float fsqrtf128(float128 x) {
+  return fputil::sqrt<float>(x);
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FSQRTF128_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 95398f4c9e074..789976fa81451 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5241,8 +5241,8 @@ add_entrypoint_object(
   HDRS
     ../fsqrtf128.h
   DEPENDS
-    libc.src.__support.macros.properties.types
-    libc.src.__support.FPUtil.generic.sqrt
+    libc.src.__support.math.fsqrtf128
+    libc.src.errno.errno
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/fsqrtf128.cpp b/libc/src/math/generic/fsqrtf128.cpp
index f2c049529d6cd..1b2ba2fea0326 100644
--- a/libc/src/math/generic/fsqrtf128.cpp
+++ b/libc/src/math/generic/fsqrtf128.cpp
@@ -7,14 +7,11 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/fsqrtf128.h"
-#include "src/__support/FPUtil/generic/sqrt.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
-
+#include "src/__support/math/fsqrtf128.h"
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float, fsqrtf128, (float128 x)) {
-  return fputil::sqrt<float>(x);
+  return math::fsqrtf128(x);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index d0800cf84dcd5..5a0e5fc1e041b 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -58,6 +58,7 @@ add_fp_unittest(
     libc.src.__support.math.frexpf128
     libc.src.__support.math.frexpf16
     libc.src.__support.math.fsqrt
+    libc.src.__support.math.fsqrtf128
     libc.src.__support.math.hypotf
     libc.src.__support.math.ilogbf16
     libc.src.__support.math.log
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 79a15f127d853..feffe85f30a92 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -110,6 +110,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) {
 
   EXPECT_FP_EQ(float128(0x0p+0),
                LIBC_NAMESPACE::shared::atan2f128(float128(0.0), float128(0.0)));
+  EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::fsqrtf128(float128(1.0f)));
   EXPECT_FP_EQ_ALL_ROUNDING(float128(0.75), LIBC_NAMESPACE::shared::frexpf128(
                                                 float128(24), &exponent));
   EXPECT_EQ(exponent, 5);
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 953565e75a096..8da16b846c2f8 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2689,6 +2689,16 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_fsqrtf128",
+    hdrs = ["src/__support/math/fsqrtf128.h"],
+    deps = [
+        ":__support_common",
+        ":__support_fputil_sqrt",
+        ":__support_macros_config",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_exp10m1f",
     hdrs = ["src/__support/math/exp10m1f.h"],
@@ -4350,7 +4360,8 @@ libc_math_function(
 libc_math_function(
     name = "fsqrtf128",
     additional_deps = [
-        ":__support_fputil_sqrt",
+        ":__support_math_fsqrtf128",
+        ":errno",
     ],
 )
 

>From 1299c12152476457b22003fc4b0cc5ef8f08249d Mon Sep 17 00:00:00 2001
From: anonmiraj <nabilmalek48 at gmail.com>
Date: Sat, 17 Jan 2026 02:18:22 +0200
Subject: [PATCH 2/2] fix nits

---
 libc/src/__support/math/CMakeLists.txt        |  1 -
 .../llvm-project-overlay/libc/BUILD.bazel     | 20 +++++++++----------
 2 files changed, 10 insertions(+), 11 deletions(-)

diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index b8e18bbbec450..2c04a922cb390 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1133,7 +1133,6 @@ add_header_library(
     libc.src.__support.math.range_reduction_double
     libc.src.__support.math.sincos_eval
     libc.src.__support.macros.optimization
-
 )
 
 add_header_library(
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 8da16b846c2f8..71802df2b836a 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2689,16 +2689,6 @@ libc_support_library(
     ],
 )
 
-libc_support_library(
-    name = "__support_math_fsqrtf128",
-    hdrs = ["src/__support/math/fsqrtf128.h"],
-    deps = [
-        ":__support_common",
-        ":__support_fputil_sqrt",
-        ":__support_macros_config",
-    ],
-)
-
 libc_support_library(
     name = "__support_math_exp10m1f",
     hdrs = ["src/__support/math/exp10m1f.h"],
@@ -2831,6 +2821,16 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_fsqrtf128",
+    hdrs = ["src/__support/math/fsqrtf128.h"],
+    deps = [
+        ":__support_common",
+        ":__support_fputil_sqrt",
+        ":__support_macros_config",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_inv_trigf_utils",
     hdrs = ["src/__support/math/inv_trigf_utils.h"],



More information about the libc-commits mailing list