cuddZddLin.c 31.4 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9
/**CFile***********************************************************************

  FileName    [cuddZddLin.c]

  PackageName [cudd]

  Synopsis    [Procedures for dynamic variable ordering of ZDDs.]

  Description [Internal procedures included in this module:
10 11 12 13 14 15 16 17 18 19 20 21 22
                    <ul>
                    <li> cuddZddLinearSifting()
                    </ul>
               Static procedures included in this module:
                    <ul>
                    <li> cuddZddLinearInPlace()
                    <li> cuddZddLinerAux()
                    <li> cuddZddLinearUp()
                    <li> cuddZddLinearDown()
                    <li> cuddZddLinearBackward()
                    <li> cuddZddUndoMoves()
                    </ul>
              ]
Alan Mishchenko committed
23 24 25 26 27

  SeeAlso     [cuddLinear.c cuddZddReord.c]

  Author      [Fabio Somenzi]

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
  Copyright   [Copyright (c) 1995-2004, Regents of the University of Colorado

  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:

  Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.

  Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution.

  Neither the name of the University of Colorado nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  POSSIBILITY OF SUCH DAMAGE.]
Alan Mishchenko committed
59 60 61

******************************************************************************/

62
#include "misc/util/util_hack.h"
63
#include "cuddInt.h"
Alan Mishchenko committed
64

65 66 67
ABC_NAMESPACE_IMPL_START


68

Alan Mishchenko committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
/*---------------------------------------------------------------------------*/
/* Constant declarations                                                     */
/*---------------------------------------------------------------------------*/

#define CUDD_SWAP_MOVE 0
#define CUDD_LINEAR_TRANSFORM_MOVE 1
#define CUDD_INVERSE_TRANSFORM_MOVE 2

/*---------------------------------------------------------------------------*/
/* Stucture declarations                                                     */
/*---------------------------------------------------------------------------*/


/*---------------------------------------------------------------------------*/
/* Type declarations                                                         */
/*---------------------------------------------------------------------------*/


/*---------------------------------------------------------------------------*/
/* Variable declarations                                                     */
/*---------------------------------------------------------------------------*/

#ifndef lint
92
static char rcsid[] DD_UNUSED = "$Id: cuddZddLin.c,v 1.14 2004/08/13 18:04:53 fabio Exp $";
Alan Mishchenko committed
93 94
#endif

95 96 97 98
extern  int     *zdd_entry;
extern  int     zddTotalNumberSwapping;
static  int     zddTotalNumberLinearTr;
static  DdNode  *empty;
Alan Mishchenko committed
99 100 101 102 103 104 105 106 107 108 109 110 111


/*---------------------------------------------------------------------------*/
/* Macro declarations                                                        */
/*---------------------------------------------------------------------------*/


/**AutomaticStart*************************************************************/

/*---------------------------------------------------------------------------*/
/* Static function prototypes                                                */
/*---------------------------------------------------------------------------*/

112 113 114 115 116 117
static int cuddZddLinearInPlace (DdManager * table, int x, int y);
static int cuddZddLinearAux (DdManager *table, int x, int xLow, int xHigh);
static Move * cuddZddLinearUp (DdManager *table, int y, int xLow, Move *prevMoves);
static Move * cuddZddLinearDown (DdManager *table, int x, int xHigh, Move *prevMoves);
static int cuddZddLinearBackward (DdManager *table, int size, Move *moves);
static Move* cuddZddUndoMoves (DdManager *table, Move *moves);
Alan Mishchenko committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

/**AutomaticEnd***************************************************************/


/*---------------------------------------------------------------------------*/
/* Definition of exported functions                                          */
/*---------------------------------------------------------------------------*/


/*---------------------------------------------------------------------------*/
/* Definition of internal functions                                          */
/*---------------------------------------------------------------------------*/




/**Function********************************************************************

  Synopsis    [Implementation of the linear sifting algorithm for ZDDs.]

  Description [Implementation of the linear sifting algorithm for ZDDs.
  Assumes that no dead nodes are present.
    <ol>
    <li> Order all the variables according to the number of entries
    in each unique table.
    <li> Sift the variable up and down and applies the XOR transformation,
    remembering each time the total size of the DD heap.
    <li> Select the best permutation.
    <li> Repeat 3 and 4 for all variables.
    </ol>
  Returns 1 if successful; 0 otherwise.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
int
cuddZddLinearSifting(
  DdManager * table,
  int  lower,
  int  upper)
{
161 162 163 164 165
    int i;
    int *var;
    int size;
    int x;
    int result;
Alan Mishchenko committed
166
#ifdef DD_STATS
167
    int previousSize;
Alan Mishchenko committed
168 169 170 171 172 173 174
#endif

    size = table->sizeZ;
    empty = table->zero;

    /* Find order in which to sift variables. */
    var = NULL;
