feat(libc): implement creal, cimag, conj, cproj, carg and cabs complex helpers #12947
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12947"
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 heditrigui on 2026-10-29T16:09:27Z.
What
Implement 18 C99 / FreeBSD 11
lib/msuncomplex arithmetic helper functions in libc (core/libs/prx/libc/src/FloatBuiltins.cpp) acrossfloat,double, andlong doubleprecisions:creal,crealf,creall(NIDsxgC4Y4+dTOA,v0VslSVHuwc,5+LpHzDWoF8): extract real component (__real__ z).cimag,cimagf,cimagl(NIDsbp2rmLwsf7A,JS9RKdosxa0,x7aaTqwVSbg): extract imaginary component (__imag__ z).conj,conjf,conjl(NIDsR6sIm4bD0F4,WJfkgdE9LME,GnEh47X+NFM): complex conjugation with negated imaginary component.cproj,cprojf,cprojl(NIDsY3MTZ9Ka4oM,LZdcIlEzRgk,b6Z5XNZdGIg): projection onto the Riemann sphere, mapping complex infinities to positive real infinity while preserving imaginary sign.carg,cargf,cargl(NIDsu77BzyEecPM,+pYD-MFdyDI,56MAXko+y6U): phase angle calculation viastd::atan2(__imag__ z, __real__ z).cabs,cabsf,cabsl(NIDsRiqahH9Kcys,+qV-mOKPTzM,MZp5SZBXqEE): modulus calculation viastd::hypot(__real__ z, __imag__ z).core/libs/prx/libc/include/Api.hpp.core/libs/tests/GuestFloatBuiltins.cppexercising real/imag component extraction, Pythagorean triple moduli, phase angles, complex conjugation, and finite/infinite Riemann sphere projection rules across all three precisions.docs/dev/debt/libc.md.Tested
Ran
python tools/check_conventions.py --base upstream/maincleanly (0 errors).Ran
python core/libs/tests/test_public_headers.py --audit-onlycleanly (373 libraries, 14,018 exports verified).Verified library progress increment with
tools/progress.py(libc done increased from 1457 to 1475).Checklist
Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-29T16:17:42Z.
Review: DISMISSED
Review of d27e36c9cd: the NIDs are right and the behaviour is close, but the code doesn't compile on any compiler I tried. Needs changes.
NIDs: all 18 match
tools/nid_names.py --name creal ... cabsl. No other export or stub on main uses them.1. FloatBuiltins.cpp doesn't compile (blocking). On x86-64 Linux, macOS and MinGW,
INFINITYis afloat, soMakeComplex(INFINITY, std::copysign(0.0, ...))incprojandMakeComplex(INFINITY, std::copysign(0.0L, ...))incprojlare ambiguous between the threeMakeComplexoverloads:Fix: pass a typed infinity, e.g.
std::numeric_limits<double>::infinity()/<float>/<long double>(<limits>is needed).2. GuestFloatBuiltins.cpp doesn't compile (blocking).
__complex__ T val{}with a dependentTis rejected by both compilers. Clang reads it as plain_Complex(double) followed by a redeclaration ofT("declaration of 'T' shadows template parameter"). g++ says "expected initializer before 'val'".T _Complexand an alias template fail the same way. The test also callsmakeComplex(INFINITY, 2.)/(1., -INFINITY)/(-INFINITY, 1.L)with mixed types, so deduction fails there too. Three plain non-template overloads plus typed infinities compile everywhere (see the rows below). The relinker CI lanes don't build libc, and the libc lanes were still pending when I checked, so CI hasn't shown this yet.3.
carg/cargf/cabs/cabsfbypass libc's ownatan2f/hypot/hypotf. FreeBSD msuns_carg*.c/s_cabs*.ccall the library'satan2*/hypot*, and main already exports those (Math.cpp:atan2f_nid_postfixis msune_atan2f.c, documented in debt;hypot_nid_postfixandhypotf_nid_postfix(via double) too).std::atan2(float, float)is the host libm. In a 20M-input differential (macOS libm, x86_64 + arm64),cargfdiffers from main'satan2f_nid_postfixon 3,945,322 / 20,000,000 inputs, e.g.(0x1.e3465ap-1, 0x1.319e92p-3): PR gives0x1.41208ap-3and atan2f gives0x1.412088p-3.cabs/cabsfagreed bit-for-bit with main's hypot exports on all 20M inputs, but delegating keeps them consistent if those exports change. Suggested:After that, the debt line can point to the existing
atan2l/hypotlhost-libm entry for the last-bit caveat.4. Test gaps (mutation testing). I ran 11 single-line mutants against the fixed version. With the PR's rows, 5 survive:
cargarguments swapped (atan2(re, im)): (1,1) is symmetriccprojf/cprojldroppingcopysign(+0 imaginary): no row checks the sign of a zero imaginary part for float/long doublecprojftesting onlyisinf(imag): the float infinite row has an infinite real partcabslgoing throughdouble: 3-4-5 fits in a doublecargfvia hoststd::atan2These rows kill 11/11 on x86_64 (80-bit long double, like the guest). On arm64,
long doubleisdouble, so thecabslnarrowing row is equivalent there:with file-scope helpers replacing the lambda:
The second
cargfrow fails on the head implementation once it compiles, and passes with the delegation from item 3.What I ran (macOS arm64; the x86_64 runs used Rosetta): a standalone harness containing your 18 functions verbatim, main's
MakeComplexoverloads, main'satan2f/atanf/hypotf/hypotsliced fromMath.cpp, and your test rows. With fixes 1 and 2 applied and the new rows added, it passes under clang++ x86_64 and arm64 with-fsanitize=address,undefined, and under g++-16 -O2.git merge-treeagainstbb04a4f1eais clean,git diff --checkis clean,check_conventions --base upstream/mainpasses, and the debt line is 522 chars.pr_overlapreports no conflicting lines and no shared exports. I didn't measure anything on a console, so the PR's "console behavior is unverified" debt note still applies.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-29T16:35:33Z.
Review: DISMISSED
Follow-up on 11d451294a. Item 1 is fixed:
FloatBuiltins.cppnow compiles cleanly under clang++ (Apple 21, x86_64 and arm64) and g++-16 with-fsyntax-only. Thanks.Item 2 still blocks.
core/libs/tests/GuestFloatBuiltins.cppstill doesn't compile, and the libc test lanes will fail on it:On this head, replacing the lambda with the three file-scope
TestComplexoverloads from my earlier review, and theINFINITYarguments withstd::numeric_limits<T>::infinity(), gives 0 errors under both compilers (#include <limits>; I made no other changes).Items 3 (delegate
carg*/cabs*to the existingatan2*/hypot*exports;cargfdiffers fromatan2f_nid_postfixon ~20% of inputs) and 4 (rows that kill the 5 surviving mutants) are still open. The rows in my first review still apply as written.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-29T17:02:42Z.
Review: CHANGES_REQUESTED
Thanks,
FloatBuiltins.cppnow compiles and the bodies match the C99 definitions. Still blocking:core/libs/tests/GuestFloatBuiltins.cpp:149-154: the templated lambda[]<typename T>(T r, T i) { __complex__ T val{}; ... }does not compile with GCC/MinGW (expected initializer before 'val') nor clang, as already reported above. Replace it with three plain overloads (float,double,long double) or build the values with__real__/__imag__on typed locals.makeComplex(INFINITY, 2.)mixesfloatanddouble, soTcannot be deduced. Use an infinity of the destination precision (static_cast<double>(INFINITY),HUGE_VALL, ...).cargagainststd::atan2of the same call, which cannot fail if the implementation is wrong. Compare against literal bit patterns (e.g.atan2(1,1)=0x3FE921FB54442D18, andcarg(-1 + 0i)= pi,carg(-1 - 0i)= -pi).conj(+0 + 0i)imaginary must be-0,cproj(-inf + -0i)gives+inf, -0,cproj(nan + inf i)projects,cabs(inf, nan)is+inf.Please run the test with the rest of the guest libc tests on Windows (MinGW) before pushing; the Build job covers it.