cuddSubsetHB.c 44.2 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7
/**CFile***********************************************************************

  FileName    [cuddSubsetHB.c]

  PackageName [cudd]

  Synopsis    [Procedure to subset the given BDD by choosing the heavier
8
               branches.]
Alan Mishchenko committed
9 10 11 12


  Description [External procedures provided by this module:
                <ul>
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
                <li> Cudd_SubsetHeavyBranch()
                <li> Cudd_SupersetHeavyBranch()
                </ul>
               Internal procedures included in this module:
                <ul>
                <li> cuddSubsetHeavyBranch()
                </ul>
               Static procedures included in this module:
                <ul>
                <li> ResizeCountMintermPages();
                <li> ResizeNodeDataPages()
                <li> ResizeCountNodePages()
                <li> SubsetCountMintermAux()
                <li> SubsetCountMinterm()
                <li> SubsetCountNodesAux()
                <li> SubsetCountNodes()
                <li> BuildSubsetBdd()
                </ul>
                ]
Alan Mishchenko committed
32 33 34 35 36

  SeeAlso     [cuddSubsetSP.c]

  Author      [Kavita Ravi]

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

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

#ifdef __STDC__
#include <float.h>
#else
#define DBL_MAX_EXP 1024
#endif
76
#include "misc/util/util_hack.h"
Alan Mishchenko committed
77 78
#include "cuddInt.h"

79 80 81
ABC_NAMESPACE_IMPL_START


82

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

87 88
#define DEFAULT_PAGE_SIZE 2048
#define DEFAULT_NODE_DATA_PAGE_SIZE 1024
Alan Mishchenko committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
#define INITIAL_PAGES 128


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

/* data structure to store the information on each node. It keeps
 * the number of minterms represented by the DAG rooted at this node
 * in terms of the number of variables specified by the user, number
 * of nodes in this DAG and the number of nodes of its child with
 * lesser number of minterms that are not shared by the child with
 * more minterms
 */
struct NodeData {
    double *mintermPointer;
    int *nodesPointer;
    int *lightChildNodesPointer;
};

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

typedef struct NodeData NodeData_t;

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

#ifndef lint
120
static char rcsid[] DD_UNUSED = "$Id: cuddSubsetHB.c,v 1.37 2009/02/20 02:14:58 fabio Exp $";
Alan Mishchenko committed
121 122 123 124
#endif

static int memOut;
#ifdef DEBUG
125
static  int             num_calls;
Alan Mishchenko committed
126 127
#endif

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
static  DdNode          *zero, *one; /* constant functions */
static  double          **mintermPages; /* pointers to the pages */
static  int             **nodePages; /* pointers to the pages */
static  int             **lightNodePages; /* pointers to the pages */
static  double          *currentMintermPage; /* pointer to the current
                                                   page */
static  double          max; /* to store the 2^n value of the number
                              * of variables */

static  int             *currentNodePage; /* pointer to the current
                                                   page */
static  int             *currentLightNodePage; /* pointer to the
                                                *  current page */
static  int             pageIndex; /* index to next element */
static  int             page; /* index to current page */
static  int             pageSize = DEFAULT_PAGE_SIZE; /* page size */
Alan Mishchenko committed
144 145
static  int             maxPages; /* number of page pointers */

146 147 148 149 150 151
static  NodeData_t      *currentNodeDataPage; /* pointer to the current
                                                 page */
static  int             nodeDataPage; /* index to next element */
static  int             nodeDataPageIndex; /* index to next element */
static  NodeData_t      **nodeDataPages; /* index to current page */
static  int             nodeDataPageSize = DEFAULT_NODE_DATA_PAGE_SIZE;
Alan Mishchenko committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165
                                                     /* page size */
static  int             maxNodeDataPages; /* number of page pointers */


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

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

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

166 167 168 169 170 171 172 173 174
static void ResizeNodeDataPages (void);
static void ResizeCountMintermPages (void);
static void ResizeCountNodePages (void);
static double SubsetCountMintermAux (DdNode *node, double max, st_table *table);
static st_table * SubsetCountMinterm (DdNode *node, int nvars);
static int SubsetCountNodesAux (DdNode *node, st_table *table, double max);
static int SubsetCountNodes (DdNode *node, st_table *table, int nvars);
static void StoreNodes (st_table *storeTable, DdManager *dd, DdNode *node);
static DdNode * BuildSubsetBdd (DdManager *dd, DdNode *node, int *size, st_table *visitedTable, int threshold, st_table *storeTable, st_table *approxTable);
Alan Mishchenko committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

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


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

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

  Synopsis    [Extracts a dense subset from a BDD with the heavy branch
  heuristic.]

  Description [Extracts a dense subset from a BDD. This procedure
  builds a subset by throwing away one of the children of each node,
  starting from the root, until the result is small enough. The child
  that is eliminated from the result is the one that contributes the
  fewer minterms.  Returns a pointer to the BDD of the subset if
  successful. NULL if the procedure runs out of memory. The parameter
  numVars is the maximum number of variables to be used in minterm
  calculation and node count calculation.  The optimal number should
  be as close as possible to the size of the support of f.  However,
  it is safe to pass the value returned by Cudd_ReadSize for numVars
  when the number of variables is under 1023.  If numVars is larger
  than 1023, it will overflow. If a 0 parameter is passed then the
  procedure will compute a value which will avoid overflow but will
  cause underflow with 2046 variables or more.]

  SideEffects [None]

  SeeAlso     [Cudd_SubsetShortPaths Cudd_SupersetHeavyBranch Cudd_ReadSize]