Alan Mishchenko committed
175
    zdd_entry = ABC_ALLOC(int, size);
Alan Mishchenko committed
176
    if (zdd_entry == NULL) {
177 178
        table->errorCode = CUDD_MEMORY_OUT;
        goto cuddZddSiftingOutOfMem;
Alan Mishchenko committed
179
    }
Alan Mishchenko committed
180
    var = ABC_ALLOC(int, size);
Alan Mishchenko committed
181
    if (var == NULL) {
182 183
        table->errorCode = CUDD_MEMORY_OUT;
        goto cuddZddSiftingOutOfMem;
Alan Mishchenko committed
184 185 186
    }

    for (i = 0; i < size; i++) {
187 188 189
        x = table->permZ[i];
        zdd_entry[i] = table->subtableZ[x].keys;
        var[i] = i;
Alan Mishchenko committed
190 191
    }

192
    qsort((void *)var, size, sizeof(int), (DD_QSFP)cuddZddUniqueCompare);
Alan Mishchenko committed
193 194 195

    /* Now sift. */
    for (i = 0; i < ddMin(table->siftMaxVar, size); i++) {
196 197 198 199
        if (zddTotalNumberSwapping >= table->siftMaxSwap)
            break;
        x = table->permZ[var[i]];
        if (x < lower || x > upper) continue;
Alan Mishchenko committed
200
#ifdef DD_STATS
201
        previousSize = table->keysZ;
Alan Mishchenko committed
202
#endif
203 204 205
        result = cuddZddLinearAux(table, x, lower, upper);
        if (!result)
            goto cuddZddSiftingOutOfMem;
Alan Mishchenko committed
206
#ifdef DD_STATS
207 208 209 210 211 212 213 214 215
        if (table->keysZ < (unsigned) previousSize) {
            (void) fprintf(table->out,"-");
        } else if (table->keysZ > (unsigned) previousSize) {
            (void) fprintf(table->out,"+");     /* should never happen */
            (void) fprintf(table->out,"\nSize increased from %d to %d while sifting variable %d\n", previousSize, table->keysZ , var[i]);
        } else {
            (void) fprintf(table->out,"=");
        }
        fflush(table->out);
Alan Mishchenko committed
216 217 218
#endif
    }

Alan Mishchenko committed
219 220
    ABC_FREE(var);
    ABC_FREE(zdd_entry);
Alan Mishchenko committed
221 222 223 224 225

    return(1);

cuddZddSiftingOutOfMem:

Alan Mishchenko committed
226 227
    if (zdd_entry != NULL) ABC_FREE(zdd_entry);
    if (var != NULL) ABC_FREE(var);
Alan Mishchenko committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253

    return(0);

} /* end of cuddZddLinearSifting */


/*---------------------------------------------------------------------------*/
/* Definition of static functions                                            */
/*---------------------------------------------------------------------------*/


