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.

1166 lines
28 KiB

  1. /**CFile***********************************************************************
  2. FileName [cuddZddSetop.c]
  3. PackageName [cudd]
  4. Synopsis [Set operations on ZDDs.]
  5. Description [External procedures included in this module:
  6. <ul>
  7. <li> Cudd_zddIte()
  8. <li> Cudd_zddUnion()
  9. <li> Cudd_zddIntersect()
  10. <li> Cudd_zddDiff()
  11. <li> Cudd_zddDiffConst()
  12. <li> Cudd_zddSubset1()
  13. <li> Cudd_zddSubset0()
  14. <li> Cudd_zddChange()
  15. </ul>
  16. Internal procedures included in this module:
  17. <ul>
  18. <li> cuddZddIte()
  19. <li> cuddZddUnion()
  20. <li> cuddZddIntersect()
  21. <li> cuddZddDiff()
  22. <li> cuddZddChangeAux()
  23. <li> cuddZddSubset1()
  24. <li> cuddZddSubset0()
  25. </ul>
  26. Static procedures included in this module:
  27. <ul>
  28. <li> zdd_subset1_aux()
  29. <li> zdd_subset0_aux()
  30. <li> zddVarToConst()
  31. </ul>
  32. ]
  33. SeeAlso []
  34. Author [Hyong-Kyoon Shin, In-Ho Moon]
  35. Copyright [Copyright (c) 1995-2012, Regents of the University of Colorado
  36. All rights reserved.
  37. Redistribution and use in source and binary forms, with or without
  38. modification, are permitted provided that the following conditions
  39. are met:
  40. Redistributions of source code must retain the above copyright
  41. notice, this list of conditions and the following disclaimer.
  42. Redistributions in binary form must reproduce the above copyright
  43. notice, this list of conditions and the following disclaimer in the
  44. documentation and/or other materials provided with the distribution.
  45. Neither the name of the University of Colorado nor the names of its
  46. contributors may be used to endorse or promote products derived from
  47. this software without specific prior written permission.
  48. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  49. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  50. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  51. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  52. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  53. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  54. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  55. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  56. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  57. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  58. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  59. POSSIBILITY OF SUCH DAMAGE.]
  60. ******************************************************************************/
  61. #include "util.h"
  62. #include "cuddInt.h"
  63. /*---------------------------------------------------------------------------*/
  64. /* Constant declarations */
  65. /*---------------------------------------------------------------------------*/
  66. /*---------------------------------------------------------------------------*/
  67. /* Stucture declarations */
  68. /*---------------------------------------------------------------------------*/
  69. /*---------------------------------------------------------------------------*/
  70. /* Type declarations */
  71. /*---------------------------------------------------------------------------*/
  72. /*---------------------------------------------------------------------------*/
  73. /* Variable declarations */
  74. /*---------------------------------------------------------------------------*/
  75. #ifndef lint
  76. static char rcsid[] DD_UNUSED = "$Id: cuddZddSetop.c,v 1.26 2012/02/05 01:07:19 fabio Exp $";
  77. #endif
  78. /*---------------------------------------------------------------------------*/
  79. /* Macro declarations */
  80. /*---------------------------------------------------------------------------*/
  81. #ifdef __cplusplus
  82. extern "C" {
  83. #endif
  84. /**AutomaticStart*************************************************************/
  85. /*---------------------------------------------------------------------------*/
  86. /* Static function prototypes */
  87. /*---------------------------------------------------------------------------*/
  88. static DdNode * zdd_subset1_aux (DdManager *zdd, DdNode *P, DdNode *zvar);
  89. static DdNode * zdd_subset0_aux (DdManager *zdd, DdNode *P, DdNode *zvar);
  90. static void zddVarToConst (DdNode *f, DdNode **gp, DdNode **hp, DdNode *base, DdNode *empty);
  91. /**AutomaticEnd***************************************************************/
  92. #ifdef __cplusplus
  93. }
  94. #endif
  95. /*---------------------------------------------------------------------------*/
  96. /* Definition of exported functions */
  97. /*---------------------------------------------------------------------------*/
  98. /**Function********************************************************************
  99. Synopsis [Computes the ITE of three ZDDs.]
  100. Description [Computes the ITE of three ZDDs. Returns a pointer to the
  101. result if successful; NULL otherwise.]
  102. SideEffects [None]
  103. SeeAlso []
  104. ******************************************************************************/
  105. DdNode *
  106. Cudd_zddIte(
  107. DdManager * dd,
  108. DdNode * f,
  109. DdNode * g,
  110. DdNode * h)
  111. {
  112. DdNode *res;
  113. do {
  114. dd->reordered = 0;
  115. res = cuddZddIte(dd, f, g, h);
  116. } while (dd->reordered == 1);
  117. return(res);
  118. } /* end of Cudd_zddIte */
  119. /**Function********************************************************************
  120. Synopsis [Computes the union of two ZDDs.]
  121. Description [Computes the union of two ZDDs. Returns a pointer to the
  122. result if successful; NULL otherwise.]
  123. SideEffects [None]
  124. SeeAlso []
  125. ******************************************************************************/
  126. DdNode *
  127. Cudd_zddUnion(
  128. DdManager * dd,
  129. DdNode * P,
  130. DdNode * Q)
  131. {
  132. DdNode *res;
  133. do {
  134. dd->reordered = 0;
  135. res = cuddZddUnion(dd, P, Q);
  136. } while (dd->reordered == 1);
  137. return(res);
  138. } /* end of Cudd_zddUnion */
  139. /**Function********************************************************************
  140. Synopsis [Computes the intersection of two ZDDs.]
  141. Description [Computes the intersection of two ZDDs. Returns a pointer to
  142. the result if successful; NULL otherwise.]
  143. SideEffects [None]
  144. SeeAlso []
  145. ******************************************************************************/
  146. DdNode *
  147. Cudd_zddIntersect(
  148. DdManager * dd,
  149. DdNode * P,
  150. DdNode * Q)
  151. {
  152. DdNode *res;
  153. do {
  154. dd->reordered = 0;
  155. res = cuddZddIntersect(dd, P, Q);
  156. } while (dd->reordered == 1);
  157. return(res);
  158. } /* end of Cudd_zddIntersect */
  159. /**Function********************************************************************
  160. Synopsis [Computes the difference of two ZDDs.]
  161. Description [Computes the difference of two ZDDs. Returns a pointer to the
  162. result if successful; NULL otherwise.]
  163. SideEffects [None]
  164. SeeAlso [Cudd_zddDiffConst]
  165. ******************************************************************************/
  166. DdNode *
  167. Cudd_zddDiff(
  168. DdManager * dd,
  169. DdNode * P,
  170. DdNode * Q)
  171. {
  172. DdNode *res;
  173. do {
  174. dd->reordered = 0;
  175. res = cuddZddDiff(dd, P, Q);
  176. } while (dd->reordered == 1);
  177. return(res);
  178. } /* end of Cudd_zddDiff */
  179. /**Function********************************************************************
  180. Synopsis [Performs the inclusion test for ZDDs (P implies Q).]
  181. Description [Inclusion test for ZDDs (P implies Q). No new nodes are
  182. generated by this procedure. Returns empty if true;
  183. a valid pointer different from empty or DD_NON_CONSTANT otherwise.]
  184. SideEffects [None]
  185. SeeAlso [Cudd_zddDiff]
  186. ******************************************************************************/
  187. DdNode *
  188. Cudd_zddDiffConst(
  189. DdManager * zdd,
  190. DdNode * P,
  191. DdNode * Q)
  192. {
  193. int p_top, q_top;
  194. DdNode *empty = DD_ZERO(zdd), *t, *res;
  195. DdManager *table = zdd;
  196. statLine(zdd);
  197. if (P == empty)
  198. return(empty);
  199. if (Q == empty)
  200. return(P);
  201. if (P == Q)
  202. return(empty);
  203. /* Check cache. The cache is shared by cuddZddDiff(). */
  204. res = cuddCacheLookup2Zdd(table, cuddZddDiff, P, Q);
  205. if (res != NULL)
  206. return(res);
  207. if (cuddIsConstant(P))
  208. p_top = P->index;
  209. else
  210. p_top = zdd->permZ[P->index];
  211. if (cuddIsConstant(Q))
  212. q_top = Q->index;
  213. else
  214. q_top = zdd->permZ[Q->index];
  215. if (p_top < q_top) {
  216. res = DD_NON_CONSTANT;
  217. } else if (p_top > q_top) {
  218. res = Cudd_zddDiffConst(zdd, P, cuddE(Q));
  219. } else {
  220. t = Cudd_zddDiffConst(zdd, cuddT(P), cuddT(Q));
  221. if (t != empty)
  222. res = DD_NON_CONSTANT;
  223. else
  224. res = Cudd_zddDiffConst(zdd, cuddE(P), cuddE(Q));
  225. }
  226. cuddCacheInsert2(table, cuddZddDiff, P, Q, res);
  227. return(res);
  228. } /* end of Cudd_zddDiffConst */
  229. /**Function********************************************************************
  230. Synopsis [Computes the positive cofactor of a ZDD w.r.t. a variable.]
  231. Description [Computes the positive cofactor of a ZDD w.r.t. a
  232. variable. In terms of combinations, the result is the set of all
  233. combinations in which the variable is asserted. Returns a pointer to
  234. the result if successful; NULL otherwise.]
  235. SideEffects [None]
  236. SeeAlso [Cudd_zddSubset0]
  237. ******************************************************************************/
  238. DdNode *
  239. Cudd_zddSubset1(
  240. DdManager * dd,
  241. DdNode * P,
  242. int var)
  243. {
  244. DdNode *r;
  245. do {
  246. dd->reordered = 0;
  247. r = cuddZddSubset1(dd, P, var);
  248. } while (dd->reordered == 1);
  249. return(r);
  250. } /* end of Cudd_zddSubset1 */
  251. /**Function********************************************************************
  252. Synopsis [Computes the negative cofactor of a ZDD w.r.t. a variable.]
  253. Description [Computes the negative cofactor of a ZDD w.r.t. a
  254. variable. In terms of combinations, the result is the set of all
  255. combinations in which the variable is negated. Returns a pointer to
  256. the result if successful; NULL otherwise.]
  257. SideEffects [None]
  258. SeeAlso [Cudd_zddSubset1]
  259. ******************************************************************************/
  260. DdNode *
  261. Cudd_zddSubset0(
  262. DdManager * dd,
  263. DdNode * P,
  264. int var)
  265. {
  266. DdNode *r;
  267. do {
  268. dd->reordered = 0;
  269. r = cuddZddSubset0(dd, P, var);
  270. } while (dd->reordered == 1);
  271. return(r);
  272. } /* end of Cudd_zddSubset0 */
  273. /**Function********************************************************************
  274. Synopsis [Substitutes a variable with its complement in a ZDD.]
  275. Description [Substitutes a variable with its complement in a ZDD.
  276. returns a pointer to the result if successful; NULL otherwise.]
  277. SideEffects [None]
  278. SeeAlso []
  279. ******************************************************************************/
  280. DdNode *
  281. Cudd_zddChange(
  282. DdManager * dd,
  283. DdNode * P,
  284. int var)
  285. {
  286. DdNode *res;
  287. if ((unsigned int) var >= CUDD_MAXINDEX - 1) return(NULL);
  288. do {
  289. dd->reordered = 0;
  290. res = cuddZddChange(dd, P, var);
  291. } while (dd->reordered == 1);
  292. return(res);
  293. } /* end of Cudd_zddChange */
  294. /*---------------------------------------------------------------------------*/
  295. /* Definition of internal functions */
  296. /*---------------------------------------------------------------------------*/
  297. /**Function********************************************************************
  298. Synopsis [Performs the recursive step of Cudd_zddIte.]
  299. Description []
  300. SideEffects [None]
  301. SeeAlso []
  302. ******************************************************************************/
  303. DdNode *
  304. cuddZddIte(
  305. DdManager * dd,
  306. DdNode * f,
  307. DdNode * g,
  308. DdNode * h)
  309. {
  310. DdNode *tautology, *empty;
  311. DdNode *r,*Gv,*Gvn,*Hv,*Hvn,*t,*e;
  312. unsigned int topf,topg,toph,v,top;
  313. int index;
  314. statLine(dd);
  315. /* Trivial cases. */
  316. /* One variable cases. */
  317. if (f == (empty = DD_ZERO(dd))) { /* ITE(0,G,H) = H */
  318. return(h);
  319. }
  320. topf = cuddIZ(dd,f->index);
  321. topg = cuddIZ(dd,g->index);
  322. toph = cuddIZ(dd,h->index);
  323. v = ddMin(topg,toph);
  324. top = ddMin(topf,v);
  325. tautology = (top == CUDD_MAXINDEX) ? DD_ONE(dd) : dd->univ[top];
  326. if (f == tautology) { /* ITE(1,G,H) = G */
  327. return(g);
  328. }
  329. /* From now on, f is known to not be a constant. */
  330. zddVarToConst(f,&g,&h,tautology,empty);
  331. /* Check remaining one variable cases. */
  332. if (g == h) { /* ITE(F,G,G) = G */
  333. return(g);
  334. }
  335. if (g == tautology) { /* ITE(F,1,0) = F */
  336. if (h == empty) return(f);
  337. }
  338. /* Check cache. */
  339. r = cuddCacheLookupZdd(dd,DD_ZDD_ITE_TAG,f,g,h);
  340. if (r != NULL) {
  341. return(r);
  342. }
  343. /* Recompute these because they may have changed in zddVarToConst. */
  344. topg = cuddIZ(dd,g->index);
  345. toph = cuddIZ(dd,h->index);
  346. v = ddMin(topg,toph);
  347. if (topf < v) {
  348. r = cuddZddIte(dd,cuddE(f),g,h);
  349. if (r == NULL) return(NULL);
  350. } else if (topf > v) {
  351. if (topg > v) {
  352. Gvn = g;
  353. index = h->index;
  354. } else {
  355. Gvn = cuddE(g);
  356. index = g->index;
  357. }
  358. if (toph > v) {
  359. Hv = empty; Hvn = h;
  360. } else {
  361. Hv = cuddT(h); Hvn = cuddE(h);
  362. }
  363. e = cuddZddIte(dd,f,Gvn,Hvn);
  364. if (e == NULL) return(NULL);
  365. cuddRef(e);
  366. r = cuddZddGetNode(dd,index,Hv,e);
  367. if (r == NULL) {
  368. Cudd_RecursiveDerefZdd(dd,e);
  369. return(NULL);
  370. }
  371. cuddDeref(e);
  372. } else {
  373. index = f->index;
  374. if (topg > v) {
  375. Gv = empty; Gvn = g;
  376. } else {
  377. Gv = cuddT(g); Gvn = cuddE(g);
  378. }
  379. if (toph > v) {
  380. Hv = empty; Hvn = h;
  381. } else {
  382. Hv = cuddT(h); Hvn = cuddE(h);
  383. }
  384. e = cuddZddIte(dd,cuddE(f),Gvn,Hvn);
  385. if (e == NULL) return(NULL);
  386. cuddRef(e);
  387. t = cuddZddIte(dd,cuddT(f),Gv,Hv);
  388. if (t == NULL) {
  389. Cudd_RecursiveDerefZdd(dd,e);
  390. return(NULL);
  391. }
  392. cuddRef(t);
  393. r = cuddZddGetNode(dd,index,t,e);
  394. if (r == NULL) {
  395. Cudd_RecursiveDerefZdd(dd,e);
  396. Cudd_RecursiveDerefZdd(dd,t);
  397. return(NULL);
  398. }
  399. cuddDeref(t);
  400. cuddDeref(e);
  401. }
  402. cuddCacheInsert(dd,DD_ZDD_ITE_TAG,f,g,h,r);
  403. return(r);
  404. } /* end of cuddZddIte */
  405. /**Function********************************************************************
  406. Synopsis [Performs the recursive step of Cudd_zddUnion.]
  407. Description []
  408. SideEffects [None]
  409. SeeAlso []
  410. ******************************************************************************/
  411. DdNode *
  412. cuddZddUnion(
  413. DdManager * zdd,
  414. DdNode * P,
  415. DdNode * Q)
  416. {
  417. int p_top, q_top;
  418. DdNode *empty = DD_ZERO(zdd), *t, *e, *res;
  419. DdManager *table = zdd;
  420. statLine(zdd);
  421. if (P == empty)
  422. return(Q);
  423. if (Q == empty)
  424. return(P);
  425. if (P == Q)
  426. return(P);
  427. /* Check cache */
  428. res = cuddCacheLookup2Zdd(table, cuddZddUnion, P, Q);
  429. if (res != NULL)
  430. return(res);
  431. if (cuddIsConstant(P))
  432. p_top = P->index;
  433. else
  434. p_top = zdd->permZ[P->index];
  435. if (cuddIsConstant(Q))
  436. q_top = Q->index;
  437. else
  438. q_top = zdd->permZ[Q->index];
  439. if (p_top < q_top) {
  440. e = cuddZddUnion(zdd, cuddE(P), Q);
  441. if (e == NULL) return (NULL);
  442. cuddRef(e);
  443. res = cuddZddGetNode(zdd, P->index, cuddT(P), e);
  444. if (res == NULL) {
  445. Cudd_RecursiveDerefZdd(table, e);
  446. return(NULL);
  447. }
  448. cuddDeref(e);
  449. } else if (p_top > q_top) {
  450. e = cuddZddUnion(zdd, P, cuddE(Q));
  451. if (e == NULL) return(NULL);
  452. cuddRef(e);
  453. res = cuddZddGetNode(zdd, Q->index, cuddT(Q), e);
  454. if (res == NULL) {
  455. Cudd_RecursiveDerefZdd(table, e);
  456. return(NULL);
  457. }
  458. cuddDeref(e);
  459. } else {
  460. t = cuddZddUnion(zdd, cuddT(P), cuddT(Q));
  461. if (t == NULL) return(NULL);
  462. cuddRef(t);
  463. e = cuddZddUnion(zdd, cuddE(P), cuddE(Q));
  464. if (e == NULL) {
  465. Cudd_RecursiveDerefZdd(table, t);
  466. return(NULL);
  467. }
  468. cuddRef(e);
  469. res = cuddZddGetNode(zdd, P->index, t, e);
  470. if (res == NULL) {
  471. Cudd_RecursiveDerefZdd(table, t);
  472. Cudd_RecursiveDerefZdd(table, e);
  473. return(NULL);
  474. }
  475. cuddDeref(t);
  476. cuddDeref(e);
  477. }
  478. cuddCacheInsert2(table, cuddZddUnion, P, Q, res);
  479. return(res);
  480. } /* end of cuddZddUnion */
  481. /**Function********************************************************************
  482. Synopsis [Performs the recursive step of Cudd_zddIntersect.]
  483. Description []
  484. SideEffects [None]
  485. SeeAlso []
  486. ******************************************************************************/
  487. DdNode *
  488. cuddZddIntersect(
  489. DdManager * zdd,
  490. DdNode * P,
  491. DdNode * Q)
  492. {
  493. int p_top, q_top;
  494. DdNode *empty = DD_ZERO(zdd), *t, *e, *res;
  495. DdManager *table = zdd;
  496. statLine(zdd);
  497. if (P == empty)
  498. return(empty);
  499. if (Q == empty)
  500. return(empty);
  501. if (P == Q)
  502. return(P);
  503. /* Check cache. */
  504. res = cuddCacheLookup2Zdd(table, cuddZddIntersect, P, Q);
  505. if (res != NULL)
  506. return(res);
  507. if (cuddIsConstant(P))
  508. p_top = P->index;
  509. else
  510. p_top = zdd->permZ[P->index];
  511. if (cuddIsConstant(Q))
  512. q_top = Q->index;
  513. else
  514. q_top = zdd->permZ[Q->index];
  515. if (p_top < q_top) {
  516. res = cuddZddIntersect(zdd, cuddE(P), Q);
  517. if (res == NULL) return(NULL);
  518. } else if (p_top > q_top) {
  519. res = cuddZddIntersect(zdd, P, cuddE(Q));
  520. if (res == NULL) return(NULL);
  521. } else {
  522. t = cuddZddIntersect(zdd, cuddT(P), cuddT(Q));
  523. if (t == NULL) return(NULL);
  524. cuddRef(t);
  525. e = cuddZddIntersect(zdd, cuddE(P), cuddE(Q));
  526. if (e == NULL) {
  527. Cudd_RecursiveDerefZdd(table, t);
  528. return(NULL);
  529. }
  530. cuddRef(e);
  531. res = cuddZddGetNode(zdd, P->index, t, e);
  532. if (res == NULL) {
  533. Cudd_RecursiveDerefZdd(table, t);
  534. Cudd_RecursiveDerefZdd(table, e);
  535. return(NULL);
  536. }
  537. cuddDeref(t);
  538. cuddDeref(e);
  539. }
  540. cuddCacheInsert2(table, cuddZddIntersect, P, Q, res);
  541. return(res);
  542. } /* end of cuddZddIntersect */
  543. /**Function********************************************************************
  544. Synopsis [Performs the recursive step of Cudd_zddDiff.]
  545. Description []
  546. SideEffects [None]
  547. SeeAlso []
  548. ******************************************************************************/
  549. DdNode *
  550. cuddZddDiff(
  551. DdManager * zdd,
  552. DdNode * P,
  553. DdNode * Q)
  554. {
  555. int p_top, q_top;
  556. DdNode *empty = DD_ZERO(zdd), *t, *e, *res;
  557. DdManager *table = zdd;
  558. statLine(zdd);
  559. if (P == empty)
  560. return(empty);
  561. if (Q == empty)
  562. return(P);
  563. if (P == Q)
  564. return(empty);
  565. /* Check cache. The cache is shared by Cudd_zddDiffConst(). */
  566. res = cuddCacheLookup2Zdd(table, cuddZddDiff, P, Q);
  567. if (res != NULL && res != DD_NON_CONSTANT)
  568. return(res);
  569. if (cuddIsConstant(P))
  570. p_top = P->index;
  571. else
  572. p_top = zdd->permZ[P->index];
  573. if (cuddIsConstant(Q))
  574. q_top = Q->index;
  575. else
  576. q_top = zdd->permZ[Q->index];
  577. if (p_top < q_top) {
  578. e = cuddZddDiff(zdd, cuddE(P), Q);
  579. if (e == NULL) return(NULL);
  580. cuddRef(e);
  581. res = cuddZddGetNode(zdd, P->index, cuddT(P), e);
  582. if (res == NULL) {
  583. Cudd_RecursiveDerefZdd(table, e);
  584. return(NULL);
  585. }
  586. cuddDeref(e);
  587. } else if (p_top > q_top) {
  588. res = cuddZddDiff(zdd, P, cuddE(Q));
  589. if (res == NULL) return(NULL);
  590. } else {
  591. t = cuddZddDiff(zdd, cuddT(P), cuddT(Q));
  592. if (t == NULL) return(NULL);
  593. cuddRef(t);
  594. e = cuddZddDiff(zdd, cuddE(P), cuddE(Q));
  595. if (e == NULL) {
  596. Cudd_RecursiveDerefZdd(table, t);
  597. return(NULL);
  598. }
  599. cuddRef(e);
  600. res = cuddZddGetNode(zdd, P->index, t, e);
  601. if (res == NULL) {
  602. Cudd_RecursiveDerefZdd(table, t);
  603. Cudd_RecursiveDerefZdd(table, e);
  604. return(NULL);
  605. }
  606. cuddDeref(t);
  607. cuddDeref(e);
  608. }
  609. cuddCacheInsert2(table, cuddZddDiff, P, Q, res);
  610. return(res);
  611. } /* end of cuddZddDiff */
  612. /**Function********************************************************************
  613. Synopsis [Performs the recursive step of Cudd_zddChange.]
  614. Description []
  615. SideEffects [None]
  616. SeeAlso []
  617. ******************************************************************************/
  618. DdNode *
  619. cuddZddChangeAux(
  620. DdManager * zdd,
  621. DdNode * P,
  622. DdNode * zvar)
  623. {
  624. int top_var, level;
  625. DdNode *res, *t, *e;
  626. DdNode *base = DD_ONE(zdd);
  627. DdNode *empty = DD_ZERO(zdd);
  628. statLine(zdd);
  629. if (P == empty)
  630. return(empty);
  631. if (P == base)
  632. return(zvar);
  633. /* Check cache. */
  634. res = cuddCacheLookup2Zdd(zdd, cuddZddChangeAux, P, zvar);
  635. if (res != NULL)
  636. return(res);
  637. top_var = zdd->permZ[P->index];
  638. level = zdd->permZ[zvar->index];
  639. if (top_var > level) {
  640. res = cuddZddGetNode(zdd, zvar->index, P, DD_ZERO(zdd));
  641. if (res == NULL) return(NULL);
  642. } else if (top_var == level) {
  643. res = cuddZddGetNode(zdd, zvar->index, cuddE(P), cuddT(P));
  644. if (res == NULL) return(NULL);
  645. } else {
  646. t = cuddZddChangeAux(zdd, cuddT(P), zvar);
  647. if (t == NULL) return(NULL);
  648. cuddRef(t);
  649. e = cuddZddChangeAux(zdd, cuddE(P), zvar);
  650. if (e == NULL) {
  651. Cudd_RecursiveDerefZdd(zdd, t);
  652. return(NULL);
  653. }
  654. cuddRef(e);
  655. res = cuddZddGetNode(zdd, P->index, t, e);
  656. if (res == NULL) {
  657. Cudd_RecursiveDerefZdd(zdd, t);
  658. Cudd_RecursiveDerefZdd(zdd, e);
  659. return(NULL);
  660. }
  661. cuddDeref(t);
  662. cuddDeref(e);
  663. }
  664. cuddCacheInsert2(zdd, cuddZddChangeAux, P, zvar, res);
  665. return(res);
  666. } /* end of cuddZddChangeAux */
  667. /**Function********************************************************************
  668. Synopsis [Computes the positive cofactor of a ZDD w.r.t. a variable.]
  669. Description [Computes the positive cofactor of a ZDD w.r.t. a
  670. variable. In terms of combinations, the result is the set of all
  671. combinations in which the variable is asserted. Returns a pointer to
  672. the result if successful; NULL otherwise. cuddZddSubset1 performs
  673. the same function as Cudd_zddSubset1, but does not restart if
  674. reordering has taken place. Therefore it can be called from within a
  675. recursive procedure.]
  676. SideEffects [None]
  677. SeeAlso [cuddZddSubset0 Cudd_zddSubset1]
  678. ******************************************************************************/
  679. DdNode *
  680. cuddZddSubset1(
  681. DdManager * dd,
  682. DdNode * P,
  683. int var)
  684. {
  685. DdNode *zvar, *r;
  686. DdNode *base, *empty;
  687. base = DD_ONE(dd);
  688. empty = DD_ZERO(dd);
  689. zvar = cuddUniqueInterZdd(dd, var, base, empty);
  690. if (zvar == NULL) {
  691. return(NULL);
  692. } else {
  693. cuddRef(zvar);
  694. r = zdd_subset1_aux(dd, P, zvar);
  695. if (r == NULL) {
  696. Cudd_RecursiveDerefZdd(dd, zvar);
  697. return(NULL);
  698. }
  699. cuddRef(r);
  700. Cudd_RecursiveDerefZdd(dd, zvar);
  701. }
  702. cuddDeref(r);
  703. return(r);
  704. } /* end of cuddZddSubset1 */
  705. /**Function********************************************************************
  706. Synopsis [Computes the negative cofactor of a ZDD w.r.t. a variable.]
  707. Description [Computes the negative cofactor of a ZDD w.r.t. a
  708. variable. In terms of combinations, the result is the set of all
  709. combinations in which the variable is negated. Returns a pointer to
  710. the result if successful; NULL otherwise. cuddZddSubset0 performs
  711. the same function as Cudd_zddSubset0, but does not restart if
  712. reordering has taken place. Therefore it can be called from within a
  713. recursive procedure.]
  714. SideEffects [None]
  715. SeeAlso [cuddZddSubset1 Cudd_zddSubset0]
  716. ******************************************************************************/
  717. DdNode *
  718. cuddZddSubset0(
  719. DdManager * dd,
  720. DdNode * P,
  721. int var)
  722. {
  723. DdNode *zvar, *r;
  724. DdNode *base, *empty;
  725. base = DD_ONE(dd);
  726. empty = DD_ZERO(dd);
  727. zvar = cuddUniqueInterZdd(dd, var, base, empty);
  728. if (zvar == NULL) {
  729. return(NULL);
  730. } else {
  731. cuddRef(zvar);
  732. r = zdd_subset0_aux(dd, P, zvar);
  733. if (r == NULL) {
  734. Cudd_RecursiveDerefZdd(dd, zvar);
  735. return(NULL);
  736. }
  737. cuddRef(r);
  738. Cudd_RecursiveDerefZdd(dd, zvar);
  739. }
  740. cuddDeref(r);
  741. return(r);
  742. } /* end of cuddZddSubset0 */
  743. /**Function********************************************************************
  744. Synopsis [Substitutes a variable with its complement in a ZDD.]
  745. Description [Substitutes a variable with its complement in a ZDD.
  746. returns a pointer to the result if successful; NULL
  747. otherwise. cuddZddChange performs the same function as
  748. Cudd_zddChange, but does not restart if reordering has taken
  749. place. Therefore it can be called from within a recursive
  750. procedure.]
  751. SideEffects [None]
  752. SeeAlso [Cudd_zddChange]
  753. ******************************************************************************/
  754. DdNode *
  755. cuddZddChange(
  756. DdManager * dd,
  757. DdNode * P,
  758. int var)
  759. {
  760. DdNode *zvar, *res;
  761. zvar = cuddUniqueInterZdd(dd, var, DD_ONE(dd), DD_ZERO(dd));
  762. if (zvar == NULL) return(NULL);
  763. cuddRef(zvar);
  764. res = cuddZddChangeAux(dd, P, zvar);
  765. if (res == NULL) {
  766. Cudd_RecursiveDerefZdd(dd,zvar);
  767. return(NULL);
  768. }
  769. cuddRef(res);
  770. Cudd_RecursiveDerefZdd(dd,zvar);
  771. cuddDeref(res);
  772. return(res);
  773. } /* end of cuddZddChange */
  774. /*---------------------------------------------------------------------------*/
  775. /* Definition of static functions */
  776. /*---------------------------------------------------------------------------*/
  777. /**Function********************************************************************
  778. Synopsis [Performs the recursive step of Cudd_zddSubset1.]
  779. Description []
  780. SideEffects [None]
  781. SeeAlso []
  782. ******************************************************************************/
  783. static DdNode *
  784. zdd_subset1_aux(
  785. DdManager * zdd,
  786. DdNode * P,
  787. DdNode * zvar)
  788. {
  789. int top_var, level;
  790. DdNode *res, *t, *e;
  791. DdNode *empty;
  792. statLine(zdd);
  793. empty = DD_ZERO(zdd);
  794. /* Check cache. */
  795. res = cuddCacheLookup2Zdd(zdd, zdd_subset1_aux, P, zvar);
  796. if (res != NULL)
  797. return(res);
  798. if (cuddIsConstant(P)) {
  799. res = empty;
  800. cuddCacheInsert2(zdd, zdd_subset1_aux, P, zvar, res);
  801. return(res);
  802. }
  803. top_var = zdd->permZ[P->index];
  804. level = zdd->permZ[zvar->index];
  805. if (top_var > level) {
  806. res = empty;
  807. } else if (top_var == level) {
  808. res = cuddT(P);
  809. } else {
  810. t = zdd_subset1_aux(zdd, cuddT(P), zvar);
  811. if (t == NULL) return(NULL);
  812. cuddRef(t);
  813. e = zdd_subset1_aux(zdd, cuddE(P), zvar);
  814. if (e == NULL) {
  815. Cudd_RecursiveDerefZdd(zdd, t);
  816. return(NULL);
  817. }
  818. cuddRef(e);
  819. res = cuddZddGetNode(zdd, P->index, t, e);
  820. if (res == NULL) {
  821. Cudd_RecursiveDerefZdd(zdd, t);
  822. Cudd_RecursiveDerefZdd(zdd, e);
  823. return(NULL);
  824. }
  825. cuddDeref(t);
  826. cuddDeref(e);
  827. }
  828. cuddCacheInsert2(zdd, zdd_subset1_aux, P, zvar, res);
  829. return(res);
  830. } /* end of zdd_subset1_aux */
  831. /**Function********************************************************************
  832. Synopsis [Performs the recursive step of Cudd_zddSubset0.]
  833. Description []
  834. SideEffects [None]
  835. SeeAlso []
  836. ******************************************************************************/
  837. static DdNode *
  838. zdd_subset0_aux(
  839. DdManager * zdd,
  840. DdNode * P,
  841. DdNode * zvar)
  842. {
  843. int top_var, level;
  844. DdNode *res, *t, *e;
  845. statLine(zdd);
  846. /* Check cache. */
  847. res = cuddCacheLookup2Zdd(zdd, zdd_subset0_aux, P, zvar);
  848. if (res != NULL)
  849. return(res);
  850. if (cuddIsConstant(P)) {
  851. res = P;
  852. cuddCacheInsert2(zdd, zdd_subset0_aux, P, zvar, res);
  853. return(res);
  854. }
  855. top_var = zdd->permZ[P->index];
  856. level = zdd->permZ[zvar->index];
  857. if (top_var > level) {
  858. res = P;
  859. }
  860. else if (top_var == level) {
  861. res = cuddE(P);
  862. }
  863. else {
  864. t = zdd_subset0_aux(zdd, cuddT(P), zvar);
  865. if (t == NULL) return(NULL);
  866. cuddRef(t);
  867. e = zdd_subset0_aux(zdd, cuddE(P), zvar);
  868. if (e == NULL) {
  869. Cudd_RecursiveDerefZdd(zdd, t);
  870. return(NULL);
  871. }
  872. cuddRef(e);
  873. res = cuddZddGetNode(zdd, P->index, t, e);
  874. if (res == NULL) {
  875. Cudd_RecursiveDerefZdd(zdd, t);
  876. Cudd_RecursiveDerefZdd(zdd, e);
  877. return(NULL);
  878. }
  879. cuddDeref(t);
  880. cuddDeref(e);
  881. }
  882. cuddCacheInsert2(zdd, zdd_subset0_aux, P, zvar, res);
  883. return(res);
  884. } /* end of zdd_subset0_aux */
  885. /**Function********************************************************************
  886. Synopsis [Replaces variables with constants if possible (part of
  887. canonical form).]
  888. Description []
  889. SideEffects [None]
  890. SeeAlso []
  891. ******************************************************************************/
  892. static void
  893. zddVarToConst(
  894. DdNode * f,
  895. DdNode ** gp,
  896. DdNode ** hp,
  897. DdNode * base,
  898. DdNode * empty)
  899. {
  900. DdNode *g = *gp;
  901. DdNode *h = *hp;
  902. if (f == g) { /* ITE(F,F,H) = ITE(F,1,H) = F + H */
  903. *gp = base;
  904. }
  905. if (f == h) { /* ITE(F,G,F) = ITE(F,G,0) = F * G */
  906. *hp = empty;
  907. }
  908. } /* end of zddVarToConst */