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