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.

1016 lines
26 KiB

  1. /**CFile***********************************************************************
  2. FileName [cuddBridge.c]
  3. PackageName [cudd]
  4. Synopsis [Translation from BDD to ADD and vice versa and transfer between
  5. different managers.]
  6. Description [External procedures included in this file:
  7. <ul>
  8. <li> Cudd_addBddThreshold()
  9. <li> Cudd_addBddStrictThreshold()
  10. <li> Cudd_addBddInterval()
  11. <li> Cudd_addBddIthBit()
  12. <li> Cudd_BddToAdd()
  13. <li> Cudd_addBddPattern()
  14. <li> Cudd_bddTransfer()
  15. </ul>
  16. Internal procedures included in this file:
  17. <ul>
  18. <li> cuddBddTransfer()
  19. <li> cuddAddBddDoPattern()
  20. </ul>
  21. Static procedures included in this file:
  22. <ul>
  23. <li> addBddDoThreshold()
  24. <li> addBddDoStrictThreshold()
  25. <li> addBddDoInterval()
  26. <li> addBddDoIthBit()
  27. <li> ddBddToAddRecur()
  28. <li> cuddBddTransferRecur()
  29. </ul>
  30. ]
  31. SeeAlso []
  32. Author [Fabio Somenzi]
  33. Copyright [Copyright (c) 1995-2012, Regents of the University of Colorado
  34. All rights reserved.
  35. Redistribution and use in source and binary forms, with or without
  36. modification, are permitted provided that the following conditions
  37. are met:
  38. Redistributions of source code must retain the above copyright
  39. notice, this list of conditions and the following disclaimer.
  40. Redistributions in binary form must reproduce the above copyright
  41. notice, this list of conditions and the following disclaimer in the
  42. documentation and/or other materials provided with the distribution.
  43. Neither the name of the University of Colorado nor the names of its
  44. contributors may be used to endorse or promote products derived from
  45. this software without specific prior written permission.
  46. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  47. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  48. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  49. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  50. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  51. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  52. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  53. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  54. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  55. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  56. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  57. POSSIBILITY OF SUCH DAMAGE.]
  58. ******************************************************************************/
  59. #include "util.h"
  60. #include "cuddInt.h"
  61. /*---------------------------------------------------------------------------*/
  62. /* Constant declarations */
  63. /*---------------------------------------------------------------------------*/
  64. /*---------------------------------------------------------------------------*/
  65. /* Stucture declarations */
  66. /*---------------------------------------------------------------------------*/
  67. /*---------------------------------------------------------------------------*/
  68. /* Type declarations */
  69. /*---------------------------------------------------------------------------*/
  70. /*---------------------------------------------------------------------------*/
  71. /* Variable declarations */
  72. /*---------------------------------------------------------------------------*/
  73. #ifndef lint
  74. static char rcsid[] DD_UNUSED = "$Id: cuddBridge.c,v 1.20 2012/02/05 01:07:18 fabio Exp $";
  75. #endif
  76. /*---------------------------------------------------------------------------*/
  77. /* Macro declarations */
  78. /*---------------------------------------------------------------------------*/
  79. #ifdef __cplusplus
  80. extern "C" {
  81. #endif
  82. /**AutomaticStart*************************************************************/
  83. /*---------------------------------------------------------------------------*/
  84. /* Static function prototypes */
  85. /*---------------------------------------------------------------------------*/
  86. static DdNode * addBddDoThreshold (DdManager *dd, DdNode *f, DdNode *val);
  87. static DdNode * addBddDoStrictThreshold (DdManager *dd, DdNode *f, DdNode *val);
  88. static DdNode * addBddDoInterval (DdManager *dd, DdNode *f, DdNode *l, DdNode *u);
  89. static DdNode * addBddDoIthBit (DdManager *dd, DdNode *f, DdNode *index);
  90. static DdNode * ddBddToAddRecur (DdManager *dd, DdNode *B);
  91. static DdNode * cuddBddTransferRecur (DdManager *ddS, DdManager *ddD, DdNode *f, st_table *table);
  92. /**AutomaticEnd***************************************************************/
  93. #ifdef __cplusplus
  94. }
  95. #endif
  96. /*---------------------------------------------------------------------------*/
  97. /* Definition of exported functions */
  98. /*---------------------------------------------------------------------------*/
  99. /**Function********************************************************************
  100. Synopsis [Converts an ADD to a BDD.]
  101. Description [Converts an ADD to a BDD by replacing all
  102. discriminants greater than or equal to value with 1, and all other
  103. discriminants with 0. Returns a pointer to the resulting BDD if
  104. successful; NULL otherwise.]
  105. SideEffects [None]
  106. SeeAlso [Cudd_addBddInterval Cudd_addBddPattern Cudd_BddToAdd
  107. Cudd_addBddStrictThreshold]
  108. ******************************************************************************/
  109. DdNode *
  110. Cudd_addBddThreshold(
  111. DdManager * dd,
  112. DdNode * f,
  113. CUDD_VALUE_TYPE value)
  114. {
  115. DdNode *res;
  116. DdNode *val;
  117. val = cuddUniqueConst(dd,value);
  118. if (val == NULL) return(NULL);
  119. cuddRef(val);
  120. do {
  121. dd->reordered = 0;
  122. res = addBddDoThreshold(dd, f, val);
  123. } while (dd->reordered == 1);
  124. if (res == NULL) {
  125. Cudd_RecursiveDeref(dd, val);
  126. return(NULL);
  127. }
  128. cuddRef(res);
  129. Cudd_RecursiveDeref(dd, val);
  130. cuddDeref(res);
  131. return(res);
  132. } /* end of Cudd_addBddThreshold */
  133. /**Function********************************************************************
  134. Synopsis [Converts an ADD to a BDD.]
  135. Description [Converts an ADD to a BDD by replacing all
  136. discriminants STRICTLY greater than value with 1, and all other
  137. discriminants with 0. Returns a pointer to the resulting BDD if
  138. successful; NULL otherwise.]
  139. SideEffects [None]
  140. SeeAlso [Cudd_addBddInterval Cudd_addBddPattern Cudd_BddToAdd
  141. Cudd_addBddThreshold]
  142. ******************************************************************************/
  143. DdNode *
  144. Cudd_addBddStrictThreshold(
  145. DdManager * dd,
  146. DdNode * f,
  147. CUDD_VALUE_TYPE value)
  148. {
  149. DdNode *res;
  150. DdNode *val;
  151. val = cuddUniqueConst(dd,value);
  152. if (val == NULL) return(NULL);
  153. cuddRef(val);
  154. do {
  155. dd->reordered = 0;
  156. res = addBddDoStrictThreshold(dd, f, val);
  157. } while (dd->reordered == 1);
  158. if (res == NULL) {
  159. Cudd_RecursiveDeref(dd, val);
  160. return(NULL);
  161. }
  162. cuddRef(res);
  163. Cudd_RecursiveDeref(dd, val);
  164. cuddDeref(res);
  165. return(res);
  166. } /* end of Cudd_addBddStrictThreshold */
  167. /**Function********************************************************************
  168. Synopsis [Converts an ADD to a BDD.]
  169. Description [Converts an ADD to a BDD by replacing all
  170. discriminants greater than or equal to lower and less than or equal to
  171. upper with 1, and all other discriminants with 0. Returns a pointer to
  172. the resulting BDD if successful; NULL otherwise.]
  173. SideEffects [None]
  174. SeeAlso [Cudd_addBddThreshold Cudd_addBddStrictThreshold
  175. Cudd_addBddPattern Cudd_BddToAdd]
  176. ******************************************************************************/
  177. DdNode *
  178. Cudd_addBddInterval(
  179. DdManager * dd,
  180. DdNode * f,
  181. CUDD_VALUE_TYPE lower,
  182. CUDD_VALUE_TYPE upper)
  183. {
  184. DdNode *res;
  185. DdNode *l;
  186. DdNode *u;
  187. /* Create constant nodes for the interval bounds, so that we can use
  188. ** the global cache.
  189. */
  190. l = cuddUniqueConst(dd,lower);
  191. if (l == NULL) return(NULL);
  192. cuddRef(l);
  193. u = cuddUniqueConst(dd,upper);
  194. if (u == NULL) {
  195. Cudd_RecursiveDeref(dd,l);
  196. return(NULL);
  197. }
  198. cuddRef(u);
  199. do {
  200. dd->reordered = 0;
  201. res = addBddDoInterval(dd, f, l, u);
  202. } while (dd->reordered == 1);
  203. if (res == NULL) {
  204. Cudd_RecursiveDeref(dd, l);
  205. Cudd_RecursiveDeref(dd, u);
  206. return(NULL);
  207. }
  208. cuddRef(res);
  209. Cudd_RecursiveDeref(dd, l);
  210. Cudd_RecursiveDeref(dd, u);
  211. cuddDeref(res);
  212. return(res);
  213. } /* end of Cudd_addBddInterval */
  214. /**Function********************************************************************
  215. Synopsis [Converts an ADD to a BDD by extracting the i-th bit from
  216. the leaves.]
  217. Description [Converts an ADD to a BDD by replacing all
  218. discriminants whose i-th bit is equal to 1 with 1, and all other
  219. discriminants with 0. The i-th bit refers to the integer
  220. representation of the leaf value. If the value is has a fractional
  221. part, it is ignored. Repeated calls to this procedure allow one to
  222. transform an integer-valued ADD into an array of BDDs, one for each
  223. bit of the leaf values. Returns a pointer to the resulting BDD if
  224. successful; NULL otherwise.]
  225. SideEffects [None]
  226. SeeAlso [Cudd_addBddInterval Cudd_addBddPattern Cudd_BddToAdd]
  227. ******************************************************************************/
  228. DdNode *
  229. Cudd_addBddIthBit(
  230. DdManager * dd,
  231. DdNode * f,
  232. int bit)
  233. {
  234. DdNode *res;
  235. DdNode *index;
  236. index = cuddUniqueConst(dd,(CUDD_VALUE_TYPE) bit);
  237. if (index == NULL) return(NULL);
  238. cuddRef(index);
  239. do {
  240. dd->reordered = 0;
  241. res = addBddDoIthBit(dd, f, index);
  242. } while (dd->reordered == 1);
  243. if (res == NULL) {
  244. Cudd_RecursiveDeref(dd, index);
  245. return(NULL);
  246. }
  247. cuddRef(res);
  248. Cudd_RecursiveDeref(dd, index);
  249. cuddDeref(res);
  250. return(res);
  251. } /* end of Cudd_addBddIthBit */
  252. /**Function********************************************************************
  253. Synopsis [Converts a BDD to a 0-1 ADD.]
  254. Description [Converts a BDD to a 0-1 ADD. Returns a pointer to the
  255. resulting ADD if successful; NULL otherwise.]
  256. SideEffects [None]
  257. SeeAlso [Cudd_addBddPattern Cudd_addBddThreshold Cudd_addBddInterval
  258. Cudd_addBddStrictThreshold]
  259. ******************************************************************************/
  260. DdNode *
  261. Cudd_BddToAdd(
  262. DdManager * dd,
  263. DdNode * B)
  264. {
  265. DdNode *res;
  266. do {
  267. dd->reordered = 0;
  268. res = ddBddToAddRecur(dd, B);
  269. } while (dd->reordered ==1);
  270. return(res);
  271. } /* end of Cudd_BddToAdd */
  272. /**Function********************************************************************
  273. Synopsis [Converts an ADD to a BDD.]
  274. Description [Converts an ADD to a BDD by replacing all
  275. discriminants different from 0 with 1. Returns a pointer to the
  276. resulting BDD if successful; NULL otherwise.]
  277. SideEffects [None]
  278. SeeAlso [Cudd_BddToAdd Cudd_addBddThreshold Cudd_addBddInterval
  279. Cudd_addBddStrictThreshold]
  280. ******************************************************************************/
  281. DdNode *
  282. Cudd_addBddPattern(
  283. DdManager * dd,
  284. DdNode * f)
  285. {
  286. DdNode *res;
  287. do {
  288. dd->reordered = 0;
  289. res = cuddAddBddDoPattern(dd, f);
  290. } while (dd->reordered == 1);
  291. return(res);
  292. } /* end of Cudd_addBddPattern */
  293. /**Function********************************************************************
  294. Synopsis [Convert a BDD from a manager to another one.]
  295. Description [Convert a BDD from a manager to another one. The orders of the
  296. variables in the two managers may be different. Returns a
  297. pointer to the BDD in the destination manager if successful; NULL
  298. otherwise.]
  299. SideEffects [None]
  300. SeeAlso []
  301. ******************************************************************************/
  302. DdNode *
  303. Cudd_bddTransfer(
  304. DdManager * ddSource,
  305. DdManager * ddDestination,
  306. DdNode * f)
  307. {
  308. DdNode *res;
  309. do {
  310. ddDestination->reordered = 0;
  311. res = cuddBddTransfer(ddSource, ddDestination, f);
  312. } while (ddDestination->reordered == 1);
  313. return(res);
  314. } /* end of Cudd_bddTransfer */
  315. /*---------------------------------------------------------------------------*/
  316. /* Definition of internal functions */
  317. /*---------------------------------------------------------------------------*/
  318. /**Function********************************************************************
  319. Synopsis [Convert a BDD from a manager to another one.]
  320. Description [Convert a BDD from a manager to another one. Returns a
  321. pointer to the BDD in the destination manager if successful; NULL
  322. otherwise.]
  323. SideEffects [None]
  324. SeeAlso [Cudd_bddTransfer]
  325. ******************************************************************************/
  326. DdNode *
  327. cuddBddTransfer(
  328. DdManager * ddS,
  329. DdManager * ddD,
  330. DdNode * f)
  331. {
  332. DdNode *res;
  333. st_table *table = NULL;
  334. st_generator *gen = NULL;
  335. DdNode *key, *value;
  336. table = st_init_table(st_ptrcmp,st_ptrhash);
  337. if (table == NULL) goto failure;
  338. res = cuddBddTransferRecur(ddS, ddD, f, table);
  339. if (res != NULL) cuddRef(res);
  340. /* Dereference all elements in the table and dispose of the table.
  341. ** This must be done also if res is NULL to avoid leaks in case of
  342. ** reordering. */
  343. gen = st_init_gen(table);
  344. if (gen == NULL) goto failure;
  345. while (st_gen(gen, &key, &value)) {
  346. Cudd_RecursiveDeref(ddD, value);
  347. }
  348. st_free_gen(gen); gen = NULL;
  349. st_free_table(table); table = NULL;
  350. if (res != NULL) cuddDeref(res);
  351. return(res);
  352. failure:
  353. /* No need to free gen because it is always NULL here. */
  354. if (table != NULL) st_free_table(table);
  355. return(NULL);
  356. } /* end of cuddBddTransfer */
  357. /**Function********************************************************************
  358. Synopsis [Performs the recursive step for Cudd_addBddPattern.]
  359. Description [Performs the recursive step for Cudd_addBddPattern. Returns a
  360. pointer to the resulting BDD if successful; NULL otherwise.]
  361. SideEffects [None]
  362. SeeAlso []
  363. ******************************************************************************/
  364. DdNode *
  365. cuddAddBddDoPattern(
  366. DdManager * dd,
  367. DdNode * f)
  368. {
  369. DdNode *res, *T, *E;
  370. DdNode *fv, *fvn;
  371. int v;
  372. statLine(dd);
  373. /* Check terminal case. */
  374. if (cuddIsConstant(f)) {
  375. return(Cudd_NotCond(DD_ONE(dd),f == DD_ZERO(dd)));
  376. }
  377. /* Check cache. */
  378. res = cuddCacheLookup1(dd,Cudd_addBddPattern,f);
  379. if (res != NULL) return(res);
  380. /* Recursive step. */
  381. v = f->index;
  382. fv = cuddT(f); fvn = cuddE(f);
  383. T = cuddAddBddDoPattern(dd,fv);
  384. if (T == NULL) return(NULL);
  385. cuddRef(T);
  386. E = cuddAddBddDoPattern(dd,fvn);
  387. if (E == NULL) {
  388. Cudd_RecursiveDeref(dd, T);
  389. return(NULL);
  390. }
  391. cuddRef(E);
  392. if (Cudd_IsComplement(T)) {
  393. res = (T == E) ? Cudd_Not(T) : cuddUniqueInter(dd,v,Cudd_Not(T),Cudd_Not(E));
  394. if (res == NULL) {
  395. Cudd_RecursiveDeref(dd, T);
  396. Cudd_RecursiveDeref(dd, E);
  397. return(NULL);
  398. }
  399. res = Cudd_Not(res);
  400. } else {
  401. res = (T == E) ? T : cuddUniqueInter(dd,v,T,E);
  402. if (res == NULL) {
  403. Cudd_RecursiveDeref(dd, T);
  404. Cudd_RecursiveDeref(dd, E);
  405. return(NULL);
  406. }
  407. }
  408. cuddDeref(T);
  409. cuddDeref(E);
  410. /* Store result. */
  411. cuddCacheInsert1(dd,Cudd_addBddPattern,f,res);
  412. return(res);
  413. } /* end of cuddAddBddDoPattern */
  414. /*---------------------------------------------------------------------------*/
  415. /* Definition of static functions */
  416. /*---------------------------------------------------------------------------*/
  417. /**Function********************************************************************
  418. Synopsis [Performs the recursive step for Cudd_addBddThreshold.]
  419. Description [Performs the recursive step for Cudd_addBddThreshold.
  420. Returns a pointer to the BDD if successful; NULL otherwise.]
  421. SideEffects [None]
  422. SeeAlso [addBddDoStrictThreshold]
  423. ******************************************************************************/
  424. static DdNode *
  425. addBddDoThreshold(
  426. DdManager * dd,
  427. DdNode * f,
  428. DdNode * val)
  429. {
  430. DdNode *res, *T, *E;
  431. DdNode *fv, *fvn;
  432. int v;
  433. statLine(dd);
  434. /* Check terminal case. */
  435. if (cuddIsConstant(f)) {
  436. return(Cudd_NotCond(DD_ONE(dd),cuddV(f) < cuddV(val)));
  437. }
  438. /* Check cache. */
  439. res = cuddCacheLookup2(dd,addBddDoThreshold,f,val);
  440. if (res != NULL) return(res);
  441. /* Recursive step. */
  442. v = f->index;
  443. fv = cuddT(f); fvn = cuddE(f);
  444. T = addBddDoThreshold(dd,fv,val);
  445. if (T == NULL) return(NULL);
  446. cuddRef(T);
  447. E = addBddDoThreshold(dd,fvn,val);
  448. if (E == NULL) {
  449. Cudd_RecursiveDeref(dd, T);
  450. return(NULL);
  451. }
  452. cuddRef(E);
  453. if (Cudd_IsComplement(T)) {
  454. res = (T == E) ? Cudd_Not(T) : cuddUniqueInter(dd,v,Cudd_Not(T),Cudd_Not(E));
  455. if (res == NULL) {
  456. Cudd_RecursiveDeref(dd, T);
  457. Cudd_RecursiveDeref(dd, E);
  458. return(NULL);
  459. }
  460. res = Cudd_Not(res);
  461. } else {
  462. res = (T == E) ? T : cuddUniqueInter(dd,v,T,E);
  463. if (res == NULL) {
  464. Cudd_RecursiveDeref(dd, T);
  465. Cudd_RecursiveDeref(dd, E);
  466. return(NULL);
  467. }
  468. }
  469. cuddDeref(T);
  470. cuddDeref(E);
  471. /* Store result. */
  472. cuddCacheInsert2(dd,addBddDoThreshold,f,val,res);
  473. return(res);
  474. } /* end of addBddDoThreshold */
  475. /**Function********************************************************************
  476. Synopsis [Performs the recursive step for Cudd_addBddStrictThreshold.]
  477. Description [Performs the recursive step for Cudd_addBddStrictThreshold.
  478. Returns a pointer to the BDD if successful; NULL otherwise.]
  479. SideEffects [None]
  480. SeeAlso [addBddDoThreshold]
  481. ******************************************************************************/
  482. static DdNode *
  483. addBddDoStrictThreshold(
  484. DdManager * dd,
  485. DdNode * f,
  486. DdNode * val)
  487. {
  488. DdNode *res, *T, *E;
  489. DdNode *fv, *fvn;
  490. int v;
  491. statLine(dd);
  492. /* Check terminal case. */
  493. if (cuddIsConstant(f)) {
  494. return(Cudd_NotCond(DD_ONE(dd),cuddV(f) <= cuddV(val)));
  495. }
  496. /* Check cache. */
  497. res = cuddCacheLookup2(dd,addBddDoStrictThreshold,f,val);
  498. if (res != NULL) return(res);
  499. /* Recursive step. */
  500. v = f->index;
  501. fv = cuddT(f); fvn = cuddE(f);
  502. T = addBddDoStrictThreshold(dd,fv,val);
  503. if (T == NULL) return(NULL);
  504. cuddRef(T);
  505. E = addBddDoStrictThreshold(dd,fvn,val);
  506. if (E == NULL) {
  507. Cudd_RecursiveDeref(dd, T);
  508. return(NULL);
  509. }
  510. cuddRef(E);
  511. if (Cudd_IsComplement(T)) {
  512. res = (T == E) ? Cudd_Not(T) : cuddUniqueInter(dd,v,Cudd_Not(T),Cudd_Not(E));
  513. if (res == NULL) {
  514. Cudd_RecursiveDeref(dd, T);
  515. Cudd_RecursiveDeref(dd, E);
  516. return(NULL);
  517. }
  518. res = Cudd_Not(res);
  519. } else {
  520. res = (T == E) ? T : cuddUniqueInter(dd,v,T,E);
  521. if (res == NULL) {
  522. Cudd_RecursiveDeref(dd, T);
  523. Cudd_RecursiveDeref(dd, E);
  524. return(NULL);
  525. }
  526. }
  527. cuddDeref(T);
  528. cuddDeref(E);
  529. /* Store result. */
  530. cuddCacheInsert2(dd,addBddDoStrictThreshold,f,val,res);
  531. return(res);
  532. } /* end of addBddDoStrictThreshold */
  533. /**Function********************************************************************
  534. Synopsis [Performs the recursive step for Cudd_addBddInterval.]
  535. Description [Performs the recursive step for Cudd_addBddInterval.
  536. Returns a pointer to the BDD if successful; NULL otherwise.]
  537. SideEffects [None]
  538. SeeAlso [addBddDoThreshold addBddDoStrictThreshold]
  539. ******************************************************************************/
  540. static DdNode *
  541. addBddDoInterval(
  542. DdManager * dd,
  543. DdNode * f,
  544. DdNode * l,
  545. DdNode * u)
  546. {
  547. DdNode *res, *T, *E;
  548. DdNode *fv, *fvn;
  549. int v;
  550. statLine(dd);
  551. /* Check terminal case. */
  552. if (cuddIsConstant(f)) {
  553. return(Cudd_NotCond(DD_ONE(dd),cuddV(f) < cuddV(l) || cuddV(f) > cuddV(u)));
  554. }
  555. /* Check cache. */
  556. res = cuddCacheLookup(dd,DD_ADD_BDD_DO_INTERVAL_TAG,f,l,u);
  557. if (res != NULL) return(res);
  558. /* Recursive step. */
  559. v = f->index;
  560. fv = cuddT(f); fvn = cuddE(f);
  561. T = addBddDoInterval(dd,fv,l,u);
  562. if (T == NULL) return(NULL);
  563. cuddRef(T);
  564. E = addBddDoInterval(dd,fvn,l,u);
  565. if (E == NULL) {
  566. Cudd_RecursiveDeref(dd, T);
  567. return(NULL);
  568. }
  569. cuddRef(E);
  570. if (Cudd_IsComplement(T)) {
  571. res = (T == E) ? Cudd_Not(T) : cuddUniqueInter(dd,v,Cudd_Not(T),Cudd_Not(E));
  572. if (res == NULL) {
  573. Cudd_RecursiveDeref(dd, T);
  574. Cudd_RecursiveDeref(dd, E);
  575. return(NULL);
  576. }
  577. res = Cudd_Not(res);
  578. } else {
  579. res = (T == E) ? T : cuddUniqueInter(dd,v,T,E);
  580. if (res == NULL) {
  581. Cudd_RecursiveDeref(dd, T);
  582. Cudd_RecursiveDeref(dd, E);
  583. return(NULL);
  584. }
  585. }
  586. cuddDeref(T);
  587. cuddDeref(E);
  588. /* Store result. */
  589. cuddCacheInsert(dd,DD_ADD_BDD_DO_INTERVAL_TAG,f,l,u,res);
  590. return(res);
  591. } /* end of addBddDoInterval */
  592. /**Function********************************************************************
  593. Synopsis [Performs the recursive step for Cudd_addBddIthBit.]
  594. Description [Performs the recursive step for Cudd_addBddIthBit.
  595. Returns a pointer to the BDD if successful; NULL otherwise.]
  596. SideEffects [None]
  597. SeeAlso []
  598. ******************************************************************************/
  599. static DdNode *
  600. addBddDoIthBit(
  601. DdManager * dd,
  602. DdNode * f,
  603. DdNode * index)
  604. {
  605. DdNode *res, *T, *E;
  606. DdNode *fv, *fvn;
  607. int mask, value;
  608. int v;
  609. statLine(dd);
  610. /* Check terminal case. */
  611. if (cuddIsConstant(f)) {
  612. mask = 1 << ((int) cuddV(index));
  613. value = (int) cuddV(f);
  614. return(Cudd_NotCond(DD_ONE(dd),(value & mask) == 0));
  615. }
  616. /* Check cache. */
  617. res = cuddCacheLookup2(dd,addBddDoIthBit,f,index);
  618. if (res != NULL) return(res);
  619. /* Recursive step. */
  620. v = f->index;
  621. fv = cuddT(f); fvn = cuddE(f);
  622. T = addBddDoIthBit(dd,fv,index);
  623. if (T == NULL) return(NULL);
  624. cuddRef(T);
  625. E = addBddDoIthBit(dd,fvn,index);
  626. if (E == NULL) {
  627. Cudd_RecursiveDeref(dd, T);
  628. return(NULL);
  629. }
  630. cuddRef(E);
  631. if (Cudd_IsComplement(T)) {
  632. res = (T == E) ? Cudd_Not(T) : cuddUniqueInter(dd,v,Cudd_Not(T),Cudd_Not(E));
  633. if (res == NULL) {
  634. Cudd_RecursiveDeref(dd, T);
  635. Cudd_RecursiveDeref(dd, E);
  636. return(NULL);
  637. }
  638. res = Cudd_Not(res);
  639. } else {
  640. res = (T == E) ? T : cuddUniqueInter(dd,v,T,E);
  641. if (res == NULL) {
  642. Cudd_RecursiveDeref(dd, T);
  643. Cudd_RecursiveDeref(dd, E);
  644. return(NULL);
  645. }
  646. }
  647. cuddDeref(T);
  648. cuddDeref(E);
  649. /* Store result. */
  650. cuddCacheInsert2(dd,addBddDoIthBit,f,index,res);
  651. return(res);
  652. } /* end of addBddDoIthBit */
  653. /**Function********************************************************************
  654. Synopsis [Performs the recursive step for Cudd_BddToAdd.]
  655. Description [Performs the recursive step for Cudd_BddToAdd. Returns a
  656. pointer to the resulting ADD if successful; NULL otherwise.]
  657. SideEffects [None]
  658. SeeAlso []
  659. ******************************************************************************/
  660. static DdNode *
  661. ddBddToAddRecur(
  662. DdManager * dd,
  663. DdNode * B)
  664. {
  665. DdNode *one;
  666. DdNode *res, *res1, *T, *E, *Bt, *Be;
  667. int complement = 0;
  668. statLine(dd);
  669. one = DD_ONE(dd);
  670. if (Cudd_IsConstant(B)) {
  671. if (B == one) {
  672. res = one;
  673. } else {
  674. res = DD_ZERO(dd);
  675. }
  676. return(res);
  677. }
  678. /* Check visited table */
  679. res = cuddCacheLookup1(dd,ddBddToAddRecur,B);
  680. if (res != NULL) return(res);
  681. if (Cudd_IsComplement(B)) {
  682. complement = 1;
  683. Bt = cuddT(Cudd_Regular(B));
  684. Be = cuddE(Cudd_Regular(B));
  685. } else {
  686. Bt = cuddT(B);
  687. Be = cuddE(B);
  688. }
  689. T = ddBddToAddRecur(dd, Bt);
  690. if (T == NULL) return(NULL);
  691. cuddRef(T);
  692. E = ddBddToAddRecur(dd, Be);
  693. if (E == NULL) {
  694. Cudd_RecursiveDeref(dd, T);
  695. return(NULL);
  696. }
  697. cuddRef(E);
  698. /* No need to check for T == E, because it is guaranteed not to happen. */
  699. res = cuddUniqueInter(dd, (int) Cudd_Regular(B)->index, T, E);
  700. if (res == NULL) {
  701. Cudd_RecursiveDeref(dd ,T);
  702. Cudd_RecursiveDeref(dd ,E);
  703. return(NULL);
  704. }
  705. cuddDeref(T);
  706. cuddDeref(E);
  707. if (complement) {
  708. cuddRef(res);
  709. res1 = cuddAddCmplRecur(dd, res);
  710. if (res1 == NULL) {
  711. Cudd_RecursiveDeref(dd, res);
  712. return(NULL);
  713. }
  714. cuddRef(res1);
  715. Cudd_RecursiveDeref(dd, res);
  716. res = res1;
  717. cuddDeref(res);
  718. }
  719. /* Store result. */
  720. cuddCacheInsert1(dd,ddBddToAddRecur,B,res);
  721. return(res);
  722. } /* end of ddBddToAddRecur */
  723. /**Function********************************************************************
  724. Synopsis [Performs the recursive step of Cudd_bddTransfer.]
  725. Description [Performs the recursive step of Cudd_bddTransfer.
  726. Returns a pointer to the result if successful; NULL otherwise.]
  727. SideEffects [None]
  728. SeeAlso [cuddBddTransfer]
  729. ******************************************************************************/
  730. static DdNode *
  731. cuddBddTransferRecur(
  732. DdManager * ddS,
  733. DdManager * ddD,
  734. DdNode * f,
  735. st_table * table)
  736. {
  737. DdNode *ft, *fe, *t, *e, *var, *res;
  738. DdNode *one, *zero;
  739. int index;
  740. int comple = 0;
  741. statLine(ddD);
  742. one = DD_ONE(ddD);
  743. comple = Cudd_IsComplement(f);
  744. /* Trivial cases. */
  745. if (Cudd_IsConstant(f)) return(Cudd_NotCond(one, comple));
  746. /* Make canonical to increase the utilization of the cache. */
  747. f = Cudd_NotCond(f,comple);
  748. /* Now f is a regular pointer to a non-constant node. */
  749. /* Check the cache. */
  750. if (st_lookup(table, f, &res))
  751. return(Cudd_NotCond(res,comple));
  752. /* Recursive step. */
  753. index = f->index;
  754. ft = cuddT(f); fe = cuddE(f);
  755. t = cuddBddTransferRecur(ddS, ddD, ft, table);
  756. if (t == NULL) {
  757. return(NULL);
  758. }
  759. cuddRef(t);
  760. e = cuddBddTransferRecur(ddS, ddD, fe, table);
  761. if (e == NULL) {
  762. Cudd_RecursiveDeref(ddD, t);
  763. return(NULL);
  764. }
  765. cuddRef(e);
  766. zero = Cudd_Not(one);
  767. var = cuddUniqueInter(ddD,index,one,zero);
  768. if (var == NULL) {
  769. Cudd_RecursiveDeref(ddD, t);
  770. Cudd_RecursiveDeref(ddD, e);
  771. return(NULL);
  772. }
  773. res = cuddBddIteRecur(ddD,var,t,e);
  774. if (res == NULL) {
  775. Cudd_RecursiveDeref(ddD, t);
  776. Cudd_RecursiveDeref(ddD, e);
  777. return(NULL);
  778. }
  779. cuddRef(res);
  780. Cudd_RecursiveDeref(ddD, t);
  781. Cudd_RecursiveDeref(ddD, e);
  782. if (st_add_direct(table, (char *) f, (char *) res) == ST_OUT_OF_MEM) {
  783. Cudd_RecursiveDeref(ddD, res);
  784. return(NULL);
  785. }
  786. return(Cudd_NotCond(res,comple));
  787. } /* end of cuddBddTransferRecur */