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

  FileName    [cuddDecomp.c]

  PackageName [cudd]

  Synopsis    [Functions for BDD decomposition.]

  Description [External procedures included in this file:
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
                <ul>
                <li> Cudd_bddApproxConjDecomp()
                <li> Cudd_bddApproxDisjDecomp()
                <li> Cudd_bddIterConjDecomp()
                <li> Cudd_bddIterDisjDecomp()
                <li> Cudd_bddGenConjDecomp()
                <li> Cudd_bddGenDisjDecomp()
                <li> Cudd_bddVarConjDecomp()
                <li> Cudd_bddVarDisjDecomp()
                </ul>
        Static procedures included in this module:
                <ul>
                <li> cuddConjunctsAux()
                <li> CreateBotDist()
                <li> BuildConjuncts()
                <li> ConjunctsFree()
                </ul>]
Alan Mishchenko committed
27 28 29

  Author      [Kavita Ravi, Fabio Somenzi]

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"
Alan Mishchenko committed
65 66
#include "cuddInt.h"

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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
/*---------------------------------------------------------------------------*/
/* Constant declarations                                                     */
/*---------------------------------------------------------------------------*/
#define DEPTH 5
#define THRESHOLD 10
#define NONE 0
#define PAIR_ST 1
#define PAIR_CR 2
#define G_ST 3
#define G_CR 4
#define H_ST 5
#define H_CR 6
#define BOTH_G 7
#define BOTH_H 8

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

/*---------------------------------------------------------------------------*/
/* Type declarations                                                         */
/*---------------------------------------------------------------------------*/
typedef struct Conjuncts {
    DdNode *g;
    DdNode *h;
} Conjuncts;

typedef struct  NodeStat {
    int distance;
    int localRef;
} NodeStat;


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

#ifndef lint
109
static char rcsid[] DD_UNUSED = "$Id: cuddDecomp.c,v 1.44 2004/08/13 18:04:47 fabio Exp $";
Alan Mishchenko committed
110 111
#endif

112
static  DdNode  *one, *zero;
Alan Mishchenko committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
long lastTimeG;

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


#define FactorsNotStored(factors)  ((int)((long)(factors) & 01))

#define FactorsComplement(factors) ((Conjuncts *)((long)(factors) | 01))

#define FactorsUncomplement(factors) ((Conjuncts *)((long)(factors) ^ 01))

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

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

132 133 134 135 136 137 138 139 140 141
static NodeStat * CreateBotDist (DdNode * node, st_table * distanceTable);
static double CountMinterms (DdNode * node, double max, st_table * mintermTable, FILE *fp);
static void ConjunctsFree (DdManager * dd, Conjuncts * factors);
static int PairInTables (DdNode * g, DdNode * h, st_table * ghTable);
static Conjuncts * CheckTablesCacheAndReturn (DdNode * node, DdNode * g, DdNode * h, st_table * ghTable, st_table * cacheTable);
static Conjuncts * PickOnePair (DdNode * node, DdNode * g1, DdNode * h1, DdNode * g2, DdNode * h2, st_table * ghTable, st_table * cacheTable);
static Conjuncts * CheckInTables (DdNode * node, DdNode * g1, DdNode * h1, DdNode * g2, DdNode * h2, st_table * ghTable, st_table * cacheTable, int * outOfMem);
static Conjuncts * ZeroCase (DdManager * dd, DdNode * node, Conjuncts * factorsNv, st_table * ghTable, st_table * cacheTable, int switched);
static Conjuncts * BuildConjuncts (DdManager * dd, DdNode * node, st_table * distanceTable, st_table * cacheTable, int approxDistance, int maxLocalRef, st_table * ghTable, st_table * mintermTable);
static int cuddConjunctsAux (DdManager * dd, DdNode * f, DdNode ** c1, DdNode ** c2);
Alan Mishchenko committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

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


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


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

  Synopsis    [Performs two-way conjunctive decomposition of a BDD.]

  Description [Performs two-way conjunctive decomposition of a
  BDD. This procedure owes its name to the use of supersetting to
  obtain an initial factor of the given function. Returns the number
  of conjuncts produced, that is, 2 if successful; 1 if no meaningful
  decomposition was found; 0 otherwise. The conjuncts produced by this
  procedure tend to be imbalanced.]

  SideEffects [The factors are returned in an array as side effects.
  The array is allocated by this function. It is the caller's responsibility
164
  to free it. On successful completion, the conjuncts are already
Alan Mishchenko committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
  referenced. If the function returns 0, the array for the conjuncts is
  not allocated. If the function returns 1, the only factor equals the
  function to be decomposed.]

  SeeAlso     [Cudd_bddApproxDisjDecomp Cudd_bddIterConjDecomp
  Cudd_bddGenConjDecomp Cudd_bddVarConjDecomp Cudd_RemapOverApprox
  Cudd_bddSqueeze Cudd_bddLICompaction]

******************************************************************************/
int
Cudd_bddApproxConjDecomp(
  DdManager * dd /* manager */,
  DdNode * f /* function to be decomposed */,
  DdNode *** conjuncts /* address of the first factor */)
{
    DdNode *superset1, *superset2, *glocal, *hlocal;
    int nvars = Cudd_SupportSize(dd,f);

    /* Find a tentative first factor by overapproximation and minimization. */
    superset1 = Cudd_RemapOverApprox(dd,f,nvars,0,1.0);
    if (superset1 == NULL) return(0);
    cuddRef(superset1);
    superset2 = Cudd_bddSqueeze(dd,f,superset1);
    if (superset2 == NULL) {
189 190
        Cudd_RecursiveDeref(dd,superset1);
        return(0);
Alan Mishchenko committed
191 192 193 194 195 196 197
    }
    cuddRef(superset2);
    Cudd_RecursiveDeref(dd,superset1);

    /* Compute the second factor by minimization. */
    hlocal = Cudd_bddLICompaction(dd,f,superset2);
    if (hlocal == NULL) {
198 199
        Cudd_RecursiveDeref(dd,superset2);
        return(0);
Alan Mishchenko committed
200 201 202 203 204 205 206
    }
    cuddRef(hlocal);

    /* Refine the first factor by minimization. If h turns out to be f, this
    ** step guarantees that g will be 1. */
    glocal = Cudd_bddLICompaction(dd,superset2,hlocal);
    if (glocal == NULL) {
207 208 209
        Cudd_RecursiveDeref(dd,superset2);
        Cudd_RecursiveDeref(dd,hlocal);
        return(0);
Alan Mishchenko committed
210 211 212 213 214
    }
    cuddRef(glocal);
    Cudd_RecursiveDeref(dd,superset2);

    if (glocal != DD_ONE(dd)) {
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
        if (hlocal != DD_ONE(dd)) {
            *conjuncts = ABC_ALLOC(DdNode *,2);
            if (*conjuncts == NULL) {
                Cudd_RecursiveDeref(dd,glocal);
                Cudd_RecursiveDeref(dd,hlocal);
                dd->errorCode = CUDD_MEMORY_OUT;
                return(0);
            }
            (*conjuncts)[0] = glocal;
            (*conjuncts)[1] = hlocal;
            return(2);
        } else {
            Cudd_RecursiveDeref(dd,hlocal);
            *conjuncts = ABC_ALLOC(DdNode *,1);
            if (*conjuncts == NULL) {
                Cudd_RecursiveDeref(dd,glocal);
                dd->errorCode = CUDD_MEMORY_OUT;
                return(0);
            }
            (*conjuncts)[0] = glocal;
            return(1);
Alan Mishchenko committed
236 237
        }
    } else {
238
        Cudd_RecursiveDeref(dd,glocal);
Alan Mishchenko committed
239
        *conjuncts = ABC_ALLOC(DdNode *,1);
Alan Mishchenko committed
240
        if (*conjuncts == NULL) {
241 242 243
            Cudd_RecursiveDeref(dd,hlocal);
            dd->errorCode = CUDD_MEMORY_OUT;
            return(0);
Alan Mishchenko committed
244
        }
245
        (*conjuncts)[0] = hlocal;
Alan Mishchenko committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
        return(1);
    }

} /* end of Cudd_bddApproxConjDecomp */


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

  Synopsis    [Performs two-way disjunctive decomposition of a BDD.]

  Description [Performs two-way disjunctive decomposition of a BDD.
  Returns the number of disjuncts produced, that is, 2 if successful;
  1 if no meaningful decomposition was found; 0 otherwise. The
  disjuncts produced by this procedure tend to be imbalanced.]

  SideEffects [The two disjuncts are returned in an array as side effects.
  The array is allocated by this function. It is the caller's responsibility
263
  to free it. On successful completion, the disjuncts are already
Alan Mishchenko committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
  referenced. If the function returns 0, the array for the disjuncts is
  not allocated. If the function returns 1, the only factor equals the
  function to be decomposed.]

  SeeAlso     [Cudd_bddApproxConjDecomp Cudd_bddIterDisjDecomp
  Cudd_bddGenDisjDecomp Cudd_bddVarDisjDecomp]

******************************************************************************/
int
Cudd_bddApproxDisjDecomp(
  DdManager * dd /* manager */,
  DdNode * f /* function to be decomposed */,
  DdNode *** disjuncts /* address of the array of the disjuncts */)
{
    int result, i;

    result = Cudd_bddApproxConjDecomp(dd,Cudd_Not(f),disjuncts);
    for (i = 0; i < result; i++) {
282
        (*disjuncts)[i] = Cudd_Not((*disjuncts)[i]);
Alan Mishchenko committed
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
    }
    return(result);

} /* end of Cudd_bddApproxDisjDecomp */


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

  Synopsis    [Performs two-way conjunctive decomposition of a BDD.]

  Description [Performs two-way conjunctive decomposition of a
  BDD. This procedure owes its name to the iterated use of
  supersetting to obtain a factor of the given function. Returns the
  number of conjuncts produced, that is, 2 if successful; 1 if no
  meaningful decomposition was found; 0 otherwise. The conjuncts
  produced by this procedure tend to be imbalanced.]

  SideEffects [The factors are returned in an array as side effects.
  The array is allocated by this function. It is the caller's responsibility
302
  to free it. On successful completion, the conjuncts are already
Alan Mishchenko committed
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
  referenced. If the function returns 0, the array for the conjuncts is
  not allocated. If the function returns 1, the only factor equals the
  function to be decomposed.]

  SeeAlso     [Cudd_bddIterDisjDecomp Cudd_bddApproxConjDecomp
  Cudd_bddGenConjDecomp Cudd_bddVarConjDecomp Cudd_RemapOverApprox
  Cudd_bddSqueeze Cudd_bddLICompaction]

