fix(libc): compute pow with fdlibm's algorithm, as titles' libc.prx does #10265

Merged
JJenkx merged 7 commits from upstream-pr/10265 into main 2026-11-04 17:34:48 +00:00
JJenkx commented 2026-10-23 16:04:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-23T16:04:55Z.


What

pow returned the host's pow, so its last bit, a few special values and its errno depended on the host. On Windows (UCRT) it differs from the libc.prx that titles ship on 17,275,531 of 320 million random pairs. For example, pow(0x3feafced736ee4fe, 0xc0361f9e443ea358) (about 0.843^-22.12) is 0x4045a8e0115e099a in the bundled libc.prx and 0x4045a8e0115e099b here.

The titles' copy (NID 9LCjpWyQ5Zc) is FreeBSD msun's e_pow.c, except for two special cases where it keeps the original fdlibm rules. msun later changed both to C99's, in 11.4 and already in 59bc2f0952:

call bundled libc msun 11.4 and C99
pow(1, NaN), pow(1, ±inf) NaN 1
pow(-1, ±inf) NaN (y - y) 1

Without the x == 1 shortcut, a NaN argument reaches msun's (x + 0.0) + (y + 0.0), and an infinite y with |x| == 1 returns y - y. pow now computes msun's algorithm with those two rules:

  • the yisint classification;
  • the special-value table;
  • the |y| > 2^31 path, with its huge-y shortcuts;
  • the log2(|x|) expansion with the bp, dp_h and dp_l tables;
  • the y1 * t1 split;
  • the final 2^(p_h + p_l) reconstruction.

errno: msun sets none. The titles' copy sets it in exactly these places:

  • EDOM at the two returns where a negative base meets a non-integer exponent: the general (x - x) / (x - x), and the (z - z) / (z - z) for -1 raised to a non-integer;
  • ERANGE only in the final overflow (z >= 1024) and underflow (z <= -1075) checks, including their exact-boundary cases.

The huge-y shortcuts (|y| > 2^31) overflow or underflow without errno, and so does pow(±0, negative) returning ±inf. This port sets errno in the same places.

Tested

Current-main integration — 2026-11-03. Head 3bf30fd4920e751df3825b9abb75f7a0832ecf37 includes main 4f41f4c375f5abb487dbd184fbc150f31994d762 and retains the prior public history and attribution/licence notices. Main’s newer math implementations and fixtures remain alongside this topic.

Linux x86-64 Release, GCC 16.2.1, Ninja, BUILD_TESTING=ON: rebuilt actual libc and math executables. guest_math, guest_math_exact, and guest_dinkumware_internals each pass 3/3 (9 passes, zero skips); public_headers and conventions pass. The same three groups pass 3/3 plus headers on pinned main. All 34 recursive dependency trees and 31,551 tracked blobs verify with zero errors; the update=none QuickJS test262 corpus is excluded from these library inputs. Whitespace check passes.

Source/parent/gitlink proofs, exact library/test hashes, raw logs/JUnit, and head-leased push receipts: qualification archive, SHA-256 e6a50522bc28e30871fdc2c12a88d2537515914239e8166f6237f48895126a06 (download verified). Older platform/reference reports retain their original pins; no new Windows/macOS, SDK-module, console, title/GPU or performance claim. AI-assisted: yes (Codex).

Earlier dated qualifications below retain their recorded source/platform pins.

Current-main integration (2026-11-02): 1c4650937d284044ea1f26ea386e21289e8c20e9 includes main a4eca2a056bf3bbbec98ab42e9d28163e4ea9cb8 and retains the existing public commits. Conflicts preserve main’s math implementations, regression checks and debt entries alongside this topic. Linux x86-64 Release, GCC 16.2.1: rebuilt libc and the three math executables; guest_math, guest_math_exact and guest_dinkumware_internals each pass three times (9 passes, zero skips), and public_headers passes. The same baseline groups pass on main; conventions and whitespace pass. Source and dependency blobs, executable/library hashes, raw logs and JUnit are retained. This refresh adds no new console, Windows, title, GPU or numerical-reference measurement. Qualification evidence, SHA-256 22866bd2a060ba69cd4387d471605afe20ed644e3257174b2d1488635f8c9b52 (download verified).

