Skip to content

Commit 852b892

Browse files
authored
Merge pull request #48 from msoeken/chow
Boolean function property checks.
2 parents 7f64766 + ee3f93f commit 852b892

File tree

6 files changed

+273
-0
lines changed

6 files changed

+273
-0
lines changed

docs/changelog.rst

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ v0.4 (not yet released)
2525
* Generate implicants and prime implicants: ``get_minterms``, ``get_jbuddies``, ``get_prime_implicants_morreale``
2626
`#46 <https://github.com/msoeken/kitty/pull/46>`_
2727

28+
* Function properties: ``chow_parameters``, ``is_canalizing``, ``is_horn``, ``is_krom``
29+
`#48 <https://github.com/msoeken/kitty/pull/48>`_
30+
2831
v0.3 (February 25, 2018)
2932
------------------------
3033

docs/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Welcome to kitty's documentation!
2525
constructors
2626
operations
2727
operators
28+
properties
2829
print
2930
hash
3031
implicant

docs/properties.rst

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Properties
2+
==========
3+
4+
The header ``<kitty/properties.hpp>`` implements functions compute function
5+
properties.
6+
7+
.. doc_brief_table::
8+
chow_parameters
9+
is_canalizing
10+
is_horn
11+
is_krom

include/kitty/kitty.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "operators.hpp"
5151
#include "permutation.hpp"
5252
#include "print.hpp"
53+
#include "properties.hpp"
5354
#include "spectral.hpp"
5455

5556
/*

include/kitty/properties.hpp

+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/* kitty: C++ truth table library
2+
* Copyright (C) 2017-2018 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+
/*!
27+
\file properties.hpp
28+
\brief Implements property checks for Boolean function
29+
30+
\author Mathias Soeken
31+
*/
32+
33+
#pragma once
34+
35+
#include <cinttypes>
36+
#include <utility>
37+
#include <vector>
38+
39+
#include "bit_operations.hpp"
40+
41+
namespace kitty
42+
{
43+
44+
/*! \brief Returns the Chow parameter of a function
45+
46+
The Chow parameters is a set of values \f$N(f), \Sigma(f)\f$, where \f$N(f)\f$
47+
is the size of the ON-set, and \f$\Sigma(f)\f$ is the sum of all input
48+
assignments in the ON-set. For example for \f$f = x_1 \lor x_2\f$ the
49+
function returns \f$(3, (2,2))\f$.
50+
51+
\param tt Truth table
52+
*/
53+
template<typename TT>
54+
std::pair<uint32_t, std::vector<uint32_t>> chow_parameters( const TT& tt )
55+
{
56+
assert( tt.num_vars() <= 32 );
57+
58+
const auto n = tt.num_vars();
59+
const auto nf = count_ones( tt );
60+
61+
std::vector<uint32_t> sf( n, 0u );
62+
for_each_one_bit( tt, [n, &sf]( auto minterm ) {
63+
for ( auto i = 0u; minterm; ++i )
64+
{
65+
if ( minterm & 1 )
66+
{
67+
++sf[i];
68+
}
69+
minterm >>= 1;
70+
}
71+
} );
72+
73+
return {nf, sf};
74+
}
75+
76+
/*! \brief Checks whether a function is canalizing
77+
78+
\param tt Truth table
79+
*/
80+
template<typename TT>
81+
bool is_canalizing( const TT& tt )
82+
{
83+
uint32_t f1or{}, f0or{};
84+
uint32_t f1and, f0and;
85+
86+
uint32_t max = static_cast<uint32_t>( ( uint64_t( 1 ) << tt.num_vars() ) - 1 );
87+
f1and = f0and = max;
88+
89+
for ( uint32_t i = 0u; i < tt.num_bits(); ++i )
90+
{
91+
if ( get_bit( tt, i ) == 0 )
92+
{
93+
f0and &= i;
94+
f0or |= i;
95+
}
96+
else
97+
{
98+
f1and &= i;
99+
f1or |= i;
100+
}
101+
102+
if ( f0and == 0 && f1and == 0 && f0or == max && f1or == max )
103+
{
104+
return false;
105+
}
106+
}
107+
108+
return true;
109+
}
110+
111+
/*! \brief Checks whether a function is Horn
112+
113+
A function is Horn, if it can be represented using Horn clauses.
114+
115+
\param tt Truth table
116+
*/
117+
template<typename TT>
118+
bool is_horn( const TT& tt )
119+
{
120+
for ( uint32_t i = 1u; i < tt.num_bits(); ++i )
121+
{
122+
for ( uint32_t j = 0u; j < i; ++j )
123+
{
124+
if ( get_bit( tt, j ) && get_bit( tt, i ) && !get_bit( tt, i & j ) )
125+
{
126+
return false;
127+
}
128+
}
129+
}
130+
131+
return true;
132+
}
133+
134+
/*! \brief Checks whether a function is Krom
135+
136+
A function is Krom, if it can be represented using Krom clauses.
137+
138+
\param tt Truth table
139+
*/
140+
template<typename TT>
141+
bool is_krom( const TT& tt )
142+
{
143+
for ( uint32_t i = 2u; i < tt.num_bits(); ++i )
144+
{
145+
for ( uint32_t j = 1u; j < i; ++j )
146+
{
147+
for ( uint32_t k = 0u; k < j; ++k )
148+
{
149+
const auto maj = ( i & j ) | ( i & k ) | ( j & k );
150+
if ( get_bit( tt, k ) && get_bit( tt, j ) && get_bit( tt, i ) && !get_bit( tt, maj ) )
151+
{
152+
return false;
153+
}
154+
}
155+
}
156+
}
157+
158+
return true;
159+
}
160+
161+
} // namespace kitty

