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

  FileName    [cuddPriority.c]

  PackageName [cudd]

  Synopsis    [Priority functions.]

  Description [External procedures included in this file:
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
            <ul>
            <li> Cudd_PrioritySelect()
            <li> Cudd_Xgty()
            <li> Cudd_Xeqy()
            <li> Cudd_addXeqy()
            <li> Cudd_Dxygtdxz()
            <li> Cudd_Dxygtdyz()
            <li> Cudd_Inequality()
            <li> Cudd_Disequality()
            <li> Cudd_bddInterval()
            <li> Cudd_CProjection()
            <li> Cudd_addHamming()
            <li> Cudd_MinHammingDist()
            <li> Cudd_bddClosestCube()
            </ul>
        Internal procedures included in this module:
            <ul>
            <li> cuddCProjectionRecur()
            <li> cuddBddClosestCube()
            </ul>
        Static procedures included in this module:
            <ul>
            <li> cuddMinHammingDistRecur()
            <li> separateCube()
            <li> createResult()
            </ul>
            ]
Alan Mishchenko committed
37 38 39 40 41

  SeeAlso     []

  Author      [Fabio Somenzi]

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 69 70 71 72
  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
73 74 75

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

76
#include "misc/util/util_hack.h"
Alan Mishchenko committed
77 78
#include "cuddInt.h"

79 80 81
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
82

83

Alan Mishchenko committed
84 85 86 87
/*---------------------------------------------------------------------------*/
/* Constant declarations                                                     */
/*---------------------------------------------------------------------------*/

88
#define DD_DEBUG 1
Alan Mishchenko committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

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


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


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

#ifndef lint
105
static char rcsid[] DD_UNUSED = "$Id: cuddPriority.c,v 1.33 2009/02/20 02:14:58 fabio Exp $";
Alan Mishchenko committed
106 107 108 109 110 111 112 113 114 115 116 117
#endif

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


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

/*---------------------------------------------------------------------------*/
/* Static function prototypes                                                */
/*---------------------------------------------------------------------------*/
118 119 120
static int cuddMinHammingDistRecur (DdNode * f, int *minterm, DdHashTable * table, int upperBound);
static DdNode * separateCube (DdManager *dd, DdNode *f, CUDD_VALUE_TYPE *distance);
static DdNode * createResult (DdManager *dd, unsigned int index, unsigned int phase, DdNode *cube, CUDD_VALUE_TYPE distance);
Alan Mishchenko committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 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 169 170 171 172 173 174 175 176 177 178 179 180 181 182

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


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


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

  Synopsis    [Selects pairs from R using a priority function.]

  Description [Selects pairs from a relation R(x,y) (given as a BDD)
  in such a way that a given x appears in one pair only. Uses a
  priority function to determine which y should be paired to a given x.
  Cudd_PrioritySelect returns a pointer to
  the selected function if successful; NULL otherwise.
  Three of the arguments--x, y, and z--are vectors of BDD variables.
  The first two are the variables on which R depends. The third vectore
  is a vector of auxiliary variables, used during the computation. This
  vector is optional. If a NULL value is passed instead,
  Cudd_PrioritySelect will create the working variables on the fly.
  The sizes of x and y (and z if it is not NULL) should equal n.
  The priority function Pi can be passed as a BDD, or can be built by
  Cudd_PrioritySelect. If NULL is passed instead of a DdNode *,
  parameter Pifunc is used by Cudd_PrioritySelect to build a BDD for the
  priority function. (Pifunc is a pointer to a C function.) If Pi is not
  NULL, then Pifunc is ignored. Pifunc should have the same interface as
  the standard priority functions (e.g., Cudd_Dxygtdxz).
  Cudd_PrioritySelect and Cudd_CProjection can sometimes be used
  interchangeably. Specifically, calling Cudd_PrioritySelect with
  Cudd_Xgty as Pifunc produces the same result as calling
  Cudd_CProjection with the all-zero minterm as reference minterm.
  However, depending on the application, one or the other may be
  preferable:
  <ul>
  <li> When extracting representatives from an equivalence relation,
  Cudd_CProjection has the advantage of nor requiring the auxiliary
  variables.
  <li> When computing matchings in general bipartite graphs,
  Cudd_PrioritySelect normally obtains better results because it can use
  more powerful matching schemes (e.g., Cudd_Dxygtdxz).
  </ul>
  ]

  SideEffects [If called with z == NULL, will create new variables in
  the manager.]

  SeeAlso     [Cudd_Dxygtdxz Cudd_Dxygtdyz Cudd_Xgty
  Cudd_bddAdjPermuteX Cudd_CProjection]

******************************************************************************/
DdNode *
Cudd_PrioritySelect(
  DdManager * dd /* manager */,
  DdNode * R /* BDD of the relation */,
  DdNode ** x /* array of x variables */,
  DdNode ** y /* array of y variables */,
  DdNode ** z /* array of z variables (optional: may be NULL) */,
  DdNode * Pi /* BDD of the priority function (optional: may be NULL) */,
  int  n /* size of x, y, and z */,
183
  DD_PRFP Pifunc /* function used to build Pi if it is NULL */)
Alan Mishchenko committed
184 185 186 187 188 189 190 191 192 193
{
    DdNode *res = NULL;
    DdNode *zcube = NULL;
    DdNode *Rxz, *Q;
    int createdZ = 0;
    int createdPi = 0;
    int i;

    /* Create z variables if needed. */
    if (z == NULL) {
194 195 196 197 198 199 200 201 202 203 204 205
        if (Pi != NULL) return(NULL);
        z = ABC_ALLOC(DdNode *,n);
        if (z == NULL) {
            dd->errorCode = CUDD_MEMORY_OUT;
            return(NULL);
        }
        createdZ = 1;
        for (i = 0; i < n; i++) {
            if (dd->size >= (int) CUDD_MAXINDEX - 1) goto endgame;
            z[i] = cuddUniqueInter(dd,dd->size,dd->one,Cudd_Not(dd->one));
            if (z[i] == NULL) goto endgame;
        }
Alan Mishchenko committed
206 207 208 209
    }

    /* Create priority function BDD if needed. */
    if (Pi == NULL) {
210 211 212 213
        Pi = Pifunc(dd,n,x,y,z);
        if (Pi == NULL) goto endgame;
        createdPi = 1;
        cuddRef(Pi);
Alan Mishchenko committed
214 215 216 217 218 219
    }

    /* Initialize abstraction cube. */
    zcube = DD_ONE(dd);
    cuddRef(zcube);
    for (i = n - 1; i >= 0; i--) {
220 221 222 223 224 225
        DdNode *tmpp;
        tmpp = Cudd_bddAnd(dd,z[i],zcube);
        if (tmpp == NULL) goto endgame;
        cuddRef(tmpp);
        Cudd_RecursiveDeref(dd,zcube);
        zcube = tmpp;
Alan Mishchenko committed
226 227 228 229 230 231 232 233
    }

    /* Compute subset of (x,y) pairs. */
    Rxz = Cudd_bddSwapVariables(dd,R,y,z,n);
    if (Rxz == NULL) goto endgame;
    cuddRef(Rxz);
    Q = Cudd_bddAndAbstract(dd,Rxz,Pi,zcube);
    if (Q == NULL) {
234 235
        Cudd_RecursiveDeref(dd,Rxz);
        goto endgame;
Alan Mishchenko committed
236 237 238 239 240
    }
    cuddRef(Q);
    Cudd_RecursiveDeref(dd,Rxz);
    res = Cudd_bddAnd(dd,R,Cudd_Not(Q));
    if (res == NULL) {
241 242
        Cudd_RecursiveDeref(dd,Q);
        goto endgame;
Alan Mishchenko committed
243 244 245 246 247 248 249
    }
    cuddRef(res);
    Cudd_RecursiveDeref(dd,Q);

endgame:
    if (zcube != NULL) Cudd_RecursiveDeref(dd,zcube);
    if (createdZ) {
250
        ABC_FREE(z);
Alan Mishchenko committed
251 252
    }
    if (createdPi) {
253
        Cudd_RecursiveDeref(dd,Pi);
Alan Mishchenko committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
    }
    if (res != NULL) cuddDeref(res);
    return(res);

} /* Cudd_PrioritySelect */


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

  Synopsis    [Generates a BDD for the function x &gt; y.]

  Description [This function generates a BDD for the function x &gt; y.
  Both x and y are N-bit numbers, x\[0\] x\[1\] ... x\[N-1\] and
  y\[0\] y\[1\] ...  y\[N-1\], with 0 the most significant bit.
  The BDD is built bottom-up.
269
  It has 3*N-1 internal nodes, if the variables are ordered as follows:
Alan Mishchenko committed
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
  x\[0\] y\[0\] x\[1\] y\[1\] ... x\[N-1\] y\[N-1\].
  Argument z is not used by Cudd_Xgty: it is included to make it
  call-compatible to Cudd_Dxygtdxz and Cudd_Dxygtdyz.]

  SideEffects [None]

  SeeAlso     [Cudd_PrioritySelect Cudd_Dxygtdxz Cudd_Dxygtdyz]

