75 changes: 75 additions & 0 deletions libc/test/src/math/fminl_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//===-- Unittests for fmin -----------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===---------------------------------------------------------------------===//

#include <vector>

#include "include/math.h"
#include "src/math/fminl.h"
#include "utils/FPUtil/FPBits.h"
#include "utils/UnitTest/Test.h"

using FPBits = __llvm_libc::fputil::FPBits<long double>;

long double nan = static_cast<long double>(FPBits::buildNaN(1));
long double inf = static_cast<long double>(FPBits::inf());
long double negInf = static_cast<long double>(FPBits::negInf());

TEST(FminlTest, NaNArg) {
EXPECT_EQ(inf, __llvm_libc::fminl(nan, inf));
EXPECT_EQ(negInf, __llvm_libc::fminl(negInf, nan));
EXPECT_EQ(0.0L, __llvm_libc::fminl(nan, 0.0L));
EXPECT_EQ(-0.0L, __llvm_libc::fminl(-0.0L, nan));
EXPECT_EQ(-1.2345L, __llvm_libc::fminl(nan, -1.2345L));
EXPECT_EQ(1.2345L, __llvm_libc::fminl(1.2345L, nan));
EXPECT_NE(isnan(__llvm_libc::fminl(nan, nan)), 0);
}

TEST(FminlTest, InfArg) {
EXPECT_EQ(negInf, __llvm_libc::fminl(negInf, inf));
EXPECT_EQ(0.0L, __llvm_libc::fminl(inf, 0.0L));
EXPECT_EQ(-0.0L, __llvm_libc::fminl(-0.0L, inf));
EXPECT_EQ(1.2345L, __llvm_libc::fminl(inf, 1.2345L));
EXPECT_EQ(-1.2345L, __llvm_libc::fminl(-1.2345L, inf));
}

TEST(FminlTest, NegInfArg) {
EXPECT_EQ(negInf, __llvm_libc::fminl(inf, negInf));
EXPECT_EQ(negInf, __llvm_libc::fminl(negInf, 0.0L));
EXPECT_EQ(negInf, __llvm_libc::fminl(-0.0L, negInf));
EXPECT_EQ(negInf, __llvm_libc::fminl(negInf, -1.2345L));
EXPECT_EQ(negInf, __llvm_libc::fminl(1.2345L, negInf));
}

TEST(FminlTest, BothZero) {
EXPECT_EQ(0.0L, __llvm_libc::fminl(0.0L, 0.0L));
EXPECT_EQ(-0.0L, __llvm_libc::fminl(-0.0L, 0.0L));
EXPECT_EQ(-0.0L, __llvm_libc::fminl(0.0L, -0.0L));
EXPECT_EQ(-0.0L, __llvm_libc::fminl(-0.0L, -0.0L));
}

TEST(FminlTest, InLongDoubleRange) {
using UIntType = FPBits::UIntType;
constexpr UIntType count = 10000000;
constexpr UIntType step = UIntType(-1) / count;
for (UIntType i = 0, v = 0, w = UIntType(-1); i <= count;
++i, v += step, w -= step) {
long double x = FPBits(v), y = FPBits(w);
if (isnan(x) || isinf(x))
continue;
if (isnan(y) || isinf(y))
continue;
if ((x == 0) && (y == 0))
continue;

if (x < y) {
ASSERT_EQ(x, __llvm_libc::fminl(x, y));
} else {
ASSERT_EQ(y, __llvm_libc::fminl(x, y));
}
}
}
19 changes: 19 additions & 0 deletions libc/utils/FPUtil/BasicOperations.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,25 @@ static inline T abs(T x) {
return T(bits);
}

template <typename T,
cpp::EnableIfType<cpp::IsFloatingPointType<T>::Value, int> = 0>
static inline T fmin(T x, T y) {
FPBits<T> bitx(x), bity(y);

if (bitx.isNaN()) {
return y;
} else if (bity.isNaN()) {
return x;
} else if (bitx.sign != bity.sign) {
// To make sure that fmin(+0, -0) == -0 == fmin(-0, +0), whenever x and
// y has different signs and both are not NaNs, we return the number
// with negative sign.
return (bitx.sign ? x : y);
} else {
return (x < y ? x : y);
}
}

} // namespace fputil
} // namespace __llvm_libc

Expand Down
1 change: 1 addition & 0 deletions libc/utils/FPUtil/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ add_header_library(
FloatOperations.h
FloatProperties.h
FPBits.h
BasicOperations.h
ManipulationFunctions.h
NearestIntegerOperations.h
DEPENDS
Expand Down