1 //===-- Unittests for x86 long double -------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "src/__support/FPUtil/FPBits.h"
10 #include "test/UnitTest/Test.h"
14 using FPBits
= LIBC_NAMESPACE::fputil::FPBits
<long double>;
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
22 constexpr uint32_t COUNT
= 100'000;
25 bits
.set_unbiased_exponent(FPBits::MAX_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.
30 long double nan
= bits
;
31 ASSERT_NE(static_cast<int>(isnan(nan
)), 0);
32 ASSERT_TRUE(bits
.is_nan());
35 bits
.set_implicit_bit(1);
36 for (unsigned int i
= 1; i
< COUNT
; ++i
) {
37 // If exponent has the max value and the implicit bit is 1,
38 // then the number is a NaN for all non-zero values of mantissa.
39 // Note the initial value of |i| of 1 to avoid a zero mantissa.
41 long double nan
= bits
;
42 ASSERT_NE(static_cast<int>(isnan(nan
)), 0);
43 ASSERT_TRUE(bits
.is_nan());
46 bits
.set_unbiased_exponent(1);
47 bits
.set_implicit_bit(0);
48 for (unsigned int i
= 0; i
< COUNT
; ++i
) {
49 // If exponent is non-zero and also not max, and the implicit bit is 0,
50 // then the number is a NaN for all values of mantissa.
52 long double nan
= bits
;
53 ASSERT_NE(static_cast<int>(isnan(nan
)), 0);
54 ASSERT_TRUE(bits
.is_nan());
57 bits
.set_unbiased_exponent(1);
58 bits
.set_implicit_bit(1);
59 for (unsigned int i
= 0; i
< COUNT
; ++i
) {
60 // If exponent is non-zero and also not max, and the implicit bit is 1,
61 // then the number is normal value for all values of mantissa.
63 long double valid
= bits
;
64 ASSERT_EQ(static_cast<int>(isnan(valid
)), 0);
65 ASSERT_FALSE(bits
.is_nan());
68 bits
.set_unbiased_exponent(0);
69 bits
.set_implicit_bit(1);
70 for (unsigned int i
= 0; i
< COUNT
; ++i
) {
71 // If exponent is zero, then the number is a valid but denormal value.
73 long double valid
= bits
;
74 ASSERT_EQ(static_cast<int>(isnan(valid
)), 0);
75 ASSERT_FALSE(bits
.is_nan());
78 bits
.set_unbiased_exponent(0);
79 bits
.set_implicit_bit(0);
80 for (unsigned int i
= 0; i
< COUNT
; ++i
) {
81 // If exponent is zero, then the number is a valid but denormal value.
83 long double valid
= bits
;
84 ASSERT_EQ(static_cast<int>(isnan(valid
)), 0);
85 ASSERT_FALSE(bits
.is_nan());