******************************************************************************/
DdNode *
Cudd_SubsetHeavyBranch(
  DdManager * dd /* manager */,
  DdNode * f /* function to be subset */,
  int  numVars /* number of variables in the support of f */,
  int  threshold /* maximum number of nodes in the subset */)
{
    DdNode *subset;

    memOut = 0;
    do {
219 220
        dd->reordered = 0;
        subset = cuddSubsetHeavyBranch(dd, f, numVars, threshold);
Alan Mishchenko committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
    } while ((dd->reordered == 1) && (!memOut));

    return(subset);

} /* end of Cudd_SubsetHeavyBranch */


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

  Synopsis    [Extracts a dense superset from a BDD with the heavy branch
  heuristic.]

  Description [Extracts a dense superset from a BDD. The procedure is
  identical to the subset procedure except for the fact that it
  receives the complement of the given function. Extracting the subset
  of the complement function is equivalent to extracting the superset
  of the function. This procedure builds a superset by throwing away
  one of the children of each node starting from the root of the
  complement function, until the result is small enough. The child
  that is eliminated from the result is the one that contributes the
  fewer minterms.
  Returns a pointer to the BDD of the superset if successful. NULL if
  intermediate result causes the procedure to run out of memory. The
  parameter numVars is the maximum number of variables to be used in
  minterm calculation and node count calculation.  The optimal number
  should be as close as possible to the size of the support of f.
  However, it is safe to pass the value returned by Cudd_ReadSize for
  numVars when the number of variables is under 1023.  If numVars is
  larger than 1023, it will overflow. If a 0 parameter is passed then
  the procedure will compute a value which will avoid overflow but
  will cause underflow with 2046 variables or more.]

  SideEffects [None]

  SeeAlso     [Cudd_SubsetHeavyBranch Cudd_SupersetShortPaths Cudd_ReadSize]

******************************************************************************/
DdNode *
Cudd_SupersetHeavyBranch(
  DdManager * dd /* manager */,
  DdNode * f /* function to be superset */,
  int  numVars /* number of variables in the support of f */,
  int  threshold /* maximum number of nodes in the superset */)
{
    DdNode *subset, *g;

267
    g = Cudd_Not(f);
Alan Mishchenko committed
268 269
    memOut = 0;
    do {
270 271
        dd->reordered = 0;
        subset = cuddSubsetHeavyBranch(dd, g, numVars, threshold);
Alan Mishchenko committed
272
    } while ((dd->reordered == 1) && (!memOut));
273

Alan Mishchenko committed
274
    return(Cudd_NotCond(subset, (subset != NULL)));
275

Alan Mishchenko committed
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
} /* end of Cudd_SupersetHeavyBranch */


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


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

  Synopsis    [The main procedure that returns a subset by choosing the heavier
  branch in the BDD.]

  Description [Here a subset BDD is built by throwing away one of the
  children. Starting at root, annotate each node with the number of
  minterms (in terms of the total number of variables specified -
  numVars), number of nodes taken by the DAG rooted at this node and
  number of additional nodes taken by the child that has the lesser
  minterms. The child with the lower number of minterms is thrown away
  and a dyanmic count of the nodes of the subset is kept. Once the
  threshold is reached the subset is returned to the calling
  procedure.]

  SideEffects [None]

  SeeAlso     [Cudd_SubsetHeavyBranch]