Current Linux refresh: 1d7f78cfac7e6ae3ebe20a7ddda35746c7b9b57a on main 6c52b649e7b1592f28db0cb8fe9ac604f0a242e7. Fresh isolated libc/math build passed; guest_math and guest_dinkumware_internals each passed three consecutive runs. Diff and conventions checks pass; Math.cpp and GuestMath.cpp topic changes remain unchanged after rebase. Linux logs and rebase checks.

The Windows module comparisons and negative control below are historical evidence from original head be1f1e6b223e95b0585f63bd104c70c0d38c7b81; Windows was not rerun on the refreshed commit. Results described as console results mean supplied titles' bundled libraries executed on the Windows host, not measurements on a physical console.

Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, BUILD_TESTING=ON, main 15e71dbe11.

  • Comparison: a scratch loader runs the decrypted libc.prx of PPSA07923, PPSA05916 and PPSA13934 natively and supplies __error. It compared their pow with this PR's code, checking result bits (NaN payloads included) and errno, on:

    • 320 million random pairs per title: a quarter random bit patterns, a quarter [0, 10) to the power [-100, 100), a quarter negative bases with integer exponents, and a quarter with both exponents near 1;
    • all 2,916 pairs of 54 special values: signed zeros, subnormals, 1 and its neighbours, 0.001, 1/3, 0.5, 1.5, 2, 3, 5, 10, 1024, 1231, a large odd integer just below 2^53, 2^31 and its neighbour, 2^53 and 2^64 with their neighbours, DBL_MAX, the infinities, and quiet and signalling NaNs of both signs.

    Results:

    • PPSA07923 and PPSA05916: identical bits and errno on all of them.
    • PPSA13934: identical errno. The bits differ on 85,862 random pairs, because that copy is an FMA-contracted build.
  • How the errno rule was found: I compiled msun's e_pow.c with every return instrumented and tabulated the bundled library's errno per return. Only the two NaN returns set EDOM and only the four final overflow/underflow returns set ERANGE. Direct probes of pow(2, ±1024), pow(2, -1075), pow(10, 308.25…) and pow(-1, 0.5) confirm the edges.

  • guest_math: the new CheckPowConsoleBits has 39 cases with the bundled library's results and errno:

    • x^0, NaN^0, 1^NaN, NaN^1;
    • (-1)^±inf, 0.5^inf, 2^-inf;
    • y = ±1, 2 and 0.5, (-4)^0.5 and (-1)^0.5, (-8)^3 and (-8)^2;
    • ±0^-1, -0^3, -inf^3 and -inf^0.5;
    • 2^1024, 2^1023, 2^-1074, ±2^-1075, 10^308 and 10^309;
    • the huge-y shortcuts with no errno;
    • a subnormal to the 0.5;
    • six pairs where UCRT differs.

    guest_math and guest_dinkumware_internals pass in three runs out of three on the exact head.

  • Negative control: with main's Math.cpp and these tests, guest_math aborts in a Require, while guest_dinkumware_internals still passes.

  • python tools/check_conventions.py --base origin/main and git diff --check pass.

  • Overlap and order: Refreshed as a standalone pow topic on main 6c52b649e7b1592f28db0cb8fe9ac604f0a242e7. The old dependencies described series order and shared source/test/debt anchors, rather than required function implementations. This branch contains only its pow implementation and tests. The pow port uses its own word helpers and existing host sqrt/scalbn; no other open math port is needed.

  • Not tested: macOS, a physical console, floating-point exception flags, and powl. Original module comparison and negative control were Windows-only; the new Linux build and regression checks are scoped separately above.

Current Linux qualification (2026-10-28)

Rebased onto main 2227f1366188cda5699dd20e2e756a18006a5d4b. Linux CPU validation on 2026-10-28 at 9a47f3f91057dce364da821dd903861dfc3eb4ee (Arch Linux 7.2.9, AMD Ryzen 9 7950X, GCC 16.2.1, Ninja Release, BUILD_TESTING=ON).

libc, guest_math_tests and guest_math_exact_tests rebuilt from this exact source. guest_math and guest_math_exact each passed three consecutive runs (6/6, no skips); the same two suites each passed three runs on the pinned main (6/6). Conventions and whitespace checks pass.

Exact source pins, preservation proofs, dependency checks, build logs and JUnit, SHA-256 1176269c9e6948b5252a5db9e660978d7f978fb2d9f4ed6c68bc050b10b5ae68. This is focused Linux CPU qualification; earlier Windows/title/reference measurements retain their original pins. No new title or performance result is claimed.

