workflows/release-binaries: Do a preliminary build to fill ccache (#72576)
[llvm-project.git] / libc / test / utils / FPUtil / x86_long_double_test.cpp
blob6ca9b16377c0b80efc58f440975753c0470034b5
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 //===----------------------------------------------------------------------===//
9 #include "src/__support/FPUtil/FPBits.h"
10 #include "test/UnitTest/Test.h"
12 #include <math.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
21 // behavior.
22 constexpr uint32_t COUNT = 100'000;
24 FPBits bits(0.0l);
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.
29 bits.set_mantissa(i);
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.
40 bits.set_mantissa(i);
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.
51 bits.set_mantissa(i);
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.
62 bits.set_mantissa(i);
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.
72 bits.set_mantissa(i);
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.
82 bits.set_mantissa(i);
83 long double valid = bits;
84 ASSERT_EQ(static_cast<int>(isnan(valid)), 0);
85 ASSERT_FALSE(bits.is_nan());