cuddBddAbs.c 21.5 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

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

Alan Mishchenko committed
65
#include "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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
    } while (manager->reordered == 1);

    return(res);

} /* end of Cudd_bddBooleanDiff */


/**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(
285 286 287
  DdManager *dd,                /* manager */
  DdNode *f,                    /* function */
  DdNode *var                   /* variable */)
Alan Mishchenko committed
288 289 290
{
    DdNode *F, *res, *zero, *ft, *fe;
    unsigned topf, level;
291
    DD_CTFP cacheOp;
Alan Mishchenko committed
292 293 294 295 296 297 298 299 300 301 302 303 304
    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) {
305
        return(0);
Alan Mishchenko committed
306 307
    }

308
    cacheOp = (DD_CTFP) Cudd_bddVarIsDependent;
Alan Mishchenko committed
309 310
    res = cuddCacheLookup2(dd,cacheOp,f,var);
    if (res != NULL) {
311
        return(res != zero);
Alan Mishchenko committed
312 313 314 315 316 317 318
    }

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

    if (topf == level) {
319
        retval = Cudd_bddLeq(dd,ft,Cudd_Not(fe));
Alan Mishchenko committed
320
    } else {
321 322
        retval = Cudd_bddVarIsDependent(dd,ft,var) &&
            Cudd_bddVarIsDependent(dd,fe,var);
Alan Mishchenko committed
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
    }

    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)
{
357
    DdNode      *F, *T, *E, *res, *res1, *res2, *one;
Alan Mishchenko committed
358 359 360 361 362

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

363
    /* Cube is guaranteed to be a cube at this point. */        
Alan Mishchenko committed
364 365 366 367 368 369 370
    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]) {
371 372
        cube = cuddT(cube);
        if (cube == one) return(f);
Alan Mishchenko committed
373 374 375 376
    }

    /* Check the cache. */
    if (F->ref != 1 && (res = cuddCacheLookup2(manager, Cudd_bddExistAbstract, f, cube)) != NULL) {
377
        return(res);
Alan Mishchenko committed
378 379 380 381 382
    }

    /* Compute the cofactors of f. */
    T = cuddT(F); E = cuddE(F);
    if (f != F) {
383
        T = Cudd_Not(T); E = Cudd_Not(E);
Alan Mishchenko committed
384 385 386 387
    }

    /* If the two indices are the same, so are their levels. */
    if (F->index == cube->index) {
388 389 390 391 392 393 394 395 396 397
        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
398
        cuddRef(res1);
399 400 401 402 403
        res2 = cuddBddExistAbstractRecur(manager, E, cuddT(cube));
        if (res2 == NULL) {
            Cudd_IterDerefBdd(manager,res1);
            return(NULL);
        }
Alan Mishchenko committed
404
        cuddRef(res2);
405 406 407 408 409 410 411 412
        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
413 414
        Cudd_IterDerefBdd(manager, res1);
        Cudd_IterDerefBdd(manager, res2);
415 416 417
        if (F->ref != 1)
            cuddCacheInsert2(manager, Cudd_bddExistAbstract, f, cube, res);
        cuddDeref(res);
Alan Mishchenko committed
418 419
        return(res);
    } else { /* if (cuddI(manager,F->index) < cuddI(manager,cube->index)) */
420 421
        res1 = cuddBddExistAbstractRecur(manager, T, cube);
        if (res1 == NULL) return(NULL);
Alan Mishchenko committed
422
        cuddRef(res1);
423 424 425 426 427
        res2 = cuddBddExistAbstractRecur(manager, E, cube);
        if (res2 == NULL) {
            Cudd_IterDerefBdd(manager, res1);
            return(NULL);
        }
Alan Mishchenko committed
428
        cuddRef(res2);
429
        /* ITE takes care of possible complementation of res1 and of the
Alan Mishchenko committed
430
        ** case in which res1 == res2. */
431 432 433 434 435 436 437 438 439 440
        res = cuddBddIteRecur(manager, manager->vars[F->index], res1, res2);
        if (res == NULL) {
            Cudd_IterDerefBdd(manager, res1);
            Cudd_IterDerefBdd(manager, res2);
            return(NULL);
        }
        cuddDeref(res1);
        cuddDeref(res2);
        if (F->ref != 1)
            cuddCacheInsert2(manager, Cudd_bddExistAbstract, f, cube, res);
Alan Mishchenko committed
441
        return(res);
442
    }       
