Add implementations for fmin, fminf, and fminl. Testing infrastructure update is splitted to https://reviews.llvm.org/D83931.

This commit is contained in:
Tue Ly 2020-07-09 15:30:29 -04:00
parent ef868a848e
commit 7ce32f87f9
17 changed files with 438 additions and 0 deletions

View File

@ -31,6 +31,9 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.floor
libc.src.math.floorf
libc.src.math.floorl
libc.src.math.fmin
libc.src.math.fminf
libc.src.math.fminl
libc.src.math.frexp
libc.src.math.frexpf
libc.src.math.frexpl

View File

@ -163,6 +163,9 @@ def MathAPI : PublicAPI<"math.h"> {
"floor",
"floorf",
"floorl",
"fmin",
"fminf",
"fminl",
"frexp",
"frexpf",
"frexpl",

View File

@ -64,6 +64,9 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.floor
libc.src.math.floorf
libc.src.math.floorl
libc.src.math.fmin
libc.src.math.fminf
libc.src.math.fminl
libc.src.math.frexp
libc.src.math.frexpf
libc.src.math.frexpl

View File

@ -205,6 +205,10 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"floorf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
FunctionSpec<"floorl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
FunctionSpec<"fmin", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
FunctionSpec<"fminf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
FunctionSpec<"fminl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>]>,
FunctionSpec<"frexp", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<IntPtr>]>,
FunctionSpec<"frexpf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<IntPtr>]>,
FunctionSpec<"frexpl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<IntPtr>]>,

View File

@ -413,3 +413,39 @@ add_entrypoint_object(
COMPILE_OPTIONS
-O2
)
add_entrypoint_object(
fmin
SRCS
fmin.cpp
HDRS
fmin.h
DEPENDS
libc.utils.FPUtil.fputil
COMPILE_OPTIONS
-O2
)
add_entrypoint_object(
fminf
SRCS
fminf.cpp
HDRS
fminf.h
DEPENDS
libc.utils.FPUtil.fputil
COMPILE_OPTIONS
-O2
)
add_entrypoint_object(
fminl
SRCS
fminl.cpp
HDRS
fminl.h
DEPENDS
libc.utils.FPUtil.fputil
COMPILE_OPTIONS
-O2
)

18
libc/src/math/fmin.cpp Normal file
View File

@ -0,0 +1,18 @@
//===-- Implementation of fmin function -----------------------------------===//
//
// 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 "src/__support/common.h"
#include "utils/FPUtil/BasicOperations.h"
namespace __llvm_libc {
double LLVM_LIBC_ENTRYPOINT(fmin)(double x, double y) {
return fputil::fmin(x, y);
}
} // namespace __llvm_libc

18
libc/src/math/fmin.h Normal file
View File

@ -0,0 +1,18 @@
//===-- Implementation header for fmin --------------------------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_SRC_MATH_FMIN_H
#define LLVM_LIBC_SRC_MATH_FMIN_H
namespace __llvm_libc {
double fmin(double x, double y);
} // namespace __llvm_libc
#endif // LLVM_LIBC_SRC_MATH_FMIN_H

18
libc/src/math/fminf.cpp Normal file
View File

@ -0,0 +1,18 @@
//===-- Implementation of fminf function ----------------------------------===//
//
// 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 "src/__support/common.h"
#include "utils/FPUtil/BasicOperations.h"
namespace __llvm_libc {
float LLVM_LIBC_ENTRYPOINT(fminf)(float x, float y) {
return fputil::fmin(x, y);
}
} // namespace __llvm_libc

18
libc/src/math/fminf.h Normal file
View File

@ -0,0 +1,18 @@
//===-- Implementation header for fminf -------------------------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_SRC_MATH_FMINF_H
#define LLVM_LIBC_SRC_MATH_FMINF_H
namespace __llvm_libc {
float fminf(float x, float y);
} // namespace __llvm_libc
#endif // LLVM_LIBC_SRC_MATH_FMINF_H

18
libc/src/math/fminl.cpp Normal file
View File

@ -0,0 +1,18 @@
//===-- Implementation of fminl function ----------------------------------===//
//
// 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 "src/__support/common.h"
#include "utils/FPUtil/BasicOperations.h"
namespace __llvm_libc {
long double LLVM_LIBC_ENTRYPOINT(fminl)(long double x, long double y) {
return fputil::fmin(x, y);
}
} // namespace __llvm_libc

18
libc/src/math/fminl.h Normal file
View File

@ -0,0 +1,18 @@
//===-- Implementation header for fminl -------------------------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_SRC_MATH_FMINL_H
#define LLVM_LIBC_SRC_MATH_FMINL_H
namespace __llvm_libc {
long double fminl(long double x, long double y);
} // namespace __llvm_libc
#endif // LLVM_LIBC_SRC_MATH_FMINL_H

View File

@ -437,3 +437,39 @@ add_math_unittest(
libc.src.math.modfl
libc.utils.FPUtil.fputil
)
add_math_unittest(
fminf_test
SUITE
libc_math_unittests
SRCS
fminf_test.cpp
DEPENDS
libc.include.math
libc.src.math.fminf
libc.utils.FPUtil.fputil
)
add_math_unittest(
fmin_test
SUITE
libc_math_unittests
SRCS
fmin_test.cpp
DEPENDS
libc.include.math
libc.src.math.fmin
libc.utils.FPUtil.fputil
)
add_math_unittest(
fminl_test
SUITE
libc_math_unittests
SRCS
fminl_test.cpp
DEPENDS
libc.include.math
libc.src.math.fminl
libc.utils.FPUtil.fputil
)

View File

