Skip to content

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

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

Fix float-to-integer conversion for wide destination types#8986
tautschnig wants to merge 1 commit intodiffblue: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.

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).

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
@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 on lines +409 to 410
exprt offset = from_integer(effective_width - 1, signedbv_typet(spec.e));
const minus_exprt distance(offset, unpacked.exponent);
Comment on lines +48 to +54
__CPROVER_assume(e == 3000000000.0f); // 3 billion, fits in unsigned
unsigned int r5 = (unsigned int)e;
// 3000000000.0f is exactly 0x1.6bcc41p+31 = 3000000000 (exact)
// Actually 3e9 rounds to 3000000000 in float? Let's check:
// 3000000000 = 0xB2D05E00, float: 0x1.65a0bcp+31 = 2999999488
// Use an exact value instead.
__CPROVER_assume(e == 16777216.0f);
// needs the full 32-bit integer range.

#include <assert.h>
#include <limits.h>
@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.52%. Comparing base (a6531d4) to head (1195e13).

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #8986      +/-   ##
===========================================
+ Coverage    80.50%   80.52%   +0.02%     
===========================================
  Files         1704     1704              
  Lines       188796   188805       +9     
  Branches        73       73              
===========================================
+ Hits        151993   152044      +51     
+ Misses       36803    36761      -42     

☔ 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.

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