Skip to content

Commit 312f6d5

Browse files
committed
refactor: use dispatch! for rv32 executors
1 parent 8d3d06e commit 312f6d5

File tree

9 files changed

+155
-292
lines changed

9 files changed

+155
-292
lines changed

extensions/rv32im/circuit/src/branch_eq/execution.rs

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ impl<A, const NUM_LIMBS: usize> BranchEqualExecutor<A, NUM_LIMBS> {
5353
}
5454
}
5555

56+
macro_rules! dispatch {
57+
($execute_impl:ident, $is_bne:ident) => {
58+
if $is_bne {
59+
Ok($execute_impl::<_, _, true>)
60+
} else {
61+
Ok($execute_impl::<_, _, false>)
62+
}
63+
};
64+
}
65+
5666
impl<F, A, const NUM_LIMBS: usize> Executor<F> for BranchEqualExecutor<A, NUM_LIMBS>
5767
where
5868
F: PrimeField32,
@@ -71,12 +81,7 @@ where
7181
) -> Result<ExecuteFunc<F, Ctx>, StaticProgramError> {
7282
let data: &mut BranchEqualPreCompute = data.borrow_mut();
7383
let is_bne = self.pre_compute_impl(pc, inst, data)?;
74-
let fn_ptr = if is_bne {
75-
execute_e1_impl::<_, _, true>
76-
} else {
77-
execute_e1_impl::<_, _, false>
78-
};
79-
Ok(fn_ptr)
84+
dispatch!(execute_e1_impl, is_bne)
8085
}
8186

8287
#[cfg(feature = "tco")]
@@ -91,12 +96,7 @@ where
9196
{
9297
let data: &mut BranchEqualPreCompute = data.borrow_mut();
9398
let is_bne = self.pre_compute_impl(pc, inst, data)?;
94-
let fn_ptr = if is_bne {
95-
execute_e1_tco_handler::<_, _, true>
96-
} else {
97-
execute_e1_tco_handler::<_, _, false>
98-
};
99-
Ok(fn_ptr)
99+
dispatch!(execute_e1_tco_handler, is_bne)
100100
}
101101
}
102102

@@ -121,12 +121,7 @@ where
121121
let data: &mut E2PreCompute<BranchEqualPreCompute> = data.borrow_mut();
122122
data.chip_idx = chip_idx as u32;
123123
let is_bne = self.pre_compute_impl(pc, inst, &mut data.data)?;
124-
let fn_ptr = if is_bne {
125-
execute_e2_impl::<_, _, true>
126-
} else {
127-
execute_e2_impl::<_, _, false>
128-
};
129-
Ok(fn_ptr)
124+
dispatch!(execute_e2_impl, is_bne)
130125
}
131126

132127
#[cfg(feature = "tco")]
@@ -143,12 +138,7 @@ where
143138
let data: &mut E2PreCompute<BranchEqualPreCompute> = data.borrow_mut();
144139
data.chip_idx = chip_idx as u32;
145140
let is_bne = self.pre_compute_impl(pc, inst, &mut data.data)?;
146-
let fn_ptr = if is_bne {
147-
execute_e2_tco_handler::<_, _, true>
148-
} else {
149-
execute_e2_tco_handler::<_, _, false>
150-
};
151-
Ok(fn_ptr)
141+
dispatch!(execute_e2_tco_handler, is_bne)
152142
}
153143
}
154144

extensions/rv32im/circuit/src/branch_lt/execution.rs

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ struct BranchLePreCompute {
2121
b: u8,
2222
}
2323

24+
macro_rules! dispatch {
25+
($execute_impl:ident, $local_opcode:ident) => {
26+
match $local_opcode {
27+
BranchLessThanOpcode::BLT => Ok($execute_impl::<_, _, BltOp>),
28+
BranchLessThanOpcode::BLTU => Ok($execute_impl::<_, _, BltuOp>),
29+
BranchLessThanOpcode::BGE => Ok($execute_impl::<_, _, BgeOp>),
30+
BranchLessThanOpcode::BGEU => Ok($execute_impl::<_, _, BgeuOp>),
31+
}
32+
};
33+
}
34+
2435
impl<A, const NUM_LIMBS: usize, const LIMB_BITS: usize>
2536
BranchLessThanExecutor<A, NUM_LIMBS, LIMB_BITS>
2637
{
@@ -72,13 +83,7 @@ where
7283
) -> Result<ExecuteFunc<F, Ctx>, StaticProgramError> {
7384
let data: &mut BranchLePreCompute = data.borrow_mut();
7485
let local_opcode = self.pre_compute_impl(pc, inst, data)?;
75-
let fn_ptr = match local_opcode {
76-
BranchLessThanOpcode::BLT => execute_e1_impl::<_, _, BltOp>,
77-
BranchLessThanOpcode::BLTU => execute_e1_impl::<_, _, BltuOp>,
78-
BranchLessThanOpcode::BGE => execute_e1_impl::<_, _, BgeOp>,
79-
BranchLessThanOpcode::BGEU => execute_e1_impl::<_, _, BgeuOp>,
80-
};
81-
Ok(fn_ptr)
86+
dispatch!(execute_e1_impl, local_opcode)
8287
}
8388

