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.

890 lines
23 KiB

  1. /**
  2. @file
  3. @ingroup cudd
  4. @brief Functions to find irredundant SOP covers as ZDDs from BDDs.
  5. @author 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. /** \endcond */
  56. /*---------------------------------------------------------------------------*/
  57. /* Definition of exported functions */
  58. /*---------------------------------------------------------------------------*/
  59. /**
  60. @brief Computes an ISOP in %ZDD form from BDDs.
  61. @details Computes an irredundant sum of products (ISOP) in %ZDD
  62. form from BDDs. The two BDDs L and U represent the lower bound and
  63. the upper bound, respectively, of the function. The ISOP uses two
  64. %ZDD variables for each %BDD variable: One for the positive literal,
  65. and one for the negative literal. These two variables should be
  66. adjacent in the %ZDD order. The two %ZDD variables corresponding to
  67. %BDD variable <code>i</code> should have indices <code>2i</code> and
  68. <code>2i+1</code>. The result of this procedure depends on the
  69. variable order. If successful, Cudd_zddIsop returns the %BDD for
  70. the function chosen from the interval. The %ZDD representing the
  71. irredundant cover is returned as a side effect in zdd_I. In case of
  72. failure, NULL is returned.
  73. @return the %BDD for the chosen function if successful; NULL otherwise.
  74. @sideeffect zdd_I holds the pointer to the %ZDD for the ISOP on
  75. successful return.
  76. @see Cudd_bddIsop Cudd_zddVarsFromBddVars
  77. */
  78. DdNode *
  79. Cudd_zddIsop(
  80. DdManager * dd,
  81. DdNode * L,
  82. DdNode * U,
  83. DdNode ** zdd_I)
  84. {
  85. DdNode *res;
  86. int autoDynZ;
  87. autoDynZ = dd->autoDynZ;
  88. dd->autoDynZ = 0;
  89. do {
  90. dd->reordered = 0;
  91. res = cuddZddIsop(dd, L, U, zdd_I);
  92. } while (dd->reordered == 1);
  93. dd->autoDynZ = autoDynZ;
  94. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  95. dd->timeoutHandler(dd, dd->tohArg);
  96. }
  97. return(res);
  98. } /* end of Cudd_zddIsop */
  99. /**
  100. @brief Computes a %BDD in the interval between L and U with a
  101. simple sum-of-product cover.
  102. @details This procedure is similar to Cudd_zddIsop, but it does not
  103. return the %ZDD for the cover.
  104. @return a pointer to the %BDD if successful; NULL otherwise.
  105. @sideeffect None
  106. @see Cudd_zddIsop
  107. */
  108. DdNode *
  109. Cudd_bddIsop(
  110. DdManager * dd,
  111. DdNode * L,
  112. DdNode * U)
  113. {
  114. DdNode *res;
  115. do {
  116. dd->reordered = 0;
  117. res = cuddBddIsop(dd, L, U);
  118. } while (dd->reordered == 1);
  119. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  120. dd->timeoutHandler(dd, dd->tohArg);
  121. }
  122. return(res);
  123. } /* end of Cudd_bddIsop */
  124. /**
  125. @brief Converts a %ZDD cover to a %BDD.
  126. @details Converts a %ZDD cover to a %BDD for the function represented
  127. by the cover.
  128. @return a %BDD node if successful; otherwise it returns NULL.
  129. @see Cudd_zddIsop
  130. */
  131. DdNode *
  132. Cudd_MakeBddFromZddCover(
  133. DdManager * dd,
  134. DdNode * node)
  135. {
  136. DdNode *res;
  137. do {
  138. dd->reordered = 0;
  139. res = cuddMakeBddFromZddCover(dd, node);
  140. } while (dd->reordered == 1);
  141. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  142. dd->timeoutHandler(dd, dd->tohArg);
  143. }
  144. return(res);
  145. } /* end of Cudd_MakeBddFromZddCover */
  146. /*---------------------------------------------------------------------------*/
  147. /* Definition of internal functions */
  148. /*---------------------------------------------------------------------------*/
  149. /**
  150. @brief Performs the recursive step of Cudd_zddIsop.
  151. @sideeffect None
  152. @see Cudd_zddIsop
  153. */
  154. DdNode *
  155. cuddZddIsop(
  156. DdManager * dd,
  157. DdNode * L,
  158. DdNode * U,
  159. DdNode ** zdd_I)
  160. {
  161. DdNode *one = DD_ONE(dd);
  162. DdNode *zero = Cudd_Not(one);
  163. DdNode *zdd_one = DD_ONE(dd);
  164. DdNode *zdd_zero = DD_ZERO(dd);
  165. int v, top_l, top_u;
  166. DdNode *Lsub0, *Usub0, *Lsub1, *Usub1, *Ld, *Ud;
  167. DdNode *Lsuper0, *Usuper0, *Lsuper1, *Usuper1;
  168. DdNode *Isub0, *Isub1, *Id;
  169. DdNode *zdd_Isub0, *zdd_Isub1, *zdd_Id;
  170. DdNode *x;
  171. DdNode *term0, *term1, *sum;
  172. DdNode *Lv, *Uv, *Lnv, *Unv;
  173. DdNode *r, *y, *z;
  174. unsigned index;
  175. DD_CTFP cacheOp;
  176. statLine(dd);
  177. if (L == zero) {
  178. *zdd_I = zdd_zero;
  179. return(zero);
  180. }
  181. if (U == one) {
  182. *zdd_I = zdd_one;
  183. return(one);
  184. }
  185. if (U == zero || L == one) {
  186. printf("*** ERROR : illegal condition for ISOP (U < L).\n");
  187. exit(1);
  188. }
  189. /* Check the cache. We store two results for each recursive call.
  190. ** One is the BDD, and the other is the ZDD. Both are needed.
  191. ** Hence we need a double hit in the cache to terminate the
  192. ** recursion. Clearly, collisions may evict only one of the two
  193. ** results. */
  194. cacheOp = (DD_CTFP) cuddZddIsop;
  195. r = cuddCacheLookup2(dd, cuddBddIsop, L, U);
  196. if (r) {
  197. *zdd_I = cuddCacheLookup2Zdd(dd, cacheOp, L, U);
  198. if (*zdd_I)
  199. return(r);
  200. else {
  201. /* The BDD result may have been dead. In that case
  202. ** cuddCacheLookup2 would have called cuddReclaim,
  203. ** whose effects we now have to undo. */
  204. cuddRef(r);
  205. Cudd_RecursiveDeref(dd, r);
  206. }
  207. }
  208. top_l = dd->perm[Cudd_Regular(L)->index];
  209. top_u = dd->perm[Cudd_Regular(U)->index];
  210. v = ddMin(top_l, top_u);
  211. /* Compute cofactors. */
  212. if (top_l == v) {
  213. index = Cudd_Regular(L)->index;
  214. Lv = Cudd_T(L);
  215. Lnv = Cudd_E(L);
  216. if (Cudd_IsComplement(L)) {
  217. Lv = Cudd_Not(Lv);
  218. Lnv = Cudd_Not(Lnv);
  219. }
  220. }
  221. else {
  222. index = Cudd_Regular(U)->index;
  223. Lv = Lnv = L;
  224. }
  225. if (top_u == v) {
  226. Uv = Cudd_T(U);
  227. Unv = Cudd_E(U);
  228. if (Cudd_IsComplement(U)) {
  229. Uv = Cudd_Not(Uv);
  230. Unv = Cudd_Not(Unv);
  231. }
  232. }
  233. else {
  234. Uv = Unv = U;
  235. }
  236. Lsub0 = cuddBddAndRecur(dd, Lnv, Cudd_Not(Uv));
  237. if (Lsub0 == NULL)
  238. return(NULL);
  239. Cudd_Ref(Lsub0);
  240. Usub0 = Unv;
  241. Lsub1 = cuddBddAndRecur(dd, Lv, Cudd_Not(Unv));
  242. if (Lsub1 == NULL) {
  243. Cudd_RecursiveDeref(dd, Lsub0);
  244. return(NULL);
  245. }
  246. Cudd_Ref(Lsub1);
  247. Usub1 = Uv;
  248. Isub0 = cuddZddIsop(dd, Lsub0, Usub0, &zdd_Isub0);
  249. if (Isub0 == NULL) {
  250. Cudd_RecursiveDeref(dd, Lsub0);
  251. Cudd_RecursiveDeref(dd, Lsub1);
  252. return(NULL);
  253. }
  254. /*
  255. if ((!cuddIsConstant(Cudd_Regular(Isub0))) &&
  256. (Cudd_Regular(Isub0)->index != zdd_Isub0->index / 2 ||
  257. dd->permZ[index * 2] > dd->permZ[zdd_Isub0->index])) {
  258. printf("*** ERROR : illegal permutation in ZDD. ***\n");
  259. }
  260. */
  261. Cudd_Ref(Isub0);
  262. Cudd_Ref(zdd_Isub0);
  263. Isub1 = cuddZddIsop(dd, Lsub1, Usub1, &zdd_Isub1);
  264. if (Isub1 == NULL) {
  265. Cudd_RecursiveDeref(dd, Lsub0);
  266. Cudd_RecursiveDeref(dd, Lsub1);
  267. Cudd_RecursiveDeref(dd, Isub0);
  268. Cudd_RecursiveDerefZdd(dd, zdd_Isub0);
  269. return(NULL);
  270. }
  271. /*
  272. if ((!cuddIsConstant(Cudd_Regular(Isub1))) &&
  273. (Cudd_Regular(Isub1)->index != zdd_Isub1->index / 2 ||
  274. dd->permZ[index * 2] > dd->permZ[zdd_Isub1->index])) {
  275. printf("*** ERROR : illegal permutation in ZDD. ***\n");
  276. }
  277. */
  278. Cudd_Ref(Isub1);
  279. Cudd_Ref(zdd_Isub1);
  280. Cudd_RecursiveDeref(dd, Lsub0);
  281. Cudd_RecursiveDeref(dd, Lsub1);
  282. Lsuper0 = cuddBddAndRecur(dd, Lnv, Cudd_Not(Isub0));
  283. if (Lsuper0 == NULL) {
  284. Cudd_RecursiveDeref(dd, Isub0);
  285. Cudd_RecursiveDerefZdd(dd, zdd_Isub0);
  286. Cudd_RecursiveDeref(dd, Isub1);
  287. Cudd_RecursiveDerefZdd(dd, zdd_Isub1);
  288. return(NULL);
  289. }
  290. Cudd_Ref(Lsuper0);
  291. Lsuper1 = cuddBddAndRecur(dd, Lv, Cudd_Not(Isub1));
  292. if (Lsuper1 == NULL) {
  293. Cudd_RecursiveDeref(dd, Isub0);
  294. Cudd_RecursiveDerefZdd(dd, zdd_Isub0);
  295. Cudd_RecursiveDeref(dd, Isub1);
  296. Cudd_RecursiveDerefZdd(dd, zdd_Isub1);
  297. Cudd_RecursiveDeref(dd, Lsuper0);
  298. return(NULL);
  299. }
  300. Cudd_Ref(Lsuper1);
  301. Usuper0 = Unv;
  302. Usuper1 = Uv;
  303. /* Ld = Lsuper0 + Lsuper1 */
  304. Ld = cuddBddAndRecur(dd, Cudd_Not(Lsuper0), Cudd_Not(Lsuper1));
  305. if (Ld == NULL) {
  306. Cudd_RecursiveDeref(dd, Isub0);
  307. Cudd_RecursiveDerefZdd(dd, zdd_Isub0);
  308. Cudd_RecursiveDeref(dd, Isub1);
  309. Cudd_RecursiveDerefZdd(dd, zdd_Isub1);
  310. Cudd_RecursiveDeref(dd, Lsuper0);
  311. Cudd_RecursiveDeref(dd, Lsuper1);
  312. return(NULL);
  313. }
  314. Ld = Cudd_Not(Ld);
  315. Cudd_Ref(Ld);
  316. /* Ud = Usuper0 * Usuper1 */
  317. Ud = cuddBddAndRecur(dd, Usuper0, Usuper1);
  318. if (Ud == NULL) {
  319. Cudd_RecursiveDeref(dd, Isub0);
  320. Cudd_RecursiveDerefZdd(dd, zdd_Isub0);
  321. Cudd_RecursiveDeref(dd, Isub1);
  322. Cudd_RecursiveDerefZdd(dd, zdd_Isub1);
  323. Cudd_RecursiveDeref(dd, Lsuper0);
  324. Cudd_RecursiveDeref(dd, Lsuper1);
  325. Cudd_RecursiveDeref(dd, Ld);
  326. return(NULL);
  327. }
  328. Cudd_Ref(Ud);
  329. Cudd_RecursiveDeref(dd, Lsuper0);
  330. Cudd_RecursiveDeref(dd, Lsuper1);
  331. Id = cuddZddIsop(dd, Ld, Ud, &zdd_Id);
  332. if (Id == NULL) {
  333. Cudd_RecursiveDeref(dd, Isub0);
  334. Cudd_RecursiveDerefZdd(dd, zdd_Isub0);
  335. Cudd_RecursiveDeref(dd, Isub1);
  336. Cudd_RecursiveDerefZdd(dd, zdd_Isub1);
  337. Cudd_RecursiveDeref(dd, Ld);
  338. Cudd_RecursiveDeref(dd, Ud);
  339. return(NULL);
  340. }
  341. /*
  342. if ((!cuddIsConstant(Cudd_Regular(Id))) &&
  343. (Cudd_Regular(Id)->index != zdd_Id->index / 2 ||
  344. dd->permZ[index * 2] > dd->permZ[zdd_Id->index])) {
  345. printf("*** ERROR : illegal permutation in ZDD. ***\n");
  346. }
  347. */
  348. Cudd_Ref(Id);
  349. Cudd_Ref(zdd_Id);
  350. Cudd_RecursiveDeref(dd, Ld);
  351. Cudd_RecursiveDeref(dd, Ud);
  352. x = cuddUniqueInter(dd, index, one, zero);
  353. if (x == NULL) {
  354. Cudd_RecursiveDeref(dd, Isub0);
  355. Cudd_RecursiveDerefZdd(dd, zdd_Isub0);
  356. Cudd_RecursiveDeref(dd, Isub1);
  357. Cudd_RecursiveDerefZdd(dd, zdd_Isub1);
  358. Cudd_RecursiveDeref(dd, Id);
  359. Cudd_RecursiveDerefZdd(dd, zdd_Id);
  360. return(NULL);
  361. }
  362. Cudd_Ref(x);
  363. /* term0 = x * Isub0 */
  364. term0 = cuddBddAndRecur(dd, Cudd_Not(x), Isub0);
  365. if (term0 == NULL) {
  366. Cudd_RecursiveDeref(dd, Isub0);
  367. Cudd_RecursiveDerefZdd(dd, zdd_Isub0);
  368. Cudd_RecursiveDeref(dd, Isub1);
  369. Cudd_RecursiveDerefZdd(dd, zdd_Isub1);
  370. Cudd_RecursiveDeref(dd, Id);
  371. Cudd_RecursiveDerefZdd(dd, zdd_Id);
  372. Cudd_RecursiveDeref(dd, x);
  373. return(NULL);
  374. }
  375. Cudd_Ref(term0);
  376. Cudd_RecursiveDeref(dd, Isub0);
  377. /* term1 = x * Isub1 */
  378. term1 = cuddBddAndRecur(dd, x, Isub1);
  379. if (term1 == NULL) {
  380. Cudd_RecursiveDerefZdd(dd, zdd_Isub0);
  381. Cudd_RecursiveDeref(dd, Isub1);
  382. Cudd_RecursiveDerefZdd(dd, zdd_Isub1);
  383. Cudd_RecursiveDeref(dd, Id);
  384. Cudd_RecursiveDerefZdd(dd, zdd_Id);
  385. Cudd_RecursiveDeref(dd, x);
  386. Cudd_RecursiveDeref(dd, term0);
  387. return(NULL);
  388. }
  389. Cudd_Ref(term1);
  390. Cudd_RecursiveDeref(dd, x);
  391. Cudd_RecursiveDeref(dd, Isub1);
  392. /* sum = term0 + term1 */
  393. sum = cuddBddAndRecur(dd, Cudd_Not(term0), Cudd_Not(term1));
  394. if (sum == NULL) {
  395. Cudd_RecursiveDerefZdd(dd, zdd_Isub0);
  396. Cudd_RecursiveDerefZdd(dd, zdd_Isub1);
  397. Cudd_RecursiveDeref(dd, Id);
  398. Cudd_RecursiveDerefZdd(dd, zdd_Id);
  399. Cudd_RecursiveDeref(dd, term0);
  400. Cudd_RecursiveDeref(dd, term1);
  401. return(NULL);
  402. }
  403. sum = Cudd_Not(sum);
  404. Cudd_Ref(sum);
  405. Cudd_RecursiveDeref(dd, term0);
  406. Cudd_RecursiveDeref(dd, term1);
  407. /* r = sum + Id */
  408. r = cuddBddAndRecur(dd, Cudd_Not(sum), Cudd_Not(Id));
  409. r = Cudd_NotCond(r, r != NULL);
  410. if (r == NULL) {
  411. Cudd_RecursiveDerefZdd(dd, zdd_Isub0);
  412. Cudd_RecursiveDerefZdd(dd, zdd_Isub1);
  413. Cudd_RecursiveDeref(dd, Id);
  414. Cudd_RecursiveDerefZdd(dd, zdd_Id);
  415. Cudd_RecursiveDeref(dd, sum);
  416. return(NULL);
  417. }
  418. Cudd_Ref(r);
  419. Cudd_RecursiveDeref(dd, sum);
  420. Cudd_RecursiveDeref(dd, Id);
  421. if (zdd_Isub0 != zdd_zero) {
  422. z = cuddZddGetNodeIVO(dd, index * 2 + 1, zdd_Isub0, zdd_Id);
  423. if (z == NULL) {
  424. Cudd_RecursiveDerefZdd(dd, zdd_Isub0);
  425. Cudd_RecursiveDerefZdd(dd, zdd_Isub1);
  426. Cudd_RecursiveDerefZdd(dd, zdd_Id);
  427. Cudd_RecursiveDeref(dd, r);
  428. return(NULL);
  429. }
  430. }
  431. else {
  432. z = zdd_Id;
  433. }
  434. Cudd_Ref(z);
  435. if (zdd_Isub1 != zdd_zero) {
  436. y = cuddZddGetNodeIVO(dd, index * 2, zdd_Isub1, z);
  437. if (y == NULL) {
  438. Cudd_RecursiveDerefZdd(dd, zdd_Isub0);
  439. Cudd_RecursiveDerefZdd(dd, zdd_Isub1);
  440. Cudd_RecursiveDerefZdd(dd, zdd_Id);
  441. Cudd_RecursiveDeref(dd, r);
  442. Cudd_RecursiveDerefZdd(dd, z);
  443. return(NULL);
  444. }
  445. }
  446. else
  447. y = z;
  448. Cudd_Ref(y);
  449. Cudd_RecursiveDerefZdd(dd, zdd_Isub0);
  450. Cudd_RecursiveDerefZdd(dd, zdd_Isub1);
  451. Cudd_RecursiveDerefZdd(dd, zdd_Id);
  452. Cudd_RecursiveDerefZdd(dd, z);
  453. cuddCacheInsert2(dd, cuddBddIsop, L, U, r);
  454. cuddCacheInsert2(dd, cacheOp, L, U, y);
  455. Cudd_Deref(r);
  456. Cudd_Deref(y);
  457. *zdd_I = y;
  458. /*
  459. if (Cudd_Regular(r)->index != y->index / 2) {
  460. printf("*** ERROR : mismatch in indices between BDD and ZDD. ***\n");
  461. }
  462. */
  463. return(r);
  464. } /* end of cuddZddIsop */
  465. /**
  466. @brief Performs the recursive step of Cudd_bddIsop.
  467. @sideeffect None
  468. @see Cudd_bddIsop
  469. */
  470. DdNode *
  471. cuddBddIsop(
  472. DdManager * dd,
  473. DdNode * L,
  474. DdNode * U)
  475. {
  476. DdNode *one = DD_ONE(dd);
  477. DdNode *zero = Cudd_Not(one);
  478. int v, top_l, top_u;
  479. DdNode *Lsub0, *Usub0, *Lsub1, *Usub1, *Ld, *Ud;
  480. DdNode *Lsuper0, *Usuper0, *Lsuper1, *Usuper1;
  481. DdNode *Isub0, *Isub1, *Id;
  482. DdNode *x;
  483. DdNode *term0, *term1, *sum;
  484. DdNode *Lv, *Uv, *Lnv, *Unv;
  485. DdNode *r;
  486. unsigned index;
  487. statLine(dd);
  488. if (L == zero)
  489. return(zero);
  490. if (U == one)
  491. return(one);
  492. /* Check cache */
  493. r = cuddCacheLookup2(dd, cuddBddIsop, L, U);
  494. if (r)
  495. return(r);
  496. top_l = dd->perm[Cudd_Regular(L)->index];
  497. top_u = dd->perm[Cudd_Regular(U)->index];
  498. v = ddMin(top_l, top_u);
  499. /* Compute cofactors */
  500. if (top_l == v) {
  501. index = Cudd_Regular(L)->index;
  502. Lv = Cudd_T(L);
  503. Lnv = Cudd_E(L);
  504. if (Cudd_IsComplement(L)) {
  505. Lv = Cudd_Not(Lv);
  506. Lnv = Cudd_Not(Lnv);
  507. }
  508. }
  509. else {
  510. index = Cudd_Regular(U)->index;
  511. Lv = Lnv = L;
  512. }
  513. if (top_u == v) {
  514. Uv = Cudd_T(U);
  515. Unv = Cudd_E(U);
  516. if (Cudd_IsComplement(U)) {
  517. Uv = Cudd_Not(Uv);
  518. Unv = Cudd_Not(Unv);
  519. }
  520. }
  521. else {
  522. Uv = Unv = U;
  523. }
  524. Lsub0 = cuddBddAndRecur(dd, Lnv, Cudd_Not(Uv));
  525. if (Lsub0 == NULL)
  526. return(NULL);
  527. Cudd_Ref(Lsub0);
  528. Usub0 = Unv;
  529. Lsub1 = cuddBddAndRecur(dd, Lv, Cudd_Not(Unv));
  530. if (Lsub1 == NULL) {
  531. Cudd_RecursiveDeref(dd, Lsub0);
  532. return(NULL);
  533. }
  534. Cudd_Ref(Lsub1);
  535. Usub1 = Uv;
  536. Isub0 = cuddBddIsop(dd, Lsub0, Usub0);
  537. if (Isub0 == NULL) {
  538. Cudd_RecursiveDeref(dd, Lsub0);
  539. Cudd_RecursiveDeref(dd, Lsub1);
  540. return(NULL);
  541. }
  542. Cudd_Ref(Isub0);
  543. Isub1 = cuddBddIsop(dd, Lsub1, Usub1);
  544. if (Isub1 == NULL) {
  545. Cudd_RecursiveDeref(dd, Lsub0);
  546. Cudd_RecursiveDeref(dd, Lsub1);
  547. Cudd_RecursiveDeref(dd, Isub0);
  548. return(NULL);
  549. }
  550. Cudd_Ref(Isub1);
  551. Cudd_RecursiveDeref(dd, Lsub0);
  552. Cudd_RecursiveDeref(dd, Lsub1);
  553. Lsuper0 = cuddBddAndRecur(dd, Lnv, Cudd_Not(Isub0));
  554. if (Lsuper0 == NULL) {
  555. Cudd_RecursiveDeref(dd, Isub0);
  556. Cudd_RecursiveDeref(dd, Isub1);
  557. return(NULL);
  558. }
  559. Cudd_Ref(Lsuper0);
  560. Lsuper1 = cuddBddAndRecur(dd, Lv, Cudd_Not(Isub1));
  561. if (Lsuper1 == NULL) {
  562. Cudd_RecursiveDeref(dd, Isub0);
  563. Cudd_RecursiveDeref(dd, Isub1);
  564. Cudd_RecursiveDeref(dd, Lsuper0);
  565. return(NULL);
  566. }
  567. Cudd_Ref(Lsuper1);
  568. Usuper0 = Unv;
  569. Usuper1 = Uv;
  570. /* Ld = Lsuper0 + Lsuper1 */
  571. Ld = cuddBddAndRecur(dd, Cudd_Not(Lsuper0), Cudd_Not(Lsuper1));
  572. Ld = Cudd_NotCond(Ld, Ld != NULL);
  573. if (Ld == NULL) {
  574. Cudd_RecursiveDeref(dd, Isub0);
  575. Cudd_RecursiveDeref(dd, Isub1);
  576. Cudd_RecursiveDeref(dd, Lsuper0);
  577. Cudd_RecursiveDeref(dd, Lsuper1);
  578. return(NULL);
  579. }
  580. Cudd_Ref(Ld);
  581. Ud = cuddBddAndRecur(dd, Usuper0, Usuper1);
  582. if (Ud == NULL) {
  583. Cudd_RecursiveDeref(dd, Isub0);
  584. Cudd_RecursiveDeref(dd, Isub1);
  585. Cudd_RecursiveDeref(dd, Lsuper0);
  586. Cudd_RecursiveDeref(dd, Lsuper1);
  587. Cudd_RecursiveDeref(dd, Ld);
  588. return(NULL);
  589. }
  590. Cudd_Ref(Ud);
  591. Cudd_RecursiveDeref(dd, Lsuper0);
  592. Cudd_RecursiveDeref(dd, Lsuper1);
  593. Id = cuddBddIsop(dd, Ld, Ud);
  594. if (Id == NULL) {
  595. Cudd_RecursiveDeref(dd, Isub0);
  596. Cudd_RecursiveDeref(dd, Isub1);
  597. Cudd_RecursiveDeref(dd, Ld);
  598. Cudd_RecursiveDeref(dd, Ud);
  599. return(NULL);
  600. }
  601. Cudd_Ref(Id);
  602. Cudd_RecursiveDeref(dd, Ld);
  603. Cudd_RecursiveDeref(dd, Ud);
  604. x = cuddUniqueInter(dd, index, one, zero);
  605. if (x == NULL) {
  606. Cudd_RecursiveDeref(dd, Isub0);
  607. Cudd_RecursiveDeref(dd, Isub1);
  608. Cudd_RecursiveDeref(dd, Id);
  609. return(NULL);
  610. }
  611. Cudd_Ref(x);
  612. term0 = cuddBddAndRecur(dd, Cudd_Not(x), Isub0);
  613. if (term0 == NULL) {
  614. Cudd_RecursiveDeref(dd, Isub0);
  615. Cudd_RecursiveDeref(dd, Isub1);
  616. Cudd_RecursiveDeref(dd, Id);
  617. Cudd_RecursiveDeref(dd, x);
  618. return(NULL);
  619. }
  620. Cudd_Ref(term0);
  621. Cudd_RecursiveDeref(dd, Isub0);
  622. term1 = cuddBddAndRecur(dd, x, Isub1);
  623. if (term1 == NULL) {
  624. Cudd_RecursiveDeref(dd, Isub1);
  625. Cudd_RecursiveDeref(dd, Id);
  626. Cudd_RecursiveDeref(dd, x);
  627. Cudd_RecursiveDeref(dd, term0);
  628. return(NULL);
  629. }
  630. Cudd_Ref(term1);
  631. Cudd_RecursiveDeref(dd, x);
  632. Cudd_RecursiveDeref(dd, Isub1);
  633. /* sum = term0 + term1 */
  634. sum = cuddBddAndRecur(dd, Cudd_Not(term0), Cudd_Not(term1));
  635. sum = Cudd_NotCond(sum, sum != NULL);
  636. if (sum == NULL) {
  637. Cudd_RecursiveDeref(dd, Id);
  638. Cudd_RecursiveDeref(dd, term0);
  639. Cudd_RecursiveDeref(dd, term1);
  640. return(NULL);
  641. }
  642. Cudd_Ref(sum);
  643. Cudd_RecursiveDeref(dd, term0);
  644. Cudd_RecursiveDeref(dd, term1);
  645. /* r = sum + Id */
  646. r = cuddBddAndRecur(dd, Cudd_Not(sum), Cudd_Not(Id));
  647. r = Cudd_NotCond(r, r != NULL);
  648. if (r == NULL) {
  649. Cudd_RecursiveDeref(dd, Id);
  650. Cudd_RecursiveDeref(dd, sum);
  651. return(NULL);
  652. }
  653. Cudd_Ref(r);
  654. Cudd_RecursiveDeref(dd, sum);
  655. Cudd_RecursiveDeref(dd, Id);
  656. cuddCacheInsert2(dd, cuddBddIsop, L, U, r);
  657. Cudd_Deref(r);
  658. return(r);
  659. } /* end of cuddBddIsop */
  660. /**
  661. @brief Converts a %ZDD cover to a %BDD.
  662. @details It is a recursive algorithm that works as follows. First it
  663. computes 3 cofactors of a %ZDD cover: f1, f0 and fd. Second, it
  664. compute BDDs (b1, b0 and bd) of f1, f0 and fd. Third, it computes
  665. T=b1+bd and E=b0+bd. Fourth, it computes ITE(v,T,E) where v is the
  666. variable which has the index of the top node of the %ZDD cover. In
  667. this case, since the index of v can be larger than either the one of
  668. T or the one of E, cuddUniqueInterIVO is called, where IVO stands
  669. for independent from variable ordering.
  670. @return a %BDD node if successful; otherwise it returns NULL.
  671. @see Cudd_MakeBddFromZddCover
  672. */
  673. DdNode *
  674. cuddMakeBddFromZddCover(
  675. DdManager * dd,
  676. DdNode * node)
  677. {
  678. DdNode *neW;
  679. unsigned v;
  680. DdNode *f1, *f0, *fd;
  681. DdNode *b1, *b0, *bd;
  682. DdNode *T, *E;
  683. statLine(dd);
  684. if (node == dd->one)
  685. return(dd->one);
  686. if (node == dd->zero)
  687. return(Cudd_Not(dd->one));
  688. /* Check cache */
  689. neW = cuddCacheLookup1(dd, cuddMakeBddFromZddCover, node);
  690. if (neW)
  691. return(neW);
  692. v = Cudd_Regular(node)->index; /* either yi or zi */
  693. if (cuddZddGetCofactors3(dd, node, v, &f1, &f0, &fd)) return(NULL);
  694. Cudd_Ref(f1);
  695. Cudd_Ref(f0);
  696. Cudd_Ref(fd);
  697. b1 = cuddMakeBddFromZddCover(dd, f1);
  698. if (!b1) {
  699. Cudd_RecursiveDerefZdd(dd, f1);
  700. Cudd_RecursiveDerefZdd(dd, f0);
  701. Cudd_RecursiveDerefZdd(dd, fd);
  702. return(NULL);
  703. }
  704. Cudd_Ref(b1);
  705. b0 = cuddMakeBddFromZddCover(dd, f0);
  706. if (!b0) {
  707. Cudd_RecursiveDerefZdd(dd, f1);
  708. Cudd_RecursiveDerefZdd(dd, f0);
  709. Cudd_RecursiveDerefZdd(dd, fd);
  710. Cudd_RecursiveDeref(dd, b1);
  711. return(NULL);
  712. }
  713. Cudd_Ref(b0);
  714. Cudd_RecursiveDerefZdd(dd, f1);
  715. Cudd_RecursiveDerefZdd(dd, f0);
  716. if (fd != dd->zero) {
  717. bd = cuddMakeBddFromZddCover(dd, fd);
  718. if (!bd) {
  719. Cudd_RecursiveDerefZdd(dd, fd);
  720. Cudd_RecursiveDeref(dd, b1);
  721. Cudd_RecursiveDeref(dd, b0);
  722. return(NULL);
  723. }
  724. Cudd_Ref(bd);
  725. Cudd_RecursiveDerefZdd(dd, fd);
  726. T = cuddBddAndRecur(dd, Cudd_Not(b1), Cudd_Not(bd));
  727. if (!T) {
  728. Cudd_RecursiveDeref(dd, b1);
  729. Cudd_RecursiveDeref(dd, b0);
  730. Cudd_RecursiveDeref(dd, bd);
  731. return(NULL);
  732. }
  733. T = Cudd_NotCond(T, T != NULL);
  734. Cudd_Ref(T);
  735. Cudd_RecursiveDeref(dd, b1);
  736. E = cuddBddAndRecur(dd, Cudd_Not(b0), Cudd_Not(bd));
  737. if (!E) {
  738. Cudd_RecursiveDeref(dd, b0);
  739. Cudd_RecursiveDeref(dd, bd);
  740. Cudd_RecursiveDeref(dd, T);
  741. return(NULL);
  742. }
  743. E = Cudd_NotCond(E, E != NULL);
  744. Cudd_Ref(E);
  745. Cudd_RecursiveDeref(dd, b0);
  746. Cudd_RecursiveDeref(dd, bd);
  747. }
  748. else {
  749. Cudd_RecursiveDerefZdd(dd, fd);
  750. T = b1;
  751. E = b0;
  752. }
  753. if (Cudd_IsComplement(T)) {
  754. neW = cuddUniqueInterIVO(dd, v / 2, Cudd_Not(T), Cudd_Not(E));
  755. if (!neW) {
  756. Cudd_RecursiveDeref(dd, T);
  757. Cudd_RecursiveDeref(dd, E);
  758. return(NULL);
  759. }
  760. neW = Cudd_Not(neW);
  761. }
  762. else {
  763. neW = cuddUniqueInterIVO(dd, v / 2, T, E);
  764. if (!neW) {
  765. Cudd_RecursiveDeref(dd, T);
  766. Cudd_RecursiveDeref(dd, E);
  767. return(NULL);
  768. }
  769. }
  770. Cudd_Ref(neW);
  771. Cudd_RecursiveDeref(dd, T);
  772. Cudd_RecursiveDeref(dd, E);
  773. cuddCacheInsert1(dd, cuddMakeBddFromZddCover, node, neW);
  774. Cudd_Deref(neW);
  775. return(neW);
  776. } /* end of cuddMakeBddFromZddCover */
  777. /*---------------------------------------------------------------------------*/
  778. /* Definition of static functions */
  779. /*---------------------------------------------------------------------------*/