You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've been doing some thinking about how we can perform "unsafe maths" to avoid all those nasty overflow checks and similar.
In general how this is being done is by converting to Field type, doing your operations and then converting back to the desired type. This runs into the trouble that converting back to the original type will automatically include an overflow check despite the developer knowing that it's unnecessary.
In the line (ret as u112 >> BITS as u112) as u56 we're performing 2 casts (BITS as u112 gets handled at compile time so doesn't count) which automatically includes 2 truncations despite the fact that we know that these casts are valid and will not cause an overflow in the target type. If we had an unsafe cast then we could simplify this down to a single division rather than it having 3.
In order to do this we need to remove the truncation logic from a cast so that doing x as u8 would be represented in SSA as two instructions (a truncation and then a cast). Cast instructions would then not be represented in the ACIR at all but only exist to change the types of values in SSA.
Once this is done we can add some form of unsafe_cast function to the stdlib which would emit just a cast instruction without the truncation.
The text was updated successfully, but these errors were encountered:
…struction (#3946)
# Description
## Problem\*
Resolves#3749
## Summary\*
This PR removes the implicit `Instruction::Truncate` which lives inside
`Instruction::Cast` so that it's purely responsible for casting the type
of the value in SSA rather than massaging the input to fit in the
desired type.
## Additional Context
## Documentation\*
Check one:
- [ ] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[Exceptional Case]** Documentation to be submitted in a separate
PR.
# PR Checklist\*
- [ ] I have tested the changes locally.
- [ ] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
---------
Co-authored-by: jfecher <[email protected]>
I've been doing some thinking about how we can perform "unsafe maths" to avoid all those nasty overflow checks and similar.
In general how this is being done is by converting to
Field
type, doing your operations and then converting back to the desired type. This runs into the trouble that converting back to the original type will automatically include an overflow check despite the developer knowing that it's unnecessary.As an example where this is useful, see: https://github.com/shuklaayush/noir-bigint/blob/9a876290c9f75058dc53e74d25da5b3d79ada0d8/crates/biguint/src/utils.nr#L6-L21
In the line
(ret as u112 >> BITS as u112) as u56
we're performing 2 casts (BITS as u112
gets handled at compile time so doesn't count) which automatically includes 2 truncations despite the fact that we know that these casts are valid and will not cause an overflow in the target type. If we had an unsafe cast then we could simplify this down to a single division rather than it having 3.In order to do this we need to remove the truncation logic from a cast so that doing
x as u8
would be represented in SSA as two instructions (a truncation and then a cast). Cast instructions would then not be represented in the ACIR at all but only exist to change the types of values in SSA.Once this is done we can add some form of
unsafe_cast
function to the stdlib which would emit just a cast instruction without the truncation.The text was updated successfully, but these errors were encountered: