1987fac79SSiva Chandra Reddy //===-- Unittests for x86 long double -------------------------------------===//
2987fac79SSiva Chandra Reddy //
3987fac79SSiva Chandra Reddy // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4987fac79SSiva Chandra Reddy // See https://llvm.org/LICENSE.txt for license information.
5987fac79SSiva Chandra Reddy // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6987fac79SSiva Chandra Reddy //
7987fac79SSiva Chandra Reddy //===----------------------------------------------------------------------===//
8987fac79SSiva Chandra Reddy 
9c120edc7SMichael Jones #include "src/__support/FPUtil/FPBits.h"
10987fac79SSiva Chandra Reddy #include "utils/UnitTest/Test.h"
11987fac79SSiva Chandra Reddy 
12cee1e7d1SSiva Chandra Reddy #include <math.h>
13cee1e7d1SSiva Chandra Reddy 
14987fac79SSiva Chandra Reddy using FPBits = __llvm_libc::fputil::FPBits<long double>;
15987fac79SSiva Chandra Reddy 
TEST(X86LongDoubleTest,is_nan)16*1c92911eSMichael Jones TEST(X86LongDoubleTest, is_nan) {
17987fac79SSiva Chandra Reddy   // In the nan checks below, we use the macro isnan from math.h to ensure that
18987fac79SSiva Chandra Reddy   // a number is actually a NaN. The isnan macro resolves to the compiler
19987fac79SSiva Chandra Reddy   // builtin function. Hence, matching LLVM-libc's notion of NaN with the
20987fac79SSiva Chandra Reddy   // isnan result ensures that LLVM-libc's behavior matches the compiler's
21987fac79SSiva Chandra Reddy   // behavior.
22987fac79SSiva Chandra Reddy 
23987fac79SSiva Chandra Reddy   FPBits bits(0.0l);
24*1c92911eSMichael Jones   bits.exponent = FPBits::MAX_EXPONENT;
25987fac79SSiva Chandra Reddy   for (unsigned int i = 0; i < 1000000; ++i) {
26987fac79SSiva Chandra Reddy     // If exponent has the max value and the implicit bit is 0,
27987fac79SSiva Chandra Reddy     // then the number is a NaN for all values of mantissa.
28987fac79SSiva Chandra Reddy     bits.mantissa = i;
29987fac79SSiva Chandra Reddy     long double nan = bits;
30987fac79SSiva Chandra Reddy     ASSERT_NE(isnan(nan), 0);
31*1c92911eSMichael Jones     ASSERT_TRUE(bits.is_nan());
32987fac79SSiva Chandra Reddy   }
33987fac79SSiva Chandra Reddy 
34987fac79SSiva Chandra Reddy   bits.implicitBit = 1;
35987fac79SSiva Chandra Reddy   for (unsigned int i = 1; i < 1000000; ++i) {
36987fac79SSiva Chandra Reddy     // If exponent has the max value and the implicit bit is 1,
37987fac79SSiva Chandra Reddy     // then the number is a NaN for all non-zero values of mantissa.
38987fac79SSiva Chandra Reddy     // Note the initial value of |i| of 1 to avoid a zero mantissa.
39987fac79SSiva Chandra Reddy     bits.mantissa = i;
40987fac79SSiva Chandra Reddy     long double nan = bits;
41987fac79SSiva Chandra Reddy     ASSERT_NE(isnan(nan), 0);
42*1c92911eSMichael Jones     ASSERT_TRUE(bits.is_nan());
43987fac79SSiva Chandra Reddy   }
44987fac79SSiva Chandra Reddy 
45987fac79SSiva Chandra Reddy   bits.exponent = 1;
46987fac79SSiva Chandra Reddy   bits.implicitBit = 0;
47987fac79SSiva Chandra Reddy   for (unsigned int i = 0; i < 1000000; ++i) {
48987fac79SSiva Chandra Reddy     // If exponent is non-zero and also not max, and the implicit bit is 0,
49987fac79SSiva Chandra Reddy     // then the number is a NaN for all values of mantissa.
50987fac79SSiva Chandra Reddy     bits.mantissa = i;
51987fac79SSiva Chandra Reddy     long double nan = bits;
52987fac79SSiva Chandra Reddy     ASSERT_NE(isnan(nan), 0);
53*1c92911eSMichael Jones     ASSERT_TRUE(bits.is_nan());
54987fac79SSiva Chandra Reddy   }
55987fac79SSiva Chandra Reddy 
56987fac79SSiva Chandra Reddy   bits.exponent = 1;
57987fac79SSiva Chandra Reddy   bits.implicitBit = 1;
58987fac79SSiva Chandra Reddy   for (unsigned int i = 0; i < 1000000; ++i) {
59987fac79SSiva Chandra Reddy     // If exponent is non-zero and also not max, and the implicit bit is 1,
60987fac79SSiva Chandra Reddy     // then the number is normal value for all values of mantissa.
61987fac79SSiva Chandra Reddy     bits.mantissa = i;
62987fac79SSiva Chandra Reddy     long double valid = bits;
63987fac79SSiva Chandra Reddy     ASSERT_EQ(isnan(valid), 0);
64*1c92911eSMichael Jones     ASSERT_FALSE(bits.is_nan());
65987fac79SSiva Chandra Reddy   }
66987fac79SSiva Chandra Reddy 
67987fac79SSiva Chandra Reddy   bits.exponent = 0;
68987fac79SSiva Chandra Reddy   bits.implicitBit = 1;
69987fac79SSiva Chandra Reddy   for (unsigned int i = 0; i < 1000000; ++i) {
70987fac79SSiva Chandra Reddy     // If exponent is zero, then the number is a valid but denormal value.
71987fac79SSiva Chandra Reddy     bits.mantissa = i;
72987fac79SSiva Chandra Reddy     long double valid = bits;
73987fac79SSiva Chandra Reddy     ASSERT_EQ(isnan(valid), 0);
74*1c92911eSMichael Jones     ASSERT_FALSE(bits.is_nan());
75987fac79SSiva Chandra Reddy   }
76987fac79SSiva Chandra Reddy 
77987fac79SSiva Chandra Reddy   bits.exponent = 0;
78987fac79SSiva Chandra Reddy   bits.implicitBit = 0;
79987fac79SSiva Chandra Reddy   for (unsigned int i = 0; i < 1000000; ++i) {
80987fac79SSiva Chandra Reddy     // If exponent is zero, then the number is a valid but denormal value.
81987fac79SSiva Chandra Reddy     bits.mantissa = i;
82987fac79SSiva Chandra Reddy     long double valid = bits;
83987fac79SSiva Chandra Reddy     ASSERT_EQ(isnan(valid), 0);
84*1c92911eSMichael Jones     ASSERT_FALSE(bits.is_nan());
85987fac79SSiva Chandra Reddy   }
86987fac79SSiva Chandra Reddy }
87