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

  FileName    [cuddLinear.c]

  PackageName [cudd]

  Synopsis    [Functions for DD reduction by linear transformations.]

  Description [ Internal procedures included in this module:
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
                <ul>
                <li> cuddLinearAndSifting()
                <li> cuddLinearInPlace()
                <li> cuddUpdateInteractionMatrix()
                <li> cuddInitLinear()
                <li> cuddResizeLinear()
                </ul>
        Static procedures included in this module:
                <ul>
                <li> ddLinearUniqueCompare()
                <li> ddLinearAndSiftingAux()
                <li> ddLinearAndSiftingUp()
                <li> ddLinearAndSiftingDown()
                <li> ddLinearAndSiftingBackward()
                <li> ddUndoMoves()
                <li> cuddXorLinear()
                </ul>]
Alan Mishchenko committed
27 28 29

  Author      [Fabio Somenzi]

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

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

64
#include "misc/util/util_hack.h"
Alan Mishchenko committed
65 66
#include "cuddInt.h"

67 68 69
ABC_NAMESPACE_IMPL_START


70

Alan Mishchenko committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
/*---------------------------------------------------------------------------*/
/* Constant declarations                                                     */
/*---------------------------------------------------------------------------*/

#define CUDD_SWAP_MOVE 0
#define CUDD_LINEAR_TRANSFORM_MOVE 1
#define CUDD_INVERSE_TRANSFORM_MOVE 2
#if SIZEOF_LONG == 8
#define BPL 64
#define LOGBPL 6
#else
#define BPL 32
#define LOGBPL 5
#endif

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

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

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

#ifndef lint
99
static char rcsid[] DD_UNUSED = "$Id: cuddLinear.c,v 1.28 2009/02/19 16:21:03 fabio Exp $";
Alan Mishchenko committed
100 101
#endif

102
static  int     *entry;
Alan Mishchenko committed
103 104

#ifdef DD_STATS
105 106 107
extern  int     ddTotalNumberSwapping;
extern  int     ddTotalNISwaps;
static  int     ddTotalNumberLinearTr;
Alan Mishchenko committed
108 109 110
#endif

#ifdef DD_DEBUG
111
static  int     zero = 0;
Alan Mishchenko committed
112 113 114 115 116 117 118 119 120 121 122 123
#endif

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

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

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

124 125 126 127 128 129 130
static int ddLinearUniqueCompare (int *ptrX, int *ptrY);
static int ddLinearAndSiftingAux (DdManager *table, int x, int xLow, int xHigh);
static Move * ddLinearAndSiftingUp (DdManager *table, int y, int xLow, Move *prevMoves);
static Move * ddLinearAndSiftingDown (DdManager *table, int x, int xHigh, Move *prevMoves);
static int ddLinearAndSiftingBackward (DdManager *table, int size, Move *moves);
static Move* ddUndoMoves (DdManager *table, Move *moves);
static void cuddXorLinear (DdManager *table, int x, int y);
Alan Mishchenko committed
131 132 133 134 135 136 137 138 139 140

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


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


/**Function********************************************************************
141

Alan Mishchenko committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
  Synopsis    [Prints the linear transform matrix.]

  Description [Prints the linear transform matrix. Returns 1 in case of
  success; 0 otherwise.]

  SideEffects [none]

  SeeAlso     []

******************************************************************************/
int
Cudd_PrintLinear(
  DdManager * table)
{
    int i,j,k;
    int retval;
    int nvars = table->linearSize;
    int wordsPerRow = ((nvars - 1) >> LOGBPL) + 1;
    long word;

    for (i = 0; i < nvars; i++) {
163 164 165 166 167 168 169
        for (j = 0; j < wordsPerRow; j++) {
            word = table->linear[i*wordsPerRow + j];
            for (k = 0; k < BPL; k++) {
                retval = fprintf(table->out,"%ld",word & 1);
                if (retval == 0) return(0);
                word >>= 1;
            }
Alan Mishchenko committed
170
        }
171 172
        retval = fprintf(table->out,"\n");
        if (retval == 0) return(0);
Alan Mishchenko committed
173 174 175 176 177 178 179
    }
    return(1);

} /* end of Cudd_PrintLinear */


/**Function********************************************************************
180

Alan Mishchenko committed
181 182 183 184 185 186 187 188 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 232 233 234 235 236 237 238 239 240 241 242 243 244
  Synopsis    [Reads an entry of the linear transform matrix.]

  Description [Reads an entry of the linear transform matrix.]

  SideEffects [none]

  SeeAlso     []

******************************************************************************/
int
Cudd_ReadLinear(
  DdManager * table /* CUDD manager */,
  int  x /* row index */,
  int  y /* column index */)
{
    int nvars = table->size;
    int wordsPerRow = ((nvars - 1) >> LOGBPL) + 1;
    long word;
    int bit;
    int result;

    assert(table->size == table->linearSize);

    word = wordsPerRow * x + (y >> LOGBPL);
    bit  = y & (BPL-1);
    result = (int) ((table->linear[word] >> bit) & 1);
    return(result);

} /* end of Cudd_ReadLinear */


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


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

  Synopsis    [BDD reduction based on combination of sifting and linear
  transformations.]

  Description [BDD reduction based on combination of sifting and linear
  transformations.  Assumes that no dead nodes are present.
    <ol>
    <li> Order all the variables according to the number of entries
    in each unique table.
    <li> Sift the variable up and down, remembering each time the
    total size of the DD heap. At each position, linear transformation
    of the two adjacent variables is tried and is accepted if it reduces
    the size of the DD.
    <li> Select the best permutation.
    <li> Repeat 3 and 4 for all variables.
    </ol>
  Returns 1 if successful; 0 otherwise.]

  SideEffects [None]