Canonical declaration cleanup (2026-10-28)

At 7d51a8b3314a, removed 93 duplicate local export declarations from GuestMath.cpp; its included Api.hpp supplies them. Production code and numeric test bodies are unchanged. Original fails the actual header audit; candidate passes. Per-branch affected-test syntax compilation and conventions/whitespace pass; full standalone-header compilation passed once for the byte-identical header tree shared by the four affected PRs. Source/proofs/logs, SHA256 44f560018e4f6b52e8dda4158383f64fc3059fcdb6444840fe5c04fb2c884f11. No new numerical-runtime/platform claim; earlier runs retain original pins. Normal fast-forward only, no rebase. AI-assisted: Codex.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source: none; the msun function is rewritten, as #9668, #5036 and the sinhf entry did
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4985647887 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10265). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-23T16:04:55Z. --- ### What `pow` returned the host's `pow`, so its last bit, a few special values and its `errno` depended on the host. On Windows (UCRT) it differs from the `libc.prx` that titles ship on 17,275,531 of 320 million random pairs. For example, `pow(0x3feafced736ee4fe, 0xc0361f9e443ea358)` (about 0.843^-22.12) is `0x4045a8e0115e099a` in the bundled `libc.prx` and `0x4045a8e0115e099b` here. The titles' copy (NID `9LCjpWyQ5Zc`) is FreeBSD msun's [`e_pow.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/e_pow.c), except for two special cases where it keeps the original fdlibm rules. msun later changed both to C99's, in 11.4 and already in `59bc2f0952`: | call | bundled libc | msun 11.4 and C99 | |---|---|---| | `pow(1, NaN)`, `pow(1, ±inf)` | NaN | 1 | | `pow(-1, ±inf)` | NaN (`y - y`) | 1 | Without the `x == 1` shortcut, a NaN argument reaches msun's `(x + 0.0) + (y + 0.0)`, and an infinite `y` with `|x| == 1` returns `y - y`. `pow` now computes msun's algorithm with those two rules: - the `yisint` classification; - the special-value table; - the `|y| > 2^31` path, with its huge-`y` shortcuts; - the `log2(|x|)` expansion with the `bp`, `dp_h` and `dp_l` tables; - the `y1 * t1` split; - the final `2^(p_h + p_l)` reconstruction. **`errno`:** msun sets none. The titles' copy sets it in exactly these places: - `EDOM` at the two returns where a negative base meets a non-integer exponent: the general `(x - x) / (x - x)`, and the `(z - z) / (z - z)` for `-1` raised to a non-integer; - `ERANGE` only in the final overflow (`z >= 1024`) and underflow (`z <= -1075`) checks, including their exact-boundary cases. The huge-`y` shortcuts (`|y| > 2^31`) overflow or underflow without `errno`, and so does `pow(±0, negative)` returning ±inf. This port sets `errno` in the same places. ### Tested **Current-main integration — 2026-11-03.** Head `3bf30fd4920e751df3825b9abb75f7a0832ecf37` includes main `4f41f4c375f5abb487dbd184fbc150f31994d762` and retains the prior public history and attribution/licence notices. Main’s newer math implementations and fixtures remain alongside this topic. Linux x86-64 Release, GCC 16.2.1, Ninja, BUILD_TESTING=ON: rebuilt actual libc and math executables. `guest_math`, `guest_math_exact`, and `guest_dinkumware_internals` each pass 3/3 (**9 passes, zero skips**); `public_headers` and conventions pass. The same three groups pass 3/3 plus headers on pinned main. All 34 recursive dependency trees and 31,551 tracked blobs verify with zero errors; the update=none QuickJS test262 corpus is excluded from these library inputs. Whitespace check passes. Source/parent/gitlink proofs, exact library/test hashes, raw logs/JUnit, and head-leased push receipts: [qualification archive](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/math15-main4f41-linux-20261103.tar.gz), SHA-256 `e6a50522bc28e30871fdc2c12a88d2537515914239e8166f6237f48895126a06` (download verified). Older platform/reference reports retain their original pins; no new Windows/macOS, SDK-module, console, title/GPU or performance claim. AI-assisted: yes (Codex). Earlier dated qualifications below retain their recorded source/platform pins. **Current-main integration (2026-11-02):** `1c4650937d284044ea1f26ea386e21289e8c20e9` includes main `a4eca2a056bf3bbbec98ab42e9d28163e4ea9cb8` and retains the existing public commits. Conflicts preserve main’s math implementations, regression checks and debt entries alongside this topic. Linux x86-64 Release, GCC 16.2.1: rebuilt libc and the three math executables; `guest_math`, `guest_math_exact` and `guest_dinkumware_internals` each pass three times (9 passes, zero skips), and `public_headers` passes. The same baseline groups pass on main; conventions and whitespace pass. Source and dependency blobs, executable/library hashes, raw logs and JUnit are retained. This refresh adds no new console, Windows, title, GPU or numerical-reference measurement. [Qualification evidence](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/math-root14-maina4eca-linux-20261102-terminal.tar.gz), SHA-256 `22866bd2a060ba69cd4387d471605afe20ed644e3257174b2d1488635f8c9b52` (download verified). Current Linux refresh: `1d7f78cfac7e6ae3ebe20a7ddda35746c7b9b57a` on main `6c52b649e7b1592f28db0cb8fe9ac604f0a242e7`. Fresh isolated libc/math build passed; `guest_math` and `guest_dinkumware_internals` each passed three consecutive runs. Diff and conventions checks pass; Math.cpp and GuestMath.cpp topic changes remain unchanged after rebase. [Linux logs and rebase checks](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/pr10265-head1d7f78cf-linux-validation.tar.gz). The Windows module comparisons and negative control below are historical evidence from original head `be1f1e6b223e95b0585f63bd104c70c0d38c7b81`; Windows was not rerun on the refreshed commit. Results described as console results mean supplied titles' bundled libraries executed on the Windows host, not measurements on a physical console. Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, `BUILD_TESTING=ON`, `main` 15e71dbe11. - **Comparison:** a scratch loader runs the decrypted `libc.prx` of PPSA07923, PPSA05916 and PPSA13934 natively and supplies `__error`. It compared their `pow` with this PR's code, checking result bits (NaN payloads included) and `errno`, on: - 320 million random pairs per title: a quarter random bit patterns, a quarter `[0, 10)` to the power `[-100, 100)`, a quarter negative bases with integer exponents, and a quarter with both exponents near 1; - all 2,916 pairs of 54 special values: signed zeros, subnormals, 1 and its neighbours, 0.001, 1/3, 0.5, 1.5, 2, 3, 5, 10, 1024, 1231, a large odd integer just below 2^53, 2^31 and its neighbour, 2^53 and 2^64 with their neighbours, `DBL_MAX`, the infinities, and quiet and signalling NaNs of both signs. Results: - PPSA07923 and PPSA05916: identical bits and `errno` on all of them. - PPSA13934: identical `errno`. The bits differ on 85,862 random pairs, because that copy is an FMA-contracted build. - **How the `errno` rule was found:** I compiled msun's `e_pow.c` with every `return` instrumented and tabulated the bundled library's `errno` per return. Only the two NaN returns set `EDOM` and only the four final overflow/underflow returns set `ERANGE`. Direct probes of `pow(2, ±1024)`, `pow(2, -1075)`, `pow(10, 308.25…)` and `pow(-1, 0.5)` confirm the edges. - **`guest_math`:** the new `CheckPowConsoleBits` has 39 cases with the bundled library's results and `errno`: - `x^0`, `NaN^0`, `1^NaN`, `NaN^1`; - `(-1)^±inf`, `0.5^inf`, `2^-inf`; - `y = ±1`, `2` and `0.5`, `(-4)^0.5` and `(-1)^0.5`, `(-8)^3` and `(-8)^2`; - `±0^-1`, `-0^3`, `-inf^3` and `-inf^0.5`; - `2^1024`, `2^1023`, `2^-1074`, `±2^-1075`, `10^308` and `10^309`; - the huge-`y` shortcuts with no `errno`; - a subnormal to the 0.5; - six pairs where UCRT differs. `guest_math` and `guest_dinkumware_internals` pass in three runs out of three on the exact head. - **Negative control:** with `main`'s `Math.cpp` and these tests, `guest_math` aborts in a `Require`, while `guest_dinkumware_internals` still passes. - `python tools/check_conventions.py --base origin/main` and `git diff --check` pass. - **Overlap and order:** Refreshed as a standalone pow topic on main `6c52b649e7b1592f28db0cb8fe9ac604f0a242e7`. The old dependencies described series order and shared source/test/debt anchors, rather than required function implementations. This branch contains only its pow implementation and tests. The pow port uses its own word helpers and existing host sqrt/scalbn; no other open math port is needed. - **Not tested:** macOS, a physical console, floating-point exception flags, and `powl`. Original module comparison and negative control were Windows-only; the new Linux build and regression checks are scoped separately above. <!-- codex-math-refresh-20261028-repeat-pr10265 --> ### Current Linux qualification (2026-10-28) Rebased onto `main` `2227f1366188cda5699dd20e2e756a18006a5d4b`. Linux CPU validation on 2026-10-28 at `9a47f3f91057dce364da821dd903861dfc3eb4ee` (Arch Linux 7.2.9, AMD Ryzen 9 7950X, GCC 16.2.1, Ninja Release, BUILD_TESTING=ON). `libc`, `guest_math_tests` and `guest_math_exact_tests` rebuilt from this exact source. `guest_math` and `guest_math_exact` each passed three consecutive runs (6/6, no skips); the same two suites each passed three runs on the pinned main (6/6). Conventions and whitespace checks pass. [Exact source pins, preservation proofs, dependency checks, build logs and JUnit](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/owned-math25-main2227-linux-20261028-repeat.tar.gz), SHA-256 `1176269c9e6948b5252a5db9e660978d7f978fb2d9f4ed6c68bc050b10b5ae68`. This is focused Linux CPU qualification; earlier Windows/title/reference measurements retain their original pins. No new title or performance result is claimed. ### Canonical declaration cleanup (2026-10-28) At `7d51a8b3314a`, removed 93 duplicate local export declarations from `GuestMath.cpp`; its included `Api.hpp` supplies them. Production code and numeric test bodies are unchanged. Original fails the actual header audit; candidate passes. Per-branch affected-test syntax compilation and conventions/whitespace pass; full standalone-header compilation passed once for the byte-identical header tree shared by the four affected PRs. [Source/proofs/logs](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/math-canonical-declarations-linux-20261028.tar.gz), SHA256 `44f560018e4f6b52e8dda4158383f64fc3059fcdb6444840fe5c04fb2c884f11`. No new numerical-runtime/platform claim; earlier runs retain original pins. Normal fast-forward only, no rebase. AI-assisted: Codex. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source: none; the msun function is rewritten, as #9668, #5036 and the `sinhf` entry did - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
harisibrar1 commented 2026-10-24 13:29:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T13:29:03Z.

