Skip to content

Keep the xGEEQUB and xGBEQUB scale factors in the safe range - #1506

Open
rmlarsen wants to merge 2 commits into
Reference-LAPACK:masterfrom
rmlarsen:geequb-radix-rounding
Open

rmlarsen wants to merge 2 commits into
Reference-LAPACK:masterfrom
rmlarsen:geequb-radix-rounding

Conversation

@rmlarsen

@rmlarsen rmlarsen commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

AI disclosure: This PR was prepared with Claude Code.

Summary

xGEEQUB and xGBEQUB (s/d/c/z) round each row and column scale factor to a power of the radix without limiting the exponent. For a row or column whose largest entry is subnormal enough, the factor underflows to 0 and the routine reports the row as exactly zero. For a largest entry close to the overflow threshold, the factor and AMAX become Infinity for a finite matrix. For A = diag(1, 4.9E-324, 1), DGEEQUB returns INFO = 2 (DGEEQU returns INFO = 0). For A = diag(1, HUGE, 1), DGEEQUB returns AMAX = Infinity (DGEEQU returns HUGE).

Description

Both routines compute R(I) = RADIX**INT( LOG( R(I) ) / LOGRDX ), where R(I) holds the largest entry of the row, and likewise for C(J) with the entries scaled by the row factors.

  • When the largest entry X is at most 2^-1024 (2^-128 in single precision), the exponent is -1024 or lower. RADIX**E for negative E is evaluated as 1 / RADIX**(-E), and RADIX1024 overflows, so the factor is 0. Entries in (2^-1024, 2^-1023) still work, since RADIX-1023 is representable. The test RCMIN.EQ.ZERO then returns INFO = I (or M+J) for a row or column that is not zero.
  • When X is close to HUGE, LOG( X ) / LOGRDX rounds to 1024 (128 in single precision), and RADIX**1024 is Infinity. The quantity that overflows is the rounded factor itself, not an independent computation of AMAX: AMAX is the largest factor, so it is Infinity too. On the test machine (glibc log) this happens for X >= 1.79769313486217E308, about 1E-13 relative below HUGE = 1.7976931348623157E308 (X >= 3.4028117E38 in single precision, HUGE = 3.4028235E38). Exactly where the threshold lies depends on the log implementation.

xSYEQUB, xHEEQUB and xPOEQUB round with the same expression, but the scale is a reciprocal square root of the entries (xPOEQUB explicitly, xSYEQUB and xHEEQUB through their iteration), so in the experiments run here the exponent stayed within about +-540 and RADIX**E did not overflow or underflow. They are not changed. They have other failures for extreme inputs that are unrelated to the rounding (for example xSYEQUB computes 1/S for the row maxima, which is Infinity for a row below 2^-1024), which this PR does not address.

Fix

Clamp the quotient LOG( X ) / LOGRDX in floating point to MINEXPONENT( ONE ) - DIGITS( ONE ) .. MAXEXPONENT( ONE ) - 1 (-1074..1023 in double precision) before converting it to an integer, and form the power with SCALE( ONE, N ).

  • The clamp is before INT because an infinite largest entry (an infinite real entry, or a complex entry that is finite but whose CABS1 = |re| + |im| overflows) would otherwise be converted from Infinity to INTEGER, which is undefined. Clamping after the conversion would be wrong: on x86-64 the conversion gives INT_MIN, a clamp to the lowest exponent, and so the row would be scaled up by the largest factor with INFO = 0, while on AArch64 it saturates the other way. Now such a row gets 2^1023 and is scaled down, as in xGEEQU, on both.
  • In the complex routines the column pass formed CABS1( A(I,J) )*R(I), which is Infinity for an entry whose CABS1 overflows, whatever R(I) is. With the clamp alone that gave a column factor of SMLNUM on top of a row factor of SMLNUM, and xLAQGE then scaled the entry to exactly zero. The row factor is now applied to the real and the imaginary part before they are added (CGEEQUB, ZGEEQUB, CGBEQUB, ZGBEQUB). R(I) is a power of the radix, so the value is unchanged wherever nothing underflows. For A = diag((HUGE,HUGE), 1), ZGEEQUB now returns R(1) = 2.2E-308 and C(1) = 1/8, and the entry is scaled to about (0.5,0.5).
  • For exponents in the normal range the factor is bit for bit the old RADIX**INT( ... ). I compared 5 million random values each in single and double precision and found no difference.
  • A subnormal largest entry gives its exact power of the radix, so it is not reported as zero and AMAX is the radix-rounded maximum, not SMLNUM.
  • The later clamp of the inverted factors to SMLNUM and BIGNUM is unchanged. The Purpose section already says the factors are restricted to powers of the radix between SMLNUM and BIGNUM; it now adds that a row or column whose largest entry is smaller than SMLNUM is scaled as if that entry were SMLNUM, as in xGEEQU.
  • NaN: the factor is only formed when the maximum is > 0, so a NaN never reaches the conversion. Its handling is the same as before.
  • SCALE uses the model radix, LOGRDX the radix from xLAMCH( 'B' ); both are 2 on IEEE machines.