******************************************************************************/
int
cuddLinearAndSifting(
  DdManager * table,
  int  lower,
  int  upper)
{
245 246 247 248 249
    int         i;
    int         *var;
    int         size;
    int         x;
    int         result;
Alan Mishchenko committed
250
#ifdef DD_STATS
251
    int         previousSize;
Alan Mishchenko committed
252 253 254 255 256 257 258 259 260 261 262
#endif

#ifdef DD_STATS
    ddTotalNumberLinearTr = 0;
#endif

    size = table->size;

    var = NULL;
    entry = NULL;
    if (table->linear == NULL) {
263 264
        result = cuddInitLinear(table);
        if (result == 0) goto cuddLinearAndSiftingOutOfMem;
Alan Mishchenko committed
265
#if 0
266 267 268
        (void) fprintf(table->out,"\n");
        result = Cudd_PrintLinear(table);
        if (result == 0) goto cuddLinearAndSiftingOutOfMem;
Alan Mishchenko committed
269 270
#endif
    } else if (table->size != table->linearSize) {
271 272
        result = cuddResizeLinear(table);
        if (result == 0) goto cuddLinearAndSiftingOutOfMem;
Alan Mishchenko committed
273
#if 0
274 275 276
        (void) fprintf(table->out,"\n");
        result = Cudd_PrintLinear(table);
        if (result == 0) goto cuddLinearAndSiftingOutOfMem;
Alan Mishchenko committed
277 278 279 280
#endif
    }

    /* Find order in which to sift variables. */
Alan Mishchenko committed
281
    entry = ABC_ALLOC(int,size);
Alan Mishchenko committed
282
    if (entry == NULL) {
283 284
        table->errorCode = CUDD_MEMORY_OUT;
        goto cuddLinearAndSiftingOutOfMem;
Alan Mishchenko committed
285
    }
Alan Mishchenko committed
286
    var = ABC_ALLOC(int,size);
Alan Mishchenko committed
287
    if (var == NULL) {
288 289
        table->errorCode = CUDD_MEMORY_OUT;
        goto cuddLinearAndSiftingOutOfMem;
Alan Mishchenko committed
290 291 292
    }

    for (i = 0; i < size; i++) {
293 294 295
        x = table->perm[i];
        entry[i] = table->subtables[x].keys;
        var[i] = i;
Alan Mishchenko committed
296 297
    }

298
    qsort((void *)var,(size_t)size,sizeof(int),(DD_QSFP)ddLinearUniqueCompare);
Alan Mishchenko committed
299 300 301

    /* Now sift. */
    for (i = 0; i < ddMin(table->siftMaxVar,size); i++) {
302 303
        x = table->perm[var[i]];
        if (x < lower || x > upper) continue;
Alan Mishchenko committed
304
#ifdef DD_STATS
305
        previousSize = table->keys - table->isolated;
Alan Mishchenko committed
306
#endif
307 308
        result = ddLinearAndSiftingAux(table,x,lower,upper);
        if (!result) goto cuddLinearAndSiftingOutOfMem;
Alan Mishchenko committed
309
#ifdef DD_STATS
310 311 312 313 314 315 316 317 318
        if (table->keys < (unsigned) previousSize + table->isolated) {
            (void) fprintf(table->out,"-");
        } else if (table->keys > (unsigned) previousSize + table->isolated) {
            (void) fprintf(table->out,"+");     /* should never happen */
            (void) fprintf(table->out,"\nSize increased from %d to %d while sifting variable %d\n", previousSize, table->keys - table->isolated, var[i]);
        } else {
            (void) fprintf(table->out,"=");
        }
        fflush(table->out);
Alan Mishchenko committed
319 320
#endif
#ifdef DD_DEBUG
321
        (void) Cudd_DebugCheck(table);
Alan Mishchenko committed
322 323 324
#endif
    }

Alan Mishchenko committed
325 326
    ABC_FREE(var);
    ABC_FREE(entry);
Alan Mishchenko committed
327 328 329

#ifdef DD_STATS
    (void) fprintf(table->out,"\n#:L_LINSIFT %8d: linear trans.",
330
                   ddTotalNumberLinearTr);
Alan Mishchenko committed
331 332 333 334 335 336
#endif

    return(1);

cuddLinearAndSiftingOutOfMem:

Alan Mishchenko committed
337 338
    if (entry != NULL) ABC_FREE(entry);
    if (var != NULL) ABC_FREE(var);
Alan Mishchenko committed
339

340
    return(0);
Alan Mishchenko committed
341 342 343 344

} /* end of cuddLinearAndSifting */


