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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

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 88.97638% with 28 lines in your changes missing coverage. Please review.

Project coverage is 78.94%. Comparing base (af4e2e4) to head (8b80200).

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 82.14% 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.50% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #8538      +/-   ##
===========================================
+ Coverage    78.92%   78.94%   +0.01%     
===========================================
  Files         1732     1732              
  Lines       198958   199206     +248     
  Branches     18543    18609      +66     
===========================================
+ Hits        157037   157266     +229     
- Misses       41921    41940      +19     

☔ 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 +174

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

return v;
};

auto round_to_integral =
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we also test for some small and large values halfway between integers, previous below, next above, x (e.g. 0.5, 0.49999999, 0.500000001) and cross that with the different rounding modes so check that ties are actually resolved as expected?

'

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