volute 1.2.1

Boolean functions implementation, represented as lookup tables (LUT) or sum-of-products (SOP)
Documentation
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
use crate::operations::{cmp, flip_inplace, not_inplace, swap_adjacent_inplace};

/// Single bit flips to visit all possible binary values (Gray code)
const FLIPS: &[&[u8]] = &[
    &[],
    &[0, 0],
    &[0, 1, 0, 1],
    &[0, 1, 0, 2, 0, 1, 0, 2],
    &[0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3],
    &[
        0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1,
        0, 4,
    ],
    &[
        0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1,
        0, 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2,
        0, 1, 0, 5,
    ],
];

/// Adjacent swaps to visit all possible permutations (Steinhaus-Johnson-Trotter)
const SWAPS: &[&[u8]] = &[
    &[],
    &[],
    &[0, 0],
    &[0, 1, 0, 1, 0, 1],
    &[
        0, 1, 2, 0, 2, 1, 0, 2, 0, 1, 2, 0, 2, 1, 0, 2, 0, 1, 2, 0, 2, 1, 0, 2,
    ],
    &[
        0, 1, 2, 3, 0, 3, 2, 1, 0, 2, 0, 1, 2, 3, 2, 3, 2, 1, 0, 1, 0, 1, 2, 3, 2, 3, 2, 1, 0, 2,
        0, 1, 2, 3, 0, 3, 2, 1, 0, 3, 0, 1, 2, 3, 0, 3, 2, 1, 0, 2, 0, 1, 2, 3, 2, 3, 2, 1, 0, 1,
        0, 1, 2, 3, 2, 3, 2, 1, 0, 2, 0, 1, 2, 3, 0, 3, 2, 1, 0, 3, 0, 1, 2, 3, 0, 3, 2, 1, 0, 2,
        0, 1, 2, 3, 2, 3, 2, 1, 0, 1, 0, 1, 2, 3, 2, 3, 2, 1, 0, 2, 0, 1, 2, 3, 0, 3, 2, 1, 0, 3,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0, 1, 2, 3, 4, 2, 4, 3, 2, 1, 0, 4, 0, 1, 2, 3, 4, 0,
        4, 3, 2, 1, 0, 4, 0, 1, 2, 3, 4, 2, 4, 3, 2, 1, 0, 2, 0, 1, 2, 3, 4, 0, 4, 3, 2, 1, 0, 4,
    ],
];

/// Generate all flips to visit all N-bit gray code
pub fn generate_gray_flips(nb_bits: usize, rollback: bool) -> Vec<u8> {
    // Basically just a gray code count
    let mut flips: Vec<u8> = Vec::new();
    let end: usize = 1 << nb_bits;
    for i in 1..end {
        let j = i - 1;
        let pred = j ^ (j >> 1);
        let gray = i ^ (i >> 1);
        let diff = pred ^ gray;
        flips.push(diff.trailing_zeros() as u8);
    }
    if rollback {
        flips.push((nb_bits - 1) as u8);
    }
    flips
}

/// Find the position where a swap occurs in a permutation
fn find_permutation_swap(p1: &[u8], p2: &[u8]) -> u8 {
    assert_eq!(p1.len(), p2.len());
    for i in 0..p1.len() - 1 {
        if p1[i] != p2[i] {
            check_permutation_swap(p1, p2, i);
            return i as u8;
        }
    }
    panic!("No difference found between permutations")
}

/// Find the position where a swap occurs in a permutation
fn check_permutation_swap(p1: &[u8], p2: &[u8], ind: usize) {
    assert_eq!(p1.len(), p2.len());
    for i in 0..p1.len() - 1 {
        if i != ind && i != ind + 1 {
            assert_eq!(p1[i], p2[i]);
        }
    }
    assert_eq!(&p1[ind], &p2[ind + 1]);
    assert_eq!(&p1[ind + 1], &p2[ind]);
}

