Skip to content

Commit 395643e

Browse files
authored
[libc][math] Refactor frexpf16 implementation to header-only in src/__support/math folder. (#147889)
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
1 parent c3abe3f commit 395643e

File tree

7 files changed

+95
-7
lines changed

7 files changed

+95
-7
lines changed

libc/shared/math.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
#include "math/expf.h"
1515
#include "math/expf16.h"
1616
#include "math/frexpf128.h"
17+
#include "math/frexpf16.h"
1718

1819
#endif // LLVM_LIBC_SHARED_MATH_H

libc/shared/math/frexpf16.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===-- Shared frexpf16 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_FREXPF16_H
10+
#define LLVM_LIBC_SHARED_MATH_FREXPF16_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/frexpf16.h"
18+
19+
namespace LIBC_NAMESPACE_DECL {
20+
namespace shared {
21+
22+
using math::frexpf16;
23+
24+
} // namespace shared
25+
} // namespace LIBC_NAMESPACE_DECL
26+
27+
#endif // LIBC_TYPES_HAS_FLOAT16
28+
29+
#endif // LLVM_LIBC_SHARED_MATH_FREXPF16_H

libc/src/__support/math/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,13 @@ add_header_library(
6464
libc.src.__support.macros.properties.types
6565
libc.src.__support.FPUtil.manipulation_functions
6666
)
67+
68+
add_header_library(
69+
frexpf16
70+
HDRS
71+
frexpf16.h
72+
DEPENDS
73+
libc.src.__support.macros.config
74+
libc.src.__support.macros.properties.types
75+
libc.src.__support.FPUtil.manipulation_functions
76+
)

libc/src/__support/math/frexpf16.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===-- Implementation header for frexpf16 ----------------------*- 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_FREXPF16_H
10+
#define LLVM_LIBC_SRC___SUPPORT_MATH_FREXPF16_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 frexpf16(float16 x, int *exp) {
25+
return fputil::frexp(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_FREXPF16_H

libc/src/math/generic/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,8 +1779,7 @@ add_entrypoint_object(
17791779
HDRS
17801780
../frexpf16.h
17811781
DEPENDS
1782-
libc.src.__support.macros.properties.types
1783-
libc.src.__support.FPUtil.manipulation_functions
1782+
libc.src.__support.math.frexpf16
17841783
)
17851784

17861785
add_entrypoint_object(

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

1413
namespace LIBC_NAMESPACE_DECL {
1514

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

2019
} // namespace LIBC_NAMESPACE_DECL

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2158,6 +2158,17 @@ libc_support_library(
21582158
],
21592159
)
21602160

2161+
libc_support_library(
2162+
name = "__support_math_frexpf16",
2163+
hdrs = ["src/__support/math/frexpf16.h"],
2164+
deps = [
2165+
":__support_macros_config",
2166+
":__support_macros_properties_types",
2167+
":__support_fputil_manipulation_functions",
2168+
":llvm_libc_macros_float16_macros"
2169+
],
2170+
)
2171+
21612172
############################### complex targets ################################
21622173

21632174
libc_function(
@@ -3216,7 +3227,12 @@ libc_math_function(
32163227
]
32173228
)
32183229

3219-
libc_math_function(name = "frexpf16")
3230+
libc_math_function(
3231+
name = "frexpf16",
3232+
additional_deps = [
3233+
":__support_math_frexpf16"
3234+
],
3235+
)
32203236

32213237
libc_math_function(name = "fromfp")
32223238

0 commit comments

Comments
 (0)