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.

940 lines
24 KiB

  1. /**
  2. @file
  3. @ingroup cudd
  4. @brief Functions for exact variable reordering.
  5. @author Cheng Hua, Fabio Somenzi
  6. @copyright@parblock
  7. Copyright (c) 1995-2015, Regents of the University of Colorado
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions
  11. are met:
  12. Redistributions of source code must retain the above copyright
  13. notice, this list of conditions and the following disclaimer.
  14. Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in the
  16. documentation and/or other materials provided with the distribution.
  17. Neither the name of the University of Colorado nor the names of its
  18. contributors may be used to endorse or promote products derived from
  19. this software without specific prior written permission.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  30. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. POSSIBILITY OF SUCH DAMAGE.
  32. @endparblock
  33. */
  34. #include "util.h"
  35. #include "cuddInt.h"
  36. /*---------------------------------------------------------------------------*/
  37. /* Constant declarations */
  38. /*---------------------------------------------------------------------------*/
  39. /*---------------------------------------------------------------------------*/
  40. /* Stucture declarations */
  41. /*---------------------------------------------------------------------------*/
  42. /*---------------------------------------------------------------------------*/
  43. /* Type declarations */
  44. /*---------------------------------------------------------------------------*/
  45. /*---------------------------------------------------------------------------*/
  46. /* Variable declarations */
  47. /*---------------------------------------------------------------------------*/
  48. /*---------------------------------------------------------------------------*/
  49. /* Macro declarations */
  50. /*---------------------------------------------------------------------------*/
  51. /** \cond */
  52. /*---------------------------------------------------------------------------*/
  53. /* Static function prototypes */
  54. /*---------------------------------------------------------------------------*/
  55. static int getMaxBinomial (int n);
  56. static DdHalfWord ** getMatrix (int rows, int cols);
  57. static void freeMatrix (DdHalfWord **matrix);
  58. static int getLevelKeys (DdManager *table, int l);
  59. static int ddShuffle (DdManager *table, DdHalfWord *permutation, int lower, int upper);
  60. static int ddSiftUp (DdManager *table, int x, int xLow);
  61. static int updateUB (DdManager *table, int oldBound, DdHalfWord *bestOrder, int lower, int upper);
  62. static int ddCountRoots (DdManager *table, int lower, int upper);
  63. static void ddClearGlobal (DdManager *table, int lower, int maxlevel);
  64. static int computeLB (DdManager *table, DdHalfWord *order, int roots, int cost, int lower, int upper, int level);
  65. static int updateEntry (DdManager *table, DdHalfWord *order, int level, int cost, DdHalfWord **orders, int *costs, int subsets, char *mask, int lower, int upper);
  66. static void pushDown (DdHalfWord *order, int j, int level);
  67. static DdHalfWord * initSymmInfo (DdManager *table, int lower, int upper);
  68. static int checkSymmInfo (DdManager *table, DdHalfWord *symmInfo, int index, int level);
  69. /** \endcond */
  70. /*---------------------------------------------------------------------------*/
  71. /* Definition of exported functions */
  72. /*---------------------------------------------------------------------------*/
  73. /*---------------------------------------------------------------------------*/
  74. /* Definition of internal functions */
  75. /*---------------------------------------------------------------------------*/
  76. /**
  77. @brief Exact variable ordering algorithm.
  78. @details Finds an optimum order for the variables between lower and
  79. upper.
  80. @return 1 if successful; 0 otherwise.
  81. @sideeffect None
  82. */
  83. int
  84. cuddExact(
  85. DdManager * table,
  86. int lower,
  87. int upper)
  88. {
  89. int k, i, j;
  90. int maxBinomial, oldSubsets, newSubsets;
  91. int subsetCost;
  92. int size; /* number of variables to be reordered */
  93. int unused, nvars, level, result;
  94. int upperBound, lowerBound, cost;
  95. int roots;
  96. char *mask = NULL;
  97. DdHalfWord *symmInfo = NULL;
  98. DdHalfWord **newOrder = NULL;
  99. DdHalfWord **oldOrder = NULL;
  100. int *newCost = NULL;
  101. int *oldCost = NULL;
  102. DdHalfWord **tmpOrder;
  103. int *tmpCost;
  104. DdHalfWord *bestOrder = NULL;
  105. DdHalfWord *order;
  106. #ifdef DD_STATS
  107. int ddTotalSubsets;
  108. #endif
  109. /* Restrict the range to be reordered by excluding unused variables
  110. ** at the two ends. */
  111. while (table->subtables[lower].keys == 1 &&
  112. table->vars[table->invperm[lower]]->ref == 1 &&
  113. lower < upper)
  114. lower++;
  115. while (table->subtables[upper].keys == 1 &&
  116. table->vars[table->invperm[upper]]->ref == 1 &&
  117. lower < upper)
  118. upper--;
  119. if (lower == upper) return(1); /* trivial problem */
  120. /* Apply symmetric sifting to get a good upper bound and to extract
  121. ** symmetry information. */
  122. result = cuddSymmSiftingConv(table,lower,upper);
  123. if (result == 0) goto cuddExactOutOfMem;
  124. #ifdef DD_STATS
  125. (void) fprintf(table->out,"\n");
  126. table->totalShuffles = 0;
  127. ddTotalSubsets = 0;
  128. #endif
  129. /* Initialization. */
  130. nvars = table->size;
  131. size = upper - lower + 1;
  132. /* Count unused variable among those to be reordered. This is only
  133. ** used to compute maxBinomial. */
  134. unused = 0;
  135. for (i = lower + 1; i < upper; i++) {
  136. if (table->subtables[i].keys == 1 &&
  137. table->vars[table->invperm[i]]->ref == 1)
  138. unused++;
  139. }
  140. /* Find the maximum number of subsets we may have to store. */
  141. maxBinomial = getMaxBinomial(size - unused);
  142. if (maxBinomial == -1) goto cuddExactOutOfMem;
  143. newOrder = getMatrix(maxBinomial, size);
  144. if (newOrder == NULL) goto cuddExactOutOfMem;
  145. newCost = ALLOC(int, maxBinomial);
  146. if (newCost == NULL) goto cuddExactOutOfMem;
  147. oldOrder = getMatrix(maxBinomial, size);
  148. if (oldOrder == NULL) goto cuddExactOutOfMem;
  149. oldCost = ALLOC(int, maxBinomial);
  150. if (oldCost == NULL) goto cuddExactOutOfMem;
  151. bestOrder = ALLOC(DdHalfWord, size);
  152. if (bestOrder == NULL) goto cuddExactOutOfMem;
  153. mask = ALLOC(char, nvars);
  154. if (mask == NULL) goto cuddExactOutOfMem;
  155. symmInfo = initSymmInfo(table, lower, upper);
  156. if (symmInfo == NULL) goto cuddExactOutOfMem;
  157. roots = ddCountRoots(table, lower, upper);
  158. /* Initialize the old order matrix for the empty subset and the best
  159. ** order to the current order. The cost for the empty subset includes
  160. ** the cost of the levels between upper and the constants. These levels
  161. ** are not going to change. Hence, we count them only once.
  162. */
  163. oldSubsets = 1;
  164. for (i = 0; i < size; i++) {
  165. oldOrder[0][i] = bestOrder[i] = (DdHalfWord) table->invperm[i+lower];
  166. }
  167. subsetCost = (int) table->constants.keys;
  168. for (i = upper + 1; i < nvars; i++)
  169. subsetCost += getLevelKeys(table,i);
  170. oldCost[0] = subsetCost;
  171. /* The upper bound is initialized to the current size of the BDDs. */
  172. upperBound = (int) (table->keys - table->isolated);
  173. /* Now consider subsets of increasing size. */
  174. for (k = 1; k <= size; k++) {
  175. #ifdef DD_STATS
  176. (void) fprintf(table->out,"Processing subsets of size %d\n", k);
  177. fflush(table->out);
  178. #endif
  179. newSubsets = 0;
  180. level = size - k; /* offset of first bottom variable */
  181. for (i = 0; i < oldSubsets; i++) { /* for each subset of size k-1 */
  182. order = oldOrder[i];
  183. cost = oldCost[i];
  184. lowerBound = computeLB(table, order, roots, cost, lower, upper,
  185. level);
  186. if (lowerBound >= upperBound)
  187. continue;
  188. /* Impose new order. */
  189. result = ddShuffle(table, order, lower, upper);
  190. if (result == 0) goto cuddExactOutOfMem;
  191. upperBound = updateUB(table,upperBound,bestOrder,lower,upper);
  192. /* For each top bottom variable. */
  193. for (j = level; j >= 0; j--) {
  194. /* Skip unused variables. */
  195. if (table->subtables[j+lower-1].keys == 1 &&
  196. table->vars[table->invperm[j+lower-1]]->ref == 1) continue;
  197. /* Find cost under this order. */
  198. subsetCost = cost + getLevelKeys(table, lower + level);
  199. newSubsets = updateEntry(table, order, level, subsetCost,
  200. newOrder, newCost, newSubsets, mask,
  201. lower, upper);
  202. if (j == 0)
  203. break;
  204. if (checkSymmInfo(table, symmInfo, (int) order[j-1], level) == 0)
  205. continue;
  206. pushDown(order,j-1,level);
  207. /* Impose new order. */
  208. result = ddShuffle(table, order, lower, upper);
  209. if (result == 0) goto cuddExactOutOfMem;
  210. upperBound = updateUB(table,upperBound,bestOrder,lower,upper);
  211. } /* for each bottom variable */
  212. } /* for each subset of size k */
  213. /* New orders become old orders in preparation for next iteration. */
  214. tmpOrder = oldOrder; tmpCost = oldCost;
  215. oldOrder = newOrder; oldCost = newCost;
  216. newOrder = tmpOrder; newCost = tmpCost;
  217. #ifdef DD_STATS
  218. ddTotalSubsets += newSubsets;
  219. #endif
  220. oldSubsets = newSubsets;
  221. }
  222. result = ddShuffle(table, bestOrder, lower, upper);
  223. if (result == 0) goto cuddExactOutOfMem;
  224. #ifdef DD_STATS
  225. #ifdef DD_VERBOSE
  226. (void) fprintf(table->out,"\n");
  227. #endif
  228. (void) fprintf(table->out,"#:S_EXACT %8d: total subsets\n",
  229. ddTotalSubsets);
  230. (void) fprintf(table->out,"#:H_EXACT %8d: total shuffles",
  231. table->totalShuffles);
  232. #endif
  233. freeMatrix(newOrder);
  234. freeMatrix(oldOrder);
  235. FREE(bestOrder);
  236. FREE(oldCost);
  237. FREE(newCost);
  238. FREE(symmInfo);
  239. FREE(mask);
  240. return(1);
  241. cuddExactOutOfMem:
  242. if (newOrder != NULL) freeMatrix(newOrder);
  243. if (oldOrder != NULL) freeMatrix(oldOrder);
  244. if (bestOrder != NULL) FREE(bestOrder);
  245. if (oldCost != NULL) FREE(oldCost);
  246. if (newCost != NULL) FREE(newCost);
  247. if (symmInfo != NULL) FREE(symmInfo);
  248. if (mask != NULL) FREE(mask);
  249. table->errorCode = CUDD_MEMORY_OUT;
  250. return(0);
  251. } /* end of cuddExact */
  252. /**
  253. @brief Returns the maximum value of `(n choose k)` for a given `n`.
  254. @details Computes the maximum value of `(n choose k)` for a given
  255. `n`. The maximum value occurs for `k = n/2` when `n` is even, or `k =
  256. (n-1)/2` when `n` is odd. The algorithm used in this procedure avoids
  257. intermediate overflow problems. It is based on the identity
  258. binomial(n,k) = n/k * binomial(n-1,k-1).
  259. @return the computed value if successful; -1 if out of range.
  260. @sideeffect None
  261. */
  262. static int
  263. getMaxBinomial(
  264. int n)
  265. {
  266. double i, j, result;
  267. if (n < 0 || n > 33) return(-1); /* error */
  268. if (n < 2) return(1);
  269. for (result = (double)((n+3)/2), i = result+1, j=2; i <= n; i++, j++) {
  270. result *= i;
  271. result /= j;
  272. }
  273. return((int)result);
  274. } /* end of getMaxBinomial */
  275. #if 0
  276. /**
  277. @brief Returns the gcd of two integers.
  278. @details Uses the binary GCD algorithm described in Cormen,
  279. Leiserson, and Rivest.
  280. @sideeffect None
  281. */
  282. static int
  283. gcd(
  284. int x,
  285. int y)
  286. {
  287. int a;
  288. int b;
  289. int lsbMask;
  290. /* GCD(n,0) = n. */
  291. if (x == 0) return(y);
  292. if (y == 0) return(x);
  293. a = x; b = y; lsbMask = 1;
  294. /* Here both a and b are != 0. The iteration maintains this invariant.
  295. ** Hence, we only need to check for when they become equal.
  296. */
  297. while (a != b) {
  298. if (a & lsbMask) {
  299. if (b & lsbMask) { /* both odd */
  300. if (a < b) {
  301. b = (b - a) >> 1;
  302. } else {
  303. a = (a - b) >> 1;
  304. }
  305. } else { /* a odd, b even */
  306. b >>= 1;
  307. }
  308. } else {
  309. if (b & lsbMask) { /* a even, b odd */
  310. a >>= 1;
  311. } else { /* both even */
  312. lsbMask <<= 1;
  313. }
  314. }
  315. }
  316. return(a);
  317. } /* end of gcd */
  318. #endif
  319. /**
  320. @brief Allocates a two-dimensional matrix of ints.
  321. @return the pointer to the matrix if successful; NULL otherwise.
  322. @sideeffect None
  323. @see freeMatrix
  324. */
  325. static DdHalfWord **
  326. getMatrix(
  327. int rows /* number of rows */,
  328. int cols /* number of columns */)
  329. {
  330. DdHalfWord **matrix;
  331. int i;
  332. if (cols*rows == 0) return(NULL);
  333. matrix = ALLOC(DdHalfWord *, rows);
  334. if (matrix == NULL) return(NULL);
  335. matrix[0] = ALLOC(DdHalfWord, cols*rows);
  336. if (matrix[0] == NULL) {
  337. FREE(matrix);
  338. return(NULL);
  339. }
  340. for (i = 1; i < rows; i++) {
  341. matrix[i] = matrix[i-1] + cols;
  342. }
  343. return(matrix);
  344. } /* end of getMatrix */
  345. /**
  346. @brief Frees a two-dimensional matrix allocated by getMatrix.
  347. @sideeffect None
  348. @see getMatrix
  349. */
  350. static void
  351. freeMatrix(
  352. DdHalfWord ** matrix)
  353. {
  354. FREE(matrix[0]);
  355. FREE(matrix);
  356. return;
  357. } /* end of freeMatrix */
  358. /**
  359. @brief Returns the number of nodes at one level of a unique table.
  360. @details The projection function, if isolated, is not counted.
  361. @sideeffect None
  362. */
  363. static int
  364. getLevelKeys(
  365. DdManager * table,
  366. int l)
  367. {
  368. int isolated;
  369. int x; /* x is an index */
  370. x = table->invperm[l];
  371. isolated = table->vars[x]->ref == 1;
  372. return((int) table->subtables[l].keys - isolated);
  373. } /* end of getLevelKeys */
  374. /**
  375. @brief Reorders variables according to a given permutation.
  376. @details The i-th permutation array contains the index of the
  377. variable that should be brought to the i-th level. ddShuffle assumes
  378. that no dead nodes are present and that the interaction matrix is
  379. properly initialized. The reordering is achieved by a series of
  380. upward sifts.
  381. @return 1 if successful; 0 otherwise.
  382. @sideeffect None
  383. */
  384. static int
  385. ddShuffle(
  386. DdManager * table,
  387. DdHalfWord * permutation,
  388. int lower,
  389. int upper)
  390. {
  391. DdHalfWord index;
  392. int level;
  393. int position;
  394. #if 0
  395. int numvars;
  396. #endif
  397. int result;
  398. #if defined(DD_STATS) && defined(DD_VERBOSE)
  399. int initialSize;
  400. int finalSize;
  401. #endif
  402. #if defined(DD_STATS) && defined(DD_VERBOSE)
  403. initialSize = (int) (table->keys - table->isolated);
  404. #endif
  405. #if 0
  406. numvars = table->size;
  407. (void) fprintf(table->out,"%d:", table->totalShuffles);
  408. for (level = 0; level < numvars; level++) {
  409. (void) fprintf(table->out," %d", table->invperm[level]);
  410. }
  411. (void) fprintf(table->out,"\n");
  412. #endif
  413. for (level = 0; level <= upper - lower; level++) {
  414. index = permutation[level];
  415. position = table->perm[index];
  416. result = ddSiftUp(table,position,level+lower);
  417. if (!result) return(0);
  418. }
  419. #ifdef DD_STATS
  420. table->totalShuffles++;
  421. #ifdef DD_VERBOSE
  422. finalSize = (int) (table->keys - table->isolated);
  423. if (finalSize < initialSize) {
  424. (void) fprintf(table->out,"-");
  425. } else if (finalSize > initialSize) {
  426. (void) fprintf(table->out,"+");
  427. } else {
  428. (void) fprintf(table->out,"=");
  429. }
  430. if ((table->totalShuffles & 63) == 0) (void) fprintf(table->out,"\n");
  431. fflush(table->out);
  432. #endif
  433. #endif
  434. return(1);
  435. } /* end of ddShuffle */
  436. /**
  437. @brief Moves one variable up.
  438. @details Takes a variable from position x and sifts it up to
  439. position xLow; xLow should be less than or equal to x.
  440. @return 1 if successful; 0 otherwise
  441. @sideeffect None
  442. */
  443. static int
  444. ddSiftUp(
  445. DdManager * table,
  446. int x,
  447. int xLow)
  448. {
  449. int y;
  450. int size;
  451. y = cuddNextLow(table,x);
  452. while (y >= xLow) {
  453. size = cuddSwapInPlace(table,y,x);
  454. if (size == 0) {
  455. return(0);
  456. }
  457. x = y;
  458. y = cuddNextLow(table,x);
  459. }
  460. return(1);
  461. } /* end of ddSiftUp */
  462. /**
  463. @brief Updates the upper bound and saves the best order seen so far.
  464. @return the current value of the upper bound.
  465. @sideeffect None
  466. */
  467. static int
  468. updateUB(
  469. DdManager * table,
  470. int oldBound,
  471. DdHalfWord * bestOrder,
  472. int lower,
  473. int upper)
  474. {
  475. int i;
  476. int newBound = (int) (table->keys - table->isolated);
  477. if (newBound < oldBound) {
  478. #ifdef DD_STATS
  479. (void) fprintf(table->out,"New upper bound = %d\n", newBound);
  480. fflush(table->out);
  481. #endif
  482. for (i = lower; i <= upper; i++)
  483. bestOrder[i-lower] = (DdHalfWord) table->invperm[i];
  484. return(newBound);
  485. } else {
  486. return(oldBound);
  487. }
  488. } /* end of updateUB */
  489. /**
  490. @brief Counts the number of roots.
  491. @details Counts the number of roots at the levels between lower and
  492. upper. The computation is based on breadth-first search. A node is
  493. a root if it is not reachable from any previously visited node.
  494. (All the nodes at level lower are therefore considered roots.) The
  495. roots that are constant nodes are always ignored. The visited flag
  496. uses the LSB of the next pointer.
  497. @return the root count.
  498. @sideeffect None
  499. @see ddClearGlobal
  500. */
  501. static int
  502. ddCountRoots(
  503. DdManager * table,
  504. int lower,
  505. int upper)
  506. {
  507. int i,j;
  508. DdNode *f;
  509. DdNodePtr *nodelist;
  510. DdNode *sentinel = &(table->sentinel);
  511. int slots;
  512. int roots = 0;
  513. int maxlevel = lower;
  514. for (i = lower; i <= upper; i++) {
  515. nodelist = table->subtables[i].nodelist;
  516. slots = (int) table->subtables[i].slots;
  517. for (j = 0; j < slots; j++) {
  518. f = nodelist[j];
  519. while (f != sentinel) {
  520. /* A node is a root of the DAG if it cannot be
  521. ** reached by nodes above it. If a node was never
  522. ** reached during the previous depth-first searches,
  523. ** then it is a root, and we start a new depth-first
  524. ** search from it.
  525. */
  526. if (!Cudd_IsComplement(f->next)) {
  527. if (f != table->vars[f->index]) {
  528. roots++;
  529. }
  530. }
  531. if (!cuddIsConstant(cuddT(f))) {
  532. cuddT(f)->next = Cudd_Complement(cuddT(f)->next);
  533. if (table->perm[cuddT(f)->index] > maxlevel)
  534. maxlevel = table->perm[cuddT(f)->index];
  535. }
  536. if (!Cudd_IsConstantInt(cuddE(f))) {
  537. Cudd_Regular(cuddE(f))->next =
  538. Cudd_Complement(Cudd_Regular(cuddE(f))->next);
  539. if (table->perm[Cudd_Regular(cuddE(f))->index] > maxlevel)
  540. maxlevel = table->perm[Cudd_Regular(cuddE(f))->index];
  541. }
  542. f = Cudd_Regular(f->next);
  543. }
  544. }
  545. }
  546. ddClearGlobal(table, lower, maxlevel);
  547. return(roots);
  548. } /* end of ddCountRoots */
  549. /**
  550. @brief Scans the %DD and clears the LSB of the next pointers.
  551. @details The LSB of the next pointers are used as markers to tell
  552. whether a node was reached. Once the roots are counted, these flags
  553. are reset.
  554. @sideeffect None
  555. @see ddCountRoots
  556. */
  557. static void
  558. ddClearGlobal(
  559. DdManager * table,
  560. int lower,
  561. int maxlevel)
  562. {
  563. int i,j;
  564. DdNode *f;
  565. DdNodePtr *nodelist;
  566. DdNode *sentinel = &(table->sentinel);
  567. int slots;
  568. for (i = lower; i <= maxlevel; i++) {
  569. nodelist = table->subtables[i].nodelist;
  570. slots = (int) table->subtables[i].slots;
  571. for (j = 0; j < slots; j++) {
  572. f = nodelist[j];
  573. while (f != sentinel) {
  574. f->next = Cudd_Regular(f->next);
  575. f = f->next;
  576. }
  577. }
  578. }
  579. } /* end of ddClearGlobal */
  580. /**
  581. @brief Computes a lower bound on the size of a %BDD.
  582. @details The lower bound depends on the following factors:
  583. <ul>
  584. <li> size of the lower part of it;
  585. <li> size of the part of the upper part not subjected to reordering;
  586. <li> number of roots in the part of the %BDD subjected to reordering;
  587. <li> variable in the support of the roots in the upper part of the
  588. %BDD subjected to reordering.
  589. </ul>
  590. @sideeffect None
  591. */
  592. static int
  593. computeLB(
  594. DdManager * table /**< manager */,
  595. DdHalfWord * order /**< optimal order for the subset */,
  596. int roots /**< roots between lower and upper */,
  597. int cost /**< minimum cost for the subset */,
  598. int lower /**< lower level to be reordered */,
  599. int upper /**< upper level to be reordered */,
  600. int level /**< offset for the current top bottom var */
  601. )
  602. {
  603. int i;
  604. int lb = cost;
  605. int lb1 = 0;
  606. int lb2;
  607. int support;
  608. DdHalfWord ref;
  609. /* The levels not involved in reordering are not going to change.
  610. ** Add their sizes to the lower bound.
  611. */
  612. for (i = 0; i < lower; i++) {
  613. lb += getLevelKeys(table,i);
  614. }
  615. /* If a variable is in the support, then there is going
  616. ** to be at least one node labeled by that variable.
  617. */
  618. for (i = lower; i <= lower+level; i++) {
  619. support = table->subtables[i].keys > 1 ||
  620. table->vars[order[i-lower]]->ref > 1;
  621. lb1 += support;
  622. }
  623. /* Estimate the number of nodes required to connect the roots to
  624. ** the nodes in the bottom part. */
  625. if (lower+level+1 < table->size) {
  626. if (lower+level < upper)
  627. ref = table->vars[order[level+1]]->ref;
  628. else
  629. ref = table->vars[table->invperm[upper+1]]->ref;
  630. lb2 = (int) table->subtables[lower+level+1].keys -
  631. (ref > (DdHalfWord) 1) - roots;
  632. } else {
  633. lb2 = 0;
  634. }
  635. lb += lb1 > lb2 ? lb1 : lb2;
  636. return(lb);
  637. } /* end of computeLB */
  638. /**
  639. @brief Updates entry for a subset.
  640. @details Finds the subset, if it exists. If the new order for the
  641. subset has lower cost, or if the subset did not exist, it stores the
  642. new order and cost.
  643. @return the number of subsets currently in the table.
  644. @sideeffect None
  645. */
  646. static int
  647. updateEntry(
  648. DdManager * table,
  649. DdHalfWord * order,
  650. int level,
  651. int cost,
  652. DdHalfWord ** orders,
  653. int * costs,
  654. int subsets,
  655. char * mask,
  656. int lower,
  657. int upper)
  658. {
  659. int i, j;
  660. int size = upper - lower + 1;
  661. /* Build a mask that says what variables are in this subset. */
  662. for (i = lower; i <= upper; i++)
  663. mask[table->invperm[i]] = 0;
  664. for (i = level; i < size; i++)
  665. mask[order[i]] = 1;
  666. /* Check each subset until a match is found or all subsets are examined. */
  667. for (i = 0; i < subsets; i++) {
  668. DdHalfWord *subset = orders[i];
  669. for (j = level; j < size; j++) {
  670. if (mask[subset[j]] == 0)
  671. break;
  672. }
  673. if (j == size) /* no mismatches: success */
  674. break;
  675. }
  676. if (i == subsets || cost < costs[i]) { /* add or replace */
  677. for (j = 0; j < size; j++)
  678. orders[i][j] = order[j];
  679. costs[i] = cost;
  680. subsets += (i == subsets);
  681. }
  682. return(subsets);
  683. } /* end of updateEntry */
  684. /**
  685. @brief Pushes a variable in the order down to position "level."
  686. @sideeffect None
  687. */
  688. static void
  689. pushDown(
  690. DdHalfWord * order,
  691. int j,
  692. int level)
  693. {
  694. int i;
  695. DdHalfWord tmp;
  696. tmp = order[j];
  697. for (i = j; i < level; i++) {
  698. order[i] = order[i+1];
  699. }
  700. order[level] = tmp;
  701. return;
  702. } /* end of pushDown */
  703. /**
  704. @brief Gathers symmetry information.
  705. @details Translates the symmetry information stored in the next
  706. field of each subtable from level to indices. This procedure is called
  707. immediately after symmetric sifting, so that the next fields are correct.
  708. By translating this informaton in terms of indices, we make it independent
  709. of subsequent reorderings. The format used is that of the next fields:
  710. a circular list where each variable points to the next variable in the
  711. same symmetry group. Only the entries between lower and upper are
  712. considered. The procedure returns a pointer to an array
  713. holding the symmetry information if successful; NULL otherwise.
  714. @sideeffect None
  715. @see checkSymmInfo
  716. */
  717. static DdHalfWord *
  718. initSymmInfo(
  719. DdManager * table,
  720. int lower,
  721. int upper)
  722. {
  723. int level, index, next, nextindex;
  724. DdHalfWord *symmInfo;
  725. symmInfo = ALLOC(DdHalfWord, table->size);
  726. if (symmInfo == NULL) return(NULL);
  727. for (level = lower; level <= upper; level++) {
  728. index = table->invperm[level];
  729. next = (int) table->subtables[level].next;
  730. nextindex = table->invperm[next];
  731. symmInfo[index] = (DdHalfWord) nextindex;
  732. }
  733. return(symmInfo);
  734. } /* end of initSymmInfo */
  735. /**
  736. @brief Check symmetry condition.
  737. @details Returns 1 if a variable is the one with the highest index
  738. among those belonging to a symmetry group that are in the top part of
  739. the %BDD. The top part is given by level.
  740. @sideeffect None
  741. @see initSymmInfo
  742. */
  743. static int
  744. checkSymmInfo(
  745. DdManager * table,
  746. DdHalfWord * symmInfo,
  747. int index,
  748. int level)
  749. {
  750. int i;
  751. i = (int) symmInfo[index];
  752. while (i != index) {
  753. if (index < i && table->perm[i] <= level)
  754. return(0);
  755. i = (int) symmInfo[i];
  756. }
  757. return(1);
  758. } /* end of checkSymmInfo */