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