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

  FileName    [cuddSat.c]

  PackageName [cudd]

  Synopsis    [Functions for the solution of satisfiability related
  problems.]

  Description [External procedures included in this file:
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
                <ul>
                <li> Cudd_Eval()
                <li> Cudd_ShortestPath()
                <li> Cudd_LargestCube()
                <li> Cudd_ShortestLength()
                <li> Cudd_Decreasing()
                <li> Cudd_Increasing()
                <li> Cudd_EquivDC()
                <li> Cudd_bddLeqUnless()
                <li> Cudd_EqualSupNorm()
                <li> Cudd_bddMakePrime()
                </ul>
        Internal procedures included in this module:
                <ul>
                <li> cuddBddMakePrime()
                </ul>
        Static procedures included in this module:
                <ul>
                <li> freePathPair()
                <li> getShortest()
                <li> getPath()
                <li> getLargest()
                <li> getCube()
                </ul>]
Alan Mishchenko committed
35 36 37

  Author      [Seh-Woong Jeong, Fabio Somenzi]

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
  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
69 70 71

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

Alan Mishchenko committed
72
#include "util_hack.h"
Alan Mishchenko committed
73 74
#include "cuddInt.h"

75 76 77
ABC_NAMESPACE_IMPL_START


78

Alan Mishchenko committed
79 80 81 82
/*---------------------------------------------------------------------------*/
/* Constant declarations                                                     */
/*---------------------------------------------------------------------------*/

83
#define DD_BIGGY        1000000
Alan Mishchenko committed
84 85 86 87 88 89 90 91 92 93

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

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

typedef struct cuddPathPair {
94 95
    int pos;
    int neg;
Alan Mishchenko committed
96 97 98 99 100 101 102
} cuddPathPair;

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

#ifndef lint
103
static char rcsid[] DD_UNUSED = "$Id: cuddSat.c,v 1.36 2009/03/08 02:49:02 fabio Exp $";
Alan Mishchenko committed
104 105
#endif

106
static  DdNode  *one, *zero;
Alan Mishchenko committed
107 108 109 110 111

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

112 113 114 115 116
#define WEIGHT(weight, col)     ((weight) == NULL ? 1 : weight[col])

#ifdef __cplusplus
extern "C" {
#endif
Alan Mishchenko committed
117 118 119 120 121 122 123

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

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

124 125 126 127 128
static enum st_retval freePathPair (char *key, char *value, char *arg);
static cuddPathPair getShortest (DdNode *root, int *cost, int *support, st_table *visited);
static DdNode * getPath (DdManager *manager, st_table *visited, DdNode *f, int *weight, int cost);
static cuddPathPair getLargest (DdNode *root, st_table *visited);
static DdNode * getCube (DdManager *manager, st_table *visited, DdNode *f, int cost);
Alan Mishchenko committed
129 130 131

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

132 133 134
#ifdef __cplusplus
}
#endif
Alan Mishchenko committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

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


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

  Synopsis    [Returns the value of a DD for a given variable assignment.]

  Description [Finds the value of a DD for a given variable
  assignment. The variable assignment is passed in an array of int's,
  that should specify a zero or a one for each variable in the support
  of the function. Returns a pointer to a constant node. No new nodes
  are produced.]

  SideEffects [None]

  SeeAlso     [Cudd_bddLeq Cudd_addEvalConst]

******************************************************************************/
DdNode *
Cudd_Eval(
  DdManager * dd,
  DdNode * f,
  int * inputs)
{
    int comple;
    DdNode *ptr;

    comple = Cudd_IsComplement(f);
    ptr = Cudd_Regular(f);

    while (!cuddIsConstant(ptr)) {
169 170 171 172 173 174
        if (inputs[ptr->index] == 1) {
            ptr = cuddT(ptr);
        } else {
            comple ^= Cudd_IsComplement(cuddE(ptr));
            ptr = Cudd_Regular(cuddE(ptr));
        }
Alan Mishchenko committed
175 176 177 178 179 180 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
    }
    return(Cudd_NotCond(ptr,comple));

} /* end of Cudd_Eval */


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

  Synopsis    [Finds a shortest path in a DD.]

  Description [Finds a shortest path in a DD. f is the DD we want to
  get the shortest path for; weight\[i\] is the weight of the THEN arc
  coming from the node whose index is i. If weight is NULL, then unit
  weights are assumed for all THEN arcs. All ELSE arcs have 0 weight.
  If non-NULL, both weight and support should point to arrays with at
  least as many entries as there are variables in the manager.
  Returns the shortest path as the BDD of a cube.]

  SideEffects [support contains on return the true support of f.
  If support is NULL on entry, then Cudd_ShortestPath does not compute
  the true support info. length contains the length of the path.]

  SeeAlso     [Cudd_ShortestLength Cudd_LargestCube]

