abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
cuddZddPort.c
Go to the documentation of this file.
1 /**CFile***********************************************************************
2 
3  FileName [cuddZddPort.c]
4 
5  PackageName [cudd]
6 
7  Synopsis [Functions that translate BDDs to ZDDs.]
8 
9  Description [External procedures included in this module:
10  <ul>
11  <li> Cudd_zddPortFromBdd()
12  <li> Cudd_zddPortToBdd()
13  </ul>
14  Internal procedures included in this module:
15  <ul>
16  </ul>
17  Static procedures included in this module:
18  <ul>
19  <li> zddPortFromBddStep()
20  <li> zddPortToBddStep()
21  </ul>
22  ]
23 
24  SeeAlso []
25 
26  Author [Hyong-kyoon Shin, In-Ho Moon]
27 
28  Copyright [Copyright (c) 1995-2004, Regents of the University of Colorado
29 
30  All rights reserved.
31 
32  Redistribution and use in source and binary forms, with or without
33  modification, are permitted provided that the following conditions
34  are met:
35 
36  Redistributions of source code must retain the above copyright
37  notice, this list of conditions and the following disclaimer.
38 
39  Redistributions in binary form must reproduce the above copyright
40  notice, this list of conditions and the following disclaimer in the
41  documentation and/or other materials provided with the distribution.
42 
43  Neither the name of the University of Colorado nor the names of its
44  contributors may be used to endorse or promote products derived from
45  this software without specific prior written permission.
46 
47  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
48  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
49  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
50  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
51  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
52  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
53  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
54  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
55  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
57  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
58  POSSIBILITY OF SUCH DAMAGE.]
59 
60 ******************************************************************************/
61 
62 #include "misc/util/util_hack.h"
63 #include "cuddInt.h"
64 
66 
67 
68 
69 /*---------------------------------------------------------------------------*/
70 /* Constant declarations */
71 /*---------------------------------------------------------------------------*/
72 
73 
74 /*---------------------------------------------------------------------------*/
75 /* Stucture declarations */
76 /*---------------------------------------------------------------------------*/
77 
78 
79 /*---------------------------------------------------------------------------*/
80 /* Type declarations */
81 /*---------------------------------------------------------------------------*/
82 
83 
84 /*---------------------------------------------------------------------------*/
85 /* Variable declarations */
86 /*---------------------------------------------------------------------------*/
87 
88 #ifndef lint
89 static char rcsid[] DD_UNUSED = "$Id: cuddZddPort.c,v 1.13 2004/08/13 18:04:53 fabio Exp $";
90 #endif
91 
92 /*---------------------------------------------------------------------------*/
93 /* Macro declarations */
94 /*---------------------------------------------------------------------------*/
95 
96 
97 /**AutomaticStart*************************************************************/
98 
99 /*---------------------------------------------------------------------------*/
100 /* Static function prototypes */
101 /*---------------------------------------------------------------------------*/
102 
103 static DdNode * zddPortFromBddStep (DdManager *dd, DdNode *B, int expected);
104 static DdNode * zddPortToBddStep (DdManager *dd, DdNode *f, int depth);
105 
106 /**AutomaticEnd***************************************************************/
107 
108 
109 /*---------------------------------------------------------------------------*/
110 /* Definition of exported functions */
111 /*---------------------------------------------------------------------------*/
112 
113 
114 /**Function********************************************************************
115 
116  Synopsis [Converts a BDD into a ZDD.]
117 
118  Description [Converts a BDD into a ZDD. This function assumes that
119  there is a one-to-one correspondence between the BDD variables and the
120  ZDD variables, and that the variable order is the same for both types
121  of variables. These conditions are established if the ZDD variables
122  are created by one call to Cudd_zddVarsFromBddVars with multiplicity =
123  1. Returns a pointer to the resulting ZDD if successful; NULL otherwise.]
124 
125  SideEffects [None]
126 
127  SeeAlso [Cudd_zddVarsFromBddVars]
128 
129 ******************************************************************************/
130 DdNode *
132  DdManager * dd,
133  DdNode * B)
134 {
135  DdNode *res;
136 
137  do {
138  dd->reordered = 0;
139  res = zddPortFromBddStep(dd,B,0);
140  } while (dd->reordered == 1);
141 
142  return(res);
143 
144 } /* end of Cudd_zddPortFromBdd */
145 
146 
147 /**Function********************************************************************
148 
149  Synopsis [Converts a ZDD into a BDD.]
150 
151  Description [Converts a ZDD into a BDD. Returns a pointer to the resulting
152  ZDD if successful; NULL otherwise.]
153 
154  SideEffects [None]
155 
156  SeeAlso [Cudd_zddPortFromBdd]
157 
158 ******************************************************************************/
159 DdNode *
161  DdManager * dd,
162  DdNode * f)
163 {
164  DdNode *res;
165 
166  do {
167  dd->reordered = 0;
168  res = zddPortToBddStep(dd,f,0);
169  } while (dd->reordered == 1);
170 
171  return(res);
172 
173 } /* end of Cudd_zddPortToBdd */
174 
175 
176 /*---------------------------------------------------------------------------*/
177 /* Definition of internal functions */
178 /*---------------------------------------------------------------------------*/
179 
180 /*---------------------------------------------------------------------------*/
181 /* Definition of static functions */
182 /*---------------------------------------------------------------------------*/
183 
184 
185 /**Function********************************************************************
186 
187  Synopsis [Performs the recursive step of Cudd_zddPortFromBdd.]
188 
189  Description []
190 
191  SideEffects [None]
192 
193  SeeAlso []
194 
195 ******************************************************************************/
196 static DdNode *
198  DdManager * dd,
199  DdNode * B,
200  int expected)
201 {
202  DdNode *res, *prevZdd, *t, *e;
203  DdNode *Breg, *Bt, *Be;
204  int id, level;
205 
206  statLine(dd);
207  /* Terminal cases. */
208  if (B == Cudd_Not(DD_ONE(dd)))
209  return(DD_ZERO(dd));
210  if (B == DD_ONE(dd)) {
211  if (expected >= dd->sizeZ) {
212  return(DD_ONE(dd));
213  } else {
214  return(dd->univ[expected]);
215  }
216  }
217 
218  Breg = Cudd_Regular(B);
219 
220  /* Computed table look-up. */
222  if (res != NULL) {
223  level = cuddI(dd,Breg->index);
224  /* Adding DC vars. */
225  if (expected < level) {
226  /* Add suppressed variables. */
227  cuddRef(res);
228  for (level--; level >= expected; level--) {
229  prevZdd = res;
230  id = dd->invperm[level];
231  res = cuddZddGetNode(dd, id, prevZdd, prevZdd);
232  if (res == NULL) {
233  Cudd_RecursiveDerefZdd(dd, prevZdd);
234  return(NULL);
235  }
236  cuddRef(res);
237  Cudd_RecursiveDerefZdd(dd, prevZdd);
238  }
239  cuddDeref(res);
240  }
241  return(res);
242  } /* end of cache look-up */
243 
244  if (Cudd_IsComplement(B)) {
245  Bt = Cudd_Not(cuddT(Breg));
246  Be = Cudd_Not(cuddE(Breg));
247  } else {
248  Bt = cuddT(Breg);
249  Be = cuddE(Breg);
250  }
251 
252  id = Breg->index;
253  level = cuddI(dd,id);
254  t = zddPortFromBddStep(dd, Bt, level+1);
255  if (t == NULL) return(NULL);
256  cuddRef(t);
257  e = zddPortFromBddStep(dd, Be, level+1);
258  if (e == NULL) {
259  Cudd_RecursiveDerefZdd(dd, t);
260  return(NULL);
261  }
262  cuddRef(e);
263  res = cuddZddGetNode(dd, id, t, e);
264  if (res == NULL) {
265  Cudd_RecursiveDerefZdd(dd, t);
266  Cudd_RecursiveDerefZdd(dd, e);
267  return(NULL);
268  }
269  cuddRef(res);
270  Cudd_RecursiveDerefZdd(dd, t);
271  Cudd_RecursiveDerefZdd(dd, e);
272 
274 
275  for (level--; level >= expected; level--) {
276  prevZdd = res;
277  id = dd->invperm[level];
278  res = cuddZddGetNode(dd, id, prevZdd, prevZdd);
279  if (res == NULL) {
280  Cudd_RecursiveDerefZdd(dd, prevZdd);
281  return(NULL);
282  }
283  cuddRef(res);
284  Cudd_RecursiveDerefZdd(dd, prevZdd);
285  }
286 
287  cuddDeref(res);
288  return(res);
289 
290 } /* end of zddPortFromBddStep */
291 
292 
293 /**Function********************************************************************
294 
295  Synopsis [Performs the recursive step of Cudd_zddPortToBdd.]
296 
297  Description []
298 
299  SideEffects [None]
300 
301  SeeAlso []
302 
303 ******************************************************************************/
304 static DdNode *
306  DdManager * dd /* manager */,
307  DdNode * f /* ZDD to be converted */,
308  int depth /* recursion depth */)
309 {
310  DdNode *one, *zero, *T, *E, *res, *var;
311  unsigned int index;
312  unsigned int level;
313 
314  statLine(dd);
315  one = DD_ONE(dd);
316  zero = DD_ZERO(dd);
317  if (f == zero) return(Cudd_Not(one));
318 
319  if (depth == dd->sizeZ) return(one);
320 
321  index = dd->invpermZ[depth];
322  level = cuddIZ(dd,f->index);
323  var = cuddUniqueInter(dd,index,one,Cudd_Not(one));
324  if (var == NULL) return(NULL);
325  cuddRef(var);
326 
327  if (level > (unsigned) depth) {
328  E = zddPortToBddStep(dd,f,depth+1);
329  if (E == NULL) {
330  Cudd_RecursiveDeref(dd,var);
331  return(NULL);
332  }
333  cuddRef(E);
334  res = cuddBddIteRecur(dd,var,Cudd_Not(one),E);
335  if (res == NULL) {
336  Cudd_RecursiveDeref(dd,var);
337  Cudd_RecursiveDeref(dd,E);
338  return(NULL);
339  }
340  cuddRef(res);
341  Cudd_RecursiveDeref(dd,var);
342  Cudd_RecursiveDeref(dd,E);
343  cuddDeref(res);
344  return(res);
345  }
346 
348  if (res != NULL) {
349  Cudd_RecursiveDeref(dd,var);
350  return(res);
351  }
352 
353  T = zddPortToBddStep(dd,cuddT(f),depth+1);
354  if (T == NULL) {
355  Cudd_RecursiveDeref(dd,var);
356  return(NULL);
357  }
358  cuddRef(T);
359  E = zddPortToBddStep(dd,cuddE(f),depth+1);
360  if (E == NULL) {
361  Cudd_RecursiveDeref(dd,var);
362  Cudd_RecursiveDeref(dd,T);
363  return(NULL);
364  }
365  cuddRef(E);
366 
367  res = cuddBddIteRecur(dd,var,T,E);
368  if (res == NULL) {
369  Cudd_RecursiveDeref(dd,var);
370  Cudd_RecursiveDeref(dd,T);
371  Cudd_RecursiveDeref(dd,E);
372  return(NULL);
373  }
374  cuddRef(res);
375  Cudd_RecursiveDeref(dd,var);
376  Cudd_RecursiveDeref(dd,T);
377  Cudd_RecursiveDeref(dd,E);
378  cuddDeref(res);
379 
381 
382  return(res);
383 
384 } /* end of zddPortToBddStep */
385 
386 
388 
389 
#define cuddRef(n)
Definition: cuddInt.h:584
void Cudd_RecursiveDerefZdd(DdManager *table, DdNode *n)
Definition: cuddRef.c:385
static DdNode * zddPortFromBddStep(DdManager *dd, DdNode *B, int expected)
Definition: cuddZddPort.c:197
#define cuddDeref(n)
Definition: cuddInt.h:604
#define cuddIZ(dd, index)
Definition: cuddInt.h:704
void Cudd_RecursiveDeref(DdManager *table, DdNode *n)
Definition: cuddRef.c:154
Definition: cudd.h:278
DdNode * cuddZddGetNode(DdManager *zdd, int id, DdNode *T, DdNode *E)
Definition: cuddTable.c:1041
#define Cudd_Not(node)
Definition: cudd.h:367
int * invpermZ
Definition: cuddInt.h:389
int var(Lit p)
Definition: SolverTypes.h:62
#define Cudd_Regular(node)
Definition: cudd.h:397
static DdNode * zddPortToBddStep(DdManager *dd, DdNode *f, int depth)
Definition: cuddZddPort.c:305
#define statLine(dd)
Definition: cuddInt.h:1037
int reordered
Definition: cuddInt.h:409
DdNode * Cudd_zddPortFromBdd(DdManager *dd, DdNode *B)
Definition: cuddZddPort.c:131
#define Cudd_IsComplement(node)
Definition: cudd.h:425
DdNode * cuddCacheLookup1(DdManager *table, DD_CTFP1 op, DdNode *f)
Definition: cuddCache.c:556
static ABC_NAMESPACE_IMPL_START char rcsid[] DD_UNUSED
Definition: cuddZddPort.c:89
static DdNode * one
Definition: cuddDecomp.c:112
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
#define cuddT(node)
Definition: cuddInt.h:636
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
#define cuddI(dd, index)
Definition: cuddInt.h:686
int sizeZ
Definition: cuddInt.h:362
DdHalfWord index
Definition: cudd.h:279
#define cuddE(node)
Definition: cuddInt.h:652
int * invperm
Definition: cuddInt.h:388
#define DD_ONE(dd)
Definition: cuddInt.h:911
DdNode * cuddUniqueInter(DdManager *unique, int index, DdNode *T, DdNode *E)
Definition: cuddTable.c:1128
static DdNode * zero
Definition: cuddApa.c:100
DdNode * cuddCacheLookup1Zdd(DdManager *table, DD_CTFP1 op, DdNode *f)
Definition: cuddCache.c:664
void cuddCacheInsert1(DdManager *table, DD_CTFP1 op, DdNode *f, DdNode *data)
Definition: cuddCache.c:323
DdNode ** univ
Definition: cuddInt.h:392
DdNode * Cudd_zddPortToBdd(DdManager *dd, DdNode *f)
Definition: cuddZddPort.c:160
#define DD_ZERO(dd)
Definition: cuddInt.h:927
DdNode * cuddBddIteRecur(DdManager *dd, DdNode *f, DdNode *g, DdNode *h)
Definition: cuddBddIte.c:633