******************************************************************************/
DdNode *
Cudd_Xgty(
  DdManager * dd /* DD manager */,
  int  N /* number of x and y variables */,
  DdNode ** z /* array of z variables: unused */,
  DdNode ** x /* array of x variables */,
  DdNode ** y /* array of y variables */)
{
    DdNode *u, *v, *w;
    int     i;

    /* Build bottom part of BDD outside loop. */
    u = Cudd_bddAnd(dd, x[N-1], Cudd_Not(y[N-1]));
    if (u == NULL) return(NULL);
    cuddRef(u);

    /* Loop to build the rest of the BDD. */
    for (i = N-2; i >= 0; i--) {
297 298 299 300 301 302 303 304 305 306 307 308 309
        v = Cudd_bddAnd(dd, y[i], Cudd_Not(u));
        if (v == NULL) {
            Cudd_RecursiveDeref(dd, u);
            return(NULL);
        }
        cuddRef(v);
        w = Cudd_bddAnd(dd, Cudd_Not(y[i]), u);
        if (w == NULL) {
            Cudd_RecursiveDeref(dd, u);
            Cudd_RecursiveDeref(dd, v);
            return(NULL);
        }
        cuddRef(w);
Alan Mishchenko committed
310
        Cudd_RecursiveDeref(dd, u);
311 312 313 314 315 316 317
        u = Cudd_bddIte(dd, x[i], Cudd_Not(v), w);
        if (u == NULL) {
            Cudd_RecursiveDeref(dd, v);
            Cudd_RecursiveDeref(dd, w);
            return(NULL);
        }
        cuddRef(u);
Alan Mishchenko committed
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
        Cudd_RecursiveDeref(dd, v);
        Cudd_RecursiveDeref(dd, w);

    }
    cuddDeref(u);
    return(u);

} /* end of Cudd_Xgty */


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

  Synopsis    [Generates a BDD for the function x==y.]

  Description [This function generates a BDD for the function x==y.
  Both x and y are N-bit numbers, x\[0\] x\[1\] ... x\[N-1\] and
  y\[0\] y\[1\] ...  y\[N-1\], with 0 the most significant bit.
  The BDD is built bottom-up.
336
  It has 3*N-1 internal nodes, if the variables are ordered as follows:
Alan Mishchenko committed
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
  x\[0\] y\[0\] x\[1\] y\[1\] ... x\[N-1\] y\[N-1\]. ]

  SideEffects [None]

  SeeAlso     [Cudd_addXeqy]

******************************************************************************/
DdNode *
Cudd_Xeqy(
  DdManager * dd /* DD manager */,
  int  N /* number of x and y variables */,
  DdNode ** x /* array of x variables */,
  DdNode ** y /* array of y variables */)
{
    DdNode *u, *v, *w;
    int     i;

    /* Build bottom part of BDD outside loop. */
    u = Cudd_bddIte(dd, x[N-1], y[N-1], Cudd_Not(y[N-1]));
    if (u == NULL) return(NULL);
    cuddRef(u);

    /* Loop to build the rest of the BDD. */
    for (i = N-2; i >= 0; i--) {
361 362 363 364 365 366 367 368 369 370 371 372 373
        v = Cudd_bddAnd(dd, y[i], u);
        if (v == NULL) {
            Cudd_RecursiveDeref(dd, u);
            return(NULL);
        }
        cuddRef(v);
        w = Cudd_bddAnd(dd, Cudd_Not(y[i]), u);
        if (w == NULL) {
            Cudd_RecursiveDeref(dd, u);
            Cudd_RecursiveDeref(dd, v);
            return(NULL);
        }
        cuddRef(w);
Alan Mishchenko committed
374
        Cudd_RecursiveDeref(dd, u);
375 376 377 378 379 380 381
        u = Cudd_bddIte(dd, x[i], v, w);
        if (u == NULL) {
            Cudd_RecursiveDeref(dd, v);
            Cudd_RecursiveDeref(dd, w);
            return(NULL);
        }
        cuddRef(u);
Alan Mishchenko committed
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
        Cudd_RecursiveDeref(dd, v);
        Cudd_RecursiveDeref(dd, w);
    }
    cuddDeref(u);
    return(u);

} /* end of Cudd_Xeqy */


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

  Synopsis    [Generates an ADD for the function x==y.]

  Description [This function generates an ADD for the function x==y.
  Both x and y are N-bit numbers, x\[0\] x\[1\] ... x\[N-1\] and
  y\[0\] y\[1\] ...  y\[N-1\], with 0 the most significant bit.
  The ADD is built bottom-up.
399
  It has 3*N-1 internal nodes, if the variables are ordered as follows:
Alan Mishchenko committed
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
  x\[0\] y\[0\] x\[1\] y\[1\] ... x\[N-1\] y\[N-1\]. ]

  SideEffects [None]

  SeeAlso     [Cudd_Xeqy]

******************************************************************************/
DdNode *
Cudd_addXeqy(
  DdManager * dd /* DD manager */,
  int  N /* number of x and y variables */,
  DdNode ** x /* array of x variables */,
  DdNode ** y /* array of y variables */)
{
    DdNode *one, *zero;
    DdNode *u, *v, *w;
    int     i;

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

    /* Build bottom part of ADD outside loop. */
    v = Cudd_addIte(dd, y[N-1], one, zero);
    if (v == NULL) return(NULL);
    cuddRef(v);
    w = Cudd_addIte(dd, y[N-1], zero, one);
    if (w == NULL) {
427 428
        Cudd_RecursiveDeref(dd, v);
        return(NULL);
Alan Mishchenko committed
429 430 431
    }
    cuddRef(w);
    u = Cudd_addIte(dd, x[N-1], v, w);
432 433 434 435
    if (u == NULL) {
        Cudd_RecursiveDeref(dd, v);
        Cudd_RecursiveDeref(dd, w);
        return(NULL);
Alan Mishchenko committed
436 437 438 439 440 441 442
    }
    cuddRef(u);
    Cudd_RecursiveDeref(dd, v);
    Cudd_RecursiveDeref(dd, w);

    /* Loop to build the rest of the ADD. */
    for (i = N-2; i >= 0; i--) {
443 444 445 446 447 448 449 450 451 452 453 454 455
        v = Cudd_addIte(dd, y[i], u, zero);
        if (v == NULL) {
            Cudd_RecursiveDeref(dd, u);
            return(NULL);
        }
        cuddRef(v);
        w = Cudd_addIte(dd, y[i], zero, u);
        if (w == NULL) {
            Cudd_RecursiveDeref(dd, u);
            Cudd_RecursiveDeref(dd, v);
            return(NULL);
        }
        cuddRef(w);
Alan Mishchenko committed
456
        Cudd_RecursiveDeref(dd, u);
457 458 459 460 461 462 463
        u = Cudd_addIte(dd, x[i], v, w);
        if (w == NULL) {
            Cudd_RecursiveDeref(dd, v);
            Cudd_RecursiveDeref(dd, w);
            return(NULL);
        }
        cuddRef(u);
Alan Mishchenko committed
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
        Cudd_RecursiveDeref(dd, v);
        Cudd_RecursiveDeref(dd, w);
    }
    cuddDeref(u);
    return(u);

} /* end of Cudd_addXeqy */


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

  Synopsis    [Generates a BDD for the function d(x,y) &gt; d(x,z).]

  Description [This function generates a BDD for the function d(x,y)
  &gt; d(x,z);
  x, y, and z are N-bit numbers, x\[0\] x\[1\] ... x\[N-1\],
  y\[0\] y\[1\] ...  y\[N-1\], and z\[0\] z\[1\] ...  z\[N-1\],
  with 0 the most significant bit.
  The distance d(x,y) is defined as:
483
        \sum_{i=0}^{N-1}(|x_i - y_i| \cdot 2^{N-i-1}).
Alan Mishchenko committed
484
  The BDD is built bottom-up.
485
  It has 7*N-3 internal nodes, if the variables are ordered as follows:
Alan Mishchenko committed
486 487 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
  x\[0\] y\[0\] z\[0\] x\[1\] y\[1\] z\[1\] ... x\[N-1\] y\[N-1\] z\[N-1\]. ]

  SideEffects [None]

  SeeAlso     [Cudd_PrioritySelect Cudd_Dxygtdyz Cudd_Xgty Cudd_bddAdjPermuteX]

