-
Notifications
You must be signed in to change notification settings - Fork 334
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
implement saturating arithmetic #587
Comments
@mbudiu-vmw I have been thinking about this a little bit. Because of the arithmetic engine of bmv2, which operates over arbitrary integers, it looks like I will need to introduce 2 new operations: an unsigned saturating cast ("usat_cast") and a signed saturating cast ("sat_cast"). In each case, the operands will be the expression to cast and the bitwidth of the type. For example: If
If
For the signed case, let's assume Note that when In pseudo-code:
BTW, I thought about having a single saturating cast operation that would take the boundaries (min and max) instead of the bitwidth, but most operations in bmv2 have 2 operands and not 3. Let me know what you think and if you think there would be any issues in the compiler side with this solution. |
This looks pretty good. |
Are you using the multiprecision library? What do you do when the width is the same as the native type you use for computing E and the native type overflows (or wraps around)? |
bmv2 never uses C++ native types for operations. |
+1 |
P4 specification: http://github.com/p4lang/p4-spec/pull/609
Reference implementation proposal: https://github.com/p4lang/p4-spec/wiki/saturating#reference-implementation
The text was updated successfully, but these errors were encountered: