Repository navigation
feat(libSceLibcInternal): implement _Logpoly, _FLogpoly and _LLogpoly - #23719
Conversation
|
Probe sources and raw output of the console measurements (PS5, firmware 13.40, payload process, ps5-payload-sdk v0.45, elfldr, 2026-11-19). Lines are Raw output, default modesRaw output, every rounding mode and x87 precisionProbe source, default modes#include <dlfcn.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
static const uint64_t DoubleInputs[] = {
0x0000000000000000ull, 0x8000000000000000ull, 0x3f50000000000000ull, 0x3f88000000000000ull, 0x3fa921fb54442d18ull,
0x3fd5555555555555ull, 0x3ff0000000000000ull, 0xbfe8000000000000ull, 0x3fcfffffe0000000ull, 0x40223456789abcdeull,
0xbfb6a09e667f3bcdull, 0x3eb0000000000001ull, 0x7ff0000000000000ull, 0xfff0000000000000ull, 0x7ff8000000000000ull,
0xfff8000000000000ull, 0x7ff8000000001234ull, 0xfff80000deadbeefull, 0x7ff0000000001234ull, 0xfff4000000000001ull};
static const uint32_t FloatInputs[] = {
0x00000000u, 0x80000000u, 0x3a800000u, 0x3c400000u, 0x3d490fdbu, 0x3eaaaaabu, 0x3f800000u, 0xbf400000u, 0x3e7fffffu, 0x4111a2b4u,
0xbdb504f3u, 0x35800000u, 0x7f800000u, 0xff800000u, 0x7fc00000u, 0xffc00000u, 0x7fc01234u, 0xffc0beefu, 0x7f801234u, 0xffa00001u};
static const struct {
uint64_t mantissa;
uint16_t exponent;
} ExtendedInputs[] = {
{0x0000000000000000ull, 0x0000}, {0x0000000000000000ull, 0x8000}, {0x8000000000000000ull, 0x3ff5}, {0xc000000000000000ull, 0x3ff8},
{0xc90fdaa22168c000ull, 0x3ffa}, {0xaaaaaaaaaaaaa800ull, 0x3ffd}, {0x8000000000000000ull, 0x3fff}, {0xc000000000000000ull, 0xbffe},
{0xffffff0000000000ull, 0x3ffc}, {0x91a2b3c4d5e6f000ull, 0x4002}, {0xb504f333f9de6800ull, 0xbffb}, {0x8000000000000800ull, 0x3feb},
{0x8000000000000000ull, 0x7fff}, {0x8000000000000000ull, 0xffff}, {0xc000000000000000ull, 0x7fff}, {0xc000000000000000ull, 0xffff},
{0xc000000000001234ull, 0x7fff}, {0xc0000000deadbeefull, 0xffff}, {0x8000000000001234ull, 0x7fff}, {0xa000000000000001ull, 0xffff}};
static void emit(FILE* file, const char* line) {
fputs(line, stdout);
if (file) fputs(line, file);
}
int main(void) {
char line[160];
FILE* file = fopen("/data/anyps5_logpoly.txt", "w");
void* module = dlopen("libSceLibcInternal.sprx", RTLD_LAZY);
if (!module) {
snprintf(line, sizeof(line), "dlopen failed: %s\n", dlerror());
emit(file, line);
if (file) fclose(file);
return 1;
}
double (*logpoly)(double) = dlsym(module, "_Logpoly");
float (*flogpoly)(float) = dlsym(module, "_FLogpoly");
long double (*llogpoly)(long double) = dlsym(module, "_LLogpoly");
snprintf(line, sizeof(line), "symbols %d %d %d\n", logpoly != 0, flogpoly != 0, llogpoly != 0);
emit(file, line);
for (size_t i = 0; logpoly && i < sizeof(DoubleInputs) / sizeof(DoubleInputs[0]); ++i) {
double x, y;
uint64_t bits;
memcpy(&x, &DoubleInputs[i], sizeof(x));
y = logpoly(x);
memcpy(&bits, &y, sizeof(bits));
snprintf(line, sizeof(line), "d %016llx %016llx\n", (unsigned long long)DoubleInputs[i], (unsigned long long)bits);
emit(file, line);
}
for (size_t i = 0; flogpoly && i < sizeof(FloatInputs) / sizeof(FloatInputs[0]); ++i) {
float x, y;
uint32_t bits;
memcpy(&x, &FloatInputs[i], sizeof(x));
y = flogpoly(x);
memcpy(&bits, &y, sizeof(bits));
snprintf(line, sizeof(line), "f %08x %08x\n", FloatInputs[i], bits);
emit(file, line);
}
for (size_t i = 0; llogpoly && i < sizeof(ExtendedInputs) / sizeof(ExtendedInputs[0]); ++i) {
long double x = 0, y;
uint64_t mantissa = 0;
uint16_t exponent = 0;
memcpy(&x, &ExtendedInputs[i].mantissa, 8);
memcpy((unsigned char*)&x + 8, &ExtendedInputs[i].exponent, 2);
y = llogpoly(x);
memcpy(&mantissa, &y, 8);
memcpy(&exponent, (unsigned char*)&y + 8, 2);
snprintf(line, sizeof(line), "l %016llx %04x %016llx %04x\n", (unsigned long long)ExtendedInputs[i].mantissa,
ExtendedInputs[i].exponent, (unsigned long long)mantissa, exponent);
emit(file, line);
}
emit(file, "done\n");
if (file) fclose(file);
return 0;
}Probe source, modes#include <dlfcn.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
static const uint64_t DoubleInputs[] = {
0x0000000000000000ull, 0x8000000000000000ull, 0x3f50000000000000ull, 0x3f88000000000000ull, 0x3fa921fb54442d18ull,
0x3fd5555555555555ull, 0x3ff0000000000000ull, 0xbfe8000000000000ull, 0x3fcfffffe0000000ull, 0x40223456789abcdeull,
0xbfb6a09e667f3bcdull, 0x3eb0000000000001ull, 0x7ff0000000000000ull, 0xfff0000000000000ull, 0x7ff8000000000000ull,
0xfff8000000000000ull, 0x7ff8000000001234ull, 0xfff80000deadbeefull, 0x7ff0000000001234ull, 0xfff4000000000001ull};
static const uint32_t FloatInputs[] = {
0x00000000u, 0x80000000u, 0x3a800000u, 0x3c400000u, 0x3d490fdbu, 0x3eaaaaabu, 0x3f800000u, 0xbf400000u, 0x3e7fffffu, 0x4111a2b4u,
0xbdb504f3u, 0x35800000u, 0x7f800000u, 0xff800000u, 0x7fc00000u, 0xffc00000u, 0x7fc01234u, 0xffc0beefu, 0x7f801234u, 0xffa00001u};
static const struct {
uint64_t mantissa;
uint16_t exponent;
} ExtendedInputs[] = {
{0x0000000000000000ull, 0x0000}, {0x0000000000000000ull, 0x8000}, {0x8000000000000000ull, 0x3ff5}, {0xc000000000000000ull, 0x3ff8},
{0xc90fdaa22168c000ull, 0x3ffa}, {0xaaaaaaaaaaaaa800ull, 0x3ffd}, {0x8000000000000000ull, 0x3fff}, {0xc000000000000000ull, 0xbffe},
{0xffffff0000000000ull, 0x3ffc}, {0x91a2b3c4d5e6f000ull, 0x4002}, {0xb504f333f9de6800ull, 0xbffb}, {0x8000000000000800ull, 0x3feb},
{0x8000000000000000ull, 0x7fff}, {0x8000000000000000ull, 0xffff}, {0xc000000000000000ull, 0x7fff}, {0xc000000000000000ull, 0xffff},
{0xc000000000001234ull, 0x7fff}, {0xc0000000deadbeefull, 0xffff}, {0x8000000000001234ull, 0x7fff}, {0xa000000000000001ull, 0xffff}};
static unsigned get_mxcsr(void) { unsigned v; __asm__ volatile("stmxcsr %0" : "=m"(v)); return v; }
static void set_mxcsr(unsigned v) { __asm__ volatile("ldmxcsr %0" : : "m"(v)); }
static unsigned short get_cw(void) { unsigned short v; __asm__ volatile("fnstcw %0" : "=m"(v)); return v; }
static void set_cw(unsigned short v) { __asm__ volatile("fldcw %0" : : "m"(v)); }
#define COUNT 20
static uint64_t DoubleResults[4][COUNT];
static uint32_t FloatResults[4][COUNT];
static uint64_t ExtendedMantissa[12][COUNT];
static uint16_t ExtendedExponent[12][COUNT];
static const unsigned short Precision[3] = {0x000, 0x200, 0x300};
static const int PrecisionBits[3] = {24, 53, 64};
int main(void) {
void* module = dlopen("libSceLibcInternal.sprx", RTLD_LAZY);
if (!module) {
printf("dlopen failed: %s\n", dlerror());
return 1;
}
double (*logpoly)(double) = dlsym(module, "_Logpoly");
float (*flogpoly)(float) = dlsym(module, "_FLogpoly");
long double (*llogpoly)(long double) = dlsym(module, "_LLogpoly");
if (!logpoly || !flogpoly || !llogpoly) {
printf("dlsym failed\n");
return 1;
}
const unsigned mxcsr = get_mxcsr();
const unsigned short cw = get_cw();
for (unsigned rounding = 0; rounding < 4; ++rounding) {
for (int i = 0; i < COUNT; ++i) {
double x, y;
float fx, fy;
memcpy(&x, &DoubleInputs[i], 8);
memcpy(&fx, &FloatInputs[i], 4);
set_mxcsr((mxcsr & ~0x6000u) | (rounding << 13));
y = logpoly(x);
fy = flogpoly(fx);
set_mxcsr(mxcsr);
memcpy(&DoubleResults[rounding][i], &y, 8);
memcpy(&FloatResults[rounding][i], &fy, 4);
}
}
for (unsigned precision = 0; precision < 3; ++precision) {
for (unsigned rounding = 0; rounding < 4; ++rounding) {
for (int i = 0; i < COUNT; ++i) {
long double x = 0, y;
memcpy(&x, &ExtendedInputs[i].mantissa, 8);
memcpy((unsigned char*)&x + 8, &ExtendedInputs[i].exponent, 2);
set_cw((cw & ~0x0f00) | Precision[precision] | (rounding << 10));
y = llogpoly(x);
set_cw(cw);
memcpy(&ExtendedMantissa[precision * 4 + rounding][i], &y, 8);
memcpy(&ExtendedExponent[precision * 4 + rounding][i], (unsigned char*)&y + 8, 2);
}
}
}
printf("default mxcsr %08x x87cw %04x\n", mxcsr, cw);
for (unsigned rounding = 0; rounding < 4; ++rounding)
for (int i = 0; i < COUNT; ++i) printf("d r%u %016llx %016llx\n", rounding, (unsigned long long)DoubleInputs[i], (unsigned long long)DoubleResults[rounding][i]);
for (unsigned rounding = 0; rounding < 4; ++rounding)
for (int i = 0; i < COUNT; ++i) printf("f r%u %08x %08x\n", rounding, FloatInputs[i], FloatResults[rounding][i]);
for (unsigned precision = 0; precision < 3; ++precision)
for (unsigned rounding = 0; rounding < 4; ++rounding)
for (int i = 0; i < COUNT; ++i)
printf("l p%d r%u %016llx %04x %016llx %04x\n", PrecisionBits[precision], rounding, (unsigned long long)ExtendedInputs[i].mantissa,
ExtendedInputs[i].exponent, (unsigned long long)ExtendedMantissa[precision * 4 + rounding][i], ExtendedExponent[precision * 4 + rounding][i]);
printf("done\n");
return 0;
} |
PrivacyOnion
left a comment
There was a problem hiding this comment.
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against d65f6da22e:
- Conventions:
tools/check_conventions.pypassed. - Build:
guest_libc_internal_logpoly_testsbuilt cleanly. - CTest (
guest_libc_internal_logpoly):1/1 Test #4351: guest_libc_internal_logpoly ...... Passed 0.01 sec 100% tests passed, 0 tests failed out of 1 - Negative control: with
core/libs/prx/libSceLibcInternal/Export.cpp,core/libs/prx/libSceLibcInternal/include/MathInternals.hpp,core/libs/prx/libSceLibcInternal/Logpoly.cppreverted tomain, the new tests do not build/link without the change; restoring it makes them build and pass. - Code reading:
- Verified _Logpoly and _FLogpoly implementations in Logpoly.cpp evaluate Horner's scheme using std::fma in order matching disassembly (0xd84d0 and 0xd8510) and probe measurements across all rounding modes
- Verified _LLogpoly implementation in Logpoly.cpp delegates to _LPoly_nid_postfix with LibcDetail::LogPolynomial and degree 7 (size - 1) matching disassembly 0x47ce0
- Verified function prototypes in MathInternals.hpp match caller conventions and export signatures (float, double, long double returns with respective scalar arguments)
- Verified technical debt fragment docs/dev/debt/changes/feat-libsce-libcinternal-logpoly.md correctly updates Additional imported names and records functional implementation with firmware test details
- Verified comprehensive unit tests in GuestLibcInternalLogpoly.cpp covering normal, subnormal, infinity, NaN, and MXCSR/x87 rounding and precision control modes
Looks Good To Me
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed — looks good
Nothing blocks this change; the targeted source review found no verified defect, and the reported console measurements support the implementation.
What I ran
Reviewed head d65f6da22ecee54a16db20ba46fd1efc8d5a9cf1 using the prepared diff and repository reader repo_read.py. No commands or tests were run in this review.
Read the full prepared diff, description, commit list, manifest, and all seven changed files listed in the manifest. Also read the declarations of _LPoly_nid_postfix, its implementation, the LogPolynomial table, and the CMake test registration context. No changed files were excluded.
Results
| Check | Result | Note |
|---|---|---|
| Targeted source review | passed | No verified defect found |
| Tests in this review | not run | Author-reported test results only |
| Console behavior | not run | Description reports firmware 13.40 measurements |
Findings
Blocking
None.
Non-blocking
None.
Not verified
This review did not run the test suite, validate the reported PS5 measurements, or inspect behavior on other firmware, Linux, or macOS. The author reports the logpoly test, related tests, conventions check, and debt check passed; those results were not independently run here.
To get this to a good state
Nothing is required.
|
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
|
What
Implements
_Logpoly,_FLogpolyand_LLogpoly, which threw with no parameters declared. #21722 left them stubbed until a firmware measurement of their return values; this is that measurement.Source:
libSceLibcInternal.sprx, firmware 13.40 (sha2562350ddff2b536e32480bbb16d754ff018be6d8866f0a340deb165587155fd334), read from its disassembly and measured on a PS5._Logpoly(0xd84d0),doublein and out inxmm0, no calls:_FLogpoly(0xd8510),floatin and out inxmm0, no calls:_LLogpoly(0x47ce0),long doubleon the stack, calls_LPoly(0x47d10) with an eight-entry table and degree 7:doubleandfloatcoefficients are the bytes of the constants those instructions load.LibcDetail::LogPolynomial, which_LLogalready uses (fix(libc): compute _LLog with the bundled libc.prx's Dinkumware code #11270), so_LLogpolypasses that table to_LPolyand copies nothing._LPowis the only caller inside the module. No firmware 13.40 module imports the three names.Console measurement: PS5, firmware 13.40, a payload process built with ps5-payload-sdk v0.45 and loaded through elfldr on 2026-11-19. It resolves the three exports with
dlopen/dlsymand prints the result bits. The process starts with MXCSR 0x9fc0 and x87 control word 0x037f. Probe sources and raw output are in the first comment.mainThe same 20 inputs per type were measured again with the control registers changed around each call:
_Logpolyand_FLogpolyin the four MXCSR rounding modes,_LLogpolyin the four x87 rounding modes at 24-bit, 53-bit and 64-bit precision. All 400 results equal this PR's, bit for bit, which covers what a title's floating-point settings could change.The pending fragment of #23457 changed two of the debt entries this PR changes, so its result lines lose
_FLogpolyand_LLogpolyhere, astools/debt_fold.py --checkasks.Not tested: other firmware, the floating-point exception flags, Linux and macOS.
Tested
mainat 6db6a3e.guest_libc_internal_logpolycompares the result bits of the 20 inputs per type with the console's, plus 22 of the console's results in directed rounding modes and reduced x87 precision: passes. A local harness compared all 400 mode results. Negative control: with the last digit of onedoublecoefficient changed it fails.guest_libc_internal_scaled_powandpublic_headerspass.python tools/check_conventions.py --base origin/mainandpython tools/debt_fold.py --checkpass.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)