******************************************************************************/
DdNode *
Cudd_ShortestPath(
  DdManager * manager,
  DdNode * f,
  int * weight,
  int * support,
  int * length)
{
208
    DdNode      *F;
Alan Mishchenko committed
209
    st_table    *visited;
210
    DdNode      *sol;
Alan Mishchenko committed
211
    cuddPathPair *rootPair;
212 213
    int         complement, cost;
    int         i;
Alan Mishchenko committed
214 215 216 217

    one = DD_ONE(manager);
    zero = DD_ZERO(manager);

218 219 220
    /* Initialize support. Support does not depend on variable order.
    ** Hence, it does not need to be reinitialized if reordering occurs.
    */
Alan Mishchenko committed
221
    if (support) {
222
      for (i = 0; i < manager->size; i++) {
Alan Mishchenko committed
223
        support[i] = 0;
224
      }
Alan Mishchenko committed
225 226 227
    }

    if (f == Cudd_Not(one) || f == zero) {
228 229
      *length = DD_BIGGY;
      return(Cudd_Not(one));
Alan Mishchenko committed
230 231 232
    }
    /* From this point on, a path exists. */

233 234
    do {
        manager->reordered = 0;
Alan Mishchenko committed
235

236 237
        /* Initialize visited table. */
        visited = st_init_table(st_ptrcmp, st_ptrhash);
Alan Mishchenko committed
238

239 240
        /* Now get the length of the shortest path(s) from f to 1. */
        (void) getShortest(f, weight, support, visited);
Alan Mishchenko committed
241

242
        complement = Cudd_IsComplement(f);
Alan Mishchenko committed
243

244
        F = Cudd_Regular(f);
Alan Mishchenko committed
245

246
        if (!st_lookup(visited, (const char *)F, (char **)&rootPair)) return(NULL);
Alan Mishchenko committed
247

248 249 250 251 252 253 254 255 256 257 258 259 260
        if (complement) {
          cost = rootPair->neg;
        } else {
          cost = rootPair->pos;
        }

        /* Recover an actual shortest path. */
        sol = getPath(manager,visited,f,weight,cost);

        st_foreach(visited, freePathPair, NULL);
        st_free_table(visited);

    } while (manager->reordered == 1);
Alan Mishchenko committed
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289

    *length = cost;
    return(sol);

} /* end of Cudd_ShortestPath */


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

  Synopsis    [Finds a largest cube in a DD.]

  Description [Finds a largest cube in a DD. f is the DD we want to
  get the largest cube for. The problem is translated into the one of
  finding a shortest path in f, when both THEN and ELSE arcs are assumed to
  have unit length. This yields a largest cube in the disjoint cover
  corresponding to the DD. Therefore, it is not necessarily the largest
  implicant of f.  Returns the largest cube as a BDD.]

  SideEffects [The number of literals of the cube is returned in length.]

  SeeAlso     [Cudd_ShortestPath]

******************************************************************************/
DdNode *
Cudd_LargestCube(
  DdManager * manager,
  DdNode * f,
  int * length)
{
290
    register    DdNode  *F;
Alan Mishchenko committed
291
    st_table    *visited;
292
    DdNode      *sol;
Alan Mishchenko committed
293
    cuddPathPair *rootPair;
294
    int         complement, cost;
Alan Mishchenko committed
295 296 297 298 299

    one = DD_ONE(manager);
    zero = DD_ZERO(manager);

    if (f == Cudd_Not(one) || f == zero) {
300 301
        *length = DD_BIGGY;
        return(Cudd_Not(one));
Alan Mishchenko committed
302 303 304
    }
    /* From this point on, a path exists. */

305 306
    do {
        manager->reordered = 0;
Alan Mishchenko committed
307

308 309
        /* Initialize visited table. */
        visited = st_init_table(st_ptrcmp, st_ptrhash);
Alan Mishchenko committed
310

311 312
        /* Now get the length of the shortest path(s) from f to 1. */
        (void) getLargest(f, visited);
Alan Mishchenko committed
313

314
        complement = Cudd_IsComplement(f);
Alan Mishchenko committed
315

316
        F = Cudd_Regular(f);
Alan Mishchenko committed
317

318
        if (!st_lookup(visited, (const char *)F, (char **)&rootPair)) return(NULL);
Alan Mishchenko committed
319

320 321 322 323 324 325 326 327 328 329 330 331 332
        if (complement) {
          cost = rootPair->neg;
        } else {
          cost = rootPair->pos;
        }

        /* Recover an actual shortest path. */
        sol = getCube(manager,visited,f,cost);

        st_foreach(visited, freePathPair, NULL);
        st_free_table(visited);

    } while (manager->reordered == 1);
Alan Mishchenko committed
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347

    *length = cost;
    return(sol);

} /* end of Cudd_LargestCube */


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

  Synopsis    [Find the length of the shortest path(s) in a DD.]

  Description [Find the length of the shortest path(s) in a DD. f is
  the DD we want to get the shortest path for; weight\[i\] is the
  weight of the THEN edge coming from the node whose index is i. All
  ELSE edges have 0 weight. Returns the length of the shortest
348 349
  path(s) if such a path is found; a large number if the function is
  identically 0, and CUDD_OUT_OF_MEM in case of failure.]
Alan Mishchenko committed
350 351 352 353 354 355 356 357 358 359 360 361

  SideEffects [None]

  SeeAlso     [Cudd_ShortestPath]

******************************************************************************/
int
Cudd_ShortestLength(
  DdManager * manager,
  DdNode * f,
  int * weight)
{
362
    register    DdNode  *F;
Alan Mishchenko committed
363 364
    st_table    *visited;
    cuddPathPair *my_pair;
365
    int         complement, cost;
Alan Mishchenko committed
366 367 368 369 370

    one = DD_ONE(manager);
    zero = DD_ZERO(manager);

    if (f == Cudd_Not(one) || f == zero) {
371
        return(DD_BIGGY);
Alan Mishchenko committed
372 373 374 375 376 377 378 379 380 381 382 383 384
    }

    /* From this point on, a path exists. */
    /* Initialize visited table and support. */
    visited = st_init_table(st_ptrcmp, st_ptrhash);

    /* Now get the length of the shortest path(s) from f to 1. */
    (void) getShortest(f, weight, NULL, visited);

    complement = Cudd_IsComplement(f);

    F = Cudd_Regular(f);

385
    if (!st_lookup(visited, (const char *)F, (char **)&my_pair)) return(CUDD_OUT_OF_MEM);
Alan Mishchenko committed
386 387
    
    if (complement) {
388
        cost = my_pair->neg;
Alan Mishchenko committed
389
    } else {
390
        cost = my_pair->pos;
Alan Mishchenko committed
391 392
    }

393
    st_foreach(visited, freePathPair, NULL);
Alan Mishchenko committed
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
    st_free_table(visited);

    return(cost);

} /* end of Cudd_ShortestLength */


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

  Synopsis    [Determines whether a BDD is negative unate in a
  variable.]

  Description [Determines whether the function represented by BDD f is
  negative unate (monotonic decreasing) in variable i. Returns the
  constant one is f is unate and the (logical) constant zero if it is not.
  This function does not generate any new nodes.]

  SideEffects [None]

  SeeAlso     [Cudd_Increasing]

