Skip to content

Commit cf9e521

Browse files
committed
[libc][math] Refactor fmaf implementation to header-only in src/__support/math folder.
1 parent 8ec6dc1 commit cf9e521

File tree

9 files changed

+73
-8
lines changed

9 files changed

+73
-8
lines changed

libc/shared/math.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include "math/expm1f.h"
5959
#include "math/expm1f16.h"
6060
#include "math/fma.h"
61+
#include "math/fmaf.h"
6162
#include "math/frexpf.h"
6263
#include "math/frexpf128.h"
6364
#include "math/frexpf16.h"

libc/shared/math/fmaf.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- Shared fmaf 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_FMAF_H
10+
#define LLVM_LIBC_SHARED_MATH_FMAF_H
11+
12+
#include "shared/libc_common.h"
13+
#include "src/__support/math/fmaf.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
namespace shared {
17+
18+
using math::fmaf;
19+
20+
} // namespace shared
21+
} // namespace LIBC_NAMESPACE_DECL
22+
23+
#endif // LLVM_LIBC_SHARED_MATH_FMAF_H

libc/src/__support/math/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,14 @@ add_header_library(
601601
libc.src.__support.FPUtil.fma
602602
)
603603

604+
add_header_library(
605+
fmaf
606+
HDRS
607+
fmaf.h
608+
DEPENDS
609+
libc.src.__support.FPUtil.fma
610+
)
611+
604612
add_header_library(
605613
frexpf128
606614
HDRS

libc/src/__support/math/fmaf.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===-- Implementation header for fmaf --------------------------*- 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_FMAF_H
10+
#define LLVM_LIBC_SRC___SUPPORT_MATH_FMAF_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 float fmaf(float x, float y, float z) {
20+
return fputil::fma<float>(x, y, z);
21+
}
22+
23+
} // namespace math
24+
25+
} // namespace LIBC_NAMESPACE_DECL
26+
27+
#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FMAF_H

libc/src/math/generic/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4675,7 +4675,7 @@ add_entrypoint_object(
46754675
HDRS
46764676
../fmaf.h
46774677
DEPENDS
4678-
libc.src.__support.FPUtil.fma
4678+
libc.src.__support.math.fmaf
46794679
)
46804680

46814681
add_entrypoint_object(

libc/src/math/generic/fmaf.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/fmaf.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/fmaf.h"
1411

1512
namespace LIBC_NAMESPACE_DECL {
1613

1714
LLVM_LIBC_FUNCTION(float, fmaf, (float x, float y, float z)) {
18-
return fputil::fma<float>(x, y, z);
15+
return math::fmaf(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
@@ -54,6 +54,7 @@ add_fp_unittest(
5454
libc.src.__support.math.expf
5555
libc.src.__support.math.expf16
5656
libc.src.__support.math.fma
57+
libc.src.__support.math.fmaf
5758
libc.src.__support.math.frexpf
5859
libc.src.__support.math.frexpf128
5960
libc.src.__support.math.frexpf16

libc/test/shared/shared_math_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat) {
6767
EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::expf(0.0f));
6868
EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::exp2f(0.0f));
6969
EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::expm1f(0.0f));
70-
70+
EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::fmaf(0.0f, 0.0f, 0.0f));
7171
EXPECT_FP_EQ_ALL_ROUNDING(0.75f,
7272
LIBC_NAMESPACE::shared::frexpf(24.0f, &exponent));
7373
EXPECT_EQ(exponent, 5);

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2800,6 +2800,14 @@ libc_support_library(
28002800
],
28012801
)
28022802

2803+
libc_support_library(
2804+
name = "__support_math_fmaf",
2805+
hdrs = ["src/__support/math/fmaf.h"],
2806+
deps = [
2807+
":__support_fputil_fma",
2808+
],
2809+
)
2810+
28032811
libc_support_library(
28042812
name = "__support_math_frexpf128",
28052813
hdrs = ["src/__support/math/frexpf128.h"],
@@ -4021,7 +4029,7 @@ libc_math_function(
40214029
libc_math_function(
40224030
name = "fmaf",
40234031
additional_deps = [
4024-
":__support_fputil_fma",
4032+
":__support_math_fmaf",
40254033
],
40264034
)
40274035

0 commit comments

Comments
 (0)