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.

1711 lines
46 KiB

  1. /**CFile***********************************************************************
  2. FileName [cuddZddSymm.c]
  3. PackageName [cudd]
  4. Synopsis [Functions for symmetry-based ZDD variable reordering.]
  5. Description [External procedures included in this module:
  6. <ul>
  7. <li> Cudd_zddSymmProfile()
  8. </ul>
  9. Internal procedures included in this module:
  10. <ul>
  11. <li> cuddZddSymmCheck()
  12. <li> cuddZddSymmSifting()
  13. <li> cuddZddSymmSiftingConv()
  14. </ul>
  15. Static procedures included in this module:
  16. <ul>
  17. <li> cuddZddUniqueCompare()
  18. <li> cuddZddSymmSiftingAux()
  19. <li> cuddZddSymmSiftingConvAux()
  20. <li> cuddZddSymmSifting_up()
  21. <li> cuddZddSymmSifting_down()
  22. <li> zdd_group_move()
  23. <li> cuddZddSymmSiftingBackward()
  24. <li> zdd_group_move_backward()
  25. </ul>
  26. ]
  27. SeeAlso [cuddSymmetry.c]
  28. Author [Hyong-Kyoon Shin, In-Ho Moon]
  29. Copyright [Copyright (c) 1995-2012, Regents of the University of Colorado
  30. All rights reserved.
  31. Redistribution and use in source and binary forms, with or without
  32. modification, are permitted provided that the following conditions
  33. are met:
  34. Redistributions of source code must retain the above copyright
  35. notice, this list of conditions and the following disclaimer.
  36. Redistributions in binary form must reproduce the above copyright
  37. notice, this list of conditions and the following disclaimer in the
  38. documentation and/or other materials provided with the distribution.
  39. Neither the name of the University of Colorado nor the names of its
  40. contributors may be used to endorse or promote products derived from
  41. this software without specific prior written permission.
  42. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  43. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  44. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  45. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  46. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  47. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  48. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  49. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  50. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  51. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  52. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  53. POSSIBILITY OF SUCH DAMAGE.]
  54. ******************************************************************************/
  55. #include "util.h"
  56. #include "cuddInt.h"
  57. /*---------------------------------------------------------------------------*/
  58. /* Constant declarations */
  59. /*---------------------------------------------------------------------------*/
  60. #define ZDD_MV_OOM (Move *)1
  61. /*---------------------------------------------------------------------------*/
  62. /* Stucture declarations */
  63. /*---------------------------------------------------------------------------*/
  64. /*---------------------------------------------------------------------------*/
  65. /* Type declarations */
  66. /*---------------------------------------------------------------------------*/
  67. /*---------------------------------------------------------------------------*/
  68. /* Variable declarations */
  69. /*---------------------------------------------------------------------------*/
  70. #ifndef lint
  71. static char rcsid[] DD_UNUSED = "$Id: cuddZddSymm.c,v 1.31 2012/02/05 01:07:19 fabio Exp $";
  72. #endif
  73. extern int *zdd_entry;
  74. extern int zddTotalNumberSwapping;
  75. static DdNode *empty;
  76. /*---------------------------------------------------------------------------*/
  77. /* Macro declarations */
  78. /*---------------------------------------------------------------------------*/
  79. /**AutomaticStart*************************************************************/
  80. /*---------------------------------------------------------------------------*/
  81. /* Static function prototypes */
  82. /*---------------------------------------------------------------------------*/
  83. static int cuddZddSymmSiftingAux (DdManager *table, int x, int x_low, int x_high);
  84. static int cuddZddSymmSiftingConvAux (DdManager *table, int x, int x_low, int x_high);
  85. static Move * cuddZddSymmSifting_up (DdManager *table, int x, int x_low, int initial_size);
  86. static Move * cuddZddSymmSifting_down (DdManager *table, int x, int x_high, int initial_size);
  87. static int cuddZddSymmSiftingBackward (DdManager *table, Move *moves, int size);
  88. static int zdd_group_move (DdManager *table, int x, int y, Move **moves);
  89. static int zdd_group_move_backward (DdManager *table, int x, int y);
  90. static void cuddZddSymmSummary (DdManager *table, int lower, int upper, int *symvars, int *symgroups);
  91. /**AutomaticEnd***************************************************************/
  92. /*---------------------------------------------------------------------------*/
  93. /* Definition of exported functions */
  94. /*---------------------------------------------------------------------------*/
  95. /**Function********************************************************************
  96. Synopsis [Prints statistics on symmetric ZDD variables.]
  97. Description []
  98. SideEffects [None]
  99. SeeAlso []
  100. ******************************************************************************/
  101. void
  102. Cudd_zddSymmProfile(
  103. DdManager * table,
  104. int lower,
  105. int upper)
  106. {
  107. int i, x, gbot;
  108. int TotalSymm = 0;
  109. int TotalSymmGroups = 0;
  110. for (i = lower; i < upper; i++) {
  111. if (table->subtableZ[i].next != (unsigned) i) {
  112. x = i;
  113. (void) fprintf(table->out,"Group:");
  114. do {
  115. (void) fprintf(table->out," %d", table->invpermZ[x]);
  116. TotalSymm++;
  117. gbot = x;
  118. x = table->subtableZ[x].next;
  119. } while (x != i);
  120. TotalSymmGroups++;
  121. #ifdef DD_DEBUG
  122. assert(table->subtableZ[gbot].next == (unsigned) i);
  123. #endif
  124. i = gbot;
  125. (void) fprintf(table->out,"\n");
  126. }
  127. }
  128. (void) fprintf(table->out,"Total Symmetric = %d\n", TotalSymm);
  129. (void) fprintf(table->out,"Total Groups = %d\n", TotalSymmGroups);
  130. } /* end of Cudd_zddSymmProfile */
  131. /*---------------------------------------------------------------------------*/
  132. /* Definition of internal functions */
  133. /*---------------------------------------------------------------------------*/
  134. /**Function********************************************************************
  135. Synopsis [Checks for symmetry of x and y.]
  136. Description [Checks for symmetry of x and y. Ignores projection
  137. functions, unless they are isolated. Returns 1 in case of
  138. symmetry; 0 otherwise.]
  139. SideEffects [None]
  140. SeeAlso []
  141. ******************************************************************************/
  142. int
  143. cuddZddSymmCheck(
  144. DdManager * table,
  145. int x,
  146. int y)
  147. {
  148. int i;
  149. DdNode *f, *f0, *f1, *f01, *f00, *f11, *f10;
  150. int yindex;
  151. int xsymmy = 1;
  152. int xsymmyp = 1;
  153. int arccount = 0;
  154. int TotalRefCount = 0;
  155. int symm_found;
  156. empty = table->zero;
  157. yindex = table->invpermZ[y];
  158. for (i = table->subtableZ[x].slots - 1; i >= 0; i--) {
  159. f = table->subtableZ[x].nodelist[i];
  160. while (f != NULL) {
  161. /* Find f1, f0, f11, f10, f01, f00 */
  162. f1 = cuddT(f);
  163. f0 = cuddE(f);
  164. if ((int) f1->index == yindex) {
  165. f11 = cuddT(f1);
  166. f10 = cuddE(f1);
  167. if (f10 != empty)
  168. arccount++;
  169. } else {
  170. if ((int) f0->index != yindex) {
  171. return(0); /* f bypasses layer y */
  172. }
  173. f11 = empty;
  174. f10 = f1;
  175. }
  176. if ((int) f0->index == yindex) {
  177. f01 = cuddT(f0);
  178. f00 = cuddE(f0);
  179. if (f00 != empty)
  180. arccount++;
  181. } else {
  182. f01 = empty;
  183. f00 = f0;
  184. }
  185. if (f01 != f10)
  186. xsymmy = 0;
  187. if (f11 != f00)
  188. xsymmyp = 0;
  189. if ((xsymmy == 0) && (xsymmyp == 0))
  190. return(0);
  191. f = f->next;
  192. } /* for each element of the collision list */
  193. } /* for each slot of the subtable */
  194. /* Calculate the total reference counts of y
  195. ** whose else arc is not empty.
  196. */
  197. for (i = table->subtableZ[y].slots - 1; i >= 0; i--) {
  198. f = table->subtableZ[y].nodelist[i];
  199. while (f != NIL(DdNode)) {
  200. if (cuddE(f) != empty)
  201. TotalRefCount += f->ref;
  202. f = f->next;
  203. }
  204. }
  205. symm_found = (arccount == TotalRefCount);
  206. #if defined(DD_DEBUG) && defined(DD_VERBOSE)
  207. if (symm_found) {
  208. int xindex = table->invpermZ[x];
  209. (void) fprintf(table->out,
  210. "Found symmetry! x =%d\ty = %d\tPos(%d,%d)\n",
  211. xindex,yindex,x,y);
  212. }
  213. #endif
  214. return(symm_found);
  215. } /* end cuddZddSymmCheck */
  216. /**Function********************************************************************
  217. Synopsis [Symmetric sifting algorithm for ZDDs.]
  218. Description [Symmetric sifting algorithm.
  219. Assumes that no dead nodes are present.
  220. <ol>
  221. <li> Order all the variables according to the number of entries in
  222. each unique subtable.
  223. <li> Sift the variable up and down, remembering each time the total
  224. size of the ZDD heap and grouping variables that are symmetric.
  225. <li> Select the best permutation.
  226. <li> Repeat 3 and 4 for all variables.
  227. </ol>
  228. Returns 1 plus the number of symmetric variables if successful; 0
  229. otherwise.]
  230. SideEffects [None]
  231. SeeAlso [cuddZddSymmSiftingConv]
  232. ******************************************************************************/
  233. int
  234. cuddZddSymmSifting(
  235. DdManager * table,
  236. int lower,
  237. int upper)
  238. {
  239. int i;
  240. int *var;
  241. int nvars;
  242. int x;
  243. int result;
  244. int symvars;
  245. int symgroups;
  246. int iteration;
  247. #ifdef DD_STATS
  248. int previousSize;
  249. #endif
  250. nvars = table->sizeZ;
  251. /* Find order in which to sift variables. */
  252. var = NULL;
  253. zdd_entry = ALLOC(int, nvars);
  254. if (zdd_entry == NULL) {
  255. table->errorCode = CUDD_MEMORY_OUT;
  256. goto cuddZddSymmSiftingOutOfMem;
  257. }
  258. var = ALLOC(int, nvars);
  259. if (var == NULL) {
  260. table->errorCode = CUDD_MEMORY_OUT;
  261. goto cuddZddSymmSiftingOutOfMem;
  262. }
  263. for (i = 0; i < nvars; i++) {
  264. x = table->permZ[i];
  265. zdd_entry[i] = table->subtableZ[x].keys;
  266. var[i] = i;
  267. }
  268. qsort((void *)var, nvars, sizeof(int), (DD_QSFP)cuddZddUniqueCompare);
  269. /* Initialize the symmetry of each subtable to itself. */
  270. for (i = lower; i <= upper; i++)
  271. table->subtableZ[i].next = i;
  272. iteration = ddMin(table->siftMaxVar, nvars);
  273. for (i = 0; i < iteration; i++) {
  274. if (zddTotalNumberSwapping >= table->siftMaxSwap)
  275. break;
  276. if (util_cpu_time() - table->startTime > table->timeLimit) {
  277. table->autoDynZ = 0; /* prevent further reordering */
  278. break;
  279. }
  280. x = table->permZ[var[i]];
  281. #ifdef DD_STATS
  282. previousSize = table->keysZ;
  283. #endif
  284. if (x < lower || x > upper) continue;
  285. if (table->subtableZ[x].next == (unsigned) x) {
  286. result = cuddZddSymmSiftingAux(table, x, lower, upper);
  287. if (!result)
  288. goto cuddZddSymmSiftingOutOfMem;
  289. #ifdef DD_STATS
  290. if (table->keysZ < (unsigned) previousSize) {
  291. (void) fprintf(table->out,"-");
  292. } else if (table->keysZ > (unsigned) previousSize) {
  293. (void) fprintf(table->out,"+");
  294. #ifdef DD_VERBOSE
  295. (void) fprintf(table->out,"\nSize increased from %d to %d while sifting variable %d\n", previousSize, table->keysZ, var[i]);
  296. #endif
  297. } else {
  298. (void) fprintf(table->out,"=");
  299. }
  300. fflush(table->out);
  301. #endif
  302. }
  303. }
  304. FREE(var);
  305. FREE(zdd_entry);
  306. cuddZddSymmSummary(table, lower, upper, &symvars, &symgroups);
  307. #ifdef DD_STATS
  308. (void) fprintf(table->out,"\n#:S_SIFTING %8d: symmetric variables\n",symvars);
  309. (void) fprintf(table->out,"#:G_SIFTING %8d: symmetric groups\n",symgroups);
  310. #endif
  311. return(1+symvars);
  312. cuddZddSymmSiftingOutOfMem:
  313. if (zdd_entry != NULL)
  314. FREE(zdd_entry);
  315. if (var != NULL)
  316. FREE(var);
  317. return(0);
  318. } /* end of cuddZddSymmSifting */
  319. /**Function********************************************************************
  320. Synopsis [Symmetric sifting to convergence algorithm for ZDDs.]
  321. Description [Symmetric sifting to convergence algorithm for ZDDs.
  322. Assumes that no dead nodes are present.
  323. <ol>
  324. <li> Order all the variables according to the number of entries in
  325. each unique subtable.
  326. <li> Sift the variable up and down, remembering each time the total
  327. size of the ZDD heap and grouping variables that are symmetric.
  328. <li> Select the best permutation.
  329. <li> Repeat 3 and 4 for all variables.
  330. <li> Repeat 1-4 until no further improvement.
  331. </ol>
  332. Returns 1 plus the number of symmetric variables if successful; 0
  333. otherwise.]
  334. SideEffects [None]
  335. SeeAlso [cuddZddSymmSifting]
  336. ******************************************************************************/
  337. int
  338. cuddZddSymmSiftingConv(
  339. DdManager * table,
  340. int lower,
  341. int upper)
  342. {
  343. int i;
  344. int *var;
  345. int nvars;
  346. int initialSize;
  347. int x;
  348. int result;
  349. int symvars;
  350. int symgroups;
  351. int classes;
  352. int iteration;
  353. #ifdef DD_STATS
  354. int previousSize;
  355. #endif
  356. initialSize = table->keysZ;
  357. nvars = table->sizeZ;
  358. /* Find order in which to sift variables. */
  359. var = NULL;
  360. zdd_entry = ALLOC(int, nvars);
  361. if (zdd_entry == NULL) {
  362. table->errorCode = CUDD_MEMORY_OUT;
  363. goto cuddZddSymmSiftingConvOutOfMem;
  364. }
  365. var = ALLOC(int, nvars);
  366. if (var == NULL) {
  367. table->errorCode = CUDD_MEMORY_OUT;
  368. goto cuddZddSymmSiftingConvOutOfMem;
  369. }
  370. for (i = 0; i < nvars; i++) {
  371. x = table->permZ[i];
  372. zdd_entry[i] = table->subtableZ[x].keys;
  373. var[i] = i;
  374. }
  375. qsort((void *)var, nvars, sizeof(int), (DD_QSFP)cuddZddUniqueCompare);
  376. /* Initialize the symmetry of each subtable to itself
  377. ** for first pass of converging symmetric sifting.
  378. */
  379. for (i = lower; i <= upper; i++)
  380. table->subtableZ[i].next = i;
  381. iteration = ddMin(table->siftMaxVar, table->sizeZ);
  382. for (i = 0; i < iteration; i++) {
  383. if (zddTotalNumberSwapping >= table->siftMaxSwap)
  384. break;
  385. if (util_cpu_time() - table->startTime > table->timeLimit) {
  386. table->autoDynZ = 0; /* prevent further reordering */
  387. break;
  388. }
  389. x = table->permZ[var[i]];
  390. if (x < lower || x > upper) continue;
  391. /* Only sift if not in symmetry group already. */
  392. if (table->subtableZ[x].next == (unsigned) x) {
  393. #ifdef DD_STATS
  394. previousSize = table->keysZ;
  395. #endif
  396. result = cuddZddSymmSiftingAux(table, x, lower, upper);
  397. if (!result)
  398. goto cuddZddSymmSiftingConvOutOfMem;
  399. #ifdef DD_STATS
  400. if (table->keysZ < (unsigned) previousSize) {
  401. (void) fprintf(table->out,"-");
  402. } else if (table->keysZ > (unsigned) previousSize) {
  403. (void) fprintf(table->out,"+");
  404. #ifdef DD_VERBOSE
  405. (void) fprintf(table->out,"\nSize increased from %d to %d while sifting variable %d\n", previousSize, table->keysZ, var[i]);
  406. #endif
  407. } else {
  408. (void) fprintf(table->out,"=");
  409. }
  410. fflush(table->out);
  411. #endif
  412. }
  413. }
  414. /* Sifting now until convergence. */
  415. while ((unsigned) initialSize > table->keysZ) {
  416. initialSize = table->keysZ;
  417. #ifdef DD_STATS
  418. (void) fprintf(table->out,"\n");
  419. #endif
  420. /* Here we consider only one representative for each symmetry class. */
  421. for (x = lower, classes = 0; x <= upper; x++, classes++) {
  422. while ((unsigned) x < table->subtableZ[x].next)
  423. x = table->subtableZ[x].next;
  424. /* Here x is the largest index in a group.
  425. ** Groups consists of adjacent variables.
  426. ** Hence, the next increment of x will move it to a new group.
  427. */
  428. i = table->invpermZ[x];
  429. zdd_entry[i] = table->subtableZ[x].keys;
  430. var[classes] = i;
  431. }
  432. qsort((void *)var,classes,sizeof(int),(DD_QSFP)cuddZddUniqueCompare);
  433. /* Now sift. */
  434. iteration = ddMin(table->siftMaxVar, nvars);
  435. for (i = 0; i < iteration; i++) {
  436. if (zddTotalNumberSwapping >= table->siftMaxSwap)
  437. break;
  438. if (util_cpu_time() - table->startTime > table->timeLimit) {
  439. table->autoDynZ = 0; /* prevent further reordering */
  440. break;
  441. }
  442. x = table->permZ[var[i]];
  443. if ((unsigned) x >= table->subtableZ[x].next) {
  444. #ifdef DD_STATS
  445. previousSize = table->keysZ;
  446. #endif
  447. result = cuddZddSymmSiftingConvAux(table, x, lower, upper);
  448. if (!result)
  449. goto cuddZddSymmSiftingConvOutOfMem;
  450. #ifdef DD_STATS
  451. if (table->keysZ < (unsigned) previousSize) {
  452. (void) fprintf(table->out,"-");
  453. } else if (table->keysZ > (unsigned) previousSize) {
  454. (void) fprintf(table->out,"+");
  455. #ifdef DD_VERBOSE
  456. (void) fprintf(table->out,"\nSize increased from %d to %d while sifting variable %d\n", previousSize, table->keysZ, var[i]);
  457. #endif
  458. } else {
  459. (void) fprintf(table->out,"=");
  460. }
  461. fflush(table->out);
  462. #endif
  463. }
  464. } /* for */
  465. }
  466. cuddZddSymmSummary(table, lower, upper, &symvars, &symgroups);
  467. #ifdef DD_STATS
  468. (void) fprintf(table->out,"\n#:S_SIFTING %8d: symmetric variables\n",
  469. symvars);
  470. (void) fprintf(table->out,"#:G_SIFTING %8d: symmetric groups\n",
  471. symgroups);
  472. #endif
  473. FREE(var);
  474. FREE(zdd_entry);
  475. return(1+symvars);
  476. cuddZddSymmSiftingConvOutOfMem:
  477. if (zdd_entry != NULL)
  478. FREE(zdd_entry);
  479. if (var != NULL)
  480. FREE(var);
  481. return(0);
  482. } /* end of cuddZddSymmSiftingConv */
  483. /*---------------------------------------------------------------------------*/
  484. /* Definition of static functions */
  485. /*---------------------------------------------------------------------------*/
  486. /**Function********************************************************************
  487. Synopsis [Given x_low <= x <= x_high moves x up and down between the
  488. boundaries.]
  489. Description [Given x_low <= x <= x_high moves x up and down between the
  490. boundaries. Finds the best position and does the required changes.
  491. Assumes that x is not part of a symmetry group. Returns 1 if
  492. successful; 0 otherwise.]
  493. SideEffects [None]
  494. SeeAlso []
  495. ******************************************************************************/
  496. static int
  497. cuddZddSymmSiftingAux(
  498. DdManager * table,
  499. int x,
  500. int x_low,
  501. int x_high)
  502. {
  503. Move *move;
  504. Move *move_up; /* list of up move */
  505. Move *move_down; /* list of down move */
  506. int initial_size;
  507. int result;
  508. int i;
  509. int topbot; /* index to either top or bottom of symmetry group */
  510. int init_group_size, final_group_size;
  511. initial_size = table->keysZ;
  512. move_down = NULL;
  513. move_up = NULL;
  514. /* Look for consecutive symmetries above x. */
  515. for (i = x; i > x_low; i--) {
  516. if (!cuddZddSymmCheck(table, i - 1, i))
  517. break;
  518. /* find top of i-1's symmetry */
  519. topbot = table->subtableZ[i - 1].next;
  520. table->subtableZ[i - 1].next = i;
  521. table->subtableZ[x].next = topbot;
  522. /* x is bottom of group so its symmetry is top of i-1's
  523. group */
  524. i = topbot + 1; /* add 1 for i--, new i is top of symm group */
  525. }
  526. /* Look for consecutive symmetries below x. */
  527. for (i = x; i < x_high; i++) {
  528. if (!cuddZddSymmCheck(table, i, i + 1))
  529. break;
  530. /* find bottom of i+1's symm group */
  531. topbot = i + 1;
  532. while ((unsigned) topbot < table->subtableZ[topbot].next)
  533. topbot = table->subtableZ[topbot].next;
  534. table->subtableZ[topbot].next = table->subtableZ[i].next;
  535. table->subtableZ[i].next = i + 1;
  536. i = topbot - 1; /* add 1 for i++,
  537. new i is bottom of symm group */
  538. }
  539. /* Now x maybe in the middle of a symmetry group. */
  540. if (x == x_low) { /* Sift down */
  541. /* Find bottom of x's symm group */
  542. while ((unsigned) x < table->subtableZ[x].next)
  543. x = table->subtableZ[x].next;
  544. i = table->subtableZ[x].next;
  545. init_group_size = x - i + 1;
  546. move_down = cuddZddSymmSifting_down(table, x, x_high,
  547. initial_size);
  548. /* after that point x --> x_high, unless early term */
  549. if (move_down == ZDD_MV_OOM)
  550. goto cuddZddSymmSiftingAuxOutOfMem;
  551. if (move_down == NULL ||
  552. table->subtableZ[move_down->y].next != move_down->y) {
  553. /* symmetry detected may have to make another complete
  554. pass */
  555. if (move_down != NULL)
  556. x = move_down->y;
  557. else
  558. x = table->subtableZ[x].next;
  559. i = x;
  560. while ((unsigned) i < table->subtableZ[i].next) {
  561. i = table->subtableZ[i].next;
  562. }
  563. final_group_size = i - x + 1;
  564. if (init_group_size == final_group_size) {
  565. /* No new symmetry groups detected,
  566. return to best position */
  567. result = cuddZddSymmSiftingBackward(table,
  568. move_down, initial_size);
  569. }
  570. else {
  571. initial_size = table->keysZ;
  572. move_up = cuddZddSymmSifting_up(table, x, x_low,
  573. initial_size);
  574. result = cuddZddSymmSiftingBackward(table, move_up,
  575. initial_size);
  576. }
  577. }
  578. else {
  579. result = cuddZddSymmSiftingBackward(table, move_down,
  580. initial_size);
  581. /* move backward and stop at best position */
  582. }
  583. if (!result)
  584. goto cuddZddSymmSiftingAuxOutOfMem;
  585. }
  586. else if (x == x_high) { /* Sift up */
  587. /* Find top of x's symm group */
  588. while ((unsigned) x < table->subtableZ[x].next)
  589. x = table->subtableZ[x].next;
  590. x = table->subtableZ[x].next;
  591. i = x;
  592. while ((unsigned) i < table->subtableZ[i].next) {
  593. i = table->subtableZ[i].next;
  594. }
  595. init_group_size = i - x + 1;
  596. move_up = cuddZddSymmSifting_up(table, x, x_low, initial_size);
  597. /* after that point x --> x_low, unless early term */
  598. if (move_up == ZDD_MV_OOM)
  599. goto cuddZddSymmSiftingAuxOutOfMem;
  600. if (move_up == NULL ||
  601. table->subtableZ[move_up->x].next != move_up->x) {
  602. /* symmetry detected may have to make another complete
  603. pass */
  604. if (move_up != NULL)
  605. x = move_up->x;
  606. else {
  607. while ((unsigned) x < table->subtableZ[x].next)
  608. x = table->subtableZ[x].next;
  609. }
  610. i = table->subtableZ[x].next;
  611. final_group_size = x - i + 1;
  612. if (init_group_size == final_group_size) {
  613. /* No new symmetry groups detected,
  614. return to best position */
  615. result = cuddZddSymmSiftingBackward(table, move_up,
  616. initial_size);
  617. }
  618. else {
  619. initial_size = table->keysZ;
  620. move_down = cuddZddSymmSifting_down(table, x, x_high,
  621. initial_size);
  622. result = cuddZddSymmSiftingBackward(table, move_down,
  623. initial_size);
  624. }
  625. }
  626. else {
  627. result = cuddZddSymmSiftingBackward(table, move_up,
  628. initial_size);
  629. /* move backward and stop at best position */
  630. }
  631. if (!result)
  632. goto cuddZddSymmSiftingAuxOutOfMem;
  633. }
  634. else if ((x - x_low) > (x_high - x)) { /* must go down first:
  635. shorter */
  636. /* Find bottom of x's symm group */
  637. while ((unsigned) x < table->subtableZ[x].next)
  638. x = table->subtableZ[x].next;
  639. move_down = cuddZddSymmSifting_down(table, x, x_high,
  640. initial_size);
  641. /* after that point x --> x_high, unless early term */
  642. if (move_down == ZDD_MV_OOM)
  643. goto cuddZddSymmSiftingAuxOutOfMem;
  644. if (move_down != NULL) {
  645. x = move_down->y;
  646. }
  647. else {
  648. x = table->subtableZ[x].next;
  649. }
  650. i = x;
  651. while ((unsigned) i < table->subtableZ[i].next) {
  652. i = table->subtableZ[i].next;
  653. }
  654. init_group_size = i - x + 1;
  655. move_up = cuddZddSymmSifting_up(table, x, x_low, initial_size);
  656. if (move_up == ZDD_MV_OOM)
  657. goto cuddZddSymmSiftingAuxOutOfMem;
  658. if (move_up == NULL ||
  659. table->subtableZ[move_up->x].next != move_up->x) {
  660. /* symmetry detected may have to make another complete
  661. pass */
  662. if (move_up != NULL) {
  663. x = move_up->x;
  664. }
  665. else {
  666. while ((unsigned) x < table->subtableZ[x].next)
  667. x = table->subtableZ[x].next;
  668. }
  669. i = table->subtableZ[x].next;
  670. final_group_size = x - i + 1;
  671. if (init_group_size == final_group_size) {
  672. /* No new symmetry groups detected,
  673. return to best position */
  674. result = cuddZddSymmSiftingBackward(table, move_up,
  675. initial_size);
  676. }
  677. else {
  678. while (move_down != NULL) {
  679. move = move_down->next;
  680. cuddDeallocMove(table, move_down);
  681. move_down = move;
  682. }
  683. initial_size = table->keysZ;
  684. move_down = cuddZddSymmSifting_down(table, x, x_high,
  685. initial_size);
  686. result = cuddZddSymmSiftingBackward(table, move_down,
  687. initial_size);
  688. }
  689. }
  690. else {
  691. result = cuddZddSymmSiftingBackward(table, move_up,
  692. initial_size);
  693. /* move backward and stop at best position */
  694. }
  695. if (!result)
  696. goto cuddZddSymmSiftingAuxOutOfMem;
  697. }
  698. else { /* moving up first:shorter */
  699. /* Find top of x's symmetry group */
  700. while ((unsigned) x < table->subtableZ[x].next)
  701. x = table->subtableZ[x].next;
  702. x = table->subtableZ[x].next;
  703. move_up = cuddZddSymmSifting_up(table, x, x_low, initial_size);
  704. /* after that point x --> x_high, unless early term */
  705. if (move_up == ZDD_MV_OOM)
  706. goto cuddZddSymmSiftingAuxOutOfMem;
  707. if (move_up != NULL) {
  708. x = move_up->x;
  709. }
  710. else {
  711. while ((unsigned) x < table->subtableZ[x].next)
  712. x = table->subtableZ[x].next;
  713. }
  714. i = table->subtableZ[x].next;
  715. init_group_size = x - i + 1;
  716. move_down = cuddZddSymmSifting_down(table, x, x_high,
  717. initial_size);
  718. if (move_down == ZDD_MV_OOM)
  719. goto cuddZddSymmSiftingAuxOutOfMem;
  720. if (move_down == NULL ||
  721. table->subtableZ[move_down->y].next != move_down->y) {
  722. /* symmetry detected may have to make another complete
  723. pass */
  724. if (move_down != NULL) {
  725. x = move_down->y;
  726. }
  727. else {
  728. x = table->subtableZ[x].next;
  729. }
  730. i = x;
  731. while ((unsigned) i < table->subtableZ[i].next) {
  732. i = table->subtableZ[i].next;
  733. }
  734. final_group_size = i - x + 1;
  735. if (init_group_size == final_group_size) {
  736. /* No new symmetries detected,
  737. go back to best position */
  738. result = cuddZddSymmSiftingBackward(table, move_down,
  739. initial_size);
  740. }
  741. else {
  742. while (move_up != NULL) {
  743. move = move_up->next;
  744. cuddDeallocMove(table, move_up);
  745. move_up = move;
  746. }
  747. initial_size = table->keysZ;
  748. move_up = cuddZddSymmSifting_up(table, x, x_low,
  749. initial_size);
  750. result = cuddZddSymmSiftingBackward(table, move_up,
  751. initial_size);
  752. }
  753. }
  754. else {
  755. result = cuddZddSymmSiftingBackward(table, move_down,
  756. initial_size);
  757. /* move backward and stop at best position */
  758. }
  759. if (!result)
  760. goto cuddZddSymmSiftingAuxOutOfMem;
  761. }
  762. while (move_down != NULL) {
  763. move = move_down->next;
  764. cuddDeallocMove(table, move_down);
  765. move_down = move;
  766. }
  767. while (move_up != NULL) {
  768. move = move_up->next;
  769. cuddDeallocMove(table, move_up);
  770. move_up = move;
  771. }
  772. return(1);
  773. cuddZddSymmSiftingAuxOutOfMem:
  774. if (move_down != ZDD_MV_OOM) {
  775. while (move_down != NULL) {
  776. move = move_down->next;
  777. cuddDeallocMove(table, move_down);
  778. move_down = move;
  779. }
  780. }
  781. if (move_up != ZDD_MV_OOM) {
  782. while (move_up != NULL) {
  783. move = move_up->next;
  784. cuddDeallocMove(table, move_up);
  785. move_up = move;
  786. }
  787. }
  788. return(0);
  789. } /* end of cuddZddSymmSiftingAux */
  790. /**Function********************************************************************
  791. Synopsis [Given x_low <= x <= x_high moves x up and down between the
  792. boundaries.]
  793. Description [Given x_low <= x <= x_high moves x up and down between the
  794. boundaries. Finds the best position and does the required changes.
  795. Assumes that x is either an isolated variable, or it is the bottom of
  796. a symmetry group. All symmetries may not have been found, because of
  797. exceeded growth limit. Returns 1 if successful; 0 otherwise.]
  798. SideEffects [None]
  799. SeeAlso []
  800. ******************************************************************************/
  801. static int
  802. cuddZddSymmSiftingConvAux(
  803. DdManager * table,
  804. int x,
  805. int x_low,
  806. int x_high)
  807. {
  808. Move *move;
  809. Move *move_up; /* list of up move */
  810. Move *move_down; /* list of down move */
  811. int initial_size;
  812. int result;
  813. int i;
  814. int init_group_size, final_group_size;
  815. initial_size = table->keysZ;
  816. move_down = NULL;
  817. move_up = NULL;
  818. if (x == x_low) { /* Sift down */
  819. i = table->subtableZ[x].next;
  820. init_group_size = x - i + 1;
  821. move_down = cuddZddSymmSifting_down(table, x, x_high,
  822. initial_size);
  823. /* after that point x --> x_high, unless early term */
  824. if (move_down == ZDD_MV_OOM)
  825. goto cuddZddSymmSiftingConvAuxOutOfMem;
  826. if (move_down == NULL ||
  827. table->subtableZ[move_down->y].next != move_down->y) {
  828. /* symmetry detected may have to make another complete
  829. pass */
  830. if (move_down != NULL)
  831. x = move_down->y;
  832. else {
  833. while ((unsigned) x < table->subtableZ[x].next)
  834. x = table->subtableZ[x].next;
  835. x = table->subtableZ[x].next;
  836. }
  837. i = x;
  838. while ((unsigned) i < table->subtableZ[i].next) {
  839. i = table->subtableZ[i].next;
  840. }
  841. final_group_size = i - x + 1;
  842. if (init_group_size == final_group_size) {
  843. /* No new symmetries detected,
  844. go back to best position */
  845. result = cuddZddSymmSiftingBackward(table, move_down,
  846. initial_size);
  847. }
  848. else {
  849. initial_size = table->keysZ;
  850. move_up = cuddZddSymmSifting_up(table, x, x_low,
  851. initial_size);
  852. result = cuddZddSymmSiftingBackward(table, move_up,
  853. initial_size);
  854. }
  855. }
  856. else {
  857. result = cuddZddSymmSiftingBackward(table, move_down,
  858. initial_size);
  859. /* move backward and stop at best position */
  860. }
  861. if (!result)
  862. goto cuddZddSymmSiftingConvAuxOutOfMem;
  863. }
  864. else if (x == x_high) { /* Sift up */
  865. /* Find top of x's symm group */
  866. while ((unsigned) x < table->subtableZ[x].next)
  867. x = table->subtableZ[x].next;
  868. x = table->subtableZ[x].next;
  869. i = x;
  870. while ((unsigned) i < table->subtableZ[i].next) {
  871. i = table->subtableZ[i].next;
  872. }
  873. init_group_size = i - x + 1;
  874. move_up = cuddZddSymmSifting_up(table, x, x_low, initial_size);
  875. /* after that point x --> x_low, unless early term */
  876. if (move_up == ZDD_MV_OOM)
  877. goto cuddZddSymmSiftingConvAuxOutOfMem;
  878. if (move_up == NULL ||
  879. table->subtableZ[move_up->x].next != move_up->x) {
  880. /* symmetry detected may have to make another complete
  881. pass */
  882. if (move_up != NULL)
  883. x = move_up->x;
  884. else {
  885. while ((unsigned) x < table->subtableZ[x].next)
  886. x = table->subtableZ[x].next;
  887. }
  888. i = table->subtableZ[x].next;
  889. final_group_size = x - i + 1;
  890. if (init_group_size == final_group_size) {
  891. /* No new symmetry groups detected,
  892. return to best position */
  893. result = cuddZddSymmSiftingBackward(table, move_up,
  894. initial_size);
  895. }
  896. else {
  897. initial_size = table->keysZ;
  898. move_down = cuddZddSymmSifting_down(table, x, x_high,
  899. initial_size);
  900. result = cuddZddSymmSiftingBackward(table, move_down,
  901. initial_size);
  902. }
  903. }
  904. else {
  905. result = cuddZddSymmSiftingBackward(table, move_up,
  906. initial_size);
  907. /* move backward and stop at best position */
  908. }
  909. if (!result)
  910. goto cuddZddSymmSiftingConvAuxOutOfMem;
  911. }
  912. else if ((x - x_low) > (x_high - x)) { /* must go down first:
  913. shorter */
  914. move_down = cuddZddSymmSifting_down(table, x, x_high,
  915. initial_size);
  916. /* after that point x --> x_high */
  917. if (move_down == ZDD_MV_OOM)
  918. goto cuddZddSymmSiftingConvAuxOutOfMem;
  919. if (move_down != NULL) {
  920. x = move_down->y;
  921. }
  922. else {
  923. while ((unsigned) x < table->subtableZ[x].next)
  924. x = table->subtableZ[x].next;
  925. x = table->subtableZ[x].next;
  926. }
  927. i = x;
  928. while ((unsigned) i < table->subtableZ[i].next) {
  929. i = table->subtableZ[i].next;
  930. }
  931. init_group_size = i - x + 1;
  932. move_up = cuddZddSymmSifting_up(table, x, x_low, initial_size);
  933. if (move_up == ZDD_MV_OOM)
  934. goto cuddZddSymmSiftingConvAuxOutOfMem;
  935. if (move_up == NULL ||
  936. table->subtableZ[move_up->x].next != move_up->x) {
  937. /* symmetry detected may have to make another complete
  938. pass */
  939. if (move_up != NULL) {
  940. x = move_up->x;
  941. }
  942. else {
  943. while ((unsigned) x < table->subtableZ[x].next)
  944. x = table->subtableZ[x].next;
  945. }
  946. i = table->subtableZ[x].next;
  947. final_group_size = x - i + 1;
  948. if (init_group_size == final_group_size) {
  949. /* No new symmetry groups detected,
  950. return to best position */
  951. result = cuddZddSymmSiftingBackward(table, move_up,
  952. initial_size);
  953. }
  954. else {
  955. while (move_down != NULL) {
  956. move = move_down->next;
  957. cuddDeallocMove(table, move_down);
  958. move_down = move;
  959. }
  960. initial_size = table->keysZ;
  961. move_down = cuddZddSymmSifting_down(table, x, x_high,
  962. initial_size);
  963. result = cuddZddSymmSiftingBackward(table, move_down,
  964. initial_size);
  965. }
  966. }
  967. else {
  968. result = cuddZddSymmSiftingBackward(table, move_up,
  969. initial_size);
  970. /* move backward and stop at best position */
  971. }
  972. if (!result)
  973. goto cuddZddSymmSiftingConvAuxOutOfMem;
  974. }
  975. else { /* moving up first:shorter */
  976. /* Find top of x's symmetry group */
  977. x = table->subtableZ[x].next;
  978. move_up = cuddZddSymmSifting_up(table, x, x_low, initial_size);
  979. /* after that point x --> x_high, unless early term */
  980. if (move_up == ZDD_MV_OOM)
  981. goto cuddZddSymmSiftingConvAuxOutOfMem;
  982. if (move_up != NULL) {
  983. x = move_up->x;
  984. }
  985. else {
  986. while ((unsigned) x < table->subtableZ[x].next)
  987. x = table->subtableZ[x].next;
  988. }
  989. i = table->subtableZ[x].next;
  990. init_group_size = x - i + 1;
  991. move_down = cuddZddSymmSifting_down(table, x, x_high,
  992. initial_size);
  993. if (move_down == ZDD_MV_OOM)
  994. goto cuddZddSymmSiftingConvAuxOutOfMem;
  995. if (move_down == NULL ||
  996. table->subtableZ[move_down->y].next != move_down->y) {
  997. /* symmetry detected may have to make another complete
  998. pass */
  999. if (move_down != NULL) {
  1000. x = move_down->y;
  1001. }
  1002. else {
  1003. while ((unsigned) x < table->subtableZ[x].next)
  1004. x = table->subtableZ[x].next;
  1005. x = table->subtableZ[x].next;
  1006. }
  1007. i = x;
  1008. while ((unsigned) i < table->subtableZ[i].next) {
  1009. i = table->subtableZ[i].next;
  1010. }
  1011. final_group_size = i - x + 1;
  1012. if (init_group_size == final_group_size) {
  1013. /* No new symmetries detected,
  1014. go back to best position */
  1015. result = cuddZddSymmSiftingBackward(table, move_down,
  1016. initial_size);
  1017. }
  1018. else {
  1019. while (move_up != NULL) {
  1020. move = move_up->next;
  1021. cuddDeallocMove(table, move_up);
  1022. move_up = move;
  1023. }
  1024. initial_size = table->keysZ;
  1025. move_up = cuddZddSymmSifting_up(table, x, x_low,
  1026. initial_size);
  1027. result = cuddZddSymmSiftingBackward(table, move_up,
  1028. initial_size);
  1029. }
  1030. }
  1031. else {
  1032. result = cuddZddSymmSiftingBackward(table, move_down,
  1033. initial_size);
  1034. /* move backward and stop at best position */
  1035. }
  1036. if (!result)
  1037. goto cuddZddSymmSiftingConvAuxOutOfMem;
  1038. }
  1039. while (move_down != NULL) {
  1040. move = move_down->next;
  1041. cuddDeallocMove(table, move_down);
  1042. move_down = move;
  1043. }
  1044. while (move_up != NULL) {
  1045. move = move_up->next;
  1046. cuddDeallocMove(table, move_up);
  1047. move_up = move;
  1048. }
  1049. return(1);
  1050. cuddZddSymmSiftingConvAuxOutOfMem:
  1051. if (move_down != ZDD_MV_OOM) {
  1052. while (move_down != NULL) {
  1053. move = move_down->next;
  1054. cuddDeallocMove(table, move_down);
  1055. move_down = move;
  1056. }
  1057. }
  1058. if (move_up != ZDD_MV_OOM) {
  1059. while (move_up != NULL) {
  1060. move = move_up->next;
  1061. cuddDeallocMove(table, move_up);
  1062. move_up = move;
  1063. }
  1064. }
  1065. return(0);
  1066. } /* end of cuddZddSymmSiftingConvAux */
  1067. /**Function********************************************************************
  1068. Synopsis [Moves x up until either it reaches the bound (x_low) or
  1069. the size of the ZDD heap increases too much.]
  1070. Description [Moves x up until either it reaches the bound (x_low) or
  1071. the size of the ZDD heap increases too much. Assumes that x is the top
  1072. of a symmetry group. Checks x for symmetry to the adjacent
  1073. variables. If symmetry is found, the symmetry group of x is merged
  1074. with the symmetry group of the other variable. Returns the set of
  1075. moves in case of success; ZDD_MV_OOM if memory is full.]
  1076. SideEffects [None]
  1077. SeeAlso []
  1078. ******************************************************************************/
  1079. static Move *
  1080. cuddZddSymmSifting_up(
  1081. DdManager * table,
  1082. int x,
  1083. int x_low,
  1084. int initial_size)
  1085. {
  1086. Move *moves;
  1087. Move *move;
  1088. int y;
  1089. int size;
  1090. int limit_size = initial_size;
  1091. int i, gytop;
  1092. moves = NULL;
  1093. y = cuddZddNextLow(table, x);
  1094. while (y >= x_low) {
  1095. gytop = table->subtableZ[y].next;
  1096. if (cuddZddSymmCheck(table, y, x)) {
  1097. /* Symmetry found, attach symm groups */
  1098. table->subtableZ[y].next = x;
  1099. i = table->subtableZ[x].next;
  1100. while (table->subtableZ[i].next != (unsigned) x)
  1101. i = table->subtableZ[i].next;
  1102. table->subtableZ[i].next = gytop;
  1103. }
  1104. else if ((table->subtableZ[x].next == (unsigned) x) &&
  1105. (table->subtableZ[y].next == (unsigned) y)) {
  1106. /* x and y have self symmetry */
  1107. size = cuddZddSwapInPlace(table, y, x);
  1108. if (size == 0)
  1109. goto cuddZddSymmSifting_upOutOfMem;
  1110. move = (Move *)cuddDynamicAllocNode(table);
  1111. if (move == NULL)
  1112. goto cuddZddSymmSifting_upOutOfMem;
  1113. move->x = y;
  1114. move->y = x;
  1115. move->size = size;
  1116. move->next = moves;
  1117. moves = move;
  1118. if ((double)size >
  1119. (double)limit_size * table->maxGrowth)
  1120. return(moves);
  1121. if (size < limit_size)
  1122. limit_size = size;
  1123. }
  1124. else { /* Group move */
  1125. size = zdd_group_move(table, y, x, &moves);
  1126. if ((double)size >
  1127. (double)limit_size * table->maxGrowth)
  1128. return(moves);
  1129. if (size < limit_size)
  1130. limit_size = size;
  1131. }
  1132. x = gytop;
  1133. y = cuddZddNextLow(table, x);
  1134. }
  1135. return(moves);
  1136. cuddZddSymmSifting_upOutOfMem:
  1137. while (moves != NULL) {
  1138. move = moves->next;
  1139. cuddDeallocMove(table, moves);
  1140. moves = move;
  1141. }
  1142. return(ZDD_MV_OOM);
  1143. } /* end of cuddZddSymmSifting_up */
  1144. /**Function********************************************************************
  1145. Synopsis [Moves x down until either it reaches the bound (x_high) or
  1146. the size of the ZDD heap increases too much.]
  1147. Description [Moves x down until either it reaches the bound (x_high)
  1148. or the size of the ZDD heap increases too much. Assumes that x is the
  1149. bottom of a symmetry group. Checks x for symmetry to the adjacent
  1150. variables. If symmetry is found, the symmetry group of x is merged
  1151. with the symmetry group of the other variable. Returns the set of
  1152. moves in case of success; ZDD_MV_OOM if memory is full.]
  1153. SideEffects [None]
  1154. SeeAlso []
  1155. ******************************************************************************/
  1156. static Move *
  1157. cuddZddSymmSifting_down(
  1158. DdManager * table,
  1159. int x,
  1160. int x_high,
  1161. int initial_size)
  1162. {
  1163. Move *moves;
  1164. Move *move;
  1165. int y;
  1166. int size;
  1167. int limit_size = initial_size;
  1168. int i, gxtop, gybot;
  1169. moves = NULL;
  1170. y = cuddZddNextHigh(table, x);
  1171. while (y <= x_high) {
  1172. gybot = table->subtableZ[y].next;
  1173. while (table->subtableZ[gybot].next != (unsigned) y)
  1174. gybot = table->subtableZ[gybot].next;
  1175. if (cuddZddSymmCheck(table, x, y)) {
  1176. /* Symmetry found, attach symm groups */
  1177. gxtop = table->subtableZ[x].next;
  1178. table->subtableZ[x].next = y;
  1179. i = table->subtableZ[y].next;
  1180. while (table->subtableZ[i].next != (unsigned) y)
  1181. i = table->subtableZ[i].next;
  1182. table->subtableZ[i].next = gxtop;
  1183. }
  1184. else if ((table->subtableZ[x].next == (unsigned) x) &&
  1185. (table->subtableZ[y].next == (unsigned) y)) {
  1186. /* x and y have self symmetry */
  1187. size = cuddZddSwapInPlace(table, x, y);
  1188. if (size == 0)
  1189. goto cuddZddSymmSifting_downOutOfMem;
  1190. move = (Move *)cuddDynamicAllocNode(table);
  1191. if (move == NULL)
  1192. goto cuddZddSymmSifting_downOutOfMem;
  1193. move->x = x;
  1194. move->y = y;
  1195. move->size = size;
  1196. move->next = moves;
  1197. moves = move;
  1198. if ((double)size >
  1199. (double)limit_size * table->maxGrowth)
  1200. return(moves);
  1201. if (size < limit_size)
  1202. limit_size = size;
  1203. x = y;
  1204. y = cuddZddNextHigh(table, x);
  1205. }
  1206. else { /* Group move */
  1207. size = zdd_group_move(table, x, y, &moves);
  1208. if ((double)size >
  1209. (double)limit_size * table->maxGrowth)
  1210. return(moves);
  1211. if (size < limit_size)
  1212. limit_size = size;
  1213. }
  1214. x = gybot;
  1215. y = cuddZddNextHigh(table, x);
  1216. }
  1217. return(moves);
  1218. cuddZddSymmSifting_downOutOfMem:
  1219. while (moves != NULL) {
  1220. move = moves->next;
  1221. cuddDeallocMove(table, moves);
  1222. moves = move;
  1223. }
  1224. return(ZDD_MV_OOM);
  1225. } /* end of cuddZddSymmSifting_down */
  1226. /**Function********************************************************************
  1227. Synopsis [Given a set of moves, returns the ZDD heap to the position
  1228. giving the minimum size.]
  1229. Description [Given a set of moves, returns the ZDD heap to the
  1230. position giving the minimum size. In case of ties, returns to the
  1231. closest position giving the minimum size. Returns 1 in case of
  1232. success; 0 otherwise.]
  1233. SideEffects [None]
  1234. SeeAlso []
  1235. ******************************************************************************/
  1236. static int
  1237. cuddZddSymmSiftingBackward(
  1238. DdManager * table,
  1239. Move * moves,
  1240. int size)
  1241. {
  1242. int i;
  1243. int i_best;
  1244. Move *move;
  1245. int res;
  1246. i_best = -1;
  1247. for (move = moves, i = 0; move != NULL; move = move->next, i++) {
  1248. if (move->size < size) {
  1249. i_best = i;
  1250. size = move->size;
  1251. }
  1252. }
  1253. for (move = moves, i = 0; move != NULL; move = move->next, i++) {
  1254. if (i == i_best) break;
  1255. if ((table->subtableZ[move->x].next == move->x) &&
  1256. (table->subtableZ[move->y].next == move->y)) {
  1257. res = cuddZddSwapInPlace(table, move->x, move->y);
  1258. if (!res) return(0);
  1259. }
  1260. else { /* Group move necessary */
  1261. res = zdd_group_move_backward(table, move->x, move->y);
  1262. }
  1263. if (i_best == -1 && res == size)
  1264. break;
  1265. }
  1266. return(1);
  1267. } /* end of cuddZddSymmSiftingBackward */
  1268. /**Function********************************************************************
  1269. Synopsis [Swaps two groups.]
  1270. Description [Swaps two groups. x is assumed to be the bottom variable
  1271. of the first group. y is assumed to be the top variable of the second
  1272. group. Updates the list of moves. Returns the number of keys in the
  1273. table if successful; 0 otherwise.]
  1274. SideEffects [None]
  1275. SeeAlso []
  1276. ******************************************************************************/
  1277. static int
  1278. zdd_group_move(
  1279. DdManager * table,
  1280. int x,
  1281. int y,
  1282. Move ** moves)
  1283. {
  1284. Move *move;
  1285. int size;
  1286. int i, temp, gxtop, gxbot, gybot, yprev;
  1287. int swapx, swapy;
  1288. #ifdef DD_DEBUG
  1289. assert(x < y); /* we assume that x < y */
  1290. #endif
  1291. /* Find top and bottom for the two groups. */
  1292. gxtop = table->subtableZ[x].next;
  1293. gxbot = x;
  1294. gybot = table->subtableZ[y].next;
  1295. while (table->subtableZ[gybot].next != (unsigned) y)
  1296. gybot = table->subtableZ[gybot].next;
  1297. yprev = gybot;
  1298. while (x <= y) {
  1299. while (y > gxtop) {
  1300. /* Set correct symmetries. */
  1301. temp = table->subtableZ[x].next;
  1302. if (temp == x)
  1303. temp = y;
  1304. i = gxtop;
  1305. for (;;) {
  1306. if (table->subtableZ[i].next == (unsigned) x) {
  1307. table->subtableZ[i].next = y;
  1308. break;
  1309. } else {
  1310. i = table->subtableZ[i].next;
  1311. }
  1312. }
  1313. if (table->subtableZ[y].next != (unsigned) y) {
  1314. table->subtableZ[x].next = table->subtableZ[y].next;
  1315. } else {
  1316. table->subtableZ[x].next = x;
  1317. }
  1318. if (yprev != y) {
  1319. table->subtableZ[yprev].next = x;
  1320. } else {
  1321. yprev = x;
  1322. }
  1323. table->subtableZ[y].next = temp;
  1324. size = cuddZddSwapInPlace(table, x, y);
  1325. if (size == 0)
  1326. goto zdd_group_moveOutOfMem;
  1327. swapx = x;
  1328. swapy = y;
  1329. y = x;
  1330. x--;
  1331. } /* while y > gxtop */
  1332. /* Trying to find the next y. */
  1333. if (table->subtableZ[y].next <= (unsigned) y) {
  1334. gybot = y;
  1335. } else {
  1336. y = table->subtableZ[y].next;
  1337. }
  1338. yprev = gxtop;
  1339. gxtop++;
  1340. gxbot++;
  1341. x = gxbot;
  1342. } /* while x <= y, end of group movement */
  1343. move = (Move *)cuddDynamicAllocNode(table);
  1344. if (move == NULL)
  1345. goto zdd_group_moveOutOfMem;
  1346. move->x = swapx;
  1347. move->y = swapy;
  1348. move->size = table->keysZ;
  1349. move->next = *moves;
  1350. *moves = move;
  1351. return(table->keysZ);
  1352. zdd_group_moveOutOfMem:
  1353. while (*moves != NULL) {
  1354. move = (*moves)->next;
  1355. cuddDeallocMove(table, *moves);
  1356. *moves = move;
  1357. }
  1358. return(0);
  1359. } /* end of zdd_group_move */
  1360. /**Function********************************************************************
  1361. Synopsis [Undoes the swap of two groups.]
  1362. Description [Undoes the swap of two groups. x is assumed to be the
  1363. bottom variable of the first group. y is assumed to be the top
  1364. variable of the second group. Returns 1 if successful; 0 otherwise.]
  1365. SideEffects [None]
  1366. SeeAlso []
  1367. ******************************************************************************/
  1368. static int
  1369. zdd_group_move_backward(
  1370. DdManager * table,
  1371. int x,
  1372. int y)
  1373. {
  1374. int size;
  1375. int i, temp, gxtop, gxbot, gybot, yprev;
  1376. #ifdef DD_DEBUG
  1377. assert(x < y); /* we assume that x < y */
  1378. #endif
  1379. /* Find top and bottom of the two groups. */
  1380. gxtop = table->subtableZ[x].next;
  1381. gxbot = x;
  1382. gybot = table->subtableZ[y].next;
  1383. while (table->subtableZ[gybot].next != (unsigned) y)
  1384. gybot = table->subtableZ[gybot].next;
  1385. yprev = gybot;
  1386. while (x <= y) {
  1387. while (y > gxtop) {
  1388. /* Set correct symmetries. */
  1389. temp = table->subtableZ[x].next;
  1390. if (temp == x)
  1391. temp = y;
  1392. i = gxtop;
  1393. for (;;) {
  1394. if (table->subtableZ[i].next == (unsigned) x) {
  1395. table->subtableZ[i].next = y;
  1396. break;
  1397. } else {
  1398. i = table->subtableZ[i].next;
  1399. }
  1400. }
  1401. if (table->subtableZ[y].next != (unsigned) y) {
  1402. table->subtableZ[x].next = table->subtableZ[y].next;
  1403. } else {
  1404. table->subtableZ[x].next = x;
  1405. }
  1406. if (yprev != y) {
  1407. table->subtableZ[yprev].next = x;
  1408. } else {
  1409. yprev = x;
  1410. }
  1411. table->subtableZ[y].next = temp;
  1412. size = cuddZddSwapInPlace(table, x, y);
  1413. if (size == 0)
  1414. return(0);
  1415. y = x;
  1416. x--;
  1417. } /* while y > gxtop */
  1418. /* Trying to find the next y. */
  1419. if (table->subtableZ[y].next <= (unsigned) y) {
  1420. gybot = y;
  1421. } else {
  1422. y = table->subtableZ[y].next;
  1423. }
  1424. yprev = gxtop;
  1425. gxtop++;
  1426. gxbot++;
  1427. x = gxbot;
  1428. } /* while x <= y, end of group movement backward */
  1429. return(size);
  1430. } /* end of zdd_group_move_backward */
  1431. /**Function********************************************************************
  1432. Synopsis [Counts numbers of symmetric variables and symmetry
  1433. groups.]
  1434. Description []
  1435. SideEffects [None]
  1436. ******************************************************************************/
  1437. static void
  1438. cuddZddSymmSummary(
  1439. DdManager * table,
  1440. int lower,
  1441. int upper,
  1442. int * symvars,
  1443. int * symgroups)
  1444. {
  1445. int i,x,gbot;
  1446. int TotalSymm = 0;
  1447. int TotalSymmGroups = 0;
  1448. for (i = lower; i <= upper; i++) {
  1449. if (table->subtableZ[i].next != (unsigned) i) {
  1450. TotalSymmGroups++;
  1451. x = i;
  1452. do {
  1453. TotalSymm++;
  1454. gbot = x;
  1455. x = table->subtableZ[x].next;
  1456. } while (x != i);
  1457. #ifdef DD_DEBUG
  1458. assert(table->subtableZ[gbot].next == (unsigned) i);
  1459. #endif
  1460. i = gbot;
  1461. }
  1462. }
  1463. *symvars = TotalSymm;
  1464. *symgroups = TotalSymmGroups;
  1465. return;
  1466. } /* end of cuddZddSymmSummary */