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

  FileName    [cuddZddIsop.c]

  PackageName [cudd]

  Synopsis    [Functions to find irredundant SOP covers as ZDDs from BDDs.]

  Description [External procedures included in this module:
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
                    <ul>
                    <li> Cudd_bddIsop()
                    <li> Cudd_zddIsop()
                    <li> Cudd_MakeBddFromZddCover()
                    </ul>
               Internal procedures included in this module:
                    <ul>
                    <li> cuddBddIsop()
                    <li> cuddZddIsop()
                    <li> cuddMakeBddFromZddCover()
                    </ul>
               Static procedures included in this module:
                    <ul>
                    </ul>
              ]
Alan Mishchenko committed
25 26 27 28 29

  SeeAlso     []

  Author      [In-Ho Moon]

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
  Copyright   [Copyright (c) 1995-2004, Regents of the University of Colorado

  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:

  Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.

  Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution.

  Neither the name of the University of Colorado nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  POSSIBILITY OF SUCH DAMAGE.]
Alan Mishchenko committed
61 62 63

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

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

67 68 69
ABC_NAMESPACE_IMPL_START


70

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


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


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


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

#ifndef lint
91
static char rcsid[] DD_UNUSED = "$Id: cuddZddIsop.c,v 1.20 2009/02/19 16:26:12 fabio Exp $";
Alan Mishchenko committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 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
#endif

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


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

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


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

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

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

  Synopsis    [Computes an ISOP in ZDD form from BDDs.]

  Description [Computes an irredundant sum of products (ISOP) in ZDD
  form from BDDs. The two BDDs L and U represent the lower bound and
  the upper bound, respectively, of the function. The ISOP uses two
  ZDD variables for each BDD variable: One for the positive literal,
  and one for the negative literal. These two variables should be
  adjacent in the ZDD order. The two ZDD variables corresponding to
  BDD variable <code>i</code> should have indices <code>2i</code> and
  <code>2i+1</code>.  The result of this procedure depends on the
  variable order. If successful, Cudd_zddIsop returns the BDD for
  the function chosen from the interval. The ZDD representing the
  irredundant cover is returned as a side effect in zdd_I. In case of
  failure, NULL is returned.]

  SideEffects [zdd_I holds the pointer to the ZDD for the ISOP on
  successful return.]

  SeeAlso     [Cudd_bddIsop Cudd_zddVarsFromBddVars]

******************************************************************************/
135
DdNode  *
Alan Mishchenko committed
136 137 138 139 140 141
Cudd_zddIsop(
  DdManager * dd,
  DdNode * L,
  DdNode * U,
  DdNode ** zdd_I)
{
142 143
    DdNode      *res;
    int         autoDynZ;
Alan Mishchenko committed
144 145 146 147 148

    autoDynZ = dd->autoDynZ;
    dd->autoDynZ = 0;

    do {
149 150
        dd->reordered = 0;
        res = cuddZddIsop(dd, L, U, zdd_I);
Alan Mishchenko committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    } while (dd->reordered == 1);
    dd->autoDynZ = autoDynZ;
    return(res);

} /* end of Cudd_zddIsop */


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

  Synopsis    [Computes a BDD in the interval between L and U with a
  simple sum-of-produuct cover.]

  Description [Computes a BDD in the interval between L and U with a
  simple sum-of-produuct cover. This procedure is similar to
  Cudd_zddIsop, but it does not return the ZDD for the cover. Returns
  a pointer to the BDD if successful; NULL otherwise.]

  SideEffects [None]

  SeeAlso     [Cudd_zddIsop]

******************************************************************************/
173
DdNode  *
Alan Mishchenko committed
174 175 176 177 178
Cudd_bddIsop(
  DdManager * dd,
  DdNode * L,
  DdNode * U)
{
179
    DdNode      *res;
Alan Mishchenko committed
180 181

    do {
182 183
        dd->reordered = 0;
        res = cuddBddIsop(dd, L, U);
Alan Mishchenko committed
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    } while (dd->reordered == 1);
    return(res);

} /* end of Cudd_bddIsop */


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

  Synopsis [Converts a ZDD cover to a BDD graph.]

  Description [Converts a ZDD cover to a BDD graph. If successful, it
  returns a BDD node, otherwise it returns NULL.]

  SideEffects []

  SeeAlso     [cuddMakeBddFromZddCover]

******************************************************************************/
202
DdNode  *
Alan Mishchenko committed
203 204 205 206
Cudd_MakeBddFromZddCover(
  DdManager * dd,
  DdNode * node)
{
207
    DdNode      *res;
Alan Mishchenko committed
208 209

    do {
210 211
        dd->reordered = 0;
        res = cuddMakeBddFromZddCover(dd, node);
Alan Mishchenko committed
212 213 214 215 216 217 218 219 220 221
    } while (dd->reordered == 1);
    return(res);
} /* end of Cudd_MakeBddFromZddCover */


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


