Repository navigation
Conversation
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>
Codecov Report❌ Patch coverage is Please upload reports for the commit 0234317 to get more accurate results.
Additional details and impacted files@@ 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
Continue to review full report in Codecov by Harness.
|
| 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, |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
| * 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 ) ) |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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>
AI disclosure: This PR was prepared with Claude Code.
Summary
xGEEQUBandxGBEQUB(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 andAMAXbecome Infinity for a finite matrix. ForA = diag(1, 4.9E-324, 1),DGEEQUBreturns INFO = 2 (DGEEQUreturns INFO = 0). ForA = diag(1, HUGE, 1),DGEEQUBreturnsAMAX= Infinity (DGEEQUreturns HUGE).Description
Both routines compute
R(I) = RADIX**INT( LOG( R(I) ) / LOGRDX ), whereR(I)holds the largest entry of the row, and likewise forC(J)with the entries scaled by the row factors.RADIX**Efor negative E is evaluated as1 / RADIX**(-E), and RADIX1024 overflows, so the factor is 0. Entries in (2^-1024, 2^-1023) still work, since RADIX-1023 is representable. The testRCMIN.EQ.ZEROthen returns INFO = I (or M+J) for a row or column that is not zero.LOG( X ) / LOGRDXrounds to 1024 (128 in single precision), and RADIX**1024 is Infinity. The quantity that overflows is the rounded factor itself, not an independent computation ofAMAX:AMAXis the largest factor, so it is Infinity too. On the test machine (glibclog) 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 thelogimplementation.xSYEQUB,xHEEQUBandxPOEQUBround with the same expression, but the scale is a reciprocal square root of the entries (xPOEQUBexplicitly,xSYEQUBandxHEEQUBthrough 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 examplexSYEQUBcomputes1/Sfor the row maxima, which is Infinity for a row below 2^-1024), which this PR does not address.Fix
Clamp the quotient
LOG( X ) / LOGRDXin floating point toMINEXPONENT( ONE ) - DIGITS( ONE )..MAXEXPONENT( ONE ) - 1(-1074..1023 in double precision) before converting it to an integer, and form the power withSCALE( ONE, N ).INTbecause an infinite largest entry (an infinite real entry, or a complex entry that is finite but whoseCABS1 = |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 inxGEEQU, on both.CABS1( A(I,J) )*R(I), which is Infinity for an entry whoseCABS1overflows, whateverR(I)is. With the clamp alone that gave a column factor of SMLNUM on top of a row factor of SMLNUM, andxLAQGEthen 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. ForA = diag((HUGE,HUGE), 1),ZGEEQUBnow returns R(1) = 2.2E-308 and C(1) = 1/8, and the entry is scaled to about (0.5,0.5).RADIX**INT( ... ). I compared 5 million random values each in single and double precision and found no difference.AMAXis the radix-rounded maximum, not SMLNUM.xGEEQU.> 0, so a NaN never reaches the conversion. Its handling is the same as before.SCALEuses the model radix,LOGRDXthe radix fromxLAMCH( 'B' ); both are 2 on IEEE machines.Reproducer
On master:
With the fix:
(
AMAXfrom theBroutine is the largest scale factor, a power of the radix, so 2^1023 here.)Regression test
The EQ path (
xCHKEQ) did not test theBvariants at all.SCHKEQ,DCHKEQ,CCHKEQandZCHKEQnow callxGEEQUBandxGBEQUBondiag(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 positiveAMAXand, for the small values,R(2)(rows) orC(2)(columns) equal to BIGNUM. A 1-by-1 matrix with a subnormal entry must give a nonzeroAMAXbelow SMLNUM. InCCHKEQandZCHKEQ, a finite matrix with an entry whoseCABS1overflows must give INFO = 0, a finiteAMAX, 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 reportxGEEQUBandxGBEQUBseparately. On the parent commit all eight routines fail (for exampleDGEEQUBandDGBEQUBwith value 0.450D+16 andSGEEQUBwith 0.839E+07, against the threshold 2); with the fix the four EQ paths pass on x86-64 Linux. The expert drivers that use theBroutines are only built with XBLAS, so they do not cover this.Results on infinite or overflowing maxima (
DGEEQUBon[Inf 1; 1 1],ZGEEQUBon[(HUGE,HUGE) 1; 1 1]), INFO /R(1)/AMAX:Test suite
The full test suite passes, configured as in CI (
CBLAS=ON,LAPACKE=ON,LAPACKE_WITH_TMG=ON) with the_64API. All 333 CTest entries pass, with 5755188 LAPACK tests (5755190 for the_64API), 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 withCMAKE_BUILD_TYPE=Releaseon x86-64 Linux.The second commit (the column pass of the complex routines, and standard
CMPLXin place ofDCMPLXinZCHKEQ) was tested with GNU Fortran 16.2.0 on arm64 macOS with the same CI flags and without the_64API: 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
Local ScalarsandIntrinsic Functionsdeclarations, three lines afterLOGRDX, and the two rounding statements, so a small textual conflict in the declaration area is possible and is easy to resolve.xGEEQU,xGEEQUBand their band versions can still report a column as zero when the productABS( A(I,J) )*R(I)underflows (a tiny entry in a row whose other entries are large). That is shared withxGEEQUand not changed.xSYEQUB,xHEEQUB(see Description).xCHKEQroutines (for thexLAQ*scaling routines), so whichever is merged second needs a trivial rebase of theRESLTSslots.Checklist