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.

1080 lines
23 KiB

  1. /**
  2. @file
  3. @ingroup cudd
  4. @brief Set operations on 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 * zdd_subset1_aux (DdManager *zdd, DdNode *P, DdNode *zvar);
  56. static DdNode * zdd_subset0_aux (DdManager *zdd, DdNode *P, DdNode *zvar);
  57. static void zddVarToConst (DdNode *f, DdNode **gp, DdNode **hp, DdNode *base, DdNode *empty);
  58. /** \endcond */
  59. /*---------------------------------------------------------------------------*/
  60. /* Definition of exported functions */
  61. /*---------------------------------------------------------------------------*/
  62. /**
  63. @brief Computes the ITE of three ZDDs.
  64. @return a pointer to the result if successful; NULL otherwise.
  65. @sideeffect None
  66. */
  67. DdNode *
  68. Cudd_zddIte(
  69. DdManager * dd,
  70. DdNode * f,
  71. DdNode * g,
  72. DdNode * h)
  73. {
  74. DdNode *res;
  75. do {
  76. dd->reordered = 0;
  77. res = cuddZddIte(dd, f, g, h);
  78. } while (dd->reordered == 1);
  79. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  80. dd->timeoutHandler(dd, dd->tohArg);
  81. }
  82. return(res);
  83. } /* end of Cudd_zddIte */
  84. /**
  85. @brief Computes the union of two ZDDs.
  86. @return a pointer to the result if successful; NULL otherwise.
  87. @sideeffect None
  88. */
  89. DdNode *
  90. Cudd_zddUnion(
  91. DdManager * dd,
  92. DdNode * P,
  93. DdNode * Q)
  94. {
  95. DdNode *res;
  96. do {
  97. dd->reordered = 0;
  98. res = cuddZddUnion(dd, P, Q);
  99. } while (dd->reordered == 1);
  100. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  101. dd->timeoutHandler(dd, dd->tohArg);
  102. }
  103. return(res);
  104. } /* end of Cudd_zddUnion */
  105. /**
  106. @brief Computes the intersection of two ZDDs.
  107. @return a pointer to the result if successful; NULL otherwise.
  108. @sideeffect None
  109. */
  110. DdNode *
  111. Cudd_zddIntersect(
  112. DdManager * dd,
  113. DdNode * P,
  114. DdNode * Q)
  115. {
  116. DdNode *res;
  117. do {
  118. dd->reordered = 0;
  119. res = cuddZddIntersect(dd, P, Q);
  120. } while (dd->reordered == 1);
  121. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  122. dd->timeoutHandler(dd, dd->tohArg);
  123. }
  124. return(res);
  125. } /* end of Cudd_zddIntersect */
  126. /**
  127. @brief Computes the difference of two ZDDs.
  128. @return a pointer to the result if successful; NULL otherwise.
  129. @sideeffect None
  130. */
  131. DdNode *
  132. Cudd_zddDiff(
  133. DdManager * dd,
  134. DdNode * P,
  135. DdNode * Q)
  136. {
  137. DdNode *res;
  138. do {
  139. dd->reordered = 0;
  140. res = cuddZddDiff(dd, P, Q);
  141. } while (dd->reordered == 1);
  142. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  143. dd->timeoutHandler(dd, dd->tohArg);
  144. }
  145. return(res);
  146. } /* end of Cudd_zddDiff */
  147. /**
  148. @brief Performs the inclusion test for ZDDs (P implies Q).
  149. @details No new nodes are generated by this procedure.
  150. @return empty if true; a valid pointer different from empty or
  151. DD_NON_CONSTANT otherwise.
  152. @sideeffect None
  153. @see Cudd_zddDiff
  154. */
  155. DdNode *
  156. Cudd_zddDiffConst(
  157. DdManager * zdd,
  158. DdNode * P,
  159. DdNode * Q)
  160. {
  161. int p_top, q_top;
  162. DdNode *empty = DD_ZERO(zdd), *t, *res;
  163. DdManager *table = zdd;
  164. statLine(zdd);
  165. if (P == empty)
  166. return(empty);
  167. if (Q == empty)
  168. return(P);
  169. if (P == Q)
  170. return(empty);
  171. /* Check cache. The cache is shared by cuddZddDiff(). */
  172. res = cuddCacheLookup2Zdd(table, cuddZddDiff, P, Q);
  173. if (res != NULL)
  174. return(res);
  175. if (cuddIsConstant(P))
  176. p_top = P->index;
  177. else
  178. p_top = zdd->permZ[P->index];
  179. if (cuddIsConstant(Q))
  180. q_top = Q->index;
  181. else
  182. q_top = zdd->permZ[Q->index];
  183. if (p_top < q_top) {
  184. res = DD_NON_CONSTANT;
  185. } else if (p_top > q_top) {
  186. res = Cudd_zddDiffConst(zdd, P, cuddE(Q));
  187. } else {
  188. t = Cudd_zddDiffConst(zdd, cuddT(P), cuddT(Q));
  189. if (t != empty)
  190. res = DD_NON_CONSTANT;
  191. else
  192. res = Cudd_zddDiffConst(zdd, cuddE(P), cuddE(Q));
  193. }
  194. cuddCacheInsert2(table, cuddZddDiff, P, Q, res);
  195. return(res);
  196. } /* end of Cudd_zddDiffConst */
  197. /**
  198. @brief Computes the positive cofactor of a %ZDD w.r.t. a variable.
  199. @details In terms of combinations, the result is the set of all
  200. combinations in which the variable is asserted.
  201. @return a pointer to the result if successful; NULL otherwise.
  202. @sideeffect None
  203. @see Cudd_zddSubset0
  204. */
  205. DdNode *
  206. Cudd_zddSubset1(
  207. DdManager * dd,
  208. DdNode * P,
  209. int var)
  210. {
  211. DdNode *r;
  212. do {
  213. dd->reordered = 0;
  214. r = cuddZddSubset1(dd, P, var);
  215. } while (dd->reordered == 1);
  216. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  217. dd->timeoutHandler(dd, dd->tohArg);
  218. }
  219. return(r);
  220. } /* end of Cudd_zddSubset1 */
  221. /**
  222. @brief Computes the negative cofactor of a %ZDD w.r.t. a variable.
  223. @details In terms of combinations, the result is the set of all
  224. combinations in which the variable is negated.
  225. @return a pointer to the result if successful; NULL otherwise.
  226. @sideeffect None
  227. @see Cudd_zddSubset1
  228. */
  229. DdNode *
  230. Cudd_zddSubset0(
  231. DdManager * dd,
  232. DdNode * P,
  233. int var)
  234. {
  235. DdNode *r;
  236. do {
  237. dd->reordered = 0;
  238. r = cuddZddSubset0(dd, P, var);
  239. } while (dd->reordered == 1);
  240. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  241. dd->timeoutHandler(dd, dd->tohArg);
  242. }
  243. return(r);
  244. } /* end of Cudd_zddSubset0 */
  245. /**
  246. @brief Substitutes a variable with its complement in a %ZDD.
  247. @return a pointer to the result if successful; NULL otherwise.
  248. @sideeffect None
  249. */
  250. DdNode *
  251. Cudd_zddChange(
  252. DdManager * dd,
  253. DdNode * P,
  254. int var)
  255. {
  256. DdNode *res;
  257. if ((unsigned int) var >= CUDD_MAXINDEX - 1) return(NULL);
  258. do {
  259. dd->reordered = 0;
  260. res = cuddZddChange(dd, P, var);
  261. } while (dd->reordered == 1);
  262. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  263. dd->timeoutHandler(dd, dd->tohArg);
  264. }
  265. return(res);
  266. } /* end of Cudd_zddChange */
  267. /*---------------------------------------------------------------------------*/
  268. /* Definition of internal functions */
  269. /*---------------------------------------------------------------------------*/
  270. /**
  271. @brief Performs the recursive step of Cudd_zddIte.
  272. @sideeffect None
  273. */
  274. DdNode *
  275. cuddZddIte(
  276. DdManager * dd,
  277. DdNode * f,
  278. DdNode * g,
  279. DdNode * h)
  280. {
  281. DdNode *tautology, *empty;
  282. DdNode *r,*Gv,*Gvn,*Hv,*Hvn,*t,*e;
  283. int topf,topg,toph,v,top;
  284. unsigned int index;
  285. statLine(dd);
  286. /* Trivial cases. */
  287. /* One variable cases. */
  288. if (f == (empty = DD_ZERO(dd))) { /* ITE(0,G,H) = H */
  289. return(h);
  290. }
  291. topf = cuddIZ(dd,f->index);
  292. topg = cuddIZ(dd,g->index);
  293. toph = cuddIZ(dd,h->index);
  294. v = ddMin(topg,toph);
  295. top = ddMin(topf,v);
  296. tautology = (top == CUDD_MAXINDEX) ? DD_ONE(dd) : dd->univ[top];
  297. if (f == tautology) { /* ITE(1,G,H) = G */
  298. return(g);
  299. }
  300. /* From now on, f is known to not be a constant. */
  301. zddVarToConst(f,&g,&h,tautology,empty);
  302. /* Check remaining one variable cases. */
  303. if (g == h) { /* ITE(F,G,G) = G */
  304. return(g);
  305. }
  306. if (g == tautology) { /* ITE(F,1,0) = F */
  307. if (h == empty) return(f);
  308. }
  309. /* Check cache. */
  310. r = cuddCacheLookupZdd(dd,DD_ZDD_ITE_TAG,f,g,h);
  311. if (r != NULL) {
  312. return(r);
  313. }
  314. /* Recompute these because they may have changed in zddVarToConst. */
  315. topg = cuddIZ(dd,g->index);
  316. toph = cuddIZ(dd,h->index);
  317. v = ddMin(topg,toph);
  318. if (topf < v) {
  319. r = cuddZddIte(dd,cuddE(f),g,h);
  320. if (r == NULL) return(NULL);
  321. } else if (topf > v) {
  322. if (topg > v) {
  323. Gvn = g;
  324. index = h->index;
  325. } else {
  326. Gvn = cuddE(g);
  327. index = g->index;
  328. }
  329. if (toph > v) {
  330. Hv = empty; Hvn = h;
  331. } else {
  332. Hv = cuddT(h); Hvn = cuddE(h);
  333. }
  334. e = cuddZddIte(dd,f,Gvn,Hvn);
  335. if (e == NULL) return(NULL);
  336. cuddRef(e);
  337. r = cuddZddGetNode(dd,index,Hv,e);
  338. if (r == NULL) {
  339. Cudd_RecursiveDerefZdd(dd,e);
  340. return(NULL);
  341. }
  342. cuddDeref(e);
  343. } else {
  344. index = f->index;
  345. if (topg > v) {
  346. Gv = empty; Gvn = g;
  347. } else {
  348. Gv = cuddT(g); Gvn = cuddE(g);
  349. }
  350. if (toph > v) {
  351. Hv = empty; Hvn = h;
  352. } else {
  353. Hv = cuddT(h); Hvn = cuddE(h);
  354. }
  355. e = cuddZddIte(dd,cuddE(f),Gvn,Hvn);
  356. if (e == NULL) return(NULL);
  357. cuddRef(e);
  358. t = cuddZddIte(dd,cuddT(f),Gv,Hv);
  359. if (t == NULL) {
  360. Cudd_RecursiveDerefZdd(dd,e);
  361. return(NULL);
  362. }
  363. cuddRef(t);
  364. r = cuddZddGetNode(dd,index,t,e);
  365. if (r == NULL) {
  366. Cudd_RecursiveDerefZdd(dd,e);
  367. Cudd_RecursiveDerefZdd(dd,t);
  368. return(NULL);
  369. }
  370. cuddDeref(t);
  371. cuddDeref(e);
  372. }
  373. cuddCacheInsert(dd,DD_ZDD_ITE_TAG,f,g,h,r);
  374. return(r);
  375. } /* end of cuddZddIte */
  376. /**
  377. @brief Performs the recursive step of Cudd_zddUnion.
  378. @sideeffect None
  379. */
  380. DdNode *
  381. cuddZddUnion(
  382. DdManager * zdd,
  383. DdNode * P,
  384. DdNode * Q)
  385. {
  386. int p_top, q_top;
  387. DdNode *empty = DD_ZERO(zdd), *t, *e, *res;
  388. DdManager *table = zdd;
  389. statLine(zdd);
  390. if (P == empty)
  391. return(Q);
  392. if (Q == empty)
  393. return(P);
  394. if (P == Q)
  395. return(P);
  396. /* Check cache */
  397. res = cuddCacheLookup2Zdd(table, cuddZddUnion, P, Q);
  398. if (res != NULL)
  399. return(res);
  400. if (cuddIsConstant(P))
  401. p_top = P->index;
  402. else
  403. p_top = zdd->permZ[P->index];
  404. if (cuddIsConstant(Q))
  405. q_top = Q->index;
  406. else
  407. q_top = zdd->permZ[Q->index];
  408. if (p_top < q_top) {
  409. e = cuddZddUnion(zdd, cuddE(P), Q);
  410. if (e == NULL) return (NULL);
  411. cuddRef(e);
  412. res = cuddZddGetNode(zdd, P->index, cuddT(P), e);
  413. if (res == NULL) {
  414. Cudd_RecursiveDerefZdd(table, e);
  415. return(NULL);
  416. }
  417. cuddDeref(e);
  418. } else if (p_top > q_top) {
  419. e = cuddZddUnion(zdd, P, cuddE(Q));
  420. if (e == NULL) return(NULL);
  421. cuddRef(e);
  422. res = cuddZddGetNode(zdd, Q->index, cuddT(Q), e);
  423. if (res == NULL) {
  424. Cudd_RecursiveDerefZdd(table, e);
  425. return(NULL);
  426. }
  427. cuddDeref(e);
  428. } else {
  429. t = cuddZddUnion(zdd, cuddT(P), cuddT(Q));
  430. if (t == NULL) return(NULL);
  431. cuddRef(t);
  432. e = cuddZddUnion(zdd, cuddE(P), cuddE(Q));
  433. if (e == NULL) {
  434. Cudd_RecursiveDerefZdd(table, t);
  435. return(NULL);
  436. }
  437. cuddRef(e);
  438. res = cuddZddGetNode(zdd, P->index, t, e);
  439. if (res == NULL) {
  440. Cudd_RecursiveDerefZdd(table, t);
  441. Cudd_RecursiveDerefZdd(table, e);
  442. return(NULL);
  443. }
  444. cuddDeref(t);
  445. cuddDeref(e);
  446. }
  447. cuddCacheInsert2(table, cuddZddUnion, P, Q, res);
  448. return(res);
  449. } /* end of cuddZddUnion */
  450. /**
  451. @brief Performs the recursive step of Cudd_zddIntersect.
  452. @sideeffect None
  453. */
  454. DdNode *
  455. cuddZddIntersect(
  456. DdManager * zdd,
  457. DdNode * P,
  458. DdNode * Q)
  459. {
  460. int p_top, q_top;
  461. DdNode *empty = DD_ZERO(zdd), *t, *e, *res;
  462. DdManager *table = zdd;
  463. statLine(zdd);
  464. if (P == empty)
  465. return(empty);
  466. if (Q == empty)
  467. return(empty);
  468. if (P == Q)
  469. return(P);
  470. /* Check cache. */
  471. res = cuddCacheLookup2Zdd(table, cuddZddIntersect, P, Q);
  472. if (res != NULL)
  473. return(res);
  474. if (cuddIsConstant(P))
  475. p_top = P->index;
  476. else
  477. p_top = zdd->permZ[P->index];
  478. if (cuddIsConstant(Q))
  479. q_top = Q->index;
  480. else
  481. q_top = zdd->permZ[Q->index];
  482. if (p_top < q_top) {
  483. res = cuddZddIntersect(zdd, cuddE(P), Q);
  484. if (res == NULL) return(NULL);
  485. } else if (p_top > q_top) {
  486. res = cuddZddIntersect(zdd, P, cuddE(Q));
  487. if (res == NULL) return(NULL);
  488. } else {
  489. t = cuddZddIntersect(zdd, cuddT(P), cuddT(Q));
  490. if (t == NULL) return(NULL);
  491. cuddRef(t);
  492. e = cuddZddIntersect(zdd, cuddE(P), cuddE(Q));
  493. if (e == NULL) {
  494. Cudd_RecursiveDerefZdd(table, t);
  495. return(NULL);
  496. }
  497. cuddRef(e);
  498. res = cuddZddGetNode(zdd, P->index, t, e);
  499. if (res == NULL) {
  500. Cudd_RecursiveDerefZdd(table, t);
  501. Cudd_RecursiveDerefZdd(table, e);
  502. return(NULL);
  503. }
  504. cuddDeref(t);
  505. cuddDeref(e);
  506. }
  507. cuddCacheInsert2(table, cuddZddIntersect, P, Q, res);
  508. return(res);
  509. } /* end of cuddZddIntersect */
  510. /**
  511. @brief Performs the recursive step of Cudd_zddDiff.
  512. @sideeffect None
  513. */
  514. DdNode *
  515. cuddZddDiff(
  516. DdManager * zdd,
  517. DdNode * P,
  518. DdNode * Q)
  519. {
  520. int p_top, q_top;
  521. DdNode *empty = DD_ZERO(zdd), *t, *e, *res;
  522. DdManager *table = zdd;
  523. statLine(zdd);
  524. if (P == empty)
  525. return(empty);
  526. if (Q == empty)
  527. return(P);
  528. if (P == Q)
  529. return(empty);
  530. /* Check cache. The cache is shared by Cudd_zddDiffConst(). */
  531. res = cuddCacheLookup2Zdd(table, cuddZddDiff, P, Q);
  532. if (res != NULL && res != DD_NON_CONSTANT)
  533. return(res);
  534. if (cuddIsConstant(P))
  535. p_top = P->index;
  536. else
  537. p_top = zdd->permZ[P->index];
  538. if (cuddIsConstant(Q))
  539. q_top = Q->index;
  540. else
  541. q_top = zdd->permZ[Q->index];
  542. if (p_top < q_top) {
  543. e = cuddZddDiff(zdd, cuddE(P), Q);
  544. if (e == NULL) return(NULL);
  545. cuddRef(e);
  546. res = cuddZddGetNode(zdd, P->index, cuddT(P), e);
  547. if (res == NULL) {
  548. Cudd_RecursiveDerefZdd(table, e);
  549. return(NULL);
  550. }
  551. cuddDeref(e);
  552. } else if (p_top > q_top) {
  553. res = cuddZddDiff(zdd, P, cuddE(Q));
  554. if (res == NULL) return(NULL);
  555. } else {
  556. t = cuddZddDiff(zdd, cuddT(P), cuddT(Q));
  557. if (t == NULL) return(NULL);
  558. cuddRef(t);
  559. e = cuddZddDiff(zdd, cuddE(P), cuddE(Q));
  560. if (e == NULL) {
  561. Cudd_RecursiveDerefZdd(table, t);
  562. return(NULL);
  563. }
  564. cuddRef(e);
  565. res = cuddZddGetNode(zdd, P->index, t, e);
  566. if (res == NULL) {
  567. Cudd_RecursiveDerefZdd(table, t);
  568. Cudd_RecursiveDerefZdd(table, e);
  569. return(NULL);
  570. }
  571. cuddDeref(t);
  572. cuddDeref(e);
  573. }
  574. cuddCacheInsert2(table, cuddZddDiff, P, Q, res);
  575. return(res);
  576. } /* end of cuddZddDiff */
  577. /**
  578. @brief Performs the recursive step of Cudd_zddChange.
  579. @sideeffect None
  580. */
  581. DdNode *
  582. cuddZddChangeAux(
  583. DdManager * zdd,
  584. DdNode * P,
  585. DdNode * zvar)
  586. {
  587. int top_var, level;
  588. DdNode *res, *t, *e;
  589. DdNode *base = DD_ONE(zdd);
  590. DdNode *empty = DD_ZERO(zdd);
  591. statLine(zdd);
  592. if (P == empty)
  593. return(empty);
  594. if (P == base)
  595. return(zvar);
  596. /* Check cache. */
  597. res = cuddCacheLookup2Zdd(zdd, cuddZddChangeAux, P, zvar);
  598. if (res != NULL)
  599. return(res);
  600. top_var = zdd->permZ[P->index];
  601. level = zdd->permZ[zvar->index];
  602. if (top_var > level) {
  603. res = cuddZddGetNode(zdd, zvar->index, P, DD_ZERO(zdd));
  604. if (res == NULL) return(NULL);
  605. } else if (top_var == level) {
  606. res = cuddZddGetNode(zdd, zvar->index, cuddE(P), cuddT(P));
  607. if (res == NULL) return(NULL);
  608. } else {
  609. t = cuddZddChangeAux(zdd, cuddT(P), zvar);
  610. if (t == NULL) return(NULL);
  611. cuddRef(t);
  612. e = cuddZddChangeAux(zdd, cuddE(P), zvar);
  613. if (e == NULL) {
  614. Cudd_RecursiveDerefZdd(zdd, t);
  615. return(NULL);
  616. }
  617. cuddRef(e);
  618. res = cuddZddGetNode(zdd, P->index, t, e);
  619. if (res == NULL) {
  620. Cudd_RecursiveDerefZdd(zdd, t);
  621. Cudd_RecursiveDerefZdd(zdd, e);
  622. return(NULL);
  623. }
  624. cuddDeref(t);
  625. cuddDeref(e);
  626. }
  627. cuddCacheInsert2(zdd, cuddZddChangeAux, P, zvar, res);
  628. return(res);
  629. } /* end of cuddZddChangeAux */
  630. /**
  631. @brief Computes the positive cofactor of a %ZDD w.r.t. a variable.
  632. @details In terms of combinations, the result is the set of all
  633. combinations in which the variable is asserted. cuddZddSubset1
  634. performs the same function as Cudd_zddSubset1, but does not restart
  635. if reordering has taken place. Therefore it can be called from
  636. within a recursive procedure.
  637. @return a pointer to the result if successful; NULL otherwise.
  638. @sideeffect None
  639. @see cuddZddSubset0 Cudd_zddSubset1
  640. */
  641. DdNode *
  642. cuddZddSubset1(
  643. DdManager * dd,
  644. DdNode * P,
  645. int var)
  646. {
  647. DdNode *zvar, *r;
  648. DdNode *base, *empty;
  649. base = DD_ONE(dd);
  650. empty = DD_ZERO(dd);
  651. zvar = cuddUniqueInterZdd(dd, var, base, empty);
  652. if (zvar == NULL) {
  653. return(NULL);
  654. } else {
  655. cuddRef(zvar);
  656. r = zdd_subset1_aux(dd, P, zvar);
  657. if (r == NULL) {
  658. Cudd_RecursiveDerefZdd(dd, zvar);
  659. return(NULL);
  660. }
  661. cuddRef(r);
  662. Cudd_RecursiveDerefZdd(dd, zvar);
  663. }
  664. cuddDeref(r);
  665. return(r);
  666. } /* end of cuddZddSubset1 */
  667. /**
  668. @brief Computes the negative cofactor of a %ZDD w.r.t. a variable.
  669. @details In terms of combinations, the result is the set of all
  670. combinations in which the variable is negated. cuddZddSubset0
  671. performs the same function as Cudd_zddSubset0, but does not restart
  672. if reordering has taken place. Therefore it can be called from
  673. within a recursive procedure.
  674. @return a pointer to the result if successful; NULL otherwise.
  675. @sideeffect None
  676. @see cuddZddSubset1 Cudd_zddSubset0
  677. */
  678. DdNode *
  679. cuddZddSubset0(
  680. DdManager * dd,
  681. DdNode * P,
  682. int var)
  683. {
  684. DdNode *zvar, *r;
  685. DdNode *base, *empty;
  686. base = DD_ONE(dd);
  687. empty = DD_ZERO(dd);
  688. zvar = cuddUniqueInterZdd(dd, var, base, empty);
  689. if (zvar == NULL) {
  690. return(NULL);
  691. } else {
  692. cuddRef(zvar);
  693. r = zdd_subset0_aux(dd, P, zvar);
  694. if (r == NULL) {
  695. Cudd_RecursiveDerefZdd(dd, zvar);
  696. return(NULL);
  697. }
  698. cuddRef(r);
  699. Cudd_RecursiveDerefZdd(dd, zvar);
  700. }
  701. cuddDeref(r);
  702. return(r);
  703. } /* end of cuddZddSubset0 */
  704. /**
  705. @brief Substitutes a variable with its complement in a %ZDD.
  706. @details cuddZddChange performs the same function as Cudd_zddChange,
  707. but does not restart if reordering has taken place. Therefore it can
  708. be called from within a recursive procedure.
  709. @return a pointer to the result if successful; NULL otherwise.
  710. @sideeffect None
  711. @see Cudd_zddChange
  712. */
  713. DdNode *
  714. cuddZddChange(
  715. DdManager * dd,
  716. DdNode * P,
  717. int var)
  718. {
  719. DdNode *zvar, *res;
  720. zvar = cuddUniqueInterZdd(dd, var, DD_ONE(dd), DD_ZERO(dd));
  721. if (zvar == NULL) return(NULL);
  722. cuddRef(zvar);
  723. res = cuddZddChangeAux(dd, P, zvar);
  724. if (res == NULL) {
  725. Cudd_RecursiveDerefZdd(dd,zvar);
  726. return(NULL);
  727. }
  728. cuddRef(res);
  729. Cudd_RecursiveDerefZdd(dd,zvar);
  730. cuddDeref(res);
  731. return(res);
  732. } /* end of cuddZddChange */
  733. /*---------------------------------------------------------------------------*/
  734. /* Definition of static functions */
  735. /*---------------------------------------------------------------------------*/
  736. /**
  737. @brief Performs the recursive step of Cudd_zddSubset1.
  738. @sideeffect None
  739. */
  740. static DdNode *
  741. zdd_subset1_aux(
  742. DdManager * zdd,
  743. DdNode * P,
  744. DdNode * zvar)
  745. {
  746. int top_var, level;
  747. DdNode *res, *t, *e;
  748. DdNode *empty;
  749. statLine(zdd);
  750. empty = DD_ZERO(zdd);
  751. /* Check cache. */
  752. res = cuddCacheLookup2Zdd(zdd, zdd_subset1_aux, P, zvar);
  753. if (res != NULL)
  754. return(res);
  755. if (cuddIsConstant(P)) {
  756. res = empty;
  757. cuddCacheInsert2(zdd, zdd_subset1_aux, P, zvar, res);
  758. return(res);
  759. }
  760. top_var = zdd->permZ[P->index];
  761. level = zdd->permZ[zvar->index];
  762. if (top_var > level) {
  763. res = empty;
  764. } else if (top_var == level) {
  765. res = cuddT(P);
  766. } else {
  767. t = zdd_subset1_aux(zdd, cuddT(P), zvar);
  768. if (t == NULL) return(NULL);
  769. cuddRef(t);
  770. e = zdd_subset1_aux(zdd, cuddE(P), zvar);
  771. if (e == NULL) {
  772. Cudd_RecursiveDerefZdd(zdd, t);
  773. return(NULL);
  774. }
  775. cuddRef(e);
  776. res = cuddZddGetNode(zdd, P->index, t, e);
  777. if (res == NULL) {
  778. Cudd_RecursiveDerefZdd(zdd, t);
  779. Cudd_RecursiveDerefZdd(zdd, e);
  780. return(NULL);
  781. }
  782. cuddDeref(t);
  783. cuddDeref(e);
  784. }
  785. cuddCacheInsert2(zdd, zdd_subset1_aux, P, zvar, res);
  786. return(res);
  787. } /* end of zdd_subset1_aux */
  788. /**
  789. @brief Performs the recursive step of Cudd_zddSubset0.
  790. @sideeffect None
  791. */
  792. static DdNode *
  793. zdd_subset0_aux(
  794. DdManager * zdd,
  795. DdNode * P,
  796. DdNode * zvar)
  797. {
  798. int top_var, level;
  799. DdNode *res, *t, *e;
  800. statLine(zdd);
  801. /* Check cache. */
  802. res = cuddCacheLookup2Zdd(zdd, zdd_subset0_aux, P, zvar);
  803. if (res != NULL)
  804. return(res);
  805. if (cuddIsConstant(P)) {
  806. res = P;
  807. cuddCacheInsert2(zdd, zdd_subset0_aux, P, zvar, res);
  808. return(res);
  809. }
  810. top_var = zdd->permZ[P->index];
  811. level = zdd->permZ[zvar->index];
  812. if (top_var > level) {
  813. res = P;
  814. }
  815. else if (top_var == level) {
  816. res = cuddE(P);
  817. }
  818. else {
  819. t = zdd_subset0_aux(zdd, cuddT(P), zvar);
  820. if (t == NULL) return(NULL);
  821. cuddRef(t);
  822. e = zdd_subset0_aux(zdd, cuddE(P), zvar);
  823. if (e == NULL) {
  824. Cudd_RecursiveDerefZdd(zdd, t);
  825. return(NULL);
  826. }
  827. cuddRef(e);
  828. res = cuddZddGetNode(zdd, P->index, t, e);
  829. if (res == NULL) {
  830. Cudd_RecursiveDerefZdd(zdd, t);
  831. Cudd_RecursiveDerefZdd(zdd, e);
  832. return(NULL);
  833. }
  834. cuddDeref(t);
  835. cuddDeref(e);
  836. }
  837. cuddCacheInsert2(zdd, zdd_subset0_aux, P, zvar, res);
  838. return(res);
  839. } /* end of zdd_subset0_aux */
  840. /**
  841. @brief Replaces variables with constants if possible (part of
  842. canonical form).
  843. @sideeffect None
  844. */
  845. static void
  846. zddVarToConst(
  847. DdNode * f,
  848. DdNode ** gp,
  849. DdNode ** hp,
  850. DdNode * base,
  851. DdNode * empty)
  852. {
  853. DdNode *g = *gp;
  854. DdNode *h = *hp;
  855. if (f == g) { /* ITE(F,F,H) = ITE(F,1,H) = F + H */
  856. *gp = base;
  857. }
  858. if (f == h) { /* ITE(F,G,F) = ITE(F,G,0) = F * G */
  859. *hp = empty;
  860. }
  861. } /* end of zddVarToConst */