cuddZddIsop.c 27.3 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 222 223 224 225 226 227 228 229 230 231 232
    } while (dd->reordered == 1);
    return(res);
} /* end of Cudd_MakeBddFromZddCover */


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


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

  Synopsis [Performs the recursive step of Cudd_zddIsop.]

  Description []

  SideEffects [None]

  SeeAlso     [Cudd_zddIsop]

******************************************************************************/
233
DdNode  *
Alan Mishchenko committed
234 235 236 237 238 239
cuddZddIsop(
  DdManager * dd,
  DdNode * L,
  DdNode * U,
  DdNode ** zdd_I)
{
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
    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
255 256 257

    statLine(dd);
    if (L == zero) {
258
        *zdd_I = zdd_zero;
Alan Mishchenko committed
259 260 261
        return(zero);
    }
    if (U == one) {
262
        *zdd_I = zdd_one;
Alan Mishchenko committed
263 264 265 266
        return(one);
    }

    if (U == zero || L == one) {
267 268
        printf("*** ERROR : illegal condition for ISOP (U < L).\n");
        exit(1);
Alan Mishchenko committed
269 270 271 272 273 274 275
    }

    /* 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. */
276
    cacheOp = (DD_CTFP) cuddZddIsop;
Alan Mishchenko committed
277 278
    r = cuddCacheLookup2(dd, cuddBddIsop, L, U);
    if (r) {
279 280 281 282 283 284 285 286 287 288
        *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
289 290 291 292 293 294 295 296
    }

    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) {
297
        index = Cudd_Regular(L)->index;
Alan Mishchenko committed
298 299 300 301 302 303 304 305
        Lv = Cudd_T(L);
        Lnv = Cudd_E(L);
        if (Cudd_IsComplement(L)) {
            Lv = Cudd_Not(Lv);
            Lnv = Cudd_Not(Lnv);
        }
    }
    else {
306
        index = Cudd_Regular(U)->index;
Alan Mishchenko committed
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
        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)
324
        return(NULL);
