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.

327 lines
9.5 KiB

  1. /**CFile***********************************************************************
  2. FileName [cuddCof.c]
  3. PackageName [cudd]
  4. Synopsis [Cofactoring functions.]
  5. Description [External procedures included in this module:
  6. <ul>
  7. <li> Cudd_Cofactor()
  8. <li> Cudd_CheckCube()
  9. </ul>
  10. Internal procedures included in this module:
  11. <ul>
  12. <li> cuddGetBranches()
  13. <li> cuddCofactorRecur()
  14. </ul>
  15. ]
  16. SeeAlso []
  17. Author [Fabio Somenzi]
  18. Copyright [Copyright (c) 1995-2012, Regents of the University of Colorado
  19. All rights reserved.
  20. Redistribution and use in source and binary forms, with or without
  21. modification, are permitted provided that the following conditions
  22. are met:
  23. Redistributions of source code must retain the above copyright
  24. notice, this list of conditions and the following disclaimer.
  25. Redistributions in binary form must reproduce the above copyright
  26. notice, this list of conditions and the following disclaimer in the
  27. documentation and/or other materials provided with the distribution.
  28. Neither the name of the University of Colorado nor the names of its
  29. contributors may be used to endorse or promote products derived from
  30. this software without specific prior written permission.
  31. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  32. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  33. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  34. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  35. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  36. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  37. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  40. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  41. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  42. POSSIBILITY OF SUCH DAMAGE.]
  43. ******************************************************************************/
  44. #include "util.h"
  45. #include "cuddInt.h"
  46. /*---------------------------------------------------------------------------*/
  47. /* Constant declarations */
  48. /*---------------------------------------------------------------------------*/
  49. /*---------------------------------------------------------------------------*/
  50. /* Stucture declarations */
  51. /*---------------------------------------------------------------------------*/
  52. /*---------------------------------------------------------------------------*/
  53. /* Type declarations */
  54. /*---------------------------------------------------------------------------*/
  55. /*---------------------------------------------------------------------------*/
  56. /* Variable declarations */
  57. /*---------------------------------------------------------------------------*/
  58. #ifndef lint
  59. static char rcsid[] DD_UNUSED = "$Id: cuddCof.c,v 1.11 2012/02/05 01:07:18 fabio Exp $";
  60. #endif
  61. /*---------------------------------------------------------------------------*/
  62. /* Macro declarations */
  63. /*---------------------------------------------------------------------------*/
  64. /**AutomaticStart*************************************************************/
  65. /*---------------------------------------------------------------------------*/
  66. /* Static function prototypes */
  67. /*---------------------------------------------------------------------------*/
  68. /**AutomaticEnd***************************************************************/
  69. /*---------------------------------------------------------------------------*/
  70. /* Definition of exported functions */
  71. /*---------------------------------------------------------------------------*/
  72. /**Function********************************************************************
  73. Synopsis [Computes the cofactor of f with respect to g.]
  74. Description [Computes the cofactor of f with respect to g; g must be
  75. the BDD or the ADD of a cube. Returns a pointer to the cofactor if
  76. successful; NULL otherwise.]
  77. SideEffects [None]
  78. SeeAlso [Cudd_bddConstrain Cudd_bddRestrict]
  79. ******************************************************************************/
  80. DdNode *
  81. Cudd_Cofactor(
  82. DdManager * dd,
  83. DdNode * f,
  84. DdNode * g)
  85. {
  86. DdNode *res,*zero;
  87. zero = Cudd_Not(DD_ONE(dd));
  88. if (g == zero || g == DD_ZERO(dd)) {
  89. (void) fprintf(dd->err,"Cudd_Cofactor: Invalid restriction 1\n");
  90. dd->errorCode = CUDD_INVALID_ARG;
  91. return(NULL);
  92. }
  93. do {
  94. dd->reordered = 0;
  95. res = cuddCofactorRecur(dd,f,g);
  96. } while (dd->reordered == 1);
  97. return(res);
  98. } /* end of Cudd_Cofactor */
  99. /**Function********************************************************************
  100. Synopsis [Checks whether g is the BDD of a cube.]
  101. Description [Checks whether g is the BDD of a cube. Returns 1 in case
  102. of success; 0 otherwise. The constant 1 is a valid cube, but all other
  103. constant functions cause cuddCheckCube to return 0.]
  104. SideEffects [None]
  105. SeeAlso []
  106. ******************************************************************************/
  107. int
  108. Cudd_CheckCube(
  109. DdManager * dd,
  110. DdNode * g)
  111. {
  112. DdNode *g1,*g0,*one,*zero;
  113. one = DD_ONE(dd);
  114. if (g == one) return(1);
  115. if (Cudd_IsConstant(g)) return(0);
  116. zero = Cudd_Not(one);
  117. cuddGetBranches(g,&g1,&g0);
  118. if (g0 == zero) {
  119. return(Cudd_CheckCube(dd, g1));
  120. }
  121. if (g1 == zero) {
  122. return(Cudd_CheckCube(dd, g0));
  123. }
  124. return(0);
  125. } /* end of Cudd_CheckCube */
  126. /*---------------------------------------------------------------------------*/
  127. /* Definition of internal functions */
  128. /*---------------------------------------------------------------------------*/
  129. /**Function********************************************************************
  130. Synopsis [Computes the children of g.]
  131. Description []
  132. SideEffects [None]
  133. SeeAlso []
  134. ******************************************************************************/
  135. void
  136. cuddGetBranches(
  137. DdNode * g,
  138. DdNode ** g1,
  139. DdNode ** g0)
  140. {
  141. DdNode *G = Cudd_Regular(g);
  142. *g1 = cuddT(G);
  143. *g0 = cuddE(G);
  144. if (Cudd_IsComplement(g)) {
  145. *g1 = Cudd_Not(*g1);
  146. *g0 = Cudd_Not(*g0);
  147. }
  148. } /* end of cuddGetBranches */
  149. /**Function********************************************************************
  150. Synopsis [Performs the recursive step of Cudd_Cofactor.]
  151. Description [Performs the recursive step of Cudd_Cofactor. Returns a
  152. pointer to the cofactor if successful; NULL otherwise.]
  153. SideEffects [None]
  154. SeeAlso [Cudd_Cofactor]
  155. ******************************************************************************/
  156. DdNode *
  157. cuddCofactorRecur(
  158. DdManager * dd,
  159. DdNode * f,
  160. DdNode * g)
  161. {
  162. DdNode *one,*zero,*F,*G,*g1,*g0,*f1,*f0,*t,*e,*r;
  163. unsigned int topf,topg;
  164. int comple;
  165. statLine(dd);
  166. F = Cudd_Regular(f);
  167. if (cuddIsConstant(F)) return(f);
  168. one = DD_ONE(dd);
  169. /* The invariant g != 0 is true on entry to this procedure and is
  170. ** recursively maintained by it. Therefore it suffices to test g
  171. ** against one to make sure it is not constant.
  172. */
  173. if (g == one) return(f);
  174. /* From now on, f and g are known not to be constants. */
  175. comple = f != F;
  176. r = cuddCacheLookup2(dd,Cudd_Cofactor,F,g);
  177. if (r != NULL) {
  178. return(Cudd_NotCond(r,comple));
  179. }
  180. topf = dd->perm[F->index];
  181. G = Cudd_Regular(g);
  182. topg = dd->perm[G->index];
  183. /* We take the cofactors of F because we are going to rely on
  184. ** the fact that the cofactors of the complement are the complements
  185. ** of the cofactors to better utilize the cache. Variable comple
  186. ** remembers whether we have to complement the result or not.
  187. */
  188. if (topf <= topg) {
  189. f1 = cuddT(F); f0 = cuddE(F);
  190. } else {
  191. f1 = f0 = F;
  192. }
  193. if (topg <= topf) {
  194. g1 = cuddT(G); g0 = cuddE(G);
  195. if (g != G) { g1 = Cudd_Not(g1); g0 = Cudd_Not(g0); }
  196. } else {
  197. g1 = g0 = g;
  198. }
  199. zero = Cudd_Not(one);
  200. if (topf >= topg) {
  201. if (g0 == zero || g0 == DD_ZERO(dd)) {
  202. r = cuddCofactorRecur(dd, f1, g1);
  203. } else if (g1 == zero || g1 == DD_ZERO(dd)) {
  204. r = cuddCofactorRecur(dd, f0, g0);
  205. } else {
  206. (void) fprintf(dd->out,
  207. "Cudd_Cofactor: Invalid restriction 2\n");
  208. dd->errorCode = CUDD_INVALID_ARG;
  209. return(NULL);
  210. }
  211. if (r == NULL) return(NULL);
  212. } else /* if (topf < topg) */ {
  213. t = cuddCofactorRecur(dd, f1, g);
  214. if (t == NULL) return(NULL);
  215. cuddRef(t);
  216. e = cuddCofactorRecur(dd, f0, g);
  217. if (e == NULL) {
  218. Cudd_RecursiveDeref(dd, t);
  219. return(NULL);
  220. }
  221. cuddRef(e);
  222. if (t == e) {
  223. r = t;
  224. } else if (Cudd_IsComplement(t)) {
  225. r = cuddUniqueInter(dd,(int)F->index,Cudd_Not(t),Cudd_Not(e));
  226. if (r != NULL)
  227. r = Cudd_Not(r);
  228. } else {
  229. r = cuddUniqueInter(dd,(int)F->index,t,e);
  230. }
  231. if (r == NULL) {
  232. Cudd_RecursiveDeref(dd ,e);
  233. Cudd_RecursiveDeref(dd ,t);
  234. return(NULL);
  235. }
  236. cuddDeref(t);
  237. cuddDeref(e);
  238. }
  239. cuddCacheInsert2(dd,Cudd_Cofactor,F,g,r);
  240. return(Cudd_NotCond(r,comple));
  241. } /* end of cuddCofactorRecur */
  242. /*---------------------------------------------------------------------------*/
  243. /* Definition of static functions */
  244. /*---------------------------------------------------------------------------*/