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

  FileName    [cuddSymmetry.c]

  PackageName [cudd]

  Synopsis    [Functions for symmetry-based variable reordering.]

  Description [External procedures included in this file:
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
                <ul>
                <li> Cudd_SymmProfile()
                </ul>
        Internal procedures included in this module:
                <ul>
                <li> cuddSymmCheck()
                <li> cuddSymmSifting()
                <li> cuddSymmSiftingConv()
                </ul>
        Static procedures included in this module:
                <ul>
                <li> ddSymmUniqueCompare()
                <li> ddSymmSiftingAux()
                <li> ddSymmSiftingConvAux()
                <li> ddSymmSiftingUp()
                <li> ddSymmSiftingDown()
                <li> ddSymmGroupMove()
                <li> ddSymmGroupMoveBackward()
                <li> ddSymmSiftingBackward()
                <li> ddSymmSummary()
                </ul>]
Alan Mishchenko committed
31 32 33

  Author      [Shipra Panda, Fabio Somenzi]

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 59 60 61 62 63 64
  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
65 66 67

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

Alan Mishchenko committed
68
#include "util_hack.h"
Alan Mishchenko committed
69 70
#include "cuddInt.h"

71 72 73
ABC_NAMESPACE_IMPL_START


74

Alan Mishchenko committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
/*---------------------------------------------------------------------------*/
/* Constant declarations                                                     */
/*---------------------------------------------------------------------------*/

#define MV_OOM (Move *)1

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

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

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

#ifndef lint
94
static char rcsid[] DD_UNUSED = "$Id: cuddSymmetry.c,v 1.26 2009/02/19 16:23:54 fabio Exp $";
Alan Mishchenko committed
95 96
#endif

97
static  int     *entry;
Alan Mishchenko committed
98

99
extern  int     ddTotalNumberSwapping;
Alan Mishchenko committed
100
#ifdef DD_STATS
101
extern  int     ddTotalNISwaps;
Alan Mishchenko committed
102 103 104 105 106 107 108 109 110 111 112 113
#endif

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

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

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

114 115 116 117 118 119 120 121 122
static int ddSymmUniqueCompare (int *ptrX, int *ptrY);
static int ddSymmSiftingAux (DdManager *table, int x, int xLow, int xHigh);
static int ddSymmSiftingConvAux (DdManager *table, int x, int xLow, int xHigh);
static Move * ddSymmSiftingUp (DdManager *table, int y, int xLow);
static Move * ddSymmSiftingDown (DdManager *table, int x, int xHigh);
static int ddSymmGroupMove (DdManager *table, int x, int y, Move **moves);
static int ddSymmGroupMoveBackward (DdManager *table, int x, int y);
static int ddSymmSiftingBackward (DdManager *table, Move *moves, int size);
static void ddSymmSummary (DdManager *table, int lower, int upper, int *symvars, int *symgroups);
Alan Mishchenko committed
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

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


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


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

  Synopsis    [Prints statistics on symmetric variables.]

  Description []

  SideEffects [None]

******************************************************************************/
void
Cudd_SymmProfile(
  DdManager * table,
  int  lower,
  int  upper)
{
    int i,x,gbot;
    int TotalSymm = 0;
    int TotalSymmGroups = 0;

    for (i = lower; i <= upper; i++) {
152 153 154 155 156 157 158 159 160 161
        if (table->subtables[i].next != (unsigned) i) {
            x = i;
            (void) fprintf(table->out,"Group:");
            do {
                (void) fprintf(table->out,"  %d",table->invperm[x]);
                TotalSymm++;
                gbot = x;
                x = table->subtables[x].next;
            } while (x != i);
            TotalSymmGroups++;
Alan Mishchenko committed
162
#ifdef DD_DEBUG
163
            assert(table->subtables[gbot].next == (unsigned) i);
Alan Mishchenko committed
164
#endif
165 166 167
            i = gbot;
            (void) fprintf(table->out,"\n");
        }
Alan Mishchenko committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    }
    (void) fprintf(table->out,"Total Symmetric = %d\n",TotalSymm);
    (void) fprintf(table->out,"Total Groups = %d\n",TotalSymmGroups);

} /* end of Cudd_SymmProfile */


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


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

  Synopsis    [Checks for symmetry of x and y.]

  Description [Checks for symmetry of x and y. Ignores projection
  functions, unless they are isolated. Returns 1 in case of symmetry; 0
  otherwise.]

  SideEffects [None]

******************************************************************************/
int
cuddSymmCheck(
  DdManager * table,
  int  x,
  int  y)
{
    DdNode *f,*f0,*f1,*f01,*f00,*f11,*f10;
198 199 200 201 202
    int comple;         /* f0 is complemented */
    int xsymmy;         /* x and y may be positively symmetric */
    int xsymmyp;        /* x and y may be negatively symmetric */
    int arccount;       /* number of arcs from layer x to layer y */
    int TotalRefCount;  /* total reference count of layer y minus 1 */
Alan Mishchenko committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    int yindex;
    int i;
    DdNodePtr *list;
    int slots;
    DdNode *sentinel = &(table->sentinel);
#ifdef DD_DEBUG
    int xindex;
#endif

    /* Checks that x and y are not the projection functions.
    ** For x it is sufficient to check whether there is only one
    ** node; indeed, if there is one node, it is the projection function
    ** and it cannot point to y. Hence, if y isn't just the projection
    ** function, it has one arc coming from a layer different from x.
    */
    if (table->subtables[x].keys == 1) {
219
        return(0);
Alan Mishchenko committed
220 221 222
    }
    yindex = table->invperm[y];
    if (table->subtables[y].keys == 1) {
223 224
        if (table->vars[yindex]->ref == 1)
            return(0);
Alan Mishchenko committed
225 226 227 228 229 230 231
    }

    xsymmy = xsymmyp = 1;
    arccount = 0;
    slots = table->subtables[x].slots;
    list = table->subtables[x].nodelist;
    for (i = 0; i < slots; i++) {
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
        f = list[i];
        while (f != sentinel) {
            /* Find f1, f0, f11, f10, f01, f00. */
            f1 = cuddT(f);
            f0 = Cudd_Regular(cuddE(f));
            comple = Cudd_IsComplement(cuddE(f));
            if ((int) f1->index == yindex) {
                arccount++;
                f11 = cuddT(f1); f10 = cuddE(f1);
            } else {
                if ((int) f0->index != yindex) {
                    /* If f is an isolated projection function it is
                    ** allowed to bypass layer y.
                    */
                    if (f1 != DD_ONE(table) || f0 != DD_ONE(table) || f->ref != 1)
                        return(0); /* f bypasses layer y */
                }
                f11 = f10 = f1;
            }
            if ((int) f0->index == yindex) {
                arccount++;
                f01 = cuddT(f0); f00 = cuddE(f0);
            } else {
                f01 = f00 = f0;
            }
            if (comple) {
                f01 = Cudd_Not(f01);
                f00 = Cudd_Not(f00);
            }

            if (f1 != DD_ONE(table) || f0 != DD_ONE(table) || f->ref != 1) {
                xsymmy &= f01 == f10;
                xsymmyp &= f11 == f00;
                if ((xsymmy == 0) && (xsymmyp == 0))
                    return(0);
            }

            f = f->next;
        } /* while */
Alan Mishchenko committed
271 272 273 274 275 276 277
    } /* for */

    /* Calculate the total reference counts of y */
    TotalRefCount = -1; /* -1 for projection function */
    slots = table->subtables[y].slots;
    list = table->subtables[y].nodelist;
    for (i = 0; i < slots; i++) {
278 279 280 281 282
        f = list[i];
        while (f != sentinel) {
            TotalRefCount += f->ref;
            f = f->next;
        }
Alan Mishchenko committed
283 284 285 286
    }

#if defined(DD_DEBUG) && defined(DD_VERBOSE)
    if (arccount == TotalRefCount) {
287 288 289 290
        xindex = table->invperm[x];
        (void) fprintf(table->out,
                       "Found symmetry! x =%d\ty = %d\tPos(%d,%d)\n",
                       xindex,yindex,x,y);
Alan Mishchenko committed
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
    }
#endif

    return(arccount == TotalRefCount);

} /* end of cuddSymmCheck */


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

  Synopsis    [Symmetric sifting algorithm.]

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

  SideEffects [None]

  SeeAlso     [cuddSymmSiftingConv]

