The source code and dockerfile for the GSW2024 AI Lab.
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

986 lines
23 KiB

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