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

  FileName    [cuddZddUtil.c]

  PackageName [cudd]

  Synopsis    [Utility functions for ZDDs.]

  Description [External procedures included in this module:
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
                    <ul>
                    <li> Cudd_zddPrintMinterm()
                    <li> Cudd_zddPrintCover()
                    <li> Cudd_zddPrintDebug()
                    <li> Cudd_zddFirstPath()
                    <li> Cudd_zddNextPath()
                    <li> Cudd_zddCoverPathToString()
                    <li> Cudd_zddDumpDot()
                    </ul>
               Internal procedures included in this module:
                    <ul>
                    <li> cuddZddP()
                    </ul>
               Static procedures included in this module:
                    <ul>
                    <li> zp2()
                    <li> zdd_print_minterm_aux()
                    <li> zddPrintCoverAux()
                    </ul>
              ]
Alan Mishchenko committed
30 31 32

  SeeAlso     []

33
  Author      [Hyong-Kyoon Shin, In-Ho Moon, Fabio Somenzi]
Alan Mishchenko committed
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 61 62 63 64 65
  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
66 67 68

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

69
#include "misc/util/util_hack.h"
70
#include "cuddInt.h"
Alan Mishchenko committed
71

72 73 74
ABC_NAMESPACE_IMPL_START


75

Alan Mishchenko committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
/*---------------------------------------------------------------------------*/
/* Constant declarations                                                     */
/*---------------------------------------------------------------------------*/


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


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


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

#ifndef lint
96
static char rcsid[] DD_UNUSED = "$Id: cuddZddUtil.c,v 1.27 2009/03/08 02:49:02 fabio Exp $";
Alan Mishchenko committed
97 98 99 100 101 102 103 104 105 106 107 108 109
#endif

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


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

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

110
static int zp2 (DdManager *zdd, DdNode *f, st__table *t);
111 112
static void zdd_print_minterm_aux (DdManager *zdd, DdNode *node, int level, int *list);
static void zddPrintCoverAux (DdManager *zdd, DdNode *node, int level, int *list);
Alan Mishchenko committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

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


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


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

  Synopsis    [Prints a disjoint sum of product form for a ZDD.]

  Description [Prints a disjoint sum of product form for a ZDD. Returns 1
  if successful; 0 otherwise.]

  SideEffects [None]

  SeeAlso     [Cudd_zddPrintDebug Cudd_zddPrintCover]

******************************************************************************/
int
Cudd_zddPrintMinterm(
  DdManager * zdd,
  DdNode * node)
{
139 140
    int         i, size;
    int         *list;
Alan Mishchenko committed
141 142

    size = (int)zdd->sizeZ;
Alan Mishchenko committed
143
    list = ABC_ALLOC(int, size);
Alan Mishchenko committed
144
    if (list == NULL) {
145 146
        zdd->errorCode = CUDD_MEMORY_OUT;
        return(0);
Alan Mishchenko committed
147 148 149
    }
    for (i = 0; i < size; i++) list[i] = 3; /* bogus value should disappear */
    zdd_print_minterm_aux(zdd, node, 0, list);
Alan Mishchenko committed
150
    ABC_FREE(list);
Alan Mishchenko committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    return(1);

} /* end of Cudd_zddPrintMinterm */


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

  Synopsis    [Prints a sum of products from a ZDD representing a cover.]

  Description [Prints a sum of products from a ZDD representing a cover.
  Returns 1 if successful; 0 otherwise.]

  SideEffects [None]

  SeeAlso     [Cudd_zddPrintMinterm]

******************************************************************************/
int
Cudd_zddPrintCover(
  DdManager * zdd,
  DdNode * node)
{
173 174
    int         i, size;
    int         *list;
Alan Mishchenko committed
175 176 177

    size = (int)zdd->sizeZ;
    if (size % 2 != 0) return(0); /* number of variables should be even */
Alan Mishchenko committed
178
    list = ABC_ALLOC(int, size);
Alan Mishchenko committed
179
    if (list == NULL) {
180 181
        zdd->errorCode = CUDD_MEMORY_OUT;
        return(0);
Alan Mishchenko committed
182 183 184
    }
    for (i = 0; i < size; i++) list[i] = 3; /* bogus value should disappear */
    zddPrintCoverAux(zdd, node, 0, list);
Alan Mishchenko committed
185
    ABC_FREE(list);
Alan Mishchenko committed
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 219 220
    return(1);

} /* end of Cudd_zddPrintCover */


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

  Synopsis [Prints to the standard output a ZDD and its statistics.]

  Description [Prints to the standard output a DD and its statistics.
  The statistics include the number of nodes and the number of minterms.
  (The number of minterms is also the number of combinations in the set.)
  The statistics are printed if pr &gt; 0.  Specifically:
  <ul>
  <li> pr = 0 : prints nothing
  <li> pr = 1 : prints counts of nodes and minterms
  <li> pr = 2 : prints counts + disjoint sum of products
  <li> pr = 3 : prints counts + list of nodes
  <li> pr &gt; 3 : prints counts + disjoint sum of products + list of nodes
  </ul>
  Returns 1 if successful; 0 otherwise.
  ]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
