-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathripple_carry_adder.rs
65 lines (55 loc) · 2.22 KB
/
ripple_carry_adder.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 crate::model::adder::half_adder::HalfAdder;
use crate::model::adder::full_adder::FullAdder;
use std::cmp::max;
// Declare RippleCarryAdder
pub struct RippleCarryAdder {
full_adder: FullAdder,
}
// Implement the previously declared RippleCarryAdder struct
impl RippleCarryAdder {
// Returns a new RippleCarryAdder object
pub fn new() -> RippleCarryAdder {
return RippleCarryAdder {
full_adder: FullAdder::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 carry bit that gets rippled along
let mut res: Vec<bool> = Vec::new();
let mut carry = 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 carry bit to the calculated
// carry bit and push the calculated sum to the result vec
let result = self.full_adder.calc(*bit_a, *bit_b, carry);
carry = result.carry;
res.push(result.sum);
}
// Push the last carry bit and reverse the result vec
res.push(carry);
res.reverse();
// Wrap the result vec in a BinaryRepresentation
return BinaryRepresentation::from_vec(res);
}
}