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.

264 lines
8.5 KiB

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