Skip to content

gesvj/gsvj0: Avoid integer overflow in EMPTSW = N*(N-1)/2 - #1454

Merged
jschueller merged 2 commits into
Reference-LAPACK:masterfrom
jschueller:issue1312-src5
Oct 29, 2026
Merged

jschueller merged 2 commits into
Reference-LAPACK:masterfrom
jschueller:issue1312-src5

Conversation

@jschueller

Copy link
Copy Markdown
Collaborator

N*(N-1) overflows 32-bit signed arithmetic for N >= 46342 even though the quotient N*(N-1)/2 still fits (up to N = 65536). The overflowed threshold corrupts the NOTROT >= EMPTSW convergence decision (and traps under -ftrapv). Halve the even factor first; one of N, N-1 is always even so the division is exact and the intermediate never exceeds the final value.

Part of #1312

@jschueller jschueller mentioned this pull request Oct 26, 2026
@codecov

codecov Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.95652% with 24 lines in your changes missing coverage. Please review.
✅ Project coverage is 70.04%. Comparing base (6a1a8b8) to head (9d260b7).
⚠️ Report is 6 commits behind head on master.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
TESTING/EIG/test_cgesvj_emptsw.f90 86.36% 4 Missing and 2 partials ⚠️
TESTING/EIG/test_dgesvj_emptsw.f90 86.36% 4 Missing and 2 partials ⚠️
TESTING/EIG/test_sgesvj_emptsw.f90 86.36% 4 Missing and 2 partials ⚠️
TESTING/EIG/test_zgesvj_emptsw.f90 86.36% 4 Missing and 2 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1454      +/-   ##
==========================================
+ Coverage   69.72%   70.04%   +0.32%     
==========================================
  Files        6204     6208       +4     
  Lines      497194   497409     +215     
  Branches    23430    23450      +20     
==========================================
+ Hits       346652   348401    +1749     
+ Misses     150116   148574    -1542     
- Partials      426      434       +8     
Components Coverage Δ
BLAS 99.76% <ø> (ø)
CBLAS 96.98% <ø> (ø)
LAPACK 83.45% <100.00%> (+0.79%) ⬆️
LAPACKE 2.15% <ø> (ø)
TMGLIB 55.69% <ø> (ø)
BLAS testing 88.77% <ø> (ø)
CBLAS testing 89.63% <ø> (ø)
LAPACK testing 82.26% <86.36%> (+0.01%) ⬆️
LAPACKE testing ∅ <ø> (∅)
Files with missing lines Coverage Δ
SRC/cgesvj.f 69.59% <100.00%> (+3.44%) ⬆️
SRC/cgsvj0.f 62.90% <100.00%> (+62.90%) ⬆️
SRC/dgesvj.f 73.81% <100.00%> (+2.97%) ⬆️
SRC/dgsvj0.f 64.43% <100.00%> (+64.43%) ⬆️
SRC/sgesvj.f 75.00% <100.00%> (+2.96%) ⬆️
SRC/sgsvj0.f 69.50% <100.00%> (+69.50%) ⬆️
SRC/zgesvj.f 71.15% <100.00%> (+3.43%) ⬆️
SRC/zgsvj0.f 57.09% <100.00%> (+57.09%) ⬆️
TESTING/EIG/test_cgesvj_emptsw.f90 86.36% <86.36%> (ø)
TESTING/EIG/test_dgesvj_emptsw.f90 86.36% <86.36%> (ø)
... and 2 more

... and 14 files with indirect coverage changes


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 6a1a8b8...9d260b7. Read the comment docs.

N*(N-1) overflows 32-bit signed arithmetic for N >= 46342 even
though the quotient N*(N-1)/2 still fits (up to N = 65536). The
overflowed threshold corrupts the NOTROT >= EMPTSW convergence
decision (and traps under -ftrapv). Halve the even factor first;
one of N, N-1 is always even so the division is exact and the
intermediate never exceeds the final value.

Part of Reference-LAPACK#1312

@mohawk2 mohawk2 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The actual formula to avoid overflow is a thing of beauty. It did make me think that in C, the MOD would be achieved with a bitwise & 1.

The new tests do exercise a similarly-written formula, but do not constitute a guarantee that the actual code in the routine will stay using that formula. Is there a way to get at the calculation directly? Or even make it a routine that is called by the driver, but independently callable by a test program?

@mohawk2 mohawk2 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The updated test code is indeed a lot clearer with if instead of +abs(value-ref). The question about the test formula's lack of direct connection to the live code remains.

@ACSimon33

Copy link
Copy Markdown
Collaborator

Instead of these new unit tests, which just check the INFO and whether singular values are positive, we could raise the test sizes in TESTING/svd.in. Either we could add a new test:

20                                            Number of values of M
0 0 0 1 1 1 2 2 3 3 3 10 10 16 16 30 30 40 40 66 Values of M
0 1 3 0 1 2 0 1 0 1 3 10 16 10 16 30 40 30 40 66 Values of N

or we could just replace the 40 with 66:

19                                            Number of values of M
0 0 0 1 1 1 2 2 3 3 3 10 10 16 16 30 30 66 66 Values of M
0 1 3 0 1 2 0 1 0 1 3 10 16 10 16 30 66 30 66 Values of N

This could break the threshold, so we would need to test it, but this should cover all the changes.

@jschueller

Copy link
Copy Markdown
Collaborator Author

ok, done

@jschueller
jschueller force-pushed the issue1312-src5 branch 3 times, most recently from f5efe37 to 1cf687b Compare October 28, 2026 10:52
@ACSimon33

Copy link
Copy Markdown
Collaborator

xGSVJ0 and xGSVJ1 are reached through the SVD tests. Not directly through the xGESVJ test, but through the xGEJSV test, which calls xGESVJ on triangular factors.

I think these new unit tests for a one-line change are overkill when we can use the standard testing framework. I'd just increase the 40x40 case in the input file to 65x65.

@jschueller

Copy link
Copy Markdown
Collaborator Author

ok

@ACSimon33

Copy link
Copy Markdown
Collaborator

Ah, sorry, the thresholds are failing on some machines now. I didn't test them all beforehand. Let me check if we can adjust the threshold or if it's too high

xGSVJ0 is only reached through xGESVJ's triangular blocked path for
N > 64, which the SVD tests (JOBA='G') never exercise. Add
per-precision drivers calling xGESVJ with N=66 'L' and N=65 'U'
inputs to drive the blocked path, validating the live factorization
directly (residual and orthogonality) without duplicating the EMPTSW
expression.

A shared svd.in size bump was tried instead (40->65/66) but rejected:
it perturbs unrelated bidiagonal accuracy checks in a
platform-dependent way (N=66: CBD test(10)=6400; N=65: SBD test(9)
~40 locally, over threshold on arm64/newer gfortran), failing CI.

Part of Reference-LAPACK#1312
@jschueller

Copy link
Copy Markdown
Collaborator Author

it passed, what did you change ?

@ACSimon33

ACSimon33 commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator

it passed, what did you change ?

I changed nothing. You reverted the change back to your new tests, no? It's fine by me. We can merge this for now and have a look at the threshold and testing framework at some other time.

@jschueller

Copy link
Copy Markdown
Collaborator Author

ok, lets go, do you approve ?

@jschueller
jschueller merged commit 119af65 into Reference-LAPACK:master Oct 29, 2026
47 checks passed
@jschueller
jschueller deleted the issue1312-src5 branch October 29, 2026 19:50
@jschueller

Copy link
Copy Markdown
Collaborator Author

thank you both for the feedback

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.

3 participants