[libc-commits] [libc] [llvm] [libc][math] Refactor fadd family to header-only (PR #182185)

via libc-commits libc-commits at lists.llvm.org
Wed Feb 18 15:56:32 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Mohamed Emad (hulxv)

<details>
<summary>Changes</summary>

Refactors the fadd math family to be header-only.

Closes https://github.com/llvm/llvm-project/issues/182184

Target Functions:
  - fadd
  - faddf128
  - faddl

---
Full diff: https://github.com/llvm/llvm-project/pull/182185.diff


15 Files Affected:

- (modified) libc/shared/math.h (+3) 
- (added) libc/shared/math/fadd.h (+22) 
- (added) libc/shared/math/faddf128.h (+28) 
- (added) libc/shared/math/faddl.h (+22) 
- (modified) libc/src/__support/math/CMakeLists.txt (+25) 
- (added) libc/src/__support/math/fadd.h (+26) 
- (added) libc/src/__support/math/faddf128.h (+32) 
- (added) libc/src/__support/math/faddl.h (+26) 
- (modified) libc/src/math/generic/CMakeLists.txt (+3-4) 
- (modified) libc/src/math/generic/fadd.cpp (+2-4) 
- (modified) libc/src/math/generic/faddf128.cpp (+2-4) 
- (modified) libc/src/math/generic/faddl.cpp (+2-4) 
- (modified) libc/test/shared/CMakeLists.txt (+3) 
- (modified) libc/test/shared/shared_math_test.cpp (+7) 
- (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+46-3) 


``````````diff
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 8a5aca82c6ec3..2adb08c0026fe 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -83,6 +83,9 @@
 #include "math/f16sqrt.h"
 #include "math/f16sqrtf.h"
 #include "math/f16sqrtl.h"
+#include "math/fadd.h"
+#include "math/faddf128.h"
+#include "math/faddl.h"
 #include "math/ffma.h"
 #include "math/ffmal.h"
 #include "math/frexpf.h"
diff --git a/libc/shared/math/fadd.h b/libc/shared/math/fadd.h
new file mode 100644
index 0000000000000..117ea1ff91903
--- /dev/null
+++ b/libc/shared/math/fadd.h
@@ -0,0 +1,22 @@
+//===-- Shared fadd 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_FADD_H
+#define LLVM_LIBC_SHARED_MATH_FADD_H
+
+#include "src/__support/math/fadd.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::fadd;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_FADD_H
diff --git a/libc/shared/math/faddf128.h b/libc/shared/math/faddf128.h
new file mode 100644
index 0000000000000..7971240828967
--- /dev/null
+++ b/libc/shared/math/faddf128.h
@@ -0,0 +1,28 @@
+//===-- Shared faddf128 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_FADDF128_H
+#define LLVM_LIBC_SHARED_MATH_FADDF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "src/__support/math/faddf128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::faddf128;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SHARED_MATH_FADDF128_H
diff --git a/libc/shared/math/faddl.h b/libc/shared/math/faddl.h
new file mode 100644
index 0000000000000..8b1f706027964
--- /dev/null
+++ b/libc/shared/math/faddl.h
@@ -0,0 +1,22 @@
+//===-- Shared faddl 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_FADDL_H
+#define LLVM_LIBC_SHARED_MATH_FADDL_H
+
+#include "src/__support/math/faddl.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::faddl;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_FADDL_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index e21fe8ef0ab93..1fdd934c0b388 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -751,6 +751,31 @@ add_header_library(
     libc.src.__support.FPUtil.generic.add_sub
     libc.src.__support.macros.config
 )