******************************************************************************/
DdNode *
Cudd_Dxygtdxz(
  DdManager * dd /* DD manager */,
  int  N /* number of x, y, and z variables */,
  DdNode ** x /* array of x variables */,
  DdNode ** y /* array of y variables */,
  DdNode ** z /* array of z variables */)
{
    DdNode *one, *zero;
    DdNode *z1, *z2, *z3, *z4, *y1_, *y2, *x1;
    int     i;

    one = DD_ONE(dd);
    zero = Cudd_Not(one);

    /* Build bottom part of BDD outside loop. */
    y1_ = Cudd_bddIte(dd, y[N-1], one, Cudd_Not(z[N-1]));
    if (y1_ == NULL) return(NULL);
    cuddRef(y1_);
    y2 = Cudd_bddIte(dd, y[N-1], z[N-1], one);
    if (y2 == NULL) {
514 515
        Cudd_RecursiveDeref(dd, y1_);
        return(NULL);
Alan Mishchenko committed
516 517 518 519
    }
    cuddRef(y2);
    x1 = Cudd_bddIte(dd, x[N-1], y1_, y2);
    if (x1 == NULL) {
520 521 522
        Cudd_RecursiveDeref(dd, y1_);
        Cudd_RecursiveDeref(dd, y2);
        return(NULL);
Alan Mishchenko committed
523 524 525 526 527 528 529
    }
    cuddRef(x1);
    Cudd_RecursiveDeref(dd, y1_);
    Cudd_RecursiveDeref(dd, y2);

    /* Loop to build the rest of the BDD. */
    for (i = N-2; i >= 0; i--) {
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
        z1 = Cudd_bddIte(dd, z[i], one, Cudd_Not(x1));
        if (z1 == NULL) {
            Cudd_RecursiveDeref(dd, x1);
            return(NULL);
        }
        cuddRef(z1);
        z2 = Cudd_bddIte(dd, z[i], x1, one);
        if (z2 == NULL) {
            Cudd_RecursiveDeref(dd, x1);
            Cudd_RecursiveDeref(dd, z1);
            return(NULL);
        }
        cuddRef(z2);
        z3 = Cudd_bddIte(dd, z[i], one, x1);
        if (z3 == NULL) {
            Cudd_RecursiveDeref(dd, x1);
            Cudd_RecursiveDeref(dd, z1);
            Cudd_RecursiveDeref(dd, z2);
            return(NULL);
        }
        cuddRef(z3);
        z4 = Cudd_bddIte(dd, z[i], x1, zero);
        if (z4 == NULL) {
            Cudd_RecursiveDeref(dd, x1);
            Cudd_RecursiveDeref(dd, z1);
            Cudd_RecursiveDeref(dd, z2);
            Cudd_RecursiveDeref(dd, z3);
            return(NULL);
        }
        cuddRef(z4);
Alan Mishchenko committed
560
        Cudd_RecursiveDeref(dd, x1);
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
        y1_ = Cudd_bddIte(dd, y[i], z2, Cudd_Not(z1));
        if (y1_ == NULL) {
            Cudd_RecursiveDeref(dd, z1);
            Cudd_RecursiveDeref(dd, z2);
            Cudd_RecursiveDeref(dd, z3);
            Cudd_RecursiveDeref(dd, z4);
            return(NULL);
        }
        cuddRef(y1_);
        y2 = Cudd_bddIte(dd, y[i], z4, z3);
        if (y2 == NULL) {
            Cudd_RecursiveDeref(dd, z1);
            Cudd_RecursiveDeref(dd, z2);
            Cudd_RecursiveDeref(dd, z3);
            Cudd_RecursiveDeref(dd, z4);
            Cudd_RecursiveDeref(dd, y1_);
            return(NULL);
        }
        cuddRef(y2);
Alan Mishchenko committed
580 581 582 583
        Cudd_RecursiveDeref(dd, z1);
        Cudd_RecursiveDeref(dd, z2);
        Cudd_RecursiveDeref(dd, z3);
        Cudd_RecursiveDeref(dd, z4);
584 585 586 587 588 589 590
        x1 = Cudd_bddIte(dd, x[i], y1_, y2);
        if (x1 == NULL) {
            Cudd_RecursiveDeref(dd, y1_);
            Cudd_RecursiveDeref(dd, y2);
            return(NULL);
        }
        cuddRef(x1);
Alan Mishchenko committed
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
        Cudd_RecursiveDeref(dd, y1_);
        Cudd_RecursiveDeref(dd, y2);
    }
    cuddDeref(x1);
    return(Cudd_Not(x1));

} /* end of Cudd_Dxygtdxz */


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

  Synopsis    [Generates a BDD for the function d(x,y) &gt; d(y,z).]

  Description [This function generates a BDD for the function d(x,y)
  &gt; d(y,z);
  x, y, and z are N-bit numbers, x\[0\] x\[1\] ... x\[N-1\],
  y\[0\] y\[1\] ...  y\[N-1\], and z\[0\] z\[1\] ...  z\[N-1\],
  with 0 the most significant bit.
  The distance d(x,y) is defined as:
610
        \sum_{i=0}^{N-1}(|x_i - y_i| \cdot 2^{N-i-1}).
Alan Mishchenko committed
611
  The BDD is built bottom-up.
612
  It has 7*N-3 internal nodes, if the variables are ordered as follows:
Alan Mishchenko committed
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
  x\[0\] y\[0\] z\[0\] x\[1\] y\[1\] z\[1\] ... x\[N-1\] y\[N-1\] z\[N-1\]. ]

  SideEffects [None]

  SeeAlso     [Cudd_PrioritySelect Cudd_Dxygtdxz Cudd_Xgty Cudd_bddAdjPermuteX]

******************************************************************************/
DdNode *
Cudd_Dxygtdyz(
  DdManager * dd /* DD manager */,
  int  N /* number of x, y, and z variables */,
  DdNode ** x /* array of x variables */,
  DdNode ** y /* array of y variables */,
  DdNode ** z /* array of z variables */)
{
    DdNode *one, *zero;
    DdNode *z1, *z2, *z3, *z4, *y1_, *y2, *x1;
    int     i;

    one = DD_ONE(dd);
    zero = Cudd_Not(one);

    /* Build bottom part of BDD outside loop. */
    y1_ = Cudd_bddIte(dd, y[N-1], one, z[N-1]);
    if (y1_ == NULL) return(NULL);
    cuddRef(y1_);
    y2 = Cudd_bddIte(dd, y[N-1], z[N-1], zero);
    if (y2 == NULL) {
641 642
        Cudd_RecursiveDeref(dd, y1_);
        return(NULL);
Alan Mishchenko committed
643 644 645 646
    }
    cuddRef(y2);
    x1 = Cudd_bddIte(dd, x[N-1], y1_, Cudd_Not(y2));
    if (x1 == NULL) {
647 648 649
        Cudd_RecursiveDeref(dd, y1_);
        Cudd_RecursiveDeref(dd, y2);
        return(NULL);
Alan Mishchenko committed
650 651 652 653 654 655 656
    }
    cuddRef(x1);
    Cudd_RecursiveDeref(dd, y1_);
    Cudd_RecursiveDeref(dd, y2);

    /* Loop to build the rest of the BDD. */
    for (i = N-2; i >= 0; i--) {
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
        z1 = Cudd_bddIte(dd, z[i], x1, zero);
        if (z1 == NULL) {
            Cudd_RecursiveDeref(dd, x1);
            return(NULL);
        }
        cuddRef(z1);
        z2 = Cudd_bddIte(dd, z[i], x1, one);
        if (z2 == NULL) {
            Cudd_RecursiveDeref(dd, x1);
            Cudd_RecursiveDeref(dd, z1);
            return(NULL);
        }
        cuddRef(z2);
        z3 = Cudd_bddIte(dd, z[i], one, x1);
        if (z3 == NULL) {
            Cudd_RecursiveDeref(dd, x1);
            Cudd_RecursiveDeref(dd, z1);
            Cudd_RecursiveDeref(dd, z2);
            return(NULL);
        }
        cuddRef(z3);
        z4 = Cudd_bddIte(dd, z[i], one, Cudd_Not(x1));
        if (z4 == NULL) {
            Cudd_RecursiveDeref(dd, x1);
            Cudd_RecursiveDeref(dd, z1);
            Cudd_RecursiveDeref(dd, z2);
            Cudd_RecursiveDeref(dd, z3);
            return(NULL);
        }
        cuddRef(z4);
Alan Mishchenko committed
687
        Cudd_RecursiveDeref(dd, x1);
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
        y1_ = Cudd_bddIte(dd, y[i], z2, z1);
        if (y1_ == NULL) {
            Cudd_RecursiveDeref(dd, z1);
            Cudd_RecursiveDeref(dd, z2);
            Cudd_RecursiveDeref(dd, z3);
            Cudd_RecursiveDeref(dd, z4);
            return(NULL);
        }
        cuddRef(y1_);
        y2 = Cudd_bddIte(dd, y[i], z4, Cudd_Not(z3));
        if (y2 == NULL) {
            Cudd_RecursiveDeref(dd, z1);
            Cudd_RecursiveDeref(dd, z2);
            Cudd_RecursiveDeref(dd, z3);
            Cudd_RecursiveDeref(dd, z4);
            Cudd_RecursiveDeref(dd, y1_);
            return(NULL);
        }
        cuddRef(y2);
Alan Mishchenko committed
707 708 709 710
        Cudd_RecursiveDeref(dd, z1);
        Cudd_RecursiveDeref(dd, z2);
        Cudd_RecursiveDeref(dd, z3);
        Cudd_RecursiveDeref(dd, z4);
711 712 713 714 715 716 717
        x1 = Cudd_bddIte(dd, x[i], y1_, Cudd_Not(y2));
        if (x1 == NULL) {
            Cudd_RecursiveDeref(dd, y1_);
            Cudd_RecursiveDeref(dd, y2);
            return(NULL);
        }
        cuddRef(x1);
Alan Mishchenko committed
718 719 720 721 722 723 724 725 726 727 728
        Cudd_RecursiveDeref(dd, y1_);
        Cudd_RecursiveDeref(dd, y2);
    }
    cuddDeref(x1);
    return(Cudd_Not(x1));

} /* end of Cudd_Dxygtdyz */


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

729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 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
  Synopsis    [Generates a BDD for the function x - y &ge; c.]

  Description [This function generates a BDD for the function x -y &ge; c.
  Both x and y are N-bit numbers, x\[0\] x\[1\] ... x\[N-1\] and
  y\[0\] y\[1\] ...  y\[N-1\], with 0 the most significant bit.
  The BDD is built bottom-up.
  It has a linear number of nodes if the variables are ordered as follows:
  x\[0\] y\[0\] x\[1\] y\[1\] ... x\[N-1\] y\[N-1\].]

  SideEffects [None]

  SeeAlso     [Cudd_Xgty]

******************************************************************************/
DdNode *
Cudd_Inequality(
  DdManager * dd /* DD manager */,
  int  N /* number of x and y variables */,
  int c /* right-hand side constant */,
  DdNode ** x /* array of x variables */,
  DdNode ** y /* array of y variables */)
{
    /* The nodes at level i represent values of the difference that are
    ** multiples of 2^i.  We use variables with names starting with k
    ** to denote the multipliers of 2^i in such multiples. */
    int kTrue = c;
    int kFalse = c - 1;
    /* Mask used to compute the ceiling function.  Since we divide by 2^i,
    ** we want to know whether the dividend is a multiple of 2^i.  If it is,
    ** then ceiling and floor coincide; otherwise, they differ by one. */
    int mask = 1;
    int i;

    DdNode *f = NULL;           /* the eventual result */
    DdNode *one = DD_ONE(dd);
    DdNode *zero = Cudd_Not(one);

    /* Two x-labeled nodes are created at most at each iteration.  They are
    ** stored, along with their k values, in these variables.  At each level,
    ** the old nodes are freed and the new nodes are copied into the old map.
    */
    DdNode *map[2] = {0};
    int invalidIndex = 1 << (N-1);
    int index[2] = {invalidIndex, invalidIndex};

    /* This should never happen. */
    if (N < 0) return(NULL);

    /* If there are no bits, both operands are 0.  The result depends on c. */
    if (N == 0) {
        if (c >= 0) return(one);
        else return(zero);
    }

    /* The maximum or the minimum difference comparing to c can generate the terminal case */
    if ((1 << N) - 1 < c) return(zero);
    else if ((-(1 << N) + 1) >= c) return(one);

    /* Build the result bottom up. */
    for (i = 1; i <= N; i++) {
        int kTrueLower, kFalseLower;
        int leftChild, middleChild, rightChild;
        DdNode *g0, *g1, *fplus, *fequal, *fminus;
        int j;
793
        DdNode *newMap[2] = {NULL};
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 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 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 874 875 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 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 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 972 973 974 975 976 977 978 979 980
        int newIndex[2];

        kTrueLower = kTrue;
        kFalseLower = kFalse;
        /* kTrue = ceiling((c-1)/2^i) + 1 */
        kTrue = ((c-1) >> i) + ((c & mask) != 1) + 1;
        mask = (mask << 1) | 1;
        /* kFalse = floor(c/2^i) - 1 */
        kFalse = (c >> i) - 1;
        newIndex[0] = invalidIndex;
        newIndex[1] = invalidIndex;

        for (j = kFalse + 1; j < kTrue; j++) {
            /* Skip if node is not reachable from top of BDD. */
            if ((j >= (1 << (N - i))) || (j <= -(1 << (N -i)))) continue;

            /* Find f- */
            leftChild = (j << 1) - 1;
            if (leftChild >= kTrueLower) {
                fminus = one;
            } else if (leftChild <= kFalseLower) {
                fminus = zero;
            } else {
                assert(leftChild == index[0] || leftChild == index[1]);
                if (leftChild == index[0]) {
                    fminus = map[0];
                } else {
                    fminus = map[1];
                }
            }

            /* Find f= */
            middleChild = j << 1;
            if (middleChild >= kTrueLower) {
                fequal = one;
            } else if (middleChild <= kFalseLower) {
                fequal = zero;
            } else {
                assert(middleChild == index[0] || middleChild == index[1]);
                if (middleChild == index[0]) {
                    fequal = map[0];
                } else {
                    fequal = map[1];
                }
            }

            /* Find f+ */
            rightChild = (j << 1) + 1;
            if (rightChild >= kTrueLower) {
                fplus = one;
            } else if (rightChild <= kFalseLower) {
                fplus = zero;
            } else {
                assert(rightChild == index[0] || rightChild == index[1]);
                if (rightChild == index[0]) {
                    fplus = map[0];
                } else {
                    fplus = map[1];
                }
            }

            /* Build new nodes. */
            g1 = Cudd_bddIte(dd, y[N - i], fequal, fplus);
            if (g1 == NULL) {
                if (index[0] != invalidIndex) Cudd_IterDerefBdd(dd, map[0]);
                if (index[1] != invalidIndex) Cudd_IterDerefBdd(dd, map[1]);
                if (newIndex[0] != invalidIndex) Cudd_IterDerefBdd(dd, newMap[0]);
                if (newIndex[1] != invalidIndex) Cudd_IterDerefBdd(dd, newMap[1]);
                return(NULL);
            }
            cuddRef(g1);
            g0 = Cudd_bddIte(dd, y[N - i], fminus, fequal);
            if (g0 == NULL) {
                Cudd_IterDerefBdd(dd, g1);
                if (index[0] != invalidIndex) Cudd_IterDerefBdd(dd, map[0]);
                if (index[1] != invalidIndex) Cudd_IterDerefBdd(dd, map[1]);
                if (newIndex[0] != invalidIndex) Cudd_IterDerefBdd(dd, newMap[0]);
                if (newIndex[1] != invalidIndex) Cudd_IterDerefBdd(dd, newMap[1]);
                return(NULL);
            }
            cuddRef(g0);
            f = Cudd_bddIte(dd, x[N - i], g1, g0);
            if (f == NULL) {
                Cudd_IterDerefBdd(dd, g1);
                Cudd_IterDerefBdd(dd, g0);
                if (index[0] != invalidIndex) Cudd_IterDerefBdd(dd, map[0]);
                if (index[1] != invalidIndex) Cudd_IterDerefBdd(dd, map[1]);
                if (newIndex[0] != invalidIndex) Cudd_IterDerefBdd(dd, newMap[0]);
                if (newIndex[1] != invalidIndex) Cudd_IterDerefBdd(dd, newMap[1]);
                return(NULL);
            }
            cuddRef(f);
            Cudd_IterDerefBdd(dd, g1);
            Cudd_IterDerefBdd(dd, g0);

            /* Save newly computed node in map. */
            assert(newIndex[0] == invalidIndex || newIndex[1] == invalidIndex);
            if (newIndex[0] == invalidIndex) {
                newIndex[0] = j;
                newMap[0] = f;
            } else {
                newIndex[1] = j;
                newMap[1] = f;
            }
        }

        /* Copy new map to map. */
        if (index[0] != invalidIndex) Cudd_IterDerefBdd(dd, map[0]);
        if (index[1] != invalidIndex) Cudd_IterDerefBdd(dd, map[1]);
        map[0] = newMap[0];
        map[1] = newMap[1];
        index[0] = newIndex[0];
        index[1] = newIndex[1];
    }

    cuddDeref(f);
    return(f);

} /* end of Cudd_Inequality */


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

  Synopsis    [Generates a BDD for the function x - y != c.]

  Description [This function generates a BDD for the function x -y != c.
  Both x and y are N-bit numbers, x\[0\] x\[1\] ... x\[N-1\] and
  y\[0\] y\[1\] ...  y\[N-1\], with 0 the most significant bit.
  The BDD is built bottom-up.
  It has a linear number of nodes if the variables are ordered as follows:
  x\[0\] y\[0\] x\[1\] y\[1\] ... x\[N-1\] y\[N-1\].]

  SideEffects [None]

  SeeAlso     [Cudd_Xgty]