Alan Mishchenko committed
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

} /* 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) {
478
        return(zero);
Alan Mishchenko committed
479 480
    }
    if (f == Cudd_Not(g)) {
481
        return(one);
Alan Mishchenko committed
482 483
    }
    if (cube == one) {
484
        return(cuddBddXorRecur(manager, f, g));
Alan Mishchenko committed
485 486
    }
    if (f == one) {
487
        return(cuddBddExistAbstractRecur(manager, Cudd_Not(g), cube));
Alan Mishchenko committed
488 489
    }
    if (g == one) {
490
        return(cuddBddExistAbstractRecur(manager, Cudd_Not(f), cube));
Alan Mishchenko committed
491 492
    }
    if (f == zero) {
493
        return(cuddBddExistAbstractRecur(manager, g, cube));
Alan Mishchenko committed
494 495
    }
    if (g == zero) {
496
        return(cuddBddExistAbstractRecur(manager, f, cube));
Alan Mishchenko committed
497 498 499 500 501
    }

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

    if (f > g) { /* Try to increase cache efficiency. */
502 503 504
        DdNode *tmp = f;
        f = g;
        g = tmp;
Alan Mishchenko committed
505 506 507 508 509
    }

    /* Check cache. */
    r = cuddCacheLookup(manager, DD_BDD_XOR_EXIST_ABSTRACT_TAG, f, g, cube);
    if (r != NULL) {
510
        return(r);
Alan Mishchenko committed
511 512 513 514 515 516 517 518 519 520 521 522 523
    }

    /* 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) {
524
        return(cuddBddXorExistAbstractRecur(manager, f, g, cuddT(cube)));
Alan Mishchenko committed
525 526 527 528
    }
    /* Now, topcube >= top. */

    if (topf == top) {
529 530 531 532 533 534 535
        index = F->index;
        fv = cuddT(F);
        fnv = cuddE(F);
        if (Cudd_IsComplement(f)) {
            fv = Cudd_Not(fv);
            fnv = Cudd_Not(fnv);
        }
Alan Mishchenko committed
536
    } else {
537 538
        index = G->index;
        fv = fnv = f;
Alan Mishchenko committed
539 540 541
    }

    if (topg == top) {
542 543 544 545 546 547
        gv = cuddT(G);
        gnv = cuddE(G);
        if (Cudd_IsComplement(g)) {
            gv = Cudd_Not(gv);
            gnv = Cudd_Not(gnv);
        }
Alan Mishchenko committed
548
    } else {
549
        gv = gnv = g;
Alan Mishchenko committed
550 551 552
    }

    if (topcube == top) {
553
        Cube = cuddT(cube);
Alan Mishchenko committed
554
    } else {
555
        Cube = cube;
Alan Mishchenko committed
556 557 558 559 560 561 562 563 564
    }

    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) {
565 566
        cuddCacheInsert(manager, DD_BDD_XOR_EXIST_ABSTRACT_TAG, f, g, cube, one);
        return(one);
Alan Mishchenko committed
567 568 569 570 571 572 573 574
    }
    cuddRef(t);

    e = cuddBddXorExistAbstractRecur(manager, fnv, gnv, Cube);
    if (e == NULL) {
        Cudd_IterDerefBdd(manager, t);
        return(NULL);
    }
575 576 577 578
    cuddRef(e);

    if (topcube == top) {       /* abstract */
        r = cuddBddAndRecur(manager, Cudd_Not(t), Cudd_Not(e));
Alan Mishchenko committed
579
        if (r == NULL) {
580 581 582
            Cudd_IterDerefBdd(manager, t);
            Cudd_IterDerefBdd(manager, e);
            return(NULL);
Alan Mishchenko committed
583 584
        }
        r = Cudd_Not(r);
585
        cuddRef(r);
Alan Mishchenko committed
586 587
        Cudd_IterDerefBdd(manager, t);
        Cudd_IterDerefBdd(manager, e);
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
        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
609
        }
610 611
        cuddDeref(e);
        cuddDeref(t);
Alan Mishchenko committed
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
    }
    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]) {
643 644
        /* f does not depend on var. */
        return(Cudd_Not(DD_ONE(manager)));
Alan Mishchenko committed
645 646 647 648 649 650
    }

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

    /* If the two indices are the same, so are their levels. */
    if (f->index == var->index) {
651
        res = cuddBddXorRecur(manager, cuddT(f), cuddE(f));
Alan Mishchenko committed
652 653 654 655 656 657 658 659
        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) {
660
        return(res);
Alan Mishchenko committed
661 662 663 664 665 666 667 668 669 670
    }

    /* 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) {
671 672
        Cudd_IterDerefBdd(manager, res1);
        return(NULL);
Alan Mishchenko committed
673 674 675 676 677 678
    }
    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) {
679 680 681
        Cudd_IterDerefBdd(manager, res1);
        Cudd_IterDerefBdd(manager, res2);
        return(NULL);
Alan Mishchenko committed
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
    }
    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 */

720

721 722
ABC_NAMESPACE_IMPL_END

723