diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt --- a/libc/config/linux/aarch64/entrypoints.txt +++ b/libc/config/linux/aarch64/entrypoints.txt @@ -32,6 +32,9 @@ 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 diff --git a/libc/config/linux/api.td b/libc/config/linux/api.td --- a/libc/config/linux/api.td +++ b/libc/config/linux/api.td @@ -163,6 +163,9 @@ "floor", "floorf", "floorl", + "fmax", + "fmaxf", + "fmaxl", "fmin", "fminf", "fminl", diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -68,6 +68,9 @@ 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 diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td --- a/libc/spec/stdc.td +++ b/libc/spec/stdc.td @@ -209,6 +209,10 @@ FunctionSpec<"fminf", RetValSpec, [ArgSpec, ArgSpec]>, FunctionSpec<"fminl", RetValSpec, [ArgSpec, ArgSpec]>, + FunctionSpec<"fmax", RetValSpec, [ArgSpec, ArgSpec]>, + FunctionSpec<"fmaxf", RetValSpec, [ArgSpec, ArgSpec]>, + FunctionSpec<"fmaxl", RetValSpec, [ArgSpec, ArgSpec]>, + FunctionSpec<"frexp", RetValSpec, [ArgSpec, ArgSpec]>, FunctionSpec<"frexpf", RetValSpec, [ArgSpec, ArgSpec]>, FunctionSpec<"frexpl", RetValSpec, [ArgSpec, ArgSpec]>, diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt --- a/libc/src/math/CMakeLists.txt +++ b/libc/src/math/CMakeLists.txt @@ -449,3 +449,39 @@ 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 +) diff --git a/libc/src/math/fmax.h b/libc/src/math/fmax.h new file mode 100644 --- /dev/null +++ b/libc/src/math/fmax.h @@ -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 diff --git a/libc/src/math/fmax.cpp b/libc/src/math/fmax.cpp new file mode 100644 --- /dev/null +++ b/libc/src/math/fmax.cpp @@ -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 diff --git a/libc/src/math/fmaxf.h b/libc/src/math/fmaxf.h new file mode 100644 --- /dev/null +++ b/libc/src/math/fmaxf.h @@ -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 diff --git a/libc/src/math/fmaxf.cpp b/libc/src/math/fmaxf.cpp new file mode 100644 --- /dev/null +++ b/libc/src/math/fmaxf.cpp @@ -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 diff --git a/libc/src/math/fmaxl.h b/libc/src/math/fmaxl.h new file mode 100644 --- /dev/null +++ b/libc/src/math/fmaxl.h @@ -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 diff --git a/libc/src/math/fmaxl.cpp b/libc/src/math/fmaxl.cpp new file mode 100644 --- /dev/null +++ b/libc/src/math/fmaxl.cpp @@ -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 diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt --- a/libc/test/src/math/CMakeLists.txt +++ b/libc/test/src/math/CMakeLists.txt @@ -472,4 +472,40 @@ libc.include.math libc.src.math.fminl libc.utils.FPUtil.fputil -) \ No newline at end of file +) + +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 +) diff --git a/libc/test/src/math/fmax_test.cpp b/libc/test/src/math/fmax_test.cpp new file mode 100644 --- /dev/null +++ b/libc/test/src/math/fmax_test.cpp @@ -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 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)); + } + } +} diff --git a/libc/test/src/math/fmaxf_test.cpp b/libc/test/src/math/fmaxf_test.cpp new file mode 100644 --- /dev/null +++ b/libc/test/src/math/fmaxf_test.cpp @@ -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 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)); + } + } +} diff --git a/libc/test/src/math/fmaxl_test.cpp b/libc/test/src/math/fmaxl_test.cpp new file mode 100644 --- /dev/null +++ b/libc/test/src/math/fmaxl_test.cpp @@ -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 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)); + } + } +} diff --git a/libc/utils/FPUtil/BasicOperations.h b/libc/utils/FPUtil/BasicOperations.h --- a/libc/utils/FPUtil/BasicOperations.h +++ b/libc/utils/FPUtil/BasicOperations.h @@ -43,6 +43,25 @@ } } +template ::Value, int> = 0> +static inline T fmax(T x, T y) { + FPBits 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