345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
/**Function********************************************************************

  Synopsis    [Linearly combines two adjacent variables.]

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

  SideEffects [The two subtables corrresponding to variables x and y are
  modified. The global counters of the unique table are also affected.]

  SeeAlso     [cuddSwapInPlace]

******************************************************************************/
int
cuddLinearInPlace(
  DdManager * table,
  int  x,
  int  y)
{
    DdNodePtr *xlist, *ylist;
    int    xindex, yindex;
    int    xslots, yslots;
    int    xshift, yshift;
    int    oldxkeys, oldykeys;
    int    newxkeys, newykeys;
    int    comple, newcomplement;
    int    i;
    int    posn;
    int    isolated;
    DdNode *f,*f0,*f1,*f01,*f00,*f11,*f10,*newf1,*newf0;
380
    DdNode *g,*next,*last=NULL;
381 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 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 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 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
    DdNodePtr *previousP;
    DdNode *tmp;
    DdNode *sentinel = &(table->sentinel);
#ifdef DD_DEBUG
    int    count, idcheck;
#endif

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

    xindex = table->invperm[x];
    yindex = table->invperm[y];

    if (cuddTestInteract(table,xindex,yindex)) {
#ifdef DD_STATS
        ddTotalNumberLinearTr++;
#endif
        /* Get parameters of x subtable. */
        xlist = table->subtables[x].nodelist;
        oldxkeys = table->subtables[x].keys;
        xslots = table->subtables[x].slots;
        xshift = table->subtables[x].shift;

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

        newxkeys = 0;
        newykeys = oldykeys;

        /* Check whether the two projection functions involved in this
        ** swap are isolated. At the end, we'll be able to tell how many
        ** isolated projection functions are there by checking only these
        ** two functions again. This is done to eliminate the isolated
        ** projection functions from the node count.
        */
        isolated = - ((table->vars[xindex]->ref == 1) +
                     (table->vars[yindex]->ref == 1));

        /* The nodes in the x layer are put in a chain.
        ** The chain is handled as a FIFO; g points to the beginning and
        ** last points to the end.
        */
        g = NULL;
#ifdef DD_DEBUG
        last = NULL;
#endif
        for (i = 0; i < xslots; i++) {
            f = xlist[i];
            if (f == sentinel) continue;
            xlist[i] = sentinel;
            if (g == NULL) {
                g = f;
            } else {
                last->next = f;
            }
            while ((next = f->next) != sentinel) {
                f = next;
            } /* while there are elements in the collision chain */
            last = f;
        } /* for each slot of the x subtable */
#ifdef DD_DEBUG
        /* last is always assigned in the for loop because there is at
        ** least one key */
        assert(last != NULL);
#endif
        last->next = NULL;

#ifdef DD_COUNT
        table->swapSteps += oldxkeys;
#endif
        /* Take care of the x nodes that must be re-expressed.
        ** They form a linked list pointed by g.
        */
        f = g;
        while (f != NULL) {
            next = f->next;
            /* Find f1, f0, f11, f10, f01, f00. */
            f1 = cuddT(f);
#ifdef DD_DEBUG
            assert(!(Cudd_IsComplement(f1)));
#endif
            if ((int) f1->index == yindex) {
                f11 = cuddT(f1); f10 = cuddE(f1);
            } else {
                f11 = f10 = f1;
            }
#ifdef DD_DEBUG
            assert(!(Cudd_IsComplement(f11)));
#endif
            f0 = cuddE(f);
            comple = Cudd_IsComplement(f0);
            f0 = Cudd_Regular(f0);
            if ((int) f0->index == yindex) {
                f01 = cuddT(f0); f00 = cuddE(f0);
            } else {
                f01 = f00 = f0;
            }
            if (comple) {
                f01 = Cudd_Not(f01);
                f00 = Cudd_Not(f00);
            }
            /* Decrease ref count of f1. */
            cuddSatDec(f1->ref);
            /* Create the new T child. */
            if (f11 == f00) {
                newf1 = f11;
                cuddSatInc(newf1->ref);
            } else {
                /* Check ylist for triple (yindex,f11,f00). */
499
                posn = ddHash(cuddF2L(f11), cuddF2L(f00), yshift);
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 547 548 549 550 551 552
                /* For each element newf1 in collision list ylist[posn]. */
                previousP = &(ylist[posn]);
                newf1 = *previousP;
                while (f11 < cuddT(newf1)) {
                    previousP = &(newf1->next);
                    newf1 = *previousP;
                }
                while (f11 == cuddT(newf1) && f00 < cuddE(newf1)) {
                    previousP = &(newf1->next);
                    newf1 = *previousP;
                }
                if (cuddT(newf1) == f11 && cuddE(newf1) == f00) {
                    cuddSatInc(newf1->ref);
                } else { /* no match */
                    newf1 = cuddDynamicAllocNode(table);
                    if (newf1 == NULL)
                        goto cuddLinearOutOfMem;
                    newf1->index = yindex; newf1->ref = 1;
                    cuddT(newf1) = f11;
                    cuddE(newf1) = f00;
                    /* Insert newf1 in the collision list ylist[posn];
                    ** increase the ref counts of f11 and f00.
                    */
                    newykeys++;
                    newf1->next = *previousP;
                    *previousP = newf1;
                    cuddSatInc(f11->ref);
                    tmp = Cudd_Regular(f00);
                    cuddSatInc(tmp->ref);
                }
            }
            cuddT(f) = newf1;
#ifdef DD_DEBUG
            assert(!(Cudd_IsComplement(newf1)));
#endif

            /* Do the same for f0, keeping complement dots into account. */
            /* decrease ref count of f0 */
            tmp = Cudd_Regular(f0);
            cuddSatDec(tmp->ref);
            /* create the new E child */
            if (f01 == f10) {
                newf0 = f01;
                tmp = Cudd_Regular(newf0);
                cuddSatInc(tmp->ref);
            } else {
                /* make sure f01 is regular */
                newcomplement = Cudd_IsComplement(f01);
                if (newcomplement) {
                    f01 = Cudd_Not(f01);
                    f10 = Cudd_Not(f10);
                }
                /* Check ylist for triple (yindex,f01,f10). */
553
                posn = ddHash(cuddF2L(f01), cuddF2L(f10), yshift);
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
                /* For each element newf0 in collision list ylist[posn]. */
                previousP = &(ylist[posn]);
                newf0 = *previousP;
                while (f01 < cuddT(newf0)) {
                    previousP = &(newf0->next);
                    newf0 = *previousP;
                }
                while (f01 == cuddT(newf0) && f10 < cuddE(newf0)) {
                    previousP = &(newf0->next);
                    newf0 = *previousP;
                }
                if (cuddT(newf0) == f01 && cuddE(newf0) == f10) {
                    cuddSatInc(newf0->ref);
                } else { /* no match */
                    newf0 = cuddDynamicAllocNode(table);
                    if (newf0 == NULL)
                        goto cuddLinearOutOfMem;
                    newf0->index = yindex; newf0->ref = 1;
                    cuddT(newf0) = f01;
                    cuddE(newf0) = f10;
                    /* Insert newf0 in the collision list ylist[posn];
                    ** increase the ref counts of f01 and f10.
                    */
                    newykeys++;
                    newf0->next = *previousP;
                    *previousP = newf0;
                    cuddSatInc(f01->ref);
                    tmp = Cudd_Regular(f10);
                    cuddSatInc(tmp->ref);
                }
                if (newcomplement) {
                    newf0 = Cudd_Not(newf0);
                }
            }
            cuddE(f) = newf0;

            /* Re-insert the modified f in xlist.
            ** The modified f does not already exists in xlist.
            ** (Because of the uniqueness of the cofactors.)
            */
594
            posn = ddHash(cuddF2L(newf1), cuddF2L(newf0), xshift);
595 596 597 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 633 634 635 636 637 638 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 670 671 672 673 674 675 676 677 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 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 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 779 780 781 782 783 784 785 786 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 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
            newxkeys++;
            previousP = &(xlist[posn]);
            tmp = *previousP;
            while (newf1 < cuddT(tmp)) {
                previousP = &(tmp->next);
                tmp = *previousP;
            }
            while (newf1 == cuddT(tmp) && newf0 < cuddE(tmp)) {
                previousP = &(tmp->next);
                tmp = *previousP;
            }
            f->next = *previousP;
            *previousP = f;
            f = next;
        } /* while f != NULL */

        /* GC the y layer. */

        /* For each node f in ylist. */
        for (i = 0; i < yslots; i++) {
            previousP = &(ylist[i]);
            f = *previousP;
            while (f != sentinel) {
                next = f->next;
                if (f->ref == 0) {
                    tmp = cuddT(f);
                    cuddSatDec(tmp->ref);
                    tmp = Cudd_Regular(cuddE(f));
                    cuddSatDec(tmp->ref);
                    cuddDeallocNode(table,f);
                    newykeys--;
                } else {
                    *previousP = f;
                    previousP = &(f->next);
                }
                f = next;
            } /* while f */
            *previousP = sentinel;
        } /* for every collision list */

#ifdef DD_DEBUG
#if 0
        (void) fprintf(table->out,"Linearly combining %d and %d\n",x,y);
#endif
        count = 0;
        idcheck = 0;
        for (i = 0; i < yslots; i++) {
            f = ylist[i];
            while (f != sentinel) {
                count++;
                if (f->index != (DdHalfWord) yindex)
                    idcheck++;
                f = f->next;
            }
        }
        if (count != newykeys) {
            fprintf(table->err,"Error in finding newykeys\toldykeys = %d\tnewykeys = %d\tactual = %d\n",oldykeys,newykeys,count);
        }
        if (idcheck != 0)
            fprintf(table->err,"Error in id's of ylist\twrong id's = %d\n",idcheck);
        count = 0;
        idcheck = 0;
        for (i = 0; i < xslots; i++) {
            f = xlist[i];
            while (f != sentinel) {
                count++;
                if (f->index != (DdHalfWord) xindex)
                    idcheck++;
                f = f->next;
            }
        }
        if (count != newxkeys || newxkeys != oldxkeys) {
            fprintf(table->err,"Error in finding newxkeys\toldxkeys = %d \tnewxkeys = %d \tactual = %d\n",oldxkeys,newxkeys,count);
        }
        if (idcheck != 0)
            fprintf(table->err,"Error in id's of xlist\twrong id's = %d\n",idcheck);
#endif

        isolated += (table->vars[xindex]->ref == 1) +
                    (table->vars[yindex]->ref == 1);
        table->isolated += isolated;

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

        /* Here we should update the linear combination table
        ** to record that x <- x EXNOR y. This is done by complementing
        ** the (x,y) entry of the table.
        */

        table->keys += newykeys - oldykeys;

        cuddXorLinear(table,xindex,yindex);
    }

#ifdef DD_DEBUG
    if (zero) {
        (void) Cudd_DebugCheck(table);
    }
#endif

    return(table->keys - table->isolated);

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

    return (0);

} /* end of cuddLinearInPlace */


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

  Synopsis    [Updates the interaction matrix.]

  Description []

  SideEffects [none]

  SeeAlso     []

