[libc][math] Refactor fma implementation to header-only in src/__support/math folder. (#163968)

Part of #147386

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
This commit is contained in:
Muhammad Bassiouni 2026-03-17 21:24:27 +02:00 committed by GitHub
parent 3d0e7e04c8
commit d0d1f0b7af
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 74 additions and 7 deletions

View File

@ -137,6 +137,7 @@
#include "math/floorf128.h"
#include "math/floorf16.h"
#include "math/floorl.h"
#include "math/fma.h"
#include "math/fmabf16.h"
#include "math/fmax.h"
#include "math/fmaxbf16.h"

23
libc/shared/math/fma.h Normal file
View File

@ -0,0 +1,23 @@
//===-- Shared fma 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_FMA_H
#define LLVM_LIBC_SHARED_MATH_FMA_H
#include "shared/libc_common.h"
#include "src/__support/math/fma.h"
namespace LIBC_NAMESPACE_DECL {
namespace shared {
using math::fma;
} // namespace shared
} // namespace LIBC_NAMESPACE_DECL
#endif // LLVM_LIBC_SHARED_MATH_FMA_H

View File

@ -1320,6 +1320,15 @@ add_header_library(
libc.src.__support.macros.properties.types
)
add_header_library(
fma
HDRS
fma.h
DEPENDS
libc.src.__support.FPUtil.fma
libc.src.__support.macros.config
)
add_header_library(
ffma
HDRS

View File

@ -0,0 +1,27 @@
//===-- Implementation header for fma ---------------------------*- 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_FMA_H
#define LLVM_LIBC_SRC___SUPPORT_MATH_FMA_H
#include "src/__support/FPUtil/FMA.h"
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
namespace math {
LIBC_INLINE double fma(double x, double y, double z) {
return fputil::fma<double>(x, y, z);
}
} // namespace math
} // namespace LIBC_NAMESPACE_DECL
#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FMA_H

View File

@ -4399,7 +4399,7 @@ add_entrypoint_object(
HDRS
../fma.h
DEPENDS
libc.src.__support.FPUtil.fma
libc.src.__support.math.fma
)
add_entrypoint_object(

View File

@ -7,15 +7,12 @@
//===----------------------------------------------------------------------===//
#include "src/math/fma.h"
#include "src/__support/common.h"
#include "src/__support/FPUtil/FMA.h"
#include "src/__support/macros/config.h"
#include "src/__support/math/fma.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(double, fma, (double x, double y, double z)) {
return fputil::fma<double>(x, y, z);
return math::fma(x, y, z);
}
} // namespace LIBC_NAMESPACE_DECL

View File

@ -134,6 +134,7 @@ add_fp_unittest(
libc.src.__support.math.floorf128
libc.src.__support.math.floorf16
libc.src.__support.math.floorl
libc.src.__support.math.fma
libc.src.__support.math.fmabf16
libc.src.__support.math.fmax
libc.src.__support.math.fmaxbf16

View File

@ -247,6 +247,7 @@ TEST(LlvmLibcSharedMathTest, AllDouble) {
EXPECT_FP_EQ(0x1p+0, LIBC_NAMESPACE::shared::exp2(0.0));
EXPECT_FP_EQ(0x1p+0, LIBC_NAMESPACE::shared::exp10(0.0));
EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::expm1(0.0));
EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::fma(0.0, 0.0, 0.0));
EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::ffma(0.0, 0.0, 0.0));
EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::hypot(0.0, 0.0));
EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::fsqrt(0.0));

View File

@ -4311,6 +4311,14 @@ libc_support_library(
],
)
libc_support_library(
name = "__support_math_fma",
hdrs = ["src/__support/math/fma.h"],
deps = [
":__support_fputil_fma",
],
)
libc_support_library(
name = "__support_math_frexpf128",
hdrs = ["src/__support/math/frexpf128.h"],
@ -7090,7 +7098,7 @@ libc_math_function(
libc_math_function(
name = "fmaf16",
additional_deps = [
":__support_fputil_fma",
":__support_math_fma",
],
)