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

  FileName    [cuddExact.c]

  PackageName [cudd]

  Synopsis    [Functions for exact variable reordering.]

  Description [External procedures included in this file:
10 11 12 13 14 15 16 17
                <ul>
                </ul>
        Internal procedures included in this module:
                <ul>
                <li> cuddExact()
                </ul>
        Static procedures included in this module:
                <ul>
Alan Mishchenko committed
18
                <li> getMaxBinomial()
19
                <li> gcd()
Alan Mishchenko committed
20
                <li> getMatrix()
21
                <li> freeMatrix()
Alan Mishchenko committed
22 23 24
                <li> getLevelKeys()
                <li> ddShuffle()
                <li> ddSiftUp()
25 26 27 28 29 30 31
                <li> updateUB()
                <li> ddCountRoots()
                <li> ddClearGlobal()
                <li> computeLB()
                <li> updateEntry()
                <li> pushDown()
                <li> initSymmInfo()
Alan Mishchenko committed
32 33 34 35
                </ul>]

  Author      [Cheng Hua, Fabio Somenzi]

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"
Alan Mishchenko committed
71 72
#include "cuddInt.h"

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
/*---------------------------------------------------------------------------*/
/* Constant declarations                                                     */
/*---------------------------------------------------------------------------*/


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

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

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

#ifndef lint
95
static char rcsid[] DD_UNUSED = "$Id: cuddExact.c,v 1.28 2009/02/19 16:19:19 fabio Exp $";
Alan Mishchenko committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
#endif

#ifdef DD_STATS
static int ddTotalShuffles;
#endif

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

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

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

112 113 114 115 116 117 118 119 120 121 122 123 124 125
static int getMaxBinomial (int n);
static DdHalfWord ** getMatrix (int rows, int cols);
static void freeMatrix (DdHalfWord **matrix);
static int getLevelKeys (DdManager *table, int l);
static int ddShuffle (DdManager *table, DdHalfWord *permutation, int lower, int upper);
static int ddSiftUp (DdManager *table, int x, int xLow);
static int updateUB (DdManager *table, int oldBound, DdHalfWord *bestOrder, int lower, int upper);
static int ddCountRoots (DdManager *table, int lower, int upper);
static void ddClearGlobal (DdManager *table, int lower, int maxlevel);
static int computeLB (DdManager *table, DdHalfWord *order, int roots, int cost, int lower, int upper, int level);
static int updateEntry (DdManager *table, DdHalfWord *order, int level, int cost, DdHalfWord **orders, int *costs, int subsets, char *mask, int lower, int upper);
static void pushDown (DdHalfWord *order, int j, int level);
static DdHalfWord * initSymmInfo (DdManager *table, int lower, int upper);
static int checkSymmInfo (DdManager *table, DdHalfWord *symmInfo, int index, int level);
Alan Mishchenko committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

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


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

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


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

  Synopsis    [Exact variable ordering algorithm.]

  Description [Exact variable ordering algorithm. Finds an optimum
  order for the variables between lower and upper.  Returns 1 if
  successful; 0 otherwise.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
int
cuddExact(
  DdManager * table,
  int  lower,
  int  upper)
{
    int k, i, j;
    int maxBinomial, oldSubsets, newSubsets;
    int subsetCost;
161
    int size;                   /* number of variables to be reordered */
Alan Mishchenko committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
    int unused, nvars, level, result;
    int upperBound, lowerBound, cost;
    int roots;
    char *mask = NULL;
    DdHalfWord  *symmInfo = NULL;
    DdHalfWord **newOrder = NULL;
    DdHalfWord **oldOrder = NULL;
    int *newCost = NULL;
    int *oldCost = NULL;
    DdHalfWord **tmpOrder;
    int *tmpCost;
    DdHalfWord *bestOrder = NULL;
    DdHalfWord *order;
#ifdef DD_STATS
    int  ddTotalSubsets;
#endif

    /* Restrict the range to be reordered by excluding unused variables
    ** at the two ends. */
    while (table->subtables[lower].keys == 1 &&
182 183 184
           table->vars[table->invperm[lower]]->ref == 1 &&
           lower < upper)
        lower++;
Alan Mishchenko committed
185
    while (table->subtables[upper].keys == 1 &&
186 187 188
           table->vars[table->invperm[upper]]->ref == 1 &&
           lower < upper)
        upper--;
Alan Mishchenko committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
    if (lower == upper) return(1); /* trivial problem */

    /* Apply symmetric sifting to get a good upper bound and to extract
    ** symmetry information. */
    result = cuddSymmSiftingConv(table,lower,upper);
    if (result == 0) goto cuddExactOutOfMem;

#ifdef DD_STATS
    (void) fprintf(table->out,"\n");
    ddTotalShuffles = 0;
    ddTotalSubsets = 0;
#endif

    /* Initialization. */
    nvars = table->size;
    size = upper - lower + 1;
    /* Count unused variable among those to be reordered.  This is only
    ** used to compute maxBinomial. */
    unused = 0;
    for (i = lower + 1; i < upper; i++) {
209 210 211
        if (table->subtables[i].keys == 1 &&
            table->vars[table->invperm[i]]->ref == 1)
            unused++;
Alan Mishchenko committed
212 213 214 215 216 217 218 219 220
    }

    /* Find the maximum number of subsets we may have to store. */
    maxBinomial = getMaxBinomial(size - unused);
    if (maxBinomial == -1) goto cuddExactOutOfMem;

    newOrder = getMatrix(maxBinomial, size);
    if (newOrder == NULL) goto cuddExactOutOfMem;

Alan Mishchenko committed
221
    newCost = ABC_ALLOC(int, maxBinomial);
Alan Mishchenko committed
222 223 224 225 226
    if (newCost == NULL) goto cuddExactOutOfMem;

    oldOrder = getMatrix(maxBinomial, size);
    if (oldOrder == NULL) goto cuddExactOutOfMem;

Alan Mishchenko committed
227
    oldCost = ABC_ALLOC(int, maxBinomial);
Alan Mishchenko committed
228 229
    if (oldCost == NULL) goto cuddExactOutOfMem;

Alan Mishchenko committed
230
    bestOrder = ABC_ALLOC(DdHalfWord, size);
Alan Mishchenko committed
231 232
    if (bestOrder == NULL) goto cuddExactOutOfMem;

Alan Mishchenko committed
233
    mask = ABC_ALLOC(char, nvars);
Alan Mishchenko committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247
    if (mask == NULL) goto cuddExactOutOfMem;

    symmInfo = initSymmInfo(table, lower, upper);
    if (symmInfo == NULL) goto cuddExactOutOfMem;

    roots = ddCountRoots(table, lower, upper);

    /* Initialize the old order matrix for the empty subset and the best
    ** order to the current order. The cost for the empty subset includes
    ** the cost of the levels between upper and the constants. These levels
    ** are not going to change. Hence, we count them only once.
    */
    oldSubsets = 1;
    for (i = 0; i < size; i++) {
248
        oldOrder[0][i] = bestOrder[i] = (DdHalfWord) table->invperm[i+lower];
Alan Mishchenko committed
249 250 251
    }
    subsetCost = table->constants.keys;
    for (i = upper + 1; i < nvars; i++)
252
        subsetCost += getLevelKeys(table,i);
Alan Mishchenko committed
253 254 255 256 257 258
    oldCost[0] = subsetCost;
    /* The upper bound is initialized to the current size of the BDDs. */
    upperBound = table->keys - table->isolated;

    /* Now consider subsets of increasing size. */
    for (k = 1; k <= size; k++) {
259 260 261
#ifdef DD_STATS
        (void) fprintf(table->out,"Processing subsets of size %d\n", k);
        fflush(table->out);
Alan Mishchenko committed
262
#endif
263 264 265 266 267 268 269 270 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
        newSubsets = 0;
        level = size - k;               /* offset of first bottom variable */

        for (i = 0; i < oldSubsets; i++) { /* for each subset of size k-1 */
            order = oldOrder[i];
            cost = oldCost[i];
            lowerBound = computeLB(table, order, roots, cost, lower, upper,
                                   level);
            if (lowerBound >= upperBound)
                continue;
            /* Impose new order. */
            result = ddShuffle(table, order, lower, upper);
            if (result == 0) goto cuddExactOutOfMem;
            upperBound = updateUB(table,upperBound,bestOrder,lower,upper);
            /* For each top bottom variable. */
            for (j = level; j >= 0; j--) {
                /* Skip unused variables. */
                if (table->subtables[j+lower-1].keys == 1 &&
                    table->vars[table->invperm[j+lower-1]]->ref == 1) continue;
                /* Find cost under this order. */
                subsetCost = cost + getLevelKeys(table, lower + level);
                newSubsets = updateEntry(table, order, level, subsetCost,
                                         newOrder, newCost, newSubsets, mask,
                                         lower, upper);
                if (j == 0)
                    break;
                if (checkSymmInfo(table, symmInfo, order[j-1], level) == 0)
                    continue;
                pushDown(order,j-1,level);
                /* Impose new order. */
                result = ddShuffle(table, order, lower, upper);
                if (result == 0) goto cuddExactOutOfMem;
                upperBound = updateUB(table,upperBound,bestOrder,lower,upper);
            } /* for each bottom variable */
        } /* for each subset of size k */

        /* New orders become old orders in preparation for next iteration. */
        tmpOrder = oldOrder; tmpCost = oldCost;
        oldOrder = newOrder; oldCost = newCost;
        newOrder = tmpOrder; newCost = tmpCost;
Alan Mishchenko committed
303
#ifdef DD_STATS
304
        ddTotalSubsets += newSubsets;
Alan Mishchenko committed
305
#endif
306
        oldSubsets = newSubsets;
Alan Mishchenko committed
307 308 309 310 311 312 313 314
    }
    result = ddShuffle(table, bestOrder, lower, upper);
    if (result == 0) goto cuddExactOutOfMem;
#ifdef DD_STATS
#ifdef DD_VERBOSE
    (void) fprintf(table->out,"\n");
#endif
    (void) fprintf(table->out,"#:S_EXACT   %8d: total subsets\n",
315
                   ddTotalSubsets);
Alan Mishchenko committed
316
    (void) fprintf(table->out,"#:H_EXACT   %8d: total shuffles",
317
                   ddTotalShuffles);
Alan Mishchenko committed
318 319 320 321
#endif

    freeMatrix(newOrder);
    freeMatrix(oldOrder);
Alan Mishchenko committed
322 323 324 325 326
    ABC_FREE(bestOrder);
    ABC_FREE(oldCost);
    ABC_FREE(newCost);
    ABC_FREE(symmInfo);
    ABC_FREE(mask);
Alan Mishchenko committed
327 328 329 330 331 332
    return(1);

cuddExactOutOfMem:

    if (newOrder != NULL) freeMatrix(newOrder);
    if (oldOrder != NULL) freeMatrix(oldOrder);
Alan Mishchenko committed
333 334 335 336 337
    if (bestOrder != NULL) ABC_FREE(bestOrder);
    if (oldCost != NULL) ABC_FREE(oldCost);
    if (newCost != NULL) ABC_FREE(newCost);
    if (symmInfo != NULL) ABC_FREE(symmInfo);
    if (mask != NULL) ABC_FREE(mask);
Alan Mishchenko committed
338 339 340 341 342 343 344 345 346 347 348 349
    table->errorCode = CUDD_MEMORY_OUT;
    return(0);

} /* end of cuddExact */


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

  Synopsis    [Returns the maximum value of (n choose k) for a given n.]

  Description [Computes the maximum value of (n choose k) for a given
  n.  The maximum value occurs for k = n/2 when n is even, or k =
350 351 352 353 354 355
  (n-1)/2 when n is odd.  The algorithm used in this procedure avoids
  intermediate overflow problems.  It is based on the identity
  <pre>
    binomial(n,k) = n/k * binomial(n-1,k-1).
  </pre>
  Returns the computed value if successful; -1 if out of range.]
Alan Mishchenko committed
356 357 358 359 360 361 362 363

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static int
getMaxBinomial(
364
  int n)
Alan Mishchenko committed
365
{
366 367 368 369
    double i, j, result;

    if (n < 0 || n > 33) return(-1); /* error */
    if (n < 2) return(1);
Alan Mishchenko committed
370

371 372 373 374
    for (result = (double)((n+3)/2), i = result+1, j=2; i <= n; i++, j++) {
        result *= i;
        result /= j;
    }
Alan Mishchenko committed
375

376
    return((int)result);
Alan Mishchenko committed
377

378
} /* end of getMaxBinomial */
Alan Mishchenko committed
379 380


381
#if 0
Alan Mishchenko committed
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
/**Function********************************************************************

  Synopsis    [Returns the gcd of two integers.]

  Description [Returns the gcd of two integers. Uses the binary GCD
  algorithm described in Cormen, Leiserson, and Rivest.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static int
gcd(
  int  x,
  int  y)
{
    int a;
    int b;
    int lsbMask;

    /* GCD(n,0) = n. */
    if (x == 0) return(y);
    if (y == 0) return(x);

    a = x; b = y; lsbMask = 1;
408

Alan Mishchenko committed
409 410 411 412
    /* Here both a and b are != 0. The iteration maintains this invariant.
    ** Hence, we only need to check for when they become equal.
    */
    while (a != b) {
413 414 415 416 417 418 419 420 421 422
        if (a & lsbMask) {
            if (b & lsbMask) {  /* both odd */
                if (a < b) {
                    b = (b - a) >> 1;
                } else {
                    a = (a - b) >> 1;
                }
            } else {            /* a odd, b even */
                b >>= 1;
            }
Alan Mishchenko committed
423
        } else {
424 425 426 427 428
            if (b & lsbMask) {  /* a even, b odd */
                a >>= 1;
            } else {            /* both even */
                lsbMask <<= 1;
            }
Alan Mishchenko committed
429 430 431 432 433 434
        }
    }

    return(a);

} /* end of gcd */
435
#endif
Alan Mishchenko committed
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458


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

  Synopsis    [Allocates a two-dimensional matrix of ints.]

  Description [Allocates a two-dimensional matrix of ints.
  Returns the pointer to the matrix if successful; NULL otherwise.]

  SideEffects [None]

  SeeAlso     [freeMatrix]