******************************************************************************/
int
cuddSymmSifting(
  DdManager * table,
  int  lower,
325
  int  upper)
Alan Mishchenko committed
326
{
327 328 329 330 331 332 333
    int         i;
    int         *var;
    int         size;
    int         x;
    int         result;
    int         symvars;
    int         symgroups;
Alan Mishchenko committed
334
#ifdef DD_STATS
335
    int         previousSize;
Alan Mishchenko committed
336 337 338 339 340 341
#endif

    size = table->size;

    /* Find order in which to sift variables. */
    var = NULL;
Alan Mishchenko committed
342
    entry = ABC_ALLOC(int,size);
Alan Mishchenko committed
343
    if (entry == NULL) {
344 345
        table->errorCode = CUDD_MEMORY_OUT;
        goto ddSymmSiftingOutOfMem;
Alan Mishchenko committed
346
    }
Alan Mishchenko committed
347
    var = ABC_ALLOC(int,size);
Alan Mishchenko committed
348
    if (var == NULL) {
349 350
        table->errorCode = CUDD_MEMORY_OUT;
        goto ddSymmSiftingOutOfMem;
Alan Mishchenko committed
351 352 353
    }

    for (i = 0; i < size; i++) {
354 355 356
        x = table->perm[i];
        entry[i] = table->subtables[x].keys;
        var[i] = i;
Alan Mishchenko committed
357 358
    }

359
    qsort((void *)var,size,sizeof(int),(DD_QSFP)ddSymmUniqueCompare);
Alan Mishchenko committed
360 361 362

    /* Initialize the symmetry of each subtable to itself. */
    for (i = lower; i <= upper; i++) {
363
        table->subtables[i].next = i;
Alan Mishchenko committed
364 365 366
    }

    for (i = 0; i < ddMin(table->siftMaxVar,size); i++) {
367 368 369 370 371 372
        if (ddTotalNumberSwapping >= table->siftMaxSwap)
            break;
        // enable timeout during variable reodering - alanmi 2/13/11
        if ( table->TimeStop && table->TimeStop < clock() )
            break;
        x = table->perm[var[i]];
Alan Mishchenko committed
373
#ifdef DD_STATS
374
        previousSize = table->keys - table->isolated;
Alan Mishchenko committed
375
#endif
376 377 378 379
        if (x < lower || x > upper) continue;
        if (table->subtables[x].next == (unsigned) x) {
            result = ddSymmSiftingAux(table,x,lower,upper);
            if (!result) goto ddSymmSiftingOutOfMem;
Alan Mishchenko committed
380
#ifdef DD_STATS
381 382 383 384 385 386 387 388 389
            if (table->keys < (unsigned) previousSize + table->isolated) {
                (void) fprintf(table->out,"-");
            } else if (table->keys > (unsigned) previousSize +
                       table->isolated) {
                (void) fprintf(table->out,"+"); /* should never happen */
            } else {
                (void) fprintf(table->out,"=");
            }
            fflush(table->out);
Alan Mishchenko committed
390
#endif
391
        }
Alan Mishchenko committed
392 393
    }

Alan Mishchenko committed
394 395
    ABC_FREE(var);
    ABC_FREE(entry);
Alan Mishchenko committed
396 397 398 399 400

    ddSymmSummary(table, lower, upper, &symvars, &symgroups);

#ifdef DD_STATS
    (void) fprintf(table->out, "\n#:S_SIFTING %8d: symmetric variables\n",
401
                   symvars);
Alan Mishchenko committed
402
    (void) fprintf(table->out, "#:G_SIFTING %8d: symmetric groups",
403
                   symgroups);
Alan Mishchenko committed
404 405 406 407 408 409
#endif

    return(1+symvars);

ddSymmSiftingOutOfMem:

Alan Mishchenko committed
410 411
    if (entry != NULL) ABC_FREE(entry);
    if (var != NULL) ABC_FREE(var);
Alan Mishchenko committed
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446

    return(0);

} /* end of cuddSymmSifting */


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

  Synopsis    [Symmetric sifting to convergence algorithm.]

  Description [Symmetric sifting to convergence algorithm.
  Assumes that no dead nodes are present.
    <ol>
    <li> Order all the variables according to the number of entries in
    each unique subtable.
    <li> Sift the variable up and down, remembering each time the total
    size of the DD heap and grouping variables that are symmetric.
    <li> Select the best permutation.
    <li> Repeat 3 and 4 for all variables.
    <li> Repeat 1-4 until no further improvement.
    </ol>
  Returns 1 plus the number of symmetric variables if successful; 0
  otherwise.]

  SideEffects [None]

  SeeAlso     [cuddSymmSifting]

