Skip to content

Commit fb69b92

Browse files
author
Siva Chandra Reddy
committed
[libc][NFC] Add common template test class for sqrt, sqrtf and sqrtl.
1 parent b2a3d31 commit fb69b92

4 files changed

Lines changed: 88 additions & 171 deletions

File tree

‎libc/test/src/math/SqrtTest.h‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//===-- Utility class to test fabs[f|l] -------------------------*- 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+
#include "utils/FPUtil/TestHelpers.h"
10+
#include "utils/MPFRWrapper/MPFRUtils.h"
11+
#include "utils/UnitTest/Test.h"
12+
13+
#include <math.h>
14+
15+
namespace mpfr = __llvm_libc::testing::mpfr;
16+
17+
template <typename T> class SqrtTest : public __llvm_libc::testing::Test {
18+
19+
DECLARE_SPECIAL_CONSTANTS(T)
20+
21+
static constexpr UIntType HiddenBit =
22+
UIntType(1) << __llvm_libc::fputil::MantissaWidth<T>::value;
23+
24+
public:
25+
typedef T (*SqrtFunc)(T);
26+
27+
void testSpecialNumbers(SqrtFunc func) {
28+
ASSERT_FP_EQ(aNaN, func(aNaN));
29+
ASSERT_FP_EQ(inf, func(inf));
30+
ASSERT_FP_EQ(aNaN, func(negInf));
31+
ASSERT_FP_EQ(0.0, func(0.0));
32+
ASSERT_FP_EQ(-0.0, func(-0.0));
33+
ASSERT_FP_EQ(aNaN, func(T(-1.0)));
34+
ASSERT_FP_EQ(T(1.0), func(T(1.0)));
35+
ASSERT_FP_EQ(T(2.0), func(T(4.0)));
36+
ASSERT_FP_EQ(T(3.0), func(T(9.0)));
37+
}
38+
39+
void testDenormalValues(SqrtFunc func) {
40+
for (UIntType mant = 1; mant < HiddenBit; mant <<= 1) {
41+
FPBits denormal(T(0.0));
42+
denormal.encoding.mantissa = mant;
43+
44+
ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, T(denormal),
45+
func(denormal), T(0.5));
46+
}
47+
48+
constexpr UIntType count = 1'000'001;
49+
constexpr UIntType step = HiddenBit / count;
50+
for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
51+
T x = *reinterpret_cast<T *>(&v);
52+
ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, x, func(x), 0.5);
53+
}
54+
}
55+
56+
void testNormalRange(SqrtFunc func) {
57+
constexpr UIntType count = 10'000'001;
58+
constexpr UIntType step = UIntType(-1) / count;
59+
for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
60+
T x = *reinterpret_cast<T *>(&v);
61+
if (isnan(x) || (x < 0)) {
62+
continue;
63+
}
64+
ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, x, func(x), 0.5);
65+
}
66+
}
67+
};
68+
69+
#define LIST_SQRT_TESTS(T, func) \
70+
using LlvmLibcSqrtTest = SqrtTest<T>; \
71+
TEST_F(LlvmLibcSqrtTest, SpecialNumbers) { testSpecialNumbers(&func); } \
72+
TEST_F(LlvmLibcSqrtTest, DenormalValues) { testDenormalValues(&func); } \
73+
TEST_F(LlvmLibcSqrtTest, NormalRange) { testNormalRange(&func); }

‎libc/test/src/math/sqrt_test.cpp‎

Lines changed: 5 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,13 @@
1-
//===-- Unittests for sqrt -----------------------------------------------===//
1+
//===-- Unittests for sqrt ------------------------------------------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
7-
//===---------------------------------------------------------------------===//
7+
//===----------------------------------------------------------------------===//
88

9-
#include "src/math/sqrt.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<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<double>::value;
22-
23-
DECLARE_SPECIAL_CONSTANTS(double)
9+
#include "SqrtTest.h"
2410

