Repository navigation
Conversation
…LAPACK The _MSC_VER branches of the helpers in the C-translated LAPACK sources did not implement complex arithmetic correctly: - zpow_ui (and cpow_ui outside clahqr/chgeqz/claesy) multiplied real and imaginary parts element-wise, and inverted them element-wise for negative exponents. ZLAHQR uses pow_zi for its shift, so for real input with complex-conjugate eigenvalues the shift degenerates, ZLADIV divides by zero and ZLAHQR/ZHSEQR/ZGEEV return INFO > 0 with NaN eigenvalues. ZHGEQZ (ZGGEV/ZGGES) is affected the same way. - zdotu_/cdotu_ used xi*yi instead of xi*yr in the imaginary part, giving wrong results (with INFO = 0) from xTRSYL, xSYTRI, xSPTRI and others in both precisions. - the strided branch of cdotc_ had the same yi/yr mixup (e.g. CLAPLL with INCX > 1). Replace all copies of these MSVC helpers with one canonical version that uses _Cmulcc/_FCmulcc and _Cbuild/_FCbuild, including the unused copies so they cannot be picked up broken later. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…w_ui For negative exponents, compute 1/x with Smith's scaled division so that inputs such as 1e200 or 1e-200 give the representable result instead of 0 or NaN, and take the exponent magnitude in an unsigned long long without evaluating -n, so the most negative integer and ILP64 exponents are handled. All current callers use n = 2, so this does not change results there. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
sstamenk
marked this pull request as ready for review
November 26, 2026 08:41
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #5574 (from #5562), which fixed the MSVC
z_div/c_divmacros used by the C-translated LAPACK. Other hand-written_MSC_VERcomplex helpers in the same per-file headers are still wrong, and some of them are on hot paths:zpow_uimultiplies real and imaginary parts element-wise instead of doing complex multiplication.ZLAHQRusespow_zifor its shift, so for real-valued input with complex-conjugate eigenvalues the shift degenerates andZLADIVdivides by zero.ZLAHQR/ZHSEQR/ZGEEVthen returnINFO > 0; for[[1, 0.5], [-1, 1]],ZGEEVreturnsINFO = 2with NaN eigenvalues.ZHGEQZuses it the same way, soZGGEV/ZGGESare affected. Mostcpow_uicopies have the same element-wise multiply; the three that are used (clahqr.c,chgeqz.c,claesy.c) multiply correctly but still invert element-wise forn < 0.zdotu_/cdotu_usexi*yiinstead ofxi*yrin the imaginary part, in both stride branches. Their callers includexTRSYL,xSYTRI,xSYTRI_ROOK,xSPTRIandxLATRS/xLATBS/xLATPS;xTRSYLandxSYTRIwere confirmed to return wrong results withINFO = 0.cdotc_has the sameyi/yrmixup in its strided branch (e.g.CLAPLLwithINCX > 1).The variants differ from file to file (some copies of
zdotc_were already correct), so this replaces the_MSC_VERbranch of all six helpers in every file (493 files, used and unused copies) with one canonical version built on_Cmulcc/_FCmulccand_Cbuild/_FCbuild, asz_divalready does. For negative exponents, the pow helpers use a scaled reciprocal and take the exponent magnitude asunsigned long long. No current caller uses a negative exponent, but the unscaled form would overflow or underflow at large or small magnitudes. The non-MSVC branches and everything outside these helper bodies are byte-for-byte unchanged. The files were rewritten by a small script, so a different formatting is easy to regenerate if preferred.Testing
Windows testing was done with clang-cl 19 / Windows SDK 26100,
-DC_LAPACK=ON -DTARGET=HASWELL -DDYNAMIC_ARCH=OFF(same settings as #5562), on the OpenBLAS commit used by AMD's TheRock (18638c7), with an equivalent patch to the 59 files that actually call these helpers. I have not built this exact branch on Windows. The routines were called directly through ctypes and checked against residuals or numpy/scipy references:zlahqron real matrices with complex eigenvalues (n=2..40)INFO=2..38zgeev,[[1, 0.5], [-1, 1]]INFO=2zggev, real pair with complex eigenvaluesINFO=2/6ztrsyl/ctrsyl(N/C combinations)zsytri/csytri(U/L)‖A·A⁻¹−I‖1-6clapllwithINCX=INCY=2PyTorch's
test/torch_np/numpy_tests/linalg/test_linalg.py(TestEig/TestEigvalscomplex128) andtest_eig_numpy/test_eigvals_numpycomplex128 intest/test_linalg.pygo from failing to passing. No other tests in the CPU subset changed.For this branch, a standalone gcc build of the new helper bodies (with emulated
_Fcomplex/_Dcomplex) checks pow for n = -3..5, reciprocals of 1e±200 / 1e±30 (where the unscaled form returns 0 or NaN), the most negative exponent, anddotc/dotuwith stride 1 and 2 in both precisions.I have not run the
lapack-netlibTESTING drivers on Windows. As far as I can tell, no CI job builds the translated LAPACK (C_LAPACK=ON) with MSVC or clang-cl, which is likely how this went unnoticed.Separately (not in this PR): some files still carry an older form of the
c_div/z_divmacros (e.g.INSTALL/dsecnd_NONE.c), so #5574 did not reach every copy.🤖 Generated with Claude Code