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

  FileName    [cuddApa.c]

  PackageName [cudd]

  Synopsis    [Arbitrary precision arithmetic functions.]

  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 30 31 32 33 34 35
                <ul>
                <li> Cudd_ApaNumberOfDigits()
                <li> Cudd_NewApaNumber()
                <li> Cudd_ApaCopy()
                <li> Cudd_ApaAdd()
                <li> Cudd_ApaSubtract()
                <li> Cudd_ApaShortDivision()
                <li> Cudd_ApaIntDivision()
                <li> Cudd_ApaShiftRight()
                <li> Cudd_ApaSetToLiteral()
                <li> Cudd_ApaPowerOfTwo()
                <li> Cudd_ApaCompare()
                <li> Cudd_ApaCompareRatios()
                <li> Cudd_ApaPrintHex()
                <li> Cudd_ApaPrintDecimal()
                <li> Cudd_ApaPrintExponential()
                <li> Cudd_ApaCountMinterm()
                <li> Cudd_ApaPrintMinterm()
                <li> Cudd_ApaPrintMintermExp()
                <li> Cudd_ApaPrintDensity()
                </ul>
        Static procedures included in this module:
                <ul>
                <li> cuddApaCountMintermAux()
                <li> cuddApaStCountfree()
                </ul>]
Alan Mishchenko committed
36 37 38

  Author      [Fabio Somenzi]

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 68 69
  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
70 71 72

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

73
#include "src/misc/util/util_hack.h"
Alan Mishchenko committed
74 75
#include "cuddInt.h"

76 77 78
ABC_NAMESPACE_IMPL_START


79

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

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

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

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

#ifndef lint
97
static char rcsid[] DD_UNUSED = "$Id: cuddApa.c,v 1.19 2009/03/08 01:27:50 fabio Exp $";
Alan Mishchenko committed
98 99
#endif

100
static  DdNode  *background, *zero;
Alan Mishchenko committed
101 102 103 104 105

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

106 107 108
#ifdef __cplusplus
extern "C" {
#endif
Alan Mishchenko committed
109 110 111 112 113 114 115

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

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

116 117
static DdApaNumber cuddApaCountMintermAux (DdNode * node, int digits, DdApaNumber max, DdApaNumber min, st_table * table);
static enum st_retval cuddApaStCountfree (char * key, char * value, char * arg);
Alan Mishchenko committed
118 119 120

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

121 122 123 124
#ifdef __cplusplus
} /* end of extern "C" */
#endif

