1 //===-- Unittests for fminf ----------------------------------------------===// 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/fminf.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 = static_cast<float>(FPBits::buildNaN(1)); 17 float inf = static_cast<float>(FPBits::inf()); 18 float negInf = static_cast<float>(FPBits::negInf()); 19 20 TEST(FminfTest, NaNArg) { 21 EXPECT_EQ(inf, __llvm_libc::fminf(nan, inf)); 22 EXPECT_EQ(negInf, __llvm_libc::fminf(negInf, nan)); 23 EXPECT_EQ(0.0f, __llvm_libc::fminf(nan, 0.0f)); 24 EXPECT_EQ(-0.0f, __llvm_libc::fminf(-0.0f, nan)); 25 EXPECT_EQ(-1.2345f, __llvm_libc::fminf(nan, -1.2345f)); 26 EXPECT_EQ(1.2345f, __llvm_libc::fminf(1.2345f, nan)); 27 EXPECT_NE(isnan(__llvm_libc::fminf(nan, nan)), 0); 28 } 29 30 TEST(FminfTest, InfArg) { 31 EXPECT_EQ(negInf, __llvm_libc::fminf(negInf, inf)); 32 EXPECT_EQ(0.0f, __llvm_libc::fminf(inf, 0.0f)); 33 EXPECT_EQ(-0.0f, __llvm_libc::fminf(-0.0f, inf)); 34 EXPECT_EQ(1.2345f, __llvm_libc::fminf(inf, 1.2345f)); 35 EXPECT_EQ(-1.2345f, __llvm_libc::fminf(-1.2345f, inf)); 36 } 37 38 TEST(FminfTest, NegInfArg) { 39 EXPECT_EQ(negInf, __llvm_libc::fminf(inf, negInf)); 40 EXPECT_EQ(negInf, __llvm_libc::fminf(negInf, 0.0f)); 41 EXPECT_EQ(negInf, __llvm_libc::fminf(-0.0f, negInf)); 42 EXPECT_EQ(negInf, __llvm_libc::fminf(negInf, -1.2345f)); 43 EXPECT_EQ(negInf, __llvm_libc::fminf(1.2345f, negInf)); 44 } 45 46 TEST(FminfTest, BothZero) { 47 EXPECT_EQ(0.0f, __llvm_libc::fminf(0.0f, 0.0f)); 48 EXPECT_EQ(-0.0f, __llvm_libc::fminf(-0.0f, 0.0f)); 49 EXPECT_EQ(-0.0f, __llvm_libc::fminf(0.0f, -0.0f)); 50 EXPECT_EQ(-0.0f, __llvm_libc::fminf(-0.0f, -0.0f)); 51 } 52 53 TEST(FminfTest, InFloatRange) { 54 using UIntType = FPBits::UIntType; 55 constexpr UIntType count = 10000000; 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::fminf(x, y)); 69 } else { 70 ASSERT_EQ(y, __llvm_libc::fminf(x, y)); 71 } 72 } 73 } 74