******************************************************************************/
DdNode *
cuddSubsetHeavyBranch(
  DdManager * dd /* DD manager */,
  DdNode * f /* current DD */,
  int  numVars /* maximum number of variables */,
  int  threshold /* threshold size for the subset */)
{

    int i, *size;
    st_table *visitedTable;
    int numNodes;
    NodeData_t *currNodeQual;
    DdNode *subset;
    st_table *storeTable, *approxTable;
    char *key, *value;
    st_generator *stGen;
320

Alan Mishchenko committed
321
    if (f == NULL) {
322 323 324
        fprintf(dd->err, "Cannot subset, nil object\n");
        dd->errorCode = CUDD_INVALID_ARG;
        return(NULL);
Alan Mishchenko committed
325 326
    }

327
    one  = Cudd_ReadOne(dd);
Alan Mishchenko committed
328 329 330 331 332 333 334 335
    zero = Cudd_Not(one);

    /* If user does not know numVars value, set it to the maximum
     * exponent that the pow function can take. The -1 is due to the
     * discrepancy in the value that pow takes and the value that
     * log gives.
     */
    if (numVars == 0) {
336 337
        /* set default value */
        numVars = DBL_MAX_EXP - 1;
Alan Mishchenko committed
338 339 340
    }

    if (Cudd_IsConstant(f)) {
341
        return(f);
Alan Mishchenko committed
342 343 344 345 346 347 348 349
    }

    max = pow(2.0, (double)numVars);

    /* Create visited table where structures for node data are allocated and
       stored in a st_table */
    visitedTable = SubsetCountMinterm(f, numVars);
    if ((visitedTable == NULL) || memOut) {
350 351 352
        (void) fprintf(dd->err, "Out-of-memory; Cannot subset\n");
        dd->errorCode = CUDD_MEMORY_OUT;
        return(0);
Alan Mishchenko committed
353 354 355
    }
    numNodes = SubsetCountNodes(f, visitedTable, numVars);
    if (memOut) {
356 357 358
        (void) fprintf(dd->err, "Out-of-memory; Cannot subset\n");
        dd->errorCode = CUDD_MEMORY_OUT;
        return(0);
Alan Mishchenko committed
359 360
    }

361 362 363 364
    if (st_lookup(visitedTable, (const char *)f, (char **)&currNodeQual) == 0) {
        fprintf(dd->err,
                "Something is wrong, ought to be node quality table\n");
        dd->errorCode = CUDD_INTERNAL_ERROR;
Alan Mishchenko committed
365 366
    }

Alan Mishchenko committed
367
    size = ABC_ALLOC(int, 1);
Alan Mishchenko committed
368
    if (size == NULL) {
369 370
        dd->errorCode = CUDD_MEMORY_OUT;
        return(NULL);
Alan Mishchenko committed
371 372 373 374 375 376 377 378 379 380 381
    }
    *size = numNodes;

#ifdef DEBUG
    num_calls = 0;
#endif
    /* table to store nodes being created. */
    storeTable = st_init_table(st_ptrcmp, st_ptrhash);
    /* insert the constant */
    cuddRef(one);
    if (st_insert(storeTable, (char *)Cudd_ReadOne(dd), NIL(char)) ==
382 383
        ST_OUT_OF_MEM) {
        fprintf(dd->out, "Something wrong, st_table insert failed\n");
Alan Mishchenko committed
384 385 386 387
    }
    /* table to store approximations of nodes */
    approxTable = st_init_table(st_ptrcmp, st_ptrhash);
    subset = (DdNode *)BuildSubsetBdd(dd, f, size, visitedTable, threshold,
388
                                      storeTable, approxTable);
Alan Mishchenko committed
389
    if (subset != NULL) {
390
        cuddRef(subset);
Alan Mishchenko committed
391 392 393 394
    }

    stGen = st_init_gen(approxTable);
    if (stGen == NULL) {
395 396
        st_free_table(approxTable);
        return(NULL);
Alan Mishchenko committed
397
    }
398
    while(st_gen(stGen, (const char **)&key, (char **)&value)) {
399
        Cudd_RecursiveDeref(dd, (DdNode *)value);
Alan Mishchenko committed
400 401 402 403 404 405
    }
    st_free_gen(stGen); stGen = NULL;
    st_free_table(approxTable);

    stGen = st_init_gen(storeTable);
    if (stGen == NULL) {
406 407
        st_free_table(storeTable);
        return(NULL);
Alan Mishchenko committed
408
    }
409
    while(st_gen(stGen, (const char **)&key, (char **)&value)) {
410
        Cudd_RecursiveDeref(dd, (DdNode *)key);
Alan Mishchenko committed
411 412 413 414 415
    }
    st_free_gen(stGen); stGen = NULL;
    st_free_table(storeTable);

    for (i = 0; i <= page; i++) {
416
        ABC_FREE(mintermPages[i]);
Alan Mishchenko committed
417
    }
Alan Mishchenko committed
418
    ABC_FREE(mintermPages);
Alan Mishchenko committed
419
    for (i = 0; i <= page; i++) {
420
        ABC_FREE(nodePages[i]);
Alan Mishchenko committed
421
    }
Alan Mishchenko committed
422
    ABC_FREE(nodePages);
Alan Mishchenko committed
423
    for (i = 0; i <= page; i++) {
424
        ABC_FREE(lightNodePages[i]);
Alan Mishchenko committed
425
    }
Alan Mishchenko committed
426
    ABC_FREE(lightNodePages);
Alan Mishchenko committed
427
    for (i = 0; i <= nodeDataPage; i++) {
428
        ABC_FREE(nodeDataPages[i]);
Alan Mishchenko committed
429
    }
Alan Mishchenko committed
430
    ABC_FREE(nodeDataPages);
Alan Mishchenko committed
431
    st_free_table(visitedTable);
Alan Mishchenko committed
432
    ABC_FREE(size);
Alan Mishchenko committed
433 434 435 436 437 438 439 440
#if 0
    (void) Cudd_DebugCheck(dd);
    (void) Cudd_CheckKeys(dd);
#endif

    if (subset != NULL) {
#ifdef DD_DEBUG
      if (!Cudd_bddLeq(dd, subset, f)) {
441 442 443
            fprintf(dd->err, "Wrong subset\n");
            dd->errorCode = CUDD_INTERNAL_ERROR;
            return(NULL);
Alan Mishchenko committed
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
      }
#endif
        cuddDeref(subset);
        return(subset);
    } else {
        return(NULL);
    }
} /* end of cuddSubsetHeavyBranch */


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


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

  Synopsis    [Resize the number of pages allocated to store the node data.]

  Description [Resize the number of pages allocated to store the node data
  The procedure  moves the counter to the next page when the end of
  the page is reached and allocates new pages when necessary.]

  SideEffects [Changes the size of pages, page, page index, maximum
  number of pages freeing stuff in case of memory out. ]

  SeeAlso     []

******************************************************************************/
static void
474
ResizeNodeDataPages(void)
Alan Mishchenko committed
475 476 477 478 479 480 481 482 483 484
{
    int i;
    NodeData_t **newNodeDataPages;

    nodeDataPage++;
    /* If the current page index is larger than the number of pages
     * allocated, allocate a new page array. Page numbers are incremented by
     * INITIAL_PAGES
     */
    if (nodeDataPage == maxNodeDataPages) {
485 486 487 488 489 490 491 492 493 494 495 496 497 498
        newNodeDataPages = ABC_ALLOC(NodeData_t *,maxNodeDataPages + INITIAL_PAGES);
        if (newNodeDataPages == NULL) {
            for (i = 0; i < nodeDataPage; i++) ABC_FREE(nodeDataPages[i]);
            ABC_FREE(nodeDataPages);
            memOut = 1;
            return;
        } else {
            for (i = 0; i < maxNodeDataPages; i++) {
                newNodeDataPages[i] = nodeDataPages[i];
            }
            /* Increase total page count */
            maxNodeDataPages += INITIAL_PAGES;
            ABC_FREE(nodeDataPages);
            nodeDataPages = newNodeDataPages;
Alan Mishchenko committed
499 500 501 502
        }
    }
    /* Allocate a new page */
    currentNodeDataPage = nodeDataPages[nodeDataPage] =
503
        ABC_ALLOC(NodeData_t ,nodeDataPageSize);
Alan Mishchenko committed
504
    if (currentNodeDataPage == NULL) {
505 506 507 508
        for (i = 0; i < nodeDataPage; i++) ABC_FREE(nodeDataPages[i]);
        ABC_FREE(nodeDataPages);
        memOut = 1;
        return;
Alan Mishchenko committed
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
    }
    /* reset page index */
    nodeDataPageIndex = 0;
    return;

} /* end of ResizeNodeDataPages */


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

  Synopsis    [Resize the number of pages allocated to store the minterm
  counts. ]

  Description [Resize the number of pages allocated to store the minterm
  counts.  The procedure  moves the counter to the next page when the
  end of the page is reached and allocates new pages when necessary.]

526
  SideEffects [Changes the size of minterm pages, page, page index, maximum
Alan Mishchenko committed
527 528 529 530 531 532
  number of pages freeing stuff in case of memory out. ]

  SeeAlso     []

