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.

971 lines
28 KiB

  1. /**CFile***********************************************************************
  2. FileName [cuddZddLin.c]
  3. PackageName [cudd]
  4. Synopsis [Procedures for dynamic variable ordering of ZDDs.]
  5. Description [Internal procedures included in this module:
  6. <ul>
  7. <li> cuddZddLinearSifting()
  8. </ul>
  9. Static procedures included in this module:
  10. <ul>
  11. <li> cuddZddLinearInPlace()
  12. <li> cuddZddLinerAux()
  13. <li> cuddZddLinearUp()
  14. <li> cuddZddLinearDown()
  15. <li> cuddZddLinearBackward()
  16. <li> cuddZddUndoMoves()
  17. </ul>
  18. ]
  19. SeeAlso [cuddLinear.c cuddZddReord.c]
  20. Author [Fabio Somenzi]
  21. Copyright [Copyright (c) 1995-2012, Regents of the University of Colorado
  22. All rights reserved.
  23. Redistribution and use in source and binary forms, with or without
  24. modification, are permitted provided that the following conditions
  25. are met:
  26. Redistributions of source code must retain the above copyright
  27. notice, this list of conditions and the following disclaimer.
  28. Redistributions in binary form must reproduce the above copyright
  29. notice, this list of conditions and the following disclaimer in the
  30. documentation and/or other materials provided with the distribution.
  31. Neither the name of the University of Colorado nor the names of its
  32. contributors may be used to endorse or promote products derived from
  33. this software without specific prior written permission.
  34. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  37. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  38. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  39. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  40. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  41. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  42. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  43. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  44. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  45. POSSIBILITY OF SUCH DAMAGE.]
  46. ******************************************************************************/
  47. #include "util.h"
  48. #include "cuddInt.h"
  49. /*---------------------------------------------------------------------------*/
  50. /* Constant declarations */
  51. /*---------------------------------------------------------------------------*/
  52. #define CUDD_SWAP_MOVE 0
  53. #define CUDD_LINEAR_TRANSFORM_MOVE 1
  54. #define CUDD_INVERSE_TRANSFORM_MOVE 2
  55. /*---------------------------------------------------------------------------*/
  56. /* Stucture declarations */
  57. /*---------------------------------------------------------------------------*/
  58. /*---------------------------------------------------------------------------*/
  59. /* Type declarations */
  60. /*---------------------------------------------------------------------------*/
  61. /*---------------------------------------------------------------------------*/
  62. /* Variable declarations */
  63. /*---------------------------------------------------------------------------*/
  64. #ifndef lint
  65. static char rcsid[] DD_UNUSED = "$Id: cuddZddLin.c,v 1.16 2012/02/05 01:07:19 fabio Exp $";
  66. #endif
  67. extern int *zdd_entry;
  68. extern int zddTotalNumberSwapping;
  69. static int zddTotalNumberLinearTr;
  70. static DdNode *empty;
  71. /*---------------------------------------------------------------------------*/
  72. /* Macro declarations */
  73. /*---------------------------------------------------------------------------*/
  74. /**AutomaticStart*************************************************************/
  75. /*---------------------------------------------------------------------------*/
  76. /* Static function prototypes */
  77. /*---------------------------------------------------------------------------*/
  78. static int cuddZddLinearInPlace (DdManager * table, int x, int y);
  79. static int cuddZddLinearAux (DdManager *table, int x, int xLow, int xHigh);
  80. static Move * cuddZddLinearUp (DdManager *table, int y, int xLow, Move *prevMoves);
  81. static Move * cuddZddLinearDown (DdManager *table, int x, int xHigh, Move *prevMoves);
  82. static int cuddZddLinearBackward (DdManager *table, int size, Move *moves);
  83. static Move* cuddZddUndoMoves (DdManager *table, Move *moves);
  84. /**AutomaticEnd***************************************************************/
  85. /*---------------------------------------------------------------------------*/
  86. /* Definition of exported functions */
  87. /*---------------------------------------------------------------------------*/
  88. /*---------------------------------------------------------------------------*/
  89. /* Definition of internal functions */
  90. /*---------------------------------------------------------------------------*/
  91. /**Function********************************************************************
  92. Synopsis [Implementation of the linear sifting algorithm for ZDDs.]
  93. Description [Implementation of the linear sifting algorithm for ZDDs.
  94. Assumes that no dead nodes are present.
  95. <ol>
  96. <li> Order all the variables according to the number of entries
  97. in each unique table.
  98. <li> Sift the variable up and down and applies the XOR transformation,
  99. remembering each time the total size of the DD heap.
  100. <li> Select the best permutation.
  101. <li> Repeat 3 and 4 for all variables.
  102. </ol>
  103. Returns 1 if successful; 0 otherwise.]
  104. SideEffects [None]
  105. SeeAlso []
  106. ******************************************************************************/
  107. int
  108. cuddZddLinearSifting(
  109. DdManager * table,
  110. int lower,
  111. int upper)
  112. {
  113. int i;
  114. int *var;
  115. int size;
  116. int x;
  117. int result;
  118. #ifdef DD_STATS
  119. int previousSize;
  120. #endif
  121. size = table->sizeZ;
  122. empty = table->zero;
  123. /* Find order in which to sift variables. */
  124. var = NULL;
  125. zdd_entry = ALLOC(int, size);
  126. if (zdd_entry == NULL) {
  127. table->errorCode = CUDD_MEMORY_OUT;
  128. goto cuddZddSiftingOutOfMem;
  129. }
  130. var = ALLOC(int, size);
  131. if (var == NULL) {
  132. table->errorCode = CUDD_MEMORY_OUT;
  133. goto cuddZddSiftingOutOfMem;
  134. }
  135. for (i = 0; i < size; i++) {
  136. x = table->permZ[i];
  137. zdd_entry[i] = table->subtableZ[x].keys;
  138. var[i] = i;
  139. }
  140. qsort((void *)var, size, sizeof(int), (DD_QSFP)cuddZddUniqueCompare);
  141. /* Now sift. */
  142. for (i = 0; i < ddMin(table->siftMaxVar, size); i++) {
  143. if (zddTotalNumberSwapping >= table->siftMaxSwap)
  144. break;
  145. if (util_cpu_time() - table->startTime > table->timeLimit) {
  146. table->autoDynZ = 0; /* prevent further reordering */
  147. break;
  148. }
  149. x = table->permZ[var[i]];
  150. if (x < lower || x > upper) continue;
  151. #ifdef DD_STATS
  152. previousSize = table->keysZ;
  153. #endif
  154. result = cuddZddLinearAux(table, x, lower, upper);
  155. if (!result)
  156. goto cuddZddSiftingOutOfMem;
  157. #ifdef DD_STATS
  158. if (table->keysZ < (unsigned) previousSize) {
  159. (void) fprintf(table->out,"-");
  160. } else if (table->keysZ > (unsigned) previousSize) {
  161. (void) fprintf(table->out,"+"); /* should never happen */
  162. (void) fprintf(table->out,"\nSize increased from %d to %d while sifting variable %d\n", previousSize, table->keysZ , var[i]);
  163. } else {
  164. (void) fprintf(table->out,"=");
  165. }
  166. fflush(table->out);
  167. #endif
  168. }
  169. FREE(var);
  170. FREE(zdd_entry);
  171. return(1);
  172. cuddZddSiftingOutOfMem:
  173. if (zdd_entry != NULL) FREE(zdd_entry);
  174. if (var != NULL) FREE(var);
  175. return(0);
  176. } /* end of cuddZddLinearSifting */
  177. /*---------------------------------------------------------------------------*/
  178. /* Definition of static functions */
  179. /*---------------------------------------------------------------------------*/
  180. /**Function********************************************************************
  181. Synopsis [Linearly combines two adjacent variables.]
  182. Description [Linearly combines two adjacent variables. It assumes
  183. that no dead nodes are present on entry to this procedure. The
  184. procedure then guarantees that no dead nodes will be present when it
  185. terminates. cuddZddLinearInPlace assumes that x &lt; y. Returns the
  186. number of keys in the table if successful; 0 otherwise.]
  187. SideEffects [None]
  188. SeeAlso [cuddZddSwapInPlace cuddLinearInPlace]
  189. ******************************************************************************/
  190. static int
  191. cuddZddLinearInPlace(
  192. DdManager * table,
  193. int x,
  194. int y)
  195. {
  196. DdNodePtr *xlist, *ylist;
  197. int xindex, yindex;
  198. int xslots, yslots;
  199. int xshift, yshift;
  200. int oldxkeys, oldykeys;
  201. int newxkeys, newykeys;
  202. int i;
  203. int posn;
  204. DdNode *f, *f1, *f0, *f11, *f10, *f01, *f00;
  205. DdNode *newf1, *newf0, *g, *next, *previous;
  206. DdNode *special;
  207. #ifdef DD_DEBUG
  208. assert(x < y);
  209. assert(cuddZddNextHigh(table,x) == y);
  210. assert(table->subtableZ[x].keys != 0);
  211. assert(table->subtableZ[y].keys != 0);
  212. assert(table->subtableZ[x].dead == 0);
  213. assert(table->subtableZ[y].dead == 0);
  214. #endif
  215. zddTotalNumberLinearTr++;
  216. /* Get parameters of x subtable. */
  217. xindex = table->invpermZ[x];
  218. xlist = table->subtableZ[x].nodelist;
  219. oldxkeys = table->subtableZ[x].keys;
  220. xslots = table->subtableZ[x].slots;
  221. xshift = table->subtableZ[x].shift;
  222. newxkeys = 0;
  223. /* Get parameters of y subtable. */
  224. yindex = table->invpermZ[y];
  225. ylist = table->subtableZ[y].nodelist;
  226. oldykeys = table->subtableZ[y].keys;
  227. yslots = table->subtableZ[y].slots;
  228. yshift = table->subtableZ[y].shift;
  229. newykeys = oldykeys;
  230. /* The nodes in the x layer are put in two chains. The chain
  231. ** pointed by g holds the normal nodes. When re-expressed they stay
  232. ** in the x list. The chain pointed by special holds the elements
  233. ** that will move to the y list.
  234. */
  235. g = special = NULL;
  236. for (i = 0; i < xslots; i++) {
  237. f = xlist[i];
  238. if (f == NULL) continue;
  239. xlist[i] = NULL;
  240. while (f != NULL) {
  241. next = f->next;
  242. f1 = cuddT(f);
  243. /* if (f1->index == yindex) */ cuddSatDec(f1->ref);
  244. f0 = cuddE(f);
  245. /* if (f0->index == yindex) */ cuddSatDec(f0->ref);
  246. if ((int) f1->index == yindex && cuddE(f1) == empty &&
  247. (int) f0->index != yindex) {
  248. f->next = special;
  249. special = f;
  250. } else {
  251. f->next = g;
  252. g = f;
  253. }
  254. f = next;
  255. } /* while there are elements in the collision chain */
  256. } /* for each slot of the x subtable */
  257. /* Mark y nodes with pointers from above x. We mark them by
  258. ** changing their index to x.
  259. */
  260. for (i = 0; i < yslots; i++) {
  261. f = ylist[i];
  262. while (f != NULL) {
  263. if (f->ref != 0) {
  264. f->index = xindex;
  265. }
  266. f = f->next;
  267. } /* while there are elements in the collision chain */
  268. } /* for each slot of the y subtable */
  269. /* Move special nodes to the y list. */
  270. f = special;
  271. while (f != NULL) {
  272. next = f->next;
  273. f1 = cuddT(f);
  274. f11 = cuddT(f1);
  275. cuddT(f) = f11;
  276. cuddSatInc(f11->ref);
  277. f0 = cuddE(f);
  278. cuddSatInc(f0->ref);
  279. f->index = yindex;
  280. /* Insert at the beginning of the list so that it will be
  281. ** found first if there is a duplicate. The duplicate will
  282. ** eventually be moved or garbage collected. No node
  283. ** re-expression will add a pointer to it.
  284. */
  285. posn = ddHash(f11, f0, yshift);
  286. f->next = ylist[posn];
  287. ylist[posn] = f;
  288. newykeys++;
  289. f = next;
  290. }
  291. /* Take care of the remaining x nodes that must be re-expressed.
  292. ** They form a linked list pointed by g.
  293. */
  294. f = g;
  295. while (f != NULL) {
  296. #ifdef DD_COUNT
  297. table->swapSteps++;
  298. #endif
  299. next = f->next;
  300. /* Find f1, f0, f11, f10, f01, f00. */
  301. f1 = cuddT(f);
  302. if ((int) f1->index == yindex || (int) f1->index == xindex) {
  303. f11 = cuddT(f1); f10 = cuddE(f1);
  304. } else {
  305. f11 = empty; f10 = f1;
  306. }
  307. f0 = cuddE(f);
  308. if ((int) f0->index == yindex || (int) f0->index == xindex) {
  309. f01 = cuddT(f0); f00 = cuddE(f0);
  310. } else {
  311. f01 = empty; f00 = f0;
  312. }
  313. /* Create the new T child. */
  314. if (f01 == empty) {
  315. newf1 = f10;
  316. cuddSatInc(newf1->ref);
  317. } else {
  318. /* Check ylist for triple (yindex, f01, f10). */
  319. posn = ddHash(f01, f10, yshift);
  320. /* For each element newf1 in collision list ylist[posn]. */
  321. newf1 = ylist[posn];
  322. /* Search the collision chain skipping the marked nodes. */
  323. while (newf1 != NULL) {
  324. if (cuddT(newf1) == f01 && cuddE(newf1) == f10 &&
  325. (int) newf1->index == yindex) {
  326. cuddSatInc(newf1->ref);
  327. break; /* match */
  328. }
  329. newf1 = newf1->next;
  330. } /* while newf1 */
  331. if (newf1 == NULL) { /* no match */
  332. newf1 = cuddDynamicAllocNode(table);
  333. if (newf1 == NULL)
  334. goto zddSwapOutOfMem;
  335. newf1->index = yindex; newf1->ref = 1;
  336. cuddT(newf1) = f01;
  337. cuddE(newf1) = f10;
  338. /* Insert newf1 in the collision list ylist[pos];
  339. ** increase the ref counts of f01 and f10
  340. */
  341. newykeys++;
  342. newf1->next = ylist[posn];
  343. ylist[posn] = newf1;
  344. cuddSatInc(f01->ref);
  345. cuddSatInc(f10->ref);
  346. }
  347. }
  348. cuddT(f) = newf1;
  349. /* Do the same for f0. */
  350. /* Create the new E child. */
  351. if (f11 == empty) {
  352. newf0 = f00;
  353. cuddSatInc(newf0->ref);
  354. } else {
  355. /* Check ylist for triple (yindex, f11, f00). */
  356. posn = ddHash(f11, f00, yshift);
  357. /* For each element newf0 in collision list ylist[posn]. */
  358. newf0 = ylist[posn];
  359. while (newf0 != NULL) {
  360. if (cuddT(newf0) == f11 && cuddE(newf0) == f00 &&
  361. (int) newf0->index == yindex) {
  362. cuddSatInc(newf0->ref);
  363. break; /* match */
  364. }
  365. newf0 = newf0->next;
  366. } /* while newf0 */
  367. if (newf0 == NULL) { /* no match */
  368. newf0 = cuddDynamicAllocNode(table);
  369. if (newf0 == NULL)
  370. goto zddSwapOutOfMem;
  371. newf0->index = yindex; newf0->ref = 1;
  372. cuddT(newf0) = f11; cuddE(newf0) = f00;
  373. /* Insert newf0 in the collision list ylist[posn];
  374. ** increase the ref counts of f11 and f00.
  375. */
  376. newykeys++;
  377. newf0->next = ylist[posn];
  378. ylist[posn] = newf0;
  379. cuddSatInc(f11->ref);
  380. cuddSatInc(f00->ref);
  381. }
  382. }
  383. cuddE(f) = newf0;
  384. /* Re-insert the modified f in xlist.
  385. ** The modified f does not already exists in xlist.
  386. ** (Because of the uniqueness of the cofactors.)
  387. */
  388. posn = ddHash(newf1, newf0, xshift);
  389. newxkeys++;
  390. f->next = xlist[posn];
  391. xlist[posn] = f;
  392. f = next;
  393. } /* while f != NULL */
  394. /* GC the y layer and move the marked nodes to the x list. */
  395. /* For each node f in ylist. */
  396. for (i = 0; i < yslots; i++) {
  397. previous = NULL;
  398. f = ylist[i];
  399. while (f != NULL) {
  400. next = f->next;
  401. if (f->ref == 0) {
  402. cuddSatDec(cuddT(f)->ref);
  403. cuddSatDec(cuddE(f)->ref);
  404. cuddDeallocNode(table, f);
  405. newykeys--;
  406. if (previous == NULL)
  407. ylist[i] = next;
  408. else
  409. previous->next = next;
  410. } else if ((int) f->index == xindex) { /* move marked node */
  411. if (previous == NULL)
  412. ylist[i] = next;
  413. else
  414. previous->next = next;
  415. f1 = cuddT(f);
  416. cuddSatDec(f1->ref);
  417. /* Check ylist for triple (yindex, f1, empty). */
  418. posn = ddHash(f1, empty, yshift);
  419. /* For each element newf1 in collision list ylist[posn]. */
  420. newf1 = ylist[posn];
  421. while (newf1 != NULL) {
  422. if (cuddT(newf1) == f1 && cuddE(newf1) == empty &&
  423. (int) newf1->index == yindex) {
  424. cuddSatInc(newf1->ref);
  425. break; /* match */
  426. }
  427. newf1 = newf1->next;
  428. } /* while newf1 */
  429. if (newf1 == NULL) { /* no match */
  430. newf1 = cuddDynamicAllocNode(table);
  431. if (newf1 == NULL)
  432. goto zddSwapOutOfMem;
  433. newf1->index = yindex; newf1->ref = 1;
  434. cuddT(newf1) = f1; cuddE(newf1) = empty;
  435. /* Insert newf1 in the collision list ylist[posn];
  436. ** increase the ref counts of f1 and empty.
  437. */
  438. newykeys++;
  439. newf1->next = ylist[posn];
  440. ylist[posn] = newf1;
  441. if (posn == i && previous == NULL)
  442. previous = newf1;
  443. cuddSatInc(f1->ref);
  444. cuddSatInc(empty->ref);
  445. }
  446. cuddT(f) = newf1;
  447. f0 = cuddE(f);
  448. /* Insert f in x list. */
  449. posn = ddHash(newf1, f0, xshift);
  450. newxkeys++;
  451. newykeys--;
  452. f->next = xlist[posn];
  453. xlist[posn] = f;
  454. } else {
  455. previous = f;
  456. }
  457. f = next;
  458. } /* while f */
  459. } /* for i */
  460. /* Set the appropriate fields in table. */
  461. table->subtableZ[x].keys = newxkeys;
  462. table->subtableZ[y].keys = newykeys;
  463. table->keysZ += newxkeys + newykeys - oldxkeys - oldykeys;
  464. /* Update univ section; univ[x] remains the same. */
  465. table->univ[y] = cuddT(table->univ[x]);
  466. #if 0
  467. (void) fprintf(table->out,"x = %d y = %d\n", x, y);
  468. (void) Cudd_DebugCheck(table);
  469. (void) Cudd_CheckKeys(table);
  470. #endif
  471. return (table->keysZ);
  472. zddSwapOutOfMem:
  473. (void) fprintf(table->err, "Error: cuddZddSwapInPlace out of memory\n");
  474. return (0);
  475. } /* end of cuddZddLinearInPlace */
  476. /**Function********************************************************************
  477. Synopsis [Given xLow <= x <= xHigh moves x up and down between the
  478. boundaries.]
  479. Description [Given xLow <= x <= xHigh moves x up and down between the
  480. boundaries. Finds the best position and does the required changes.
  481. Returns 1 if successful; 0 otherwise.]
  482. SideEffects [None]
  483. SeeAlso []
  484. ******************************************************************************/
  485. static int
  486. cuddZddLinearAux(
  487. DdManager * table,
  488. int x,
  489. int xLow,
  490. int xHigh)
  491. {
  492. Move *move;
  493. Move *moveUp; /* list of up move */
  494. Move *moveDown; /* list of down move */
  495. int initial_size;
  496. int result;
  497. initial_size = table->keysZ;
  498. #ifdef DD_DEBUG
  499. assert(table->subtableZ[x].keys > 0);
  500. #endif
  501. moveDown = NULL;
  502. moveUp = NULL;
  503. if (x == xLow) {
  504. moveDown = cuddZddLinearDown(table, x, xHigh, NULL);
  505. /* At this point x --> xHigh. */
  506. if (moveDown == (Move *) CUDD_OUT_OF_MEM)
  507. goto cuddZddLinearAuxOutOfMem;
  508. /* Move backward and stop at best position. */
  509. result = cuddZddLinearBackward(table, initial_size, moveDown);
  510. if (!result)
  511. goto cuddZddLinearAuxOutOfMem;
  512. } else if (x == xHigh) {
  513. moveUp = cuddZddLinearUp(table, x, xLow, NULL);
  514. /* At this point x --> xLow. */
  515. if (moveUp == (Move *) CUDD_OUT_OF_MEM)
  516. goto cuddZddLinearAuxOutOfMem;
  517. /* Move backward and stop at best position. */
  518. result = cuddZddLinearBackward(table, initial_size, moveUp);
  519. if (!result)
  520. goto cuddZddLinearAuxOutOfMem;
  521. } else if ((x - xLow) > (xHigh - x)) { /* must go down first: shorter */
  522. moveDown = cuddZddLinearDown(table, x, xHigh, NULL);
  523. /* At this point x --> xHigh. */
  524. if (moveDown == (Move *) CUDD_OUT_OF_MEM)
  525. goto cuddZddLinearAuxOutOfMem;
  526. moveUp = cuddZddUndoMoves(table,moveDown);
  527. #ifdef DD_DEBUG
  528. assert(moveUp == NULL || moveUp->x == x);
  529. #endif
  530. moveUp = cuddZddLinearUp(table, x, xLow, moveUp);
  531. if (moveUp == (Move *) CUDD_OUT_OF_MEM)
  532. goto cuddZddLinearAuxOutOfMem;
  533. /* Move backward and stop at best position. */
  534. result = cuddZddLinearBackward(table, initial_size, moveUp);
  535. if (!result)
  536. goto cuddZddLinearAuxOutOfMem;
  537. } else {
  538. moveUp = cuddZddLinearUp(table, x, xLow, NULL);
  539. /* At this point x --> xHigh. */
  540. if (moveUp == (Move *) CUDD_OUT_OF_MEM)
  541. goto cuddZddLinearAuxOutOfMem;
  542. /* Then move up. */
  543. moveDown = cuddZddUndoMoves(table,moveUp);
  544. #ifdef DD_DEBUG
  545. assert(moveDown == NULL || moveDown->y == x);
  546. #endif
  547. moveDown = cuddZddLinearDown(table, x, xHigh, moveDown);
  548. if (moveDown == (Move *) CUDD_OUT_OF_MEM)
  549. goto cuddZddLinearAuxOutOfMem;
  550. /* Move backward and stop at best position. */
  551. result = cuddZddLinearBackward(table, initial_size, moveDown);
  552. if (!result)
  553. goto cuddZddLinearAuxOutOfMem;
  554. }
  555. while (moveDown != NULL) {
  556. move = moveDown->next;
  557. cuddDeallocMove(table, moveDown);
  558. moveDown = move;
  559. }
  560. while (moveUp != NULL) {
  561. move = moveUp->next;
  562. cuddDeallocMove(table, moveUp);
  563. moveUp = move;
  564. }
  565. return(1);
  566. cuddZddLinearAuxOutOfMem:
  567. if (moveDown != (Move *) CUDD_OUT_OF_MEM) {
  568. while (moveDown != NULL) {
  569. move = moveDown->next;
  570. cuddDeallocMove(table, moveDown);
  571. moveDown = move;
  572. }
  573. }
  574. if (moveUp != (Move *) CUDD_OUT_OF_MEM) {
  575. while (moveUp != NULL) {
  576. move = moveUp->next;
  577. cuddDeallocMove(table, moveUp);
  578. moveUp = move;
  579. }
  580. }
  581. return(0);
  582. } /* end of cuddZddLinearAux */
  583. /**Function********************************************************************
  584. Synopsis [Sifts a variable up applying the XOR transformation.]
  585. Description [Sifts a variable up applying the XOR
  586. transformation. Moves y up until either it reaches the bound (xLow)
  587. or the size of the ZDD heap increases too much. Returns the set of
  588. moves in case of success; NULL if memory is full.]
  589. SideEffects [None]
  590. SeeAlso []
  591. ******************************************************************************/
  592. static Move *
  593. cuddZddLinearUp(
  594. DdManager * table,
  595. int y,
  596. int xLow,
  597. Move * prevMoves)
  598. {
  599. Move *moves;
  600. Move *move;
  601. int x;
  602. int size, newsize;
  603. int limitSize;
  604. moves = prevMoves;
  605. limitSize = table->keysZ;
  606. x = cuddZddNextLow(table, y);
  607. while (x >= xLow) {
  608. size = cuddZddSwapInPlace(table, x, y);
  609. if (size == 0)
  610. goto cuddZddLinearUpOutOfMem;
  611. newsize = cuddZddLinearInPlace(table, x, y);
  612. if (newsize == 0)
  613. goto cuddZddLinearUpOutOfMem;
  614. move = (Move *) cuddDynamicAllocNode(table);
  615. if (move == NULL)
  616. goto cuddZddLinearUpOutOfMem;
  617. move->x = x;
  618. move->y = y;
  619. move->next = moves;
  620. moves = move;
  621. move->flags = CUDD_SWAP_MOVE;
  622. if (newsize > size) {
  623. /* Undo transformation. The transformation we apply is
  624. ** its own inverse. Hence, we just apply the transformation
  625. ** again.
  626. */
  627. newsize = cuddZddLinearInPlace(table,x,y);
  628. if (newsize == 0) goto cuddZddLinearUpOutOfMem;
  629. #ifdef DD_DEBUG
  630. if (newsize != size) {
  631. (void) fprintf(table->err,"Change in size after identity transformation! From %d to %d\n",size,newsize);
  632. }
  633. #endif
  634. } else {
  635. size = newsize;
  636. move->flags = CUDD_LINEAR_TRANSFORM_MOVE;
  637. }
  638. move->size = size;
  639. if ((double)size > (double)limitSize * table->maxGrowth)
  640. break;
  641. if (size < limitSize)
  642. limitSize = size;
  643. y = x;
  644. x = cuddZddNextLow(table, y);
  645. }
  646. return(moves);
  647. cuddZddLinearUpOutOfMem:
  648. while (moves != NULL) {
  649. move = moves->next;
  650. cuddDeallocMove(table, moves);
  651. moves = move;
  652. }
  653. return((Move *) CUDD_OUT_OF_MEM);
  654. } /* end of cuddZddLinearUp */
  655. /**Function********************************************************************
  656. Synopsis [Sifts a variable down and applies the XOR transformation.]
  657. Description [Sifts a variable down. Moves x down until either it
  658. reaches the bound (xHigh) or the size of the ZDD heap increases too
  659. much. Returns the set of moves in case of success; NULL if memory is
  660. full.]
  661. SideEffects [None]
  662. SeeAlso []
  663. ******************************************************************************/
  664. static Move *
  665. cuddZddLinearDown(
  666. DdManager * table,
  667. int x,
  668. int xHigh,
  669. Move * prevMoves)
  670. {
  671. Move *moves;
  672. Move *move;
  673. int y;
  674. int size, newsize;
  675. int limitSize;
  676. moves = prevMoves;
  677. limitSize = table->keysZ;
  678. y = cuddZddNextHigh(table, x);
  679. while (y <= xHigh) {
  680. size = cuddZddSwapInPlace(table, x, y);
  681. if (size == 0)
  682. goto cuddZddLinearDownOutOfMem;
  683. newsize = cuddZddLinearInPlace(table, x, y);
  684. if (newsize == 0)
  685. goto cuddZddLinearDownOutOfMem;
  686. move = (Move *) cuddDynamicAllocNode(table);
  687. if (move == NULL)
  688. goto cuddZddLinearDownOutOfMem;
  689. move->x = x;
  690. move->y = y;
  691. move->next = moves;
  692. moves = move;
  693. move->flags = CUDD_SWAP_MOVE;
  694. if (newsize > size) {
  695. /* Undo transformation. The transformation we apply is
  696. ** its own inverse. Hence, we just apply the transformation
  697. ** again.
  698. */
  699. newsize = cuddZddLinearInPlace(table,x,y);
  700. if (newsize == 0) goto cuddZddLinearDownOutOfMem;
  701. if (newsize != size) {
  702. (void) fprintf(table->err,"Change in size after identity transformation! From %d to %d\n",size,newsize);
  703. }
  704. } else {
  705. size = newsize;
  706. move->flags = CUDD_LINEAR_TRANSFORM_MOVE;
  707. }
  708. move->size = size;
  709. if ((double)size > (double)limitSize * table->maxGrowth)
  710. break;
  711. if (size < limitSize)
  712. limitSize = size;
  713. x = y;
  714. y = cuddZddNextHigh(table, x);
  715. }
  716. return(moves);
  717. cuddZddLinearDownOutOfMem:
  718. while (moves != NULL) {
  719. move = moves->next;
  720. cuddDeallocMove(table, moves);
  721. moves = move;
  722. }
  723. return((Move *) CUDD_OUT_OF_MEM);
  724. } /* end of cuddZddLinearDown */
  725. /**Function********************************************************************
  726. Synopsis [Given a set of moves, returns the ZDD heap to the position
  727. giving the minimum size.]
  728. Description [Given a set of moves, returns the ZDD heap to the
  729. position giving the minimum size. In case of ties, returns to the
  730. closest position giving the minimum size. Returns 1 in case of
  731. success; 0 otherwise.]
  732. SideEffects [None]
  733. SeeAlso []
  734. ******************************************************************************/
  735. static int
  736. cuddZddLinearBackward(
  737. DdManager * table,
  738. int size,
  739. Move * moves)
  740. {
  741. Move *move;
  742. int res;
  743. /* Find the minimum size among moves. */
  744. for (move = moves; move != NULL; move = move->next) {
  745. if (move->size < size) {
  746. size = move->size;
  747. }
  748. }
  749. for (move = moves; move != NULL; move = move->next) {
  750. if (move->size == size) return(1);
  751. if (move->flags == CUDD_LINEAR_TRANSFORM_MOVE) {
  752. res = cuddZddLinearInPlace(table,(int)move->x,(int)move->y);
  753. if (!res) return(0);
  754. }
  755. res = cuddZddSwapInPlace(table, move->x, move->y);
  756. if (!res)
  757. return(0);
  758. if (move->flags == CUDD_INVERSE_TRANSFORM_MOVE) {
  759. res = cuddZddLinearInPlace(table,(int)move->x,(int)move->y);
  760. if (!res) return(0);
  761. }
  762. }
  763. return(1);
  764. } /* end of cuddZddLinearBackward */
  765. /**Function********************************************************************
  766. Synopsis [Given a set of moves, returns the ZDD heap to the order
  767. in effect before the moves.]
  768. Description [Given a set of moves, returns the ZDD heap to the
  769. order in effect before the moves. Returns 1 in case of success;
  770. 0 otherwise.]
  771. SideEffects [None]
  772. ******************************************************************************/
  773. static Move*
  774. cuddZddUndoMoves(
  775. DdManager * table,
  776. Move * moves)
  777. {
  778. Move *invmoves = NULL;
  779. Move *move;
  780. Move *invmove;
  781. int size;
  782. for (move = moves; move != NULL; move = move->next) {
  783. invmove = (Move *) cuddDynamicAllocNode(table);
  784. if (invmove == NULL) goto cuddZddUndoMovesOutOfMem;
  785. invmove->x = move->x;
  786. invmove->y = move->y;
  787. invmove->next = invmoves;
  788. invmoves = invmove;
  789. if (move->flags == CUDD_SWAP_MOVE) {
  790. invmove->flags = CUDD_SWAP_MOVE;
  791. size = cuddZddSwapInPlace(table,(int)move->x,(int)move->y);
  792. if (!size) goto cuddZddUndoMovesOutOfMem;
  793. } else if (move->flags == CUDD_LINEAR_TRANSFORM_MOVE) {
  794. invmove->flags = CUDD_INVERSE_TRANSFORM_MOVE;
  795. size = cuddZddLinearInPlace(table,(int)move->x,(int)move->y);
  796. if (!size) goto cuddZddUndoMovesOutOfMem;
  797. size = cuddZddSwapInPlace(table,(int)move->x,(int)move->y);
  798. if (!size) goto cuddZddUndoMovesOutOfMem;
  799. } else { /* must be CUDD_INVERSE_TRANSFORM_MOVE */
  800. #ifdef DD_DEBUG
  801. (void) fprintf(table->err,"Unforseen event in ddUndoMoves!\n");
  802. #endif
  803. invmove->flags = CUDD_LINEAR_TRANSFORM_MOVE;
  804. size = cuddZddSwapInPlace(table,(int)move->x,(int)move->y);
  805. if (!size) goto cuddZddUndoMovesOutOfMem;
  806. size = cuddZddLinearInPlace(table,(int)move->x,(int)move->y);
  807. if (!size) goto cuddZddUndoMovesOutOfMem;
  808. }
  809. invmove->size = size;
  810. }
  811. return(invmoves);
  812. cuddZddUndoMovesOutOfMem:
  813. while (invmoves != NULL) {
  814. move = invmoves->next;
  815. cuddDeallocMove(table, invmoves);
  816. invmoves = move;
  817. }
  818. return((Move *) CUDD_OUT_OF_MEM);
  819. } /* end of cuddZddUndoMoves */