cuddWindow.c 30.5 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6
/**CFile***********************************************************************

  FileName    [cuddWindow.c]

  PackageName [cudd]

7
  Synopsis    [Functions for variable reordering by window permutation.]
Alan Mishchenko committed
8 9

  Description [Internal procedures included in this module:
10 11 12 13 14 15 16 17 18 19 20 21 22 23
                <ul>
                <li> cuddWindowReorder()
                </ul>
        Static procedures included in this module:
                <ul>
                <li> ddWindow2()
                <li> ddWindowConv2()
                <li> ddPermuteWindow3()
                <li> ddWindow3()
                <li> ddWindowConv3()
                <li> ddPermuteWindow4()
                <li> ddWindow4()
                <li> ddWindowConv4()
                </ul>]
Alan Mishchenko committed
24 25 26

  Author      [Fabio Somenzi]

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
  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
58 59 60

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

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

64 65 66
ABC_NAMESPACE_IMPL_START


67

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


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


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


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

#ifndef lint
88
static char rcsid[] DD_UNUSED = "$Id: cuddWindow.c,v 1.14 2009/02/20 02:14:58 fabio Exp $";
Alan Mishchenko committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
#endif

#ifdef DD_STATS
extern  int     ddTotalNumberSwapping;
extern  int     ddTotalNISwaps;
#endif

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


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

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

107 108 109 110 111 112 113 114
static int ddWindow2 (DdManager *table, int low, int high);
static int ddWindowConv2 (DdManager *table, int low, int high);
static int ddPermuteWindow3 (DdManager *table, int x);
static int ddWindow3 (DdManager *table, int low, int high);
static int ddWindowConv3 (DdManager *table, int low, int high);
static int ddPermuteWindow4 (DdManager *table, int w);
static int ddWindow4 (DdManager *table, int low, int high);
static int ddWindowConv4 (DdManager *table, int low, int high);
Alan Mishchenko committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

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


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

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


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

  Synopsis    [Reorders by applying the method of the sliding window.]

  Description [Reorders by applying the method of the sliding window.
  Tries all possible permutations to the variables in a window that
  slides from low to high. The size of the window is determined by
  submethod.  Assumes that no dead nodes are present.  Returns 1 in
  case of success; 0 otherwise.]

  SideEffects [None]

******************************************************************************/
int
cuddWindowReorder(
  DdManager * table /* DD table */,
  int low /* lowest index to reorder */,
  int high /* highest index to reorder */,
  Cudd_ReorderingType submethod /* window reordering option */)
{

    int res;
#ifdef DD_DEBUG
    int supposedOpt;
#endif

    switch (submethod) {
    case CUDD_REORDER_WINDOW2:
156 157
        res = ddWindow2(table,low,high);
        break;
Alan Mishchenko committed
158
    case CUDD_REORDER_WINDOW3:
159 160
        res = ddWindow3(table,low,high);
        break;
Alan Mishchenko committed
161
    case CUDD_REORDER_WINDOW4:
162 163
        res = ddWindow4(table,low,high);
        break;
Alan Mishchenko committed
164
    case CUDD_REORDER_WINDOW2_CONV:
165 166
        res = ddWindowConv2(table,low,high);
        break;
Alan Mishchenko committed
167
    case CUDD_REORDER_WINDOW3_CONV:
168
        res = ddWindowConv3(table,low,high);
Alan Mishchenko committed
169
#ifdef DD_DEBUG
170 171 172 173 174 175
        supposedOpt = table->keys - table->isolated;
        res = ddWindow3(table,low,high);
        if (table->keys - table->isolated != (unsigned) supposedOpt) {
            (void) fprintf(table->err, "Convergence failed! (%d != %d)\n",
                           table->keys - table->isolated, supposedOpt);
        }
Alan Mishchenko committed
176
#endif
177
        break;
Alan Mishchenko committed
178
    case CUDD_REORDER_WINDOW4_CONV:
179
        res = ddWindowConv4(table,low,high);
Alan Mishchenko committed
180
#ifdef DD_DEBUG
181 182 183 184 185 186
        supposedOpt = table->keys - table->isolated;
        res = ddWindow4(table,low,high);
        if (table->keys - table->isolated != (unsigned) supposedOpt) {
            (void) fprintf(table->err,"Convergence failed! (%d != %d)\n",
                           table->keys - table->isolated, supposedOpt);
        }
Alan Mishchenko committed
187
#endif
188
        break;
Alan Mishchenko committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
    default: return(0);
    }

    return(res);

} /* end of cuddWindowReorder */


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

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

  Synopsis    [Reorders by applying a sliding window of width 2.]

  Description [Reorders by applying a sliding window of width 2.
  Tries both permutations of the variables in a window
  that slides from low to high.  Assumes that no dead nodes are
  present.  Returns 1 in case of success; 0 otherwise.]

  SideEffects [None]

******************************************************************************/
static int
ddWindow2(
  DdManager * table,
  int  low,
  int  high)
{

    int x;
    int res;
    int size;

#ifdef DD_DEBUG
    assert(low >= 0 && high < table->size);
#endif

    if (high-low < 1) return(0);

    res = table->keys - table->isolated;
    for (x = low; x < high; x++) {
232
        size = res;
Alan Mishchenko committed
233 234
        res = cuddSwapInPlace(table,x,x+1);
        if (res == 0) return(0);
235 236 237 238
        if (res >= size) { /* no improvement: undo permutation */
            res = cuddSwapInPlace(table,x,x+1);
            if (res == 0) return(0);
        }
Alan Mishchenko committed
239
#ifdef DD_STATS
240 241 242 243 244 245
        if (res < size) {
            (void) fprintf(table->out,"-");
        } else {
            (void) fprintf(table->out,"=");
        }
        fflush(table->out);
Alan Mishchenko committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
#endif
    }

    return(1);

} /* end of ddWindow2 */


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

  Synopsis    [Reorders by repeatedly applying a sliding window of width 2.]

  Description [Reorders by repeatedly applying a sliding window of width
  2. Tries both permutations of the variables in a window
  that slides from low to high.  Assumes that no dead nodes are
  present.  Uses an event-driven approach to determine convergence.
  Returns 1 in case of success; 0 otherwise.]

  SideEffects [None]

******************************************************************************/
static int
ddWindowConv2(
  DdManager * table,
  int  low,
  int  high)
{
    int x;
    int res;
    int nwin;
    int newevent;
    int *events;
    int size;

#ifdef DD_DEBUG
    assert(low >= 0 && high < table->size);
#endif

    if (high-low < 1) return(ddWindowConv2(table,low,high));

    nwin = high-low;
Alan Mishchenko committed
287
    events = ABC_ALLOC(int,nwin);
Alan Mishchenko committed
288
    if (events == NULL) {
289 290
        table->errorCode = CUDD_MEMORY_OUT;
        return(0);
Alan Mishchenko committed
291 292
    }
    for (x=0; x<nwin; x++) {
293
        events[x] = 1;
Alan Mishchenko committed
294 295 296 297
    }

    res = table->keys - table->isolated;
    do {
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
        newevent = 0;
        for (x=0; x<nwin; x++) {
            if (events[x]) {
                size = res;
                res = cuddSwapInPlace(table,x+low,x+low+1);
                if (res == 0) {
                    ABC_FREE(events);
                    return(0);
                }
                if (res >= size) { /* no improvement: undo permutation */
                    res = cuddSwapInPlace(table,x+low,x+low+1);
                    if (res == 0) {
                        ABC_FREE(events);
                        return(0);
                    }
                }
                if (res < size) {
                    if (x < nwin-1)     events[x+1] = 1;
                    if (x > 0)          events[x-1] = 1;
                    newevent = 1;
                }
                events[x] = 0;
Alan Mishchenko committed
320
#ifdef DD_STATS
321 322 323 324 325 326
                if (res < size) {
                    (void) fprintf(table->out,"-");
                } else {
                    (void) fprintf(table->out,"=");
                }
                fflush(table->out);
Alan Mishchenko committed
327
#endif
328
            }
Alan Mishchenko committed
329 330
        }
#ifdef DD_STATS
331 332 333 334
        if (newevent) {
            (void) fprintf(table->out,"|");
            fflush(table->out);
        }
Alan Mishchenko committed
335 336 337
#endif
    } while (newevent);

Alan Mishchenko committed
338
    ABC_FREE(events);
Alan Mishchenko committed
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365

    return(1);

} /* end of ddWindowConv3 */


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

  Synopsis [Tries all the permutations of the three variables between
  x and x+2 and retains the best.]

  Description [Tries all the permutations of the three variables between
  x and x+2 and retains the best. Assumes that no dead nodes are
  present.  Returns the index of the best permutation (1-6) in case of
  success; 0 otherwise.Assumes that no dead nodes are present.  Returns
  the index of the best permutation (1-6) in case of success; 0
  otherwise.]

  SideEffects [None]

