You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

290 lines
8.5 KiB

  1. /**CFile***********************************************************************
  2. FileName [cuddAddNeg.c]
  3. PackageName [cudd]
  4. Synopsis [Function to compute the negation of an ADD.]
  5. Description [External procedures included in this module:
  6. <ul>
  7. <li> Cudd_addNegate()
  8. <li> Cudd_addRoundOff()
  9. </ul>
  10. Internal procedures included in this module:
  11. <ul>
  12. <li> cuddAddNegateRecur()
  13. <li> cuddAddRoundOffRecur()
  14. </ul> ]
  15. Author [Fabio Somenzi, Balakrishna Kumthekar]
  16. Copyright [Copyright (c) 1995-2012, Regents of the University of Colorado
  17. All rights reserved.
  18. Redistribution and use in source and binary forms, with or without
  19. modification, are permitted provided that the following conditions
  20. are met:
  21. Redistributions of source code must retain the above copyright
  22. notice, this list of conditions and the following disclaimer.
  23. Redistributions in binary form must reproduce the above copyright
  24. notice, this list of conditions and the following disclaimer in the
  25. documentation and/or other materials provided with the distribution.
  26. Neither the name of the University of Colorado nor the names of its
  27. contributors may be used to endorse or promote products derived from
  28. this software without specific prior written permission.
  29. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  32. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  33. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  34. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  35. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  36. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  37. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  38. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  39. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. POSSIBILITY OF SUCH DAMAGE.]
  41. ******************************************************************************/
  42. #include "util.h"
  43. #include "cuddInt.h"
  44. /*---------------------------------------------------------------------------*/
  45. /* Constant declarations */
  46. /*---------------------------------------------------------------------------*/
  47. /*---------------------------------------------------------------------------*/
  48. /* Stucture declarations */
  49. /*---------------------------------------------------------------------------*/
  50. /*---------------------------------------------------------------------------*/
  51. /* Type declarations */
  52. /*---------------------------------------------------------------------------*/
  53. /*---------------------------------------------------------------------------*/
  54. /* Variable declarations */
  55. /*---------------------------------------------------------------------------*/
  56. #ifndef lint
  57. static char rcsid[] DD_UNUSED = "$Id: cuddAddNeg.c,v 1.14 2012/02/05 01:07:18 fabio Exp $";
  58. #endif
  59. /*---------------------------------------------------------------------------*/
  60. /* Macro declarations */
  61. /*---------------------------------------------------------------------------*/
  62. /**AutomaticStart*************************************************************/
  63. /*---------------------------------------------------------------------------*/
  64. /* Static function prototypes */
  65. /*---------------------------------------------------------------------------*/
  66. /**AutomaticEnd***************************************************************/
  67. /*---------------------------------------------------------------------------*/
  68. /* Definition of exported functions */
  69. /*---------------------------------------------------------------------------*/
  70. /**Function********************************************************************
  71. Synopsis [Computes the additive inverse of an ADD.]
  72. Description [Computes the additive inverse of an ADD. Returns a pointer
  73. to the result if successful; NULL otherwise.]
  74. SideEffects [None]
  75. SeeAlso [Cudd_addCmpl]
  76. ******************************************************************************/
  77. DdNode *
  78. Cudd_addNegate(
  79. DdManager * dd,
  80. DdNode * f)
  81. {
  82. DdNode *res;
  83. do {
  84. dd->reordered = 0;
  85. res = cuddAddNegateRecur(dd,f);
  86. } while (dd->reordered == 1);
  87. return(res);
  88. } /* end of Cudd_addNegate */
  89. /**Function********************************************************************
  90. Synopsis [Rounds off the discriminants of an ADD.]
  91. Description [Rounds off the discriminants of an ADD. The discriminants are
  92. rounded off to N digits after the decimal. Returns a pointer to the result
  93. ADD if successful; NULL otherwise.]
  94. SideEffects [None]
  95. SeeAlso []
  96. ******************************************************************************/
  97. DdNode *
  98. Cudd_addRoundOff(
  99. DdManager * dd,
  100. DdNode * f,
  101. int N)
  102. {
  103. DdNode *res;
  104. double trunc = pow(10.0,(double)N);
  105. do {
  106. dd->reordered = 0;
  107. res = cuddAddRoundOffRecur(dd,f,trunc);
  108. } while (dd->reordered == 1);
  109. return(res);
  110. } /* end of Cudd_addRoundOff */
  111. /*---------------------------------------------------------------------------*/
  112. /* Definition of internal functions */
  113. /*---------------------------------------------------------------------------*/
  114. /**Function********************************************************************
  115. Synopsis [Implements the recursive step of Cudd_addNegate.]
  116. Description [Implements the recursive step of Cudd_addNegate.
  117. Returns a pointer to the result.]
  118. SideEffects [None]
  119. ******************************************************************************/
  120. DdNode *
  121. cuddAddNegateRecur(
  122. DdManager * dd,
  123. DdNode * f)
  124. {
  125. DdNode *res,
  126. *fv, *fvn,
  127. *T, *E;
  128. statLine(dd);
  129. /* Check terminal cases. */
  130. if (cuddIsConstant(f)) {
  131. res = cuddUniqueConst(dd,-cuddV(f));
  132. return(res);
  133. }
  134. /* Check cache */
  135. res = cuddCacheLookup1(dd,Cudd_addNegate,f);
  136. if (res != NULL) return(res);
  137. /* Recursive Step */
  138. fv = cuddT(f);
  139. fvn = cuddE(f);
  140. T = cuddAddNegateRecur(dd,fv);
  141. if (T == NULL) return(NULL);
  142. cuddRef(T);
  143. E = cuddAddNegateRecur(dd,fvn);
  144. if (E == NULL) {
  145. Cudd_RecursiveDeref(dd,T);
  146. return(NULL);
  147. }
  148. cuddRef(E);
  149. res = (T == E) ? T : cuddUniqueInter(dd,(int)f->index,T,E);
  150. if (res == NULL) {
  151. Cudd_RecursiveDeref(dd, T);
  152. Cudd_RecursiveDeref(dd, E);
  153. return(NULL);
  154. }
  155. cuddDeref(T);
  156. cuddDeref(E);
  157. /* Store result. */
  158. cuddCacheInsert1(dd,Cudd_addNegate,f,res);
  159. return(res);
  160. } /* end of cuddAddNegateRecur */
  161. /**Function********************************************************************
  162. Synopsis [Implements the recursive step of Cudd_addRoundOff.]
  163. Description [Implements the recursive step of Cudd_addRoundOff.
  164. Returns a pointer to the result.]
  165. SideEffects [None]
  166. ******************************************************************************/
  167. DdNode *
  168. cuddAddRoundOffRecur(
  169. DdManager * dd,
  170. DdNode * f,
  171. double trunc)
  172. {
  173. DdNode *res, *fv, *fvn, *T, *E;
  174. double n;
  175. DD_CTFP1 cacheOp;
  176. statLine(dd);
  177. if (cuddIsConstant(f)) {
  178. n = ceil(cuddV(f)*trunc)/trunc;
  179. res = cuddUniqueConst(dd,n);
  180. return(res);
  181. }
  182. cacheOp = (DD_CTFP1) Cudd_addRoundOff;
  183. res = cuddCacheLookup1(dd,cacheOp,f);
  184. if (res != NULL) {
  185. return(res);
  186. }
  187. /* Recursive Step */
  188. fv = cuddT(f);
  189. fvn = cuddE(f);
  190. T = cuddAddRoundOffRecur(dd,fv,trunc);
  191. if (T == NULL) {
  192. return(NULL);
  193. }
  194. cuddRef(T);
  195. E = cuddAddRoundOffRecur(dd,fvn,trunc);
  196. if (E == NULL) {
  197. Cudd_RecursiveDeref(dd,T);
  198. return(NULL);
  199. }
  200. cuddRef(E);
  201. res = (T == E) ? T : cuddUniqueInter(dd,(int)f->index,T,E);
  202. if (res == NULL) {
  203. Cudd_RecursiveDeref(dd,T);
  204. Cudd_RecursiveDeref(dd,E);
  205. return(NULL);
  206. }
  207. cuddDeref(T);
  208. cuddDeref(E);
  209. /* Store result. */
  210. cuddCacheInsert1(dd,cacheOp,f,res);
  211. return(res);
  212. } /* end of cuddAddRoundOffRecur */
  213. /*---------------------------------------------------------------------------*/
  214. /* Definition of static functions */
  215. /*---------------------------------------------------------------------------*/