Skip to content

Fix MSVC versions of complex power and dot helpers in f2c-translated LAPACK - #6286

Open
sstamenk wants to merge 2 commits into
OpenMathLib:developfrom
sstamenk:fix-msvc-complex-pow-dot
Open

sstamenk wants to merge 2 commits into
OpenMathLib:developfrom
sstamenk:fix-msvc-complex-pow-dot

Conversation

@sstamenk

@sstamenk sstamenk commented Nov 26, 2026 •

Copy link
Copy Markdown

Follow-up to #5574 (from #5562), which fixed the MSVC z_div/c_div macros used by the C-translated LAPACK. Other hand-written _MSC_VER complex helpers in the same per-file headers are still wrong, and some of them are on hot paths:

  • zpow_ui multiplies real and imaginary parts element-wise instead of doing complex multiplication. ZLAHQR uses pow_zi for its shift, so for real-valued input with complex-conjugate eigenvalues the shift degenerates and ZLADIV divides by zero. ZLAHQR/ZHSEQR/ZGEEV then return INFO > 0; for [[1, 0.5], [-1, 1]], ZGEEV returns INFO = 2 with NaN eigenvalues. ZHGEQZ uses it the same way, so ZGGEV/ZGGES are affected. Most cpow_ui copies have the same element-wise multiply; the three that are used (clahqr.c, chgeqz.c, claesy.c) multiply correctly but still invert element-wise for n < 0.
  • zdotu_/cdotu_ use xi*yi instead of xi*yr in the imaginary part, in both stride branches. Their callers include xTRSYL, xSYTRI, xSYTRI_ROOK, xSPTRI and xLATRS/xLATBS/xLATPS; xTRSYL and xSYTRI were confirmed to return wrong results with INFO = 0.
  • cdotc_ has the same yi/yr mixup in its strided branch (e.g. CLAPLL with INCX > 1).

The variants differ from file to file (some copies of zdotc_ were already correct), so this replaces the _MSC_VER branch of all six helpers in every file (493 files, used and unused copies) with one canonical version built on _Cmulcc/_FCmulcc and _Cbuild/_FCbuild, as z_div already does. For negative exponents, the pow helpers use a scaled reciprocal and take the exponent magnitude as unsigned 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:

before after
zlahqr on real matrices with complex eigenvalues (n=2..40) INFO=2..38 pass
zgeev, [[1, 0.5], [-1, 1]] INFO=2 pass
zggev, real pair with complex eigenvalues INFO=2 / 6 pass
ztrsyl/ctrsyl (N/C combinations) relative residual 0.17-0.71 ~1e-16 / ~1e-7
zsytri/csytri (U/L) ‖A·A⁻¹−I‖ 1-6 pass
clapll with INCX=INCY=2 16% error pass
total, 56 checks over both precisions 34/56 56/56

PyTorch's test/torch_np/numpy_tests/linalg/test_linalg.py (TestEig/TestEigvals complex128) and test_eig_numpy/test_eigvals_numpy complex128 in test/test_linalg.py go 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, and dotc/dotu with stride 1 and 2 in both precisions.

I have not run the lapack-netlib TESTING 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_div macros (e.g. INSTALL/dsecnd_NONE.c), so #5574 did not reach every copy.

🤖 Generated with Claude Code

sstamenk and others added 2 commits November 26, 2026 08:17
…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
sstamenk marked this pull request as ready for review November 26, 2026 08:41

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant