1 //===-- Unittests for fmaxl -----------------------------------------------===//
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/math/fmaxl.h"
10 #include "utils/FPUtil/FPBits.h"
11 #include "utils/FPUtil/TestHelpers.h"
12 #include "utils/UnitTest/Test.h"
13 #include <math.h>
14 
15 using FPBits = __llvm_libc::fputil::FPBits<long double>;
16 
17 DECLARE_SPECIAL_CONSTANTS(long double)
18 
19 TEST(LlvmLibcFmaxlTest, NaNArg) {
20   EXPECT_FP_EQ(inf, __llvm_libc::fmaxl(aNaN, inf));
21   EXPECT_FP_EQ(negInf, __llvm_libc::fmaxl(negInf, aNaN));
22   EXPECT_FP_EQ(0.0L, __llvm_libc::fmaxl(aNaN, 0.0L));
23   EXPECT_FP_EQ(-0.0L, __llvm_libc::fmaxl(-0.0L, aNaN));
24   EXPECT_FP_EQ(-1.2345L, __llvm_libc::fmaxl(aNaN, -1.2345L));
25   EXPECT_FP_EQ(1.2345L, __llvm_libc::fmaxl(1.2345L, aNaN));
26   EXPECT_FP_EQ(aNaN, __llvm_libc::fmaxl(aNaN, aNaN));
27 }
28 
29 TEST(LlvmLibcFmaxlTest, InfArg) {
30   EXPECT_FP_EQ(inf, __llvm_libc::fmaxl(negInf, inf));
31   EXPECT_FP_EQ(inf, __llvm_libc::fmaxl(inf, 0.0L));
32   EXPECT_FP_EQ(inf, __llvm_libc::fmaxl(-0.0L, inf));
33   EXPECT_FP_EQ(inf, __llvm_libc::fmaxl(inf, 1.2345L));
34   EXPECT_FP_EQ(inf, __llvm_libc::fmaxl(-1.2345L, inf));
35 }
36 
37 TEST(LlvmLibcFmaxlTest, NegInfArg) {
38   EXPECT_FP_EQ(inf, __llvm_libc::fmaxl(inf, negInf));
39   EXPECT_FP_EQ(0.0L, __llvm_libc::fmaxl(negInf, 0.0L));
40   EXPECT_FP_EQ(-0.0L, __llvm_libc::fmaxl(-0.0L, negInf));
41   EXPECT_FP_EQ(-1.2345L, __llvm_libc::fmaxl(negInf, -1.2345L));
42   EXPECT_FP_EQ(1.2345L, __llvm_libc::fmaxl(1.2345L, negInf));
43 }
44 
45 TEST(LlvmLibcFmaxlTest, BothZero) {
46   EXPECT_FP_EQ(0.0L, __llvm_libc::fmaxl(0.0L, 0.0L));
47   EXPECT_FP_EQ(0.0L, __llvm_libc::fmaxl(-0.0L, 0.0L));
48   EXPECT_FP_EQ(0.0L, __llvm_libc::fmaxl(0.0L, -0.0L));
49   EXPECT_FP_EQ(-0.0L, __llvm_libc::fmaxl(-0.0L, -0.0L));
50 }
51 
52 TEST(LlvmLibcFmaxlTest, InLongDoubleRange) {
53   using UIntType = FPBits::UIntType;
54   constexpr UIntType count = 10000001;
55   constexpr UIntType step = UIntType(-1) / count;
56   for (UIntType i = 0, v = 0, w = UIntType(-1); i <= count;
57        ++i, v += step, w -= step) {
58     long double x = FPBits(v), y = FPBits(w);
59     if (isnan(x) || isinf(x))
60       continue;
61     if (isnan(y) || isinf(y))
62       continue;
63     if ((x == 0) && (y == 0))
64       continue;
65 
66     if (x > y) {
67       ASSERT_FP_EQ(x, __llvm_libc::fmaxl(x, y));
68     } else {
69       ASSERT_FP_EQ(y, __llvm_libc::fmaxl(x, y));
70     }
71   }
72 }
73