Skip to content

Commit 2b937da

Browse files
authored
[libc][math] Refactor dsqrtl implementation to header-only in src/__support/math folder. (llvm#154868)
Part of llvm#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 3e57a0d commit 2b937da

File tree

10 files changed

+76
-13
lines changed

10 files changed

+76
-13
lines changed

libc/shared/math.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "math/coshf16.h"
4040
#include "math/cospif.h"
4141
#include "math/cospif16.h"
42+
#include "math/dsqrtl.h"
4243
#include "math/erff.h"
4344
#include "math/exp.h"
4445
#include "math/exp10.h"

libc/shared/math/dsqrtl.h

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

libc/src/__support/FPUtil/generic/sqrt.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ LIBC_INLINE void normalize<long double>(int &exponent, UInt128 &mantissa) {
6969
// Correctly rounded IEEE 754 SQRT for all rounding modes.
7070
// Shift-and-add algorithm.
7171
template <typename OutType, typename InType>
72-
LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<OutType> &&
73-
cpp::is_floating_point_v<InType> &&
74-
sizeof(OutType) <= sizeof(InType),
75-
OutType>
72+
LIBC_INLINE static constexpr cpp::enable_if_t<
73+
cpp::is_floating_point_v<OutType> && cpp::is_floating_point_v<InType> &&
74+
sizeof(OutType) <= sizeof(InType),
75+
OutType>
7676
sqrt(InType x) {
7777
if constexpr (internal::SpecialLongDouble<OutType>::VALUE &&
7878
internal::SpecialLongDouble<InType>::VALUE) {

libc/src/__support/math/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,14 @@ add_header_library(
473473
libc.src.__support.macros.optimization
474474
)
475475

476+
add_header_library(
477+
dsqrtl
478+
HDRS
479+
dsqrtl.h
480+
DEPENDS
481+
libc.src.__support.FPUtil.generic.sqrt
482+
)
483+
476484
add_header_library(
477485
erff
478486
HDRS

libc/src/__support/math/dsqrtl.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===-- Implementation header for dsqrtl ------------------------*- 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_DSQRTL_H
10+
#define LLVM_LIBC_SRC___SUPPORT_MATH_DSQRTL_H
11+
12+
#include "src/__support/FPUtil/generic/sqrt.h"
13+
14+
namespace LIBC_NAMESPACE_DECL {
15+
16+
namespace math {
17+
18+
LIBC_INLINE static constexpr double dsqrtl(long double x) {
19+
return fputil::sqrt<double>(x);
20+
}
21+
22+
} // namespace math
23+
24+
} // namespace LIBC_NAMESPACE_DECL
25+
26+
#endif // LLVM_LIBC_SRC___SUPPORT_MATH_DSQRTL_H

libc/src/math/generic/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ add_entrypoint_object(
270270
HDRS
271271
../dsqrtl.h
272272
DEPENDS
273-
libc.src.__support.FPUtil.generic.sqrt
273+
libc.src.__support.math.dsqrtl
274274
)
275275

276276
add_entrypoint_object(

libc/src/math/generic/dsqrtl.cpp

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

99
#include "src/math/dsqrtl.h"
10-
#include "src/__support/FPUtil/generic/sqrt.h"
11-
#include "src/__support/common.h"
12-
#include "src/__support/macros/config.h"
13-
10+
#include "src/__support/math/dsqrtl.h"
1411
namespace LIBC_NAMESPACE_DECL {
1512

16-
LLVM_LIBC_FUNCTION(double, dsqrtl, (long double x)) {
17-
return fputil::sqrt<double>(x);
18-
}
13+
LLVM_LIBC_FUNCTION(double, dsqrtl, (long double x)) { return math::dsqrtl(x); }
1914

2015
} // namespace LIBC_NAMESPACE_DECL

libc/test/shared/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ add_fp_unittest(
3535
libc.src.__support.math.coshf16
3636
libc.src.__support.math.cospif
3737
libc.src.__support.math.cospif16
38+
libc.src.__support.math.dsqrtl
3839
libc.src.__support.math.erff
3940
libc.src.__support.math.exp
4041
libc.src.__support.math.exp10

libc/test/shared/shared_math_test.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat) {
5757
EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::cosf(0.0f));
5858
EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::coshf(0.0f));
5959
EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::cospif(0.0f));
60+
EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::dsqrtl(0.0f));
6061
EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::erff(0.0f));
6162
EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::exp10f(0.0f));
6263
EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::expf(0.0f));

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2649,6 +2649,14 @@ libc_support_library(
26492649
],
26502650
)
26512651

2652+
libc_support_library(
2653+
name = "__support_math_dsqrtl",
2654+
hdrs = ["src/__support/math/dsqrtl.h"],
2655+
deps = [
2656+
":__support_fputil_sqrt",
2657+
],
2658+
)
2659+
26522660
libc_support_library(
26532661
name = "__support_math_erff",
26542662
hdrs = ["src/__support/math/erff.h"],
@@ -3525,7 +3533,7 @@ libc_math_function(name = "dmulf128")
35253533
libc_math_function(
35263534
name = "dsqrtl",
35273535
additional_deps = [
3528-
":__support_fputil_sqrt",
3536+
":__support_math_dsqrtl",
35293537
],
35303538
)
35313539

0 commit comments

Comments
 (0)