Alan Mishchenko committed
325 326 327 328
    Cudd_Ref(Lsub0);
    Usub0 = Unv;
    Lsub1 = cuddBddAndRecur(dd, Lv, Cudd_Not(Unv));
    if (Lsub1 == NULL) {
329 330
        Cudd_RecursiveDeref(dd, Lsub0);
        return(NULL);
Alan Mishchenko committed
331 332 333 334 335 336
    }
    Cudd_Ref(Lsub1);
    Usub1 = Uv;

    Isub0 = cuddZddIsop(dd, Lsub0, Usub0, &zdd_Isub0);
    if (Isub0 == NULL) {
337 338 339
        Cudd_RecursiveDeref(dd, Lsub0);
        Cudd_RecursiveDeref(dd, Lsub1);
        return(NULL);
Alan Mishchenko committed
340 341 342
    }
    /*
    if ((!cuddIsConstant(Cudd_Regular(Isub0))) &&
343 344 345
        (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
346 347 348 349 350 351
    }
    */
    Cudd_Ref(Isub0);
    Cudd_Ref(zdd_Isub0);
    Isub1 = cuddZddIsop(dd, Lsub1, Usub1, &zdd_Isub1);
    if (Isub1 == NULL) {
352 353 354 355 356
        Cudd_RecursiveDeref(dd, Lsub0);
        Cudd_RecursiveDeref(dd, Lsub1);
        Cudd_RecursiveDeref(dd, Isub0);
        Cudd_RecursiveDerefZdd(dd, zdd_Isub0);
        return(NULL);
Alan Mishchenko committed
357 358 359
    }
    /*
    if ((!cuddIsConstant(Cudd_Regular(Isub1))) &&
360 361 362
        (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
363 364 365 366 367 368 369 370 371
    }
    */
    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) {
372 373 374 375 376
        Cudd_RecursiveDeref(dd, Isub0);
        Cudd_RecursiveDerefZdd(dd, zdd_Isub0);
        Cudd_RecursiveDeref(dd, Isub1);
        Cudd_RecursiveDerefZdd(dd, zdd_Isub1);
        return(NULL);
Alan Mishchenko committed
377 378 379 380
    }
    Cudd_Ref(Lsuper0);
    Lsuper1 = cuddBddAndRecur(dd, Lv, Cudd_Not(Isub1));
    if (Lsuper1 == NULL) {
381 382 383 384 385 386
        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
387 388 389 390 391 392 393 394
    }
    Cudd_Ref(Lsuper1);
    Usuper0 = Unv;
    Usuper1 = Uv;

    /* Ld = Lsuper0 + Lsuper1 */
    Ld = cuddBddAndRecur(dd, Cudd_Not(Lsuper0), Cudd_Not(Lsuper1));
    if (Ld == NULL) {
395 396 397 398 399 400 401
        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
402 403 404 405 406 407
    }
    Ld = Cudd_Not(Ld);
    Cudd_Ref(Ld);
    /* Ud = Usuper0 * Usuper1 */
    Ud = cuddBddAndRecur(dd, Usuper0, Usuper1);
    if (Ud == NULL) {
408 409 410 411 412 413 414 415
        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
416 417 418 419 420 421 422
    }
    Cudd_Ref(Ud);
    Cudd_RecursiveDeref(dd, Lsuper0);
    Cudd_RecursiveDeref(dd, Lsuper1);

    Id = cuddZddIsop(dd, Ld, Ud, &zdd_Id);
    if (Id == NULL) {
423 424 425 426 427 428 429
        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
430 431 432
    }
    /*
    if ((!cuddIsConstant(Cudd_Regular(Id))) &&
433 434 435
        (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
436 437 438 439 440 441 442 443 444
    }
    */
    Cudd_Ref(Id);
    Cudd_Ref(zdd_Id);
    Cudd_RecursiveDeref(dd, Ld);
    Cudd_RecursiveDeref(dd, Ud);

    x = cuddUniqueInter(dd, index, one, zero);
    if (x == NULL) {
445 446 447 448 449 450 451
        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
452 453 454 455 456
    }
    Cudd_Ref(x);
    /* term0 = x * Isub0 */
    term0 = cuddBddAndRecur(dd, Cudd_Not(x), Isub0);
    if (term0 == NULL) {
457 458 459 460 461 462 463 464
        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
465 466 467 468 469 470
    }
    Cudd_Ref(term0);
    Cudd_RecursiveDeref(dd, Isub0);
    /* term1 = x * Isub1 */
    term1 = cuddBddAndRecur(dd, x, Isub1);
    if (term1 == NULL) {
471 472 473 474 475 476 477 478
        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
479 480 481 482 483 484 485
    }
    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) {
486 487 488 489 490 491 492
        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
493 494 495 496 497 498 499 500 501
    }
    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) {
502 503 504 505 506 507
        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
508 509 510 511 512 513
    }
    Cudd_Ref(r);
    Cudd_RecursiveDeref(dd, sum);
    Cudd_RecursiveDeref(dd, Id);

    if (zdd_Isub0 != zdd_zero) {
514 515 516 517 518 519 520 521
        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
522 523
    }
    else {
524
        z = zdd_Id;
Alan Mishchenko committed
525 526 527
    }
    Cudd_Ref(z);
    if (zdd_Isub1 != zdd_zero) {
528 529 530 531 532 533 534 535 536
        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
537 538
    }
    else
539
        y = z;
Alan Mishchenko committed
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
    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) {
555
        printf("*** ERROR : mismatch in indices between BDD and ZDD. ***\n");
Alan Mishchenko committed
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
    }
    */
    return(r);

} /* end of cuddZddIsop */


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

  Synopsis [Performs the recursive step of Cudd_bddIsop.]

  Description []

  SideEffects [None]

  SeeAlso     [Cudd_bddIsop]

******************************************************************************/
574
DdNode  *
Alan Mishchenko committed
575 576 577 578 579
cuddBddIsop(
  DdManager * dd,
  DdNode * L,
  DdNode * U)
{
580 581 582 583 584 585 586 587 588 589 590
    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
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608

    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) {
609
        index = Cudd_Regular(L)->index;
Alan Mishchenko committed
610 611 612 613 614 615 616 617
        Lv = Cudd_T(L);
        Lnv = Cudd_E(L);
        if (Cudd_IsComplement(L)) {
            Lv = Cudd_Not(Lv);
            Lnv = Cudd_Not(Lnv);
        }
    }
    else {
618
        index = Cudd_Regular(U)->index;
Alan Mishchenko committed
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
        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)
636
        return(NULL);
