Skip to content

Commit 4456083

Browse files
authored
Merge pull request #40 from msoeken/permmask
Compute permutation masks
2 parents 38d3db7 + f87346a commit 4456083

9 files changed

+412
-58
lines changed

docs/canonization.rst

-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ The header ``<kitty/affine.hpp>`` implements canonization algorithms based on
2020
linear and affine transformations.
2121

2222
.. doc_brief_table::
23-
delta_swap_inplace
24-
delta_swap
25-
permute_with_masks_inplace
26-
permute_with_masks
2723
exact_linear_canonization
2824
exact_linear_output_canonization
2925
exact_affine_canonization

docs/changelog.rst

+4-1
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@ v0.3 (not yet released)
3333
* Linear and affine canonization: ``exact_linear_canonization``, ``exact_linear_output_canonization``, ``exact_affine_canonization``, ``exact_affine_output_canonization``
3434
`#36 <https://github.com/msoeken/kitty/pull/36>`_
3535

36-
* Compute PPRM ESOP for truth table
36+
* Compute PPRM ESOP for truth table: ``esop_from_pprm``
3737
`#38 <https://github.com/msoeken/kitty/pull/38>`_
3838

39+
* Compute permutation masks and delta-swap operations: ``delta_swap_inplace``, ``delta_swap``, ``permute_with_masks_inplace``, ``permute_with_masks``, ``compute_permutation_masks``,
40+
`#40 <https://github.com/msoeken/kitty/pull/40>`_
41+
3942
v0.2 (December 21, 2017)
4043
------------------------
4144

docs/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Welcome to kitty's documentation!
2929
hash
3030
canonization
3131
cube
32+
permutation
3233
esop
3334
isop
3435
reference

docs/permutation.rst

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Permutations
2+
============
3+
4+
The header ``<kitty/permutation.hpp>`` implements permutation algorithms to
5+
be used with truth tables.
6+
7+
.. doc_brief_table::
8+
delta_swap_inplace
9+
delta_swap
10+
permute_with_masks_inplace
11+
permute_with_masks
12+
compute_permutation_masks
13+

include/kitty/affine.hpp

+14-48
Original file line numberDiff line numberDiff line change
@@ -43,83 +43,48 @@
4343
namespace kitty
4444
{
4545

46-
/*! \brief Applies delta-swap operation
47-
48-
The delta-swap operation swaps all position pairs \f$(i, i+\delta)\f$, for
49-
which \f$\omega\f$ is set 1 at position \f$i\f$.
50-
51-
See also Eq. 7.1.3-(69) in The Art of Computer Programming.
52-
53-
\param tt Truth table
54-
\param delta Index distance delta
55-
\param omega Enable mask
56-
*/
46+
/*! \cond PRIVATE */
47+
namespace detail
48+
{
49+
/* all delta-swap operations have been optimized to work with integer masks omega */
5750
template<typename TT>
58-
inline void delta_swap_inplace( TT& tt, uint64_t delta, uint64_t omega )
51+
inline void delta_swap_inplace_opt( TT& tt, uint64_t delta, uint64_t omega )
5952
{
6053
assert( tt.num_vars() <= 6u );
6154
const uint64_t y = ( tt._bits[0] ^ ( tt._bits[0] >> delta ) ) & omega;
6255
tt._bits[0] = tt._bits[0] ^ y ^ ( y << delta );
6356
}
6457

65-
/*! \cond PRIVATE */
6658
template<int NumVars>
67-
inline void delta_swap_inplace( static_truth_table<NumVars, true>& tt, uint64_t delta, uint64_t omega )
59+
inline void delta_swap_inplace_opt( static_truth_table<NumVars, true>& tt, uint64_t delta, uint64_t omega )
6860
{
6961
assert ( NumVars <= 6 );
7062
const uint64_t y = ( tt._bits ^ ( tt._bits >> delta ) ) & omega;
7163
tt._bits = tt._bits ^ y ^ ( y << delta );
7264
}
73-
/*! \endcond */
74-
75-
/*! \brief Applies delta-swap operation
7665

77-
Out-of-place variant for `delta_swap_inplace`.
78-
*/
7966
template<typename TT>
80-
TT delta_swap( const TT& tt, uint64_t delta, uint64_t omega )
81-
{
82-
auto copy = tt;
83-
delta_swap_inplace( copy, delta, omega );
84-
return copy;
85-
}
86-
87-
/*! \brief Permutes a truth table using a sequence of delta-swaps
88-
89-
Masks is an array containing the \f$\omega\f$ masks. The \f$\delta\f$ values
90-
are chosen as increasing and decreasing powers of 2, as described in Eq.
91-
7.1.3-(71) of The Art of Computer Programming.
92-
93-
\param tt Truth table
94-
\param masks Array of omega-masks
95-
*/
96-
template<typename TT>
97-
void permute_with_masks_inplace( TT& tt, uint64_t const* masks )
67+
void permute_with_masks_inplace_opt( TT& tt, uint64_t const* masks )
9868
{
9969
for ( auto k = 0; k < tt.num_vars(); ++k )
10070
{
101-
delta_swap_inplace( tt, uint64_t( 1 ) << k, masks[k] );
71+
delta_swap_inplace_opt( tt, uint64_t( 1 ) << k, masks[k] );
10272
}
10373

10474
for ( int k = tt.num_vars() - 2, i = tt.num_vars(); k >= 0; --k, ++i )
10575
{
106-
delta_swap_inplace( tt, uint64_t( 1 ) << k, masks[i] );
76+
delta_swap_inplace_opt( tt, uint64_t( 1 ) << k, masks[i] );
10777
}
10878
}
10979

110-
/*! \brief Permutes a truth table using a sequence of delta-swaps
111-
112-
Out-of-place variant of `permute_with_masks_inplace`.
113-
*/
11480
template<typename TT>
115-
TT permute_with_masks( const TT& tt, uint64_t const* masks )
81+
TT permute_with_masks_opt( const TT& tt, uint64_t const* masks )
11682
{
11783
auto copy = tt;
118-
permute_with_masks_inplace( copy, masks );
84+
permute_with_masks_inplace_opt( copy, masks );
11985
return copy;
12086
}
12187

122-
/*! \cond PRIVATE */
12388
template<typename Fn>
12489
inline void for_each_permutation_mask( unsigned num_vars, Fn&& fn )
12590
{
@@ -135,6 +100,7 @@ inline void for_each_permutation_mask( unsigned num_vars, Fn&& fn )
135100
fn( &detail::linear_masks[i] );
136101
}
137102
}
103+
}
138104
/*! \endcond */
139105

140106
/*! \brief Applies exact linear classification
@@ -150,8 +116,8 @@ TT exact_linear_canonization( const TT& tt )
150116
{
151117
auto min = tt;
152118

153-
for_each_permutation_mask( tt.num_vars(), [&min, &tt]( const auto* mask ) {
154-
min = std::min( min, permute_with_masks( tt, mask ) );
119+
detail::for_each_permutation_mask( tt.num_vars(), [&min, &tt]( const auto* mask ) {
120+
min = std::min( min, detail::permute_with_masks_opt( tt, mask ) );
155121
});
156122

157123
return min;

include/kitty/kitty.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "npn.hpp"
4747
#include "operations.hpp"
4848
#include "operators.hpp"
49+
#include "permutation.hpp"
4950
#include "print.hpp"
5051
#include "spectral.hpp"
5152

0 commit comments

Comments
 (0)