Skip to content

Fix the sine in the small-cosine branch of CROTG/ZROTG - #6282

Merged
martin-frbg merged 1 commit into
OpenMathLib:developfrom
vera-rubin:fix/zrotg-small-cosine-sine
Nov 27, 2026
Merged

martin-frbg merged 1 commit into
OpenMathLib:developfrom
vera-rubin:fix/zrotg-small-cosine-sine

Conversation

@vera-rubin

Copy link
Copy Markdown
Contributor

In the unscaled path of interface/zrotg.c, when |a|^2 < (|a|^2 + |b|^2) * safmin (a is tiny next to b), the sine is computed as

*S = *S1 * (*DA) / adahsq;
*(S+1) = *(S1+1) * (*DA) / adahsq;

This multiplies conj(b) by the real part of a only. For a complex a the sine has the wrong phase and |c|^2 + |s|^2 != 1. Reference BLAS computes s = conjg(g) * (f / d) in this branch.

Example: zrotg with a = (2^-200, 2^-200) and b = (2^400, 0) returns s = (0.7071, 0) instead of (0.7071, 0.7071). crotg with a = (2^-30, 2^-30) and b = (2^50, 0) does the same.

The fix uses the full complex product, written the same way as in the neighbouring ada >= h * safmin branch. This branch is separate from the scaled-path sine fixed in #6270 (#6269); the two changes do not overlap.

Tests: new unbalanced_complex_a and c_api_unbalanced_complex_a cases in utest/test_extensions/test_crotg.c and test_zrotg.c check c, s and r for these inputs through the Fortran and C interfaces. All four fail before the fix (s[1] is 0) and pass after it. I also added myself to CONTRIBUTORS.md.

Tested with make TARGET=HASWELL DYNAMIC_ARCH=0 NO_LAPACK=1 NOFORTRAN=1 NO_SHARED=1 USE_OPENMP=0 on Linux x86-64 with GCC 13.3: utest 159 of 159 and utest_ext 1487 of 1487 pass with the fix, and so do the CBLAS tests in ctest. Without it, only the four new cases fail. I had no Fortran compiler, so the Fortran BLAS tests and the LAPACK tests did not run.

When |a|^2 < (|a|^2 + |b|^2) * safmin, the unscaled branch computed the
sine from the real part of a only, so for a complex a the sine had the
wrong phase and |c|^2 + |s|^2 != 1. Use the full product
conj(b) * a / sqrt(|a|^2 * h), as the neighbouring branch does.
@martin-frbg

Copy link
Copy Markdown
Collaborator

Thanks - not sure what happened to CI, I'll close and reopen to initiate a (hopefully) full rerun

@martin-frbg martin-frbg reopened this Nov 25, 2026
@martin-frbg
martin-frbg merged commit 402f17f into OpenMathLib:develop Nov 27, 2026
109 of 110 checks passed
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.

2 participants