******************************************************************************/
int
cuddSymmSiftingConv(
  DdManager * table,
  int  lower,
  int  upper)
{
447 448 449 450 451 452 453 454 455
    int         i;
    int         *var;
    int         size;
    int         x;
    int         result;
    int         symvars;
    int         symgroups;
    int         classes;
    int         initialSize;
Alan Mishchenko committed
456
#ifdef DD_STATS
457
    int         previousSize;
Alan Mishchenko committed
458 459 460 461 462 463 464 465
#endif

    initialSize = table->keys - table->isolated;

    size = table->size;

    /* Find order in which to sift variables. */
    var = NULL;
Alan Mishchenko committed
466
    entry = ABC_ALLOC(int,size);
Alan Mishchenko committed
467
    if (entry == NULL) {
468 469
        table->errorCode = CUDD_MEMORY_OUT;
        goto ddSymmSiftingConvOutOfMem;
Alan Mishchenko committed
470
    }
Alan Mishchenko committed
471
    var = ABC_ALLOC(int,size);
Alan Mishchenko committed
472
    if (var == NULL) {
473 474
        table->errorCode = CUDD_MEMORY_OUT;
        goto ddSymmSiftingConvOutOfMem;
Alan Mishchenko committed
475 476 477
    }

    for (i = 0; i < size; i++) {
478 479 480
        x = table->perm[i];
        entry[i] = table->subtables[x].keys;
        var[i] = i;
Alan Mishchenko committed
481 482
    }

483
    qsort((void *)var,size,sizeof(int),(DD_QSFP)ddSymmUniqueCompare);
Alan Mishchenko committed
484 485 486 487 488

    /* Initialize the symmetry of each subtable to itself
    ** for first pass of converging symmetric sifting.
    */
    for (i = lower; i <= upper; i++) {
489
        table->subtables[i].next = i;
Alan Mishchenko committed
490 491 492
    }

    for (i = 0; i < ddMin(table->siftMaxVar, table->size); i++) {
493 494 495 496 497 498
        if (ddTotalNumberSwapping >= table->siftMaxSwap)
            break;
        x = table->perm[var[i]];
        if (x < lower || x > upper) continue;
        /* Only sift if not in symmetry group already. */
        if (table->subtables[x].next == (unsigned) x) {
Alan Mishchenko committed
499
#ifdef DD_STATS
500
            previousSize = table->keys - table->isolated;
Alan Mishchenko committed
501
#endif
502 503
            result = ddSymmSiftingAux(table,x,lower,upper);
            if (!result) goto ddSymmSiftingConvOutOfMem;
Alan Mishchenko committed
504
#ifdef DD_STATS
505 506 507 508 509 510 511 512 513
            if (table->keys < (unsigned) previousSize + table->isolated) {
                (void) fprintf(table->out,"-");
            } else if (table->keys > (unsigned) previousSize +
                       table->isolated) {
                (void) fprintf(table->out,"+");
            } else {
                (void) fprintf(table->out,"=");
            }
            fflush(table->out);
Alan Mishchenko committed
514
#endif
515
        }
Alan Mishchenko committed
516 517 518 519
    }

    /* Sifting now until convergence. */
    while ((unsigned) initialSize > table->keys - table->isolated) {
520
        initialSize = table->keys - table->isolated;
Alan Mishchenko committed
521
#ifdef DD_STATS
522
        (void) fprintf(table->out,"\n");
Alan Mishchenko committed
523 524
#endif
        /* Here we consider only one representative for each symmetry class. */
525 526 527 528 529 530 531 532 533 534 535
        for (x = lower, classes = 0; x <= upper; x++, classes++) {
            while ((unsigned) x < table->subtables[x].next) {
                x = table->subtables[x].next;
            }
            /* Here x is the largest index in a group.
            ** Groups consist of adjacent variables.
            ** Hence, the next increment of x will move it to a new group.
            */
            i = table->invperm[x];
            entry[i] = table->subtables[x].keys;
            var[classes] = i;
Alan Mishchenko committed
536 537
        }

538
        qsort((void *)var,classes,sizeof(int),(DD_QSFP)ddSymmUniqueCompare);
Alan Mishchenko committed
539

540 541 542 543 544 545
        /* Now sift. */
        for (i = 0; i < ddMin(table->siftMaxVar,classes); i++) {
            if (ddTotalNumberSwapping >= table->siftMaxSwap)
                break;
            x = table->perm[var[i]];
            if ((unsigned) x >= table->subtables[x].next) {
Alan Mishchenko committed
546
#ifdef DD_STATS
547
                previousSize = table->keys - table->isolated;
Alan Mishchenko committed
548
#endif
549 550
                result = ddSymmSiftingConvAux(table,x,lower,upper);
                if (!result ) goto ddSymmSiftingConvOutOfMem;
Alan Mishchenko committed
551
#ifdef DD_STATS
552 553 554 555 556 557 558 559 560
                if (table->keys < (unsigned) previousSize + table->isolated) {
                    (void) fprintf(table->out,"-");
                } else if (table->keys > (unsigned) previousSize +
                           table->isolated) {
                    (void) fprintf(table->out,"+");
                } else {
                    (void) fprintf(table->out,"=");
                }
                fflush(table->out);
Alan Mishchenko committed
561
#endif
562 563
            }
        } /* for */
Alan Mishchenko committed
564 565 566 567 568 569
    }

    ddSymmSummary(table, lower, upper, &symvars, &symgroups);

#ifdef DD_STATS
    (void) fprintf(table->out, "\n#:S_SIFTING %8d: symmetric variables\n",
570
                   symvars);
Alan Mishchenko committed
571
    (void) fprintf(table->out, "#:G_SIFTING %8d: symmetric groups",
572
                   symgroups);
Alan Mishchenko committed
573 574
#endif

Alan Mishchenko committed
575 576
    ABC_FREE(var);
    ABC_FREE(entry);
Alan Mishchenko committed
577 578 579 580 581

    return(1+symvars);

ddSymmSiftingConvOutOfMem:

Alan Mishchenko committed
582 583
    if (entry != NULL) ABC_FREE(entry);
    if (var != NULL) ABC_FREE(var);
Alan Mishchenko committed
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613

    return(0);

} /* end of cuddSymmSiftingConv */


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


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

  Synopsis    [Comparison function used by qsort.]

  Description [Comparison function used by qsort to order the variables
  according to the number of keys in the subtables.
  Returns the difference in number of keys between the two
  variables being compared.]

  SideEffects [None]

******************************************************************************/
static int
ddSymmUniqueCompare(
  int * ptrX,
  int * ptrY)
{
#if 0
    if (entry[*ptrY] == entry[*ptrX]) {
614
        return((*ptrX) - (*ptrY));
Alan Mishchenko committed
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
    }
#endif
    return(entry[*ptrY] - entry[*ptrX]);

} /* end of ddSymmUniqueCompare */