******************************************************************************/
DdNode *
Cudd_Decreasing(
  DdManager * dd,
  DdNode * f,
  int  i)
{
    unsigned int topf, level;
    DdNode *F, *fv, *fvn, *res;
424
    DD_CTFP cacheOp;
Alan Mishchenko committed
425 426 427 428 429 430 431 432 433 434 435 436 437 438

    statLine(dd);
#ifdef DD_DEBUG
    assert(0 <= i && i < dd->size);
#endif

    F = Cudd_Regular(f);
    topf = cuddI(dd,F->index);

    /* Check terminal case. If topf > i, f does not depend on var.
    ** Therefore, f is unate in i.
    */
    level = (unsigned) dd->perm[i];
    if (topf > level) {
439
        return(DD_ONE(dd));
Alan Mishchenko committed
440 441 442 443 444
    }

    /* From now on, f is not constant. */

    /* Check cache. */
445
    cacheOp = (DD_CTFP) Cudd_Decreasing;
Alan Mishchenko committed
446 447
    res = cuddCacheLookup2(dd,cacheOp,f,dd->vars[i]);
    if (res != NULL) {
448
        return(res);
Alan Mishchenko committed
449 450 451 452 453
    }

    /* Compute cofactors. */
    fv = cuddT(F); fvn = cuddE(F);
    if (F != f) {
454 455
        fv = Cudd_Not(fv);
        fvn = Cudd_Not(fvn);
Alan Mishchenko committed
456 457 458
    }

    if (topf == (unsigned) level) {
459 460 461 462 463 464 465 466 467
        /* Special case: if fv is regular, fv(1,...,1) = 1;
        ** If in addition fvn is complemented, fvn(1,...,1) = 0.
        ** But then f(1,1,...,1) > f(0,1,...,1). Hence f is not
        ** monotonic decreasing in i.
        */
        if (!Cudd_IsComplement(fv) && Cudd_IsComplement(fvn)) {
            return(Cudd_Not(DD_ONE(dd)));
        }
        res = Cudd_bddLeq(dd,fv,fvn) ? DD_ONE(dd) : Cudd_Not(DD_ONE(dd));
Alan Mishchenko committed
468
    } else {
469 470 471 472
        res = Cudd_Decreasing(dd,fv,i);
        if (res == DD_ONE(dd)) {
            res = Cudd_Decreasing(dd,fvn,i);
        }
Alan Mishchenko committed
473 474 475 476 477 478 479 480 481 482 483 484 485 486
    }

    cuddCacheInsert2(dd,cacheOp,f,dd->vars[i],res);
    return(res);

} /* end of Cudd_Decreasing */


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

  Synopsis    [Determines whether a BDD is positive unate in a
  variable.]

  Description [Determines whether the function represented by BDD f is
487
  positive unate (monotonic increasing) in variable i. It is based on
Alan Mishchenko committed
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
  Cudd_Decreasing and the fact that f is monotonic increasing in i if
  and only if its complement is monotonic decreasing in i.]

  SideEffects [None]

  SeeAlso     [Cudd_Decreasing]

******************************************************************************/
DdNode *
Cudd_Increasing(
  DdManager * dd,
  DdNode * f,
  int  i)
{
    return(Cudd_Decreasing(dd,Cudd_Not(f),i));

} /* end of Cudd_Increasing */


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

  Synopsis    [Tells whether F and G are identical wherever D is 0.]

  Description [Tells whether F and G are identical wherever D is 0.  F
  and G are either two ADDs or two BDDs.  D is either a 0-1 ADD or a
  BDD.  The function returns 1 if F and G are equivalent, and 0
  otherwise.  No new nodes are created.]

  SideEffects [None]

  SeeAlso     [Cudd_bddLeqUnless]