******************************************************************************/
static void
533
ResizeCountMintermPages(void)
Alan Mishchenko committed
534 535 536 537 538 539 540 541 542 543
{
    int i;
    double **newMintermPages;

    page++;
    /* If the current page index is larger than the number of pages
     * allocated, allocate a new page array. Page numbers are incremented by
     * INITIAL_PAGES
     */
    if (page == maxPages) {
544 545 546 547 548 549 550 551 552 553 554 555 556 557
        newMintermPages = ABC_ALLOC(double *,maxPages + INITIAL_PAGES);
        if (newMintermPages == NULL) {
            for (i = 0; i < page; i++) ABC_FREE(mintermPages[i]);
            ABC_FREE(mintermPages);
            memOut = 1;
            return;
        } else {
            for (i = 0; i < maxPages; i++) {
                newMintermPages[i] = mintermPages[i];
            }
            /* Increase total page count */
            maxPages += INITIAL_PAGES;
            ABC_FREE(mintermPages);
            mintermPages = newMintermPages;
Alan Mishchenko committed
558 559 560
        }
    }
    /* Allocate a new page */
Alan Mishchenko committed
561
    currentMintermPage = mintermPages[page] = ABC_ALLOC(double,pageSize);
Alan Mishchenko committed
562
    if (currentMintermPage == NULL) {
563 564 565 566
        for (i = 0; i < page; i++) ABC_FREE(mintermPages[i]);
        ABC_FREE(mintermPages);
        memOut = 1;
        return;
Alan Mishchenko committed
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
    }
    /* reset page index */
    pageIndex = 0;
    return;

} /* end of ResizeCountMintermPages */


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

  Synopsis    [Resize the number of pages allocated to store the node counts.]

  Description [Resize the number of pages allocated to store the node counts.
  The procedure  moves the counter to the next page when the end of
  the page is reached and allocates new pages when necessary.]

  SideEffects [Changes the size of pages, page, page index, maximum
  number of pages freeing stuff in case of memory out.]

  SeeAlso     []

******************************************************************************/
static void
590
ResizeCountNodePages(void)
Alan Mishchenko committed
591 592 593 594 595 596 597 598 599 600 601
{
    int i;
    int **newNodePages;

    page++;

    /* If the current page index is larger than the number of pages
     * allocated, allocate a new page array. The number of pages is incremented
     * by INITIAL_PAGES.
     */
    if (page == maxPages) {
602 603 604 605 606 607 608 609 610 611 612 613 614 615
        newNodePages = ABC_ALLOC(int *,maxPages + INITIAL_PAGES);
        if (newNodePages == NULL) {
            for (i = 0; i < page; i++) ABC_FREE(nodePages[i]);
            ABC_FREE(nodePages);
            for (i = 0; i < page; i++) ABC_FREE(lightNodePages[i]);
            ABC_FREE(lightNodePages);
            memOut = 1;
            return;
        } else {
            for (i = 0; i < maxPages; i++) {
                newNodePages[i] = nodePages[i];
            }
            ABC_FREE(nodePages);
            nodePages = newNodePages;
Alan Mishchenko committed
616 617
        }

618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
        newNodePages = ABC_ALLOC(int *,maxPages + INITIAL_PAGES);
        if (newNodePages == NULL) {
            for (i = 0; i < page; i++) ABC_FREE(nodePages[i]);
            ABC_FREE(nodePages);
            for (i = 0; i < page; i++) ABC_FREE(lightNodePages[i]);
            ABC_FREE(lightNodePages);
            memOut = 1;
            return;
        } else {
            for (i = 0; i < maxPages; i++) {
                newNodePages[i] = lightNodePages[i];
            }
            ABC_FREE(lightNodePages);
            lightNodePages = newNodePages;
        }
        /* Increase total page count */
        maxPages += INITIAL_PAGES;
    }
    /* Allocate a new page */
    currentNodePage = nodePages[page] = ABC_ALLOC(int,pageSize);
    if (currentNodePage == NULL) {
Alan Mishchenko committed
639 640 641 642
        for (i = 0; i < page; i++) ABC_FREE(nodePages[i]);
        ABC_FREE(nodePages);
        for (i = 0; i < page; i++) ABC_FREE(lightNodePages[i]);
        ABC_FREE(lightNodePages);
Alan Mishchenko committed
643 644 645 646
        memOut = 1;
        return;
    }
    /* Allocate a new page */
Alan Mishchenko committed
647
    currentLightNodePage = lightNodePages[page] = ABC_ALLOC(int,pageSize);
Alan Mishchenko committed
648
    if (currentLightNodePage == NULL) {
649 650 651 652 653 654
        for (i = 0; i <= page; i++) ABC_FREE(nodePages[i]);
        ABC_FREE(nodePages);
        for (i = 0; i < page; i++) ABC_FREE(lightNodePages[i]);
        ABC_FREE(lightNodePages);
        memOut = 1;
        return;
Alan Mishchenko committed
655 656 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
    }
    /* reset page index */
    pageIndex = 0;
    return;

} /* end of ResizeCountNodePages */


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

  Synopsis    [Recursively counts minterms of each node in the DAG.]

  Description [Recursively counts minterms of each node in the DAG.
  Similar to the cuddCountMintermAux which recursively counts the
  number of minterms for the dag rooted at each node in terms of the
  total number of variables (max). This procedure creates the node
  data structure and stores the minterm count as part of the node
  data structure. ]

  SideEffects [Creates structures of type node quality and fills the st_table]

  SeeAlso     [SubsetCountMinterm]

