ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
kitGraph.c
Go to the documentation of this file.
1
20
21#include "kit.h"
22#include "misc/extra/extra.h"
23
25
26
30
34
47{
48 Kit_Graph_t * pGraph;
49 pGraph = ABC_ALLOC( Kit_Graph_t, 1 );
50 memset( pGraph, 0, sizeof(Kit_Graph_t) );
51 pGraph->nLeaves = nLeaves;
52 pGraph->nSize = nLeaves;
53 pGraph->nCap = 2 * nLeaves + 50;
54 pGraph->pNodes = ABC_ALLOC( Kit_Node_t, pGraph->nCap );
55 memset( pGraph->pNodes, 0, sizeof(Kit_Node_t) * pGraph->nSize );
56 return pGraph;
57}
58
71{
72 Kit_Graph_t * pGraph;
73 pGraph = ABC_ALLOC( Kit_Graph_t, 1 );
74 memset( pGraph, 0, sizeof(Kit_Graph_t) );
75 pGraph->fConst = 1;
76 pGraph->eRoot.fCompl = 1;
77 return pGraph;
78}
79
92{
93 Kit_Graph_t * pGraph;
94 pGraph = ABC_ALLOC( Kit_Graph_t, 1 );
95 memset( pGraph, 0, sizeof(Kit_Graph_t) );
96 pGraph->fConst = 1;
97 return pGraph;
98}
99
111Kit_Graph_t * Kit_GraphCreateLeaf( int iLeaf, int nLeaves, int fCompl )
112{
113 Kit_Graph_t * pGraph;
114 assert( 0 <= iLeaf && iLeaf < nLeaves );
115 pGraph = Kit_GraphCreate( nLeaves );
116 pGraph->eRoot.Node = iLeaf;
117 pGraph->eRoot.fCompl = fCompl;
118 return pGraph;
119}
120
132void Kit_GraphFree( Kit_Graph_t * pGraph )
133{
134 ABC_FREE( pGraph->pNodes );
135 ABC_FREE( pGraph );
136}
137
150{
151 Kit_Node_t * pNode;
152 if ( pGraph->nSize == pGraph->nCap )
153 {
154 pGraph->pNodes = ABC_REALLOC( Kit_Node_t, pGraph->pNodes, 2 * pGraph->nCap );
155 pGraph->nCap = 2 * pGraph->nCap;
156 }
157 pNode = pGraph->pNodes + pGraph->nSize++;
158 memset( pNode, 0, sizeof(Kit_Node_t) );
159 return pNode;
160}
161
174{
175 Kit_Node_t * pNode;
176 // get the new node
177 pNode = Kit_GraphAppendNode( pGraph );
178 // set the inputs and other info
179 pNode->eEdge0 = eEdge0;
180 pNode->eEdge1 = eEdge1;
181 pNode->fCompl0 = eEdge0.fCompl;
182 pNode->fCompl1 = eEdge1.fCompl;
183 return Kit_EdgeCreate( pGraph->nSize - 1, 0 );
184}
185
198{
199 Kit_Node_t * pNode;
200 // get the new node
201 pNode = Kit_GraphAppendNode( pGraph );
202 // set the inputs and other info
203 pNode->eEdge0 = eEdge0;
204 pNode->eEdge1 = eEdge1;
205 pNode->fCompl0 = eEdge0.fCompl;
206 pNode->fCompl1 = eEdge1.fCompl;
207 // make adjustments for the OR gate
208 pNode->fNodeOr = 1;
209 pNode->eEdge0.fCompl = !pNode->eEdge0.fCompl;
210 pNode->eEdge1.fCompl = !pNode->eEdge1.fCompl;
211 return Kit_EdgeCreate( pGraph->nSize - 1, 1 );
212}
213
226{
227 Kit_Edge_t eNode0, eNode1, eNode;
228 if ( Type == 0 )
229 {
230 // derive the first AND
231 eEdge0.fCompl ^= 1;
232 eNode0 = Kit_GraphAddNodeAnd( pGraph, eEdge0, eEdge1 );
233 eEdge0.fCompl ^= 1;
234 // derive the second AND
235 eEdge1.fCompl ^= 1;
236 eNode1 = Kit_GraphAddNodeAnd( pGraph, eEdge0, eEdge1 );
237 // derive the final OR
238 eNode = Kit_GraphAddNodeOr( pGraph, eNode0, eNode1 );
239 }
240 else
241 {
242 // derive the first AND
243 eNode0 = Kit_GraphAddNodeAnd( pGraph, eEdge0, eEdge1 );
244 // derive the second AND
245 eEdge0.fCompl ^= 1;
246 eEdge1.fCompl ^= 1;
247 eNode1 = Kit_GraphAddNodeAnd( pGraph, eEdge0, eEdge1 );
248 // derive the final OR
249 eNode = Kit_GraphAddNodeOr( pGraph, eNode0, eNode1 );
250 eNode.fCompl ^= 1;
251 }
252 return eNode;
253}
254
266Kit_Edge_t Kit_GraphAddNodeMux( Kit_Graph_t * pGraph, Kit_Edge_t eEdgeC, Kit_Edge_t eEdgeT, Kit_Edge_t eEdgeE, int Type )
267{
268 Kit_Edge_t eNode0, eNode1, eNode;
269 if ( Type == 0 )
270 {
271 // derive the first AND
272 eNode0 = Kit_GraphAddNodeAnd( pGraph, eEdgeC, eEdgeT );
273 // derive the second AND
274 eEdgeC.fCompl ^= 1;
275 eNode1 = Kit_GraphAddNodeAnd( pGraph, eEdgeC, eEdgeE );
276 // derive the final OR
277 eNode = Kit_GraphAddNodeOr( pGraph, eNode0, eNode1 );
278 }
279 else
280 {
281 // complement the arguments
282 eEdgeT.fCompl ^= 1;
283 eEdgeE.fCompl ^= 1;
284 // derive the first AND
285 eNode0 = Kit_GraphAddNodeAnd( pGraph, eEdgeC, eEdgeT );
286 // derive the second AND
287 eEdgeC.fCompl ^= 1;
288 eNode1 = Kit_GraphAddNodeAnd( pGraph, eEdgeC, eEdgeE );
289 // derive the final OR
290 eNode = Kit_GraphAddNodeOr( pGraph, eNode0, eNode1 );
291 eNode.fCompl ^= 1;
292 }
293 return eNode;
294}
295
307unsigned Kit_GraphToTruth( Kit_Graph_t * pGraph )
308{
309 unsigned uTruths[5] = { 0xAAAAAAAA, 0xCCCCCCCC, 0xF0F0F0F0, 0xFF00FF00, 0xFFFF0000 };
310 unsigned uTruth = 0, uTruth0, uTruth1;
311 Kit_Node_t * pNode;
312 int i;
313
314 // sanity checks
315 assert( Kit_GraphLeaveNum(pGraph) >= 0 );
316 assert( Kit_GraphLeaveNum(pGraph) <= pGraph->nSize );
317 assert( Kit_GraphLeaveNum(pGraph) <= 5 );
318
319 // check for constant function
320 if ( Kit_GraphIsConst(pGraph) )
321 return Kit_GraphIsComplement(pGraph)? 0 : ~((unsigned)0);
322 // check for a literal
323 if ( Kit_GraphIsVar(pGraph) )
324 return Kit_GraphIsComplement(pGraph)? ~uTruths[Kit_GraphVarInt(pGraph)] : uTruths[Kit_GraphVarInt(pGraph)];
325
326 // assign the elementary variables
327 Kit_GraphForEachLeaf( pGraph, pNode, i )
328 pNode->pFunc = (void *)(long)uTruths[i];
329
330 // compute the function for each internal node
331 Kit_GraphForEachNode( pGraph, pNode, i )
332 {
333 uTruth0 = (unsigned)(long)Kit_GraphNode(pGraph, pNode->eEdge0.Node)->pFunc;
334 uTruth1 = (unsigned)(long)Kit_GraphNode(pGraph, pNode->eEdge1.Node)->pFunc;
335 uTruth0 = pNode->eEdge0.fCompl? ~uTruth0 : uTruth0;
336 uTruth1 = pNode->eEdge1.fCompl? ~uTruth1 : uTruth1;
337 uTruth = uTruth0 & uTruth1;
338 pNode->pFunc = (void *)(long)uTruth;
339 }
340
341 // complement the result if necessary
342 return Kit_GraphIsComplement(pGraph)? ~uTruth : uTruth;
343}
344
356Kit_Graph_t * Kit_TruthToGraph( unsigned * pTruth, int nVars, Vec_Int_t * vMemory )
357{
358 Kit_Graph_t * pGraph;
359 int RetValue;
360 // derive SOP
361 RetValue = Kit_TruthIsop( pTruth, nVars, vMemory, 1 ); // tried 1 and found not useful in "renode"
362 if ( RetValue == -1 )
363 return NULL;
364 if ( Vec_IntSize(vMemory) > (1<<16) )
365 return NULL;
366// printf( "Isop size = %d.\n", Vec_IntSize(vMemory) );
367 assert( RetValue == 0 || RetValue == 1 );
368 // derive factored form
369 pGraph = Kit_SopFactor( vMemory, RetValue, nVars, vMemory );
370 return pGraph;
371}
372
384Kit_Graph_t * Kit_TruthToGraph2( unsigned * pTruth0, unsigned * pTruth1, int nVars, Vec_Int_t * vMemory )
385{
386 Kit_Graph_t * pGraph;
387 int RetValue;
388 // derive SOP
389 RetValue = Kit_TruthIsop2( pTruth0, pTruth1, nVars, vMemory, 0, 0 ); // tried 1 and found not useful in "renode"
390 if ( RetValue == -1 )
391 return NULL;
392 if ( Vec_IntSize(vMemory) > (1<<16) )
393 return NULL;
394// printf( "Isop size = %d.\n", Vec_IntSize(vMemory) );
395 assert( RetValue == 0 || RetValue == 1 );
396 // derive factored form
397 pGraph = Kit_SopFactor( vMemory, RetValue, nVars, vMemory );
398 return pGraph;
399}
400
413{
414 int Depth0, Depth1, Depth;
415 if ( pNode == pLeaf )
416 return 0;
417 if ( Kit_GraphNodeIsVar(pGraph, pNode) )
418 return -100;
419 Depth0 = Kit_GraphLeafDepth_rec( pGraph, Kit_GraphNodeFanin0(pGraph, pNode), pLeaf );
420 Depth1 = Kit_GraphLeafDepth_rec( pGraph, Kit_GraphNodeFanin1(pGraph, pNode), pLeaf );
421 Depth = KIT_MAX( Depth0, Depth1 );
422 Depth = (Depth == -100) ? -100 : Depth + 1;
423 return Depth;
424}
425
438{
439 int Depth0, Depth1;
440 if ( Kit_GraphNodeIsVar(pGraph, pNode) )
441 return 0;
442 Depth0 = Kit_GraphLevelNum_rec( pGraph, Kit_GraphNodeFanin0(pGraph, pNode) );
443 Depth1 = Kit_GraphLevelNum_rec( pGraph, Kit_GraphNodeFanin1(pGraph, pNode) );
444 return 1 + KIT_MAX( Depth0, Depth1 );
445}
446
458int Kit_TruthStats( unsigned * pTruth, int nVars, Vec_Int_t * vMemory )
459{
460 Kit_Graph_t * pGraph = Kit_TruthToGraph( pTruth, nVars, vMemory );
461 int nNodes = Kit_GraphNodeNum( pGraph );
462 int nLevels = Kit_GraphLevelNum_rec( pGraph, Kit_GraphNodeLast(pGraph) );
463 Kit_GraphFree( pGraph );
464 return (nLevels << 16) | nNodes;
465}
466int * Kit_TruthStatsArray( unsigned * pArray, int nVars, int nFuncs )
467{
468 int f, * pRes = ABC_CALLOC( int, nFuncs );
469 int nInts = Abc_TruthWordNum( nVars );
470 Vec_Int_t * vMemory = Vec_IntAlloc( 1 << 16 );
471 for ( f = 0; f < nFuncs; f++ )
472 pRes[f] = Kit_TruthStats( pArray + f*nInts, nVars, vMemory );
473 Vec_IntFree( vMemory );
474 return pRes;
475}
476int Kit_TruthFindVarNum( char * pFileName )
477{
478 int i;
479 for ( i = 0; i < (int)strlen(pFileName); i++ )
480 if ( pFileName[i] >= '0' && pFileName[i] <= '9' )
481 return atoi(pFileName+i);
482 return -1;
483}
484int * Kit_TruthTest( char * pFileName )
485{
486 abctime clk = Abc_Clock(); int i;
487 int nFileSize = Extra_FileSize( pFileName );
488 int nVars = Kit_TruthFindVarNum( pFileName );
489 int nFuncs = nFileSize / 4 / Abc_TruthWordNum(nVars);
490 unsigned * pA = (unsigned *)Extra_FileReadContents( pFileName );
491 int * pResult = Kit_TruthStatsArray( pA, nVars, nFuncs );
492 printf( "Finished proceessing %d functions with %d variables. ", nFuncs, nVars );
493 Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
494 ABC_FREE( pA );
495 for ( i = 0; i < 5; i++ )
496 printf( "Function %3d : AND2 = %3d Lev = %3d\n", i, pResult[i] & 0xFFFF, pResult[i] >> 16 );
497 return pResult;
498}
499
511int Kit_TruthLitNum( unsigned * pTruth, int nVars, Vec_Int_t * vMemory )
512{
513 Kit_Graph_t * pGraph;
514 int RetValue, nLits;
515 RetValue = Kit_TruthIsop( pTruth, nVars, vMemory, 1 );
516 if ( RetValue == -1 || Vec_IntSize(vMemory) > (1<<16) )
517 return -1;
518 assert( RetValue == 0 || RetValue == 1 );
519 pGraph = Kit_SopFactor( vMemory, RetValue, nVars, vMemory );
520 nLits = 1 + Kit_GraphNodeNum( pGraph );
521 Kit_GraphFree( pGraph );
522 return nLits;
523}
524
528
530
ABC_INT64_T abctime
Definition abc_global.h:332
#define ABC_ALLOC(type, num)
Definition abc_global.h:264
#define ABC_REALLOC(type, obj, num)
Definition abc_global.h:268
#define ABC_CALLOC(type, num)
Definition abc_global.h:265
#define ABC_FREE(obj)
Definition abc_global.h:267
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
Definition bblif.c:37
int Extra_FileSize(char *pFileName)
char * Extra_FileReadContents(char *pFileName)
int * Kit_TruthStatsArray(unsigned *pArray, int nVars, int nFuncs)
Definition kitGraph.c:466
Kit_Edge_t Kit_GraphAddNodeMux(Kit_Graph_t *pGraph, Kit_Edge_t eEdgeC, Kit_Edge_t eEdgeT, Kit_Edge_t eEdgeE, int Type)
Definition kitGraph.c:266
unsigned Kit_GraphToTruth(Kit_Graph_t *pGraph)
Definition kitGraph.c:307
int * Kit_TruthTest(char *pFileName)
Definition kitGraph.c:484
Kit_Edge_t Kit_GraphAddNodeAnd(Kit_Graph_t *pGraph, Kit_Edge_t eEdge0, Kit_Edge_t eEdge1)
Definition kitGraph.c:173
int Kit_TruthLitNum(unsigned *pTruth, int nVars, Vec_Int_t *vMemory)
Definition kitGraph.c:511
Kit_Graph_t * Kit_GraphCreateConst1()
Definition kitGraph.c:91
Kit_Graph_t * Kit_TruthToGraph2(unsigned *pTruth0, unsigned *pTruth1, int nVars, Vec_Int_t *vMemory)
Definition kitGraph.c:384
int Kit_GraphLevelNum_rec(Kit_Graph_t *pGraph, Kit_Node_t *pNode)
Definition kitGraph.c:437
Kit_Node_t * Kit_GraphAppendNode(Kit_Graph_t *pGraph)
Definition kitGraph.c:149
int Kit_GraphLeafDepth_rec(Kit_Graph_t *pGraph, Kit_Node_t *pNode, Kit_Node_t *pLeaf)
Definition kitGraph.c:412
void Kit_GraphFree(Kit_Graph_t *pGraph)
Definition kitGraph.c:132
int Kit_TruthStats(unsigned *pTruth, int nVars, Vec_Int_t *vMemory)
Definition kitGraph.c:458
Kit_Graph_t * Kit_GraphCreateLeaf(int iLeaf, int nLeaves, int fCompl)
Definition kitGraph.c:111
Kit_Edge_t Kit_GraphAddNodeOr(Kit_Graph_t *pGraph, Kit_Edge_t eEdge0, Kit_Edge_t eEdge1)
Definition kitGraph.c:197
Kit_Edge_t Kit_GraphAddNodeXor(Kit_Graph_t *pGraph, Kit_Edge_t eEdge0, Kit_Edge_t eEdge1, int Type)
Definition kitGraph.c:225
Kit_Graph_t * Kit_GraphCreateConst0()
Definition kitGraph.c:70
ABC_NAMESPACE_IMPL_START Kit_Graph_t * Kit_GraphCreate(int nLeaves)
DECLARATIONS ///.
Definition kitGraph.c:46
Kit_Graph_t * Kit_TruthToGraph(unsigned *pTruth, int nVars, Vec_Int_t *vMemory)
Definition kitGraph.c:356
int Kit_TruthFindVarNum(char *pFileName)
Definition kitGraph.c:476
#define Kit_GraphForEachNode(pGraph, pAnd, i)
Definition kit.h:507
struct Kit_Graph_t_ Kit_Graph_t
Definition kit.h:88
int Kit_TruthIsop(unsigned *puTruth, int nVars, Vec_Int_t *vMemory, int fTryBoth)
Definition kitIsop.c:134
Kit_Graph_t * Kit_SopFactor(Vec_Int_t *vCover, int fCompl, int nVars, Vec_Int_t *vMemory)
FUNCTION DEFINITIONS ///.
Definition kitFactor.c:55
struct Kit_Node_t_ Kit_Node_t
Definition kit.h:69
int Kit_TruthIsop2(unsigned *puTruth0, unsigned *puTruth1, int nVars, Vec_Int_t *vMemory, int fTryBoth, int fReturnTt)
FUNCTION DEFINITIONS ///.
Definition kitIsop.c:55
#define KIT_MAX(a, b)
Definition kit.h:175
#define Kit_GraphForEachLeaf(pGraph, pLeaf, i)
Definition kit.h:505
struct Kit_Edge_t_ Kit_Edge_t
Definition kit.h:62
unsigned fCompl
Definition kit.h:65
unsigned Node
Definition kit.h:66
Kit_Edge_t eRoot
Definition kit.h:96
Kit_Node_t * pNodes
Definition kit.h:95
int fConst
Definition kit.h:91
int nLeaves
Definition kit.h:92
int nCap
Definition kit.h:94
int nSize
Definition kit.h:93
unsigned fNodeOr
Definition kit.h:79
Kit_Edge_t eEdge0
Definition kit.h:72
Kit_Edge_t eEdge1
Definition kit.h:73
void * pFunc
Definition kit.h:76
unsigned fCompl1
Definition kit.h:81
unsigned fCompl0
Definition kit.h:80
#define assert(ex)
Definition util_old.h:213
char * memset()
int strlen()