******************************************************************************/
static DdHalfWord **
getMatrix(
  int  rows /* number of rows */,
  int  cols /* number of columns */)
{
    DdHalfWord **matrix;
    int i;

    if (cols*rows == 0) return(NULL);
Alan Mishchenko committed
459
    matrix = ABC_ALLOC(DdHalfWord *, rows);
Alan Mishchenko committed
460
    if (matrix == NULL) return(NULL);
Alan Mishchenko committed
461
    matrix[0] = ABC_ALLOC(DdHalfWord, cols*rows);
462 463 464 465
    if (matrix[0] == NULL) {
        ABC_FREE(matrix);
        return(NULL);
    }
Alan Mishchenko committed
466
    for (i = 1; i < rows; i++) {
467
        matrix[i] = matrix[i-1] + cols;
Alan Mishchenko committed
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
    }
    return(matrix);

} /* end of getMatrix */


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

  Synopsis    [Frees a two-dimensional matrix allocated by getMatrix.]

  Description []

  SideEffects [None]

  SeeAlso     [getMatrix]

******************************************************************************/
static void
freeMatrix(
  DdHalfWord ** matrix)
{
Alan Mishchenko committed
489 490
    ABC_FREE(matrix[0]);
    ABC_FREE(matrix);
Alan Mishchenko committed
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
    return;

} /* end of freeMatrix */


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

  Synopsis    [Returns the number of nodes at one level of a unique table.]

  Description [Returns the number of nodes at one level of a unique table.
  The projection function, if isolated, is not counted.]

  SideEffects [None]

  SeeAlso []

