218 changes: 0 additions & 218 deletions libc/src/__support/FPUtil/generic/sqrt.h

This file was deleted.

22 changes: 0 additions & 22 deletions libc/src/__support/FPUtil/sqrt.h

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,26 @@
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_SUPPORT_FPUTIL_GENERIC_SQRT_80_BIT_LONG_DOUBLE_H
#define LLVM_LIBC_SRC_SUPPORT_FPUTIL_GENERIC_SQRT_80_BIT_LONG_DOUBLE_H
#ifndef LLVM_LIBC_SRC_SUPPORT_FPUTIL_X86_64_SQRT_LONG_DOUBLE_H
#define LLVM_LIBC_SRC_SUPPORT_FPUTIL_X86_64_SQRT_LONG_DOUBLE_H

#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/architectures.h"

#if !defined(LLVM_LIBC_ARCH_X86)
#error "Invalid include"
#endif

#include "src/__support/CPP/TypeTraits.h"
#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/FPUtil/Sqrt.h"

namespace __llvm_libc {
namespace fputil {
namespace x86 {

inline void normalize(int &exponent, __uint128_t &mantissa) {
namespace internal {

template <>
inline void normalize<long double>(int &exponent, __uint128_t &mantissa) {
// Use binary search to shift the leading 1 bit similar to float.
// With MantissaWidth<long double> = 63, it will take
// ceil(log2(63)) = 6 steps checking the mantissa bits.
Expand All @@ -34,9 +43,11 @@ inline void normalize(int &exponent, __uint128_t &mantissa) {
}
}

// Correctly rounded SQRT for all rounding modes.
} // namespace internal

// Correctly rounded SQRT with round to nearest, ties to even.
// Shift-and-add algorithm.
static inline long double sqrt(long double x) {
template <> inline long double sqrt<long double, 0>(long double x) {
using UIntType = typename FPBits<long double>::UIntType;
constexpr UIntType ONE = UIntType(1)
<< int(MantissaWidth<long double>::VALUE);
Expand Down Expand Up @@ -67,7 +78,7 @@ static inline long double sqrt(long double x) {
if (bits.get_implicit_bit()) {
x_mant |= ONE;
} else if (bits.get_unbiased_exponent() == 0) {
normalize(x_exp, x_mant);
internal::normalize<long double>(x_exp, x_mant);
}

// Step 1b: Make sure the exponent is even.
Expand Down Expand Up @@ -115,16 +126,9 @@ static inline long double sqrt(long double x) {
y |= (static_cast<UIntType>(x_exp)
<< (MantissaWidth<long double>::VALUE + 1));

switch (get_round()) {
case FE_TONEAREST:
// Round to nearest, ties to even
if (rb && (lsb || (r != 0)))
++y;
break;
case FE_UPWARD:
if (rb || (r != 0))
++y;
break;
// Round to nearest, ties to even
if (rb && (lsb || (r != 0))) {
++y;
}

// Extract output
Expand All @@ -137,8 +141,7 @@ static inline long double sqrt(long double x) {
}
}

} // namespace x86
} // namespace fputil
} // namespace __llvm_libc

#endif // LLVM_LIBC_SRC_SUPPORT_FPUTIL_GENERIC_SQRT_80_BIT_LONG_DOUBLE_H
#endif // LLVM_LIBC_SRC_SUPPORT_FPUTIL_X86_64_SQRT_LONG_DOUBLE_H
44 changes: 0 additions & 44 deletions libc/src/__support/FPUtil/x86_64/sqrt.h

This file was deleted.

20 changes: 20 additions & 0 deletions libc/src/math/aarch64/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,23 @@ add_entrypoint_object(
COMPILE_OPTIONS
-O2
)

add_entrypoint_object(
sqrt
SRCS
sqrt.cpp
HDRS
../sqrt.h
COMPILE_OPTIONS
-O2
)

add_entrypoint_object(
sqrtf
SRCS
sqrtf.cpp
HDRS
../sqrtf.h
COMPILE_OPTIONS
-O2
)
12 changes: 3 additions & 9 deletions libc/src/math/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -859,10 +859,8 @@ add_entrypoint_object(
../sqrt.h
DEPENDS
libc.src.__support.FPUtil.fputil
libc.src.__support.FPUtil.sqrt
COMPILE_OPTIONS
-O3
-Wno-c++17-extensions
-O2
)

add_entrypoint_object(
Expand All @@ -873,10 +871,8 @@ add_entrypoint_object(
../sqrtf.h
DEPENDS
libc.src.__support.FPUtil.fputil
libc.src.__support.FPUtil.sqrt
COMPILE_OPTIONS
-O3
-Wno-c++17-extensions
-O2
)

add_entrypoint_object(
Expand All @@ -887,10 +883,8 @@ add_entrypoint_object(
../sqrtl.h
DEPENDS
libc.src.__support.FPUtil.fputil
libc.src.__support.FPUtil.sqrt
COMPILE_OPTIONS
-O3
-Wno-c++17-extensions
-O2
)

add_entrypoint_object(
Expand Down
2 changes: 1 addition & 1 deletion libc/src/math/generic/sqrt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//===----------------------------------------------------------------------===//

#include "src/math/sqrt.h"
#include "src/__support/FPUtil/sqrt.h"
#include "src/__support/FPUtil/Sqrt.h"
#include "src/__support/common.h"

namespace __llvm_libc {
Expand Down
2 changes: 1 addition & 1 deletion libc/src/math/generic/sqrtf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//===----------------------------------------------------------------------===//

#include "src/math/sqrtf.h"
#include "src/__support/FPUtil/sqrt.h"
#include "src/__support/FPUtil/Sqrt.h"
#include "src/__support/common.h"

namespace __llvm_libc {
Expand Down
2 changes: 1 addition & 1 deletion libc/src/math/generic/sqrtl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//===----------------------------------------------------------------------===//

#include "src/math/sqrtl.h"
#include "src/__support/FPUtil/sqrt.h"
#include "src/__support/FPUtil/Sqrt.h"
#include "src/__support/common.h"

namespace __llvm_libc {
Expand Down
30 changes: 30 additions & 0 deletions libc/src/math/x86_64/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,33 @@ add_entrypoint_object(
COMPILE_OPTIONS
-O2
)

add_entrypoint_object(
sqrt
SRCS
sqrt.cpp
HDRS
../sqrt.h
COMPILE_OPTIONS
-O2
)

add_entrypoint_object(
sqrtf
SRCS
sqrtf.cpp
HDRS
../sqrtf.h
COMPILE_OPTIONS
-O2
)

add_entrypoint_object(
sqrtl
SRCS
sqrtl.cpp
HDRS
../sqrtl.h
COMPILE_OPTIONS
-O2
)
20 changes: 20 additions & 0 deletions libc/src/math/x86_64/sqrt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation of the sqrt function for x86_64 --------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/math/sqrt.h"
#include "src/__support/common.h"

namespace __llvm_libc {

LLVM_LIBC_FUNCTION(double, sqrt, (double x)) {
double result;
__asm__ __volatile__("sqrtsd %x1, %x0" : "=x"(result) : "x"(x));
return result;
}

} // namespace __llvm_libc
20 changes: 20 additions & 0 deletions libc/src/math/x86_64/sqrtf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation of the sqrtf function for x86_64 -------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/math/sqrtf.h"
#include "src/__support/common.h"

namespace __llvm_libc {

LLVM_LIBC_FUNCTION(float, sqrtf, (float x)) {
float result;
__asm__ __volatile__("sqrtss %x1, %x0" : "=x"(result) : "x"(x));
return result;
}

} // namespace __llvm_libc
20 changes: 20 additions & 0 deletions libc/src/math/x86_64/sqrtl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation of the sqrtl function for x86_64 -------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/math/sqrtl.h"
#include "src/__support/common.h"

namespace __llvm_libc {

LLVM_LIBC_FUNCTION(long double, sqrtl, (long double x)) {
long double result;
__asm__ __volatile__("fsqrt" : "=t"(result) : "t"(x));
return result;
}

} // namespace __llvm_libc
77 changes: 20 additions & 57 deletions libc/test/src/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -983,63 +983,26 @@ add_fp_unittest(
libc.src.__support.FPUtil.fputil
)

add_fp_unittest(
sqrtl_test
NEED_MPFR
SUITE
libc_math_unittests
SRCS
sqrtl_test.cpp
DEPENDS
libc.include.math
libc.src.math.sqrtl
libc.src.__support.FPUtil.fputil
)

add_fp_unittest(
generic_sqrtf_test
NEED_MPFR
SUITE
libc_math_unittests
SRCS
generic_sqrtf_test.cpp
DEPENDS
libc.src.__support.FPUtil.fputil
libc.src.__support.FPUtil.generic.sqrt
COMPILE_OPTIONS
-O3
-Wno-c++17-extensions
)

add_fp_unittest(
generic_sqrt_test
NEED_MPFR
SUITE
libc_math_unittests
SRCS
generic_sqrt_test.cpp
DEPENDS
libc.src.__support.FPUtil.fputil
libc.src.__support.FPUtil.generic.sqrt
COMPILE_OPTIONS
-O3
-Wno-c++17-extensions
)

add_fp_unittest(
generic_sqrtl_test
NEED_MPFR
SUITE
libc_math_unittests
SRCS
generic_sqrtl_test.cpp
DEPENDS
libc.src.__support.FPUtil.fputil
libc.src.__support.FPUtil.generic.sqrt
COMPILE_OPTIONS
-O3
-Wno-c++17-extensions
)
# The quad precision test for sqrt against MPFR currently suffers
# from insufficient precision in MPFR calculations leading to
# https://hal.archives-ouvertes.fr/hal-01091186/document. We will
# renable after fixing the precision issue.
if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
add_fp_unittest(
sqrtl_test
NEED_MPFR
SUITE
libc_math_unittests
SRCS
sqrtl_test.cpp
DEPENDS
libc.include.math
libc.src.math.sqrtl
libc.src.__support.FPUtil.fputil
)
else()
message(STATUS "Skipping sqrtl_test")
endif()

add_fp_unittest(
remquof_test
Expand Down
13 changes: 0 additions & 13 deletions libc/test/src/math/generic_sqrt_test.cpp

This file was deleted.

13 changes: 0 additions & 13 deletions libc/test/src/math/generic_sqrtf_test.cpp

This file was deleted.

13 changes: 0 additions & 13 deletions libc/test/src/math/generic_sqrtl_test.cpp

This file was deleted.

50 changes: 16 additions & 34 deletions utils/bazel/llvm-project-overlay/libc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,15 @@ fputil_common_hdrs = [
"src/__support/FPUtil/NearestIntegerOperations.h",
"src/__support/FPUtil/NormalFloat.h",
"src/__support/FPUtil/PlatformDefs.h",
"src/__support/FPUtil/Sqrt.h",
]

fputil_hdrs = selects.with_or({
"//conditions:default": fputil_common_hdrs,
PLATFORM_CPU_X86_64: fputil_common_hdrs + [
"src/__support/FPUtil/x86_64/LongDoubleBits.h",
"src/__support/FPUtil/x86_64/NextAfterLongDouble.h",
"src/__support/FPUtil/x86_64/SqrtLongDouble.h",
"src/__support/FPUtil/x86_64/FEnvImpl.h",
],
PLATFORM_CPU_ARM64: fputil_common_hdrs + [
Expand All @@ -104,31 +106,6 @@ cc_library(
],
)

sqrt_common_hdrs = [
"src/__support/FPUtil/sqrt.h",
"src/__support/FPUtil/generic/sqrt.h",
"src/__support/FPUtil/generic/sqrt_80_bit_long_double.h",
]

sqrt_hdrs = selects.with_or({
"//conditions:default": sqrt_common_hdrs,
PLATFORM_CPU_X86_64: sqrt_common_hdrs + [
"src/__support/FPUtil/x86_64/sqrt.h",
],
PLATFORM_CPU_ARM64: sqrt_common_hdrs + [
"src/__support/FPUtil/aarch64/sqrt.h",
],
})

cc_library(
name = "__support_fputil_sqrt",
hdrs = sqrt_hdrs,
deps = [
":__support_fputil",
":libc_root",
],
)

################################ fenv targets ################################

libc_function(
Expand Down Expand Up @@ -461,23 +438,28 @@ libc_math_function(

libc_math_function(
name = "sqrt",
additional_deps = [
":__support_fputil_sqrt",
]
specializations = [
"aarch64",
"generic",
"x86_64",
],
)

libc_math_function(
name = "sqrtf",
additional_deps = [
":__support_fputil_sqrt",
]
specializations = [
"aarch64",
"generic",
"x86_64",
],
)

libc_math_function(
name = "sqrtl",
additional_deps = [
":__support_fputil_sqrt",
]
specializations = [
"generic",
"x86_64",
],
)

libc_math_function(name = "copysign")
Expand Down