1 //===-- Unittests for sqrtl ----------------------------------------------===//
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 #include "src/math/sqrtl.h"
10 #include "utils/FPUtil/FPBits.h"
11 #include "utils/FPUtil/TestHelpers.h"
12 #include "utils/MPFRWrapper/MPFRUtils.h"
13 #include <math.h>
14
15 using FPBits = __llvm_libc::fputil::FPBits<long double>;
16 using UIntType = typename FPBits::UIntType;
17
18 namespace mpfr = __llvm_libc::testing::mpfr;
19
20 constexpr UIntType HiddenBit =
21 UIntType(1) << __llvm_libc::fputil::MantissaWidth<long double>::value;
22
23 DECLARE_SPECIAL_CONSTANTS(long double)
24
TEST(SqrtlTest,SpecialValues)25 TEST(SqrtlTest, SpecialValues) {
26 ASSERT_FP_EQ(nan, __llvm_libc::sqrtl(nan));
27 ASSERT_FP_EQ(inf, __llvm_libc::sqrtl(inf));
28 ASSERT_FP_EQ(nan, __llvm_libc::sqrtl(negInf));
29 ASSERT_FP_EQ(0.0L, __llvm_libc::sqrtl(0.0L));
30 ASSERT_FP_EQ(-0.0L, __llvm_libc::sqrtl(-0.0L));
31 ASSERT_FP_EQ(nan, __llvm_libc::sqrtl(-1.0L));
32 ASSERT_FP_EQ(1.0L, __llvm_libc::sqrtl(1.0L));
33 ASSERT_FP_EQ(2.0L, __llvm_libc::sqrtl(4.0L));
34 ASSERT_FP_EQ(3.0L, __llvm_libc::sqrtl(9.0L));
35 }
36
TEST(SqrtlTest,DenormalValues)37 TEST(SqrtlTest, DenormalValues) {
38 for (UIntType mant = 1; mant < HiddenBit; mant <<= 1) {
39 FPBits denormal(0.0L);
40 denormal.mantissa = mant;
41
42 ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, static_cast<long double>(denormal),
43 __llvm_libc::sqrtl(denormal), 0.5);
44 }
45
46 constexpr UIntType count = 1'000'001;
47 constexpr UIntType step = HiddenBit / count;
48 for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
49 long double x = *reinterpret_cast<long double *>(&v);
50 ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, x, __llvm_libc::sqrtl(x), 0.5);
51 }
52 }
53
TEST(SqrtlTest,InLongDoubleRange)54 TEST(SqrtlTest, InLongDoubleRange) {
55 constexpr UIntType count = 10'000'001;
56 constexpr UIntType step = UIntType(-1) / count;
57 for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
58 long double x = *reinterpret_cast<long double *>(&v);
59 if (isnan(x) || (x < 0)) {
60 continue;
61 }
62
63 ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, x, __llvm_libc::sqrtl(x), 0.5);
64 }
65 }
66