use crate::constants::AUX_TRACE;
use crate::writer::Writer;
use crate::{constants::MAIN_TRACE, error::CodegenError};
use air_ir::{ConstraintDomain, ConstraintRoot, TraceSegmentId};
pub fn contraint_root_domain(boundary: &&ConstraintRoot) -> u8 {
match boundary.domain() {
ConstraintDomain::FirstRow => 0,
ConstraintDomain::LastRow => 1,
ConstraintDomain::EveryRow => panic!("EveryRow is not supported"),
ConstraintDomain::EveryFrame(_) => panic!("EveryFrame is not supported"),
}
}
pub fn periodic_group_to_memory_offset(group: u32) -> u32 {
group * 2 + 1
}
pub fn load_quadratic_element(
writer: &mut Writer,
base_addr: u32,
element: u32,
) -> Result<(), CodegenError> {
let target_word: u32 = element / 2;
let address = base_addr + target_word;
writer.padw();
writer.mem_loadw(address);
match element % 2 {
0 => {
writer.movdn(3);
writer.movdn(3);
writer.drop();
writer.drop();
}
1 => {
writer.drop();
writer.drop();
}
_ => unreachable!(),
}
Ok(())
}
pub fn quadratic_element_square(writer: &mut Writer, n: u32) {
for _ in 0..n {
writer.dup(1);
writer.dup(1);
writer.ext2mul();
}
}
pub fn boundary_group_to_procedure_name(
trace: TraceSegmentId,
domain: ConstraintDomain,
) -> &'static str {
match (trace, domain) {
(MAIN_TRACE, ConstraintDomain::FirstRow) => "compute_boundary_constraints_main_first",
(MAIN_TRACE, ConstraintDomain::LastRow) => "compute_boundary_constraints_main_last",
(AUX_TRACE, ConstraintDomain::FirstRow) => "compute_boundary_constraints_aux_first",
(AUX_TRACE, ConstraintDomain::LastRow) => "compute_boundary_constraints_aux_last",
_ => panic!("Invalid boundary constraint"),
}
}