8489
#[cfg(feature = "tco")]
@@ -93,13 +98,7 @@ where
9398
{
9499
let data: &mut BranchLePreCompute = data.borrow_mut();
95100
let local_opcode = self.pre_compute_impl(pc, inst, data)?;
96-
let fn_ptr = match local_opcode {
97-
BranchLessThanOpcode::BLT => execute_e1_tco_handler::<_, _, BltOp>,
98-
BranchLessThanOpcode::BLTU => execute_e1_tco_handler::<_, _, BltuOp>,
99-
BranchLessThanOpcode::BGE => execute_e1_tco_handler::<_, _, BgeOp>,
100-
BranchLessThanOpcode::BGEU => execute_e1_tco_handler::<_, _, BgeuOp>,
101-
};
102-
Ok(fn_ptr)
101+
dispatch!(execute_e1_tco_handler, local_opcode)
103102
}
104103
}
105104

@@ -125,13 +124,7 @@ where
125124
let data: &mut E2PreCompute<BranchLePreCompute> = data.borrow_mut();
126125
data.chip_idx = chip_idx as u32;
127126
let local_opcode = self.pre_compute_impl(pc, inst, &mut data.data)?;
128-
let fn_ptr = match local_opcode {
129-
BranchLessThanOpcode::BLT => execute_e2_impl::<_, _, BltOp>,
130-
BranchLessThanOpcode::BLTU => execute_e2_impl::<_, _, BltuOp>,
131-
BranchLessThanOpcode::BGE => execute_e2_impl::<_, _, BgeOp>,
132-
BranchLessThanOpcode::BGEU => execute_e2_impl::<_, _, BgeuOp>,
133-
};
134-
Ok(fn_ptr)
127+
dispatch!(execute_e2_impl, local_opcode)
135128
}
136129

137130
#[cfg(feature = "tco")]
@@ -148,13 +141,7 @@ where
148141
let data: &mut E2PreCompute<BranchLePreCompute> = data.borrow_mut();
149142
data.chip_idx = chip_idx as u32;
150143
let local_opcode = self.pre_compute_impl(pc, inst, &mut data.data)?;
151-
let fn_ptr = match local_opcode {
152-
BranchLessThanOpcode::BLT => execute_e2_tco_handler::<_, _, BltOp>,
153-
BranchLessThanOpcode::BLTU => execute_e2_tco_handler::<_, _, BltuOp>,
154-
BranchLessThanOpcode::BGE => execute_e2_tco_handler::<_, _, BgeOp>,
155-
BranchLessThanOpcode::BGEU => execute_e2_tco_handler::<_, _, BgeuOp>,
156-
};
157-
Ok(fn_ptr)
144+
dispatch!(execute_e2_tco_handler, local_opcode)
158145
}
159146
}
160147

extensions/rv32im/circuit/src/hintstore/execution.rs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ impl Rv32HintStoreExecutor {
6060
}
6161
}
6262

63+
macro_rules! dispatch {
64+
($execute_impl:ident, $local_opcode:ident) => {
65+
match $local_opcode {
66+
HINT_STOREW => Ok($execute_impl::<_, _, true>),
67+
HINT_BUFFER => Ok($execute_impl::<_, _, false>),
68+
}
69+
};
70+
}
71+
6372
impl<F> Executor<F> for Rv32HintStoreExecutor
6473
where
6574
F: PrimeField32,
@@ -77,11 +86,7 @@ where
7786
) -> Result<ExecuteFunc<F, Ctx>, StaticProgramError> {
7887
let pre_compute: &mut HintStorePreCompute = data.borrow_mut();
7988
let local_opcode = self.pre_compute_impl(pc, inst, pre_compute)?;
80-
let fn_ptr = match local_opcode {
81-
HINT_STOREW => execute_e1_impl::<_, _, true>,
82-
HINT_BUFFER => execute_e1_impl::<_, _, false>,
83-
};
84-
Ok(fn_ptr)
89+
dispatch!(execute_e1_impl, local_opcode)
8590
}
8691