/**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.
  Assumes that x is not part of a symmetry group. Returns 1 if
  successful; 0 otherwise.]

  SideEffects [None]

******************************************************************************/
static int
ddSymmSiftingAux(
  DdManager * table,
  int  x,
  int  xLow,
  int  xHigh)
{
    Move *move;
643 644 645 646
    Move *moveUp;       /* list of up moves */
    Move *moveDown;     /* list of down moves */
    int  initialSize;
    int  result;
Alan Mishchenko committed
647
    int  i;
648
    int  topbot;        /* index to either top or bottom of symmetry group */
Alan Mishchenko committed
649 650 651 652 653 654 655 656 657 658 659 660 661 662
    int  initGroupSize, finalGroupSize;


#ifdef DD_DEBUG
    /* check for previously detected symmetry */
    assert(table->subtables[x].next == (unsigned) x);
#endif

    initialSize = table->keys - table->isolated;

    moveDown = NULL;
    moveUp = NULL;

    if ((x - xLow) > (xHigh - x)) {
663 664 665 666 667 668 669 670 671 672 673 674
        /* Will go down first, unless x == xHigh:
        ** Look for consecutive symmetries above x.
        */
        for (i = x; i > xLow; i--) {
            if (!cuddSymmCheck(table,i-1,i))
                break;
            topbot = table->subtables[i-1].next; /* find top of i-1's group */
            table->subtables[i-1].next = i;
            table->subtables[x].next = topbot; /* x is bottom of group so its */
                                               /* next is top of i-1's group */
            i = topbot + 1; /* add 1 for i--; new i is top of symm group */
        }
Alan Mishchenko committed
675
    } else {
676 677 678 679 680 681 682 683 684 685 686 687 688 689
        /* Will go up first unless x == xlow:
        ** Look for consecutive symmetries below x.
        */
        for (i = x; i < xHigh; i++) {
            if (!cuddSymmCheck(table,i,i+1))
                break;
            /* find bottom of i+1's symm group */
            topbot = i + 1;
            while ((unsigned) topbot < table->subtables[topbot].next) {
                topbot = table->subtables[topbot].next;
            }
            table->subtables[topbot].next = table->subtables[i].next;
            table->subtables[i].next = i + 1;
            i = topbot - 1; /* subtract 1 for i++; new i is bottom of group */
Alan Mishchenko committed
690 691 692 693 694 695 696
        }
    }

    /* Now x may be in the middle of a symmetry group.
    ** Find bottom of x's symm group.
    */
    while ((unsigned) x < table->subtables[x].next)
697
        x = table->subtables[x].next;
Alan Mishchenko committed
698 699 700 701

    if (x == xLow) { /* Sift down */

#ifdef DD_DEBUG
702 703
        /* x must be a singleton */
        assert((unsigned) x == table->subtables[x].next);
Alan Mishchenko committed
704
#endif
705
        if (x == xHigh) return(1);      /* just one variable */
Alan Mishchenko committed
706

707
        initGroupSize = 1;
Alan Mishchenko committed
708

709 710 711 712
        moveDown = ddSymmSiftingDown(table,x,xHigh);
            /* after this point x --> xHigh, unless early term */
        if (moveDown == MV_OOM) goto ddSymmSiftingAuxOutOfMem;
        if (moveDown == NULL) return(1);
Alan Mishchenko committed
713

714 715 716 717 718 719
        x = moveDown->y;
        /* Find bottom of x's group */
        i = x;
        while ((unsigned) i < table->subtables[i].next) {
            i = table->subtables[i].next;
        }
Alan Mishchenko committed
720
#ifdef DD_DEBUG
721 722 723
        /* x should be the top of the symmetry group and i the bottom */
        assert((unsigned) i >= table->subtables[i].next);
        assert((unsigned) x == table->subtables[i].next);
Alan Mishchenko committed
724
#endif
725
        finalGroupSize = i - x + 1;
Alan Mishchenko committed
726

727 728 729 730 731 732 733 734 735
        if (initGroupSize == finalGroupSize) {
            /* No new symmetry groups detected, return to best position */
            result = ddSymmSiftingBackward(table,moveDown,initialSize);
        } else {
            initialSize = table->keys - table->isolated;
            moveUp = ddSymmSiftingUp(table,x,xLow);
            result = ddSymmSiftingBackward(table,moveUp,initialSize);
        }
        if (!result) goto ddSymmSiftingAuxOutOfMem;
Alan Mishchenko committed
736 737

    } else if (cuddNextHigh(table,x) > xHigh) { /* Sift up */
738 739 740
        /* Find top of x's symm group */
        i = x;                          /* bottom */
        x = table->subtables[x].next;   /* top */
Alan Mishchenko committed
741

742
        if (x == xLow) return(1); /* just one big group */
Alan Mishchenko committed
743

744
        initGroupSize = i - x + 1;
Alan Mishchenko committed
745

746 747 748 749
        moveUp = ddSymmSiftingUp(table,x,xLow);
            /* after this point x --> xLow, unless early term */
        if (moveUp == MV_OOM) goto ddSymmSiftingAuxOutOfMem;
        if (moveUp == NULL) return(1);
Alan Mishchenko committed
750

751 752 753
        x = moveUp->x;
        /* Find top of x's group */
        i = table->subtables[x].next;
Alan Mishchenko committed
754
#ifdef DD_DEBUG
755 756 757
        /* x should be the bottom of the symmetry group and i the top */
        assert((unsigned) x >= table->subtables[x].next);
        assert((unsigned) i == table->subtables[x].next);
Alan Mishchenko committed
758
#endif
759
        finalGroupSize = x - i + 1;
Alan Mishchenko committed
760

761 762 763 764 765 766 767 768 769
        if (initGroupSize == finalGroupSize) {
            /* No new symmetry groups detected, return to best position */
            result = ddSymmSiftingBackward(table,moveUp,initialSize);
        } else {
            initialSize = table->keys - table->isolated;
            moveDown = ddSymmSiftingDown(table,x,xHigh);
            result = ddSymmSiftingBackward(table,moveDown,initialSize);
        }
        if (!result) goto ddSymmSiftingAuxOutOfMem;
Alan Mishchenko committed
770 771 772

    } else if ((x - xLow) > (xHigh - x)) { /* must go down first: shorter */

773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
        moveDown = ddSymmSiftingDown(table,x,xHigh);
        /* at this point x == xHigh, unless early term */
        if (moveDown == MV_OOM) goto ddSymmSiftingAuxOutOfMem;

        if (moveDown != NULL) {
            x = moveDown->y;    /* x is top here */
            i = x;
            while ((unsigned) i < table->subtables[i].next) {
                i = table->subtables[i].next;
            }
        } else {
            i = x;
            while ((unsigned) i < table->subtables[i].next) {
                i = table->subtables[i].next;
            }
            x = table->subtables[i].next;
Alan Mishchenko committed
789 790 791
        }
#ifdef DD_DEBUG
        /* x should be the top of the symmetry group and i the bottom */
792 793
        assert((unsigned) i >= table->subtables[i].next);
        assert((unsigned) x == table->subtables[i].next);
Alan Mishchenko committed
794
#endif
795
        initGroupSize = i - x + 1;
Alan Mishchenko committed
796

797 798
        moveUp = ddSymmSiftingUp(table,x,xLow);
        if (moveUp == MV_OOM) goto ddSymmSiftingAuxOutOfMem;
Alan Mishchenko committed
799

800 801 802 803 804 805 806 807
        if (moveUp != NULL) {
            x = moveUp->x;
            i = table->subtables[x].next;
        } else {
            i = x;
            while ((unsigned) x < table->subtables[x].next)
                x = table->subtables[x].next;
        }
Alan Mishchenko committed
808
#ifdef DD_DEBUG
809 810 811
        /* x should be the bottom of the symmetry group and i the top */
        assert((unsigned) x >= table->subtables[x].next);
        assert((unsigned) i == table->subtables[x].next);
Alan Mishchenko committed
812
#endif
813
        finalGroupSize = x - i + 1;
Alan Mishchenko committed
814

815 816 817 818 819 820 821 822 823 824 825 826
        if (initGroupSize == finalGroupSize) {
            /* No new symmetry groups detected, return to best position */
            result = ddSymmSiftingBackward(table,moveUp,initialSize);
        } else {
            while (moveDown != NULL) {
                move = moveDown->next;
                cuddDeallocMove(table, moveDown);
                moveDown = move;
            }
            initialSize = table->keys - table->isolated;
            moveDown = ddSymmSiftingDown(table,x,xHigh);
            result = ddSymmSiftingBackward(table,moveDown,initialSize);
Alan Mishchenko committed
827
        }
828
        if (!result) goto ddSymmSiftingAuxOutOfMem;
Alan Mishchenko committed
829 830 831

    } else { /* moving up first: shorter */
        /* Find top of x's symmetry group */
832
        x = table->subtables[x].next;
Alan Mishchenko committed
833

834 835 836
        moveUp = ddSymmSiftingUp(table,x,xLow);
        /* at this point x == xHigh, unless early term */
        if (moveUp == MV_OOM) goto ddSymmSiftingAuxOutOfMem;
Alan Mishchenko committed
837

838 839 840 841 842 843 844 845
        if (moveUp != NULL) {
            x = moveUp->x;
            i = table->subtables[x].next;
        } else {
            while ((unsigned) x < table->subtables[x].next)
                x = table->subtables[x].next;
            i = table->subtables[x].next;
        }
Alan Mishchenko committed
846 847
#ifdef DD_DEBUG
        /* x is bottom of the symmetry group and i is top */
848 849
        assert((unsigned) x >= table->subtables[x].next);
        assert((unsigned) i == table->subtables[x].next);
Alan Mishchenko committed
850
#endif
851
        initGroupSize = x - i + 1;
Alan Mishchenko committed
852

853 854 855 856 857 858 859 860 861 862 863 864
        moveDown = ddSymmSiftingDown(table,x,xHigh);
        if (moveDown == MV_OOM) goto ddSymmSiftingAuxOutOfMem;

        if (moveDown != NULL) {
            x = moveDown->y;
            i = x;
            while ((unsigned) i < table->subtables[i].next) {
                i = table->subtables[i].next;
            }
        } else {
            i = x;
            x = table->subtables[x].next;
Alan Mishchenko committed
865 866
        }
#ifdef DD_DEBUG
867 868 869
        /* x should be the top of the symmetry group and i the bottom */
        assert((unsigned) i >= table->subtables[i].next);
        assert((unsigned) x == table->subtables[i].next);
Alan Mishchenko committed
870
#endif
871
        finalGroupSize = i - x + 1;
Alan Mishchenko committed
872

873 874 875 876 877 878 879 880 881 882 883 884
        if (initGroupSize == finalGroupSize) {
            /* No new symmetries detected, go back to best position */
            result = ddSymmSiftingBackward(table,moveDown,initialSize);
        } else {
            while (moveUp != NULL) {
                move = moveUp->next;
                cuddDeallocMove(table, moveUp);
                moveUp = move;
            }
            initialSize = table->keys - table->isolated;
            moveUp = ddSymmSiftingUp(table,x,xLow);
            result = ddSymmSiftingBackward(table,moveUp,initialSize);
Alan Mishchenko committed
885
        }
886
        if (!result) goto ddSymmSiftingAuxOutOfMem;
Alan Mishchenko committed
887 888 889
    }

    while (moveDown != NULL) {
890 891 892
        move = moveDown->next;
        cuddDeallocMove(table, moveDown);
        moveDown = move;
Alan Mishchenko committed
893 894
    }
    while (moveUp != NULL) {
895 896 897
        move = moveUp->next;
        cuddDeallocMove(table, moveUp);
        moveUp = move;
Alan Mishchenko committed
898 899 900 901 902 903
    }

    return(1);

ddSymmSiftingAuxOutOfMem:
    if (moveDown != MV_OOM) {
904 905 906 907 908
        while (moveDown != NULL) {
            move = moveDown->next;
            cuddDeallocMove(table, moveDown);
            moveDown = move;
        }
Alan Mishchenko committed
909 910
    }
    if (moveUp != MV_OOM) {
911 912 913 914 915
        while (moveUp != NULL) {
            move = moveUp->next;
            cuddDeallocMove(table, moveUp);
            moveUp = move;
        }
Alan Mishchenko committed
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
    }

    return(0);

} /* end of ddSymmSiftingAux */


