Skip to content

Commit

Permalink
fix sign extension check on imm and function renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
hero78119 committed Oct 29, 2024
1 parent 6bcfbb0 commit 0d462d5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
21 changes: 12 additions & 9 deletions ceno_emul/src/rv32im.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ impl DecodedInstruction {
}

/// Indicate whether the immediate is interpreted as a signed integer, and it is negative.
pub fn imm_is_negative(&self) -> bool {
pub fn imm_field_is_negative(&self) -> bool {
match self.codes() {
InsnCodes { format: R | U, .. } => false,
InsnCodes {
Expand All @@ -323,27 +323,30 @@ impl DecodedInstruction {
(i.category, i.kind)
}

pub fn imm12_sign_ext(&self) -> u32 {
self.imm_is_negative() as u32 * 0xfffff000
}

pub fn imm_b(&self) -> u32 {
self.imm12_sign_ext()
(self.top_bit * 0xfffff000)
| ((self.rd & 1) << 11)
| ((self.func7 & 0x3f) << 5)
| (self.rd & 0x1e)
}

/// checks if the imm requires sign extension
pub fn requires_sign_ext(&self) -> bool {
!matches!(self.codes(), InsnCodes { kind: SLTIU, .. })
}

pub fn imm_i(&self) -> u32 {
self.imm12_sign_ext() | (self.func7 << 5) | self.rs2
((self.requires_sign_ext() as u32) * self.top_bit * 0xfffff000)
| (self.func7 << 5)
| self.rs2
}

pub fn imm_s(&self) -> u32 {
self.imm12_sign_ext() | (self.func7 << 5) | self.rd
(self.top_bit * 0xfffff000) | (self.func7 << 5) | self.rd
}

pub fn imm_j(&self) -> u32 {
self.imm12_sign_ext()
(self.top_bit * 0xfff00000)
| (self.rs1 << 15)
| (self.func3 << 12)
| ((self.rs2 & 1) << 11)
Expand Down
2 changes: 1 addition & 1 deletion ceno_zkvm/src/tables/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl InsnRecord<u32> {
/// Interpret the immediate or funct7 as unsigned or signed depending on the instruction.
/// Convert negative values from two's complement to field.
pub fn imm_or_funct7_field<F: SmallField>(insn: &DecodedInstruction) -> F {
if insn.imm_is_negative() {
if insn.imm_field_is_negative() {
-F::from(-(insn.imm_or_funct7() as i32) as u64)
} else {
F::from(insn.imm_or_funct7() as u64)
Expand Down

0 comments on commit 0d462d5

Please sign in to comment.