******************************************************************************/
int
Cudd_bddIterConjDecomp(
  DdManager * dd /* manager */,
  DdNode * f /* function to be decomposed */,
  DdNode *** conjuncts /* address of the array of conjuncts */)
{
    DdNode *superset1, *superset2, *old[2], *res[2];
    int sizeOld, sizeNew;
    int nvars = Cudd_SupportSize(dd,f);

    old[0] = DD_ONE(dd);
    cuddRef(old[0]);
    old[1] = f;
    cuddRef(old[1]);
    sizeOld = Cudd_SharingSize(old,2);

    do {
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
        /* Find a tentative first factor by overapproximation and
        ** minimization. */
        superset1 = Cudd_RemapOverApprox(dd,old[1],nvars,0,1.0);
        if (superset1 == NULL) {
            Cudd_RecursiveDeref(dd,old[0]);
            Cudd_RecursiveDeref(dd,old[1]);
            return(0);
        }
        cuddRef(superset1);
        superset2 = Cudd_bddSqueeze(dd,old[1],superset1);
        if (superset2 == NULL) {
            Cudd_RecursiveDeref(dd,old[0]);
            Cudd_RecursiveDeref(dd,old[1]);
            Cudd_RecursiveDeref(dd,superset1);
            return(0);
        }
        cuddRef(superset2);
Alan Mishchenko committed
346
        Cudd_RecursiveDeref(dd,superset1);
347 348 349 350 351 352 353 354
        res[0] = Cudd_bddAnd(dd,old[0],superset2);
        if (res[0] == NULL) {
            Cudd_RecursiveDeref(dd,superset2);
            Cudd_RecursiveDeref(dd,old[0]);
            Cudd_RecursiveDeref(dd,old[1]);
            return(0);
        }
        cuddRef(res[0]);
Alan Mishchenko committed
355
        Cudd_RecursiveDeref(dd,superset2);
356 357 358 359
        if (res[0] == old[0]) {
            Cudd_RecursiveDeref(dd,res[0]);
            break;      /* avoid infinite loop */
        }
Alan Mishchenko committed
360

361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
        /* Compute the second factor by minimization. */
        res[1] = Cudd_bddLICompaction(dd,old[1],res[0]);
        if (res[1] == NULL) {
            Cudd_RecursiveDeref(dd,old[0]);
            Cudd_RecursiveDeref(dd,old[1]);
            return(0);
        }
        cuddRef(res[1]);

        sizeNew = Cudd_SharingSize(res,2);
        if (sizeNew <= sizeOld) {
            Cudd_RecursiveDeref(dd,old[0]);
            old[0] = res[0];
            Cudd_RecursiveDeref(dd,old[1]);
            old[1] = res[1];
            sizeOld = sizeNew;
        } else {
            Cudd_RecursiveDeref(dd,res[0]);
            Cudd_RecursiveDeref(dd,res[1]);
            break;
        }
Alan Mishchenko committed
382 383 384 385 386 387 388

    } while (1);

    /* Refine the first factor by minimization. If h turns out to
    ** be f, this step guarantees that g will be 1. */
    superset1 = Cudd_bddLICompaction(dd,old[0],old[1]);
    if (superset1 == NULL) {
389 390 391
        Cudd_RecursiveDeref(dd,old[0]);
        Cudd_RecursiveDeref(dd,old[1]);
        return(0);
Alan Mishchenko committed
392 393 394 395 396 397
    }
    cuddRef(superset1);
    Cudd_RecursiveDeref(dd,old[0]);
    old[0] = superset1;

    if (old[0] != DD_ONE(dd)) {
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
        if (old[1] != DD_ONE(dd)) {
            *conjuncts = ABC_ALLOC(DdNode *,2);
            if (*conjuncts == NULL) {
                Cudd_RecursiveDeref(dd,old[0]);
                Cudd_RecursiveDeref(dd,old[1]);
                dd->errorCode = CUDD_MEMORY_OUT;
                return(0);
            }
            (*conjuncts)[0] = old[0];
            (*conjuncts)[1] = old[1];
            return(2);
        } else {
            Cudd_RecursiveDeref(dd,old[1]);
            *conjuncts = ABC_ALLOC(DdNode *,1);
            if (*conjuncts == NULL) {
                Cudd_RecursiveDeref(dd,old[0]);
                dd->errorCode = CUDD_MEMORY_OUT;
                return(0);
            }
            (*conjuncts)[0] = old[0];
            return(1);
Alan Mishchenko committed
419 420
        }
    } else {
421
        Cudd_RecursiveDeref(dd,old[0]);
Alan Mishchenko committed
422
        *conjuncts = ABC_ALLOC(DdNode *,1);
Alan Mishchenko committed
423
        if (*conjuncts == NULL) {
424 425 426
            Cudd_RecursiveDeref(dd,old[1]);
            dd->errorCode = CUDD_MEMORY_OUT;
            return(0);
Alan Mishchenko committed
427
        }
428
        (*conjuncts)[0] = old[1];
Alan Mishchenko committed
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
        return(1);
    }

} /* end of Cudd_bddIterConjDecomp */


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

  Synopsis    [Performs two-way disjunctive decomposition of a BDD.]

  Description [Performs two-way disjunctive decomposition of a BDD.
  Returns the number of disjuncts produced, that is, 2 if successful;
  1 if no meaningful decomposition was found; 0 otherwise. The
  disjuncts produced by this procedure tend to be imbalanced.]

  SideEffects [The two disjuncts are returned in an array as side effects.
  The array is allocated by this function. It is the caller's responsibility
446
  to free it. On successful completion, the disjuncts are already
Alan Mishchenko committed
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
  referenced. If the function returns 0, the array for the disjuncts is
  not allocated. If the function returns 1, the only factor equals the
  function to be decomposed.]

  SeeAlso     [Cudd_bddIterConjDecomp Cudd_bddApproxDisjDecomp
  Cudd_bddGenDisjDecomp Cudd_bddVarDisjDecomp]

******************************************************************************/
int
Cudd_bddIterDisjDecomp(
  DdManager * dd /* manager */,
  DdNode * f /* function to be decomposed */,
  DdNode *** disjuncts /* address of the array of the disjuncts */)
{
    int result, i;

    result = Cudd_bddIterConjDecomp(dd,Cudd_Not(f),disjuncts);
    for (i = 0; i < result; i++) {
465
        (*disjuncts)[i] = Cudd_Not((*disjuncts)[i]);
Alan Mishchenko committed
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
    }
    return(result);

} /* end of Cudd_bddIterDisjDecomp */


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

  Synopsis    [Performs two-way conjunctive decomposition of a BDD.]

  Description [Performs two-way conjunctive decomposition of a
  BDD. This procedure owes its name to the fact tht it generalizes the
  decomposition based on the cofactors with respect to one
  variable. Returns the number of conjuncts produced, that is, 2 if
  successful; 1 if no meaningful decomposition was found; 0
  otherwise. The conjuncts produced by this procedure tend to be
  balanced.]

  SideEffects [The two factors are returned in an array as side effects.
  The array is allocated by this function. It is the caller's responsibility
486
  to free it. On successful completion, the conjuncts are already
Alan Mishchenko committed
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
  referenced. If the function returns 0, the array for the conjuncts is
  not allocated. If the function returns 1, the only factor equals the
  function to be decomposed.]

  SeeAlso     [Cudd_bddGenDisjDecomp Cudd_bddApproxConjDecomp
  Cudd_bddIterConjDecomp Cudd_bddVarConjDecomp]

******************************************************************************/
int
Cudd_bddGenConjDecomp(
  DdManager * dd /* manager */,
  DdNode * f /* function to be decomposed */,
  DdNode *** conjuncts /* address of the array of conjuncts */)
{
    int result;
    DdNode *glocal, *hlocal;

    one = DD_ONE(dd);
    zero = Cudd_Not(one);
    
    do {
508 509
        dd->reordered = 0;
        result = cuddConjunctsAux(dd, f, &glocal, &hlocal);
Alan Mishchenko committed
510 511 512
    } while (dd->reordered == 1);

    if (result == 0) {
513
        return(0);
Alan Mishchenko committed
514 515 516
    }

    if (glocal != one) {
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
        if (hlocal != one) {
            *conjuncts = ABC_ALLOC(DdNode *,2);
            if (*conjuncts == NULL) {
                Cudd_RecursiveDeref(dd,glocal);
                Cudd_RecursiveDeref(dd,hlocal);
                dd->errorCode = CUDD_MEMORY_OUT;
                return(0);
            }
            (*conjuncts)[0] = glocal;
            (*conjuncts)[1] = hlocal;
            return(2);
        } else {
            Cudd_RecursiveDeref(dd,hlocal);
            *conjuncts = ABC_ALLOC(DdNode *,1);
            if (*conjuncts == NULL) {
                Cudd_RecursiveDeref(dd,glocal);
                dd->errorCode = CUDD_MEMORY_OUT;
                return(0);
            }
            (*conjuncts)[0] = glocal;
            return(1);
Alan Mishchenko committed
538 539
        }
    } else {
540
        Cudd_RecursiveDeref(dd,glocal);
Alan Mishchenko committed
541
        *conjuncts = ABC_ALLOC(DdNode *,1);
Alan Mishchenko committed
542
        if (*conjuncts == NULL) {
543 544 545
            Cudd_RecursiveDeref(dd,hlocal);
            dd->errorCode = CUDD_MEMORY_OUT;
            return(0);
Alan Mishchenko committed
546
        }
547
        (*conjuncts)[0] = hlocal;
Alan Mishchenko committed
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
        return(1);
    }

} /* end of Cudd_bddGenConjDecomp */


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

  Synopsis    [Performs two-way disjunctive decomposition of a BDD.]

  Description [Performs two-way disjunctive decomposition of a BDD.
  Returns the number of disjuncts produced, that is, 2 if successful;
  1 if no meaningful decomposition was found; 0 otherwise. The
  disjuncts produced by this procedure tend to be balanced.]

  SideEffects [The two disjuncts are returned in an array as side effects.
  The array is allocated by this function. It is the caller's responsibility
565
  to free it. On successful completion, the disjuncts are already
Alan Mishchenko committed
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
  referenced. If the function returns 0, the array for the disjuncts is
  not allocated. If the function returns 1, the only factor equals the
  function to be decomposed.]

  SeeAlso     [Cudd_bddGenConjDecomp Cudd_bddApproxDisjDecomp
  Cudd_bddIterDisjDecomp Cudd_bddVarDisjDecomp]

