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.

316 lines
9.0 KiB

  1. /**CFile***********************************************************************
  2. FileName [cuddAddFind.c]
  3. PackageName [cudd]
  4. Synopsis [Functions to find maximum and minimum in an ADD and to
  5. extract the i-th bit.]
  6. Description [External procedures included in this module:
  7. <ul>
  8. <li> Cudd_addFindMax()
  9. <li> Cudd_addFindMin()
  10. <li> Cudd_addIthBit()
  11. </ul>
  12. Static functions included in this module:
  13. <ul>
  14. <li> addDoIthBit()
  15. </ul>]
  16. Author [Fabio Somenzi]
  17. Copyright [Copyright (c) 1995-2012, Regents of the University of Colorado
  18. All rights reserved.
  19. Redistribution and use in source and binary forms, with or without
  20. modification, are permitted provided that the following conditions
  21. are met:
  22. Redistributions of source code must retain the above copyright
  23. notice, this list of conditions and the following disclaimer.
  24. Redistributions in binary form must reproduce the above copyright
  25. notice, this list of conditions and the following disclaimer in the
  26. documentation and/or other materials provided with the distribution.
  27. Neither the name of the University of Colorado nor the names of its
  28. contributors may be used to endorse or promote products derived from
  29. this software without specific prior written permission.
  30. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  33. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  34. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  35. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  36. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  39. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  40. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  41. POSSIBILITY OF SUCH DAMAGE.]
  42. ******************************************************************************/
  43. #include "util.h"
  44. #include "cuddInt.h"
  45. /*---------------------------------------------------------------------------*/
  46. /* Constant declarations */
  47. /*---------------------------------------------------------------------------*/
  48. /*---------------------------------------------------------------------------*/
  49. /* Stucture declarations */
  50. /*---------------------------------------------------------------------------*/
  51. /*---------------------------------------------------------------------------*/
  52. /* Type declarations */
  53. /*---------------------------------------------------------------------------*/
  54. /*---------------------------------------------------------------------------*/
  55. /* Variable declarations */
  56. /*---------------------------------------------------------------------------*/
  57. #ifndef lint
  58. static char rcsid[] DD_UNUSED = "$Id: cuddAddFind.c,v 1.9 2012/02/05 01:07:18 fabio Exp $";
  59. #endif
  60. /*---------------------------------------------------------------------------*/
  61. /* Macro declarations */
  62. /*---------------------------------------------------------------------------*/
  63. #ifdef __cplusplus
  64. extern "C" {
  65. #endif
  66. /**AutomaticStart*************************************************************/
  67. /*---------------------------------------------------------------------------*/
  68. /* Static function prototypes */
  69. /*---------------------------------------------------------------------------*/
  70. static DdNode * addDoIthBit (DdManager *dd, DdNode *f, DdNode *index);
  71. /**AutomaticEnd***************************************************************/
  72. #ifdef __cplusplus
  73. }
  74. #endif
  75. /*---------------------------------------------------------------------------*/
  76. /* Definition of exported functions */
  77. /*---------------------------------------------------------------------------*/
  78. /**Function********************************************************************
  79. Synopsis [Finds the maximum discriminant of f.]
  80. Description [Returns a pointer to a constant ADD.]
  81. SideEffects [None]
  82. ******************************************************************************/
  83. DdNode *
  84. Cudd_addFindMax(
  85. DdManager * dd,
  86. DdNode * f)
  87. {
  88. DdNode *t, *e, *res;
  89. statLine(dd);
  90. if (cuddIsConstant(f)) {
  91. return(f);
  92. }
  93. res = cuddCacheLookup1(dd,Cudd_addFindMax,f);
  94. if (res != NULL) {
  95. return(res);
  96. }
  97. t = Cudd_addFindMax(dd,cuddT(f));
  98. if (t == DD_PLUS_INFINITY(dd)) return(t);
  99. e = Cudd_addFindMax(dd,cuddE(f));
  100. res = (cuddV(t) >= cuddV(e)) ? t : e;
  101. cuddCacheInsert1(dd,Cudd_addFindMax,f,res);
  102. return(res);
  103. } /* end of Cudd_addFindMax */
  104. /**Function********************************************************************
  105. Synopsis [Finds the minimum discriminant of f.]
  106. Description [Returns a pointer to a constant ADD.]
  107. SideEffects [None]
  108. ******************************************************************************/
  109. DdNode *
  110. Cudd_addFindMin(
  111. DdManager * dd,
  112. DdNode * f)
  113. {
  114. DdNode *t, *e, *res;
  115. statLine(dd);
  116. if (cuddIsConstant(f)) {
  117. return(f);
  118. }
  119. res = cuddCacheLookup1(dd,Cudd_addFindMin,f);
  120. if (res != NULL) {
  121. return(res);
  122. }
  123. t = Cudd_addFindMin(dd,cuddT(f));
  124. if (t == DD_MINUS_INFINITY(dd)) return(t);
  125. e = Cudd_addFindMin(dd,cuddE(f));
  126. res = (cuddV(t) <= cuddV(e)) ? t : e;
  127. cuddCacheInsert1(dd,Cudd_addFindMin,f,res);
  128. return(res);
  129. } /* end of Cudd_addFindMin */
  130. /**Function********************************************************************
  131. Synopsis [Extracts the i-th bit from an ADD.]
  132. Description [Produces an ADD from another ADD by replacing all
  133. discriminants whose i-th bit is equal to 1 with 1, and all other
  134. discriminants with 0. The i-th bit refers to the integer
  135. representation of the leaf value. If the value is has a fractional
  136. part, it is ignored. Repeated calls to this procedure allow one to
  137. transform an integer-valued ADD into an array of ADDs, one for each
  138. bit of the leaf values. Returns a pointer to the resulting ADD if
  139. successful; NULL otherwise.]
  140. SideEffects [None]
  141. SeeAlso [Cudd_addBddIthBit]
  142. ******************************************************************************/
  143. DdNode *
  144. Cudd_addIthBit(
  145. DdManager * dd,
  146. DdNode * f,
  147. int bit)
  148. {
  149. DdNode *res;
  150. DdNode *index;
  151. /* Use a constant node to remember the bit, so that we can use the
  152. ** global cache.
  153. */
  154. index = cuddUniqueConst(dd,(CUDD_VALUE_TYPE) bit);
  155. if (index == NULL) return(NULL);
  156. cuddRef(index);
  157. do {
  158. dd->reordered = 0;
  159. res = addDoIthBit(dd, f, index);
  160. } while (dd->reordered == 1);
  161. if (res == NULL) {
  162. Cudd_RecursiveDeref(dd, index);
  163. return(NULL);
  164. }
  165. cuddRef(res);
  166. Cudd_RecursiveDeref(dd, index);
  167. cuddDeref(res);
  168. return(res);
  169. } /* end of Cudd_addIthBit */
  170. /*---------------------------------------------------------------------------*/
  171. /* Definition of internal functions */
  172. /*---------------------------------------------------------------------------*/
  173. /*---------------------------------------------------------------------------*/
  174. /* Definition of static functions */
  175. /*---------------------------------------------------------------------------*/
  176. /**Function********************************************************************
  177. Synopsis [Performs the recursive step for Cudd_addIthBit.]
  178. Description [Performs the recursive step for Cudd_addIthBit.
  179. Returns a pointer to the BDD if successful; NULL otherwise.]
  180. SideEffects [None]
  181. SeeAlso []
  182. ******************************************************************************/
  183. static DdNode *
  184. addDoIthBit(
  185. DdManager * dd,
  186. DdNode * f,
  187. DdNode * index)
  188. {
  189. DdNode *res, *T, *E;
  190. DdNode *fv, *fvn;
  191. int mask, value;
  192. int v;
  193. statLine(dd);
  194. /* Check terminal case. */
  195. if (cuddIsConstant(f)) {
  196. mask = 1 << ((int) cuddV(index));
  197. value = (int) cuddV(f);
  198. return((value & mask) == 0 ? DD_ZERO(dd) : DD_ONE(dd));
  199. }
  200. /* Check cache. */
  201. res = cuddCacheLookup2(dd,addDoIthBit,f,index);
  202. if (res != NULL) return(res);
  203. /* Recursive step. */
  204. v = f->index;
  205. fv = cuddT(f); fvn = cuddE(f);
  206. T = addDoIthBit(dd,fv,index);
  207. if (T == NULL) return(NULL);
  208. cuddRef(T);
  209. E = addDoIthBit(dd,fvn,index);
  210. if (E == NULL) {
  211. Cudd_RecursiveDeref(dd, T);
  212. return(NULL);
  213. }
  214. cuddRef(E);
  215. res = (T == E) ? T : cuddUniqueInter(dd,v,T,E);
  216. if (res == NULL) {
  217. Cudd_RecursiveDeref(dd, T);
  218. Cudd_RecursiveDeref(dd, E);
  219. return(NULL);
  220. }
  221. cuddDeref(T);
  222. cuddDeref(E);
  223. /* Store result. */
  224. cuddCacheInsert2(dd,addDoIthBit,f,index,res);
  225. return(res);
  226. } /* end of addDoIthBit */