1 //===-- Utility class to test canonicalize[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 #ifndef LLVM_LIBC_TEST_SRC_MATH_SMOKE_CANONICALIZETEST_H 10 #define LLVM_LIBC_TEST_SRC_MATH_SMOKE_CANONICALIZETEST_H 11 12 #include "src/__support/FPUtil/FEnvImpl.h" 13 #include "src/__support/FPUtil/FPBits.h" 14 #include "src/__support/integer_literals.h" 15 #include "test/UnitTest/FEnvSafeTest.h" 16 #include "test/UnitTest/FPMatcher.h" 17 #include "test/UnitTest/Test.h" 18 19 #include "hdr/math_macros.h" 20 21 #define TEST_SPECIAL(x, y, expected, expected_exception) \ 22 EXPECT_EQ(expected, f(&x, &y)); \ 23 EXPECT_FP_EXCEPTION(expected_exception); \ 24 LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT) 25 26 #define TEST_REGULAR(x, y, expected) TEST_SPECIAL(x, y, expected, 0) 27 28 using LIBC_NAMESPACE::operator""_u128; 29 30 template <typename T> 31 class CanonicalizeTest : public LIBC_NAMESPACE::testing::FEnvSafeTest { 32 33 DECLARE_SPECIAL_CONSTANTS(T) 34 35 public: 36 typedef int (*CanonicalizeFunc)(T *, const T *); 37 testSpecialNumbers(CanonicalizeFunc f)38 void testSpecialNumbers(CanonicalizeFunc f) { 39 T cx; 40 41 TEST_SPECIAL(cx, zero, 0, 0); 42 EXPECT_FP_EQ(cx, zero); 43 44 TEST_SPECIAL(cx, neg_zero, 0, 0); 45 EXPECT_FP_EQ(cx, neg_zero); 46 47 TEST_SPECIAL(cx, inf, 0, 0); 48 EXPECT_FP_EQ(cx, inf); 49 50 TEST_SPECIAL(cx, neg_inf, 0, 0); 51 EXPECT_FP_EQ(cx, neg_inf); 52 53 TEST_SPECIAL(cx, sNaN, 1, FE_INVALID); 54 EXPECT_FP_EQ(cx, aNaN); 55 } 56 testX64_80SpecialNumbers(CanonicalizeFunc f)57 void testX64_80SpecialNumbers(CanonicalizeFunc f) { 58 if constexpr (LIBC_NAMESPACE::fputil::get_fp_type<T>() == 59 LIBC_NAMESPACE::fputil::FPType::X86_Binary80) { 60 T cx; 61 // Exponent | Significand | Meaning 62 // | Bits 63-62 | Bits 61-0 | 63 // All Ones | 00 | Zero | Pseudo Infinity, Value = SNaN 64 FPBits test1(0x00000000'00007FFF'00000000'00000000_u128); 65 const T test1_val = test1.get_val(); 66 TEST_SPECIAL(cx, test1_val, 1, FE_INVALID); 67 EXPECT_FP_EQ(cx, aNaN); 68 69 // Exponent | Significand | Meaning 70 // | Bits 63-62 | Bits 61-0 | 71 // All Ones | 00 | Non-Zero | Pseudo NaN, Value = SNaN 72 FPBits test2_1(0x00000000'00007FFF'00000000'00000001_u128); 73 const T test2_1_val = test2_1.get_val(); 74 TEST_SPECIAL(cx, test2_1_val, 1, FE_INVALID); 75 EXPECT_FP_EQ(cx, aNaN); 76 77 FPBits test2_2(0x00000000'00007FFF'00000042'70000001_u128); 78 const T test2_2_val = test2_2.get_val(); 79 TEST_SPECIAL(cx, test2_2_val, 1, FE_INVALID); 80 EXPECT_FP_EQ(cx, aNaN); 81 82 FPBits test2_3(0x00000000'00007FFF'00000000'08261001_u128); 83 const T test2_3_val = test2_3.get_val(); 84 TEST_SPECIAL(cx, test2_3_val, 1, FE_INVALID); 85 EXPECT_FP_EQ(cx, aNaN); 86 87 FPBits test2_4(0x00000000'00007FFF'00007800'08261001_u128); 88 const T test2_4_val = test2_4.get_val(); 89 TEST_SPECIAL(cx, test2_4_val, 1, FE_INVALID); 90 EXPECT_FP_EQ(cx, aNaN); 91 92 // Exponent | Significand | Meaning 93 // | Bits 63-62 | Bits 61-0 | 94 // All Ones | 01 | Anything | Pseudo NaN, Value = SNaN 95 FPBits test3_1(0x00000000'00007FFF'40000000'00000000_u128); 96 const T test3_1_val = test3_1.get_val(); 97 TEST_SPECIAL(cx, test3_1_val, 1, FE_INVALID); 98 EXPECT_FP_EQ(cx, aNaN); 99 100 FPBits test3_2(0x00000000'00007FFF'40000042'70000001_u128); 101 const T test3_2_val = test3_2.get_val(); 102 TEST_SPECIAL(cx, test3_2_val, 1, FE_INVALID); 103 EXPECT_FP_EQ(cx, aNaN); 104 105 FPBits test3_3(0x00000000'00007FFF'40000000'08261001_u128); 106 const T test3_3_val = test3_3.get_val(); 107 TEST_SPECIAL(cx, test3_3_val, 1, FE_INVALID); 108 EXPECT_FP_EQ(cx, aNaN); 109 110 FPBits test3_4(0x00000000'00007FFF'40007800'08261001_u128); 111 const T test3_4_val = test3_4.get_val(); 112 TEST_SPECIAL(cx, test3_4_val, 1, FE_INVALID); 113 EXPECT_FP_EQ(cx, aNaN); 114 115 // Exponent | Significand | Meaning 116 // | Bit 63 | Bits 62-0 | 117 // All zeroes | One | Anything | Pseudo Denormal, Value = 118 // | | | (−1)**s × m × 2**−16382 119 FPBits test4_1(0x00000000'00000000'80000000'00000000_u128); 120 const T test4_1_val = test4_1.get_val(); 121 TEST_SPECIAL(cx, test4_1_val, 0, 0); 122 EXPECT_FP_EQ( 123 cx, FPBits::make_value(test4_1.get_explicit_mantissa(), 0).get_val()); 124 125 FPBits test4_2(0x00000000'00000000'80000042'70000001_u128); 126 const T test4_2_val = test4_2.get_val(); 127 TEST_SPECIAL(cx, test4_2_val, 0, 0); 128 EXPECT_FP_EQ( 129 cx, FPBits::make_value(test4_2.get_explicit_mantissa(), 0).get_val()); 130 131 FPBits test4_3(0x00000000'00000000'80000000'08261001_u128); 132 const T test4_3_val = test4_3.get_val(); 133 TEST_SPECIAL(cx, test4_3_val, 0, 0); 134 EXPECT_FP_EQ( 135 cx, FPBits::make_value(test4_3.get_explicit_mantissa(), 0).get_val()); 136 137 // Exponent | Significand | Meaning 138 // | Bit 63 | Bits 62-0 | 139 // All Other | Zero | Anything | Unnormal, Value = SNaN 140 // Values | | | 141 FPBits test5_1(0x00000000'00000040'00000000'00000001_u128); 142 const T test5_1_val = test5_1.get_val(); 143 TEST_SPECIAL(cx, test5_1_val, 1, FE_INVALID); 144 EXPECT_FP_EQ(cx, aNaN); 145 146 FPBits test5_2(0x00000000'00000230'00000042'70000001_u128); 147 const T test5_2_val = test5_2.get_val(); 148 TEST_SPECIAL(cx, test5_2_val, 1, FE_INVALID); 149 EXPECT_FP_EQ(cx, aNaN); 150 151 FPBits test5_3(0x00000000'00000560'00000000'08261001_u128); 152 const T test5_3_val = test5_3.get_val(); 153 TEST_SPECIAL(cx, test5_3_val, 1, FE_INVALID); 154 EXPECT_FP_EQ(cx, aNaN); 155 156 FPBits test5_4(0x00000000'00000780'00000028'16000000_u128); 157 const T test5_4_val = test5_4.get_val(); 158 TEST_SPECIAL(cx, test5_4_val, 1, FE_INVALID); 159 EXPECT_FP_EQ(cx, aNaN); 160 161 FPBits test5_5(0x00000000'00000900'00000042'70000001_u128); 162 const T test5_5_val = test5_5.get_val(); 163 TEST_SPECIAL(cx, test5_5_val, 1, FE_INVALID); 164 EXPECT_FP_EQ(cx, aNaN); 165 166 FPBits test5_6(0x00000000'00000AB0'00000000'08261001_u128); 167 const T test5_6_val = test5_6.get_val(); 168 TEST_SPECIAL(cx, test5_6_val, 1, FE_INVALID); 169 EXPECT_FP_EQ(cx, aNaN); 170 } 171 } 172 testRegularNumbers(CanonicalizeFunc f)173 void testRegularNumbers(CanonicalizeFunc f) { 174 T cx; 175 const T test_var_1 = T(1.0); 176 TEST_REGULAR(cx, test_var_1, 0); 177 EXPECT_FP_EQ(cx, test_var_1); 178 const T test_var_2 = T(-1.0); 179 TEST_REGULAR(cx, test_var_2, 0); 180 EXPECT_FP_EQ(cx, test_var_2); 181 const T test_var_3 = T(10.0); 182 TEST_REGULAR(cx, test_var_3, 0); 183 EXPECT_FP_EQ(cx, test_var_3); 184 const T test_var_4 = T(-10.0); 185 TEST_REGULAR(cx, test_var_4, 0); 186 EXPECT_FP_EQ(cx, test_var_4); 187 const T test_var_5 = T(1234.0); 188 TEST_REGULAR(cx, test_var_5, 0); 189 EXPECT_FP_EQ(cx, test_var_5); 190 const T test_var_6 = T(-1234.0); 191 TEST_REGULAR(cx, test_var_6, 0); 192 EXPECT_FP_EQ(cx, test_var_6); 193 } 194 }; 195 196 #define LIST_CANONICALIZE_TESTS(T, func) \ 197 using LlvmLibcCanonicalizeTest = CanonicalizeTest<T>; \ 198 TEST_F(LlvmLibcCanonicalizeTest, SpecialNumbers) { \ 199 testSpecialNumbers(&func); \ 200 } \ 201 TEST_F(LlvmLibcCanonicalizeTest, RegularNubmers) { \ 202 testRegularNumbers(&func); \ 203 } 204 205 #define X86_80_SPECIAL_CANONICALIZE_TEST(T, func) \ 206 using LlvmLibcCanonicalizeTest = CanonicalizeTest<T>; \ 207 TEST_F(LlvmLibcCanonicalizeTest, X64_80SpecialNumbers) { \ 208 testX64_80SpecialNumbers(&func); \ 209 } 210 211 #endif // LLVM_LIBC_TEST_SRC_MATH_SMOKE_CANONICALIZETEST_H 212