Skip to content

Commit 123e2ab

Browse files
committed
[libc][math] Refactor fmaf16 implementation to header-only in src/__support/math folder.
1 parent 44ee475 commit 123e2ab

File tree

9 files changed

+86
-8
lines changed

9 files changed

+86
-8
lines changed

libc/shared/math.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#include "math/expm1f16.h"
6060
#include "math/fma.h"
6161
#include "math/fmaf.h"
62+
#include "math/fmaf16.h"
6263
#include "math/frexpf.h"
6364
#include "math/frexpf128.h"
6465
#include "math/frexpf16.h"

libc/shared/math/fmaf16.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===-- Shared fmaf16 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_FMAF16_H
10+
#define LLVM_LIBC_SHARED_MATH_FMAF16_H
11+
12+
#include "include/llvm-libc-macros/float16-macros.h"
13+
#include "shared/libc_common.h"
14+
15+
#ifdef LIBC_TYPES_HAS_FLOAT16
16+
17+
#include "src/__support/math/fmaf16.h"
18+
19+
namespace LIBC_NAMESPACE_DECL {
20+
namespace shared {
21+
22+
using math::fmaf16;
23+
24+
} // namespace shared
25+
} // namespace LIBC_NAMESPACE_DECL
26+
27+
#endif // LIBC_TYPES_HAS_FLOAT16
28+
29+
#endif // LLVM_LIBC_SHARED_MATH_FMAF16_H

libc/src/__support/math/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,14 @@ add_header_library(
609609
libc.src.__support.FPUtil.fma
610610
)
611611

612+
add_header_library(
613+
fmaf16
614+
HDRS
615+
fmaf16.h
616+
DEPENDS
617+
libc.src.__support.FPUtil.fma
618+
)
619+
612620
add_header_library(
613621
frexpf128
614622
HDRS

libc/src/__support/math/fmaf16.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===-- Implementation header for fmaf16 ------------------------*- 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_FMAF16_H
10+
#define LLVM_LIBC_SRC___SUPPORT_MATH_FMAF16_H
11+
12+
#include "include/llvm-libc-macros/float16-macros.h"
13+
14+
#ifdef LIBC_TYPES_HAS_FLOAT16
15+
16+
#include "src/__support/FPUtil/FMA.h"
17+
#include "src/__support/macros/config.h"
18+
19+
namespace LIBC_NAMESPACE_DECL {
20+
21+
namespace math {
22+
23+
LIBC_INLINE static float16 fmaf16(float16 x, float16 y, float16 z) {
24+
return fputil::fma<float16>(x, y, z);
25+
}
26+
27+
} // namespace math
28+
29+
} // namespace LIBC_NAMESPACE_DECL
30+
31+
#endif // LIBC_TYPES_HAS_FLOAT16
32+
33+
#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FMAF16_H

libc/src/math/generic/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4711,8 +4711,7 @@ add_entrypoint_object(
47114711
HDRS
47124712
../fmaf16.h
47134713
DEPENDS
4714-
libc.src.__support.FPUtil.fma
4715-
libc.src.__support.macros.properties.types
4714+
libc.src.__support.math.fmaf16
47164715
)
47174716

47184717
add_entrypoint_object(

libc/src/math/generic/fmaf16.cpp

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

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

1412
namespace LIBC_NAMESPACE_DECL {
1513

1614
LLVM_LIBC_FUNCTION(float16, fmaf16, (float16 x, float16 y, float16 z)) {
17-
return fputil::fma<float16>(x, y, z);
15+
return math::fmaf16(x, y, z);
1816
}
1917

2018
} // namespace LIBC_NAMESPACE_DECL

libc/test/shared/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ add_fp_unittest(
5555
libc.src.__support.math.expf16
5656
libc.src.__support.math.fma
5757
libc.src.__support.math.fmaf
58+
libc.src.__support.math.fmaf16
5859
libc.src.__support.math.frexpf
5960
libc.src.__support.math.frexpf128
6061
libc.src.__support.math.frexpf16

libc/test/shared/shared_math_test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
3232
EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::exp2m1f16(0.0f16));
3333
EXPECT_FP_EQ(0x1p+0f16, LIBC_NAMESPACE::shared::expf16(0.0f16));
3434
EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::expm1f16(0.0f16));
35-
35+
EXPECT_FP_EQ(0x0p+0f16,
36+
LIBC_NAMESPACE::shared::fmaf16(0.0f16, 0.0f16, 0.0f16));
3637
ASSERT_FP_EQ(float16(8 << 5), LIBC_NAMESPACE::shared::ldexpf16(8.0f16, 5));
3738
ASSERT_FP_EQ(float16(-1 * (8 << 5)),
3839
LIBC_NAMESPACE::shared::ldexpf16(-8.0f16, 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_fmaf16",
2812+
hdrs = ["src/__support/math/fmaf16.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"],
@@ -4031,7 +4039,7 @@ libc_math_function(
40314039
libc_math_function(
40324040
name = "fmaf16",
40334041
additional_deps = [
4034-
":__support_fputil_fma",
4042+
":__support_math_fmaf16",
40354043
],
40364044
)
40374045

0 commit comments

Comments
 (0)