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