[libc-commits] [libc] [llvm] [libc][math] Refactor f16fma to header only (PR #176244)
Islam Imad via libc-commits
libc-commits at lists.llvm.org
Thu Jan 15 13:42:07 PST 2026
https://github.com/Islam-Imad updated https://github.com/llvm/llvm-project/pull/176244
>From ef68a183b6a0564c7489e4233e76cf36315dcd88 Mon Sep 17 00:00:00 2001
From: Islam-Imad <islamimad404 at gmail.com>
Date: Thu, 15 Jan 2026 20:20:12 +0200
Subject: [PATCH 1/3] refactor f16fma to header only closes #175320
Part of #175313
---
libc/shared/math.h | 1 +
libc/shared/math/f16fma.h | 31 +++++++++++++++++
libc/src/__support/math/CMakeLists.txt | 11 +++++++
libc/src/__support/math/f16fma.h | 33 +++++++++++++++++++
libc/src/math/generic/CMakeLists.txt | 3 +-
libc/src/math/generic/f16fma.cpp | 6 ++--
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 2 ++
.../llvm-project-overlay/libc/BUILD.bazel | 14 +++++++-
9 files changed, 95 insertions(+), 7 deletions(-)
create mode 100644 libc/shared/math/f16fma.h
create mode 100644 libc/src/__support/math/f16fma.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 287f3aea1565a..43d8e1d32f391 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -57,6 +57,7 @@
#include "math/expm1.h"
#include "math/expm1f.h"
#include "math/expm1f16.h"
+#include "math/f16fma.h"
#include "math/frexpf.h"
#include "math/frexpf128.h"
#include "math/frexpf16.h"
diff --git a/libc/shared/math/f16fma.h b/libc/shared/math/f16fma.h
new file mode 100644
index 0000000000000..e473bf96fc331
--- /dev/null
+++ b/libc/shared/math/f16fma.h
@@ -0,0 +1,31 @@
+//===-- Shared f16fma 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_F16FMA_H
+#define LLVM_LIBC_SHARED_MATH_F16FMA_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+#include "shared/libc_common.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/math/f16fma.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace shared {
+
+using math::f16fma;
+
+} // namespace shared
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_MATH_F16FMA_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 4139a1b1d3444..a1fdf37d57fd1 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -631,6 +631,17 @@ add_header_library(
libc.src.__support.FPUtil.manipulation_functions
)
+add_header_library(
+ f16fma
+ HDRS
+ f16fma.h
+ DEPENDS
+ libc.src.__support.FPUtil.fma
+ libc.src.__support.macros.config
+ libc.src.__support.macros.properties.types
+ libc.include.llvm-libc-macros.float16_macros
+)
+
add_header_library(
ilogbf16
HDRS
diff --git a/libc/src/__support/math/f16fma.h b/libc/src/__support/math/f16fma.h
new file mode 100644
index 0000000000000..f7bb2fe33e963
--- /dev/null
+++ b/libc/src/__support/math/f16fma.h
@@ -0,0 +1,33 @@
+//===-- Implementation header for f16fma ------------------------*- 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_F16FMA_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_F16FMA_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 f16fma(double x, double y, 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_F16FMA_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 6de4cf376bf02..505abfe695e4d 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -5136,8 +5136,7 @@ add_entrypoint_object(
HDRS
../f16fma.h
DEPENDS
- libc.src.__support.macros.properties.types
- libc.src.__support.FPUtil.fma
+ libc.src.__support.math.f16fma
)
add_entrypoint_object(
diff --git a/libc/src/math/generic/f16fma.cpp b/libc/src/math/generic/f16fma.cpp
index 70314b5420fad..e6673e71ad77e 100644
--- a/libc/src/math/generic/f16fma.cpp
+++ b/libc/src/math/generic/f16fma.cpp
@@ -7,14 +7,12 @@
//===----------------------------------------------------------------------===//
#include "src/math/f16fma.h"
-#include "src/__support/FPUtil/FMA.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/f16fma.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(float16, f16fma, (double x, double y, double z)) {
- return fputil::fma<float16>(x, y, z);
+ return math::f16fma(x, y, z);
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index c5955ecfd54cb..b8ed33f325891 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -53,6 +53,7 @@ add_fp_unittest(
libc.src.__support.math.exp10f16
libc.src.__support.math.expf
libc.src.__support.math.expf16
+ libc.src.__support.math.f16fma
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 20a98a1a9138c..078732afc7252 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -33,6 +33,8 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
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(float16(10.0), LIBC_NAMESPACE::shared::f16fma(2.0, 3.0, 4.0));
+
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 c03c21b834895..e475725864b33 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2804,6 +2804,17 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_math_f16fma",
+ hdrs = ["src/__support/math/f16fma.h"],
+ deps = [
+ ":__support_fputil_fma",
+ ":__support_macros_config",
+ ":__support_macros_properties_types",
+ ":llvm_libc_macros_float16_macros",
+ ],
+)
+
libc_support_library(
name = "__support_math_frexpf128",
hdrs = ["src/__support/math/frexpf128.h"],
@@ -2823,6 +2834,7 @@ libc_support_library(
],
)
+
libc_support_library(
name = "__support_math_frexpf16",
hdrs = ["src/__support/math/frexpf16.h"],
@@ -3923,7 +3935,7 @@ libc_math_function(name = "f16divl")
libc_math_function(
name = "f16fma",
additional_deps = [
- ":__support_fputil_fma",
+ ":__support_math_f16fma",
],
)
>From ef13a2626dbc13827d7f05069f12746b4f562959 Mon Sep 17 00:00:00 2001
From: Islam-Imad <islamimad404 at gmail.com>
Date: Thu, 15 Jan 2026 23:33:01 +0200
Subject: [PATCH 2/3] format bazel
---
utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 1 -
1 file changed, 1 deletion(-)
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index e475725864b33..cabd347a4d26c 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2834,7 +2834,6 @@ libc_support_library(
],
)
-
libc_support_library(
name = "__support_math_frexpf16",
hdrs = ["src/__support/math/frexpf16.h"],
>From 6e75b51262efc3f9dc502ecce101ebd86e22ff3a Mon Sep 17 00:00:00 2001
From: Islam-Imad <islamimad404 at gmail.com>
Date: Thu, 15 Jan 2026 23:41:50 +0200
Subject: [PATCH 3/3] fix bazel build faliure
---
utils/bazel/llvm-project-overlay/clang/BUILD.bazel | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/utils/bazel/llvm-project-overlay/clang/BUILD.bazel b/utils/bazel/llvm-project-overlay/clang/BUILD.bazel
index 9d698d48f595d..b9f022207da65 100644
--- a/utils/bazel/llvm-project-overlay/clang/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/clang/BUILD.bazel
@@ -1807,8 +1807,8 @@ cc_library(
"//llvm:AllTargetsAsmParsers",
"//llvm:AllTargetsCodeGens",
"//llvm:Core",
- "//llvm:JITLink",
"//llvm:ExecutionEngine",
+ "//llvm:JITLink",
"//llvm:MC",
"//llvm:Option",
"//llvm:OrcDebugging",
More information about the libc-commits
mailing list