[libc] Add implementations of fmax, fmaxf, and fmaxl.

Summary: Add implementations of fmax, fmaxf, and fmaxl.

Reviewers: sivachandra

Subscribers: mgorny, tschuett, libc-commits, ecnelises

Tags: #libc-project

Differential Revision: https://reviews.llvm.org/D84385
This commit is contained in:
Tue Ly 2020-07-23 02:04:33 -04:00
parent 9b2164063f
commit 4096088e19
16 changed files with 432 additions and 1 deletions

View File

@ -32,6 +32,9 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.floor
libc.src.math.floorf
libc.src.math.floorl
libc.src.math.fmax
libc.src.math.fmaxf
libc.src.math.fmaxl
libc.src.math.fmin
libc.src.math.fminf
libc.src.math.fminl

View File

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

View File

@ -68,6 +68,9 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.fmin
libc.src.math.fminf
libc.src.math.fminl
libc.src.math.fmax
libc.src.math.fmaxf
libc.src.math.fmaxl
libc.src.math.frexp
libc.src.math.frexpf
libc.src.math.frexpl

View File

@ -209,6 +209,10 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"fminf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
FunctionSpec<"fminl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>]>,
FunctionSpec<"fmax", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
FunctionSpec<"fmaxf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
FunctionSpec<"fmaxl", 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

@ -449,3 +449,39 @@ add_entrypoint_object(
COMPILE_OPTIONS
-O2
)
add_entrypoint_object(
fmax
SRCS
fmax.cpp
HDRS
fmax.h
DEPENDS
libc.utils.FPUtil.fputil
COMPILE_OPTIONS
-O2
)
add_entrypoint_object(
fmaxf
SRCS
fmaxf.cpp
HDRS
fmaxf.h
DEPENDS
libc.utils.FPUtil.fputil
COMPILE_OPTIONS
-O2
)
add_entrypoint_object(
fmaxl
SRCS
fmaxl.cpp
HDRS
fmaxl.h
DEPENDS
libc.utils.FPUtil.fputil
COMPILE_OPTIONS
-O2
)

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

@ -0,0 +1,18 @@
//===-- Implementation of fmax 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(fmax)(double x, double y) {
return fputil::fmax(x, y);
}
} // namespace __llvm_libc

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

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

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

@ -0,0 +1,18 @@
//===-- Implementation of fmaxf 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(fmaxf)(float x, float y) {
return fputil::fmax(x, y);
}
} // namespace __llvm_libc

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

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

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

@ -0,0 +1,18 @@
//===-- Implementation of fmaxl 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(fmaxl)(long double x, long double y) {
return fputil::fmax(x, y);
}
} // namespace __llvm_libc

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

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

View File

@ -472,4 +472,40 @@ add_math_unittest(
libc.include.math
libc.src.math.fminl
libc.utils.FPUtil.fputil
)
)
add_math_unittest(
fmaxf_test
SUITE
libc_math_unittests
SRCS
fmaxf_test.cpp
DEPENDS
libc.include.math
libc.src.math.fmaxf
libc.utils.FPUtil.fputil
)
add_math_unittest(
fmax_test
SUITE
libc_math_unittests
SRCS
fmax_test.cpp
DEPENDS
libc.include.math
libc.src.math.fmax
libc.utils.FPUtil.fputil
)
add_math_unittest(
fmaxl_test
SUITE
libc_math_unittests
SRCS
fmaxl_test.cpp
DEPENDS
libc.include.math
libc.src.math.fmaxl
libc.utils.FPUtil.fputil
)

View File

@ -0,0 +1,73 @@
//===-- Unittests for fmax -----------------------------------------------===//
//
// 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 "include/math.h"
#include "src/math/fmax.h"
#include "utils/FPUtil/FPBits.h"
#include "utils/UnitTest/Test.h"
using FPBits = __llvm_libc::fputil::FPBits<double>;
double nan = FPBits::buildNaN(1);
double inf = FPBits::inf();
double negInf = FPBits::negInf();
TEST(FmaxTest, NaNArg) {
EXPECT_EQ(inf, __llvm_libc::fmax(nan, inf));
EXPECT_EQ(negInf, __llvm_libc::fmax(negInf, nan));
EXPECT_EQ(0.0, __llvm_libc::fmax(nan, 0.0));
EXPECT_EQ(-0.0, __llvm_libc::fmax(-0.0, nan));
EXPECT_EQ(-1.2345, __llvm_libc::fmax(nan, -1.2345));
EXPECT_EQ(1.2345, __llvm_libc::fmax(1.2345, nan));
EXPECT_NE(isnan(__llvm_libc::fmax(nan, nan)), 0);
}
TEST(FmaxTest, InfArg) {
EXPECT_EQ(inf, __llvm_libc::fmax(negInf, inf));
EXPECT_EQ(inf, __llvm_libc::fmax(inf, 0.0));
EXPECT_EQ(inf, __llvm_libc::fmax(-0.0, inf));
EXPECT_EQ(inf, __llvm_libc::fmax(inf, 1.2345));
EXPECT_EQ(inf, __llvm_libc::fmax(-1.2345, inf));
}
TEST(FmaxTest, NegInfArg) {
EXPECT_EQ(inf, __llvm_libc::fmax(inf, negInf));
EXPECT_EQ(0.0, __llvm_libc::fmax(negInf, 0.0));
EXPECT_EQ(-0.0, __llvm_libc::fmax(-0.0, negInf));
EXPECT_EQ(-1.2345, __llvm_libc::fmax(negInf, -1.2345));
EXPECT_EQ(1.2345, __llvm_libc::fmax(1.2345, negInf));
}
TEST(FmaxTest, BothZero) {
EXPECT_EQ(0.0, __llvm_libc::fmax(0.0, 0.0));
EXPECT_EQ(0.0, __llvm_libc::fmax(-0.0, 0.0));
EXPECT_EQ(0.0, __llvm_libc::fmax(0.0, -0.0));
EXPECT_EQ(-0.0, __llvm_libc::fmax(-0.0, -0.0));
}
TEST(FmaxTest, InDoubleRange) {
using UIntType = FPBits::UIntType;
constexpr UIntType count = 10000001;
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::fmax(x, y));
} else {
ASSERT_EQ(y, __llvm_libc::fmax(x, y));
}
}
}

