Skip to content

Fix float-to-integer conversion for wide destination types#8986

Open
tautschnig wants to merge 1 commit into
diffblue:developfrom
tautschnig:fix-float-to-integer
Open

Fix float-to-integer conversion for wide destination types#8986
tautschnig wants to merge 1 commit into
diffblue:developfrom
tautschnig:fix-float-to-integer

Conversation

@tautschnig
Copy link
Copy Markdown
Collaborator

float_bvt::to_integer did not extend the fraction to dest_width before shifting, causing incorrect results when dest_width exceeds the fraction width (the shift distance went negative for large exponents).

  • Each commit message has a non-empty body, explaining why the change was made.
  • n/a Methods or procedures I have added are documented, following the guidelines provided in CODING_STANDARD.md.
  • n/a The feature or user visible behaviour I have added or modified has been documented in the User Guide in doc/cprover-manual/
  • Regression or unit tests are included, or existing tests cover the modified code (in this case I have detailed which ones those are in the commit message).
  • n/a My commit message includes data points confirming performance improvements (if claimed).
  • My PR is restricted to a single feature or bugfix.
  • n/a White-space or formatting changes outside the feature-related changed lines are in commits of their own.

@tautschnig tautschnig self-assigned this Apr 28, 2026
Copilot AI review requested due to automatic review settings April 28, 2026 11:30
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Fixes incorrect float-to-integer conversion when the destination integer width exceeds the float fraction width by extending the fraction before shifting and adding a regression test.

Changes:

  • Extend unpacked.fraction to dest_width (when needed) before computing the shift for integer conversion.
  • Adjust the shift offset computation to use the effective fraction width.
  • Add regression tests covering wide float-to-int conversions (bitvector + SMT paths).

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
src/solvers/floatbv/float_bv.cpp Extends fraction width before shifting to avoid negative/incorrect shift distances for wide integer targets.
regression/cbmc/Float-to-int-wide/main.c Adds a regression program exercising float-to-(wide)int conversions.
regression/cbmc/Float-to-int-wide/test.desc Adds a FloatBV regression test harness entry.
regression/cbmc/Float-to-int-wide/test_smt.desc Adds an SMT regression test harness entry for the same scenario.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/solvers/floatbv/float_bv.cpp
Comment thread regression/cbmc/Float-to-int-wide/main.c Outdated
Comment thread regression/cbmc/Float-to-int-wide/main.c Outdated
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 28, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.62%. Comparing base (01ebe42) to head (e14b54d).

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #8986      +/-   ##
===========================================
+ Coverage    80.59%   80.62%   +0.02%     
===========================================
  Files         1711     1711              
  Lines       189454   189464      +10     
  Branches        73       73              
===========================================
+ Hits        152697   152746      +49     
+ Misses       36757    36718      -39     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

float_bvt::to_integer did not extend the fraction to dest_width
before shifting, causing incorrect results when dest_width exceeds
the fraction width (the shift distance went negative for large
exponents). The fix mirrors the extend-then-shift pattern already
used by float_utilst::to_integer.

The PR carries over a latent constraint shared between both encodings:
the shift distance is built in a bit-vector of width spec.e, so
`dest_width - 1` must be representable in that signed/unsigned
bit width. For the integer types C currently supports this holds —
float (spec.e = 8) supports dest_width up to 128 (i.e. up to
__int128); double (spec.e = 11) supports dest_width up to 1024.
That assumption is now an explicit PRECONDITION on both
float_bvt::to_integer and float_utilst::to_integer, with matching
'keep in sync' comments. Widening the subtraction type to support
even wider integer destinations (and pulling the integer-conversion
logic out into a single helper shared between the two IR layers)
is a sensible follow-up.

Regression test exercises the bug across the parameter space: int
(at and above 2^24), unsigned int (including a 2^31 case where
unsigned-vs-signed semantics matter), long long from float and
double sources (the 64-bit destination cases that were the most
common practical breakage), and __int128 from both float and
double sources where supported. Without the fix, eight of these
eleven assertions fail on develop.

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
@tautschnig tautschnig force-pushed the fix-float-to-integer branch from 1195e13 to e14b54d Compare May 29, 2026 16:00
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