int
Cudd_zddPrintDebug(
  DdManager * zdd,
  DdNode * f,
  int  n,
  int  pr)
{
221 222 223 224
    DdNode      *empty = DD_ZERO(zdd);
    int         nodes;
    double      minterms;
    int         retval = 1;
Alan Mishchenko committed
225 226

    if (f == empty && pr > 0) {
227 228 229
        (void) fprintf(zdd->out,": is the empty ZDD\n");
        (void) fflush(zdd->out);
        return(1);
Alan Mishchenko committed
230 231 232
    }

    if (pr > 0) {
233 234 235 236 237 238 239 240 241 242 243 244 245
        nodes = Cudd_zddDagSize(f);
        if (nodes == CUDD_OUT_OF_MEM) retval = 0;
        minterms = Cudd_zddCountMinterm(zdd, f, n);
        if (minterms == (double)CUDD_OUT_OF_MEM) retval = 0;
        (void) fprintf(zdd->out,": %d nodes %g minterms\n",
                       nodes, minterms);
        if (pr > 2)
            if (!cuddZddP(zdd, f)) retval = 0;
        if (pr == 2 || pr > 3) {
            if (!Cudd_zddPrintMinterm(zdd, f)) retval = 0;
            (void) fprintf(zdd->out,"\n");
        }
        (void) fflush(zdd->out);
Alan Mishchenko committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
    }
    return(retval);

} /* end of Cudd_zddPrintDebug */



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

  Synopsis    [Finds the first path of a ZDD.]

  Description [Defines an iterator on the paths of a ZDD
  and finds its first path. Returns a generator that contains the
  information necessary to continue the enumeration if successful; NULL
  otherwise.<p>
  A path is represented as an array of literals, which are integers in
  {0, 1, 2}; 0 represents an else arc out of a node, 1 represents a then arc
  out of a node, and 2 stands for the absence of a node.
  The size of the array equals the number of variables in the manager at
  the time Cudd_zddFirstCube is called.<p>
  The paths that end in the empty terminal are not enumerated.]

  SideEffects [The first path is returned as a side effect.]

  SeeAlso     [Cudd_zddForeachPath Cudd_zddNextPath Cudd_GenFree
  Cudd_IsGenEmpty]