/**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.
  Assumes that x is either an isolated variable, or it is the bottom of
  a symmetry group. All symmetries may not have been found, because of
  exceeded growth limit. Returns 1 if successful; 0 otherwise.]

  SideEffects [None]

******************************************************************************/
static int
ddSymmSiftingConvAux(
  DdManager * table,
  int  x,
  int  xLow,
  int  xHigh)
{
    Move *move;
945 946 947 948
    Move *moveUp;       /* list of up moves */
    Move *moveDown;     /* list of down moves */
    int  initialSize;
    int  result;
Alan Mishchenko committed
949 950 951 952 953 954 955 956 957 958 959
    int  i;
    int  initGroupSize, finalGroupSize;


    initialSize = table->keys - table->isolated;

    moveDown = NULL;
    moveUp = NULL;

    if (x == xLow) { /* Sift down */
#ifdef DD_DEBUG
960 961
        /* x is bottom of symmetry group */
        assert((unsigned) x >= table->subtables[x].next);
Alan Mishchenko committed
962 963
#endif
        i = table->subtables[x].next;
964
        initGroupSize = x - i + 1;
Alan Mishchenko committed
965

966 967 968 969
        moveDown = ddSymmSiftingDown(table,x,xHigh);
        /* at this point x == xHigh, unless early term */
        if (moveDown == MV_OOM) goto ddSymmSiftingConvAuxOutOfMem;
        if (moveDown == NULL) return(1);
Alan Mishchenko committed
970

971 972 973 974 975
        x = moveDown->y;
        i = x;
        while ((unsigned) i < table->subtables[i].next) {
            i = table->subtables[i].next;
        }
Alan Mishchenko committed
976
#ifdef DD_DEBUG
977 978 979
        /* x should be the top of the symmetric group and i the bottom */
        assert((unsigned) i >= table->subtables[i].next);
        assert((unsigned) x == table->subtables[i].next);
Alan Mishchenko committed
980
#endif
981
        finalGroupSize = i - x + 1;
Alan Mishchenko committed
982

983 984 985 986 987 988 989 990 991
        if (initGroupSize == finalGroupSize) {
            /* No new symmetries detected, go back to best position */
            result = ddSymmSiftingBackward(table,moveDown,initialSize);
        } else {
            initialSize = table->keys - table->isolated;
            moveUp = ddSymmSiftingUp(table,x,xLow);
            result = ddSymmSiftingBackward(table,moveUp,initialSize);
        }
        if (!result) goto ddSymmSiftingConvAuxOutOfMem;
Alan Mishchenko committed
992 993

    } else if (cuddNextHigh(table,x) > xHigh) { /* Sift up */
994 995 996 997 998
        /* Find top of x's symm group */
        while ((unsigned) x < table->subtables[x].next)
            x = table->subtables[x].next;
        i = x;                          /* bottom */
        x = table->subtables[x].next;   /* top */
Alan Mishchenko committed
999

1000
        if (x == xLow) return(1);
Alan Mishchenko committed
1001

1002
        initGroupSize = i - x + 1;
Alan Mishchenko committed
1003

1004 1005 1006 1007
        moveUp = ddSymmSiftingUp(table,x,xLow);
            /* at this point x == xLow, unless early term */
        if (moveUp == MV_OOM) goto ddSymmSiftingConvAuxOutOfMem;
        if (moveUp == NULL) return(1);
Alan Mishchenko committed
1008

1009 1010
        x = moveUp->x;
        i = table->subtables[x].next;
Alan Mishchenko committed
1011
#ifdef DD_DEBUG
1012 1013 1014
        /* x should be the bottom of the symmetry group and i the top */
        assert((unsigned) x >= table->subtables[x].next);
        assert((unsigned) i == table->subtables[x].next);
Alan Mishchenko committed
1015
#endif
1016
        finalGroupSize = x - i + 1;
Alan Mishchenko committed
1017

1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
        if (initGroupSize == finalGroupSize) {
            /* No new symmetry groups detected, return to best position */
            result = ddSymmSiftingBackward(table,moveUp,initialSize);
        } else {
            initialSize = table->keys - table->isolated;
            moveDown = ddSymmSiftingDown(table,x,xHigh);
            result = ddSymmSiftingBackward(table,moveDown,initialSize);
        }
        if (!result)
            goto ddSymmSiftingConvAuxOutOfMem;
Alan Mishchenko committed
1028 1029

    } else if ((x - xLow) > (xHigh - x)) { /* must go down first: shorter */
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
        moveDown = ddSymmSiftingDown(table,x,xHigh);
            /* at this point x == xHigh, unless early term */
        if (moveDown == MV_OOM) goto ddSymmSiftingConvAuxOutOfMem;

        if (moveDown != NULL) {
            x = moveDown->y;
            i = x;
            while ((unsigned) i < table->subtables[i].next) {
                i = table->subtables[i].next;
            }
        } else {
            while ((unsigned) x < table->subtables[x].next)
                x = table->subtables[x].next;
            i = x;
            x = table->subtables[x].next;
Alan Mishchenko committed
1045 1046 1047
        }
#ifdef DD_DEBUG
        /* x should be the top of the symmetry group and i the bottom */
1048 1049
        assert((unsigned) i >= table->subtables[i].next);
        assert((unsigned) x == table->subtables[i].next);
Alan Mishchenko committed
1050
#endif
1051
        initGroupSize = i - x + 1;
Alan Mishchenko committed
1052

1053 1054
        moveUp = ddSymmSiftingUp(table,x,xLow);
        if (moveUp == MV_OOM) goto ddSymmSiftingConvAuxOutOfMem;
Alan Mishchenko committed
1055

1056 1057 1058 1059 1060 1061 1062 1063
        if (moveUp != NULL) {
            x = moveUp->x;
            i = table->subtables[x].next;
        } else {
            i = x;
            while ((unsigned) x < table->subtables[x].next)
                x = table->subtables[x].next;
        }
Alan Mishchenko committed
1064
#ifdef DD_DEBUG
1065 1066 1067
        /* x should be the bottom of the symmetry group and i the top */
        assert((unsigned) x >= table->subtables[x].next);
        assert((unsigned) i == table->subtables[x].next);
Alan Mishchenko committed
1068
#endif
1069
        finalGroupSize = x - i + 1;
Alan Mishchenko committed
1070

1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
        if (initGroupSize == finalGroupSize) {
            /* No new symmetry groups detected, return to best position */
            result = ddSymmSiftingBackward(table,moveUp,initialSize);
        } else {
            while (moveDown != NULL) {
                move = moveDown->next;
                cuddDeallocMove(table, moveDown);
                moveDown = move;
            }
            initialSize = table->keys - table->isolated;
            moveDown = ddSymmSiftingDown(table,x,xHigh);
            result = ddSymmSiftingBackward(table,moveDown,initialSize);
Alan Mishchenko committed
1083
        }
1084
        if (!result) goto ddSymmSiftingConvAuxOutOfMem;
Alan Mishchenko committed
1085 1086

    } else { /* moving up first: shorter */
1087 1088
        /* Find top of x's symmetry group */
        x = table->subtables[x].next;
Alan Mishchenko committed
1089

1090 1091 1092
        moveUp = ddSymmSiftingUp(table,x,xLow);
        /* at this point x == xHigh, unless early term */
        if (moveUp == MV_OOM) goto ddSymmSiftingConvAuxOutOfMem;
Alan Mishchenko committed
1093

1094 1095 1096 1097 1098 1099 1100 1101
        if (moveUp != NULL) {
            x = moveUp->x;
            i = table->subtables[x].next;
        } else {
            i = x;
            while ((unsigned) x < table->subtables[x].next)
                x = table->subtables[x].next;
        }
Alan Mishchenko committed
1102 1103 1104 1105 1106 1107 1108
#ifdef DD_DEBUG
        /* x is bottom of the symmetry group and i is top */
        assert((unsigned) x >= table->subtables[x].next);
        assert((unsigned) i == table->subtables[x].next);
#endif
        initGroupSize = x - i + 1;

1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
        moveDown = ddSymmSiftingDown(table,x,xHigh);
        if (moveDown == MV_OOM) goto ddSymmSiftingConvAuxOutOfMem;

        if (moveDown != NULL) {
            x = moveDown->y;
            i = x;
            while ((unsigned) i < table->subtables[i].next) {
                i = table->subtables[i].next;
            }
        } else {
            i = x;
            x = table->subtables[x].next;
Alan Mishchenko committed
1121 1122
        }
#ifdef DD_DEBUG
1123 1124 1125
        /* x should be the top of the symmetry group and i the bottom */
        assert((unsigned) i >= table->subtables[i].next);
        assert((unsigned) x == table->subtables[i].next);
Alan Mishchenko committed
1126
#endif
1127
        finalGroupSize = i - x + 1;
Alan Mishchenko committed
1128

1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
        if (initGroupSize == finalGroupSize) {
            /* No new symmetries detected, go back to best position */
            result = ddSymmSiftingBackward(table,moveDown,initialSize);
        } else {
            while (moveUp != NULL) {
                move = moveUp->next;
                cuddDeallocMove(table, moveUp);
                moveUp = move;
            }
            initialSize = table->keys - table->isolated;
            moveUp = ddSymmSiftingUp(table,x,xLow);
            result = ddSymmSiftingBackward(table,moveUp,initialSize);
Alan Mishchenko committed
1141
        }
1142
        if (!result) goto ddSymmSiftingConvAuxOutOfMem;
Alan Mishchenko committed
1143 1144 1145
    }

    while (moveDown != NULL) {
1146 1147 1148
        move = moveDown->next;
        cuddDeallocMove(table, moveDown);
        moveDown = move;
Alan Mishchenko committed
1149 1150
    }
    while (moveUp != NULL) {
1151 1152 1153
        move = moveUp->next;
        cuddDeallocMove(table, moveUp);
        moveUp = move;
Alan Mishchenko committed
1154 1155 1156 1157 1158 1159
    }

    return(1);

ddSymmSiftingConvAuxOutOfMem:
    if (moveDown != MV_OOM) {
1160 1161 1162 1163 1164
        while (moveDown != NULL) {
            move = moveDown->next;
            cuddDeallocMove(table, moveDown);
            moveDown = move;
        }
Alan Mishchenko committed
1165 1166
    }
    if (moveUp != MV_OOM) {
1167 1168 1169 1170 1171
        while (moveUp != NULL) {
            move = moveUp->next;
            cuddDeallocMove(table, moveUp);
            moveUp = move;
        }
Alan Mishchenko committed
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
    }

    return(0);

} /* end of ddSymmSiftingConvAux */


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

  Synopsis    [Moves x up until either it reaches the bound (xLow) or
  the size of the DD heap increases too much.]

  Description [Moves x up until either it reaches the bound (xLow) or
  the size of the DD heap increases too much. Assumes that x is the top
  of a symmetry group.  Checks x for symmetry to the adjacent
  variables. If symmetry is found, the symmetry group of x is merged
  with the symmetry group of the other variable. Returns the set of
  moves in case of success; MV_OOM if memory is full.]

  SideEffects [None]

