43
43
namespace kitty
44
44
{
45
45
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 */
57
50
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 )
59
52
{
60
53
assert ( tt.num_vars () <= 6u );
61
54
const uint64_t y = ( tt._bits [0 ] ^ ( tt._bits [0 ] >> delta ) ) & omega;
62
55
tt._bits [0 ] = tt._bits [0 ] ^ y ^ ( y << delta );
63
56
}
64
57
65
- /* ! \cond PRIVATE */
66
58
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 )
68
60
{
69
61
assert ( NumVars <= 6 );
70
62
const uint64_t y = ( tt._bits ^ ( tt._bits >> delta ) ) & omega;
71
63
tt._bits = tt._bits ^ y ^ ( y << delta );
72
64
}
73
- /* ! \endcond */
74
-
75
- /* ! \brief Applies delta-swap operation
76
65
77
- Out-of-place variant for `delta_swap_inplace`.
78
- */
79
66
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 )
98
68
{
99
69
for ( auto k = 0 ; k < tt.num_vars (); ++k )
100
70
{
101
- delta_swap_inplace ( tt, uint64_t ( 1 ) << k, masks[k] );
71
+ delta_swap_inplace_opt ( tt, uint64_t ( 1 ) << k, masks[k] );
102
72
}
103
73
104
74
for ( int k = tt.num_vars () - 2 , i = tt.num_vars (); k >= 0 ; --k, ++i )
105
75
{
106
- delta_swap_inplace ( tt, uint64_t ( 1 ) << k, masks[i] );
76
+ delta_swap_inplace_opt ( tt, uint64_t ( 1 ) << k, masks[i] );
107
77
}
108
78
}
109
79
110
- /* ! \brief Permutes a truth table using a sequence of delta-swaps
111
-
112
- Out-of-place variant of `permute_with_masks_inplace`.
113
- */
114
80
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 )
116
82
{
117
83
auto copy = tt;
118
- permute_with_masks_inplace ( copy, masks );
84
+ permute_with_masks_inplace_opt ( copy, masks );
119
85
return copy;
120
86
}
121
87
122
- /* ! \cond PRIVATE */
123
88
template <typename Fn>
124
89
inline void for_each_permutation_mask ( unsigned num_vars, Fn&& fn )
125
90
{
@@ -135,6 +100,7 @@ inline void for_each_permutation_mask( unsigned num_vars, Fn&& fn )
135
100
fn ( &detail::linear_masks[i] );
136
101
}
137
102
}
103
+ }
138
104
/* ! \endcond */
139
105
140
106
/* ! \brief Applies exact linear classification
@@ -150,8 +116,8 @@ TT exact_linear_canonization( const TT& tt )
150
116
{
151
117
auto min = tt;
152
118
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 ) );
155
121
});
156
122
157
123
return min;
0 commit comments