222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
#ifdef USE_CASH_DUMMY
/**Function********************************************************************

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

******************************************************************************/
static DdNode  *
cuddZddIsop_dummy(DdManager * dd, DdNode * L, DdNode * U)
{
  assert(0);
  return 0;
}
#endif


Alan Mishchenko committed
238 239 240 241 242 243 244 245 246 247 248
/**Function********************************************************************

  Synopsis [Performs the recursive step of Cudd_zddIsop.]

  Description []

  SideEffects [None]

  SeeAlso     [Cudd_zddIsop]

******************************************************************************/
249
DdNode  *
Alan Mishchenko committed
250 251 252 253 254 255
cuddZddIsop(
  DdManager * dd,
  DdNode * L,
  DdNode * U,
  DdNode ** zdd_I)
{
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
    DdNode      *one = DD_ONE(dd);
    DdNode      *zero = Cudd_Not(one);
    DdNode      *zdd_one = DD_ONE(dd);
    DdNode      *zdd_zero = DD_ZERO(dd);
    int         v, top_l, top_u;
    DdNode      *Lsub0, *Usub0, *Lsub1, *Usub1, *Ld, *Ud;
    DdNode      *Lsuper0, *Usuper0, *Lsuper1, *Usuper1;
    DdNode      *Isub0, *Isub1, *Id;
    DdNode      *zdd_Isub0, *zdd_Isub1, *zdd_Id;
    DdNode      *x;
    DdNode      *term0, *term1, *sum;
    DdNode      *Lv, *Uv, *Lnv, *Unv;
    DdNode      *r, *y, *z;
    int         index;
    DD_CTFP     cacheOp;
Alan Mishchenko committed
271 272 273

    statLine(dd);
    if (L == zero) {
274
        *zdd_I = zdd_zero;
Alan Mishchenko committed
275 276 277
        return(zero);
    }
    if (U == one) {
278
        *zdd_I = zdd_one;
Alan Mishchenko committed
279 280 281 282
        return(one);
    }

    if (U == zero || L == one) {
283 284
        printf("*** ERROR : illegal condition for ISOP (U < L).\n");
        exit(1);
Alan Mishchenko committed
285 286 287 288 289 290 291
    }

    /* Check the cache. We store two results for each recursive call.
    ** One is the BDD, and the other is the ZDD. Both are needed.
    ** Hence we need a double hit in the cache to terminate the
    ** recursion. Clearly, collisions may evict only one of the two
    ** results. */
292 293 294
#ifdef USE_CASH_DUMMY
    cacheOp = (DD_CTFP) cuddZddIsop_dummy;
#else
295
    cacheOp = (DD_CTFP) cuddZddIsop;
296
#endif
Alan Mishchenko committed
297 298
    r = cuddCacheLookup2(dd, cuddBddIsop, L, U);
    if (r) {
299 300 301 302 303 304 305 306 307 308
        *zdd_I = cuddCacheLookup2Zdd(dd, cacheOp, L, U);
        if (*zdd_I)
            return(r);
        else {
            /* The BDD result may have been dead. In that case
            ** cuddCacheLookup2 would have called cuddReclaim,
            ** whose effects we now have to undo. */
            cuddRef(r);
            Cudd_RecursiveDeref(dd, r);
        }
Alan Mishchenko committed
309 310 311 312 313 314 315 316
    }

    top_l = dd->perm[Cudd_Regular(L)->index];
    top_u = dd->perm[Cudd_Regular(U)->index];
    v = ddMin(top_l, top_u);

    /* Compute cofactors. */
    if (top_l == v) {
317
        index = Cudd_Regular(L)->index;
Alan Mishchenko committed
318 319 320 321 322 323 324 325
        Lv = Cudd_T(L);
        Lnv = Cudd_E(L);
        if (Cudd_IsComplement(L)) {
            Lv = Cudd_Not(Lv);
            Lnv = Cudd_Not(Lnv);
        }
    }
    else {
326
        index = Cudd_Regular(U)->index;
Alan Mishchenko committed
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
        Lv = Lnv = L;
    }

    if (top_u == v) {
        Uv = Cudd_T(U);
        Unv = Cudd_E(U);
        if (Cudd_IsComplement(U)) {
            Uv = Cudd_Not(Uv);
            Unv = Cudd_Not(Unv);
        }
    }
    else {
        Uv = Unv = U;
    }

    Lsub0 = cuddBddAndRecur(dd, Lnv, Cudd_Not(Uv));
    if (Lsub0 == NULL)
344
        return(NULL);
Alan Mishchenko committed
345 346 347 348
    Cudd_Ref(Lsub0);
    Usub0 = Unv;
    Lsub1 = cuddBddAndRecur(dd, Lv, Cudd_Not(Unv));
    if (Lsub1 == NULL) {
349 350
        Cudd_RecursiveDeref(dd, Lsub0);
        return(NULL);
Alan Mishchenko committed
351 352 353 354 355 356
    }
    Cudd_Ref(Lsub1);
    Usub1 = Uv;

    Isub0 = cuddZddIsop(dd, Lsub0, Usub0, &zdd_Isub0);
    if (Isub0 == NULL) {
357 358 359
        Cudd_RecursiveDeref(dd, Lsub0);
        Cudd_RecursiveDeref(dd, Lsub1);
        return(NULL);
Alan Mishchenko committed
360 361 362
    }
    /*
    if ((!cuddIsConstant(Cudd_Regular(Isub0))) &&
363 364 365
        (Cudd_Regular(Isub0)->index != zdd_Isub0->index / 2 ||
        dd->permZ[index * 2] > dd->permZ[zdd_Isub0->index])) {
        printf("*** ERROR : illegal permutation in ZDD. ***\n");
Alan Mishchenko committed
366 367 368 369 370 371
    }
    */
    Cudd_Ref(Isub0);
    Cudd_Ref(zdd_Isub0);
    Isub1 = cuddZddIsop(dd, Lsub1, Usub1, &zdd_Isub1);
    if (Isub1 == NULL) {
372 373 374 375 376
        Cudd_RecursiveDeref(dd, Lsub0);
        Cudd_RecursiveDeref(dd, Lsub1);
        Cudd_RecursiveDeref(dd, Isub0);
        Cudd_RecursiveDerefZdd(dd, zdd_Isub0);
        return(NULL);
Alan Mishchenko committed
377 378 379
    }
    /*
    if ((!cuddIsConstant(Cudd_Regular(Isub1))) &&
380 381 382
        (Cudd_Regular(Isub1)->index != zdd_Isub1->index / 2 ||
        dd->permZ[index * 2] > dd->permZ[zdd_Isub1->index])) {
        printf("*** ERROR : illegal permutation in ZDD. ***\n");
Alan Mishchenko committed
383 384 385 386 387 388 389 390 391
    }
    */
    Cudd_Ref(Isub1);
    Cudd_Ref(zdd_Isub1);
    Cudd_RecursiveDeref(dd, Lsub0);
    Cudd_RecursiveDeref(dd, Lsub1);

    Lsuper0 = cuddBddAndRecur(dd, Lnv, Cudd_Not(Isub0));
    if (Lsuper0 == NULL) {
392 393 394 395 396
        Cudd_RecursiveDeref(dd, Isub0);
        Cudd_RecursiveDerefZdd(dd, zdd_Isub0);
        Cudd_RecursiveDeref(dd, Isub1);
        Cudd_RecursiveDerefZdd(dd, zdd_Isub1);
        return(NULL);
Alan Mishchenko committed
397 398 399 400
    }
    Cudd_Ref(Lsuper0);
    Lsuper1 = cuddBddAndRecur(dd, Lv, Cudd_Not(Isub1));
    if (Lsuper1 == NULL) {
401 402 403 404 405 406
        Cudd_RecursiveDeref(dd, Isub0);
        Cudd_RecursiveDerefZdd(dd, zdd_Isub0);
        Cudd_RecursiveDeref(dd, Isub1);
        Cudd_RecursiveDerefZdd(dd, zdd_Isub1);
        Cudd_RecursiveDeref(dd, Lsuper0);
        return(NULL);
Alan Mishchenko committed
407 408 409 410 411 412 413 414
    }
    Cudd_Ref(Lsuper1);
    Usuper0 = Unv;
    Usuper1 = Uv;

    /* Ld = Lsuper0 + Lsuper1 */
    Ld = cuddBddAndRecur(dd, Cudd_Not(Lsuper0), Cudd_Not(Lsuper1));
    if (Ld == NULL) {
415 416 417 418 419 420 421
        Cudd_RecursiveDeref(dd, Isub0);
        Cudd_RecursiveDerefZdd(dd, zdd_Isub0);
        Cudd_RecursiveDeref(dd, Isub1);
        Cudd_RecursiveDerefZdd(dd, zdd_Isub1);
        Cudd_RecursiveDeref(dd, Lsuper0);
        Cudd_RecursiveDeref(dd, Lsuper1);
        return(NULL);
Alan Mishchenko committed
422 423 424 425 426 427
    }
    Ld = Cudd_Not(Ld);
    Cudd_Ref(Ld);
    /* Ud = Usuper0 * Usuper1 */
    Ud = cuddBddAndRecur(dd, Usuper0, Usuper1);
    if (Ud == NULL) {
428 429 430 431 432 433 434 435
        Cudd_RecursiveDeref(dd, Isub0);
        Cudd_RecursiveDerefZdd(dd, zdd_Isub0);
        Cudd_RecursiveDeref(dd, Isub1);
        Cudd_RecursiveDerefZdd(dd, zdd_Isub1);
        Cudd_RecursiveDeref(dd, Lsuper0);
        Cudd_RecursiveDeref(dd, Lsuper1);
        Cudd_RecursiveDeref(dd, Ld);
        return(NULL);
Alan Mishchenko committed
436 437 438 439 440 441 442
    }
    Cudd_Ref(Ud);
    Cudd_RecursiveDeref(dd, Lsuper0);
    Cudd_RecursiveDeref(dd, Lsuper1);

    Id = cuddZddIsop(dd, Ld, Ud, &zdd_Id);
    if (Id == NULL) {
443 444 445 446 447 448 449
        Cudd_RecursiveDeref(dd, Isub0);
        Cudd_RecursiveDerefZdd(dd, zdd_Isub0);
        Cudd_RecursiveDeref(dd, Isub1);
        Cudd_RecursiveDerefZdd(dd, zdd_Isub1);
        Cudd_RecursiveDeref(dd, Ld);
        Cudd_RecursiveDeref(dd, Ud);
        return(NULL);
Alan Mishchenko committed
450 451 452
    }
    /*
    if ((!cuddIsConstant(Cudd_Regular(Id))) &&
453 454 455
        (Cudd_Regular(Id)->index != zdd_Id->index / 2 ||
        dd->permZ[index * 2] > dd->permZ[zdd_Id->index])) {
        printf("*** ERROR : illegal permutation in ZDD. ***\n");
Alan Mishchenko committed
456 457 458 459 460 461 462 463 464
    }
    */
    Cudd_Ref(Id);
    Cudd_Ref(zdd_Id);
    Cudd_RecursiveDeref(dd, Ld);
    Cudd_RecursiveDeref(dd, Ud);

    x = cuddUniqueInter(dd, index, one, zero);
    if (x == NULL) {
465 466 467 468 469 470 471
        Cudd_RecursiveDeref(dd, Isub0);
        Cudd_RecursiveDerefZdd(dd, zdd_Isub0);
        Cudd_RecursiveDeref(dd, Isub1);
        Cudd_RecursiveDerefZdd(dd, zdd_Isub1);
        Cudd_RecursiveDeref(dd, Id);
        Cudd_RecursiveDerefZdd(dd, zdd_Id);
        return(NULL);
Alan Mishchenko committed
472 473 474 475 476
    }
    Cudd_Ref(x);
    /* term0 = x * Isub0 */
    term0 = cuddBddAndRecur(dd, Cudd_Not(x), Isub0);
    if (term0 == NULL) {
477 478 479 480 481 482 483 484
        Cudd_RecursiveDeref(dd, Isub0);
        Cudd_RecursiveDerefZdd(dd, zdd_Isub0);
        Cudd_RecursiveDeref(dd, Isub1);
        Cudd_RecursiveDerefZdd(dd, zdd_Isub1);
        Cudd_RecursiveDeref(dd, Id);
        Cudd_RecursiveDerefZdd(dd, zdd_Id);
        Cudd_RecursiveDeref(dd, x);
        return(NULL);
Alan Mishchenko committed
485 486 487 488 489 490
    }
    Cudd_Ref(term0);
    Cudd_RecursiveDeref(dd, Isub0);
    /* term1 = x * Isub1 */
    term1 = cuddBddAndRecur(dd, x, Isub1);
    if (term1 == NULL) {
491 492 493 494 495 496 497 498
        Cudd_RecursiveDerefZdd(dd, zdd_Isub0);
        Cudd_RecursiveDeref(dd, Isub1);
        Cudd_RecursiveDerefZdd(dd, zdd_Isub1);
        Cudd_RecursiveDeref(dd, Id);
        Cudd_RecursiveDerefZdd(dd, zdd_Id);
        Cudd_RecursiveDeref(dd, x);
        Cudd_RecursiveDeref(dd, term0);
        return(NULL);
Alan Mishchenko committed
499 500 501 502 503 504 505
    }
    Cudd_Ref(term1);
    Cudd_RecursiveDeref(dd, x);
    Cudd_RecursiveDeref(dd, Isub1);
    /* sum = term0 + term1 */
    sum = cuddBddAndRecur(dd, Cudd_Not(term0), Cudd_Not(term1));
    if (sum == NULL) {
506 507 508 509 510 511 512
        Cudd_RecursiveDerefZdd(dd, zdd_Isub0);
        Cudd_RecursiveDerefZdd(dd, zdd_Isub1);
        Cudd_RecursiveDeref(dd, Id);
        Cudd_RecursiveDerefZdd(dd, zdd_Id);
        Cudd_RecursiveDeref(dd, term0);
        Cudd_RecursiveDeref(dd, term1);
        return(NULL);
Alan Mishchenko committed
513 514 515 516 517 518 519 520 521
    }
    sum = Cudd_Not(sum);
    Cudd_Ref(sum);
    Cudd_RecursiveDeref(dd, term0);
    Cudd_RecursiveDeref(dd, term1);
    /* r = sum + Id */
    r = cuddBddAndRecur(dd, Cudd_Not(sum), Cudd_Not(Id));
    r = Cudd_NotCond(r, r != NULL);
    if (r == NULL) {
522 523 524 525 526 527
        Cudd_RecursiveDerefZdd(dd, zdd_Isub0);
        Cudd_RecursiveDerefZdd(dd, zdd_Isub1);
        Cudd_RecursiveDeref(dd, Id);
        Cudd_RecursiveDerefZdd(dd, zdd_Id);
        Cudd_RecursiveDeref(dd, sum);
        return(NULL);
Alan Mishchenko committed
528 529 530 531 532 533
    }
    Cudd_Ref(r);
    Cudd_RecursiveDeref(dd, sum);
    Cudd_RecursiveDeref(dd, Id);

    if (zdd_Isub0 != zdd_zero) {
534 535 536 537 538 539 540 541
        z = cuddZddGetNodeIVO(dd, index * 2 + 1, zdd_Isub0, zdd_Id);
        if (z == NULL) {
            Cudd_RecursiveDerefZdd(dd, zdd_Isub0);
            Cudd_RecursiveDerefZdd(dd, zdd_Isub1);
            Cudd_RecursiveDerefZdd(dd, zdd_Id);
            Cudd_RecursiveDeref(dd, r);
            return(NULL);
        }
Alan Mishchenko committed
542 543
    }
    else {
544
        z = zdd_Id;
Alan Mishchenko committed
545 546 547
    }
    Cudd_Ref(z);
    if (zdd_Isub1 != zdd_zero) {
548 549 550 551 552 553 554 555 556
        y = cuddZddGetNodeIVO(dd, index * 2, zdd_Isub1, z);
        if (y == NULL) {
            Cudd_RecursiveDerefZdd(dd, zdd_Isub0);
            Cudd_RecursiveDerefZdd(dd, zdd_Isub1);
            Cudd_RecursiveDerefZdd(dd, zdd_Id);
            Cudd_RecursiveDeref(dd, r);
            Cudd_RecursiveDerefZdd(dd, z);
            return(NULL);
        }
Alan Mishchenko committed
557 558
    }
    else
559
        y = z;
Alan Mishchenko committed
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
    Cudd_Ref(y);

    Cudd_RecursiveDerefZdd(dd, zdd_Isub0);
    Cudd_RecursiveDerefZdd(dd, zdd_Isub1);
    Cudd_RecursiveDerefZdd(dd, zdd_Id);
    Cudd_RecursiveDerefZdd(dd, z);

    cuddCacheInsert2(dd, cuddBddIsop, L, U, r);
    cuddCacheInsert2(dd, cacheOp, L, U, y);

    Cudd_Deref(r);
    Cudd_Deref(y);
    *zdd_I = y;
    /*
    if (Cudd_Regular(r)->index != y->index / 2) {
575
        printf("*** ERROR : mismatch in indices between BDD and ZDD. ***\n");
Alan Mishchenko committed
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
    }
    */
    return(r);

} /* end of cuddZddIsop */


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

  Synopsis [Performs the recursive step of Cudd_bddIsop.]

  Description []

  SideEffects [None]

  SeeAlso     [Cudd_bddIsop]

