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