/**Function********************************************************************

  Synopsis    [Linearly combines two adjacent variables.]

  Description [Linearly combines two adjacent variables. It assumes
  that no dead nodes are present on entry to this procedure.  The
  procedure then guarantees that no dead nodes will be present when it
  terminates.  cuddZddLinearInPlace assumes that x &lt; y.  Returns the
  number of keys in the table if successful; 0 otherwise.]

  SideEffects [None]

  SeeAlso     [cuddZddSwapInPlace cuddLinearInPlace]

******************************************************************************/
254
static int
Alan Mishchenko committed
255 256 257 258 259 260
cuddZddLinearInPlace(
  DdManager * table,
  int  x,
  int  y)
{
    DdNodePtr *xlist, *ylist;
261 262 263
    int         xindex, yindex;
    int         xslots, yslots;
    int         xshift, yshift;
Alan Mishchenko committed
264 265
    int         oldxkeys, oldykeys;
    int         newxkeys, newykeys;
266 267 268 269 270
    int         i;
    int         posn;
    DdNode      *f, *f1, *f0, *f11, *f10, *f01, *f00;
    DdNode      *newf1, *newf0, *g, *next, *previous;
    DdNode      *special;
Alan Mishchenko committed
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305

#ifdef DD_DEBUG
    assert(x < y);
    assert(cuddZddNextHigh(table,x) == y);
    assert(table->subtableZ[x].keys != 0);
    assert(table->subtableZ[y].keys != 0);
    assert(table->subtableZ[x].dead == 0);
    assert(table->subtableZ[y].dead == 0);
#endif

    zddTotalNumberLinearTr++;

    /* Get parameters of x subtable. */
    xindex   = table->invpermZ[x];
    xlist    = table->subtableZ[x].nodelist;
    oldxkeys = table->subtableZ[x].keys;
    xslots   = table->subtableZ[x].slots;
    xshift   = table->subtableZ[x].shift;
    newxkeys = 0;

    /* Get parameters of y subtable. */
    yindex   = table->invpermZ[y];
    ylist    = table->subtableZ[y].nodelist;
    oldykeys = table->subtableZ[y].keys;
    yslots   = table->subtableZ[y].slots;
    yshift   = table->subtableZ[y].shift;
    newykeys = oldykeys;

    /* The nodes in the x layer are put in two chains.  The chain
    ** pointed by g holds the normal nodes. When re-expressed they stay
    ** in the x list. The chain pointed by special holds the elements
    ** that will move to the y list.
    */
    g = special = NULL;
    for (i = 0; i < xslots; i++) {
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
        f = xlist[i];
        if (f == NULL) continue;
        xlist[i] = NULL;
        while (f != NULL) {
            next = f->next;
            f1 = cuddT(f);
            /* if (f1->index == yindex) */ cuddSatDec(f1->ref);
            f0 = cuddE(f);
            /* if (f0->index == yindex) */ cuddSatDec(f0->ref);
            if ((int) f1->index == yindex && cuddE(f1) == empty &&
                (int) f0->index != yindex) {
                f->next = special;
                special = f;
            } else {
                f->next = g;
                g = f;
            }
            f = next;
        } /* while there are elements in the collision chain */
Alan Mishchenko committed
325 326 327 328 329 330
    } /* for each slot of the x subtable */

    /* Mark y nodes with pointers from above x. We mark them by
    **  changing their index to x.
    */
    for (i = 0; i < yslots; i++) {
331 332 333 334 335 336 337
        f = ylist[i];
        while (f != NULL) {
            if (f->ref != 0) {
                f->index = xindex;
            }
            f = f->next;
        } /* while there are elements in the collision chain */
Alan Mishchenko committed
338 339 340 341 342
    } /* for each slot of the y subtable */

    /* Move special nodes to the y list. */
    f = special;
    while (f != NULL) {
343 344 345 346 347 348 349 350 351 352 353 354 355
        next = f->next;
        f1 = cuddT(f);
        f11 = cuddT(f1);
        cuddT(f) = f11;
        cuddSatInc(f11->ref);
        f0 = cuddE(f);
        cuddSatInc(f0->ref);
        f->index = yindex;
        /* Insert at the beginning of the list so that it will be
        ** found first if there is a duplicate. The duplicate will
        ** eventually be moved or garbage collected. No node
        ** re-expression will add a pointer to it.
        */
356
        posn = ddHash(cuddF2L(f11), cuddF2L(f0), yshift);
357 358 359 360
        f->next = ylist[posn];
        ylist[posn] = f;
        newykeys++;
        f = next;
Alan Mishchenko committed
361 362 363 364 365 366 367 368
    }

    /* Take care of the remaining x nodes that must be re-expressed.
    ** They form a linked list pointed by g.
    */
    f = g;
    while (f != NULL) {
#ifdef DD_COUNT
369
        table->swapSteps++;
Alan Mishchenko committed
370
#endif
371 372 373 374 375 376 377
        next = f->next;
        /* Find f1, f0, f11, f10, f01, f00. */
        f1 = cuddT(f);
        if ((int) f1->index == yindex || (int) f1->index == xindex) {
            f11 = cuddT(f1); f10 = cuddE(f1);
        } else {
            f11 = empty; f10 = f1;
Alan Mishchenko committed
378
        }
379 380 381 382 383
        f0 = cuddE(f);
        if ((int) f0->index == yindex || (int) f0->index == xindex) {
            f01 = cuddT(f0); f00 = cuddE(f0);
        } else {
            f01 = empty; f00 = f0;
Alan Mishchenko committed
384
        }
385 386 387 388 389 390
        /* Create the new T child. */
        if (f01 == empty) {
            newf1 = f10;
            cuddSatInc(newf1->ref);
        } else {
            /* Check ylist for triple (yindex, f01, f10). */
391
            posn = ddHash(cuddF2L(f01), cuddF2L(f10), yshift);
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
            /* For each element newf1 in collision list ylist[posn]. */
            newf1 = ylist[posn];
            /* Search the collision chain skipping the marked nodes. */
            while (newf1 != NULL) {
                if (cuddT(newf1) == f01 && cuddE(newf1) == f10 &&
                    (int) newf1->index == yindex) {
                    cuddSatInc(newf1->ref);
                    break; /* match */
                }
                newf1 = newf1->next;
            } /* while newf1 */
            if (newf1 == NULL) {        /* no match */
                newf1 = cuddDynamicAllocNode(table);
                if (newf1 == NULL)
                    goto zddSwapOutOfMem;
                newf1->index = yindex; newf1->ref = 1;
                cuddT(newf1) = f01;
                cuddE(newf1) = f10;
                /* Insert newf1 in the collision list ylist[pos];
                ** increase the ref counts of f01 and f10
                */
                newykeys++;
                newf1->next = ylist[posn];
                ylist[posn] = newf1;
                cuddSatInc(f01->ref);
                cuddSatInc(f10->ref);
            }
        }
        cuddT(f) = newf1;
Alan Mishchenko committed
421

422 423 424 425
        /* Do the same for f0. */
        /* Create the new E child. */
        if (f11 == empty) {
            newf0 = f00;
Alan Mishchenko committed
426
            cuddSatInc(newf0->ref);
427 428
        } else {
            /* Check ylist for triple (yindex, f11, f00). */
429
            posn = ddHash(cuddF2L(f11), cuddF2L(f00), yshift);
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
            /* For each element newf0 in collision list ylist[posn]. */
            newf0 = ylist[posn];
            while (newf0 != NULL) {
                if (cuddT(newf0) == f11 && cuddE(newf0) == f00 &&
                    (int) newf0->index == yindex) {
                    cuddSatInc(newf0->ref);
                    break; /* match */
                }
                newf0 = newf0->next;
            } /* while newf0 */
            if (newf0 == NULL) {        /* no match */
                newf0 = cuddDynamicAllocNode(table);
                if (newf0 == NULL)
                    goto zddSwapOutOfMem;
                newf0->index = yindex; newf0->ref = 1;
                cuddT(newf0) = f11; cuddE(newf0) = f00;
                /* Insert newf0 in the collision list ylist[posn];
                ** increase the ref counts of f11 and f00.
                */
                newykeys++;
                newf0->next = ylist[posn];
                ylist[posn] = newf0;
                cuddSatInc(f11->ref);
                cuddSatInc(f00->ref);
            }
Alan Mishchenko committed
455
        }
456
        cuddE(f) = newf0;
Alan Mishchenko committed
457

458 459 460 461
        /* Re-insert the modified f in xlist.
        ** The modified f does not already exists in xlist.
        ** (Because of the uniqueness of the cofactors.)
        */
462
        posn = ddHash(cuddF2L(newf1), cuddF2L(newf0), xshift);
463 464 465 466
        newxkeys++;
        f->next = xlist[posn];
        xlist[posn] = f;
        f = next;
Alan Mishchenko committed
467 468 469 470 471 472
    } /* while f != NULL */

    /* GC the y layer and move the marked nodes to the x list. */

    /* For each node f in ylist. */
    for (i = 0; i < yslots; i++) {
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
        previous = NULL;
        f = ylist[i];
        while (f != NULL) {
            next = f->next;
            if (f->ref == 0) {
                cuddSatDec(cuddT(f)->ref);
                cuddSatDec(cuddE(f)->ref);
                cuddDeallocNode(table, f);
                newykeys--;
                if (previous == NULL)
                    ylist[i] = next;
                else
                    previous->next = next;
            } else if ((int) f->index == xindex) { /* move marked node */
                if (previous == NULL)
                    ylist[i] = next;
                else
                    previous->next = next;
                f1 = cuddT(f);
                cuddSatDec(f1->ref);
                /* Check ylist for triple (yindex, f1, empty). */
494
                posn = ddHash(cuddF2L(f1), cuddF2L(empty), yshift);
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
                /* For each element newf1 in collision list ylist[posn]. */
                newf1 = ylist[posn];
                while (newf1 != NULL) {
                    if (cuddT(newf1) == f1 && cuddE(newf1) == empty &&
                        (int) newf1->index == yindex) {
                        cuddSatInc(newf1->ref);
                        break; /* match */
                    }
                    newf1 = newf1->next;
                } /* while newf1 */
                if (newf1 == NULL) {    /* no match */
                    newf1 = cuddDynamicAllocNode(table);
                    if (newf1 == NULL)
                        goto zddSwapOutOfMem;
                    newf1->index = yindex; newf1->ref = 1;
                    cuddT(newf1) = f1; cuddE(newf1) = empty;
                    /* Insert newf1 in the collision list ylist[posn];
                    ** increase the ref counts of f1 and empty.
                    */
                    newykeys++;
                    newf1->next = ylist[posn];
                    ylist[posn] = newf1;
                    if (posn == i && previous == NULL)
                        previous = newf1;
                    cuddSatInc(f1->ref);
                    cuddSatInc(empty->ref);
                }
                cuddT(f) = newf1;
                f0 = cuddE(f);
                /* Insert f in x list. */
525
                posn = ddHash(cuddF2L(newf1), cuddF2L(f0), xshift);
526 527 528 529 530 531
                newxkeys++;
                newykeys--;
                f->next = xlist[posn];
                xlist[posn] = f;
            } else {
                previous = f;
Alan Mishchenko committed
532
            }
533 534
            f = next;
        } /* while f */
Alan Mishchenko committed
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
    } /* for i */

    /* Set the appropriate fields in table. */
    table->subtableZ[x].keys     = newxkeys;
    table->subtableZ[y].keys     = newykeys;

    table->keysZ += newxkeys + newykeys - oldxkeys - oldykeys;

    /* Update univ section; univ[x] remains the same. */
    table->univ[y] = cuddT(table->univ[x]);

#if 0
    (void) fprintf(table->out,"x = %d  y = %d\n", x, y);
    (void) Cudd_DebugCheck(table);
    (void) Cudd_CheckKeys(table);
#endif

    return (table->keysZ);

zddSwapOutOfMem:
    (void) fprintf(table->err, "Error: cuddZddSwapInPlace out of memory\n");

    return (0);

} /* end of cuddZddLinearInPlace */