View File

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

View File

@ -0,0 +1,73 @@
//===-- Unittests for fmaxl -----------------------------------------------===//
//
// 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 "include/math.h"
#include "src/math/fmaxl.h"
#include "utils/FPUtil/FPBits.h"
#include "utils/UnitTest/Test.h"
using FPBits = __llvm_libc::fputil::FPBits<long double>;
long double nan = FPBits::buildNaN(1);
long double inf = FPBits::inf();
long double negInf = FPBits::negInf();
TEST(FmaxlTest, NaNArg) {
EXPECT_EQ(inf, __llvm_libc::fmaxl(nan, inf));
EXPECT_EQ(negInf, __llvm_libc::fmaxl(negInf, nan));
EXPECT_EQ(0.0L, __llvm_libc::fmaxl(nan, 0.0L));
EXPECT_EQ(-0.0L, __llvm_libc::fmaxl(-0.0L, nan));
EXPECT_EQ(-1.2345L, __llvm_libc::fmaxl(nan, -1.2345L));
EXPECT_EQ(1.2345L, __llvm_libc::fmaxl(1.2345L, nan));
EXPECT_NE(isnan(__llvm_libc::fmaxl(nan, nan)), 0);
}
TEST(FmaxlTest, InfArg) {
EXPECT_EQ(inf, __llvm_libc::fmaxl(negInf, inf));
EXPECT_EQ(inf, __llvm_libc::fmaxl(inf, 0.0L));
EXPECT_EQ(inf, __llvm_libc::fmaxl(-0.0L, inf));
EXPECT_EQ(inf, __llvm_libc::fmaxl(inf, 1.2345L));
EXPECT_EQ(inf, __llvm_libc::fmaxl(-1.2345L, inf));
}
TEST(FmaxlTest, NegInfArg) {
EXPECT_EQ(inf, __llvm_libc::fmaxl(inf, negInf));
EXPECT_EQ(0.0L, __llvm_libc::fmaxl(negInf, 0.0L));
EXPECT_EQ(-0.0L, __llvm_libc::fmaxl(-0.0L, negInf));
EXPECT_EQ(-1.2345L, __llvm_libc::fmaxl(negInf, -1.2345L));
EXPECT_EQ(1.2345L, __llvm_libc::fmaxl(1.2345L, negInf));
}
TEST(FmaxlTest, BothZero) {
EXPECT_EQ(0.0L, __llvm_libc::fmaxl(0.0L, 0.0L));
EXPECT_EQ(0.0L, __llvm_libc::fmaxl(-0.0L, 0.0L));
EXPECT_EQ(0.0L, __llvm_libc::fmaxl(0.0L, -0.0L));
EXPECT_EQ(-0.0L, __llvm_libc::fmaxl(-0.0L, -0.0L));
}
TEST(FmaxlTest, InLongDoubleRange) {
using UIntType = FPBits::UIntType;
constexpr UIntType count = 10000001;
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::fmaxl(x, y));
} else {
ASSERT_EQ(y, __llvm_libc::fmaxl(x, y));
}
}
}

View File

@ -43,6 +43,25 @@ static inline T fmin(T x, T y) {
}
}
template <typename T,
cpp::EnableIfType<cpp::IsFloatingPointType<T>::Value, int> = 0>
static inline T fmax(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 fmax(+0, -0) == +0 == fmax(-0, +0), whenever x and
// y has different signs and both are not NaNs, we return the number
// with positive sign.
return (bitx.sign ? y : x);
} else {
return (x > y ? x : y);
}
}
} // namespace fputil
} // namespace __llvm_libc