******************************************************************************/
DdGen *
Cudd_zddFirstPath(
  DdManager * zdd,
  DdNode * f,
  int ** path)
{
    DdGen *gen;
    DdNode *top, *next, *prev;
    int i;
    int nvars;

    /* Sanity Check. */
    if (zdd == NULL || f == NULL) return(NULL);

    /* Allocate generator an initialize it. */
Alan Mishchenko committed
289
    gen = ABC_ALLOC(DdGen,1);
Alan Mishchenko committed
290
    if (gen == NULL) {
291 292
        zdd->errorCode = CUDD_MEMORY_OUT;
        return(NULL);
Alan Mishchenko committed
293 294 295 296 297 298 299 300 301 302 303 304
    }

    gen->manager = zdd;
    gen->type = CUDD_GEN_ZDD_PATHS;
    gen->status = CUDD_GEN_EMPTY;
    gen->gen.cubes.cube = NULL;
    gen->gen.cubes.value = DD_ZERO_VAL;
    gen->stack.sp = 0;
    gen->stack.stack = NULL;
    gen->node = NULL;

    nvars = zdd->sizeZ;
Alan Mishchenko committed
305
    gen->gen.cubes.cube = ABC_ALLOC(int,nvars);
Alan Mishchenko committed
306
    if (gen->gen.cubes.cube == NULL) {
307 308 309
        zdd->errorCode = CUDD_MEMORY_OUT;
        ABC_FREE(gen);
        return(NULL);
Alan Mishchenko committed
310 311 312 313 314 315 316
    }
    for (i = 0; i < nvars; i++) gen->gen.cubes.cube[i] = 2;

    /* The maximum stack depth is one plus the number of variables.
    ** because a path may have nodes at all levels, including the
    ** constant level.
    */
317
    gen->stack.stack = ABC_ALLOC(DdNodePtr, nvars+1);
Alan Mishchenko committed
318
    if (gen->stack.stack == NULL) {
319 320 321 322
        zdd->errorCode = CUDD_MEMORY_OUT;
        ABC_FREE(gen->gen.cubes.cube);
        ABC_FREE(gen);
        return(NULL);
Alan Mishchenko committed
323 324 325 326 327 328 329 330
    }
    for (i = 0; i <= nvars; i++) gen->stack.stack[i] = NULL;

    /* Find the first path of the ZDD. */
    gen->stack.stack[gen->stack.sp] = f; gen->stack.sp++;

    while (1) {
        top = gen->stack.stack[gen->stack.sp-1];
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
        if (!cuddIsConstant(Cudd_Regular(top))) {
            /* Take the else branch first. */
            gen->gen.cubes.cube[Cudd_Regular(top)->index] = 0;
            next = cuddE(Cudd_Regular(top));
            gen->stack.stack[gen->stack.sp] = Cudd_Not(next); gen->stack.sp++;
        } else if (Cudd_Regular(top) == DD_ZERO(zdd)) {
            /* Backtrack. */
            while (1) {
                if (gen->stack.sp == 1) {
                    /* The current node has no predecessor. */
                    gen->status = CUDD_GEN_EMPTY;
                    gen->stack.sp--;
                    goto done;
                }
                prev = Cudd_Regular(gen->stack.stack[gen->stack.sp-2]);
                next = cuddT(prev);
                if (next != top) { /* follow the then branch next */
                    gen->gen.cubes.cube[prev->index] = 1;
                    gen->stack.stack[gen->stack.sp-1] = next;
                    break;
                }
                /* Pop the stack and try again. */
                gen->gen.cubes.cube[prev->index] = 2;
                gen->stack.sp--;
                top = gen->stack.stack[gen->stack.sp-1];
            }
        } else {
            gen->status = CUDD_GEN_NONEMPTY;
            gen->gen.cubes.value = cuddV(Cudd_Regular(top));
            goto done;
Alan Mishchenko committed
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
        }
    }

done:
    *path = gen->gen.cubes.cube;
    return(gen);

} /* end of Cudd_zddFirstPath */


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

  Synopsis    [Generates the next path of a ZDD.]

  Description [Generates the next path of a ZDD onset,
  using generator gen. Returns 0 if the enumeration is completed; 1
  otherwise.]

  SideEffects [The path is returned as a side effect. The
  generator is modified.]

  SeeAlso     [Cudd_zddForeachPath Cudd_zddFirstPath Cudd_GenFree
  Cudd_IsGenEmpty]

******************************************************************************/
int
Cudd_zddNextPath(
  DdGen * gen,
  int ** path)
{
    DdNode *top, *next, *prev;
    DdManager *zdd = gen->manager;

    /* Backtrack from previously reached terminal node. */
    while (1) {
        if (gen->stack.sp == 1) {
            /* The current node has no predecessor. */
            gen->status = CUDD_GEN_EMPTY;
            gen->stack.sp--;
            goto done;
        }
402 403
        top = gen->stack.stack[gen->stack.sp-1];
        prev = Cudd_Regular(gen->stack.stack[gen->stack.sp-2]);
Alan Mishchenko committed
404 405 406 407 408 409 410 411 412
        next = cuddT(prev);
        if (next != top) { /* follow the then branch next */
            gen->gen.cubes.cube[prev->index] = 1;
            gen->stack.stack[gen->stack.sp-1] = next;
            break;
        }
        /* Pop the stack and try again. */
        gen->gen.cubes.cube[prev->index] = 2;
        gen->stack.sp--;
413 414 415
    }

    while (1) {
Alan Mishchenko committed
416
        top = gen->stack.stack[gen->stack.sp-1];
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
        if (!cuddIsConstant(Cudd_Regular(top))) {
            /* Take the else branch first. */
            gen->gen.cubes.cube[Cudd_Regular(top)->index] = 0;
            next = cuddE(Cudd_Regular(top));
            gen->stack.stack[gen->stack.sp] = Cudd_Not(next); gen->stack.sp++;
        } else if (Cudd_Regular(top) == DD_ZERO(zdd)) {
            /* Backtrack. */
            while (1) {
                if (gen->stack.sp == 1) {
                    /* The current node has no predecessor. */
                    gen->status = CUDD_GEN_EMPTY;
                    gen->stack.sp--;
                    goto done;
                }
                prev = Cudd_Regular(gen->stack.stack[gen->stack.sp-2]);
                next = cuddT(prev);
                if (next != top) { /* follow the then branch next */
                    gen->gen.cubes.cube[prev->index] = 1;
                    gen->stack.stack[gen->stack.sp-1] = next;
                    break;
                }
                /* Pop the stack and try again. */
                gen->gen.cubes.cube[prev->index] = 2;
                gen->stack.sp--;
                top = gen->stack.stack[gen->stack.sp-1];
            }
        } else {
            gen->status = CUDD_GEN_NONEMPTY;
            gen->gen.cubes.value = cuddV(Cudd_Regular(top));
            goto done;
Alan Mishchenko committed
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 474 475
        }
    }

done:
    if (gen->status == CUDD_GEN_EMPTY) return(0);
    *path = gen->gen.cubes.cube;
    return(1);

} /* end of Cudd_zddNextPath */


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

  Synopsis    [Converts a path of a ZDD representing a cover to a string.]

  Description [Converts a path of a ZDD representing a cover to a
  string.  The string represents an implicant of the cover.  The path
  is typically produced by Cudd_zddForeachPath.  Returns a pointer to
  the string if successful; NULL otherwise.  If the str input is NULL,
  it allocates a new string.  The string passed to this function must
  have enough room for all variables and for the terminator.]

  SideEffects [None]

  SeeAlso     [Cudd_zddForeachPath]