******************************************************************************/
static int
ddPermuteWindow3(
  DdManager * table,
  int  x)
{
    int y,z;
366 367
    int size,sizeNew;
    int best;
Alan Mishchenko committed
368 369 370 371 372 373 374 375

#ifdef DD_DEBUG
    assert(table->dead == 0);
    assert(x+2 < table->size);
#endif

    size = table->keys - table->isolated;
    y = x+1; z = y+1;
376

Alan Mishchenko committed
377 378 379 380 381 382 383
    /* The permutation pattern is:
    ** (x,y)(y,z)
    ** repeated three times to get all 3! = 6 permutations.
    */
#define ABC 1
    best = ABC;

384
#define BAC 2
Alan Mishchenko committed
385 386
    sizeNew = cuddSwapInPlace(table,x,y);
    if (sizeNew < size) {
387 388 389
        if (sizeNew == 0) return(0);
        best = BAC;
        size = sizeNew;
Alan Mishchenko committed
390 391 392 393
    }
#define BCA 3
    sizeNew = cuddSwapInPlace(table,y,z);
    if (sizeNew < size) {
394 395 396
        if (sizeNew == 0) return(0);
        best = BCA;
        size = sizeNew;
Alan Mishchenko committed
397 398 399 400
    }
#define CBA 4
    sizeNew = cuddSwapInPlace(table,x,y);
    if (sizeNew < size) {
401 402 403
        if (sizeNew == 0) return(0);
        best = CBA;
        size = sizeNew;
Alan Mishchenko committed
404 405 406 407
    }
#define CAB 5
    sizeNew = cuddSwapInPlace(table,y,z);
    if (sizeNew < size) {
408 409 410
        if (sizeNew == 0) return(0);
        best = CAB;
        size = sizeNew;
Alan Mishchenko committed
411 412 413 414
    }
#define ACB 6
    sizeNew = cuddSwapInPlace(table,x,y);
    if (sizeNew < size) {
415 416 417
        if (sizeNew == 0) return(0);
        best = ACB;
        size = sizeNew;
Alan Mishchenko committed
418 419 420 421 422 423 424 425 426 427 428 429
    }

    /* Now take the shortest route to the best permuytation.
    ** The initial permutation is ACB.
    */
    switch(best) {
    case BCA: if (!cuddSwapInPlace(table,y,z)) return(0);
    case CBA: if (!cuddSwapInPlace(table,x,y)) return(0);
    case ABC: if (!cuddSwapInPlace(table,y,z)) return(0);
    case ACB: break;
    case BAC: if (!cuddSwapInPlace(table,y,z)) return(0);
    case CAB: if (!cuddSwapInPlace(table,x,y)) return(0);
430
               break;
Alan Mishchenko committed
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
    default: return(0);
    }

#ifdef DD_DEBUG
    assert(table->keys - table->isolated == (unsigned) size);
#endif

    return(best);

} /* end of ddPermuteWindow3 */


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

  Synopsis    [Reorders by applying a sliding window of width 3.]

  Description [Reorders by applying a sliding window of width 3.
  Tries all possible permutations to the variables in a
  window that slides from low to high.  Assumes that no dead nodes are
  present.  Returns 1 in case of success; 0 otherwise.]

  SideEffects [None]

