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.

1795 lines
46 KiB

  1. /**
  2. @file
  3. @ingroup cudd
  4. @brief Functions for the solution of satisfiability related problems.
  5. @author Seh-Woong Jeong, 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. #define DD_BIGGY 100000000
  40. /*---------------------------------------------------------------------------*/
  41. /* Stucture declarations */
  42. /*---------------------------------------------------------------------------*/
  43. /*---------------------------------------------------------------------------*/
  44. /* Type declarations */
  45. /*---------------------------------------------------------------------------*/
  46. /**
  47. @brief Type of item stored in memoization table.
  48. */
  49. typedef struct cuddPathPair {
  50. int pos;
  51. int neg;
  52. } cuddPathPair;
  53. /*---------------------------------------------------------------------------*/
  54. /* Variable declarations */
  55. /*---------------------------------------------------------------------------*/
  56. /*---------------------------------------------------------------------------*/
  57. /* Macro declarations */
  58. /*---------------------------------------------------------------------------*/
  59. #define WEIGHT(weight, col) ((weight) == NULL ? 1 : weight[col])
  60. /** \cond */
  61. /*---------------------------------------------------------------------------*/
  62. /* Static function prototypes */
  63. /*---------------------------------------------------------------------------*/
  64. static enum st_retval freePathPair (void *key, void *value, void *arg);
  65. static cuddPathPair getShortest (DdManager *dd, DdNode *root, int *cost, int *support, st_table *visited);
  66. static DdNode * getPath (DdManager *manager, st_table *visited, DdNode *f, int *weight, int cost);
  67. static cuddPathPair getLargest (DdManager *dd, DdNode *root, st_table *visited);
  68. static DdNode * getCube (DdManager *manager, st_table *visited, DdNode *f, int cost);
  69. static DdNode * ddBddMaximallyExpand(DdManager *dd, DdNode *lb, DdNode *ub, DdNode *f);
  70. static int ddBddShortestPathUnate(DdManager *dd, DdNode *f, int *phases, st_table *table);
  71. static DdNode * ddGetLargestCubeUnate(DdManager *dd, DdNode *f, int *phases, st_table *table);
  72. /** \endcond */
  73. /*---------------------------------------------------------------------------*/
  74. /* Definition of exported functions */
  75. /*---------------------------------------------------------------------------*/
  76. /**
  77. @brief Returns the value of a %DD for a given variable assignment.
  78. @details The variable assignment is passed in an array of int's,
  79. that should specify a zero or a one for each variable in the support
  80. of the function.
  81. @return a pointer to a constant node. No new nodes are produced.
  82. @sideeffect None
  83. @see Cudd_bddLeq Cudd_addEvalConst
  84. */
  85. DdNode *
  86. Cudd_Eval(
  87. DdManager * dd,
  88. DdNode * f,
  89. int * inputs)
  90. {
  91. int comple;
  92. DdNode *ptr;
  93. (void) dd; /* avoid warning */
  94. comple = Cudd_IsComplement(f);
  95. ptr = Cudd_Regular(f);
  96. while (!cuddIsConstant(ptr)) {
  97. if (inputs[ptr->index] == 1) {
  98. ptr = cuddT(ptr);
  99. } else {
  100. comple ^= Cudd_IsComplement(cuddE(ptr));
  101. ptr = Cudd_Regular(cuddE(ptr));
  102. }
  103. }
  104. return(Cudd_NotCond(ptr,comple));
  105. } /* end of Cudd_Eval */
  106. /**
  107. @brief Finds a shortest path in a %DD.
  108. @details f is the %DD we want to get the shortest path for;
  109. weight\[i\] is the weight of the THEN arc coming from the node whose
  110. index is i. If weight is NULL, then unit weights are assumed for all
  111. THEN arcs. All ELSE arcs have 0 weight. If non-NULL, both weight
  112. and support should point to arrays with at least as many entries as
  113. there are variables in the manager.
  114. @return the shortest path as the %BDD of a cube.
  115. @sideeffect support contains on return the true support of f.
  116. If support is NULL on entry, then Cudd_ShortestPath does not compute
  117. the true support info. length contains the length of the path.
  118. @see Cudd_ShortestLength Cudd_LargestCube
  119. */
  120. DdNode *
  121. Cudd_ShortestPath(
  122. DdManager * manager,
  123. DdNode * f,
  124. int * weight,
  125. int * support,
  126. int * length)
  127. {
  128. DdNode *F;
  129. st_table *visited;
  130. DdNode *sol;
  131. cuddPathPair *rootPair;
  132. int complement, cost;
  133. int i;
  134. DdNode *one = DD_ONE(manager);
  135. DdNode *zero = DD_ZERO(manager);
  136. /* Initialize support. Support does not depend on variable order.
  137. ** Hence, it does not need to be reinitialized if reordering occurs.
  138. */
  139. if (support) {
  140. for (i = 0; i < manager->size; i++) {
  141. support[i] = 0;
  142. }
  143. }
  144. if (f == Cudd_Not(one) || f == zero) {
  145. *length = DD_BIGGY;
  146. return(Cudd_Not(one));
  147. }
  148. /* From this point on, a path exists. */
  149. do {
  150. manager->reordered = 0;
  151. /* Initialize visited table. */
  152. visited = st_init_table(st_ptrcmp, st_ptrhash);
  153. /* Now get the length of the shortest path(s) from f to 1. */
  154. (void) getShortest(manager, f, weight, support, visited);
  155. complement = Cudd_IsComplement(f);
  156. F = Cudd_Regular(f);
  157. if (!st_lookup(visited, F, (void **) &rootPair)) return(NULL);
  158. if (complement) {
  159. cost = rootPair->neg;
  160. } else {
  161. cost = rootPair->pos;
  162. }
  163. /* Recover an actual shortest path. */
  164. sol = getPath(manager,visited,f,weight,cost);
  165. st_foreach(visited, freePathPair, NULL);
  166. st_free_table(visited);
  167. } while (manager->reordered == 1);
  168. if (manager->errorCode == CUDD_TIMEOUT_EXPIRED && manager->timeoutHandler) {
  169. manager->timeoutHandler(manager, manager->tohArg);
  170. }
  171. *length = cost;
  172. return(sol);
  173. } /* end of Cudd_ShortestPath */
  174. /**
  175. @brief Finds a largest cube in a %DD.
  176. @details f is the %DD we want to get the largest cube for. The
  177. problem is translated into the one of finding a shortest path in f,
  178. when both THEN and ELSE arcs are assumed to have unit length. This
  179. yields a largest cube in the disjoint cover corresponding to the
  180. %DD. Therefore, it is not necessarily the largest implicant of f.
  181. @return the largest cube as a %BDD.
  182. @sideeffect The number of literals of the cube is returned in the location
  183. pointed by length if it is non-null.
  184. @see Cudd_ShortestPath
  185. */
  186. DdNode *
  187. Cudd_LargestCube(
  188. DdManager * manager,
  189. DdNode * f,
  190. int * length)
  191. {
  192. DdNode *F;
  193. st_table *visited;
  194. DdNode *sol;
  195. cuddPathPair *rootPair;
  196. int complement, cost;
  197. DdNode *one = DD_ONE(manager);
  198. DdNode *zero = DD_ZERO(manager);
  199. if (f == Cudd_Not(one) || f == zero) {
  200. if (length != NULL) {
  201. *length = DD_BIGGY;
  202. }
  203. return(Cudd_Not(one));
  204. }
  205. /* From this point on, a path exists. */
  206. do {
  207. manager->reordered = 0;
  208. /* Initialize visited table. */
  209. visited = st_init_table(st_ptrcmp, st_ptrhash);
  210. /* Now get the length of the shortest path(s) from f to 1. */
  211. (void) getLargest(manager, f, visited);
  212. complement = Cudd_IsComplement(f);
  213. F = Cudd_Regular(f);
  214. if (!st_lookup(visited, F, (void **) &rootPair)) return(NULL);
  215. if (complement) {
  216. cost = rootPair->neg;
  217. } else {
  218. cost = rootPair->pos;
  219. }
  220. /* Recover an actual shortest path. */
  221. sol = getCube(manager,visited,f,cost);
  222. st_foreach(visited, freePathPair, NULL);
  223. st_free_table(visited);
  224. } while (manager->reordered == 1);
  225. if (length != NULL) {
  226. *length = cost;
  227. }
  228. if (manager->errorCode == CUDD_TIMEOUT_EXPIRED && manager->timeoutHandler) {
  229. manager->timeoutHandler(manager, manager->tohArg);
  230. }
  231. return(sol);
  232. } /* end of Cudd_LargestCube */
  233. /**
  234. @brief Find the length of the shortest path(s) in a %DD.
  235. @details f is the %DD we want to get the shortest path for;
  236. weight\[i\] is the weight of the THEN edge coming from the node
  237. whose index is i. All ELSE edges have 0 weight.
  238. @return the length of the shortest path(s) if such a path is found;
  239. a large number if the function is identically 0, and CUDD_OUT_OF_MEM
  240. in case of failure.
  241. @sideeffect None
  242. @see Cudd_ShortestPath
  243. */
  244. int
  245. Cudd_ShortestLength(
  246. DdManager * manager,
  247. DdNode * f,
  248. int * weight)
  249. {
  250. DdNode *F;
  251. st_table *visited;
  252. cuddPathPair *my_pair;
  253. int complement, cost;
  254. DdNode *one = DD_ONE(manager);
  255. DdNode *zero = DD_ZERO(manager);
  256. if (f == Cudd_Not(one) || f == zero) {
  257. return(DD_BIGGY);
  258. }
  259. /* From this point on, a path exists. */
  260. /* Initialize visited table and support. */
  261. visited = st_init_table(st_ptrcmp, st_ptrhash);
  262. /* Now get the length of the shortest path(s) from f to 1. */
  263. (void) getShortest(manager, f, weight, NULL, visited);
  264. complement = Cudd_IsComplement(f);
  265. F = Cudd_Regular(f);
  266. if (!st_lookup(visited, F, (void **) &my_pair)) return(CUDD_OUT_OF_MEM);
  267. if (complement) {
  268. cost = my_pair->neg;
  269. } else {
  270. cost = my_pair->pos;
  271. }
  272. st_foreach(visited, freePathPair, NULL);
  273. st_free_table(visited);
  274. return(cost);
  275. } /* end of Cudd_ShortestLength */
  276. /**
  277. @brief Checks whether a %BDD is negative unate in a
  278. variable.
  279. @details Determines whether the function represented by %BDD f is
  280. negative unate (monotonic decreasing) in variable i. This function
  281. does not generate any new nodes.
  282. @return the constant one is f is unate and the (logical) constant
  283. zero if it is not.
  284. @sideeffect None
  285. @see Cudd_Increasing
  286. */
  287. DdNode *
  288. Cudd_Decreasing(
  289. DdManager * dd,
  290. DdNode * f,
  291. int i)
  292. {
  293. int topf, level;
  294. DdNode *F, *fv, *fvn, *res;
  295. DD_CTFP cacheOp;
  296. statLine(dd);
  297. #ifdef DD_DEBUG
  298. assert(0 <= i && i < dd->size);
  299. #endif
  300. F = Cudd_Regular(f);
  301. topf = cuddI(dd,F->index);
  302. /* Check terminal case. If topf > i, f does not depend on var.
  303. ** Therefore, f is unate in i.
  304. */
  305. level = (unsigned) dd->perm[i];
  306. if (topf > level) {
  307. return(DD_ONE(dd));
  308. }
  309. /* From now on, f is not constant. */
  310. /* Check cache. */
  311. cacheOp = (DD_CTFP) Cudd_Decreasing;
  312. res = cuddCacheLookup2(dd,cacheOp,f,dd->vars[i]);
  313. if (res != NULL) {
  314. return(res);
  315. }
  316. checkWhetherToGiveUp(dd);
  317. /* Compute cofactors. */
  318. fv = cuddT(F); fvn = cuddE(F);
  319. if (F != f) {
  320. fv = Cudd_Not(fv);
  321. fvn = Cudd_Not(fvn);
  322. }
  323. if (topf == level) {
  324. /* Special case: if fv is regular, fv(1,...,1) = 1;
  325. ** If in addition fvn is complemented, fvn(1,...,1) = 0.
  326. ** But then f(1,1,...,1) > f(0,1,...,1). Hence f is not
  327. ** monotonic decreasing in i.
  328. */
  329. if (!Cudd_IsComplement(fv) && Cudd_IsComplement(fvn)) {
  330. return(Cudd_Not(DD_ONE(dd)));
  331. }
  332. res = Cudd_bddLeq(dd,fv,fvn) ? DD_ONE(dd) : Cudd_Not(DD_ONE(dd));
  333. } else {
  334. res = Cudd_Decreasing(dd,fv,i);
  335. if (res == DD_ONE(dd)) {
  336. res = Cudd_Decreasing(dd,fvn,i);
  337. }
  338. }
  339. cuddCacheInsert2(dd,cacheOp,f,dd->vars[i],res);
  340. return(res);
  341. } /* end of Cudd_Decreasing */
  342. /**
  343. @brief Checks whether a %BDD is positive unate in a variable.
  344. @details Determines whether the function represented by %BDD f is
  345. positive unate (monotonic increasing) in variable i. It is based on
  346. Cudd_Decreasing and the fact that f is monotonic increasing in i if
  347. and only if its complement is monotonic decreasing in i.
  348. @sideeffect None
  349. @see Cudd_Decreasing
  350. */
  351. DdNode *
  352. Cudd_Increasing(
  353. DdManager * dd,
  354. DdNode * f,
  355. int i)
  356. {
  357. return(Cudd_Decreasing(dd,Cudd_Not(f),i));
  358. } /* end of Cudd_Increasing */
  359. /**
  360. @brief Tells whether F and G are identical wherever D is 0.
  361. @details F and G are either two ADDs or two BDDs. D is either a 0-1
  362. %ADD or a %BDD. No new nodes are created.
  363. @return 1 if F and G are equivalent, and 0 otherwise.
  364. @sideeffect None
  365. @see Cudd_bddLeqUnless
  366. */
  367. int
  368. Cudd_EquivDC(
  369. DdManager * dd,
  370. DdNode * F,
  371. DdNode * G,
  372. DdNode * D)
  373. {
  374. DdNode *tmp, *One, *Gr, *Dr;
  375. DdNode *Fv, *Fvn, *Gv, *Gvn, *Dv, *Dvn;
  376. int res;
  377. int flevel, glevel, dlevel, top;
  378. One = DD_ONE(dd);
  379. statLine(dd);
  380. /* Check terminal cases. */
  381. if (D == One || F == G) return(1);
  382. if (D == Cudd_Not(One) || D == DD_ZERO(dd) || F == Cudd_Not(G)) return(0);
  383. /* From now on, D is non-constant. */
  384. /* Normalize call to increase cache efficiency. */
  385. if (F > G) {
  386. tmp = F;
  387. F = G;
  388. G = tmp;
  389. }
  390. if (Cudd_IsComplement(F)) {
  391. F = Cudd_Not(F);
  392. G = Cudd_Not(G);
  393. }
  394. /* From now on, F is regular. */
  395. /* Check cache. */
  396. tmp = cuddCacheLookup(dd,DD_EQUIV_DC_TAG,F,G,D);
  397. if (tmp != NULL) return(tmp == One);
  398. /* Find splitting variable. */
  399. flevel = cuddI(dd,F->index);
  400. Gr = Cudd_Regular(G);
  401. glevel = cuddI(dd,Gr->index);
  402. top = ddMin(flevel,glevel);
  403. Dr = Cudd_Regular(D);
  404. dlevel = dd->perm[Dr->index];
  405. top = ddMin(top,dlevel);
  406. /* Compute cofactors. */
  407. if (top == flevel) {
  408. Fv = cuddT(F);
  409. Fvn = cuddE(F);
  410. } else {
  411. Fv = Fvn = F;
  412. }
  413. if (top == glevel) {
  414. Gv = cuddT(Gr);
  415. Gvn = cuddE(Gr);
  416. if (G != Gr) {
  417. Gv = Cudd_Not(Gv);
  418. Gvn = Cudd_Not(Gvn);
  419. }
  420. } else {
  421. Gv = Gvn = G;
  422. }
  423. if (top == dlevel) {
  424. Dv = cuddT(Dr);
  425. Dvn = cuddE(Dr);
  426. if (D != Dr) {
  427. Dv = Cudd_Not(Dv);
  428. Dvn = Cudd_Not(Dvn);
  429. }
  430. } else {
  431. Dv = Dvn = D;
  432. }
  433. /* Solve recursively. */
  434. res = Cudd_EquivDC(dd,Fv,Gv,Dv);
  435. if (res != 0) {
  436. res = Cudd_EquivDC(dd,Fvn,Gvn,Dvn);
  437. }
  438. cuddCacheInsert(dd,DD_EQUIV_DC_TAG,F,G,D,(res) ? One : Cudd_Not(One));
  439. return(res);
  440. } /* end of Cudd_EquivDC */
  441. /**
  442. @brief Tells whether f is less than of equal to G unless D is 1.
  443. @details f, g, and D are BDDs. No new nodes are created.
  444. @return 1 if f is less than of equal to G, and 0 otherwise.
  445. @sideeffect None
  446. @see Cudd_EquivDC Cudd_bddLeq Cudd_bddIteConstant
  447. */
  448. int
  449. Cudd_bddLeqUnless(
  450. DdManager *dd,
  451. DdNode *f,
  452. DdNode *g,
  453. DdNode *D)
  454. {
  455. DdNode *tmp, *One, *F, *G;
  456. DdNode *Ft, *Fe, *Gt, *Ge, *Dt, *De;
  457. int res;
  458. int flevel, glevel, dlevel, top;
  459. statLine(dd);
  460. One = DD_ONE(dd);
  461. /* Check terminal cases. */
  462. if (f == g || g == One || f == Cudd_Not(One) || D == One ||
  463. D == f || D == Cudd_Not(g)) return(1);
  464. /* Check for two-operand cases. */
  465. if (D == Cudd_Not(One) || D == g || D == Cudd_Not(f))
  466. return(Cudd_bddLeq(dd,f,g));
  467. if (g == Cudd_Not(One) || g == Cudd_Not(f)) return(Cudd_bddLeq(dd,f,D));
  468. if (f == One) return(Cudd_bddLeq(dd,Cudd_Not(g),D));
  469. /* From now on, f, g, and D are non-constant, distinct, and
  470. ** non-complementary. */
  471. /* Normalize call to increase cache efficiency. We rely on the
  472. ** fact that f <= g unless D is equivalent to not(g) <= not(f)
  473. ** unless D and to f <= D unless g. We make sure that D is
  474. ** regular, and that at most one of f and g is complemented. We also
  475. ** ensure that when two operands can be swapped, the one with the
  476. ** lowest address comes first. */
  477. if (Cudd_IsComplement(D)) {
  478. if (Cudd_IsComplement(g)) {
  479. /* Special case: if f is regular and g is complemented,
  480. ** f(1,...,1) = 1 > 0 = g(1,...,1). If D(1,...,1) = 0, return 0.
  481. */
  482. if (!Cudd_IsComplement(f)) return(0);
  483. /* !g <= D unless !f or !D <= g unless !f */
  484. tmp = D;
  485. D = Cudd_Not(f);
  486. if (g < tmp) {
  487. f = Cudd_Not(g);
  488. g = tmp;
  489. } else {
  490. f = Cudd_Not(tmp);
  491. }
  492. } else {
  493. if (Cudd_IsComplement(f)) {
  494. /* !D <= !f unless g or !D <= g unless !f */
  495. tmp = f;
  496. f = Cudd_Not(D);
  497. if (tmp < g) {
  498. D = g;
  499. g = Cudd_Not(tmp);
  500. } else {
  501. D = Cudd_Not(tmp);
  502. }
  503. } else {
  504. /* f <= D unless g or !D <= !f unless g */
  505. tmp = D;
  506. D = g;
  507. if (tmp < f) {
  508. g = Cudd_Not(f);
  509. f = Cudd_Not(tmp);
  510. } else {
  511. g = tmp;
  512. }
  513. }
  514. }
  515. } else {
  516. if (Cudd_IsComplement(g)) {
  517. if (Cudd_IsComplement(f)) {
  518. /* !g <= !f unless D or !g <= D unless !f */
  519. tmp = f;
  520. f = Cudd_Not(g);
  521. if (D < tmp) {
  522. g = D;
  523. D = Cudd_Not(tmp);
  524. } else {
  525. g = Cudd_Not(tmp);
  526. }
  527. } else {
  528. /* f <= g unless D or !g <= !f unless D */
  529. if (g < f) {
  530. tmp = g;
  531. g = Cudd_Not(f);
  532. f = Cudd_Not(tmp);
  533. }
  534. }
  535. } else {
  536. /* f <= g unless D or f <= D unless g */
  537. if (D < g) {
  538. tmp = D;
  539. D = g;
  540. g = tmp;
  541. }
  542. }
  543. }
  544. /* From now on, D is regular. */
  545. /* Check cache. */
  546. tmp = cuddCacheLookup(dd,DD_BDD_LEQ_UNLESS_TAG,f,g,D);
  547. if (tmp != NULL) return(tmp == One);
  548. /* Find splitting variable. */
  549. F = Cudd_Regular(f);
  550. flevel = dd->perm[F->index];
  551. G = Cudd_Regular(g);
  552. glevel = dd->perm[G->index];
  553. top = ddMin(flevel,glevel);
  554. dlevel = dd->perm[D->index];
  555. top = ddMin(top,dlevel);
  556. /* Compute cofactors. */
  557. if (top == flevel) {
  558. Ft = cuddT(F);
  559. Fe = cuddE(F);
  560. if (F != f) {
  561. Ft = Cudd_Not(Ft);
  562. Fe = Cudd_Not(Fe);
  563. }
  564. } else {
  565. Ft = Fe = f;
  566. }
  567. if (top == glevel) {
  568. Gt = cuddT(G);
  569. Ge = cuddE(G);
  570. if (G != g) {
  571. Gt = Cudd_Not(Gt);
  572. Ge = Cudd_Not(Ge);
  573. }
  574. } else {
  575. Gt = Ge = g;
  576. }
  577. if (top == dlevel) {
  578. Dt = cuddT(D);
  579. De = cuddE(D);
  580. } else {
  581. Dt = De = D;
  582. }
  583. /* Solve recursively. */
  584. res = Cudd_bddLeqUnless(dd,Ft,Gt,Dt);
  585. if (res != 0) {
  586. res = Cudd_bddLeqUnless(dd,Fe,Ge,De);
  587. }
  588. cuddCacheInsert(dd,DD_BDD_LEQ_UNLESS_TAG,f,g,D,Cudd_NotCond(One,!res));
  589. return(res);
  590. } /* end of Cudd_bddLeqUnless */
  591. /**
  592. @brief Compares two ADDs for equality within tolerance.
  593. @details Two ADDs are reported to be equal if the maximum difference
  594. between them (the sup norm of their difference) is less than or
  595. equal to the tolerance parameter. If parameter <code>pr</code> is
  596. positive the first failure is reported to the standard output.
  597. @return 1 if the two ADDs are equal (within tolerance); 0 otherwise.
  598. @sideeffect None
  599. */
  600. int
  601. Cudd_EqualSupNorm(
  602. DdManager * dd /**< manager */,
  603. DdNode * f /**< first %ADD */,
  604. DdNode * g /**< second %ADD */,
  605. CUDD_VALUE_TYPE tolerance /**< maximum allowed difference */,
  606. int pr /**< verbosity level */)
  607. {
  608. DdNode *fv, *fvn, *gv, *gvn, *r;
  609. int topf, topg;
  610. statLine(dd);
  611. /* Check terminal cases. */
  612. if (f == g) return(1);
  613. if (cuddIsConstant(f) && cuddIsConstant(g)) {
  614. if (ddEqualVal(cuddV(f),cuddV(g),tolerance)) {
  615. return(1);
  616. } else {
  617. if (pr>0) {
  618. (void) fprintf(dd->out,"Offending nodes:\n");
  619. (void) fprintf(dd->out,
  620. "f: address = %p\t value = %40.30f\n",
  621. (void *) f, cuddV(f));
  622. (void) fprintf(dd->out,
  623. "g: address = %p\t value = %40.30f\n",
  624. (void *) g, cuddV(g));
  625. }
  626. return(0);
  627. }
  628. }
  629. /* We only insert the result in the cache if the comparison is
  630. ** successful. Therefore, if we hit we return 1. */
  631. r = cuddCacheLookup2(dd,(DD_CTFP)Cudd_EqualSupNorm,f,g);
  632. if (r != NULL) {
  633. return(1);
  634. }
  635. /* Compute the cofactors and solve the recursive subproblems. */
  636. topf = cuddI(dd,f->index);
  637. topg = cuddI(dd,g->index);
  638. if (topf <= topg) {fv = cuddT(f); fvn = cuddE(f);} else {fv = fvn = f;}
  639. if (topg <= topf) {gv = cuddT(g); gvn = cuddE(g);} else {gv = gvn = g;}
  640. if (!Cudd_EqualSupNorm(dd,fv,gv,tolerance,pr)) return(0);
  641. if (!Cudd_EqualSupNorm(dd,fvn,gvn,tolerance,pr)) return(0);
  642. cuddCacheInsert2(dd,(DD_CTFP)Cudd_EqualSupNorm,f,g,DD_ONE(dd));
  643. return(1);
  644. } /* end of Cudd_EqualSupNorm */
  645. /**Function********************************************************************
  646. Synopsis [Compares two ADDs for equality within tolerance.]
  647. Description [Same as Cudd_EqualSupNorm but tests for max _relative_ difference
  648. i.e. (f-g/f)<e instead of (f-g)<e ]
  649. SideEffects [None]
  650. SeeAlso []
  651. ******************************************************************************/
  652. int
  653. Cudd_EqualSupNormRel(
  654. DdManager * dd /* manager */,
  655. DdNode * f /* first ADD */,
  656. DdNode * g /* second ADD */,
  657. CUDD_VALUE_TYPE tolerance /* maximum allowed difference */,
  658. int pr /* verbosity level */)
  659. {
  660. DdNode *fv, *fvn, *gv, *gvn, *r;
  661. unsigned int topf, topg;
  662. statLine(dd);
  663. /* Check terminal cases. */
  664. if (f == g) return(1);
  665. if (Cudd_IsConstant(f) && Cudd_IsConstant(g)) {
  666. if (ddAbs((cuddV(f) - cuddV(g))/cuddV(f)) < tolerance) {
  667. return(1);
  668. } else {
  669. if (pr>0) {
  670. (void) fprintf(dd->out,"Offending nodes:\n");
  671. (void) fprintf(dd->out,
  672. "f: address = %p\t value = %40.30f\n",
  673. (void *) f, cuddV(f));
  674. (void) fprintf(dd->out,
  675. "g: address = %p\t value = %40.30f\n",
  676. (void *) g, cuddV(g));
  677. }
  678. return(0);
  679. }
  680. }
  681. /* We only insert the result in the cache if the comparison is
  682. ** successful. Therefore, if we hit we return 1. */
  683. r = cuddCacheLookup2(dd,(DD_CTFP)Cudd_EqualSupNormRel,f,g);
  684. if (r != NULL) {
  685. return(1);
  686. }
  687. /* Compute the cofactors and solve the recursive subproblems. */
  688. topf = cuddI(dd,f->index);
  689. topg = cuddI(dd,g->index);
  690. if (topf <= topg) {fv = cuddT(f); fvn = cuddE(f);} else {fv = fvn = f;}
  691. if (topg <= topf) {gv = cuddT(g); gvn = cuddE(g);} else {gv = gvn = g;}
  692. if (!Cudd_EqualSupNormRel(dd,fv,gv,tolerance,pr)) return(0);
  693. if (!Cudd_EqualSupNormRel(dd,fvn,gvn,tolerance,pr)) return(0);
  694. cuddCacheInsert2(dd,(DD_CTFP)Cudd_EqualSupNormRel,f,g,DD_ONE(dd));
  695. return(1);
  696. } /* end of Cudd_EqualSupNormRel */
  697. /**
  698. @brief Expands cube to a prime implicant of f.
  699. @return the prime if successful; NULL otherwise. In particular,
  700. NULL is returned if cube is not a real cube or is not an implicant
  701. of f.
  702. @sideeffect None
  703. @see Cudd_bddMaximallyExpand
  704. */
  705. DdNode *
  706. Cudd_bddMakePrime(
  707. DdManager *dd /**< manager */,
  708. DdNode *cube /**< cube to be expanded */,
  709. DdNode *f /**< function of which the cube is to be made a prime */)
  710. {
  711. DdNode *res;
  712. if (!Cudd_bddLeq(dd,cube,f)) return(NULL);
  713. do {
  714. dd->reordered = 0;
  715. res = cuddBddMakePrime(dd,cube,f);
  716. } while (dd->reordered == 1);
  717. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  718. dd->timeoutHandler(dd, dd->tohArg);
  719. }
  720. return(res);
  721. } /* end of Cudd_bddMakePrime */
  722. /**
  723. @brief Expands lb to prime implicants of (f and ub).
  724. @details Expands lb to all prime implicants of (f and ub) that
  725. contain lb. Assumes that lb is contained in ub.
  726. @return the disjunction of the primes if lb is contained in f;
  727. returns the zero %BDD if lb is not contained in f; returns NULL in
  728. case of failure. In particular, NULL is returned if cube is not a
  729. real cube or is not an implicant of f. Returning the disjunction of
  730. all prime implicants works because the resulting function is unate.
  731. @sideeffect None
  732. @see Cudd_bddMakePrime
  733. */
  734. DdNode *
  735. Cudd_bddMaximallyExpand(
  736. DdManager *dd /**< manager */,
  737. DdNode *lb /**< cube to be expanded */,
  738. DdNode *ub /**< upper bound cube */,
  739. DdNode *f /**< function against which to expand */)
  740. {
  741. DdNode *res;
  742. if (!Cudd_bddLeq(dd,lb,ub)) return(NULL);
  743. do {
  744. dd->reordered = 0;
  745. res = ddBddMaximallyExpand(dd,lb,ub,f);
  746. } while (dd->reordered == 1);
  747. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  748. dd->timeoutHandler(dd, dd->tohArg);
  749. }
  750. return(res);
  751. } /* end of Cudd_bddMaximallyExpand */
  752. /**
  753. @brief Find a largest prime implicant of a unate function.
  754. @details The behavior is undefined if f is not unate. The third
  755. argument is used to determine whether f is unate positive
  756. (increasing) or negative (decreasing) in each of the variables in
  757. its support.
  758. @return the %BDD for the prime if succesful; NULL otherwise.
  759. @sideeffect None
  760. @see Cudd_bddMaximallyExpand
  761. */
  762. DdNode *
  763. Cudd_bddLargestPrimeUnate(
  764. DdManager *dd /**< manager */,
  765. DdNode *f /**< unate function */,
  766. DdNode *phaseBdd /**< cube of the phases */)
  767. {
  768. DdNode *res;
  769. int *phases;
  770. int retval;
  771. st_table *table;
  772. /* Extract phase vector for quick access. */
  773. phases = ALLOC(int, dd->size);
  774. if (phases == NULL) return(NULL);
  775. retval = Cudd_BddToCubeArray(dd, phaseBdd, phases);
  776. if (retval == 0) {
  777. FREE(phases);
  778. return(NULL);
  779. }
  780. do {
  781. dd->reordered = 0;
  782. table = st_init_table(st_ptrcmp,st_ptrhash);
  783. if (table == NULL) {
  784. FREE(phases);
  785. return(NULL);
  786. }
  787. (void) ddBddShortestPathUnate(dd, f, phases, table);
  788. res = ddGetLargestCubeUnate(dd, f, phases, table);
  789. st_free_table(table);
  790. } while (dd->reordered == 1);
  791. FREE(phases);
  792. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  793. dd->timeoutHandler(dd, dd->tohArg);
  794. }
  795. return(res);
  796. } /* end of Cudd_bddLargestPrimeUnate */
  797. /*---------------------------------------------------------------------------*/
  798. /* Definition of internal functions */
  799. /*---------------------------------------------------------------------------*/
  800. /**
  801. @brief Performs the recursive step of Cudd_bddMakePrime.
  802. @return the prime if successful; NULL otherwise.
  803. @sideeffect None
  804. */
  805. DdNode *
  806. cuddBddMakePrime(
  807. DdManager *dd /**< manager */,
  808. DdNode *cube /**< cube to be expanded */,
  809. DdNode *f /**< function of which the cube is to be made a prime */)
  810. {
  811. DdNode *scan;
  812. DdNode *t, *e;
  813. DdNode *res = cube;
  814. DdNode *lzero = Cudd_Not(DD_ONE(dd));
  815. Cudd_Ref(res);
  816. scan = cube;
  817. while (!Cudd_IsConstantInt(scan)) {
  818. DdNode *reg = Cudd_Regular(scan);
  819. DdNode *var = dd->vars[reg->index];
  820. DdNode *expanded = Cudd_bddExistAbstract(dd,res,var);
  821. if (expanded == NULL) {
  822. Cudd_RecursiveDeref(dd,res);
  823. return(NULL);
  824. }
  825. Cudd_Ref(expanded);
  826. if (Cudd_bddLeq(dd,expanded,f)) {
  827. Cudd_RecursiveDeref(dd,res);
  828. res = expanded;
  829. } else {
  830. Cudd_RecursiveDeref(dd,expanded);
  831. }
  832. cuddGetBranches(scan,&t,&e);
  833. if (t == lzero) {
  834. scan = e;
  835. } else if (e == lzero) {
  836. scan = t;
  837. } else {
  838. Cudd_RecursiveDeref(dd,res);
  839. return(NULL); /* cube is not a cube */
  840. }
  841. }
  842. if (scan == DD_ONE(dd)) {
  843. Cudd_Deref(res);
  844. return(res);
  845. } else {
  846. Cudd_RecursiveDeref(dd,res);
  847. return(NULL);
  848. }
  849. } /* end of cuddBddMakePrime */
  850. /*---------------------------------------------------------------------------*/
  851. /* Definition of static functions */
  852. /*---------------------------------------------------------------------------*/
  853. /**
  854. @brief Frees the entries of the visited symbol table.
  855. @return ST_CONTINUE.
  856. @sideeffect None
  857. */
  858. static enum st_retval
  859. freePathPair(
  860. void * key,
  861. void * value,
  862. void * arg)
  863. {
  864. cuddPathPair *pair = (cuddPathPair *) value;
  865. (void) key; /* avoid warning */
  866. (void) arg; /* avoid warning */
  867. FREE(pair);
  868. return(ST_CONTINUE);
  869. } /* end of freePathPair */
  870. /**
  871. @brief Finds the length of the shortest path(s) in a %DD.
  872. @details Uses a local symbol table to store the lengths for each
  873. node. Only the lengths for the regular nodes are entered in the
  874. table, because those for the complement nodes are simply obtained by
  875. swapping the two lenghts.
  876. @return a pair of lengths: the length of the shortest path to 1; and
  877. the length of the shortest path to 0. This is done so as to take
  878. complement arcs into account.
  879. @sideeffect Accumulates the support of the %DD in support.
  880. */
  881. static cuddPathPair
  882. getShortest(
  883. DdManager * dd,
  884. DdNode * root,
  885. int * cost,
  886. int * support,
  887. st_table * visited)
  888. {
  889. cuddPathPair *my_pair, res_pair, pair_T, pair_E;
  890. DdNode *my_root, *T, *E;
  891. int weight;
  892. DdNode *zero = DD_ZERO(dd);
  893. my_root = Cudd_Regular(root);
  894. if (st_lookup(visited, my_root, (void **) &my_pair)) {
  895. if (Cudd_IsComplement(root)) {
  896. res_pair.pos = my_pair->neg;
  897. res_pair.neg = my_pair->pos;
  898. } else {
  899. res_pair.pos = my_pair->pos;
  900. res_pair.neg = my_pair->neg;
  901. }
  902. return(res_pair);
  903. }
  904. /* In the case of a BDD the following test is equivalent to
  905. ** testing whether the BDD is the constant 1. This formulation,
  906. ** however, works for ADDs as well, by assuming the usual
  907. ** dichotomy of 0 and != 0.
  908. */
  909. if (cuddIsConstant(my_root)) {
  910. if (my_root != zero) {
  911. res_pair.pos = 0;
  912. res_pair.neg = DD_BIGGY;
  913. } else {
  914. res_pair.pos = DD_BIGGY;
  915. res_pair.neg = 0;
  916. }
  917. } else {
  918. T = cuddT(my_root);
  919. E = cuddE(my_root);
  920. pair_T = getShortest(dd, T, cost, support, visited);
  921. pair_E = getShortest(dd, E, cost, support, visited);
  922. weight = WEIGHT(cost, my_root->index);
  923. res_pair.pos = ddMin(pair_T.pos+weight, pair_E.pos);
  924. res_pair.neg = ddMin(pair_T.neg+weight, pair_E.neg);
  925. /* Update support. */
  926. if (support != NULL) {
  927. support[my_root->index] = 1;
  928. }
  929. }
  930. my_pair = ALLOC(cuddPathPair, 1);
  931. if (my_pair == NULL) {
  932. if (Cudd_IsComplement(root)) {
  933. int tmp = res_pair.pos;
  934. res_pair.pos = res_pair.neg;
  935. res_pair.neg = tmp;
  936. }
  937. return(res_pair);
  938. }
  939. my_pair->pos = res_pair.pos;
  940. my_pair->neg = res_pair.neg;
  941. st_insert(visited, my_root, my_pair);
  942. if (Cudd_IsComplement(root)) {
  943. res_pair.pos = my_pair->neg;
  944. res_pair.neg = my_pair->pos;
  945. } else {
  946. res_pair.pos = my_pair->pos;
  947. res_pair.neg = my_pair->neg;
  948. }
  949. return(res_pair);
  950. } /* end of getShortest */
  951. /**
  952. @brief Build a %BDD for a shortest path of f.
  953. @details Given the minimum length from the root, and the minimum
  954. lengths for each node (in visited), apply triangulation at each
  955. node. Of the two children of each node on a shortest path, at least
  956. one is on a shortest path. In case of ties the procedure chooses the
  957. THEN children.
  958. @return a pointer to the cube %BDD representing the path if
  959. successful; NULL otherwise.
  960. @sideeffect None
  961. */
  962. static DdNode *
  963. getPath(
  964. DdManager * manager,
  965. st_table * visited,
  966. DdNode * f,
  967. int * weight,
  968. int cost)
  969. {
  970. DdNode *sol, *tmp;
  971. DdNode *my_dd, *T, *E;
  972. cuddPathPair *T_pair, *E_pair;
  973. int Tcost, Ecost;
  974. int complement;
  975. my_dd = Cudd_Regular(f);
  976. complement = Cudd_IsComplement(f);
  977. sol = DD_ONE(manager);
  978. cuddRef(sol);
  979. while (!cuddIsConstant(my_dd)) {
  980. Tcost = cost - WEIGHT(weight, my_dd->index);
  981. Ecost = cost;
  982. T = cuddT(my_dd);
  983. E = cuddE(my_dd);
  984. if (complement) {T = Cudd_Not(T); E = Cudd_Not(E);}
  985. st_lookup(visited, Cudd_Regular(T), (void **) &T_pair);
  986. if ((Cudd_IsComplement(T) && T_pair->neg == Tcost) ||
  987. (!Cudd_IsComplement(T) && T_pair->pos == Tcost)) {
  988. tmp = cuddBddAndRecur(manager,manager->vars[my_dd->index],sol);
  989. if (tmp == NULL) {
  990. Cudd_RecursiveDeref(manager,sol);
  991. return(NULL);
  992. }
  993. cuddRef(tmp);
  994. Cudd_RecursiveDeref(manager,sol);
  995. sol = tmp;
  996. complement = Cudd_IsComplement(T);
  997. my_dd = Cudd_Regular(T);
  998. cost = Tcost;
  999. continue;
  1000. }
  1001. st_lookup(visited, Cudd_Regular(E), (void **) &E_pair);
  1002. if ((Cudd_IsComplement(E) && E_pair->neg == Ecost) ||
  1003. (!Cudd_IsComplement(E) && E_pair->pos == Ecost)) {
  1004. tmp = cuddBddAndRecur(manager,Cudd_Not(manager->vars[my_dd->index]),sol);
  1005. if (tmp == NULL) {
  1006. Cudd_RecursiveDeref(manager,sol);
  1007. return(NULL);
  1008. }
  1009. cuddRef(tmp);
  1010. Cudd_RecursiveDeref(manager,sol);
  1011. sol = tmp;
  1012. complement = Cudd_IsComplement(E);
  1013. my_dd = Cudd_Regular(E);
  1014. cost = Ecost;
  1015. continue;
  1016. }
  1017. (void) fprintf(manager->err,"We shouldn't be here!!\n");
  1018. manager->errorCode = CUDD_INTERNAL_ERROR;
  1019. return(NULL);
  1020. }
  1021. cuddDeref(sol);
  1022. return(sol);
  1023. } /* end of getPath */
  1024. /**
  1025. @brief Finds the size of the largest cube(s) in a %DD.
  1026. @details This problem is translated into finding the shortest paths
  1027. from a node when both THEN and ELSE arcs have unit lengths. Uses a
  1028. local symbol table to store the lengths for each node. Only the
  1029. lengths for the regular nodes are entered in the table, because
  1030. those for the complement nodes are simply obtained by swapping the
  1031. two lenghts.
  1032. @return a pair of lengths: the length of the shortest path to 1; and
  1033. the length of the shortest path to 0. This is done so as to take
  1034. complement arcs into account.
  1035. @sideeffect none
  1036. */
  1037. static cuddPathPair
  1038. getLargest(
  1039. DdManager * dd,
  1040. DdNode * root,
  1041. st_table * visited)
  1042. {
  1043. cuddPathPair *my_pair, res_pair, pair_T, pair_E;
  1044. DdNode *my_root, *T, *E;
  1045. DdNode *zero = DD_ZERO(dd);
  1046. my_root = Cudd_Regular(root);
  1047. if (st_lookup(visited, my_root, (void **) &my_pair)) {
  1048. if (Cudd_IsComplement(root)) {
  1049. res_pair.pos = my_pair->neg;
  1050. res_pair.neg = my_pair->pos;
  1051. } else {
  1052. res_pair.pos = my_pair->pos;
  1053. res_pair.neg = my_pair->neg;
  1054. }
  1055. return(res_pair);
  1056. }
  1057. /* In the case of a BDD the following test is equivalent to
  1058. ** testing whether the BDD is the constant 1. This formulation,
  1059. ** however, works for ADDs as well, by assuming the usual
  1060. ** dichotomy of 0 and != 0.
  1061. */
  1062. if (cuddIsConstant(my_root)) {
  1063. if (my_root != zero) {
  1064. res_pair.pos = 0;
  1065. res_pair.neg = DD_BIGGY;
  1066. } else {
  1067. res_pair.pos = DD_BIGGY;
  1068. res_pair.neg = 0;
  1069. }
  1070. } else {
  1071. T = cuddT(my_root);
  1072. E = cuddE(my_root);
  1073. pair_T = getLargest(dd, T, visited);
  1074. pair_E = getLargest(dd, E, visited);
  1075. res_pair.pos = ddMin(pair_T.pos, pair_E.pos) + 1;
  1076. res_pair.neg = ddMin(pair_T.neg, pair_E.neg) + 1;
  1077. }
  1078. my_pair = ALLOC(cuddPathPair, 1);
  1079. if (my_pair == NULL) { /* simply do not cache this result */
  1080. if (Cudd_IsComplement(root)) {
  1081. int tmp = res_pair.pos;
  1082. res_pair.pos = res_pair.neg;
  1083. res_pair.neg = tmp;
  1084. }
  1085. return(res_pair);
  1086. }
  1087. my_pair->pos = res_pair.pos;
  1088. my_pair->neg = res_pair.neg;
  1089. /* Caching may fail without affecting correctness. */
  1090. st_insert(visited, my_root, my_pair);
  1091. if (Cudd_IsComplement(root)) {
  1092. res_pair.pos = my_pair->neg;
  1093. res_pair.neg = my_pair->pos;
  1094. } else {
  1095. res_pair.pos = my_pair->pos;
  1096. res_pair.neg = my_pair->neg;
  1097. }
  1098. return(res_pair);
  1099. } /* end of getLargest */
  1100. /**
  1101. @brief Build a %BDD for a largest cube of f.
  1102. @details Given the minimum length from the root, and the minimum
  1103. lengths for each node (in visited), apply triangulation at each
  1104. node. Of the two children of each node on a shortest path, at least
  1105. one is on a shortest path. In case of ties the procedure chooses the
  1106. THEN children.
  1107. @return a pointer to the cube %BDD representing the path if
  1108. successful; NULL otherwise.
  1109. @sideeffect None
  1110. */
  1111. static DdNode *
  1112. getCube(
  1113. DdManager * manager,
  1114. st_table * visited,
  1115. DdNode * f,
  1116. int cost)
  1117. {
  1118. DdNode *sol, *tmp;
  1119. DdNode *my_dd, *T, *E;
  1120. cuddPathPair *T_pair, *E_pair;
  1121. int Tcost, Ecost;
  1122. int complement;
  1123. my_dd = Cudd_Regular(f);
  1124. complement = Cudd_IsComplement(f);
  1125. sol = DD_ONE(manager);
  1126. cuddRef(sol);
  1127. while (!cuddIsConstant(my_dd)) {
  1128. Tcost = cost - 1;
  1129. Ecost = cost - 1;
  1130. T = cuddT(my_dd);
  1131. E = cuddE(my_dd);
  1132. if (complement) {T = Cudd_Not(T); E = Cudd_Not(E);}
  1133. if (!st_lookup(visited, Cudd_Regular(T), (void **)&T_pair)) return(NULL);
  1134. if ((Cudd_IsComplement(T) && T_pair->neg == Tcost) ||
  1135. (!Cudd_IsComplement(T) && T_pair->pos == Tcost)) {
  1136. tmp = cuddBddAndRecur(manager,manager->vars[my_dd->index],sol);
  1137. if (tmp == NULL) {
  1138. Cudd_RecursiveDeref(manager,sol);
  1139. return(NULL);
  1140. }
  1141. cuddRef(tmp);
  1142. Cudd_RecursiveDeref(manager,sol);
  1143. sol = tmp;
  1144. complement = Cudd_IsComplement(T);
  1145. my_dd = Cudd_Regular(T);
  1146. cost = Tcost;
  1147. continue;
  1148. }
  1149. if (!st_lookup(visited, Cudd_Regular(E), (void **)&E_pair)) return(NULL);
  1150. if ((Cudd_IsComplement(E) && E_pair->neg == Ecost) ||
  1151. (!Cudd_IsComplement(E) && E_pair->pos == Ecost)) {
  1152. tmp = cuddBddAndRecur(manager,Cudd_Not(manager->vars[my_dd->index]),sol);
  1153. if (tmp == NULL) {
  1154. Cudd_RecursiveDeref(manager,sol);
  1155. return(NULL);
  1156. }
  1157. cuddRef(tmp);
  1158. Cudd_RecursiveDeref(manager,sol);
  1159. sol = tmp;
  1160. complement = Cudd_IsComplement(E);
  1161. my_dd = Cudd_Regular(E);
  1162. cost = Ecost;
  1163. continue;
  1164. }
  1165. (void) fprintf(manager->err,"We shouldn't be here!\n");
  1166. manager->errorCode = CUDD_INTERNAL_ERROR;
  1167. return(NULL);
  1168. }
  1169. cuddDeref(sol);
  1170. return(sol);
  1171. } /* end of getCube */
  1172. /**
  1173. @brief Performs the recursive step of Cudd_bddMaximallyExpand.
  1174. @details On entry to this function, ub and lb should be different
  1175. from the zero %BDD. The function then maintains this invariant.
  1176. @return set of primes or zero %BDD if successful; NULL otherwise.
  1177. @sideeffect None
  1178. */
  1179. static DdNode *
  1180. ddBddMaximallyExpand(
  1181. DdManager *dd /**< manager */,
  1182. DdNode *lb /**< cube to be expanded */,
  1183. DdNode *ub /**< upper bound cube */,
  1184. DdNode *f /**< function against which to expand */)
  1185. {
  1186. DdNode *lone, *lzero, *lbv, *lbvn, *lbnx, *ubv, *ubvn, *fv, *fvn, *res;
  1187. DdNode *F, *UB, *LB, *t, *e;
  1188. int top, toplb, topub, topf;
  1189. unsigned int index;
  1190. statLine(dd);
  1191. /* Terminal cases. */
  1192. lone = DD_ONE(dd);
  1193. lzero = Cudd_Not(lone);
  1194. assert(ub != lzero && lb != lzero);
  1195. /** There are three major terminal cases in theory:
  1196. ** ub -> f : return ub
  1197. ** lb == f : return lb
  1198. ** not(lb -> f): return zero
  1199. ** Only the second case can be checked exactly in constant time.
  1200. ** For the others, we check for sufficient conditions.
  1201. */
  1202. if (ub == f || f == lone) return(ub);
  1203. if (lb == f) return(lb);
  1204. if (f == lzero || ub == Cudd_Not(f) || lb == lone || lb == Cudd_Not(f))
  1205. return(lzero);
  1206. if (!Cudd_IsComplement(lb) && Cudd_IsComplement(f)) return(lzero);
  1207. /* Here lb and f are not constant. */
  1208. /* Check cache. Since lb and ub are cubes, their local reference counts
  1209. ** are always 1. Hence, we only check the reference count of f.
  1210. */
  1211. F = Cudd_Regular(f);
  1212. if (F->ref != 1) {
  1213. DdNode *tmp = cuddCacheLookup(dd, DD_BDD_MAX_EXP_TAG, lb, ub, f);
  1214. if (tmp != NULL) {
  1215. return(tmp);
  1216. }
  1217. }
  1218. checkWhetherToGiveUp(dd);
  1219. /* Compute cofactors. For lb we use the non-zero one in
  1220. ** both branches of the recursion.
  1221. */
  1222. LB = Cudd_Regular(lb);
  1223. UB = Cudd_Regular(ub);
  1224. topf = dd->perm[F->index];
  1225. toplb = dd->perm[LB->index];
  1226. topub = (ub == lone) ? CUDD_CONST_INDEX : (unsigned int) dd->perm[UB->index];
  1227. assert(toplb <= topub);
  1228. top = ddMin(topf,toplb);
  1229. if (toplb == top) {
  1230. index = LB->index;
  1231. lbv = cuddT(LB);
  1232. lbvn = cuddE(LB);
  1233. if (lb != LB) {
  1234. lbv = Cudd_Not(lbv);
  1235. lbvn = Cudd_Not(lbvn);
  1236. }
  1237. if (lbv == lzero) {
  1238. lbnx = lbvn;
  1239. } else {
  1240. lbnx = lbv;
  1241. }
  1242. } else {
  1243. index = F->index;
  1244. lbnx = lbv = lbvn = lb;
  1245. }
  1246. if (topub == top) {
  1247. ubv = cuddT(UB);
  1248. ubvn = cuddE(UB);
  1249. if (ub != UB) {
  1250. ubv = Cudd_Not(ubv);
  1251. ubvn = Cudd_Not(ubvn);
  1252. }
  1253. } else {
  1254. ubv = ubvn = ub;
  1255. }
  1256. if (topf == top) {
  1257. fv = cuddT(F);
  1258. fvn = cuddE(F);
  1259. if (f != F) {
  1260. fv = Cudd_Not(fv);
  1261. fvn = Cudd_Not(fvn);
  1262. }
  1263. } else {
  1264. fv = fvn = f;
  1265. }
  1266. /* Recursive calls. */
  1267. if (ubv != lzero) {
  1268. t = ddBddMaximallyExpand(dd, lbnx, ubv, fv);
  1269. if (t == NULL) return(NULL);
  1270. } else {
  1271. assert(topub == toplb && topub == top && lbv == lzero);
  1272. t = lzero;
  1273. }
  1274. cuddRef(t);
  1275. /* If the top variable appears only in lb, the positive and negative
  1276. ** cofactors of each operand are the same. We want to avoid a
  1277. ** needless recursive call, which would force us to give up the
  1278. ** cache optimization trick based on reference counts.
  1279. */
  1280. if (ubv == ubvn && fv == fvn) {
  1281. res = t;
  1282. } else {
  1283. if (ubvn != lzero) {
  1284. e = ddBddMaximallyExpand(dd, lbnx, ubvn, fvn);
  1285. if (e == NULL) {
  1286. Cudd_IterDerefBdd(dd,t);
  1287. return(NULL);
  1288. }
  1289. } else {
  1290. assert(topub == toplb && topub == top && lbvn == lzero);
  1291. e = lzero;
  1292. }
  1293. if (t == e) {
  1294. res = t;
  1295. } else {
  1296. cuddRef(e);
  1297. if (toplb == top) {
  1298. if (lbv == lzero) {
  1299. /* Top variable appears in negative phase. */
  1300. if (t != lone) {
  1301. DdNode *newT;
  1302. if (Cudd_IsComplement(t)) {
  1303. newT = cuddUniqueInter(dd, index, Cudd_Not(t), lzero);
  1304. if (newT == NULL) {
  1305. Cudd_IterDerefBdd(dd,t);
  1306. Cudd_IterDerefBdd(dd,e);
  1307. return(NULL);
  1308. }
  1309. newT = Cudd_Not(newT);
  1310. } else {
  1311. newT = cuddUniqueInter(dd, index, t, lone);
  1312. if (newT == NULL) {
  1313. Cudd_IterDerefBdd(dd,t);
  1314. Cudd_IterDerefBdd(dd,e);
  1315. return(NULL);
  1316. }
  1317. }
  1318. cuddRef(newT);
  1319. cuddDeref(t);
  1320. t = newT;
  1321. }
  1322. } else if (lbvn == lzero) {
  1323. /* Top variable appears in positive phase. */
  1324. if (e != lone) {
  1325. DdNode *newE;
  1326. newE = cuddUniqueInter(dd, index, lone, e);
  1327. if (newE == NULL) {
  1328. Cudd_IterDerefBdd(dd,t);
  1329. Cudd_IterDerefBdd(dd,e);
  1330. return(NULL);
  1331. }
  1332. cuddRef(newE);
  1333. cuddDeref(e);
  1334. e = newE;
  1335. }
  1336. } else {
  1337. /* Not a cube. */
  1338. Cudd_IterDerefBdd(dd,t);
  1339. Cudd_IterDerefBdd(dd,e);
  1340. return(NULL);
  1341. }
  1342. }
  1343. /* Combine results. */
  1344. res = cuddBddAndRecur(dd, t, e);
  1345. if (res == NULL) {
  1346. Cudd_IterDerefBdd(dd,t);
  1347. Cudd_IterDerefBdd(dd,e);
  1348. return(NULL);
  1349. }
  1350. cuddRef(res);
  1351. Cudd_IterDerefBdd(dd,t);
  1352. Cudd_IterDerefBdd(dd,e);
  1353. }
  1354. }
  1355. /* Cache result and return. */
  1356. if (F->ref != 1) {
  1357. cuddCacheInsert(dd, DD_BDD_MAX_EXP_TAG, lb, ub, f, res);
  1358. }
  1359. cuddDeref(res);
  1360. return(res);
  1361. } /* end of ddBddMaximallyExpand */
  1362. /**
  1363. @brief Performs shortest path computation on a unate function.
  1364. @details This function is based on the observation that in the %BDD
  1365. of a unate function no node except the constant is reachable from
  1366. the root via paths of different parity.
  1367. @return the length of the shortest path to one if successful;
  1368. CUDD_OUT_OF_MEM otherwise.
  1369. @sideeffect None
  1370. @see getShortest
  1371. */
  1372. static int
  1373. ddBddShortestPathUnate(
  1374. DdManager *dd,
  1375. DdNode *f,
  1376. int *phases,
  1377. st_table *table)
  1378. {
  1379. int positive, l, lT, lE;
  1380. DdNode *lone = DD_ONE(dd);
  1381. DdNode *lzero = Cudd_Not(lone);
  1382. DdNode *F, *fv, *fvn;
  1383. if (st_lookup_int(table, f, &l)) {
  1384. return(l);
  1385. }
  1386. if (f == lone) {
  1387. l = 0;
  1388. } else if (f == lzero) {
  1389. l = DD_BIGGY;
  1390. } else {
  1391. F = Cudd_Regular(f);
  1392. fv = cuddT(F);
  1393. fvn = cuddE(F);
  1394. if (f != F) {
  1395. fv = Cudd_Not(fv);
  1396. fvn = Cudd_Not(fvn);
  1397. }
  1398. lT = ddBddShortestPathUnate(dd, fv, phases, table);
  1399. lE = ddBddShortestPathUnate(dd, fvn, phases, table);
  1400. positive = phases[F->index];
  1401. l = positive ? ddMin(lT+1, lE) : ddMin(lT, lE+1);
  1402. }
  1403. if (st_insert(table, f, (void *)(ptrint) l) == ST_OUT_OF_MEM) {
  1404. return(CUDD_OUT_OF_MEM);
  1405. }
  1406. return(l);
  1407. } /* end of ddShortestPathUnate */
  1408. /**
  1409. @brief Extracts largest prime of a unate function.
  1410. @return the %BDD of the prime if successful; NULL otherwise.
  1411. @sideeffect None
  1412. @see getPath
  1413. */
  1414. static DdNode *
  1415. ddGetLargestCubeUnate(
  1416. DdManager *dd,
  1417. DdNode *f,
  1418. int *phases,
  1419. st_table *table)
  1420. {
  1421. DdNode *res, *scan;
  1422. int cost;
  1423. res = DD_ONE(dd);
  1424. cuddRef(res);
  1425. scan = f;
  1426. st_lookup_int(table, scan, &cost);
  1427. while (!Cudd_IsConstantInt(scan)) {
  1428. int Pcost, Ncost, Tcost;
  1429. DdNode *tmp, *T, *E;
  1430. DdNode *rscan = Cudd_Regular(scan);
  1431. unsigned int index = rscan->index;
  1432. assert(phases[index] == 0 || phases[index] == 1);
  1433. int positive = phases[index] == 1;
  1434. Pcost = positive ? cost - 1 : cost;
  1435. Ncost = positive ? cost : cost - 1;
  1436. T = cuddT(rscan);
  1437. E = cuddE(rscan);
  1438. if (rscan != scan) {
  1439. T = Cudd_Not(T);
  1440. E = Cudd_Not(E);
  1441. }
  1442. tmp = res;
  1443. st_lookup_int(table, T, &Tcost);
  1444. if (Tcost == Pcost) {
  1445. cost = Pcost;
  1446. scan = T;
  1447. if (positive) {
  1448. tmp = cuddBddAndRecur(dd, dd->vars[index], res);
  1449. }
  1450. } else {
  1451. cost = Ncost;
  1452. scan = E;
  1453. if (!positive) {
  1454. tmp = cuddBddAndRecur(dd, Cudd_Not(dd->vars[index]), res);
  1455. }
  1456. }
  1457. if (tmp == NULL) {
  1458. Cudd_IterDerefBdd(dd, res);
  1459. return(NULL);
  1460. }
  1461. cuddRef(tmp);
  1462. Cudd_IterDerefBdd(dd, res);
  1463. res = tmp;
  1464. }
  1465. cuddDeref(res);
  1466. return(res);
  1467. } /* end of ddGetLargestCubeUnate */