******************************************************************************/
static double
SubsetCountMintermAux(
  DdNode * node /* function to analyze */,
  double  max /* number of minterms of constant 1 */,
  st_table * table /* visitedTable table */)
{

686 687 688
    DdNode      *N,*Nv,*Nnv; /* nodes to store cofactors  */
    double      min,*pmin; /* minterm count */
    double      min1, min2; /* minterm count */
Alan Mishchenko committed
689 690 691 692 693 694 695 696 697 698
    NodeData_t *dummy;
    NodeData_t *newEntry;
    int i;

#ifdef DEBUG
    num_calls++;
#endif

    /* Constant case */
    if (Cudd_IsConstant(node)) {
699 700 701 702 703
        if (node == zero) {
            return(0.0);
        } else {
            return(max);
        }
Alan Mishchenko committed
704 705
    } else {

706 707 708 709 710
        /* check if entry for this node exists */
        if (st_lookup(table, (const char *)node, (char **)&dummy)) {
            min = *(dummy->mintermPointer);
            return(min);
        }
Alan Mishchenko committed
711

712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
        /* Make the node regular to extract cofactors */
        N = Cudd_Regular(node);

        /* store the cofactors */
        Nv = Cudd_T(N);
        Nnv = Cudd_E(N);

        Nv = Cudd_NotCond(Nv, Cudd_IsComplement(node));
        Nnv = Cudd_NotCond(Nnv, Cudd_IsComplement(node));

        min1 =  SubsetCountMintermAux(Nv, max,table)/2.0;
        if (memOut) return(0.0);
        min2 =  SubsetCountMintermAux(Nnv,max,table)/2.0;
        if (memOut) return(0.0);
        min = (min1+min2);

        /* if page index is at the bottom, then create a new page */
        if (pageIndex == pageSize) ResizeCountMintermPages();
        if (memOut) {
            for (i = 0; i <= nodeDataPage; i++) ABC_FREE(nodeDataPages[i]);
            ABC_FREE(nodeDataPages);
            st_free_table(table);
            return(0.0);
        }
Alan Mishchenko committed
736

737 738 739
        /* point to the correct location in the page */
        pmin = currentMintermPage+pageIndex;
        pageIndex++;
Alan Mishchenko committed
740

741 742
        /* store the minterm count of this node in the page */
        *pmin = min;
Alan Mishchenko committed
743

744 745 746 747 748 749 750 751
        /* Note I allocate the struct here. Freeing taken care of later */
        if (nodeDataPageIndex == nodeDataPageSize) ResizeNodeDataPages();
        if (memOut) {
            for (i = 0; i <= page; i++) ABC_FREE(mintermPages[i]);
            ABC_FREE(mintermPages);
            st_free_table(table);
            return(0.0);
        }
Alan Mishchenko committed
752

753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
        newEntry = currentNodeDataPage + nodeDataPageIndex;
        nodeDataPageIndex++;

        /* points to the correct location in the page */
        newEntry->mintermPointer = pmin;
        /* initialize this field of the Node Quality structure */
        newEntry->nodesPointer = NULL;

        /* insert entry for the node in the table */
        if (st_insert(table,(char *)node, (char *)newEntry) == ST_OUT_OF_MEM) {
            memOut = 1;
            for (i = 0; i <= page; i++) ABC_FREE(mintermPages[i]);
            ABC_FREE(mintermPages);
            for (i = 0; i <= nodeDataPage; i++) ABC_FREE(nodeDataPages[i]);
            ABC_FREE(nodeDataPages);
            st_free_table(table);
            return(0.0);
        }
        return(min);
Alan Mishchenko committed
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
    }

} /* end of SubsetCountMintermAux */


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

  Synopsis    [Counts minterms of each node in the DAG]

  Description [Counts minterms of each node in the DAG. Similar to the
  Cudd_CountMinterm procedure except this returns the minterm count for
  all the nodes in the bdd in an st_table.]

  SideEffects [none]

  SeeAlso     [SubsetCountMintermAux]

******************************************************************************/
static st_table *
SubsetCountMinterm(
  DdNode * node /* function to be analyzed */,
  int nvars /* number of variables node depends on */)
{
    st_table    *table;
    int i;


#ifdef DEBUG
    num_calls = 0;
#endif

    max = pow(2.0,(double) nvars);
804
    table = st_init_table(st_ptrcmp,st_ptrhash);
Alan Mishchenko committed
805 806
    if (table == NULL) goto OUT_OF_MEM;
    maxPages = INITIAL_PAGES;
Alan Mishchenko committed
807
    mintermPages = ABC_ALLOC(double *,maxPages);
Alan Mishchenko committed
808
    if (mintermPages == NULL) {
809 810
        st_free_table(table);
        goto OUT_OF_MEM;
Alan Mishchenko committed
811 812
    }
    page = 0;
Alan Mishchenko committed
813
    currentMintermPage = ABC_ALLOC(double,pageSize);
Alan Mishchenko committed
814 815
    mintermPages[page] = currentMintermPage;
    if (currentMintermPage == NULL) {
816 817 818
        ABC_FREE(mintermPages);
        st_free_table(table);
        goto OUT_OF_MEM;
Alan Mishchenko committed
819 820 821
    }
    pageIndex = 0;
    maxNodeDataPages = INITIAL_PAGES;
Alan Mishchenko committed
822
    nodeDataPages = ABC_ALLOC(NodeData_t *, maxNodeDataPages);
Alan Mishchenko committed
823
    if (nodeDataPages == NULL) {
824 825 826 827
        for (i = 0; i <= page ; i++) ABC_FREE(mintermPages[i]);
        ABC_FREE(mintermPages);
        st_free_table(table);
        goto OUT_OF_MEM;
Alan Mishchenko committed
828 829
    }
    nodeDataPage = 0;
Alan Mishchenko committed
830
    currentNodeDataPage = ABC_ALLOC(NodeData_t ,nodeDataPageSize);
Alan Mishchenko committed
831 832
    nodeDataPages[nodeDataPage] = currentNodeDataPage;
    if (currentNodeDataPage == NULL) {
833 834 835 836 837
        for (i = 0; i <= page ; i++) ABC_FREE(mintermPages[i]);
        ABC_FREE(mintermPages);
        ABC_FREE(nodeDataPages);
        st_free_table(table);
        goto OUT_OF_MEM;
Alan Mishchenko committed
838 839 840
    }
    nodeDataPageIndex = 0;

841
    (void) SubsetCountMintermAux(node,max,table);
Alan Mishchenko committed
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
    if (memOut) goto OUT_OF_MEM;
    return(table);

OUT_OF_MEM:
    memOut = 1;
    return(NULL);

} /* end of SubsetCountMinterm */


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

  Synopsis    [Recursively counts the number of nodes under the dag.
  Also counts the number of nodes under the lighter child of
  this node.]

  Description [Recursively counts the number of nodes under the dag.
  Also counts the number of nodes under the lighter child of
  this node. . Note that the same dag may be the lighter child of two
  different nodes and have different counts. As with the minterm counts,
  the node counts are stored in pages to be space efficient and the
  address for these node counts are stored in an st_table associated
  to each node. ]

  SideEffects [Updates the node data table with node counts]

  SeeAlso     [SubsetCountNodes]

