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

  FileName    [cuddZddSymm.c]

  PackageName [cudd]

  Synopsis    [Functions for symmetry-based ZDD variable reordering.]

  Description [External procedures included in this module:
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
                    <ul>
                    <li> Cudd_zddSymmProfile()
                    </ul>
               Internal procedures included in this module:
                    <ul>
                    <li> cuddZddSymmCheck()
                    <li> cuddZddSymmSifting()
                    <li> cuddZddSymmSiftingConv()
                    </ul>
               Static procedures included in this module:
                    <ul>
                    <li> cuddZddUniqueCompare()
                    <li> cuddZddSymmSiftingAux()
                    <li> cuddZddSymmSiftingConvAux()
                    <li> cuddZddSymmSifting_up()
                    <li> cuddZddSymmSifting_down()
                    <li> zdd_group_move()
                    <li> cuddZddSymmSiftingBackward()
                    <li> zdd_group_move_backward()
                    </ul>
              ]
Alan Mishchenko committed
31 32 33 34 35

  SeeAlso     [cuddSymmetry.c]

  Author      [Hyong-Kyoon Shin, In-Ho Moon]

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 65 66
  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
67 68 69

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

70
#include "misc/util/util_hack.h"
71
#include "cuddInt.h"
Alan Mishchenko committed
72

73 74 75
ABC_NAMESPACE_IMPL_START


76

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

#define ZDD_MV_OOM (Move *)1

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

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

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

#ifndef lint
96
static char rcsid[] DD_UNUSED = "$Id: cuddZddSymm.c,v 1.29 2004/08/13 18:04:54 fabio Exp $";
Alan Mishchenko committed
97 98
#endif

99
extern int      *zdd_entry;
Alan Mishchenko committed
100

101
extern int      zddTotalNumberSwapping;
Alan Mishchenko committed
102

103
static DdNode   *empty;
Alan Mishchenko committed
104 105 106 107 108 109 110 111 112 113 114 115

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


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

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

116 117 118 119 120 121 122 123
static int cuddZddSymmSiftingAux (DdManager *table, int x, int x_low, int x_high);
static int cuddZddSymmSiftingConvAux (DdManager *table, int x, int x_low, int x_high);
static Move * cuddZddSymmSifting_up (DdManager *table, int x, int x_low, int initial_size);
static Move * cuddZddSymmSifting_down (DdManager *table, int x, int x_high, int initial_size);
static int cuddZddSymmSiftingBackward (DdManager *table, Move *moves, int size);
static int zdd_group_move (DdManager *table, int x, int y, Move **moves);
static int zdd_group_move_backward (DdManager *table, int x, int y);
static void cuddZddSymmSummary (DdManager *table, int lower, int upper, int *symvars, int *symgroups);
Alan Mishchenko committed
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

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


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


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

  Synopsis [Prints statistics on symmetric ZDD variables.]

  Description []

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
void
Cudd_zddSymmProfile(
  DdManager * table,
  int  lower,
  int  upper)
{
150 151 152
    int         i, x, gbot;
    int         TotalSymm = 0;
    int         TotalSymmGroups = 0;
Alan Mishchenko committed
153 154

    for (i = lower; i < upper; i++) {
155 156 157 158 159 160 161 162 163 164
        if (table->subtableZ[i].next != (unsigned) i) {
            x = i;
            (void) fprintf(table->out,"Group:");
            do {
                (void) fprintf(table->out,"  %d", table->invpermZ[x]);
                TotalSymm++;
                gbot = x;
                x = table->subtableZ[x].next;
            } while (x != i);
            TotalSymmGroups++;
Alan Mishchenko committed
165
#ifdef DD_DEBUG
166
            assert(table->subtableZ[gbot].next == (unsigned) i);
Alan Mishchenko committed
167
#endif
168 169 170
            i = gbot;
            (void) fprintf(table->out,"\n");
        }
Alan Mishchenko committed
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 198 199 200 201
    }
    (void) fprintf(table->out,"Total Symmetric = %d\n", TotalSymm);
    (void) fprintf(table->out,"Total Groups = %d\n", TotalSymmGroups);

} /* end of Cudd_zddSymmProfile */


/*---------------------------------------------------------------------------*/
/* 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]

  SeeAlso     []

******************************************************************************/
int
cuddZddSymmCheck(
  DdManager * table,
  int  x,
  int  y)
{
202 203 204 205 206 207 208 209
    int         i;
    DdNode      *f, *f0, *f1, *f01, *f00, *f11, *f10;
    int         yindex;
    int         xsymmy = 1;
    int         xsymmyp = 1;
    int         arccount = 0;
    int         TotalRefCount = 0;
    int         symm_found;
Alan Mishchenko committed
210 211 212 213 214

    empty = table->zero;

    yindex = table->invpermZ[y];
    for (i = table->subtableZ[x].slots - 1; i >= 0; i--) {
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
        f = table->subtableZ[x].nodelist[i];
        while (f != NULL) {
            /* Find f1, f0, f11, f10, f01, f00 */
            f1 = cuddT(f);
            f0 = cuddE(f);
            if ((int) f1->index == yindex) {
                f11 = cuddT(f1);
                f10 = cuddE(f1);
                if (f10 != empty)
                    arccount++;
            } else {
                if ((int) f0->index != yindex) {
                    return(0); /* f bypasses layer y */
                }
                f11 = empty;
                f10 = f1;
            }
            if ((int) f0->index == yindex) {
                f01 = cuddT(f0);
                f00 = cuddE(f0);
                if (f00 != empty)
                    arccount++;
            } else {
                f01 = empty;
                f00 = f0;
            }
            if (f01 != f10)
                xsymmy = 0;
            if (f11 != f00)
                xsymmyp = 0;
            if ((xsymmy == 0) && (xsymmyp == 0))
                return(0);

            f = f->next;
        } /* for each element of the collision list */
Alan Mishchenko committed
250 251 252 253 254 255
    } /* for each slot of the subtable */

    /* Calculate the total reference counts of y
    ** whose else arc is not empty.
    */
    for (i = table->subtableZ[y].slots - 1; i >= 0; i--) {
256 257 258 259 260 261
        f = table->subtableZ[y].nodelist[i];
        while (f != NIL(DdNode)) {
            if (cuddE(f) != empty)
                TotalRefCount += f->ref;
            f = f->next;
        }
Alan Mishchenko committed
262 263 264 265 266
    }

    symm_found = (arccount == TotalRefCount);
#if defined(DD_DEBUG) && defined(DD_VERBOSE)
    if (symm_found) {
267 268 269 270
        int xindex = table->invpermZ[x];
        (void) fprintf(table->out,
                       "Found symmetry! x =%d\ty = %d\tPos(%d,%d)\n",
                       xindex,yindex,x,y);
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 306
    }
#endif

    return(symm_found);

} /* end cuddZddSymmCheck */


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

  Synopsis [Symmetric sifting algorithm for ZDDs.]

  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 ZDD 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     [cuddZddSymmSiftingConv]

