| 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 "test/UnitTest/Test.h" |
| 11 | |
| 12 | #include "hdr/math_macros.h" |
| 13 | |
| 14 | using FPBits = LIBC_NAMESPACE::fputil::FPBits<long double>; |
| 15 | |
| 16 | TEST(LlvmLibcX86LongDoubleTest, 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 | constexpr uint32_t COUNT = 100'000; |
| 23 | |
| 24 | FPBits bits(0.0l); |
| 25 | bits.set_biased_exponent(FPBits::MAX_BIASED_EXPONENT); |
| 26 | for (unsigned int i = 0; i < COUNT; ++i) { |
| 27 | // If exponent has the max value and the implicit bit is 0, |
| 28 | // then the number is a NaN for all values of mantissa. |
| 29 | bits.set_mantissa(i); |
| 30 | ASSERT_TRUE(bits.is_nan()); |
| 31 | } |
| 32 | |
| 33 | bits.set_implicit_bit(1); |
| 34 | for (unsigned int i = 1; i < COUNT; ++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.set_mantissa(i); |
| 39 | ASSERT_TRUE(bits.is_nan()); |
| 40 | } |
| 41 | |
| 42 | bits.set_biased_exponent(1); |
| 43 | bits.set_implicit_bit(0); |
| 44 | for (unsigned int i = 0; i < COUNT; ++i) { |
| 45 | // If exponent is non-zero and also not max, and the implicit bit is 0, |
| 46 | // then the number is a NaN for all values of mantissa. |
| 47 | bits.set_mantissa(i); |
| 48 | ASSERT_TRUE(bits.is_nan()); |
| 49 | } |
| 50 | |
| 51 | bits.set_biased_exponent(1); |
| 52 | bits.set_implicit_bit(1); |
| 53 | for (unsigned int i = 0; i < COUNT; ++i) { |
| 54 | // If exponent is non-zero and also not max, and the implicit bit is 1, |
| 55 | // then the number is normal value for all values of mantissa. |
| 56 | bits.set_mantissa(i); |
| 57 | ASSERT_FALSE(bits.is_nan()); |
| 58 | } |
| 59 | |
| 60 | bits.set_biased_exponent(0); |
| 61 | bits.set_implicit_bit(1); |
| 62 | for (unsigned int i = 0; i < COUNT; ++i) { |
| 63 | // If exponent is zero, then the number is a valid but denormal value. |
| 64 | bits.set_mantissa(i); |
| 65 | ASSERT_FALSE(bits.is_nan()); |
| 66 | } |
| 67 | |
| 68 | bits.set_biased_exponent(0); |
| 69 | bits.set_implicit_bit(0); |
| 70 | for (unsigned int i = 0; i < COUNT; ++i) { |
| 71 | // If exponent is zero, then the number is a valid but denormal value. |
| 72 | bits.set_mantissa(i); |
| 73 | ASSERT_FALSE(bits.is_nan()); |
| 74 | } |
| 75 | } |
| 76 | |