Alan Mishchenko committed
637 638 639 640
    Cudd_Ref(Lsub0);
    Usub0 = Unv;
    Lsub1 = cuddBddAndRecur(dd, Lv, Cudd_Not(Unv));
    if (Lsub1 == NULL) {
641 642
        Cudd_RecursiveDeref(dd, Lsub0);
        return(NULL);
Alan Mishchenko committed
643 644 645 646 647 648
    }
    Cudd_Ref(Lsub1);
    Usub1 = Uv;

    Isub0 = cuddBddIsop(dd, Lsub0, Usub0);
    if (Isub0 == NULL) {
649 650 651
        Cudd_RecursiveDeref(dd, Lsub0);
        Cudd_RecursiveDeref(dd, Lsub1);
        return(NULL);
Alan Mishchenko committed
652 653 654 655
    }
    Cudd_Ref(Isub0);
    Isub1 = cuddBddIsop(dd, Lsub1, Usub1);
    if (Isub1 == NULL) {
656 657 658 659
        Cudd_RecursiveDeref(dd, Lsub0);
        Cudd_RecursiveDeref(dd, Lsub1);
        Cudd_RecursiveDeref(dd, Isub0);
        return(NULL);
Alan Mishchenko committed
660 661 662 663 664 665 666
    }
    Cudd_Ref(Isub1);
    Cudd_RecursiveDeref(dd, Lsub0);
    Cudd_RecursiveDeref(dd, Lsub1);

    Lsuper0 = cuddBddAndRecur(dd, Lnv, Cudd_Not(Isub0));
    if (Lsuper0 == NULL) {
667 668 669
        Cudd_RecursiveDeref(dd, Isub0);
        Cudd_RecursiveDeref(dd, Isub1);
        return(NULL);
Alan Mishchenko committed
670 671 672 673
    }
    Cudd_Ref(Lsuper0);
    Lsuper1 = cuddBddAndRecur(dd, Lv, Cudd_Not(Isub1));
    if (Lsuper1 == NULL) {
674 675 676 677
        Cudd_RecursiveDeref(dd, Isub0);
        Cudd_RecursiveDeref(dd, Isub1);
        Cudd_RecursiveDeref(dd, Lsuper0);
        return(NULL);
Alan Mishchenko committed
678 679 680 681 682 683 684 685 686
    }
    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) {
687 688 689 690 691
        Cudd_RecursiveDeref(dd, Isub0);
        Cudd_RecursiveDeref(dd, Isub1);
        Cudd_RecursiveDeref(dd, Lsuper0);
        Cudd_RecursiveDeref(dd, Lsuper1);
        return(NULL);
Alan Mishchenko committed
692 693 694 695
    }
    Cudd_Ref(Ld);
    Ud = cuddBddAndRecur(dd, Usuper0, Usuper1);
    if (Ud == NULL) {
696 697 698 699 700 701
        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
702 703 704 705 706 707 708
    }
    Cudd_Ref(Ud);
    Cudd_RecursiveDeref(dd, Lsuper0);
    Cudd_RecursiveDeref(dd, Lsuper1);

    Id = cuddBddIsop(dd, Ld, Ud);
    if (Id == NULL) {
709 710 711 712 713
        Cudd_RecursiveDeref(dd, Isub0);
        Cudd_RecursiveDeref(dd, Isub1);
        Cudd_RecursiveDeref(dd, Ld);
        Cudd_RecursiveDeref(dd, Ud);
        return(NULL);
Alan Mishchenko committed
714 715 716 717 718 719 720
    }
    Cudd_Ref(Id);
    Cudd_RecursiveDeref(dd, Ld);
    Cudd_RecursiveDeref(dd, Ud);

    x = cuddUniqueInter(dd, index, one, zero);
    if (x == NULL) {
721 722 723 724
        Cudd_RecursiveDeref(dd, Isub0);
        Cudd_RecursiveDeref(dd, Isub1);
        Cudd_RecursiveDeref(dd, Id);
        return(NULL);
Alan Mishchenko committed
725 726 727 728
    }
    Cudd_Ref(x);
    term0 = cuddBddAndRecur(dd, Cudd_Not(x), Isub0);
    if (term0 == NULL) {
729 730 731 732 733
        Cudd_RecursiveDeref(dd, Isub0);
        Cudd_RecursiveDeref(dd, Isub1);
        Cudd_RecursiveDeref(dd, Id);
        Cudd_RecursiveDeref(dd, x);
        return(NULL);
Alan Mishchenko committed
734 735 736 737 738
    }
    Cudd_Ref(term0);
    Cudd_RecursiveDeref(dd, Isub0);
    term1 = cuddBddAndRecur(dd, x, Isub1);
    if (term1 == NULL) {
739 740 741 742 743
        Cudd_RecursiveDeref(dd, Isub1);
        Cudd_RecursiveDeref(dd, Id);
        Cudd_RecursiveDeref(dd, x);
        Cudd_RecursiveDeref(dd, term0);
        return(NULL);
Alan Mishchenko committed
744 745 746 747 748 749 750 751
    }
    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) {
752 753 754 755
        Cudd_RecursiveDeref(dd, Id);
        Cudd_RecursiveDeref(dd, term0);
        Cudd_RecursiveDeref(dd, term1);
        return(NULL);
Alan Mishchenko committed
756 757 758 759 760 761 762 763
    }
    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) {
764 765 766
        Cudd_RecursiveDeref(dd, Id);
        Cudd_RecursiveDeref(dd, sum);
        return(NULL);
Alan Mishchenko committed
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
    }
    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]