/// Generate all unique permutations, with a single adjacent swap between consecutive ones
fn generate_single_swap_permutations(num_vars: usize) -> Vec<Vec<u8>> {
    if num_vars == 0 {
        vec![vec![]]
    } else if num_vars == 1 {
        vec![vec![0]]
    } else if num_vars == 2 {
        vec![vec![1, 0], vec![0, 1]]
    } else {
        let all_perms = generate_single_swap_permutations(num_vars - 1);
        let mut next_perms: Vec<Vec<u8>> = vec![];
        for (i, cur) in all_perms.iter().enumerate() {
            if i % 2 == 0 {
                for j in 0..cur.len() + 1 {
                    let mut p = cur.clone();
                    p.insert(j, (num_vars - 1) as u8);
                    next_perms.push(p);
                }
            } else {
                for j in (0..cur.len() + 1).rev() {
                    let mut p = cur.clone();
                    p.insert(j, (num_vars - 1) as u8);
                    next_perms.push(p);
                }
            }
        }
        next_perms
    }
}

/// Generate all swaps for P canonization
pub fn generate_swaps(num_vars: usize, rollback: bool) -> Vec<u8> {
    let perms = generate_single_swap_permutations(num_vars);
    let mut swaps: Vec<u8> = Vec::new();
    for i in 0..perms.len() - 1 {
        swaps.push(find_permutation_swap(&perms[i], &perms[i + 1]));
    }
    if rollback && !swaps.is_empty() {
        swaps.push(find_permutation_swap(
            perms.last().unwrap(),
            perms.first().unwrap(),
        ));
    }
    swaps
}

/// Run all swaps on the P canonization, and return the index of the best result
#[inline(always)]
pub fn p_canonization_ind(
    num_vars: usize,
    table: &mut [u64],
    best: &mut [u64],
    all_swaps: &[u8],
) -> usize {
    best.clone_from_slice(table);
    let mut best_ind = usize::MAX;
    for (ind, &swap) in all_swaps.iter().enumerate() {
        swap_adjacent_inplace(num_vars, table, swap as usize);
        if cmp(table, best).is_lt() {
            best_ind = ind;
            best.clone_from_slice(table);
        }
    }
    best_ind
}

/// Returns whether the Lut is p-canonical
pub fn is_p_canonical_helper(
    num_vars: usize,
    table: &[u64],
    work: &mut [u64],
    all_swaps: &[u8],
) -> bool {
    work.clone_from_slice(table);
    for &swap in all_swaps.iter() {
        swap_adjacent_inplace(num_vars, work, swap as usize);
        if cmp(work, table).is_lt() {
            return false;
        }
    }
    true
}

/// Run all flips on the N canonization, and return the best solution
#[inline(always)]
pub fn n_canonization_helper(
    num_vars: usize,
    table: &mut [u64],
    best: &mut [u64],
    all_flips: &[u8],
) -> u32 {
    let mut best_flip = 0;
    let mut cur_flip = 0;
    best.clone_from_slice(table);
    for &flip in all_flips {
        cur_flip ^= 1 << flip;
        flip_inplace(num_vars, table, flip as usize);
        for _ in 0..2 {
            cur_flip ^= 1 << num_vars;
            not_inplace(num_vars, table);
            if cmp(table, best).is_lt() {
                best_flip = cur_flip;
                best.clone_from_slice(table);
            }
        }
    }
    best_flip
}

/// Returns whether the Lut is n-canonical
pub fn is_n_canonical_helper(
    num_vars: usize,
    table: &[u64],
    work: &mut [u64],
    all_flips: &[u8],
) -> bool {
    work.clone_from_slice(table);
    for &flip in all_flips {
        flip_inplace(num_vars, work, flip as usize);
        for _ in 0..2 {
            not_inplace(num_vars, work);
            if cmp(work, table).is_lt() {
                return false;
            }
        }
    }
    true
}