Reproducer

program repro
  implicit none
  double precision :: a(3,3), r(3), c(3), rowcnd, colcnd, amax
  integer :: info, k
  double precision :: x(2)
  x = [4.9406564584124654d-324, huge(1d0)]   ! smallest subnormal; HUGE
  do k = 1, 2
    a = 0
    a(1,1) = 1; a(2,2) = x(k); a(3,3) = 1
    call dgeequb(3, 3, a, 3, r, c, rowcnd, colcnd, amax, info)
    print '(a,es10.3,a,i2,a,es10.3,a,es10.3)', 'DGEEQUB a22=', x(k), &
      '  INFO=', info, '  R(2)=', r(2), '  AMAX=', amax
    call dgeequ(3, 3, a, 3, r, c, rowcnd, colcnd, amax, info)
    print '(a,es10.3,a,i2,a,es10.3,a,es10.3)', 'DGEEQU  a22=', x(k), &
      '  INFO=', info, '  R(2)=', r(2), '  AMAX=', amax
  end do
end program repro

On master:

DGEEQUB a22= 4.941-324  INFO= 2  R(2)= 0.000E+00  AMAX= 1.000E+00
DGEEQU  a22= 4.941-324  INFO= 0  R(2)= 4.494+307  AMAX= 1.000E+00
DGEEQUB a22= 1.798+308  INFO= 0  R(2)= 2.225-308  AMAX=  Infinity
DGEEQU  a22= 1.798+308  INFO= 0  R(2)= 2.225-308  AMAX= 1.798+308

With the fix:

DGEEQUB a22= 4.941-324  INFO= 0  R(2)= 4.494+307  AMAX= 1.000E+00
DGEEQU  a22= 4.941-324  INFO= 0  R(2)= 4.494+307  AMAX= 1.000E+00
DGEEQUB a22= 1.798+308  INFO= 0  R(2)= 2.225-308  AMAX= 8.988+307
DGEEQU  a22= 1.798+308  INFO= 0  R(2)= 2.225-308  AMAX= 1.798+308

(AMAX from the B routine is the largest scale factor, a power of the radix, so 2^1023 here.)

Regression test

The EQ path (xCHKEQ) did not test the B variants at all. SCHKEQ, DCHKEQ, CCHKEQ and ZCHKEQ now call xGEEQUB and xGBEQUB on diag(1, X, 1) and on [1 X; 1 0] for X = SMLNUM*EPS (the smallest subnormal), SMLNUM/4 and the overflow threshold, the two small values only if they are nonzero (platforms that flush subnormals to zero skip them). They require INFO = 0, a finite positive AMAX and, for the small values, R(2) (rows) or C(2) (columns) equal to BIGNUM. A 1-by-1 matrix with a subnormal entry must give a nonzero AMAX below SMLNUM. In CCHKEQ and ZCHKEQ, a finite matrix with an entry whose CABS1 overflows must give INFO = 0, a finite AMAX, a row and column factor below 1, and a product (HUGE*R(1))*C(1) between 1/10 and 10, so that the two factors together do not scale the entry to zero. Two new result slots report xGEEQUB and xGBEQUB separately. On the parent commit all eight routines fail (for example DGEEQUB and DGBEQUB with value 0.450D+16 and SGEEQUB with 0.839E+07, against the threshold 2); with the fix the four EQ paths pass on x86-64 Linux. The expert drivers that use the B routines are only built with XBLAS, so they do not cover this.

Results on infinite or overflowing maxima (DGEEQUB on [Inf 1; 1 1], ZGEEQUB on [(HUGE,HUGE) 1; 1 1]), INFO / R(1) / AMAX:

  • master, x86-64: INFO = 1, R(1) = 0, AMAX = 1; master, AArch64: INFO = 0, R(1) = 2.2E-308, AMAX = Infinity.
  • with the fix, both: INFO = 0, R(1) = 2.2E-308, AMAX = 8.988E+307.

