feat(libSceLibcInternal): implement the _Atan and _Tan families for Dinkumware's arguments #21716

Merged
Krille2004zzz merged 3 commits from upstream-pr/21716 into main 2026-11-19 18:32:12 +00:00
Krille2004zzz commented 2026-11-16 16:37:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Krille2004zzz on 2026-11-16T16:37:01Z.


What

Implements _Atan, _FAtan, _LAtan, _Tan, _FTan and _LTan in libSceLibcInternal (new QuadrantTrig.cpp), replacing the parameterless stubs. Only the arguments Dinkumware's own callers pass are implemented; every other argument still throws through NotImplemented_nid_no_patch. Because of that, tools/progress.py still counts all six as pending.

  • Signatures. _Atan(double, int), _FAtan(float, int) and _LAtan(long double, int) come from Dinkumware's public xmath.h. _Tan(x, int quads) comes from the declaration in Dinkumware V6.50's xxtan.h.
  • Behaviour. It is taken only from how Dinkumware's callers use these functions. No Dinkumware implementation (xatan.c, xxxatan.h, xtan.c, xxxtan.h) is ported or used.
    • _Atan(x, flags): atan, atan2, asin and acos (xxatan.h, xxatan2.h, xxasin.h, xxacos.h) pass x in [0, 1] and flags built from 0x2, 0x4 and 0x8, and return the result as their own. Those callers only make sense if flags 0, 0x2, 0x4 and 0x6 give atan(x), pi/2 - atan(x), pi/2 + atan(x) and pi - atan(x), and 0x8 negates the result. That is what is implemented. An x outside [0, 1], NaN, infinity, the 0x1 bit or bits above 0x8 throw.
    • _Tan(x, quads): Dinkumware's tan(x) is _Tan(x, 0), so quads 0 returns libc's tan, tanf or tanl. Any other quads value throws, because no caller in that library shows what it does.
  • Results. They come from libc's msun atan/atanl/tan/tanf/tanl. For flags 0x2, 0x4 and 0x6 the double version adds a two-part pi/2, as msun's atan2 does.
  • Debt. A Functional entry records that none of this is measured on the console's module and that last bits can differ.
  • No measurements. None were available. Nothing in this repository's PRs or issues measures these six, so no measurement is credited. A firmware reading of the 0x1 bit and of nonzero quads would let the throwing paths be implemented.
  • Debt lists left unchanged. I left the "Additional imported names" lists untouched. The _Atan list entry appears twice word for word, so a fragment cannot change it, and the _LAtan entry is the one #21704 changes. The functions also still throw for every other argument.

Tested