******************************************************************************/
int
Cudd_bddGenDisjDecomp(
  DdManager * dd /* manager */,
  DdNode * f /* function to be decomposed */,
  DdNode *** disjuncts /* address of the array of the disjuncts */)
{
    int result, i;

    result = Cudd_bddGenConjDecomp(dd,Cudd_Not(f),disjuncts);
    for (i = 0; i < result; i++) {
584
        (*disjuncts)[i] = Cudd_Not((*disjuncts)[i]);
Alan Mishchenko committed
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
    }
    return(result);

} /* end of Cudd_bddGenDisjDecomp */


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

  Synopsis    [Performs two-way conjunctive decomposition of a BDD.]

  Description [Conjunctively decomposes one BDD according to a
  variable.  If <code>f</code> is the function of the BDD and
  <code>x</code> is the variable, the decomposition is
  <code>(f+x)(f+x')</code>.  The variable is chosen so as to balance
  the sizes of the two conjuncts and to keep them small.  Returns the
  number of conjuncts produced, that is, 2 if successful; 1 if no
  meaningful decomposition was found; 0 otherwise.]

  SideEffects [The two factors are returned in an array as side effects.
  The array is allocated by this function. It is the caller's responsibility
605
  to free it. On successful completion, the conjuncts are already
Alan Mishchenko committed
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
  referenced. If the function returns 0, the array for the conjuncts is
  not allocated. If the function returns 1, the only factor equals the
  function to be decomposed.]

  SeeAlso     [Cudd_bddVarDisjDecomp Cudd_bddGenConjDecomp
  Cudd_bddApproxConjDecomp Cudd_bddIterConjDecomp]

*****************************************************************************/
int
Cudd_bddVarConjDecomp(
  DdManager * dd /* manager */,
  DdNode * f /* function to be decomposed */,
  DdNode *** conjuncts /* address of the array of conjuncts */)
{
    int best;
    int min;
    DdNode *support, *scan, *var, *glocal, *hlocal;

    /* Find best cofactoring variable. */
    support = Cudd_Support(dd,f);
    if (support == NULL) return(0);
    if (Cudd_IsConstant(support)) {
628 629 630 631 632 633 634 635
        *conjuncts = ABC_ALLOC(DdNode *,1);
        if (*conjuncts == NULL) {
            dd->errorCode = CUDD_MEMORY_OUT;
            return(0);
        }
        (*conjuncts)[0] = f;
        cuddRef((*conjuncts)[0]);
        return(1);
Alan Mishchenko committed
636 637 638 639 640 641
    }
    cuddRef(support);
    min = 1000000000;
    best = -1;
    scan = support;
    while (!Cudd_IsConstant(scan)) {
642 643 644 645 646 647 648 649 650 651
        int i = scan->index;
        int est1 = Cudd_EstimateCofactor(dd,f,i,1);
        int est0 = Cudd_EstimateCofactor(dd,f,i,0);
        /* Minimize the size of the larger of the two cofactors. */
        int est = (est1 > est0) ? est1 : est0;
        if (est < min) {
            min = est;
            best = i;
        }
        scan = cuddT(scan);
Alan Mishchenko committed
652 653 654 655 656 657 658 659 660
    }
#ifdef DD_DEBUG
    assert(best >= 0 && best < dd->size);
#endif
    Cudd_RecursiveDeref(dd,support);

    var = Cudd_bddIthVar(dd,best);
    glocal = Cudd_bddOr(dd,f,var);
    if (glocal == NULL) {
661
        return(0);
Alan Mishchenko committed
662 663 664 665
    }
    cuddRef(glocal);
    hlocal = Cudd_bddOr(dd,f,Cudd_Not(var));
    if (hlocal == NULL) {
666 667
        Cudd_RecursiveDeref(dd,glocal);
        return(0);
Alan Mishchenko committed
668 669 670 671
    }
    cuddRef(hlocal);

    if (glocal != DD_ONE(dd)) {
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
        if (hlocal != DD_ONE(dd)) {
            *conjuncts = ABC_ALLOC(DdNode *,2);
            if (*conjuncts == NULL) {
                Cudd_RecursiveDeref(dd,glocal);
                Cudd_RecursiveDeref(dd,hlocal);
                dd->errorCode = CUDD_MEMORY_OUT;
                return(0);
            }
            (*conjuncts)[0] = glocal;
            (*conjuncts)[1] = hlocal;
            return(2);
        } else {
            Cudd_RecursiveDeref(dd,hlocal);
            *conjuncts = ABC_ALLOC(DdNode *,1);
            if (*conjuncts == NULL) {
                Cudd_RecursiveDeref(dd,glocal);
                dd->errorCode = CUDD_MEMORY_OUT;
                return(0);
            }
            (*conjuncts)[0] = glocal;
            return(1);
Alan Mishchenko committed
693 694
        }
    } else {
695
        Cudd_RecursiveDeref(dd,glocal);
Alan Mishchenko committed
696
        *conjuncts = ABC_ALLOC(DdNode *,1);
Alan Mishchenko committed
697
        if (*conjuncts == NULL) {
698 699 700
            Cudd_RecursiveDeref(dd,hlocal);
            dd->errorCode = CUDD_MEMORY_OUT;
            return(0);
Alan Mishchenko committed
701
        }
702
        (*conjuncts)[0] = hlocal;
Alan Mishchenko committed
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
        return(1);
    }

} /* end of Cudd_bddVarConjDecomp */


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

  Synopsis    [Performs two-way disjunctive decomposition of a BDD.]

  Description [Performs two-way disjunctive decomposition of a BDD
  according to a variable. If <code>f</code> is the function of the
  BDD and <code>x</code> is the variable, the decomposition is
  <code>f*x + f*x'</code>.  The variable is chosen so as to balance
  the sizes of the two disjuncts and to keep them small.  Returns the
  number of disjuncts produced, that is, 2 if successful; 1 if no
  meaningful decomposition was found; 0 otherwise.]

  SideEffects [The two disjuncts are returned in an array as side effects.
  The array is allocated by this function. It is the caller's responsibility
723
  to free it. On successful completion, the disjuncts are already
Alan Mishchenko committed
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
  referenced. If the function returns 0, the array for the disjuncts is
  not allocated. If the function returns 1, the only factor equals the
  function to be decomposed.]

  SeeAlso     [Cudd_bddVarConjDecomp Cudd_bddApproxDisjDecomp
  Cudd_bddIterDisjDecomp Cudd_bddGenDisjDecomp]

******************************************************************************/
int
Cudd_bddVarDisjDecomp(
  DdManager * dd /* manager */,
  DdNode * f /* function to be decomposed */,
  DdNode *** disjuncts /* address of the array of the disjuncts */)
{
    int result, i;

    result = Cudd_bddVarConjDecomp(dd,Cudd_Not(f),disjuncts);
    for (i = 0; i < result; i++) {
742
        (*disjuncts)[i] = Cudd_Not((*disjuncts)[i]);
Alan Mishchenko committed
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
    }
    return(result);

} /* end of Cudd_bddVarDisjDecomp */


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

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


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

  Synopsis    [Get longest distance of node from constant.]

  Description [Get longest distance of node from constant. Returns the
  distance of the root from the constant if successful; CUDD_OUT_OF_MEM
  otherwise.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static NodeStat *
CreateBotDist(
  DdNode * node,
  st_table * distanceTable)
{
    DdNode *N, *Nv, *Nnv;
    int distance, distanceNv, distanceNnv;
    NodeStat *nodeStat, *nodeStatNv, *nodeStatNnv;

#if 0
    if (Cudd_IsConstant(node)) {
782
        return(0);
Alan Mishchenko committed
783 784 785 786 787
    }
#endif
    
    /* Return the entry in the table if found. */
    N = Cudd_Regular(node);
788 789 790
    if (st_lookup(distanceTable, (const char *)N, (char **)&nodeStat)) {
        nodeStat->localRef++;
        return(nodeStat);
Alan Mishchenko committed
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
    }

    Nv = cuddT(N);
    Nnv = cuddE(N);
    Nv = Cudd_NotCond(Nv, Cudd_IsComplement(node));
    Nnv = Cudd_NotCond(Nnv, Cudd_IsComplement(node));

    /* Recur on the children. */
    nodeStatNv = CreateBotDist(Nv, distanceTable);
    if (nodeStatNv == NULL) return(NULL);
    distanceNv = nodeStatNv->distance;

    nodeStatNnv = CreateBotDist(Nnv, distanceTable);
    if (nodeStatNnv == NULL) return(NULL);
    distanceNnv = nodeStatNnv->distance;
    /* Store max distance from constant; note sometimes this distance
    ** may be to 0.
    */
    distance = (distanceNv > distanceNnv) ? (distanceNv+1) : (distanceNnv + 1);

Alan Mishchenko committed
811
    nodeStat = ABC_ALLOC(NodeStat, 1);
Alan Mishchenko committed
812
    if (nodeStat == NULL) {
813
        return(0);
Alan Mishchenko committed
814 815 816 817 818
    }
    nodeStat->distance = distance;
    nodeStat->localRef = 1;
    
    if (st_insert(distanceTable, (char *)N, (char *)nodeStat) ==
819 820
        ST_OUT_OF_MEM) {
        return(0);
Alan Mishchenko committed
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

    }
    return(nodeStat);

} /* end of CreateBotDist */


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

  Synopsis    [Count the number of minterms of each node ina a BDD and
  store it in a hash table.]

  Description []

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static double
CountMinterms(
  DdNode * node,
  double  max,
  st_table * mintermTable,
  FILE *fp)
{
    DdNode *N, *Nv, *Nnv;
    double min, minNv, minNnv;
    double *dummy;

    N = Cudd_Regular(node);

    if (cuddIsConstant(N)) {
854 855 856 857 858
        if (node == zero) {
            return(0);
        } else {
            return(max);
        }
Alan Mishchenko committed
859 860 861
    }

    /* Return the entry in the table if found. */
862 863 864
    if (st_lookup(mintermTable, (const char *)node, (char **)&dummy)) {
        min = *dummy;
        return(min);
Alan Mishchenko committed
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
    }

    Nv = cuddT(N);
    Nnv = cuddE(N);
    Nv = Cudd_NotCond(Nv, Cudd_IsComplement(node));
    Nnv = Cudd_NotCond(Nnv, Cudd_IsComplement(node));

    /* Recur on the children. */
    minNv = CountMinterms(Nv, max, mintermTable, fp);
    if (minNv == -1.0) return(-1.0);
    minNnv = CountMinterms(Nnv, max, mintermTable, fp);
    if (minNnv == -1.0) return(-1.0);
    min = minNv / 2.0 + minNnv / 2.0;
    /* store 
     */

Alan Mishchenko committed
881
    dummy = ABC_ALLOC(double, 1);
Alan Mishchenko committed
882 883 884
    if (dummy == NULL) return(-1.0);
    *dummy = min;
    if (st_insert(mintermTable, (char *)node, (char *)dummy) == ST_OUT_OF_MEM) {
885
        (void) fprintf(fp, "st table insert failed\n");
Alan Mishchenko committed
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
    }
    return(min);

} /* end of CountMinterms */


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

  Synopsis    [Free factors structure]

  Description []

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static void
ConjunctsFree(
  DdManager * dd,
  Conjuncts * factors)
{
    Cudd_RecursiveDeref(dd, factors->g);
    Cudd_RecursiveDeref(dd, factors->h);
Alan Mishchenko committed
910
    ABC_FREE(factors);
Alan Mishchenko committed
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
    return;

} /* end of ConjunctsFree */


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

  Synopsis    [Check whether the given pair is in the tables.]

  Description [.Check whether the given pair is in the tables.  gTable
  and hTable are combined.
  absence in both is indicated by 0,
  presence in gTable is indicated by 1,
  presence in hTable by 2 and
  presence in both by 3.
  The values returned by this function are PAIR_ST,
  PAIR_CR, G_ST, G_CR, H_ST, H_CR, BOTH_G, BOTH_H, NONE.
  PAIR_ST implies g in gTable and h in hTable
  PAIR_CR implies g in hTable and h in gTable
  G_ST implies g in gTable and h not in any table
  G_CR implies g in hTable and h not in any table
  H_ST implies h in hTable and g not in any table
  H_CR implies h in gTable and g not in any table
  BOTH_G implies both in gTable
  BOTH_H implies both in hTable
  NONE implies none in table; ]

  SideEffects []

  SeeAlso     [CheckTablesCacheAndReturn CheckInTables]

******************************************************************************/
static int
PairInTables(
  DdNode * g,
  DdNode * h,
  st_table * ghTable)
{
    int valueG, valueH, gPresent, hPresent;

    valueG = valueH = gPresent = hPresent = 0;
    
    gPresent = st_lookup_int(ghTable, (char *)Cudd_Regular(g), &valueG);
    hPresent = st_lookup_int(ghTable, (char *)Cudd_Regular(h), &valueH);

    if (!gPresent && !hPresent) return(NONE);

    if (!hPresent) {
959 960
        if (valueG & 1) return(G_ST);
        if (valueG & 2) return(G_CR);
Alan Mishchenko committed
961 962
    }
    if (!gPresent) {
963 964
        if (valueH & 1) return(H_CR);
        if (valueH & 2) return(H_ST);
Alan Mishchenko committed
965 966 967 968 969 970
    }
    /* both in tables */
    if ((valueG & 1) && (valueH & 2)) return(PAIR_ST);
    if ((valueG & 2) && (valueH & 1)) return(PAIR_CR);
    
    if (valueG & 1) {
971
        return(BOTH_G);
Alan Mishchenko committed
972
    } else {
973
        return(BOTH_H);
Alan Mishchenko committed
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
    }
    
} /* end of PairInTables */


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

  Synopsis    [Check the tables for the existence of pair and return one
  combination, cache the result.]

  Description [Check the tables for the existence of pair and return
  one combination, cache the result. The assumption is that one of the
  conjuncts is already in the tables.]

  SideEffects [g and h referenced for the cache]

  SeeAlso     [ZeroCase]

******************************************************************************/
static Conjuncts *
CheckTablesCacheAndReturn(
  DdNode * node,
  DdNode * g,
  DdNode * h,
  st_table * ghTable,
  st_table * cacheTable)
{
    int pairValue;
    int value;
    Conjuncts *factors;
    
    value = 0;
    /* check tables */
    pairValue = PairInTables(g, h, ghTable);
    assert(pairValue != NONE);
    /* if both dont exist in table, we know one exists(either g or h).
     * Therefore store the other and proceed
     */
Alan Mishchenko committed
1012
    factors = ABC_ALLOC(Conjuncts, 1);
Alan Mishchenko committed
1013 1014
    if (factors == NULL) return(NULL);
    if ((pairValue == BOTH_H) || (pairValue == H_ST)) {
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
        if (g != one) {
            value = 0;
            if (st_lookup_int(ghTable, (char *)Cudd_Regular(g), &value)) {
                value |= 1;
            } else {
                value = 1;
            }
            if (st_insert(ghTable, (char *)Cudd_Regular(g),
                          (char *)(long)value) == ST_OUT_OF_MEM) {
                return(NULL);
            }
Alan Mishchenko committed
1026
        }
1027 1028
        factors->g = g;
        factors->h = h;
Alan Mishchenko committed
1029
    } else  if ((pairValue == BOTH_G) || (pairValue == G_ST)) {
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
        if (h != one) {
            value = 0;
            if (st_lookup_int(ghTable, (char *)Cudd_Regular(h), &value)) {
                value |= 2;
            } else {
                value = 2;
            }
            if (st_insert(ghTable, (char *)Cudd_Regular(h),
                          (char *)(long)value) == ST_OUT_OF_MEM) {
                return(NULL);
            }
Alan Mishchenko committed
1041
        }
1042 1043
        factors->g = g;
        factors->h = h;
Alan Mishchenko committed
1044
    } else if (pairValue == H_CR) {
1045 1046 1047 1048 1049 1050
        if (g != one) {
            value = 2;
            if (st_insert(ghTable, (char *)Cudd_Regular(g),
                          (char *)(long)value) == ST_OUT_OF_MEM) {
                return(NULL);
            }
Alan Mishchenko committed
1051
        }
1052 1053
        factors->g = h;
        factors->h = g;
Alan Mishchenko committed
1054
    } else if (pairValue == G_CR) {
1055 1056 1057 1058 1059 1060
        if (h != one) {
            value = 1;
            if (st_insert(ghTable, (char *)Cudd_Regular(h),
                          (char *)(long)value) == ST_OUT_OF_MEM) {
                return(NULL);
            }
Alan Mishchenko committed
1061
        }
1062 1063
        factors->g = h;
        factors->h = g;
Alan Mishchenko committed
1064 1065
    } else if (pairValue == PAIR_CR) {
    /* pair exists in table */
1066 1067
        factors->g = h;
        factors->h = g;
Alan Mishchenko committed
1068
    } else if (pairValue == PAIR_ST) {
1069 1070
        factors->g = g;
        factors->h = h;
Alan Mishchenko committed
1071
    }
1072
            
Alan Mishchenko committed
1073 1074
    /* cache the result for this node */
    if (st_insert(cacheTable, (char *)node, (char *)factors) == ST_OUT_OF_MEM) {
1075 1076
        ABC_FREE(factors);
        return(NULL);
Alan Mishchenko committed
1077 1078 1079 1080 1081
    }

    return(factors);

} /* end of CheckTablesCacheAndReturn */
1082
        
Alan Mishchenko committed
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
/**Function********************************************************************

  Synopsis    [Check the tables for the existence of pair and return one
  combination, store in cache.]

  Description [Check the tables for the existence of pair and return
  one combination, store in cache. The pair that has more pointers to
  it is picked. An approximation of the number of local pointers is
  made by taking the reference count of the pairs sent. ]

  SideEffects []

  SeeAlso     [ZeroCase BuildConjuncts]

******************************************************************************/
static Conjuncts *
PickOnePair(
  DdNode * node,
  DdNode * g1,
  DdNode * h1,
  DdNode * g2,
  DdNode * h2,
  st_table * ghTable,
  st_table * cacheTable)
{
    int value;
    Conjuncts *factors;
    int oneRef, twoRef;
    
Alan Mishchenko committed
1112
    factors = ABC_ALLOC(Conjuncts, 1);
Alan Mishchenko committed
1113 1114 1115 1116
    if (factors == NULL) return(NULL);

    /* count the number of pointers to pair 2 */
    if (h2 == one) {
1117
        twoRef = (Cudd_Regular(g2))->ref;
Alan Mishchenko committed
1118
    } else if (g2 == one) {
1119
        twoRef = (Cudd_Regular(h2))->ref;
Alan Mishchenko committed
1120
    } else {
1121
        twoRef = ((Cudd_Regular(g2))->ref + (Cudd_Regular(h2))->ref)/2;
Alan Mishchenko committed
1122 1123 1124 1125
    }

    /* count the number of pointers to pair 1 */
    if (h1 == one) {
1126
        oneRef  = (Cudd_Regular(g1))->ref;
Alan Mishchenko committed
1127
    } else if (g1 == one) {
1128
        oneRef  = (Cudd_Regular(h1))->ref;
Alan Mishchenko committed
1129
    } else {
1130
        oneRef = ((Cudd_Regular(g1))->ref + (Cudd_Regular(h1))->ref)/2;
Alan Mishchenko committed
1131 1132 1133 1134
    }

    /* pick the pair with higher reference count */
    if (oneRef >= twoRef) {
1135 1136
        factors->g = g1;
        factors->h = h1;
Alan Mishchenko committed
1137
    } else {
1138 1139
        factors->g = g2;
        factors->h = h2;
Alan Mishchenko committed
1140 1141 1142 1143 1144 1145 1146
    }
    
    /*
     * Store computed factors in respective tables to encourage
     * recombination.
     */
    if (factors->g != one) {
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
        /* insert g in htable */
        value = 0;
        if (st_lookup_int(ghTable, (char *)Cudd_Regular(factors->g), &value)) {
            if (value == 2) {
                value |= 1;
                if (st_insert(ghTable, (char *)Cudd_Regular(factors->g),
                              (char *)(long)value) == ST_OUT_OF_MEM) {
                    ABC_FREE(factors);
                    return(NULL);
                }
            }
        } else {
            value = 1;
            if (st_insert(ghTable, (char *)Cudd_Regular(factors->g),
                          (char *)(long)value) == ST_OUT_OF_MEM) {
                ABC_FREE(factors);
                return(NULL);
            }
Alan Mishchenko committed
1165 1166 1167 1168
        }
    }

    if (factors->h != one) {
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
        /* insert h in htable */
        value = 0;
        if (st_lookup_int(ghTable, (char *)Cudd_Regular(factors->h), &value)) {
            if (value == 1) {
                value |= 2;
                if (st_insert(ghTable, (char *)Cudd_Regular(factors->h),
                              (char *)(long)value) == ST_OUT_OF_MEM) {
                    ABC_FREE(factors);
                    return(NULL);
                }
            }       
        } else {
            value = 2;
            if (st_insert(ghTable, (char *)Cudd_Regular(factors->h),
                          (char *)(long)value) == ST_OUT_OF_MEM) {
                ABC_FREE(factors);
                return(NULL);
            }
Alan Mishchenko committed
1187 1188 1189 1190 1191
        }
    }
    
    /* Store factors in cache table for later use. */
    if (st_insert(cacheTable, (char *)node, (char *)factors) ==
1192 1193 1194
            ST_OUT_OF_MEM) {
        ABC_FREE(factors);
        return(NULL);
Alan Mishchenko committed
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
    }

    return(factors);

} /* end of PickOnePair */


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

  Synopsis [Check if the two pairs exist in the table, If any of the
  conjuncts do exist, store in the cache and return the corresponding pair.]

  Description [Check if the two pairs exist in the table. If any of
  the conjuncts do exist, store in the cache and return the
  corresponding pair.]

  SideEffects []

  SeeAlso     [ZeroCase BuildConjuncts]

******************************************************************************/
static Conjuncts *
CheckInTables(
  DdNode * node,
  DdNode * g1,
  DdNode * h1,
  DdNode * g2,
  DdNode * h2,
  st_table * ghTable,
  st_table * cacheTable,
  int * outOfMem)
{
    int pairValue1,  pairValue2;
    Conjuncts *factors;
    int value;
    
    *outOfMem = 0;

    /* check existence of pair in table */
    pairValue1 = PairInTables(g1, h1, ghTable);
    pairValue2 = PairInTables(g2, h2, ghTable);

    /* if none of the 4 exist in the gh tables, return NULL */
    if ((pairValue1 == NONE) && (pairValue2 == NONE)) {
1239
        return NULL;
Alan Mishchenko committed
1240 1241
    }
    
Alan Mishchenko committed
1242
    factors = ABC_ALLOC(Conjuncts, 1);
Alan Mishchenko committed
1243
    if (factors == NULL) {
1244 1245
        *outOfMem = 1;
        return NULL;
Alan Mishchenko committed
1246 1247 1248 1249
    }

    /* pairs that already exist in the table get preference. */
    if (pairValue1 == PAIR_ST) {
1250 1251
        factors->g = g1;
        factors->h = h1;
Alan Mishchenko committed
1252
    } else if (pairValue2 == PAIR_ST) {
1253 1254
        factors->g = g2;
        factors->h = h2;
Alan Mishchenko committed
1255
    } else if (pairValue1 == PAIR_CR) {
1256 1257
        factors->g = h1;
        factors->h = g1;
Alan Mishchenko committed
1258
    } else if (pairValue2 == PAIR_CR) {
1259 1260
        factors->g = h2;
        factors->h = g2;
Alan Mishchenko committed
1261
    } else if (pairValue1 == G_ST) {
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
        /* g exists in the table, h is not found in either table */
        factors->g = g1;
        factors->h = h1;
        if (h1 != one) {
            value = 2;
            if (st_insert(ghTable, (char *)Cudd_Regular(h1),
                          (char *)(long)value) == ST_OUT_OF_MEM) {
                *outOfMem = 1;
                ABC_FREE(factors);
                return(NULL);
            }
Alan Mishchenko committed
1273 1274
        }
    } else if (pairValue1 == BOTH_G) {
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
        /* g and h are  found in the g table */
        factors->g = g1;
        factors->h = h1;
        if (h1 != one) {
            value = 3;
            if (st_insert(ghTable, (char *)Cudd_Regular(h1),
                          (char *)(long)value) == ST_OUT_OF_MEM) {
                *outOfMem = 1;
                ABC_FREE(factors);
                return(NULL);
            }
Alan Mishchenko committed
1286 1287
        }
    } else if (pairValue1 == H_ST) {
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
        /* h exists in the table, g is not found in either table */
        factors->g = g1;
        factors->h = h1;
        if (g1 != one) {
            value = 1;
            if (st_insert(ghTable, (char *)Cudd_Regular(g1),
                          (char *)(long)value) == ST_OUT_OF_MEM) {
                *outOfMem = 1;
                ABC_FREE(factors);
                return(NULL);
            }
Alan Mishchenko committed
1299 1300
        }
    } else if (pairValue1 == BOTH_H) {
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
        /* g and h are  found in the h table */
        factors->g = g1;
        factors->h = h1;
        if (g1 != one) {
            value = 3;
            if (st_insert(ghTable, (char *)Cudd_Regular(g1),
                          (char *)(long)value) == ST_OUT_OF_MEM) {
                *outOfMem = 1;
                ABC_FREE(factors);
                return(NULL);
            }
Alan Mishchenko committed
1312 1313
        }
    } else if (pairValue2 == G_ST) {
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
        /* g exists in the table, h is not found in either table */
        factors->g = g2;
        factors->h = h2;
        if (h2 != one) {
            value = 2;
            if (st_insert(ghTable, (char *)Cudd_Regular(h2),
                          (char *)(long)value) == ST_OUT_OF_MEM) {
                *outOfMem = 1;
                ABC_FREE(factors);
                return(NULL);
            }
Alan Mishchenko committed
1325 1326
        }
    } else if  (pairValue2 == BOTH_G) {
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
        /* g and h are  found in the g table */
        factors->g = g2;
        factors->h = h2;
        if (h2 != one) {
            value = 3;
            if (st_insert(ghTable, (char *)Cudd_Regular(h2),
                          (char *)(long)value) == ST_OUT_OF_MEM) {
                *outOfMem = 1;
                ABC_FREE(factors);
                return(NULL);
            }
Alan Mishchenko committed
1338 1339
        }
    } else if (pairValue2 == H_ST) { 
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
        /* h exists in the table, g is not found in either table */
        factors->g = g2;
        factors->h = h2;
        if (g2 != one) {
            value = 1;
            if (st_insert(ghTable, (char *)Cudd_Regular(g2),
                          (char *)(long)value) == ST_OUT_OF_MEM) {
                *outOfMem = 1;
                ABC_FREE(factors);
                return(NULL);
            }
Alan Mishchenko committed
1351 1352
        }
    } else if (pairValue2 == BOTH_H) {
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363
        /* g and h are  found in the h table */
        factors->g = g2;
        factors->h = h2;
        if (g2 != one) {
            value = 3;
            if (st_insert(ghTable, (char *)Cudd_Regular(g2),
                          (char *)(long)value) == ST_OUT_OF_MEM) {
                *outOfMem = 1;
                ABC_FREE(factors);
                return(NULL);
            }
Alan Mishchenko committed
1364 1365
        }
    } else if (pairValue1 == G_CR) {
1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376
        /* g found in h table and h in none */
        factors->g = h1;
        factors->h = g1;
        if (h1 != one) {
            value = 1;
            if (st_insert(ghTable, (char *)Cudd_Regular(h1),
                          (char *)(long)value) == ST_OUT_OF_MEM) {
                *outOfMem = 1;
                ABC_FREE(factors);
                return(NULL);
            }
Alan Mishchenko committed
1377 1378
        }
    } else if (pairValue1 == H_CR) {
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
        /* h found in g table and g in none */
        factors->g = h1;
        factors->h = g1;
        if (g1 != one) {
            value = 2;
            if (st_insert(ghTable, (char *)Cudd_Regular(g1),
                          (char *)(long)value) == ST_OUT_OF_MEM) {
                *outOfMem = 1;
                ABC_FREE(factors);
                return(NULL);
            }
Alan Mishchenko committed
1390 1391
        }
    } else if (pairValue2 == G_CR) {
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
        /* g found in h table and h in none */
        factors->g = h2;
        factors->h = g2;
        if (h2 != one) {
            value = 1;
            if (st_insert(ghTable, (char *)Cudd_Regular(h2),
                          (char *)(long)value) == ST_OUT_OF_MEM) {
                *outOfMem = 1;
                ABC_FREE(factors);
                return(NULL);
            }
Alan Mishchenko committed
1403 1404
        }
    } else if (pairValue2 == H_CR) {
1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415
        /* h found in g table and g in none */
        factors->g = h2;
        factors->h = g2;
        if (g2 != one) {
            value = 2;
            if (st_insert(ghTable, (char *)Cudd_Regular(g2),
                          (char *)(long)value) == ST_OUT_OF_MEM) {
                *outOfMem = 1;
                ABC_FREE(factors);
                return(NULL);
            }
Alan Mishchenko committed
1416 1417 1418 1419 1420
        }
    }
    
    /* Store factors in cache table for later use. */
    if (st_insert(cacheTable, (char *)node, (char *)factors) ==
1421 1422 1423 1424
            ST_OUT_OF_MEM) {
        *outOfMem = 1;
        ABC_FREE(factors);
        return(NULL);
Alan Mishchenko committed
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
    }
    return factors;
} /* end of CheckInTables */



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

  Synopsis    [If one child is zero, do explicitly what Restrict does or better]

  Description [If one child is zero, do explicitly what Restrict does or better.
  First separate a variable and its child in the base case. In case of a cube
  times a function, separate the cube and function. As a last resort, look in
  tables.]

  SideEffects [Frees the BDDs in factorsNv. factorsNv itself is not freed
  because it is freed above.]

  SeeAlso     [BuildConjuncts]

******************************************************************************/
static Conjuncts *
ZeroCase(
  DdManager * dd,
  DdNode * node,
  Conjuncts * factorsNv,
  st_table * ghTable,
  st_table * cacheTable,
  int switched)
{
    int topid;
    DdNode *g, *h, *g1, *g2, *h1, *h2, *x, *N, *G, *H, *Gv, *Gnv;
    DdNode *Hv, *Hnv;
    int value;
    int outOfMem;
    Conjuncts *factors;
    
    /* get var at this node */
    N = Cudd_Regular(node);
    topid = N->index;
    x = dd->vars[topid];
    x = (switched) ? Cudd_Not(x): x;
    cuddRef(x);

    /* Seprate variable and child */
    if (factorsNv->g == one) {
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500
        Cudd_RecursiveDeref(dd, factorsNv->g);
        factors = ABC_ALLOC(Conjuncts, 1);
        if (factors == NULL) {
            dd->errorCode = CUDD_MEMORY_OUT;
            Cudd_RecursiveDeref(dd, factorsNv->h);
            Cudd_RecursiveDeref(dd, x);
            return(NULL);
        }
        factors->g = x;
        factors->h = factorsNv->h;
        /* cache the result*/
        if (st_insert(cacheTable, (char *)node, (char *)factors) == ST_OUT_OF_MEM) {
            dd->errorCode = CUDD_MEMORY_OUT;
            Cudd_RecursiveDeref(dd, factorsNv->h); 
            Cudd_RecursiveDeref(dd, x);
            ABC_FREE(factors);
            return NULL;
        }
        
        /* store  x in g table, the other node is already in the table */
        if (st_lookup_int(ghTable, (char *)Cudd_Regular(x), &value)) {
            value |= 1;
        } else {
            value = 1;
        }
        if (st_insert(ghTable, (char *)Cudd_Regular(x), (char *)(long)value) == ST_OUT_OF_MEM) {
            dd->errorCode = CUDD_MEMORY_OUT;
            return NULL;
        }
        return(factors);
Alan Mishchenko committed
1501 1502 1503 1504
    }
    
    /* Seprate variable and child */
    if (factorsNv->h == one) {
1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533
        Cudd_RecursiveDeref(dd, factorsNv->h);
        factors = ABC_ALLOC(Conjuncts, 1);
        if (factors == NULL) {
            dd->errorCode = CUDD_MEMORY_OUT;
            Cudd_RecursiveDeref(dd, factorsNv->g);
            Cudd_RecursiveDeref(dd, x);
            return(NULL);
        }
        factors->g = factorsNv->g;
        factors->h = x;
        /* cache the result. */
        if (st_insert(cacheTable, (char *)node, (char *)factors) == ST_OUT_OF_MEM) {
            dd->errorCode = CUDD_MEMORY_OUT;
            Cudd_RecursiveDeref(dd, factorsNv->g);
            Cudd_RecursiveDeref(dd, x);
            ABC_FREE(factors);
            return(NULL);
        }
        /* store x in h table,  the other node is already in the table */
        if (st_lookup_int(ghTable, (char *)Cudd_Regular(x), &value)) {
            value |= 2;
        } else {
            value = 2;
        }
        if (st_insert(ghTable, (char *)Cudd_Regular(x), (char *)(long)value) == ST_OUT_OF_MEM) {
            dd->errorCode = CUDD_MEMORY_OUT;
            return NULL;
        }
        return(factors);
Alan Mishchenko committed
1534 1535 1536 1537 1538 1539 1540 1541 1542
    }

    G = Cudd_Regular(factorsNv->g);
    Gv = cuddT(G);
    Gnv = cuddE(G);
    Gv = Cudd_NotCond(Gv, Cudd_IsComplement(node));
    Gnv = Cudd_NotCond(Gnv, Cudd_IsComplement(node));
    /* if the child below is a variable */
    if ((Gv == zero) || (Gnv == zero)) {
1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565
        h = factorsNv->h;
        g = cuddBddAndRecur(dd, x, factorsNv->g);
        if (g != NULL)  cuddRef(g);
        Cudd_RecursiveDeref(dd, factorsNv->g); 
        Cudd_RecursiveDeref(dd, x);
        if (g == NULL) {
            Cudd_RecursiveDeref(dd, factorsNv->h); 
            return NULL;
        }
        /* CheckTablesCacheAndReturn responsible for allocating
         * factors structure., g,h referenced for cache store  the
         */
        factors = CheckTablesCacheAndReturn(node,
                                            g,
                                            h,
                                            ghTable,
                                            cacheTable);
        if (factors == NULL) {
            dd->errorCode = CUDD_MEMORY_OUT;
            Cudd_RecursiveDeref(dd, g);
            Cudd_RecursiveDeref(dd, h);
        }
        return(factors); 
Alan Mishchenko committed
1566 1567 1568 1569 1570 1571 1572 1573 1574
    }

    H = Cudd_Regular(factorsNv->h);
    Hv = cuddT(H);
    Hnv = cuddE(H);
    Hv = Cudd_NotCond(Hv, Cudd_IsComplement(node));
    Hnv = Cudd_NotCond(Hnv, Cudd_IsComplement(node));
    /* if the child below is a variable */
    if ((Hv == zero) || (Hnv == zero)) {
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597
        g = factorsNv->g;
        h = cuddBddAndRecur(dd, x, factorsNv->h);
        if (h!= NULL) cuddRef(h);
        Cudd_RecursiveDeref(dd, factorsNv->h);
        Cudd_RecursiveDeref(dd, x);
        if (h == NULL) {
            Cudd_RecursiveDeref(dd, factorsNv->g);
            return NULL;
        }
        /* CheckTablesCacheAndReturn responsible for allocating
         * factors structure.g,h referenced for table store 
         */
        factors = CheckTablesCacheAndReturn(node,
                                            g,
                                            h,
                                            ghTable,
                                            cacheTable);
        if (factors == NULL) {
            dd->errorCode = CUDD_MEMORY_OUT;
            Cudd_RecursiveDeref(dd, g);
            Cudd_RecursiveDeref(dd, h);
        }
        return(factors); 
Alan Mishchenko committed
1598 1599 1600 1601 1602 1603 1604 1605 1606
    }

    /* build g1 = x*g; h1 = h */
    /* build g2 = g; h2 = x*h */
    Cudd_RecursiveDeref(dd, x);
    h1 = factorsNv->h;
    g1 = cuddBddAndRecur(dd, x, factorsNv->g);
    if (g1 != NULL) cuddRef(g1);
    if (g1 == NULL) {
1607 1608 1609
        Cudd_RecursiveDeref(dd, factorsNv->g); 
        Cudd_RecursiveDeref(dd, factorsNv->h);
        return NULL;
Alan Mishchenko committed
1610 1611 1612 1613 1614 1615
    }
    
    g2 = factorsNv->g;
    h2 = cuddBddAndRecur(dd, x, factorsNv->h);
    if (h2 != NULL) cuddRef(h2);
    if (h2 == NULL) {
1616 1617 1618
        Cudd_RecursiveDeref(dd, factorsNv->h);
        Cudd_RecursiveDeref(dd, factorsNv->g);
        return NULL;
Alan Mishchenko committed
1619 1620 1621 1622 1623
    }

    /* check whether any pair is in tables */
    factors = CheckInTables(node, g1, h1, g2, h2, ghTable, cacheTable, &outOfMem);
    if (outOfMem) {
1624
        dd->errorCode = CUDD_MEMORY_OUT;
Alan Mishchenko committed
1625 1626
        Cudd_RecursiveDeref(dd, g1);
        Cudd_RecursiveDeref(dd, h1);
1627 1628 1629
        Cudd_RecursiveDeref(dd, g2);
        Cudd_RecursiveDeref(dd, h2);
        return NULL;
Alan Mishchenko committed
1630
    }
1631 1632 1633 1634 1635 1636 1637 1638 1639
    if (factors != NULL) {
        if ((factors->g == g1) || (factors->g == h1)) {
            Cudd_RecursiveDeref(dd, g2);
            Cudd_RecursiveDeref(dd, h2);
        } else {
            Cudd_RecursiveDeref(dd, g1);
            Cudd_RecursiveDeref(dd, h1);
        }
        return factors;
Alan Mishchenko committed
1640 1641 1642 1643 1644
    }

    /* check for each pair in tables and choose one */
    factors = PickOnePair(node,g1, h1, g2, h2, ghTable, cacheTable);
    if (factors == NULL) {
1645 1646 1647
        dd->errorCode = CUDD_MEMORY_OUT;
        Cudd_RecursiveDeref(dd, g1);
        Cudd_RecursiveDeref(dd, h1);
Alan Mishchenko committed
1648 1649 1650
        Cudd_RecursiveDeref(dd, g2);
        Cudd_RecursiveDeref(dd, h2);
    } else {
1651 1652 1653 1654 1655 1656 1657 1658
        /* now free what was created and not used */
        if ((factors->g == g1) || (factors->g == h1)) {
            Cudd_RecursiveDeref(dd, g2);
            Cudd_RecursiveDeref(dd, h2);
        } else {
            Cudd_RecursiveDeref(dd, g1);
            Cudd_RecursiveDeref(dd, h1);
        }
Alan Mishchenko committed
1659
    }
1660
        
Alan Mishchenko committed
1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695
    return(factors);
} /* end of ZeroCase */


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

  Synopsis    [Builds the conjuncts recursively, bottom up.]

  Description [Builds the conjuncts recursively, bottom up. Constants
  are returned as (f, f). The cache is checked for previously computed
  result. The decomposition points are determined by the local
  reference count of this node and the longest distance from the
  constant. At the decomposition point, the factors returned are (f,
  1). Recur on the two children. The order is determined by the
  heavier branch. Combine the factors of the two children and pick the
  one that already occurs in the gh table. Occurence in g is indicated
  by value 1, occurence in h by 2, occurence in both 3.]

  SideEffects []

  SeeAlso     [cuddConjunctsAux]

******************************************************************************/
static Conjuncts *
BuildConjuncts(
  DdManager * dd,
  DdNode * node,
  st_table * distanceTable,
  st_table * cacheTable,
  int approxDistance,
  int maxLocalRef,
  st_table * ghTable,
  st_table * mintermTable)
{
    int topid, distance;
1696
    Conjuncts *factorsNv = NULL, *factorsNnv = NULL, *factors;
Alan Mishchenko committed
1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708
    Conjuncts *dummy;
    DdNode *N, *Nv, *Nnv, *temp, *g1, *g2, *h1, *h2, *topv;
    double minNv = 0.0, minNnv = 0.0;
    double *doubleDummy;
    int switched =0;
    int outOfMem;
    int freeNv = 0, freeNnv = 0, freeTemp;
    NodeStat *nodeStat;
    int value;

    /* if f is constant, return (f,f) */
    if (Cudd_IsConstant(node)) {
1709 1710 1711 1712 1713 1714 1715 1716
        factors = ABC_ALLOC(Conjuncts, 1);
        if (factors == NULL) {
            dd->errorCode = CUDD_MEMORY_OUT;
            return(NULL);
        }
        factors->g = node;
        factors->h = node;
        return(FactorsComplement(factors));
Alan Mishchenko committed
1717 1718 1719
    }

    /* If result (a pair of conjuncts) in cache, return the factors. */
1720 1721 1722
    if (st_lookup(cacheTable, (const char *)node, (char **)&dummy)) {
        factors = dummy;
        return(factors);
Alan Mishchenko committed
1723 1724 1725 1726
    }
    
    /* check distance and local reference count of this node */
    N = Cudd_Regular(node);
1727 1728 1729 1730
    if (!st_lookup(distanceTable, (const char *)N, (char **)&nodeStat)) {
        (void) fprintf(dd->err, "Not in table, Something wrong\n");
        dd->errorCode = CUDD_INTERNAL_ERROR;
        return(NULL);
Alan Mishchenko committed
1731 1732 1733 1734 1735
    }
    distance = nodeStat->distance;

    /* at or below decomposition point, return (f, 1) */
    if (((nodeStat->localRef > maxLocalRef*2/3) &&
1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763
         (distance < approxDistance*2/3)) ||
            (distance <= approxDistance/4)) {
        factors = ABC_ALLOC(Conjuncts, 1);
        if (factors == NULL) {
            dd->errorCode = CUDD_MEMORY_OUT;
            return(NULL);
        }
        /* alternate assigning (f,1) */
        value = 0;
        if (st_lookup_int(ghTable, (char *)Cudd_Regular(node), &value)) {
            if (value == 3) {
                if (!lastTimeG) {
                    factors->g = node;
                    factors->h = one;
                    lastTimeG = 1;
                } else {
                    factors->g = one;
                    factors->h = node;
                    lastTimeG = 0; 
                }
            } else if (value == 1) {
                factors->g = node;
                factors->h = one;
            } else {
                factors->g = one;
                factors->h = node;
            }
        } else if (!lastTimeG) {
Alan Mishchenko committed
1764 1765 1766
            factors->g = node;
            factors->h = one;
            lastTimeG = 1;
1767 1768 1769 1770 1771 1772
            value = 1;
            if (st_insert(ghTable, (char *)Cudd_Regular(node), (char *)(long)value) == ST_OUT_OF_MEM) {
                dd->errorCode = CUDD_MEMORY_OUT;
                ABC_FREE(factors);
                return NULL;
            }
Alan Mishchenko committed
1773 1774 1775
        } else {
            factors->g = one;
            factors->h = node;
1776 1777 1778 1779 1780 1781 1782
            lastTimeG = 0;
            value = 2;
            if (st_insert(ghTable, (char *)Cudd_Regular(node), (char *)(long)value) == ST_OUT_OF_MEM) {
                dd->errorCode = CUDD_MEMORY_OUT;
                ABC_FREE(factors);
                return NULL;
            }
Alan Mishchenko committed
1783
        }
1784
        return(FactorsComplement(factors));
Alan Mishchenko committed
1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796
    }
    
    /* get the children and recur */
    Nv = cuddT(N);
    Nnv = cuddE(N);
    Nv = Cudd_NotCond(Nv, Cudd_IsComplement(node));
    Nnv = Cudd_NotCond(Nnv, Cudd_IsComplement(node));

    /* Choose which subproblem to solve first based on the number of
     * minterms. We go first where there are more minterms.
     */
    if (!Cudd_IsConstant(Nv)) {
1797 1798 1799 1800 1801 1802
        if (!st_lookup(mintermTable, (const char *)Nv, (char **)&doubleDummy)) {
            (void) fprintf(dd->err, "Not in table: Something wrong\n");
            dd->errorCode = CUDD_INTERNAL_ERROR;
            return(NULL);
        }
        minNv = *doubleDummy;
Alan Mishchenko committed
1803 1804 1805
    }
    
    if (!Cudd_IsConstant(Nnv)) {
1806 1807 1808 1809 1810 1811
        if (!st_lookup(mintermTable, (const char *)Nnv, (char **)&doubleDummy)) {
            (void) fprintf(dd->err, "Not in table: Something wrong\n");
            dd->errorCode = CUDD_INTERNAL_ERROR;
            return(NULL);
        }
        minNnv = *doubleDummy;
Alan Mishchenko committed
1812 1813 1814
    }
    
    if (minNv < minNnv) {
1815 1816 1817 1818
        temp = Nv;
        Nv = Nnv;
        Nnv = temp;
        switched = 1;
Alan Mishchenko committed
1819 1820 1821 1822
    }

    /* build gt, ht recursively */
    if (Nv != zero) {
1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839
        factorsNv = BuildConjuncts(dd, Nv, distanceTable,
                                   cacheTable, approxDistance, maxLocalRef, 
                                   ghTable, mintermTable);
        if (factorsNv == NULL) return(NULL);
        freeNv = FactorsNotStored(factorsNv);
        factorsNv = (freeNv) ? FactorsUncomplement(factorsNv) : factorsNv;
        cuddRef(factorsNv->g);
        cuddRef(factorsNv->h);
        
        /* Deal with the zero case */
        if (Nnv == zero) {
            /* is responsible for freeing factorsNv */
            factors = ZeroCase(dd, node, factorsNv, ghTable,
                               cacheTable, switched);
            if (freeNv) ABC_FREE(factorsNv);
            return(factors);
        }
Alan Mishchenko committed
1840 1841 1842 1843
    }

    /* build ge, he recursively */
    if (Nnv != zero) {
1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865
        factorsNnv = BuildConjuncts(dd, Nnv, distanceTable,
                                    cacheTable, approxDistance, maxLocalRef,
                                    ghTable, mintermTable);
        if (factorsNnv == NULL) {
            Cudd_RecursiveDeref(dd, factorsNv->g);
            Cudd_RecursiveDeref(dd, factorsNv->h);
            if (freeNv) ABC_FREE(factorsNv);
            return(NULL);
        }
        freeNnv = FactorsNotStored(factorsNnv);
        factorsNnv = (freeNnv) ? FactorsUncomplement(factorsNnv) : factorsNnv;
        cuddRef(factorsNnv->g);
        cuddRef(factorsNnv->h);
        
        /* Deal with the zero case */
        if (Nv == zero) {
            /* is responsible for freeing factorsNv */
            factors = ZeroCase(dd, node, factorsNnv, ghTable,
                               cacheTable, switched);
            if (freeNnv) ABC_FREE(factorsNnv);
            return(factors);
        }
Alan Mishchenko committed
1866 1867 1868 1869 1870 1871
    }

    /* construct the 2 pairs */
    /* g1 = x*gt + x'*ge; h1 = x*ht + x'*he; */
    /* g2 = x*gt + x'*he; h2 = x*ht + x'*ge */
    if (switched) {
1872 1873 1874 1875 1876 1877
        factors = factorsNnv;
        factorsNnv = factorsNv;
        factorsNv = factors;
        freeTemp = freeNv;
        freeNv = freeNnv;
        freeNnv = freeTemp;
Alan Mishchenko committed
1878 1879 1880 1881 1882 1883 1884 1885
    }

    /* Build the factors for this node. */
    topid = N->index;
    topv = dd->vars[topid];
    
    g1 = cuddBddIteRecur(dd, topv, factorsNv->g, factorsNnv->g);
    if (g1 == NULL) {
1886 1887 1888 1889 1890 1891 1892
        Cudd_RecursiveDeref(dd, factorsNv->g);
        Cudd_RecursiveDeref(dd, factorsNv->h);
        Cudd_RecursiveDeref(dd, factorsNnv->g);
        Cudd_RecursiveDeref(dd, factorsNnv->h);
        if (freeNv) ABC_FREE(factorsNv);
        if (freeNnv) ABC_FREE(factorsNnv);
        return(NULL);
Alan Mishchenko committed
1893 1894 1895 1896 1897 1898
    }

    cuddRef(g1);

    h1 = cuddBddIteRecur(dd, topv, factorsNv->h, factorsNnv->h);
    if (h1 == NULL) {
1899 1900 1901 1902 1903 1904 1905 1906
        Cudd_RecursiveDeref(dd, factorsNv->g);
        Cudd_RecursiveDeref(dd, factorsNv->h);
        Cudd_RecursiveDeref(dd, factorsNnv->g);
        Cudd_RecursiveDeref(dd, factorsNnv->h);
        Cudd_RecursiveDeref(dd, g1);
        if (freeNv) ABC_FREE(factorsNv);
        if (freeNnv) ABC_FREE(factorsNnv);
        return(NULL);
Alan Mishchenko committed
1907 1908 1909 1910 1911 1912
    }

    cuddRef(h1);

    g2 = cuddBddIteRecur(dd, topv, factorsNv->g, factorsNnv->h);
    if (g2 == NULL) {
1913 1914 1915 1916 1917 1918 1919 1920 1921
        Cudd_RecursiveDeref(dd, factorsNv->h);
        Cudd_RecursiveDeref(dd, factorsNv->g);
        Cudd_RecursiveDeref(dd, factorsNnv->g);
        Cudd_RecursiveDeref(dd, factorsNnv->h);
        Cudd_RecursiveDeref(dd, g1);
        Cudd_RecursiveDeref(dd, h1);
        if (freeNv) ABC_FREE(factorsNv);
        if (freeNnv) ABC_FREE(factorsNnv);
        return(NULL);
Alan Mishchenko committed
1922 1923 1924 1925 1926 1927 1928
    }
    cuddRef(g2);
    Cudd_RecursiveDeref(dd, factorsNv->g);
    Cudd_RecursiveDeref(dd, factorsNnv->h);

    h2 = cuddBddIteRecur(dd, topv, factorsNv->h, factorsNnv->g);
    if (h2 == NULL) {
1929 1930 1931 1932 1933 1934 1935 1936 1937 1938
        Cudd_RecursiveDeref(dd, factorsNv->g);
        Cudd_RecursiveDeref(dd, factorsNv->h);
        Cudd_RecursiveDeref(dd, factorsNnv->g);
        Cudd_RecursiveDeref(dd, factorsNnv->h);
        Cudd_RecursiveDeref(dd, g1);
        Cudd_RecursiveDeref(dd, h1);
        Cudd_RecursiveDeref(dd, g2);
        if (freeNv) ABC_FREE(factorsNv);
        if (freeNnv) ABC_FREE(factorsNnv);
        return(NULL);
Alan Mishchenko committed
1939 1940 1941 1942
    }
    cuddRef(h2);
    Cudd_RecursiveDeref(dd, factorsNv->h);
    Cudd_RecursiveDeref(dd, factorsNnv->g);
Alan Mishchenko committed
1943 1944
    if (freeNv) ABC_FREE(factorsNv);
    if (freeNnv) ABC_FREE(factorsNnv);
Alan Mishchenko committed
1945 1946 1947 1948

    /* check for each pair in tables and choose one */
    factors = CheckInTables(node, g1, h1, g2, h2, ghTable, cacheTable, &outOfMem);
    if (outOfMem) {
1949
        dd->errorCode = CUDD_MEMORY_OUT;
Alan Mishchenko committed
1950 1951
        Cudd_RecursiveDeref(dd, g1);
        Cudd_RecursiveDeref(dd, h1);
1952 1953 1954
        Cudd_RecursiveDeref(dd, g2);
        Cudd_RecursiveDeref(dd, h2);
        return(NULL);
Alan Mishchenko committed
1955
    }
1956 1957 1958 1959 1960 1961 1962 1963 1964
    if (factors != NULL) {
        if ((factors->g == g1) || (factors->g == h1)) {
            Cudd_RecursiveDeref(dd, g2);
            Cudd_RecursiveDeref(dd, h2);
        } else {
            Cudd_RecursiveDeref(dd, g1);
            Cudd_RecursiveDeref(dd, h1);
        }
        return(factors);
Alan Mishchenko committed
1965 1966 1967 1968 1969
    }

    /* if not in tables, pick one pair */
    factors = PickOnePair(node,g1, h1, g2, h2, ghTable, cacheTable);
    if (factors == NULL) {
1970 1971 1972
        dd->errorCode = CUDD_MEMORY_OUT;
        Cudd_RecursiveDeref(dd, g1);
        Cudd_RecursiveDeref(dd, h1);
Alan Mishchenko committed
1973 1974 1975
        Cudd_RecursiveDeref(dd, g2);
        Cudd_RecursiveDeref(dd, h2);
    } else {
1976 1977 1978 1979 1980 1981 1982 1983
        /* now free what was created and not used */
        if ((factors->g == g1) || (factors->g == h1)) {
            Cudd_RecursiveDeref(dd, g2);
            Cudd_RecursiveDeref(dd, h2);
        } else {
            Cudd_RecursiveDeref(dd, g1);
            Cudd_RecursiveDeref(dd, h1);
        }
Alan Mishchenko committed
1984
    }
1985
        
Alan Mishchenko committed
1986 1987 1988 1989 1990 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 2022 2023 2024 2025 2026 2027 2028
    return(factors);
    
} /* end of BuildConjuncts */


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

  Synopsis    [Procedure to compute two conjunctive factors of f and place in *c1 and *c2.]

  Description [Procedure to compute two conjunctive factors of f and
  place in *c1 and *c2. Sets up the required data - table of distances
  from the constant and local reference count. Also minterm table. ]

  SideEffects []

  SeeAlso     []

******************************************************************************/
static int
cuddConjunctsAux(
  DdManager * dd,
  DdNode * f,
  DdNode ** c1,
  DdNode ** c2)
{
    st_table *distanceTable = NULL;
    st_table *cacheTable = NULL;
    st_table *mintermTable = NULL;
    st_table *ghTable = NULL;
    st_generator *stGen;
    char *key, *value;
    Conjuncts *factors;
    int distance, approxDistance;
    double max, minterms;
    int freeFactors;
    NodeStat *nodeStat;
    int maxLocalRef;
    
    /* initialize */
    *c1 = NULL;
    *c2 = NULL;

    /* initialize distances table */
2029
    distanceTable = st_init_table(st_ptrcmp,st_ptrhash);
Alan Mishchenko committed
2030 2031 2032
    if (distanceTable == NULL) goto outOfMem;
    
    /* make the entry for the constant */
Alan Mishchenko committed
2033
    nodeStat = ABC_ALLOC(NodeStat, 1);
Alan Mishchenko committed
2034 2035 2036 2037
    if (nodeStat == NULL) goto outOfMem;
    nodeStat->distance = 0;
    nodeStat->localRef = 1;
    if (st_insert(distanceTable, (char *)one, (char *)nodeStat) == ST_OUT_OF_MEM) {
2038
        goto outOfMem;
Alan Mishchenko committed
2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049
    }

    /* Count node distances from constant. */
    nodeStat = CreateBotDist(f, distanceTable);
    if (nodeStat == NULL) goto outOfMem;

    /* set the distance for the decomposition points */
    approxDistance = (DEPTH < nodeStat->distance) ? nodeStat->distance : DEPTH;
    distance = nodeStat->distance;

    if (distance < approxDistance) {
2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061
        /* Too small to bother. */
        *c1 = f;
        *c2 = DD_ONE(dd);
        cuddRef(*c1); cuddRef(*c2);
        stGen = st_init_gen(distanceTable);
        if (stGen == NULL) goto outOfMem;
        while(st_gen(stGen, (const char **)&key, (char **)&value)) {
            ABC_FREE(value);
        }
        st_free_gen(stGen); stGen = NULL;
        st_free_table(distanceTable);
        return(1);
Alan Mishchenko committed
2062 2063 2064 2065 2066 2067
    }

    /* record the maximum local reference count */
    maxLocalRef = 0;
    stGen = st_init_gen(distanceTable);
    if (stGen == NULL) goto outOfMem;
2068
    while(st_gen(stGen, (const char **)&key, (char **)&value)) {
2069 2070 2071
        nodeStat = (NodeStat *)value;
        maxLocalRef = (nodeStat->localRef > maxLocalRef) ?
            nodeStat->localRef : maxLocalRef;
Alan Mishchenko committed
2072 2073 2074
    }
    st_free_gen(stGen); stGen = NULL;

2075
            
Alan Mishchenko committed
2076 2077
    /* Count minterms for each node. */
    max = pow(2.0, (double)Cudd_SupportSize(dd,f)); /* potential overflow */
2078
    mintermTable = st_init_table(st_ptrcmp,st_ptrhash);
Alan Mishchenko committed
2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090
    if (mintermTable == NULL) goto outOfMem;
    minterms = CountMinterms(f, max, mintermTable, dd->err);
    if (minterms == -1.0) goto outOfMem;
    
    lastTimeG = Cudd_Random() & 1;
    cacheTable = st_init_table(st_ptrcmp, st_ptrhash);
    if (cacheTable == NULL) goto outOfMem;
    ghTable = st_init_table(st_ptrcmp, st_ptrhash);
    if (ghTable == NULL) goto outOfMem;

    /* Build conjuncts. */
    factors = BuildConjuncts(dd, f, distanceTable, cacheTable,
2091
                             approxDistance, maxLocalRef, ghTable, mintermTable);
Alan Mishchenko committed
2092 2093
    if (factors == NULL) goto outOfMem;

2094
    /* free up tables */
Alan Mishchenko committed
2095 2096
    stGen = st_init_gen(distanceTable);
    if (stGen == NULL) goto outOfMem;
2097
    while(st_gen(stGen, (const char **)&key, (char **)&value)) {
2098
        ABC_FREE(value);
Alan Mishchenko committed
2099 2100 2101 2102 2103 2104 2105
    }
    st_free_gen(stGen); stGen = NULL;
    st_free_table(distanceTable); distanceTable = NULL;
    st_free_table(ghTable); ghTable = NULL;
    
    stGen = st_init_gen(mintermTable);
    if (stGen == NULL) goto outOfMem;
2106
    while(st_gen(stGen, (const char **)&key, (char **)&value)) {
2107
        ABC_FREE(value);
Alan Mishchenko committed
2108 2109 2110 2111 2112 2113 2114
    }
    st_free_gen(stGen); stGen = NULL;
    st_free_table(mintermTable); mintermTable = NULL;

    freeFactors = FactorsNotStored(factors);
    factors = (freeFactors) ? FactorsUncomplement(factors) : factors;
    if (factors != NULL) {
2115 2116 2117 2118 2119 2120
        *c1 = factors->g;
        *c2 = factors->h;
        cuddRef(*c1);
        cuddRef(*c2);
        if (freeFactors) ABC_FREE(factors);
        
Alan Mishchenko committed
2121
#if 0    
2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134
        if ((*c1 == f) && (!Cudd_IsConstant(f))) {
            assert(*c2 == one);
        }
        if ((*c2 == f) && (!Cudd_IsConstant(f))) {
            assert(*c1 == one);
        }
        
        if ((*c1 != one) && (!Cudd_IsConstant(f))) {
            assert(!Cudd_bddLeq(dd, *c2, *c1));
        }
        if ((*c2 != one) && (!Cudd_IsConstant(f))) {
            assert(!Cudd_bddLeq(dd, *c1, *c2));
        }
Alan Mishchenko committed
2135 2136 2137 2138 2139
#endif
    }

    stGen = st_init_gen(cacheTable);
    if (stGen == NULL) goto outOfMem;
2140
    while(st_gen(stGen, (const char **)&key, (char **)&value)) {
2141
        ConjunctsFree(dd, (Conjuncts *)value);
Alan Mishchenko committed
2142 2143 2144 2145 2146 2147 2148 2149 2150
    }
    st_free_gen(stGen); stGen = NULL;

    st_free_table(cacheTable); cacheTable = NULL;

    return(1);

outOfMem:
    if (distanceTable != NULL) {
2151 2152 2153 2154 2155 2156 2157
        stGen = st_init_gen(distanceTable);
        if (stGen == NULL) goto outOfMem;
        while(st_gen(stGen, (const char **)&key, (char **)&value)) {
            ABC_FREE(value);
        }
        st_free_gen(stGen); stGen = NULL;
        st_free_table(distanceTable); distanceTable = NULL;
Alan Mishchenko committed
2158 2159
    }
    if (mintermTable != NULL) {
2160 2161 2162 2163 2164 2165 2166
        stGen = st_init_gen(mintermTable);
        if (stGen == NULL) goto outOfMem;
        while(st_gen(stGen, (const char **)&key, (char **)&value)) {
            ABC_FREE(value);
        }
        st_free_gen(stGen); stGen = NULL;
        st_free_table(mintermTable); mintermTable = NULL;
Alan Mishchenko committed
2167 2168 2169
    }
    if (ghTable != NULL) st_free_table(ghTable);
    if (cacheTable != NULL) {
2170 2171 2172 2173 2174 2175 2176
        stGen = st_init_gen(cacheTable);
        if (stGen == NULL) goto outOfMem;
        while(st_gen(stGen, (const char **)&key, (char **)&value)) {
            ConjunctsFree(dd, (Conjuncts *)value);
        }
        st_free_gen(stGen); stGen = NULL;
        st_free_table(cacheTable); cacheTable = NULL;
Alan Mishchenko committed
2177 2178 2179 2180 2181
    }
    dd->errorCode = CUDD_MEMORY_OUT;
    return(0);

} /* end of cuddConjunctsAux */
2182 2183


2184 2185
ABC_NAMESPACE_IMPL_END