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.

1774 lines
49 KiB

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