******************************************************************************/
static Move *
ddSymmSiftingUp(
  DdManager * table,
  int  y,
  int  xLow)
{
    Move *moves;
    Move *move;
1202 1203
    int  x;
    int  size;
Alan Mishchenko committed
1204 1205 1206 1207 1208 1209 1210
    int  i;
    int  gxtop,gybot;
    int  limitSize;
    int  xindex, yindex;
    int  zindex;
    int  z;
    int  isolated;
1211
    int  L;     /* lower bound on DD size */
Alan Mishchenko committed
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
#ifdef DD_DEBUG
    int  checkL;
#endif


    moves = NULL;
    yindex = table->invperm[y];

    /* Initialize the lower bound.
    ** The part of the DD below the bottom of y' group will not change.
    ** The part of the DD above y that does not interact with y will not
    ** change. The rest may vanish in the best case, except for
    ** the nodes at level xLow, which will not vanish, regardless.
    */
    limitSize = L = table->keys - table->isolated;
    gybot = y;
    while ((unsigned) gybot < table->subtables[gybot].next)
1229
        gybot = table->subtables[gybot].next;
Alan Mishchenko committed
1230
    for (z = xLow + 1; z <= gybot; z++) {
1231 1232 1233 1234 1235
        zindex = table->invperm[z];
        if (zindex == yindex || cuddTestInteract(table,zindex,yindex)) {
            isolated = table->vars[zindex]->ref == 1;
            L -= table->subtables[z].keys - isolated;
        }
Alan Mishchenko committed
1236 1237 1238 1239 1240
    }

    x = cuddNextLow(table,y);
    while (x >= xLow && L <= limitSize) {
#ifdef DD_DEBUG
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
        gybot = y;
        while ((unsigned) gybot < table->subtables[gybot].next)
            gybot = table->subtables[gybot].next;
        checkL = table->keys - table->isolated;
        for (z = xLow + 1; z <= gybot; z++) {
            zindex = table->invperm[z];
            if (zindex == yindex || cuddTestInteract(table,zindex,yindex)) {
                isolated = table->vars[zindex]->ref == 1;
                checkL -= table->subtables[z].keys - isolated;
            }
Alan Mishchenko committed
1251
        }
1252
        assert(L == checkL);
Alan Mishchenko committed
1253
#endif
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
        gxtop = table->subtables[x].next;
        if (cuddSymmCheck(table,x,y)) {
            /* Symmetry found, attach symm groups */
            table->subtables[x].next = y;
            i = table->subtables[y].next;
            while (table->subtables[i].next != (unsigned) y)
                i = table->subtables[i].next;
            table->subtables[i].next = gxtop;
        } else if (table->subtables[x].next == (unsigned) x &&
                   table->subtables[y].next == (unsigned) y) {
            /* x and y have self symmetry */
            xindex = table->invperm[x];
            size = cuddSwapInPlace(table,x,y);
Alan Mishchenko committed
1267
#ifdef DD_DEBUG
1268 1269
            assert(table->subtables[x].next == (unsigned) x);
            assert(table->subtables[y].next == (unsigned) y);
Alan Mishchenko committed
1270
#endif
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
            if (size == 0) goto ddSymmSiftingUpOutOfMem;
            /* Update the lower bound. */
            if (cuddTestInteract(table,xindex,yindex)) {
                isolated = table->vars[xindex]->ref == 1;
                L += table->subtables[y].keys - isolated;
            }
            move = (Move *) cuddDynamicAllocNode(table);
            if (move == NULL) goto ddSymmSiftingUpOutOfMem;
            move->x = x;
            move->y = y;
            move->size = size;
            move->next = moves;
            moves = move;
            if ((double) size > (double) limitSize * table->maxGrowth)
                return(moves);
            if (size < limitSize) limitSize = size;
        } else { /* Group move */
            size = ddSymmGroupMove(table,x,y,&moves);
            if (size == 0) goto ddSymmSiftingUpOutOfMem;
            /* Update the lower bound. */
            z = moves->y;
            do {
                zindex = table->invperm[z];
                if (cuddTestInteract(table,zindex,yindex)) {
                    isolated = table->vars[zindex]->ref == 1;
                    L += table->subtables[z].keys - isolated;
                }
                z = table->subtables[z].next;
            } while (z != (int) moves->y);
            if ((double) size > (double) limitSize * table->maxGrowth)
                return(moves);
            if (size < limitSize) limitSize = size;
Alan Mishchenko committed
1303
        }
1304 1305
        y = gxtop;
        x = cuddNextLow(table,y);
Alan Mishchenko committed
1306 1307 1308 1309 1310 1311
    }

    return(moves);

ddSymmSiftingUpOutOfMem:
    while (moves != NULL) {
1312 1313 1314
        move = moves->next;
        cuddDeallocMove(table, moves);
        moves = move;
Alan Mishchenko committed
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
    }
    return(MV_OOM);

} /* end of ddSymmSiftingUp */


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

  Synopsis    [Moves x down until either it reaches the bound (xHigh) or
  the size of the DD heap increases too much.]

  Description [Moves x down until either it reaches the bound (xHigh)
  or the size of the DD heap increases too much. Assumes that x is the
  bottom of a symmetry group. Checks x for symmetry to the adjacent
  variables. If symmetry is found, the symmetry group of x is merged
  with the symmetry group of the other variable. Returns the set of
  moves in case of success; MV_OOM if memory is full.]

  SideEffects [None]