******************************************************************************/
static int
getLevelKeys(
  DdManager * table,
  int  l)
{
    int isolated;
    int x;        /* x is an index */

    x = table->invperm[l];
    isolated = table->vars[x]->ref == 1;

    return(table->subtables[l].keys - isolated);

} /* end of getLevelKeys */


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

  Synopsis    [Reorders variables according to a given permutation.]

  Description [Reorders variables according to a given permutation.
  The i-th permutation array contains the index of the variable that
  should be brought to the i-th level. ddShuffle assumes that no
  dead nodes are present and that the interaction matrix is properly
  initialized.  The reordering is achieved by a series of upward sifts.
  Returns 1 if successful; 0 otherwise.]

  SideEffects [None]

  SeeAlso []

******************************************************************************/
static int
ddShuffle(
  DdManager * table,
  DdHalfWord * permutation,
  int  lower,
  int  upper)
{
547 548 549 550 551 552 553
    DdHalfWord  index;
    int         level;
    int         position;
#if 0
    int         numvars;
#endif
    int         result;
Alan Mishchenko committed
554
#ifdef DD_STATS
555 556
    long        localTime;
    int         initialSize;
Alan Mishchenko committed
557
#ifdef DD_VERBOSE
558
    int         finalSize;
Alan Mishchenko committed
559
#endif
560
    int         previousSize;
Alan Mishchenko committed
561 562 563 564 565 566 567
#endif

#ifdef DD_STATS
    localTime = util_cpu_time();
    initialSize = table->keys - table->isolated;
#endif

568
#if 0
Alan Mishchenko committed
569 570 571 572
    numvars = table->size;

    (void) fprintf(table->out,"%d:", ddTotalShuffles);
    for (level = 0; level < numvars; level++) {
573
        (void) fprintf(table->out," %d", table->invperm[level]);
Alan Mishchenko committed
574 575 576 577 578
    }
    (void) fprintf(table->out,"\n");
#endif

    for (level = 0; level <= upper - lower; level++) {
579 580
        index = permutation[level];
        position = table->perm[index];
Alan Mishchenko committed
581
#ifdef DD_STATS
582
        previousSize = table->keys - table->isolated;
Alan Mishchenko committed
583
#endif
584 585
        result = ddSiftUp(table,position,level+lower);
        if (!result) return(0);
Alan Mishchenko committed
586 587 588 589 590 591 592
    }

#ifdef DD_STATS
    ddTotalShuffles++;
#ifdef DD_VERBOSE
    finalSize = table->keys - table->isolated;
    if (finalSize < initialSize) {
593
        (void) fprintf(table->out,"-");
Alan Mishchenko committed
594
    } else if (finalSize > initialSize) {
595
        (void) fprintf(table->out,"+");
Alan Mishchenko committed
596
    } else {
597
        (void) fprintf(table->out,"=");
Alan Mishchenko committed
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
    }
    if ((ddTotalShuffles & 63) == 0) (void) fprintf(table->out,"\n");
    fflush(table->out);
#endif
#endif

    return(1);

} /* end of ddShuffle */


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

  Synopsis    [Moves one variable up.]

  Description [Takes a variable from position x and sifts it up to
  position xLow;  xLow should be less than or equal to x.
  Returns 1 if successful; 0 otherwise]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static int