8792
#[cfg(feature = "tco")]
@@ -96,11 +101,7 @@ where
96101
{
97102
let pre_compute: &mut HintStorePreCompute = data.borrow_mut();
98103
let local_opcode = self.pre_compute_impl(pc, inst, pre_compute)?;
99-
let fn_ptr = match local_opcode {
100-
HINT_STOREW => execute_e1_tco_handler::<_, _, true>,
101-
HINT_BUFFER => execute_e1_tco_handler::<_, _, false>,
102-
};
103-
Ok(fn_ptr)
104+
dispatch!(execute_e1_tco_handler, local_opcode)
104105
}
105106
}
106107

@@ -125,11 +126,7 @@ where
125126
let pre_compute: &mut E2PreCompute<HintStorePreCompute> = data.borrow_mut();
126127
pre_compute.chip_idx = chip_idx as u32;
127128
let local_opcode = self.pre_compute_impl(pc, inst, &mut pre_compute.data)?;
128-
let fn_ptr = match local_opcode {
129-
HINT_STOREW => execute_e2_impl::<_, _, true>,
130-
HINT_BUFFER => execute_e2_impl::<_, _, false>,
131-
};
132-
Ok(fn_ptr)
129+
dispatch!(execute_e2_impl, local_opcode)
133130
}
134131

135132
#[cfg(feature = "tco")]
@@ -146,11 +143,7 @@ where
146143
let pre_compute: &mut E2PreCompute<HintStorePreCompute> = data.borrow_mut();
147144
pre_compute.chip_idx = chip_idx as u32;
148145
let local_opcode = self.pre_compute_impl(pc, inst, &mut pre_compute.data)?;
149-
let fn_ptr = match local_opcode {
150-
HINT_STOREW => execute_e2_tco_handler::<_, _, true>,
151-
HINT_BUFFER => execute_e2_tco_handler::<_, _, false>,
152-
};
153-
Ok(fn_ptr)
146+
dispatch!(execute_e2_tco_handler, local_opcode)
154147
}
155148
}
156149

extensions/rv32im/circuit/src/jal_lui/execution.rs

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ impl<A> Rv32JalLuiExecutor<A> {
4343
}
4444
}
4545

46+
macro_rules! dispatch {
47+
($execute_impl:ident, $is_jal:ident, $enabled:ident) => {
48+
match ($is_jal, $enabled) {
49+
(true, true) => Ok($execute_impl::<_, _, true, true>),
50+
(true, false) => Ok($execute_impl::<_, _, true, false>),
51+
(false, true) => Ok($execute_impl::<_, _, false, true>),
52+
(false, false) => Ok($execute_impl::<_, _, false, false>),
53+
}
54+
};
55+
}
56+
4657
impl<F, A> Executor<F> for Rv32JalLuiExecutor<A>
4758
where
4859
F: PrimeField32,
@@ -60,13 +71,7 @@ where
6071
) -> Result<ExecuteFunc<F, Ctx>, StaticProgramError> {
6172
let data: &mut JalLuiPreCompute = data.borrow_mut();
6273
let (is_jal, enabled) = self.pre_compute_impl(inst, data)?;
63-
let fn_ptr = match (is_jal, enabled) {
64-
(true, true) => execute_e1_impl::<_, _, true, true>,
65-
(true, false) => execute_e1_impl::<_, _, true, false>,
66-
(false, true) => execute_e1_impl::<_, _, false, true>,
67-
(false, false) => execute_e1_impl::<_, _, false, false>,
68-
};
69-
Ok(fn_ptr)
74+
dispatch!(execute_e1_impl, is_jal, enabled)
7075
}
7176

7277
#[cfg(feature = "tco")]
@@ -81,13 +86,7 @@ where
8186
{
8287
let data: &mut JalLuiPreCompute = data.borrow_mut();
8388
let (is_jal, enabled) = self.pre_compute_impl(inst, data)?;
84-
let fn_ptr = match (is_jal, enabled) {
85-
(true, true) => execute_e1_tco_handler::<_, _, true, true>,
86-
(true, false) => execute_e1_tco_handler::<_, _, true, false>,
87-
(false, true) => execute_e1_tco_handler::<_, _, false, true>,
88-
(false, false) => execute_e1_tco_handler::<_, _, false, false>,
89-
};
90-
Ok(fn_ptr)
89+
dispatch!(execute_e1_tco_handler, is_jal, enabled)
9190
}
9291
}
9392