Review: APPROVED


Checked against main 6cd36e6517 and FreeBSD release/11.4.0 lib/msun/src/e_pow.c:

  • Compared pow_nid_postfix with e_pow.c line by line. The algorithm, every constant (identical literals) and the overflow/underflow checks match. The only changes are the two documented fdlibm rules: no x == 1 shortcut, and y - y for |x| == 1 with infinite y.
  • errno is set only at the two NaN returns (EDOM) and the final overflow/underflow returns (ERANGE), matching the description and the debt entry.
  • Ran all 39 CheckPowConsoleBits cases through an independent Python port of the 11.4 source with the same two changes; every result and errno matches.
  • It merges cleanly onto current main. With the other open msun PRs it only conflicts at the shared insertion points in GuestMath.cpp, and nothing is defined twice. Looks good to me.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638415035 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10265#pullrequestreview-5638415035). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T13:29:03Z. **Review:** APPROVED --- Checked against main 6cd36e6517 and FreeBSD release/11.4.0 lib/msun/src/e_pow.c: - Compared `pow_nid_postfix` with e_pow.c line by line. The algorithm, every constant (identical literals) and the overflow/underflow checks match. The only changes are the two documented fdlibm rules: no `x == 1` shortcut, and `y - y` for `|x| == 1` with infinite `y`. - `errno` is set only at the two NaN returns (`EDOM`) and the final overflow/underflow returns (`ERANGE`), matching the description and the debt entry. - Ran all 39 `CheckPowConsoleBits` cases through an independent Python port of the 11.4 source with the same two changes; every result and errno matches. - It merges cleanly onto current main. With the other open msun PRs it only conflicts at the shared insertion points in GuestMath.cpp, and nothing is defined twice. Looks good to me. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!10265
No description provided.