******************************************************************************/
int
Cudd_EquivDC(
  DdManager * dd,
  DdNode * F,
  DdNode * G,
  DdNode * D)
{
    DdNode *tmp, *One, *Gr, *Dr;
    DdNode *Fv, *Fvn, *Gv, *Gvn, *Dv, *Dvn;
    int res;
    unsigned int flevel, glevel, dlevel, top;

    One = DD_ONE(dd);

    statLine(dd);
    /* Check terminal cases. */
    if (D == One || F == G) return(1);
    if (D == Cudd_Not(One) || D == DD_ZERO(dd) || F == Cudd_Not(G)) return(0);

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

    /* Normalize call to increase cache efficiency. */
    if (F > G) {
544 545 546
        tmp = F;
        F = G;
        G = tmp;
Alan Mishchenko committed
547 548
    }
    if (Cudd_IsComplement(F)) {
549 550
        F = Cudd_Not(F);
        G = Cudd_Not(G);
Alan Mishchenko committed
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
    }

    /* From now on, F is regular. */

    /* Check cache. */
    tmp = cuddCacheLookup(dd,DD_EQUIV_DC_TAG,F,G,D);
    if (tmp != NULL) return(tmp == One);

    /* Find splitting variable. */
    flevel = cuddI(dd,F->index);
    Gr = Cudd_Regular(G);
    glevel = cuddI(dd,Gr->index);
    top = ddMin(flevel,glevel);
    Dr = Cudd_Regular(D);
    dlevel = dd->perm[Dr->index];
    top = ddMin(top,dlevel);

    /* Compute cofactors. */
    if (top == flevel) {
570 571
        Fv = cuddT(F);
        Fvn = cuddE(F);
Alan Mishchenko committed
572
    } else {
573
        Fv = Fvn = F;
Alan Mishchenko committed
574 575
    }
    if (top == glevel) {
576 577 578 579 580 581
        Gv = cuddT(Gr);
        Gvn = cuddE(Gr);
        if (G != Gr) {
            Gv = Cudd_Not(Gv);
            Gvn = Cudd_Not(Gvn);
        }
Alan Mishchenko committed
582
    } else {
583
        Gv = Gvn = G;
Alan Mishchenko committed
584 585
    }
    if (top == dlevel) {
586 587 588 589 590 591
        Dv = cuddT(Dr);
        Dvn = cuddE(Dr);
        if (D != Dr) {
            Dv = Cudd_Not(Dv);
            Dvn = Cudd_Not(Dvn);
        }
Alan Mishchenko committed
592
    } else {
593
        Dv = Dvn = D;
Alan Mishchenko committed
594 595 596 597 598
    }

    /* Solve recursively. */
    res = Cudd_EquivDC(dd,Fv,Gv,Dv);
    if (res != 0) {
599
        res = Cudd_EquivDC(dd,Fvn,Gvn,Dvn);
Alan Mishchenko committed
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
    }
    cuddCacheInsert(dd,DD_EQUIV_DC_TAG,F,G,D,(res) ? One : Cudd_Not(One));

    return(res);

} /* end of Cudd_EquivDC */


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

  Synopsis    [Tells whether f is less than of equal to G unless D is 1.]

  Description [Tells whether f is less than of equal to G unless D is
  1.  f, g, and D are BDDs.  The function returns 1 if f is less than
  of equal to G, and 0 otherwise.  No new nodes are created.]

  SideEffects [None]

  SeeAlso     [Cudd_EquivDC Cudd_bddLeq Cudd_bddIteConstant]

