[libc-commits] [libc] [llvm] [libc][math] Refactor f16fmaf128 to Header Only (PR #177850)

via libc-commits libc-commits at lists.llvm.org
Sun Jan 25 03:22:41 PST 2026


https://github.com/Sukumarsawant created https://github.com/llvm/llvm-project/pull/177850


PR for #175318 

In preparation for:
https://discourse.llvm.org/t/rfc-make-clang-builtin-math-functions-constexpr-with-llvm-libc-to-support-c-23-constexpr-math-functions/86450

please do check for any edits


>From 4ef15ef01b0e8b0ff4bd723ec55741229938d170 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sun, 25 Jan 2026 02:37:26 +0530
Subject: [PATCH 1/5] new files added

---
 libc/shared/math/f16fmaf128.h        | 30 ++++++++++++++++++++++++
 libc/src/__support/math/f16fmaf128.h | 34 ++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+)
 create mode 100644 libc/shared/math/f16fmaf128.h
 create mode 100644 libc/src/__support/math/f16fmaf128.h

diff --git a/libc/shared/math/f16fmaf128.h b/libc/shared/math/f16fmaf128.h
new file mode 100644
index 0000000000000..f3dc9f07db550
--- /dev/null
+++ b/libc/shared/math/f16fmaf128.h
@@ -0,0 +1,30 @@
+//===-- Shared f16fmaf128 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_F16FMAF128_H
+#define LLVM_LIBC_SHARED_MATH_F16FMAF128_H
+
+#include "include/llvm-libc-macros/float128-macros.h"
+#include "shared/libc_common.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+
+#include "src/__support/math/f16fmaf128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::f16fmaf128;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SHARED_MATH_F16FMAF128_H
diff --git a/libc/src/__support/math/f16fmaf128.h b/libc/src/__support/math/f16fmaf128.h
new file mode 100644
index 0000000000000..a341e87eede48
--- /dev/null
+++ b/libc/src/__support/math/f16fmaf128.h
@@ -0,0 +1,34 @@
+//===-- Implementation header for f16fmaf128 ------------------------*- 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_F16FMAF128_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_F16FMAF128_H
+
+#include "include/llvm-libc-macros/float128-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "src/__support/FPUtil/FMA.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static float16 f16fmaf128(float128 x, float128 y, float128 z) {
+  return fputil::fma<float16>(x, y, z);
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_F16FMAF128_H

>From a01ef8ee0824be730324434bfd134b41e27664d3 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sun, 25 Jan 2026 02:37:55 +0530
Subject: [PATCH 2/5] edited files

---
 libc/shared/math.h                                |  1 +
 libc/src/__support/math/CMakeLists.txt            | 11 +++++++++++
 libc/src/math/generic/CMakeLists.txt              |  3 +--
 libc/src/math/generic/f16fmaf128.cpp              |  7 +++----
 libc/test/shared/CMakeLists.txt                   |  1 +
 libc/test/shared/shared_math_test.cpp             |  5 +++++
 utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 13 ++++++++++++-
 7 files changed, 34 insertions(+), 7 deletions(-)

diff --git a/libc/shared/math.h b/libc/shared/math.h
index bad2b07ecb993..ffd7aabfa5303 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -59,6 +59,7 @@
 #include "math/expm1f.h"
 #include "math/expm1f16.h"
 #include "math/f16fma.h"
+#include "math/f16fmaf128.h"
 #include "math/frexpf.h"
 #include "math/frexpf128.h"
 #include "math/frexpf16.h"
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index bcc5b1024b8da..e0966d5edc2af 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -648,6 +648,17 @@ add_header_library(
     libc.include.llvm-libc-macros.float16_macros
 )
 