/**Function********************************************************************

  Synopsis    [Given xLow <= x <= xHigh moves x up and down between the
  boundaries.]

  Description [Given xLow <= x <= xHigh moves x up and down between the
  boundaries. Finds the best position and does the required changes.
  Returns 1 if successful; 0 otherwise.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static int
cuddZddLinearAux(
  DdManager * table,
  int  x,
  int  xLow,
  int  xHigh)
{
583 584 585
    Move        *move;
    Move        *moveUp;        /* list of up move */
    Move        *moveDown;      /* list of down move */
Alan Mishchenko committed
586

587 588
    int         initial_size;
    int         result;
Alan Mishchenko committed
589 590 591 592 593 594 595 596 597 598 599

    initial_size = table->keysZ;

#ifdef DD_DEBUG
    assert(table->subtableZ[x].keys > 0);
#endif

    moveDown = NULL;
    moveUp = NULL;

    if (x == xLow) {
600 601 602 603 604 605 606 607
        moveDown = cuddZddLinearDown(table, x, xHigh, NULL);
        /* At this point x --> xHigh. */
        if (moveDown == (Move *) CUDD_OUT_OF_MEM)
            goto cuddZddLinearAuxOutOfMem;
        /* Move backward and stop at best position. */
        result = cuddZddLinearBackward(table, initial_size, moveDown);
        if (!result)
            goto cuddZddLinearAuxOutOfMem;
Alan Mishchenko committed
608 609

    } else if (x == xHigh) {
610 611 612 613 614 615 616 617
        moveUp = cuddZddLinearUp(table, x, xLow, NULL);
        /* At this point x --> xLow. */
        if (moveUp == (Move *) CUDD_OUT_OF_MEM)
            goto cuddZddLinearAuxOutOfMem;
        /* Move backward and stop at best position. */
        result = cuddZddLinearBackward(table, initial_size, moveUp);
        if (!result)
            goto cuddZddLinearAuxOutOfMem;
Alan Mishchenko committed
618 619

    } else if ((x - xLow) > (xHigh - x)) { /* must go down first: shorter */
620 621 622 623 624
        moveDown = cuddZddLinearDown(table, x, xHigh, NULL);
        /* At this point x --> xHigh. */
        if (moveDown == (Move *) CUDD_OUT_OF_MEM)
            goto cuddZddLinearAuxOutOfMem;
        moveUp = cuddZddUndoMoves(table,moveDown);
Alan Mishchenko committed
625
#ifdef DD_DEBUG
626
        assert(moveUp == NULL || moveUp->x == x);
Alan Mishchenko committed
627
#endif
628 629 630 631 632 633 634
        moveUp = cuddZddLinearUp(table, x, xLow, moveUp);
        if (moveUp == (Move *) CUDD_OUT_OF_MEM)
            goto cuddZddLinearAuxOutOfMem;
        /* Move backward and stop at best position. */
        result = cuddZddLinearBackward(table, initial_size, moveUp);
        if (!result)
            goto cuddZddLinearAuxOutOfMem;
Alan Mishchenko committed
635 636

    } else {
637 638 639 640 641 642
        moveUp = cuddZddLinearUp(table, x, xLow, NULL);
        /* At this point x --> xHigh. */
        if (moveUp == (Move *) CUDD_OUT_OF_MEM)
            goto cuddZddLinearAuxOutOfMem;
        /* Then move up. */
        moveDown = cuddZddUndoMoves(table,moveUp);
Alan Mishchenko committed
643
#ifdef DD_DEBUG
644
        assert(moveDown == NULL || moveDown->y == x);
Alan Mishchenko committed
645
#endif
646 647 648 649 650 651 652
        moveDown = cuddZddLinearDown(table, x, xHigh, moveDown);
        if (moveDown == (Move *) CUDD_OUT_OF_MEM)
            goto cuddZddLinearAuxOutOfMem;
        /* Move backward and stop at best position. */
        result = cuddZddLinearBackward(table, initial_size, moveDown);
        if (!result)
            goto cuddZddLinearAuxOutOfMem;
Alan Mishchenko committed
653 654 655
    }

    while (moveDown != NULL) {
656 657 658
        move = moveDown->next;
        cuddDeallocMove(table, moveDown);
        moveDown = move;
Alan Mishchenko committed
659 660
    }
    while (moveUp != NULL) {
661 662 663
        move = moveUp->next;
        cuddDeallocMove(table, moveUp);
        moveUp = move;
Alan Mishchenko committed
664 665 666 667 668 669
    }

    return(1);

