[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 12:59:14 PST 2026


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

>From 7c408b20498e30aea64dec688ada9046fcdec30e Mon Sep 17 00:00:00 2001
From: anonmiraj <nabilmalek48 at gmail.com>
Date: Thu, 15 Jan 2026 16:16:56 +0200
Subject: [PATCH] [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 2e7bbe1ef13be..63aa686cd3fc5 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/ilogbf16.h"
 #include "math/ldexpf.h"
 #include "math/ldexpf128.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 b45ccfc0eb3cb..52c945a4e17d5 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(
   range_reduction_double
   HDRS
@@ -1085,6 +1094,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 009a3033083bd..a60c8579fda6f 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5244,8 +5244,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 58214bf6f88eb..3da0d1e5ded3a 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.ilogbf16
     libc.src.__support.math.log
     libc.src.__support.math.ldexpf
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 6b78de0281347..060174be23a84 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -115,6 +115,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) {
                LIBC_NAMESPACE::shared::ldexpf128(float128(8), 5));
   ASSERT_FP_EQ(float128(-1 * (8 << 5)),
                LIBC_NAMESPACE::shared::ldexpf128(float128(-8), 5));
+  EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::fsqrtf128(float128(1.0f)));
 }
 
 #endif // LIBC_TYPES_HAS_FLOAT128
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 4251f20a5dfcf..bec4d2557b439 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"],
@@ -4310,7 +4320,8 @@ libc_math_function(
 libc_math_function(
     name = "fsqrtf128",
     additional_deps = [
-        ":__support_fputil_sqrt",
+        ":__support_math_fsqrtf128",
+        ":errno",
     ],
 )
 



More information about the libc-commits mailing list