@@ -112,13 +111,7 @@ where
112111
let data: &mut E2PreCompute<JalLuiPreCompute> = data.borrow_mut();
113112
data.chip_idx = chip_idx as u32;
114113
let (is_jal, enabled) = self.pre_compute_impl(inst, &mut data.data)?;
115-
let fn_ptr = match (is_jal, enabled) {
116-
(true, true) => execute_e2_impl::<_, _, true, true>,
117-
(true, false) => execute_e2_impl::<_, _, true, false>,
118-
(false, true) => execute_e2_impl::<_, _, false, true>,
119-
(false, false) => execute_e2_impl::<_, _, false, false>,
120-
};
121-
Ok(fn_ptr)
114+
dispatch!(execute_e2_impl, is_jal, enabled)
122115
}
123116

124117
#[cfg(feature = "tco")]
@@ -135,13 +128,7 @@ where
135128
let data: &mut E2PreCompute<JalLuiPreCompute> = data.borrow_mut();
136129
data.chip_idx = chip_idx as u32;
137130
let (is_jal, enabled) = self.pre_compute_impl(inst, &mut data.data)?;
138-
let fn_ptr = match (is_jal, enabled) {
139-
(true, true) => execute_e2_tco_handler::<_, _, true, true>,
140-
(true, false) => execute_e2_tco_handler::<_, _, true, false>,
141-
(false, true) => execute_e2_tco_handler::<_, _, false, true>,
142-
(false, false) => execute_e2_tco_handler::<_, _, false, false>,
143-
};
144-
Ok(fn_ptr)
131+
dispatch!(execute_e2_tco_handler, is_jal, enabled)
145132
}
146133
}
147134

extensions/rv32im/circuit/src/jalr/execution.rs

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ impl<A> Rv32JalrExecutor<A> {
4444
}
4545
}
4646

47+
macro_rules! dispatch {
48+
($execute_impl:ident, $enabled:ident) => {
49+
if $enabled {
50+
Ok($execute_impl::<_, _, true>)
51+
} else {
52+
Ok($execute_impl::<_, _, false>)
53+
}
54+
};
55+
}
56+
4757
impl<F, A> Executor<F> for Rv32JalrExecutor<A>
4858
where
4959
F: PrimeField32,
@@ -61,12 +71,7 @@ where
6171
) -> Result<ExecuteFunc<F, Ctx>, StaticProgramError> {
6272
let data: &mut JalrPreCompute = data.borrow_mut();
6373
let enabled = self.pre_compute_impl(pc, inst, data)?;
64-
let fn_ptr = if enabled {
65-
execute_e1_impl::<_, _, true>
66-
} else {
67-
execute_e1_impl::<_, _, false>
68-
};
69-
Ok(fn_ptr)
74+
dispatch!(execute_e1_impl, enabled)
7075
}
7176

7277
#[cfg(feature = "tco")]
@@ -81,12 +86,7 @@ where
8186
{
8287
let data: &mut JalrPreCompute = data.borrow_mut();
8388
let enabled = self.pre_compute_impl(pc, inst, data)?;
84-
let fn_ptr = if enabled {
85-
execute_e1_tco_handler::<_, _, true>
86-
} else {
87-
execute_e1_tco_handler::<_, _, false>
88-
};
89-
Ok(fn_ptr)
89+
dispatch!(execute_e1_tco_handler, enabled)
9090
}
9191
}
9292

@@ -111,12 +111,7 @@ where
111111
let data: &mut E2PreCompute<JalrPreCompute> = data.borrow_mut();
112112
data.chip_idx = chip_idx as u32;
113113
let enabled = self.pre_compute_impl(pc, inst, &mut data.data)?;
114-
let fn_ptr = if enabled {
115-
execute_e2_impl::<_, _, true>
116-
} else {
117-
execute_e2_impl::<_, _, false>
118-
};
119-
Ok(fn_ptr)
114+
dispatch!(execute_e2_impl, enabled)
120115
}
121116

122117
#[cfg(feature = "tco")]
@@ -133,12 +128,7 @@ where
133128
let data: &mut E2PreCompute<JalrPreCompute> = data.borrow_mut();
134129
data.chip_idx = chip_idx as u32;
135130
let enabled = self.pre_compute_impl(pc, inst, &mut data.data)?;
136-
let fn_ptr = if enabled {
137-
execute_e2_tco_handler::<_, _, true>
138-
} else {
139-
execute_e2_tco_handler::<_, _, false>
140-
};
141-
Ok(fn_ptr)
131+
dispatch!(execute_e2_tco_handler, enabled)
142132
}
143133
}
144134

0 commit comments

Comments
 (0)