cuddZddLinearAuxOutOfMem:
    if (moveDown != (Move *) CUDD_OUT_OF_MEM) {
670 671 672 673 674
        while (moveDown != NULL) {
            move = moveDown->next;
            cuddDeallocMove(table, moveDown);
            moveDown = move;
        }
Alan Mishchenko committed
675 676
    }
    if (moveUp != (Move *) CUDD_OUT_OF_MEM) {
677 678 679 680 681
        while (moveUp != NULL) {
            move = moveUp->next;
            cuddDeallocMove(table, moveUp);
            moveUp = move;
        }
Alan Mishchenko committed
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
    }

    return(0);

} /* end of cuddZddLinearAux */


/**Function********************************************************************

  Synopsis    [Sifts a variable up applying the XOR transformation.]

  Description [Sifts a variable up applying the XOR
  transformation. Moves y up until either it reaches the bound (xLow)
  or the size of the ZDD heap increases too much.  Returns the set of
  moves in case of success; NULL if memory is full.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static Move *
cuddZddLinearUp(
  DdManager * table,
  int  y,
  int  xLow,
  Move * prevMoves)
{
710 711 712 713 714
    Move        *moves;
    Move        *move;
    int         x;
    int         size, newsize;
    int         limitSize;
Alan Mishchenko committed
715 716 717 718 719 720

    moves = prevMoves;
    limitSize = table->keysZ;

    x = cuddZddNextLow(table, y);
    while (x >= xLow) {
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
        size = cuddZddSwapInPlace(table, x, y);
        if (size == 0)
            goto cuddZddLinearUpOutOfMem;
        newsize = cuddZddLinearInPlace(table, x, y);
        if (newsize == 0)
            goto cuddZddLinearUpOutOfMem;
        move = (Move *) cuddDynamicAllocNode(table);
        if (move == NULL)
            goto cuddZddLinearUpOutOfMem;
        move->x = x;
        move->y = y;
        move->next = moves;
        moves = move;
        move->flags = CUDD_SWAP_MOVE;
        if (newsize > size) {
            /* Undo transformation. The transformation we apply is
            ** its own inverse. Hence, we just apply the transformation
            ** again.
            */
            newsize = cuddZddLinearInPlace(table,x,y);
            if (newsize == 0) goto cuddZddLinearUpOutOfMem;
