Skip to content

Commit 52a8c5a

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

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
@@ -602,6 +602,14 @@ add_header_library(
602602
libc.src.__support.FPUtil.fma
603603
)
604604

605+
add_header_library(
606+
fmaf
607+
HDRS
608+
fmaf.h
609+
DEPENDS
610+
libc.src.__support.FPUtil.fma
611+
)
612+
605613
add_header_library(
606614
frexpf128
607615
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
@@ -2807,6 +2807,14 @@ libc_support_library(
28072807
],
28082808
)
28092809

2810+
libc_support_library(
2811+
name = "__support_math_fmaf",
2812+
hdrs = ["src/__support/math/fmaf.h"],
2813+
deps = [
2814+
":__support_fputil_fma",
2815+
],
2816+
)
2817+
28102818
libc_support_library(
28112819
name = "__support_math_frexpf128",
28122820
hdrs = ["src/__support/math/frexpf128.h"],
@@ -4028,7 +4036,7 @@ libc_math_function(
40284036
libc_math_function(
40294037
name = "fmaf",
40304038
additional_deps = [
4031-
":__support_fputil_fma",
4039+
":__support_math_fmaf",
40324040
],
40334041
)
40344042

0 commit comments

Comments
 (0)