ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
kitBdd.c
Go to the documentation of this file.
1
20
21#include "kit.h"
22
23#ifdef ABC_USE_CUDD
24#include "bdd/extrab/extraBdd.h"
25#endif
26
28
29
33
37
38#ifdef ABC_USE_CUDD
39
51DdNode * Kit_SopToBdd( DdManager * dd, Kit_Sop_t * cSop, int nVars )
52{
53 DdNode * bSum, * bCube, * bTemp, * bVar;
54 unsigned uCube;
55 int Value, i, v;
56 assert( nVars < 16 );
57 // start the cover
58 bSum = Cudd_ReadLogicZero(dd); Cudd_Ref( bSum );
59 // check the logic function of the node
60 Kit_SopForEachCube( cSop, uCube, i )
61 {
62 bCube = Cudd_ReadOne(dd); Cudd_Ref( bCube );
63 for ( v = 0; v < nVars; v++ )
64 {
65 Value = ((uCube >> 2*v) & 3);
66 if ( Value == 1 )
67 bVar = Cudd_Not( Cudd_bddIthVar( dd, v ) );
68 else if ( Value == 2 )
69 bVar = Cudd_bddIthVar( dd, v );
70 else
71 continue;
72 bCube = Cudd_bddAnd( dd, bTemp = bCube, bVar ); Cudd_Ref( bCube );
73 Cudd_RecursiveDeref( dd, bTemp );
74 }
75 bSum = Cudd_bddOr( dd, bTemp = bSum, bCube );
76 Cudd_Ref( bSum );
77 Cudd_RecursiveDeref( dd, bTemp );
78 Cudd_RecursiveDeref( dd, bCube );
79 }
80 // complement the result if necessary
81 Cudd_Deref( bSum );
82 return bSum;
83}
84
96DdNode * Kit_GraphToBdd( DdManager * dd, Kit_Graph_t * pGraph )
97{
98 DdNode * bFunc, * bFunc0, * bFunc1;
99 Kit_Node_t * pNode = NULL; // Suppress "might be used uninitialized"
100 int i;
101
102 // sanity checks
103 assert( Kit_GraphLeaveNum(pGraph) >= 0 );
104 assert( Kit_GraphLeaveNum(pGraph) <= pGraph->nSize );
105
106 // check for constant function
107 if ( Kit_GraphIsConst(pGraph) )
108 return Cudd_NotCond( b1, Kit_GraphIsComplement(pGraph) );
109 // check for a literal
110 if ( Kit_GraphIsVar(pGraph) )
111 return Cudd_NotCond( Cudd_bddIthVar(dd, Kit_GraphVarInt(pGraph)), Kit_GraphIsComplement(pGraph) );
112
113 // assign the elementary variables
114 Kit_GraphForEachLeaf( pGraph, pNode, i )
115 pNode->pFunc = Cudd_bddIthVar( dd, i );
116
117 // compute the function for each internal node
118 Kit_GraphForEachNode( pGraph, pNode, i )
119 {
120 bFunc0 = Cudd_NotCond( Kit_GraphNode(pGraph, pNode->eEdge0.Node)->pFunc, pNode->eEdge0.fCompl );
121 bFunc1 = Cudd_NotCond( Kit_GraphNode(pGraph, pNode->eEdge1.Node)->pFunc, pNode->eEdge1.fCompl );
122 pNode->pFunc = Cudd_bddAnd( dd, bFunc0, bFunc1 ); Cudd_Ref( (DdNode *)pNode->pFunc );
123 }
124
125 // deref the intermediate results
126 bFunc = (DdNode *)pNode->pFunc; Cudd_Ref( bFunc );
127 Kit_GraphForEachNode( pGraph, pNode, i )
128 Cudd_RecursiveDeref( dd, (DdNode *)pNode->pFunc );
129 Cudd_Deref( bFunc );
130
131 // complement the result if necessary
132 return Cudd_NotCond( bFunc, Kit_GraphIsComplement(pGraph) );
133}
134
146DdNode * Kit_TruthToBdd_rec( DdManager * dd, unsigned * pTruth, int iBit, int nVars, int nVarsTotal, int fMSBonTop )
147{
148 DdNode * bF0, * bF1, * bF;
149 int Var;
150 if ( nVars <= 5 )
151 {
152 unsigned uTruth, uMask;
153 uMask = ((~(unsigned)0) >> (32 - (1<<nVars)));
154 uTruth = (pTruth[iBit>>5] >> (iBit&31)) & uMask;
155 if ( uTruth == 0 )
156 return b0;
157 if ( uTruth == uMask )
158 return b1;
159 }
160 // find the variable to use
161 Var = fMSBonTop? nVarsTotal-nVars : nVars-1;
162 // other special cases can be added
163 bF0 = Kit_TruthToBdd_rec( dd, pTruth, iBit, nVars-1, nVarsTotal, fMSBonTop ); Cudd_Ref( bF0 );
164 bF1 = Kit_TruthToBdd_rec( dd, pTruth, iBit+(1<<(nVars-1)), nVars-1, nVarsTotal, fMSBonTop ); Cudd_Ref( bF1 );
165 bF = Cudd_bddIte( dd, dd->vars[Var], bF1, bF0 ); Cudd_Ref( bF );
166 Cudd_RecursiveDeref( dd, bF0 );
167 Cudd_RecursiveDeref( dd, bF1 );
168 Cudd_Deref( bF );
169 return bF;
170}
171
187DdNode * Kit_TruthToBdd( DdManager * dd, unsigned * pTruth, int nVars, int fMSBonTop )
188{
189 return Kit_TruthToBdd_rec( dd, pTruth, 0, nVars, nVars, fMSBonTop );
190}
191
203int Kit_SopFactorVerify( Vec_Int_t * vCover, Kit_Graph_t * pFForm, int nVars )
204{
205 static DdManager * dd = NULL;
206 Kit_Sop_t Sop, * cSop = &Sop;
207 DdNode * bFunc1, * bFunc2;
208 Vec_Int_t * vMemory;
209 int RetValue;
210 // get the manager
211 if ( dd == NULL )
212 dd = Cudd_Init( 16, 0, CUDD_UNIQUE_SLOTS, CUDD_CACHE_SLOTS, 0 );
213 // derive SOP
214 vMemory = Vec_IntAlloc( Vec_IntSize(vCover) );
215 Kit_SopCreate( cSop, vCover, nVars, vMemory );
216 // get the functions
217 bFunc1 = Kit_SopToBdd( dd, cSop, nVars ); Cudd_Ref( bFunc1 );
218 bFunc2 = Kit_GraphToBdd( dd, pFForm ); Cudd_Ref( bFunc2 );
219//Extra_bddPrint( dd, bFunc1 ); printf("\n");
220//Extra_bddPrint( dd, bFunc2 ); printf("\n");
221 RetValue = (bFunc1 == bFunc2);
222 if ( bFunc1 != bFunc2 )
223 {
224 int s;
225 Extra_bddPrint( dd, bFunc1 ); printf("\n");
226 Extra_bddPrint( dd, bFunc2 ); printf("\n");
227 s = 0;
228 }
229 Cudd_RecursiveDeref( dd, bFunc1 );
230 Cudd_RecursiveDeref( dd, bFunc2 );
231 Vec_IntFree( vMemory );
232 return RetValue;
233}
234
235#endif
236
240
241
243
#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
#define b0
Definition bbrImage.c:96
#define b1
Definition bbrImage.c:97
int Var
Definition exorList.c:228
void Extra_bddPrint(DdManager *dd, DdNode *F)
int Kit_SopFactorVerify(Vec_Int_t *cSop, Kit_Graph_t *pFForm, int nVars)
#define Kit_GraphForEachNode(pGraph, pAnd, i)
Definition kit.h:507
struct Kit_Graph_t_ Kit_Graph_t
Definition kit.h:88
void Kit_SopCreate(Kit_Sop_t *cResult, Vec_Int_t *vInput, int nVars, Vec_Int_t *vMemory)
DECLARATIONS ///.
Definition kitSop.c:45
typedefABC_NAMESPACE_HEADER_START struct Kit_Sop_t_ Kit_Sop_t
INCLUDES ///.
Definition kit.h:54
struct Kit_Node_t_ Kit_Node_t
Definition kit.h:69
#define Kit_SopForEachCube(cSop, uCube, i)
ITERATORS ///.
Definition kit.h:500
#define Kit_GraphForEachLeaf(pGraph, pLeaf, i)
Definition kit.h:505
unsigned fCompl
Definition kit.h:65
unsigned Node
Definition kit.h:66
int nSize
Definition kit.h:93
Kit_Edge_t eEdge0
Definition kit.h:72
Kit_Edge_t eEdge1
Definition kit.h:73
void * pFunc
Definition kit.h:76
#define assert(ex)
Definition util_old.h:213