******************************************************************************/
char *
Cudd_zddCoverPathToString(
476 477 478
  DdManager *zdd                /* DD manager */,
  int *path                     /* path of ZDD representing a cover */,
  char *str                     /* pointer to string to use if != NULL */
Alan Mishchenko committed
479 480 481 482 483 484 485 486 487
  )
{
    int nvars = zdd->sizeZ;
    int i;
    char *res;

    if (nvars & 1) return(NULL);
    nvars >>= 1;
    if (str == NULL) {
488 489
        res = ABC_ALLOC(char, nvars+1);
        if (res == NULL) return(NULL);
Alan Mishchenko committed
490
    } else {
491
        res = str;
Alan Mishchenko committed
492 493
    }
    for (i = 0; i < nvars; i++) {
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
        int v = (path[2*i] << 2) | path[2*i+1];
        switch (v) {
        case 0:
        case 2:
        case 8:
        case 10:
            res[i] = '-';
            break;
        case 1:
        case 9:
            res[i] = '0';
            break;
        case 4:
        case 6:
            res[i] = '1';
            break;
        default:
            res[i] = '?';
        }
Alan Mishchenko committed
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
    }
    res[nvars] = 0;

    return(res);

} /* end of Cudd_zddCoverPathToString */


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

  Synopsis    [Writes a dot file representing the argument ZDDs.]

  Description [Writes a file representing the argument ZDDs in a format
  suitable for the graph drawing program dot.
  It returns 1 in case of success; 0 otherwise (e.g., out-of-memory,
  file system full).
  Cudd_zddDumpDot does not close the file: This is the caller
  responsibility. Cudd_zddDumpDot uses a minimal unique subset of the
  hexadecimal address of a node as name for it.
  If the argument inames is non-null, it is assumed to hold the pointers
  to the names of the inputs. Similarly for onames.
  Cudd_zddDumpDot uses the following convention to draw arcs:
    <ul>
    <li> solid line: THEN arcs;
    <li> dashed line: ELSE arcs.
    </ul>
  The dot options are chosen so that the drawing fits on a letter-size
  sheet.
  ]

  SideEffects [None]

  SeeAlso     [Cudd_DumpDot Cudd_zddPrintDebug]

