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.

381 lines
11 KiB

  1. /**CFile***********************************************************************
  2. FileName [cuddZddPort.c]
  3. PackageName [cudd]
  4. Synopsis [Functions that translate BDDs to ZDDs.]
  5. Description [External procedures included in this module:
  6. <ul>
  7. <li> Cudd_zddPortFromBdd()
  8. <li> Cudd_zddPortToBdd()
  9. </ul>
  10. Internal procedures included in this module:
  11. <ul>
  12. </ul>
  13. Static procedures included in this module:
  14. <ul>
  15. <li> zddPortFromBddStep()
  16. <li> zddPortToBddStep()
  17. </ul>
  18. ]
  19. SeeAlso []
  20. Author [Hyong-kyoon Shin, In-Ho Moon]
  21. Copyright [Copyright (c) 1995-2012, Regents of the University of Colorado
  22. All rights reserved.
  23. Redistribution and use in source and binary forms, with or without
  24. modification, are permitted provided that the following conditions
  25. are met:
  26. Redistributions of source code must retain the above copyright
  27. notice, this list of conditions and the following disclaimer.
  28. Redistributions in binary form must reproduce the above copyright
  29. notice, this list of conditions and the following disclaimer in the
  30. documentation and/or other materials provided with the distribution.
  31. Neither the name of the University of Colorado nor the names of its
  32. contributors may be used to endorse or promote products derived from
  33. this software without specific prior written permission.
  34. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  37. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  38. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  39. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  40. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  41. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  42. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  43. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  44. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  45. POSSIBILITY OF SUCH DAMAGE.]
  46. ******************************************************************************/
  47. #include "util.h"
  48. #include "cuddInt.h"
  49. /*---------------------------------------------------------------------------*/
  50. /* Constant declarations */
  51. /*---------------------------------------------------------------------------*/
  52. /*---------------------------------------------------------------------------*/
  53. /* Stucture declarations */
  54. /*---------------------------------------------------------------------------*/
  55. /*---------------------------------------------------------------------------*/
  56. /* Type declarations */
  57. /*---------------------------------------------------------------------------*/
  58. /*---------------------------------------------------------------------------*/
  59. /* Variable declarations */
  60. /*---------------------------------------------------------------------------*/
  61. #ifndef lint
  62. static char rcsid[] DD_UNUSED = "$Id: cuddZddPort.c,v 1.14 2012/02/05 01:07:19 fabio Exp $";
  63. #endif
  64. /*---------------------------------------------------------------------------*/
  65. /* Macro declarations */
  66. /*---------------------------------------------------------------------------*/
  67. /**AutomaticStart*************************************************************/
  68. /*---------------------------------------------------------------------------*/
  69. /* Static function prototypes */
  70. /*---------------------------------------------------------------------------*/
  71. static DdNode * zddPortFromBddStep (DdManager *dd, DdNode *B, int expected);
  72. static DdNode * zddPortToBddStep (DdManager *dd, DdNode *f, int depth);
  73. /**AutomaticEnd***************************************************************/
  74. /*---------------------------------------------------------------------------*/
  75. /* Definition of exported functions */
  76. /*---------------------------------------------------------------------------*/
  77. /**Function********************************************************************
  78. Synopsis [Converts a BDD into a ZDD.]
  79. Description [Converts a BDD into a ZDD. This function assumes that
  80. there is a one-to-one correspondence between the BDD variables and the
  81. ZDD variables, and that the variable order is the same for both types
  82. of variables. These conditions are established if the ZDD variables
  83. are created by one call to Cudd_zddVarsFromBddVars with multiplicity =
  84. 1. Returns a pointer to the resulting ZDD if successful; NULL otherwise.]
  85. SideEffects [None]
  86. SeeAlso [Cudd_zddVarsFromBddVars]
  87. ******************************************************************************/
  88. DdNode *
  89. Cudd_zddPortFromBdd(
  90. DdManager * dd,
  91. DdNode * B)
  92. {
  93. DdNode *res;
  94. do {
  95. dd->reordered = 0;
  96. res = zddPortFromBddStep(dd,B,0);
  97. } while (dd->reordered == 1);
  98. return(res);
  99. } /* end of Cudd_zddPortFromBdd */
  100. /**Function********************************************************************
  101. Synopsis [Converts a ZDD into a BDD.]
  102. Description [Converts a ZDD into a BDD. Returns a pointer to the resulting
  103. ZDD if successful; NULL otherwise.]
  104. SideEffects [None]
  105. SeeAlso [Cudd_zddPortFromBdd]
  106. ******************************************************************************/
  107. DdNode *
  108. Cudd_zddPortToBdd(
  109. DdManager * dd,
  110. DdNode * f)
  111. {
  112. DdNode *res;
  113. do {
  114. dd->reordered = 0;
  115. res = zddPortToBddStep(dd,f,0);
  116. } while (dd->reordered == 1);
  117. return(res);
  118. } /* end of Cudd_zddPortToBdd */
  119. /*---------------------------------------------------------------------------*/
  120. /* Definition of internal functions */
  121. /*---------------------------------------------------------------------------*/
  122. /*---------------------------------------------------------------------------*/
  123. /* Definition of static functions */
  124. /*---------------------------------------------------------------------------*/
  125. /**Function********************************************************************
  126. Synopsis [Performs the recursive step of Cudd_zddPortFromBdd.]
  127. Description []
  128. SideEffects [None]
  129. SeeAlso []
  130. ******************************************************************************/
  131. static DdNode *
  132. zddPortFromBddStep(
  133. DdManager * dd,
  134. DdNode * B,
  135. int expected)
  136. {
  137. DdNode *res, *prevZdd, *t, *e;
  138. DdNode *Breg, *Bt, *Be;
  139. int id, level;
  140. statLine(dd);
  141. /* Terminal cases. */
  142. if (B == Cudd_Not(DD_ONE(dd)))
  143. return(DD_ZERO(dd));
  144. if (B == DD_ONE(dd)) {
  145. if (expected >= dd->sizeZ) {
  146. return(DD_ONE(dd));
  147. } else {
  148. return(dd->univ[expected]);
  149. }
  150. }
  151. Breg = Cudd_Regular(B);
  152. /* Computed table look-up. */
  153. res = cuddCacheLookup1Zdd(dd,Cudd_zddPortFromBdd,B);
  154. if (res != NULL) {
  155. level = cuddI(dd,Breg->index);
  156. /* Adding DC vars. */
  157. if (expected < level) {
  158. /* Add suppressed variables. */
  159. cuddRef(res);
  160. for (level--; level >= expected; level--) {
  161. prevZdd = res;
  162. id = dd->invperm[level];
  163. res = cuddZddGetNode(dd, id, prevZdd, prevZdd);
  164. if (res == NULL) {
  165. Cudd_RecursiveDerefZdd(dd, prevZdd);
  166. return(NULL);
  167. }
  168. cuddRef(res);
  169. Cudd_RecursiveDerefZdd(dd, prevZdd);
  170. }
  171. cuddDeref(res);
  172. }
  173. return(res);
  174. } /* end of cache look-up */
  175. if (Cudd_IsComplement(B)) {
  176. Bt = Cudd_Not(cuddT(Breg));
  177. Be = Cudd_Not(cuddE(Breg));
  178. } else {
  179. Bt = cuddT(Breg);
  180. Be = cuddE(Breg);
  181. }
  182. id = Breg->index;
  183. level = cuddI(dd,id);
  184. t = zddPortFromBddStep(dd, Bt, level+1);
  185. if (t == NULL) return(NULL);
  186. cuddRef(t);
  187. e = zddPortFromBddStep(dd, Be, level+1);
  188. if (e == NULL) {
  189. Cudd_RecursiveDerefZdd(dd, t);
  190. return(NULL);
  191. }
  192. cuddRef(e);
  193. res = cuddZddGetNode(dd, id, t, e);
  194. if (res == NULL) {
  195. Cudd_RecursiveDerefZdd(dd, t);
  196. Cudd_RecursiveDerefZdd(dd, e);
  197. return(NULL);
  198. }
  199. cuddRef(res);
  200. Cudd_RecursiveDerefZdd(dd, t);
  201. Cudd_RecursiveDerefZdd(dd, e);
  202. cuddCacheInsert1(dd,Cudd_zddPortFromBdd,B,res);
  203. for (level--; level >= expected; level--) {
  204. prevZdd = res;
  205. id = dd->invperm[level];
  206. res = cuddZddGetNode(dd, id, prevZdd, prevZdd);
  207. if (res == NULL) {
  208. Cudd_RecursiveDerefZdd(dd, prevZdd);
  209. return(NULL);
  210. }
  211. cuddRef(res);
  212. Cudd_RecursiveDerefZdd(dd, prevZdd);
  213. }
  214. cuddDeref(res);
  215. return(res);
  216. } /* end of zddPortFromBddStep */
  217. /**Function********************************************************************
  218. Synopsis [Performs the recursive step of Cudd_zddPortToBdd.]
  219. Description []
  220. SideEffects [None]
  221. SeeAlso []
  222. ******************************************************************************/
  223. static DdNode *
  224. zddPortToBddStep(
  225. DdManager * dd /* manager */,
  226. DdNode * f /* ZDD to be converted */,
  227. int depth /* recursion depth */)
  228. {
  229. DdNode *one, *zero, *T, *E, *res, *var;
  230. unsigned int index;
  231. unsigned int level;
  232. statLine(dd);
  233. one = DD_ONE(dd);
  234. zero = DD_ZERO(dd);
  235. if (f == zero) return(Cudd_Not(one));
  236. if (depth == dd->sizeZ) return(one);
  237. index = dd->invpermZ[depth];
  238. level = cuddIZ(dd,f->index);
  239. var = cuddUniqueInter(dd,index,one,Cudd_Not(one));
  240. if (var == NULL) return(NULL);
  241. cuddRef(var);
  242. if (level > (unsigned) depth) {
  243. E = zddPortToBddStep(dd,f,depth+1);
  244. if (E == NULL) {
  245. Cudd_RecursiveDeref(dd,var);
  246. return(NULL);
  247. }
  248. cuddRef(E);
  249. res = cuddBddIteRecur(dd,var,Cudd_Not(one),E);
  250. if (res == NULL) {
  251. Cudd_RecursiveDeref(dd,var);
  252. Cudd_RecursiveDeref(dd,E);
  253. return(NULL);
  254. }
  255. cuddRef(res);
  256. Cudd_RecursiveDeref(dd,var);
  257. Cudd_RecursiveDeref(dd,E);
  258. cuddDeref(res);
  259. return(res);
  260. }
  261. res = cuddCacheLookup1(dd,Cudd_zddPortToBdd,f);
  262. if (res != NULL) {
  263. Cudd_RecursiveDeref(dd,var);
  264. return(res);
  265. }
  266. T = zddPortToBddStep(dd,cuddT(f),depth+1);
  267. if (T == NULL) {
  268. Cudd_RecursiveDeref(dd,var);
  269. return(NULL);
  270. }
  271. cuddRef(T);
  272. E = zddPortToBddStep(dd,cuddE(f),depth+1);
  273. if (E == NULL) {
  274. Cudd_RecursiveDeref(dd,var);
  275. Cudd_RecursiveDeref(dd,T);
  276. return(NULL);
  277. }
  278. cuddRef(E);
  279. res = cuddBddIteRecur(dd,var,T,E);
  280. if (res == NULL) {
  281. Cudd_RecursiveDeref(dd,var);
  282. Cudd_RecursiveDeref(dd,T);
  283. Cudd_RecursiveDeref(dd,E);
  284. return(NULL);
  285. }
  286. cuddRef(res);
  287. Cudd_RecursiveDeref(dd,var);
  288. Cudd_RecursiveDeref(dd,T);
  289. Cudd_RecursiveDeref(dd,E);
  290. cuddDeref(res);
  291. cuddCacheInsert1(dd,Cudd_zddPortToBdd,f,res);
  292. return(res);
  293. } /* end of zddPortToBddStep */