Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce floatbv_round_to_integral_exprt #8538

Merged
merged 1 commit into from
Feb 15, 2025
Merged

Conversation

kroening
Copy link
Member

@kroening kroening commented Dec 21, 2024

This adds a new expression, floatbv_round_to_integral, which rounds an IEEE 754 floating-point number given as bit-vector to the nearest integer, considering the explicitly given rounding mode.

  • Each commit message has a non-empty body, explaining why the change was made.
  • 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.

@kroening kroening force-pushed the round_to_integral branch 2 times, most recently from 84b83ec to b52d32e Compare December 21, 2024 14:45
@kroening kroening changed the title Introduce round_to_integral_exprt Introduce floatbv_round_to_integral_exprt Dec 21, 2024
@kroening kroening force-pushed the round_to_integral branch 9 times, most recently from 9c6589a to e189f1c Compare December 21, 2024 21:49
@kroening kroening marked this pull request as ready for review December 21, 2024 22:07
Copy link

codecov bot commented Dec 21, 2024

Codecov Report

Attention: Patch coverage is 91.16719% with 28 lines in your changes missing coverage. Please review.

Project coverage is 79.63%. Comparing base (b712143) to head (49850ea).
Report is 2 commits behind head on develop.

Files with missing lines Patch % Lines
src/solvers/smt2/smt2_conv.cpp 8.33% 11 Missing ⚠️
src/solvers/smt2/smt2_parser.cpp 12.50% 7 Missing ⚠️
src/util/ieee_float.cpp 85.29% 5 Missing ⚠️
src/ansi-c/c_typecheck_expr.cpp 75.00% 3 Missing ⚠️
src/solvers/flattening/boolbv_floatbv_op.cpp 87.50% 1 Missing ⚠️
unit/util/ieee_float.cpp 98.94% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #8538      +/-   ##
===========================================
+ Coverage    79.60%   79.63%   +0.02%     
===========================================
  Files         1733     1733              
  Lines       197377   197686     +309     
  Branches     18169    18226      +57     
===========================================
+ Hits        157118   157418     +300     
- Misses       40259    40268       +9     

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

@kroening kroening force-pushed the round_to_integral branch 4 times, most recently from f59ad70 to 151d627 Compare January 2, 2025 13:28
src/ansi-c/library/math.c Show resolved Hide resolved
@@ -136,6 +136,32 @@ bvt float_utilst::to_integer(
return result;
}

bvt float_utilst::round_to_integral(const bvt &src)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Where does this actually consider the rounding mode?

Copy link
Member Author

Choose a reason for hiding this comment

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

It calls add_sub.

Comment on lines +154 to +177

auto tmp1 = add_sub(src, magic_number_bv, false);

auto tmp2 = add_sub(tmp1, magic_number_bv, true);
Copy link
Collaborator

Choose a reason for hiding this comment

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

If ieee_floatt::round_to_integral is to be trusted, shouldn't the sign bit be used instead of hard-coding false and true?

Copy link
Member Author

Choose a reason for hiding this comment

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

Note line 153 -- this variant tweaks the sign bit of the magic constant instead.

@kroening kroening force-pushed the round_to_integral branch 2 times, most recently from ec1cfc6 to 643372e Compare January 9, 2025 23:10
@kroening kroening force-pushed the round_to_integral branch 4 times, most recently from b6b25aa to ddeedbc Compare January 10, 2025 14:47
Copy link
Collaborator

@remi-delmas-3000 remi-delmas-3000 left a comment

Choose a reason for hiding this comment

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

looks good but could use some comments to explain how fractional bits get cleared due to the exponent alignment with the magic constant, and tests for halfway values and different tie breaking modes

unit/util/ieee_float.cpp Show resolved Hide resolved
@kroening kroening force-pushed the round_to_integral branch 5 times, most recently from 90018fc to 5e40ab0 Compare February 11, 2025 16:15
@kroening kroening force-pushed the round_to_integral branch 2 times, most recently from 7f1be0e to a6d1ba4 Compare February 11, 2025 19:28

magic_number_bv.back() = src.back(); // copy sign bit

auto tmp1 = add_sub(src, magic_number_bv, false);
Copy link
Member

Choose a reason for hiding this comment

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

Can this overflow when the number of exponent bits is small?

Copy link
Member Author

Choose a reason for hiding this comment

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

In that case, ge_magic_number will be true.

This adds a new expression, floatbv_round_to_integral, which rounds an IEEE
754 floating-point number given as bit-vector to the nearest integer,
considering the explicitly given rounding mode.
@kroening kroening enabled auto-merge February 15, 2025 17:32
@kroening kroening merged commit 0fc8a49 into develop Feb 15, 2025
38 of 39 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants