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

  FileName    [cuddBddAbs.c]

  PackageName [cudd]

  Synopsis    [Quantification functions for BDDs.]

  Description [External procedures included in this module:
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
                <ul>
                <li> Cudd_bddExistAbstract()
                <li> Cudd_bddXorExistAbstract()
                <li> Cudd_bddUnivAbstract()
                <li> Cudd_bddBooleanDiff()
                <li> Cudd_bddVarIsDependent()
                </ul>
        Internal procedures included in this module:
                <ul>
                <li> cuddBddExistAbstractRecur()
                <li> cuddBddXorExistAbstractRecur()
                <li> cuddBddBooleanDiffRecur()
                </ul>
        Static procedures included in this module:
                <ul>
                <li> bddCheckPositiveCube()
                </ul>
                ]
Alan Mishchenko committed
28 29 30

  Author      [Fabio Somenzi]

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 61
  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
62 63 64

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

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

68 69 70
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
71

72

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


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


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


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

#ifndef lint
93
static char rcsid[] DD_UNUSED = "$Id: cuddBddAbs.c,v 1.26 2004/08/13 18:04:46 fabio Exp $";
Alan Mishchenko committed
94 95 96 97 98 99 100 101 102 103 104 105 106
#endif

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


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

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

107
static int bddCheckPositiveCube (DdManager *manager, DdNode *cube);
Alan Mishchenko committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

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


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


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

  Synopsis [Existentially abstracts all the variables in cube from f.]

  Description [Existentially abstracts all the variables in cube from f.
  Returns the abstracted BDD if successful; NULL otherwise.]

  SideEffects [None]

  SeeAlso     [Cudd_bddUnivAbstract Cudd_addExistAbstract]

******************************************************************************/
DdNode *
Cudd_bddExistAbstract(
  DdManager * manager,
  DdNode * f,
  DdNode * cube)
{
    DdNode *res;

    if (bddCheckPositiveCube(manager, cube) == 0) {
        (void) fprintf(manager->err,
139 140
                       "Error: Can only abstract positive cubes\n");
        manager->errorCode = CUDD_INVALID_ARG;
Alan Mishchenko committed
141 142 143 144
        return(NULL);
    }

    do {
145 146
        manager->reordered = 0;
        res = cuddBddExistAbstractRecur(manager, f, cube);
Alan Mishchenko committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    } while (manager->reordered == 1);

    return(res);

} /* end of Cudd_bddExistAbstract */


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

  Synopsis [Takes the exclusive OR of two BDDs and simultaneously abstracts the
  variables in cube.]

  Description [Takes the exclusive OR of two BDDs and simultaneously abstracts
  the variables in cube. The variables are existentially abstracted.  Returns a
  pointer to the result is successful; NULL otherwise.]

  SideEffects [None]

  SeeAlso     [Cudd_bddUnivAbstract Cudd_bddExistAbstract Cudd_bddAndAbstract]

******************************************************************************/
DdNode *
Cudd_bddXorExistAbstract(
  DdManager * manager,
  DdNode * f,
  DdNode * g,
  DdNode * cube)
{
    DdNode *res;

    if (bddCheckPositiveCube(manager, cube) == 0) {
        (void) fprintf(manager->err,
179 180
                       "Error: Can only abstract positive cubes\n");
        manager->errorCode = CUDD_INVALID_ARG;
Alan Mishchenko committed
181 182 183 184
        return(NULL);
    }

    do {
185 186
        manager->reordered = 0;
        res = cuddBddXorExistAbstractRecur(manager, f, g, cube);
Alan Mishchenko committed
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
    } while (manager->reordered == 1);

    return(res);

} /* end of Cudd_bddXorExistAbstract */


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

  Synopsis    [Universally abstracts all the variables in cube from f.]

  Description [Universally abstracts all the variables in cube from f.
  Returns the abstracted BDD if successful; NULL otherwise.]

  SideEffects [None]

  SeeAlso     [Cudd_bddExistAbstract Cudd_addUnivAbstract]

******************************************************************************/
DdNode *
Cudd_bddUnivAbstract(
  DdManager * manager,
  DdNode * f,
  DdNode * cube)
{
212
    DdNode      *res;
Alan Mishchenko committed
213 214

    if (bddCheckPositiveCube(manager, cube) == 0) {
215 216 217 218
        (void) fprintf(manager->err,
                       "Error: Can only abstract positive cubes\n");
        manager->errorCode = CUDD_INVALID_ARG;
        return(NULL);
Alan Mishchenko committed
219 220 221
    }

    do {
222 223
        manager->reordered = 0;
        res = cuddBddExistAbstractRecur(manager, Cudd_Not(f), cube);
Alan Mishchenko committed
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
    } while (manager->reordered == 1);
    if (res != NULL) res = Cudd_Not(res);

    return(res);

} /* end of Cudd_bddUnivAbstract */


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

  Synopsis    [Computes the boolean difference of f with respect to x.]

  Description [Computes the boolean difference of f with respect to the
  variable with index x.  Returns the BDD of the boolean difference if
  successful; NULL otherwise.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
DdNode *
Cudd_bddBooleanDiff(
  DdManager * manager,
  DdNode * f,
  int  x)
{
    DdNode *res, *var;

    /* If the variable is not currently in the manager, f cannot
    ** depend on it.
    */
    if (x >= manager->size) return(Cudd_Not(DD_ONE(manager)));
    var = manager->vars[x];

    do {
260 261
        manager->reordered = 0;
        res = cuddBddBooleanDiffRecur(manager, Cudd_Regular(f), var);
Alan Mishchenko committed
262 263 264 265 266 267 268
    } while (manager->reordered == 1);

    return(res);

} /* end of Cudd_bddBooleanDiff */


269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
#ifdef USE_CASH_DUMMY
/**Function********************************************************************

  Synopsis    We need to declare a function passed to cuddCacheLookup2 that can
              be casted to DD_CTFP.

******************************************************************************/
static DdNode *
Cudd_bddVarIsDependent_dummy(DdManager *dd, DdNode *f, DdNode *var)
{
  assert(0);
  return 0;
}
#endif

Alan Mishchenko committed
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
/**Function********************************************************************

  Synopsis    [Checks whether a variable is dependent on others in a
  function.]

  Description [Checks whether a variable is dependent on others in a
  function.  Returns 1 if the variable is dependent; 0 otherwise. No
  new nodes are created.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
int
Cudd_bddVarIsDependent(
300 301 302
  DdManager *dd,                /* manager */
  DdNode *f,                    /* function */
  DdNode *var                   /* variable */)