******************************************************************************/
static int
ddWindow3(
  DdManager * table,
  int  low,
  int  high)
{

    int x;
    int res;

#ifdef DD_DEBUG
    assert(low >= 0 && high < table->size);
#endif

    if (high-low < 2) return(ddWindow2(table,low,high));

    for (x = low; x+1 < high; x++) {
472 473
        res = ddPermuteWindow3(table,x);
        if (res == 0) return(0);
Alan Mishchenko committed
474
#ifdef DD_STATS
475 476 477 478 479 480
        if (res == ABC) {
            (void) fprintf(table->out,"=");
        } else {
            (void) fprintf(table->out,"-");
        }
        fflush(table->out);
Alan Mishchenko committed
481 482 483 484 485 486 487 488 489 490 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
#endif
    }

    return(1);

} /* end of ddWindow3 */


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

  Synopsis    [Reorders by repeatedly applying a sliding window of width 3.]

  Description [Reorders by repeatedly applying a sliding window of width
  3. Tries all possible permutations to the variables in a
  window that slides from low to high.  Assumes that no dead nodes are
  present.  Uses an event-driven approach to determine convergence.
  Returns 1 in case of success; 0 otherwise.]

  SideEffects [None]

******************************************************************************/
static int
ddWindowConv3(
  DdManager * table,
  int  low,
  int  high)
{
    int x;
    int res;
    int nwin;
    int newevent;
    int *events;

#ifdef DD_DEBUG
    assert(low >= 0 && high < table->size);
#endif

    if (high-low < 2) return(ddWindowConv2(table,low,high));

    nwin = high-low-1;
Alan Mishchenko committed
521
    events = ABC_ALLOC(int,nwin);
Alan Mishchenko committed
522
    if (events == NULL) {
523 524
        table->errorCode = CUDD_MEMORY_OUT;
        return(0);
Alan Mishchenko committed
525 526
    }
    for (x=0; x<nwin; x++) {
527
        events[x] = 1;
Alan Mishchenko committed
528 529 530
    }

    do {
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
        newevent = 0;
        for (x=0; x<nwin; x++) {
            if (events[x]) {
                res = ddPermuteWindow3(table,x+low);
                switch (res) {
                case ABC:
                    break;
                case BAC:
                    if (x < nwin-1)     events[x+1] = 1;
                    if (x > 1)          events[x-2] = 1;
                    newevent = 1;
                    break;
                case BCA:
                case CBA:
                case CAB:
                    if (x < nwin-2)     events[x+2] = 1;
                    if (x < nwin-1)     events[x+1] = 1;
                    if (x > 0)          events[x-1] = 1;
                    if (x > 1)          events[x-2] = 1;
                    newevent = 1;
                    break;
                case ACB:
                    if (x < nwin-2)     events[x+2] = 1;
                    if (x > 0)          events[x-1] = 1;
                    newevent = 1;
                    break;
                default:
                    ABC_FREE(events);
                    return(0);
                }
                events[x] = 0;
Alan Mishchenko committed
562
#ifdef DD_STATS
563 564 565 566 567 568
                if (res == ABC) {
                    (void) fprintf(table->out,"=");
                } else {
                    (void) fprintf(table->out,"-");
                }
                fflush(table->out);
Alan Mishchenko committed
569
#endif
570
            }
Alan Mishchenko committed
571 572
        }
#ifdef DD_STATS
573 574 575 576
        if (newevent) {
            (void) fprintf(table->out,"|");
            fflush(table->out);
        }
Alan Mishchenko committed
577 578 579
#endif
    } while (newevent);

Alan Mishchenko committed
580
    ABC_FREE(events);
Alan Mishchenko committed
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

    return(1);

} /* end of ddWindowConv3 */


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

  Synopsis [Tries all the permutations of the four variables between w
  and w+3 and retains the best.]

  Description [Tries all the permutations of the four variables between
  w and w+3 and retains the best. Assumes that no dead nodes are
  present.  Returns the index of the best permutation (1-24) in case of
  success; 0 otherwise.]

  SideEffects [None]

