Skip to content

Commit 65aa635

Browse files
authored
Merge pull request #11 from msoeken/isop
Create ISOP representations
2 parents bc091f4 + 37d38f3 commit 65aa635

16 files changed

+576
-6
lines changed

docs/changelog.rst

+13-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ v0.1
1212
`#1 <https://github.com/msoeken/kitty/pull/1>`_
1313
`#8 <https://github.com/msoeken/kitty/pull/8>`_
1414

15-
* Constructors ``create_nth_var``, ``create_from_binary_string``, ``create_from_hex_string``, ``create_random``, and ``create_majority``
15+
* Constructors: ``create_nth_var``, ``create_from_binary_string``, ``create_from_hex_string``, ``create_random``, ``create_from_cubes``, and ``create_majority``
1616
`#1 <https://github.com/msoeken/kitty/pull/1>`_
1717
`#4 <https://github.com/msoeken/kitty/pull/4>`_
1818
`#5 <https://github.com/msoeken/kitty/pull/5>`_
1919
`#9 <https://github.com/msoeken/kitty/pull/9>`_
20+
`#11 <https://github.com/msoeken/kitty/pull/11>`_
2021

2122
* Unary and binary operations: ``unary_not``, ``unary_not_if``, ``binary_and``, ``binary_or``, and ``binary_xor``
2223
`#2 <https://github.com/msoeken/kitty/pull/2>`_
@@ -29,8 +30,12 @@ v0.1
2930
`#4 <https://github.com/msoeken/kitty/pull/4>`_
3031
`#8 <https://github.com/msoeken/kitty/pull/8>`_
3132

32-
* Operators: ``~``, ``==``, ``<``
33+
* Predicates: ``has_var``, ``is_const0``
34+
`#11 <https://github.com/msoeken/kitty/pull/11>`_
35+
36+
* Operators: ``~``, ``&``, ``&=``, ``|``, ``|=``, ``^``, ``^=``, ``==``, ``<``
3337
`#8 <https://github.com/msoeken/kitty/pull/8>`_
38+
`#11 <https://github.com/msoeken/kitty/pull/11>`_
3439

3540
* Swap adjacent variables: ``swap_adjacent_inplace``, ``swap_adjacent``
3641
`#6 <https://github.com/msoeken/kitty/pull/6>`_
@@ -44,5 +49,11 @@ v0.1
4449
* Enumerate truth tables: ``next_inplace``, ``next``
4550
`#10 <https://github.com/msoeken/kitty/pull/10>`_
4651

52+
* Compute co-factors: ``cofactor0_inplace``, ``cofactor0``, ``cofactor1_inplace``, ``cofactor1``
53+
`#11 <https://github.com/msoeken/kitty/pull/11>`_
54+
4755
* NPN canonization: ``exact_npn_canonization``, ``create_from_npn_config``
4856
`#8 <https://github.com/msoeken/kitty/pull/8>`_
57+
58+
* Compute ISOP representation: ``isop``
59+
`#11 <https://github.com/msoeken/kitty/pull/11>`_

docs/conf.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,9 @@ def run(self):
215215
tbody = nodes.tbody()
216216
for c in self.content:
217217
name = c.strip()
218+
query = name.replace("&", " &")
218219

219-
for elem in tree.findall("./compounddef/sectiondef/memberdef/[name='%s']" % name):
220+
for elem in tree.findall("./compounddef/sectiondef/memberdef/[name='%s']" % query):
220221
args = ', '.join(e.text for e in elem.findall("./param/declname"))
221222

222223
func = nodes.entry('', nodes.literal(text = '%s(%s)' % (name, args)))

docs/constructors.rst

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ The header ``<kitty/constructors.hpp>`` implements operations to construct truth
88
create_from_binary_string
99
create_from_hex_string
1010
create_random
11+
create_from_cubes
1112
create_majority
1213

1314

docs/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Welcome to kitty's documentation!
1616
operations
1717
operators
1818
canonization
19+
isop
1920
examples
2021
reference
2122
changelog

docs/isop.rst

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
ISOP representations
2+
====================
3+
4+
The header ``<kitty/isop.hpp>`` implements methods to compute irredundant sum-of-products (ISOP) representations.
5+
6+
.. doc_brief_table::
7+
isop
8+
9+

docs/operations.rst

+8-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ Predicates
1919
.. doc_brief_table::
2020
equal
2121
less_than
22+
has_var
23+
is_const0
2224