******************************************************************************/
void
cuddUpdateInteractionMatrix(
  DdManager * table,
  int  xindex,
  int  yindex)
{
    int i;
    for (i = 0; i < yindex; i++) {
        if (i != xindex && cuddTestInteract(table,i,yindex)) {
            if (i < xindex) {
                cuddSetInteract(table,i,xindex);
            } else {
                cuddSetInteract(table,xindex,i);
            }
        }
    }
    for (i = yindex+1; i < table->size; i++) {
        if (i != xindex && cuddTestInteract(table,yindex,i)) {
            if (i < xindex) {
                cuddSetInteract(table,i,xindex);
            } else {
                cuddSetInteract(table,xindex,i);
            }
        }
    }

} /* end of cuddUpdateInteractionMatrix */


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

  Synopsis    [Initializes the linear transform matrix.]

  Description [Initializes the linear transform matrix.  Returns 1 if
  successful; 0 otherwise.]

  SideEffects [none]

  SeeAlso     []

******************************************************************************/
int
cuddInitLinear(
  DdManager * table)
{
    int words;
    int wordsPerRow;
    int nvars;
    int word;
    int bit;
    int i;
    long *linear;

    nvars = table->size;
    wordsPerRow = ((nvars - 1) >> LOGBPL) + 1;
    words = wordsPerRow * nvars;
    table->linear = linear = ABC_ALLOC(long,words);
    if (linear == NULL) {
        table->errorCode = CUDD_MEMORY_OUT;
        return(0);
    }
    table->memused += words * sizeof(long);
    table->linearSize = nvars;
    for (i = 0; i < words; i++) linear[i] = 0;
    for (i = 0; i < nvars; i++) {
        word = wordsPerRow * i + (i >> LOGBPL);
        bit  = i & (BPL-1);
        linear[word] = 1 << bit;
    }
    return(1);

} /* end of cuddInitLinear */


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

  Synopsis    [Resizes the linear transform matrix.]

  Description [Resizes the linear transform matrix.  Returns 1 if
  successful; 0 otherwise.]

  SideEffects [none]

  SeeAlso     []