Alan Mishchenko committed
742
#ifdef DD_DEBUG
743 744 745
            if (newsize != size) {
                (void) fprintf(table->err,"Change in size after identity transformation! From %d to %d\n",size,newsize);
            }
Alan Mishchenko committed
746
#endif
747 748 749 750 751
        } else {
            size = newsize;
            move->flags = CUDD_LINEAR_TRANSFORM_MOVE;
        }
        move->size = size;
Alan Mishchenko committed
752

753 754
        if ((double)size > (double)limitSize * table->maxGrowth)
            break;
Alan Mishchenko committed
755
        if (size < limitSize)
756
            limitSize = size;
Alan Mishchenko committed
757

758 759
        y = x;
        x = cuddZddNextLow(table, y);
Alan Mishchenko committed
760 761 762 763 764
    }
    return(moves);

cuddZddLinearUpOutOfMem:
    while (moves != NULL) {
765 766 767
        move = moves->next;
        cuddDeallocMove(table, moves);
        moves = move;
Alan Mishchenko committed
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
    }
    return((Move *) CUDD_OUT_OF_MEM);

} /* end of cuddZddLinearUp */


/**Function********************************************************************

  Synopsis    [Sifts a variable down and applies the XOR transformation.]

  Description [Sifts a variable down. Moves x down until either it
  reaches the bound (xHigh) or the size of the ZDD heap increases too
  much. Returns the set of moves in case of success; NULL if memory is
  full.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static Move *
cuddZddLinearDown(
  DdManager * table,
  int  x,
  int  xHigh,
  Move * prevMoves)
{
795 796 797 798 799
    Move        *moves;
    Move        *move;
    int         y;
    int         size, newsize;
    int         limitSize;
Alan Mishchenko committed
800 801 802 803 804 805

    moves = prevMoves;
    limitSize = table->keysZ;

    y = cuddZddNextHigh(table, x);
    while (y <= xHigh) {
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
        size = cuddZddSwapInPlace(table, x, y);
        if (size == 0)
            goto cuddZddLinearDownOutOfMem;
        newsize = cuddZddLinearInPlace(table, x, y);
        if (newsize == 0)
            goto cuddZddLinearDownOutOfMem;
        move = (Move *) cuddDynamicAllocNode(table);
        if (move == NULL)
            goto cuddZddLinearDownOutOfMem;
        move->x = x;
        move->y = y;
        move->next = moves;
        moves = move;
        move->flags = CUDD_SWAP_MOVE;
        if (newsize > size) {
            /* Undo transformation. The transformation we apply is
            ** its own inverse. Hence, we just apply the transformation
            ** again.
            */
            newsize = cuddZddLinearInPlace(table,x,y);
            if (newsize == 0) goto cuddZddLinearDownOutOfMem;
            if (newsize != size) {
                (void) fprintf(table->err,"Change in size after identity transformation! From %d to %d\n",size,newsize);
            }
        } else {
            size = newsize;
            move->flags = CUDD_LINEAR_TRANSFORM_MOVE;
Alan Mishchenko committed
833
        }
