-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathripple_carry_subtractor.rs
65 lines (55 loc) · 2.3 KB
/
ripple_carry_subtractor.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use crate::model::binary_representation::BinaryRepresentation;
use std::cmp::max;
use crate::model::subtractor::half_subtractor::HalfSubtractor;
use crate::model::subtractor::full_subtractor::FullSubtractor;
// Declare RippleCarrySubtractor
pub struct RippleCarrySubtractor {
full_sub: FullSubtractor,
}
// Implement the previously declared RippleCarrySubtractor struct
impl RippleCarrySubtractor {
// Returns a new RippleCarrySubtractor object
pub fn new() -> RippleCarrySubtractor {
return RippleCarrySubtractor {
full_sub: FullSubtractor::new(),
};
}
// Calculates the sum of a and b
pub fn calc(&self, a: &BinaryRepresentation, b: &BinaryRepresentation) -> BinaryRepresentation {
// Set length to longest array length
let m_len = max(a.arr.len(), b.arr.len());
// Create a clone of a
let mut a_clone = BinaryRepresentation::from_vec((*a.arr).to_owned());
// Transform (pad with zeros) and reverse the clone
a_clone.transform(m_len);
a_clone.reverse();
// Create a clone of b
let mut b_clone = BinaryRepresentation::from_vec((*b.arr).to_owned());
// Transform (pad with zeros) and reverse the clone
b_clone.transform(m_len);
b_clone.reverse();
// Declare the vec that holds the result and the borrow bit that gets rippled along
let mut res: Vec<bool> = Vec::new();
let mut borrow = false;
// Loop
for i in 0..m_len {
// Try to fetch bit at index i
let bit_a = a_clone.arr.get(i)
.or(Some(&false))
.expect("Failed to fetch bool");
let bit_b = b_clone.arr.get(i)
.or(Some(&false))
.expect("Failed to fetch bool");
// Calculate result of the bits at index i, set the borrow bit to the calculated
// borrow bit and push the calculated diff to the result vec
let result = self.full_sub.calc(*bit_a, *bit_b, borrow);
borrow = result.borrow;
res.push(result.diff);
}
// Push the last borrow bit and reverse the result vec
res.push(borrow);
res.reverse();
// Wrap the result vec in a BinaryRepresentation
return BinaryRepresentation::from_vec(res);
}
}