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 "src/math/fmaxf.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<float>;
16 
17 DECLARE_SPECIAL_CONSTANTS(float)
18 
19 TEST(LlvmLibcFmaxfTest, NaNArg) {
20   EXPECT_FP_EQ(inf, __llvm_libc::fmaxf(aNaN, inf));
21   EXPECT_FP_EQ(negInf, __llvm_libc::fmaxf(negInf, aNaN));
22   EXPECT_FP_EQ(0.0f, __llvm_libc::fmaxf(aNaN, 0.0f));
23   EXPECT_FP_EQ(-0.0f, __llvm_libc::fmaxf(-0.0f, aNaN));
24   EXPECT_FP_EQ(-1.2345f, __llvm_libc::fmaxf(aNaN, -1.2345f));
25   EXPECT_FP_EQ(1.2345f, __llvm_libc::fmaxf(1.2345f, aNaN));
26   EXPECT_FP_EQ(aNaN, __llvm_libc::fmaxf(aNaN, aNaN));
27 }
28 
29 TEST(LlvmLibcFmaxfTest, InfArg) {
30   EXPECT_FP_EQ(inf, __llvm_libc::fmaxf(negInf, inf));
31   EXPECT_FP_EQ(inf, __llvm_libc::fmaxf(inf, 0.0f));
32   EXPECT_FP_EQ(inf, __llvm_libc::fmaxf(-0.0f, inf));
33   EXPECT_FP_EQ(inf, __llvm_libc::fmaxf(inf, 1.2345f));
34   EXPECT_FP_EQ(inf, __llvm_libc::fmaxf(-1.2345f, inf));
35 }
36 
37 TEST(LlvmLibcFmaxfTest, NegInfArg) {
38   EXPECT_FP_EQ(inf, __llvm_libc::fmaxf(inf, negInf));
39   EXPECT_FP_EQ(0.0f, __llvm_libc::fmaxf(negInf, 0.0f));
40   EXPECT_FP_EQ(-0.0f, __llvm_libc::fmaxf(-0.0f, negInf));
41   EXPECT_FP_EQ(-1.2345f, __llvm_libc::fmaxf(negInf, -1.2345f));
42   EXPECT_FP_EQ(1.2345f, __llvm_libc::fmaxf(1.2345f, negInf));
43 }
44 
45 TEST(LlvmLibcFmaxfTest, BothZero) {
46   EXPECT_FP_EQ(0.0f, __llvm_libc::fmaxf(0.0f, 0.0f));
47   EXPECT_FP_EQ(0.0f, __llvm_libc::fmaxf(-0.0f, 0.0f));
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 }
51 
52 TEST(LlvmLibcFmaxfTest, InFloatRange) {
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     float 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::fmaxf(x, y));
68     } else {
69       ASSERT_FP_EQ(y, __llvm_libc::fmaxf(x, y));
70     }
71   }
72 }
73