Skip to content

Commit 55adc0c

Browse files
committed
[libc][math] Refactor fma implementation to header-only in src/__support/math folder.
1 parent 2c713ce commit 55adc0c

File tree

9 files changed

+75
-9
lines changed

9 files changed

+75
-9
lines changed

libc/shared/math.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#include "math/expm1.h"
5858
#include "math/expm1f.h"
5959
#include "math/expm1f16.h"
60+
#include "math/fma.h"
6061
#include "math/frexpf.h"
6162
#include "math/frexpf128.h"
6263
#include "math/frexpf16.h"

libc/shared/math/fma.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- Shared fma function -------------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SHARED_MATH_FMA_H
10+
#define LLVM_LIBC_SHARED_MATH_FMA_H
11+
12+
#include "shared/libc_common.h"
13+
#include "src/__support/math/fma.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
namespace shared {
17+
18+
using math::fma;
19+
20+
} // namespace shared
21+
} // namespace LIBC_NAMESPACE_DECL
22+
23+
#endif // LLVM_LIBC_SHARED_MATH_FMA_H

libc/src/__support/math/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,14 @@ add_header_library(
593593
libc.src.__support.math.exp10_float16_constants
594594
)
595595

596+
add_header_library(
597+
fma
598+
HDRS
599+
fma.h
600+
DEPENDS
601+
libc.src.__support.FPUtil.fma
602+
)
603+
596604
add_header_library(
597605
frexpf128
598606
HDRS

libc/src/__support/math/fma.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===-- Implementation header for fma ---------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_FMA_H
10+
#define LLVM_LIBC_SRC___SUPPORT_MATH_FMA_H
11+
12+
#include "src/__support/FPUtil/FMA.h"
13+
#include "src/__support/macros/config.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
namespace math {
18+
19+
LIBC_INLINE static double fma(double x, double y, double z) {
20+
return fputil::fma<double>(x, y, z);
21+
}
22+
23+
} // namespace math
24+
25+
} // namespace LIBC_NAMESPACE_DECL
26+
27+
#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FMA_H

libc/src/math/generic/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4722,7 +4722,7 @@ add_entrypoint_object(
47224722
HDRS
47234723
../fma.h
47244724
DEPENDS
4725-
libc.src.__support.FPUtil.fma
4725+
libc.src.__support.math.fma
47264726
)
47274727

47284728
add_entrypoint_object(

libc/src/math/generic/fma.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/math/fma.h"
10-
#include "src/__support/common.h"
11-
12-
#include "src/__support/FPUtil/FMA.h"
13-
#include "src/__support/macros/config.h"
10+
#include "src/__support/math/fma.h"
1411

1512
namespace LIBC_NAMESPACE_DECL {
1613

1714
LLVM_LIBC_FUNCTION(double, fma, (double x, double y, double z)) {
18-
return fputil::fma<double>(x, y, z);
15+
return math::fma(x, y, z);
1916
}
2017

2118
} // namespace LIBC_NAMESPACE_DECL

libc/test/shared/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ add_fp_unittest(
5353
libc.src.__support.math.exp10f16
5454
libc.src.__support.math.expf
5555
libc.src.__support.math.expf16
56+
libc.src.__support.math.fma
5657
libc.src.__support.math.frexpf
5758
libc.src.__support.math.frexpf128
5859
libc.src.__support.math.frexpf16

libc/test/shared/shared_math_test.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ TEST(LlvmLibcSharedMathTest, AllDouble) {
9090
EXPECT_FP_EQ(0x1p+0, LIBC_NAMESPACE::shared::exp2(0.0));
9191
EXPECT_FP_EQ(0x1p+0, LIBC_NAMESPACE::shared::exp10(0.0));
9292
EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::expm1(0.0));
93+
EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::fma(0.0, 0.0, 0.0));
9394
}
9495

9596
#ifdef LIBC_TYPES_HAS_FLOAT128

utils/bazel/llvm-project-overlay/libc/BUILD.bazel

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2791,6 +2791,14 @@ libc_support_library(
27912791
],
27922792
)
27932793

2794+
libc_support_library(
2795+
name = "__support_math_fma",
2796+
hdrs = ["src/__support/math/fma.h"],
2797+
deps = [
2798+
":__support_fputil_fma",
2799+
],
2800+
)
2801+
27942802
libc_support_library(
27952803
name = "__support_math_frexpf128",
27962804
hdrs = ["src/__support/math/frexpf128.h"],
@@ -3093,15 +3101,15 @@ libc_support_library(
30933101
name = "__support_math_expm1f16",
30943102
hdrs = ["src/__support/math/expm1f16.h"],
30953103
deps = [
3104+
":__support_fputil_except_value_utils",
30963105
":__support_fputil_fma",
30973106
":__support_fputil_multiply_add",
30983107
":__support_fputil_nearest_integer",
30993108
":__support_fputil_polyeval",
31003109
":__support_fputil_rounding_mode",
3101-
":__support_fputil_except_value_utils",
31023110
":__support_macros_optimization",
31033111
":__support_macros_properties_cpu_features",
3104-
":__support_math_expxf16_utils"
3112+
":__support_math_expxf16_utils",
31053113
],
31063114
)
31073115

@@ -4001,7 +4009,7 @@ libc_math_function(name = "floorf16")
40014009
libc_math_function(
40024010
name = "fma",
40034011
additional_deps = [
4004-
":__support_fputil_fma",
4012+
":__support_math_fma",
40054013
],
40064014
)
40074015

0 commit comments

Comments
 (0)