******************************************************************************/
DdNode *
Cudd_Disequality(
  DdManager * dd /* DD manager */,
  int  N /* number of x and y variables */,
  int c /* right-hand side constant */,
  DdNode ** x /* array of x variables */,
  DdNode ** y /* array of y variables */)
{
    /* The nodes at level i represent values of the difference that are
    ** multiples of 2^i.  We use variables with names starting with k
    ** to denote the multipliers of 2^i in such multiples. */
    int kTrueLb = c + 1;
    int kTrueUb = c - 1;
    int kFalse = c;
    /* Mask used to compute the ceiling function.  Since we divide by 2^i,
    ** we want to know whether the dividend is a multiple of 2^i.  If it is,
    ** then ceiling and floor coincide; otherwise, they differ by one. */
    int mask = 1;
    int i;

    DdNode *f = NULL;           /* the eventual result */
    DdNode *one = DD_ONE(dd);
    DdNode *zero = Cudd_Not(one);

    /* Two x-labeled nodes are created at most at each iteration.  They are
    ** stored, along with their k values, in these variables.  At each level,
    ** the old nodes are freed and the new nodes are copied into the old map.
    */
    DdNode *map[2] = {0};
    int invalidIndex = 1 << (N-1);
    int index[2] = {invalidIndex, invalidIndex};

    /* This should never happen. */
    if (N < 0) return(NULL);

    /* If there are no bits, both operands are 0.  The result depends on c. */
    if (N == 0) {
        if (c != 0) return(one);
        else return(zero);
    }

    /* The maximum or the minimum difference comparing to c can generate the terminal case */
    if ((1 << N) - 1 < c || (-(1 << N) + 1) > c) return(one);

    /* Build the result bottom up. */
    for (i = 1; i <= N; i++) {
        int kTrueLbLower, kTrueUbLower;
        int leftChild, middleChild, rightChild;
        DdNode *g0, *g1, *fplus, *fequal, *fminus;
        int j;
981
        DdNode *newMap[2] = {NULL};
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 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 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 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
        int newIndex[2];

        kTrueLbLower = kTrueLb;
        kTrueUbLower = kTrueUb;
        /* kTrueLb = floor((c-1)/2^i) + 2 */
        kTrueLb = ((c-1) >> i) + 2;
        /* kTrueUb = ceiling((c+1)/2^i) - 2 */
        kTrueUb = ((c+1) >> i) + (((c+2) & mask) != 1) - 2;
        mask = (mask << 1) | 1;
        newIndex[0] = invalidIndex;
        newIndex[1] = invalidIndex;

        for (j = kTrueUb + 1; j < kTrueLb; j++) {
            /* Skip if node is not reachable from top of BDD. */
            if ((j >= (1 << (N - i))) || (j <= -(1 << (N -i)))) continue;

            /* Find f- */
            leftChild = (j << 1) - 1;
            if (leftChild >= kTrueLbLower || leftChild <= kTrueUbLower) {
                fminus = one;
            } else if (i == 1 && leftChild == kFalse) {
                fminus = zero;
            } else {
                assert(leftChild == index[0] || leftChild == index[1]);
                if (leftChild == index[0]) {
                    fminus = map[0];
                } else {
                    fminus = map[1];
                }
            }

            /* Find f= */
            middleChild = j << 1;
            if (middleChild >= kTrueLbLower || middleChild <= kTrueUbLower) {
                fequal = one;
            } else if (i == 1 && middleChild == kFalse) {
                fequal = zero;
            } else {
                assert(middleChild == index[0] || middleChild == index[1]);
                if (middleChild == index[0]) {
                    fequal = map[0];
                } else {
                    fequal = map[1];
                }
            }

            /* Find f+ */
            rightChild = (j << 1) + 1;
            if (rightChild >= kTrueLbLower || rightChild <= kTrueUbLower) {
                fplus = one;
            } else if (i == 1 && rightChild == kFalse) {
                fplus = zero;
            } else {
                assert(rightChild == index[0] || rightChild == index[1]);
                if (rightChild == index[0]) {
                    fplus = map[0];
                } else {
                    fplus = map[1];
                }
            }

            /* Build new nodes. */
            g1 = Cudd_bddIte(dd, y[N - i], fequal, fplus);
            if (g1 == NULL) {
                if (index[0] != invalidIndex) Cudd_IterDerefBdd(dd, map[0]);
                if (index[1] != invalidIndex) Cudd_IterDerefBdd(dd, map[1]);
                if (newIndex[0] != invalidIndex) Cudd_IterDerefBdd(dd, newMap[0]);
                if (newIndex[1] != invalidIndex) Cudd_IterDerefBdd(dd, newMap[1]);
                return(NULL);
            }
            cuddRef(g1);
            g0 = Cudd_bddIte(dd, y[N - i], fminus, fequal);
            if (g0 == NULL) {
                Cudd_IterDerefBdd(dd, g1);
                if (index[0] != invalidIndex) Cudd_IterDerefBdd(dd, map[0]);
                if (index[1] != invalidIndex) Cudd_IterDerefBdd(dd, map[1]);
                if (newIndex[0] != invalidIndex) Cudd_IterDerefBdd(dd, newMap[0]);
                if (newIndex[1] != invalidIndex) Cudd_IterDerefBdd(dd, newMap[1]);
                return(NULL);
            }
            cuddRef(g0);
            f = Cudd_bddIte(dd, x[N - i], g1, g0);
            if (f == NULL) {
                Cudd_IterDerefBdd(dd, g1);
                Cudd_IterDerefBdd(dd, g0);
                if (index[0] != invalidIndex) Cudd_IterDerefBdd(dd, map[0]);
                if (index[1] != invalidIndex) Cudd_IterDerefBdd(dd, map[1]);
                if (newIndex[0] != invalidIndex) Cudd_IterDerefBdd(dd, newMap[0]);
                if (newIndex[1] != invalidIndex) Cudd_IterDerefBdd(dd, newMap[1]);
                return(NULL);
            }
            cuddRef(f);
            Cudd_IterDerefBdd(dd, g1);
            Cudd_IterDerefBdd(dd, g0);

            /* Save newly computed node in map. */
            assert(newIndex[0] == invalidIndex || newIndex[1] == invalidIndex);
            if (newIndex[0] == invalidIndex) {
                newIndex[0] = j;
                newMap[0] = f;
            } else {
                newIndex[1] = j;
                newMap[1] = f;
            }
        }

        /* Copy new map to map. */
        if (index[0] != invalidIndex) Cudd_IterDerefBdd(dd, map[0]);
        if (index[1] != invalidIndex) Cudd_IterDerefBdd(dd, map[1]);
        map[0] = newMap[0];
        map[1] = newMap[1];
        index[0] = newIndex[0];
        index[1] = newIndex[1];
    }

    cuddDeref(f);
    return(f);

} /* end of Cudd_Disequality */


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

  Synopsis    [Generates a BDD for the function lowerB &le; x &le; upperB.]

  Description [This function generates a BDD for the function
  lowerB &le; x &le; upperB, where x is an N-bit number,
  x\[0\] x\[1\] ... x\[N-1\], with 0 the most significant bit (important!).
  The number of variables N should be sufficient to represent the bounds;
  otherwise, the bounds are truncated to their N least significant bits.
  Two BDDs are built bottom-up for lowerB &le; x and x &le; upperB, and they
  are finally conjoined.]

  SideEffects [None]

  SeeAlso     [Cudd_Xgty]

******************************************************************************/
DdNode *
Cudd_bddInterval(
  DdManager * dd /* DD manager */,
  int  N /* number of x variables */,
  DdNode ** x /* array of x variables */,
  unsigned int lowerB /* lower bound */,
  unsigned int upperB /* upper bound */)
{
    DdNode *one, *zero;
    DdNode *r, *rl, *ru;
    int     i;

    one = DD_ONE(dd);
    zero = Cudd_Not(one);

    rl = one;
    cuddRef(rl);
    ru = one;
    cuddRef(ru);

    /* Loop to build the rest of the BDDs. */
    for (i = N-1; i >= 0; i--) {
        DdNode *vl, *vu;
        vl = Cudd_bddIte(dd, x[i],
                         lowerB&1 ? rl : one,
                         lowerB&1 ? zero : rl);
        if (vl == NULL) {
            Cudd_IterDerefBdd(dd, rl);
            Cudd_IterDerefBdd(dd, ru);
            return(NULL);
        }
        cuddRef(vl);
        Cudd_IterDerefBdd(dd, rl);
        rl = vl;
        lowerB >>= 1;
        vu = Cudd_bddIte(dd, x[i],
                         upperB&1 ? ru : zero,
                         upperB&1 ? one : ru);
        if (vu == NULL) {
            Cudd_IterDerefBdd(dd, rl);
            Cudd_IterDerefBdd(dd, ru);
            return(NULL);
        }
        cuddRef(vu);
        Cudd_IterDerefBdd(dd, ru);
        ru = vu;
        upperB >>= 1;
    }

    /* Conjoin the two bounds. */
    r = Cudd_bddAnd(dd, rl, ru);
    if (r == NULL) {
        Cudd_IterDerefBdd(dd, rl);
        Cudd_IterDerefBdd(dd, ru);
        return(NULL);
    }
    cuddRef(r);
    Cudd_IterDerefBdd(dd, rl);
    Cudd_IterDerefBdd(dd, ru);
    cuddDeref(r);
    return(r);

} /* end of Cudd_bddInterval */


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

Alan Mishchenko committed
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
  Synopsis    [Computes the compatible projection of R w.r.t. cube Y.]

  Description [Computes the compatible projection of relation R with
  respect to cube Y. Returns a pointer to the c-projection if
  successful; NULL otherwise. For a comparison between Cudd_CProjection
  and Cudd_PrioritySelect, see the documentation of the latter.]

  SideEffects [None]

  SeeAlso     [Cudd_PrioritySelect]