******************************************************************************/
int
cuddZddSymmSifting(
  DdManager * table,
  int  lower,
  int  upper)
{
307 308 309 310 311 312 313 314
    int         i;
    int         *var;
    int         nvars;
    int         x;
    int         result;
    int         symvars;
    int         symgroups;
    int         iteration;
Alan Mishchenko committed
315
#ifdef DD_STATS
316
    int         previousSize;
Alan Mishchenko committed
317 318 319 320 321 322
#endif

    nvars = table->sizeZ;

    /* Find order in which to sift variables. */
    var = NULL;
Alan Mishchenko committed
323
    zdd_entry = ABC_ALLOC(int, nvars);
Alan Mishchenko committed
324
    if (zdd_entry == NULL) {
325 326
        table->errorCode = CUDD_MEMORY_OUT;
        goto cuddZddSymmSiftingOutOfMem;
Alan Mishchenko committed
327
    }
Alan Mishchenko committed
328
    var = ABC_ALLOC(int, nvars);
Alan Mishchenko committed
329
    if (var == NULL) {
330 331
        table->errorCode = CUDD_MEMORY_OUT;
        goto cuddZddSymmSiftingOutOfMem;
Alan Mishchenko committed
332 333 334
    }

    for (i = 0; i < nvars; i++) {
335 336 337
        x = table->permZ[i];
        zdd_entry[i] = table->subtableZ[x].keys;
        var[i] = i;
Alan Mishchenko committed
338 339
    }

340
    qsort((void *)var, nvars, sizeof(int), (DD_QSFP)cuddZddUniqueCompare);
Alan Mishchenko committed
341 342 343

    /* Initialize the symmetry of each subtable to itself. */
    for (i = lower; i <= upper; i++)
344
        table->subtableZ[i].next = i;
Alan Mishchenko committed
345 346 347

    iteration = ddMin(table->siftMaxVar, nvars);
    for (i = 0; i < iteration; i++) {
348 349 350
        if (zddTotalNumberSwapping >= table->siftMaxSwap)
            break;
        x = table->permZ[var[i]];
Alan Mishchenko committed
351
#ifdef DD_STATS
352
        previousSize = table->keysZ;
Alan Mishchenko committed
353
#endif
354 355 356 357 358
        if (x < lower || x > upper) continue;
        if (table->subtableZ[x].next == (unsigned) x) {
            result = cuddZddSymmSiftingAux(table, x, lower, upper);
            if (!result)
                goto cuddZddSymmSiftingOutOfMem;
Alan Mishchenko committed
359
#ifdef DD_STATS
360 361 362 363
            if (table->keysZ < (unsigned) previousSize) {
                (void) fprintf(table->out,"-");
            } else if (table->keysZ > (unsigned) previousSize) {
                (void) fprintf(table->out,"+");
Alan Mishchenko committed
364
#ifdef DD_VERBOSE
365
                (void) fprintf(table->out,"\nSize increased from %d to %d while sifting variable %d\n", previousSize, table->keysZ, var[i]);
Alan Mishchenko committed
366
#endif
367 368 369 370
            } else {
                (void) fprintf(table->out,"=");
            }
            fflush(table->out);
Alan Mishchenko committed
371
#endif
372
        }
Alan Mishchenko committed
373 374
    }

Alan Mishchenko committed
375 376
    ABC_FREE(var);
    ABC_FREE(zdd_entry);
Alan Mishchenko committed
377 378 379 380 381 382 383 384 385 386 387 388 389

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

#ifdef DD_STATS
    (void) fprintf(table->out,"\n#:S_SIFTING %8d: symmetric variables\n",symvars);
    (void) fprintf(table->out,"#:G_SIFTING %8d: symmetric groups\n",symgroups);
#endif

    return(1+symvars);

cuddZddSymmSiftingOutOfMem:

    if (zdd_entry != NULL)
390
        ABC_FREE(zdd_entry);
Alan Mishchenko committed
391
    if (var != NULL)
392
        ABC_FREE(var);
Alan Mishchenko committed
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 421 422 423 424 425 426 427

    return(0);

} /* end of cuddZddSymmSifting */


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

  Synopsis [Symmetric sifting to convergence algorithm for ZDDs.]

  Description [Symmetric sifting to convergence 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 subtable.
    <li> Sift the variable up and down, remembering each time the total
    size of the ZDD 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     [cuddZddSymmSifting]

******************************************************************************/
int
cuddZddSymmSiftingConv(
  DdManager * table,
  int  lower,
  int  upper)
{
428 429 430 431 432 433 434 435 436 437
    int         i;
    int         *var;
    int         nvars;
    int         initialSize;
    int         x;
    int         result;
    int         symvars;
    int         symgroups;
    int         classes;
    int         iteration;
Alan Mishchenko committed
438 439 440 441 442 443 444 445 446 447
#ifdef DD_STATS
    int         previousSize;
#endif

    initialSize = table->keysZ;

    nvars = table->sizeZ;

    /* Find order in which to sift variables. */
    var = NULL;
Alan Mishchenko committed
448
    zdd_entry = ABC_ALLOC(int, nvars);
Alan Mishchenko committed
449
    if (zdd_entry == NULL) {
450 451
        table->errorCode = CUDD_MEMORY_OUT;
        goto cuddZddSymmSiftingConvOutOfMem;
Alan Mishchenko committed
452
    }
Alan Mishchenko committed
453
    var = ABC_ALLOC(int, nvars);
Alan Mishchenko committed
454
    if (var == NULL) {
455 456
        table->errorCode = CUDD_MEMORY_OUT;
        goto cuddZddSymmSiftingConvOutOfMem;
Alan Mishchenko committed
457 458 459
    }

    for (i = 0; i < nvars; i++) {
460 461 462
        x = table->permZ[i];
        zdd_entry[i] = table->subtableZ[x].keys;
        var[i] = i;
Alan Mishchenko committed
463 464
    }

465
    qsort((void *)var, nvars, sizeof(int), (DD_QSFP)cuddZddUniqueCompare);
Alan Mishchenko committed
466 467 468 469 470

    /* Initialize the symmetry of each subtable to itself
    ** for first pass of converging symmetric sifting.
    */
    for (i = lower; i <= upper; i++)
471
        table->subtableZ[i].next = i;
Alan Mishchenko committed
472 473 474

    iteration = ddMin(table->siftMaxVar, table->sizeZ);
    for (i = 0; i < iteration; i++) {
475 476 477 478 479 480
        if (zddTotalNumberSwapping >= table->siftMaxSwap)
            break;
        x = table->permZ[var[i]];
        if (x < lower || x > upper) continue;
        /* Only sift if not in symmetry group already. */
        if (table->subtableZ[x].next == (unsigned) x) {
Alan Mishchenko committed
481
#ifdef DD_STATS
482
            previousSize = table->keysZ;
Alan Mishchenko committed
483
#endif
484 485 486
            result = cuddZddSymmSiftingAux(table, x, lower, upper);
            if (!result)
                goto cuddZddSymmSiftingConvOutOfMem;
Alan Mishchenko committed
487
#ifdef DD_STATS
488 489 490 491
            if (table->keysZ < (unsigned) previousSize) {
                (void) fprintf(table->out,"-");
            } else if (table->keysZ > (unsigned) previousSize) {
                (void) fprintf(table->out,"+");
Alan Mishchenko committed
492
#ifdef DD_VERBOSE
493
                (void) fprintf(table->out,"\nSize increased from %d to %d while sifting variable %d\n", previousSize, table->keysZ, var[i]);
Alan Mishchenko committed
494
#endif
495 496 497 498
            } else {
                (void) fprintf(table->out,"=");
            }
            fflush(table->out);
Alan Mishchenko committed
499
#endif
500
        }
Alan Mishchenko committed
501 502 503 504
    }

    /* Sifting now until convergence. */
    while ((unsigned) initialSize > table->keysZ) {
505
        initialSize = table->keysZ;
Alan Mishchenko committed
506
#ifdef DD_STATS
507
        (void) fprintf(table->out,"\n");
Alan Mishchenko committed
508
#endif
509 510 511 512 513 514 515 516 517 518 519 520
        /* Here we consider only one representative for each symmetry class. */
        for (x = lower, classes = 0; x <= upper; x++, classes++) {
            while ((unsigned) x < table->subtableZ[x].next)
                x = table->subtableZ[x].next;
            /* Here x is the largest index in a group.
            ** Groups consists of adjacent variables.
            ** Hence, the next increment of x will move it to a new group.
            */
            i = table->invpermZ[x];
            zdd_entry[i] = table->subtableZ[x].keys;
            var[classes] = i;
        }
Alan Mishchenko committed
521

522
        qsort((void *)var,classes,sizeof(int),(DD_QSFP)cuddZddUniqueCompare);
Alan Mishchenko committed
523

524 525 526 527 528 529 530
        /* Now sift. */
        iteration = ddMin(table->siftMaxVar, nvars);
        for (i = 0; i < iteration; i++) {
            if (zddTotalNumberSwapping >= table->siftMaxSwap)
                break;
            x = table->permZ[var[i]];
            if ((unsigned) x >= table->subtableZ[x].next) {
Alan Mishchenko committed
531
#ifdef DD_STATS
532
                previousSize = table->keysZ;
Alan Mishchenko committed
533
#endif
534 535 536
                result = cuddZddSymmSiftingConvAux(table, x, lower, upper);
                if (!result)
                    goto cuddZddSymmSiftingConvOutOfMem;
Alan Mishchenko committed
537
#ifdef DD_STATS
538 539 540 541
                if (table->keysZ < (unsigned) previousSize) {
                    (void) fprintf(table->out,"-");
                } else if (table->keysZ > (unsigned) previousSize) {
                    (void) fprintf(table->out,"+");
Alan Mishchenko committed
542
#ifdef DD_VERBOSE
543
                (void) fprintf(table->out,"\nSize increased from %d to %d while sifting variable %d\n", previousSize, table->keysZ, var[i]);
Alan Mishchenko committed
544
#endif
545 546 547 548
                } else {
                    (void) fprintf(table->out,"=");
                }
                fflush(table->out);
Alan Mishchenko committed
549
#endif
550 551
            }
        } /* for */
Alan Mishchenko committed
552 553 554 555 556 557
    }

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

#ifdef DD_STATS
    (void) fprintf(table->out,"\n#:S_SIFTING %8d: symmetric variables\n",
558
                   symvars);
Alan Mishchenko committed
559
    (void) fprintf(table->out,"#:G_SIFTING %8d: symmetric groups\n",
560
                   symgroups);
Alan Mishchenko committed
561 562
#endif

Alan Mishchenko committed
563 564
    ABC_FREE(var);
    ABC_FREE(zdd_entry);
Alan Mishchenko committed
565 566 567 568 569 570

    return(1+symvars);

cuddZddSymmSiftingConvOutOfMem:

    if (zdd_entry != NULL)
571
        ABC_FREE(zdd_entry);
Alan Mishchenko committed
572
    if (var != NULL)
573
        ABC_FREE(var);
Alan Mishchenko committed
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607

    return(0);

} /* end of cuddZddSymmSiftingConv */


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


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

  Synopsis [Given x_low <= x <= x_high moves x up and down between the
  boundaries.]

  Description [Given x_low <= x <= x_high 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]

  SeeAlso     []

******************************************************************************/
static int
cuddZddSymmSiftingAux(
  DdManager * table,
  int  x,
  int  x_low,
  int  x_high)
{
    Move *move;
608
    Move *move_up;      /* list of up move */
Alan Mishchenko committed
609
    Move *move_down;    /* list of down move */
610 611 612 613 614
    int  initial_size;
    int  result;
    int  i;
    int  topbot;        /* index to either top or bottom of symmetry group */
    int  init_group_size, final_group_size;
Alan Mishchenko committed
615 616 617 618 619 620 621 622

    initial_size = table->keysZ;

    move_down = NULL;
    move_up = NULL;

    /* Look for consecutive symmetries above x. */
    for (i = x; i > x_low; i--) {
623
        if (!cuddZddSymmCheck(table, i - 1, i))
Alan Mishchenko committed
624
            break;
625 626 627 628 629 630 631
        /* find top of i-1's symmetry */
        topbot = table->subtableZ[i - 1].next;
        table->subtableZ[i - 1].next = i;
        table->subtableZ[x].next = topbot;
        /* x is bottom of group so its symmetry is top of i-1's
           group */
        i = topbot + 1; /* add 1 for i--, new i is top of symm group */
Alan Mishchenko committed
632 633 634
    }
    /* Look for consecutive symmetries below x. */
    for (i = x; i < x_high; i++) {
635
        if (!cuddZddSymmCheck(table, i, i + 1))
Alan Mishchenko committed
636
            break;
637 638 639 640 641 642 643 644 645
        /* find bottom of i+1's symm group */
        topbot = i + 1;
        while ((unsigned) topbot < table->subtableZ[topbot].next)
            topbot = table->subtableZ[topbot].next;

        table->subtableZ[topbot].next = table->subtableZ[i].next;
        table->subtableZ[i].next = i + 1;
        i = topbot - 1; /* add 1 for i++,
                           new i is bottom of symm group */
Alan Mishchenko committed
646 647 648 649
    }

    /* Now x maybe in the middle of a symmetry group. */
    if (x == x_low) { /* Sift down */
650 651 652
        /* Find bottom of x's symm group */
        while ((unsigned) x < table->subtableZ[x].next)
            x = table->subtableZ[x].next;
Alan Mishchenko committed
653

654 655
        i = table->subtableZ[x].next;
        init_group_size = x - i + 1;
Alan Mishchenko committed
656

657 658 659 660 661 662 663 664 665 666
        move_down = cuddZddSymmSifting_down(table, x, x_high,
            initial_size);
        /* after that point x --> x_high, unless early term */
        if (move_down == ZDD_MV_OOM)
            goto cuddZddSymmSiftingAuxOutOfMem;

        if (move_down == NULL ||
            table->subtableZ[move_down->y].next != move_down->y) {
            /* symmetry detected may have to make another complete
               pass */
Alan Mishchenko committed
667
            if (move_down != NULL)
668 669 670 671 672 673 674 675
                x = move_down->y;
            else
                x = table->subtableZ[x].next;
            i = x;
            while ((unsigned) i < table->subtableZ[i].next) {
                i = table->subtableZ[i].next;
            }
            final_group_size = i - x + 1;
Alan Mishchenko committed
676

677 678 679 680 681 682 683 684 685 686 687 688 689
            if (init_group_size == final_group_size) {
                /* No new symmetry groups detected,
                   return to best position */
                result = cuddZddSymmSiftingBackward(table,
                    move_down, initial_size);
            }
            else {
                initial_size = table->keysZ;
                move_up = cuddZddSymmSifting_up(table, x, x_low,
                    initial_size);
                result = cuddZddSymmSiftingBackward(table, move_up,
                    initial_size);
            }
Alan Mishchenko committed
690 691
        }
        else {
692 693 694
            result = cuddZddSymmSiftingBackward(table, move_down,
                initial_size);
            /* move backward and stop at best position */
Alan Mishchenko committed
695
        }
696 697
        if (!result)
            goto cuddZddSymmSiftingAuxOutOfMem;
Alan Mishchenko committed
698 699
    }
    else if (x == x_high) { /* Sift up */
700 701 702
        /* Find top of x's symm group */
        while ((unsigned) x < table->subtableZ[x].next)
            x = table->subtableZ[x].next;
Alan Mishchenko committed
703 704
        x = table->subtableZ[x].next;

705 706 707 708 709
        i = x;
        while ((unsigned) i < table->subtableZ[i].next) {
            i = table->subtableZ[i].next;
        }
        init_group_size = i - x + 1;
Alan Mishchenko committed
710

711 712 713 714
        move_up = cuddZddSymmSifting_up(table, x, x_low, initial_size);
        /* after that point x --> x_low, unless early term */
        if (move_up == ZDD_MV_OOM)
            goto cuddZddSymmSiftingAuxOutOfMem;
Alan Mishchenko committed
715

716 717 718 719
        if (move_up == NULL ||
            table->subtableZ[move_up->x].next != move_up->x) {
            /* symmetry detected may have to make another complete
                pass */
Alan Mishchenko committed
720
            if (move_up != NULL)
721 722 723 724 725 726 727
                x = move_up->x;
            else {
                while ((unsigned) x < table->subtableZ[x].next)
                    x = table->subtableZ[x].next;
            }
            i = table->subtableZ[x].next;
            final_group_size = x - i + 1;
Alan Mishchenko committed
728

729 730 731 732 733 734 735 736 737 738 739 740 741
            if (init_group_size == final_group_size) {
                /* No new symmetry groups detected,
                   return to best position */
                result = cuddZddSymmSiftingBackward(table, move_up,
                    initial_size);
            }
            else {
                initial_size = table->keysZ;
                move_down = cuddZddSymmSifting_down(table, x, x_high,
                    initial_size);
                result = cuddZddSymmSiftingBackward(table, move_down,
                    initial_size);
            }
Alan Mishchenko committed
742 743
        }
        else {
744 745 746
            result = cuddZddSymmSiftingBackward(table, move_up,
                initial_size);
            /* move backward and stop at best position */
Alan Mishchenko committed
747
        }
748 749
        if (!result)
            goto cuddZddSymmSiftingAuxOutOfMem;
Alan Mishchenko committed
750 751
    }
    else if ((x - x_low) > (x_high - x)) { /* must go down first:
752 753 754 755
                                                shorter */
        /* Find bottom of x's symm group */
        while ((unsigned) x < table->subtableZ[x].next)
            x = table->subtableZ[x].next;
Alan Mishchenko committed
756

757 758 759 760 761
        move_down = cuddZddSymmSifting_down(table, x, x_high,
            initial_size);
        /* after that point x --> x_high, unless early term */
        if (move_down == ZDD_MV_OOM)
            goto cuddZddSymmSiftingAuxOutOfMem;
Alan Mishchenko committed
762

763 764
        if (move_down != NULL) {
            x = move_down->y;
Alan Mishchenko committed
765 766 767 768
        }
        else {
            x = table->subtableZ[x].next;
        }
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
        i = x;
        while ((unsigned) i < table->subtableZ[i].next) {
            i = table->subtableZ[i].next;
        }
        init_group_size = i - x + 1;

        move_up = cuddZddSymmSifting_up(table, x, x_low, initial_size);
        if (move_up == ZDD_MV_OOM)
            goto cuddZddSymmSiftingAuxOutOfMem;

        if (move_up == NULL ||
            table->subtableZ[move_up->x].next != move_up->x) {
            /* symmetry detected may have to make another complete
               pass */
            if (move_up != NULL) {
                x = move_up->x;
            }
            else {
                while ((unsigned) x < table->subtableZ[x].next)
                    x = table->subtableZ[x].next;
            }
            i = table->subtableZ[x].next;
            final_group_size = x - i + 1;
Alan Mishchenko committed
792

793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
            if (init_group_size == final_group_size) {
                /* No new symmetry groups detected,
                   return to best position */
                result = cuddZddSymmSiftingBackward(table, move_up,
                    initial_size);
            }
            else {
                while (move_down != NULL) {
                    move = move_down->next;
                    cuddDeallocMove(table, move_down);
                    move_down = move;
                }
                initial_size = table->keysZ;
                move_down = cuddZddSymmSifting_down(table, x, x_high,
                    initial_size);
                result = cuddZddSymmSiftingBackward(table, move_down,
                    initial_size);
            }
Alan Mishchenko committed
811 812
        }
        else {
813 814 815
            result = cuddZddSymmSiftingBackward(table, move_up,
                initial_size);
            /* move backward and stop at best position */
Alan Mishchenko committed
816
        }
817 818
        if (!result)
            goto cuddZddSymmSiftingAuxOutOfMem;
Alan Mishchenko committed
819 820 821
    }
    else { /* moving up first:shorter */
        /* Find top of x's symmetry group */
822 823
        while ((unsigned) x < table->subtableZ[x].next)
            x = table->subtableZ[x].next;
Alan Mishchenko committed
824 825
        x = table->subtableZ[x].next;

826 827 828 829
        move_up = cuddZddSymmSifting_up(table, x, x_low, initial_size);
        /* after that point x --> x_high, unless early term */
        if (move_up == ZDD_MV_OOM)
            goto cuddZddSymmSiftingAuxOutOfMem;
Alan Mishchenko committed
830

831 832
        if (move_up != NULL) {
            x = move_up->x;
Alan Mishchenko committed
833 834
        }
        else {
835 836
            while ((unsigned) x < table->subtableZ[x].next)
                x = table->subtableZ[x].next;
Alan Mishchenko committed
837
        }
838 839
        i = table->subtableZ[x].next;
        init_group_size = x - i + 1;
Alan Mishchenko committed
840

841
        move_down = cuddZddSymmSifting_down(table, x, x_high,
Alan Mishchenko committed
842
            initial_size);
843 844 845 846 847 848 849 850 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 878 879
        if (move_down == ZDD_MV_OOM)
            goto cuddZddSymmSiftingAuxOutOfMem;

        if (move_down == NULL ||
            table->subtableZ[move_down->y].next != move_down->y) {
            /* symmetry detected may have to make another complete
               pass */
            if (move_down != NULL) {
                x = move_down->y;
            }
            else {
                x = table->subtableZ[x].next;
            }
            i = x;
            while ((unsigned) i < table->subtableZ[i].next) {
                i = table->subtableZ[i].next;
            }
            final_group_size = i - x + 1;

            if (init_group_size == final_group_size) {
                /* No new symmetries detected,
                   go back to best position */
                result = cuddZddSymmSiftingBackward(table, move_down,
                    initial_size);
            }
            else {
                while (move_up != NULL) {
                    move = move_up->next;
                    cuddDeallocMove(table, move_up);
                    move_up = move;
                }
                initial_size = table->keysZ;
                move_up = cuddZddSymmSifting_up(table, x, x_low,
                    initial_size);
                result = cuddZddSymmSiftingBackward(table, move_up,
                    initial_size);
            }
Alan Mishchenko committed
880 881
        }
        else {
882 883 884
            result = cuddZddSymmSiftingBackward(table, move_down,
                initial_size);
            /* move backward and stop at best position */
Alan Mishchenko committed
885
        }
886 887
        if (!result)
            goto cuddZddSymmSiftingAuxOutOfMem;
Alan Mishchenko committed
888 889 890
    }

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

    return(1);

cuddZddSymmSiftingAuxOutOfMem:
    if (move_down != ZDD_MV_OOM) {
905 906 907 908 909
        while (move_down != NULL) {
            move = move_down->next;
            cuddDeallocMove(table, move_down);
            move_down = move;
        }
Alan Mishchenko committed
910 911
    }
    if (move_up != ZDD_MV_OOM) {
912 913 914 915 916
        while (move_up != NULL) {
            move = move_up->next;
            cuddDeallocMove(table, move_up);
            move_up = move;
        }
Alan Mishchenko committed
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 945 946
    }

    return(0);

} /* end of cuddZddSymmSiftingAux */


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

  Synopsis [Given x_low <= x <= x_high moves x up and down between the
  boundaries.]

  Description [Given x_low <= x <= x_high 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]

  SeeAlso     []

******************************************************************************/
static int
cuddZddSymmSiftingConvAux(
  DdManager * table,
  int  x,
  int  x_low,
  int  x_high)
{
947 948 949 950 951 952 953
    Move        *move;
    Move        *move_up;       /* list of up move */
    Move        *move_down;     /* list of down move */
    int         initial_size;
    int         result;
    int         i;
    int         init_group_size, final_group_size;
Alan Mishchenko committed
954 955 956 957 958 959 960 961

    initial_size = table->keysZ;

    move_down = NULL;
    move_up = NULL;

    if (x == x_low) { /* Sift down */
        i = table->subtableZ[x].next;
962
        init_group_size = x - i + 1;
Alan Mishchenko committed
963

964
        move_down = cuddZddSymmSifting_down(table, x, x_high,
Alan Mishchenko committed
965
            initial_size);
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
        /* after that point x --> x_high, unless early term */
        if (move_down == ZDD_MV_OOM)
            goto cuddZddSymmSiftingConvAuxOutOfMem;

        if (move_down == NULL ||
            table->subtableZ[move_down->y].next != move_down->y) {
            /* symmetry detected may have to make another complete
                pass */
            if (move_down != NULL)
                x = move_down->y;
            else {
                while ((unsigned) x < table->subtableZ[x].next)
                    x = table->subtableZ[x].next;
                x = table->subtableZ[x].next;
            }
            i = x;
            while ((unsigned) i < table->subtableZ[i].next) {
                i = table->subtableZ[i].next;
            }
            final_group_size = i - x + 1;

            if (init_group_size == final_group_size) {
                /* No new symmetries detected,
                   go back to best position */
                result = cuddZddSymmSiftingBackward(table, move_down,
                    initial_size);
            }
            else {
                initial_size = table->keysZ;
                move_up = cuddZddSymmSifting_up(table, x, x_low,
                    initial_size);
                result = cuddZddSymmSiftingBackward(table, move_up,
                    initial_size);
            }
Alan Mishchenko committed
1000 1001
        }
        else {
1002 1003 1004
            result = cuddZddSymmSiftingBackward(table, move_down,
                initial_size);
            /* move backward and stop at best position */
Alan Mishchenko committed
1005
        }
1006 1007
        if (!result)
            goto cuddZddSymmSiftingConvAuxOutOfMem;
Alan Mishchenko committed
1008 1009
    }
    else if (x == x_high) { /* Sift up */
1010 1011 1012
        /* Find top of x's symm group */
        while ((unsigned) x < table->subtableZ[x].next)
            x = table->subtableZ[x].next;
Alan Mishchenko committed
1013 1014
        x = table->subtableZ[x].next;

1015 1016 1017 1018 1019
        i = x;
        while ((unsigned) i < table->subtableZ[i].next) {
            i = table->subtableZ[i].next;
        }
        init_group_size = i - x + 1;
Alan Mishchenko committed
1020

1021 1022 1023 1024
        move_up = cuddZddSymmSifting_up(table, x, x_low, initial_size);
        /* after that point x --> x_low, unless early term */
        if (move_up == ZDD_MV_OOM)
            goto cuddZddSymmSiftingConvAuxOutOfMem;
Alan Mishchenko committed
1025

1026 1027 1028 1029
        if (move_up == NULL ||
            table->subtableZ[move_up->x].next != move_up->x) {
            /* symmetry detected may have to make another complete
               pass */
Alan Mishchenko committed
1030
            if (move_up != NULL)
1031 1032 1033 1034 1035 1036 1037
                x = move_up->x;
            else {
                while ((unsigned) x < table->subtableZ[x].next)
                    x = table->subtableZ[x].next;
            }
            i = table->subtableZ[x].next;
            final_group_size = x - i + 1;
Alan Mishchenko committed
1038

1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
            if (init_group_size == final_group_size) {
                /* No new symmetry groups detected,
                   return to best position */
                result = cuddZddSymmSiftingBackward(table, move_up,
                    initial_size);
            }
            else {
                initial_size = table->keysZ;
                move_down = cuddZddSymmSifting_down(table, x, x_high,
                    initial_size);
                result = cuddZddSymmSiftingBackward(table, move_down,
                    initial_size);
            }
Alan Mishchenko committed
1052 1053
        }
        else {
1054 1055 1056
            result = cuddZddSymmSiftingBackward(table, move_up,
                initial_size);
            /* move backward and stop at best position */
Alan Mishchenko committed
1057
        }
1058 1059
        if (!result)
            goto cuddZddSymmSiftingConvAuxOutOfMem;
Alan Mishchenko committed
1060 1061
    }
    else if ((x - x_low) > (x_high - x)) { /* must go down first:
1062 1063 1064 1065 1066 1067
                                                shorter */
        move_down = cuddZddSymmSifting_down(table, x, x_high,
            initial_size);
        /* after that point x --> x_high */
        if (move_down == ZDD_MV_OOM)
            goto cuddZddSymmSiftingConvAuxOutOfMem;
Alan Mishchenko committed
1068

1069 1070
        if (move_down != NULL) {
            x = move_down->y;
Alan Mishchenko committed
1071 1072
        }
        else {
1073 1074
            while ((unsigned) x < table->subtableZ[x].next)
                x = table->subtableZ[x].next;
Alan Mishchenko committed
1075 1076
            x = table->subtableZ[x].next;
        }
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
        i = x;
        while ((unsigned) i < table->subtableZ[i].next) {
            i = table->subtableZ[i].next;
        }
        init_group_size = i - x + 1;

        move_up = cuddZddSymmSifting_up(table, x, x_low, initial_size);
        if (move_up == ZDD_MV_OOM)
            goto cuddZddSymmSiftingConvAuxOutOfMem;

        if (move_up == NULL ||
            table->subtableZ[move_up->x].next != move_up->x) {
            /* symmetry detected may have to make another complete
               pass */
            if (move_up != NULL) {
                x = move_up->x;
            }
            else {
                while ((unsigned) x < table->subtableZ[x].next)
                    x = table->subtableZ[x].next;
            }
Alan Mishchenko committed
1098 1099 1100 1101
            i = table->subtableZ[x].next;
            final_group_size = x - i + 1;

            if (init_group_size == final_group_size) {
1102 1103
                /* No new symmetry groups detected,
                   return to best position */
Alan Mishchenko committed
1104
                result = cuddZddSymmSiftingBackward(table, move_up,
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
                    initial_size);
            }
            else {
                while (move_down != NULL) {
                    move = move_down->next;
                    cuddDeallocMove(table, move_down);
                    move_down = move;
                }
                initial_size = table->keysZ;
                move_down = cuddZddSymmSifting_down(table, x, x_high,
                    initial_size);
                result = cuddZddSymmSiftingBackward(table, move_down,
                    initial_size);
Alan Mishchenko committed
1118 1119
            }
        }
1120 1121 1122 1123
        else {
            result = cuddZddSymmSiftingBackward(table, move_up,
                initial_size);
            /* move backward and stop at best position */
Alan Mishchenko committed
1124
        }
1125 1126
        if (!result)
            goto cuddZddSymmSiftingConvAuxOutOfMem;
Alan Mishchenko committed
1127 1128
    }
    else { /* moving up first:shorter */
1129 1130
        /* Find top of x's symmetry group */
        x = table->subtableZ[x].next;
Alan Mishchenko committed
1131

1132 1133 1134 1135
        move_up = cuddZddSymmSifting_up(table, x, x_low, initial_size);
        /* after that point x --> x_high, unless early term */
        if (move_up == ZDD_MV_OOM)
            goto cuddZddSymmSiftingConvAuxOutOfMem;
Alan Mishchenko committed
1136

1137 1138 1139 1140 1141 1142 1143
        if (move_up != NULL) {
            x = move_up->x;
        }
        else {
            while ((unsigned) x < table->subtableZ[x].next)
                x = table->subtableZ[x].next;
        }
Alan Mishchenko committed
1144 1145 1146
        i = table->subtableZ[x].next;
        init_group_size = x - i + 1;

1147 1148 1149 1150
        move_down = cuddZddSymmSifting_down(table, x, x_high,
            initial_size);
        if (move_down == ZDD_MV_OOM)
            goto cuddZddSymmSiftingConvAuxOutOfMem;
Alan Mishchenko committed
1151

1152 1153 1154 1155
        if (move_down == NULL ||
            table->subtableZ[move_down->y].next != move_down->y) {
            /* symmetry detected may have to make another complete
               pass */
Alan Mishchenko committed
1156
            if (move_down != NULL) {
1157 1158 1159 1160 1161 1162 1163
                x = move_down->y;
            }
            else {
                while ((unsigned) x < table->subtableZ[x].next)
                    x = table->subtableZ[x].next;
                x = table->subtableZ[x].next;
            }
Alan Mishchenko committed
1164 1165 1166 1167
            i = x;
            while ((unsigned) i < table->subtableZ[i].next) {
                i = table->subtableZ[i].next;
            }
1168
            final_group_size = i - x + 1;
Alan Mishchenko committed
1169 1170

            if (init_group_size == final_group_size) {
1171 1172
                /* No new symmetries detected,
                   go back to best position */
Alan Mishchenko committed
1173
                result = cuddZddSymmSiftingBackward(table, move_down,
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
                    initial_size);
            }
            else {
                while (move_up != NULL) {
                    move = move_up->next;
                    cuddDeallocMove(table, move_up);
                    move_up = move;
                }
                initial_size = table->keysZ;
                move_up = cuddZddSymmSifting_up(table, x, x_low,
                    initial_size);
                result = cuddZddSymmSiftingBackward(table, move_up,
                    initial_size);
Alan Mishchenko committed
1187 1188
            }
        }
1189 1190 1191 1192
        else {
            result = cuddZddSymmSiftingBackward(table, move_down,
                initial_size);
            /* move backward and stop at best position */
Alan Mishchenko committed
1193
        }
1194 1195
        if (!result)
            goto cuddZddSymmSiftingConvAuxOutOfMem;
Alan Mishchenko committed
1196 1197 1198
    }

    while (move_down != NULL) {
1199 1200 1201
        move = move_down->next;
        cuddDeallocMove(table, move_down);
        move_down = move;
Alan Mishchenko committed
1202 1203
    }
    while (move_up != NULL) {
1204 1205 1206
        move = move_up->next;
        cuddDeallocMove(table, move_up);
        move_up = move;
Alan Mishchenko committed
1207 1208 1209 1210 1211 1212
    }

    return(1);

cuddZddSymmSiftingConvAuxOutOfMem:
    if (move_down != ZDD_MV_OOM) {
1213 1214 1215 1216 1217
        while (move_down != NULL) {
            move = move_down->next;
            cuddDeallocMove(table, move_down);
            move_down = move;
        }
Alan Mishchenko committed
1218 1219
    }
    if (move_up != ZDD_MV_OOM) {
1220 1221 1222 1223 1224
        while (move_up != NULL) {
            move = move_up->next;
            cuddDeallocMove(table, move_up);
            move_up = move;
        }
Alan Mishchenko committed
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
    }

    return(0);

} /* end of cuddZddSymmSiftingConvAux */


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

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

  Description [Moves x up until either it reaches the bound (x_low) or
  the size of the ZDD 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; ZDD_MV_OOM if memory is full.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static Move *
cuddZddSymmSifting_up(
  DdManager * table,
  int  x,
  int  x_low,
  int  initial_size)
{
1256 1257 1258 1259 1260 1261
    Move        *moves;
    Move        *move;
    int         y;
    int         size;
    int         limit_size = initial_size;
    int         i, gytop;
Alan Mishchenko committed
1262 1263 1264 1265

    moves = NULL;
    y = cuddZddNextLow(table, x);
    while (y >= x_low) {
1266 1267 1268 1269 1270 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 1303 1304
        gytop = table->subtableZ[y].next;
        if (cuddZddSymmCheck(table, y, x)) {
            /* Symmetry found, attach symm groups */
            table->subtableZ[y].next = x;
            i = table->subtableZ[x].next;
            while (table->subtableZ[i].next != (unsigned) x)
                i = table->subtableZ[i].next;
            table->subtableZ[i].next = gytop;
        }
        else if ((table->subtableZ[x].next == (unsigned) x) &&
            (table->subtableZ[y].next == (unsigned) y)) {
            /* x and y have self symmetry */
            size = cuddZddSwapInPlace(table, y, x);
            if (size == 0)
                goto cuddZddSymmSifting_upOutOfMem;
            move = (Move *)cuddDynamicAllocNode(table);
            if (move == NULL)
                goto cuddZddSymmSifting_upOutOfMem;
            move->x = y;
            move->y = x;
            move->size = size;
            move->next = moves;
            moves = move;
            if ((double)size >
                (double)limit_size * table->maxGrowth)
                return(moves);
            if (size < limit_size)
                limit_size = size;
        }
        else { /* Group move */
            size = zdd_group_move(table, y, x, &moves);
            if ((double)size >
                (double)limit_size * table->maxGrowth)
                return(moves);
            if (size < limit_size)
                limit_size = size;
        }
        x = gytop;
        y = cuddZddNextLow(table, x);
Alan Mishchenko committed
1305 1306 1307 1308 1309 1310
    }

    return(moves);

cuddZddSymmSifting_upOutOfMem:
    while (moves != NULL) {
1311 1312 1313
        move = moves->next;
        cuddDeallocMove(table, moves);
        moves = move;
Alan Mishchenko committed
1314 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(ZDD_MV_OOM);

} /* end of cuddZddSymmSifting_up */


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

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

  Description [Moves x down until either it reaches the bound (x_high)
  or the size of the ZDD 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; ZDD_MV_OOM if memory is full.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static Move *
cuddZddSymmSifting_down(
  DdManager * table,
  int  x,
  int  x_high,
  int  initial_size)
{
1344 1345 1346 1347 1348 1349
    Move        *moves;
    Move        *move;
    int         y;
    int         size;
    int         limit_size = initial_size;
    int         i, gxtop, gybot;
Alan Mishchenko committed
1350 1351 1352 1353

    moves = NULL;
    y = cuddZddNextHigh(table, x);
    while (y <= x_high) {
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
        gybot = table->subtableZ[y].next;
        while (table->subtableZ[gybot].next != (unsigned) y)
            gybot = table->subtableZ[gybot].next;
        if (cuddZddSymmCheck(table, x, y)) {
            /* Symmetry found, attach symm groups */
            gxtop = table->subtableZ[x].next;
            table->subtableZ[x].next = y;
            i = table->subtableZ[y].next;
            while (table->subtableZ[i].next != (unsigned) y)
                i = table->subtableZ[i].next;
            table->subtableZ[i].next = gxtop;
        }
        else if ((table->subtableZ[x].next == (unsigned) x) &&
            (table->subtableZ[y].next == (unsigned) y)) {
            /* x and y have self symmetry */
            size = cuddZddSwapInPlace(table, x, y);
            if (size == 0)
                goto cuddZddSymmSifting_downOutOfMem;
            move = (Move *)cuddDynamicAllocNode(table);
            if (move == NULL)
                goto cuddZddSymmSifting_downOutOfMem;
            move->x = x;
            move->y = y;
            move->size = size;
            move->next = moves;
            moves = move;
            if ((double)size >
                (double)limit_size * table->maxGrowth)
                return(moves);
            if (size < limit_size)
                limit_size = size;
            x = y;
            y = cuddZddNextHigh(table, x);
        }
        else { /* Group move */
            size = zdd_group_move(table, x, y, &moves);
            if ((double)size >
                (double)limit_size * table->maxGrowth)
                return(moves);
            if (size < limit_size)
                limit_size = size;
        }
        x = gybot;
Alan Mishchenko committed
1397 1398 1399 1400 1401 1402 1403
        y = cuddZddNextHigh(table, x);
    }

    return(moves);

cuddZddSymmSifting_downOutOfMem:
    while (moves != NULL) {
1404 1405 1406
        move = moves->next;
        cuddDeallocMove(table, moves);
        moves = move;
Alan Mishchenko committed
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
    }
    return(ZDD_MV_OOM);

} /* end of cuddZddSymmSifting_down */


/**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
cuddZddSymmSiftingBackward(
  DdManager * table,
  Move * moves,
  int  size)
{
1434 1435 1436
    int         i;
    int         i_best;
    Move        *move;
Alan Mishchenko committed
1437
    int         res = -1;
Alan Mishchenko committed
1438 1439 1440

    i_best = -1;
    for (move = moves, i = 0; move != NULL; move = move->next, i++) {
1441 1442 1443 1444
        if (move->size < size) {
            i_best = i;
            size = move->size;
        }
Alan Mishchenko committed
1445 1446 1447
    }

    for (move = moves, i = 0; move != NULL; move = move->next, i++) {
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
        if (i == i_best) break;
        if ((table->subtableZ[move->x].next == move->x) &&
            (table->subtableZ[move->y].next == move->y)) {
            res = cuddZddSwapInPlace(table, move->x, move->y);
            if (!res) return(0);
        }
        else { /* Group move necessary */
            res = zdd_group_move_backward(table, move->x, move->y);
        }
        if (i_best == -1 && res == size)
            break;
Alan Mishchenko committed
1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
    }

    return(1);

} /* end of cuddZddSymmSiftingBackward */


/**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]

  SeeAlso     []

******************************************************************************/
static int
zdd_group_move(
  DdManager * table,
  int  x,
  int  y,
  Move ** moves)
{
1487 1488 1489
    Move        *move;
    int         size;
    int         i, temp, gxtop, gxbot, gybot, yprev;
1490
    int         swapx = -1, swapy = -1;
Alan Mishchenko committed
1491 1492

#ifdef DD_DEBUG
1493
    assert(x < y);      /* we assume that x < y */
Alan Mishchenko committed
1494 1495 1496 1497 1498 1499
#endif
    /* Find top and bottom for the two groups. */
    gxtop = table->subtableZ[x].next;
    gxbot = x;
    gybot = table->subtableZ[y].next;
    while (table->subtableZ[gybot].next != (unsigned) y)
1500
        gybot = table->subtableZ[gybot].next;
Alan Mishchenko committed
1501 1502 1503
    yprev = gybot;

    while (x <= y) {
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522
        while (y > gxtop) {
            /* Set correct symmetries. */
            temp = table->subtableZ[x].next;
            if (temp == x)
                temp = y;
            i = gxtop;
            for (;;) {
                if (table->subtableZ[i].next == (unsigned) x) {
                    table->subtableZ[i].next = y;
                    break;
                } else {
                    i = table->subtableZ[i].next;
                }
            }
            if (table->subtableZ[y].next != (unsigned) y) {
                table->subtableZ[x].next = table->subtableZ[y].next;
            } else {
                table->subtableZ[x].next = x;
            }
Alan Mishchenko committed
1523

1524 1525 1526 1527 1528 1529
            if (yprev != y) {
                table->subtableZ[yprev].next = x;
            } else {
                yprev = x;
            }
            table->subtableZ[y].next = temp;
Alan Mishchenko committed
1530

1531 1532 1533
            size = cuddZddSwapInPlace(table, x, y);
            if (size == 0)
                goto zdd_group_moveOutOfMem;
Alan Mishchenko committed
1534
            swapx = x;
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
            swapy = y;
            y = x;
            x--;
        } /* while y > gxtop */

        /* Trying to find the next y. */
        if (table->subtableZ[y].next <= (unsigned) y) {
            gybot = y;
        } else {
            y = table->subtableZ[y].next;
        }
Alan Mishchenko committed
1546

1547 1548 1549 1550
        yprev = gxtop;
        gxtop++;
        gxbot++;
        x = gxbot;
Alan Mishchenko committed
1551 1552 1553
    } /* while x <= y, end of group movement */
    move = (Move *)cuddDynamicAllocNode(table);
    if (move == NULL)
1554
        goto zdd_group_moveOutOfMem;
Alan Mishchenko committed
1555 1556 1557 1558 1559 1560 1561 1562 1563 1564
    move->x = swapx;
    move->y = swapy;
    move->size = table->keysZ;
    move->next = *moves;
    *moves = move;

    return(table->keysZ);

zdd_group_moveOutOfMem:
    while (*moves != NULL) {
1565 1566 1567
        move = (*moves)->next;
        cuddDeallocMove(table, *moves);
        *moves = move;
Alan Mishchenko committed
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592
    }
    return(0);

} /* end of zdd_group_move */


/**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 1 if successful; 0 otherwise.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static int
zdd_group_move_backward(
  DdManager * table,
  int  x,
  int  y)
{
1593
    int        size = -1;
1594
    int        i, temp, gxtop, gxbot, gybot, yprev;
Alan Mishchenko committed
1595 1596

#ifdef DD_DEBUG
1597
    assert(x < y);      /* we assume that x < y */
Alan Mishchenko committed
1598 1599 1600 1601 1602 1603
#endif
    /* Find top and bottom of the two groups. */
    gxtop = table->subtableZ[x].next;
    gxbot = x;
    gybot = table->subtableZ[y].next;
    while (table->subtableZ[gybot].next != (unsigned) y)
1604
        gybot = table->subtableZ[gybot].next;
Alan Mishchenko committed
1605 1606 1607
    yprev = gybot;

    while (x <= y) {
1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
        while (y > gxtop) {
            /* Set correct symmetries. */
            temp = table->subtableZ[x].next;
            if (temp == x)
                temp = y;
            i = gxtop;
            for (;;) {
                if (table->subtableZ[i].next == (unsigned) x) {
                    table->subtableZ[i].next = y;
                    break;
                } else {
                    i = table->subtableZ[i].next;
                }
            }
            if (table->subtableZ[y].next != (unsigned) y) {
                table->subtableZ[x].next = table->subtableZ[y].next;
            } else {
                table->subtableZ[x].next = x;
            }
Alan Mishchenko committed
1627

1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644
            if (yprev != y) {
                table->subtableZ[yprev].next = x;
            } else {
                yprev = x;
            }
            table->subtableZ[y].next = temp;

            size = cuddZddSwapInPlace(table, x, y);
            if (size == 0)
                return(0);
            y = x;
            x--;
        } /* while y > gxtop */

        /* Trying to find the next y. */
        if (table->subtableZ[y].next <= (unsigned) y) {
            gybot = y;
Alan Mishchenko committed
1645
        } else {
1646
            y = table->subtableZ[y].next;
Alan Mishchenko committed
1647 1648
        }

1649 1650 1651 1652
        yprev = gxtop;
        gxtop++;
        gxbot++;
        x = gxbot;
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
    } /* while x <= y, end of group movement backward */

    return(size);

} /* end of zdd_group_move_backward */


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

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

  Description []

  SideEffects [None]

******************************************************************************/
static void
cuddZddSymmSummary(
  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->subtableZ[i].next != (unsigned) i) {
            TotalSymmGroups++;
            x = i;
            do {
                TotalSymm++;
                gbot = x;
                x = table->subtableZ[x].next;
            } while (x != i);
Alan Mishchenko committed
1691
#ifdef DD_DEBUG
1692
            assert(table->subtableZ[gbot].next == (unsigned) i);
Alan Mishchenko committed
1693
#endif
1694 1695
            i = gbot;
        }
Alan Mishchenko committed
1696 1697 1698 1699 1700 1701 1702 1703
    }
    *symvars = TotalSymm;
    *symgroups = TotalSymmGroups;

    return;

} /* end of cuddZddSymmSummary */

1704

1705 1706
ABC_NAMESPACE_IMPL_END

1707