******************************************************************************/
int
Cudd_zddDumpDot(
  DdManager * dd /* manager */,
  int  n /* number of output nodes to be dumped */,
  DdNode ** f /* array of output nodes to be dumped */,
  char ** inames /* array of input names (or NULL) */,
  char ** onames /* array of output names (or NULL) */,
  FILE * fp /* pointer to the dump file */)
{
557 558 559 560
    DdNode      *support = NULL;
    DdNode      *scan;
    int         *sorted = NULL;
    int         nvars = dd->sizeZ;
561 562
    st__table    *visited = NULL;
    st__generator *gen;
563 564 565 566 567
    int         retval;
    int         i, j;
    int         slots;
    DdNodePtr   *nodelist;
    long        refAddr, diff, mask;
Alan Mishchenko committed
568 569

    /* Build a bit array with the support of f. */
Alan Mishchenko committed
570
    sorted = ABC_ALLOC(int,nvars);
Alan Mishchenko committed
571
    if (sorted == NULL) {
572 573
        dd->errorCode = CUDD_MEMORY_OUT;
        goto failure;
Alan Mishchenko committed
574 575 576 577 578
    }
    for (i = 0; i < nvars; i++) sorted[i] = 0;

    /* Take the union of the supports of each output function. */
    for (i = 0; i < n; i++) {
579 580 581 582 583 584 585 586 587
        support = Cudd_Support(dd,f[i]);
        if (support == NULL) goto failure;
        cuddRef(support);
        scan = support;
        while (!cuddIsConstant(scan)) {
            sorted[scan->index] = 1;
            scan = cuddT(scan);
        }
        Cudd_RecursiveDeref(dd,support);
Alan Mishchenko committed
588
    }
589
    support = NULL; /* so that we do not try to free it in case of failure */
Alan Mishchenko committed
590 591

    /* Initialize symbol table for visited nodes. */
592
    visited = st__init_table( st__ptrcmp, st__ptrhash);
Alan Mishchenko committed
593 594 595 596
    if (visited == NULL) goto failure;

    /* Collect all the nodes of this DD in the symbol table. */
    for (i = 0; i < n; i++) {
597 598
        retval = cuddCollectNodes(f[i],visited);
        if (retval == 0) goto failure;
Alan Mishchenko committed
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
    }

    /* Find how many most significant hex digits are identical
    ** in the addresses of all the nodes. Build a mask based
    ** on this knowledge, so that digits that carry no information
    ** will not be printed. This is done in two steps.
    **  1. We scan the symbol table to find the bits that differ
    **     in at least 2 addresses.
    **  2. We choose one of the possible masks. There are 8 possible
    **     masks for 32-bit integer, and 16 possible masks for 64-bit
    **     integers.
    */

    /* Find the bits that are different. */
    refAddr = (long) f[0];
    diff = 0;
615 616
    gen = st__init_gen(visited);
    while ( st__gen(gen, (const char **)&scan, NULL)) {
617
        diff |= refAddr ^ (long) scan;
Alan Mishchenko committed
618
    }
619
    st__free_gen(gen);
Alan Mishchenko committed
620 621 622

    /* Choose the mask. */
    for (i = 0; (unsigned) i < 8 * sizeof(long); i += 4) {
623 624
        mask = (1 << i) - 1;
        if (diff <= mask) break;
Alan Mishchenko committed
625 626 627 628 629 630
    }

    /* Write the header and the global attributes. */
    retval = fprintf(fp,"digraph \"ZDD\" {\n");
    if (retval == EOF) return(0);
    retval = fprintf(fp,
631
        "size = \"7.5,10\"\ncenter = true;\nedge [dir = none];\n");
Alan Mishchenko committed
632 633 634 635 636 637 638 639 640 641 642 643 644 645
    if (retval == EOF) return(0);

    /* Write the input name subgraph by scanning the support array. */
    retval = fprintf(fp,"{ node [shape = plaintext];\n");
    if (retval == EOF) goto failure;
    retval = fprintf(fp,"  edge [style = invis];\n");
    if (retval == EOF) goto failure;
    /* We use a name ("CONST NODES") with an embedded blank, because
    ** it is unlikely to appear as an input name.
    */
    retval = fprintf(fp,"  \"CONST NODES\" [style = invis];\n");
    if (retval == EOF) goto failure;
    for (i = 0; i < nvars; i++) {
        if (sorted[dd->invpermZ[i]]) {
646 647 648 649 650
            if (inames == NULL) {
                retval = fprintf(fp,"\" %d \" -> ", dd->invpermZ[i]);
            } else {
                retval = fprintf(fp,"\" %s \" -> ", inames[dd->invpermZ[i]]);
            }
Alan Mishchenko committed
651 652 653 654 655 656 657 658 659 660
            if (retval == EOF) goto failure;
        }
    }
    retval = fprintf(fp,"\"CONST NODES\"; \n}\n");
    if (retval == EOF) goto failure;

    /* Write the output node subgraph. */
    retval = fprintf(fp,"{ rank = same; node [shape = box]; edge [style = invis];\n");
    if (retval == EOF) goto failure;
    for (i = 0; i < n; i++) {
661 662 663 664 665 666 667 668 669 670 671 672
        if (onames == NULL) {
            retval = fprintf(fp,"\"F%d\"", i);
        } else {
            retval = fprintf(fp,"\"  %s  \"", onames[i]);
        }
        if (retval == EOF) goto failure;
        if (i == n - 1) {
            retval = fprintf(fp,"; }\n");
        } else {
            retval = fprintf(fp," -> ");
        }
        if (retval == EOF) goto failure;
Alan Mishchenko committed
673 674 675 676 677
    }

    /* Write rank info: All nodes with the same index have the same rank. */
    for (i = 0; i < nvars; i++) {
        if (sorted[dd->invpermZ[i]]) {
678
            retval = fprintf(fp,"{ rank = same; ");
Alan Mishchenko committed
679
            if (retval == EOF) goto failure;
680 681 682 683 684
            if (inames == NULL) {
                retval = fprintf(fp,"\" %d \";\n", dd->invpermZ[i]);
            } else {
                retval = fprintf(fp,"\" %s \";\n", inames[dd->invpermZ[i]]);
            }
Alan Mishchenko committed
685
            if (retval == EOF) goto failure;
686 687 688 689 690
            nodelist = dd->subtableZ[i].nodelist;
            slots = dd->subtableZ[i].slots;
            for (j = 0; j < slots; j++) {
                scan = nodelist[j];
                while (scan != NULL) {
691
                    if ( st__is_member(visited,(char *) scan)) {
692 693 694 695 696 697 698
                        retval = fprintf(fp,"\"%p\";\n", (void *)
                                         ((mask & (ptrint) scan) /
                                          sizeof(DdNode)));
                        if (retval == EOF) goto failure;
                    }
                    scan = scan->next;
                }
Alan Mishchenko committed
699
            }
700 701
            retval = fprintf(fp,"}\n");
            if (retval == EOF) goto failure;
Alan Mishchenko committed
702 703 704 705 706
        }
    }

    /* All constants have the same rank. */
    retval = fprintf(fp,
707
        "{ rank = same; \"CONST NODES\";\n{ node [shape = box]; ");
Alan Mishchenko committed
708 709 710 711
    if (retval == EOF) goto failure;
    nodelist = dd->constants.nodelist;
    slots = dd->constants.slots;
    for (j = 0; j < slots; j++) {
712 713
        scan = nodelist[j];
        while (scan != NULL) {
714
            if ( st__is_member(visited,(char *) scan)) {
715 716 717 718 719
                retval = fprintf(fp,"\"%p\";\n", (void *)
                                 ((mask & (ptrint) scan) / sizeof(DdNode)));
                if (retval == EOF) goto failure;
            }
            scan = scan->next;
Alan Mishchenko committed
720 721 722 723 724 725 726 727
        }
    }
    retval = fprintf(fp,"}\n}\n");
    if (retval == EOF) goto failure;

    /* Write edge info. */
    /* Edges from the output nodes. */
    for (i = 0; i < n; i++) {
728 729 730 731 732 733 734 735 736 737
        if (onames == NULL) {
            retval = fprintf(fp,"\"F%d\"", i);
        } else {
            retval = fprintf(fp,"\"  %s  \"", onames[i]);
        }
        if (retval == EOF) goto failure;
        retval = fprintf(fp," -> \"%p\" [style = solid];\n",
                         (void *) ((mask & (ptrint) f[i]) /
                                          sizeof(DdNode)));
        if (retval == EOF) goto failure;
Alan Mishchenko committed
738 739 740 741 742
    }

    /* Edges from internal nodes. */
    for (i = 0; i < nvars; i++) {
        if (sorted[dd->invpermZ[i]]) {
743 744 745 746 747
            nodelist = dd->subtableZ[i].nodelist;
            slots = dd->subtableZ[i].slots;
            for (j = 0; j < slots; j++) {
                scan = nodelist[j];
                while (scan != NULL) {
748
                    if ( st__is_member(visited,(char *) scan)) {
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
                        retval = fprintf(fp,
                            "\"%p\" -> \"%p\";\n",
                            (void *) ((mask & (ptrint) scan) / sizeof(DdNode)),
                            (void *) ((mask & (ptrint) cuddT(scan)) /
                                      sizeof(DdNode)));
                        if (retval == EOF) goto failure;
                        retval = fprintf(fp,
                                         "\"%p\" -> \"%p\" [style = dashed];\n",
                                         (void *) ((mask & (ptrint) scan)
                                                   / sizeof(DdNode)),
                                         (void *) ((mask & (ptrint)
                                                    cuddE(scan)) /
                                                   sizeof(DdNode)));
                        if (retval == EOF) goto failure;
                    }
                    scan = scan->next;
                }
Alan Mishchenko committed
766 767 768 769 770 771 772 773
            }
        }
    }

    /* Write constant labels. */
    nodelist = dd->constants.nodelist;
    slots = dd->constants.slots;
    for (j = 0; j < slots; j++) {
774 775
        scan = nodelist[j];
        while (scan != NULL) {
776
            if ( st__is_member(visited,(char *) scan)) {
777 778 779 780 781 782 783
                retval = fprintf(fp,"\"%p\" [label = \"%g\"];\n",
                                 (void *) ((mask & (ptrint) scan) /
                                           sizeof(DdNode)),
                                 cuddV(scan));
                if (retval == EOF) goto failure;
            }
            scan = scan->next;
Alan Mishchenko committed
784 785 786 787 788 789 790
        }
    }

    /* Write trailer and return. */
    retval = fprintf(fp,"}\n");
    if (retval == EOF) goto failure;

791
    st__free_table(visited);
Alan Mishchenko committed
792
    ABC_FREE(sorted);
Alan Mishchenko committed
793 794 795
    return(1);

failure:
Alan Mishchenko committed
796
    if (sorted != NULL) ABC_FREE(sorted);
797
    if (visited != NULL) st__free_table(visited);
Alan Mishchenko committed
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
    return(0);

} /* end of Cudd_zddDumpBlif */


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


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

  Synopsis [Prints a ZDD to the standard output. One line per node is
  printed.]