25-
TEST(LlvmLibcSqrtTest, SpecialValues) {
26-
ASSERT_FP_EQ(aNaN, __llvm_libc::sqrt(aNaN));
27-
ASSERT_FP_EQ(inf, __llvm_libc::sqrt(inf));
28-
ASSERT_FP_EQ(aNaN, __llvm_libc::sqrt(negInf));
29-
ASSERT_FP_EQ(0.0, __llvm_libc::sqrt(0.0));
30-
ASSERT_FP_EQ(-0.0, __llvm_libc::sqrt(-0.0));
31-
ASSERT_FP_EQ(aNaN, __llvm_libc::sqrt(-1.0));
32-
ASSERT_FP_EQ(1.0, __llvm_libc::sqrt(1.0));
33-
ASSERT_FP_EQ(2.0, __llvm_libc::sqrt(4.0));
34-
ASSERT_FP_EQ(3.0, __llvm_libc::sqrt(9.0));
35-
}
36-
37-
TEST(LlvmLibcSqrtTest, DenormalValues) {
38-
for (UIntType mant = 1; mant < HiddenBit; mant <<= 1) {
39-
FPBits denormal(0.0);
40-
denormal.encoding.mantissa = mant;
41-
42-
ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, double(denormal),
43-
__llvm_libc::sqrt(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-
double x = *reinterpret_cast<double *>(&v);
50-
ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, x, __llvm_libc::sqrt(x), 0.5);
51-
}
52-
}
53-
54-
TEST(LlvmLibcSqrtTest, InDoubleRange) {
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-
double x = *reinterpret_cast<double *>(&v);
59-
if (isnan(x) || (x < 0)) {
60-
continue;
61-
}
11+
#include "src/math/sqrt.h"
6212

63-
ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, x, __llvm_libc::sqrt(x), 0.5);
64-
}
65-
}
13+
LIST_SQRT_TESTS(double, __llvm_libc::sqrt)

‎libc/test/src/math/sqrtf_test.cpp‎

Lines changed: 5 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,13 @@
1-
//===-- Unittests for sqrtf -----------------------------------------------===//
1+
//===-- Unittests for sqrtf------------------------------------------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
7-
//===---------------------------------------------------------------------===//
7+
//===----------------------------------------------------------------------===//
88

9-
#include "src/math/sqrtf.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<float>;
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<float>::value;
22-
23-
DECLARE_SPECIAL_CONSTANTS(float)
9+
#include "SqrtTest.h"
2410

25-
TEST(LlvmLibcSqrtfTest, SpecialValues) {
26-
ASSERT_FP_EQ(aNaN, __llvm_libc::sqrtf(aNaN));
27-
ASSERT_FP_EQ(inf, __llvm_libc::sqrtf(inf));
28-
ASSERT_FP_EQ(aNaN, __llvm_libc::sqrtf(negInf));
29-
ASSERT_FP_EQ(0.0f, __llvm_libc::sqrtf(0.0f));
30-
ASSERT_FP_EQ(-0.0f, __llvm_libc::sqrtf(-0.0f));
31-
ASSERT_FP_EQ(aNaN, __llvm_libc::sqrtf(-1.0f));
32-
ASSERT_FP_EQ(1.0f, __llvm_libc::sqrtf(1.0f));
33-
ASSERT_FP_EQ(2.0f, __llvm_libc::sqrtf(4.0f));
34-
ASSERT_FP_EQ(3.0f, __llvm_libc::sqrtf(9.0f));
35-
}
36-
37-
TEST(LlvmLibcSqrtfTest, DenormalValues) {
38-
for (UIntType mant = 1; mant < HiddenBit; mant <<= 1) {
39-
FPBits denormal(0.0f);
40-
denormal.encoding.mantissa = mant;
41-
42-
ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, float(denormal),
43-
__llvm_libc::sqrtf(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-
float x = *reinterpret_cast<float *>(&v);
50-
ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, x, __llvm_libc::sqrtf(x), 0.5);
51-
}
52-
}
53-
54-
TEST(LlvmLibcSqrtfTest, InFloatRange) {
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-
float x = *reinterpret_cast<float *>(&v);
59-
if (isnan(x) || (x < 0)) {
60-
continue;
61-
}
11+
#include "src/math/sqrtf.h"
6212

63-
ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, x, __llvm_libc::sqrtf(x), 0.5);
64-
}
65-
}
13+
LIST_SQRT_TESTS(float, __llvm_libc::sqrtf)

‎libc/test/src/math/sqrtl_test.cpp‎

Lines changed: 5 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,13 @@
1-
//===-- Unittests for sqrtl ----------------------------------------------===//
1+
//===-- Unittests for sqrtl------------------------------------------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
7-
//===---------------------------------------------------------------------===//
7+
//===----------------------------------------------------------------------===//
88

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)
9+
#include "SqrtTest.h"
2410

25-
TEST(LlvmLibcSqrtlTest, SpecialValues) {
26-
ASSERT_FP_EQ(aNaN, __llvm_libc::sqrtl(aNaN));
27-
ASSERT_FP_EQ(inf, __llvm_libc::sqrtl(inf));
28-
ASSERT_FP_EQ(aNaN, __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(aNaN, __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-
37-
TEST(LlvmLibcSqrtlTest, DenormalValues) {
38-
for (UIntType mant = 1; mant < HiddenBit; mant <<= 1) {
39-
FPBits denormal(0.0L);
40-
denormal.encoding.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-
54-
TEST(LlvmLibcSqrtlTest, 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-
}
11+
#include "src/math/sqrtl.h"
6212

63-
ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, x, __llvm_libc::sqrtl(x), 0.5);
64-
}
65-
}
13+
LIST_SQRT_TESTS(long double, __llvm_libc::sqrtl)

0 commit comments

Comments
 (0)