[libc] [llvm] [libc][math] Refactor hypotf16 to Header Only. (PR #180511)

via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 13 22:13:26 PST 2026


https://github.com/Sukumarsawant updated https://github.com/llvm/llvm-project/pull/180511

>From 6628fecb7488f683275f893e5bb312961dc44d66 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Mon, 9 Feb 2026 14:14:02 +0530
Subject: [PATCH 1/3]  intial commit

---
 libc/shared/math.h                            |   1 +
 libc/shared/math/hypotf16.h                   |  31 +++++
 libc/src/__support/math/CMakeLists.txt        |  15 +++
 libc/src/__support/math/hypotf16.h            |  97 +++++++++++++++
 libc/src/math/generic/CMakeLists.txt          |   9 +-
 libc/src/math/generic/hypotf16.cpp            | 111 ++++--------------
 libc/test/shared/CMakeLists.txt               |   1 +
 libc/test/shared/shared_math_test.cpp         |   1 +
 .../llvm-project-overlay/libc/BUILD.bazel     |  21 +++-
 9 files changed, 187 insertions(+), 100 deletions(-)
 create mode 100644 libc/shared/math/hypotf16.h
 create mode 100644 libc/src/__support/math/hypotf16.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 66132326c2257..74051f4b7240a 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -73,6 +73,7 @@
 #include "math/fsqrtf128.h"
 #include "math/fsqrtl.h"
 #include "math/hypotf.h"
+#include "math/hypotf16.h"
 #include "math/ilogb.h"
 #include "math/ilogbf.h"
 #include "math/ilogbf128.h"
diff --git a/libc/shared/math/hypotf16.h b/libc/shared/math/hypotf16.h
new file mode 100644
index 0000000000000..ebb83e3450fcd
--- /dev/null
+++ b/libc/shared/math/hypotf16.h
@@ -0,0 +1,31 @@
+//===-- Shared hypotf16 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_HYPOTF16_H
+#define LLVM_LIBC_SHARED_MATH_HYPOTF16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+#include "shared/libc_common.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/math/hypotf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace shared {
+
+using math::hypotf16;
+
+} // namespace shared
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_MATH_HYPOTF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index c0293c0969217..bc701954f1309 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1124,6 +1124,21 @@ add_header_library(
     libc.src.__support.macros.optimization
 )
 
