abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
cuddAddFind.c
Go to the documentation of this file.
1 /**CFile***********************************************************************
2 
3  FileName [cuddAddFind.c]
4 
5  PackageName [cudd]
6 
7  Synopsis [Functions to find maximum and minimum in an ADD and to
8  extract the i-th bit.]
9 
10  Description [External procedures included in this module:
11  <ul>
12  <li> Cudd_addFindMax()
13  <li> Cudd_addFindMin()
14  <li> Cudd_addIthBit()
15  </ul>
16  Static functions included in this module:
17  <ul>
18  <li> addDoIthBit()
19  </ul>]
20 
21  Author [Fabio Somenzi]
22 
23  Copyright [Copyright (c) 1995-2004, Regents of the University of Colorado
24 
25  All rights reserved.
26 
27  Redistribution and use in source and binary forms, with or without
28  modification, are permitted provided that the following conditions
29  are met:
30 
31  Redistributions of source code must retain the above copyright
32  notice, this list of conditions and the following disclaimer.
33 
34  Redistributions in binary form must reproduce the above copyright
35  notice, this list of conditions and the following disclaimer in the
36  documentation and/or other materials provided with the distribution.
37 
38  Neither the name of the University of Colorado nor the names of its
39  contributors may be used to endorse or promote products derived from
40  this software without specific prior written permission.
41 
42  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
43  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
44  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
45  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
46  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
47  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
48  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
49  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
50  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
52  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
53  POSSIBILITY OF SUCH DAMAGE.]
54 
55 ******************************************************************************/
56 
57 #include "misc/util/util_hack.h"
58 #include "cuddInt.h"
59 
61 
62 
63 
64 /*---------------------------------------------------------------------------*/
65 /* Constant declarations */
66 /*---------------------------------------------------------------------------*/
67 
68 
69 /*---------------------------------------------------------------------------*/
70 /* Stucture declarations */
71 /*---------------------------------------------------------------------------*/
72 
73 
74 /*---------------------------------------------------------------------------*/
75 /* Type declarations */
76 /*---------------------------------------------------------------------------*/
77 
78 
79 /*---------------------------------------------------------------------------*/
80 /* Variable declarations */
81 /*---------------------------------------------------------------------------*/
82 
83 #ifndef lint
84 static char rcsid[] DD_UNUSED = "$Id: cuddAddFind.c,v 1.8 2004/08/13 18:04:45 fabio Exp $";
85 #endif
86 
87 
88 /*---------------------------------------------------------------------------*/
89 /* Macro declarations */
90 /*---------------------------------------------------------------------------*/
91 
92 #ifdef __cplusplus
93 extern "C" {
94 #endif
95 
96 /**AutomaticStart*************************************************************/
97 
98 /*---------------------------------------------------------------------------*/
99 /* Static function prototypes */
100 /*---------------------------------------------------------------------------*/
101 
102 static DdNode * addDoIthBit (DdManager *dd, DdNode *f, DdNode *index);
103 
104 /**AutomaticEnd***************************************************************/
105 
106 #ifdef __cplusplus
107 }
108 #endif
109 
110 /*---------------------------------------------------------------------------*/
111 /* Definition of exported functions */
112 /*---------------------------------------------------------------------------*/
113 
114 /**Function********************************************************************
115 
116  Synopsis [Finds the maximum discriminant of f.]
117 
118  Description [Returns a pointer to a constant ADD.]
119 
120  SideEffects [None]
121 
122 ******************************************************************************/
123 DdNode *
125  DdManager * dd,
126  DdNode * f)
127 {
128  DdNode *t, *e, *res;
129 
130  statLine(dd);
131  if (cuddIsConstant(f)) {
132  return(f);
133  }
134 
135  res = cuddCacheLookup1(dd,Cudd_addFindMax,f);
136  if (res != NULL) {
137  return(res);
138  }
139 
140  t = Cudd_addFindMax(dd,cuddT(f));
141  if (t == DD_PLUS_INFINITY(dd)) return(t);
142 
143  e = Cudd_addFindMax(dd,cuddE(f));
144 
145  res = (cuddV(t) >= cuddV(e)) ? t : e;
146 
148 
149  return(res);
150 
151 } /* end of Cudd_addFindMax */
152 
153 
154 /**Function********************************************************************
155 
156  Synopsis [Finds the minimum discriminant of f.]
157 
158  Description [Returns a pointer to a constant ADD.]
159 
160  SideEffects [None]
161 
162 ******************************************************************************/
163 DdNode *
165  DdManager * dd,
166  DdNode * f)
167 {
168  DdNode *t, *e, *res;
169 
170  statLine(dd);
171  if (cuddIsConstant(f)) {
172  return(f);
173  }
174 
175  res = cuddCacheLookup1(dd,Cudd_addFindMin,f);
176  if (res != NULL) {
177  return(res);
178  }
179 
180  t = Cudd_addFindMin(dd,cuddT(f));
181  if (t == DD_MINUS_INFINITY(dd)) return(t);
182 
183  e = Cudd_addFindMin(dd,cuddE(f));
184 
185  res = (cuddV(t) <= cuddV(e)) ? t : e;
186 
188 
189  return(res);
190 
191 } /* end of Cudd_addFindMin */
192 
193 
194 /**Function********************************************************************
195 
196  Synopsis [Extracts the i-th bit from an ADD.]
197 
198  Description [Produces an ADD from another ADD by replacing all
199  discriminants whose i-th bit is equal to 1 with 1, and all other
200  discriminants with 0. The i-th bit refers to the integer
201  representation of the leaf value. If the value is has a fractional
202  part, it is ignored. Repeated calls to this procedure allow one to
203  transform an integer-valued ADD into an array of ADDs, one for each
204  bit of the leaf values. Returns a pointer to the resulting ADD if
205  successful; NULL otherwise.]
206 
207  SideEffects [None]
208 
209  SeeAlso [Cudd_addBddIthBit]
210 
211 ******************************************************************************/
212 DdNode *
214  DdManager * dd,
215  DdNode * f,
216  int bit)
217 {
218  DdNode *res;
219  DdNode *index;
220 
221  /* Use a constant node to remember the bit, so that we can use the
222  ** global cache.
223  */
224  index = cuddUniqueConst(dd,(CUDD_VALUE_TYPE) bit);
225  if (index == NULL) return(NULL);
226  cuddRef(index);
227 
228  do {
229  dd->reordered = 0;
230  res = addDoIthBit(dd, f, index);
231  } while (dd->reordered == 1);
232 
233  if (res == NULL) {
234  Cudd_RecursiveDeref(dd, index);
235  return(NULL);
236  }
237  cuddRef(res);
238  Cudd_RecursiveDeref(dd, index);
239  cuddDeref(res);
240  return(res);
241 
242 } /* end of Cudd_addIthBit */
243 
244 
245 /*---------------------------------------------------------------------------*/
246 /* Definition of internal functions */
247 /*---------------------------------------------------------------------------*/
248 
249 
250 /*---------------------------------------------------------------------------*/
251 /* Definition of static functions */
252 /*---------------------------------------------------------------------------*/
253 
254 
255 /**Function********************************************************************
256 
257  Synopsis [Performs the recursive step for Cudd_addIthBit.]
258 
259  Description [Performs the recursive step for Cudd_addIthBit.
260  Returns a pointer to the BDD if successful; NULL otherwise.]
261 
262  SideEffects [None]
263 
264  SeeAlso []
265 
266 ******************************************************************************/
267 static DdNode *
269  DdManager * dd,
270  DdNode * f,
271  DdNode * index)
272 {
273  DdNode *res, *T, *E;
274  DdNode *fv, *fvn;
275  int mask, value;
276  int v;
277 
278  statLine(dd);
279  /* Check terminal case. */
280  if (cuddIsConstant(f)) {
281  mask = 1 << ((int) cuddV(index));
282  value = (int) cuddV(f);
283  return((value & mask) == 0 ? DD_ZERO(dd) : DD_ONE(dd));
284  }
285 
286  /* Check cache. */
287  res = cuddCacheLookup2(dd,addDoIthBit,f,index);
288  if (res != NULL) return(res);
289 
290  /* Recursive step. */
291  v = f->index;
292  fv = cuddT(f); fvn = cuddE(f);
293 
294  T = addDoIthBit(dd,fv,index);
295  if (T == NULL) return(NULL);
296  cuddRef(T);
297 
298  E = addDoIthBit(dd,fvn,index);
299  if (E == NULL) {
300  Cudd_RecursiveDeref(dd, T);
301  return(NULL);
302  }
303  cuddRef(E);
304 
305  res = (T == E) ? T : cuddUniqueInter(dd,v,T,E);
306  if (res == NULL) {
307  Cudd_RecursiveDeref(dd, T);
308  Cudd_RecursiveDeref(dd, E);
309  return(NULL);
310  }
311  cuddDeref(T);
312  cuddDeref(E);
313 
314  /* Store result. */
315  cuddCacheInsert2(dd,addDoIthBit,f,index,res);
316 
317  return(res);
318 
319 } /* end of addDoIthBit */
320 
321 
323 
324 
#define cuddRef(n)
Definition: cuddInt.h:584
#define cuddDeref(n)
Definition: cuddInt.h:604
void Cudd_RecursiveDeref(DdManager *table, DdNode *n)
Definition: cuddRef.c:154
Definition: cudd.h:278
void cuddCacheInsert2(DdManager *table, DD_CTFP op, DdNode *f, DdNode *g, DdNode *data)
Definition: cuddCache.c:277
DdNode * cuddUniqueConst(DdManager *unique, CUDD_VALUE_TYPE value)
Definition: cuddTable.c:1450
DdNode * Cudd_addFindMin(DdManager *dd, DdNode *f)
Definition: cuddAddFind.c:164
#define statLine(dd)
Definition: cuddInt.h:1037
DdNode * Cudd_addFindMax(DdManager *dd, DdNode *f)
Definition: cuddAddFind.c:124
#define DD_MINUS_INFINITY(dd)
Definition: cuddInt.h:955
#define cuddV(node)
Definition: cuddInt.h:668
DdNode * cuddCacheLookup2(DdManager *table, DD_CTFP op, DdNode *f, DdNode *g)
Definition: cuddCache.c:502
int reordered
Definition: cuddInt.h:409
DdNode * Cudd_addIthBit(DdManager *dd, DdNode *f, int bit)
Definition: cuddAddFind.c:213
DdNode * cuddCacheLookup1(DdManager *table, DD_CTFP1 op, DdNode *f)
Definition: cuddCache.c:556
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
#define cuddIsConstant(node)
Definition: cuddInt.h:620
#define cuddT(node)
Definition: cuddInt.h:636
static DdNode * addDoIthBit(DdManager *dd, DdNode *f, DdNode *index)
Definition: cuddAddFind.c:268
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
#define CUDD_VALUE_TYPE
Definition: cudd.h:94
static ABC_NAMESPACE_IMPL_START char rcsid[] DD_UNUSED
Definition: cuddAddFind.c:84
DdHalfWord index
Definition: cudd.h:279
#define cuddE(node)
Definition: cuddInt.h:652
#define DD_PLUS_INFINITY(dd)
Definition: cuddInt.h:941
int value
#define DD_ONE(dd)
Definition: cuddInt.h:911
DdNode * cuddUniqueInter(DdManager *unique, int index, DdNode *T, DdNode *E)
Definition: cuddTable.c:1128
void cuddCacheInsert1(DdManager *table, DD_CTFP1 op, DdNode *f, DdNode *data)
Definition: cuddCache.c:323
#define DD_ZERO(dd)
Definition: cuddInt.h:927