Skip to content

Commit cc33f16

Browse files
committed
[libc][math] Refactor fma implementation to header-only in src/__support/math folder.
1 parent b9a0a45 commit cc33f16

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
@@ -594,6 +594,14 @@ add_header_library(
594594
libc.src.__support.math.exp10_float16_constants
595595
)
596596

597+
add_header_library(
598+
fma
599+
HDRS
600+
fma.h
601+
DEPENDS
602+
libc.src.__support.FPUtil.fma
603+
)
604+
597605
add_header_library(
598606
frexpf128
599607
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
@@ -4696,7 +4696,7 @@ add_entrypoint_object(
46964696
HDRS
46974697
../fma.h
46984698
DEPENDS
4699-
libc.src.__support.FPUtil.fma
4699+
libc.src.__support.math.fma
47004700
)
47014701

47024702
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
@@ -2799,6 +2799,14 @@ libc_support_library(
27992799
],
28002800
)
28012801

2802+
libc_support_library(
2803+
name = "__support_math_fma",
2804+
hdrs = ["src/__support/math/fma.h"],
2805+
deps = [
2806+
":__support_fputil_fma",
2807+
],
2808+
)
2809+
28022810
libc_support_library(
28032811
name = "__support_math_frexpf128",
28042812
hdrs = ["src/__support/math/frexpf128.h"],
@@ -3101,15 +3109,15 @@ libc_support_library(
31013109
name = "__support_math_expm1f16",
31023110
hdrs = ["src/__support/math/expm1f16.h"],
31033111
deps = [
3112+
":__support_fputil_except_value_utils",
31043113
":__support_fputil_fma",
31053114
":__support_fputil_multiply_add",
31063115
":__support_fputil_nearest_integer",
31073116
":__support_fputil_polyeval",
31083117
":__support_fputil_rounding_mode",
3109-
":__support_fputil_except_value_utils",
31103118
":__support_macros_optimization",
31113119
":__support_macros_properties_cpu_features",
3112-
":__support_math_expxf16_utils"
3120+
":__support_math_expxf16_utils",
31133121
],
31143122
)
31153123

@@ -4013,7 +4021,7 @@ libc_math_function(name = "floorf16")
40134021
libc_math_function(
40144022
name = "fma",
40154023
additional_deps = [
4016-
":__support_fputil_fma",
4024+
":__support_math_fma",
40174025
],
40184026
)
40194027

0 commit comments

Comments
 (0)