******************************************************************************/
594
DdNode  *
Alan Mishchenko committed
595 596 597 598 599
cuddBddIsop(
  DdManager * dd,
  DdNode * L,
  DdNode * U)
{
600 601 602 603 604 605 606 607 608 609 610
    DdNode      *one = DD_ONE(dd);
    DdNode      *zero = Cudd_Not(one);
    int         v, top_l, top_u;
    DdNode      *Lsub0, *Usub0, *Lsub1, *Usub1, *Ld, *Ud;
    DdNode      *Lsuper0, *Usuper0, *Lsuper1, *Usuper1;
    DdNode      *Isub0, *Isub1, *Id;
    DdNode      *x;
    DdNode      *term0, *term1, *sum;
    DdNode      *Lv, *Uv, *Lnv, *Unv;
    DdNode      *r;
    int         index;
Alan Mishchenko committed
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628

    statLine(dd);
    if (L == zero)
        return(zero);
    if (U == one)
        return(one);

    /* Check cache */
    r = cuddCacheLookup2(dd, cuddBddIsop, L, U);
    if (r)
        return(r);

    top_l = dd->perm[Cudd_Regular(L)->index];
    top_u = dd->perm[Cudd_Regular(U)->index];
    v = ddMin(top_l, top_u);

    /* Compute cofactors */
    if (top_l == v) {
629
        index = Cudd_Regular(L)->index;
Alan Mishchenko committed
630 631 632 633 634 635 636 637
        Lv = Cudd_T(L);
        Lnv = Cudd_E(L);
        if (Cudd_IsComplement(L)) {
            Lv = Cudd_Not(Lv);
            Lnv = Cudd_Not(Lnv);
        }
    }
    else {
638
        index = Cudd_Regular(U)->index;
Alan Mishchenko committed
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
        Lv = Lnv = L;
    }

    if (top_u == v) {
        Uv = Cudd_T(U);
        Unv = Cudd_E(U);
        if (Cudd_IsComplement(U)) {
            Uv = Cudd_Not(Uv);
            Unv = Cudd_Not(Unv);
        }
    }
    else {
        Uv = Unv = U;
    }

    Lsub0 = cuddBddAndRecur(dd, Lnv, Cudd_Not(Uv));
    if (Lsub0 == NULL)
656
        return(NULL);
Alan Mishchenko committed
657 658 659 660
    Cudd_Ref(Lsub0);
    Usub0 = Unv;
    Lsub1 = cuddBddAndRecur(dd, Lv, Cudd_Not(Unv));
    if (Lsub1 == NULL) {
661 662
        Cudd_RecursiveDeref(dd, Lsub0);
        return(NULL);
Alan Mishchenko committed
663 664 665 666 667 668
    }
    Cudd_Ref(Lsub1);
    Usub1 = Uv;

    Isub0 = cuddBddIsop(dd, Lsub0, Usub0);
    if (Isub0 == NULL) {
669 670 671
        Cudd_RecursiveDeref(dd, Lsub0);
        Cudd_RecursiveDeref(dd, Lsub1);
        return(NULL);
Alan Mishchenko committed
672 673 674 675
    }
    Cudd_Ref(Isub0);
    Isub1 = cuddBddIsop(dd, Lsub1, Usub1);
    if (Isub1 == NULL) {
676 677 678 679
        Cudd_RecursiveDeref(dd, Lsub0);
        Cudd_RecursiveDeref(dd, Lsub1);
        Cudd_RecursiveDeref(dd, Isub0);
        return(NULL);
Alan Mishchenko committed
680 681 682 683 684 685 686
    }
    Cudd_Ref(Isub1);
    Cudd_RecursiveDeref(dd, Lsub0);
    Cudd_RecursiveDeref(dd, Lsub1);

    Lsuper0 = cuddBddAndRecur(dd, Lnv, Cudd_Not(Isub0));
    if (Lsuper0 == NULL) {
687 688 689
        Cudd_RecursiveDeref(dd, Isub0);
        Cudd_RecursiveDeref(dd, Isub1);
        return(NULL);
Alan Mishchenko committed
690 691 692 693
    }
    Cudd_Ref(Lsuper0);
    Lsuper1 = cuddBddAndRecur(dd, Lv, Cudd_Not(Isub1));
    if (Lsuper1 == NULL) {
694 695 696 697
        Cudd_RecursiveDeref(dd, Isub0);
        Cudd_RecursiveDeref(dd, Isub1);
        Cudd_RecursiveDeref(dd, Lsuper0);
        return(NULL);
Alan Mishchenko committed
698 699 700 701 702 703 704 705 706
    }
    Cudd_Ref(Lsuper1);
    Usuper0 = Unv;
    Usuper1 = Uv;

    /* Ld = Lsuper0 + Lsuper1 */
    Ld = cuddBddAndRecur(dd, Cudd_Not(Lsuper0), Cudd_Not(Lsuper1));
    Ld = Cudd_NotCond(Ld, Ld != NULL);
    if (Ld == NULL) {
707 708 709 710 711
        Cudd_RecursiveDeref(dd, Isub0);
        Cudd_RecursiveDeref(dd, Isub1);
        Cudd_RecursiveDeref(dd, Lsuper0);
        Cudd_RecursiveDeref(dd, Lsuper1);
        return(NULL);
Alan Mishchenko committed
712 713 714 715
    }
    Cudd_Ref(Ld);
    Ud = cuddBddAndRecur(dd, Usuper0, Usuper1);
    if (Ud == NULL) {
716 717 718 719 720 721
        Cudd_RecursiveDeref(dd, Isub0);
        Cudd_RecursiveDeref(dd, Isub1);
        Cudd_RecursiveDeref(dd, Lsuper0);
        Cudd_RecursiveDeref(dd, Lsuper1);
        Cudd_RecursiveDeref(dd, Ld);
        return(NULL);
Alan Mishchenko committed
722 723 724 725 726 727 728
    }
    Cudd_Ref(Ud);
    Cudd_RecursiveDeref(dd, Lsuper0);
    Cudd_RecursiveDeref(dd, Lsuper1);

    Id = cuddBddIsop(dd, Ld, Ud);
    if (Id == NULL) {
729 730 731 732 733
        Cudd_RecursiveDeref(dd, Isub0);
        Cudd_RecursiveDeref(dd, Isub1);
        Cudd_RecursiveDeref(dd, Ld);
        Cudd_RecursiveDeref(dd, Ud);
        return(NULL);
Alan Mishchenko committed
734 735 736 737 738 739 740
    }
    Cudd_Ref(Id);
    Cudd_RecursiveDeref(dd, Ld);
    Cudd_RecursiveDeref(dd, Ud);

    x = cuddUniqueInter(dd, index, one, zero);
    if (x == NULL) {
741 742 743 744
        Cudd_RecursiveDeref(dd, Isub0);
        Cudd_RecursiveDeref(dd, Isub1);
        Cudd_RecursiveDeref(dd, Id);
        return(NULL);
Alan Mishchenko committed
745 746 747 748
    }
    Cudd_Ref(x);
    term0 = cuddBddAndRecur(dd, Cudd_Not(x), Isub0);
    if (term0 == NULL) {
749 750 751 752 753
        Cudd_RecursiveDeref(dd, Isub0);
        Cudd_RecursiveDeref(dd, Isub1);
        Cudd_RecursiveDeref(dd, Id);
        Cudd_RecursiveDeref(dd, x);
        return(NULL);
Alan Mishchenko committed
754 755 756 757 758
    }
    Cudd_Ref(term0);
    Cudd_RecursiveDeref(dd, Isub0);
    term1 = cuddBddAndRecur(dd, x, Isub1);
    if (term1 == NULL) {
759 760 761 762 763
        Cudd_RecursiveDeref(dd, Isub1);
        Cudd_RecursiveDeref(dd, Id);
        Cudd_RecursiveDeref(dd, x);
        Cudd_RecursiveDeref(dd, term0);
        return(NULL);
Alan Mishchenko committed
764 765 766 767 768 769 770 771
    }
    Cudd_Ref(term1);
    Cudd_RecursiveDeref(dd, x);
    Cudd_RecursiveDeref(dd, Isub1);
    /* sum = term0 + term1 */
    sum = cuddBddAndRecur(dd, Cudd_Not(term0), Cudd_Not(term1));
    sum = Cudd_NotCond(sum, sum != NULL);
    if (sum == NULL) {
772 773 774 775
        Cudd_RecursiveDeref(dd, Id);
        Cudd_RecursiveDeref(dd, term0);
        Cudd_RecursiveDeref(dd, term1);
        return(NULL);
Alan Mishchenko committed
776 777 778 779 780 781 782 783
    }
    Cudd_Ref(sum);
    Cudd_RecursiveDeref(dd, term0);
    Cudd_RecursiveDeref(dd, term1);
    /* r = sum + Id */
    r = cuddBddAndRecur(dd, Cudd_Not(sum), Cudd_Not(Id));
    r = Cudd_NotCond(r, r != NULL);
    if (r == NULL) {
784 785 786
        Cudd_RecursiveDeref(dd, Id);
        Cudd_RecursiveDeref(dd, sum);
        return(NULL);
Alan Mishchenko committed
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
    }
    Cudd_Ref(r);
    Cudd_RecursiveDeref(dd, sum);
    Cudd_RecursiveDeref(dd, Id);

    cuddCacheInsert2(dd, cuddBddIsop, L, U, r);

    Cudd_Deref(r);
    return(r);

} /* end of cuddBddIsop */


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

  Synopsis [Converts a ZDD cover to a BDD graph.]

  Description [Converts a ZDD cover to a BDD graph. If successful, it
  returns a BDD node, otherwise it returns NULL. It is a recursive
  algorithm as the following. First computes 3 cofactors of a ZDD cover;
  f1, f0 and fd. Second, compute BDDs(b1, b0 and bd) of f1, f0 and fd.
  Third, compute T=b1+bd and E=b0+bd. Fourth, compute ITE(v,T,E) where v
  is the variable which has the index of the top node of the ZDD cover.
  In this case, since the index of v can be larger than either one of T or
  one of E, cuddUniqueInterIVO is called, here IVO stands for
  independent variable ordering.]

  SideEffects []

  SeeAlso     [Cudd_MakeBddFromZddCover]