813
  Description [Prints a ZDD to the standard output. One line per node is
Alan Mishchenko committed
814 815 816 817 818 819 820 821 822 823 824 825 826
  printed. Returns 1 if successful; 0 otherwise.]

  SideEffects [None]

  SeeAlso     [Cudd_zddPrintDebug]

******************************************************************************/
int
cuddZddP(
  DdManager * zdd,
  DdNode * f)
{
    int retval;
827
    st__table *table = st__init_table( st__ptrcmp, st__ptrhash);
Alan Mishchenko committed
828 829 830 831

    if (table == NULL) return(0);

    retval = zp2(zdd, f, table);
832
    st__free_table(table);
Alan Mishchenko committed
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
    (void) fputc('\n', zdd->out);
    return(retval);

} /* end of cuddZddP */


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


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

  Synopsis [Performs the recursive step of cuddZddP.]

  Description [Performs the recursive step of cuddZddP. Returns 1 in
  case of success; 0 otherwise.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static int
zp2(
  DdManager * zdd,
  DdNode * f,
860
  st__table * t)
Alan Mishchenko committed
861
{
862 863 864 865
    DdNode      *n;
    int         T, E;
    DdNode      *base = DD_ONE(zdd);

Alan Mishchenko committed
866
    if (f == NULL)
867
        return(0);
Alan Mishchenko committed
868 869 870

    if (Cudd_IsConstant(f)) {
        (void)fprintf(zdd->out, "ID = %d\n", (f == base));
871
        return(1);
Alan Mishchenko committed
872
    }
873
    if ( st__is_member(t, (char *)f) == 1)
874
        return(1);
Alan Mishchenko committed
875

876
    if ( st__insert(t, (char *) f, NULL) == st__OUT_OF_MEM)
877
        return(0);
Alan Mishchenko committed
878 879

#if SIZEOF_VOID_P == 8
880 881
    (void) fprintf(zdd->out, "ID = 0x%lx\tindex = %u\tr = %u\t",
        (ptruint)f / (ptruint) sizeof(DdNode), f->index, f->ref);
Alan Mishchenko committed
882
#else
883 884
    (void) fprintf(zdd->out, "ID = 0x%x\tindex = %hu\tr = %hu\t",
        (ptruint)f / (ptruint) sizeof(DdNode), f->index, f->ref);
Alan Mishchenko committed
885 886 887 888 889
#endif

    n = cuddT(f);
    if (Cudd_IsConstant(n)) {
        (void) fprintf(zdd->out, "T = %d\t\t", (n == base));
890
        T = 1;
Alan Mishchenko committed
891 892
    } else {
#if SIZEOF_VOID_P == 8
893 894
        (void) fprintf(zdd->out, "T = 0x%lx\t", (ptruint) n /
                       (ptruint) sizeof(DdNode));
Alan Mishchenko committed
895
#else
896 897
        (void) fprintf(zdd->out, "T = 0x%x\t", (ptruint) n /
                       (ptruint) sizeof(DdNode));
Alan Mishchenko committed
898
#endif
899
        T = 0;
Alan Mishchenko committed
900 901 902 903 904
    }

    n = cuddE(f);
    if (Cudd_IsConstant(n)) {
        (void) fprintf(zdd->out, "E = %d\n", (n == base));
905
        E = 1;
Alan Mishchenko committed
906 907
    } else {
#if SIZEOF_VOID_P == 8
908 909
        (void) fprintf(zdd->out, "E = 0x%lx\n", (ptruint) n /
                      (ptruint) sizeof(DdNode));
Alan Mishchenko committed
910
#else
911 912
        (void) fprintf(zdd->out, "E = 0x%x\n", (ptruint) n /
                       (ptruint) sizeof(DdNode));
Alan Mishchenko committed
913
#endif
914
        E = 0;
Alan Mishchenko committed
915 916 917
    }

    if (E == 0)
918
        if (zp2(zdd, cuddE(f), t) == 0) return(0);
Alan Mishchenko committed
919
    if (T == 0)
920
        if (zp2(zdd, cuddT(f), t) == 0) return(0);
Alan Mishchenko committed
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
    return(1);

} /* end of zp2 */


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

  Synopsis    [Performs the recursive step of Cudd_zddPrintMinterm.]

  Description []

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static void
zdd_print_minterm_aux(
  DdManager * zdd /* manager */,
  DdNode * node /* current node */,
  int  level /* depth in the recursion */,
  int * list /* current recursion path */)
{
944 945 946
    DdNode      *Nv, *Nnv;
    int         i, v;
    DdNode      *base = DD_ONE(zdd);
Alan Mishchenko committed
947 948

    if (Cudd_IsConstant(node)) {
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
        if (node == base) {
            /* Check for missing variable. */
            if (level != zdd->sizeZ) {
                list[zdd->invpermZ[level]] = 0;
                zdd_print_minterm_aux(zdd, node, level + 1, list);
                return;
            }
            /* Terminal case: Print one cube based on the current recursion
            ** path.
            */
            for (i = 0; i < zdd->sizeZ; i++) {
                v = list[i];
                if (v == 0)
                    (void) fprintf(zdd->out,"0");
                else if (v == 1)
                    (void) fprintf(zdd->out,"1");
                else if (v == 3)
                    (void) fprintf(zdd->out,"@");       /* should never happen */
                else
                    (void) fprintf(zdd->out,"-");
            }
            (void) fprintf(zdd->out," 1\n");
        }
    } else {
Alan Mishchenko committed
973
        /* Check for missing variable. */
974 975 976 977
        if (level != cuddIZ(zdd,node->index)) {
            list[zdd->invpermZ[level]] = 0;
            zdd_print_minterm_aux(zdd, node, level + 1, list);
            return;
Alan Mishchenko committed
978
        }
979 980 981 982 983 984 985

        Nnv = cuddE(node);
        Nv = cuddT(node);
        if (Nv == Nnv) {
            list[node->index] = 2;
            zdd_print_minterm_aux(zdd, Nnv, level + 1, list);
            return;
Alan Mishchenko committed
986 987
        }

988 989 990
        list[node->index] = 1;
        zdd_print_minterm_aux(zdd, Nv, level + 1, list);
        list[node->index] = 0;
Alan Mishchenko committed
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
        zdd_print_minterm_aux(zdd, Nnv, level + 1, list);
    }
    return;

} /* end of zdd_print_minterm_aux */


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

  Synopsis    [Performs the recursive step of Cudd_zddPrintCover.]

  Description []

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static void
zddPrintCoverAux(
  DdManager * zdd /* manager */,
  DdNode * node /* current node */,
  int  level /* depth in the recursion */,
  int * list /* current recursion path */)
{
1016 1017 1018
    DdNode      *Nv, *Nnv;
    int         i, v;
    DdNode      *base = DD_ONE(zdd);
Alan Mishchenko committed
1019 1020

    if (Cudd_IsConstant(node)) {
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
        if (node == base) {
            /* Check for missing variable. */
            if (level != zdd->sizeZ) {
                list[zdd->invpermZ[level]] = 0;
                zddPrintCoverAux(zdd, node, level + 1, list);
                return;
            }
            /* Terminal case: Print one cube based on the current recursion
            ** path.
            */
            for (i = 0; i < zdd->sizeZ; i += 2) {
                v = list[i] * 4 + list[i+1];
                if (v == 0)
                    (void) putc('-',zdd->out);
                else if (v == 4)
                    (void) putc('1',zdd->out);
                else if (v == 1)
                    (void) putc('0',zdd->out);
                else
                    (void) putc('@',zdd->out); /* should never happen */
            }
            (void) fprintf(zdd->out," 1\n");
        }
    } else {
Alan Mishchenko committed
1045
        /* Check for missing variable. */
1046 1047 1048 1049
        if (level != cuddIZ(zdd,node->index)) {
            list[zdd->invpermZ[level]] = 0;
            zddPrintCoverAux(zdd, node, level + 1, list);
            return;
Alan Mishchenko committed
1050
        }
1051 1052 1053 1054 1055 1056 1057

        Nnv = cuddE(node);
        Nv = cuddT(node);
        if (Nv == Nnv) {
            list[node->index] = 2;
            zddPrintCoverAux(zdd, Nnv, level + 1, list);
            return;
Alan Mishchenko committed
1058 1059
        }

1060 1061 1062
        list[node->index] = 1;
        zddPrintCoverAux(zdd, Nv, level + 1, list);
        list[node->index] = 0;
Alan Mishchenko committed
1063 1064 1065 1066 1067
        zddPrintCoverAux(zdd, Nnv, level + 1, list);
    }
    return;

} /* end of zddPrintCoverAux */
1068 1069


1070 1071
ABC_NAMESPACE_IMPL_END