834
        move->size = size;
Alan Mishchenko committed
835

836 837
        if ((double)size > (double)limitSize * table->maxGrowth)
            break;
Alan Mishchenko committed
838
        if (size < limitSize)
839
            limitSize = size;
Alan Mishchenko committed
840

841 842
        x = y;
        y = cuddZddNextHigh(table, x);
Alan Mishchenko committed
843 844 845 846 847
    }
    return(moves);

cuddZddLinearDownOutOfMem:
    while (moves != NULL) {
848 849 850
        move = moves->next;
        cuddDeallocMove(table, moves);
        moves = move;
Alan Mishchenko committed
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
    }
    return((Move *) CUDD_OUT_OF_MEM);

} /* end of cuddZddLinearDown */


/**Function********************************************************************

  Synopsis    [Given a set of moves, returns the ZDD heap to the position
  giving the minimum size.]

  Description [Given a set of moves, returns the ZDD heap to the
  position giving the minimum size. In case of ties, returns to the
  closest position giving the minimum size. Returns 1 in case of
  success; 0 otherwise.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static int
cuddZddLinearBackward(
  DdManager * table,
  int  size,
  Move * moves)
{
878 879
    Move        *move;
    int         res;
Alan Mishchenko committed
880 881 882

    /* Find the minimum size among moves. */
    for (move = moves; move != NULL; move = move->next) {
883 884 885
        if (move->size < size) {
            size = move->size;
        }
Alan Mishchenko committed
886 887 888
    }

    for (move = moves; move != NULL; move = move->next) {
889 890 891 892 893 894 895 896 897 898 899 900
        if (move->size == size) return(1);
        if (move->flags == CUDD_LINEAR_TRANSFORM_MOVE) {
            res = cuddZddLinearInPlace(table,(int)move->x,(int)move->y);
            if (!res) return(0);
        }
        res = cuddZddSwapInPlace(table, move->x, move->y);
        if (!res)
            return(0);
        if (move->flags == CUDD_INVERSE_TRANSFORM_MOVE) {
            res = cuddZddLinearInPlace(table,(int)move->x,(int)move->y);
            if (!res) return(0);
        }
Alan Mishchenko committed
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
    }

    return(1);

} /* end of cuddZddLinearBackward */