+add_header_library(
+  hypotf16
+  HDRS
+    hypotf16.h
+  DEPENDS
+    libc.src.__support.FPUtil.fenv_impl
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.cast
+    libc.src.__support.FPUtil.multiply_add
+    libc.src.__support.FPUtil.sqrt
+    libc.src.__support.macros.optimization
+    libc.src.__support.macros.properties.types
+    libc.include.llvm-libc-macros.float16_macros
+)
+
 add_header_library(
   ilogbl
   HDRS
diff --git a/libc/src/__support/math/hypotf16.h b/libc/src/__support/math/hypotf16.h
new file mode 100644
index 0000000000000..3efa70850ec18
--- /dev/null
+++ b/libc/src/__support/math/hypotf16.h
@@ -0,0 +1,97 @@
+//===-- Implementation header for hypotf16 ----------------------*- 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_HYPOTF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_HYPOTF16_H
+
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/sqrt.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/optimization.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE float16 hypotf16(float16 x, float16 y) {
+  using FloatBits = fputil::FPBits<float>;
+  using FPBits = fputil::FPBits<float16>;
+
+  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;
+
+  uint16_t a_u = a_bits.uintval();
+  uint16_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();
+  }
+
+  float af = fputil::cast<float>(a_bits.get_val());
+  float bf = fputil::cast<float>(b_bits.get_val());
+
+  // Compiler runtime basic operations for float16 might not be correctly
+  // rounded for all rounding modes.
+  if (LIBC_UNLIKELY(a_u - b_u >=
+                    static_cast<uint16_t>((FPBits::FRACTION_LEN + 2)
+                                          << FPBits::FRACTION_LEN)))
+    return fputil::cast<float16>(af + bf);
+
+  // These squares are exact.
+  float a_sq = af * af;
+  float sum_sq = fputil::multiply_add(bf, bf, a_sq);
+
+  FloatBits result(fputil::sqrt<float>(sum_sq));
+  uint32_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'0FFE) == 0)) {
+    float r_d = result.get_val();
+
+    // Perform rounding correction.
+    float sum_sq_lo = fputil::multiply_add(bf, bf, a_sq - sum_sq);
+    float err = sum_sq_lo - fputil::multiply_add(r_d, r_d, -sum_sq);
+
+    if (err > 0) {
+      r_u |= 1;
+    } else if ((err < 0) && (r_u & 1) == 0) {
+      r_u -= 1;
+    } else if ((r_u & 0x0000'1FFF) == 0) {
+      // The rounded result is exact.
+      fputil::clear_except_if_required(FE_INEXACT);
+    }
+    return fputil::cast<float16>(FloatBits(r_u).get_val());
+  }
+
+  return fputil::cast<float16>(result.get_val());
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_HYPOTF16_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 96dc0e7ca4851..9eda20e1e41b2 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3180,13 +3180,8 @@ add_entrypoint_object(
   HDRS
     ../hypotf16.h
   DEPENDS
-    libc.src.__support.FPUtil.fenv_impl
-    libc.src.__support.FPUtil.fp_bits
-    libc.src.__support.FPUtil.cast
-    libc.src.__support.FPUtil.multiply_add
-    libc.src.__support.FPUtil.sqrt
-    libc.src.__support.macros.optimization
-    libc.src.__support.macros.properties.types
+    libc.src.__support.math.hypotf16
+    libc.src.errno.errno
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/hypotf16.cpp b/libc/src/math/generic/hypotf16.cpp
index fa90069f9ff0d..8d02849b23d1c 100644
--- a/libc/src/math/generic/hypotf16.cpp
+++ b/libc/src/math/generic/hypotf16.cpp
@@ -1,91 +1,20 @@
-//===-- Implementation of hypotf16 function -------------------------------===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-#include "src/math/hypotf16.h"
-#include "src/__support/FPUtil/FEnvImpl.h"
-#include "src/__support/FPUtil/FPBits.h"
-#include "src/__support/FPUtil/cast.h"
-#include "src/__support/FPUtil/multiply_add.h"
-#include "src/__support/FPUtil/sqrt.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/optimization.h"
-#include "src/__support/macros/properties/types.h"
-
-namespace LIBC_NAMESPACE_DECL {
-
-// For targets where conversion from float to float16 has to be
-// emulated, fputil::hypot<float16> is faster
-LLVM_LIBC_FUNCTION(float16, hypotf16, (float16 x, float16 y)) {
-  using FloatBits = fputil::FPBits<float>;
-  using FPBits = fputil::FPBits<float16>;
-
-  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;
-
-  uint16_t a_u = a_bits.uintval();
-  uint16_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();
-  }
-
-  float af = fputil::cast<float>(a_bits.get_val());
-  float bf = fputil::cast<float>(b_bits.get_val());
-
-  // Compiler runtime basic operations for float16 might not be correctly
-  // rounded for all rounding modes.
-  if (LIBC_UNLIKELY(a_u - b_u >=
-                    static_cast<uint16_t>((FPBits::FRACTION_LEN + 2)
-                                          << FPBits::FRACTION_LEN)))
-    return fputil::cast<float16>(af + bf);
-
-  // These squares are exact.
-  float a_sq = af * af;
-  float sum_sq = fputil::multiply_add(bf, bf, a_sq);
-
-  FloatBits result(fputil::sqrt<float>(sum_sq));
-  uint32_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'0FFE) == 0)) {
-    float r_d = result.get_val();
-
-    // Perform rounding correction.
-    float sum_sq_lo = fputil::multiply_add(bf, bf, a_sq - sum_sq);
-    float err = sum_sq_lo - fputil::multiply_add(r_d, r_d, -sum_sq);
-
-    if (err > 0) {
-      r_u |= 1;
-    } else if ((err < 0) && (r_u & 1) == 0) {
-      r_u -= 1;
-    } else if ((r_u & 0x0000'1FFF) == 0) {
-      // The rounded result is exact.
-      fputil::clear_except_if_required(FE_INEXACT);
-    }
-    return fputil::cast<float16>(FloatBits(r_u).get_val());
-  }
-
-  return fputil::cast<float16>(result.get_val());
-}
-
-} // namespace LIBC_NAMESPACE_DECL
+//===-- Implementation of hypotf16 function -------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/hypotf16.h"
+#include "src/__support/math/hypotf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+// For targets where conversion from float to float16 has to be
+// emulated, fputil::hypot<float16> is faster
+LLVM_LIBC_FUNCTION(float16, hypotf16, (float16 x, float16 y)) {
+  return math::hypotf16(x, y);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 7d72ced3e057c..12053fa4a68ac 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -69,6 +69,7 @@ add_fp_unittest(
     libc.src.__support.math.fsqrtf128
     libc.src.__support.math.fsqrtl
     libc.src.__support.math.hypotf
+    libc.src.__support.math.hypotf16
     libc.src.__support.math.ilogb
     libc.src.__support.math.ilogbf
     libc.src.__support.math.ilogbf16
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 9e024387efbfa..f5594f511eb27 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -33,6 +33,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
   EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::exp2m1f16(0.0f16));
   EXPECT_FP_EQ(0x1p+0f16, LIBC_NAMESPACE::shared::expf16(0.0f16));
   EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::expm1f16(0.0f16));
+  EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::hypotf16(0.0f16,0.0f16));
   EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::logf16(1.0f16));
   EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::sinhf16(0.0f16));
 
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index ae7228c52d3c0..906534548c63c 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3742,6 +3742,23 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_hypotf16",
+    hdrs = ["src/__support/math/hypotf16.h"],
+    deps = [
+        ":__support_common",
+        ":__support_fputil_cast",
+        ":__support_fputil_fenv_impl",
+        ":__support_fputil_fp_bits",
+        ":__support_fputil_multiply_add",
+        ":__support_fputil_sqrt",
+        ":__support_macros_config",
+        ":__support_macros_optimization",
+        ":__support_macros_properties_types",
+        ":llvm_libc_macros_float16_macros",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_sinhf",
     hdrs = ["src/__support/math/sinhf.h"],
@@ -4885,8 +4902,8 @@ libc_math_function(
 libc_math_function(
     name = "hypotf16",
     additional_deps = [
-        ":__support_fputil_multiply_add",
-        ":__support_fputil_sqrt",
+        ":__support_math_hypotf16",
+        ":errno",
     ],
 )
 

>From 9776762be56b4cf3388a237b3a7b4852dddabaf5 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Mon, 9 Feb 2026 18:01:45 +0530
Subject: [PATCH 2/3] crlf to lf cpp file + formatted + tested

---
 libc/src/math/generic/hypotf16.cpp    | 40 +++++++++++++--------------
 libc/test/shared/shared_math_test.cpp |  2 +-
 2 files changed, 21 insertions(+), 21 deletions(-)

diff --git a/libc/src/math/generic/hypotf16.cpp b/libc/src/math/generic/hypotf16.cpp
index 8d02849b23d1c..6bd76eab22a74 100644
--- a/libc/src/math/generic/hypotf16.cpp
+++ b/libc/src/math/generic/hypotf16.cpp
@@ -1,20 +1,20 @@
-//===-- Implementation of hypotf16 function -------------------------------===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-#include "src/math/hypotf16.h"
-#include "src/__support/math/hypotf16.h"
-
-namespace LIBC_NAMESPACE_DECL {
-
-// For targets where conversion from float to float16 has to be
-// emulated, fputil::hypot<float16> is faster
-LLVM_LIBC_FUNCTION(float16, hypotf16, (float16 x, float16 y)) {
-  return math::hypotf16(x, y);
-}
-
-} // namespace LIBC_NAMESPACE_DECL
+//===-- Implementation of hypotf16 function -------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/hypotf16.h"
+#include "src/__support/math/hypotf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+// For targets where conversion from float to float16 has to be
+// emulated, fputil::hypot<float16> is faster
+LLVM_LIBC_FUNCTION(float16, hypotf16, (float16 x, float16 y)) {
+  return math::hypotf16(x, y);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index f5594f511eb27..2977112f21adc 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -33,7 +33,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
   EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::exp2m1f16(0.0f16));
   EXPECT_FP_EQ(0x1p+0f16, LIBC_NAMESPACE::shared::expf16(0.0f16));
   EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::expm1f16(0.0f16));
-  EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::hypotf16(0.0f16,0.0f16));
+  EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::hypotf16(0.0f16, 0.0f16));
   EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::logf16(1.0f16));
   EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::sinhf16(0.0f16));
 

