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.

357 lines
9.4 KiB

3 months ago
  1. /**
  2. @file
  3. @ingroup cudd
  4. @brief Functions that translate BDDs to ZDDs.
  5. @author Hyong-kyoon Shin, In-Ho Moon
  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. static DdNode * zddPortFromBddStep (DdManager *dd, DdNode *B, int expected);
  56. static DdNode * zddPortToBddStep (DdManager *dd, DdNode *f, int depth);
  57. /** \endcond */
  58. /*---------------------------------------------------------------------------*/
  59. /* Definition of exported functions */
  60. /*---------------------------------------------------------------------------*/
  61. /**
  62. @brief Converts a %BDD into a %ZDD.
  63. @details This function assumes that there is a one-to-one
  64. correspondence between the %BDD variables and the %ZDD variables, and
  65. that the variable order is the same for both types of
  66. variables. These conditions are established if the %ZDD variables are
  67. created by one call to Cudd_zddVarsFromBddVars with multiplicity = 1.
  68. @return a pointer to the resulting %ZDD if successful; NULL otherwise.
  69. @sideeffect None
  70. @see Cudd_zddVarsFromBddVars
  71. */
  72. DdNode *
  73. Cudd_zddPortFromBdd(
  74. DdManager * dd,
  75. DdNode * B)
  76. {
  77. DdNode *res;
  78. do {
  79. dd->reordered = 0;
  80. res = zddPortFromBddStep(dd,B,0);
  81. } while (dd->reordered == 1);
  82. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  83. dd->timeoutHandler(dd, dd->tohArg);
  84. }
  85. return(res);
  86. } /* end of Cudd_zddPortFromBdd */
  87. /**
  88. @brief Converts a %ZDD into a %BDD.
  89. @return a pointer to the resulting %ZDD if successful; NULL
  90. otherwise.
  91. @sideeffect None
  92. @see Cudd_zddPortFromBdd
  93. */
  94. DdNode *
  95. Cudd_zddPortToBdd(
  96. DdManager * dd,
  97. DdNode * f)
  98. {
  99. DdNode *res;
  100. do {
  101. dd->reordered = 0;
  102. res = zddPortToBddStep(dd,f,0);
  103. } while (dd->reordered == 1);
  104. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  105. dd->timeoutHandler(dd, dd->tohArg);
  106. }
  107. return(res);
  108. } /* end of Cudd_zddPortToBdd */
  109. /*---------------------------------------------------------------------------*/
  110. /* Definition of internal functions */
  111. /*---------------------------------------------------------------------------*/
  112. /*---------------------------------------------------------------------------*/
  113. /* Definition of static functions */
  114. /*---------------------------------------------------------------------------*/
  115. /**
  116. @brief Performs the recursive step of Cudd_zddPortFromBdd.
  117. @sideeffect None
  118. */
  119. static DdNode *
  120. zddPortFromBddStep(
  121. DdManager * dd,
  122. DdNode * B,
  123. int expected)
  124. {
  125. DdNode *res, *prevZdd, *t, *e;
  126. DdNode *Breg, *Bt, *Be;
  127. int id;
  128. int level;
  129. statLine(dd);
  130. /* Terminal cases. */
  131. if (B == Cudd_Not(DD_ONE(dd)))
  132. return(DD_ZERO(dd));
  133. if (B == DD_ONE(dd)) {
  134. if (expected >= dd->sizeZ) {
  135. return(DD_ONE(dd));
  136. } else {
  137. return(dd->univ[expected]);
  138. }
  139. }
  140. Breg = Cudd_Regular(B);
  141. /* Computed table look-up. */
  142. res = cuddCacheLookup1Zdd(dd,Cudd_zddPortFromBdd,B);
  143. if (res != NULL) {
  144. level = cuddI(dd,Breg->index);
  145. /* Adding DC vars. */
  146. if (expected < level) {
  147. /* Add suppressed variables. */
  148. cuddRef(res);
  149. for (level--; level >= expected; level--) {
  150. prevZdd = res;
  151. id = dd->invperm[level];
  152. res = cuddZddGetNode(dd, id, prevZdd, prevZdd);
  153. if (res == NULL) {
  154. Cudd_RecursiveDerefZdd(dd, prevZdd);
  155. return(NULL);
  156. }
  157. cuddRef(res);
  158. Cudd_RecursiveDerefZdd(dd, prevZdd);
  159. }
  160. cuddDeref(res);
  161. }
  162. return(res);
  163. } /* end of cache look-up */
  164. if (Cudd_IsComplement(B)) {
  165. Bt = Cudd_Not(cuddT(Breg));
  166. Be = Cudd_Not(cuddE(Breg));
  167. } else {
  168. Bt = cuddT(Breg);
  169. Be = cuddE(Breg);
  170. }
  171. id = (int) Breg->index;
  172. level = cuddI(dd,id);
  173. t = zddPortFromBddStep(dd, Bt, level+1);
  174. if (t == NULL) return(NULL);
  175. cuddRef(t);
  176. e = zddPortFromBddStep(dd, Be, level+1);
  177. if (e == NULL) {
  178. Cudd_RecursiveDerefZdd(dd, t);
  179. return(NULL);
  180. }
  181. cuddRef(e);
  182. res = cuddZddGetNode(dd, id, t, e);
  183. if (res == NULL) {
  184. Cudd_RecursiveDerefZdd(dd, t);
  185. Cudd_RecursiveDerefZdd(dd, e);
  186. return(NULL);
  187. }
  188. cuddRef(res);
  189. Cudd_RecursiveDerefZdd(dd, t);
  190. Cudd_RecursiveDerefZdd(dd, e);
  191. cuddCacheInsert1(dd,Cudd_zddPortFromBdd,B,res);
  192. for (level--; level >= expected; level--) {
  193. prevZdd = res;
  194. id = dd->invperm[level];
  195. res = cuddZddGetNode(dd, id, prevZdd, prevZdd);
  196. if (res == NULL) {
  197. Cudd_RecursiveDerefZdd(dd, prevZdd);
  198. return(NULL);
  199. }
  200. cuddRef(res);
  201. Cudd_RecursiveDerefZdd(dd, prevZdd);
  202. }
  203. cuddDeref(res);
  204. return(res);
  205. } /* end of zddPortFromBddStep */
  206. /**
  207. @brief Performs the recursive step of Cudd_zddPortToBdd.
  208. @sideeffect None
  209. */
  210. static DdNode *
  211. zddPortToBddStep(
  212. DdManager * dd /* manager */,
  213. DdNode * f /* %ZDD to be converted */,
  214. int depth /* recursion depth */)
  215. {
  216. DdNode *one, *zero, *T, *E, *res, *var;
  217. int index;
  218. int level;
  219. statLine(dd);
  220. one = DD_ONE(dd);
  221. zero = DD_ZERO(dd);
  222. if (f == zero) return(Cudd_Not(one));
  223. if (depth == dd->sizeZ) return(one);
  224. index = dd->invpermZ[depth];
  225. level = cuddIZ(dd,f->index);
  226. var = cuddUniqueInter(dd,index,one,Cudd_Not(one));
  227. if (var == NULL) return(NULL);
  228. cuddRef(var);
  229. if (level > depth) {
  230. E = zddPortToBddStep(dd,f,depth+1);
  231. if (E == NULL) {
  232. Cudd_RecursiveDeref(dd,var);
  233. return(NULL);
  234. }
  235. cuddRef(E);
  236. res = cuddBddIteRecur(dd,var,Cudd_Not(one),E);
  237. if (res == NULL) {
  238. Cudd_RecursiveDeref(dd,var);
  239. Cudd_RecursiveDeref(dd,E);
  240. return(NULL);
  241. }
  242. cuddRef(res);
  243. Cudd_RecursiveDeref(dd,var);
  244. Cudd_RecursiveDeref(dd,E);
  245. cuddDeref(res);
  246. return(res);
  247. }
  248. res = cuddCacheLookup1(dd,Cudd_zddPortToBdd,f);
  249. if (res != NULL) {
  250. Cudd_RecursiveDeref(dd,var);
  251. return(res);
  252. }
  253. T = zddPortToBddStep(dd,cuddT(f),depth+1);
  254. if (T == NULL) {
  255. Cudd_RecursiveDeref(dd,var);
  256. return(NULL);
  257. }
  258. cuddRef(T);
  259. E = zddPortToBddStep(dd,cuddE(f),depth+1);
  260. if (E == NULL) {
  261. Cudd_RecursiveDeref(dd,var);
  262. Cudd_RecursiveDeref(dd,T);
  263. return(NULL);
  264. }
  265. cuddRef(E);
  266. res = cuddBddIteRecur(dd,var,T,E);
  267. if (res == NULL) {
  268. Cudd_RecursiveDeref(dd,var);
  269. Cudd_RecursiveDeref(dd,T);
  270. Cudd_RecursiveDeref(dd,E);
  271. return(NULL);
  272. }
  273. cuddRef(res);
  274. Cudd_RecursiveDeref(dd,var);
  275. Cudd_RecursiveDeref(dd,T);
  276. Cudd_RecursiveDeref(dd,E);
  277. cuddDeref(res);
  278. cuddCacheInsert1(dd,Cudd_zddPortToBdd,f,res);
  279. return(res);
  280. } /* end of zddPortToBddStep */