+add_header_library(
+  fadd
+  HDRS
+    fadd.h
+  DEPENDS
+    libc.src.__support.FPUtil.generic.add_sub
+    libc.src.__support.macros.config
+)
+add_header_library(
+  faddf128
+  HDRS
+    faddf128.h
+  DEPENDS
+    libc.include.llvm-libc-types.float128
+    libc.src.__support.FPUtil.generic.add_sub
+    libc.src.__support.macros.config
+)
+add_header_library(
+  faddl
+  HDRS
+    faddl.h
+  DEPENDS
+    libc.src.__support.FPUtil.generic.add_sub
+    libc.src.__support.macros.config
+)
 
 add_header_library(
   ffmal
diff --git a/libc/src/__support/math/fadd.h b/libc/src/__support/math/fadd.h
new file mode 100644
index 0000000000000..d38e7bbaf973d
--- /dev/null
+++ b/libc/src/__support/math/fadd.h
@@ -0,0 +1,26 @@
+//===-- Implementation header for fadd --------------------------*- 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_FADD_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FADD_H
+
+#include "src/__support/FPUtil/generic/add_sub.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE constexpr float fadd(double x, double y) {
+  return fputil::generic::add<float>(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FADD_H
diff --git a/libc/src/__support/math/faddf128.h b/libc/src/__support/math/faddf128.h
new file mode 100644
index 0000000000000..8b3828a6fac7c
--- /dev/null
+++ b/libc/src/__support/math/faddf128.h
@@ -0,0 +1,32 @@
+//===-- Implementation header for faddf128 ----------------------*- 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_FADDF128_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FADDF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "src/__support/FPUtil/generic/add_sub.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE constexpr float faddf128(float128 x, float128 y) {
+  return fputil::generic::add<float>(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FADDF128_H
diff --git a/libc/src/__support/math/faddl.h b/libc/src/__support/math/faddl.h
new file mode 100644
index 0000000000000..525209ec6348f
--- /dev/null
+++ b/libc/src/__support/math/faddl.h
@@ -0,0 +1,26 @@
+//===-- Implementation header for faddl -------------------------*- 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_FADDL_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FADDL_H
+
+#include "src/__support/FPUtil/generic/add_sub.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE constexpr float faddl(long double x, long double y) {
+  return fputil::generic::add<float>(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FADDL_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 57a29665318a3..4631cdfcba19c 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -564,7 +564,7 @@ add_entrypoint_object(
   HDRS
     ../fadd.h
   DEPENDS
-    libc.src.__support.FPUtil.generic.add_sub
+    libc.src.__support.math.fadd
 )
 
 add_entrypoint_object(
@@ -574,7 +574,7 @@ add_entrypoint_object(
   HDRS
     ../faddl.h
   DEPENDS
-    libc.src.__support.FPUtil.generic.add_sub
+    libc.src.__support.math.faddl
 )
 
 add_entrypoint_object(
@@ -584,8 +584,7 @@ add_entrypoint_object(
   HDRS
     ../faddf128.h
   DEPENDS
-    libc.src.__support.FPUtil.generic.add_sub
-    libc.src.__support.macros.properties.types
+    libc.src.__support.math.faddf128
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/fadd.cpp b/libc/src/math/generic/fadd.cpp
index 60460f8708657..4d7ed1ae8f01f 100644
--- a/libc/src/math/generic/fadd.cpp
+++ b/libc/src/math/generic/fadd.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/fadd.h"
-#include "src/__support/FPUtil/generic/add_sub.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/fadd.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float, fadd, (double x, double y)) {
-  return fputil::generic::add<float>(x, y);
+  return math::fadd(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/faddf128.cpp b/libc/src/math/generic/faddf128.cpp
index 953b1a1ce092e..ac2f623240082 100644
--- a/libc/src/math/generic/faddf128.cpp
+++ b/libc/src/math/generic/faddf128.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/faddf128.h"
-#include "src/__support/FPUtil/generic/add_sub.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/faddf128.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float, faddf128, (float128 x, float128 y)) {
-  return fputil::generic::add<float>(x, y);
+  return math::faddf128(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/faddl.cpp b/libc/src/math/generic/faddl.cpp
index b5c6ab4031c38..3ad2fb58f6b5a 100644
--- a/libc/src/math/generic/faddl.cpp
+++ b/libc/src/math/generic/faddl.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/faddl.h"
-#include "src/__support/FPUtil/generic/add_sub.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/faddl.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float, faddl, (long double x, long double y)) {
-  return fputil::generic::add<float>(x, y);
+  return math::faddl(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 53ad309af4f30..fc1a9e8408151 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -79,6 +79,9 @@ add_fp_unittest(
     libc.src.__support.math.f16sqrt
     libc.src.__support.math.f16sqrtf
     libc.src.__support.math.f16sqrtl
+    libc.src.__support.math.fadd
+    libc.src.__support.math.faddf128
+    libc.src.__support.math.faddl
     libc.src.__support.math.ffma
     libc.src.__support.math.ffmal
     libc.src.__support.math.frexpf
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 7f2f39ac7a285..475aacc97a17f 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -187,6 +187,8 @@ TEST(LlvmLibcSharedMathTest, AllDouble) {
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::canonicalize(&canonicalize_cx,
                                                     &canonicalize_x));
   EXPECT_FP_EQ(0.0, canonicalize_cx);
+
+  EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::fadd(0.0, 0.0));
 }
 
 TEST(LlvmLibcSharedMathTest, AllLongDouble) {
@@ -203,6 +205,8 @@ TEST(LlvmLibcSharedMathTest, AllLongDouble) {
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::canonicalizel(&canonicalizel_cx,
                                                      &canonicalizel_x));
   EXPECT_FP_EQ(0x0p+0L, canonicalizel_cx);
+
+  EXPECT_FP_EQ(0x0p+0L, LIBC_NAMESPACE::shared::faddl(0.0L, 0.0L));
 }
 
 #ifdef LIBC_TYPES_HAS_FLOAT128
@@ -238,6 +242,9 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) {
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::canonicalizef128(&canonicalizef128_cx,
                                                         &canonicalizef128_x));
   EXPECT_FP_EQ(float128(0.0), canonicalizef128_cx);
+
+  EXPECT_FP_EQ(float128(0.0),
+               LIBC_NAMESPACE::shared::faddf128(float128(0.0), float128(0.0)));
 }
 
 #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 b90688af7a1d2..645a33742e936 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3053,6 +3053,34 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_fadd",
+    hdrs = ["src/__support/math/fadd.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+        ":__support_macros_config",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_faddf128",
+    hdrs = ["src/__support/math/faddf128.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+        ":__support_macros_config",
+        ":llvm_libc_types_float128",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_faddl",
+    hdrs = ["src/__support/math/faddl.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+        ":__support_macros_config",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_f16fma",
     hdrs = ["src/__support/math/f16fma.h"],
@@ -4969,11 +4997,26 @@ libc_math_function(name = "fabsf128")
 
 libc_math_function(name = "fabsf16")
 
-libc_math_function(name = "fadd")
+libc_math_function(
+    name = "fadd",
+    additional_deps = [
+        ":__support_math_fadd",
+    ],
+)
 
-libc_math_function(name = "faddl")
+libc_math_function(
+    name = "faddl",
+    additional_deps = [
+        ":__support_math_faddl",
+    ],
+)
 
-libc_math_function(name = "faddf128")
+libc_math_function(
+    name = "faddf128",
+    additional_deps = [
+        ":__support_math_faddf128",
+    ],
+)
 
 libc_math_function(name = "fdim")
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/182185


More information about the libc-commits mailing list