diff --git a/core/libs/prx/libc/CMakeLists.txt b/core/libs/prx/libc/CMakeLists.txt index 30aaf71ba88f..1c1070661ea9 100644 --- a/core/libs/prx/libc/CMakeLists.txt +++ b/core/libs/prx/libc/CMakeLists.txt @@ -11,6 +11,7 @@ target_sources(libc PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/Multibyte.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/StringExtensions.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Math.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/Rounding.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Time.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/FileAndHeap.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/StdioExtras.cpp @@ -37,6 +38,9 @@ target_sources(libc PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/exception/Exports.cpp ) +set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}/src/Rounding.cpp + TARGET_DIRECTORY libc PROPERTIES COMPILE_OPTIONS "-frounding-math") + target_include_directories(libc PRIVATE ${LIBS_INCLUDE_DIR} ) @@ -46,6 +50,16 @@ if(WIN32) endif() if(BUILD_TESTING) + add_test_executable(guest_rounding_tests ${LIBS_INCLUDE_DIR}/tests/GuestRounding.cpp) + target_include_directories(guest_rounding_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_rounding_tests PRIVATE libc) + configure_windows_unwind(guest_rounding_tests) + add_test(NAME guest_rounding COMMAND guest_rounding_tests) + if(WIN32) + set_tests_properties(guest_rounding PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() + add_test_executable(guest_time_conversion_tests ${LIBS_INCLUDE_DIR}/tests/GuestTimeConversion.cpp) target_include_directories(guest_time_conversion_tests PRIVATE ${LIBS_INCLUDE_DIR}) target_link_libraries(guest_time_conversion_tests PRIVATE libc) diff --git a/core/libs/prx/libc/src/Rounding.cpp b/core/libs/prx/libc/src/Rounding.cpp new file mode 100644 index 000000000000..e8b0d5ca3ce1 --- /dev/null +++ b/core/libs/prx/libc/src/Rounding.cpp @@ -0,0 +1,24 @@ +#include "prx/libc/include/general/VabiMacros.hpp" + +#include +#include +#include + +static_assert(sizeof(long double) == 16 && std::numeric_limits::digits == 64); +static_assert(sizeof(long long) == sizeof(std::int64_t)); + +extern "C" { + +double APS5_VABI rint_nid_postfix(double value) { return std::rint(value); } +float APS5_VABI rintf_nid_postfix(float value) { return std::rint(value); } +long double APS5_VABI rintl_nid_postfix(long double value) { return std::rint(value); } + +std::int64_t APS5_VABI lrint_nid_postfix(double value) { return std::llrint(value); } +std::int64_t APS5_VABI lrintf_nid_postfix(float value) { return std::llrint(value); } +std::int64_t APS5_VABI lrintl_nid_postfix(long double value) { return std::llrint(value); } + +std::int64_t APS5_VABI llrint_nid_postfix(double value) { return std::llrint(value); } +std::int64_t APS5_VABI llrintf_nid_postfix(float value) { return std::llrint(value); } +std::int64_t APS5_VABI llrintl_nid_postfix(long double value) { return std::llrint(value); } + +} diff --git a/core/libs/tests/GuestRounding.cpp b/core/libs/tests/GuestRounding.cpp new file mode 100644 index 000000000000..f1d0f73aadd8 --- /dev/null +++ b/core/libs/tests/GuestRounding.cpp @@ -0,0 +1,122 @@ +#include "prx/libc/include/general/VabiMacros.hpp" + +#include +#include +#include +#include +#include +#include +#include + +extern "C" { +double APS5_VABI rint_nid_postfix(double); +float APS5_VABI rintf_nid_postfix(float); +long double APS5_VABI rintl_nid_postfix(long double); +std::int64_t APS5_VABI lrint_nid_postfix(double); +std::int64_t APS5_VABI lrintf_nid_postfix(float); +std::int64_t APS5_VABI lrintl_nid_postfix(long double); +std::int64_t APS5_VABI llrint_nid_postfix(double); +std::int64_t APS5_VABI llrintf_nid_postfix(float); +std::int64_t APS5_VABI llrintl_nid_postfix(long double); +} + +namespace { + +void Require(bool condition, const char* message) { + if (!condition) { + std::fprintf(stderr, "Guest rounding: %s\n", message); + std::abort(); + } +} + +struct RoundingCase { + long double input; + std::int64_t expected[4]; +}; + +constexpr int modes[] = {FE_TONEAREST, FE_DOWNWARD, FE_UPWARD, FE_TOWARDZERO}; +constexpr RoundingCase cases[] = { + {1.5L, {2, 1, 2, 1}}, + {2.5L, {2, 2, 3, 2}}, + {-1.5L, {-2, -2, -1, -1}}, + {-2.5L, {-2, -3, -2, -2}}, + {0.25L, {0, 0, 1, 0}}, + {-0.25L, {0, -1, 0, 0}}, + {0.L, {0, 0, 0, 0}}, + {3.L, {3, 3, 3, 3}}, +}; + +template +void CheckFloating(TValue (APS5_VABI *function)(TValue)) { + for (std::size_t mode = 0; mode < 4; ++mode) { + Require(std::fesetround(modes[mode]) == 0, "cannot set rounding mode"); + for (const auto& test : cases) { + const auto result = function(static_cast(test.input)); + if (result != static_cast(test.expected[mode])) { + std::fprintf(stderr, "floating width %zu, mode %d: input %g, expected %lld, got %g\n", sizeof(TValue), modes[mode], static_cast(test.input), static_cast(test.expected[mode]), static_cast(result)); + } + Require(result == static_cast(test.expected[mode]), "floating result ignores rounding mode"); + if (result == 0) Require(std::signbit(result) == std::signbit(test.input), "rounded zero lost its sign"); + } + const auto negativeZero = function(static_cast(-0.L)); + Require(negativeZero == 0 && std::signbit(negativeZero), "negative zero not preserved"); + const auto infinity = std::numeric_limits::infinity(); + Require(function(infinity) == infinity && function(-infinity) == -infinity, "infinity not preserved"); + Require(std::isnan(function(std::numeric_limits::quiet_NaN())), "NaN not preserved"); + std::feclearexcept(FE_ALL_EXCEPT); + Require(function(static_cast(1.5L)) == static_cast(cases[0].expected[mode]), "inexact result incorrect"); + Require(std::fetestexcept(FE_INEXACT) != 0, "rint did not raise inexact"); + Require(std::fegetround() == modes[mode], "rounding mode changed"); + } +} + +template +void CheckInteger(std::int64_t (APS5_VABI *function)(TValue)) { + for (std::size_t mode = 0; mode < 4; ++mode) { + Require(std::fesetround(modes[mode]) == 0, "cannot set rounding mode"); + for (const auto& test : cases) { + Require(function(static_cast(test.input)) == test.expected[mode], "integer result ignores rounding mode"); + } + Require(function(static_cast(0x1p40L)) == INT64_C(1099511627776), "positive result narrowed to 32 bits"); + Require(function(static_cast(-0x1p40L)) == -INT64_C(1099511627776), "negative result narrowed to 32 bits"); + Require(function(static_cast(-0x1p63L)) == INT64_MIN, "minimum 64-bit result incorrect"); + std::feclearexcept(FE_ALL_EXCEPT); + (void)function(static_cast(1.5L)); + Require(std::fetestexcept(FE_INEXACT) != 0, "integer conversion did not raise inexact"); + for (const auto invalid : {std::numeric_limits::infinity(), std::numeric_limits::quiet_NaN(), static_cast(0x1p63L)}) { + std::feclearexcept(FE_ALL_EXCEPT); + (void)function(invalid); + Require(std::fetestexcept(FE_INVALID) != 0, "invalid conversion did not raise invalid"); + } + Require(std::fegetround() == modes[mode], "rounding mode changed"); + } +} + +void CheckExtendedPrecision() { + Require(std::fesetround(FE_TONEAREST) == 0, "cannot set nearest rounding"); + const long double input = 0x1p62L + 1.5L; + constexpr auto expected = INT64_C(4611686018427387906); + Require(rintl_nid_postfix(input) == 0x1p62L + 2.L, "rintl narrowed its input to double"); + Require(lrintl_nid_postfix(input) == expected && llrintl_nid_postfix(input) == expected, "extended conversion narrowed its input to double"); + const long double maximum = 0x1p63L - 1.L; + Require(lrintl_nid_postfix(maximum) == INT64_MAX && llrintl_nid_postfix(maximum) == INT64_MAX, "maximum 64-bit integer conversion incorrect"); +} + +} + +int main() { + std::fenv_t saved; + Require(std::fegetenv(&saved) == 0, "cannot save floating environment"); + CheckFloating(rint_nid_postfix); + CheckFloating(rintf_nid_postfix); + CheckFloating(rintl_nid_postfix); + CheckInteger(lrint_nid_postfix); + CheckInteger(lrintf_nid_postfix); + CheckInteger(lrintl_nid_postfix); + CheckInteger(llrint_nid_postfix); + CheckInteger(llrintf_nid_postfix); + CheckInteger(llrintl_nid_postfix); + CheckExtendedPrecision(); + Require(std::fesetenv(&saved) == 0, "cannot restore floating environment"); + return 0; +}