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.

977 lines
26 KiB

  1. /**
  2. @file
  3. @ingroup cudd
  4. @brief Quantification functions for BDDs.
  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 bddCheckPositiveCube (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. @return the abstracted %BDD if successful; NULL otherwise.
  63. @sideeffect None
  64. @see Cudd_bddUnivAbstract Cudd_addExistAbstract
  65. */
  66. DdNode *
  67. Cudd_bddExistAbstract(
  68. DdManager * manager,
  69. DdNode * f,
  70. DdNode * cube)
  71. {
  72. DdNode *res;
  73. if (bddCheckPositiveCube(manager, cube) == 0) {
  74. (void) fprintf(manager->err,
  75. "Error: Can only abstract positive cubes\n");
  76. manager->errorCode = CUDD_INVALID_ARG;
  77. return(NULL);
  78. }
  79. do {
  80. manager->reordered = 0;
  81. res = cuddBddExistAbstractRecur(manager, f, cube);
  82. } while (manager->reordered == 1);
  83. if (manager->errorCode == CUDD_TIMEOUT_EXPIRED && manager->timeoutHandler) {
  84. manager->timeoutHandler(manager, manager->tohArg);
  85. }
  86. return(res);
  87. } /* end of Cudd_bddExistAbstract */
  88. /**Function********************************************************************
  89. Synopsis [Just like Cudd_bddExistAbstract, but instead of abstracting the
  90. variables in the given cube, picks a unique representative that realizes the
  91. existential truth value.]
  92. Description [Returns the resulting BDD if successful; NULL otherwise.]
  93. SideEffects [None]
  94. SeeAlso []
  95. Note: Added by Christian Dehnert 9/21/15
  96. ******************************************************************************/
  97. DdNode *
  98. Cudd_bddExistAbstractRepresentative(
  99. DdManager * manager,
  100. DdNode * f,
  101. DdNode * cube)
  102. {
  103. DdNode *res;
  104. if (bddCheckPositiveCube(manager, cube) == 0) {
  105. (void) fprintf(manager->err,"Error: Can only abstract positive cubes\n");
  106. manager->errorCode = CUDD_INVALID_ARG;
  107. return(NULL);
  108. }
  109. do {
  110. manager->reordered = 0;
  111. res = cuddBddExistAbstractRepresentativeRecur(manager, f, cube);
  112. } while (manager->reordered == 1);
  113. return(res);
  114. } /* end of Cudd_bddExistAbstractRepresentative */
  115. /**
  116. @brief Existentially abstracts all the variables in cube from f.
  117. @return the abstracted %BDD if successful; NULL if the intermediate
  118. result blows up or more new nodes than <code>limit</code> are
  119. required.
  120. @sideeffect None
  121. @see Cudd_bddExistAbstract
  122. */
  123. DdNode *
  124. Cudd_bddExistAbstractLimit(
  125. DdManager * manager,
  126. DdNode * f,
  127. DdNode * cube,
  128. unsigned int limit)
  129. {
  130. DdNode *res;
  131. unsigned int saveLimit = manager->maxLive;
  132. if (bddCheckPositiveCube(manager, cube) == 0) {
  133. (void) fprintf(manager->err,
  134. "Error: Can only abstract positive cubes\n");
  135. manager->errorCode = CUDD_INVALID_ARG;
  136. return(NULL);
  137. }
  138. manager->maxLive = (manager->keys - manager->dead) +
  139. (manager->keysZ - manager->deadZ) + limit;
  140. do {
  141. manager->reordered = 0;
  142. res = cuddBddExistAbstractRecur(manager, f, cube);
  143. } while (manager->reordered == 1);
  144. manager->maxLive = saveLimit;
  145. if (manager->errorCode == CUDD_TIMEOUT_EXPIRED && manager->timeoutHandler) {
  146. manager->timeoutHandler(manager, manager->tohArg);
  147. }
  148. return(res);
  149. } /* end of Cudd_bddExistAbstractLimit */
  150. /**
  151. @brief Takes the exclusive OR of two BDDs and simultaneously abstracts the
  152. variables in cube.
  153. @details The variables are existentially abstracted.
  154. @return a pointer to the result is successful; NULL otherwise.
  155. @sideeffect None
  156. @see Cudd_bddUnivAbstract Cudd_bddExistAbstract Cudd_bddAndAbstract
  157. */
  158. DdNode *
  159. Cudd_bddXorExistAbstract(
  160. DdManager * manager,
  161. DdNode * f,
  162. DdNode * g,
  163. DdNode * cube)
  164. {
  165. DdNode *res;
  166. if (bddCheckPositiveCube(manager, cube) == 0) {
  167. (void) fprintf(manager->err,
  168. "Error: Can only abstract positive cubes\n");
  169. manager->errorCode = CUDD_INVALID_ARG;
  170. return(NULL);
  171. }
  172. do {
  173. manager->reordered = 0;
  174. res = cuddBddXorExistAbstractRecur(manager, f, g, cube);
  175. } while (manager->reordered == 1);
  176. if (manager->errorCode == CUDD_TIMEOUT_EXPIRED && manager->timeoutHandler) {
  177. manager->timeoutHandler(manager, manager->tohArg);
  178. }
  179. return(res);
  180. } /* end of Cudd_bddXorExistAbstract */
  181. /**
  182. @brief Universally abstracts all the variables in cube from f.
  183. @return the abstracted %BDD if successful; NULL otherwise.
  184. @sideeffect None
  185. @see Cudd_bddExistAbstract Cudd_addUnivAbstract
  186. */
  187. DdNode *
  188. Cudd_bddUnivAbstract(
  189. DdManager * manager,
  190. DdNode * f,
  191. DdNode * cube)
  192. {
  193. DdNode *res;
  194. if (bddCheckPositiveCube(manager, cube) == 0) {
  195. (void) fprintf(manager->err,
  196. "Error: Can only abstract positive cubes\n");
  197. manager->errorCode = CUDD_INVALID_ARG;
  198. return(NULL);
  199. }
  200. do {
  201. manager->reordered = 0;
  202. res = cuddBddExistAbstractRecur(manager, Cudd_Not(f), cube);
  203. } while (manager->reordered == 1);
  204. if (res != NULL) res = Cudd_Not(res);
  205. if (manager->errorCode == CUDD_TIMEOUT_EXPIRED && manager->timeoutHandler) {
  206. manager->timeoutHandler(manager, manager->tohArg);
  207. }
  208. return(res);
  209. } /* end of Cudd_bddUnivAbstract */
  210. /**
  211. @brief Computes the boolean difference of f with respect to x.
  212. @details Computes the boolean difference of f with respect to the
  213. variable with index x.
  214. @return the %BDD of the boolean difference if successful; NULL
  215. otherwise.
  216. @sideeffect None
  217. */
  218. DdNode *
  219. Cudd_bddBooleanDiff(
  220. DdManager * manager,
  221. DdNode * f,
  222. int x)
  223. {
  224. DdNode *res, *var;
  225. /* If the variable is not currently in the manager, f cannot
  226. ** depend on it.
  227. */
  228. if (x >= manager->size) return(Cudd_Not(DD_ONE(manager)));
  229. var = manager->vars[x];
  230. do {
  231. manager->reordered = 0;
  232. res = cuddBddBooleanDiffRecur(manager, Cudd_Regular(f), var);
  233. } while (manager->reordered == 1);
  234. if (manager->errorCode == CUDD_TIMEOUT_EXPIRED && manager->timeoutHandler) {
  235. manager->timeoutHandler(manager, manager->tohArg);
  236. }
  237. return(res);
  238. } /* end of Cudd_bddBooleanDiff */
  239. /**
  240. @brief Checks whether a variable is dependent on others in a
  241. function.
  242. @details No new nodes are created.
  243. @return 1 if the variable is dependent; 0 otherwise.
  244. @sideeffect None
  245. */
  246. int
  247. Cudd_bddVarIsDependent(
  248. DdManager *dd, /**< manager */
  249. DdNode *f, /**< function */
  250. DdNode *var /**< variable */)
  251. {
  252. DdNode *F, *res, *zero, *ft, *fe;
  253. unsigned topf, level;
  254. DD_CTFP cacheOp;
  255. int retval;
  256. zero = Cudd_Not(DD_ONE(dd));
  257. F = Cudd_Regular(f);
  258. if (cuddIsConstant(F)) return(f == zero);
  259. /* From now on f is not constant. */
  260. topf = (unsigned) dd->perm[F->index];
  261. level = (unsigned) dd->perm[var->index];
  262. /* Check terminal case. If topf > index of var, f does not depend on var.
  263. ** Therefore, var is not dependent in f. */
  264. if (topf > level) {
  265. return(0);
  266. }
  267. cacheOp = (DD_CTFP) Cudd_bddVarIsDependent;
  268. res = cuddCacheLookup2(dd,cacheOp,f,var);
  269. if (res != NULL) {
  270. return(res != zero);
  271. }
  272. /* Compute cofactors. */
  273. ft = Cudd_NotCond(cuddT(F), f != F);
  274. fe = Cudd_NotCond(cuddE(F), f != F);
  275. if (topf == level) {
  276. retval = Cudd_bddLeq(dd,ft,Cudd_Not(fe));
  277. } else {
  278. retval = Cudd_bddVarIsDependent(dd,ft,var) &&
  279. Cudd_bddVarIsDependent(dd,fe,var);
  280. }
  281. cuddCacheInsert2(dd,cacheOp,f,var,Cudd_NotCond(zero,retval));
  282. return(retval);
  283. } /* Cudd_bddVarIsDependent */
  284. /*---------------------------------------------------------------------------*/
  285. /* Definition of internal functions */
  286. /*---------------------------------------------------------------------------*/
  287. /**
  288. @brief Performs the recursive steps of Cudd_bddExistAbstract.
  289. @details It is also used by Cudd_bddUnivAbstract.
  290. @return the %BDD obtained by abstracting the variables of cube from f
  291. if successful; NULL otherwise.
  292. @sideeffect None
  293. @see Cudd_bddExistAbstract Cudd_bddUnivAbstract
  294. */
  295. DdNode *
  296. cuddBddExistAbstractRecur(
  297. DdManager * manager,
  298. DdNode * f,
  299. DdNode * cube)
  300. {
  301. DdNode *F, *T, *E, *res, *res1, *res2, *one;
  302. statLine(manager);
  303. one = DD_ONE(manager);
  304. F = Cudd_Regular(f);
  305. /* Cube is guaranteed to be a cube at this point. */
  306. if (cube == one || F == one) {
  307. return(f);
  308. }
  309. /* From now on, f and cube are non-constant. */
  310. /* Abstract a variable that does not appear in f. */
  311. while (manager->perm[F->index] > manager->perm[cube->index]) {
  312. cube = cuddT(cube);
  313. if (cube == one) return(f);
  314. }
  315. /* Check the cache. */
  316. if (F->ref != 1 && (res = cuddCacheLookup2(manager, Cudd_bddExistAbstract, f, cube)) != NULL) {
  317. return(res);
  318. }
  319. checkWhetherToGiveUp(manager);
  320. /* Compute the cofactors of f. */
  321. T = cuddT(F); E = cuddE(F);
  322. if (f != F) {
  323. T = Cudd_Not(T); E = Cudd_Not(E);
  324. }
  325. /* If the two indices are the same, so are their levels. */
  326. if (F->index == cube->index) {
  327. if (T == one || E == one || T == Cudd_Not(E)) {
  328. return(one);
  329. }
  330. res1 = cuddBddExistAbstractRecur(manager, T, cuddT(cube));
  331. if (res1 == NULL) return(NULL);
  332. if (res1 == one) {
  333. if (F->ref != 1)
  334. cuddCacheInsert2(manager, Cudd_bddExistAbstract, f, cube, one);
  335. return(one);
  336. }
  337. cuddRef(res1);
  338. res2 = cuddBddExistAbstractRecur(manager, E, cuddT(cube));
  339. if (res2 == NULL) {
  340. Cudd_IterDerefBdd(manager,res1);
  341. return(NULL);
  342. }
  343. cuddRef(res2);
  344. res = cuddBddAndRecur(manager, Cudd_Not(res1), Cudd_Not(res2));
  345. if (res == NULL) {
  346. Cudd_IterDerefBdd(manager, res1);
  347. Cudd_IterDerefBdd(manager, res2);
  348. return(NULL);
  349. }
  350. res = Cudd_Not(res);
  351. cuddRef(res);
  352. Cudd_IterDerefBdd(manager, res1);
  353. Cudd_IterDerefBdd(manager, res2);
  354. if (F->ref != 1)
  355. cuddCacheInsert2(manager, Cudd_bddExistAbstract, f, cube, res);
  356. cuddDeref(res);
  357. return(res);
  358. } else { /* if (cuddI(manager,F->index) < cuddI(manager,cube->index)) */
  359. res1 = cuddBddExistAbstractRecur(manager, T, cube);
  360. if (res1 == NULL) return(NULL);
  361. cuddRef(res1);
  362. res2 = cuddBddExistAbstractRecur(manager, E, cube);
  363. if (res2 == NULL) {
  364. Cudd_IterDerefBdd(manager, res1);
  365. return(NULL);
  366. }
  367. cuddRef(res2);
  368. /* ITE takes care of possible complementation of res1 and of the
  369. ** case in which res1 == res2. */
  370. res = cuddBddIteRecur(manager, manager->vars[F->index], res1, res2);
  371. if (res == NULL) {
  372. Cudd_IterDerefBdd(manager, res1);
  373. Cudd_IterDerefBdd(manager, res2);
  374. return(NULL);
  375. }
  376. cuddDeref(res1);
  377. cuddDeref(res2);
  378. if (F->ref != 1)
  379. cuddCacheInsert2(manager, Cudd_bddExistAbstract, f, cube, res);
  380. return(res);
  381. }
  382. } /* end of cuddBddExistAbstractRecur */
  383. /**Function********************************************************************
  384. Synopsis [Performs the recursive steps of Cudd_bddExistAbstractRepresentative.]
  385. Description [Performs the recursive steps of Cudd_bddExistAbstractRepresentative.
  386. Returns the BDD obtained by picking a representative over the variables in
  387. the given cube for all other valuations. Returns the resulting BDD if successful;
  388. NULL otherwise.]
  389. SideEffects [None]
  390. SeeAlso []
  391. ******************************************************************************/
  392. DdNode *
  393. cuddBddExistAbstractRepresentativeRecur(
  394. DdManager * manager,
  395. DdNode * f,
  396. DdNode * cube)
  397. {
  398. DdNode *F, *T, *E, *res, *res1, *res2, *one, *zero, *left, *right, *tmp, *res1Inf, *res2Inf;
  399. statLine(manager);
  400. one = DD_ONE(manager);
  401. zero = Cudd_Not(one);
  402. F = Cudd_Regular(f);
  403. // Store whether f is negated.
  404. int fIsNegated = f != F;
  405. /* Cube is guaranteed to be a cube at this point. */
  406. if (F == one) {
  407. if (fIsNegated) {
  408. return f;
  409. }
  410. if (cube == one) {
  411. return one;
  412. } else {
  413. res = cuddBddExistAbstractRepresentativeRecur(manager, f, cuddT(cube));
  414. if (res == NULL) {
  415. return(NULL);
  416. }
  417. cuddRef(res);
  418. // res1 = cuddUniqueInter(manager, (int) cube->index, zero, res);
  419. // We now build in the necessary negation ourselves.
  420. res1 = cuddUniqueInter(manager, (int) cube->index, one, Cudd_Not(res));
  421. if (res1 == NULL) {
  422. Cudd_IterDerefBdd(manager,res);
  423. return(NULL);
  424. }
  425. res1 = Cudd_Not(res1);
  426. cuddDeref(res);
  427. return(res1);
  428. }
  429. } else if (cube == one) {
  430. return f;
  431. }
  432. /* From now on, cube and f are non-constant. */
  433. /* Check the cache. */
  434. if (F->ref != 1 && (res = cuddCacheLookup2(manager, Cudd_bddExistAbstractRepresentative, f, cube)) != NULL) {
  435. return(res);
  436. }
  437. /* Abstract a variable that does not appear in f. */
  438. if (manager->perm[F->index] > manager->perm[cube->index]) {
  439. res = cuddBddExistAbstractRepresentativeRecur(manager, f, cuddT(cube));
  440. if (res == NULL) {
  441. return(NULL);
  442. }
  443. cuddRef(res);
  444. // res1 = cuddUniqueInter(manager, (int) cube->index, zero, res);
  445. // We now build in the necessary negation ourselves.
  446. res1 = cuddUniqueInter(manager, (int) cube->index, one, Cudd_Not(res));
  447. if (res1 == NULL) {
  448. Cudd_IterDerefBdd(manager,res);
  449. return(NULL);
  450. }
  451. res1 = Cudd_Not(res1);
  452. cuddDeref(res);
  453. return(res1);
  454. }
  455. /* Compute the cofactors of f. */
  456. T = cuddT(F); E = cuddE(F);
  457. if (f != F) {
  458. T = Cudd_Not(T); E = Cudd_Not(E);
  459. }
  460. /* If the two indices are the same, so are their levels. */
  461. if (F->index == cube->index) {
  462. res1 = cuddBddExistAbstractRepresentativeRecur(manager, E, cuddT(cube));
  463. if (res1 == NULL) {
  464. return(NULL);
  465. }
  466. if (res1 == one) {
  467. if (F->ref != 1) {
  468. cuddCacheInsert2(manager, Cudd_bddExistAbstractRepresentative, f, cube, Cudd_Not(cube));
  469. }
  470. return(Cudd_Not(cube));
  471. }
  472. cuddRef(res1);
  473. res2 = cuddBddExistAbstractRepresentativeRecur(manager, T, cuddT(cube));
  474. if (res2 == NULL) {
  475. Cudd_IterDerefBdd(manager,res1);
  476. return(NULL);
  477. }
  478. cuddRef(res2);
  479. left = cuddBddExistAbstractRecur(manager, E, cuddT(cube));
  480. if (left == NULL) {
  481. Cudd_IterDerefBdd(manager, res1);
  482. Cudd_IterDerefBdd(manager, res2);
  483. return(NULL);
  484. }
  485. cuddRef(left);
  486. res1Inf = cuddBddIteRecur(manager, left, res1, zero);
  487. if (res1Inf == NULL) {
  488. Cudd_IterDerefBdd(manager,res1);
  489. Cudd_IterDerefBdd(manager,res2);
  490. Cudd_IterDerefBdd(manager,left);
  491. return(NULL);
  492. }
  493. cuddRef(res1Inf);
  494. Cudd_IterDerefBdd(manager,res1);
  495. res2Inf = cuddBddIteRecur(manager, left, zero, res2);
  496. if (res2Inf == NULL) {
  497. Cudd_IterDerefBdd(manager,res1);
  498. Cudd_IterDerefBdd(manager,res2);
  499. Cudd_IterDerefBdd(manager,left);
  500. Cudd_IterDerefBdd(manager,res1Inf);
  501. return(NULL);
  502. }
  503. cuddRef(res2Inf);
  504. Cudd_IterDerefBdd(manager,res2);
  505. Cudd_IterDerefBdd(manager,left);
  506. assert(res1Inf != res2Inf);
  507. int compl = Cudd_IsComplement(res2Inf);
  508. res = cuddUniqueInter(manager, (int) F->index, Cudd_Regular(res2Inf), compl ? Cudd_Not(res1Inf) : res1Inf);
  509. if (res == NULL) {
  510. Cudd_IterDerefBdd(manager,res1Inf);
  511. Cudd_IterDerefBdd(manager,res2Inf);
  512. return(NULL);
  513. }
  514. if (compl) {
  515. res = Cudd_Not(res);
  516. }
  517. cuddRef(res);
  518. cuddDeref(res1Inf);
  519. cuddDeref(res2Inf);
  520. cuddCacheInsert2(manager, Cudd_bddExistAbstractRepresentative, f, cube, res);
  521. cuddDeref(res);
  522. return(res);
  523. } else { /* if (cuddI(manager,F->index) < cuddI(manager,cube->index)) */
  524. res1 = cuddBddExistAbstractRepresentativeRecur(manager, E, cube);
  525. if (res1 == NULL){
  526. return(NULL);
  527. }
  528. cuddRef(res1);
  529. res2 = cuddBddExistAbstractRepresentativeRecur(manager, T, cube);
  530. if (res2 == NULL) {
  531. Cudd_IterDerefBdd(manager, res1);
  532. return(NULL);
  533. }
  534. cuddRef(res2);
  535. /* ITE takes care of possible complementation of res1 and of the
  536. ** case in which res1 == res2. */
  537. int compl = Cudd_IsComplement(res2);
  538. res = cuddUniqueInter(manager, (int)F->index, Cudd_Regular(res2), compl ? Cudd_Not(res1) : res1);
  539. if (res == NULL) {
  540. Cudd_IterDerefBdd(manager, res1);
  541. Cudd_IterDerefBdd(manager, res2);
  542. return(NULL);
  543. }
  544. if (compl) {
  545. res = Cudd_Not(res);
  546. }
  547. cuddDeref(res1);
  548. cuddDeref(res2);
  549. if (F->ref != 1) {
  550. cuddCacheInsert2(manager, Cudd_bddExistAbstractRepresentative, f, cube, res);
  551. }
  552. return(res);
  553. }
  554. } /* end of cuddBddExistAbstractRepresentativeRecur */
  555. /**
  556. @brief Takes the exclusive OR of two BDDs and simultaneously abstracts the
  557. variables in cube.
  558. @details The variables are existentially abstracted.
  559. @return a pointer to the result is successful; NULL otherwise.
  560. @sideeffect None
  561. @see Cudd_bddAndAbstract
  562. */
  563. DdNode *
  564. cuddBddXorExistAbstractRecur(
  565. DdManager * manager,
  566. DdNode * f,
  567. DdNode * g,
  568. DdNode * cube)
  569. {
  570. DdNode *F, *fv, *fnv, *G, *gv, *gnv;
  571. DdNode *one, *zero, *r, *t, *e, *Cube;
  572. int topf, topg, topcube, top;
  573. unsigned int index;
  574. statLine(manager);
  575. one = DD_ONE(manager);
  576. zero = Cudd_Not(one);
  577. /* Terminal cases. */
  578. if (f == g) {
  579. return(zero);
  580. }
  581. if (f == Cudd_Not(g)) {
  582. return(one);
  583. }
  584. if (cube == one) {
  585. return(cuddBddXorRecur(manager, f, g));
  586. }
  587. if (f == one) {
  588. return(cuddBddExistAbstractRecur(manager, Cudd_Not(g), cube));
  589. }
  590. if (g == one) {
  591. return(cuddBddExistAbstractRecur(manager, Cudd_Not(f), cube));
  592. }
  593. if (f == zero) {
  594. return(cuddBddExistAbstractRecur(manager, g, cube));
  595. }
  596. if (g == zero) {
  597. return(cuddBddExistAbstractRecur(manager, f, cube));
  598. }
  599. /* At this point f, g, and cube are not constant. */
  600. if (f > g) { /* Try to increase cache efficiency. */
  601. DdNode *tmp = f;
  602. f = g;
  603. g = tmp;
  604. }
  605. /* Check cache. */
  606. r = cuddCacheLookup(manager, DD_BDD_XOR_EXIST_ABSTRACT_TAG, f, g, cube);
  607. if (r != NULL) {
  608. return(r);
  609. }
  610. checkWhetherToGiveUp(manager);
  611. /* Here we can skip the use of cuddI, because the operands are known
  612. ** to be non-constant.
  613. */
  614. F = Cudd_Regular(f);
  615. topf = manager->perm[F->index];
  616. G = Cudd_Regular(g);
  617. topg = manager->perm[G->index];
  618. top = ddMin(topf, topg);
  619. topcube = manager->perm[cube->index];
  620. if (topcube < top) {
  621. return(cuddBddXorExistAbstractRecur(manager, f, g, cuddT(cube)));
  622. }
  623. /* Now, topcube >= top. */
  624. if (topf == top) {
  625. index = F->index;
  626. fv = cuddT(F);
  627. fnv = cuddE(F);
  628. if (Cudd_IsComplement(f)) {
  629. fv = Cudd_Not(fv);
  630. fnv = Cudd_Not(fnv);
  631. }
  632. } else {
  633. index = G->index;
  634. fv = fnv = f;
  635. }
  636. if (topg == top) {
  637. gv = cuddT(G);
  638. gnv = cuddE(G);
  639. if (Cudd_IsComplement(g)) {
  640. gv = Cudd_Not(gv);
  641. gnv = Cudd_Not(gnv);
  642. }
  643. } else {
  644. gv = gnv = g;
  645. }
  646. if (topcube == top) {
  647. Cube = cuddT(cube);
  648. } else {
  649. Cube = cube;
  650. }
  651. t = cuddBddXorExistAbstractRecur(manager, fv, gv, Cube);
  652. if (t == NULL) return(NULL);
  653. /* Special case: 1 OR anything = 1. Hence, no need to compute
  654. ** the else branch if t is 1.
  655. */
  656. if (t == one && topcube == top) {
  657. cuddCacheInsert(manager, DD_BDD_XOR_EXIST_ABSTRACT_TAG, f, g, cube, one);
  658. return(one);
  659. }
  660. cuddRef(t);
  661. e = cuddBddXorExistAbstractRecur(manager, fnv, gnv, Cube);
  662. if (e == NULL) {
  663. Cudd_IterDerefBdd(manager, t);
  664. return(NULL);
  665. }
  666. cuddRef(e);
  667. if (topcube == top) { /* abstract */
  668. r = cuddBddAndRecur(manager, Cudd_Not(t), Cudd_Not(e));
  669. if (r == NULL) {
  670. Cudd_IterDerefBdd(manager, t);
  671. Cudd_IterDerefBdd(manager, e);
  672. return(NULL);
  673. }
  674. r = Cudd_Not(r);
  675. cuddRef(r);
  676. Cudd_IterDerefBdd(manager, t);
  677. Cudd_IterDerefBdd(manager, e);
  678. cuddDeref(r);
  679. } else if (t == e) {
  680. r = t;
  681. cuddDeref(t);
  682. cuddDeref(e);
  683. } else {
  684. if (Cudd_IsComplement(t)) {
  685. r = cuddUniqueInter(manager,(int)index,Cudd_Not(t),Cudd_Not(e));
  686. if (r == NULL) {
  687. Cudd_IterDerefBdd(manager, t);
  688. Cudd_IterDerefBdd(manager, e);
  689. return(NULL);
  690. }
  691. r = Cudd_Not(r);
  692. } else {
  693. r = cuddUniqueInter(manager,(int)index,t,e);
  694. if (r == NULL) {
  695. Cudd_IterDerefBdd(manager, t);
  696. Cudd_IterDerefBdd(manager, e);
  697. return(NULL);
  698. }
  699. }
  700. cuddDeref(e);
  701. cuddDeref(t);
  702. }
  703. cuddCacheInsert(manager, DD_BDD_XOR_EXIST_ABSTRACT_TAG, f, g, cube, r);
  704. return (r);
  705. } /* end of cuddBddXorExistAbstractRecur */
  706. /**
  707. @brief Performs the recursive steps of Cudd_bddBoleanDiff.
  708. @details Exploits the fact that dF/dx = dF'/dx.
  709. @return the %BDD obtained by XORing the cofactors of f with respect
  710. to var if successful; NULL otherwise.
  711. @sideeffect None
  712. */
  713. DdNode *
  714. cuddBddBooleanDiffRecur(
  715. DdManager * manager,
  716. DdNode * f,
  717. DdNode * var)
  718. {
  719. DdNode *T, *E, *res, *res1, *res2;
  720. statLine(manager);
  721. if (cuddI(manager,f->index) > manager->perm[var->index]) {
  722. /* f does not depend on var. */
  723. return(Cudd_Not(DD_ONE(manager)));
  724. }
  725. /* From now on, f is non-constant. */
  726. /* If the two indices are the same, so are their levels. */
  727. if (f->index == var->index) {
  728. res = cuddBddXorRecur(manager, cuddT(f), cuddE(f));
  729. return(res);
  730. }
  731. /* From now on, cuddI(manager,f->index) < cuddI(manager,cube->index). */
  732. /* Check the cache. */
  733. res = cuddCacheLookup2(manager, cuddBddBooleanDiffRecur, f, var);
  734. if (res != NULL) {
  735. return(res);
  736. }
  737. /* Compute the cofactors of f. */
  738. T = cuddT(f); E = cuddE(f);
  739. res1 = cuddBddBooleanDiffRecur(manager, T, var);
  740. if (res1 == NULL) return(NULL);
  741. cuddRef(res1);
  742. res2 = cuddBddBooleanDiffRecur(manager, Cudd_Regular(E), var);
  743. if (res2 == NULL) {
  744. Cudd_IterDerefBdd(manager, res1);
  745. return(NULL);
  746. }
  747. cuddRef(res2);
  748. /* ITE takes care of possible complementation of res1 and of the
  749. ** case in which res1 == res2. */
  750. res = cuddBddIteRecur(manager, manager->vars[f->index], res1, res2);
  751. if (res == NULL) {
  752. Cudd_IterDerefBdd(manager, res1);
  753. Cudd_IterDerefBdd(manager, res2);
  754. return(NULL);
  755. }
  756. cuddDeref(res1);
  757. cuddDeref(res2);
  758. cuddCacheInsert2(manager, cuddBddBooleanDiffRecur, f, var, res);
  759. return(res);
  760. } /* end of cuddBddBooleanDiffRecur */
  761. /*---------------------------------------------------------------------------*/
  762. /* Definition of static functions */
  763. /*---------------------------------------------------------------------------*/
  764. /**
  765. @brief Checks whether cube is a %BDD representing the product of
  766. positive literals.
  767. @return 1 in case of success; 0 otherwise.
  768. @sideeffect None
  769. */
  770. static int
  771. bddCheckPositiveCube(
  772. DdManager * manager,
  773. DdNode * cube)
  774. {
  775. if (Cudd_IsComplement(cube)) return(0);
  776. if (cube == DD_ONE(manager)) return(1);
  777. if (cuddIsConstant(cube)) return(0);
  778. if (cuddE(cube) == Cudd_Not(DD_ONE(manager))) {
  779. return(bddCheckPositiveCube(manager, cuddT(cube)));
  780. }
  781. return(0);
  782. } /* end of bddCheckPositiveCube */