/**Function********************************************************************

  Synopsis    [Given a set of moves, returns the ZDD heap to the order
  in effect before the moves.]

  Description [Given a set of moves, returns the ZDD heap to the
  order in effect before the moves.  Returns 1 in case of success;
  0 otherwise.]

  SideEffects [None]

******************************************************************************/
static Move*
cuddZddUndoMoves(
  DdManager * table,
  Move * moves)
{
    Move *invmoves = NULL;
    Move *move;
    Move *invmove;
928
    int size;
Alan Mishchenko committed
929 930

    for (move = moves; move != NULL; move = move->next) {
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
        invmove = (Move *) cuddDynamicAllocNode(table);
        if (invmove == NULL) goto cuddZddUndoMovesOutOfMem;
        invmove->x = move->x;
        invmove->y = move->y;
        invmove->next = invmoves;
        invmoves = invmove;
        if (move->flags == CUDD_SWAP_MOVE) {
            invmove->flags = CUDD_SWAP_MOVE;
            size = cuddZddSwapInPlace(table,(int)move->x,(int)move->y);
            if (!size) goto cuddZddUndoMovesOutOfMem;
        } else if (move->flags == CUDD_LINEAR_TRANSFORM_MOVE) {
            invmove->flags = CUDD_INVERSE_TRANSFORM_MOVE;
            size = cuddZddLinearInPlace(table,(int)move->x,(int)move->y);
            if (!size) goto cuddZddUndoMovesOutOfMem;
            size = cuddZddSwapInPlace(table,(int)move->x,(int)move->y);
            if (!size) goto cuddZddUndoMovesOutOfMem;
        } else { /* must be CUDD_INVERSE_TRANSFORM_MOVE */
Alan Mishchenko committed
948
#ifdef DD_DEBUG
949
            (void) fprintf(table->err,"Unforseen event in ddUndoMoves!\n");
Alan Mishchenko committed
950
#endif
951 952 953 954 955 956 957
            invmove->flags = CUDD_LINEAR_TRANSFORM_MOVE;
            size = cuddZddSwapInPlace(table,(int)move->x,(int)move->y);
            if (!size) goto cuddZddUndoMovesOutOfMem;
            size = cuddZddLinearInPlace(table,(int)move->x,(int)move->y);
            if (!size) goto cuddZddUndoMovesOutOfMem;
        }
        invmove->size = size;
Alan Mishchenko committed
958 959 960 961 962 963
    }

    return(invmoves);

cuddZddUndoMovesOutOfMem:
    while (invmoves != NULL) {
964 965 966
        move = invmoves->next;
        cuddDeallocMove(table, invmoves);
        invmoves = move;
Alan Mishchenko committed
967 968 969 970 971
    }
    return((Move *) CUDD_OUT_OF_MEM);

} /* end of cuddZddUndoMoves */

972

973 974
ABC_NAMESPACE_IMPL_END

975