Alan Mishchenko committed
303 304 305
{
    DdNode *F, *res, *zero, *ft, *fe;
    unsigned topf, level;
306
    DD_CTFP cacheOp;
Alan Mishchenko committed
307 308 309 310 311 312 313 314 315 316 317 318 319
    int retval;

    zero = Cudd_Not(DD_ONE(dd));
    if (Cudd_IsConstant(f)) return(f == zero);

    /* From now on f is not constant. */
    F = Cudd_Regular(f);
    topf = (unsigned) dd->perm[F->index];
    level = (unsigned) dd->perm[var->index];

    /* Check terminal case. If topf > index of var, f does not depend on var.
    ** Therefore, var is not dependent in f. */
    if (topf > level) {
320
        return(0);
Alan Mishchenko committed
321 322
    }

323 324 325
#ifdef USE_CASH_DUMMY
    cacheOp = (DD_CTFP) Cudd_bddVarIsDependent_dummy;
#else
326
    cacheOp = (DD_CTFP) Cudd_bddVarIsDependent;
327
#endif
Alan Mishchenko committed
328 329
    res = cuddCacheLookup2(dd,cacheOp,f,var);
    if (res != NULL) {
330
        return(res != zero);
Alan Mishchenko committed
331 332 333 334 335 336 337
    }

    /* Compute cofactors. */
    ft = Cudd_NotCond(cuddT(F), f != F);
    fe = Cudd_NotCond(cuddE(F), f != F);

    if (topf == level) {
338
        retval = Cudd_bddLeq(dd,ft,Cudd_Not(fe));
Alan Mishchenko committed
339
    } else {
340 341
        retval = Cudd_bddVarIsDependent(dd,ft,var) &&
            Cudd_bddVarIsDependent(dd,fe,var);
Alan Mishchenko committed
342 343 344 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
    }

    cuddCacheInsert2(dd,cacheOp,f,var,Cudd_NotCond(zero,retval));

    return(retval);

} /* Cudd_bddVarIsDependent */


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


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

  Synopsis    [Performs the recursive steps of Cudd_bddExistAbstract.]

  Description [Performs the recursive steps of Cudd_bddExistAbstract.
  Returns the BDD obtained by abstracting the variables
  of cube from f if successful; NULL otherwise. It is also used by
  Cudd_bddUnivAbstract.]

  SideEffects [None]

  SeeAlso     [Cudd_bddExistAbstract Cudd_bddUnivAbstract]

******************************************************************************/
DdNode *
cuddBddExistAbstractRecur(
  DdManager * manager,
  DdNode * f,
  DdNode * cube)
{
376
    DdNode      *F, *T, *E, *res, *res1, *res2, *one;
Alan Mishchenko committed
377 378 379 380 381

    statLine(manager);
    one = DD_ONE(manager);
    F = Cudd_Regular(f);

382
    /* Cube is guaranteed to be a cube at this point. */        
Alan Mishchenko committed
383 384 385 386 387 388 389
    if (cube == one || F == one) {  
        return(f);
    }
    /* From now on, f and cube are non-constant. */

    /* Abstract a variable that does not appear in f. */
    while (manager->perm[F->index] > manager->perm[cube->index]) {
390 391
        cube = cuddT(cube);
        if (cube == one) return(f);
Alan Mishchenko committed
392 393 394 395
    }

    /* Check the cache. */
    if (F->ref != 1 && (res = cuddCacheLookup2(manager, Cudd_bddExistAbstract, f, cube)) != NULL) {
396
        return(res);
Alan Mishchenko committed
397 398 399 400 401
    }

    /* Compute the cofactors of f. */
    T = cuddT(F); E = cuddE(F);
    if (f != F) {
402
        T = Cudd_Not(T); E = Cudd_Not(E);
Alan Mishchenko committed
403 404 405 406
    }

    /* If the two indices are the same, so are their levels. */
    if (F->index == cube->index) {
407 408 409 410 411 412 413 414 415 416
        if (T == one || E == one || T == Cudd_Not(E)) {
            return(one);
        }
        res1 = cuddBddExistAbstractRecur(manager, T, cuddT(cube));
        if (res1 == NULL) return(NULL);
        if (res1 == one) {
            if (F->ref != 1)
                cuddCacheInsert2(manager, Cudd_bddExistAbstract, f, cube, one);
            return(one);
        }
Alan Mishchenko committed
417
        cuddRef(res1);
418 419 420 421 422
        res2 = cuddBddExistAbstractRecur(manager, E, cuddT(cube));
        if (res2 == NULL) {
            Cudd_IterDerefBdd(manager,res1);
            return(NULL);
        }
Alan Mishchenko committed
423
        cuddRef(res2);
424 425 426 427 428 429 430 431
        res = cuddBddAndRecur(manager, Cudd_Not(res1), Cudd_Not(res2));
        if (res == NULL) {
            Cudd_IterDerefBdd(manager, res1);
            Cudd_IterDerefBdd(manager, res2);
            return(NULL);
        }
        res = Cudd_Not(res);
        cuddRef(res);
Alan Mishchenko committed
432 433
        Cudd_IterDerefBdd(manager, res1);
        Cudd_IterDerefBdd(manager, res2);
434 435 436
        if (F->ref != 1)
            cuddCacheInsert2(manager, Cudd_bddExistAbstract, f, cube, res);
        cuddDeref(res);
Alan Mishchenko committed
437 438
        return(res);
    } else { /* if (cuddI(manager,F->index) < cuddI(manager,cube->index)) */
439 440
        res1 = cuddBddExistAbstractRecur(manager, T, cube);
        if (res1 == NULL) return(NULL);
Alan Mishchenko committed
441
        cuddRef(res1);
442 443 444 445 446
        res2 = cuddBddExistAbstractRecur(manager, E, cube);
        if (res2 == NULL) {
            Cudd_IterDerefBdd(manager, res1);
            return(NULL);
        }
Alan Mishchenko committed
447
        cuddRef(res2);
448
        /* ITE takes care of possible complementation of res1 and of the
Alan Mishchenko committed
449
        ** case in which res1 == res2. */
450 451 452 453 454 455
        res = cuddBddIteRecur(manager, manager->vars[F->index], res1, res2);
        if (res == NULL) {
            Cudd_IterDerefBdd(manager, res1);
            Cudd_IterDerefBdd(manager, res2);
            return(NULL);
        }
456 457 458 459
        cuddRef(res); //Added
        Cudd_IterDerefBdd(manager, res1); //cuddDeref(res1);
        Cudd_IterDerefBdd(manager, res2); //cuddDeref(res2);
        cuddDeref(res); //Added
460 461
        if (F->ref != 1)
            cuddCacheInsert2(manager, Cudd_bddExistAbstract, f, cube, res);
Alan Mishchenko committed
462
        return(res);
463
    }       
Alan Mishchenko committed
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

} /* end of cuddBddExistAbstractRecur */


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

  Synopsis [Takes the exclusive OR of two BDDs and simultaneously abstracts the
  variables in cube.]

  Description [Takes the exclusive OR of two BDDs and simultaneously abstracts
  the variables in cube. The variables are existentially abstracted.  Returns a
  pointer to the result is successful; NULL otherwise.]

  SideEffects [None]

  SeeAlso     [Cudd_bddAndAbstract]