******************************************************************************/
int
cuddResizeLinear(
  DdManager * table)
{
    int words,oldWords;
    int wordsPerRow,oldWordsPerRow;
    int nvars,oldNvars;
    int word,oldWord;
    int bit;
    int i,j;
    long *linear,*oldLinear;

    oldNvars = table->linearSize;
    oldWordsPerRow = ((oldNvars - 1) >> LOGBPL) + 1;
    oldWords = oldWordsPerRow * oldNvars;
    oldLinear = table->linear;

    nvars = table->size;
    wordsPerRow = ((nvars - 1) >> LOGBPL) + 1;
    words = wordsPerRow * nvars;
    table->linear = linear = ABC_ALLOC(long,words);
    if (linear == NULL) {
        table->errorCode = CUDD_MEMORY_OUT;
        return(0);
    }
    table->memused += (words - oldWords) * sizeof(long);
    for (i = 0; i < words; i++) linear[i] = 0;

    /* Copy old matrix. */
    for (i = 0; i < oldNvars; i++) {
        for (j = 0; j < oldWordsPerRow; j++) {
            oldWord = oldWordsPerRow * i + j;
            word = wordsPerRow * i + j;
            linear[word] = oldLinear[oldWord];
        }
    }
    ABC_FREE(oldLinear);

    /* Add elements to the diagonal. */
    for (i = oldNvars; i < nvars; i++) {
        word = wordsPerRow * i + (i >> LOGBPL);
        bit  = i & (BPL-1);
        linear[word] = 1 << bit;
    }
    table->linearSize = nvars;

    return(1);

} /* end of cuddResizeLinear */


Alan Mishchenko committed
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
/*---------------------------------------------------------------------------*/
/* Definition of static functions                                            */
/*---------------------------------------------------------------------------*/


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

  Synopsis    [Comparison function used by qsort.]

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

  SideEffects [None]

******************************************************************************/
static int
ddLinearUniqueCompare(
  int * ptrX,
  int * ptrY)
{
#if 0
    if (entry[*ptrY] == entry[*ptrX]) {
878
        return((*ptrX) - (*ptrY));
Alan Mishchenko committed
879 880 881 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
    }
#endif
    return(entry[*ptrY] - entry[*ptrX]);

} /* end of ddLinearUniqueCompare */


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

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

  Description [Given xLow <= x <= xHigh moves x up and down between the
  boundaries. At each step a linear transformation is tried, and, if it
  decreases the size of the DD, it is accepted. Finds the best position
  and does the required changes.  Returns 1 if successful; 0 otherwise.]

  SideEffects [None]