******************************************************************************/
static int
SubsetCountNodesAux(
  DdNode * node /* current node */,
  st_table * table /* table to update node count, also serves as visited table. */,
  double  max /* maximum number of variables */)
{
    int tval, eval, i;
    DdNode *N, *Nv, *Nnv;
    double minNv, minNnv;
    NodeData_t *dummyN, *dummyNv, *dummyNnv, *dummyNBar;
    int *pmin, *pminBar, *val;

    if ((node == NULL) || Cudd_IsConstant(node))
884
        return(0);
Alan Mishchenko committed
885 886

    /* if this node has been processed do nothing */
887 888 889 890
    if (st_lookup(table, (const char *)node, (char **)&dummyN) == 1) {
        val = dummyN->nodesPointer;
        if (val != NULL)
            return(0);
Alan Mishchenko committed
891
    } else {
892
        return(0);
Alan Mishchenko committed
893 894 895 896 897
    }

    N  = Cudd_Regular(node);
    Nv = Cudd_T(N);
    Nnv = Cudd_E(N);
898

Alan Mishchenko committed
899 900 901 902 903
    Nv = Cudd_NotCond(Nv, Cudd_IsComplement(node));
    Nnv = Cudd_NotCond(Nnv, Cudd_IsComplement(node));

    /* find the minterm counts for the THEN and ELSE branches */
    if (Cudd_IsConstant(Nv)) {
904 905 906 907 908
        if (Nv == zero) {
            minNv = 0.0;
        } else {
            minNv = max;
        }
Alan Mishchenko committed
909
    } else {
910 911 912 913 914
        if (st_lookup(table, (const char *)Nv, (char **)&dummyNv) == 1)
            minNv = *(dummyNv->mintermPointer);
        else {
            return(0);
        }
Alan Mishchenko committed
915 916
    }
    if (Cudd_IsConstant(Nnv)) {
917 918 919 920 921
        if (Nnv == zero) {
            minNnv = 0.0;
        } else {
            minNnv = max;
        }
Alan Mishchenko committed
922
    } else {
923 924 925 926 927 928
        if (st_lookup(table, (const char *)Nnv, (char **)&dummyNnv) == 1) {
            minNnv = *(dummyNnv->mintermPointer);
        }
        else {
            return(0);
        }
Alan Mishchenko committed
929 930 931 932 933
    }


    /* recur based on which has larger minterm, */
    if (minNv >= minNnv) {
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
        tval = SubsetCountNodesAux(Nv, table, max);
        if (memOut) return(0);
        eval = SubsetCountNodesAux(Nnv, table, max);
        if (memOut) return(0);

        /* store the node count of the lighter child. */
        if (pageIndex == pageSize) ResizeCountNodePages();
        if (memOut) {
            for (i = 0; i <= page; i++) ABC_FREE(mintermPages[i]);
            ABC_FREE(mintermPages);
            for (i = 0; i <= nodeDataPage; i++) ABC_FREE(nodeDataPages[i]);
            ABC_FREE(nodeDataPages);
            st_free_table(table);
            return(0);
        }
        pmin = currentLightNodePage + pageIndex;
        *pmin = eval; /* Here the ELSE child is lighter */
        dummyN->lightChildNodesPointer = pmin;
Alan Mishchenko committed
952 953

    } else {
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
        eval = SubsetCountNodesAux(Nnv, table, max);
        if (memOut) return(0);
        tval = SubsetCountNodesAux(Nv, table, max);
        if (memOut) return(0);

        /* store the node count of the lighter child. */
        if (pageIndex == pageSize) ResizeCountNodePages();
        if (memOut) {
            for (i = 0; i <= page; i++) ABC_FREE(mintermPages[i]);
            ABC_FREE(mintermPages);
            for (i = 0; i <= nodeDataPage; i++) ABC_FREE(nodeDataPages[i]);
            ABC_FREE(nodeDataPages);
            st_free_table(table);
            return(0);
        }
        pmin = currentLightNodePage + pageIndex;
        *pmin = tval; /* Here the THEN child is lighter */
        dummyN->lightChildNodesPointer = pmin;
Alan Mishchenko committed
972 973 974 975 976 977 978 979 980 981 982 983 984 985

    }
    /* updating the page index for node count storage. */
    pmin = currentNodePage + pageIndex;
    *pmin = tval + eval + 1;
    dummyN->nodesPointer = pmin;

    /* pageIndex is parallel page index for count_nodes and count_lightNodes */
    pageIndex++;

    /* if this node has been reached first, it belongs to a heavier
       branch. Its complement will be reached later on a lighter branch.
       Hence the complement has zero node count. */

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
    if (st_lookup(table, (const char *)Cudd_Not(node), (char **)&dummyNBar) == 1)  {
        if (pageIndex == pageSize) ResizeCountNodePages();
        if (memOut) {
            for (i = 0; i < page; i++) ABC_FREE(mintermPages[i]);
            ABC_FREE(mintermPages);
            for (i = 0; i < nodeDataPage; i++) ABC_FREE(nodeDataPages[i]);
            ABC_FREE(nodeDataPages);
            st_free_table(table);
            return(0);
        }
        pminBar = currentLightNodePage + pageIndex;
        *pminBar = 0;
        dummyNBar->lightChildNodesPointer = pminBar;
        /* The lighter child has less nodes than the parent.
         * So if parent 0 then lighter child zero
         */
        if (pageIndex == pageSize) ResizeCountNodePages();
        if (memOut) {
            for (i = 0; i < page; i++) ABC_FREE(mintermPages[i]);
            ABC_FREE(mintermPages);
            for (i = 0; i < nodeDataPage; i++) ABC_FREE(nodeDataPages[i]);
            ABC_FREE(nodeDataPages);
            st_free_table(table);
            return(0);
        }
        pminBar = currentNodePage + pageIndex;
        *pminBar = 0;
        dummyNBar->nodesPointer = pminBar ; /* maybe should point to zero */
Alan Mishchenko committed
1014

1015
        pageIndex++;
Alan Mishchenko committed
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
    }
    return(*pmin);
} /*end of SubsetCountNodesAux */


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

  Synopsis    [Counts the nodes under the current node and its lighter child]

  Description [Counts the nodes under the current node and its lighter
  child. Calls a recursive procedure to count the number of nodes of
  a DAG rooted at a particular node and the number of nodes taken by its
  lighter child.]

  SideEffects [None]

  SeeAlso     [SubsetCountNodesAux]