******************************************************************************/
DdNode *
Cudd_CProjection(
  DdManager * dd,
  DdNode * R,
  DdNode * Y)
{
    DdNode *res;
    DdNode *support;

    if (cuddCheckCube(dd,Y) == 0) {
1209 1210 1211 1212
        (void) fprintf(dd->err,
        "Error: The third argument of Cudd_CProjection should be a cube\n");
        dd->errorCode = CUDD_INVALID_ARG;
        return(NULL);
Alan Mishchenko committed
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
    }

    /* Compute the support of Y, which is used by the abstraction step
    ** in cuddCProjectionRecur.
    */
    support = Cudd_Support(dd,Y);
    if (support == NULL) return(NULL);
    cuddRef(support);

    do {
1223 1224
        dd->reordered = 0;
        res = cuddCProjectionRecur(dd,R,Y,support);
Alan Mishchenko committed
1225 1226 1227
    } while (dd->reordered == 1);

    if (res == NULL) {
1228 1229
        Cudd_RecursiveDeref(dd,support);
        return(NULL);
Alan Mishchenko committed
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
    }
    cuddRef(res);
    Cudd_RecursiveDeref(dd,support);
    cuddDeref(res);

    return(res);

} /* end of Cudd_CProjection */


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

  Synopsis    [Computes the Hamming distance ADD.]

  Description [Computes the Hamming distance ADD. Returns an ADD that
  gives the Hamming distance between its two arguments if successful;
  NULL otherwise. The two vectors xVars and yVars identify the variables
  that form the two arguments.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
DdNode *
Cudd_addHamming(
  DdManager * dd,
  DdNode ** xVars,
  DdNode ** yVars,
  int  nVars)
{
    DdNode  *result,*tempBdd;
    DdNode  *tempAdd,*temp;
    int     i;

    result = DD_ZERO(dd);
    cuddRef(result);

    for (i = 0; i < nVars; i++) {
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
        tempBdd = Cudd_bddIte(dd,xVars[i],Cudd_Not(yVars[i]),yVars[i]);
        if (tempBdd == NULL) {
            Cudd_RecursiveDeref(dd,result);
            return(NULL);
        }
        cuddRef(tempBdd);
        tempAdd = Cudd_BddToAdd(dd,tempBdd);
        if (tempAdd == NULL) {
            Cudd_RecursiveDeref(dd,tempBdd);
            Cudd_RecursiveDeref(dd,result);
            return(NULL);
        }
        cuddRef(tempAdd);
Alan Mishchenko committed
1282
        Cudd_RecursiveDeref(dd,tempBdd);
1283 1284 1285 1286 1287 1288 1289
        temp = Cudd_addApply(dd,Cudd_addPlus,tempAdd,result);
        if (temp == NULL) {
            Cudd_RecursiveDeref(dd,tempAdd);
            Cudd_RecursiveDeref(dd,result);
            return(NULL);
        }
        cuddRef(temp);
Alan Mishchenko committed
1290 1291
        Cudd_RecursiveDeref(dd,tempAdd);
        Cudd_RecursiveDeref(dd,result);
1292
        result = temp;
Alan Mishchenko committed
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
    }

    cuddDeref(result);
    return(result);

} /* end of Cudd_addHamming */


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

  Synopsis    [Returns the minimum Hamming distance between f and minterm.]

  Description [Returns the minimum Hamming distance between the
  minterms of a function f and a reference minterm. The function is
  given as a BDD; the minterm is given as an array of integers, one
  for each variable in the manager.  Returns the minimum distance if
  it is less than the upper bound; the upper bound if the minimum
  distance is at least as large; CUDD_OUT_OF_MEM in case of failure.]

  SideEffects [None]

  SeeAlso     [Cudd_addHamming Cudd_bddClosestCube]

******************************************************************************/
int
Cudd_MinHammingDist(
  DdManager *dd /* DD manager */,
  DdNode *f /* function to examine */,
  int *minterm /* reference minterm */,
  int upperBound /* distance above which an approximate answer is OK */)
{
    DdHashTable *table;
    CUDD_VALUE_TYPE epsilon;
    int res;

    table = cuddHashTableInit(dd,1,2);
    if (table == NULL) {
1330
        return(CUDD_OUT_OF_MEM);
Alan Mishchenko committed
1331 1332 1333 1334 1335 1336 1337 1338
    }
    epsilon = Cudd_ReadEpsilon(dd);
    Cudd_SetEpsilon(dd,(CUDD_VALUE_TYPE)0.0);
    res = cuddMinHammingDistRecur(f,minterm,table,upperBound);
    cuddHashTableQuit(table);
    Cudd_SetEpsilon(dd,epsilon);

    return(res);
1339

Alan Mishchenko committed
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
} /* end of Cudd_MinHammingDist */


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

  Synopsis    [Finds a cube of f at minimum Hamming distance from g.]

  Description [Finds a cube of f at minimum Hamming distance from the
  minterms of g.  All the minterms of the cube are at the minimum
  distance.  If the distance is 0, the cube belongs to the
  intersection of f and g.  Returns the cube if successful; NULL
  otherwise.]

  SideEffects [The distance is returned as a side effect.]

  SeeAlso     [Cudd_MinHammingDist]

******************************************************************************/
DdNode *
Cudd_bddClosestCube(
  DdManager *dd,
  DdNode * f,
  DdNode *g,
  int *distance)
{
    DdNode *res, *acube;
    CUDD_VALUE_TYPE rdist;

    /* Compute the cube and distance as a single ADD. */
    do {
1370 1371
        dd->reordered = 0;
        res = cuddBddClosestCube(dd,f,g,CUDD_CONST_INDEX + 1.0);
Alan Mishchenko committed
1372 1373 1374 1375 1376 1377
    } while (dd->reordered == 1);
    if (res == NULL) return(NULL);
    cuddRef(res);

    /* Unpack distance and cube. */
    do {
1378 1379
        dd->reordered = 0;
        acube = separateCube(dd, res, &rdist);
Alan Mishchenko committed
1380 1381
    } while (dd->reordered == 1);
    if (acube == NULL) {
1382 1383
        Cudd_RecursiveDeref(dd, res);
        return(NULL);
Alan Mishchenko committed
1384 1385 1386 1387 1388 1389
    }
    cuddRef(acube);
    Cudd_RecursiveDeref(dd, res);

    /* Convert cube from ADD to BDD. */
    do {
1390 1391
        dd->reordered = 0;
        res = cuddAddBddDoPattern(dd, acube);
Alan Mishchenko committed
1392 1393
    } while (dd->reordered == 1);
    if (res == NULL) {
1394 1395
        Cudd_RecursiveDeref(dd, acube);
        return(NULL);
Alan Mishchenko committed
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
    }
    cuddRef(res);
    Cudd_RecursiveDeref(dd, acube);

    *distance = (int) rdist;
    cuddDeref(res);
    return(res);

} /* end of Cudd_bddClosestCube */


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


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

  Synopsis    [Performs the recursive step of Cudd_CProjection.]

  Description [Performs the recursive step of Cudd_CProjection. Returns
  the projection if successful; NULL otherwise.]

  SideEffects [None]

  SeeAlso     [Cudd_CProjection]

******************************************************************************/
DdNode *
cuddCProjectionRecur(
  DdManager * dd,
  DdNode * R,
  DdNode * Y,
  DdNode * Ysupp)
{
    DdNode *res, *res1, *res2, *resA;
    DdNode *r, *y, *RT, *RE, *YT, *YE, *Yrest, *Ra, *Ran, *Gamma, *Alpha;
1433
    unsigned int topR, topY, top, index = 0;
Alan Mishchenko committed
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
    DdNode *one = DD_ONE(dd);

    statLine(dd);
    if (Y == one) return(R);

#ifdef DD_DEBUG
    assert(!Cudd_IsConstant(Y));
#endif

    if (R == Cudd_Not(one)) return(R);

    res = cuddCacheLookup2(dd, Cudd_CProjection, R, Y);
    if (res != NULL) return(res);

    r = Cudd_Regular(R);
    topR = cuddI(dd,r->index);
    y = Cudd_Regular(Y);
    topY = cuddI(dd,y->index);

    top = ddMin(topR, topY);

    /* Compute the cofactors of R */
    if (topR == top) {
1457 1458 1459 1460 1461 1462
        index = r->index;
        RT = cuddT(r);
        RE = cuddE(r);
        if (r != R) {
            RT = Cudd_Not(RT); RE = Cudd_Not(RE);
        }
Alan Mishchenko committed
1463
    } else {
1464
        RT = RE = R;
Alan Mishchenko committed
1465 1466 1467
    }

    if (topY > top) {
1468 1469 1470 1471 1472
        /* Y does not depend on the current top variable.
        ** We just need to compute the results on the two cofactors of R
        ** and make them the children of a node labeled r->index.
        */
        res1 = cuddCProjectionRecur(dd,RT,Y,Ysupp);
Alan Mishchenko committed
1473 1474
        if (res1 == NULL) return(NULL);
        cuddRef(res1);
1475 1476 1477 1478
        res2 = cuddCProjectionRecur(dd,RE,Y,Ysupp);
        if (res2 == NULL) {
            Cudd_RecursiveDeref(dd,res1);
            return(NULL);
Alan Mishchenko committed
1479
        }
1480 1481
        cuddRef(res2);
        res = cuddBddIteRecur(dd, dd->vars[index], res1, res2);
Alan Mishchenko committed
1482
        if (res == NULL) {
1483 1484 1485
            Cudd_RecursiveDeref(dd,res1);
            Cudd_RecursiveDeref(dd,res2);
            return(NULL);
Alan Mishchenko committed
1486
        }
1487 1488 1489
        /* If we have reached this point, res1 and res2 are now
        ** incorporated in res. cuddDeref is therefore sufficient.
        */
Alan Mishchenko committed
1490
        cuddDeref(res1);
1491
        cuddDeref(res2);
Alan Mishchenko committed
1492
    } else {
1493 1494 1495 1496 1497 1498
        /* Compute the cofactors of Y */
        index = y->index;
        YT = cuddT(y);
        YE = cuddE(y);
        if (y != Y) {
            YT = Cudd_Not(YT); YE = Cudd_Not(YE);
Alan Mishchenko committed
1499
        }
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
        if (YT == Cudd_Not(one)) {
            Alpha  = Cudd_Not(dd->vars[index]);
            Yrest = YE;
            Ra = RE;
            Ran = RT;
        } else {
            Alpha = dd->vars[index];
            Yrest = YT;
            Ra = RT;
            Ran = RE;
Alan Mishchenko committed
1510
        }
1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563
        Gamma = cuddBddExistAbstractRecur(dd,Ra,cuddT(Ysupp));
        if (Gamma == NULL) return(NULL);
        if (Gamma == one) {
            res1 = cuddCProjectionRecur(dd,Ra,Yrest,cuddT(Ysupp));
            if (res1 == NULL) return(NULL);
            cuddRef(res1);
            res = cuddBddAndRecur(dd, Alpha, res1);
            if (res == NULL) {
                Cudd_RecursiveDeref(dd,res1);
                return(NULL);
            }
            cuddDeref(res1);
        } else if (Gamma == Cudd_Not(one)) {
            res1 = cuddCProjectionRecur(dd,Ran,Yrest,cuddT(Ysupp));
            if (res1 == NULL) return(NULL);
            cuddRef(res1);
            res = cuddBddAndRecur(dd, Cudd_Not(Alpha), res1);
            if (res == NULL) {
                Cudd_RecursiveDeref(dd,res1);
                return(NULL);
            }
            cuddDeref(res1);
        } else {
            cuddRef(Gamma);
            resA = cuddCProjectionRecur(dd,Ran,Yrest,cuddT(Ysupp));
            if (resA == NULL) {
                Cudd_RecursiveDeref(dd,Gamma);
                return(NULL);
            }
            cuddRef(resA);
            res2 = cuddBddAndRecur(dd, Cudd_Not(Gamma), resA);
            if (res2 == NULL) {
                Cudd_RecursiveDeref(dd,Gamma);
                Cudd_RecursiveDeref(dd,resA);
                return(NULL);
            }
            cuddRef(res2);
            Cudd_RecursiveDeref(dd,Gamma);
            Cudd_RecursiveDeref(dd,resA);
            res1 = cuddCProjectionRecur(dd,Ra,Yrest,cuddT(Ysupp));
            if (res1 == NULL) {
                Cudd_RecursiveDeref(dd,res2);
                return(NULL);
            }
            cuddRef(res1);
            res = cuddBddIteRecur(dd, Alpha, res1, res2);
            if (res == NULL) {
                Cudd_RecursiveDeref(dd,res1);
                Cudd_RecursiveDeref(dd,res2);
                return(NULL);
            }
            cuddDeref(res1);
            cuddDeref(res2);
Alan Mishchenko committed
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
        }
    }

    cuddCacheInsert2(dd,Cudd_CProjection,R,Y,res);

    return(res);

} /* end of cuddCProjectionRecur */


1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
#ifdef USE_CASH_DUMMY
/**Function********************************************************************

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

******************************************************************************/
DdNode *
Cudd_bddClosestCube_dummy(DdManager *dd, DdNode *f, DdNode *g)
{
  assert(0);
  return 0;
}
#endif


Alan Mishchenko committed
1590 1591 1592 1593 1594 1595 1596
/**Function********************************************************************

  Synopsis    [Performs the recursive step of Cudd_bddClosestCube.]

  Description [Performs the recursive step of Cudd_bddClosestCube.
  Returns the cube if succesful; NULL otherwise.  The procedure uses a
  four-way recursion to examine all four combinations of cofactors of
1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654
  <code>f</code> and <code>g</code> according to the following formula.
  <pre>
    H(f,g) = min(H(ft,gt), H(fe,ge), H(ft,ge)+1, H(fe,gt)+1)
  </pre>
  Bounding is based on the following observations.
  <ul>
  <li> If we already found two points at distance 0, there is no point in
       continuing.  Furthermore,
  <li> If F == not(G) then the best we can hope for is a minimum distance
       of 1.  If we have already found two points at distance 1, there is
       no point in continuing.  (Indeed, H(F,G) == 1 in this case.  We
       have to continue, though, to find the cube.)
  </ul>
  The variable <code>bound</code> is set at the largest value of the distance
  that we are still interested in.  Therefore, we desist when
  <pre>
    (bound == -1) and (F != not(G)) or (bound == 0) and (F == not(G)).
  </pre>
  If we were maximally aggressive in using the bound, we would always
  set the bound to the minimum distance seen thus far minus one.  That
  is, we would maintain the invariant
  <pre>
    bound < minD,
  </pre>
  except at the very beginning, when we have no value for
  <code>minD</code>.<p>

  However, we do not use <code>bound < minD</code> when examining the
  two negative cofactors, because we try to find a large cube at
  minimum distance.  To do so, we try to find a cube in the negative
  cofactors at the same or smaller distance from the cube found in the
  positive cofactors.<p>

  When we compute <code>H(ft,ge)</code> and <code>H(fe,gt)</code> we
  know that we are going to add 1 to the result of the recursive call
  to account for the difference in the splitting variable.  Therefore,
  we decrease the bound correspondingly.<p>

  Another important observation concerns the need of examining all
  four pairs of cofators only when both <code>f</code> and
  <code>g</code> depend on the top variable.<p>

  Suppose <code>gt == ge == g</code>.  (That is, <code>g</code> does
  not depend on the top variable.)  Then
  <pre>
    H(f,g) = min(H(ft,g), H(fe,g), H(ft,g)+1, H(fe,g)+1)
           = min(H(ft,g), H(fe,g)) .
  </pre>
  Therefore, under these circumstances, we skip the two "cross" cases.<p>

  An interesting feature of this function is the scheme used for
  caching the results in the global computed table.  Since we have a
  cube and a distance, we combine them to form an ADD.  The
  combination replaces the zero child of the top node of the cube with
  the negative of the distance.  (The use of the negative is to avoid
  ambiguity with 1.)  The degenerate cases (zero and one) are treated
  specially because the distance is known (0 for one, and infinity for
  zero).]
Alan Mishchenko committed
1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676

  SideEffects [None]

  SeeAlso     [Cudd_bddClosestCube]

******************************************************************************/
DdNode *
cuddBddClosestCube(
  DdManager *dd,
  DdNode *f,
  DdNode *g,
  CUDD_VALUE_TYPE bound)
{
    DdNode *res, *F, *G, *ft, *fe, *gt, *ge, *tt, *ee;
    DdNode *ctt, *cee, *cte, *cet;
    CUDD_VALUE_TYPE minD, dtt, dee, dte, det;
    DdNode *one = DD_ONE(dd);
    DdNode *lzero = Cudd_Not(one);
    DdNode *azero = DD_ZERO(dd);
    unsigned int topf, topg, index;

    statLine(dd);
1677
    if (bound < (int)(f == Cudd_Not(g))) return(azero);
Alan Mishchenko committed
1678 1679 1680 1681 1682 1683 1684 1685
    /* Terminal cases. */
    if (g == lzero || f == lzero) return(azero);
    if (f == one && g == one) return(one);

    /* Check cache. */
    F = Cudd_Regular(f);
    G = Cudd_Regular(g);
    if (F->ref != 1 || G->ref != 1) {
1686 1687 1688
#ifdef USE_CASH_DUMMY
        res = cuddCacheLookup2(dd,(DD_CTFP) Cudd_bddClosestCube_dummy, f, g);
#else
1689
        res = cuddCacheLookup2(dd,(DD_CTFP) Cudd_bddClosestCube, f, g);
1690
#endif
1691
        if (res != NULL) return(res);
Alan Mishchenko committed
1692 1693 1694 1695 1696 1697 1698
    }

    topf = cuddI(dd,F->index);
    topg = cuddI(dd,G->index);

    /* Compute cofactors. */
    if (topf <= topg) {
1699 1700 1701 1702 1703 1704 1705
        index = F->index;
        ft = cuddT(F);
        fe = cuddE(F);
        if (Cudd_IsComplement(f)) {
            ft = Cudd_Not(ft);
            fe = Cudd_Not(fe);
        }
Alan Mishchenko committed
1706
    } else {
1707 1708
        index = G->index;
        ft = fe = f;
Alan Mishchenko committed
1709 1710 1711
    }

    if (topg <= topf) {
1712 1713 1714 1715 1716 1717
        gt = cuddT(G);
        ge = cuddE(G);
        if (Cudd_IsComplement(g)) {
            gt = Cudd_Not(gt);
            ge = Cudd_Not(ge);
        }
Alan Mishchenko committed
1718
    } else {
1719
        gt = ge = g;
Alan Mishchenko committed
1720 1721 1722 1723 1724 1725 1726
    }

    tt = cuddBddClosestCube(dd,ft,gt,bound);
    if (tt == NULL) return(NULL);
    cuddRef(tt);
    ctt = separateCube(dd,tt,&dtt);
    if (ctt == NULL) {
1727 1728
        Cudd_RecursiveDeref(dd, tt);
        return(NULL);
Alan Mishchenko committed
1729 1730 1731 1732 1733 1734 1735 1736
    }
    cuddRef(ctt);
    Cudd_RecursiveDeref(dd, tt);
    minD = dtt;
    bound = ddMin(bound,minD);

    ee = cuddBddClosestCube(dd,fe,ge,bound);
    if (ee == NULL) {
1737 1738
        Cudd_RecursiveDeref(dd, ctt);
        return(NULL);
Alan Mishchenko committed
1739 1740 1741 1742
    }
    cuddRef(ee);
    cee = separateCube(dd,ee,&dee);
    if (cee == NULL) {
1743 1744 1745
        Cudd_RecursiveDeref(dd, ctt);
        Cudd_RecursiveDeref(dd, ee);
        return(NULL);
Alan Mishchenko committed
1746 1747 1748 1749
    }
    cuddRef(cee);
    Cudd_RecursiveDeref(dd, ee);
    minD = ddMin(dtt, dee);
1750
    if (minD <= CUDD_CONST_INDEX) bound = ddMin(bound,minD-1);
Alan Mishchenko committed
1751 1752

    if (minD > 0 && topf == topg) {
1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767
        DdNode *te = cuddBddClosestCube(dd,ft,ge,bound-1);
        if (te == NULL) {
            Cudd_RecursiveDeref(dd, ctt);
            Cudd_RecursiveDeref(dd, cee);
            return(NULL);
        }
        cuddRef(te);
        cte = separateCube(dd,te,&dte);
        if (cte == NULL) {
            Cudd_RecursiveDeref(dd, ctt);
            Cudd_RecursiveDeref(dd, cee);
            Cudd_RecursiveDeref(dd, te);
            return(NULL);
        }
        cuddRef(cte);
Alan Mishchenko committed
1768
        Cudd_RecursiveDeref(dd, te);
1769 1770
        dte += 1.0;
        minD = ddMin(minD, dte);
Alan Mishchenko committed
1771
    } else {
1772 1773 1774
        cte = azero;
        cuddRef(cte);
        dte = CUDD_CONST_INDEX + 1.0;
Alan Mishchenko committed
1775
    }
1776
    if (minD <= CUDD_CONST_INDEX) bound = ddMin(bound,minD-1);
Alan Mishchenko committed
1777 1778

    if (minD > 0 && topf == topg) {
1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
        DdNode *et = cuddBddClosestCube(dd,fe,gt,bound-1);
        if (et == NULL) {
            Cudd_RecursiveDeref(dd, ctt);
            Cudd_RecursiveDeref(dd, cee);
            Cudd_RecursiveDeref(dd, cte);
            return(NULL);
        }
        cuddRef(et);
        cet = separateCube(dd,et,&det);
        if (cet == NULL) {
            Cudd_RecursiveDeref(dd, ctt);
            Cudd_RecursiveDeref(dd, cee);
            Cudd_RecursiveDeref(dd, cte);
            Cudd_RecursiveDeref(dd, et);
            return(NULL);
        }
        cuddRef(cet);
Alan Mishchenko committed
1796
        Cudd_RecursiveDeref(dd, et);
1797 1798
        det += 1.0;
        minD = ddMin(minD, det);
Alan Mishchenko committed
1799
    } else {
1800 1801 1802
        cet = azero;
        cuddRef(cet);
        det = CUDD_CONST_INDEX + 1.0;
Alan Mishchenko committed
1803 1804 1805
    }

    if (minD == dtt) {
1806 1807 1808 1809 1810
        if (dtt == dee && ctt == cee) {
            res = createResult(dd,CUDD_CONST_INDEX,1,ctt,dtt);
        } else {
            res = createResult(dd,index,1,ctt,dtt);
        }
Alan Mishchenko committed
1811
    } else if (minD == dee) {
1812
        res = createResult(dd,index,0,cee,dee);
Alan Mishchenko committed
1813
    } else if (minD == dte) {
1814 1815 1816 1817
#ifdef DD_DEBUG
        assert(topf == topg);
#endif
        res = createResult(dd,index,1,cte,dte);
Alan Mishchenko committed
1818
    } else {
1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829
#ifdef DD_DEBUG
        assert(topf == topg);
#endif
        res = createResult(dd,index,0,cet,det);
    }
    if (res == NULL) {
        Cudd_RecursiveDeref(dd, ctt);
        Cudd_RecursiveDeref(dd, cee);
        Cudd_RecursiveDeref(dd, cte);
        Cudd_RecursiveDeref(dd, cet);
        return(NULL);
Alan Mishchenko committed
1830 1831 1832 1833 1834 1835 1836
    }
    cuddRef(res);
    Cudd_RecursiveDeref(dd, ctt);
    Cudd_RecursiveDeref(dd, cee);
    Cudd_RecursiveDeref(dd, cte);
    Cudd_RecursiveDeref(dd, cet);

1837 1838 1839
    /* Only cache results that are different from azero to avoid
    ** storing results that depend on the value of the bound. */
    if ((F->ref != 1 || G->ref != 1) && res != azero)
1840 1841 1842
#ifdef USE_CASH_DUMMY
        cuddCacheInsert2(dd,(DD_CTFP) Cudd_bddClosestCube_dummy, f, g, res);
#else
1843
        cuddCacheInsert2(dd,(DD_CTFP) Cudd_bddClosestCube, f, g, res);
1844
#endif
Alan Mishchenko committed
1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887

    cuddDeref(res);
    return(res);

} /* end of cuddBddClosestCube */


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


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

  Synopsis    [Performs the recursive step of Cudd_MinHammingDist.]

  Description [Performs the recursive step of Cudd_MinHammingDist.
  It is based on the following identity. Let H(f) be the
  minimum Hamming distance of the minterms of f from the reference
  minterm. Then:
  <xmp>
    H(f) = min(H(f0)+h0,H(f1)+h1)
  </xmp>
  where f0 and f1 are the two cofactors of f with respect to its top
  variable; h0 is 1 if the minterm assigns 1 to the top variable of f;
  h1 is 1 if the minterm assigns 0 to the top variable of f.
  The upper bound on the distance is used to bound the depth of the
  recursion.
  Returns the minimum distance unless it exceeds the upper bound or
  computation fails.]

  SideEffects [None]

  SeeAlso     [Cudd_MinHammingDist]

******************************************************************************/
static int
cuddMinHammingDistRecur(
  DdNode * f,
  int *minterm,
  DdHashTable * table,
  int upperBound)
{
1888 1889 1890 1891
    DdNode      *F, *Ft, *Fe;
    double      h, hT, hE;
    DdNode      *zero, *res;
    DdManager   *dd = table->manager;
Alan Mishchenko committed
1892 1893 1894 1895 1896 1897 1898

    statLine(dd);
    if (upperBound == 0) return(0);

    F = Cudd_Regular(f);

    if (cuddIsConstant(F)) {
1899 1900 1901 1902 1903 1904
        zero = Cudd_Not(DD_ONE(dd));
        if (f == dd->background || f == zero) {
            return(upperBound);
        } else {
            return(0);
        }
Alan Mishchenko committed
1905 1906
    }
    if ((res = cuddHashTableLookup1(table,f)) != NULL) {
1907 1908 1909 1910 1911 1912
        h = cuddV(res);
        if (res->ref == 0) {
            dd->dead++;
            dd->constants.dead++;
        }
        return((int) h);
Alan Mishchenko committed
1913 1914 1915 1916
    }

    Ft = cuddT(F); Fe = cuddE(F);
    if (Cudd_IsComplement(f)) {
1917
        Ft = Cudd_Not(Ft); Fe = Cudd_Not(Fe);
Alan Mishchenko committed
1918 1919
    }
    if (minterm[F->index] == 0) {
1920 1921
        DdNode *temp = Ft;
        Ft = Fe; Fe = temp;
Alan Mishchenko committed
1922 1923 1924 1925 1926
    }

    hT = cuddMinHammingDistRecur(Ft,minterm,table,upperBound);
    if (hT == CUDD_OUT_OF_MEM) return(CUDD_OUT_OF_MEM);
    if (hT == 0) {
1927
        hE = upperBound;
Alan Mishchenko committed
1928
    } else {
1929 1930
        hE = cuddMinHammingDistRecur(Fe,minterm,table,upperBound - 1);
        if (hE == CUDD_OUT_OF_MEM) return(CUDD_OUT_OF_MEM);
Alan Mishchenko committed
1931 1932 1933 1934
    }
    h = ddMin(hT, hE + 1);

    if (F->ref != 1) {
1935 1936 1937 1938 1939 1940 1941
        ptrint fanout = (ptrint) F->ref;
        cuddSatDec(fanout);
        res = cuddUniqueConst(dd, (CUDD_VALUE_TYPE) h);
        if (!cuddHashTableInsert1(table,f,res,fanout)) {
            cuddRef(res); Cudd_RecursiveDeref(dd, res);
            return(CUDD_OUT_OF_MEM);
        }
Alan Mishchenko committed
1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970
    }

    return((int) h);

} /* end of cuddMinHammingDistRecur */


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

  Synopsis    [Separates cube from distance.]

  Description [Separates cube from distance.  Returns the cube if
  successful; NULL otherwise.]

  SideEffects [The distance is returned as a side effect.]

  SeeAlso     [cuddBddClosestCube createResult]

******************************************************************************/
static DdNode *
separateCube(
  DdManager *dd,
  DdNode *f,
  CUDD_VALUE_TYPE *distance)
{
    DdNode *cube, *t;

    /* One and zero are special cases because the distance is implied. */
    if (Cudd_IsConstant(f)) {
1971 1972 1973
        *distance = (f == DD_ONE(dd)) ? 0.0 :
            (1.0 + (CUDD_VALUE_TYPE) CUDD_CONST_INDEX);
        return(f);
Alan Mishchenko committed
1974 1975 1976 1977 1978 1979 1980
    }

    /* Find out which branch points to the distance and replace the top
    ** node with one pointing to zero instead. */
    t = cuddT(f);
    if (Cudd_IsConstant(t) && cuddV(t) <= 0) {
#ifdef DD_DEBUG
1981
        assert(!Cudd_IsConstant(cuddE(f)) || cuddE(f) == DD_ONE(dd));
Alan Mishchenko committed
1982
#endif
1983 1984
        *distance = -cuddV(t);
        cube = cuddUniqueInter(dd, f->index, DD_ZERO(dd), cuddE(f));
Alan Mishchenko committed
1985 1986
    } else {
#ifdef DD_DEBUG
1987
        assert(!Cudd_IsConstant(t) || t == DD_ONE(dd));
Alan Mishchenko committed
1988
#endif
1989 1990
        *distance = -cuddV(cuddE(f));
        cube = cuddUniqueInter(dd, f->index, t, DD_ZERO(dd));
Alan Mishchenko committed
1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021
    }

    return(cube);

} /* end of separateCube */


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

  Synopsis    [Builds a result for cache storage.]

  Description [Builds a result for cache storage.  Returns a pointer
  to the resulting ADD if successful; NULL otherwise.]

  SideEffects [None]

  SeeAlso     [cuddBddClosestCube separateCube]

******************************************************************************/
static DdNode *
createResult(
  DdManager *dd,
  unsigned int index,
  unsigned int phase,
  DdNode *cube,
  CUDD_VALUE_TYPE distance)
{
    DdNode *res, *constant;

    /* Special case.  The cube is either one or zero, and we do not
    ** add any variables.  Hence, the result is also one or zero,
2022
    ** and the distance remains implied by the value of the constant. */
Alan Mishchenko committed
2023 2024 2025 2026 2027 2028 2029
    if (index == CUDD_CONST_INDEX && Cudd_IsConstant(cube)) return(cube);

    constant = cuddUniqueConst(dd,-distance);
    if (constant == NULL) return(NULL);
    cuddRef(constant);

    if (index == CUDD_CONST_INDEX) {
2030 2031 2032 2033 2034 2035
        /* Replace the top node. */
        if (cuddT(cube) == DD_ZERO(dd)) {
            res = cuddUniqueInter(dd,cube->index,constant,cuddE(cube));
        } else {
            res = cuddUniqueInter(dd,cube->index,cuddT(cube),constant);
        }
Alan Mishchenko committed
2036
    } else {
2037
        /* Add a new top node. */
Alan Mishchenko committed
2038
#ifdef DD_DEBUG
2039
        assert(cuddI(dd,index) < cuddI(dd,cube->index));
Alan Mishchenko committed
2040
#endif
2041 2042 2043 2044 2045
        if (phase) {
            res = cuddUniqueInter(dd,index,cube,constant);
        } else {
            res = cuddUniqueInter(dd,index,constant,cube);
        }
Alan Mishchenko committed
2046 2047
    }
    if (res == NULL) {
2048 2049
        Cudd_RecursiveDeref(dd, constant);
        return(NULL);
Alan Mishchenko committed
2050 2051 2052 2053 2054 2055
    }
    cuddDeref(constant); /* safe because constant is part of res */

    return(res);

} /* end of createResult */
2056 2057


2058 2059
ABC_NAMESPACE_IMPL_END