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.

800 lines
23 KiB

  1. /**
  2. @file
  3. @ingroup cudd
  4. @brief Quantification functions for ADDs.
  5. @author Fabio Somenzi
  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 int addCheckPositiveCube (DdManager *manager, DdNode *cube);
  56. /** \endcond */
  57. /*---------------------------------------------------------------------------*/
  58. /* Definition of exported functions */
  59. /*---------------------------------------------------------------------------*/
  60. /**
  61. @brief Existentially Abstracts all the variables in cube from f.
  62. @details Abstracts all the variables in cube from f by summing
  63. over all possible values taken by the variables.
  64. @return the abstracted %ADD.
  65. @sideeffect None
  66. @see Cudd_addUnivAbstract Cudd_bddExistAbstract
  67. Cudd_addOrAbstract
  68. */
  69. DdNode *
  70. Cudd_addExistAbstract(
  71. DdManager * manager,
  72. DdNode * f,
  73. DdNode * cube)
  74. {
  75. DdNode *res;
  76. if (addCheckPositiveCube(manager, cube) == 0) {
  77. (void) fprintf(manager->err,"Error: Can only abstract cubes");
  78. return(NULL);
  79. }
  80. do {
  81. manager->reordered = 0;
  82. res = cuddAddExistAbstractRecur(manager, f, cube);
  83. } while (manager->reordered == 1);
  84. if (manager->errorCode == CUDD_TIMEOUT_EXPIRED && manager->timeoutHandler) {
  85. manager->timeoutHandler(manager, manager->tohArg);
  86. }
  87. return(res);
  88. } /* end of Cudd_addExistAbstract */
  89. /**
  90. @brief Universally Abstracts all the variables in cube from f.
  91. @details Abstracts all the variables in cube from f by taking
  92. the product over all possible values taken by the variable.
  93. @return the abstracted %ADD if successful; NULL otherwise.
  94. @sideeffect None
  95. @see Cudd_addExistAbstract Cudd_bddUnivAbstract
  96. Cudd_addOrAbstract
  97. */
  98. DdNode *
  99. Cudd_addUnivAbstract(
  100. DdManager * manager,
  101. DdNode * f,
  102. DdNode * cube)
  103. {
  104. DdNode *res;
  105. if (addCheckPositiveCube(manager, cube) == 0) {
  106. (void) fprintf(manager->err,"Error: Can only abstract cubes");
  107. return(NULL);
  108. }
  109. do {
  110. manager->reordered = 0;
  111. res = cuddAddUnivAbstractRecur(manager, f, cube);
  112. } while (manager->reordered == 1);
  113. if (manager->errorCode == CUDD_TIMEOUT_EXPIRED && manager->timeoutHandler) {
  114. manager->timeoutHandler(manager, manager->tohArg);
  115. }
  116. return(res);
  117. } /* end of Cudd_addUnivAbstract */
  118. /**
  119. @brief Disjunctively abstracts all the variables in cube from the
  120. 0-1 %ADD f.
  121. @details Abstracts all the variables in cube from the 0-1 %ADD f
  122. by taking the disjunction over all possible values taken by the
  123. variables.
  124. @return the abstracted %ADD if successful; NULL otherwise.
  125. @sideeffect None
  126. @see Cudd_addUnivAbstract Cudd_addExistAbstract
  127. */
  128. DdNode *
  129. Cudd_addOrAbstract(
  130. DdManager * manager,
  131. DdNode * f,
  132. DdNode * cube)
  133. {
  134. DdNode *res;
  135. if (addCheckPositiveCube(manager, cube) == 0) {
  136. (void) fprintf(manager->err,"Error: Can only abstract cubes");
  137. return(NULL);
  138. }
  139. do {
  140. manager->reordered = 0;
  141. res = cuddAddOrAbstractRecur(manager, f, cube);
  142. } while (manager->reordered == 1);
  143. if (manager->errorCode == CUDD_TIMEOUT_EXPIRED && manager->timeoutHandler) {
  144. manager->timeoutHandler(manager, manager->tohArg);
  145. }
  146. return(res);
  147. } /* end of Cudd_addOrAbstract */
  148. /**Function********************************************************************
  149. Synopsis [Abstracts all the variables in cube from the
  150. ADD f by taking the minimum.]
  151. Description [Abstracts all the variables in cube from the ADD f
  152. by taking the minimum over all possible values taken by the
  153. variables. Returns the abstracted ADD if successful; NULL
  154. otherwise.]
  155. SideEffects [None]
  156. SeeAlso [Cudd_addUnivAbstract Cudd_addExistAbstract]
  157. Note: Added by Dave Parker 14/6/99
  158. ******************************************************************************/
  159. DdNode *
  160. Cudd_addMinAbstract(
  161. DdManager * manager,
  162. DdNode * f,
  163. DdNode * cube)
  164. {
  165. DdNode *res;
  166. if (addCheckPositiveCube(manager, cube) == 0) {
  167. (void) fprintf(manager->err,"Error: Can only abstract cubes");
  168. return(NULL);
  169. }
  170. do {
  171. manager->reordered = 0;
  172. res = cuddAddMinAbstractRecur(manager, f, cube);
  173. } while (manager->reordered == 1);
  174. return(res);
  175. } /* end of Cudd_addMinAbstract */
  176. /**Function********************************************************************
  177. Synopsis [Abstracts all the variables in cube from the
  178. ADD f by taking the maximum.]
  179. Description [Abstracts all the variables in cube from the ADD f
  180. by taking the maximum over all possible values taken by the
  181. variables. Returns the abstracted ADD if successful; NULL
  182. otherwise.]
  183. SideEffects [None]
  184. SeeAlso [Cudd_addUnivAbstract Cudd_addExistAbstract]
  185. Note: Added by Dave Parker 14/6/99
  186. ******************************************************************************/
  187. DdNode *
  188. Cudd_addMaxAbstract(
  189. DdManager * manager,
  190. DdNode * f,
  191. DdNode * cube)
  192. {
  193. DdNode *res;
  194. if (addCheckPositiveCube(manager, cube) == 0) {
  195. (void) fprintf(manager->err,"Error: Can only abstract cubes");
  196. return(NULL);
  197. }
  198. do {
  199. manager->reordered = 0;
  200. res = cuddAddMaxAbstractRecur(manager, f, cube);
  201. } while (manager->reordered == 1);
  202. return(res);
  203. } /* end of Cudd_addMaxAbstract */
  204. /*---------------------------------------------------------------------------*/
  205. /* Definition of internal functions */
  206. /*---------------------------------------------------------------------------*/
  207. /**
  208. @brief Performs the recursive step of Cudd_addExistAbstract.
  209. @details Returns the %ADD obtained by abstracting the variables of
  210. cube from f, if successful; NULL otherwise.
  211. @sideeffect None
  212. */
  213. DdNode *
  214. cuddAddExistAbstractRecur(
  215. DdManager * manager,
  216. DdNode * f,
  217. DdNode * cube)
  218. {
  219. DdNode *T, *E, *res, *res1, *res2, *zero;
  220. statLine(manager);
  221. zero = DD_ZERO(manager);
  222. /* Cube is guaranteed to be a cube at this point. */
  223. if (f == zero || cuddIsConstant(cube)) {
  224. return(f);
  225. }
  226. /* Abstract a variable that does not appear in f => multiply by 2. */
  227. if (cuddI(manager,f->index) > cuddI(manager,cube->index)) {
  228. res1 = cuddAddExistAbstractRecur(manager, f, cuddT(cube));
  229. if (res1 == NULL) return(NULL);
  230. cuddRef(res1);
  231. /* Use the "internal" procedure to be alerted in case of
  232. ** dynamic reordering. If dynamic reordering occurs, we
  233. ** have to abort the entire abstraction.
  234. */
  235. res = cuddAddApplyRecur(manager,Cudd_addPlus,res1,res1);
  236. if (res == NULL) {
  237. Cudd_RecursiveDeref(manager,res1);
  238. return(NULL);
  239. }
  240. cuddRef(res);
  241. Cudd_RecursiveDeref(manager,res1);
  242. cuddDeref(res);
  243. return(res);
  244. }
  245. if ((res = cuddCacheLookup2(manager, Cudd_addExistAbstract, f, cube)) != NULL) {
  246. return(res);
  247. }
  248. checkWhetherToGiveUp(manager);
  249. T = cuddT(f);
  250. E = cuddE(f);
  251. /* If the two indices are the same, so are their levels. */
  252. if (f->index == cube->index) {
  253. res1 = cuddAddExistAbstractRecur(manager, T, cuddT(cube));
  254. if (res1 == NULL) return(NULL);
  255. cuddRef(res1);
  256. res2 = cuddAddExistAbstractRecur(manager, E, cuddT(cube));
  257. if (res2 == NULL) {
  258. Cudd_RecursiveDeref(manager,res1);
  259. return(NULL);
  260. }
  261. cuddRef(res2);
  262. res = cuddAddApplyRecur(manager, Cudd_addPlus, res1, res2);
  263. if (res == NULL) {
  264. Cudd_RecursiveDeref(manager,res1);
  265. Cudd_RecursiveDeref(manager,res2);
  266. return(NULL);
  267. }
  268. cuddRef(res);
  269. Cudd_RecursiveDeref(manager,res1);
  270. Cudd_RecursiveDeref(manager,res2);
  271. cuddCacheInsert2(manager, Cudd_addExistAbstract, f, cube, res);
  272. cuddDeref(res);
  273. return(res);
  274. } else { /* if (cuddI(manager,f->index) < cuddI(manager,cube->index)) */
  275. res1 = cuddAddExistAbstractRecur(manager, T, cube);
  276. if (res1 == NULL) return(NULL);
  277. cuddRef(res1);
  278. res2 = cuddAddExistAbstractRecur(manager, E, cube);
  279. if (res2 == NULL) {
  280. Cudd_RecursiveDeref(manager,res1);
  281. return(NULL);
  282. }
  283. cuddRef(res2);
  284. res = (res1 == res2) ? res1 :
  285. cuddUniqueInter(manager, (int) f->index, res1, res2);
  286. if (res == NULL) {
  287. Cudd_RecursiveDeref(manager,res1);
  288. Cudd_RecursiveDeref(manager,res2);
  289. return(NULL);
  290. }
  291. cuddDeref(res1);
  292. cuddDeref(res2);
  293. cuddCacheInsert2(manager, Cudd_addExistAbstract, f, cube, res);
  294. return(res);
  295. }
  296. } /* end of cuddAddExistAbstractRecur */
  297. /**
  298. @brief Performs the recursive step of Cudd_addUnivAbstract.
  299. @return the %ADD obtained by abstracting the variables of cube from
  300. f, if successful; NULL otherwise.
  301. @sideeffect None
  302. */
  303. DdNode *
  304. cuddAddUnivAbstractRecur(
  305. DdManager * manager,
  306. DdNode * f,
  307. DdNode * cube)
  308. {
  309. DdNode *T, *E, *res, *res1, *res2, *one, *zero;
  310. statLine(manager);
  311. one = DD_ONE(manager);
  312. zero = DD_ZERO(manager);
  313. /* Cube is guaranteed to be a cube at this point.
  314. ** zero and one are the only constatnts c such that c*c=c.
  315. */
  316. if (f == zero || f == one || cube == one) {
  317. return(f);
  318. }
  319. /* Abstract a variable that does not appear in f. */
  320. if (cuddI(manager,f->index) > cuddI(manager,cube->index)) {
  321. res1 = cuddAddUnivAbstractRecur(manager, f, cuddT(cube));
  322. if (res1 == NULL) return(NULL);
  323. cuddRef(res1);
  324. /* Use the "internal" procedure to be alerted in case of
  325. ** dynamic reordering. If dynamic reordering occurs, we
  326. ** have to abort the entire abstraction.
  327. */
  328. res = cuddAddApplyRecur(manager, Cudd_addTimes, res1, res1);
  329. if (res == NULL) {
  330. Cudd_RecursiveDeref(manager,res1);
  331. return(NULL);
  332. }
  333. cuddRef(res);
  334. Cudd_RecursiveDeref(manager,res1);
  335. cuddDeref(res);
  336. return(res);
  337. }
  338. if ((res = cuddCacheLookup2(manager, Cudd_addUnivAbstract, f, cube)) != NULL) {
  339. return(res);
  340. }
  341. checkWhetherToGiveUp(manager);
  342. T = cuddT(f);
  343. E = cuddE(f);
  344. /* If the two indices are the same, so are their levels. */
  345. if (f->index == cube->index) {
  346. res1 = cuddAddUnivAbstractRecur(manager, T, cuddT(cube));
  347. if (res1 == NULL) return(NULL);
  348. cuddRef(res1);
  349. res2 = cuddAddUnivAbstractRecur(manager, E, cuddT(cube));
  350. if (res2 == NULL) {
  351. Cudd_RecursiveDeref(manager,res1);
  352. return(NULL);
  353. }
  354. cuddRef(res2);
  355. res = cuddAddApplyRecur(manager, Cudd_addTimes, res1, res2);
  356. if (res == NULL) {
  357. Cudd_RecursiveDeref(manager,res1);
  358. Cudd_RecursiveDeref(manager,res2);
  359. return(NULL);
  360. }
  361. cuddRef(res);
  362. Cudd_RecursiveDeref(manager,res1);
  363. Cudd_RecursiveDeref(manager,res2);
  364. cuddCacheInsert2(manager, Cudd_addUnivAbstract, f, cube, res);
  365. cuddDeref(res);
  366. return(res);
  367. } else { /* if (cuddI(manager,f->index) < cuddI(manager,cube->index)) */
  368. res1 = cuddAddUnivAbstractRecur(manager, T, cube);
  369. if (res1 == NULL) return(NULL);
  370. cuddRef(res1);
  371. res2 = cuddAddUnivAbstractRecur(manager, E, cube);
  372. if (res2 == NULL) {
  373. Cudd_RecursiveDeref(manager,res1);
  374. return(NULL);
  375. }
  376. cuddRef(res2);
  377. res = (res1 == res2) ? res1 :
  378. cuddUniqueInter(manager, (int) f->index, res1, res2);
  379. if (res == NULL) {
  380. Cudd_RecursiveDeref(manager,res1);
  381. Cudd_RecursiveDeref(manager,res2);
  382. return(NULL);
  383. }
  384. cuddDeref(res1);
  385. cuddDeref(res2);
  386. cuddCacheInsert2(manager, Cudd_addUnivAbstract, f, cube, res);
  387. return(res);
  388. }
  389. } /* end of cuddAddUnivAbstractRecur */
  390. /**
  391. @brief Performs the recursive step of Cudd_addOrAbstract.
  392. @return the %ADD obtained by abstracting the variables of cube from
  393. f, if successful; NULL otherwise.
  394. @sideeffect None
  395. */
  396. DdNode *
  397. cuddAddOrAbstractRecur(
  398. DdManager * manager,
  399. DdNode * f,
  400. DdNode * cube)
  401. {
  402. DdNode *T, *E, *res, *res1, *res2, *one;
  403. statLine(manager);
  404. one = DD_ONE(manager);
  405. /* Cube is guaranteed to be a cube at this point. */
  406. if (cuddIsConstant(f) || cube == one) {
  407. return(f);
  408. }
  409. /* Abstract a variable that does not appear in f. */
  410. if (cuddI(manager,f->index) > cuddI(manager,cube->index)) {
  411. res = cuddAddOrAbstractRecur(manager, f, cuddT(cube));
  412. return(res);
  413. }
  414. if ((res = cuddCacheLookup2(manager, Cudd_addOrAbstract, f, cube)) != NULL) {
  415. return(res);
  416. }
  417. checkWhetherToGiveUp(manager);
  418. T = cuddT(f);
  419. E = cuddE(f);
  420. /* If the two indices are the same, so are their levels. */
  421. if (f->index == cube->index) {
  422. res1 = cuddAddOrAbstractRecur(manager, T, cuddT(cube));
  423. if (res1 == NULL) return(NULL);
  424. cuddRef(res1);
  425. if (res1 != one) {
  426. res2 = cuddAddOrAbstractRecur(manager, E, cuddT(cube));
  427. if (res2 == NULL) {
  428. Cudd_RecursiveDeref(manager,res1);
  429. return(NULL);
  430. }
  431. cuddRef(res2);
  432. res = cuddAddApplyRecur(manager, Cudd_addOr, res1, res2);
  433. if (res == NULL) {
  434. Cudd_RecursiveDeref(manager,res1);
  435. Cudd_RecursiveDeref(manager,res2);
  436. return(NULL);
  437. }
  438. cuddRef(res);
  439. Cudd_RecursiveDeref(manager,res1);
  440. Cudd_RecursiveDeref(manager,res2);
  441. } else {
  442. res = res1;
  443. }
  444. cuddCacheInsert2(manager, Cudd_addOrAbstract, f, cube, res);
  445. cuddDeref(res);
  446. return(res);
  447. } else { /* if (cuddI(manager,f->index) < cuddI(manager,cube->index)) */
  448. res1 = cuddAddOrAbstractRecur(manager, T, cube);
  449. if (res1 == NULL) return(NULL);
  450. cuddRef(res1);
  451. res2 = cuddAddOrAbstractRecur(manager, E, cube);
  452. if (res2 == NULL) {
  453. Cudd_RecursiveDeref(manager,res1);
  454. return(NULL);
  455. }
  456. cuddRef(res2);
  457. res = (res1 == res2) ? res1 :
  458. cuddUniqueInter(manager, (int) f->index, res1, res2);
  459. if (res == NULL) {
  460. Cudd_RecursiveDeref(manager,res1);
  461. Cudd_RecursiveDeref(manager,res2);
  462. return(NULL);
  463. }
  464. cuddDeref(res1);
  465. cuddDeref(res2);
  466. cuddCacheInsert2(manager, Cudd_addOrAbstract, f, cube, res);
  467. return(res);
  468. }
  469. } /* end of cuddAddOrAbstractRecur */
  470. /**Function********************************************************************
  471. Synopsis [Performs the recursive step of Cudd_addMinAbstract.]
  472. Description [Performs the recursive step of Cudd_addMinAbstract.
  473. Returns the ADD obtained by abstracting the variables of cube from f,
  474. if successful; NULL otherwise.]
  475. SideEffects [None]
  476. SeeAlso []
  477. ******************************************************************************/
  478. DdNode *
  479. cuddAddMinAbstractRecur(
  480. DdManager * manager,
  481. DdNode * f,
  482. DdNode * cube)
  483. {
  484. DdNode *T, *E, *res, *res1, *res2, *zero;
  485. zero = DD_ZERO(manager);
  486. /* Cube is guaranteed to be a cube at this point. */
  487. if (f == zero || cuddIsConstant(cube)) {
  488. return(f);
  489. }
  490. /* Abstract a variable that does not appear in f. */
  491. if (cuddI(manager,f->index) > cuddI(manager,cube->index)) {
  492. res = cuddAddMinAbstractRecur(manager, f, cuddT(cube));
  493. return(res);
  494. }
  495. if ((res = cuddCacheLookup2(manager, Cudd_addMinAbstract, f, cube)) != NULL) {
  496. return(res);
  497. }
  498. T = cuddT(f);
  499. E = cuddE(f);
  500. /* If the two indices are the same, so are their levels. */
  501. if (f->index == cube->index) {
  502. res1 = cuddAddMinAbstractRecur(manager, T, cuddT(cube));
  503. if (res1 == NULL) return(NULL);
  504. cuddRef(res1);
  505. res2 = cuddAddMinAbstractRecur(manager, E, cuddT(cube));
  506. if (res2 == NULL) {
  507. Cudd_RecursiveDeref(manager,res1);
  508. return(NULL);
  509. }
  510. cuddRef(res2);
  511. res = cuddAddApplyRecur(manager, Cudd_addMinimum, res1, res2);
  512. if (res == NULL) {
  513. Cudd_RecursiveDeref(manager,res1);
  514. Cudd_RecursiveDeref(manager,res2);
  515. return(NULL);
  516. }
  517. cuddRef(res);
  518. Cudd_RecursiveDeref(manager,res1);
  519. Cudd_RecursiveDeref(manager,res2);
  520. cuddCacheInsert2(manager, Cudd_addMinAbstract, f, cube, res);
  521. cuddDeref(res);
  522. return(res);
  523. }
  524. else { /* if (cuddI(manager,f->index) < cuddI(manager,cube->index)) */
  525. res1 = cuddAddMinAbstractRecur(manager, T, cube);
  526. if (res1 == NULL) return(NULL);
  527. cuddRef(res1);
  528. res2 = cuddAddMinAbstractRecur(manager, E, cube);
  529. if (res2 == NULL) {
  530. Cudd_RecursiveDeref(manager,res1);
  531. return(NULL);
  532. }
  533. cuddRef(res2);
  534. res = (res1 == res2) ? res1 :
  535. cuddUniqueInter(manager, (int) f->index, res1, res2);
  536. if (res == NULL) {
  537. Cudd_RecursiveDeref(manager,res1);
  538. Cudd_RecursiveDeref(manager,res2);
  539. return(NULL);
  540. }
  541. cuddDeref(res1);
  542. cuddDeref(res2);
  543. cuddCacheInsert2(manager, Cudd_addMinAbstract, f, cube, res);
  544. return(res);
  545. }
  546. } /* end of cuddAddMinAbstractRecur */
  547. /**Function********************************************************************
  548. Synopsis [Performs the recursive step of Cudd_addMaxAbstract.]
  549. Description [Performs the recursive step of Cudd_addMaxAbstract.
  550. Returns the ADD obtained by abstracting the variables of cube from f,
  551. if successful; NULL otherwise.]
  552. SideEffects [None]
  553. SeeAlso []
  554. ******************************************************************************/
  555. DdNode *
  556. cuddAddMaxAbstractRecur(
  557. DdManager * manager,
  558. DdNode * f,
  559. DdNode * cube)
  560. {
  561. DdNode *T, *E, *res, *res1, *res2, *zero;
  562. zero = DD_ZERO(manager);
  563. /* Cube is guaranteed to be a cube at this point. */
  564. if (f == zero || cuddIsConstant(cube)) {
  565. return(f);
  566. }
  567. /* Abstract a variable that does not appear in f. */
  568. if (cuddI(manager,f->index) > cuddI(manager,cube->index)) {
  569. res = cuddAddMaxAbstractRecur(manager, f, cuddT(cube));
  570. return(res);
  571. }
  572. if ((res = cuddCacheLookup2(manager, Cudd_addMaxAbstract, f, cube)) != NULL) {
  573. return(res);
  574. }
  575. T = cuddT(f);
  576. E = cuddE(f);
  577. /* If the two indices are the same, so are their levels. */
  578. if (f->index == cube->index) {
  579. res1 = cuddAddMaxAbstractRecur(manager, T, cuddT(cube));
  580. if (res1 == NULL) return(NULL);
  581. cuddRef(res1);
  582. res2 = cuddAddMaxAbstractRecur(manager, E, cuddT(cube));
  583. if (res2 == NULL) {
  584. Cudd_RecursiveDeref(manager,res1);
  585. return(NULL);
  586. }
  587. cuddRef(res2);
  588. res = cuddAddApplyRecur(manager, Cudd_addMaximum, res1, res2);
  589. if (res == NULL) {
  590. Cudd_RecursiveDeref(manager,res1);
  591. Cudd_RecursiveDeref(manager,res2);
  592. return(NULL);
  593. }
  594. cuddRef(res);
  595. Cudd_RecursiveDeref(manager,res1);
  596. Cudd_RecursiveDeref(manager,res2);
  597. cuddCacheInsert2(manager, Cudd_addMaxAbstract, f, cube, res);
  598. cuddDeref(res);
  599. return(res);
  600. }
  601. else { /* if (cuddI(manager,f->index) < cuddI(manager,cube->index)) */
  602. res1 = cuddAddMaxAbstractRecur(manager, T, cube);
  603. if (res1 == NULL) return(NULL);
  604. cuddRef(res1);
  605. res2 = cuddAddMaxAbstractRecur(manager, E, cube);
  606. if (res2 == NULL) {
  607. Cudd_RecursiveDeref(manager,res1);
  608. return(NULL);
  609. }
  610. cuddRef(res2);
  611. res = (res1 == res2) ? res1 :
  612. cuddUniqueInter(manager, (int) f->index, res1, res2);
  613. if (res == NULL) {
  614. Cudd_RecursiveDeref(manager,res1);
  615. Cudd_RecursiveDeref(manager,res2);
  616. return(NULL);
  617. }
  618. cuddDeref(res1);
  619. cuddDeref(res2);
  620. cuddCacheInsert2(manager, Cudd_addMaxAbstract, f, cube, res);
  621. return(res);
  622. }
  623. } /* end of cuddAddMaxAbstractRecur */
  624. /*---------------------------------------------------------------------------*/
  625. /* Definition of static functions */
  626. /*---------------------------------------------------------------------------*/
  627. /**
  628. @brief Checks whether cube is an %ADD representing the product
  629. of positive literals.
  630. @return 1 in case of success; 0 otherwise.
  631. @sideeffect None
  632. */
  633. static int
  634. addCheckPositiveCube(
  635. DdManager * manager,
  636. DdNode * cube)
  637. {
  638. if (Cudd_IsComplement(cube)) return(0);
  639. if (cube == DD_ONE(manager)) return(1);
  640. if (cuddIsConstant(cube)) return(0);
  641. if (cuddE(cube) == DD_ZERO(manager)) {
  642. return(addCheckPositiveCube(manager, cuddT(cube)));
  643. }
  644. return(0);
  645. } /* end of addCheckPositiveCube */