/// Run all swaps on the P canonization and all flips on the N canonization,
/// and return the index of the best result
#[inline(always)]
pub fn npn_canonization_ind(
    num_vars: usize,
    table: &mut [u64],
    best: &mut [u64],
    all_swaps: &[u8],
    all_flips: &[u8],
) -> usize {
    best.clone_from_slice(table);
    let mut best_ind = usize::MAX;
    let mut ind = 0;
    for swap in all_swaps {
        swap_adjacent_inplace(num_vars, table, *swap as usize);
        for flip in all_flips {
            flip_inplace(num_vars, table, *flip as usize);
            for _ in 0..2 {
                not_inplace(num_vars, table);
                if cmp(table, best).is_lt() {
                    best_ind = ind;
                    best.clone_from_slice(table);
                }
                ind += 1;
            }
        }
    }
    best_ind
}

/// Returns whether the Lut is npn-canonical
pub fn is_npn_canonical_helper(
    num_vars: usize,
    table: &[u64],
    work: &mut [u64],
    all_swaps: &[u8],
    all_flips: &[u8],
) -> bool {
    work.clone_from_slice(table);
    for swap in all_swaps {
        swap_adjacent_inplace(num_vars, work, *swap as usize);
        for flip in all_flips {
            flip_inplace(num_vars, work, *flip as usize);
            for _ in 0..2 {
                not_inplace(num_vars, work);
                if cmp(work, table).is_lt() {
                    return false;
                }
            }
        }
    }
    true
}

/// Find the corresponding permutation given the index of the best result
#[inline(always)]
pub fn p_canonization_res(num_vars: usize, res_perm: &mut [u8], all_swaps: &[u8], best_ind: usize) {
    debug_assert!(best_ind == usize::MAX || best_ind < all_swaps.len());
    debug_assert_eq!(res_perm.len(), num_vars);
    for (i, p) in res_perm.iter_mut().enumerate() {
        *p = i as u8;
    }
    if best_ind == usize::MAX {
        return;
    }
    for (ind, &swap) in all_swaps.iter().enumerate() {
        let swp = swap as usize;
        res_perm.swap(swp, swp + 1);
        if ind == best_ind {
            return;
        }
    }
    // Should never arrive there...
    panic!("P-canonization reached an invalid state");
}

/// Find the corresponding permutation and complementation given the index of the best result
#[inline(always)]
pub fn npn_canonization_res(
    num_vars: usize,
    res_perm: &mut [u8],
    all_swaps: &[u8],
    all_flips: &[u8],
    best_ind: usize,
) -> u32 {
    assert_eq!(res_perm.len(), num_vars);
    for (i, p) in res_perm.iter_mut().enumerate() {
        *p = i as u8;
    }
    let mut ind = 0;
    let mut cur_flip = 0;
    if best_ind == usize::MAX {
        return cur_flip;
    }

    for swap in all_swaps {
        let swp = *swap as usize;
        res_perm.swap(swp, swp + 1);
        for flip in all_flips {
            cur_flip ^= 1 << *flip;
            for _ in 0..2 {
                cur_flip ^= 1 << num_vars;
                if ind == best_ind {
                    return cur_flip;
                }
                ind += 1;
            }
        }
    }
    // Should never arrive there...
    panic!("NPN-canonization reached an invalid state");
}

pub fn p_canonization(num_vars: usize, table: &mut [u64], best: &mut [u64], res_perm: &mut [u8]) {
    if num_vars <= 6 {
        let best_ind =
            p_canonization_ind(num_vars, &mut table[0..1], &mut best[0..1], SWAPS[num_vars]);
        p_canonization_res(num_vars, res_perm, SWAPS[num_vars], best_ind);
    } else {
        let all_swaps = generate_swaps(num_vars, true);
        let best_ind = p_canonization_ind(num_vars, table, best, &all_swaps);
        p_canonization_res(num_vars, res_perm, &all_swaps, best_ind);
    }
}

