Skip to content

Commit

Permalink
Optimize comparator calculation function
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul1365972 committed Jan 12, 2024
1 parent 55fc1a0 commit d123bf1
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions crates/core/src/redpiler/backend/direct/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,16 +302,16 @@ fn get_all_input(node: &Node) -> (u8, u8) {
(input_power, side_input_power)
}

// This function is optimized for input values from 0 to 15 and does not work correctly outside that range
fn calculate_comparator_output(mode: ComparatorMode, input_strength: u8, power_on_sides: u8) -> u8 {
match mode {
ComparatorMode::Compare => {
if input_strength >= power_on_sides {
input_strength
} else {
0
}
let difference = input_strength.wrapping_sub(power_on_sides);
if difference <= 15 {
match mode {
ComparatorMode::Compare => input_strength,
ComparatorMode::Subtract => difference,
}
ComparatorMode::Subtract => input_strength.saturating_sub(power_on_sides),
} else {
0
}
}

Expand Down

0 comments on commit d123bf1

Please sign in to comment.