From 84ffa12e2e5808febd780fca0e5522bbc2a726b8 Mon Sep 17 00:00:00 2001 From: svv232 Date: Sun, 1 Dec 2024 16:55:52 -0500 Subject: [PATCH 1/3] adding instruction for branch lt --- .../src/interpreters/riscv32im/interpreter.rs | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/o1vm/src/interpreters/riscv32im/interpreter.rs b/o1vm/src/interpreters/riscv32im/interpreter.rs index 0c9207c21e..ca486910cd 100644 --- a/o1vm/src/interpreters/riscv32im/interpreter.rs +++ b/o1vm/src/interpreters/riscv32im/interpreter.rs @@ -2267,7 +2267,33 @@ pub fn interpret_sbtype(env: &mut Env, instr: SBInstruction env.set_next_instruction_pointer(addr); } SBInstruction::BranchLessThan => { - unimplemented!("BranchLessThan") + // blt: if (x[rs1] < x[rs2]) pc += sext(offset) + let local_rs1 = env.read_register(&rs1); + let local_rs2 = env.read_register(&rs2); + + let rd_scratch = env.alloc_scratch(); + + let less_than = + unsafe { env.test_less_than_signed(&local_rs1, &local_rs2, rd_scratch) }; + let offset = (less_than.clone()) * imm0_12 + + (Env::constant(1) - less_than.clone()) * Env::constant(4); + + let addr = { + let res_scratch = env.alloc_scratch(); + let overflow_scratch = env.alloc_scratch(); + let (res, _overflow) = unsafe { + env.add_witness( + &next_instruction_pointer, + &offset, + res_scratch, + overflow_scratch, + ) + }; + // FIXME: Requires a range check + res + }; + env.set_instruction_pointer(next_instruction_pointer); + env.set_next_instruction_pointer(addr); } SBInstruction::BranchGreaterThanEqual => { unimplemented!("BranchGreaterThanEqual") From fa494c39ba0a252ae00c4d4a8ad9cb6b2eb076cf Mon Sep 17 00:00:00 2001 From: svv232 Date: Sun, 1 Dec 2024 16:56:13 -0500 Subject: [PATCH 2/3] lint and format --- .../src/interpreters/riscv32im/interpreter.rs | 54 +++++++++---------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/o1vm/src/interpreters/riscv32im/interpreter.rs b/o1vm/src/interpreters/riscv32im/interpreter.rs index ca486910cd..c5f8d2739d 100644 --- a/o1vm/src/interpreters/riscv32im/interpreter.rs +++ b/o1vm/src/interpreters/riscv32im/interpreter.rs @@ -2267,33 +2267,33 @@ pub fn interpret_sbtype(env: &mut Env, instr: SBInstruction env.set_next_instruction_pointer(addr); } SBInstruction::BranchLessThan => { - // blt: if (x[rs1] < x[rs2]) pc += sext(offset) - let local_rs1 = env.read_register(&rs1); - let local_rs2 = env.read_register(&rs2); - - let rd_scratch = env.alloc_scratch(); - - let less_than = - unsafe { env.test_less_than_signed(&local_rs1, &local_rs2, rd_scratch) }; - let offset = (less_than.clone()) * imm0_12 - + (Env::constant(1) - less_than.clone()) * Env::constant(4); - - let addr = { - let res_scratch = env.alloc_scratch(); - let overflow_scratch = env.alloc_scratch(); - let (res, _overflow) = unsafe { - env.add_witness( - &next_instruction_pointer, - &offset, - res_scratch, - overflow_scratch, - ) - }; - // FIXME: Requires a range check - res - }; - env.set_instruction_pointer(next_instruction_pointer); - env.set_next_instruction_pointer(addr); + // blt: if (x[rs1] < x[rs2]) pc += sext(offset) + let local_rs1 = env.read_register(&rs1); + let local_rs2 = env.read_register(&rs2); + + let rd_scratch = env.alloc_scratch(); + + let less_than = + unsafe { env.test_less_than_signed(&local_rs1, &local_rs2, rd_scratch) }; + let offset = (less_than.clone()) * imm0_12 + + (Env::constant(1) - less_than.clone()) * Env::constant(4); + + let addr = { + let res_scratch = env.alloc_scratch(); + let overflow_scratch = env.alloc_scratch(); + let (res, _overflow) = unsafe { + env.add_witness( + &next_instruction_pointer, + &offset, + res_scratch, + overflow_scratch, + ) + }; + // FIXME: Requires a range check + res + }; + env.set_instruction_pointer(next_instruction_pointer); + env.set_next_instruction_pointer(addr); } SBInstruction::BranchGreaterThanEqual => { unimplemented!("BranchGreaterThanEqual") From 7751170407b97b09f0ce65e6225eb66a315e329a Mon Sep 17 00:00:00 2001 From: svv232 Date: Mon, 23 Dec 2024 15:21:54 -0500 Subject: [PATCH 3/3] moving scratch into less-than-variable scope --- o1vm/src/interpreters/riscv32im/interpreter.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/o1vm/src/interpreters/riscv32im/interpreter.rs b/o1vm/src/interpreters/riscv32im/interpreter.rs index c5f8d2739d..1d98141a92 100644 --- a/o1vm/src/interpreters/riscv32im/interpreter.rs +++ b/o1vm/src/interpreters/riscv32im/interpreter.rs @@ -2271,10 +2271,10 @@ pub fn interpret_sbtype(env: &mut Env, instr: SBInstruction let local_rs1 = env.read_register(&rs1); let local_rs2 = env.read_register(&rs2); - let rd_scratch = env.alloc_scratch(); - - let less_than = - unsafe { env.test_less_than_signed(&local_rs1, &local_rs2, rd_scratch) }; + let less_than = { + let rd_scratch = env.alloc_scratch(); + unsafe { env.test_less_than_signed(&local_rs1, &local_rs2, rd_scratch) } + }; let offset = (less_than.clone()) * imm0_12 + (Env::constant(1) - less_than.clone()) * Env::constant(4);