[libc-commits] [libc] [llvm] [libc][math] Refactor hypotf to Header Only. (PR #175679)
via libc-commits
libc-commits at lists.llvm.org
Mon Jan 12 15:53:27 PST 2026
https://github.com/AnonMiraj created https://github.com/llvm/llvm-project/pull/175679
builds correctly with both Clang and GCC 12.2.
Closes #175338.
>From 24bac13ae8fb319e6f06ea938a1f0c70d2af9e62 Mon Sep 17 00:00:00 2001
From: anonmiraj <nabilmalek48 at gmail.com>
Date: Tue, 13 Jan 2026 00:46:45 +0200
Subject: [PATCH] [libc][math] Refactor hypotf to Header Only.
---
libc/shared/math.h | 1 +
libc/shared/math/hypotf.h | 24 ++++
libc/src/__support/math/CMakeLists.txt | 12 ++
libc/src/__support/math/hypotf.h | 107 ++++++++++++++++++
libc/src/math/generic/CMakeLists.txt | 8 +-
libc/src/math/generic/hypotf.cpp | 86 +-------------
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 19 +++-
9 files changed, 166 insertions(+), 93 deletions(-)
create mode 100644 libc/shared/math/hypotf.h
create mode 100644 libc/src/__support/math/hypotf.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 287f3aea1565a..1a3ccc5910e1c 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -60,6 +60,7 @@
#include "math/frexpf.h"
#include "math/frexpf128.h"
#include "math/frexpf16.h"
+#include "math/hypotf.h"
#include "math/ilogbf16.h"
#include "math/ldexpf.h"
#include "math/ldexpf128.h"
diff --git a/libc/shared/math/hypotf.h b/libc/shared/math/hypotf.h
new file mode 100644
index 0000000000000..4f01926ea6a80
--- /dev/null
+++ b/libc/shared/math/hypotf.h
@@ -0,0 +1,24 @@
+//===-- Shared hypotf 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_HYPOTF_H
+#define LLVM_LIBC_SHARED_MATH_HYPOTF_H
+
+#include "shared/libc_common.h"
+
+#include "src/__support/math/hypotf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::hypotf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_HYPOTF_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 4139a1b1d3444..7e004f8ce81ee 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1078,3 +1078,15 @@ add_header_library(
libc.src.__support.math.sincos_eval
libc.src.__support.macros.optimization
)
+add_header_library(
+ hypotf
+ HDRS
+ hypotf.h
+ DEPENDS
+ libc.src.__support.FPUtil.double_double
+ libc.src.__support.FPUtil.fenv_impl
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.FPUtil.multiply_add
+ libc.src.__support.FPUtil.sqrt
+ libc.src.__support.macros.optimization
+)
diff --git a/libc/src/__support/math/hypotf.h b/libc/src/__support/math/hypotf.h
new file mode 100644
index 0000000000000..0169cf398f361
--- /dev/null
+++ b/libc/src/__support/math/hypotf.h
@@ -0,0 +1,107 @@
+//===-- Implementation header for hypotf ------------------------*- 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_HYPOTF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_HYPOTF_H
+
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/double_double.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/sqrt.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static constexpr float hypotf(float x, float y) {
+ using DoubleBits = fputil::FPBits<double>;
+ using FPBits = fputil::FPBits<float>;
+
+ FPBits x_abs = FPBits(x).abs();
+ FPBits y_abs = FPBits(y).abs();
+
+ bool x_abs_larger = x_abs.uintval() >= y_abs.uintval();
+
+ FPBits a_bits = x_abs_larger ? x_abs : y_abs;
+ FPBits b_bits = x_abs_larger ? y_abs : x_abs;
+
+ uint32_t a_u = a_bits.uintval();
+ uint32_t b_u = b_bits.uintval();
+
+ // Note: replacing `a_u >= FPBits::EXP_MASK` with `a_bits.is_inf_or_nan()`
+ // generates extra exponent bit masking instructions on x86-64.
+ if (LIBC_UNLIKELY(a_u >= FPBits::EXP_MASK)) {
+ // x or y is inf or nan
+ if (a_bits.is_signaling_nan() || b_bits.is_signaling_nan()) {
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+ if (a_bits.is_inf() || b_bits.is_inf())
+ return FPBits::inf().get_val();
+ return a_bits.get_val();
+ }
+
+ if (LIBC_UNLIKELY(a_u - b_u >=
+ static_cast<uint32_t>((FPBits::FRACTION_LEN + 2)
+ << FPBits::FRACTION_LEN)))
+ return x_abs.get_val() + y_abs.get_val();
+
+ double ad = static_cast<double>(a_bits.get_val());
+ double bd = static_cast<double>(b_bits.get_val());
+
+ // These squares are exact.
+ double a_sq = ad * ad;
+#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
+ double sum_sq = fputil::multiply_add(bd, bd, a_sq);
+#else
+ double b_sq = bd * bd;
+ double sum_sq = a_sq + b_sq;
+#endif
+
+ // Take sqrt in double precision.
+ DoubleBits result(fputil::sqrt<double>(sum_sq));
+ uint64_t r_u = result.uintval();
+
+ // If any of the sticky bits of the result are non-zero, except the LSB, then
+ // the rounded result is correct.
+ if (LIBC_UNLIKELY(((r_u + 1) & 0x0000'0000'0FFF'FFFE) == 0)) {
+ double r_d = result.get_val();
+
+ // Perform rounding correction.
+#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
+ double sum_sq_lo = fputil::multiply_add(bd, bd, a_sq - sum_sq);
+ double err = sum_sq_lo - fputil::multiply_add(r_d, r_d, -sum_sq);
+#else
+ fputil::DoubleDouble r_sq = fputil::exact_mult(r_d, r_d);
+ double sum_sq_lo = b_sq - (sum_sq - a_sq);
+ double err = (sum_sq - r_sq.hi) + (sum_sq_lo - r_sq.lo);
+#endif
+
+ if (err > 0) {
+ r_u |= 1;
+ } else if ((err < 0) && (r_u & 1) == 0) {
+ r_u -= 1;
+ } else if ((r_u & 0x0000'0000'1FFF'FFFF) == 0) {
+ // The rounded result is exact.
+ fputil::clear_except_if_required(FE_INEXACT);
+ }
+ return static_cast<float>(DoubleBits(r_u).get_val());
+ }
+
+ return static_cast<float>(result.get_val());
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_HYPOTF_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 6de4cf376bf02..f90ec0d96b279 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3269,12 +3269,8 @@ add_entrypoint_object(
HDRS
../hypotf.h
DEPENDS
- libc.src.__support.FPUtil.double_double
- libc.src.__support.FPUtil.fenv_impl
- libc.src.__support.FPUtil.fp_bits
- libc.src.__support.FPUtil.multiply_add
- libc.src.__support.FPUtil.sqrt
- libc.src.__support.macros.optimization
+ libc.src.__support.math.hypotf
+ libc.src.errno.errno
)
add_entrypoint_object(
diff --git a/libc/src/math/generic/hypotf.cpp b/libc/src/math/generic/hypotf.cpp
index ec48f62163a48..71123a74870ed 100644
--- a/libc/src/math/generic/hypotf.cpp
+++ b/libc/src/math/generic/hypotf.cpp
@@ -6,93 +6,11 @@
//
//===----------------------------------------------------------------------===//
#include "src/math/hypotf.h"
-#include "src/__support/FPUtil/FEnvImpl.h"
-#include "src/__support/FPUtil/FPBits.h"
-#include "src/__support/FPUtil/double_double.h"
-#include "src/__support/FPUtil/multiply_add.h"
-#include "src/__support/FPUtil/sqrt.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
-#include "src/__support/macros/optimization.h"
-
+#include "src/__support/math/hypotf.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(float, hypotf, (float x, float y)) {
- using DoubleBits = fputil::FPBits<double>;
- using FPBits = fputil::FPBits<float>;
-
- FPBits x_abs = FPBits(x).abs();
- FPBits y_abs = FPBits(y).abs();
-
- bool x_abs_larger = x_abs.uintval() >= y_abs.uintval();
-
- FPBits a_bits = x_abs_larger ? x_abs : y_abs;
- FPBits b_bits = x_abs_larger ? y_abs : x_abs;
-
- uint32_t a_u = a_bits.uintval();
- uint32_t b_u = b_bits.uintval();
-
- // Note: replacing `a_u >= FPBits::EXP_MASK` with `a_bits.is_inf_or_nan()`
- // generates extra exponent bit masking instructions on x86-64.
- if (LIBC_UNLIKELY(a_u >= FPBits::EXP_MASK)) {
- // x or y is inf or nan
- if (a_bits.is_signaling_nan() || b_bits.is_signaling_nan()) {
- fputil::raise_except_if_required(FE_INVALID);
- return FPBits::quiet_nan().get_val();
- }
- if (a_bits.is_inf() || b_bits.is_inf())
- return FPBits::inf().get_val();
- return a_bits.get_val();
- }
-
- if (LIBC_UNLIKELY(a_u - b_u >=
- static_cast<uint32_t>((FPBits::FRACTION_LEN + 2)
- << FPBits::FRACTION_LEN)))
- return x_abs.get_val() + y_abs.get_val();
-
- double ad = static_cast<double>(a_bits.get_val());
- double bd = static_cast<double>(b_bits.get_val());
-
- // These squares are exact.
- double a_sq = ad * ad;
-#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
- double sum_sq = fputil::multiply_add(bd, bd, a_sq);
-#else
- double b_sq = bd * bd;
- double sum_sq = a_sq + b_sq;
-#endif
-
- // Take sqrt in double precision.
- DoubleBits result(fputil::sqrt<double>(sum_sq));
- uint64_t r_u = result.uintval();
-
- // If any of the sticky bits of the result are non-zero, except the LSB, then
- // the rounded result is correct.
- if (LIBC_UNLIKELY(((r_u + 1) & 0x0000'0000'0FFF'FFFE) == 0)) {
- double r_d = result.get_val();
-
- // Perform rounding correction.
-#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
- double sum_sq_lo = fputil::multiply_add(bd, bd, a_sq - sum_sq);
- double err = sum_sq_lo - fputil::multiply_add(r_d, r_d, -sum_sq);
-#else
- fputil::DoubleDouble r_sq = fputil::exact_mult(r_d, r_d);
- double sum_sq_lo = b_sq - (sum_sq - a_sq);
- double err = (sum_sq - r_sq.hi) + (sum_sq_lo - r_sq.lo);
-#endif
-
- if (err > 0) {
- r_u |= 1;
- } else if ((err < 0) && (r_u & 1) == 0) {
- r_u -= 1;
- } else if ((r_u & 0x0000'0000'1FFF'FFFF) == 0) {
- // The rounded result is exact.
- fputil::clear_except_if_required(FE_INEXACT);
- }
- return static_cast<float>(DoubleBits(r_u).get_val());
- }
-
- return static_cast<float>(result.get_val());
+ return math::hypotf(x, y);
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index c5955ecfd54cb..0ad67fd0032c1 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -64,4 +64,5 @@ add_fp_unittest(
libc.src.__support.math.rsqrtf
libc.src.__support.math.rsqrtf16
libc.src.__support.math.sin
+ libc.src.__support.math.hypotf
)
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 20a98a1a9138c..b6dd83ca3d93f 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -69,6 +69,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat) {
EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::expf(0.0f));
EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::exp2f(0.0f));
EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::expm1f(0.0f));
+ EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::hypotf(0.0f, 0.0f));
EXPECT_FP_EQ_ALL_ROUNDING(0.75f,
LIBC_NAMESPACE::shared::frexpf(24.0f, &exponent));
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index c03c21b834895..880b7d4975f1d 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3240,7 +3240,18 @@ libc_support_library(
":__support_math_exp10f_utils",
],
)
-
+libc_support_library(
+ name = "__support_math_hypotf",
+ hdrs = ["src/__support/math/hypotf.h"],
+ deps = [
+ ":__support_fputil_double_double",
+ ":__support_fputil_fenv_impl",
+ ":__support_fputil_fp_bits",
+ ":__support_fputil_multiply_add",
+ ":__support_fputil_sqrt",
+ ":__support_macros_optimization",
+ ],
+)
############################### complex targets ################################
libc_function(
@@ -4317,8 +4328,8 @@ libc_math_function(name = "hypot")
libc_math_function(
name = "hypotf",
additional_deps = [
- ":__support_fputil_double_double",
- ":__support_fputil_sqrt",
+ ":__support_math_hypotf",
+ ":errno",
],
)
@@ -7860,3 +7871,5 @@ libc_function(
":types_wchar_t",
],
)
+
+
More information about the libc-commits
mailing list