fix(libc): compute sin, cos and tan with FreeBSD msun's algorithm, as titles' libc.prx does #10250
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/10250"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-23T15:41:56Z.
What
sin,cosandtanreturned the host's functions, so their last bit and theirerrnodepended on the host. On Windows (UCRT) they differ from thelibc.prxthat titles ship on 3,338,607, 3,340,843 and 8,098,628 of 320 million random arguments. Three examples, each a value where UCRT is 1 ulp off:sin(0x3fdada3603b109eb)0x3fda124ac129e7540x3fda124ac129e755cos(0x4079ab389c31a606)0xbfe52f138550db6f0xbfe52f138550db6etan(0xbfcdf9356ad39d8f)0xbfce889b662f705f0xbfce889b662f705eThe titles' copies (NIDs
H8ya2H00jbI,2WE3BTYVwKMandT7uyNqP7vQA) are FreeBSD msun'ss_sin.c,s_cos.cands_tan.c, over itsk_sin.c,k_cos.c,k_tan.cande_rem_pio2.c, plusk_rem_pio2.cfor huge arguments. These now compute them:RemPio2: the small multiples of π/2, the medium path with its two refinements, and the large-argument path throughk_rem_pio2.c;KernelSin,KernelCosandKernelTan;Shared reduction. #9857 ported
k_rem_pio2.cforsinf,cosfandtanf, but only for one input word andprec0, asTrigfKernelRemPio2. This PR gives it msun'snxandprecparameters and renames itKernelRemPio2, with its tablesRemPio2Ipio2andRemPio2Parts. The double path calls it with up to three words andprec1, which adds msun's second output word; the float path calls it with(…, 1, 0)and computes exactly what it did before. The float debt entry is updated to say the port is shared.errno: msun sets none. The titles' copies setEDOMwhen the argument's high word is0x7ff00000or0xfff00000: the infinities, and also the NaNs that share that high word, such as0xfff0000054442d18. Other NaNs, such as0x7ff8000000000001, leaveerrnoalone. They return msun'sx - x. This port tests the same high word.Tested
Current-main refresh — 2026-11-04. Head
7a21d9762f01427c9a3ef744f4f753162419442emerges main9ab6a281ea61c9e62a7eb07eb4cef8f19ede1c5cwhile retaining the prior public head as an ancestor. Main’s newer code, regression fixtures and debt entries are preserved alongside this topic.Linux x86-64, GCC16.2.1, Ninja Release: actual libc, libkernel and libSceAmpr CMake owners rebuilt coherently.
guest_math,guest_math_exact,guest_dinkumware_internalseach pass three consecutive runs (9 passes, zero skips). The same groups pass three times on pinned main. Public-header audit and conventions pass. Original licence-notice trailing whitespace is retained where present. The scoped recipe reuses frozen ZArchive/zstd providers; this is not a whole-project build. No new Windows, bundled-SDK, physical-console, title/GPU or FPS measurement is claimed; earlier reports retain their original pins.Exact source/parent proofs, per-branch patches/input manifests, build and CTest logs/JUnit, library/executable hashes and scoped CMake recipe, SHA-256
97a2377bc996be1690e8853e1ea090a98b5ed808bcc37a15a5cdada5c2e00845(download verified). AI-assisted: yes (Codex).Current-main integration — 2026-11-03. Head
5c054e6b5e48aef1bde87b7f6c31b70a9e00d563includes main4f41f4c375f5abb487dbd184fbc150f31994d762and retains the prior public history and attribution/licence notices. Main’s newer math implementations and fixtures remain alongside this topic. Main’s float argument-reduction tables, helper andTrigfRemPio2remain byte-identical and separate from the retained topic double-trig implementation.Linux x86-64 Release, GCC 16.2.1, Ninja, BUILD_TESTING=ON: rebuilt actual libc and math executables.
guest_math,guest_math_exact, andguest_dinkumware_internalseach pass 3/3 (9 passes, zero skips);public_headersand 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.git diff --checkreports the preserved verbatim licence trailing-space line; no other whitespace warnings occur.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):
4947eef3565f9117da59c10574a5c68df3f29760includes maina4eca2a056bf3bbbec98ab42e9d28163e4ea9cb8and 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_exactandguest_dinkumware_internalseach pass three times (9 passes, zero skips), andpublic_headerspasses. 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-25622866bd2a060ba69cd4387d471605afe20ed644e3257174b2d1488635f8c9b52(download verified).Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release,
BUILD_TESTING=ON.Comparison: a scratch loader runs the decrypted
libc.prxof PPSA07923, PPSA05916 and PPSA13934 natively and supplies__error. It compared theirsin,cosandtanwith this PR's code, checking result bits (NaN payloads included) anderrno, on:k_rem_pio2.c), plus infinities;e_rem_pio2.cand the three kernels, with four low words each;Results:
errnoon all of them.errno. The bits differ on about 0.35% (sin,cos) and 0.7% (tan) of the random arguments, because that copy is an FMA-contracted build.guest_math: the newCheckTrigConsoleBitshas 24 arguments, each with the console'ssin,cosandtanand itserrno:DBL_MAXthroughk_rem_pio2.c;inf, a NaN with an infinity's high word, and a quiet NaN;The existing
CheckTrigfConsoleBitsstill passes over the shared reduction.guest_mathpasses in three runs out of three on the exact head. (guest_dinkumware_internalsdid not exist on #9857's base; it runs in the combined preview below.)Negative control: with #9857's
Math.cppand this test,guest_mathaborts in aRequire.python tools/check_conventions.py --base e601dbd7a4andgit diff --checkpass.Overlap and order: this PR is stacked on #9857, which merges cleanly with the rest of my math series. It replaces host wrappers next to those of #10222, #10229, #10234, #10245 and #10247, so it lands last: #9777 → #9784 → #10222 → #9850 → #10229 → #10234 → #10245 → #10247 → #10250 → #10253 → the
powPR, with #10226 after #10222, #9857 before #10250 and #9771 before #10253. A combined preview of all ten heads (3e088f450d), including #9857, resolves every conflict by keeping each port and dropping each host wrapper. Each ported function is defined once and each of the eight console tests is called once, andguest_mathandguest_dinkumware_internalspass in three runs out of three on it.Not tested: Linux and macOS (the result no longer depends on the host's functions), a console, the floating-point exception flags, and
sinl/cosl/tanl(still the host's, as listed in the debt file).Current Linux qualification (2026-10-28)
Rebased onto
main2227f1366188cda5699dd20e2e756a18006a5d4b. Linux CPU validation on 2026-10-28 at4d98e8148f038bca77cc104a1796608d5e99999a(Arch Linux 7.2.9, AMD Ryzen 9 7950X, GCC 16.2.1, Ninja Release, BUILD_TESTING=ON).libc,guest_math_testsandguest_math_exact_testsrebuilt from this exact source.guest_mathandguest_math_exacteach 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
a3b71d51a5cd, removed 94 duplicate local export declarations fromGuestMath.cpp; its includedApi.hppsupplies 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, SHA25644f560018e4f6b52e8dda4158383f64fc3059fcdb6444840fe5c04fb2c884f11. No new numerical-runtime/platform claim; earlier runs retain original pins. Normal fast-forward only, no rebase. AI-assisted: Codex.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)sinhfentry didImported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T13:29:02Z.
Review: APPROVED
Checked against main
6cd36e6517and FreeBSD release/11.4.0 lib/msun/src (s_sin.c, s_cos.c, s_tan.c, k_sin.c, k_cos.c, k_tan.c, e_rem_pio2.c, k_rem_pio2.c):RemPio2andRemPio2Nearmatch e_rem_pio2.c, including the threegoto mediumcases, the two medium-path refinements and the 3-word large-argument split.KernelRemPio2'snxandprecsupport matches k_rem_pio2.c (init_jk3/4, case 1 tail), and the float callers still get the same code as before.EDOMfor an infinite argument, as described.CheckTrigConsoleBitsarguments through an independent Python port of the 11.4 source; every sin, cos and tan result and errno matches.Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.