test/properties.cpp

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/* kitty: C++ truth table library
2+
* Copyright (C) 2017-2018 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+
#include <gtest/gtest.h>
27+
28+
#include <kitty/properties.hpp>
29+
30+
#include "utility.hpp"
31+
32+
using namespace kitty;
33+
34+
class PropertiesTest : public kitty::testing::Test
35+
{
36+
};
37+
38+
TEST_F( PropertiesTest, chow_small_example )
39+
{
40+
auto r = chow_parameters( from_hex<2>( "e" ) );
41+
EXPECT_EQ( r.first, 3u );
42+
EXPECT_EQ( r.second.size(), 2u );
43+
EXPECT_EQ( r.second[0u], 2u );
44+
EXPECT_EQ( r.second[1u], 2u );
45+
}
46+
47+
TEST_F( PropertiesTest, is_canalizing )
48+
{
49+
static_truth_table<4> tt;
50+
uint32_t counter{};
51+
52+
do {
53+
if ( is_canalizing( tt ) )
54+
{
55+
++counter;
56+
}
57+
58+
next_inplace( tt );
59+
} while ( !is_const0( tt ) );
60+
61+
EXPECT_EQ( counter, 3514u );
62+
}
63+
64+
TEST_F( PropertiesTest, is_horn )
65+
{
66+
static_truth_table<4> tt;
67+
uint32_t counter{};
68+
69+
do {
70+
if ( is_horn( tt ) )
71+
{
72+
++counter;
73+
}
74+
75+
next_inplace( tt );
76+
} while ( !is_const0( tt ) );
77+
78+
EXPECT_EQ( counter, 4960u );
79+
}
80+
81+
TEST_F( PropertiesTest, is_krom )
82+
{
83+
static_truth_table<4> tt;
84+
uint32_t counter{};
85+
86+
do {
87+
if ( is_krom( tt ) )
88+
{
89+
++counter;
90+
}
91+
92+
next_inplace( tt );
93+
} while ( !is_const0( tt ) );
94+
95+
EXPECT_EQ( counter, 4170u );
96+
}

0 commit comments

Comments
 (0)