******************************************************************************/
static Move *
ddSymmSiftingDown(
  DdManager * table,
  int  x,
  int  xHigh)
{
    Move *moves;
    Move *move;
1344 1345
    int  y;
    int  size;
Alan Mishchenko committed
1346 1347
    int  limitSize;
    int  gxtop,gybot;
1348
    int  R;     /* upper bound on node decrease */
Alan Mishchenko committed
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363
    int  xindex, yindex;
    int  isolated;
    int  z;
    int  zindex;
#ifdef DD_DEBUG
    int  checkR;
#endif

    moves = NULL;
    /* Initialize R */
    xindex = table->invperm[x];
    gxtop = table->subtables[x].next;
    limitSize = size = table->keys - table->isolated;
    R = 0;
    for (z = xHigh; z > gxtop; z--) {
1364 1365 1366 1367 1368
        zindex = table->invperm[z];
        if (zindex == xindex || cuddTestInteract(table,xindex,zindex)) {
            isolated = table->vars[zindex]->ref == 1;
            R += table->subtables[z].keys - isolated;
        }
Alan Mishchenko committed
1369 1370 1371 1372 1373 1374
    }

    y = cuddNextHigh(table,x);
    while (y <= xHigh && size - R < limitSize) {
#ifdef DD_DEBUG
        gxtop = table->subtables[x].next;
1375 1376 1377 1378 1379 1380 1381
        checkR = 0;
        for (z = xHigh; z > gxtop; z--) {
            zindex = table->invperm[z];
            if (zindex == xindex || cuddTestInteract(table,xindex,zindex)) {
                isolated = table->vars[zindex]->ref == 1;
                checkR += table->subtables[z].keys - isolated;
            }
Alan Mishchenko committed
1382
        }
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
        assert(R == checkR);
#endif
        gybot = table->subtables[y].next;
        while (table->subtables[gybot].next != (unsigned) y)
            gybot = table->subtables[gybot].next;
        if (cuddSymmCheck(table,x,y)) {
            /* Symmetry found, attach symm groups */
            gxtop = table->subtables[x].next;
            table->subtables[x].next = y;
            table->subtables[gybot].next = gxtop;
        } else if (table->subtables[x].next == (unsigned) x &&
                   table->subtables[y].next == (unsigned) y) {
            /* x and y have self symmetry */
            /* Update upper bound on node decrease. */
            yindex = table->invperm[y];
            if (cuddTestInteract(table,xindex,yindex)) {
                isolated = table->vars[yindex]->ref == 1;
                R -= table->subtables[y].keys - isolated;
            }
            size = cuddSwapInPlace(table,x,y);
Alan Mishchenko committed
1403
#ifdef DD_DEBUG
1404 1405
            assert(table->subtables[x].next == (unsigned) x);
            assert(table->subtables[y].next == (unsigned) y);
Alan Mishchenko committed
1406
#endif
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443
            if (size == 0) goto ddSymmSiftingDownOutOfMem;
            move = (Move *) cuddDynamicAllocNode(table);
            if (move == NULL) goto ddSymmSiftingDownOutOfMem;
            move->x = x;
            move->y = y;
            move->size = size;
            move->next = moves;
            moves = move;
            if ((double) size > (double) limitSize * table->maxGrowth)
                return(moves);
            if (size < limitSize) limitSize = size;
        } else { /* Group move */
            /* Update upper bound on node decrease: first phase. */
            gxtop = table->subtables[x].next;
            z = gxtop + 1;
            do {
                zindex = table->invperm[z];
                if (zindex == xindex || cuddTestInteract(table,xindex,zindex)) {
                    isolated = table->vars[zindex]->ref == 1;
                    R -= table->subtables[z].keys - isolated;
                }
                z++;
            } while (z <= gybot);
            size = ddSymmGroupMove(table,x,y,&moves);
            if (size == 0) goto ddSymmSiftingDownOutOfMem;
            if ((double) size > (double) limitSize * table->maxGrowth)
                return(moves);
            if (size < limitSize) limitSize = size;
            /* Update upper bound on node decrease: second phase. */
            gxtop = table->subtables[gybot].next;
            for (z = gxtop + 1; z <= gybot; z++) {
                zindex = table->invperm[z];
                if (zindex == xindex || cuddTestInteract(table,xindex,zindex)) {
                    isolated = table->vars[zindex]->ref == 1;
                    R += table->subtables[z].keys - isolated;
                }
            }
Alan Mishchenko committed
1444
        }
1445 1446
        x = gybot;
        y = cuddNextHigh(table,x);
Alan Mishchenko committed
1447 1448 1449 1450 1451 1452
    }

    return(moves);

ddSymmSiftingDownOutOfMem:
    while (moves != NULL) {
1453 1454 1455
        move = moves->next;
        cuddDeallocMove(table, moves);
        moves = move;
Alan Mishchenko committed
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
    }
    return(MV_OOM);

} /* end of ddSymmSiftingDown */


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

  Synopsis    [Swaps two groups.]

  Description [Swaps two groups. x is assumed to be the bottom variable
  of the first group. y is assumed to be the top variable of the second
  group.  Updates the list of moves. Returns the number of keys in the
  table if successful; 0 otherwise.]

  SideEffects [None]

