diff --git a/core/libs/prx/libc/CMakeLists.txt b/core/libs/prx/libc/CMakeLists.txt index 98374869c8f1..084438118e42 100644 --- a/core/libs/prx/libc/CMakeLists.txt +++ b/core/libs/prx/libc/CMakeLists.txt @@ -183,6 +183,7 @@ target_sources(libc PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/MathGamma.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/MathExact.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/VersionedExtendedPrecision.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/MathExpf.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Bessel.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/J0f.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/J1f.cpp @@ -295,6 +296,8 @@ set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}/src/MathExact.cpp TARGET_DIRECTORY libc PROPERTIES COMPILE_OPTIONS "-frounding-math") set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}/src/VersionedExtendedPrecision.cpp TARGET_DIRECTORY libc PROPERTIES COMPILE_OPTIONS "-frounding-math") +set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}/src/MathExpf.cpp + TARGET_DIRECTORY libc PROPERTIES COMPILE_OPTIONS "-frounding-math;-ffp-contract=off") if(ANYPS5_ENABLE_TSAN) set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}/src/exception/Exports.cpp diff --git a/core/libs/prx/libc/src/Math.cpp b/core/libs/prx/libc/src/Math.cpp index f784eb455250..8da47240b4f4 100644 --- a/core/libs/prx/libc/src/Math.cpp +++ b/core/libs/prx/libc/src/Math.cpp @@ -18,6 +18,18 @@ /* e_rem_pio2f.c, k_cosf.c and s_cbrtf.c: * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. * Debugged and optimized by Bruce D. Evans. */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ +/* Derived from FreeBSD lib/msun/src/e_expf.c (release/11.4.0); lib/msun/src/s_log1pf.c (release/11.4.0) */ +/* Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. */ /* * ==================================================== * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. @@ -1983,7 +1995,74 @@ double APS5_VABI log1p_nid_postfix(double x) { } return k * ln2_hi - ((hfsq - (s * (hfsq + R) + (k * ln2_lo + c))) - f); } -float APS5_VABI log1pf_nid_postfix(float x) { return std::log1p(x); } +static float Log1pfMsun(float x) { + constexpr float Ln2High = 6.9313812256e-01f, Ln2Low = 9.0580006145e-06f, Two25 = 3.355443200e+07f; + constexpr float Lp1 = 6.6666668653e-01f, Lp2 = 4.0000000596e-01f, Lp3 = 2.8571429849e-01f, Lp4 = 2.2222198546e-01f; + constexpr float Lp5 = 1.8183572590e-01f, Lp6 = 1.5313838422e-01f, Lp7 = 1.4798198640e-01f; + const std::int32_t hx = std::bit_cast(x); + const std::int32_t ax = hx & 0x7fffffff; + std::int32_t k = 1; + std::int32_t hu = 0; + float f = 0.0f; + float c = 0.0f; + if (hx < 0x3ed413d0) { + if (ax < 0x38000000) { + volatile float two25 = Two25; + if (two25 + x > 0.0f && ax < 0x33800000) return x; + return x - x * x * 0.5f; + } + if (hx > 0 || hx <= static_cast(0xbe95f619u)) { + k = 0; + f = x; + hu = 1; + } + } + if (k != 0) { + float u; + if (hx < 0x5a000000) { + volatile float sum = 1.0f + x; + u = sum; + hu = std::bit_cast(u); + k = (hu >> 23) - 127; + c = k > 0 ? 1.0f - (u - x) : x - (u - 1.0f); + c /= u; + } else { + u = x; + hu = std::bit_cast(u); + k = (hu >> 23) - 127; + c = 0.0f; + } + hu &= 0x007fffff; + if (hu < 0x3504f4) { + u = std::bit_cast(hu | 0x3f800000); + } else { + k += 1; + u = std::bit_cast(hu | 0x3f000000); + hu = (0x00800000 - hu) >> 2; + } + f = u - 1.0f; + } + const float hfsq = 0.5f * f * f; + if (hu == 0) { + if (f == 0.0f) { + if (k == 0) return 0.0f; + c += static_cast(k) * Ln2Low; + return static_cast(k) * Ln2High + c; + } + const float R = hfsq * (1.0f - 0.66666666666666666f * f); + if (k == 0) return f - R; + return static_cast(k) * Ln2High - ((R - (static_cast(k) * Ln2Low + c)) - f); + } + const float s = f / (2.0f + f); + const float z = s * s; + const float R = z * (Lp1 + z * (Lp2 + z * (Lp3 + z * (Lp4 + z * (Lp5 + z * (Lp6 + z * Lp7)))))); + if (k == 0) return f - (hfsq - s * (hfsq + R)); + return static_cast(k) * Ln2High - ((hfsq - (s * (hfsq + R) + (static_cast(k) * Ln2Low + c))) - f); +} +float APS5_VABI log1pf_nid_postfix(float x) { + if (!std::isfinite(x) || x <= -1.0f) return std::log1p(x); + return Log1pfMsun(x); +} static double Expm1(double x) { const double tiny = 1.0e-300; const double o_threshold = 7.09782712893383973096e+02; @@ -4623,11 +4702,6 @@ double APS5_VABI __powidf2_nid_postfix(double base, int exponent) { } return reciprocal ? 1.0 / result : result; } -float APS5_VABI expf_nid_postfix(float x) { - const float result = std::exp(x); - if (std::isfinite(x) && (std::isinf(result) || result == 0.0f)) errno = ERANGE; - return result; -} static float LogfMsun(float x, bool fused) { const float ln2_hi = static_cast(6.9313812256e-01); const float ln2_lo = static_cast(9.0580006145e-06); diff --git a/core/libs/prx/libc/src/MathExpf.cpp b/core/libs/prx/libc/src/MathExpf.cpp new file mode 100644 index 000000000000..349bac25cbf3 --- /dev/null +++ b/core/libs/prx/libc/src/MathExpf.cpp @@ -0,0 +1,85 @@ +/* Adapted from FreeBSD msun e_expf.c: https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/msun/src/e_expf.c */ +/* Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "prx/libc/include/Api.hpp" +#include +#include +#include + +extern "C" { + +float APS5_VABI expf_nid_postfix(float x) { + static constexpr float halF[2] = {0.5f, -0.5f}; + static constexpr float ln2HI[2] = {0x1.62e4000000000p-1f, -0x1.62e4000000000p-1f}; + static constexpr float ln2LO[2] = {0x1.7f7d1c0000000p-20f, -0x1.7f7d1c0000000p-20f}; + static volatile float huge = 0x1.93e5940000000p+99f; + static volatile float twom100 = 0x1.0000000000000p-100f; + const float invln2 = 0x1.7154760000000p+0f; + const float P1 = 0x1.55551e0000000p-3f; + const float P2 = -0x1.6aa42a0000000p-9f; + float hi = 0.0f; + float lo = 0.0f; + std::int32_t k = 0; + std::uint32_t hx = std::bit_cast(x); + const std::int32_t xsb = (hx >> 31) & 1; + hx &= 0x7fffffff; + if (hx >= 0x42b17218) { + if (hx > 0x7f800000) { + return x + x; + } + if (hx == 0x7f800000) { + return xsb == 0 ? x : 0.0f; + } + if (x > 0x1.62e3000000000p+6f) { + _Feraise_nid_postfix(0x08); + return std::numeric_limits::infinity(); + } + if (x < -0x1.9fe36a0000000p+6f) { + _Feraise_nid_postfix(0x10); + return twom100 * twom100; + } + } + if (x == 1.0f) return 0x1.5bf0a8p+1f; + if (hx > 0x3eb17218) { + if (hx < 0x3f851592) { + hi = x - ln2HI[xsb]; + lo = ln2LO[xsb]; + k = 1 - xsb - xsb; + } else { + k = static_cast(invln2 * x + halF[xsb]); + const float t = static_cast(k); + hi = x - t * ln2HI[0]; + lo = t * ln2LO[0]; + } + x = hi - lo; + } else { + const volatile float trigger = huge + x; + if (trigger > 1.0f && hx < 0x39000000) return 1.0f + x; + } + const float t = x * x; + const float twopk = std::bit_cast(static_cast(0x7f + (k >= -125 ? k : k + 100)) << 23); + const float c = x - t * (P1 + t * P2); + if (k == 0) { + return 1.0f + (x - (x * c) / (c - 2.0f)); + } + const float y = 1.0f + (hi - (lo - (x * c) / (2.0f - c))); + if (k >= -125) { + if (k == 128) { + return (y + y) * 0x1p127f; + } + return y * twopk; + } + return y * twopk * twom100; +} + +} diff --git a/core/libs/prx/libc/tests/cmake/guest_expf_environment.cmake b/core/libs/prx/libc/tests/cmake/guest_expf_environment.cmake new file mode 100644 index 000000000000..5e2d97e04e5b --- /dev/null +++ b/core/libs/prx/libc/tests/cmake/guest_expf_environment.cmake @@ -0,0 +1,11 @@ +if(BUILD_TESTING) + add_test_executable(guest_expf_environment_tests ${LIBS_INCLUDE_DIR}/tests/GuestExpfEnvironment.cpp) + target_include_directories(guest_expf_environment_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_expf_environment_tests PRIVATE libc) + configure_windows_unwind(guest_expf_environment_tests) + add_test(NAME guest_expf_environment COMMAND guest_expf_environment_tests) + set_tests_properties(guest_expf_environment PROPERTIES TIMEOUT 30) + if(WIN32) + set_tests_properties(guest_expf_environment PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") + endif() +endif() diff --git a/core/libs/tests/GuestExpfEnvironment.cpp b/core/libs/tests/GuestExpfEnvironment.cpp new file mode 100644 index 000000000000..21e098057110 --- /dev/null +++ b/core/libs/tests/GuestExpfEnvironment.cpp @@ -0,0 +1,338 @@ +#include "prx/libc/include/Api.hpp" +#include "prx/libc/include/GuestCpu.hpp" +#include +#include +#include +#include + +struct ExpectedExp { + unsigned rounding; + std::uint32_t bits; + int flags; + int error; +}; + +struct ExpCase { + std::uint32_t input; + std::array expected; +}; + +static constexpr ExpCase Cases[] = { + {0x00000000u, {{ + {0x000u, 0x3f800000u, 0, 73}, + {0x400u, 0x3f800000u, 0, 73}, + {0x800u, 0x3f800000u, 0, 73}, + {0xc00u, 0x3f800000u, 0, 73}, + }}}, + {0x80000000u, {{ + {0x000u, 0x3f800000u, 0, 73}, + {0x400u, 0x3f800000u, 0, 73}, + {0x800u, 0x3f800000u, 0, 73}, + {0xc00u, 0x3f800000u, 0, 73}, + }}}, + {0x00000001u, {{ + {0x000u, 0x3f800000u, 34, 73}, + {0x400u, 0x3f800000u, 34, 73}, + {0x800u, 0x3f800001u, 34, 73}, + {0xc00u, 0x3f800000u, 34, 73}, + }}}, + {0x80000001u, {{ + {0x000u, 0x3f800000u, 34, 73}, + {0x400u, 0x3f7fffffu, 34, 73}, + {0x800u, 0x3f800000u, 34, 73}, + {0xc00u, 0x3f7fffffu, 34, 73}, + }}}, + {0x007fffffu, {{ + {0x000u, 0x3f800000u, 34, 73}, + {0x400u, 0x3f800000u, 34, 73}, + {0x800u, 0x3f800001u, 34, 73}, + {0xc00u, 0x3f800000u, 34, 73}, + }}}, + {0x807fffffu, {{ + {0x000u, 0x3f800000u, 34, 73}, + {0x400u, 0x3f7fffffu, 34, 73}, + {0x800u, 0x3f800000u, 34, 73}, + {0xc00u, 0x3f7fffffu, 34, 73}, + }}}, + {0x00800000u, {{ + {0x000u, 0x3f800000u, 32, 73}, + {0x400u, 0x3f800000u, 32, 73}, + {0x800u, 0x3f800001u, 32, 73}, + {0xc00u, 0x3f800000u, 32, 73}, + }}}, + {0x80800000u, {{ + {0x000u, 0x3f800000u, 32, 73}, + {0x400u, 0x3f7fffffu, 32, 73}, + {0x800u, 0x3f800000u, 32, 73}, + {0xc00u, 0x3f7fffffu, 32, 73}, + }}}, + {0x33800000u, {{ + {0x000u, 0x3f800000u, 32, 73}, + {0x400u, 0x3f800000u, 32, 73}, + {0x800u, 0x3f800001u, 32, 73}, + {0xc00u, 0x3f800000u, 32, 73}, + }}}, + {0x34000000u, {{ + {0x000u, 0x3f800001u, 32, 73}, + {0x400u, 0x3f800001u, 32, 73}, + {0x800u, 0x3f800001u, 32, 73}, + {0xc00u, 0x3f800001u, 32, 73}, + }}}, + {0xb3800000u, {{ + {0x000u, 0x3f7fffffu, 32, 73}, + {0x400u, 0x3f7fffffu, 32, 73}, + {0x800u, 0x3f7fffffu, 32, 73}, + {0xc00u, 0x3f7fffffu, 32, 73}, + }}}, + {0xb4000000u, {{ + {0x000u, 0x3f7ffffeu, 32, 73}, + {0x400u, 0x3f7ffffeu, 32, 73}, + {0x800u, 0x3f7ffffeu, 32, 73}, + {0xc00u, 0x3f7ffffeu, 32, 73}, + }}}, + {0x3e800000u, {{ + {0x000u, 0x3fa45af2u, 32, 73}, + {0x400u, 0x3fa45af1u, 32, 73}, + {0x800u, 0x3fa45af2u, 32, 73}, + {0xc00u, 0x3fa45af1u, 32, 73}, + }}}, + {0x3f000000u, {{ + {0x000u, 0x3fd3094cu, 32, 73}, + {0x400u, 0x3fd3094cu, 32, 73}, + {0x800u, 0x3fd3094du, 32, 73}, + {0xc00u, 0x3fd3094cu, 32, 73}, + }}}, + {0x3f7ffff0u, {{ + {0x000u, 0x402df84au, 32, 73}, + {0x400u, 0x402df849u, 32, 73}, + {0x800u, 0x402df84au, 32, 73}, + {0xc00u, 0x402df849u, 32, 73}, + }}}, + {0x3f800000u, {{ + {0x000u, 0x402df854u, 0, 73}, + {0x400u, 0x402df854u, 0, 73}, + {0x800u, 0x402df854u, 0, 73}, + {0xc00u, 0x402df854u, 0, 73}, + }}}, + {0x3f800006u, {{ + {0x000u, 0x402df85cu, 32, 73}, + {0x400u, 0x402df85cu, 32, 73}, + {0x800u, 0x402df85du, 32, 73}, + {0xc00u, 0x402df85cu, 32, 73}, + }}}, + {0x3f800010u, {{ + {0x000u, 0x402df86au, 32, 73}, + {0x400u, 0x402df86au, 32, 73}, + {0x800u, 0x402df86bu, 32, 73}, + {0xc00u, 0x402df86au, 32, 73}, + }}}, + {0xbf800000u, {{ + {0x000u, 0x3ebc5ab2u, 32, 73}, + {0x400u, 0x3ebc5ab1u, 32, 73}, + {0x800u, 0x3ebc5ab2u, 32, 73}, + {0xc00u, 0x3ebc5ab2u, 32, 73}, + }}}, + {0x40480000u, {{ + {0x000u, 0x41b61444u, 32, 73}, + {0x400u, 0x41b61444u, 32, 73}, + {0x800u, 0x41b61444u, 32, 73}, + {0xc00u, 0x41b61444u, 32, 73}, + }}}, + {0xc0400000u, {{ + {0x000u, 0x3d4bed86u, 32, 73}, + {0x400u, 0x3d4bed86u, 32, 73}, + {0x800u, 0x3d4bed87u, 32, 73}, + {0xc00u, 0x3d4bed86u, 32, 73}, + }}}, + {0x42800000u, {{ + {0x000u, 0x6da12cc2u, 32, 73}, + {0x400u, 0x6da12cc1u, 32, 73}, + {0x800u, 0x6da12cc2u, 32, 73}, + {0xc00u, 0x6da12cc1u, 32, 73}, + }}}, + {0x42b00000u, {{ + {0x000u, 0x7ef882b7u, 32, 73}, + {0x400u, 0x7ef882b6u, 32, 73}, + {0x800u, 0x7ef882b7u, 32, 73}, + {0xc00u, 0x7ef882b6u, 32, 73}, + }}}, + {0xc2c80000u, {{ + {0x000u, 0x0000001bu, 48, 73}, + {0x400u, 0x0000001au, 48, 73}, + {0x800u, 0x0000001bu, 48, 73}, + {0xc00u, 0x0000001au, 48, 73}, + }}}, + {0x7f7fffffu, {{ + {0x000u, 0x7f800000u, 40, 34}, + {0x400u, 0x7f800000u, 40, 34}, + {0x800u, 0x7f800000u, 40, 34}, + {0xc00u, 0x7f800000u, 40, 34}, + }}}, + {0xff7fffffu, {{ + {0x000u, 0x00000000u, 48, 34}, + {0x400u, 0x00000000u, 48, 34}, + {0x800u, 0x00000001u, 48, 34}, + {0xc00u, 0x00000000u, 48, 34}, + }}}, + {0x7f800000u, {{ + {0x000u, 0x7f800000u, 0, 73}, + {0x400u, 0x7f800000u, 0, 73}, + {0x800u, 0x7f800000u, 0, 73}, + {0xc00u, 0x7f800000u, 0, 73}, + }}}, + {0xff800000u, {{ + {0x000u, 0x00000000u, 0, 73}, + {0x400u, 0x00000000u, 0, 73}, + {0x800u, 0x00000000u, 0, 73}, + {0xc00u, 0x00000000u, 0, 73}, + }}}, + {0x7fc00000u, {{ + {0x000u, 0x7fc00000u, 0, 73}, + {0x400u, 0x7fc00000u, 0, 73}, + {0x800u, 0x7fc00000u, 0, 73}, + {0xc00u, 0x7fc00000u, 0, 73}, + }}}, + {0xffc00000u, {{ + {0x000u, 0xffc00000u, 0, 73}, + {0x400u, 0xffc00000u, 0, 73}, + {0x800u, 0xffc00000u, 0, 73}, + {0xc00u, 0xffc00000u, 0, 73}, + }}}, + {0x7fc12345u, {{ + {0x000u, 0x7fc12345u, 0, 73}, + {0x400u, 0x7fc12345u, 0, 73}, + {0x800u, 0x7fc12345u, 0, 73}, + {0xc00u, 0x7fc12345u, 0, 73}, + }}}, + {0xffc12345u, {{ + {0x000u, 0xffc12345u, 0, 73}, + {0x400u, 0xffc12345u, 0, 73}, + {0x800u, 0xffc12345u, 0, 73}, + {0xc00u, 0xffc12345u, 0, 73}, + }}}, + {0x7f812345u, {{ + {0x000u, 0x7fc12345u, 1, 73}, + {0x400u, 0x7fc12345u, 1, 73}, + {0x800u, 0x7fc12345u, 1, 73}, + {0xc00u, 0x7fc12345u, 1, 73}, + }}}, + {0xff812345u, {{ + {0x000u, 0xffc12345u, 1, 73}, + {0x400u, 0xffc12345u, 1, 73}, + {0x800u, 0xffc12345u, 1, 73}, + {0xc00u, 0xffc12345u, 1, 73}, + }}}, + {0x42b17216u, {{ + {0x000u, 0x7f7fff04u, 32, 73}, + {0x400u, 0x7f7fff04u, 32, 73}, + {0x800u, 0x7f7fff05u, 32, 73}, + {0xc00u, 0x7f7fff04u, 32, 73}, + }}}, + {0x42b17217u, {{ + {0x000u, 0x7f7fff84u, 32, 73}, + {0x400u, 0x7f7fff84u, 32, 73}, + {0x800u, 0x7f7fff85u, 32, 73}, + {0xc00u, 0x7f7fff84u, 32, 73}, + }}}, + {0x42b17218u, {{ + {0x000u, 0x7f800000u, 40, 34}, + {0x400u, 0x7f800000u, 40, 34}, + {0x800u, 0x7f800000u, 40, 34}, + {0xc00u, 0x7f800000u, 40, 34}, + }}}, + {0x42b17219u, {{ + {0x000u, 0x7f800000u, 40, 34}, + {0x400u, 0x7f800000u, 40, 34}, + {0x800u, 0x7f800000u, 40, 34}, + {0xc00u, 0x7f800000u, 40, 34}, + }}}, + {0x42b1721au, {{ + {0x000u, 0x7f800000u, 40, 34}, + {0x400u, 0x7f800000u, 40, 34}, + {0x800u, 0x7f800000u, 40, 34}, + {0xc00u, 0x7f800000u, 40, 34}, + }}}, + {0xc2cff1b3u, {{ + {0x000u, 0x00000001u, 48, 73}, + {0x400u, 0x00000000u, 48, 73}, + {0x800u, 0x00000001u, 48, 73}, + {0xc00u, 0x00000000u, 48, 73}, + }}}, + {0xc2cff1b4u, {{ + {0x000u, 0x00000001u, 48, 73}, + {0x400u, 0x00000000u, 48, 73}, + {0x800u, 0x00000001u, 48, 73}, + {0xc00u, 0x00000000u, 48, 73}, + }}}, + {0xc2cff1b5u, {{ + {0x000u, 0x00000000u, 48, 73}, + {0x400u, 0x00000000u, 48, 73}, + {0x800u, 0x00000001u, 48, 73}, + {0xc00u, 0x00000000u, 48, 73}, + }}}, + {0xc2cff1b6u, {{ + {0x000u, 0x00000000u, 48, 34}, + {0x400u, 0x00000000u, 48, 34}, + {0x800u, 0x00000001u, 48, 34}, + {0xc00u, 0x00000000u, 48, 34}, + }}}, + {0xc2cff1b7u, {{ + {0x000u, 0x00000000u, 48, 34}, + {0x400u, 0x00000000u, 48, 34}, + {0x800u, 0x00000001u, 48, 34}, + {0xc00u, 0x00000000u, 48, 34}, + }}}, + {0xc2aeac4fu, {{ + {0x000u, 0x00800026u, 32, 73}, + {0x400u, 0x00800025u, 32, 73}, + {0x800u, 0x00800026u, 32, 73}, + {0xc00u, 0x00800025u, 32, 73}, + }}}, + {0xc2aeac50u, {{ + {0x000u, 0x007fffe6u, 32, 73}, + {0x400u, 0x007fffe5u, 48, 73}, + {0x800u, 0x007fffe6u, 32, 73}, + {0xc00u, 0x007fffe5u, 48, 73}, + }}}, + {0xc2aeac51u, {{ + {0x000u, 0x007fffa6u, 32, 73}, + {0x400u, 0x007fffa5u, 48, 73}, + {0x800u, 0x007fffa6u, 32, 73}, + {0xc00u, 0x007fffa5u, 48, 73}, + }}}, +}; + +int main() { + const auto savedControl = GuestCpu::X87Control(); + const auto savedSse = GuestCpu::Mxcsr(); + const int savedError = *__error_nid_postfix(); + unsigned checked = 0; + for (const auto& item : Cases) { + for (const auto& expected : item.expected) { + for (const unsigned precision : {0u, 0x200u, 0x300u}) { + for (const int pending : {0, 4}) { + const auto control = static_cast((savedControl & ~0xf00u) | precision | expected.rounding | 0x3fu); + const unsigned mxcsr = (savedSse & ~(0x6000u | 0x8040u | 0x3fu)) | 0x1f80u | (expected.rounding << 3); + GuestCpu::SetX87Control(control); + feclearexcept_nid_postfix(0x3f); + GuestCpu::SetMxcsr(mxcsr | static_cast(pending)); + *__error_nid_postfix() = 73; + const float result = expf_nid_postfix(std::bit_cast(item.input)); + const int flags = fetestexcept_nid_postfix(0x3f); + const auto bits = std::bit_cast(result); + if (bits != expected.bits || flags != (expected.flags | pending) || *__error_nid_postfix() != expected.error || GuestCpu::X87Control() != control || (GuestCpu::Mxcsr() & ~0x3fu) != mxcsr) { + std::fprintf(stderr, "expf environment regression failed: input=%08x rc=%x pc=%x pending=%x expected=%08x flags=%x errno=%d actual=%08x flags=%x errno=%d\n",item.input,expected.rounding,precision,pending,expected.bits,expected.flags|pending,expected.error,bits,flags,*__error_nid_postfix()); + return 1; + } + ++checked; + } + } + } + } + feclearexcept_nid_postfix(0x3f); + GuestCpu::SetX87Control(savedControl); + GuestCpu::SetMxcsr(savedSse); + *__error_nid_postfix() = savedError; + std::printf("expf: %u result/flags/errno/rounding/precision/pending checks passed\n",checked); + return 0; +} diff --git a/core/libs/tests/GuestMath.cpp b/core/libs/tests/GuestMath.cpp index 5c5f3b307051..dcdfc91f9534 100644 --- a/core/libs/tests/GuestMath.cpp +++ b/core/libs/tests/GuestMath.cpp @@ -1,6 +1,7 @@ #include "prx/libc/include/Api.hpp" #include "prx/libc/include/GuestCpu.hpp" #include "prx/libc/include/general/VabiMacros.hpp" +#include #include #include #include @@ -3600,6 +3601,71 @@ static void CheckAtanhf() { *__error_nid_postfix() = 0; } +static void CheckLog1pf() { + const struct { std::uint32_t input; double expected; } cases[] = { + {0x00000000u, 0.0}, {0x80000000u, -0.0}, {0x00000001u, 0x1p-149}, + {0x33800000u, 0x1p-24}, {0xb3800000u, -0x1p-24}, + {0x3e4ccccdu, 0.1823215567939546}, {0x3f000000u, 0.4054651081081644}, + {0x3f800000u, 0.6931471805599453}, {0x41200000u, 2.3978952727983707}, + {0xbf7fffffu, -16.63553227383404}, + }; + for (const auto& test : cases) { + const float input = std::bit_cast(test.input); + const float result = log1pf_nid_postfix(input); + Require(std::abs(static_cast(result) - test.expected) <= std::max(1e-45, std::abs(test.expected) * 2e-7)); + if (input == 0.0f) Require(std::signbit(result) == std::signbit(input)); + } + Require(std::isinf(log1pf_nid_postfix(-1.0f)) && log1pf_nid_postfix(-1.0f) < 0.0f); + Require(std::isnan(log1pf_nid_postfix(-2.0f))); + Require(log1pf_nid_postfix(std::numeric_limits::infinity()) == std::numeric_limits::infinity()); + Require(std::isnan(log1pf_nid_postfix(std::numeric_limits::quiet_NaN()))); +} + +static void CheckLog1pfBitExact() { + const struct { std::uint32_t input; std::uint32_t expected; } cases[] = { + {0x00000000u, 0x00000000u}, + {0x80000000u, 0x80000000u}, + {0x00000001u, 0x00000001u}, + {0x80000001u, 0x80000001u}, + {0x337fffffu, 0x337fffffu}, + {0x33800000u, 0x33800000u}, + {0x33800001u, 0x33800001u}, + {0xb37fffffu, 0xb37fffffu}, + {0xb3800000u, 0xb3800000u}, + {0xb3800001u, 0xb3800001u}, + {0xb3000001u, 0xb3000001u}, + {0xb7904001u, 0xb7904052u}, + {0xb7ffffffu, 0xb800007fu}, + {0xb8000000u, 0xb8000080u}, + {0xb8000001u, 0xb8000081u}, + {0x37ffffffu, 0x37fffeffu}, + {0x38000000u, 0x37ffff00u}, + {0x38000001u, 0x37ffff02u}, + {0xbe95f618u, 0xbeb17216u}, + {0xbe95f619u, 0xbeb17217u}, + {0xbe95f61au, 0xbeb17218u}, + {0xbea00000u, 0xbebfd7d2u}, + {0xbf000000u, 0xbf317218u}, + {0xbf400000u, 0xbfb17218u}, + {0xbf7ffffeu, 0xc17f1402u}, + {0xbf7fffffu, 0xc1851592u}, + {0x3ed413cfu, 0x3eb1721au}, + {0x3ed413d0u, 0x3eb1721au}, + {0x3ed413d1u, 0x3eb1721bu}, + {0x3f800000u, 0x3f317218u}, + {0x40000000u, 0x3f8c9f54u}, + {0x40400000u, 0x3fb17218u}, + {0x59ffffffu, 0x4212f27cu}, + {0x5a000000u, 0x4212f27cu}, + {0x5a000001u, 0x4212f27cu}, + {0x7f7fffffu, 0x42b17218u}, + }; + for (const auto& test : cases) { + const std::uint32_t result = std::bit_cast(log1pf_nid_postfix(std::bit_cast(test.input))); + Require(result == test.expected); + } +} + static void CheckFloatClassification() { Require(_FInf_nid_postfix.bits[0] == 0x7f800000u && _FNan_nid_postfix.bits[0] == 0x7fc00000u); for (int word = 1; word < 4; ++word) Require(_FInf_nid_postfix.bits[word] == 0 && _FNan_nid_postfix.bits[word] == 0); @@ -4860,6 +4926,26 @@ static void CheckExpm1fConsoleBits() { } } +static void CheckExpfConsoleBits() { + constexpr struct { + std::uint32_t input; + std::uint32_t expected; + int error; + } cases[] = { + {0x3c0001e7u, 0x3f810104u, 0}, {0x3f000306u, 0x3fd30bcau, 0}, {0x4020050du, 0x4142fae2u, 0}, {0x42a00707u, 0x792d17d8u, 0}, + {0xbf40091eu, 0x3ef1d16au, 0}, {0xc2a00b0bu, 0x05bbd45eu, 0}, {0x00000000u, 0x3f800000u, 0}, {0x80000000u, 0x3f800000u, 0}, + {0x00000001u, 0x3f800000u, 0}, {0x38800000u, 0x3f800200u, 0}, {0x3f800000u, 0x402df854u, 0}, {0x42b17217u, 0x7f7fff84u, 0}, + {0x42b17218u, 0x7f800000u, ERANGE}, {0x7f7fffffu, 0x7f800000u, ERANGE}, {0x7f800000u, 0x7f800000u, 0}, {0xff800000u, 0x00000000u, 0}, + {0xc2aeac50u, 0x007fffe6u, 0}, {0xc2b00000u, 0x0041edc4u, 0}, {0xc2cff1b5u, 0x00000000u, 0}, {0xc2cff1b6u, 0x00000000u, ERANGE}, + {0xff7fffffu, 0x00000000u, ERANGE}, {0x7fc00001u, 0x7fc00001u, 0}, {0xffc00002u, 0xffc00002u, 0}, + }; + for (const auto& item : cases) { + errno = 1234; + Require(std::bit_cast(expf_nid_postfix(std::bit_cast(item.input))) == item.expected); + Require(errno == (item.error != 0 ? item.error : 1234)); + } +} + static void CheckRemainderConsoleBits() { constexpr struct { std::uint64_t x; @@ -5180,6 +5266,7 @@ static void CheckLog1pDomainEnvironment() { } int main() { + CheckExpfConsoleBits(); CheckFdimFlags(); CheckRoundFlagsAndSignalingNans(); CheckRemainderConsoleBits(); @@ -5858,6 +5945,8 @@ int main() { CheckSinhf(); CheckAtanhf(); CheckLogfConsoleBits(); + CheckLog1pf(); + CheckLog1pfBitExact(); CheckHypotSpecialValue(); CheckAtanfConsoleBits(); CheckAtan2fConsoleBits(); diff --git a/docs/dev/debt/changes/refresh-expf-log1pf-msun-20261117.md b/docs/dev/debt/changes/refresh-expf-log1pf-msun-20261117.md new file mode 100644 index 000000000000..5044ad4084b0 --- /dev/null +++ b/docs/dev/debt/changes/refresh-expf-log1pf-msun-20261117.md @@ -0,0 +1,10 @@ +# libc + +## add Functional + +- [log1pf](../../../core/libs/prx/libc/src/Math.cpp) (libc) follows FreeBSD msun's [`s_log1pf.c`](https://github.com/freebsd/freebsd-src/blob/59bc2f095289a10b8cfa55cbb2b45c3bd6c2d4ae/lib/msun/src/s_log1pf.c) for finite inputs above -1 with its own helper, including the negative-input branches (the `atanhf` helper only has the paths for the positive arguments that function passes). Over every such `float` it gives the same result bits as the pinned source. Invalid-domain and non-finite inputs retain the host `std::log1p` fallback. Issue [#9707](https://github.com/boykopovar/AnyPS5/issues/9707) measured about 1.4 million result-bit differences against a Windows host implementation in 20 million samples. The exact `libSceLibcInternal` implementation and title-build FMA behavior are not verified +- [expf](../../../core/libs/prx/libc/src/MathExpf.cpp) (libc) follows the msun-derived PPSA21564 implementation, including its argument-1 literal, `_Feraise` range branches, small-argument inexact trigger and directed-rounding operation order. Its dedicated source uses `-frounding-math`; guarded CPU fixtures compare result bits, raw flags, errno and caller controls. Console and Windows qualification of these changes remain unverified. Earlier all-float Windows replay applied to the older default-rounding port. FMA-contracted PPSA13934 differs by 1 ulp on 82,009 arguments, so no single implementation matches every title build. Labels: tested, console, windows + +## change [atanhf] + +- [atanhf](../../../core/libs/prx/libc/src/Math.cpp) (libc) lacks an exhaustive Linux replay; equivalence to `libSceLibcInternal` in PPSA99305 is assumed. It follows PPSA28319 bundled code and FreeBSD msun [e_atanhf.c](https://github.com/freebsd/freebsd-src/blob/59bc2f095289a10b8cfa55cbb2b45c3bd6c2d4ae/lib/msun/src/e_atanhf.c), with the positive-input log1pf paths inlined. The exported log1pf follows msun for finite inputs above -1. Console error handling sets EDOM and FE_INVALID for invalid/nonfinite inputs, and ERANGE with FE_DIVBYZERO at |x|=1. The original Windows replay matched bits, errno and exceptions over all 2^32 inputs.