[libc-commits] [libc] [llvm] [libc][math] Move hypot to shared/math and make it constexpr (PR #177588)

Chinmay Ingle via libc-commits libc-commits at lists.llvm.org
Tue Feb 24 00:07:58 PST 2026


https://github.com/chinmayingle updated https://github.com/llvm/llvm-project/pull/177588

>From 637f25dc7b896d8414f2691f7314f310412e6f96 Mon Sep 17 00:00:00 2001
From: chinmayingle <chinmay.r.ingle at gmail.com>
Date: Tue, 24 Feb 2026 11:32:34 +0530
Subject: [PATCH]  Hypot changes

---
 libc/shared/math.h                            |  1 +
 libc/shared/math/hypot.h                      | 22 ++++++++++++++++
 libc/src/__support/math/CMakeLists.txt        | 10 ++++++++
 libc/src/__support/math/hypot.h               | 25 +++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt          |  2 +-
 libc/src/math/generic/hypot.cpp               |  7 ++----
 libc/test/shared/CMakeLists.txt               |  1 +
 libc/test/shared/shared_math_test.cpp         |  1 +
 .../llvm-project-overlay/libc/BUILD.bazel     | 17 ++++++++++++-
 9 files changed, 79 insertions(+), 7 deletions(-)
 create mode 100644 libc/shared/math/hypot.h
 create mode 100644 libc/src/__support/math/hypot.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 034674735a422..e71f43e1756e6 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -118,6 +118,7 @@
 #include "math/getpayloadf128.h"
 #include "math/getpayloadf16.h"
 #include "math/getpayloadl.h"
+#include "math/hypot.h"
 #include "math/hypotf.h"
 #include "math/hypotf16.h"
 #include "math/ilogb.h"
diff --git a/libc/shared/math/hypot.h b/libc/shared/math/hypot.h
new file mode 100644
index 0000000000000..51fd006397c02
--- /dev/null
+++ b/libc/shared/math/hypot.h
@@ -0,0 +1,22 @@
+//===-- Shared hypot 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_HYPOT_H
+#define LLVM_LIBC_SHARED_MATH_HYPOT_H
+
+#include "src/__support/math/hypot.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::hypot;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_HYPOT_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index e577e9410c6c2..45696a80452c3 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1587,6 +1587,16 @@ add_header_library(
     libc.src.__support.macros.properties.types
 )
 
+add_header_library(
+  hypot
+  HDRS
+    hypot.h
+  DEPENDS
+    libc.src.__support.FPUtil.hypot
+    libc.src.__support.common
+    libc.src.__support.macros.config
+)
+
 add_header_library(
   hypotf
   HDRS
diff --git a/libc/src/__support/math/hypot.h b/libc/src/__support/math/hypot.h
new file mode 100644
index 0000000000000..5d4e30c8d8081
--- /dev/null
+++ b/libc/src/__support/math/hypot.h
@@ -0,0 +1,25 @@
+//===-- Implementation header for hypot --------------------------*- 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_HYPOT_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_HYPOT_H
+
+#include "src/__support/FPUtil/Hypot.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+LIBC_INLINE double hypot(double x, double y) { return fputil::hypot(x, y); }
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_HYPOT_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 70843ce3c0aef..8b278d22ec72d 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3196,7 +3196,7 @@ add_entrypoint_object(
   HDRS
     ../hypot.h
   DEPENDS
-    libc.src.__support.FPUtil.hypot
+    libc.src.__support.math.hypot
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/hypot.cpp b/libc/src/math/generic/hypot.cpp
index 0dfe4360bafe0..e6e5b02abcca9 100644
--- a/libc/src/math/generic/hypot.cpp
+++ b/libc/src/math/generic/hypot.cpp
@@ -5,16 +5,13 @@
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
-
 #include "src/math/hypot.h"
-#include "src/__support/FPUtil/Hypot.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/hypot.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(double, hypot, (double x, double y)) {
-  return LIBC_NAMESPACE::fputil::hypot(x, y);
+  return math::hypot(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 0cdaed2e10b49..08b4d7093d3e6 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -114,6 +114,7 @@ add_fp_unittest(
     libc.src.__support.math.getpayloadf128
     libc.src.__support.math.getpayloadf16
     libc.src.__support.math.getpayloadl
+    libc.src.__support.math.hypot
     libc.src.__support.math.hypotf
     libc.src.__support.math.hypotf16
     libc.src.__support.math.ilogb
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 312a6ac7a8d47..9836a7d260f1b 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -179,6 +179,7 @@ TEST(LlvmLibcSharedMathTest, AllDouble) {
   EXPECT_FP_EQ(0x1p+0, LIBC_NAMESPACE::shared::exp10(0.0));
   EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::expm1(0.0));
   EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::ffma(0.0, 0.0, 0.0));
+  EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::hypot(0.0, 0.0));
   EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::fsqrt(0.0));
   EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::log(1.0));
   EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::log10(1.0));
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index de443652ccf0b..4b249c24b4c4b 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -4312,6 +4312,16 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_hypot",
+    hdrs = ["src/__support/math/hypot.h"],
+    deps = [
+        ":__support_common",
+        ":__support_fputil_hypot",
+        ":__support_macros_config",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_hypotf",
     hdrs = ["src/__support/math/hypotf.h"],
@@ -5652,7 +5662,12 @@ libc_math_function(
     ],
 )
 
-libc_math_function(name = "hypot")
+libc_math_function(
+    name = "hypot",
+    additional_deps = [
+        ":__support_math_hypot",
+    ],
+)
 
 libc_math_function(
     name = "hypotf",



More information about the libc-commits mailing list