Skip to content

Commit f81c15c

Browse files
committed
[libc][math] Refactor dsqrtl implementation to header-only in src/__support/math folder.
1 parent 4d32320 commit f81c15c

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
@@ -457,6 +457,14 @@ add_header_library(
457457
libc.src.__support.macros.optimization
458458
)
459459

460+
add_header_library(
461+
dsqrtl
462+
HDRS
463+
dsqrtl.h
464+
DEPENDS
465+
libc.src.__support.FPUtil.generic.sqrt
466+
)
467+
460468
add_header_library(
461469
erff
462470
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
@@ -242,7 +242,7 @@ add_entrypoint_object(
242242
HDRS
243243
../dsqrtl.h
244244
DEPENDS
245-
libc.src.__support.FPUtil.generic.sqrt
245+
libc.src.__support.math.dsqrtl
246246
)
247247

248248
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
@@ -55,6 +55,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat) {
5555
EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::cosf(0.0f));
5656
EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::coshf(0.0f));
5757
EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::cospif(0.0f));
58+
EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::dsqrtl(0.0f));
5859
EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::erff(0.0f));
5960
EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::exp10f(0.0f));
6061
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
@@ -2419,6 +2419,14 @@ libc_support_library(
24192419
],
24202420
)
24212421

2422+
libc_support_library(
2423+
name = "__support_math_dsqrtl",
2424+
hdrs = ["src/__support/math/dsqrtl.h"],
2425+
deps = [
2426+
":__support_fputil_sqrt",
2427+
],
2428+
)
2429+
24222430
libc_support_library(
24232431
name = "__support_math_erff",
24242432
hdrs = ["src/__support/math/erff.h"],
@@ -3287,7 +3295,7 @@ libc_math_function(name = "dmulf128")
32873295
libc_math_function(
32883296
name = "dsqrtl",
32893297
additional_deps = [
3290-
":__support_fputil_sqrt",
3298+
":__support_math_dsqrtl",
32913299
],
32923300
)
32933301

0 commit comments

Comments
 (0)