Linux x86-64 (Debian, GCC 14.2.0), Release, BUILD_TESTING=ON, rebased on current main. No title tested.

  • New guest_libc_internal_quadrant_trig passes. It checks:
    • exact values: _Atan(0, 0x2) == pi/2, _Atan(0, 0x6) == pi, _Atan(0, 0xe) == -pi, _Atan(0, 0x8) == -0
    • _Atan(z, 0) == atan(z)
    • each flag combination against atan2 within 2 ulp on 8 samples
    • the acos relation Dinkumware's acos relies on, within 4 ulp
    • the float and long double forms
    • _Tan(x, 0) == tan(x) bit for bit, including NaN and infinity
  • Swapping the sign for flag 0x4 makes the test fail (_Atan 0x4 is pi/2 + atan, and the same for _LAtan).
  • ctest -R "libc_internal|dinkumware|public_headers": 159/159 pass.
  • tools/check_conventions.py --base origin/main passes.
  • tools/pr_overlap.py --branch reports no shared exports. The one conflict is with #21704: line 38 (_LAtan) of include/MathInternals.hpp sits next to that PR's line 37 (_Hypot). Whichever merges second needs a one-line rebase.
  • #13778, #17300, #18135 and #21255 already conflict with main, so they could not be compared automatically. I compared their hunks by hand: none touches these functions or the changed lines.
  • Not tested on Windows or macOS.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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
  • 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:5340957909 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21716). Originally posted by [Krille2004zzz](https://github.com/Krille2004zzz) on 2026-11-16T16:37:01Z. --- ### What Implements `_Atan`, `_FAtan`, `_LAtan`, `_Tan`, `_FTan` and `_LTan` in libSceLibcInternal (new `QuadrantTrig.cpp`), replacing the parameterless stubs. Only the arguments Dinkumware's own callers pass are implemented; every other argument still throws through `NotImplemented_nid_no_patch`. Because of that, `tools/progress.py` still counts all six as pending. - **Signatures.** `_Atan(double, int)`, `_FAtan(float, int)` and `_LAtan(long double, int)` come from Dinkumware's public [xmath.h](https://github.com/ganboing/crt.vc12/blob/f1d47126bf13d1c9dccaa6c53b72217fd0474968/src/xmath.h). `_Tan(x, int quads)` comes from the declaration in Dinkumware V6.50's [xxtan.h](https://github.com/bergsteiger/realwork/blob/19b446ce8ad2adf82ab8ce7988bc003221accad2/common/components/rtl/external/Embarcadero/DelphiBerlin/cpprtl/Source/dinkumware64/source/xxtan.h). - **Behaviour.** It is taken only from how Dinkumware's callers use these functions. No Dinkumware implementation (`xatan.c`, `xxxatan.h`, `xtan.c`, `xxxtan.h`) is ported or used. - `_Atan(x, flags)`: `atan`, `atan2`, `asin` and `acos` ([xxatan.h](https://github.com/bergsteiger/realwork/blob/19b446ce8ad2adf82ab8ce7988bc003221accad2/common/components/rtl/external/Embarcadero/DelphiBerlin/cpprtl/Source/dinkumware64/source/xxatan.h), [xxatan2.h](https://github.com/bergsteiger/realwork/blob/19b446ce8ad2adf82ab8ce7988bc003221accad2/common/components/rtl/external/Embarcadero/DelphiBerlin/cpprtl/Source/dinkumware64/source/xxatan2.h), `xxasin.h`, `xxacos.h`) pass x in [0, 1] and flags built from 0x2, 0x4 and 0x8, and return the result as their own. Those callers only make sense if flags 0, 0x2, 0x4 and 0x6 give atan(x), pi/2 - atan(x), pi/2 + atan(x) and pi - atan(x), and 0x8 negates the result. That is what is implemented. An x outside [0, 1], NaN, infinity, the 0x1 bit or bits above 0x8 throw. - `_Tan(x, quads)`: Dinkumware's `tan(x)` is `_Tan(x, 0)`, so quads 0 returns libc's `tan`, `tanf` or `tanl`. Any other quads value throws, because no caller in that library shows what it does. - **Results.** They come from libc's msun `atan`/`atanl`/`tan`/`tanf`/`tanl`. For flags 0x2, 0x4 and 0x6 the double version adds a two-part pi/2, as msun's `atan2` does. - **Debt.** A Functional entry records that none of this is measured on the console's module and that last bits can differ. - **No measurements.** None were available. Nothing in this repository's PRs or issues measures these six, so no measurement is credited. A firmware reading of the 0x1 bit and of nonzero `quads` would let the throwing paths be implemented. - **Debt lists left unchanged.** I left the "Additional imported names" lists untouched. The `_Atan` list entry appears twice word for word, so a fragment cannot change it, and the `_LAtan` entry is the one #21704 changes. The functions also still throw for every other argument. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Linux x86-64 (Debian, GCC 14.2.0), Release, `BUILD_TESTING=ON`, rebased on current `main`. No title tested. - New `guest_libc_internal_quadrant_trig` passes. It checks: - exact values: `_Atan(0, 0x2) == pi/2`, `_Atan(0, 0x6) == pi`, `_Atan(0, 0xe) == -pi`, `_Atan(0, 0x8) == -0` - `_Atan(z, 0) == atan(z)` - each flag combination against `atan2` within 2 ulp on 8 samples - the `acos` relation Dinkumware's `acos` relies on, within 4 ulp - the float and long double forms - `_Tan(x, 0) == tan(x)` bit for bit, including NaN and infinity - Swapping the sign for flag 0x4 makes the test fail (`_Atan 0x4 is pi/2 + atan`, and the same for `_LAtan`). - `ctest -R "libc_internal|dinkumware|public_headers"`: 159/159 pass. - `tools/check_conventions.py --base origin/main` passes. - `tools/pr_overlap.py --branch` reports no shared exports. The one conflict is with #21704: line 38 (`_LAtan`) of `include/MathInternals.hpp` sits next to that PR's line 37 (`_Hypot`). Whichever merges second needs a one-line rebase. - #13778, #17300, #18135 and #21255 already conflict with `main`, so they could not be compared automatically. I compared their hunks by hand: none touches these functions or the changed lines. - Not tested on Windows or macOS. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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) and the licence notice of code adapted from upstream (see AGENTS.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 - [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
PrivacyOnion commented 2026-11-16 16:43:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-16T16:43:24Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 61ff86a672:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_libc_internal_quadrant_trig_tests built cleanly.
  • CTest (guest_libc_internal_quadrant_trig):
    1/1 Test #4111: guest_libc_internal_quadrant_trig ...   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/InventoryExports2.cpp, core/libs/prx/libSceLibcInternal/include/InventoryExports2.hpp, core/libs/prx/libSceLibcInternal/include/MathInternals.hpp reverted to main, the new tests do not build/link without the change; restoring it makes them build and pass.
  • Code reading:
    • Verified domain and flag validation in KnownAtanArguments (QuadrantTrig.cpp:16-18), ensuring NaN, negative values, values > 1.0, and unknown flag bits trigger NotImplemented_nid_no_patch.
    • Verified double-double extended precision arithmetic for pi/2 and pi offsets in AtanDouble (QuadrantTrig.cpp:20-28) across all four quadrants (0x0, 0x2, 0x4, 0x6) and negation flag (0x8), including preservation of -0.0 when x=0.0 and flags=0x8.
    • Verified _LAtan_nid_postfix (QuadrantTrig.cpp:44-53) and _FAtan_nid_postfix (QuadrantTrig.cpp:39-42) signatures, argument validation, and quadrant transformations.
    • Verified _Tan_nid_postfix, _FTan_nid_postfix, and _LTan_nid_postfix (QuadrantTrig.cpp:55-68) reject non-zero quadrant counts via NotImplemented_nid_no_patch and delegate quadrant 0 to libc's tan/tanf/tanl.
    • Verified export declarations in MathInternals.hpp and InventoryExports2.hpp match the definitions in QuadrantTrig.cpp and that the parameterless stubs were removed from Export.cpp and InventoryExports2.cpp.
    • Verified AGENTS.md compliance, including the technical debt fragment in docs/dev/debt/changes/feat-libsce-libcinternal-tan-atan.md and comprehensive unit test coverage in GuestLibcInternalQuadrantTrig.cpp.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5923754284 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21716#pullrequestreview-5923754284). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-16T16:43:24Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `61ff86a672`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_libc_internal_quadrant_trig_tests` built cleanly. - **CTest** (`guest_libc_internal_quadrant_trig`): ``` 1/1 Test #4111: guest_libc_internal_quadrant_trig ... 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/InventoryExports2.cpp`, `core/libs/prx/libSceLibcInternal/include/InventoryExports2.hpp`, `core/libs/prx/libSceLibcInternal/include/MathInternals.hpp` reverted to `main`, the new tests do not build/link without the change; restoring it makes them build and pass. - **Code reading**: - Verified domain and flag validation in KnownAtanArguments (QuadrantTrig.cpp:16-18), ensuring NaN, negative values, values > 1.0, and unknown flag bits trigger NotImplemented_nid_no_patch. - Verified double-double extended precision arithmetic for pi/2 and pi offsets in AtanDouble (QuadrantTrig.cpp:20-28) across all four quadrants (0x0, 0x2, 0x4, 0x6) and negation flag (0x8), including preservation of -0.0 when x=0.0 and flags=0x8. - Verified _LAtan_nid_postfix (QuadrantTrig.cpp:44-53) and _FAtan_nid_postfix (QuadrantTrig.cpp:39-42) signatures, argument validation, and quadrant transformations. - Verified _Tan_nid_postfix, _FTan_nid_postfix, and _LTan_nid_postfix (QuadrantTrig.cpp:55-68) reject non-zero quadrant counts via NotImplemented_nid_no_patch and delegate quadrant 0 to libc's tan/tanf/tanl. - Verified export declarations in MathInternals.hpp and InventoryExports2.hpp match the definitions in QuadrantTrig.cpp and that the parameterless stubs were removed from Export.cpp and InventoryExports2.cpp. - Verified AGENTS.md compliance, including the technical debt fragment in docs/dev/debt/changes/feat-libsce-libcinternal-tan-atan.md and comprehensive unit test coverage in GuestLibcInternalQuadrantTrig.cpp. Looks Good To Me
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!21716
No description provided.