******************************************************************************/
static int
ddSymmGroupMove(
  DdManager * table,
  int  x,
  int  y,
  Move ** moves)
{
    Move *move;
1482
    int  size;
Alan Mishchenko committed
1483 1484
    int  i,j;
    int  xtop,xbot,xsize,ytop,ybot,ysize,newxtop;
1485
    int  swapx,swapy;
Alan Mishchenko committed
1486

1487 1488
#ifdef DD_DEBUG
    assert(x < y);      /* we assume that x < y */
Alan Mishchenko committed
1489 1490 1491 1492 1493 1494 1495
#endif
    /* Find top, bottom, and size for the two groups. */
    xbot = x;
    xtop = table->subtables[x].next;
    xsize = xbot - xtop + 1;
    ybot = y;
    while ((unsigned) ybot < table->subtables[ybot].next)
1496
        ybot = table->subtables[ybot].next;
Alan Mishchenko committed
1497 1498 1499 1500 1501 1502
    ytop = y;
    ysize = ybot - ytop + 1;

    /* Sift the variables of the second group up through the first group. */
    for (i = 1; i <= ysize; i++) {
        for (j = 1; j <= xsize; j++) {
1503 1504 1505 1506 1507 1508 1509
            size = cuddSwapInPlace(table,x,y);
            if (size == 0) return(0);
            swapx = x; swapy = y;
            y = x;
            x = y - 1;
        }
        y = ytop + i;
Alan Mishchenko committed
1510 1511 1512 1513 1514 1515
        x = y - 1;
    }

    /* fix symmetries */
    y = xtop; /* ytop is now where xtop used to be */
    for (i = 0; i < ysize-1 ; i++) {
1516 1517
        table->subtables[y].next = y + 1;
        y = y + 1;
Alan Mishchenko committed
1518 1519
    }
    table->subtables[y].next = xtop; /* y is bottom of its group, join */
1520
                                     /* its symmetry to top of its group */
Alan Mishchenko committed
1521 1522 1523
    x = y + 1;
    newxtop = x;
    for (i = 0; i < xsize - 1 ; i++) {
1524 1525
        table->subtables[x].next = x + 1;
        x = x + 1;
Alan Mishchenko committed
1526 1527
    }
    table->subtables[x].next = newxtop; /* x is bottom of its group, join */
1528
                                        /* its symmetry to top of its group */
Alan Mishchenko committed
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
    /* Store group move */
    move = (Move *) cuddDynamicAllocNode(table);
    if (move == NULL) return(0);
    move->x = swapx;
    move->y = swapy;
    move->size = size;
    move->next = *moves;
    *moves = move;

    return(size);

} /* end of ddSymmGroupMove */


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

  Synopsis    [Undoes the swap of two groups.]

  Description [Undoes the swap of two groups. x is assumed to be the
  bottom variable of the first group. y is assumed to be the top
  variable of the second group.  Returns the number of keys in the table
  if successful; 0 otherwise.]

  SideEffects [None]

******************************************************************************/
static int
ddSymmGroupMoveBackward(
  DdManager * table,
  int  x,
  int  y)
{
1561
    int size;
Alan Mishchenko committed
1562
    int i,j;
1563
    int xtop,xbot,xsize,ytop,ybot,ysize,newxtop;
Alan Mishchenko committed
1564

1565
#ifdef DD_DEBUG
Alan Mishchenko committed
1566 1567 1568 1569 1570 1571 1572 1573 1574
    assert(x < y); /* We assume that x < y */
#endif

    /* Find top, bottom, and size for the two groups. */
    xbot = x;
    xtop = table->subtables[x].next;
    xsize = xbot - xtop + 1;
    ybot = y;
    while ((unsigned) ybot < table->subtables[ybot].next)
1575
        ybot = table->subtables[ybot].next;
Alan Mishchenko committed
1576 1577 1578 1579 1580 1581
    ytop = y;
    ysize = ybot - ytop + 1;

    /* Sift the variables of the second group up through the first group. */
    for (i = 1; i <= ysize; i++) {
        for (j = 1; j <= xsize; j++) {
1582 1583 1584 1585 1586 1587 1588
            size = cuddSwapInPlace(table,x,y);
            if (size == 0) return(0);
            y = x;
            x = cuddNextLow(table,y);
        }
        y = ytop + i;
        x = y - 1;
Alan Mishchenko committed
1589 1590 1591 1592 1593
    }

    /* Fix symmetries. */
    y = xtop;
    for (i = 0; i < ysize-1 ; i++) {
1594 1595
        table->subtables[y].next = y + 1;
        y = y + 1;
Alan Mishchenko committed
1596 1597
    }
    table->subtables[y].next = xtop; /* y is bottom of its group, join */
1598
                                     /* its symmetry to top of its group */
Alan Mishchenko committed
1599 1600 1601
    x = y + 1;
    newxtop = x;
    for (i = 0; i < xsize-1 ; i++) {
1602 1603
        table->subtables[x].next = x + 1;
        x = x + 1;
Alan Mishchenko committed
1604 1605
    }
    table->subtables[x].next = newxtop; /* x is bottom of its group, join */
1606
                                        /* its symmetry to top of its group */
Alan Mishchenko committed
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635

    return(size);

} /* end of ddSymmGroupMoveBackward */


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

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

  Description [Given a set of moves, returns the DD 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]

******************************************************************************/
static int
ddSymmSiftingBackward(
  DdManager * table,
  Move * moves,
  int  size)
{
    Move *move;
    int  res;

    for (move = moves; move != NULL; move = move->next) {
1636 1637 1638
        if (move->size < size) {
            size = move->size;
        }
Alan Mishchenko committed
1639 1640 1641
    }

    for (move = moves; move != NULL; move = move->next) {
1642 1643 1644
        if (move->size == size) return(1);
        if (table->subtables[move->x].next == move->x && table->subtables[move->y].next == move->y) {
            res = cuddSwapInPlace(table,(int)move->x,(int)move->y);
Alan Mishchenko committed
1645
#ifdef DD_DEBUG
1646 1647
            assert(table->subtables[move->x].next == move->x);
            assert(table->subtables[move->y].next == move->y);
Alan Mishchenko committed
1648
#endif
1649 1650 1651 1652
        } else { /* Group move necessary */
            res = ddSymmGroupMoveBackward(table,(int)move->x,(int)move->y);
        }
        if (!res) return(0);
Alan Mishchenko committed
1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
    }

    return(1);

} /* end of ddSymmSiftingBackward */


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

  Synopsis    [Counts numbers of symmetric variables and symmetry
  groups.]

  Description []

  SideEffects [None]

******************************************************************************/
static void
ddSymmSummary(
  DdManager * table,
  int  lower,
  int  upper,
  int * symvars,
  int * symgroups)
{
    int i,x,gbot;
    int TotalSymm = 0;
    int TotalSymmGroups = 0;

    for (i = lower; i <= upper; i++) {
1683 1684 1685 1686 1687 1688 1689 1690
        if (table->subtables[i].next != (unsigned) i) {
            TotalSymmGroups++;
            x = i;
            do {
                TotalSymm++;
                gbot = x;
                x = table->subtables[x].next;
            } while (x != i);
Alan Mishchenko committed
1691
#ifdef DD_DEBUG
1692
            assert(table->subtables[gbot].next == (unsigned) i);
Alan Mishchenko committed
1693
#endif
1694 1695
            i = gbot;
        }
Alan Mishchenko committed
1696 1697 1698 1699 1700 1701 1702
    }
    *symvars = TotalSymm;
    *symgroups = TotalSymmGroups;

    return;

} /* end of ddSymmSummary */
1703 1704


1705 1706
ABC_NAMESPACE_IMPL_END