Skip to content

Add saturating addition/subtraction #6647

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

Merged

Conversation

tautschnig
Copy link
Collaborator

Rust natively supports saturating arithmetic. For C code, it takes MMX
instructions to have access to this, and even then it is limited to
addition and subtraction. The implementation now is equally restricted
to these two arithmetic operations.

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

@tautschnig tautschnig self-assigned this Feb 7, 2022
@tautschnig tautschnig force-pushed the feature/saturating-arithmetic branch 2 times, most recently from 95f7c8d to 22ae750 Compare February 7, 2022 17:30
@codecov
Copy link

codecov bot commented Feb 7, 2022

Codecov Report

Merging #6647 (22defef) into develop (4c2c27d) will increase coverage by 0.00%.
The diff coverage is 80.35%.

❗ Current head 22defef differs from pull request most recent head 10ea245. Consider uploading reports for the commit 10ea245 to get more accurate results

Impacted file tree graph

@@            Coverage Diff            @@
##           develop    #6647    +/-   ##
=========================================
  Coverage    76.73%   76.74%            
=========================================
  Files         1579     1579            
  Lines       182008   182173   +165     
=========================================
+ Hits        139671   139803   +132     
- Misses       42337    42370    +33     
Impacted Files Coverage Δ
src/ansi-c/c_typecheck_base.h 100.00% <ø> (ø)
src/ansi-c/expr2c.cpp 65.94% <ø> (ø)
src/solvers/flattening/boolbv.h 62.50% <ø> (ø)
src/solvers/flattening/bv_utils.h 85.71% <ø> (ø)
src/solvers/flattening/boolbv_add_sub.cpp 48.33% <43.75%> (-3.06%) ⬇️
src/ansi-c/c_typecheck_expr.cpp 76.59% <83.87%> (+0.09%) ⬆️
src/solvers/flattening/bv_utils.cpp 79.61% <96.87%> (+1.14%) ⬆️
src/goto-programs/remove_vector.cpp 97.00% <100.00%> (+0.31%) ⬆️
src/solvers/flattening/boolbv.cpp 80.61% <100.00%> (+0.10%) ⬆️
src/util/bitvector_expr.h 93.47% <100.00%> (+0.47%) ⬆️
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 57c59fc...10ea245. Read the comment docs.

@tautschnig tautschnig force-pushed the feature/saturating-arithmetic branch from 22ae750 to 98f43f7 Compare February 7, 2022 20:57
@tautschnig tautschnig marked this pull request as ready for review February 7, 2022 20:57
@tautschnig tautschnig assigned kroening and tautschnig and unassigned tautschnig Feb 7, 2022
@tautschnig tautschnig force-pushed the feature/saturating-arithmetic branch from 98f43f7 to c6c6938 Compare February 8, 2022 10:55
@tautschnig tautschnig removed their assignment Feb 8, 2022
@tautschnig tautschnig force-pushed the feature/saturating-arithmetic branch 2 times, most recently from ed29bfa to 518af1c Compare February 8, 2022 14:22
@tautschnig tautschnig self-assigned this Feb 8, 2022
Copy link

@chris-ryder chris-ryder left a comment

Choose a reason for hiding this comment

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

Assuming I've followed all the bit operations correctly, I think that looks reasonable to me. As an aside, the phrase "For C code, it takes MMX instructions to have access to this" of course only applies to x86/amd64. ARM of course has it's own instructions and compiler intrinsics for saturated arithmetic.

@@ -229,6 +229,8 @@ bvt boolbvt::convert_bitvector(const exprt &expr)
}
else if(expr.id() == ID_bitreverse)
return convert_bitreverse(to_bitreverse_expr(expr));
else if(expr.id() == ID_saturating_minus || expr.id() == ID_saturating_plus)
return convert_saturating_add_sub(to_binary_expr(expr));

Choose a reason for hiding this comment

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

Minor nit pick... there's a mis-match of terminology here - plus vs add, minus vs sub - should we pick one and settle?

@tautschnig tautschnig force-pushed the feature/saturating-arithmetic branch 4 times, most recently from 50a4004 to 22defef Compare February 9, 2022 19:16
Rust natively supports saturating arithmetic. For C code, it takes MMX
instructions to have access to this (which will be implemented in the next
commit), and even then it is limited to addition and subtraction. The
implementation now is equally restricted to these two arithmetic operations.

Fixes: diffblue#5841
With the newly added saturating addition/subtraction it is possible to
support MMX instructions performing saturating arithmetic over vectors.
@tautschnig tautschnig force-pushed the feature/saturating-arithmetic branch from 22defef to 10ea245 Compare February 9, 2022 19:30
@tautschnig tautschnig merged commit c99dcc8 into diffblue:develop Feb 9, 2022
@tautschnig tautschnig deleted the feature/saturating-arithmetic branch February 9, 2022 21:41
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