abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
kitBdd.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [kitBdd.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [Computation kit.]
8 
9  Synopsis [Procedures involving BDDs.]
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - Dec 6, 2006.]
16 
17  Revision [$Id: kitBdd.c,v 1.00 2006/12/06 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "kit.h"
22 #include "misc/extra/extraBdd.h"
23 
25 
26 
27 ////////////////////////////////////////////////////////////////////////
28 /// DECLARATIONS ///
29 ////////////////////////////////////////////////////////////////////////
30 
31 ////////////////////////////////////////////////////////////////////////
32 /// FUNCTION DEFINITIONS ///
33 ////////////////////////////////////////////////////////////////////////
34 
35 /**Function*************************************************************
36 
37  Synopsis [Derives the BDD for the given SOP.]
38 
39  Description []
40 
41  SideEffects []
42 
43  SeeAlso []
44 
45 ***********************************************************************/
46 DdNode * Kit_SopToBdd( DdManager * dd, Kit_Sop_t * cSop, int nVars )
47 {
48  DdNode * bSum, * bCube, * bTemp, * bVar;
49  unsigned uCube;
50  int Value, i, v;
51  assert( nVars < 16 );
52  // start the cover
53  bSum = Cudd_ReadLogicZero(dd); Cudd_Ref( bSum );
54  // check the logic function of the node
55  Kit_SopForEachCube( cSop, uCube, i )
56  {
57  bCube = Cudd_ReadOne(dd); Cudd_Ref( bCube );
58  for ( v = 0; v < nVars; v++ )
59  {
60  Value = ((uCube >> 2*v) & 3);
61  if ( Value == 1 )
62  bVar = Cudd_Not( Cudd_bddIthVar( dd, v ) );
63  else if ( Value == 2 )
64  bVar = Cudd_bddIthVar( dd, v );
65  else
66  continue;
67  bCube = Cudd_bddAnd( dd, bTemp = bCube, bVar ); Cudd_Ref( bCube );
68  Cudd_RecursiveDeref( dd, bTemp );
69  }
70  bSum = Cudd_bddOr( dd, bTemp = bSum, bCube );
71  Cudd_Ref( bSum );
72  Cudd_RecursiveDeref( dd, bTemp );
73  Cudd_RecursiveDeref( dd, bCube );
74  }
75  // complement the result if necessary
76  Cudd_Deref( bSum );
77  return bSum;
78 }
79 
80 /**Function*************************************************************
81 
82  Synopsis [Converts graph to BDD.]
83 
84  Description []
85 
86  SideEffects []
87 
88  SeeAlso []
89 
90 ***********************************************************************/
92 {
93  DdNode * bFunc, * bFunc0, * bFunc1;
94  Kit_Node_t * pNode = NULL; // Suppress "might be used uninitialized"
95  int i;
96 
97  // sanity checks
98  assert( Kit_GraphLeaveNum(pGraph) >= 0 );
99  assert( Kit_GraphLeaveNum(pGraph) <= pGraph->nSize );
100 
101  // check for constant function
102  if ( Kit_GraphIsConst(pGraph) )
103  return Cudd_NotCond( b1, Kit_GraphIsComplement(pGraph) );
104  // check for a literal
105  if ( Kit_GraphIsVar(pGraph) )
106  return Cudd_NotCond( Cudd_bddIthVar(dd, Kit_GraphVarInt(pGraph)), Kit_GraphIsComplement(pGraph) );
107 
108  // assign the elementary variables
109  Kit_GraphForEachLeaf( pGraph, pNode, i )
110  pNode->pFunc = Cudd_bddIthVar( dd, i );
111 
112  // compute the function for each internal node
113  Kit_GraphForEachNode( pGraph, pNode, i )
114  {
115  bFunc0 = Cudd_NotCond( Kit_GraphNode(pGraph, pNode->eEdge0.Node)->pFunc, pNode->eEdge0.fCompl );
116  bFunc1 = Cudd_NotCond( Kit_GraphNode(pGraph, pNode->eEdge1.Node)->pFunc, pNode->eEdge1.fCompl );
117  pNode->pFunc = Cudd_bddAnd( dd, bFunc0, bFunc1 ); Cudd_Ref( (DdNode *)pNode->pFunc );
118  }
119 
120  // deref the intermediate results
121  bFunc = (DdNode *)pNode->pFunc; Cudd_Ref( bFunc );
122  Kit_GraphForEachNode( pGraph, pNode, i )
123  Cudd_RecursiveDeref( dd, (DdNode *)pNode->pFunc );
124  Cudd_Deref( bFunc );
125 
126  // complement the result if necessary
127  return Cudd_NotCond( bFunc, Kit_GraphIsComplement(pGraph) );
128 }
129 
130 /**Function*************************************************************
131 
132  Synopsis []
133 
134  Description []
135 
136  SideEffects []
137 
138  SeeAlso []
139 
140 ***********************************************************************/
141 DdNode * Kit_TruthToBdd_rec( DdManager * dd, unsigned * pTruth, int iBit, int nVars, int nVarsTotal, int fMSBonTop )
142 {
143  DdNode * bF0, * bF1, * bF;
144  int Var;
145  if ( nVars <= 5 )
146  {
147  unsigned uTruth, uMask;
148  uMask = ((~(unsigned)0) >> (32 - (1<<nVars)));
149  uTruth = (pTruth[iBit>>5] >> (iBit&31)) & uMask;
150  if ( uTruth == 0 )
151  return b0;
152  if ( uTruth == uMask )
153  return b1;
154  }
155  // find the variable to use
156  Var = fMSBonTop? nVarsTotal-nVars : nVars-1;
157  // other special cases can be added
158  bF0 = Kit_TruthToBdd_rec( dd, pTruth, iBit, nVars-1, nVarsTotal, fMSBonTop ); Cudd_Ref( bF0 );
159  bF1 = Kit_TruthToBdd_rec( dd, pTruth, iBit+(1<<(nVars-1)), nVars-1, nVarsTotal, fMSBonTop ); Cudd_Ref( bF1 );
160  bF = Cudd_bddIte( dd, dd->vars[Var], bF1, bF0 ); Cudd_Ref( bF );
161  Cudd_RecursiveDeref( dd, bF0 );
162  Cudd_RecursiveDeref( dd, bF1 );
163  Cudd_Deref( bF );
164  return bF;
165 }
166 
167 /**Function*************************************************************
168 
169  Synopsis [Compute BDD corresponding to the truth table.]
170 
171  Description [If truth table has N vars, the BDD depends on N topmost
172  variables of the BDD manager. The most significant variable of the table
173  is encoded by the topmost variable of the manager. BDD construction is
174  efficient in this case because BDD is constructed one node at a time,
175  by simply adding BDD nodes on top of existent BDD nodes.]
176 
177  SideEffects []
178 
179  SeeAlso []
180 
181 ***********************************************************************/
182 DdNode * Kit_TruthToBdd( DdManager * dd, unsigned * pTruth, int nVars, int fMSBonTop )
183 {
184  return Kit_TruthToBdd_rec( dd, pTruth, 0, nVars, nVars, fMSBonTop );
185 }
186 
187 /**Function*************************************************************
188 
189  Synopsis [Verifies that the factoring is correct.]
190 
191  Description []
192 
193  SideEffects []
194 
195  SeeAlso []
196 
197 ***********************************************************************/
198 int Kit_SopFactorVerify( Vec_Int_t * vCover, Kit_Graph_t * pFForm, int nVars )
199 {
200  static DdManager * dd = NULL;
201  Kit_Sop_t Sop, * cSop = &Sop;
202  DdNode * bFunc1, * bFunc2;
203  Vec_Int_t * vMemory;
204  int RetValue;
205  // get the manager
206  if ( dd == NULL )
207  dd = Cudd_Init( 16, 0, CUDD_UNIQUE_SLOTS, CUDD_CACHE_SLOTS, 0 );
208  // derive SOP
209  vMemory = Vec_IntAlloc( Vec_IntSize(vCover) );
210  Kit_SopCreate( cSop, vCover, nVars, vMemory );
211  // get the functions
212  bFunc1 = Kit_SopToBdd( dd, cSop, nVars ); Cudd_Ref( bFunc1 );
213  bFunc2 = Kit_GraphToBdd( dd, pFForm ); Cudd_Ref( bFunc2 );
214 //Extra_bddPrint( dd, bFunc1 ); printf("\n");
215 //Extra_bddPrint( dd, bFunc2 ); printf("\n");
216  RetValue = (bFunc1 == bFunc2);
217  if ( bFunc1 != bFunc2 )
218  {
219  int s;
220  Extra_bddPrint( dd, bFunc1 ); printf("\n");
221  Extra_bddPrint( dd, bFunc2 ); printf("\n");
222  s = 0;
223  }
224  Cudd_RecursiveDeref( dd, bFunc1 );
225  Cudd_RecursiveDeref( dd, bFunc2 );
226  Vec_IntFree( vMemory );
227  return RetValue;
228 }
229 
230 ////////////////////////////////////////////////////////////////////////
231 /// END OF FILE ///
232 ////////////////////////////////////////////////////////////////////////
233 
234 
236 
Kit_Edge_t eEdge0
Definition: kit.h:69
#define CUDD_UNIQUE_SLOTS
Definition: cudd.h:97
void Cudd_RecursiveDeref(DdManager *table, DdNode *n)
Definition: cuddRef.c:154
Definition: cudd.h:278
#define Cudd_Not(node)
Definition: cudd.h:367
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
Definition: bblif.c:37
static Kit_Node_t * Kit_GraphNode(Kit_Graph_t *pGraph, int i)
Definition: kit.h:211
void Cudd_Deref(DdNode *node)
Definition: cuddRef.c:438
Kit_Edge_t eEdge1
Definition: kit.h:70
#define b1
Definition: extraBdd.h:76
DdNode * Cudd_ReadLogicZero(DdManager *dd)
Definition: cuddAPI.c:1058
static int Kit_GraphIsComplement(Kit_Graph_t *pGraph)
Definition: kit.h:205
static int Kit_GraphLeaveNum(Kit_Graph_t *pGraph)
Definition: kit.h:209
int nSize
Definition: kit.h:90
#define CUDD_CACHE_SLOTS
Definition: cudd.h:98
static int Kit_GraphIsVar(Kit_Graph_t *pGraph)
Definition: kit.h:206
DdNode * Cudd_bddIte(DdManager *dd, DdNode *f, DdNode *g, DdNode *h)
Definition: cuddBddIte.c:143
DdNode * Kit_GraphToBdd(DdManager *dd, Kit_Graph_t *pGraph)
Definition: kitBdd.c:91
#define Kit_GraphForEachLeaf(pGraph, pLeaf, i)
Definition: kit.h:502
void * pFunc
Definition: kit.h:73
static int Kit_GraphIsConst(Kit_Graph_t *pGraph)
Definition: kit.h:202
static Vec_Int_t * Vec_IntAlloc(int nCap)
FUNCTION DEFINITIONS ///.
Definition: bblif.c:149
ABC_NAMESPACE_IMPL_START DdNode * Kit_SopToBdd(DdManager *dd, Kit_Sop_t *cSop, int nVars)
DECLARATIONS ///.
Definition: kitBdd.c:46
unsigned fCompl
Definition: kit.h:62
#define Kit_GraphForEachNode(pGraph, pAnd, i)
Definition: kit.h:504
void Extra_bddPrint(DdManager *dd, DdNode *F)
Definition: extraBddMisc.c:246
unsigned Node
Definition: kit.h:63
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
DdNode * Kit_TruthToBdd(DdManager *dd, unsigned *pTruth, int nVars, int fMSBonTop)
Definition: kitBdd.c:182
DdManager * Cudd_Init(unsigned int numVars, unsigned int numVarsZ, unsigned int numSlots, unsigned int cacheSize, unsigned long maxMemory)
Definition: cuddInit.c:125
DdNode * Cudd_bddOr(DdManager *dd, DdNode *f, DdNode *g)
Definition: cuddBddIte.c:381
#define Kit_SopForEachCube(cSop, uCube, i)
ITERATORS ///.
Definition: kit.h:497
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
DdNode * Cudd_ReadOne(DdManager *dd)
Definition: cuddAPI.c:987
typedefABC_NAMESPACE_HEADER_START struct Kit_Sop_t_ Kit_Sop_t
INCLUDES ///.
Definition: kit.h:51
static int Vec_IntSize(Vec_Int_t *p)
Definition: bblif.c:252
#define b0
Definition: extraBdd.h:75
DdNode ** vars
Definition: cuddInt.h:390
DdNode * Cudd_bddIthVar(DdManager *dd, int i)
Definition: cuddAPI.c:416
int Var
Definition: SolverTypes.h:42
#define Cudd_NotCond(node, c)
Definition: cudd.h:383
DdNode * Cudd_bddAnd(DdManager *dd, DdNode *f, DdNode *g)
Definition: cuddBddIte.c:314
#define assert(ex)
Definition: util_old.h:213
static int Kit_GraphVarInt(Kit_Graph_t *pGraph)
Definition: kit.h:216
void Cudd_Ref(DdNode *n)
Definition: cuddRef.c:129
static void Vec_IntFree(Vec_Int_t *p)
Definition: bblif.c:235
DdNode * Kit_TruthToBdd_rec(DdManager *dd, unsigned *pTruth, int iBit, int nVars, int nVarsTotal, int fMSBonTop)
Definition: kitBdd.c:141
void Kit_SopCreate(Kit_Sop_t *cResult, Vec_Int_t *vInput, int nVars, Vec_Int_t *vMemory)
DECLARATIONS ///.
Definition: kitSop.c:45
int Kit_SopFactorVerify(Vec_Int_t *vCover, Kit_Graph_t *pFForm, int nVars)
Definition: kitBdd.c:198