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