Skip to content

Commit 074218d

Browse files
authored
[libc][math] Refactor ldexpf16 implementation to header-only in src/__support/math folder. (#147901)
Part of #147386 in preparation for: https://discourse.llvm.org/t/rfc-make-clang-builtin-math-functions-constexpr-with-llvm-libc-to-support-c-23-constexpr-math-functions/86450 Please merge #147895 first
1 parent c5c0da8 commit 074218d

File tree

7 files changed

+96
-7
lines changed

7 files changed

+96
-7
lines changed

libc/shared/math.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717
#include "math/frexpf128.h"
1818
#include "math/frexpf16.h"
1919
#include "math/ldexpf128.h"
20+
#include "math/ldexpf16.h"
2021

2122
#endif // LLVM_LIBC_SHARED_MATH_H

libc/shared/math/ldexpf16.h

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

libc/src/__support/math/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,13 @@ add_header_library(
9292
libc.src.__support.FPUtil.manipulation_functions
9393
libc.include.llvm-libc-types.float128
9494
)
95+
96+
add_header_library(
97+
ldexpf16
98+
HDRS
99+
ldexpf16.h
100+
DEPENDS
101+
libc.src.__support.macros.properties.types
102+
libc.src.__support.FPUtil.manipulation_functions
103+
libc.include.llvm-libc-macros.float16_macros
104+
)

libc/src/__support/math/ldexpf16.h

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

libc/src/math/generic/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,8 +1933,7 @@ add_entrypoint_object(
19331933
HDRS
19341934
../ldexpf16.h
19351935
DEPENDS
1936-
libc.src.__support.macros.properties.types
1937-
libc.src.__support.FPUtil.manipulation_functions
1936+
libc.src.__support.math.ldexpf16
19381937
)
19391938

19401939
add_entrypoint_object(

libc/src/math/generic/ldexpf16.cpp

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

99
#include "src/math/ldexpf16.h"
10-
#include "src/__support/FPUtil/ManipulationFunctions.h"
11-
#include "src/__support/common.h"
12-
#include "src/__support/macros/config.h"
10+
11+
#include "src/__support/math/ldexpf16.h"
1312

1413
namespace LIBC_NAMESPACE_DECL {
1514

1615
LLVM_LIBC_FUNCTION(float16, ldexpf16, (float16 x, int exp)) {
17-
return fputil::ldexp(x, exp);
16+
return math::ldexpf16(x, exp);
1817
}
1918

2019
} // namespace LIBC_NAMESPACE_DECL

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2187,6 +2187,16 @@ libc_support_library(
21872187
],
21882188
)
21892189

2190+
libc_support_library(
2191+
name = "__support_math_ldexpf16",
2192+
hdrs = ["src/__support/math/ldexpf16.h"],
2193+
deps = [
2194+
":__support_macros_properties_types",
2195+
":__support_fputil_manipulation_functions",
2196+
":llvm_libc_macros_float16_macros"
2197+
],
2198+
)
2199+
21902200
############################### complex targets ################################
21912201

21922202
libc_function(
@@ -3347,7 +3357,12 @@ libc_math_function(
33473357
],
33483358
)
33493359

3350-
libc_math_function(name = "ldexpf16")
3360+
libc_math_function(
3361+
name = "ldexpf16",
3362+
additional_deps = [
3363+
":__support_math_ldexpf16",
3364+
],
3365+
)
33513366

33523367
libc_math_function(name = "llogb")
33533368

0 commit comments

Comments
 (0)