|
| 1 | +/* kitty: C++ truth table library |
| 2 | + * Copyright (C) 2017 EPFL |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person |
| 5 | + * obtaining a copy of this software and associated documentation |
| 6 | + * files (the "Software"), to deal in the Software without |
| 7 | + * restriction, including without limitation the rights to use, |
| 8 | + * copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the |
| 10 | + * Software is furnished to do so, subject to the following |
| 11 | + * conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be |
| 14 | + * included in all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 18 | + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 20 | + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 21 | + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 | + * OTHER DEALINGS IN THE SOFTWARE. |
| 24 | + */ |
| 25 | + |
| 26 | +#pragma once |
| 27 | + |
| 28 | +#include <numeric> |
| 29 | + |
| 30 | +#include "detail/constants.hpp" |
| 31 | +#include "operators.hpp" |
| 32 | + |
| 33 | +namespace kitty |
| 34 | +{ |
| 35 | + |
| 36 | +/*! Exact NPN canonization |
| 37 | +
|
| 38 | + Given a truth table, this function finds the lexicographically |
| 39 | + smallest truth table in its NPN class, called NPN representative. |
| 40 | + Two functions are in the same NPN class, if one can obtain one from |
| 41 | + the other by input negation, input permutation, and output negation. |
| 42 | +
|
| 43 | + The function returns a NPN configuration which contains the |
| 44 | + necessary transformations to obtain the representative. It is a |
| 45 | + tuple of |
| 46 | +
|
| 47 | + - the NPN representative |
| 48 | + - input negations and output negation, output negation is stored as |
| 49 | + bit *n*, where *n* is the number of variables in `tt` |
| 50 | + - input permutation to apply |
| 51 | +
|
| 52 | + \param tt The truth table |
| 53 | + \return NPN configuration |
| 54 | +*/ |
| 55 | +template<typename TT> |
| 56 | +std::tuple<TT, uint32_t, std::vector<uint8_t>> exact_npn_canonization( const TT& tt ) |
| 57 | +{ |
| 58 | + const auto num_vars = tt.num_vars(); |
| 59 | + |
| 60 | + /* Special case for n = 0 */ |
| 61 | + if ( num_vars == 0 ) |
| 62 | + { |
| 63 | + const auto bit = get_bit( tt, 0 ); |
| 64 | + return std::make_tuple( unary_not_if( tt, bit ), bit, std::vector<uint8_t>{} ); |
| 65 | + } |
| 66 | + |
| 67 | + /* Special case for n = 1 */ |
| 68 | + if ( num_vars == 1 ) |
| 69 | + { |
| 70 | + const auto bit1 = get_bit( tt, 1 ); |
| 71 | + return std::make_tuple( unary_not_if( tt, bit1 ), bit1 << 1, std::vector<uint8_t>{ 0 } ); |
| 72 | + } |
| 73 | + |
| 74 | + assert( num_vars >= 2 && num_vars <= 6u ); |
| 75 | + |
| 76 | + auto t1 = tt, t2 = ~tt; |
| 77 | + auto tmin = std::min( t1, t2 ); |
| 78 | + auto invo = tmin == t2; |
| 79 | + |
| 80 | + const auto& swaps = detail::swaps[num_vars - 2u]; |
| 81 | + const auto& flips = detail::flips[num_vars - 2u]; |
| 82 | + |
| 83 | + int best_swap = -1; |
| 84 | + int best_flip = -1; |
| 85 | + |
| 86 | + for ( int i = 0; i < swaps.size(); ++i ) |
| 87 | + { |
| 88 | + const auto pos = swaps[i]; |
| 89 | + swap_adjacent_inplace( t1, pos ); |
| 90 | + swap_adjacent_inplace( t2, pos ); |
| 91 | + if ( t1 < tmin || t2 < tmin ) |
| 92 | + { |
| 93 | + best_swap = i; |
| 94 | + tmin = std::min( t1, t2 ); |
| 95 | + invo = tmin == t2; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + for ( int j = 0; j < flips.size(); ++j ) |
| 100 | + { |
| 101 | + const auto pos = flips[j]; |
| 102 | + swap_adjacent_inplace( t1, 0 ); |
| 103 | + flip_inplace( t1, pos ); |
| 104 | + swap_adjacent_inplace( t2, 0 ); |
| 105 | + flip_inplace( t2, pos ); |
| 106 | + if ( t1 < tmin || t2 < tmin ) |
| 107 | + { |
| 108 | + best_swap = -1; |
| 109 | + best_flip = j; |
| 110 | + tmin = std::min( t1, t2 ); |
| 111 | + invo = tmin == t2; |
| 112 | + } |
| 113 | + |
| 114 | + for ( int i = 0; i < swaps.size(); ++i ) |
| 115 | + { |
| 116 | + const auto pos = swaps[i]; |
| 117 | + swap_adjacent_inplace( t1, pos ); |
| 118 | + swap_adjacent_inplace( t2, pos ); |
| 119 | + if ( t1 < tmin || t2 < tmin ) |
| 120 | + { |
| 121 | + best_swap = i; |
| 122 | + best_flip = j; |
| 123 | + tmin = std::min( t1, t2 ); |
| 124 | + invo = tmin == t2; |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + std::vector<uint8_t> perm( num_vars ); |
| 130 | + std::iota( perm.begin(), perm.end(), 0u ); |
| 131 | + |
| 132 | + for ( auto i = 0; i <= best_swap; ++i ) |
| 133 | + { |
| 134 | + const auto pos = swaps[i]; |
| 135 | + std::swap( perm[pos], perm[pos + 1] ); |
| 136 | + } |
| 137 | + |
| 138 | + uint32_t phase = uint32_t( invo ) << num_vars; |
| 139 | + for ( auto i = 0; i <= best_flip; ++i ) |
| 140 | + { |
| 141 | + phase ^= 1 << flips[i]; |
| 142 | + } |
| 143 | + |
| 144 | + return std::make_tuple( tmin, phase, perm ); |
| 145 | +} |
| 146 | + |
| 147 | +/*! Obtain truth table from NPN configuration |
| 148 | +
|
| 149 | + Given an NPN configuration, which contains a representative |
| 150 | + function, input/output negations, and input permutations this |
| 151 | + function computes the original truth table. |
| 152 | +
|
| 153 | + \param config NPN configuration |
| 154 | +*/ |
| 155 | +template<typename TT> |
| 156 | +TT create_from_npn_config( const std::tuple<TT, uint32_t, std::vector<uint8_t>>& config ) |
| 157 | +{ |
| 158 | + const auto& from = std::get<0>( config ); |
| 159 | + const auto& phase = std::get<1>( config ); |
| 160 | + auto perm = std::get<2>( config ); |
| 161 | + const auto num_vars = from.num_vars(); |
| 162 | + |
| 163 | + /* is output complemented? */ |
| 164 | + auto res = ( ( phase >> num_vars ) & 1 ) ? ~from : from; |
| 165 | + |
| 166 | + /* input permutations */ |
| 167 | + for ( auto i = 0; i < num_vars; ++i ) |
| 168 | + { |
| 169 | + if ( perm[i] == i ) continue; |
| 170 | + |
| 171 | + int k = i; |
| 172 | + while ( perm[k] != i ) |
| 173 | + { |
| 174 | + ++k; |
| 175 | + } |
| 176 | + |
| 177 | + swap_inplace( res, i, k ); |
| 178 | + std::swap( perm[i], perm[k] ); |
| 179 | + } |
| 180 | + |
| 181 | + /* input complementations */ |
| 182 | + for ( auto i = 0; i < num_vars; ++i ) |
| 183 | + { |
| 184 | + if ( ( phase >> i ) & 1 ) |
| 185 | + { |
| 186 | + flip_inplace( res, i ); |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + |
| 191 | + return res; |
| 192 | +} |
| 193 | + |
| 194 | +} // namespace kitty |
0 commit comments