forked from lsils/lstools-showcase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexactmine.cpp
265 lines (218 loc) · 6.85 KB
/
exactmine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/* lstools-showcase
* Copyright (C) 2018 EPFL
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/*!
\file exactmine.cpp
\brief Mine exact logic networks
\author Mathias Soeken
*/
#include <cmath>
#include <string>
#include <unordered_set>
#include <vector>
#include <alice/alice.hpp>
#include <kitty/kitty.hpp>
#include <lorina/lorina.hpp>
#include <percy/percy.hpp>
class optimum_network
{
public:
optimum_network() = default;
optimum_network( const kitty::dynamic_truth_table& function )
: function( function ) {}
optimum_network( kitty::dynamic_truth_table&& function )
: function( std::move( function ) ) {}
bool exists() const
{
static std::vector<std::unordered_set<kitty::dynamic_truth_table, kitty::hash<kitty::dynamic_truth_table>>> hash;
if ( function.num_vars() >= hash.size() )
{
hash.resize( function.num_vars() + 1 );
}
return !hash[function.num_vars()].insert( function ).second;
}
public: /* field access */
kitty::dynamic_truth_table function{0};
std::string network;
};
namespace alice
{
ALICE_ADD_STORE( optimum_network, "opt", "o", "network", "networks" )
ALICE_DESCRIBE_STORE( optimum_network, opt )
{
if ( opt.network.empty() )
{
return fmt::format( "{}", kitty::to_hex( opt.function ) );
}
else
{
return fmt::format( "{}, optimum network computed", kitty::to_hex( opt.function ) );
}
}
ALICE_PRINT_STORE( optimum_network, os, opt )
{
os << fmt::format( "function (hex): {}\nfunction (bin): {}\n", kitty::to_hex( opt.function ), kitty::to_binary( opt.function ) );
if ( opt.network.empty() )
{
os << "no optimum network computed\n";
}
else
{
os << fmt::format( "optimum network: {}\n", opt.network );
}
}
void add_optimum_network_entry( command& cmd, kitty::dynamic_truth_table& function )
{
if ( cmd.env->variable( "npn" ) != "" )
{
function = std::get<0>( kitty::exact_npn_canonization( function ) );
}
optimum_network entry( function );
if ( !entry.exists() )
{
cmd.store<optimum_network>().extend();
cmd.store<optimum_network>().current() = entry;
}
}
class load_command : public command
{
public:
load_command( const environment::ptr& env ) : command( env, "Load new entry" )
{
add_option( "truth_table,--tt", truth_table, "truth table in hex format" );
add_flag( "--binary,-b", "read truth table as binary string" );
}
protected:
void execute() override
{
auto function = [this]() {
if ( is_set( "binary" ) )
{
const unsigned num_vars = std::log2( truth_table.size() );
kitty::dynamic_truth_table function( num_vars );
kitty::create_from_binary_string( function, truth_table );
return function;
}
else
{
const unsigned num_vars = std::log2( truth_table.size() * 4 );
kitty::dynamic_truth_table function( num_vars );
kitty::create_from_hex_string( function, truth_table );
return function;
}
}();
add_optimum_network_entry( *this, function );
}
private:
std::string truth_table;
};
ALICE_ADD_COMMAND( load, "Loading" );
class load_bench_command : public command
{
public:
load_bench_command( const environment::ptr& env ) : command( env, "Load entries from LUT functions in BENCH file" )
{
add_option( "filename,--filename", filename, "BENCH filename" )->check( ExistingFileWordExp );
add_option( "--threshold,-t", threshold, "skip functions with more than this number of inputs", 4u );
}
class lut_parser : public lorina::bench_reader
{
public:
lut_parser( load_bench_command& cmd ) : cmd( cmd ) {}
void on_gate( const std::vector<std::string>& inputs, const std::string& output, const std::string& type ) const override
{
(void)output;
const auto num_vars = inputs.size();
if ( num_vars > cmd.threshold )
return;
if ( !( type.size() > 2u && type[0] == '0' && type[1] == 'x' ) )
{
cmd.env->out() << "[w] ignore gate '" << type << "'\n";
return;
}
kitty::dynamic_truth_table function( num_vars );
kitty::create_from_hex_string( function, type.substr( 2u ) );
add_optimum_network_entry( cmd, function );
}
private:
load_bench_command& cmd;
};
protected:
void execute() override
{
lorina::read_bench( filename, lut_parser( *this ) );
}
private:
std::string filename;
unsigned threshold = 6u;
};
ALICE_ADD_COMMAND( load_bench, "Loading" );
class find_network_command : public command
{
public:
find_network_command( const environment::ptr& env ) : command( env, "Find optimum network" )
{
add_flag( "--verify", "verifies whether found network matches specification" );
add_flag( "--force,-f", "recompute optimum network if it exists" );
add_flag( "--verbose,-v", "be verbose" );
}
protected:
rules validity_rules() const override
{
return {
has_store_element<optimum_network>( env ),
{[this]() { return store<optimum_network>().current().network.empty() || is_set( "force" ); }, "optimum network already computed (use -f to override)"}};
}
void execute() override
{
auto& opt = store<optimum_network>().current();
percy::spec spec;
spec.verbosity = is_set( "verbose" ) ? 1 : 0;
spec[0] = opt.function;
percy::chain c;
if ( percy::synthesize( spec, c ) != percy::success )
{
env->out() << "[e] could not find optimum network \n";
return;
}
if ( is_set( "verify" ) )
{
if ( c.satisfies_spec( spec ) )
{
env->out() << "[i] synthesized chain matches specification\n";
}
else
{
env->err() << "[e] synthesized chain does not match specification\n";
return;
}
}
std::stringstream str;
c.to_expression( str );
opt.network = str.str();
}
};
ALICE_ADD_COMMAND( find_network, "Exact synthesis" )
} // namespace alice
ALICE_MAIN( exactmine )