Test suite

The full test suite passes, configured as in CI (CBLAS=ON, LAPACKE=ON, LAPACKE_WITH_TMG=ON) with the _64 API. All 333 CTest entries pass, with 5755188 LAPACK tests (5755190 for the _64 API), 11973112 LAPACKE tests, 319677 BLAS tests and 645492 CBLAS tests for each API, and 0 numerical errors and 0 other errors. These are the counts of master; the equilibration tests are not counted in them. A second build with CI's gfortran flags (-O2 -Wall -Wno-unused-dummy-argument -Wno-unused-variable -Wno-unused-label -Werror=conversion -fimplicit-none -frecursive -fcheck=all) passes the suite as well, and the changed files compile with the same warnings as on master. The builds used GNU Fortran and GCC 15.2.0 with CMAKE_BUILD_TYPE=Release on x86-64 Linux.

The second commit (the column pass of the complex routines, and standard CMPLX in place of DCMPLX in ZCHKEQ) was tested with GNU Fortran 16.2.0 on arm64 macOS with the same CI flags and without the _64 API: all 129 CTest entries pass, with 5755158 LAPACK tests and no errors. Without the source change the tightened check fails in all four complex routines.

Not changed here

  • cleanup: normalize named constants in LAPACK *equb routines #1227 ("cleanup: normalize named constants in LAPACK *equb routines") touches the declarations and constants of the same files. The hunks here are the Local Scalars and Intrinsic Functions declarations, three lines after LOGRDX, and the two rounding statements, so a small textual conflict in the declaration area is possible and is easy to resolve.
  • xGEEQU, xGEEQUB and their band versions can still report a column as zero when the product ABS( A(I,J) )*R(I) underflows (a tiny entry in a row whose other entries are large). That is shared with xGEEQU and not changed.
  • Extreme inputs to xSYEQUB, xHEEQUB (see Description).
  • Another pending change adds a test to the same xCHKEQ routines (for the xLAQ* scaling routines), so whichever is merged second needs a trivial rebase of the RESLTS slots.

Checklist

  • The documentation has been updated. (The Purpose section of the eight routines states how rows below SMLNUM are treated.)
  • If the PR solves a specific issue, it is set to be closed on merge. (No tracking issue.)

xGEEQUB and xGBEQUB round each row and column scale factor to a power
of the radix with RADIX**INT( LOG( X ) / LOGRDX ), where X is the
largest entry of the row or column, in the second pass scaled by the
row factors. The exponent is not limited to the range in which that
power is representable, which gives two failures.

For X at or below 2**(-1024) (2**(-128) in single precision) the
exponent is -1024 or lower. RADIX**E is evaluated as 1 / RADIX**(-E),
which overflows, so the factor becomes 0. The test on the smallest
factor then reports the row as exactly zero, with INFO = I (or M+J),
although it is not zero. diag(1, 4.9E-324, 1) gives INFO = 2 from
DGEEQUB and DGBEQUB, while DGEEQU returns INFO = 0 with R(2) = 2**1022.

For X within about 1E-13 (relative) of the overflow threshold, the
quotient LOG( X ) / LOGRDX rounds to 1024 (128 in single precision) and
RADIX**1024 is Infinity. The factor is then Infinity, and so is AMAX,
the largest factor, for a finite matrix. diag(1, HUGE, 1) gives
AMAX = Infinity from DGEEQUB where DGEEQU gives HUGE.

Clamp the quotient LOG( X ) / LOGRDX in floating point to
MINEXPONENT( ONE ) - DIGITS( ONE ) .. MAXEXPONENT( ONE ) - 1 before it
is converted to an integer, and form the power with SCALE( ONE, N ).
The clamp comes before INT because an infinite X (a complex entry whose
CABS1 = |re| + |im| overflows, or an infinite real entry) would
otherwise be converted from Infinity, which is undefined: on x86-64 it
gives INT_MIN, a clamp to the lowest exponent and so the largest
scale-up of the row. Now such a row gets 2**1023 and is scaled down, as
xGEEQU does. For exponents in the normal range the factor is bit for
bit the old one (5 million random values in each of single and double
precision, no difference). A subnormal X gives the exact subnormal
power, so it is neither reported as zero nor raised to SMLNUM in AMAX.
The following clamp of the inverted factors to SMLNUM and BIGNUM is
unchanged, and the Purpose section says so. X is not NaN here, since
the factor is only formed when X > 0.

