openvm_rv32im_circuit/adapters/
mod.rsuse std::ops::Mul;
use openvm_circuit::system::memory::{MemoryController, MemoryReadRecord};
use openvm_stark_backend::p3_field::{AbstractField, PrimeField32};
mod alu;
mod branch;
mod hintstore;
mod jalr;
mod loadstore;
mod mul;
mod rdwrite;
pub use alu::*;
pub use branch::*;
pub use hintstore::*;
pub use jalr::*;
pub use loadstore::*;
pub use mul::*;
pub use openvm_instructions::riscv::{RV32_CELL_BITS, RV32_REGISTER_NUM_LIMBS};
pub use rdwrite::*;
pub const INT256_NUM_LIMBS: usize = 32;
pub const RV_IS_TYPE_IMM_BITS: usize = 12;
pub const RV_B_TYPE_IMM_BITS: usize = 13;
pub const RV_J_TYPE_IMM_BITS: usize = 21;
pub fn compose<F: PrimeField32>(ptr_data: [F; RV32_REGISTER_NUM_LIMBS]) -> u32 {
let mut val = 0;
for (i, limb) in ptr_data.map(|x| x.as_canonical_u32()).iter().enumerate() {
val += limb << (i * 8);
}
val
}
pub fn read_rv32_register<F: PrimeField32>(
memory: &mut MemoryController<F>,
address_space: F,
pointer: F,
) -> (MemoryReadRecord<F, RV32_REGISTER_NUM_LIMBS>, u32) {
debug_assert_eq!(address_space, F::ONE);
let record = memory.read::<RV32_REGISTER_NUM_LIMBS>(address_space, pointer);
let val = compose(record.data);
(record, val)
}
pub fn unsafe_read_rv32_register<F: PrimeField32>(memory: &MemoryController<F>, pointer: F) -> u32 {
let data = memory.unsafe_read::<RV32_REGISTER_NUM_LIMBS>(F::ONE, pointer);
compose(data)
}
pub fn abstract_compose<T: AbstractField, V: Mul<T, Output = T>>(
data: [V; RV32_REGISTER_NUM_LIMBS],
) -> T {
data.into_iter()
.enumerate()
.fold(T::ZERO, |acc, (i, limb)| {
acc + limb * T::from_canonical_u32(1 << (i * RV32_CELL_BITS))
})
}