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
14using FPBits = LIBC_NAMESPACE::fputil::FPBits<long double>;
15
16TEST(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 long double nan = bits.get_val();
31 ASSERT_NE(static_cast<int>(isnan(nan)), 0);
32 ASSERT_TRUE(bits.is_nan());
33 }
34
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.
40 bits.set_mantissa(i);
41 long double nan = bits.get_val();
42 ASSERT_NE(static_cast<int>(isnan(nan)), 0);
43 ASSERT_TRUE(bits.is_nan());
44 }
45
46 bits.set_biased_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.
51 bits.set_mantissa(i);
52 long double nan = bits.get_val();
53 ASSERT_NE(static_cast<int>(isnan(nan)), 0);
54 ASSERT_TRUE(bits.is_nan());
55 }
56
57 bits.set_biased_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.
62 bits.set_mantissa(i);
63 long double valid = bits.get_val();
64 ASSERT_EQ(static_cast<int>(isnan(valid)), 0);
65 ASSERT_FALSE(bits.is_nan());
66 }
67
68 bits.set_biased_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.
72 bits.set_mantissa(i);
73 long double valid = bits.get_val();
74 ASSERT_EQ(static_cast<int>(isnan(valid)), 0);
75 ASSERT_FALSE(bits.is_nan());
76 }
77
78 bits.set_biased_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.
82 bits.set_mantissa(i);
83 long double valid = bits.get_val();
84 ASSERT_EQ(static_cast<int>(isnan(valid)), 0);
85 ASSERT_FALSE(bits.is_nan());
86 }
87}
88

source code of libc/test/utils/FPUtil/x86_long_double_test.cpp