******************************************************************************/
799
DdNode  *
Alan Mishchenko committed
800 801 802 803
cuddMakeBddFromZddCover(
  DdManager * dd,
  DdNode * node)
{
804 805 806 807 808
    DdNode      *neW;
    int         v;
    DdNode      *f1, *f0, *fd;
    DdNode      *b1, *b0, *bd;
    DdNode      *T, *E;
Alan Mishchenko committed
809 810 811

    statLine(dd);
    if (node == dd->one)
812
        return(dd->one);
Alan Mishchenko committed
813
    if (node == dd->zero)
814
        return(Cudd_Not(dd->one));
Alan Mishchenko committed
815 816 817 818

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

821 822
    v = Cudd_Regular(node)->index;      /* either yi or zi */
    if (cuddZddGetCofactors3(dd, node, v, &f1, &f0, &fd)) return(NULL);
Alan Mishchenko committed
823 824 825 826 827 828
    Cudd_Ref(f1);
    Cudd_Ref(f0);
    Cudd_Ref(fd);

    b1 = cuddMakeBddFromZddCover(dd, f1);
    if (!b1) {
829 830 831 832
        Cudd_RecursiveDerefZdd(dd, f1);
        Cudd_RecursiveDerefZdd(dd, f0);
        Cudd_RecursiveDerefZdd(dd, fd);
        return(NULL);
Alan Mishchenko committed
833 834 835
    }
    Cudd_Ref(b1);
    b0 = cuddMakeBddFromZddCover(dd, f0);
836 837 838 839 840 841
    if (!b0) {
        Cudd_RecursiveDerefZdd(dd, f1);
        Cudd_RecursiveDerefZdd(dd, f0);
        Cudd_RecursiveDerefZdd(dd, fd);
        Cudd_RecursiveDeref(dd, b1);
        return(NULL);
Alan Mishchenko committed
842 843 844 845 846
    }
    Cudd_Ref(b0);
    Cudd_RecursiveDerefZdd(dd, f1);
    Cudd_RecursiveDerefZdd(dd, f0);
    if (fd != dd->zero) {
847 848 849 850 851 852 853 854
        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
855 856
        Cudd_RecursiveDerefZdd(dd, fd);

857 858 859 860 861 862 863 864 865
        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
866
        Cudd_RecursiveDeref(dd, b1);
867 868 869 870 871 872 873 874 875
        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
876 877 878 879
        Cudd_RecursiveDeref(dd, b0);
        Cudd_RecursiveDeref(dd, bd);
    }
    else {
880 881 882
        Cudd_RecursiveDerefZdd(dd, fd);
        T = b1;
        E = b0;
Alan Mishchenko committed
883 884 885
    }

    if (Cudd_IsComplement(T)) {
886 887 888 889 890 891 892
        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
893 894
    }
    else {
895 896 897 898 899 900
        neW = cuddUniqueInterIVO(dd, v / 2, T, E);
        if (!neW) {
            Cudd_RecursiveDeref(dd, T);
            Cudd_RecursiveDeref(dd, E);
            return(NULL);
        }
Alan Mishchenko committed
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
    }
    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                                            */
/*---------------------------------------------------------------------------*/

917

918 919
ABC_NAMESPACE_IMPL_END