emergency commit
[cl-cudd.git] / distr / cudd / cuddAddNeg.c
blobe0a9b4cd572693980adf933b44c45f8401fa01e1
1 /**CFile***********************************************************************
3 FileName [cuddAddNeg.c]
5 PackageName [cudd]
7 Synopsis [Function to compute the negation of an ADD.]
9 Description [External procedures included in this module:
10 <ul>
11 <li> Cudd_addNegate()
12 <li> Cudd_addRoundOff()
13 </ul>
14 Internal procedures included in this module:
15 <ul>
16 <li> cuddAddNegateRecur()
17 <li> cuddAddRoundOffRecur()
18 </ul> ]
20 Author [Fabio Somenzi, Balakrishna Kumthekar]
22 Copyright [Copyright (c) 1995-2004, Regents of the University of Colorado
24 All rights reserved.
26 Redistribution and use in source and binary forms, with or without
27 modification, are permitted provided that the following conditions
28 are met:
30 Redistributions of source code must retain the above copyright
31 notice, this list of conditions and the following disclaimer.
33 Redistributions in binary form must reproduce the above copyright
34 notice, this list of conditions and the following disclaimer in the
35 documentation and/or other materials provided with the distribution.
37 Neither the name of the University of Colorado nor the names of its
38 contributors may be used to endorse or promote products derived from
39 this software without specific prior written permission.
41 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
42 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
43 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
44 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
45 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
46 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
47 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
51 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
52 POSSIBILITY OF SUCH DAMAGE.]
54 ******************************************************************************/
56 #include "util.h"
57 #include "cuddInt.h"
60 /*---------------------------------------------------------------------------*/
61 /* Constant declarations */
62 /*---------------------------------------------------------------------------*/
65 /*---------------------------------------------------------------------------*/
66 /* Stucture declarations */
67 /*---------------------------------------------------------------------------*/
70 /*---------------------------------------------------------------------------*/
71 /* Type declarations */
72 /*---------------------------------------------------------------------------*/
75 /*---------------------------------------------------------------------------*/
76 /* Variable declarations */
77 /*---------------------------------------------------------------------------*/
79 #ifndef lint
80 static char rcsid[] DD_UNUSED = "$Id: cuddAddNeg.c,v 1.12 2009/02/20 02:14:58 fabio Exp $";
81 #endif
84 /*---------------------------------------------------------------------------*/
85 /* Macro declarations */
86 /*---------------------------------------------------------------------------*/
89 /**AutomaticStart*************************************************************/
91 /*---------------------------------------------------------------------------*/
92 /* Static function prototypes */
93 /*---------------------------------------------------------------------------*/
96 /**AutomaticEnd***************************************************************/
99 /*---------------------------------------------------------------------------*/
100 /* Definition of exported functions */
101 /*---------------------------------------------------------------------------*/
103 /**Function********************************************************************
105 Synopsis [Computes the additive inverse of an ADD.]
107 Description [Computes the additive inverse of an ADD. Returns a pointer
108 to the result if successful; NULL otherwise.]
110 SideEffects [None]
112 SeeAlso [Cudd_addCmpl]
114 ******************************************************************************/
115 DdNode *
116 Cudd_addNegate(
117 DdManager * dd,
118 DdNode * f)
120 DdNode *res;
122 do {
123 res = cuddAddNegateRecur(dd,f);
124 } while (dd->reordered == 1);
125 return(res);
127 } /* end of Cudd_addNegate */
130 /**Function********************************************************************
132 Synopsis [Rounds off the discriminants of an ADD.]
134 Description [Rounds off the discriminants of an ADD. The discriminants are
135 rounded off to N digits after the decimal. Returns a pointer to the result
136 ADD if successful; NULL otherwise.]
138 SideEffects [None]
140 SeeAlso []
142 ******************************************************************************/
143 DdNode *
144 Cudd_addRoundOff(
145 DdManager * dd,
146 DdNode * f,
147 int N)
149 DdNode *res;
150 double trunc = pow(10.0,(double)N);
152 do {
153 res = cuddAddRoundOffRecur(dd,f,trunc);
154 } while (dd->reordered == 1);
155 return(res);
157 } /* end of Cudd_addRoundOff */
160 /*---------------------------------------------------------------------------*/
161 /* Definition of internal functions */
162 /*---------------------------------------------------------------------------*/
165 /**Function********************************************************************
167 Synopsis [Implements the recursive step of Cudd_addNegate.]
169 Description [Implements the recursive step of Cudd_addNegate.
170 Returns a pointer to the result.]
172 SideEffects [None]
174 ******************************************************************************/
175 DdNode *
176 cuddAddNegateRecur(
177 DdManager * dd,
178 DdNode * f)
180 DdNode *res,
181 *fv, *fvn,
182 *T, *E;
184 statLine(dd);
185 /* Check terminal cases. */
186 if (cuddIsConstant(f)) {
187 res = cuddUniqueConst(dd,-cuddV(f));
188 return(res);
191 /* Check cache */
192 res = cuddCacheLookup1(dd,Cudd_addNegate,f);
193 if (res != NULL) return(res);
195 /* Recursive Step */
196 fv = cuddT(f);
197 fvn = cuddE(f);
198 T = cuddAddNegateRecur(dd,fv);
199 if (T == NULL) return(NULL);
200 cuddRef(T);
202 E = cuddAddNegateRecur(dd,fvn);
203 if (E == NULL) {
204 Cudd_RecursiveDeref(dd,T);
205 return(NULL);
207 cuddRef(E);
208 res = (T == E) ? T : cuddUniqueInter(dd,(int)f->index,T,E);
209 if (res == NULL) {
210 Cudd_RecursiveDeref(dd, T);
211 Cudd_RecursiveDeref(dd, E);
212 return(NULL);
214 cuddDeref(T);
215 cuddDeref(E);
217 /* Store result. */
218 cuddCacheInsert1(dd,Cudd_addNegate,f,res);
220 return(res);
222 } /* end of cuddAddNegateRecur */
225 /**Function********************************************************************
227 Synopsis [Implements the recursive step of Cudd_addRoundOff.]
229 Description [Implements the recursive step of Cudd_addRoundOff.
230 Returns a pointer to the result.]
232 SideEffects [None]
234 ******************************************************************************/
235 DdNode *
236 cuddAddRoundOffRecur(
237 DdManager * dd,
238 DdNode * f,
239 double trunc)
242 DdNode *res, *fv, *fvn, *T, *E;
243 double n;
244 DD_CTFP1 cacheOp;
246 statLine(dd);
247 if (cuddIsConstant(f)) {
248 n = ceil(cuddV(f)*trunc)/trunc;
249 res = cuddUniqueConst(dd,n);
250 return(res);
252 cacheOp = (DD_CTFP1) Cudd_addRoundOff;
253 res = cuddCacheLookup1(dd,cacheOp,f);
254 if (res != NULL) {
255 return(res);
257 /* Recursive Step */
258 fv = cuddT(f);
259 fvn = cuddE(f);
260 T = cuddAddRoundOffRecur(dd,fv,trunc);
261 if (T == NULL) {
262 return(NULL);
264 cuddRef(T);
265 E = cuddAddRoundOffRecur(dd,fvn,trunc);
266 if (E == NULL) {
267 Cudd_RecursiveDeref(dd,T);
268 return(NULL);
270 cuddRef(E);
271 res = (T == E) ? T : cuddUniqueInter(dd,(int)f->index,T,E);
272 if (res == NULL) {
273 Cudd_RecursiveDeref(dd,T);
274 Cudd_RecursiveDeref(dd,E);
275 return(NULL);
277 cuddDeref(T);
278 cuddDeref(E);
280 /* Store result. */
281 cuddCacheInsert1(dd,cacheOp,f,res);
282 return(res);
284 } /* end of cuddAddRoundOffRecur */
286 /*---------------------------------------------------------------------------*/
287 /* Definition of static functions */
288 /*---------------------------------------------------------------------------*/