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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// SPDX-FileCopyrightText: 2022 Thomas Kramer <code@tkramer.ch>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
//! Transformations of the MIG based on the distributivity of the three-input majority function.
use crate::networks::generic_network::NodeId;
use crate::networks::{mig::*, SimplifyResult};
use itertools::Itertools;
impl Mig {
/// Try to eliminate a node based on the distributivity of the majority function.
///
/// Distributivity:
/// * M(x, y, M(u, v, z)) == M(M(x, y, u), M(x, y, v), z)
///
fn simplify_node_by_distributivity(
&mut self,
node: Maj3Node,
) -> SimplifyResult<Maj3Node, NodeId> {
if let Some((x, y, u, v, z)) = self.match_distributivity(node) {
let third_input = self.create_maj3(u, v, z);
let new_node = self.create_maj3(x, y, third_input);
SimplifyResult::new_id(new_node)
} else {
SimplifyResult::new_node(node)
}
}
/// Try to match the pattern `M(M(x, y, u), M(x, y, v), z)` on the given node.
/// This is used for eliminating a node based on the distributivity rule `M(M(x, y, u), M(x, y, v), z)` => `M(x, y, M(u, v, z))`.
///
/// If the pattern matches, return `(x, y, u, v, z)`, otherwise return `None`.
fn match_distributivity(
&self,
node: Maj3Node,
) -> Option<(MigNodeId, MigNodeId, MigNodeId, MigNodeId, MigNodeId)> {
// Find signals which are shared in two or more child nodes.
// Associate the signals with a bitmap which tells in which child nodes they appear.
let signals_grouped_by_child_idx = node
.into_iter()
.enumerate()
// Skip primary inputs.
.filter_map(|(child_idx, nid)| {
self.get_node(nid)
.to_logic_node()
.map(|idx| (child_idx, idx))
})
// 1) Get inputs of child nodes.
// Deduplicate the inputs per child node.
.map(|(child_idx, maj3)| maj3.into_iter().dedup().map(move |nid| (nid, child_idx)))
// 2) Merge the inputs of the child nodes. The result is sorted because the inputs are sorted.
.kmerge()
// 3) Deduplicate signals and remember in which child nodes they appear.
// 3.1) Group inputs together by their signal ID.
.group_by(|(nid, _i)| *nid);
let signals_shared_in_two_or_more_child_nodes: Vec<(MigNodeId, u32)> = // TODO: use SmallVec of length 9
signals_grouped_by_child_idx
.into_iter()
// For each signal, create a bitmap which tells in which children it appears.
.map(|(nid, group_members)| {
let signal_appears_in: u32 = group_members
// Look at the child node to which the input belongs
.map(|(_nid, child_idx)| child_idx)
// Create a bitmap. If the signal appears in child `j` then set the j-th bit.
.fold(0, |acc, child_idx| acc | (1 << child_idx));
(nid, signal_appears_in)
})
// Take only signals which appear in two or more child nodes.
.filter(|(_nid, appears_in)| appears_in.count_ones() >= 2)
.collect();
// Create all pairs.
let pairs = signals_shared_in_two_or_more_child_nodes
.iter()
.enumerate()
.flat_map(|(i, element1)| {
signals_shared_in_two_or_more_child_nodes[i + 1..]
.iter()
.map(move |element2| (element1, element2))
});
// Find a pair of signals which appears in at least two nodes.
let pair = pairs
.filter(|((_nid1, appears_in1), (_nid2, appears_in2))| {
(appears_in1 & appears_in2).count_ones() >= 2
})
.map(|((nid1, appears_in1), (nid2, appears_in2))| {
(*nid1, *nid2, appears_in1 & appears_in2)
})
.next();
// Find the `x`, `y`, `u`, `v` and `z` in `M(M(x, y, u), M(x, y, v), z)`
if let Some((nid_x, nid_y, appears_in)) = pair {
// There are at least two child nodes which both use the signals `nid1` and `nid2`.
// The indices of the child nodes are encoded as a bitmap in `appears_in`.
// => It is possible to eliminate a node based on the distributivity.
debug_assert!(appears_in.count_ones() >= 2);
debug_assert!(appears_in.count_ones() <= 3);
debug_assert_ne!(nid_x, nid_y);
// If there are three child nodes using both signals, take the first two only.
let appears_in = if appears_in.count_ones() > 2 {
appears_in & 0b011
} else {
appears_in
};
debug_assert_eq!(appears_in.count_ones(), 2);
// Convert the bitmap into indices.
let (child_idx1, child_idx2, third_input_idx) = match appears_in {
0b011 => (0, 1, 2),
0b101 => (0, 2, 1),
0b110 => (2, 1, 0),
_ => unreachable!("exactly two child nodes must be selected now"),
};
let children = node.to_array();
// Sanity check: The selected child nodes must not be inputs nor constants.
debug_assert!(self
.get_node(children[child_idx1])
.to_logic_node()
.is_some());
debug_assert!(self
.get_node(children[child_idx2])
.to_logic_node()
.is_some());
// Find the `u`, `v` and `z` in `M(M(x, y, u), M(x, y, v), z)`
let child1 = self.get_node(children[child_idx1]).to_logic_node().unwrap(); // Unwrap is ok, the selected child nodes must be regular nodes.
let child2 = self.get_node(children[child_idx2]).to_logic_node().unwrap(); // Unwrap is ok, the selected child nodes must be regular nodes.
let z = children[third_input_idx];
// Match the pattern `[u, x, x]` (including all permutations) and find u.
let u = match child1.to_array() {
[x, y, u] | [x, u, y] | [u, x, y]
if (x == nid_x && y == nid_y) || (y == nid_x && x == nid_y) =>
{
u
}
_ => unreachable!(),
};
// Match the pattern `[v, x, x]` (including all permutations) and find v.
let v = match child2.to_array() {
[x, y, v] | [x, v, y] | [v, x, y]
if (x == nid_x && y == nid_y) || (y == nid_x && x == nid_y) =>
{
v
}
_ => unreachable!(),
};
Some((nid_x, nid_y, u, v, z))
} else {
None
}
}
}
#[test]
fn test_match_simplify_distributivity() {
let mut mig = Mig::new();
let [x, y, u, v, z] = mig.create_primary_inputs();
// Create the following pattern and test if it is matched correctly.
// `M(M(x, y, u), M(x, y, v), z)`
let m1 = mig.create_maj3(x, y, u);
let m2 = mig.create_maj3(x, y, v);
let m3 = mig.create_maj3(m1, m2, z);
let (x1, y1, u1, v1, z1) = mig
.match_distributivity(*mig.get_node(m3).to_logic_node().unwrap())
.unwrap();
assert_eq!(z, z1);
assert!((x1, y1) == (x, y) || (y1, x1) == (x, y));
assert!((u1, v1) == (u, v) || (u1, v1) == (u, v));
}
#[test]
fn test_match_simplify_distributivity_nomatch() {
let mut mig = Mig::new();
let [x, y, u, v, w, z] = mig.create_primary_inputs();
// Create the following pattern and test if it is matched correctly.
// `M(M(x, y, u), M(x, y, v), z)`
let m1 = mig.create_maj3(x, y, u);
let m2 = mig.create_maj3(x, w, v); // Should be (x, y, v) to create a match.
let m3 = mig.create_maj3(m1, m2, z);
let match_result = mig.match_distributivity(*mig.get_node(m3).to_logic_node().unwrap());
assert_eq!(match_result, None);
}