Alan Mishchenko committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

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


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

  Synopsis    [Finds the number of digits for an arbitrary precision
  integer.]

  Description [Finds the number of digits for an arbitrary precision
  integer given the maximum number of binary digits.  The number of
  binary digits should be positive. Returns the number of digits if
  successful; 0 otherwise.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
int
Cudd_ApaNumberOfDigits(
  int  binaryDigits)
{
    int digits;

    digits = binaryDigits / DD_APA_BITS;
    if ((digits * DD_APA_BITS) != binaryDigits)
154
        digits++;
Alan Mishchenko committed
155 156 157
    return(digits);

} /* end of Cudd_ApaNumberOfDigits */
158

Alan Mishchenko committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

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

  Synopsis    [Allocates memory for an arbitrary precision integer.]

  Description [Allocates memory for an arbitrary precision
  integer. Returns a pointer to the allocated memory if successful;
  NULL otherwise.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
DdApaNumber
Cudd_NewApaNumber(
  int  digits)
{
Alan Mishchenko committed
177
    return(ABC_ALLOC(DdApaDigit, digits));
Alan Mishchenko committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

} /* end of Cudd_NewApaNumber */


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

  Synopsis    [Makes a copy of an arbitrary precision integer.]

  Description [Makes a copy of an arbitrary precision integer.]

  SideEffects [Changes parameter <code>dest</code>.]

  SeeAlso     []

******************************************************************************/
void
Cudd_ApaCopy(
  int  digits,
  DdApaNumber  source,
  DdApaNumber  dest)
{
    int i;

    for (i = 0; i < digits; i++) {
202
        dest[i] = source[i];
Alan Mishchenko committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    }

} /* end of Cudd_ApaCopy */


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

  Synopsis    [Adds two arbitrary precision integers.]

  Description [Adds two arbitrary precision integers.  Returns the
  carry out of the most significant digit.]

  SideEffects [The result of the sum is stored in parameter <code>sum</code>.]

  SeeAlso     []

******************************************************************************/
DdApaDigit
Cudd_ApaAdd(
  int  digits,
  DdApaNumber  a,
  DdApaNumber  b,
  DdApaNumber  sum)
{
    int i;
    DdApaDoubleDigit partial = 0;

    for (i = digits - 1; i >= 0; i--) {
231 232
        partial = a[i] + b[i] + DD_MSDIGIT(partial);
        sum[i] = (DdApaDigit) DD_LSDIGIT(partial);
Alan Mishchenko committed
233
    }
234
    return((DdApaDigit) DD_MSDIGIT(partial));
Alan Mishchenko committed
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

} /* end of Cudd_ApaAdd */


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

  Synopsis    [Subtracts two arbitrary precision integers.]

  Description [Subtracts two arbitrary precision integers.  Returns the
  borrow out of the most significant digit.]

  SideEffects [The result of the subtraction is stored in parameter
  <code>diff</code>.]

  SeeAlso     []

******************************************************************************/
DdApaDigit
Cudd_ApaSubtract(
  int  digits,
  DdApaNumber  a,
  DdApaNumber  b,
  DdApaNumber  diff)
{
    int i;
    DdApaDoubleDigit partial = DD_APA_BASE;

    for (i = digits - 1; i >= 0; i--) {
263 264
        partial = DD_MSDIGIT(partial) + DD_APA_MASK + a[i] - b[i];
        diff[i] = (DdApaDigit) DD_LSDIGIT(partial);
Alan Mishchenko committed
265
    }
266
    return((DdApaDigit) DD_MSDIGIT(partial) - 1);
Alan Mishchenko committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294

} /* end of Cudd_ApaSubtract */


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

  Synopsis    [Divides an arbitrary precision integer by a digit.]

  Description [Divides an arbitrary precision integer by a digit.]

  SideEffects [The quotient is returned in parameter <code>quotient</code>.]

  SeeAlso     []

******************************************************************************/
DdApaDigit
Cudd_ApaShortDivision(
  int  digits,
  DdApaNumber  dividend,
  DdApaDigit  divisor,
  DdApaNumber  quotient)
{
    int i;
    DdApaDigit remainder;
    DdApaDoubleDigit partial;

    remainder = 0;
    for (i = 0; i < digits; i++) {
295 296 297
        partial = remainder * DD_APA_BASE + dividend[i];
        quotient[i] = (DdApaDigit) (partial/(DdApaDoubleDigit)divisor);
        remainder = (DdApaDigit) (partial % divisor);
Alan Mishchenko committed
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
    }

    return(remainder);

} /* end of Cudd_ApaShortDivision */


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

  Synopsis    [Divides an arbitrary precision integer by an integer.]

  Description [Divides an arbitrary precision integer by a 32-bit
  unsigned integer. Returns the remainder of the division. This
  procedure relies on the assumption that the number of bits of a
  DdApaDigit plus the number of bits of an unsigned int is less the
  number of bits of the mantissa of a double. This guarantees that the
  product of a DdApaDigit and an unsigned int can be represented
  without loss of precision by a double. On machines where this
  assumption is not satisfied, this procedure will malfunction.]

  SideEffects [The quotient is returned in parameter <code>quotient</code>.]

  SeeAlso     [Cudd_ApaShortDivision]

******************************************************************************/
unsigned int
Cudd_ApaIntDivision(
  int  digits,
  DdApaNumber dividend,
  unsigned int divisor,
  DdApaNumber quotient)
{
    int i;
    double partial;
    unsigned int remainder = 0;
    double ddiv = (double) divisor;

    for (i = 0; i < digits; i++) {
336 337 338
        partial = (double) remainder * DD_APA_BASE + dividend[i];
        quotient[i] = (DdApaDigit) (partial / ddiv);
        remainder = (unsigned int) (partial - ((double)quotient[i] * ddiv));
Alan Mishchenko committed
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
    }

    return(remainder);

} /* end of Cudd_ApaIntDivision */


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

  Synopsis [Shifts right an arbitrary precision integer by one binary
  place.]

  Description [Shifts right an arbitrary precision integer by one
  binary place. The most significant binary digit of the result is
  taken from parameter <code>in</code>.]

  SideEffects [The result is returned in parameter <code>b</code>.]

  SeeAlso     []

******************************************************************************/
void
Cudd_ApaShiftRight(
  int  digits,
  DdApaDigit  in,
  DdApaNumber  a,
  DdApaNumber  b)
{
    int i;

    for (i = digits - 1; i > 0; i--) {
370
        b[i] = (a[i] >> 1) | ((a[i-1] & 1) << (DD_APA_BITS - 1));
Alan Mishchenko committed
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
    }
    b[0] = (a[0] >> 1) | (in << (DD_APA_BITS - 1));

} /* end of Cudd_ApaShiftRight */


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

  Synopsis    [Sets an arbitrary precision integer to a one-digit literal.]

  Description [Sets an arbitrary precision integer to a one-digit literal.]

  SideEffects [The result is returned in parameter <code>number</code>.]

  SeeAlso     []

******************************************************************************/
void
Cudd_ApaSetToLiteral(
  int  digits,
  DdApaNumber  number,
  DdApaDigit  literal)
{
    int i;

    for (i = 0; i < digits - 1; i++)
397
        number[i] = 0;
Alan Mishchenko committed
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
    number[digits - 1] = literal;

} /* end of Cudd_ApaSetToLiteral */


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

  Synopsis    [Sets an arbitrary precision integer to a power of two.]

  Description [Sets an arbitrary precision integer to a power of
  two. If the power of two is too large to be represented, the number
  is set to 0.]

  SideEffects [The result is returned in parameter <code>number</code>.]

  SeeAlso     []

******************************************************************************/
void
Cudd_ApaPowerOfTwo(
  int  digits,
  DdApaNumber  number,
  int  power)
{
    int i;
    int index;

    for (i = 0; i < digits; i++)
426
        number[i] = 0;
Alan Mishchenko committed
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
    i = digits - 1 - power / DD_APA_BITS;
    if (i < 0) return;
    index = power & (DD_APA_BITS - 1);
    number[i] = 1 << index;

} /* end of Cudd_ApaPowerOfTwo */


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

  Synopsis    [Compares two arbitrary precision integers.]

  Description [Compares two arbitrary precision integers. Returns 1 if
  the first number is larger; 0 if they are equal; -1 if the second
  number is larger.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
int
Cudd_ApaCompare(
  int digitsFirst,
  DdApaNumber  first,
  int digitsSecond,
  DdApaNumber  second)
{
    int i;
    int firstNZ, secondNZ;

    /* Find first non-zero in both numbers. */
    for (firstNZ = 0; firstNZ < digitsFirst; firstNZ++)
460
        if (first[firstNZ] != 0) break;
Alan Mishchenko committed
461
    for (secondNZ = 0; secondNZ < digitsSecond; secondNZ++)
462
        if (second[secondNZ] != 0) break;
Alan Mishchenko committed
463 464 465
    if (digitsFirst - firstNZ > digitsSecond - secondNZ) return(1);
    else if (digitsFirst - firstNZ < digitsSecond - secondNZ) return(-1);
    for (i = 0; i < digitsFirst - firstNZ; i++) {
466 467
        if (first[firstNZ + i] > second[secondNZ + i]) return(1);
        else if (first[firstNZ + i] < second[secondNZ + i]) return(-1);
Alan Mishchenko committed
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
    }
    return(0);

} /* end of Cudd_ApaCompare */


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

  Synopsis    [Compares the ratios of two arbitrary precision integers to two
  unsigned ints.]

  Description [Compares the ratios of two arbitrary precision integers
  to two unsigned ints. Returns 1 if the first number is larger; 0 if
  they are equal; -1 if the second number is larger.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
int
Cudd_ApaCompareRatios(
  int digitsFirst,
  DdApaNumber firstNum,
  unsigned int firstDen,
  int digitsSecond,
  DdApaNumber secondNum,
  unsigned int secondDen)
{
    int result;
    DdApaNumber first, second;
    unsigned int firstRem, secondRem;

    first = Cudd_NewApaNumber(digitsFirst);
    firstRem = Cudd_ApaIntDivision(digitsFirst,firstNum,firstDen,first);
    second = Cudd_NewApaNumber(digitsSecond);
    secondRem = Cudd_ApaIntDivision(digitsSecond,secondNum,secondDen,second);
    result = Cudd_ApaCompare(digitsFirst,first,digitsSecond,second);
506 507
    ABC_FREE(first);
    ABC_FREE(second);
Alan Mishchenko committed
508
    if (result == 0) {
509 510 511 512
        if ((double)firstRem/firstDen > (double)secondRem/secondDen)
            return(1);
        else if ((double)firstRem/firstDen < (double)secondRem/secondDen)
            return(-1);
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
    }
    return(result);

} /* end of Cudd_ApaCompareRatios */


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

  Synopsis    [Prints an arbitrary precision integer in hexadecimal format.]

  Description [Prints an arbitrary precision integer in hexadecimal format.
  Returns 1 if successful; 0 otherwise.]

  SideEffects [None]

  SeeAlso     [Cudd_ApaPrintDecimal Cudd_ApaPrintExponential]

******************************************************************************/
int
Cudd_ApaPrintHex(
  FILE * fp,
  int  digits,
  DdApaNumber  number)
{
    int i, result;

    for (i = 0; i < digits; i++) {
540 541 542
        result = fprintf(fp,DD_APA_HEXPRINT,number[i]);
        if (result == EOF)
            return(0);
Alan Mishchenko committed
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
    }
    return(1);

} /* end of Cudd_ApaPrintHex */


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

  Synopsis    [Prints an arbitrary precision integer in decimal format.]

  Description [Prints an arbitrary precision integer in decimal format.
  Returns 1 if successful; 0 otherwise.]

  SideEffects [None]

  SeeAlso     [Cudd_ApaPrintHex Cudd_ApaPrintExponential]

******************************************************************************/
int
Cudd_ApaPrintDecimal(
  FILE * fp,
  int  digits,
  DdApaNumber  number)
{
    int i, result;
    DdApaDigit remainder;
    DdApaNumber work;
    unsigned char *decimal;
    int leadingzero;
    int decimalDigits = (int) (digits * log10((double) DD_APA_BASE)) + 1;
573

Alan Mishchenko committed
574 575
    work = Cudd_NewApaNumber(digits);
    if (work == NULL)
576
        return(0);
Alan Mishchenko committed
577
    decimal = ABC_ALLOC(unsigned char, decimalDigits);
Alan Mishchenko committed
578
    if (decimal == NULL) {
579 580
        ABC_FREE(work);
        return(0);
Alan Mishchenko committed
581 582 583
    }
    Cudd_ApaCopy(digits,number,work);
    for (i = decimalDigits - 1; i >= 0; i--) {
584 585
        remainder = Cudd_ApaShortDivision(digits,work,(DdApaDigit) 10,work);
        decimal[i] = (unsigned char) remainder;
Alan Mishchenko committed
586
    }
Alan Mishchenko committed
587
    ABC_FREE(work);
Alan Mishchenko committed
588 589 590

    leadingzero = 1;
    for (i = 0; i < decimalDigits; i++) {
591 592 593 594 595 596 597
        leadingzero = leadingzero && (decimal[i] == 0);
        if ((!leadingzero) || (i == (decimalDigits - 1))) {
            result = fprintf(fp,"%1d",decimal[i]);
            if (result == EOF) {
                ABC_FREE(decimal);
                return(0);
            }
Alan Mishchenko committed
598 599
        }
    }
Alan Mishchenko committed
600
    ABC_FREE(decimal);
Alan Mishchenko committed
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
    return(1);

} /* end of Cudd_ApaPrintDecimal */


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

  Synopsis    [Prints an arbitrary precision integer in exponential format.]

  Description [Prints an arbitrary precision integer in exponential format.
  Returns 1 if successful; 0 otherwise.]

  SideEffects [None]

  SeeAlso     [Cudd_ApaPrintHex Cudd_ApaPrintDecimal]

******************************************************************************/
int
Cudd_ApaPrintExponential(
  FILE * fp,
  int  digits,
  DdApaNumber  number,
  int precision)
{
    int i, first, last, result;
    DdApaDigit remainder;
    DdApaNumber work;
    unsigned char *decimal;
    int decimalDigits = (int) (digits * log10((double) DD_APA_BASE)) + 1;
630

Alan Mishchenko committed
631 632
    work = Cudd_NewApaNumber(digits);
    if (work == NULL)
633
        return(0);
Alan Mishchenko committed
634
    decimal = ABC_ALLOC(unsigned char, decimalDigits);
Alan Mishchenko committed
635
    if (decimal == NULL) {
636 637
        ABC_FREE(work);
        return(0);
Alan Mishchenko committed
638 639 640 641
    }
    Cudd_ApaCopy(digits,number,work);
    first = decimalDigits - 1;
    for (i = decimalDigits - 1; i >= 0; i--) {
642 643 644
        remainder = Cudd_ApaShortDivision(digits,work,(DdApaDigit) 10,work);
        decimal[i] = (unsigned char) remainder;
        if (remainder != 0) first = i; /* keep track of MS non-zero */
Alan Mishchenko committed
645
    }
Alan Mishchenko committed
646
    ABC_FREE(work);
Alan Mishchenko committed
647 648 649
    last = ddMin(first + precision, decimalDigits);

    for (i = first; i < last; i++) {
650 651 652 653 654
        result = fprintf(fp,"%s%1d",i == first+1 ? "." : "", decimal[i]);
        if (result == EOF) {
            ABC_FREE(decimal);
            return(0);
        }
Alan Mishchenko committed
655
    }
Alan Mishchenko committed
656
    ABC_FREE(decimal);
Alan Mishchenko committed
657 658
    result = fprintf(fp,"e+%d",decimalDigits - first - 1);
    if (result == EOF) {
659
        return(0);
Alan Mishchenko committed
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
    }
    return(1);

} /* end of Cudd_ApaPrintExponential */


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

  Synopsis    [Counts the number of minterms of a DD.]

  Description [Counts the number of minterms of a DD. The function is
  assumed to depend on nvars variables. The minterm count is
  represented as an arbitrary precision unsigned integer, to allow for
  any number of variables CUDD supports.  Returns a pointer to the
  array representing the number of minterms of the function rooted at
  node if successful; NULL otherwise.]

  SideEffects [The number of digits of the result is returned in
  parameter <code>digits</code>.]

  SeeAlso     [Cudd_CountMinterm]

******************************************************************************/
DdApaNumber
Cudd_ApaCountMinterm(
  DdManager * manager,
  DdNode * node,
  int  nvars,
  int * digits)
{
690
    DdApaNumber max, min;
Alan Mishchenko committed
691
    st_table    *table;
692
    DdApaNumber i,count;
Alan Mishchenko committed
693 694 695 696 697 698 699

    background = manager->background;
    zero = Cudd_Not(manager->one);

    *digits = Cudd_ApaNumberOfDigits(nvars+1);
    max = Cudd_NewApaNumber(*digits);
    if (max == NULL) {
700
        return(NULL);
Alan Mishchenko committed
701 702 703 704
    }
    Cudd_ApaPowerOfTwo(*digits,max,nvars);
    min = Cudd_NewApaNumber(*digits);
    if (min == NULL) {
705 706
        ABC_FREE(max);
        return(NULL);
Alan Mishchenko committed
707 708
    }
    Cudd_ApaSetToLiteral(*digits,min,0);
709
    table = st_init_table(st_ptrcmp,st_ptrhash);
Alan Mishchenko committed
710
    if (table == NULL) {
711 712 713
        ABC_FREE(max);
        ABC_FREE(min);
        return(NULL);
Alan Mishchenko committed
714 715 716
    }
    i = cuddApaCountMintermAux(Cudd_Regular(node),*digits,max,min,table);
    if (i == NULL) {
717 718 719 720 721
        ABC_FREE(max);
        ABC_FREE(min);
        st_foreach(table, cuddApaStCountfree, NULL);
        st_free_table(table);
        return(NULL);
Alan Mishchenko committed
722 723 724
    }
    count = Cudd_NewApaNumber(*digits);
    if (count == NULL) {
725 726 727 728 729 730
        ABC_FREE(max);
        ABC_FREE(min);
        st_foreach(table, cuddApaStCountfree, NULL);
        st_free_table(table);
        if (Cudd_Regular(node)->ref == 1) ABC_FREE(i);
        return(NULL);
Alan Mishchenko committed
731 732
    }
    if (Cudd_IsComplement(node)) {
733
        (void) Cudd_ApaSubtract(*digits,max,i,count);
Alan Mishchenko committed
734
    } else {
735
        Cudd_ApaCopy(*digits,i,count);
Alan Mishchenko committed
736
    }
Alan Mishchenko committed
737 738
    ABC_FREE(max);
    ABC_FREE(min);
739
    st_foreach(table, cuddApaStCountfree, NULL);
Alan Mishchenko committed
740
    st_free_table(table);
Alan Mishchenko committed
741
    if (Cudd_Regular(node)->ref == 1) ABC_FREE(i);
Alan Mishchenko committed
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
    return(count);

} /* end of Cudd_ApaCountMinterm */


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

  Synopsis    [Prints the number of minterms of a BDD or ADD using
  arbitrary precision arithmetic.]

  Description [Prints the number of minterms of a BDD or ADD using
  arbitrary precision arithmetic. Returns 1 if successful; 0 otherwise.]

  SideEffects [None]

  SeeAlso     [Cudd_ApaPrintMintermExp]

******************************************************************************/
int
Cudd_ApaPrintMinterm(
  FILE * fp,
  DdManager * dd,
  DdNode * node,
  int  nvars)
{
    int digits;
    int result;
    DdApaNumber count;

    count = Cudd_ApaCountMinterm(dd,node,nvars,&digits);
    if (count == NULL)
773
        return(0);
Alan Mishchenko committed
774
    result = Cudd_ApaPrintDecimal(fp,digits,count);
Alan Mishchenko committed
775
    ABC_FREE(count);
Alan Mishchenko committed
776
    if (fprintf(fp,"\n") == EOF) {
777
        return(0);
Alan Mishchenko committed
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 804 805 806 807 808 809 810 811 812
    }
    return(result);

} /* end of Cudd_ApaPrintMinterm */


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

  Synopsis    [Prints the number of minterms of a BDD or ADD in exponential
  format using arbitrary precision arithmetic.]

  Description [Prints the number of minterms of a BDD or ADD in
  exponential format using arbitrary precision arithmetic. Parameter
  precision controls the number of signficant digits printed. Returns
  1 if successful; 0 otherwise.]

  SideEffects [None]

  SeeAlso     [Cudd_ApaPrintMinterm]

******************************************************************************/
int
Cudd_ApaPrintMintermExp(
  FILE * fp,
  DdManager * dd,
  DdNode * node,
  int  nvars,
  int precision)
{
    int digits;
    int result;
    DdApaNumber count;

    count = Cudd_ApaCountMinterm(dd,node,nvars,&digits);
    if (count == NULL)
813
        return(0);
Alan Mishchenko committed
814
    result = Cudd_ApaPrintExponential(fp,digits,count,precision);
Alan Mishchenko committed
815
    ABC_FREE(count);
Alan Mishchenko committed
816
    if (fprintf(fp,"\n") == EOF) {
817
        return(0);
Alan Mishchenko committed
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
    }
    return(result);

} /* end of Cudd_ApaPrintMintermExp */


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

  Synopsis    [Prints the density of a BDD or ADD using
  arbitrary precision arithmetic.]

  Description [Prints the density of a BDD or ADD using
  arbitrary precision arithmetic. Returns 1 if successful; 0 otherwise.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
int
Cudd_ApaPrintDensity(
  FILE * fp,
  DdManager * dd,
  DdNode * node,
  int  nvars)
{
    int digits;
    int result;
    DdApaNumber count,density;
    unsigned int size, remainder, fractional;

    count = Cudd_ApaCountMinterm(dd,node,nvars,&digits);
    if (count == NULL)
851
        return(0);
Alan Mishchenko committed
852 853 854 855
    size = Cudd_DagSize(node);
    density = Cudd_NewApaNumber(digits);
    remainder = Cudd_ApaIntDivision(digits,count,size,density);
    result = Cudd_ApaPrintDecimal(fp,digits,density);
Alan Mishchenko committed
856 857
    ABC_FREE(count);
    ABC_FREE(density);
Alan Mishchenko committed
858 859
    fractional = (unsigned int)((double)remainder / size * 1000000);
    if (fprintf(fp,".%u\n", fractional) == EOF) {
860
        return(0);
Alan Mishchenko committed
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
    }
    return(result);

} /* end of Cudd_ApaPrintDensity */


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


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


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

  Synopsis    [Performs the recursive step of Cudd_ApaCountMinterm.]

  Description [Performs the recursive step of Cudd_ApaCountMinterm.
  It is based on the following identity. Let |f| be the
  number of minterms of f. Then:
  <xmp>
    |f| = (|f0|+|f1|)/2
  </xmp>
  where f0 and f1 are the two cofactors of f.
  Uses the identity <code>|f'| = max - |f|</code>.
  The procedure expects the argument "node" to be a regular pointer, and
  guarantees this condition is met in the recursive calls.
  For efficiency, the result of a call is cached only if the node has
  a reference count greater than 1.
  Returns the number of minterms of the function rooted at node.]

  SideEffects [None]

******************************************************************************/
static DdApaNumber
cuddApaCountMintermAux(
  DdNode * node,
  int  digits,
  DdApaNumber  max,
  DdApaNumber  min,
  st_table * table)
{
    DdNode      *Nt, *Ne;
907 908
    DdApaNumber mint, mint1, mint2;
    DdApaDigit  carryout;
Alan Mishchenko committed
909 910

    if (cuddIsConstant(node)) {
911 912 913 914 915
        if (node == background || node == zero) {
            return(min);
        } else {
            return(max);
        }
Alan Mishchenko committed
916
    }
917 918
    if (node->ref > 1 && st_lookup(table, (const char *)node, (char **)&mint)) {
        return(mint);
Alan Mishchenko committed
919 920 921 922 923 924 925 926
    }

    Nt = cuddT(node); Ne = cuddE(node);

    mint1 = cuddApaCountMintermAux(Nt,  digits, max, min, table);
    if (mint1 == NULL) return(NULL);
    mint2 = cuddApaCountMintermAux(Cudd_Regular(Ne), digits, max, min, table);
    if (mint2 == NULL) {
927 928
        if (Nt->ref == 1) ABC_FREE(mint1);
        return(NULL);
Alan Mishchenko committed
929 930 931
    }
    mint = Cudd_NewApaNumber(digits);
    if (mint == NULL) {
932 933 934
        if (Nt->ref == 1) ABC_FREE(mint1);
        if (Cudd_Regular(Ne)->ref == 1) ABC_FREE(mint2);
        return(NULL);
Alan Mishchenko committed
935 936
    }
    if (Cudd_IsComplement(Ne)) {
937 938
        (void) Cudd_ApaSubtract(digits,max,mint2,mint);
        carryout = Cudd_ApaAdd(digits,mint1,mint,mint);
Alan Mishchenko committed
939
    } else {
940
        carryout = Cudd_ApaAdd(digits,mint1,mint2,mint);
Alan Mishchenko committed
941 942 943 944 945
    }
    Cudd_ApaShiftRight(digits,carryout,mint,mint);
    /* If the refernce count of a child is 1, its minterm count
    ** hasn't been stored in table.  Therefore, it must be explicitly
    ** freed here. */
Alan Mishchenko committed
946 947
    if (Nt->ref == 1) ABC_FREE(mint1);
    if (Cudd_Regular(Ne)->ref == 1) ABC_FREE(mint2);
948

Alan Mishchenko committed
949
    if (node->ref > 1) {
950 951 952 953
        if (st_insert(table, (char *)node, (char *)mint) == ST_OUT_OF_MEM) {
            ABC_FREE(mint);
            return(NULL);
        }
Alan Mishchenko committed
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
    }
    return(mint);

} /* end of cuddApaCountMintermAux */


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

  Synopsis [Frees the memory used to store the minterm counts recorded
  in the visited table.]

  Description [Frees the memory used to store the minterm counts
  recorded in the visited table. Returns ST_CONTINUE.]

  SideEffects [None]

******************************************************************************/
static enum st_retval
cuddApaStCountfree(
  char * key,
  char * value,
  char * arg)
{
977
    DdApaNumber d;
Alan Mishchenko committed
978 979

    d = (DdApaNumber) value;
Alan Mishchenko committed
980
    ABC_FREE(d);
Alan Mishchenko committed
981 982 983 984 985
    return(ST_CONTINUE);

} /* end of cuddApaStCountfree */


986 987
ABC_NAMESPACE_IMPL_END