[libc-commits] [libc] [llvm] [libc][math] Refactor f16fmal to header-only (PR #176576)

via libc-commits libc-commits at lists.llvm.org
Mon Jan 19 09:34:34 PST 2026


https://github.com/crisiumnih updated https://github.com/llvm/llvm-project/pull/176576

>From f878bda30191613347205d611dfe590cd096ca26 Mon Sep 17 00:00:00 2001
From: crisiumnih <fasfasmag at proton.me>
Date: Sat, 17 Jan 2026 22:05:04 +0530
Subject: [PATCH 1/2] [libc][math] Refactor f16fmal to header-only

---
 libc/shared/math.h                            |  1 +
 libc/shared/math/f16fmal.h                    | 31 +++++++++++++++++
 libc/src/__support/math/CMakeLists.txt        | 10 ++++++
 libc/src/__support/math/f16fmal.h             | 33 +++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt          |  3 +-
 libc/src/math/generic/f16fmal.cpp             |  6 ++--
 libc/test/shared/CMakeLists.txt               |  1 +
 libc/test/shared/shared_math_test.cpp         |  2 ++
 .../llvm-project-overlay/libc/BUILD.bazel     | 13 +++++++-
 9 files changed, 93 insertions(+), 7 deletions(-)
 create mode 100644 libc/shared/math/f16fmal.h
 create mode 100644 libc/src/__support/math/f16fmal.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index d58238703701d..cb5d0dee07f33 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/f16fmal.h"
 #include "math/frexpf.h"
 #include "math/frexpf128.h"
 #include "math/frexpf16.h"
diff --git a/libc/shared/math/f16fmal.h b/libc/shared/math/f16fmal.h
new file mode 100644
index 0000000000000..a50988d847158
--- /dev/null
+++ b/libc/shared/math/f16fmal.h
@@ -0,0 +1,31 @@
+//===-- Shared f16fmal 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_F16FMAL_H
+#define LLVM_LIBC_SHARED_MATH_F16FMAL_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+#include "shared/libc_common.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/math/f16fmal.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace shared {
+
+using math::f16fmal;
+
+} // namespace shared
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_MATH_F16FMAL_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index bf1c4463b8066..d8911d8f3850e 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -648,6 +648,16 @@ add_header_library(
     libc.include.llvm-libc-macros.float16_macros
 )
 
+add_header_library(
+  f16fmal
+  HDRS
+    f16fmal.h
+  DEPENDS
+    libc.src.__support.macros.config
+    libc.src.__support.FPUtil.fma
+    libc.include.llvm-libc-macros.float16_macros
+)
+
 add_header_library(
   ilogbf16
   HDRS
diff --git a/libc/src/__support/math/f16fmal.h b/libc/src/__support/math/f16fmal.h
new file mode 100644
index 0000000000000..98b707fb1b41d
--- /dev/null
+++ b/libc/src/__support/math/f16fmal.h
@@ -0,0 +1,33 @@
+//===-- Implementation header for f16fmal -----------------------*- 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_F16FMAL_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_F16FMAL_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/FPUtil/FMA.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static float16 f16fmal(long double x, long double y, long double z) {
+  return fputil::fma<float16>(x, y, z);
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_F16FMAL_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index b578d1805f2a8..f3e896fec4ae4 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5156,8 +5156,7 @@ add_entrypoint_object(
   HDRS
     ../f16fmal.h
   DEPENDS
-    libc.src.__support.macros.properties.types
-    libc.src.__support.FPUtil.fma
+    libc.src.__support.math.f16fmal
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/f16fmal.cpp b/libc/src/math/generic/f16fmal.cpp
index 4f2bf92af5bbb..afc23392bc9ee 100644
--- a/libc/src/math/generic/f16fmal.cpp
+++ b/libc/src/math/generic/f16fmal.cpp
@@ -7,15 +7,13 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/f16fmal.h"
-#include "src/__support/FPUtil/FMA.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/f16fmal.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float16, f16fmal,
                    (long double x, long double y, long double z)) {
-  return fputil::fma<float16>(x, y, z);
+  return math::f16fmal(x, y, z);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index cfc2eda1aaec9..3d82efda5b252 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.f16fmal
     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 88e6ecc98a0a7..8923405b9a8e0 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -35,6 +35,8 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
 
   EXPECT_FP_EQ(float16(10.0), LIBC_NAMESPACE::shared::f16fma(2.0, 3.0, 4.0));
 
+  EXPECT_FP_EQ(float16(10.0), LIBC_NAMESPACE::shared::f16fmal(2.0L, 3.0L, 4.0L));
+
   ASSERT_FP_EQ(float16(8 << 5), LIBC_NAMESPACE::shared::ldexpf16(8.0f16, 5));
   ASSERT_FP_EQ(float16(-1 * (8 << 5)),
                LIBC_NAMESPACE::shared::ldexpf16(-8.0f16, 5));
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 90b1da0e3376e..d12ad9379bbf2 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2814,6 +2814,17 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_f16fmal",
+    hdrs = ["src/__support/math/f16fmal.h"],
+    deps = [
+        ":__support_common",
+        ":__support_fputil_fma",
+        ":__support_macros_config",
+        ":llvm_libc_macros_float16_macros",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_frexpf128",
     hdrs = ["src/__support/math/frexpf128.h"],
@@ -4048,7 +4059,7 @@ libc_math_function(
 libc_math_function(
     name = "f16fmal",
     additional_deps = [
-        ":__support_fputil_fma",
+        ":__support_math_f16fmal",
     ],
 )
 

>From 2697c432ff1b7e83d49b82b543483f064add177b Mon Sep 17 00:00:00 2001
From: crisiumnih <fasfasmag at proton.me>
Date: Mon, 19 Jan 2026 23:02:15 +0530
Subject: [PATCH 2/2] formatting

---
 libc/src/__support/math/f16fmal.h     | 3 ++-
 libc/test/shared/shared_math_test.cpp | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/libc/src/__support/math/f16fmal.h b/libc/src/__support/math/f16fmal.h
index 98b707fb1b41d..6e9c4b9af13f4 100644
--- a/libc/src/__support/math/f16fmal.h
+++ b/libc/src/__support/math/f16fmal.h
@@ -20,7 +20,8 @@ namespace LIBC_NAMESPACE_DECL {
 
 namespace math {
 
-LIBC_INLINE static float16 f16fmal(long double x, long double y, long double z) {
+LIBC_INLINE static float16 f16fmal(long double x, long double y,
+                                   long double z) {
   return fputil::fma<float16>(x, y, z);
 }
 
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 8923405b9a8e0..9fa35fb297dd8 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -35,7 +35,8 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
 
   EXPECT_FP_EQ(float16(10.0), LIBC_NAMESPACE::shared::f16fma(2.0, 3.0, 4.0));
 
-  EXPECT_FP_EQ(float16(10.0), LIBC_NAMESPACE::shared::f16fmal(2.0L, 3.0L, 4.0L));
+  EXPECT_FP_EQ(float16(10.0),
+               LIBC_NAMESPACE::shared::f16fmal(2.0L, 3.0L, 4.0L));
 
   ASSERT_FP_EQ(float16(8 << 5), LIBC_NAMESPACE::shared::ldexpf16(8.0f16, 5));
   ASSERT_FP_EQ(float16(-1 * (8 << 5)),



More information about the libc-commits mailing list