+add_header_library(
+  f16fmaf128
+  HDRS
+    f16fmaf128.h
+  DEPENDS
+    libc.src.__support.FPUtil.fma
+    libc.src.__support.common
+    libc.src.__support.macros.config
+    libc.include.llvm-libc-macros.float128_macros
+)
+
 add_header_library(
   ilogbf16
   HDRS
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 6e58434415ead..af64aea9cc581 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5141,8 +5141,7 @@ add_entrypoint_object(
   HDRS
     ../f16fmaf128.h
   DEPENDS
-    libc.src.__support.macros.properties.types
-    libc.src.__support.FPUtil.fma
+    libc.src.__support.math.f16fmaf128.h
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/f16fmaf128.cpp b/libc/src/math/generic/f16fmaf128.cpp
index 5c4823c47e79f..75e52a139779b 100644
--- a/libc/src/math/generic/f16fmaf128.cpp
+++ b/libc/src/math/generic/f16fmaf128.cpp
@@ -7,14 +7,13 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/f16fmaf128.h"
-#include "src/__support/FPUtil/FMA.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/f16fmaf128.h"
+
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float16, f16fmaf128, (float128 x, float128 y, float128 z)) {
-  return fputil::fma<float16>(x, y, z);
+  return math::f16fmaf128(x, y, z);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 3fc36ed0c2198..acb078dd8aa33 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -55,6 +55,7 @@ add_fp_unittest(
     libc.src.__support.math.expf
     libc.src.__support.math.expf16
     libc.src.__support.math.f16fma
+    libc.src.__support.math.f16fmaf128
     libc.src.__support.math.frexpf
     libc.src.__support.math.frexpf128
     libc.src.__support.math.frexpf16
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 45ba79de87226..a956412360ad6 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -34,6 +34,10 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
   EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::expm1f16(0.0f16));
 
   EXPECT_FP_EQ(float16(10.0), LIBC_NAMESPACE::shared::f16fma(2.0, 3.0, 4.0));
+  
+  #if defined(LIBC_TYPES_HAS_FLOAT128)
+  EXPECT_FP_EQ(float16(10.0), LIBC_NAMESPACE::shared::f16fmaf128(2.0, 3.0, 4.0));
+  #endif
 
   ASSERT_FP_EQ(float16(8 << 5), LIBC_NAMESPACE::shared::ldexpf16(8.0f16, 5));
   ASSERT_FP_EQ(float16(-1 * (8 << 5)),
@@ -131,6 +135,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) {
   ASSERT_FP_EQ(float128(-1 * (8 << 5)),
                LIBC_NAMESPACE::shared::ldexpf128(float128(-8), 5));
   EXPECT_FP_EQ(float128(0.0), LIBC_NAMESPACE::shared::logbf128(float128(1.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 2819824a11857..03ca2f2840187 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2877,6 +2877,17 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_f16fmaf128",
+    hdrs = ["src/__support/math/f16fmaf128.h"],
+    deps = [
+        ":__support_fputil_fma",
+        ":__support_macros_config",
+        ":__support_common",
+        ":llvm_libc_macros_float128_macros",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_frexpf128",
     hdrs = ["src/__support/math/frexpf128.h"],
@@ -4154,7 +4165,7 @@ libc_math_function(
 libc_math_function(
     name = "f16fmaf128",
     additional_deps = [
-        ":__support_fputil_fma",
+        ":__support_math_f16fmaf128",
     ],
 )
 

>From 0c6dacac93e6662f31bd114f3281246d95a9f476 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sun, 25 Jan 2026 11:00:23 +0530
Subject: [PATCH 3/5] fixed .h error

---
 libc/src/math/generic/CMakeLists.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index af64aea9cc581..b6cde300bc836 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5141,7 +5141,7 @@ add_entrypoint_object(
   HDRS
     ../f16fmaf128.h
   DEPENDS
-    libc.src.__support.math.f16fmaf128.h
+    libc.src.__support.math.f16fmaf128
 )
 
 add_entrypoint_object(

>From 794b49e93cfd75b4900c7d4afb8b5ad83c9e197a Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sun, 25 Jan 2026 11:31:43 +0530
Subject: [PATCH 4/5] bazel edited

---
 utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 1 +
 1 file changed, 1 insertion(+)

diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 03ca2f2840187..200b8b1c646f7 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -4166,6 +4166,7 @@ libc_math_function(
     name = "f16fmaf128",
     additional_deps = [
         ":__support_math_f16fmaf128",
+        ":llvm_libc_types_float128",
     ],
 )
 

>From 64b311723ec04a3a3f8098213b81d414d5d7cafe Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sun, 25 Jan 2026 14:11:02 +0530
Subject: [PATCH 5/5] changed to float128

---
 libc/shared/math/f16fmaf128.h        | 2 +-
 libc/src/__support/math/f16fmaf128.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/libc/shared/math/f16fmaf128.h b/libc/shared/math/f16fmaf128.h
index f3dc9f07db550..31c99d3ff5e22 100644
--- a/libc/shared/math/f16fmaf128.h
+++ b/libc/shared/math/f16fmaf128.h
@@ -9,7 +9,7 @@
 #ifndef LLVM_LIBC_SHARED_MATH_F16FMAF128_H
 #define LLVM_LIBC_SHARED_MATH_F16FMAF128_H
 
-#include "include/llvm-libc-macros/float128-macros.h"
+#include "include/llvm-libc-types/float128.h"
 #include "shared/libc_common.h"
 
 #ifdef LIBC_TYPES_HAS_FLOAT128
diff --git a/libc/src/__support/math/f16fmaf128.h b/libc/src/__support/math/f16fmaf128.h
index a341e87eede48..605c865f580aa 100644
--- a/libc/src/__support/math/f16fmaf128.h
+++ b/libc/src/__support/math/f16fmaf128.h
@@ -9,7 +9,7 @@
 #ifndef LLVM_LIBC_SRC___SUPPORT_MATH_F16FMAF128_H
 #define LLVM_LIBC_SRC___SUPPORT_MATH_F16FMAF128_H
 
-#include "include/llvm-libc-macros/float128-macros.h"
+#include "include/llvm-libc-types/float128.h"
 
 #ifdef LIBC_TYPES_HAS_FLOAT128
 



More information about the libc-commits mailing list