ddSiftUp(
  DdManager * table,
  int  x,
  int  xLow)
{
    int        y;
    int        size;

    y = cuddNextLow(table,x);
    while (y >= xLow) {
633 634 635 636 637 638
        size = cuddSwapInPlace(table,y,x);
        if (size == 0) {
            return(0);
        }
        x = y;
        y = cuddNextLow(table,x);
Alan Mishchenko committed
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
    }
    return(1);

} /* end of ddSiftUp */


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

  Synopsis    [Updates the upper bound and saves the best order seen so far.]

  Description [Updates the upper bound and saves the best order seen so far.
  Returns the current value of the upper bound.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static int
updateUB(
  DdManager * table,
  int  oldBound,
  DdHalfWord * bestOrder,
  int  lower,
  int  upper)
{
    int i;
    int newBound = table->keys - table->isolated;

    if (newBound < oldBound) {
#ifdef DD_STATS
670 671
        (void) fprintf(table->out,"New upper bound = %d\n", newBound);
        fflush(table->out);
Alan Mishchenko committed
672
#endif
673 674 675
        for (i = lower; i <= upper; i++)
            bestOrder[i-lower] = (DdHalfWord) table->invperm[i];
        return(newBound);
Alan Mishchenko committed
676
    } else {
677
        return(oldBound);
Alan Mishchenko committed
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
    }

} /* end of updateUB */


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

  Synopsis    [Counts the number of roots.]

  Description [Counts the number of roots at the levels between lower and
  upper.  The computation is based on breadth-first search.
  A node is a root if it is not reachable from any previously visited node.
  (All the nodes at level lower are therefore considered roots.)
  The visited flag uses the LSB of the next pointer.  Returns the root
  count. The roots that are constant nodes are always ignored.]

  SideEffects [None]

  SeeAlso     [ddClearGlobal]

******************************************************************************/
static int
ddCountRoots(
  DdManager * table,
  int  lower,
  int  upper)
{
    int i,j;
    DdNode *f;
    DdNodePtr *nodelist;
    DdNode *sentinel = &(table->sentinel);
    int slots;
    int roots = 0;
    int maxlevel = lower;

    for (i = lower; i <= upper; i++) {
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
        nodelist = table->subtables[i].nodelist;
        slots = table->subtables[i].slots;
        for (j = 0; j < slots; j++) {
            f = nodelist[j];
            while (f != sentinel) {
                /* A node is a root of the DAG if it cannot be
                ** reached by nodes above it. If a node was never
                ** reached during the previous depth-first searches,
                ** then it is a root, and we start a new depth-first
                ** search from it.
                */
                if (!Cudd_IsComplement(f->next)) {
                    if (f != table->vars[f->index]) {
                        roots++;
                    }
                }
                if (!Cudd_IsConstant(cuddT(f))) {
                    cuddT(f)->next = Cudd_Complement(cuddT(f)->next);
                    if (table->perm[cuddT(f)->index] > maxlevel)
                        maxlevel = table->perm[cuddT(f)->index];
                }
                if (!Cudd_IsConstant(cuddE(f))) {
                    Cudd_Regular(cuddE(f))->next =
                        Cudd_Complement(Cudd_Regular(cuddE(f))->next);
                    if (table->perm[Cudd_Regular(cuddE(f))->index] > maxlevel)
                        maxlevel = table->perm[Cudd_Regular(cuddE(f))->index];
                }
                f = Cudd_Regular(f->next);
Alan Mishchenko committed
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
            }
        }
    }
    ddClearGlobal(table, lower, maxlevel);

    return(roots);

} /* end of ddCountRoots */


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

  Synopsis    [Scans the DD and clears the LSB of the next pointers.]

  Description [Scans the DD and clears the LSB of the next pointers.
  The LSB of the next pointers are used as markers to tell whether a
  node was reached. Once the roots are counted, these flags are
  reset.]

  SideEffects [None]

  SeeAlso     [ddCountRoots]

******************************************************************************/
static void
ddClearGlobal(
  DdManager * table,
  int  lower,
  int  maxlevel)
{
    int i,j;
    DdNode *f;
    DdNodePtr *nodelist;
    DdNode *sentinel = &(table->sentinel);
    int slots;

    for (i = lower; i <= maxlevel; i++) {
779 780 781 782 783 784 785 786
        nodelist = table->subtables[i].nodelist;
        slots = table->subtables[i].slots;
        for (j = 0; j < slots; j++) {
            f = nodelist[j];
            while (f != sentinel) {
                f->next = Cudd_Regular(f->next);
                f = f->next;
            }
Alan Mishchenko committed
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
        }
    }

} /* end of ddClearGlobal */


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

  Synopsis    [Computes a lower bound on the size of a BDD.]

  Description [Computes a lower bound on the size of a BDD from the
  following factors:
  <ul>
  <li> size of the lower part of it;
  <li> size of the part of the upper part not subjected to reordering;
  <li> number of roots in the part of the BDD subjected to reordering;
  <li> variable in the support of the roots in the upper part of the
       BDD subjected to reordering.
  <ul/>]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static int
computeLB(
814 815 816 817 818 819 820
  DdManager * table             /* manager */,
  DdHalfWord * order            /* optimal order for the subset */,
  int  roots                    /* roots between lower and upper */,
  int  cost                     /* minimum cost for the subset */,
  int  lower                    /* lower level to be reordered */,
  int  upper                    /* upper level to be reordered */,
  int  level                    /* offset for the current top bottom var */
Alan Mishchenko committed
821 822 823 824 825 826 827 828 829 830 831 832 833
  )
{
    int i;
    int lb = cost;
    int lb1 = 0;
    int lb2;
    int support;
    DdHalfWord ref;

    /* The levels not involved in reordering are not going to change.
    ** Add their sizes to the lower bound.
    */
    for (i = 0; i < lower; i++) {
834
        lb += getLevelKeys(table,i);
Alan Mishchenko committed
835 836 837 838 839
    }
    /* If a variable is in the support, then there is going
    ** to be at least one node labeled by that variable.
    */
    for (i = lower; i <= lower+level; i++) {
840 841 842
        support = table->subtables[i].keys > 1 ||
            table->vars[order[i-lower]]->ref > 1;
        lb1 += support;
Alan Mishchenko committed
843 844 845 846 847
    }

    /* Estimate the number of nodes required to connect the roots to
    ** the nodes in the bottom part. */
    if (lower+level+1 < table->size) {
848 849 850 851 852 853
        if (lower+level < upper)
            ref = table->vars[order[level+1]]->ref;
        else
            ref = table->vars[table->invperm[upper+1]]->ref;
        lb2 = table->subtables[lower+level+1].keys -
            (ref > (DdHalfWord) 1) - roots;
Alan Mishchenko committed
854
    } else {
855
        lb2 = 0;
Alan Mishchenko committed
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
    }

    lb += lb1 > lb2 ? lb1 : lb2;

    return(lb);

} /* end of computeLB */


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

  Synopsis    [Updates entry for a subset.]

  Description [Updates entry for a subset. Finds the subset, if it exists.
  If the new order for the subset has lower cost, or if the subset did not
  exist, it stores the new order and cost. Returns the number of subsets
  currently in the table.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static int
updateEntry(
  DdManager * table,
  DdHalfWord * order,
  int  level,
  int  cost,
  DdHalfWord ** orders,
  int * costs,
  int  subsets,
  char * mask,
  int  lower,
  int  upper)
{
    int i, j;
    int size = upper - lower + 1;

    /* Build a mask that says what variables are in this subset. */
    for (i = lower; i <= upper; i++)
897
        mask[table->invperm[i]] = 0;
Alan Mishchenko committed
898
    for (i = level; i < size; i++)
899
        mask[order[i]] = 1;
Alan Mishchenko committed
900 901 902

    /* Check each subset until a match is found or all subsets are examined. */
    for (i = 0; i < subsets; i++) {
903 904 905 906 907 908 909
        DdHalfWord *subset = orders[i];
        for (j = level; j < size; j++) {
            if (mask[subset[j]] == 0)
                break;
        }
        if (j == size)          /* no mismatches: success */
            break;
Alan Mishchenko committed
910
    }
911 912 913 914 915
    if (i == subsets || cost < costs[i]) {              /* add or replace */
        for (j = 0; j < size; j++)
            orders[i][j] = order[j];
        costs[i] = cost;
        subsets += (i == subsets);
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
    }
    return(subsets);

} /* end of updateEntry */


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

  Synopsis    [Pushes a variable in the order down to position "level."]

  Description []

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static void
pushDown(
  DdHalfWord * order,
  int  j,
  int  level)
{
    int i;
    DdHalfWord tmp;

    tmp = order[j];
    for (i = j; i < level; i++) {
944
        order[i] = order[i+1];
Alan Mishchenko committed
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
    }
    order[level] = tmp;
    return;

} /* end of pushDown */


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

  Synopsis    [Gathers symmetry information.]

  Description [Translates the symmetry information stored in the next
  field of each subtable from level to indices. This procedure is called
  immediately after symmetric sifting, so that the next fields are correct.
  By translating this informaton in terms of indices, we make it independent
  of subsequent reorderings. The format used is that of the next fields:
  a circular list where each variable points to the next variable in the
  same symmetry group. Only the entries between lower and upper are
  considered.  The procedure returns a pointer to an array
  holding the symmetry information if successful; NULL otherwise.]

  SideEffects [None]

  SeeAlso     [checkSymmInfo]

******************************************************************************/
static DdHalfWord *
initSymmInfo(
  DdManager * table,
  int  lower,
  int  upper)
{
    int level, index, next, nextindex;
    DdHalfWord *symmInfo;

Alan Mishchenko committed
980
    symmInfo =  ABC_ALLOC(DdHalfWord, table->size);
Alan Mishchenko committed
981 982 983
    if (symmInfo == NULL) return(NULL);

    for (level = lower; level <= upper; level++) {
984 985 986 987
        index = table->invperm[level];
        next =  table->subtables[level].next;
        nextindex = table->invperm[next];
        symmInfo[index] = nextindex;
Alan Mishchenko committed
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
    }
    return(symmInfo);

} /* end of initSymmInfo */


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

  Synopsis    [Check symmetry condition.]

  Description [Returns 1 if a variable is the one with the highest index
  among those belonging to a symmetry group that are in the top part of
  the BDD.  The top part is given by level.]

  SideEffects [None]

  SeeAlso     [initSymmInfo]

******************************************************************************/
static int
checkSymmInfo(
  DdManager * table,
  DdHalfWord * symmInfo,
  int  index,
  int  level)
{
    int i;

    i = symmInfo[index];
    while (i != index) {
1018 1019 1020
        if (index < i && table->perm[i] <= level)
            return(0);
        i = symmInfo[i];
Alan Mishchenko committed
1021 1022 1023 1024 1025
    }
    return(1);

} /* end of checkSymmInfo */

1026

1027 1028
ABC_NAMESPACE_IMPL_END