******************************************************************************/
int
Cudd_bddLeqUnless(
  DdManager *dd,
  DdNode *f,
  DdNode *g,
  DdNode *D)
{
    DdNode *tmp, *One, *F, *G;
    DdNode *Ft, *Fe, *Gt, *Ge, *Dt, *De;
    int res;
    unsigned int flevel, glevel, dlevel, top;

    statLine(dd);

    One = DD_ONE(dd);

    /* Check terminal cases. */
    if (f == g || g == One || f == Cudd_Not(One) || D == One ||
639
        D == f || D == Cudd_Not(g)) return(1);
Alan Mishchenko committed
640 641
    /* Check for two-operand cases. */
    if (D == Cudd_Not(One) || D == g || D == Cudd_Not(f))
642
        return(Cudd_bddLeq(dd,f,g));
Alan Mishchenko committed
643 644 645 646 647 648 649 650 651 652 653 654 655 656
    if (g == Cudd_Not(One) || g == Cudd_Not(f)) return(Cudd_bddLeq(dd,f,D));
    if (f == One) return(Cudd_bddLeq(dd,Cudd_Not(g),D));

    /* From now on, f, g, and D are non-constant, distinct, and
    ** non-complementary. */

    /* Normalize call to increase cache efficiency.  We rely on the
    ** fact that f <= g unless D is equivalent to not(g) <= not(f)
    ** unless D and to f <= D unless g.  We make sure that D is
    ** regular, and that at most one of f and g is complemented.  We also
    ** ensure that when two operands can be swapped, the one with the
    ** lowest address comes first. */

    if (Cudd_IsComplement(D)) {
657 658 659 660 661 662 663 664 665 666 667 668 669 670
        if (Cudd_IsComplement(g)) {
            /* Special case: if f is regular and g is complemented,
            ** f(1,...,1) = 1 > 0 = g(1,...,1).  If D(1,...,1) = 0, return 0.
            */
            if (!Cudd_IsComplement(f)) return(0);
            /* !g <= D unless !f  or  !D <= g unless !f */
            tmp = D;
            D = Cudd_Not(f);
            if (g < tmp) {
                f = Cudd_Not(g);
                g = tmp;
            } else {
                f = Cudd_Not(tmp);
            }
Alan Mishchenko committed
671
        } else {
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
            if (Cudd_IsComplement(f)) {
                /* !D <= !f unless g  or  !D <= g unless !f */
                tmp = f;
                f = Cudd_Not(D);
                if (tmp < g) {
                    D = g;
                    g = Cudd_Not(tmp);
                } else {
                    D = Cudd_Not(tmp);
                }
            } else {
                /* f <= D unless g  or  !D <= !f unless g */
                tmp = D;
                D = g;
                if (tmp < f) {
                    g = Cudd_Not(f);
                    f = Cudd_Not(tmp);
                } else {
                    g = tmp;
                }
            }
Alan Mishchenko committed
693 694
        }
    } else {
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
        if (Cudd_IsComplement(g)) {
            if (Cudd_IsComplement(f)) {
                /* !g <= !f unless D  or  !g <= D unless !f */
                tmp = f;
                f = Cudd_Not(g);
                if (D < tmp) {
                    g = D;
                    D = Cudd_Not(tmp);
                } else {
                    g = Cudd_Not(tmp);
                }
            } else {
                /* f <= g unless D  or  !g <= !f unless D */
                if (g < f) {
                    tmp = g;
                    g = Cudd_Not(f);
                    f = Cudd_Not(tmp);
                }
            }
Alan Mishchenko committed
714
        } else {
715 716 717 718 719 720
            /* f <= g unless D  or  f <= D unless g */
            if (D < g) {
                tmp = D;
                D = g;
                g = tmp;
            }
Alan Mishchenko committed
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
        }
    }

    /* From now on, D is regular. */

    /* Check cache. */
    tmp = cuddCacheLookup(dd,DD_BDD_LEQ_UNLESS_TAG,f,g,D);
    if (tmp != NULL) return(tmp == One);

    /* Find splitting variable. */
    F = Cudd_Regular(f);
    flevel = dd->perm[F->index];
    G = Cudd_Regular(g);
    glevel = dd->perm[G->index];
    top = ddMin(flevel,glevel);
    dlevel = dd->perm[D->index];
    top = ddMin(top,dlevel);

    /* Compute cofactors. */
    if (top == flevel) {
741 742 743 744 745 746
        Ft = cuddT(F);
        Fe = cuddE(F);
        if (F != f) {
            Ft = Cudd_Not(Ft);
            Fe = Cudd_Not(Fe);
        }
Alan Mishchenko committed
747
    } else {
748
        Ft = Fe = f;
Alan Mishchenko committed
749 750
    }
    if (top == glevel) {
751 752 753 754 755 756
        Gt = cuddT(G);
        Ge = cuddE(G);
        if (G != g) {
            Gt = Cudd_Not(Gt);
            Ge = Cudd_Not(Ge);
        }
Alan Mishchenko committed
757
    } else {
758
        Gt = Ge = g;
Alan Mishchenko committed
759 760
    }
    if (top == dlevel) {
761 762
        Dt = cuddT(D);
        De = cuddE(D);
Alan Mishchenko committed
763
    } else {
764
        Dt = De = D;
Alan Mishchenko committed
765 766 767 768 769
    }

    /* Solve recursively. */
    res = Cudd_bddLeqUnless(dd,Ft,Gt,Dt);
    if (res != 0) {
770
        res = Cudd_bddLeqUnless(dd,Fe,Ge,De);
Alan Mishchenko committed
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
    }
    cuddCacheInsert(dd,DD_BDD_LEQ_UNLESS_TAG,f,g,D,Cudd_NotCond(One,!res));

    return(res);

} /* end of Cudd_bddLeqUnless */


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

  Synopsis    [Compares two ADDs for equality within tolerance.]

  Description [Compares two ADDs for equality within tolerance. Two
  ADDs are reported to be equal if the maximum difference between them
  (the sup norm of their difference) is less than or equal to the
  tolerance parameter. Returns 1 if the two ADDs are equal (within
  tolerance); 0 otherwise. If parameter <code>pr</code> is positive
  the first failure is reported to the standard output.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
int
Cudd_EqualSupNorm(
  DdManager * dd /* manager */,
  DdNode * f /* first ADD */,
  DdNode * g /* second ADD */,
  CUDD_VALUE_TYPE  tolerance /* maximum allowed difference */,
  int  pr /* verbosity level */)
{
    DdNode *fv, *fvn, *gv, *gvn, *r;
    unsigned int topf, topg;

    statLine(dd);
    /* Check terminal cases. */
    if (f == g) return(1);
    if (Cudd_IsConstant(f) && Cudd_IsConstant(g)) {
810 811 812 813 814 815 816 817 818 819 820 821 822
        if (ddEqualVal(cuddV(f),cuddV(g),tolerance)) {
            return(1);
        } else {
            if (pr>0) {
                (void) fprintf(dd->out,"Offending nodes:\n");
                (void) fprintf(dd->out,
                               "f: address = %p\t value = %40.30f\n",
                               (void *) f, cuddV(f));
                (void) fprintf(dd->out,
                               "g: address = %p\t value = %40.30f\n",
                               (void *) g, cuddV(g));
            }
            return(0);
Alan Mishchenko committed
823 824 825 826 827
        }
    }

    /* We only insert the result in the cache if the comparison is
    ** successful. Therefore, if we hit we return 1. */
828
    r = cuddCacheLookup2(dd,(DD_CTFP)Cudd_EqualSupNorm,f,g);
Alan Mishchenko committed
829
    if (r != NULL) {
830
        return(1);
Alan Mishchenko committed
831 832 833 834 835 836 837 838 839 840 841 842
    }

    /* Compute the cofactors and solve the recursive subproblems. */
    topf = cuddI(dd,f->index);
    topg = cuddI(dd,g->index);

    if (topf <= topg) {fv = cuddT(f); fvn = cuddE(f);} else {fv = fvn = f;}
    if (topg <= topf) {gv = cuddT(g); gvn = cuddE(g);} else {gv = gvn = g;}

    if (!Cudd_EqualSupNorm(dd,fv,gv,tolerance,pr)) return(0);
    if (!Cudd_EqualSupNorm(dd,fvn,gvn,tolerance,pr)) return(0);

843
    cuddCacheInsert2(dd,(DD_CTFP)Cudd_EqualSupNorm,f,g,DD_ONE(dd));
Alan Mishchenko committed
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873

    return(1);

} /* end of Cudd_EqualSupNorm */


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

  Synopsis    [Expands cube to a prime implicant of f.]

  Description [Expands cube to a prime implicant of f. Returns the prime
  if successful; NULL otherwise.  In particular, NULL is returned if cube
  is not a real cube or is not an implicant of f.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
DdNode *
Cudd_bddMakePrime(
  DdManager *dd /* manager */,
  DdNode *cube /* cube to be expanded */,
  DdNode *f /* function of which the cube is to be made a prime */)
{
    DdNode *res;

    if (!Cudd_bddLeq(dd,cube,f)) return(NULL);

    do {
874 875
        dd->reordered = 0;
        res = cuddBddMakePrime(dd,cube,f);
Alan Mishchenko committed
876 877 878 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 907 908 909 910 911 912
    } while (dd->reordered == 1);
    return(res);

} /* end of Cudd_bddMakePrime */


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


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

  Synopsis    [Performs the recursive step of Cudd_bddMakePrime.]

  Description [Performs the recursive step of Cudd_bddMakePrime.
  Returns the prime if successful; NULL otherwise.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
DdNode *
cuddBddMakePrime(
  DdManager *dd /* manager */,
  DdNode *cube /* cube to be expanded */,
  DdNode *f /* function of which the cube is to be made a prime */)
{
    DdNode *scan;
    DdNode *t, *e;
    DdNode *res = cube;
    DdNode *zero = Cudd_Not(DD_ONE(dd));

    Cudd_Ref(res);
    scan = cube;
    while (!Cudd_IsConstant(scan)) {
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
        DdNode *reg = Cudd_Regular(scan);
        DdNode *var = dd->vars[reg->index];
        DdNode *expanded = Cudd_bddExistAbstract(dd,res,var);
        if (expanded == NULL) {
            return(NULL);
        }
        Cudd_Ref(expanded);
        if (Cudd_bddLeq(dd,expanded,f)) {
            Cudd_RecursiveDeref(dd,res);
            res = expanded;
        } else {
            Cudd_RecursiveDeref(dd,expanded);
        }
        cuddGetBranches(scan,&t,&e);
        if (t == zero) {
            scan = e;
        } else if (e == zero) {
            scan = t;
        } else {
            Cudd_RecursiveDeref(dd,res);
            return(NULL);       /* cube is not a cube */
        }
Alan Mishchenko committed
935 936 937
    }

    if (scan == DD_ONE(dd)) {
938 939
        Cudd_Deref(res);
        return(res);
Alan Mishchenko committed
940
    } else {
941 942
        Cudd_RecursiveDeref(dd,res);
        return(NULL);
Alan Mishchenko committed
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
    }

} /* end of cuddBddMakePrime */


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


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

  Synopsis    [Frees the entries of the visited symbol table.]

  Description [Frees the entries of the visited symbol table. Returns
  ST_CONTINUE.]

  SideEffects [None]

******************************************************************************/
static enum st_retval
freePathPair(
  char * key,
  char * value,
  char * arg)
{
    cuddPathPair *pair;

    pair = (cuddPathPair *) value;
972
        ABC_FREE(pair);
Alan Mishchenko committed
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
    return(ST_CONTINUE);

} /* end of freePathPair */


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

  Synopsis    [Finds the length of the shortest path(s) in a DD.]

  Description [Finds the length of the shortest path(s) in a DD.
  Uses a local symbol table to store the lengths for each
  node. Only the lengths for the regular nodes are entered in the table,
  because those for the complement nodes are simply obtained by swapping
  the two lenghts.
  Returns a pair of lengths: the length of the shortest path to 1;
  and the length of the shortest path to 0. This is done so as to take
  complement arcs into account.]

  SideEffects [Accumulates the support of the DD in support.]

  SeeAlso     []

******************************************************************************/
static cuddPathPair
getShortest(
  DdNode * root,
  int * cost,
  int * support,
  st_table * visited)
{
    cuddPathPair *my_pair, res_pair, pair_T, pair_E;
1004 1005
    DdNode      *my_root, *T, *E;
    int         weight;
Alan Mishchenko committed
1006 1007 1008

    my_root = Cudd_Regular(root);

1009 1010 1011 1012 1013 1014 1015 1016 1017
    if (st_lookup(visited, (const char *)my_root, (char **)&my_pair)) {
        if (Cudd_IsComplement(root)) {
            res_pair.pos = my_pair->neg;
            res_pair.neg = my_pair->pos;
        } else {
            res_pair.pos = my_pair->pos;
            res_pair.neg = my_pair->neg;
        }
        return(res_pair);
Alan Mishchenko committed
1018 1019 1020 1021 1022 1023 1024 1025
    }

    /* In the case of a BDD the following test is equivalent to
    ** testing whether the BDD is the constant 1. This formulation,
    ** however, works for ADDs as well, by assuming the usual
    ** dichotomy of 0 and != 0.
    */
    if (cuddIsConstant(my_root)) {
1026 1027 1028 1029 1030 1031 1032
        if (my_root != zero) {
            res_pair.pos = 0;
            res_pair.neg = DD_BIGGY;
        } else {
            res_pair.pos = DD_BIGGY;
            res_pair.neg = 0;
        }
Alan Mishchenko committed
1033
    } else {
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
        T = cuddT(my_root);
        E = cuddE(my_root);

        pair_T = getShortest(T, cost, support, visited);
        pair_E = getShortest(E, cost, support, visited);
        weight = WEIGHT(cost, my_root->index);
        res_pair.pos = ddMin(pair_T.pos+weight, pair_E.pos);
        res_pair.neg = ddMin(pair_T.neg+weight, pair_E.neg);

        /* Update support. */
        if (support != NULL) {
            support[my_root->index] = 1;
        }
Alan Mishchenko committed
1047 1048
    }

Alan Mishchenko committed
1049
    my_pair = ABC_ALLOC(cuddPathPair, 1);
Alan Mishchenko committed
1050
    if (my_pair == NULL) {
1051 1052 1053 1054 1055 1056
        if (Cudd_IsComplement(root)) {
            int tmp = res_pair.pos;
            res_pair.pos = res_pair.neg;
            res_pair.neg = tmp;
        }
        return(res_pair);
Alan Mishchenko committed
1057 1058 1059 1060 1061 1062
    }
    my_pair->pos = res_pair.pos;
    my_pair->neg = res_pair.neg;

    st_insert(visited, (char *)my_root, (char *)my_pair);
    if (Cudd_IsComplement(root)) {
1063 1064
        res_pair.pos = my_pair->neg;
        res_pair.neg = my_pair->pos;
Alan Mishchenko committed
1065
    } else {
1066 1067
        res_pair.pos = my_pair->pos;
        res_pair.neg = my_pair->neg;
Alan Mishchenko committed
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
    }
    return(res_pair);

} /* end of getShortest */


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

  Synopsis    [Build a BDD for a shortest path of f.]

  Description [Build a BDD for a shortest path of f.
  Given the minimum length from the root, and the minimum
  lengths for each node (in visited), apply triangulation at each node.
  Of the two children of each node on a shortest path, at least one is
  on a shortest path. In case of ties the procedure chooses the THEN
  children.
  Returns a pointer to the cube BDD representing the path if
  successful; NULL otherwise.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static DdNode *
getPath(
  DdManager * manager,
  st_table * visited,
  DdNode * f,
  int * weight,
  int  cost)
{
1100 1101
    DdNode      *sol, *tmp;
    DdNode      *my_dd, *T, *E;
Alan Mishchenko committed
1102
    cuddPathPair *T_pair, *E_pair;
1103 1104
    int         Tcost, Ecost;
    int         complement;
Alan Mishchenko committed
1105 1106 1107 1108 1109 1110 1111 1112

    my_dd = Cudd_Regular(f);
    complement = Cudd_IsComplement(f);

    sol = one;
    cuddRef(sol);

    while (!cuddIsConstant(my_dd)) {
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
        Tcost = cost - WEIGHT(weight, my_dd->index);
        Ecost = cost;

        T = cuddT(my_dd);
        E = cuddE(my_dd);

        if (complement) {T = Cudd_Not(T); E = Cudd_Not(E);}

        st_lookup(visited, (const char *)Cudd_Regular(T), (char **)&T_pair);
        if ((Cudd_IsComplement(T) && T_pair->neg == Tcost) ||
        (!Cudd_IsComplement(T) && T_pair->pos == Tcost)) {
            tmp = cuddBddAndRecur(manager,manager->vars[my_dd->index],sol);
            if (tmp == NULL) {
                Cudd_RecursiveDeref(manager,sol);
                return(NULL);
            }
            cuddRef(tmp);
            Cudd_RecursiveDeref(manager,sol);
            sol = tmp;

            complement =  Cudd_IsComplement(T);
            my_dd = Cudd_Regular(T);
            cost = Tcost;
            continue;
Alan Mishchenko committed
1137
        }
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
        st_lookup(visited, (const char *)Cudd_Regular(E), (char **)&E_pair);
        if ((Cudd_IsComplement(E) && E_pair->neg == Ecost) ||
        (!Cudd_IsComplement(E) && E_pair->pos == Ecost)) {
            tmp = cuddBddAndRecur(manager,Cudd_Not(manager->vars[my_dd->index]),sol);
            if (tmp == NULL) {
                Cudd_RecursiveDeref(manager,sol);
                return(NULL);
            }
            cuddRef(tmp);
            Cudd_RecursiveDeref(manager,sol);
            sol = tmp;
            complement = Cudd_IsComplement(E);
            my_dd = Cudd_Regular(E);
            cost = Ecost;
            continue;
Alan Mishchenko committed
1153
        }
1154 1155 1156
        (void) fprintf(manager->err,"We shouldn't be here!!\n");
        manager->errorCode = CUDD_INTERNAL_ERROR;
        return(NULL);
Alan Mishchenko committed
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
    }

    cuddDeref(sol);
    return(sol);

} /* end of getPath */


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

  Synopsis    [Finds the size of the largest cube(s) in a DD.]

  Description [Finds the size of the largest cube(s) in a DD.
  This problem is translated into finding the shortest paths from a node
  when both THEN and ELSE arcs have unit lengths.
  Uses a local symbol table to store the lengths for each
  node. Only the lengths for the regular nodes are entered in the table,
  because those for the complement nodes are simply obtained by swapping
  the two lenghts.
  Returns a pair of lengths: the length of the shortest path to 1;
  and the length of the shortest path to 0. This is done so as to take
  complement arcs into account.]

  SideEffects [none]

  SeeAlso     []

******************************************************************************/
static cuddPathPair
getLargest(
  DdNode * root,
  st_table * visited)
{
    cuddPathPair *my_pair, res_pair, pair_T, pair_E;
1191
    DdNode      *my_root, *T, *E;
Alan Mishchenko committed
1192 1193 1194

    my_root = Cudd_Regular(root);

1195 1196 1197 1198 1199 1200 1201 1202 1203
    if (st_lookup(visited, (const char *)my_root, (char **)&my_pair)) {
        if (Cudd_IsComplement(root)) {
            res_pair.pos = my_pair->neg;
            res_pair.neg = my_pair->pos;
        } else {
            res_pair.pos = my_pair->pos;
            res_pair.neg = my_pair->neg;
        }
        return(res_pair);
Alan Mishchenko committed
1204 1205 1206 1207 1208 1209 1210 1211
    }

    /* In the case of a BDD the following test is equivalent to
    ** testing whether the BDD is the constant 1. This formulation,
    ** however, works for ADDs as well, by assuming the usual
    ** dichotomy of 0 and != 0.
    */
    if (cuddIsConstant(my_root)) {
1212 1213 1214 1215 1216 1217 1218
        if (my_root != zero) {
            res_pair.pos = 0;
            res_pair.neg = DD_BIGGY;
        } else {
            res_pair.pos = DD_BIGGY;
            res_pair.neg = 0;
        }
Alan Mishchenko committed
1219
    } else {
1220 1221
        T = cuddT(my_root);
        E = cuddE(my_root);
Alan Mishchenko committed
1222

1223 1224 1225 1226
        pair_T = getLargest(T, visited);
        pair_E = getLargest(E, visited);
        res_pair.pos = ddMin(pair_T.pos, pair_E.pos) + 1;
        res_pair.neg = ddMin(pair_T.neg, pair_E.neg) + 1;
Alan Mishchenko committed
1227 1228
    }

Alan Mishchenko committed
1229
    my_pair = ABC_ALLOC(cuddPathPair, 1);
1230 1231 1232 1233 1234 1235 1236
    if (my_pair == NULL) {      /* simply do not cache this result */
        if (Cudd_IsComplement(root)) {
            int tmp = res_pair.pos;
            res_pair.pos = res_pair.neg;
            res_pair.neg = tmp;
        }
        return(res_pair);
Alan Mishchenko committed
1237 1238 1239 1240
    }
    my_pair->pos = res_pair.pos;
    my_pair->neg = res_pair.neg;

1241
    /* Caching may fail without affecting correctness. */
Alan Mishchenko committed
1242 1243
    st_insert(visited, (char *)my_root, (char *)my_pair);
    if (Cudd_IsComplement(root)) {
1244 1245
        res_pair.pos = my_pair->neg;
        res_pair.neg = my_pair->pos;
Alan Mishchenko committed
1246
    } else {
1247 1248
        res_pair.pos = my_pair->pos;
        res_pair.neg = my_pair->neg;
Alan Mishchenko committed
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
    }
    return(res_pair);

} /* end of getLargest */


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

  Synopsis    [Build a BDD for a largest cube of f.]

  Description [Build a BDD for a largest cube of f.
  Given the minimum length from the root, and the minimum
  lengths for each node (in visited), apply triangulation at each node.
  Of the two children of each node on a shortest path, at least one is
  on a shortest path. In case of ties the procedure chooses the THEN
  children.
  Returns a pointer to the cube BDD representing the path if
  successful; NULL otherwise.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static DdNode *
getCube(
  DdManager * manager,
  st_table * visited,
  DdNode * f,
  int  cost)
{
1280 1281
    DdNode      *sol, *tmp;
    DdNode      *my_dd, *T, *E;
Alan Mishchenko committed
1282
    cuddPathPair *T_pair, *E_pair;
1283 1284
    int         Tcost, Ecost;
    int         complement;
Alan Mishchenko committed
1285 1286 1287 1288 1289 1290 1291 1292

    my_dd = Cudd_Regular(f);
    complement = Cudd_IsComplement(f);

    sol = one;
    cuddRef(sol);

    while (!cuddIsConstant(my_dd)) {
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
        Tcost = cost - 1;
        Ecost = cost - 1;

        T = cuddT(my_dd);
        E = cuddE(my_dd);

        if (complement) {T = Cudd_Not(T); E = Cudd_Not(E);}

        if (!st_lookup(visited, (const char *)Cudd_Regular(T), (char **)&T_pair)) return(NULL);
        if ((Cudd_IsComplement(T) && T_pair->neg == Tcost) ||
        (!Cudd_IsComplement(T) && T_pair->pos == Tcost)) {
            tmp = cuddBddAndRecur(manager,manager->vars[my_dd->index],sol);
            if (tmp == NULL) {
                Cudd_RecursiveDeref(manager,sol);
                return(NULL);
            }
            cuddRef(tmp);
            Cudd_RecursiveDeref(manager,sol);
            sol = tmp;

            complement =  Cudd_IsComplement(T);
            my_dd = Cudd_Regular(T);
            cost = Tcost;
            continue;
Alan Mishchenko committed
1317
        }
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
        if (!st_lookup(visited, (const char *)Cudd_Regular(E), (char **)&E_pair)) return(NULL);
        if ((Cudd_IsComplement(E) && E_pair->neg == Ecost) ||
        (!Cudd_IsComplement(E) && E_pair->pos == Ecost)) {
            tmp = cuddBddAndRecur(manager,Cudd_Not(manager->vars[my_dd->index]),sol);
            if (tmp == NULL) {
                Cudd_RecursiveDeref(manager,sol);
                return(NULL);
            }
            cuddRef(tmp);
            Cudd_RecursiveDeref(manager,sol);
            sol = tmp;
            complement = Cudd_IsComplement(E);
            my_dd = Cudd_Regular(E);
            cost = Ecost;
            continue;
Alan Mishchenko committed
1333
        }
1334 1335 1336
        (void) fprintf(manager->err,"We shouldn't be here!\n");
        manager->errorCode = CUDD_INTERNAL_ERROR;
        return(NULL);
Alan Mishchenko committed
1337 1338 1339 1340 1341 1342
    }

    cuddDeref(sol);
    return(sol);

} /* end of getCube */
1343 1344


1345 1346
ABC_NAMESPACE_IMPL_END