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 @@ -29,6 +29,9 @@ 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 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", + "fmin", + "fminf", + "fminl", "frexp", "frexpf", "frexpl", 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 @@ -62,6 +62,9 @@ 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 diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td --- a/libc/spec/stdc.td +++ b/libc/spec/stdc.td @@ -205,6 +205,10 @@ FunctionSpec<"floorf", RetValSpec, [ArgSpec]>, FunctionSpec<"floorl", RetValSpec, [ArgSpec]>, + FunctionSpec<"fmin", RetValSpec, [ArgSpec, ArgSpec]>, + FunctionSpec<"fminf", RetValSpec, [ArgSpec, ArgSpec]>, + FunctionSpec<"fminl", 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 @@ -413,3 +413,39 @@ 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 +) diff --git a/libc/src/math/fmin.h b/libc/src/math/fmin.h new file mode 100644 --- /dev/null +++ b/libc/src/math/fmin.h @@ -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 diff --git a/libc/src/math/fmin.cpp b/libc/src/math/fmin.cpp new file mode 100644 --- /dev/null +++ b/libc/src/math/fmin.cpp @@ -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 diff --git a/libc/src/math/fminf.h b/libc/src/math/fminf.h new file mode 100644 --- /dev/null +++ b/libc/src/math/fminf.h @@ -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 diff --git a/libc/src/math/fminf.cpp b/libc/src/math/fminf.cpp new file mode 100644 --- /dev/null +++ b/libc/src/math/fminf.cpp @@ -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 \ No newline at end of file diff --git a/libc/src/math/fminl.h b/libc/src/math/fminl.h new file mode 100644 --- /dev/null +++ b/libc/src/math/fminl.h @@ -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 diff --git a/libc/src/math/fminl.cpp b/libc/src/math/fminl.cpp new file mode 100644 --- /dev/null +++ b/libc/src/math/fminl.cpp @@ -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 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 @@ -437,3 +437,39 @@ 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 +) \ No newline at end of file diff --git a/libc/test/src/math/fmin_test.cpp b/libc/test/src/math/fmin_test.cpp new file mode 100644 --- /dev/null +++ b/libc/test/src/math/fmin_test.cpp @@ -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 + +#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 nan = static_cast(FPBits::buildNaN(1)); +double inf = static_cast(FPBits::inf()); +double negInf = static_cast(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)); + } + } +} diff --git a/libc/test/src/math/fminf_test.cpp b/libc/test/src/math/fminf_test.cpp new file mode 100644 --- /dev/null +++ b/libc/test/src/math/fminf_test.cpp @@ -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 + +#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 nan = static_cast(FPBits::buildNaN(1)); +float inf = static_cast(FPBits::inf()); +float negInf = static_cast(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)); + } + } +} diff --git a/libc/test/src/math/fminl_test.cpp b/libc/test/src/math/fminl_test.cpp new file mode 100644 --- /dev/null +++ b/libc/test/src/math/fminl_test.cpp @@ -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 + +#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 nan = static_cast(FPBits::buildNaN(1)); +long double inf = static_cast(FPBits::inf()); +long double negInf = static_cast(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)); + } + } +} 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 @@ -24,6 +24,25 @@ return T(bits); } +template ::Value, int> = 0> +static inline T fmin(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 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 diff --git a/libc/utils/FPUtil/CMakeLists.txt b/libc/utils/FPUtil/CMakeLists.txt --- a/libc/utils/FPUtil/CMakeLists.txt +++ b/libc/utils/FPUtil/CMakeLists.txt @@ -12,6 +12,7 @@ FloatOperations.h FloatProperties.h FPBits.h + BasicOperations.h ManipulationFunctions.h DEPENDS libc.utils.CPP.standalone_cpp diff --git a/libc/utils/UnitTest/CMakeLists.txt b/libc/utils/UnitTest/CMakeLists.txt --- a/libc/utils/UnitTest/CMakeLists.txt +++ b/libc/utils/UnitTest/CMakeLists.txt @@ -8,3 +8,10 @@ target_include_directories(LibcUnitTest PUBLIC ${LIBC_SOURCE_DIR}) add_dependencies(LibcUnitTest libc.utils.CPP.standalone_cpp) target_link_libraries(LibcUnitTest PUBLIC libc_test_utils) +set_target_properties( + LibcUnitTest + PROPERTIES + CXX_STANDARD 17 + CXX_STANDARD_REQUIRED True + CXX_EXTENSIONS True +) diff --git a/libc/utils/UnitTest/Test.h b/libc/utils/UnitTest/Test.h --- a/libc/utils/UnitTest/Test.h +++ b/libc/utils/UnitTest/Test.h @@ -78,7 +78,10 @@ // |Cond| on mismatched |LHS| and |RHS| types can potentially succeed because // of type promotion. template ::Value, ValType> = 0> + cpp::EnableIfType::Value || + cpp::IsSame::Value || + cpp::IsFloatingPointType::Value, + int> = 0> static bool test(RunContext &Ctx, TestCondition Cond, ValType LHS, ValType RHS, const char *LHSStr, const char *RHSStr, const char *File, unsigned long Line) { diff --git a/libc/utils/UnitTest/Test.cpp b/libc/utils/UnitTest/Test.cpp --- a/libc/utils/UnitTest/Test.cpp +++ b/libc/utils/UnitTest/Test.cpp @@ -8,7 +8,9 @@ #include "Test.h" +#include "utils/FPUtil/FPBits.h" #include "utils/testutils/ExecuteFunction.h" +#include "utils/testutils/StringExtras.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/raw_ostream.h" @@ -36,77 +38,126 @@ bool test(RunContext &Ctx, TestCondition Cond, ValType LHS, ValType RHS, const char *LHSStr, const char *RHSStr, const char *File, unsigned long Line) { + std::string LHSHex, RHSHex; + if constexpr (cpp::IsFloatingPointType::Value) { + LHSHex = llvm::utohexstr(fputil::FPBits(LHS).bitsAsUInt()); + RHSHex = llvm::utohexstr(fputil::FPBits(RHS).bitsAsUInt()); + } else if constexpr (cpp::IsIntegral::Value || + cpp::IsSame::Value) { + LHSHex = llvm::utohexstr(LHS); + RHSHex = llvm::utohexstr(RHS); + } + + constexpr bool printHex = cpp::IsIntegral::Value || + cpp::IsSame::Value || + cpp::IsFloatingPointType::Value; + switch (Cond) { case Cond_EQ: if (LHS == RHS) return true; Ctx.markFail(); - llvm::outs() << File << ":" << Line << ": FAILURE\n" - << " Expected: " << LHSStr << '\n' - << " Which is: " << LHS << '\n' - << "To be equal to: " << RHSStr << '\n' - << " Which is: " << RHS << '\n'; - + testutils::outs() << File << ":" << Line << ": FAILURE\n" + << " Expected: " << LHSStr << '\n' + << " Which is: " << LHS << '\n'; + if constexpr (printHex) { + testutils::outs() << "In hexadecimal: " << LHSHex << '\n'; + } + testutils::outs() << "To be equal to: " << RHSStr << '\n' + << " Which is: " << RHS << '\n'; + if constexpr (printHex) { + testutils::outs() << "In hexadecimal: " << RHSHex << '\n'; + } return false; case Cond_NE: if (LHS != RHS) return true; Ctx.markFail(); - llvm::outs() << File << ":" << Line << ": FAILURE\n" - << " Expected: " << LHSStr << '\n' - << " Which is: " << LHS << '\n' - << "To be not equal to: " << RHSStr << '\n' - << " Which is: " << RHS << '\n'; + testutils::outs() << File << ":" << Line << ": FAILURE\n" + << " Expected: " << LHSStr << '\n' + << " Which is: " << LHS << '\n'; + if constexpr (printHex) { + testutils::outs() << " In hexadecimal: " << LHSHex << '\n'; + } + testutils::outs() << "To be not equal to: " << RHSStr << '\n' + << " Which is: " << RHS << '\n'; + if constexpr (printHex) { + testutils::outs() << " In hexadecimal: " << RHSHex << '\n'; + } return false; case Cond_LT: if (LHS < RHS) return true; Ctx.markFail(); - llvm::outs() << File << ":" << Line << ": FAILURE\n" - << " Expected: " << LHSStr << '\n' - << " Which is: " << LHS << '\n' - << "To be less than: " << RHSStr << '\n' - << " Which is: " << RHS << '\n'; + testutils::outs() << File << ":" << Line << ": FAILURE\n" + << " Expected: " << LHSStr << '\n' + << " Which is: " << LHS << '\n'; + if constexpr (printHex) { + testutils::outs() << " In hexadecimal: " << LHSHex << '\n'; + } + testutils::outs() << "To be less than: " << RHSStr << '\n' + << " Which is: " << RHS << '\n'; + if constexpr (printHex) { + testutils::outs() << " In hexadecimal: " << RHSHex << '\n'; + } return false; case Cond_LE: if (LHS <= RHS) return true; Ctx.markFail(); - llvm::outs() << File << ":" << Line << ": FAILURE\n" - << " Expected: " << LHSStr << '\n' - << " Which is: " << LHS << '\n' - << "To be less than or equal to: " << RHSStr << '\n' - << " Which is: " << RHS << '\n'; + testutils::outs() << File << ":" << Line << ": FAILURE\n" + << " Expected: " << LHSStr << '\n' + << " Which is: " << LHS << '\n'; + if constexpr (printHex) { + testutils::outs() << " In hexadecimal: " << LHSHex << '\n'; + } + testutils::outs() << "To be less than or equal to: " << RHSStr << '\n' + << " Which is: " << RHS << '\n'; + if constexpr (printHex) { + testutils::outs() << " In hexadecimal: " << RHSHex << '\n'; + } return false; case Cond_GT: if (LHS > RHS) return true; Ctx.markFail(); - llvm::outs() << File << ":" << Line << ": FAILURE\n" - << " Expected: " << LHSStr << '\n' - << " Which is: " << LHS << '\n' - << "To be greater than: " << RHSStr << '\n' - << " Which is: " << RHS << '\n'; + testutils::outs() << File << ":" << Line << ": FAILURE\n" + << " Expected: " << LHSStr << '\n' + << " Which is: " << LHS << '\n'; + if constexpr (printHex) { + testutils::outs() << " In hexadecimal: " << LHSHex << '\n'; + } + testutils::outs() << "To be greater than: " << RHSStr << '\n' + << " Which is: " << RHS << '\n'; + if constexpr (printHex) { + testutils::outs() << " In hexadecimal: " << RHSHex << '\n'; + } return false; case Cond_GE: if (LHS >= RHS) return true; Ctx.markFail(); - llvm::outs() << File << ":" << Line << ": FAILURE\n" - << " Expected: " << LHSStr << '\n' - << " Which is: " << LHS << '\n' - << "To be greater than or equal to: " << RHSStr << '\n' - << " Which is: " << RHS << '\n'; + testutils::outs() << File << ":" << Line << ": FAILURE\n" + << " Expected: " << LHSStr << '\n' + << " Which is: " << LHS << '\n'; + if constexpr (printHex) { + testutils::outs() << " In hexadecimal: " << LHSHex << '\n'; + } + testutils::outs() << "To be greater than or equal to: " << RHSStr << '\n' + << " Which is: " << RHS << '\n'; + if constexpr (printHex) { + testutils::outs() << " In hexadecimal: " << RHSHex << '\n'; + } return false; default: Ctx.markFail(); - llvm::outs() << "Unexpected test condition.\n"; + testutils::outs() << "Unexpected test condition.\n"; return false; } } @@ -218,6 +269,26 @@ unsigned long long RHS, const char *LHSStr, const char *RHSStr, const char *File, unsigned long Line); +template bool Test::test<__uint128_t, 0>(RunContext &Ctx, TestCondition Cond, + __uint128_t LHS, __uint128_t RHS, + const char *LHSStr, const char *RHSStr, + const char *File, unsigned long Line); + +template bool Test::test(RunContext &Ctx, TestCondition Cond, + float LHS, float RHS, const char *LHSStr, + const char *RHSStr, const char *File, + unsigned long Line); + +template bool Test::test(RunContext &Ctx, TestCondition Cond, + double LHS, double RHS, const char *LHSStr, + const char *RHSStr, const char *File, + unsigned long Line); + +template bool Test::test(RunContext &Ctx, TestCondition Cond, + long double LHS, long double RHS, + const char *LHSStr, const char *RHSStr, + const char *File, unsigned long Line); + bool Test::testStrEq(RunContext &Ctx, const char *LHS, const char *RHS, const char *LHSStr, const char *RHSStr, const char *File, unsigned long Line) { @@ -239,9 +310,9 @@ return true; Ctx.markFail(); - llvm::outs() << File << ":" << Line << ": FAILURE\n" - << "Failed to match " << LHSStr << " against " << RHSStr - << ".\n"; + testutils::outs() << File << ":" << Line << ": FAILURE\n" + << "Failed to match " << LHSStr << " against " << RHSStr + << ".\n"; testutils::StreamWrapper OutsWrapper = testutils::outs(); Matcher.explainError(OutsWrapper); return false; @@ -254,22 +325,23 @@ if (const char *error = Result.getError()) { Ctx.markFail(); - llvm::outs() << File << ":" << Line << ": FAILURE\n" << error << '\n'; + testutils::outs() << File << ":" << Line << ": FAILURE\n" << error << '\n'; return false; } if (Result.timedOut()) { Ctx.markFail(); - llvm::outs() << File << ":" << Line << ": FAILURE\n" - << "Process timed out after " << 500 << " milliseconds.\n"; + testutils::outs() << File << ":" << Line << ": FAILURE\n" + << "Process timed out after " << 500 + << " milliseconds.\n"; return false; } if (Result.exitedNormally()) { Ctx.markFail(); - llvm::outs() << File << ":" << Line << ": FAILURE\n" - << "Expected " << LHSStr - << " to be killed by a signal\nBut it exited normally!\n"; + testutils::outs() << File << ":" << Line << ": FAILURE\n" + << "Expected " << LHSStr + << " to be killed by a signal\nBut it exited normally!\n"; return false; } @@ -280,13 +352,14 @@ using testutils::signalAsString; Ctx.markFail(); - llvm::outs() << File << ":" << Line << ": FAILURE\n" - << " Expected: " << LHSStr << '\n' - << "To be killed by signal: " << Signal << '\n' - << " Which is: " << signalAsString(Signal) << '\n' - << " But it was killed by: " << KilledBy << '\n' - << " Which is: " << signalAsString(KilledBy) - << '\n'; + testutils::outs() << File << ":" << Line << ": FAILURE\n" + << " Expected: " << LHSStr << '\n' + << "To be killed by signal: " << Signal << '\n' + << " Which is: " << signalAsString(Signal) + << '\n' + << " But it was killed by: " << KilledBy << '\n' + << " Which is: " << signalAsString(KilledBy) + << '\n'; return false; } @@ -298,23 +371,24 @@ if (const char *error = Result.getError()) { Ctx.markFail(); - llvm::outs() << File << ":" << Line << ": FAILURE\n" << error << '\n'; + testutils::outs() << File << ":" << Line << ": FAILURE\n" << error << '\n'; return false; } if (Result.timedOut()) { Ctx.markFail(); - llvm::outs() << File << ":" << Line << ": FAILURE\n" - << "Process timed out after " << 500 << " milliseconds.\n"; + testutils::outs() << File << ":" << Line << ": FAILURE\n" + << "Process timed out after " << 500 + << " milliseconds.\n"; return false; } if (!Result.exitedNormally()) { Ctx.markFail(); - llvm::outs() << File << ":" << Line << ": FAILURE\n" - << "Expected " << LHSStr << '\n' - << "to exit with exit code " << ExitCode << '\n' - << "But it exited abnormally!\n"; + testutils::outs() << File << ":" << Line << ": FAILURE\n" + << "Expected " << LHSStr << '\n' + << "to exit with exit code " << ExitCode << '\n' + << "But it exited abnormally!\n"; return false; } @@ -323,11 +397,11 @@ return true; Ctx.markFail(); - llvm::outs() << File << ":" << Line << ": FAILURE\n" - << "Expected exit code of: " << LHSStr << '\n' - << " Which is: " << ActualExit << '\n' - << " To be equal to: " << RHSStr << '\n' - << " Which is: " << ExitCode << '\n'; + testutils::outs() << File << ":" << Line << ": FAILURE\n" + << "Expected exit code of: " << LHSStr << '\n' + << " Which is: " << ActualExit << '\n' + << " To be equal to: " << RHSStr << '\n' + << " Which is: " << ExitCode << '\n'; return false; } diff --git a/libc/utils/testutils/CMakeLists.txt b/libc/utils/testutils/CMakeLists.txt --- a/libc/utils/testutils/CMakeLists.txt +++ b/libc/utils/testutils/CMakeLists.txt @@ -7,6 +7,7 @@ libc_test_utils StreamWrapper.cpp StreamWrapper.h + StringExtras.h ${EFFile} ExecuteFunction.h ${FDReaderFile} @@ -14,3 +15,4 @@ LINK_COMPONENTS Support ) +target_include_directories(libc_test_utils PUBLIC ${LIBC_SOURCE_DIR}) diff --git a/libc/utils/testutils/StreamWrapper.cpp b/libc/utils/testutils/StreamWrapper.cpp --- a/libc/utils/testutils/StreamWrapper.cpp +++ b/libc/utils/testutils/StreamWrapper.cpp @@ -7,11 +7,23 @@ //===----------------------------------------------------------------------===// #include "StreamWrapper.h" +#include "StringExtras.h" +#include "utils/FPUtil/FPBits.h" + +#include "llvm/ADT/StringRef.h" #include "llvm/Support/raw_ostream.h" #include #include #include +std::string paddingZero(const std::string &s, size_t N) { + if (s.size() >= N) { + return s; + } + + return std::string(N - s.size(), '0') + s; +} + namespace __llvm_libc { namespace testutils { @@ -24,6 +36,58 @@ return *this; } +// To display the decimals of a __uint128_t, we divide the input into 3 chunks +// mod 10^13, since log10(2^128) ~ 38.5 < 39. Furthermore, log2(10^13) ~ 43.2 +// < 64, so each chunk can be properly contained with uint64_t and displayed +// natively. +// TODO: support hex and oct format +template <> +StreamWrapper &StreamWrapper::operator<<<__uint128_t>(__uint128_t t) { + assert(OS); + llvm::raw_ostream &Stream = *reinterpret_cast(OS); + + const __uint128_t chunk = 10'000'000'000'000ULL; + uint64_t bottom = static_cast(t % chunk); + t /= chunk; + uint64_t middle = static_cast(t % chunk); + uint64_t top = static_cast(t / chunk); + + if (top) { + Stream << top << paddingZero(std::to_string(middle), 13) + << paddingZero(std::to_string(bottom), 13); + } else if (middle) { + Stream << middle << paddingZero(std::to_string(bottom), 13); + } else { + Stream << bottom; + } + + return *this; +} + +// Adapted from write_double in llvm/lib/Support/NativeFormatting.cpp +template <> +StreamWrapper &StreamWrapper::operator<<(long double t) { + assert(OS); + llvm::raw_ostream &Stream = *reinterpret_cast(OS); + + fputil::FPBits bits(t); + + if (bits.isNaN()) { + Stream << "nan"; + } else if (bits.isInf()) { + if (bits.sign) { + Stream << "-"; + } + Stream << "INF"; + } else { + char Buf[32]; + snprintf(Buf, sizeof(Buf), "%.30Le", t); + Stream << Buf; + } + + return *this; +} + template StreamWrapper &StreamWrapper::operator<<(void *t); template StreamWrapper &StreamWrapper::operator<<(const char *t); template StreamWrapper &StreamWrapper::operator<<(char *t); @@ -43,6 +107,11 @@ StreamWrapper::operator<<(unsigned long long t); template StreamWrapper &StreamWrapper::operator<<(bool t); template StreamWrapper &StreamWrapper::operator<<(std::string t); +template StreamWrapper &StreamWrapper::operator<<(float t); +template StreamWrapper &StreamWrapper::operator<<(double t); + +template StreamWrapper & + StreamWrapper::operator<<(llvm::StringRef t); } // namespace testutils } // namespace __llvm_libc diff --git a/libc/utils/testutils/StringExtras.h b/libc/utils/testutils/StringExtras.h new file mode 100644 --- /dev/null +++ b/libc/utils/testutils/StringExtras.h @@ -0,0 +1,40 @@ +//===-- StringExtras.h -----------------------------------------*- 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_UTILS_TESTUTILS_STRINGEXTRAS_H +#define LLVM_LIBC_UTILS_TESTUTILS_STRINGEXTRAS_H + +#include "utils/CPP/TypeTraits.h" + +#include "llvm/ADT/StringExtras.h" + +namespace llvm { + +// Adapted from llvm/ADT/StringExtras.h +template ::Value, int> = 0> +std::string utohexstr(T X, bool LowerCase = false) { + char Buffer[33]; + char *BufPtr = std::end(Buffer); + + if (X == 0) + *--BufPtr = '0'; + + while (X) { + unsigned char Mod = static_cast(X) & 15; + *--BufPtr = llvm::hexdigit(Mod, LowerCase); + X >>= 4; + } + + return std::string(BufPtr, std::end(Buffer)); +} + +} // namespace llvm + +#endif // LLVM_LIBC_UTILS_TESTUTILS_STRINGEXTRAS_H