******************************************************************************/
819
DdNode  *
Alan Mishchenko committed
820 821 822 823
cuddMakeBddFromZddCover(
  DdManager * dd,
  DdNode * node)
{
824 825 826 827 828
    DdNode      *neW;
    int         v;
    DdNode      *f1, *f0, *fd;
    DdNode      *b1, *b0, *bd;
    DdNode      *T, *E;
Alan Mishchenko committed
829 830 831

    statLine(dd);
    if (node == dd->one)
832
        return(dd->one);
Alan Mishchenko committed
833
    if (node == dd->zero)
834
        return(Cudd_Not(dd->one));
Alan Mishchenko committed
835 836 837 838

    /* Check cache */
    neW = cuddCacheLookup1(dd, cuddMakeBddFromZddCover, node);
    if (neW)
839
        return(neW);
Alan Mishchenko committed
840

841 842
    v = Cudd_Regular(node)->index;      /* either yi or zi */
    if (cuddZddGetCofactors3(dd, node, v, &f1, &f0, &fd)) return(NULL);
Alan Mishchenko committed
843 844 845 846 847 848
    Cudd_Ref(f1);
    Cudd_Ref(f0);
    Cudd_Ref(fd);

    b1 = cuddMakeBddFromZddCover(dd, f1);
    if (!b1) {
849 850 851 852
        Cudd_RecursiveDerefZdd(dd, f1);
        Cudd_RecursiveDerefZdd(dd, f0);
        Cudd_RecursiveDerefZdd(dd, fd);
        return(NULL);
Alan Mishchenko committed
853 854 855
    }
    Cudd_Ref(b1);
    b0 = cuddMakeBddFromZddCover(dd, f0);
856 857 858 859 860 861
    if (!b0) {
        Cudd_RecursiveDerefZdd(dd, f1);
        Cudd_RecursiveDerefZdd(dd, f0);
        Cudd_RecursiveDerefZdd(dd, fd);
        Cudd_RecursiveDeref(dd, b1);
        return(NULL);
Alan Mishchenko committed
862 863 864 865 866
    }
    Cudd_Ref(b0);
    Cudd_RecursiveDerefZdd(dd, f1);
    Cudd_RecursiveDerefZdd(dd, f0);
    if (fd != dd->zero) {
867 868 869 870 871 872 873 874
        bd = cuddMakeBddFromZddCover(dd, fd);
        if (!bd) {
            Cudd_RecursiveDerefZdd(dd, fd);
            Cudd_RecursiveDeref(dd, b1);
            Cudd_RecursiveDeref(dd, b0);
            return(NULL);
        }
        Cudd_Ref(bd);
Alan Mishchenko committed
875 876
        Cudd_RecursiveDerefZdd(dd, fd);

877 878 879 880 881 882 883 884 885
        T = cuddBddAndRecur(dd, Cudd_Not(b1), Cudd_Not(bd));
        if (!T) {
            Cudd_RecursiveDeref(dd, b1);
            Cudd_RecursiveDeref(dd, b0);
            Cudd_RecursiveDeref(dd, bd);
            return(NULL);
        }
        T = Cudd_NotCond(T, T != NULL);
        Cudd_Ref(T);
Alan Mishchenko committed
886
        Cudd_RecursiveDeref(dd, b1);
887 888 889 890 891 892 893 894 895
        E = cuddBddAndRecur(dd, Cudd_Not(b0), Cudd_Not(bd));
        if (!E) {
            Cudd_RecursiveDeref(dd, b0);
            Cudd_RecursiveDeref(dd, bd);
            Cudd_RecursiveDeref(dd, T);
            return(NULL);
        }
        E = Cudd_NotCond(E, E != NULL);
        Cudd_Ref(E);
Alan Mishchenko committed
896 897 898 899
        Cudd_RecursiveDeref(dd, b0);
        Cudd_RecursiveDeref(dd, bd);
    }
    else {
900 901 902
        Cudd_RecursiveDerefZdd(dd, fd);
        T = b1;
        E = b0;
Alan Mishchenko committed
903 904 905
    }

    if (Cudd_IsComplement(T)) {
906 907 908 909 910 911 912
        neW = cuddUniqueInterIVO(dd, v / 2, Cudd_Not(T), Cudd_Not(E));
        if (!neW) {
            Cudd_RecursiveDeref(dd, T);
            Cudd_RecursiveDeref(dd, E);
            return(NULL);
        }
        neW = Cudd_Not(neW);
Alan Mishchenko committed
913 914
    }
    else {
915 916 917 918 919 920
        neW = cuddUniqueInterIVO(dd, v / 2, T, E);
        if (!neW) {
            Cudd_RecursiveDeref(dd, T);
            Cudd_RecursiveDeref(dd, E);
            return(NULL);
        }
Alan Mishchenko committed
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
    }
    Cudd_Ref(neW);
    Cudd_RecursiveDeref(dd, T);
    Cudd_RecursiveDeref(dd, E);

    cuddCacheInsert1(dd, cuddMakeBddFromZddCover, node, neW);
    Cudd_Deref(neW);
    return(neW);

} /* end of cuddMakeBddFromZddCover */


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

937

938 939
ABC_NAMESPACE_IMPL_END