Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3404537

Browse files
[libc][math] Refactor sqrt to header-only (#178335)
This refactors `sqrt` to be header-only, following the libc math refactoring plan. Part of #147386 Closes #177648
1 parent ed4aab0 commit 3404537

9 files changed

Lines changed: 70 additions & 6 deletions

File tree

libc/shared/math.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
#include "math/sinhf.h"
9999
#include "math/sinhf16.h"
100100
#include "math/sinpif.h"
101+
#include "math/sqrt.h"
101102
#include "math/sqrtf16.h"
102103
#include "math/tan.h"
103104
#include "math/tanf.h"

libc/shared/math/sqrt.h

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

libc/src/__support/math/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,14 @@ add_header_library(
14271427
libc.src.__support.macros.optimization
14281428
)
14291429

1430+
add_header_library(
1431+
sqrt
1432+
HDRS
1433+
sqrt.h
1434+
DEPENDS
1435+
libc.src.__support.FPUtil.sqrt
1436+
)
1437+
14301438
add_header_library(
14311439
dfmal
14321440
HDRS

libc/src/__support/math/sqrt.h

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

libc/src/math/generic/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2982,7 +2982,7 @@ add_entrypoint_object(
29822982
HDRS
29832983
../sqrt.h
29842984
DEPENDS
2985-
libc.src.__support.FPUtil.sqrt
2985+
libc.src.__support.math.sqrt
29862986
)
29872987

29882988

libc/src/math/generic/sqrt.cpp

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

99
#include "src/math/sqrt.h"
10-
#include "src/__support/FPUtil/sqrt.h"
11-
#include "src/__support/common.h"
12-
#include "src/__support/macros/config.h"
10+
#include "src/__support/math/sqrt.h"
1311

1412
namespace LIBC_NAMESPACE_DECL {
1513

16-
LLVM_LIBC_FUNCTION(double, sqrt, (double x)) { return fputil::sqrt<double>(x); }
14+
LLVM_LIBC_FUNCTION(double, sqrt, (double x)) { return math::sqrt(x); }
1715

1816
} // namespace LIBC_NAMESPACE_DECL

libc/test/shared/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ add_fp_unittest(
9696
libc.src.__support.math.sinhf
9797
libc.src.__support.math.sinhf16
9898
libc.src.__support.math.sinpif
99+
libc.src.__support.math.sqrt
99100
libc.src.__support.math.tan
100101
libc.src.__support.math.tanf
101102
)

libc/test/shared/shared_math_test.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ TEST(LlvmLibcSharedMathTest, AllDouble) {
120120
EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::log1p(0.0));
121121
EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::log2(1.0));
122122
EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::sin(0.0));
123+
EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::sqrt(0.0));
123124
EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::tan(0.0));
124125
EXPECT_EQ(0, LIBC_NAMESPACE::shared::ilogb(1.0));
125126
EXPECT_EQ(0L, LIBC_NAMESPACE::shared::llogb(1.0));

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3631,6 +3631,14 @@ libc_support_library(
36313631
],
36323632
)
36333633

3634+
libc_support_library(
3635+
name = "__support_math_sqrt",
3636+
hdrs = ["src/__support/math/sqrt.h"],
3637+
deps = [
3638+
":__support_fputil_sqrt",
3639+
],
3640+
)
3641+
36343642
libc_support_library(
36353643
name = "__support_math_hypotf",
36363644
hdrs = ["src/__support/math/hypotf.h"],
@@ -5339,7 +5347,7 @@ libc_math_function(
53395347
libc_math_function(
53405348
name = "sqrt",
53415349
additional_deps = [
5342-
":__support_fputil_sqrt",
5350+
":__support_math_sqrt",
53435351
],
53445352
)
53455353

0 commit comments

Comments
 (0)