******************************************************************************/
static int
SubsetCountNodes(
  DdNode * node /* function to be analyzed */,
  st_table * table /* node quality table */,
  int  nvars /* number of variables node depends on */)
{
1041
    int num;
Alan Mishchenko committed
1042 1043 1044 1045 1046 1047 1048 1049
    int i;

#ifdef DEBUG
    num_calls = 0;
#endif

    max = pow(2.0,(double) nvars);
    maxPages = INITIAL_PAGES;
Alan Mishchenko committed
1050
    nodePages = ABC_ALLOC(int *,maxPages);
Alan Mishchenko committed
1051
    if (nodePages == NULL)  {
1052
        goto OUT_OF_MEM;
Alan Mishchenko committed
1053 1054
    }

Alan Mishchenko committed
1055
    lightNodePages = ABC_ALLOC(int *,maxPages);
Alan Mishchenko committed
1056
    if (lightNodePages == NULL) {
1057 1058 1059 1060 1061 1062
        for (i = 0; i <= page; i++) ABC_FREE(mintermPages[i]);
        ABC_FREE(mintermPages);
        for (i = 0; i <= nodeDataPage; i++) ABC_FREE(nodeDataPages[i]);
        ABC_FREE(nodeDataPages);
        ABC_FREE(nodePages);
        goto OUT_OF_MEM;
Alan Mishchenko committed
1063 1064 1065
    }

    page = 0;
Alan Mishchenko committed
1066
    currentNodePage = nodePages[page] = ABC_ALLOC(int,pageSize);
Alan Mishchenko committed
1067
    if (currentNodePage == NULL) {
1068 1069 1070 1071 1072 1073 1074
        for (i = 0; i <= page; i++) ABC_FREE(mintermPages[i]);
        ABC_FREE(mintermPages);
        for (i = 0; i <= nodeDataPage; i++) ABC_FREE(nodeDataPages[i]);
        ABC_FREE(nodeDataPages);
        ABC_FREE(lightNodePages);
        ABC_FREE(nodePages);
        goto OUT_OF_MEM;
Alan Mishchenko committed
1075 1076
    }

Alan Mishchenko committed
1077
    currentLightNodePage = lightNodePages[page] = ABC_ALLOC(int,pageSize);
Alan Mishchenko committed
1078
    if (currentLightNodePage == NULL) {
1079 1080 1081 1082 1083 1084 1085 1086
        for (i = 0; i <= page; i++) ABC_FREE(mintermPages[i]);
        ABC_FREE(mintermPages);
        for (i = 0; i <= nodeDataPage; i++) ABC_FREE(nodeDataPages[i]);
        ABC_FREE(nodeDataPages);
        ABC_FREE(currentNodePage);
        ABC_FREE(lightNodePages);
        ABC_FREE(nodePages);
        goto OUT_OF_MEM;
Alan Mishchenko committed
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
    }

    pageIndex = 0;
    num = SubsetCountNodesAux(node,table,max);
    if (memOut) goto OUT_OF_MEM;
    return(num);

OUT_OF_MEM:
    memOut = 1;
    return(0);

} /* end of SubsetCountNodes */


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

  Synopsis    [Procedure to recursively store nodes that are retained in the subset.]

  Description [rocedure to recursively store nodes that are retained in the subset.]

  SideEffects [None]

  SeeAlso     [StoreNodes]

******************************************************************************/
static void
StoreNodes(
  st_table * storeTable,
  DdManager * dd,
  DdNode * node)
{
    DdNode *N, *Nt, *Ne;
    if (Cudd_IsConstant(dd)) {
1120
        return;
Alan Mishchenko committed
1121 1122
    }
    N = Cudd_Regular(node);
1123 1124
    if (st_lookup(storeTable, (char *)N, NIL(char *))) {
        return;
Alan Mishchenko committed
1125 1126 1127
    }
    cuddRef(N);
    if (st_insert(storeTable, (char *)N, NIL(char)) == ST_OUT_OF_MEM) {
1128
        fprintf(dd->err,"Something wrong, st_table insert failed\n");
Alan Mishchenko committed
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
    }

    Nt = Cudd_T(N);
    Ne = Cudd_E(N);

    StoreNodes(storeTable, dd, Nt);
    StoreNodes(storeTable, dd, Ne);
    return;

}


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

1143
  Synopsis    [Builds the subset BDD using the heavy branch method.]
