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 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "test/UnitTest/FEnvSafeTest.h" |
10 | #include "test/UnitTest/FPMatcher.h" |
11 | #include "test/UnitTest/Test.h" |
12 | #include "utils/MPFRWrapper/MPFRUtils.h" |
13 | |
14 | namespace mpfr = LIBC_NAMESPACE::testing::mpfr; |
15 | |
16 | template <typename OutType, typename InType> |
17 | class SqrtTest : public LIBC_NAMESPACE::testing::FEnvSafeTest { |
18 | |
19 | DECLARE_SPECIAL_CONSTANTS(InType) |
20 | |
21 | static constexpr StorageType HIDDEN_BIT = |
22 | StorageType(1) << LIBC_NAMESPACE::fputil::FPBits<InType>::FRACTION_LEN; |
23 | |
24 | public: |
25 | using SqrtFunc = OutType (*)(InType); |
26 | |
27 | void test_denormal_values(SqrtFunc func) { |
28 | for (StorageType mant = 1; mant < HIDDEN_BIT; mant <<= 1) { |
29 | FPBits denormal(zero); |
30 | denormal.set_mantissa(mant); |
31 | InType x = denormal.get_val(); |
32 | ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sqrt, x, func(x), 0.5); |
33 | } |
34 | |
35 | constexpr StorageType COUNT = 200'001; |
36 | constexpr StorageType STEP = HIDDEN_BIT / COUNT; |
37 | for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) { |
38 | InType x = FPBits(i).get_val(); |
39 | ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sqrt, x, func(x), 0.5); |
40 | } |
41 | } |
42 | |
43 | void test_normal_range(SqrtFunc func) { |
44 | constexpr StorageType COUNT = 200'001; |
45 | constexpr StorageType STEP = STORAGE_MAX / COUNT; |
46 | for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) { |
47 | FPBits x_bits(v); |
48 | InType x = x_bits.get_val(); |
49 | if (x_bits.is_nan() || (x < 0)) |
50 | continue; |
51 | ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sqrt, x, func(x), 0.5); |
52 | } |
53 | } |
54 | }; |
55 | |
56 | #define LIST_SQRT_TESTS(T, func) \ |
57 | using LlvmLibcSqrtTest = SqrtTest<T, T>; \ |
58 | TEST_F(LlvmLibcSqrtTest, DenormalValues) { test_denormal_values(&func); } \ |
59 | TEST_F(LlvmLibcSqrtTest, NormalRange) { test_normal_range(&func); } |
60 | |
61 | #define LIST_NARROWING_SQRT_TESTS(OutType, InType, func) \ |
62 | using LlvmLibcSqrtTest = SqrtTest<OutType, InType>; \ |
63 | TEST_F(LlvmLibcSqrtTest, DenormalValues) { test_denormal_values(&func); } \ |
64 | TEST_F(LlvmLibcSqrtTest, NormalRange) { test_normal_range(&func); } |
65 | |