-
Notifications
You must be signed in to change notification settings - Fork 12
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
Simple diff for 'Feat/Opcode Circuits for DIV, REM, and REMU opcodes' #734
base: matthias/bg/div-rem-remu-opcodes-base
Are you sure you want to change the base?
Simple diff for 'Feat/Opcode Circuits for DIV, REM, and REMU opcodes' #734
Conversation
…/div-rem-remu-opcodes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a few comments. It's too long to review in one go.
dividend_signed.assign_instance(instance, lkm, ÷nd_v)?; | ||
divisor_signed.assign_instance(instance, lkm, &divisor_v)?; | ||
|
||
let negative_division_b = (dividend_s < 0) ^ (divisor_s < 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does the _b
suffix stand for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Meant as bool
or bit
as you prefer -- should I revise the names or add a comment to make this more clear?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a _bit
suffix would be my preferred choice here, as it is still short but leads to less head scratching than just a b.
Thanks.
quotient_signed.assign_instance(instance, lkm, "ient_v)?; | ||
remainder_signed.assign_instance(instance, lkm, &remainder_v)?; | ||
|
||
let remainder_pos_orientation = if negative_division_b { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
diff --git a/ceno_zkvm/src/instructions/riscv/div.rs b/ceno_zkvm/src/instructions/riscv/div.rs
index 4ed86da4..4d141149 100644
--- a/ceno_zkvm/src/instructions/riscv/div.rs
+++ b/ceno_zkvm/src/instructions/riscv/div.rs
@@ -436,16 +436,10 @@ impl<E: ExtensionField, I: RIVInstruction> Instruction<E> for ArithInstruction<E
quotient_signed.assign_instance(instance, lkm, "ient_v)?;
remainder_signed.assign_instance(instance, lkm, &remainder_v)?;
- let remainder_pos_orientation = if negative_division_b {
- -(remainder_s as i64)
- } else {
- remainder_s as i64
- };
- let divisor_pos_orientation = if divisor_s < 0 {
- -(divisor_s as i64)
- } else {
- divisor_s as i64
- };
+ let neg_if = |b: bool, x: i32| if b { -x } else { x };
+
+ let remainder_pos_orientation = neg_if(negative_division_b, remainder_s) as i64;
+ let divisor_pos_orientation = neg_if(divisor_s < 0, divisor_s) as i64;
remainder_nonnegative.assign_instance(
instance,
This is a bit more compact.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, we can write it this way -- note that it's important here that the cast to i64
happens before negation, as one of the issues we run into is the edge case of negating the signed value -2^31
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for thinking about the special case!
I was more thinking about replacing the twice repeated relatively long logic with something shorter to read:
Rust makes it easy to define small helper functions, so we might as well use them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, wasn't much left below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All good in this file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See matthias/bg/div-rem-remu-opcodes-suggestions for more suggestions. (Though most of them are already in the review, too.)
Thanks Matthias! Gave a look in |
This is just a simpler view of the diff for https://github.com/scroll-tech/ceno/pull/596/files