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

Muhammad Bassiouni via libc-commits libc-commits at lists.llvm.org
Fri Mar 13 12:51:14 PDT 2026


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

>From cb9d479100882500d8988753483d78e1303995c2 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               | 24 +++++++++++++++++++
 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, 78 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 ede0ebd5371ac..0ceabfaa575c9 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -150,6 +150,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 0df8262cfb5f2..08d9d9739391e 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1932,6 +1932,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..13d6e3fecff01
--- /dev/null
+++ b/libc/src/__support/math/hypot.h
@@ -0,0 +1,24 @@
+//===-- 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 418cf85b84a20..27c950ead3d42 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3165,7 +3165,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 c90e5687d8c33..0f10f2939f778 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -147,6 +147,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 17045ce5edfdb..55f60860b1461 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -245,6 +245,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 31a229d94a187..276f7ad014cf9 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -5420,6 +5420,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"],
@@ -7100,7 +7110,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