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.

298 lines
7.8 KiB

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