******************************************************************************/
static int
ddPermuteWindow4(
  DdManager * table,
  int  w)
{
    int x,y,z;
606 607
    int size,sizeNew;
    int best;
Alan Mishchenko committed
608 609 610 611 612 613 614 615

#ifdef DD_DEBUG
    assert(table->dead == 0);
    assert(w+3 < table->size);
#endif

    size = table->keys - table->isolated;
    x = w+1; y = x+1; z = y+1;
616

Alan Mishchenko committed
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
    /* The permutation pattern is:
     * (w,x)(y,z)(w,x)(x,y)
     * (y,z)(w,x)(y,z)(x,y)
     * repeated three times to get all 4! = 24 permutations.
     * This gives a hamiltonian circuit of Cayley's graph.
     * The codes to the permutation are assigned in topological order.
     * The permutations at lower distance from the final permutation are
     * assigned lower codes. This way we can choose, between
     * permutations that give the same size, one that requires the minimum
     * number of swaps from the final permutation of the hamiltonian circuit.
     * There is an exception to this rule: ABCD is given Code 1, to
     * avoid oscillation when convergence is sought.
     */
#define ABCD 1
    best = ABCD;

633
#define BACD 7
Alan Mishchenko committed
634 635
    sizeNew = cuddSwapInPlace(table,w,x);
    if (sizeNew < size) {
636 637 638
        if (sizeNew == 0) return(0);
        best = BACD;
        size = sizeNew;
Alan Mishchenko committed
639 640 641 642
    }
#define BADC 13
    sizeNew = cuddSwapInPlace(table,y,z);
    if (sizeNew < size) {
643 644 645
        if (sizeNew == 0) return(0);
        best = BADC;
        size = sizeNew;
Alan Mishchenko committed
646 647 648 649
    }
#define ABDC 8
    sizeNew = cuddSwapInPlace(table,w,x);
    if (sizeNew < size || (sizeNew == size && ABDC < best)) {
650 651 652
        if (sizeNew == 0) return(0);
        best = ABDC;
        size = sizeNew;
Alan Mishchenko committed
653 654 655 656
    }
#define ADBC 14
    sizeNew = cuddSwapInPlace(table,x,y);
    if (sizeNew < size) {
657 658 659
        if (sizeNew == 0) return(0);
        best = ADBC;
        size = sizeNew;
Alan Mishchenko committed
660 661 662 663
    }
#define ADCB 9
    sizeNew = cuddSwapInPlace(table,y,z);
    if (sizeNew < size || (sizeNew == size && ADCB < best)) {
664 665 666
        if (sizeNew == 0) return(0);
        best = ADCB;
        size = sizeNew;
Alan Mishchenko committed
667 668 669 670
    }
#define DACB 15
    sizeNew = cuddSwapInPlace(table,w,x);
    if (sizeNew < size) {
671 672 673
        if (sizeNew == 0) return(0);
        best = DACB;
        size = sizeNew;
Alan Mishchenko committed
674 675 676 677
    }
#define DABC 20
    sizeNew = cuddSwapInPlace(table,y,z);
    if (sizeNew < size) {
678 679 680
        if (sizeNew == 0) return(0);
        best = DABC;
        size = sizeNew;
Alan Mishchenko committed
681 682 683 684
    }
#define DBAC 23
    sizeNew = cuddSwapInPlace(table,x,y);
    if (sizeNew < size) {
685 686 687
        if (sizeNew == 0) return(0);
        best = DBAC;
        size = sizeNew;
Alan Mishchenko committed
688 689 690 691
    }
#define BDAC 19
    sizeNew = cuddSwapInPlace(table,w,x);
    if (sizeNew < size || (sizeNew == size && BDAC < best)) {
692 693 694
        if (sizeNew == 0) return(0);
        best = BDAC;
        size = sizeNew;
Alan Mishchenko committed
695 696 697 698
    }
#define BDCA 21
    sizeNew = cuddSwapInPlace(table,y,z);
    if (sizeNew < size || (sizeNew == size && BDCA < best)) {
699 700 701
        if (sizeNew == 0) return(0);
        best = BDCA;
        size = sizeNew;
Alan Mishchenko committed
702 703 704 705
    }
#define DBCA 24
    sizeNew = cuddSwapInPlace(table,w,x);
    if (sizeNew < size) {
706 707 708
        if (sizeNew == 0) return(0);
        best = DBCA;
        size = sizeNew;
Alan Mishchenko committed
709 710 711 712
    }
#define DCBA 22
    sizeNew = cuddSwapInPlace(table,x,y);
    if (sizeNew < size || (sizeNew == size && DCBA < best)) {
713 714 715
        if (sizeNew == 0) return(0);
        best = DCBA;
        size = sizeNew;
Alan Mishchenko committed
716 717 718 719
    }
#define DCAB 18
    sizeNew = cuddSwapInPlace(table,y,z);
    if (sizeNew < size || (sizeNew == size && DCAB < best)) {
720 721 722
        if (sizeNew == 0) return(0);
        best = DCAB;
        size = sizeNew;
Alan Mishchenko committed
723 724 725 726
    }
#define CDAB 12
    sizeNew = cuddSwapInPlace(table,w,x);
    if (sizeNew < size || (sizeNew == size && CDAB < best)) {
727 728 729
        if (sizeNew == 0) return(0);
        best = CDAB;
        size = sizeNew;
Alan Mishchenko committed
730 731 732 733
    }
#define CDBA 17
    sizeNew = cuddSwapInPlace(table,y,z);
    if (sizeNew < size || (sizeNew == size && CDBA < best)) {
734 735 736
        if (sizeNew == 0) return(0);
        best = CDBA;
        size = sizeNew;
Alan Mishchenko committed
737 738 739 740
    }
#define CBDA 11
    sizeNew = cuddSwapInPlace(table,x,y);
    if (sizeNew < size || (sizeNew == size && CBDA < best)) {
741 742 743
        if (sizeNew == 0) return(0);
        best = CBDA;
        size = sizeNew;
Alan Mishchenko committed
744 745 746 747
    }
#define BCDA 16
    sizeNew = cuddSwapInPlace(table,w,x);
    if (sizeNew < size || (sizeNew == size && BCDA < best)) {
748 749 750
        if (sizeNew == 0) return(0);
        best = BCDA;
        size = sizeNew;
Alan Mishchenko committed
751 752 753 754
    }
#define BCAD 10
    sizeNew = cuddSwapInPlace(table,y,z);
    if (sizeNew < size || (sizeNew == size && BCAD < best)) {
755 756 757
        if (sizeNew == 0) return(0);
        best = BCAD;
        size = sizeNew;
Alan Mishchenko committed
758 759 760 761
    }
#define CBAD 5
    sizeNew = cuddSwapInPlace(table,w,x);
    if (sizeNew < size || (sizeNew == size && CBAD < best)) {
762 763 764
        if (sizeNew == 0) return(0);
        best = CBAD;
        size = sizeNew;
Alan Mishchenko committed
765 766 767 768
    }
#define CABD 3
    sizeNew = cuddSwapInPlace(table,x,y);
    if (sizeNew < size || (sizeNew == size && CABD < best)) {
769 770 771
        if (sizeNew == 0) return(0);
        best = CABD;
        size = sizeNew;
Alan Mishchenko committed
772 773 774 775
    }
#define CADB 6
    sizeNew = cuddSwapInPlace(table,y,z);
    if (sizeNew < size || (sizeNew == size && CADB < best)) {
776 777 778
        if (sizeNew == 0) return(0);
        best = CADB;
        size = sizeNew;
Alan Mishchenko committed
779 780 781 782
    }
#define ACDB 4
    sizeNew = cuddSwapInPlace(table,w,x);
    if (sizeNew < size || (sizeNew == size && ACDB < best)) {
783 784 785
        if (sizeNew == 0) return(0);
        best = ACDB;
        size = sizeNew;
Alan Mishchenko committed
786 787 788 789
    }
#define ACBD 2
    sizeNew = cuddSwapInPlace(table,y,z);
    if (sizeNew < size || (sizeNew == size && ACBD < best)) {
790 791 792
        if (sizeNew == 0) return(0);
        best = ACBD;
        size = sizeNew;
Alan Mishchenko committed
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
    }

    /* Now take the shortest route to the best permutation.
    ** The initial permutation is ACBD.
    */
    switch(best) {
    case DBCA: if (!cuddSwapInPlace(table,y,z)) return(0);
    case BDCA: if (!cuddSwapInPlace(table,x,y)) return(0);
    case CDBA: if (!cuddSwapInPlace(table,w,x)) return(0);
    case ADBC: if (!cuddSwapInPlace(table,y,z)) return(0);
    case ABDC: if (!cuddSwapInPlace(table,x,y)) return(0);
    case ACDB: if (!cuddSwapInPlace(table,y,z)) return(0);
    case ACBD: break;
    case DCBA: if (!cuddSwapInPlace(table,y,z)) return(0);
    case BCDA: if (!cuddSwapInPlace(table,x,y)) return(0);
    case CBDA: if (!cuddSwapInPlace(table,w,x)) return(0);
809 810 811
               if (!cuddSwapInPlace(table,x,y)) return(0);
               if (!cuddSwapInPlace(table,y,z)) return(0);
               break;
Alan Mishchenko committed
812 813 814 815 816
    case DBAC: if (!cuddSwapInPlace(table,x,y)) return(0);
    case DCAB: if (!cuddSwapInPlace(table,w,x)) return(0);
    case DACB: if (!cuddSwapInPlace(table,y,z)) return(0);
    case BACD: if (!cuddSwapInPlace(table,x,y)) return(0);
    case CABD: if (!cuddSwapInPlace(table,w,x)) return(0);
817
               break;
Alan Mishchenko committed
818 819 820
    case DABC: if (!cuddSwapInPlace(table,y,z)) return(0);
    case BADC: if (!cuddSwapInPlace(table,x,y)) return(0);
    case CADB: if (!cuddSwapInPlace(table,w,x)) return(0);
821 822
               if (!cuddSwapInPlace(table,y,z)) return(0);
               break;
Alan Mishchenko committed
823 824 825 826
    case BDAC: if (!cuddSwapInPlace(table,x,y)) return(0);
    case CDAB: if (!cuddSwapInPlace(table,w,x)) return(0);
    case ADCB: if (!cuddSwapInPlace(table,y,z)) return(0);
    case ABCD: if (!cuddSwapInPlace(table,x,y)) return(0);
827
               break;
Alan Mishchenko committed
828 829
    case BCAD: if (!cuddSwapInPlace(table,x,y)) return(0);
    case CBAD: if (!cuddSwapInPlace(table,w,x)) return(0);
830 831
               if (!cuddSwapInPlace(table,x,y)) return(0);
               break;
Alan Mishchenko committed
832 833 834 835 836 837 838 839 840 841 842 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
    default: return(0);
    }

#ifdef DD_DEBUG
    assert(table->keys - table->isolated == (unsigned) size);
#endif

    return(best);

} /* end of ddPermuteWindow4 */


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

  Synopsis    [Reorders by applying a sliding window of width 4.]

  Description [Reorders by applying a sliding window of width 4.
  Tries all possible permutations to the variables in a
  window that slides from low to high.  Assumes that no dead nodes are
  present.  Returns 1 in case of success; 0 otherwise.]

  SideEffects [None]

