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

Copilot AI left a comment

Copy link
Copy Markdown

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 Outdated
Comment thread regression/cbmc/Float-to-int-wide/main.c Outdated
Comment thread regression/cbmc/Float-to-int-wide/main.c Outdated
@codecov

codecov Bot commented Apr 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.61%. Comparing base (4ed6b6d) to head (6befe03).

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #8986      +/-   ##
===========================================
+ Coverage    80.59%   80.61%   +0.02%     
===========================================
  Files         1713     1713              
  Lines       189403   189417      +14     
  Branches        73       73              
===========================================
+ Hits        152647   152700      +53     
+ Misses       36756    36717      -39     

☔ View full report in Codecov by Harness.
📢 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.

Both encodings previously built the shift distance in a bit-vector of width
spec.e, so dest_width - 1 had to be representable there. That held for
float/double to the integer types C supports, but not for a narrow source
such as _Float16 (spec.e = 5) converted to a 64-bit or wider integer, where
the distance silently wrapped (and, before this change, was guarded by a
PRECONDITION that turned the legal conversion into an invariant failure). The
shift distance is now built in a type wide enough to hold both
effective_width - 1 and the sign-extended exponent, i.e.
max(spec.e, address_bits(effective_width) + 1), so narrow-source/wide-
destination conversions are handled correctly in both encoders. Matching
'keep in sync' comments are retained.

The float_bvt fix affects the SMT back-end, which lowers float operations
through float_bvt; test_smt.desc (--z3) is therefore the descriptor that pins
it -- eight of the float/double assertions fail there on the unfixed encoder.
The SAT path (test.desc, --floatbv) exercises float_utilst, which already
extended the fraction.

The regression test exercises the parameter space: int (at and above 2^24),
unsigned int (including a 2^31 case where unsigned-vs-signed semantics
matter), long long and __int128 from float and double sources, and _Float16
to int / long long / __int128 -- the narrow-source case the distance widening
repairs (a fixed-width distance gets the 64-bit-and-wider destinations wrong).

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
@tautschnig tautschnig force-pushed the fix-float-to-integer branch from 065d7dc to 6befe03 Compare June 11, 2026 11:34
@tautschnig tautschnig assigned kroening and martin-cs and unassigned tautschnig Jun 11, 2026
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.

4 participants