******************************************************************************/
static int
ddLinearAndSiftingAux(
  DdManager * table,
  int  x,
  int  xLow,
  int  xHigh)
{

907 908 909 910 911
    Move        *move;
    Move        *moveUp;                /* list of up moves */
    Move        *moveDown;              /* list of down moves */
    int         initialSize;
    int         result;
Alan Mishchenko committed
912 913 914 915 916 917 918

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

    moveDown = NULL;
    moveUp = NULL;

    if (x == xLow) {
919 920 921 922 923 924
        moveDown = ddLinearAndSiftingDown(table,x,xHigh,NULL);
        /* At this point x --> xHigh unless bounding occurred. */
        if (moveDown == (Move *) CUDD_OUT_OF_MEM) goto ddLinearAndSiftingAuxOutOfMem;
        /* Move backward and stop at best position. */
        result = ddLinearAndSiftingBackward(table,initialSize,moveDown);
        if (!result) goto ddLinearAndSiftingAuxOutOfMem;
Alan Mishchenko committed
925 926

    } else if (x == xHigh) {
927 928 929 930 931 932
        moveUp = ddLinearAndSiftingUp(table,x,xLow,NULL);
        /* At this point x --> xLow unless bounding occurred. */
        if (moveUp == (Move *) CUDD_OUT_OF_MEM) goto ddLinearAndSiftingAuxOutOfMem;
        /* Move backward and stop at best position. */
        result = ddLinearAndSiftingBackward(table,initialSize,moveUp);
        if (!result) goto ddLinearAndSiftingAuxOutOfMem;
Alan Mishchenko committed
933 934

    } else if ((x - xLow) > (xHigh - x)) { /* must go down first: shorter */
935 936 937 938
        moveDown = ddLinearAndSiftingDown(table,x,xHigh,NULL);
        /* At this point x --> xHigh unless bounding occurred. */
        if (moveDown == (Move *) CUDD_OUT_OF_MEM) goto ddLinearAndSiftingAuxOutOfMem;
        moveUp = ddUndoMoves(table,moveDown);
Alan Mishchenko committed
939
#ifdef DD_DEBUG
940
        assert(moveUp == NULL || moveUp->x == x);
Alan Mishchenko committed
941
#endif
942 943 944 945 946
        moveUp = ddLinearAndSiftingUp(table,x,xLow,moveUp);
        if (moveUp == (Move *) CUDD_OUT_OF_MEM) goto ddLinearAndSiftingAuxOutOfMem;
        /* Move backward and stop at best position. */
        result = ddLinearAndSiftingBackward(table,initialSize,moveUp);
        if (!result) goto ddLinearAndSiftingAuxOutOfMem;
Alan Mishchenko committed
947 948

    } else { /* must go up first: shorter */
949 950 951 952
        moveUp = ddLinearAndSiftingUp(table,x,xLow,NULL);
        /* At this point x --> xLow unless bounding occurred. */
        if (moveUp == (Move *) CUDD_OUT_OF_MEM) goto ddLinearAndSiftingAuxOutOfMem;
        moveDown = ddUndoMoves(table,moveUp);
Alan Mishchenko committed
953
#ifdef DD_DEBUG
954
        assert(moveDown == NULL || moveDown->y == x);
Alan Mishchenko committed
955
#endif
956 957 958 959 960
        moveDown = ddLinearAndSiftingDown(table,x,xHigh,moveDown);
        if (moveDown == (Move *) CUDD_OUT_OF_MEM) goto ddLinearAndSiftingAuxOutOfMem;
        /* Move backward and stop at best position. */
        result = ddLinearAndSiftingBackward(table,initialSize,moveDown);
        if (!result) goto ddLinearAndSiftingAuxOutOfMem;
Alan Mishchenko committed
961 962 963
    }

    while (moveDown != NULL) {
964 965 966
        move = moveDown->next;
        cuddDeallocMove(table, moveDown);
        moveDown = move;
Alan Mishchenko committed
967 968
    }
    while (moveUp != NULL) {
969 970 971
        move = moveUp->next;
        cuddDeallocMove(table, moveUp);
        moveUp = move;
Alan Mishchenko committed
972 973 974 975 976 977
    }

    return(1);

ddLinearAndSiftingAuxOutOfMem:
    while (moveDown != NULL) {
978 979 980
        move = moveDown->next;
        cuddDeallocMove(table, moveDown);
        moveDown = move;
Alan Mishchenko committed
981 982
    }
    while (moveUp != NULL) {
983 984 985
        move = moveUp->next;
        cuddDeallocMove(table, moveUp);
        moveUp = move;
Alan Mishchenko committed
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
    }

    return(0);

} /* end of ddLinearAndSiftingAux */


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

  Synopsis    [Sifts a variable up and applies linear transformations.]

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

  SideEffects [None]

