|
| 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 "operations.hpp" |
| 29 | +#include "operators.hpp" |
| 30 | + |
| 31 | +namespace kitty |
| 32 | +{ |
| 33 | + |
| 34 | +/*! \cond PRIVATE */ |
| 35 | +template<typename TT> |
| 36 | +TT isop_rec( const TT& tt, const TT& dc, uint8_t var_index, std::vector<uint64_t>& cubes ) |
| 37 | +{ |
| 38 | + assert( var_index <= tt.num_vars() ); |
| 39 | + assert( is_const0( tt & ~dc ) ); |
| 40 | + |
| 41 | + if ( is_const0( tt ) ) |
| 42 | + return tt; |
| 43 | + |
| 44 | + if ( is_const0( ~dc ) ) |
| 45 | + { |
| 46 | + cubes.push_back( 0 ); |
| 47 | + return dc; |
| 48 | + } |
| 49 | + |
| 50 | + assert( var_index > 0 ); |
| 51 | + |
| 52 | + int var = var_index - 1; |
| 53 | + for ( ; var >= 0; --var ) |
| 54 | + { |
| 55 | + if ( has_var( tt, var ) || has_var( dc, var ) ) |
| 56 | + break; |
| 57 | + } |
| 58 | + |
| 59 | + assert( var >= 0 ); |
| 60 | + |
| 61 | + /* co-factor */ |
| 62 | + const auto tt0 = cofactor0( tt, var ); |
| 63 | + const auto tt1 = cofactor1( tt, var ); |
| 64 | + const auto dc0 = cofactor0( dc, var ); |
| 65 | + const auto dc1 = cofactor1( dc, var ); |
| 66 | + |
| 67 | + const auto beg0 = cubes.size(); |
| 68 | + const auto res0 = isop_rec( tt0 & ~dc1, dc0, var, cubes ); |
| 69 | + const auto end0 = cubes.size(); |
| 70 | + const auto res1 = isop_rec( tt1 & ~dc0, dc1, var, cubes ); |
| 71 | + const auto end1 = cubes.size(); |
| 72 | + auto res2 = isop_rec( ( tt0 & ~res0 ) | ( tt1 & ~res1 ), dc0 & dc1, var, cubes ); |
| 73 | + |
| 74 | + auto var0 = tt.construct(); |
| 75 | + create_nth_var( var0, var, true ); |
| 76 | + auto var1 = tt.construct(); |
| 77 | + create_nth_var( var1, var ); |
| 78 | + res2 |= ( res0 & var0 ) | ( res1 & var1 ); |
| 79 | + |
| 80 | + for ( auto c = beg0; c < end0; ++c ) |
| 81 | + { |
| 82 | + cubes[c] |= 1 << ( 2 * var ); |
| 83 | + } |
| 84 | + for ( auto c = end0; c < end1; ++c ) |
| 85 | + { |
| 86 | + cubes[c] |= 1 << ( 2 * var + 1 ); |
| 87 | + } |
| 88 | + |
| 89 | + assert( is_const0( tt & ~res2 ) ); |
| 90 | + assert( is_const0( res2 & ~dc ) ); |
| 91 | + |
| 92 | + return res2; |
| 93 | +} |
| 94 | +/* \endcond */ |
| 95 | + |
| 96 | +/*! \brief Computes ISOP representation |
| 97 | +
|
| 98 | + Computes the irredundant sum-of-products representation using the |
| 99 | + Minato-Morreale algorithm [S. Minato, IEEE Trans. CAD 15(4), 1996, |
| 100 | + 377-384]. |
| 101 | +
|
| 102 | + Check the function `create_from_cubes` for a detailed description of |
| 103 | + the cubes representation. |
| 104 | +
|
| 105 | + \param tt Truth table |
| 106 | +*/ |
| 107 | +template<typename TT> |
| 108 | +inline std::vector<uint64_t> isop( const TT& tt ) |
| 109 | +{ |
| 110 | + std::vector<uint64_t> cubes; |
| 111 | + isop_rec( tt, tt, tt.num_vars(), cubes ); |
| 112 | + return cubes; |
| 113 | +} |
| 114 | + |
| 115 | +} |
0 commit comments