@ -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/fmin.h"
#include "utils/FPUtil/FPBits.h"
#include "utils/UnitTest/Test.h"
using FPBits = __llvm_libc::fputil::FPBits<double>;
double nan = static_cast<double>(FPBits::buildNaN(1));
double inf = static_cast<double>(FPBits::inf());
double negInf = static_cast<double>(FPBits::negInf());
TEST(FminTest, NaNArg) {
EXPECT_EQ(inf, __llvm_libc::fmin(nan, inf));
EXPECT_EQ(negInf, __llvm_libc::fmin(negInf, nan));
EXPECT_EQ(0.0, __llvm_libc::fmin(nan, 0.0));
EXPECT_EQ(-0.0, __llvm_libc::fmin(-0.0, nan));
EXPECT_EQ(-1.2345, __llvm_libc::fmin(nan, -1.2345));
EXPECT_EQ(1.2345, __llvm_libc::fmin(1.2345, nan));
EXPECT_NE(isnan(__llvm_libc::fmin(nan, nan)), 0);
}
TEST(FminTest, InfArg) {
EXPECT_EQ(negInf, __llvm_libc::fmin(negInf, inf));
EXPECT_EQ(0.0, __llvm_libc::fmin(inf, 0.0));
EXPECT_EQ(-0.0, __llvm_libc::fmin(-0.0, inf));
EXPECT_EQ(1.2345, __llvm_libc::fmin(inf, 1.2345));
EXPECT_EQ(-1.2345, __llvm_libc::fmin(-1.2345, inf));
}
TEST(FminTest, NegInfArg) {
EXPECT_EQ(negInf, __llvm_libc::fmin(inf, negInf));
EXPECT_EQ(negInf, __llvm_libc::fmin(negInf, 0.0));
EXPECT_EQ(negInf, __llvm_libc::fmin(-0.0, negInf));
EXPECT_EQ(negInf, __llvm_libc::fmin(negInf, -1.2345));
EXPECT_EQ(negInf, __llvm_libc::fmin(1.2345, negInf));
}
TEST(FminTest, BothZero) {
EXPECT_EQ(0.0, __llvm_libc::fmin(0.0, 0.0));
EXPECT_EQ(-0.0, __llvm_libc::fmin(-0.0, 0.0));
EXPECT_EQ(-0.0, __llvm_libc::fmin(0.0, -0.0));
EXPECT_EQ(-0.0, __llvm_libc::fmin(-0.0, -0.0));
}
TEST(FminTest, InFloatRange) {
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) {
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::fmin(x, y));
} else {
ASSERT_EQ(y, __llvm_libc::fmin(x, y));
}
}
}

View File

@ -0,0 +1,75 @@
//===-- Unittests for fminf ----------------------------------------------===//
//
// 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/fminf.h"
#include "utils/FPUtil/FPBits.h"
#include "utils/UnitTest/Test.h"
using FPBits = __llvm_libc::fputil::FPBits<float>;
float nan = static_cast<float>(FPBits::buildNaN(1));
float inf = static_cast<float>(FPBits::inf());
float negInf = static_cast<float>(FPBits::negInf());
TEST(FminfTest, NaNArg) {
EXPECT_EQ(inf, __llvm_libc::fminf(nan, inf));
EXPECT_EQ(negInf, __llvm_libc::fminf(negInf, nan));
EXPECT_EQ(0.0f, __llvm_libc::fminf(nan, 0.0f));
EXPECT_EQ(-0.0f, __llvm_libc::fminf(-0.0f, nan));
EXPECT_EQ(-1.2345f, __llvm_libc::fminf(nan, -1.2345f));
EXPECT_EQ(1.2345f, __llvm_libc::fminf(1.2345f, nan));
EXPECT_NE(isnan(__llvm_libc::fminf(nan, nan)), 0);
}
TEST(FminfTest, InfArg) {
EXPECT_EQ(negInf, __llvm_libc::fminf(negInf, inf));
EXPECT_EQ(0.0f, __llvm_libc::fminf(inf, 0.0f));
EXPECT_EQ(-0.0f, __llvm_libc::fminf(-0.0f, inf));
EXPECT_EQ(1.2345f, __llvm_libc::fminf(inf, 1.2345f));
EXPECT_EQ(-1.2345f, __llvm_libc::fminf(-1.2345f, inf));
}
TEST(FminfTest, NegInfArg) {
EXPECT_EQ(negInf, __llvm_libc::fminf(inf, negInf));
EXPECT_EQ(negInf, __llvm_libc::fminf(negInf, 0.0f));
EXPECT_EQ(negInf, __llvm_libc::fminf(-0.0f, negInf));
EXPECT_EQ(negInf, __llvm_libc::fminf(negInf, -1.2345f));
EXPECT_EQ(negInf, __llvm_libc::fminf(1.2345f, negInf));
}
TEST(FminfTest, BothZero) {
EXPECT_EQ(0.0f, __llvm_libc::fminf(0.0f, 0.0f));
EXPECT_EQ(-0.0f, __llvm_libc::fminf(-0.0f, 0.0f));
EXPECT_EQ(-0.0f, __llvm_libc::fminf(0.0f, -0.0f));
EXPECT_EQ(-0.0f, __llvm_libc::fminf(-0.0f, -0.0f));
}
TEST(FminfTest, InFloatRange) {
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) {
float 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::fminf(x, y));
} else {
ASSERT_EQ(y, __llvm_libc::fminf(x, y));
}
}
}

View File

@ -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));
}
}
}

View File

@ -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

View File

@ -14,6 +14,7 @@ add_header_library(
FloatOperations.h
FloatProperties.h
FPBits.h
BasicOperations.h
ManipulationFunctions.h
NearestIntegerOperations.h
DEPENDS