The source code and dockerfile for the GSW2024 AI Lab.
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

925 lines
25 KiB

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