Alan Mishchenko committed
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 1187 1188

  Description [The procedure carries out the building of the subset BDD
  starting at the root. Using the three different counts labelling each node,
  the procedure chooses the heavier branch starting from the root and keeps
  track of the number of nodes it discards at each step, thus keeping count
  of the size of the subset BDD dynamically. Once the threshold is satisfied,
  the procedure then calls ITE to build the BDD.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static DdNode *
BuildSubsetBdd(
  DdManager * dd /* DD manager */,
  DdNode * node /* current node */,
  int * size /* current size of the subset */,
  st_table * visitedTable /* visited table storing all node data */,
  int  threshold,
  st_table * storeTable,
  st_table * approxTable)
{

    DdNode *Nv, *Nnv, *N, *topv, *neW;
    double minNv, minNnv;
    NodeData_t *currNodeQual;
    NodeData_t *currNodeQualT;
    NodeData_t *currNodeQualE;
    DdNode *ThenBranch, *ElseBranch;
    unsigned int topid;
    char *dummy;

#ifdef DEBUG
    num_calls++;
#endif
    /*If the size of the subset is below the threshold, dont do
      anything. */
    if ((*size) <= threshold) {
      /* store nodes below this, so we can recombine if possible */
      StoreNodes(storeTable, dd, node);
      return(node);
    }

    if (Cudd_IsConstant(node))
1189
        return(node);
Alan Mishchenko committed
1190 1191

    /* Look up minterm count for this node. */
1192 1193 1194
    if (!st_lookup(visitedTable, (const char *)node, (char **)&currNodeQual)) {
        fprintf(dd->err,
                "Something is wrong, ought to be in node quality table\n");
Alan Mishchenko committed
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
    }

    /* Get children. */
    N = Cudd_Regular(node);
    Nv = Cudd_T(N);
    Nnv = Cudd_E(N);

    /* complement if necessary */
    Nv = Cudd_NotCond(Nv, Cudd_IsComplement(node));
    Nnv = Cudd_NotCond(Nnv, Cudd_IsComplement(node));

    if (!Cudd_IsConstant(Nv)) {
        /* find out minterms and nodes contributed by then child */
1208 1209 1210 1211 1212 1213 1214
        if (!st_lookup(visitedTable, (const char *)Nv, (char **)&currNodeQualT)) {
                fprintf(dd->out,"Something wrong, couldnt find nodes in node quality table\n");
                dd->errorCode = CUDD_INTERNAL_ERROR;
                return(NULL);
            }
        else {
            minNv = *(((NodeData_t *)currNodeQualT)->mintermPointer);
Alan Mishchenko committed
1215 1216
        }
    } else {
1217 1218 1219 1220 1221
        if (Nv == zero) {
            minNv = 0;
        } else  {
            minNv = max;
        }
Alan Mishchenko committed
1222 1223 1224
    }
    if (!Cudd_IsConstant(Nnv)) {
        /* find out minterms and nodes contributed by else child */
1225 1226 1227 1228 1229 1230 1231
        if (!st_lookup(visitedTable, (const char *)Nnv, (char **)&currNodeQualE)) {
            fprintf(dd->out,"Something wrong, couldnt find nodes in node quality table\n");
            dd->errorCode = CUDD_INTERNAL_ERROR;
            return(NULL);
        } else {
            minNnv = *(((NodeData_t *)currNodeQualE)->mintermPointer);
        }
Alan Mishchenko committed
1232
    } else {
1233 1234 1235 1236 1237
        if (Nnv == zero) {
            minNnv = 0;
        } else {
            minNnv = max;
        }
Alan Mishchenko committed
1238 1239 1240 1241 1242 1243 1244
    }

    /* keep track of size of subset by subtracting the number of
     * differential nodes contributed by lighter child
     */
    *size = (*(size)) - (int)*(currNodeQual->lightChildNodesPointer);
    if (minNv >= minNnv) { /*SubsetCountNodesAux procedure takes
1245
                             the Then branch in case of a tie */
Alan Mishchenko committed
1246 1247

        /* recur with the Then branch */
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
        ThenBranch = (DdNode *)BuildSubsetBdd(dd, Nv, size,
              visitedTable, threshold, storeTable, approxTable);
        if (ThenBranch == NULL) {
            return(NULL);
        }
        cuddRef(ThenBranch);
        /* The Else branch is either a node that already exists in the
         * subset, or one whose approximation has been computed, or
         * Zero.
         */
        if (st_lookup(storeTable, (char *)Cudd_Regular(Nnv), &dummy)) {
          ElseBranch = Nnv;
          cuddRef(ElseBranch);
        } else {
          if (st_lookup(approxTable, (char *)Nnv, &dummy)) {
            ElseBranch = (DdNode *)dummy;
            cuddRef(ElseBranch);
          } else {
            ElseBranch = zero;
            cuddRef(ElseBranch);
          }
        }

Alan Mishchenko committed
1271 1272 1273 1274
    }
    else {
        /* recur with the Else branch */
        ElseBranch = (DdNode *)BuildSubsetBdd(dd, Nnv, size,
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
                      visitedTable, threshold, storeTable, approxTable);
        if (ElseBranch == NULL) {
            return(NULL);
        }
        cuddRef(ElseBranch);
        /* The Then branch is either a node that already exists in the
         * subset, or one whose approximation has been computed, or
         * Zero.
         */
        if (st_lookup(storeTable, (char *)Cudd_Regular(Nv), &dummy)) {
          ThenBranch = Nv;
          cuddRef(ThenBranch);
        } else {
          if (st_lookup(approxTable, (char *)Nv, &dummy)) {
            ThenBranch = (DdNode *)dummy;
            cuddRef(ThenBranch);
          } else {
            ThenBranch = zero;
            cuddRef(ThenBranch);
          }
        }
Alan Mishchenko committed
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
    }

    /* construct the Bdd with the top variable and the two children */
    topid = Cudd_NodeReadIndex(N);
    topv = Cudd_ReadVars(dd, topid);
    cuddRef(topv);
    neW =  cuddBddIteRecur(dd, topv, ThenBranch, ElseBranch);
    if (neW != NULL) {
      cuddRef(neW);
    }
    Cudd_RecursiveDeref(dd, topv);
    Cudd_RecursiveDeref(dd, ThenBranch);
    Cudd_RecursiveDeref(dd, ElseBranch);

1310

Alan Mishchenko committed
1311
    if (neW == NULL)
1312
        return(NULL);
Alan Mishchenko committed
1313 1314
    else {
        /* store this node in the store table */
1315 1316 1317 1318
        if (!st_lookup(storeTable, (char *)Cudd_Regular(neW), &dummy)) {
          cuddRef(neW);
          if (!st_insert(storeTable, (char *)Cudd_Regular(neW), NIL(char)))
              return (NULL);
Alan Mishchenko committed
1319
        }
1320 1321 1322 1323 1324 1325 1326 1327 1328
        /* store the approximation for this node */
        if (N !=  Cudd_Regular(neW)) {
            if (st_lookup(approxTable, (char *)node, &dummy)) {
                fprintf(dd->err, "This node should not be in the approximated table\n");
            } else {
                cuddRef(neW);
                if (!st_insert(approxTable, (char *)node, (char *)neW))
                    return(NULL);
            }
Alan Mishchenko committed
1329 1330 1331 1332 1333 1334
        }
        cuddDeref(neW);
        return(neW);
    }
} /* end of BuildSubsetBdd */

1335

1336 1337
ABC_NAMESPACE_IMPL_END