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.

255 lines
7.5 KiB

  1. /**
  2. @file
  3. @ingroup cudd
  4. @brief Functions for manipulation of literal sets represented by BDDs.
  5. @author Fabio Somenzi
  6. @copyright@parblock
  7. Copyright (c) 1995-2015, Regents of the University of Colorado
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions
  11. are met:
  12. Redistributions of source code must retain the above copyright
  13. notice, this list of conditions and the following disclaimer.
  14. Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in the
  16. documentation and/or other materials provided with the distribution.
  17. Neither the name of the University of Colorado nor the names of its
  18. contributors may be used to endorse or promote products derived from
  19. this software without specific prior written permission.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  30. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. POSSIBILITY OF SUCH DAMAGE.
  32. @endparblock
  33. */
  34. #include "util.h"
  35. #include "cuddInt.h"
  36. /*---------------------------------------------------------------------------*/
  37. /* Constant declarations */
  38. /*---------------------------------------------------------------------------*/
  39. /*---------------------------------------------------------------------------*/
  40. /* Stucture declarations */
  41. /*---------------------------------------------------------------------------*/
  42. /*---------------------------------------------------------------------------*/
  43. /* Type declarations */
  44. /*---------------------------------------------------------------------------*/
  45. /*---------------------------------------------------------------------------*/
  46. /* Variable declarations */
  47. /*---------------------------------------------------------------------------*/
  48. /*---------------------------------------------------------------------------*/
  49. /* Macro declarations */
  50. /*---------------------------------------------------------------------------*/
  51. /** \cond */
  52. /*---------------------------------------------------------------------------*/
  53. /* Static function prototypes */
  54. /*---------------------------------------------------------------------------*/
  55. /** \endcond */
  56. /*---------------------------------------------------------------------------*/
  57. /* Definition of exported functions */
  58. /*---------------------------------------------------------------------------*/
  59. /**
  60. @brief Computes the intesection of two sets of literals
  61. represented as BDDs.
  62. @details Each set is represented as a cube of the literals in the
  63. set. The empty set is represented by the constant 1. No variable
  64. can be simultaneously present in both phases in a set.
  65. @return a pointer to the %BDD representing the intersected sets, if
  66. successful; NULL otherwise.
  67. @sideeffect None
  68. */
  69. DdNode *
  70. Cudd_bddLiteralSetIntersection(
  71. DdManager * dd,
  72. DdNode * f,
  73. DdNode * g)
  74. {
  75. DdNode *res;
  76. do {
  77. dd->reordered = 0;
  78. res = cuddBddLiteralSetIntersectionRecur(dd,f,g);
  79. } while (dd->reordered == 1);
  80. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  81. dd->timeoutHandler(dd, dd->tohArg);
  82. }
  83. return(res);
  84. } /* end of Cudd_bddLiteralSetIntersection */
  85. /*---------------------------------------------------------------------------*/
  86. /* Definition of internal functions */
  87. /*---------------------------------------------------------------------------*/
  88. /**
  89. @brief Performs the recursive step of
  90. Cudd_bddLiteralSetIntersection.
  91. @details Scans the cubes for common variables, and checks whether
  92. they agree in phase.
  93. @return a pointer to the resulting cube if successful; NULL
  94. otherwise.
  95. @sideeffect None
  96. */
  97. DdNode *
  98. cuddBddLiteralSetIntersectionRecur(
  99. DdManager * dd,
  100. DdNode * f,
  101. DdNode * g)
  102. {
  103. DdNode *res, *tmp;
  104. DdNode *F, *G;
  105. DdNode *fc, *gc;
  106. DdNode *one;
  107. DdNode *zero;
  108. int topf, topg, comple;
  109. int phasef, phaseg;
  110. statLine(dd);
  111. if (f == g) return(f);
  112. F = Cudd_Regular(f);
  113. G = Cudd_Regular(g);
  114. one = DD_ONE(dd);
  115. /* Here f != g. If F == G, then f and g are complementary.
  116. ** Since they are two cubes, this case only occurs when f == v,
  117. ** g == v', and v is a variable or its complement.
  118. */
  119. if (F == G) return(one);
  120. zero = Cudd_Not(one);
  121. topf = cuddI(dd,F->index);
  122. topg = cuddI(dd,G->index);
  123. /* Look for a variable common to both cubes. If there are none, this
  124. ** loop will stop when the constant node is reached in both cubes.
  125. */
  126. while (topf != topg) {
  127. if (topf < topg) { /* move down on f */
  128. comple = f != F;
  129. f = cuddT(F);
  130. if (comple) f = Cudd_Not(f);
  131. if (f == zero) {
  132. f = cuddE(F);
  133. if (comple) f = Cudd_Not(f);
  134. }
  135. F = Cudd_Regular(f);
  136. topf = cuddI(dd,F->index);
  137. } else if (topg < topf) {
  138. comple = g != G;
  139. g = cuddT(G);
  140. if (comple) g = Cudd_Not(g);
  141. if (g == zero) {
  142. g = cuddE(G);
  143. if (comple) g = Cudd_Not(g);
  144. }
  145. G = Cudd_Regular(g);
  146. topg = cuddI(dd,G->index);
  147. }
  148. }
  149. /* At this point, f == one <=> g == 1. It suffices to test one of them. */
  150. if (f == one) return(one);
  151. res = cuddCacheLookup2(dd,Cudd_bddLiteralSetIntersection,f,g);
  152. if (res != NULL) {
  153. return(res);
  154. }
  155. checkWhetherToGiveUp(dd);
  156. /* Here f and g are both non constant and have the same top variable. */
  157. comple = f != F;
  158. fc = cuddT(F);
  159. phasef = 1;
  160. if (comple) fc = Cudd_Not(fc);
  161. if (fc == zero) {
  162. fc = cuddE(F);
  163. phasef = 0;
  164. if (comple) fc = Cudd_Not(fc);
  165. }
  166. comple = g != G;
  167. gc = cuddT(G);
  168. phaseg = 1;
  169. if (comple) gc = Cudd_Not(gc);
  170. if (gc == zero) {
  171. gc = cuddE(G);
  172. phaseg = 0;
  173. if (comple) gc = Cudd_Not(gc);
  174. }
  175. tmp = cuddBddLiteralSetIntersectionRecur(dd,fc,gc);
  176. if (tmp == NULL) {
  177. return(NULL);
  178. }
  179. if (phasef != phaseg) {
  180. res = tmp;
  181. } else {
  182. cuddRef(tmp);
  183. if (phasef == 0) {
  184. res = cuddBddAndRecur(dd,Cudd_Not(dd->vars[F->index]),tmp);
  185. } else {
  186. res = cuddBddAndRecur(dd,dd->vars[F->index],tmp);
  187. }
  188. if (res == NULL) {
  189. Cudd_RecursiveDeref(dd,tmp);
  190. return(NULL);
  191. }
  192. cuddDeref(tmp); /* Just cuddDeref, because it is included in result */
  193. }
  194. cuddCacheInsert2(dd,Cudd_bddLiteralSetIntersection,f,g,res);
  195. return(res);
  196. } /* end of cuddBddLiteralSetIntersectionRecur */
  197. /*---------------------------------------------------------------------------*/
  198. /* Definition of static functions */
  199. /*---------------------------------------------------------------------------*/