pub fn is_p_canonical(num_vars: usize, table: &[u64], work: &mut [u64]) -> bool {
    if num_vars <= 6 {
        is_p_canonical_helper(num_vars, &table[0..1], &mut work[0..1], SWAPS[num_vars])
    } else {
        let all_swaps = generate_swaps(num_vars, true);
        is_p_canonical_helper(num_vars, table, work, &all_swaps)
    }
}

pub fn n_canonization(num_vars: usize, table: &mut [u64], best: &mut [u64]) -> u32 {
    if num_vars <= 6 {
        n_canonization_helper(num_vars, &mut table[0..1], &mut best[0..1], FLIPS[num_vars])
    } else {
        let all_flips = generate_gray_flips(num_vars, true);
        n_canonization_helper(num_vars, table, best, &all_flips)
    }
}

pub fn is_n_canonical(num_vars: usize, table: &[u64], work: &mut [u64]) -> bool {
    if num_vars <= 6 {
        is_n_canonical_helper(num_vars, &table[0..1], &mut work[0..1], FLIPS[num_vars])
    } else {
        let all_flips = generate_gray_flips(num_vars, true);
        is_n_canonical_helper(num_vars, table, work, &all_flips)
    }
}

pub fn npn_canonization(
    num_vars: usize,
    table: &mut [u64],
    best: &mut [u64],
    res_perm: &mut [u8],
) -> u32 {
    if num_vars <= 6 {
        let best_ind = npn_canonization_ind(
            num_vars,
            &mut table[0..1],
            &mut best[0..1],
            SWAPS[num_vars],
            FLIPS[num_vars],
        );
        npn_canonization_res(
            num_vars,
            res_perm,
            SWAPS[num_vars],
            FLIPS[num_vars],
            best_ind,
        )
    } else {
        let all_swaps = generate_swaps(num_vars, true);
        let all_flips = generate_gray_flips(num_vars, true);
        let best_ind = npn_canonization_ind(num_vars, table, best, &all_swaps, &all_flips);
        npn_canonization_res(num_vars, res_perm, &all_swaps, &all_flips, best_ind)
    }
}

pub fn is_npn_canonical(num_vars: usize, table: &[u64], work: &mut [u64]) -> bool {
    if num_vars <= 6 {
        is_npn_canonical_helper(
            num_vars,
            &table[0..1],
            &mut work[0..1],
            SWAPS[num_vars],
            FLIPS[num_vars],
        )
    } else {
        let all_swaps = generate_swaps(num_vars, true);
        let all_flips = generate_gray_flips(num_vars, true);
        is_npn_canonical_helper(num_vars, table, work, &all_swaps, &all_flips)
    }
}

#[cfg(test)]
mod tests {
    use crate::canonization::{generate_gray_flips, generate_swaps};

    #[test]
    fn test_gray_flips() {
        for i in 1..=7 {
            let flips = generate_gray_flips(i, false);
            assert_eq!(flips.len(), (1 << i) - 1);
            for f in flips {
                assert!(f < (i as u8));
            }
            let flips_rollback = generate_gray_flips(i, true);
            assert_eq!(flips_rollback.len(), 1 << i);
            for f in flips_rollback.iter() {
                assert!(*f < (i as u8));
            }

            // Check that the rollback is OK
            let mut cnt = 0u64;
            for f in flips_rollback.iter() {
                cnt ^= 1 << f;
            }
            assert_eq!(cnt, 0u64);
        }
    }

    #[test]
    fn test_swaps() {
        let mut fact = 1;
        for i in 1..=7 {
            fact *= i;
            let swaps = generate_swaps(i, false);
            assert_eq!(swaps.len(), fact - 1);
            let swaps_rollback = generate_swaps(i, true);
            assert_eq!(swaps_rollback.len(), if i == 1 { 0 } else { fact });
            // Check that the rollback is OK
            let mut perm: Vec<usize> = (0..i).collect();
            for s in swaps_rollback {
                assert!(s < (i as u8));
                perm.swap(s as usize, (s + 1) as usize);
            }
            assert_eq!(perm, (0..i).collect::<Vec<usize>>());
        }
    }
}