******************************************************************************/
DdNode *
cuddBddXorExistAbstractRecur(
  DdManager * manager,
  DdNode * f,
  DdNode * g,
  DdNode * cube)
{
    DdNode *F, *fv, *fnv, *G, *gv, *gnv;
    DdNode *one, *zero, *r, *t, *e, *Cube;
    unsigned int topf, topg, topcube, top, index;

    statLine(manager);
    one = DD_ONE(manager);
    zero = Cudd_Not(one);

    /* Terminal cases. */
    if (f == g) {
499
        return(zero);
Alan Mishchenko committed
500 501
    }
    if (f == Cudd_Not(g)) {
502
        return(one);
Alan Mishchenko committed
503 504
    }
    if (cube == one) {
505
        return(cuddBddXorRecur(manager, f, g));
Alan Mishchenko committed
506 507
    }
    if (f == one) {
508
        return(cuddBddExistAbstractRecur(manager, Cudd_Not(g), cube));
Alan Mishchenko committed
509 510
    }
    if (g == one) {
511
        return(cuddBddExistAbstractRecur(manager, Cudd_Not(f), cube));
Alan Mishchenko committed
512 513
    }
    if (f == zero) {
514
        return(cuddBddExistAbstractRecur(manager, g, cube));
Alan Mishchenko committed
515 516
    }
    if (g == zero) {
517
        return(cuddBddExistAbstractRecur(manager, f, cube));
Alan Mishchenko committed
518 519 520 521
    }

    /* At this point f, g, and cube are not constant. */

522
    if (cuddF2L(f) > cuddF2L(g)) { /* Try to increase cache efficiency. */
523 524 525
        DdNode *tmp = f;
        f = g;
        g = tmp;
Alan Mishchenko committed
526 527 528 529 530
    }

    /* Check cache. */
    r = cuddCacheLookup(manager, DD_BDD_XOR_EXIST_ABSTRACT_TAG, f, g, cube);
    if (r != NULL) {
531
        return(r);
Alan Mishchenko committed
532 533 534 535 536 537 538 539 540 541 542 543 544
    }

    /* Here we can skip the use of cuddI, because the operands are known
    ** to be non-constant.
    */
    F = Cudd_Regular(f);
    topf = manager->perm[F->index];
    G = Cudd_Regular(g);
    topg = manager->perm[G->index];
    top = ddMin(topf, topg);
    topcube = manager->perm[cube->index];

    if (topcube < top) {
545
        return(cuddBddXorExistAbstractRecur(manager, f, g, cuddT(cube)));
Alan Mishchenko committed
546 547 548 549
    }
    /* Now, topcube >= top. */

    if (topf == top) {
550 551 552 553 554 555 556
        index = F->index;
        fv = cuddT(F);
        fnv = cuddE(F);
        if (Cudd_IsComplement(f)) {
            fv = Cudd_Not(fv);
            fnv = Cudd_Not(fnv);
        }
Alan Mishchenko committed
557
    } else {
558 559
        index = G->index;
        fv = fnv = f;
Alan Mishchenko committed
560 561 562
    }

    if (topg == top) {
563 564 565 566 567 568
        gv = cuddT(G);
        gnv = cuddE(G);
        if (Cudd_IsComplement(g)) {
            gv = Cudd_Not(gv);
            gnv = Cudd_Not(gnv);
        }
Alan Mishchenko committed
569
    } else {
570
        gv = gnv = g;
Alan Mishchenko committed
571 572 573
    }

    if (topcube == top) {
574
        Cube = cuddT(cube);
Alan Mishchenko committed
575
    } else {
576
        Cube = cube;
Alan Mishchenko committed
577 578 579 580 581 582 583 584 585
    }

    t = cuddBddXorExistAbstractRecur(manager, fv, gv, Cube);
    if (t == NULL) return(NULL);

    /* Special case: 1 OR anything = 1. Hence, no need to compute
    ** the else branch if t is 1.
    */
    if (t == one && topcube == top) {
586 587
        cuddCacheInsert(manager, DD_BDD_XOR_EXIST_ABSTRACT_TAG, f, g, cube, one);
        return(one);
Alan Mishchenko committed
588 589 590 591 592 593 594 595
    }
    cuddRef(t);

    e = cuddBddXorExistAbstractRecur(manager, fnv, gnv, Cube);
    if (e == NULL) {
        Cudd_IterDerefBdd(manager, t);
        return(NULL);
    }
596 597 598 599
    cuddRef(e);

    if (topcube == top) {       /* abstract */
        r = cuddBddAndRecur(manager, Cudd_Not(t), Cudd_Not(e));
Alan Mishchenko committed
600
        if (r == NULL) {
601 602 603
            Cudd_IterDerefBdd(manager, t);
            Cudd_IterDerefBdd(manager, e);
            return(NULL);
Alan Mishchenko committed
604 605
        }
        r = Cudd_Not(r);
606
        cuddRef(r);
Alan Mishchenko committed
607 608
        Cudd_IterDerefBdd(manager, t);
        Cudd_IterDerefBdd(manager, e);
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
        cuddDeref(r);
    } else if (t == e) {
        r = t;
        cuddDeref(t);
        cuddDeref(e);
    } else {
        if (Cudd_IsComplement(t)) {
            r = cuddUniqueInter(manager,(int)index,Cudd_Not(t),Cudd_Not(e));
            if (r == NULL) {
                Cudd_IterDerefBdd(manager, t);
                Cudd_IterDerefBdd(manager, e);
                return(NULL);
            }
            r = Cudd_Not(r);
        } else {
            r = cuddUniqueInter(manager,(int)index,t,e);
            if (r == NULL) {
                Cudd_IterDerefBdd(manager, t);
                Cudd_IterDerefBdd(manager, e);
                return(NULL);
            }
Alan Mishchenko committed
630
        }
631 632
        cuddDeref(e);
        cuddDeref(t);
Alan Mishchenko committed
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
    }
    cuddCacheInsert(manager, DD_BDD_XOR_EXIST_ABSTRACT_TAG, f, g, cube, r);
    return (r);

} /* end of cuddBddXorExistAbstractRecur */


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

  Synopsis    [Performs the recursive steps of Cudd_bddBoleanDiff.]

  Description [Performs the recursive steps of Cudd_bddBoleanDiff.
  Returns the BDD obtained by XORing the cofactors of f with respect to
  var if successful; NULL otherwise. Exploits the fact that dF/dx =
  dF'/dx.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
DdNode *
cuddBddBooleanDiffRecur(
  DdManager * manager,
  DdNode * f,
  DdNode * var)
{
    DdNode *T, *E, *res, *res1, *res2;

    statLine(manager);
    if (cuddI(manager,f->index) > manager->perm[var->index]) {
664 665
        /* f does not depend on var. */
        return(Cudd_Not(DD_ONE(manager)));
Alan Mishchenko committed
666 667 668 669 670 671
    }

    /* From now on, f is non-constant. */

    /* If the two indices are the same, so are their levels. */
    if (f->index == var->index) {
672
        res = cuddBddXorRecur(manager, cuddT(f), cuddE(f));
Alan Mishchenko committed
673 674 675 676 677 678 679 680
        return(res);
    }

    /* From now on, cuddI(manager,f->index) < cuddI(manager,cube->index). */

    /* Check the cache. */
    res = cuddCacheLookup2(manager, cuddBddBooleanDiffRecur, f, var);
    if (res != NULL) {
681
        return(res);
Alan Mishchenko committed
682 683 684 685 686 687 688 689 690 691
    }

    /* Compute the cofactors of f. */
    T = cuddT(f); E = cuddE(f);

    res1 = cuddBddBooleanDiffRecur(manager, T, var);
    if (res1 == NULL) return(NULL);
    cuddRef(res1);
    res2 = cuddBddBooleanDiffRecur(manager, Cudd_Regular(E), var);
    if (res2 == NULL) {
692 693
        Cudd_IterDerefBdd(manager, res1);
        return(NULL);
Alan Mishchenko committed
694 695 696 697 698 699
    }
    cuddRef(res2);
    /* ITE takes care of possible complementation of res1 and of the
    ** case in which res1 == res2. */
    res = cuddBddIteRecur(manager, manager->vars[f->index], res1, res2);
    if (res == NULL) {
700 701 702
        Cudd_IterDerefBdd(manager, res1);
        Cudd_IterDerefBdd(manager, res2);
        return(NULL);
Alan Mishchenko committed
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
    }
    cuddDeref(res1);
    cuddDeref(res2);
    cuddCacheInsert2(manager, cuddBddBooleanDiffRecur, f, var, res);
    return(res);

} /* end of cuddBddBooleanDiffRecur */


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

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

  Synopsis [Checks whether cube is an BDD representing the product of
  positive literals.]

  Description [Returns 1 in case of success; 0 otherwise.]

  SideEffects [None]

******************************************************************************/
static int
bddCheckPositiveCube(
  DdManager * manager,
  DdNode * cube)
{
    if (Cudd_IsComplement(cube)) return(0);
    if (cube == DD_ONE(manager)) return(1);
    if (cuddIsConstant(cube)) return(0);
    if (cuddE(cube) == Cudd_Not(DD_ONE(manager))) {
        return(bddCheckPositiveCube(manager, cuddT(cube)));
    }
    return(0);

} /* end of bddCheckPositiveCube */

741

742 743
ABC_NAMESPACE_IMPL_END

744