1 //===-- Unittests for fminf ----------------------------------------------===//
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/fminf.h"
13 #include "utils/FPUtil/FPBits.h"
14 #include "utils/UnitTest/Test.h"
15 
16 using FPBits = __llvm_libc::fputil::FPBits<float>;
17 
18 float nan = static_cast<float>(FPBits::buildNaN(1));
19 float inf = static_cast<float>(FPBits::inf());
20 float negInf = static_cast<float>(FPBits::negInf());
21 
22 TEST(FminfTest, NaNArg) {
23   EXPECT_EQ(inf, __llvm_libc::fminf(nan, inf));
24   EXPECT_EQ(negInf, __llvm_libc::fminf(negInf, nan));
25   EXPECT_EQ(0.0f, __llvm_libc::fminf(nan, 0.0f));
26   EXPECT_EQ(-0.0f, __llvm_libc::fminf(-0.0f, nan));
27   EXPECT_EQ(-1.2345f, __llvm_libc::fminf(nan, -1.2345f));
28   EXPECT_EQ(1.2345f, __llvm_libc::fminf(1.2345f, nan));
29   EXPECT_NE(isnan(__llvm_libc::fminf(nan, nan)), 0);
30 }
31 
32 TEST(FminfTest, InfArg) {
33   EXPECT_EQ(negInf, __llvm_libc::fminf(negInf, inf));
34   EXPECT_EQ(0.0f, __llvm_libc::fminf(inf, 0.0f));
35   EXPECT_EQ(-0.0f, __llvm_libc::fminf(-0.0f, inf));
36   EXPECT_EQ(1.2345f, __llvm_libc::fminf(inf, 1.2345f));
37   EXPECT_EQ(-1.2345f, __llvm_libc::fminf(-1.2345f, inf));
38 }
39 
40 TEST(FminfTest, NegInfArg) {
41   EXPECT_EQ(negInf, __llvm_libc::fminf(inf, negInf));
42   EXPECT_EQ(negInf, __llvm_libc::fminf(negInf, 0.0f));
43   EXPECT_EQ(negInf, __llvm_libc::fminf(-0.0f, negInf));
44   EXPECT_EQ(negInf, __llvm_libc::fminf(negInf, -1.2345f));
45   EXPECT_EQ(negInf, __llvm_libc::fminf(1.2345f, negInf));
46 }
47 
48 TEST(FminfTest, BothZero) {
49   EXPECT_EQ(0.0f, __llvm_libc::fminf(0.0f, 0.0f));
50   EXPECT_EQ(-0.0f, __llvm_libc::fminf(-0.0f, 0.0f));
51   EXPECT_EQ(-0.0f, __llvm_libc::fminf(0.0f, -0.0f));
52   EXPECT_EQ(-0.0f, __llvm_libc::fminf(-0.0f, -0.0f));
53 }
54 
55 TEST(FminfTest, InFloatRange) {
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     float 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::fminf(x, y));
71     } else {
72       ASSERT_EQ(y, __llvm_libc::fminf(x, y));
73     }
74   }
75 }
76