23-
Truth table combination and manipulation
24-
----------------------------------------
25+
Combination and manipulation
26+
----------------------------
2527

2628
.. doc_brief_table::
2729
unary_not
@@ -32,6 +34,10 @@ Truth table combination and manipulation
3234
ternary_majority
3335
next_inplace
3436
next
37+
cofactor0_inplace
38+
cofactor0
39+
cofactor1_inplace
40+
cofactor1
3541
swap_inplace
3642
swap
3743
swap_adjacent_inplace

docs/operators.rst

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ The header ``<kitty/operators.hpp>`` implements operator shortcuts to operations
55

66
.. doc_brief_table::
77
operator~
8+
operator&
9+
operator&=
10+
operator|
11+
operator|=
12+
operator^
13+
operator^=
814
operator==
915
operator<
1016

docs/reference.rst

+5
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,8 @@ Canonization
4646

4747
.. doxygenfile:: canonization.hpp
4848

49+
ISOP representations
50+
--------------------
51+
52+
.. doxygenfile:: isop.hpp
53+

include/kitty/constructors.hpp

+51
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include "detail/constants.hpp"
3333
#include "operations.hpp"
34+
#include "operators.hpp"
3435
#include "static_truth_table.hpp"
3536

3637
namespace kitty
@@ -200,6 +201,56 @@ void create_random( TT& tt )
200201
create_random( tt, std::chrono::system_clock::now().time_since_epoch().count() );
201202
}
202203

204+
/*! \brief Creates truth table from cubes representation
205+
206+
A sum-of-product is represented as a vector of products (called
207+
cubes). A product is represented as bit-mask, where odd indexes
208+
correspond to negative literals, and positive indexes corrspond to
209+
positive literals. As an example the cube `38` which is `100110` in
210+
binary represents the product \f$ x_2\bar x_1x_0 \f$.
211+
212+
An empty truth table is given as first argument to determine type
213+
and number of variables. Literals in products that do not fit the
214+
number of variables of the truth table are ignored.
215+
216+
Since product terms are represented as 64-bit bit-masks, a cube
217+
representation only allows truth table sizes up to 32 variables.
218+
219+
\param tt Truth table
220+
\param cubes Vector of cubes
221+
*/
222+
template<typename TT>
223+
void create_from_cubes( TT& tt, const std::vector<uint64_t>& cubes )
224+
{
225+
/* we collect product terms for an SOP, start with const0 */
226+
clear( tt );
227+
228+
for ( auto cube : cubes )
229+
{
230+
auto product = ~tt.construct(); /* const1 of same size */
231+
232+
for ( auto i = 0; i < tt.num_vars(); ++i )
233+
{
234+
if ( cube & 1 ) /* negative literal */
235+
{
236+
auto var = tt.construct();
237+
create_nth_var( var, i, true );
238+
product &= var;
239+
}
240+
cube >>= 1;
241+
if ( cube & 1 ) /* positive literal */
242+
{
243+
auto var = tt.construct();
244+
create_nth_var( var, i );
245+
product &= var;
246+
}
247+
cube >>= 1;
248+
}
249+
250+
tt |= product;
251+
}
252+
}
253+
203254
/*! \brief Constructs majority-n function
204255
205256
The number of variables is determined from the truth table.

include/kitty/detail/constants.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ namespace detail
3737

3838
static constexpr uint64_t projections[] = {0xaaaaaaaaaaaaaaaa, 0xcccccccccccccccc, 0xf0f0f0f0f0f0f0f0, 0xff00ff00ff00ff00, 0xffff0000ffff0000, 0xffffffff00000000};
3939

40+
static constexpr uint64_t projections_neg[] = {0x5555555555555555, 0x3333333333333333, 0x0f0f0f0f0f0f0f0f, 0x00ff00ff00ff00ff, 0x0000ffff0000ffff, 0x00000000ffffffff};
41+
4042
static constexpr uint64_t masks[] = {0x0000000000000001, 0x0000000000000003, 0x000000000000000f, 0x00000000000000ff, 0x000000000000ffff, 0x00000000ffffffff, 0xffffffffffffffff};
4143

4244
static constexpr uint64_t permutation_masks[][3] = {

include/kitty/isop.hpp

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
}

include/kitty/kitty.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "bit_operations.hpp"
3232
#include "canonization.hpp"
3333
#include "constructors.hpp"
34+
#include "isop.hpp"
3435
#include "operations.hpp"
3536
#include "operators.hpp"
3637

0 commit comments

Comments
 (0)