1 //===-- Unittests for x86 long double -------------------------------------===// 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 "include/math.h" 10 #include "utils/FPUtil/FPBits.h" 11 #include "utils/UnitTest/Test.h" 12 13 using FPBits = __llvm_libc::fputil::FPBits<long double>; 14 15 TEST(X86LongDoubleTest, isNaN) { 16 // In the nan checks below, we use the macro isnan from math.h to ensure that 17 // a number is actually a NaN. The isnan macro resolves to the compiler 18 // builtin function. Hence, matching LLVM-libc's notion of NaN with the 19 // isnan result ensures that LLVM-libc's behavior matches the compiler's 20 // behavior. 21 22 FPBits bits(0.0l); 23 bits.exponent = FPBits::maxExponent; 24 for (unsigned int i = 0; i < 1000000; ++i) { 25 // If exponent has the max value and the implicit bit is 0, 26 // then the number is a NaN for all values of mantissa. 27 bits.mantissa = i; 28 long double nan = bits; 29 ASSERT_NE(isnan(nan), 0); 30 ASSERT_TRUE(bits.isNaN()); 31 } 32 33 bits.implicitBit = 1; 34 for (unsigned int i = 1; i < 1000000; ++i) { 35 // If exponent has the max value and the implicit bit is 1, 36 // then the number is a NaN for all non-zero values of mantissa. 37 // Note the initial value of |i| of 1 to avoid a zero mantissa. 38 bits.mantissa = i; 39 long double nan = bits; 40 ASSERT_NE(isnan(nan), 0); 41 ASSERT_TRUE(bits.isNaN()); 42 } 43 44 bits.exponent = 1; 45 bits.implicitBit = 0; 46 for (unsigned int i = 0; i < 1000000; ++i) { 47 // If exponent is non-zero and also not max, and the implicit bit is 0, 48 // then the number is a NaN for all values of mantissa. 49 bits.mantissa = i; 50 long double nan = bits; 51 ASSERT_NE(isnan(nan), 0); 52 ASSERT_TRUE(bits.isNaN()); 53 } 54 55 bits.exponent = 1; 56 bits.implicitBit = 1; 57 for (unsigned int i = 0; i < 1000000; ++i) { 58 // If exponent is non-zero and also not max, and the implicit bit is 1, 59 // then the number is normal value for all values of mantissa. 60 bits.mantissa = i; 61 long double valid = bits; 62 ASSERT_EQ(isnan(valid), 0); 63 ASSERT_FALSE(bits.isNaN()); 64 } 65 66 bits.exponent = 0; 67 bits.implicitBit = 1; 68 for (unsigned int i = 0; i < 1000000; ++i) { 69 // If exponent is zero, then the number is a valid but denormal value. 70 bits.mantissa = i; 71 long double valid = bits; 72 ASSERT_EQ(isnan(valid), 0); 73 ASSERT_FALSE(bits.isNaN()); 74 } 75 76 bits.exponent = 0; 77 bits.implicitBit = 0; 78 for (unsigned int i = 0; i < 1000000; ++i) { 79 // If exponent is zero, then the number is a valid but denormal value. 80 bits.mantissa = i; 81 long double valid = bits; 82 ASSERT_EQ(isnan(valid), 0); 83 ASSERT_FALSE(bits.isNaN()); 84 } 85 } 86