******************************************************************************/
static Move *
ddLinearAndSiftingUp(
  DdManager * table,
  int  y,
  int  xLow,
  Move * prevMoves)
{
1012 1013 1014 1015 1016 1017 1018 1019
    Move        *moves;
    Move        *move;
    int         x;
    int         size, newsize;
    int         limitSize;
    int         xindex, yindex;
    int         isolated;
    int         L;      /* lower bound on DD size */
Alan Mishchenko committed
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
#ifdef DD_DEBUG
    int checkL;
    int z;
    int zindex;
#endif

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

    /* Initialize the lower bound.
    ** The part of the DD below y will not change.
    ** The part of the DD above y that does not interact with y will not
    ** change. The rest may vanish in the best case, except for
    ** the nodes at level xLow, which will not vanish, regardless.
    */
    limitSize = L = table->keys - table->isolated;
    for (x = xLow + 1; x < y; x++) {
1037 1038 1039 1040 1041
        xindex = table->invperm[x];
        if (cuddTestInteract(table,xindex,yindex)) {
            isolated = table->vars[xindex]->ref == 1;
            L -= table->subtables[x].keys - isolated;
        }
Alan Mishchenko committed
1042 1043 1044 1045 1046 1047
    }
    isolated = table->vars[yindex]->ref == 1;
    L -= table->subtables[y].keys - isolated;

    x = cuddNextLow(table,y);
    while (x >= xLow && L <= limitSize) {
1048
        xindex = table->invperm[x];
Alan Mishchenko committed
1049
#ifdef DD_DEBUG
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
        checkL = table->keys - table->isolated;
        for (z = xLow + 1; z < y; z++) {
            zindex = table->invperm[z];
            if (cuddTestInteract(table,zindex,yindex)) {
                isolated = table->vars[zindex]->ref == 1;
                checkL -= table->subtables[z].keys - isolated;
            }
        }
        isolated = table->vars[yindex]->ref == 1;
        checkL -= table->subtables[y].keys - isolated;
        if (L != checkL) {
            (void) fprintf(table->out, "checkL(%d) != L(%d)\n",checkL,L);
Alan Mishchenko committed
1062 1063
        }
#endif
1064 1065
        size = cuddSwapInPlace(table,x,y);
        if (size == 0) goto ddLinearAndSiftingUpOutOfMem;
Alan Mishchenko committed
1066 1067
        newsize = cuddLinearInPlace(table,x,y);
        if (newsize == 0) goto ddLinearAndSiftingUpOutOfMem;
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
        move = (Move *) cuddDynamicAllocNode(table);
        if (move == NULL) goto ddLinearAndSiftingUpOutOfMem;
        move->x = x;
        move->y = y;
        move->next = moves;
        moves = move;
        move->flags = CUDD_SWAP_MOVE;
        if (newsize >= size) {
            /* Undo transformation. The transformation we apply is
            ** its own inverse. Hence, we just apply the transformation
            ** again.
            */
            newsize = cuddLinearInPlace(table,x,y);
            if (newsize == 0) goto ddLinearAndSiftingUpOutOfMem;
Alan Mishchenko committed
1082
#ifdef DD_DEBUG
1083 1084 1085
            if (newsize != size) {
                (void) fprintf(table->out,"Change in size after identity transformation! From %d to %d\n",size,newsize);
            }
Alan Mishchenko committed
1086
#endif
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
        } else if (cuddTestInteract(table,xindex,yindex)) {
            size = newsize;
            move->flags = CUDD_LINEAR_TRANSFORM_MOVE;
            cuddUpdateInteractionMatrix(table,xindex,yindex);
        }
        move->size = size;
        /* Update the lower bound. */
        if (cuddTestInteract(table,xindex,yindex)) {
            isolated = table->vars[xindex]->ref == 1;
            L += table->subtables[y].keys - isolated;
        }
        if ((double) size > (double) limitSize * table->maxGrowth) break;
        if (size < limitSize) limitSize = size;
        y = x;
        x = cuddNextLow(table,y);
Alan Mishchenko committed
1102 1103 1104 1105 1106
    }
    return(moves);

ddLinearAndSiftingUpOutOfMem:
    while (moves != NULL) {
1107 1108 1109
        move = moves->next;
        cuddDeallocMove(table, moves);
        moves = move;
Alan Mishchenko committed
1110 1111 1112 1113
    }
    return((Move *) CUDD_OUT_OF_MEM);

} /* end of ddLinearAndSiftingUp */
1114

Alan Mishchenko committed
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134

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

  Synopsis    [Sifts a variable down and applies linear transformations.]

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

  SideEffects [None]

******************************************************************************/
static Move *
ddLinearAndSiftingDown(
  DdManager * table,
  int  x,
  int  xHigh,
  Move * prevMoves)
{
1135 1136 1137 1138 1139 1140 1141 1142
    Move        *moves;
    Move        *move;
    int         y;
    int         size, newsize;
    int         R;      /* upper bound on node decrease */
    int         limitSize;
    int         xindex, yindex;
    int         isolated;
Alan Mishchenko committed
1143
#ifdef DD_DEBUG
1144 1145 1146
    int         checkR;
    int         z;
    int         zindex;
Alan Mishchenko committed
1147 1148 1149 1150 1151 1152 1153 1154
#endif

    moves = prevMoves;
    /* Initialize R */
    xindex = table->invperm[x];
    limitSize = size = table->keys - table->isolated;
    R = 0;
    for (y = xHigh; y > x; y--) {
1155 1156 1157 1158 1159
        yindex = table->invperm[y];
        if (cuddTestInteract(table,xindex,yindex)) {
            isolated = table->vars[yindex]->ref == 1;
            R += table->subtables[y].keys - isolated;
        }
Alan Mishchenko committed
1160 1161 1162 1163 1164
    }

    y = cuddNextHigh(table,x);
    while (y <= xHigh && size - R < limitSize) {
#ifdef DD_DEBUG
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
        checkR = 0;
        for (z = xHigh; z > x; z--) {
            zindex = table->invperm[z];
            if (cuddTestInteract(table,xindex,zindex)) {
                isolated = table->vars[zindex]->ref == 1;
                checkR += table->subtables[z].keys - isolated;
            }
        }
        if (R != checkR) {
            (void) fprintf(table->out, "checkR(%d) != R(%d)\n",checkR,R);
Alan Mishchenko committed
1175 1176
        }
#endif
1177 1178 1179 1180 1181 1182 1183 1184
        /* Update upper bound on node decrease. */
        yindex = table->invperm[y];
        if (cuddTestInteract(table,xindex,yindex)) {
            isolated = table->vars[yindex]->ref == 1;
            R -= table->subtables[y].keys - isolated;
        }
        size = cuddSwapInPlace(table,x,y);
        if (size == 0) goto ddLinearAndSiftingDownOutOfMem;
Alan Mishchenko committed
1185 1186
        newsize = cuddLinearInPlace(table,x,y);
        if (newsize == 0) goto ddLinearAndSiftingDownOutOfMem;
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
        move = (Move *) cuddDynamicAllocNode(table);
        if (move == NULL) goto ddLinearAndSiftingDownOutOfMem;
        move->x = x;
        move->y = y;
        move->next = moves;
        moves = move;
        move->flags = CUDD_SWAP_MOVE;
        if (newsize >= size) {
            /* Undo transformation. The transformation we apply is
            ** its own inverse. Hence, we just apply the transformation
            ** again.
            */
            newsize = cuddLinearInPlace(table,x,y);
            if (newsize == 0) goto ddLinearAndSiftingDownOutOfMem;
            if (newsize != size) {
                (void) fprintf(table->out,"Change in size after identity transformation! From %d to %d\n",size,newsize);
            }
        } else if (cuddTestInteract(table,xindex,yindex)) {
            size = newsize;
            move->flags = CUDD_LINEAR_TRANSFORM_MOVE;
            cuddUpdateInteractionMatrix(table,xindex,yindex);
Alan Mishchenko committed
1208
        }
1209 1210 1211 1212 1213
        move->size = size;
        if ((double) size > (double) limitSize * table->maxGrowth) break;
        if (size < limitSize) limitSize = size;
        x = y;
        y = cuddNextHigh(table,x);
Alan Mishchenko committed
1214 1215 1216 1217 1218
    }
    return(moves);

ddLinearAndSiftingDownOutOfMem:
    while (moves != NULL) {
1219 1220 1221
        move = moves->next;
        cuddDeallocMove(table, moves);
        moves = move;
Alan Mishchenko committed
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
    }
    return((Move *) CUDD_OUT_OF_MEM);

} /* end of ddLinearAndSiftingDown */


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

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

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

  SideEffects [None]

