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