Skip to content

Commit cfcda5d

Browse files
authored
[libc][math] Refactor ldexpf implementation to header-only in src/__support/math folder (#147906)
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 #147901 first @lntue
1 parent 3e43915 commit cfcda5d

File tree

7 files changed

+77
-6
lines changed

7 files changed

+77
-6
lines changed

libc/shared/math.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "math/frexpf.h"
1717
#include "math/frexpf128.h"
1818
#include "math/frexpf16.h"
19+
#include "math/ldexpf.h"
1920
#include "math/ldexpf128.h"
2021
#include "math/ldexpf16.h"
2122

libc/shared/math/ldexpf.h

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

libc/src/__support/math/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,11 @@ add_header_library(
102102
libc.src.__support.FPUtil.manipulation_functions
103103
libc.include.llvm-libc-macros.float16_macros
104104
)
105+
106+
add_header_library(
107+
ldexpf
108+
HDRS
109+
ldexpf.h
110+
DEPENDS
111+
libc.src.__support.FPUtil.manipulation_functions
112+
)

libc/src/__support/math/ldexpf.h

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

libc/src/math/generic/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1913,7 +1913,7 @@ add_entrypoint_object(
19131913
HDRS
19141914
../ldexpf.h
19151915
DEPENDS
1916-
libc.src.__support.FPUtil.manipulation_functions
1916+
libc.src.__support.math.ldexpf
19171917
)
19181918

19191919
add_entrypoint_object(

libc/src/math/generic/ldexpf.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/ldexpf.h"
10-
#include "src/__support/FPUtil/ManipulationFunctions.h"
11-
#include "src/__support/common.h"
12-
#include "src/__support/macros/config.h"
10+
#include "src/__support/math/ldexpf.h"
1311

1412
namespace LIBC_NAMESPACE_DECL {
1513

1614
LLVM_LIBC_FUNCTION(float, ldexpf, (float x, int exp)) {
17-
return fputil::ldexp(x, exp);
15+
return math::ldexpf(x, exp);
1816
}
1917

2018
} // namespace LIBC_NAMESPACE_DECL

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2197,6 +2197,14 @@ libc_support_library(
21972197
],
21982198
)
21992199

2200+
libc_support_library(
2201+
name = "__support_math_ldexpf",
2202+
hdrs = ["src/__support/math/ldexpf.h"],
2203+
deps = [
2204+
":__support_fputil_manipulation_functions",
2205+
],
2206+
)
2207+
22002208
############################### complex targets ################################
22012209

22022210
libc_function(
@@ -3346,7 +3354,12 @@ libc_math_function(name = "ilogbf16")
33463354

33473355
libc_math_function(name = "ldexp")
33483356

3349-
libc_math_function(name = "ldexpf")
3357+
libc_math_function(
3358+
name = "ldexpf",
3359+
additional_deps = [
3360+
":__support_math_ldexpf",
3361+
]
3362+
)
33503363

33513364
libc_math_function(name = "ldexpl")
33523365

0 commit comments

Comments
 (0)