xSYEQUB, xHEEQUB and xPOEQUB round with the same expression, but their
exponents come from a square root of the scale, so they stay within
about +-540 and cannot reach either failure; they are unchanged.

Test: SCHKEQ, DCHKEQ, CCHKEQ and ZCHKEQ did not test the B variants at
all. They now call xGEEQUB and xGBEQUB on diag( 1, X, 1 ) and on
[ 1 X; 1 0 ] for X = SMLNUM*EPS (the smallest subnormal), SMLNUM/4 and
the overflow threshold (the two small values only if they are nonzero,
for platforms that flush subnormals), requiring INFO = 0, a finite
positive AMAX and, for the small values, R( 2 ) or C( 2 ) equal to
BIGNUM. A 1-by-1 matrix with a subnormal entry must give a nonzero
AMAX below SMLNUM, and in the complex tests a finite matrix with
CABS1 overflow must give INFO = 0, a finite AMAX and a row and column
scaled down. On the parent commit the new checks fail in all eight
routines (for example DGEEQUB and DGBEQUB with value 4.5E+15 against
the threshold 2); with the fix the four EQ paths pass.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@rmlarsen
rmlarsen marked this pull request as draft November 22, 2026 03:15
@rmlarsen
rmlarsen marked this pull request as ready for review November 22, 2026 03:25
@codecov

codecov Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.71429% with 44 lines in your changes missing coverage. Please review.
✅ Project coverage is 70.73%. Comparing base (ef55c9b) to head (98ffa44).
⚠️ Report is 3 commits behind head on master.

⚠️ Current head 98ffa44 differs from pull request most recent head 0234317

Please upload reports for the commit 0234317 to get more accurate results.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
TESTING/LIN/cchkeq.f 84.41% 12 Missing ⚠️
TESTING/LIN/zchkeq.f 84.41% 12 Missing ⚠️
TESTING/LIN/dchkeq.f 83.60% 10 Missing ⚠️
TESTING/LIN/schkeq.f 83.60% 10 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1506      +/-   ##
==========================================
+ Coverage   70.63%   70.73%   +0.09%     
==========================================
  Files        6243     6243              
  Lines      501290   501574     +284     
  Branches    23516    23516              
==========================================
+ Hits       354100   354800     +700     
+ Misses     146327   145911     -416     
  Partials      863      863              
Components Coverage Δ
BLAS 99.73% <ø> (ø)
CBLAS 96.95% <ø> (ø)
LAPACK 83.96% <100.00%> (+0.23%) ⬆️
LAPACKE 4.18% <ø> (ø)
TMGLIB 55.69% <ø> (ø)
BLAS testing 88.79% <ø> (ø)
CBLAS testing 89.65% <ø> (ø)
LAPACK testing 82.40% <84.05%> (+<0.01%) ⬆️
LAPACKE testing ∅ <ø> (∅)
Files with missing lines Coverage Δ
SRC/cgbequb.f 74.39% <100.00%> (+74.39%) ⬆️
SRC/cgeequb.f 75.32% <100.00%> (+75.32%) ⬆️
SRC/dgbequb.f 74.39% <100.00%> (+74.39%) ⬆️
SRC/dgeequb.f 75.32% <100.00%> (+75.32%) ⬆️
SRC/sgbequb.f 74.39% <100.00%> (+74.39%) ⬆️
SRC/sgeequb.f 75.32% <100.00%> (+75.32%) ⬆️
SRC/zgbequb.f 74.39% <100.00%> (+74.39%) ⬆️
SRC/zgeequb.f 75.32% <100.00%> (+75.32%) ⬆️
TESTING/LIN/dchkeq.f 88.88% <83.60%> (-1.45%) ⬇️
TESTING/LIN/schkeq.f 88.88% <83.60%> (-1.45%) ⬇️
... and 2 more

Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ef55c9b...0234317. Read the comment docs.

