Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / src / math / SqrtTest.h
blob24f14b78d2f0f5d850c3335a61e9e0b2d2b3e1d8
1 //===-- Utility class to test sqrt[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 //===----------------------------------------------------------------------===//
9 #include "src/__support/CPP/bit.h"
10 #include "test/UnitTest/FPMatcher.h"
11 #include "test/UnitTest/Test.h"
12 #include "utils/MPFRWrapper/MPFRUtils.h"
14 #include <math.h>
16 namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
18 template <typename T> class SqrtTest : public LIBC_NAMESPACE::testing::Test {
20 DECLARE_SPECIAL_CONSTANTS(T)
22 static constexpr UIntType HIDDEN_BIT =
23 UIntType(1) << LIBC_NAMESPACE::fputil::MantissaWidth<T>::VALUE;
25 public:
26 typedef T (*SqrtFunc)(T);
28 void test_special_numbers(SqrtFunc func) {
29 ASSERT_FP_EQ(aNaN, func(aNaN));
30 ASSERT_FP_EQ(inf, func(inf));
31 ASSERT_FP_EQ(aNaN, func(neg_inf));
32 ASSERT_FP_EQ(0.0, func(0.0));
33 ASSERT_FP_EQ(-0.0, func(-0.0));
34 ASSERT_FP_EQ(aNaN, func(T(-1.0)));
35 ASSERT_FP_EQ(T(1.0), func(T(1.0)));
36 ASSERT_FP_EQ(T(2.0), func(T(4.0)));
37 ASSERT_FP_EQ(T(3.0), func(T(9.0)));
40 void test_denormal_values(SqrtFunc func) {
41 for (UIntType mant = 1; mant < HIDDEN_BIT; mant <<= 1) {
42 FPBits denormal(T(0.0));
43 denormal.set_mantissa(mant);
44 T x = T(denormal);
45 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sqrt, x, func(x), 0.5);
48 constexpr UIntType COUNT = 200'001;
49 constexpr UIntType STEP = HIDDEN_BIT / COUNT;
50 for (UIntType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
51 T x = LIBC_NAMESPACE::cpp::bit_cast<T>(v);
52 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sqrt, x, func(x), 0.5);
56 void test_normal_range(SqrtFunc func) {
57 constexpr UIntType COUNT = 200'001;
58 constexpr UIntType STEP = UIntType(-1) / COUNT;
59 for (UIntType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
60 T x = LIBC_NAMESPACE::cpp::bit_cast<T>(v);
61 if (isnan(x) || (x < 0)) {
62 continue;
64 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sqrt, x, func(x), 0.5);
69 #define LIST_SQRT_TESTS(T, func) \
70 using LlvmLibcSqrtTest = SqrtTest<T>; \
71 TEST_F(LlvmLibcSqrtTest, SpecialNumbers) { test_special_numbers(&func); } \
72 TEST_F(LlvmLibcSqrtTest, DenormalValues) { test_denormal_values(&func); } \
73 TEST_F(LlvmLibcSqrtTest, NormalRange) { test_normal_range(&func); }