******************************************************************************/
static int
ddLinearAndSiftingBackward(
  DdManager * table,
  int  size,
  Move * moves)
{
    Move *move;
1248
    int res;
Alan Mishchenko committed
1249 1250

    for (move = moves; move != NULL; move = move->next) {
1251 1252 1253
        if (move->size < size) {
            size = move->size;
        }
Alan Mishchenko committed
1254 1255 1256
    }

    for (move = moves; move != NULL; move = move->next) {
1257 1258 1259 1260 1261 1262
        if (move->size == size) return(1);
        if (move->flags == CUDD_LINEAR_TRANSFORM_MOVE) {
            res = cuddLinearInPlace(table,(int)move->x,(int)move->y);
            if (!res) return(0);
        }
        res = cuddSwapInPlace(table,(int)move->x,(int)move->y);
Alan Mishchenko committed
1263
        if (!res) return(0);
1264 1265 1266 1267
        if (move->flags == CUDD_INVERSE_TRANSFORM_MOVE) {
            res = cuddLinearInPlace(table,(int)move->x,(int)move->y);
            if (!res) return(0);
        }
Alan Mishchenko committed
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
    }

    return(1);

} /* end of ddLinearAndSiftingBackward */


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

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

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

  SideEffects [None]

******************************************************************************/
static Move*
ddUndoMoves(
  DdManager * table,
  Move * moves)
{
    Move *invmoves = NULL;
    Move *move;
    Move *invmove;
1295
    int size;
Alan Mishchenko committed
1296 1297

    for (move = moves; move != NULL; move = move->next) {
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
        invmove = (Move *) cuddDynamicAllocNode(table);
        if (invmove == NULL) goto ddUndoMovesOutOfMem;
        invmove->x = move->x;
        invmove->y = move->y;
        invmove->next = invmoves;
        invmoves = invmove;
        if (move->flags == CUDD_SWAP_MOVE) {
            invmove->flags = CUDD_SWAP_MOVE;
            size = cuddSwapInPlace(table,(int)move->x,(int)move->y);
            if (!size) goto ddUndoMovesOutOfMem;
        } else if (move->flags == CUDD_LINEAR_TRANSFORM_MOVE) {
            invmove->flags = CUDD_INVERSE_TRANSFORM_MOVE;
            size = cuddLinearInPlace(table,(int)move->x,(int)move->y);
            if (!size) goto ddUndoMovesOutOfMem;
            size = cuddSwapInPlace(table,(int)move->x,(int)move->y);
            if (!size) goto ddUndoMovesOutOfMem;
        } else { /* must be CUDD_INVERSE_TRANSFORM_MOVE */
Alan Mishchenko committed
1315
#ifdef DD_DEBUG
1316
            (void) fprintf(table->err,"Unforseen event in ddUndoMoves!\n");
Alan Mishchenko committed
1317
#endif
1318 1319 1320 1321 1322 1323 1324
            invmove->flags = CUDD_LINEAR_TRANSFORM_MOVE;
            size = cuddSwapInPlace(table,(int)move->x,(int)move->y);
            if (!size) goto ddUndoMovesOutOfMem;
            size = cuddLinearInPlace(table,(int)move->x,(int)move->y);
            if (!size) goto ddUndoMovesOutOfMem;
        }
        invmove->size = size;
Alan Mishchenko committed
1325 1326 1327 1328 1329 1330
    }

    return(invmoves);

ddUndoMovesOutOfMem:
    while (invmoves != NULL) {
1331 1332 1333
        move = invmoves->next;
        cuddDeallocMove(table, invmoves);
        invmoves = move;
Alan Mishchenko committed
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
    }
    return((Move *) CUDD_OUT_OF_MEM);

} /* end of ddUndoMoves */


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

  Synopsis    [XORs two rows of the linear transform matrix.]

  Description [XORs two rows of the linear transform matrix and replaces
  the first row with the result.]

  SideEffects [none]

  SeeAlso     []

******************************************************************************/
static void
cuddXorLinear(
  DdManager * table,
  int  x,
  int  y)
{
    int i;
    int nvars = table->size;
    int wordsPerRow = ((nvars - 1) >> LOGBPL) + 1;
    int xstart = wordsPerRow * x;
    int ystart = wordsPerRow * y;
    long *linear = table->linear;

    for (i = 0; i < wordsPerRow; i++) {
1366
        linear[xstart+i] ^= linear[ystart+i];
Alan Mishchenko committed
1367 1368 1369 1370
    }

} /* end of cuddXorLinear */

1371

1372 1373
ABC_NAMESPACE_IMPL_END