>From 12729476cfaae22b54a817f90e5a0be427d36898 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sat, 14 Feb 2026 11:37:42 +0530
Subject: [PATCH 3/3] FIX : removed errno and moved comment

---
 libc/src/__support/math/hypotf16.h                | 2 ++
 libc/src/math/generic/CMakeLists.txt              | 1 -
 libc/src/math/generic/hypotf16.cpp                | 2 --
 utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 1 -
 4 files changed, 2 insertions(+), 4 deletions(-)

diff --git a/libc/src/__support/math/hypotf16.h b/libc/src/__support/math/hypotf16.h
index 3efa70850ec18..07e8a0566b8ec 100644
--- a/libc/src/__support/math/hypotf16.h
+++ b/libc/src/__support/math/hypotf16.h
@@ -22,6 +22,8 @@ namespace LIBC_NAMESPACE_DECL {
 
 namespace math {
 
+// For targets where conversion from float to float16 has to be
+// emulated, fputil::hypot<float16> is faster
 LIBC_INLINE float16 hypotf16(float16 x, float16 y) {
   using FloatBits = fputil::FPBits<float>;
   using FPBits = fputil::FPBits<float16>;
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 9eda20e1e41b2..1565172d92103 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3181,7 +3181,6 @@ add_entrypoint_object(
     ../hypotf16.h
   DEPENDS
     libc.src.__support.math.hypotf16
-    libc.src.errno.errno
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/hypotf16.cpp b/libc/src/math/generic/hypotf16.cpp
index 6bd76eab22a74..749d4eedd1416 100644
--- a/libc/src/math/generic/hypotf16.cpp
+++ b/libc/src/math/generic/hypotf16.cpp
@@ -11,8 +11,6 @@
 
 namespace LIBC_NAMESPACE_DECL {
 
-// For targets where conversion from float to float16 has to be
-// emulated, fputil::hypot<float16> is faster
 LLVM_LIBC_FUNCTION(float16, hypotf16, (float16 x, float16 y)) {
   return math::hypotf16(x, y);
 }
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 906534548c63c..a28f3e7c9ea4e 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -4903,7 +4903,6 @@ libc_math_function(
     name = "hypotf16",
     additional_deps = [
         ":__support_math_hypotf16",
-        ":errno",
     ],
 )
 



More information about the llvm-commits mailing list