******************************************************************************/
static int
ddWindow4(
  DdManager * table,
  int  low,
  int  high)
{

    int w;
    int res;

#ifdef DD_DEBUG
    assert(low >= 0 && high < table->size);
#endif

    if (high-low < 3) return(ddWindow3(table,low,high));

    for (w = low; w+2 < high; w++) {
873 874
        res = ddPermuteWindow4(table,w);
        if (res == 0) return(0);
Alan Mishchenko committed
875
#ifdef DD_STATS
876 877 878 879 880 881
        if (res == ABCD) {
            (void) fprintf(table->out,"=");
        } else {
            (void) fprintf(table->out,"-");
        }
        fflush(table->out);
Alan Mishchenko committed
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
#endif
    }

    return(1);

} /* end of ddWindow4 */


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

  Synopsis    [Reorders by repeatedly applying a sliding window of width 4.]

  Description [Reorders by repeatedly applying a sliding window of width
  4. Tries all possible permutations to the variables in a
  window that slides from low to high.  Assumes that no dead nodes are
  present.  Uses an event-driven approach to determine convergence.
  Returns 1 in case of success; 0 otherwise.]

  SideEffects [None]

******************************************************************************/
static int
ddWindowConv4(
  DdManager * table,
  int  low,
  int  high)
{
    int x;
    int res;
    int nwin;
    int newevent;
    int *events;

#ifdef DD_DEBUG
    assert(low >= 0 && high < table->size);
#endif

    if (high-low < 3) return(ddWindowConv3(table,low,high));

    nwin = high-low-2;
Alan Mishchenko committed
922
    events = ABC_ALLOC(int,nwin);
Alan Mishchenko committed
923
    if (events == NULL) {
924 925
        table->errorCode = CUDD_MEMORY_OUT;
        return(0);
Alan Mishchenko committed
926 927
    }
    for (x=0; x<nwin; x++) {
928
        events[x] = 1;
Alan Mishchenko committed
929 930 931
    }

    do {
932 933 934 935 936 937 938 939 940 941 942 943 944 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 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
        newevent = 0;
        for (x=0; x<nwin; x++) {
            if (events[x]) {
                res = ddPermuteWindow4(table,x+low);
                switch (res) {
                case ABCD:
                    break;
                case BACD:
                    if (x < nwin-1)     events[x+1] = 1;
                    if (x > 2)          events[x-3] = 1;
                    newevent = 1;
                    break;
                case BADC:
                    if (x < nwin-3)     events[x+3] = 1;
                    if (x < nwin-1)     events[x+1] = 1;
                    if (x > 0)          events[x-1] = 1;
                    if (x > 2)          events[x-3] = 1;
                    newevent = 1;
                    break;
                case ABDC:
                    if (x < nwin-3)     events[x+3] = 1;
                    if (x > 0)          events[x-1] = 1;
                    newevent = 1;
                    break;
                case ADBC:
                case ADCB:
                case ACDB:
                    if (x < nwin-3)     events[x+3] = 1;
                    if (x < nwin-2)     events[x+2] = 1;
                    if (x > 0)          events[x-1] = 1;
                    if (x > 1)          events[x-2] = 1;
                    newevent = 1;
                    break;
                case DACB:
                case DABC:
                case DBAC:
                case BDAC:
                case BDCA:
                case DBCA:
                case DCBA:
                case DCAB:
                case CDAB:
                case CDBA:
                case CBDA:
                case BCDA:
                case CADB:
                    if (x < nwin-3)     events[x+3] = 1;
                    if (x < nwin-2)     events[x+2] = 1;
                    if (x < nwin-1)     events[x+1] = 1;
                    if (x > 0)          events[x-1] = 1;
                    if (x > 1)          events[x-2] = 1;
                    if (x > 2)          events[x-3] = 1;
                    newevent = 1;
                    break;
                case BCAD:
                case CBAD:
                case CABD:
                    if (x < nwin-2)     events[x+2] = 1;
                    if (x < nwin-1)     events[x+1] = 1;
                    if (x > 1)          events[x-2] = 1;
                    if (x > 2)          events[x-3] = 1;
                    newevent = 1;
                    break;
                case ACBD:
                    if (x < nwin-2)     events[x+2] = 1;
                    if (x > 1)          events[x-2] = 1;
                    newevent = 1;
                    break;
                default:
                    ABC_FREE(events);
                    return(0);
                }
                events[x] = 0;
Alan Mishchenko committed
1005
#ifdef DD_STATS
1006 1007 1008 1009 1010 1011
                if (res == ABCD) {
                    (void) fprintf(table->out,"=");
                } else {
                    (void) fprintf(table->out,"-");
                }
                fflush(table->out);
Alan Mishchenko committed
1012
#endif
1013
            }
Alan Mishchenko committed
1014 1015
        }
#ifdef DD_STATS
1016 1017 1018 1019
        if (newevent) {
            (void) fprintf(table->out,"|");
            fflush(table->out);
        }
Alan Mishchenko committed
1020 1021 1022
#endif
    } while (newevent);

Alan Mishchenko committed
1023
    ABC_FREE(events);
Alan Mishchenko committed
1024 1025 1026 1027 1028

    return(1);

} /* end of ddWindowConv4 */

1029

1030 1031
ABC_NAMESPACE_IMPL_END