Comment thread SRC/cgeequb.f
80 CONTINUE
IF( C( J ).GT.ZERO ) THEN
C( J ) = RADIX**INT( LOG( C( J ) ) / LOGRDX )
C( J ) = SCALE( ONE, INT( MAX( EMINR, MIN( EMAXR,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CABS1-overflow case still gives a wrong column factor. CABS1( A( I, J ) )*R( I ) forms the overflowed CABS1 first, so the product is Inf whatever R( I ) is; the new clamp turns that into 2**EMAXR and C( J ) comes out as SMLNUM, on top of R( I ) = SMLNUM.

Reproducer against this branch (gfortran 16.2, arm64 macOS): A = diag( (HUGE,HUGE), 1 ).

CGEEQUB info=0  R= 1.17549435E-38  1.0   C= 1.17549435E-38  1.0
CLAQGE  equed=B A11= (0.0,0.0)   A22= (1.0,0.0)

ZGEEQUB/ZLAQGE do the same (R(1) = C(1) = 2.2E-308, A11 = 0), so the equilibrated matrix is exactly singular although A is finite and nonsingular. With (HUGE/4,HUGE/4) instead, C(1) = 1 and A11 scales to (1,1). Even with the two-factor fix of #1507 the entry would end up near 5E-38 instead of O(1).

The new xCHKEQ case only checks that both factors are below one, so it passes. Suggested fix, here and in xGBEQUB (c/z): scale before summing, e.g. ABS( REAL( A( I, J ) ) )*R( I ) + ABS( AIMAG( A( I, J ) ) )*R( I ), and have the test apply the factors and check that the scaled entry is O(1).

Found in an AI-assisted self-review (Claude Code).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 0234317: the column pass of CGEEQUB, ZGEEQUB, CGBEQUB and ZGBEQUB now applies the row factor to the real and the imaginary part before adding them. The example gives R(1) = SMLNUM, C(1) = 1/8 and a scaled entry of about (0.5,0.5). The test now also requires (HUGE*R(1))*C(1) to lie between 1/10 and 10, which fails in all four routines without the source change.

Comment thread TESTING/LIN/zchkeq.f Outdated
* The row and column holding it must be scaled down, not reported
* as zero or scaled up.
*
A( 1, 1 ) = DCMPLX( XS( 3 ), XS( 3 ) )

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DCMPLX is a non-standard intrinsic. 773fd2f ("TESTING: replace non-standard double complex intrinsics") removed every use from TESTING because NAG Fortran only accepts it under -dcfuns, and this reintroduces it (the only use in this PR). The NAG jobs are skipped on fork PRs (they finish in a few seconds here) but do build on master, so this would only show up after merge. Use CMPLX( ..., KIND=... ) with a kind parameter, as that commit did.

Found in an AI-assisted self-review (Claude Code).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 0234317: CMPLX( ..., KIND=DP ).

The column pass of CGEEQUB, ZGEEQUB, CGBEQUB and ZGBEQUB forms
CABS1( A( I, J ) )*R( I ). For a finite entry whose CABS1 = |re| + |im|
overflows, CABS1 is Infinity before the row factor is applied, so the
product is Infinity whatever R( I ) is. The clamp added on this branch
turns that into the largest power of the radix, and the column factor
becomes SMLNUM on top of a row factor of SMLNUM.

For A = diag( (HUGE,HUGE), 1 ) the routines returned
R( 1 ) = C( 1 ) = SMLNUM, and xLAQGE then scaled A( 1, 1 ) to exactly
zero: the equilibrated matrix was singular although A is not. The new
test did not see it, because it only required both factors to be below
one.

Apply the row factor to the real and the imaginary part before adding
them. R( I ) is at most SMLNUM when the row maximum overflows, so each
scaled part is at most 4 and the sum is finite. R( I ) is a power of
the radix, so for entries where nothing underflows the value is the
one computed before. The example now gives R( 1 ) = SMLNUM and
C( 1 ) = 1/8, and the entry is scaled to about (0.5,0.5). The row pass
is unchanged: an infinite row maximum is clamped to the same factor as
the exact one, 2*HUGE, would be.

Test: the CABS1 overflow case in CCHKEQ and ZCHKEQ now also requires
( HUGE*R( 1 ) )*C( 1 ) to lie between 1/10 and 10. Without the source
change all four routines fail it (value 0.839E+07 in single and
0.450D+16 in double precision); with it the EQ paths pass, and the
full suite built with the CI gfortran flags runs 5755158 LAPACK tests
with no failures.

ZCHKEQ also built that entry with DCMPLX, a VAX extension that NAG
Fortran rejects and that 773fd2f removed from TESTING. It now uses
CMPLX( ..., KIND=DP ).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

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