From 49744e9fe35bd63d241456b27db4dfa4da015ff2 Mon Sep 17 00:00:00 2001 From: Nathan Chandrasekar <322284620+vera-rubin@users.noreply.github.com> Date: Sun, 22 Nov 2026 00:30:03 +0100 Subject: [PATCH] Fix the sine in the small-cosine branch of CROTG/ZROTG 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. --- CONTRIBUTORS.md | 3 +++ interface/zrotg.c | 4 ++-- utest/test_extensions/test_crotg.c | 38 ++++++++++++++++++++++++++++++ utest/test_extensions/test_zrotg.c | 38 ++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 2 deletions(-) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index d251db5688..d633e0c833 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -302,3 +302,6 @@ hheei * Nathan Moinvaziri * [2026-11-14] Fix work buffer overflow in threaded GETRF for large N + +* vera-rubin + * [2026-11-21] Fix the sine in the small-cosine branch of CROTG/ZROTG for complex inputs diff --git a/interface/zrotg.c b/interface/zrotg.c index 4ff1a2a561..836c2d7c94 100644 --- a/interface/zrotg.c +++ b/interface/zrotg.c @@ -126,8 +126,8 @@ void CNAME(void *VDA, void *VDB, FLOAT *C, void *VS) { *R = *DA * (h / adahsq); *(R+1) = *(DA+1) * (h / adahsq); } - *S = *S1 * (*DA) / adahsq; - *(S+1) = *(S1+1) * (*DA) / adahsq; + *S = *S1 * (*DA / adahsq) - *(S1+1) * (*(DA+1) / adahsq); + *(S+1) = *S1 * (*(DA+1) / adahsq) + *(S1+1) * (*DA / adahsq); } *DA=*R; *(DA+1)=*(R+1); diff --git a/utest/test_extensions/test_crotg.c b/utest/test_extensions/test_crotg.c index d84b533517..aab6ad4e89 100644 --- a/utest/test_extensions/test_crotg.c +++ b/utest/test_extensions/test_crotg.c @@ -199,6 +199,25 @@ CTEST(crotg, negative_real_negative_img) ASSERT_DBL_NEAR_TOL(-5.26498f, sa[0], SINGLE_EPS); ASSERT_DBL_NEAR_TOL(-7.01997f, sa[1], SINGLE_EPS); } +/** + * Fortran API specific test + * Test crotg with a complex a much smaller than b, where the cosine + * is computed in the small-cosine branch + */ +CTEST(crotg, unbalanced_complex_a) +{ + float sa[2] = {ldexpf(1.0, -30), ldexpf(1.0, -30)}; + float sb[2] = {ldexpf(1.0, 50), 0.0}; + float ss[2]; + float sc; + BLASFUNC(crotg)(sa, sb, &sc, ss); + ASSERT_DBL_NEAR_TOL(1.41421356237310, ldexp(sc, 80), SINGLE_EPS); + ASSERT_DBL_NEAR_TOL(0.70710678118655, ss[0], SINGLE_EPS); + ASSERT_DBL_NEAR_TOL(0.70710678118655, ss[1], SINGLE_EPS); + ASSERT_DBL_NEAR_TOL(1.41421356237310, ldexp(sa[0], -49), SINGLE_EPS); + ASSERT_DBL_NEAR_TOL(1.41421356237310, ldexp(sa[1], -49), SINGLE_EPS); +} + #ifndef NO_CBLAS /** * C API specific test @@ -363,5 +382,24 @@ CTEST(crotg, c_api_negative_real_negative_img) ASSERT_DBL_NEAR_TOL(-5.26498f, sa[0], SINGLE_EPS); ASSERT_DBL_NEAR_TOL(-7.01997f, sa[1], SINGLE_EPS); } + +/** + * C API specific test + * Test crotg with a complex a much smaller than b, where the cosine + * is computed in the small-cosine branch + */ +CTEST(crotg, c_api_unbalanced_complex_a) +{ + float sa[2] = {ldexpf(1.0, -30), ldexpf(1.0, -30)}; + float sb[2] = {ldexpf(1.0, 50), 0.0}; + float ss[2]; + float sc; + cblas_crotg(sa, sb, &sc, ss); + ASSERT_DBL_NEAR_TOL(1.41421356237310, ldexp(sc, 80), SINGLE_EPS); + ASSERT_DBL_NEAR_TOL(0.70710678118655, ss[0], SINGLE_EPS); + ASSERT_DBL_NEAR_TOL(0.70710678118655, ss[1], SINGLE_EPS); + ASSERT_DBL_NEAR_TOL(1.41421356237310, ldexp(sa[0], -49), SINGLE_EPS); + ASSERT_DBL_NEAR_TOL(1.41421356237310, ldexp(sa[1], -49), SINGLE_EPS); +} #endif #endif diff --git a/utest/test_extensions/test_zrotg.c b/utest/test_extensions/test_zrotg.c index 05cbf6a890..cb8342e687 100644 --- a/utest/test_extensions/test_zrotg.c +++ b/utest/test_extensions/test_zrotg.c @@ -200,6 +200,25 @@ CTEST(zrotg, negative_real_negative_img) ASSERT_DBL_NEAR_TOL(-7.01997150991369, sa[1], DOUBLE_EPS); } +/** + * Fortran API specific test + * Test zrotg with a complex a much smaller than b, where the cosine + * is computed in the small-cosine branch + */ +CTEST(zrotg, unbalanced_complex_a) +{ + double sa[2] = {ldexp(1.0, -200), ldexp(1.0, -200)}; + double sb[2] = {ldexp(1.0, 400), 0.0}; + double ss[2]; + double sc; + BLASFUNC(zrotg)(sa, sb, &sc, ss); + ASSERT_DBL_NEAR_TOL(1.41421356237310, ldexp(sc, 600), DOUBLE_EPS); + ASSERT_DBL_NEAR_TOL(0.70710678118655, ss[0], DOUBLE_EPS); + ASSERT_DBL_NEAR_TOL(0.70710678118655, ss[1], DOUBLE_EPS); + ASSERT_DBL_NEAR_TOL(1.41421356237310, ldexp(sa[0], -399), DOUBLE_EPS); + ASSERT_DBL_NEAR_TOL(1.41421356237310, ldexp(sa[1], -399), DOUBLE_EPS); +} + #ifndef NO_CBLAS /** * C API specific test @@ -364,5 +383,24 @@ CTEST(zrotg, c_api_negative_real_negative_img) ASSERT_DBL_NEAR_TOL(-5.26497863243527, sa[0], DOUBLE_EPS); ASSERT_DBL_NEAR_TOL(-7.01997150991369, sa[1], DOUBLE_EPS); } + +/** + * C API specific test + * Test zrotg with a complex a much smaller than b, where the cosine + * is computed in the small-cosine branch + */ +CTEST(zrotg, c_api_unbalanced_complex_a) +{ + double sa[2] = {ldexp(1.0, -200), ldexp(1.0, -200)}; + double sb[2] = {ldexp(1.0, 400), 0.0}; + double ss[2]; + double sc; + cblas_zrotg(sa, sb, &sc, ss); + ASSERT_DBL_NEAR_TOL(1.41421356237310, ldexp(sc, 600), DOUBLE_EPS); + ASSERT_DBL_NEAR_TOL(0.70710678118655, ss[0], DOUBLE_EPS); + ASSERT_DBL_NEAR_TOL(0.70710678118655, ss[1], DOUBLE_EPS); + ASSERT_DBL_NEAR_TOL(1.41421356237310, ldexp(sa[0], -399), DOUBLE_EPS); + ASSERT_DBL_NEAR_TOL(1.41421356237310, ldexp(sa[1], -399), DOUBLE_EPS); +} #endif #endif