You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

979 lines
27 KiB

  1. /**CFile***********************************************************************
  2. FileName [cuddApa.c]
  3. PackageName [cudd]
  4. Synopsis [Arbitrary precision arithmetic functions.]
  5. Description [External procedures included in this module:
  6. <ul>
  7. <li> Cudd_ApaNumberOfDigits()
  8. <li> Cudd_NewApaNumber()
  9. <li> Cudd_ApaCopy()
  10. <li> Cudd_ApaAdd()
  11. <li> Cudd_ApaSubtract()
  12. <li> Cudd_ApaShortDivision()
  13. <li> Cudd_ApaIntDivision()
  14. <li> Cudd_ApaShiftRight()
  15. <li> Cudd_ApaSetToLiteral()
  16. <li> Cudd_ApaPowerOfTwo()
  17. <li> Cudd_ApaCompare()
  18. <li> Cudd_ApaCompareRatios()
  19. <li> Cudd_ApaPrintHex()
  20. <li> Cudd_ApaPrintDecimal()
  21. <li> Cudd_ApaPrintExponential()
  22. <li> Cudd_ApaCountMinterm()
  23. <li> Cudd_ApaPrintMinterm()
  24. <li> Cudd_ApaPrintMintermExp()
  25. <li> Cudd_ApaPrintDensity()
  26. </ul>
  27. Static procedures included in this module:
  28. <ul>
  29. <li> cuddApaCountMintermAux()
  30. <li> cuddApaStCountfree()
  31. </ul>]
  32. Author [Fabio Somenzi]
  33. Copyright [Copyright (c) 1995-2012, Regents of the University of Colorado
  34. All rights reserved.
  35. Redistribution and use in source and binary forms, with or without
  36. modification, are permitted provided that the following conditions
  37. are met:
  38. Redistributions of source code must retain the above copyright
  39. notice, this list of conditions and the following disclaimer.
  40. Redistributions in binary form must reproduce the above copyright
  41. notice, this list of conditions and the following disclaimer in the
  42. documentation and/or other materials provided with the distribution.
  43. Neither the name of the University of Colorado nor the names of its
  44. contributors may be used to endorse or promote products derived from
  45. this software without specific prior written permission.
  46. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  47. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  48. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  49. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  50. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  51. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  52. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  53. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  54. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  55. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  56. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  57. POSSIBILITY OF SUCH DAMAGE.]
  58. ******************************************************************************/
  59. #include "util.h"
  60. #include "cuddInt.h"
  61. /*---------------------------------------------------------------------------*/
  62. /* Constant declarations */
  63. /*---------------------------------------------------------------------------*/
  64. /*---------------------------------------------------------------------------*/
  65. /* Stucture declarations */
  66. /*---------------------------------------------------------------------------*/
  67. /*---------------------------------------------------------------------------*/
  68. /* Type declarations */
  69. /*---------------------------------------------------------------------------*/
  70. /*---------------------------------------------------------------------------*/
  71. /* Variable declarations */
  72. /*---------------------------------------------------------------------------*/
  73. #ifndef lint
  74. static char rcsid[] DD_UNUSED = "$Id: cuddApa.c,v 1.20 2012/02/05 01:07:18 fabio Exp $";
  75. #endif
  76. static DdNode *background, *zero;
  77. /*---------------------------------------------------------------------------*/
  78. /* Macro declarations */
  79. /*---------------------------------------------------------------------------*/
  80. #ifdef __cplusplus
  81. extern "C" {
  82. #endif
  83. /**AutomaticStart*************************************************************/
  84. /*---------------------------------------------------------------------------*/
  85. /* Static function prototypes */
  86. /*---------------------------------------------------------------------------*/
  87. static DdApaNumber cuddApaCountMintermAux (DdNode * node, int digits, DdApaNumber max, DdApaNumber min, st_table * table);
  88. static enum st_retval cuddApaStCountfree (char * key, char * value, char * arg);
  89. /**AutomaticEnd***************************************************************/
  90. #ifdef __cplusplus
  91. } /* end of extern "C" */
  92. #endif
  93. /*---------------------------------------------------------------------------*/
  94. /* Definition of exported functions */
  95. /*---------------------------------------------------------------------------*/
  96. /**Function********************************************************************
  97. Synopsis [Finds the number of digits for an arbitrary precision
  98. integer.]
  99. Description [Finds the number of digits for an arbitrary precision
  100. integer given the maximum number of binary digits. The number of
  101. binary digits should be positive. Returns the number of digits if
  102. successful; 0 otherwise.]
  103. SideEffects [None]
  104. SeeAlso []
  105. ******************************************************************************/
  106. int
  107. Cudd_ApaNumberOfDigits(
  108. int binaryDigits)
  109. {
  110. int digits;
  111. digits = binaryDigits / DD_APA_BITS;
  112. if ((digits * DD_APA_BITS) != binaryDigits)
  113. digits++;
  114. return(digits);
  115. } /* end of Cudd_ApaNumberOfDigits */
  116. /**Function********************************************************************
  117. Synopsis [Allocates memory for an arbitrary precision integer.]
  118. Description [Allocates memory for an arbitrary precision
  119. integer. Returns a pointer to the allocated memory if successful;
  120. NULL otherwise.]
  121. SideEffects [None]
  122. SeeAlso []
  123. ******************************************************************************/
  124. DdApaNumber
  125. Cudd_NewApaNumber(
  126. int digits)
  127. {
  128. return(ALLOC(DdApaDigit, digits));
  129. } /* end of Cudd_NewApaNumber */
  130. /**Function********************************************************************
  131. Synopsis [Makes a copy of an arbitrary precision integer.]
  132. Description [Makes a copy of an arbitrary precision integer.]
  133. SideEffects [Changes parameter <code>dest</code>.]
  134. SeeAlso []
  135. ******************************************************************************/
  136. void
  137. Cudd_ApaCopy(
  138. int digits,
  139. DdApaNumber source,
  140. DdApaNumber dest)
  141. {
  142. int i;
  143. for (i = 0; i < digits; i++) {
  144. dest[i] = source[i];
  145. }
  146. } /* end of Cudd_ApaCopy */
  147. /**Function********************************************************************
  148. Synopsis [Adds two arbitrary precision integers.]
  149. Description [Adds two arbitrary precision integers. Returns the
  150. carry out of the most significant digit.]
  151. SideEffects [The result of the sum is stored in parameter <code>sum</code>.]
  152. SeeAlso []
  153. ******************************************************************************/
  154. DdApaDigit
  155. Cudd_ApaAdd(
  156. int digits,
  157. DdApaNumber a,
  158. DdApaNumber b,
  159. DdApaNumber sum)
  160. {
  161. int i;
  162. DdApaDoubleDigit partial = 0;
  163. for (i = digits - 1; i >= 0; i--) {
  164. partial = a[i] + b[i] + DD_MSDIGIT(partial);
  165. sum[i] = (DdApaDigit) DD_LSDIGIT(partial);
  166. }
  167. return((DdApaDigit) DD_MSDIGIT(partial));
  168. } /* end of Cudd_ApaAdd */
  169. /**Function********************************************************************
  170. Synopsis [Subtracts two arbitrary precision integers.]
  171. Description [Subtracts two arbitrary precision integers. Returns the
  172. borrow out of the most significant digit.]
  173. SideEffects [The result of the subtraction is stored in parameter
  174. <code>diff</code>.]
  175. SeeAlso []
  176. ******************************************************************************/
  177. DdApaDigit
  178. Cudd_ApaSubtract(
  179. int digits,
  180. DdApaNumber a,
  181. DdApaNumber b,
  182. DdApaNumber diff)
  183. {
  184. int i;
  185. DdApaDoubleDigit partial = DD_APA_BASE;
  186. for (i = digits - 1; i >= 0; i--) {
  187. partial = DD_MSDIGIT(partial) + DD_APA_MASK + a[i] - b[i];
  188. diff[i] = (DdApaDigit) DD_LSDIGIT(partial);
  189. }
  190. return((DdApaDigit) DD_MSDIGIT(partial) - 1);
  191. } /* end of Cudd_ApaSubtract */
  192. /**Function********************************************************************
  193. Synopsis [Divides an arbitrary precision integer by a digit.]
  194. Description [Divides an arbitrary precision integer by a digit.]
  195. SideEffects [The quotient is returned in parameter <code>quotient</code>.]
  196. SeeAlso []
  197. ******************************************************************************/
  198. DdApaDigit
  199. Cudd_ApaShortDivision(
  200. int digits,
  201. DdApaNumber dividend,
  202. DdApaDigit divisor,
  203. DdApaNumber quotient)
  204. {
  205. int i;
  206. DdApaDigit remainder;
  207. DdApaDoubleDigit partial;
  208. remainder = 0;
  209. for (i = 0; i < digits; i++) {
  210. partial = remainder * DD_APA_BASE + dividend[i];
  211. quotient[i] = (DdApaDigit) (partial/(DdApaDoubleDigit)divisor);
  212. remainder = (DdApaDigit) (partial % divisor);
  213. }
  214. return(remainder);
  215. } /* end of Cudd_ApaShortDivision */
  216. /**Function********************************************************************
  217. Synopsis [Divides an arbitrary precision integer by an integer.]
  218. Description [Divides an arbitrary precision integer by a 32-bit
  219. unsigned integer. Returns the remainder of the division. This
  220. procedure relies on the assumption that the number of bits of a
  221. DdApaDigit plus the number of bits of an unsigned int is less the
  222. number of bits of the mantissa of a double. This guarantees that the
  223. product of a DdApaDigit and an unsigned int can be represented
  224. without loss of precision by a double. On machines where this
  225. assumption is not satisfied, this procedure will malfunction.]
  226. SideEffects [The quotient is returned in parameter <code>quotient</code>.]
  227. SeeAlso [Cudd_ApaShortDivision]
  228. ******************************************************************************/
  229. unsigned int
  230. Cudd_ApaIntDivision(
  231. int digits,
  232. DdApaNumber dividend,
  233. unsigned int divisor,
  234. DdApaNumber quotient)
  235. {
  236. int i;
  237. double partial;
  238. unsigned int remainder = 0;
  239. double ddiv = (double) divisor;
  240. for (i = 0; i < digits; i++) {
  241. partial = (double) remainder * DD_APA_BASE + dividend[i];
  242. quotient[i] = (DdApaDigit) (partial / ddiv);
  243. remainder = (unsigned int) (partial - ((double)quotient[i] * ddiv));
  244. }
  245. return(remainder);
  246. } /* end of Cudd_ApaIntDivision */
  247. /**Function********************************************************************
  248. Synopsis [Shifts right an arbitrary precision integer by one binary
  249. place.]
  250. Description [Shifts right an arbitrary precision integer by one
  251. binary place. The most significant binary digit of the result is
  252. taken from parameter <code>in</code>.]
  253. SideEffects [The result is returned in parameter <code>b</code>.]
  254. SeeAlso []
  255. ******************************************************************************/
  256. void
  257. Cudd_ApaShiftRight(
  258. int digits,
  259. DdApaDigit in,
  260. DdApaNumber a,
  261. DdApaNumber b)
  262. {
  263. int i;
  264. for (i = digits - 1; i > 0; i--) {
  265. b[i] = (a[i] >> 1) | ((a[i-1] & 1) << (DD_APA_BITS - 1));
  266. }
  267. b[0] = (a[0] >> 1) | (in << (DD_APA_BITS - 1));
  268. } /* end of Cudd_ApaShiftRight */
  269. /**Function********************************************************************
  270. Synopsis [Sets an arbitrary precision integer to a one-digit literal.]
  271. Description [Sets an arbitrary precision integer to a one-digit literal.]
  272. SideEffects [The result is returned in parameter <code>number</code>.]
  273. SeeAlso []
  274. ******************************************************************************/
  275. void
  276. Cudd_ApaSetToLiteral(
  277. int digits,
  278. DdApaNumber number,
  279. DdApaDigit literal)
  280. {
  281. int i;
  282. for (i = 0; i < digits - 1; i++)
  283. number[i] = 0;
  284. number[digits - 1] = literal;
  285. } /* end of Cudd_ApaSetToLiteral */
  286. /**Function********************************************************************
  287. Synopsis [Sets an arbitrary precision integer to a power of two.]
  288. Description [Sets an arbitrary precision integer to a power of
  289. two. If the power of two is too large to be represented, the number
  290. is set to 0.]
  291. SideEffects [The result is returned in parameter <code>number</code>.]
  292. SeeAlso []
  293. ******************************************************************************/
  294. void
  295. Cudd_ApaPowerOfTwo(
  296. int digits,
  297. DdApaNumber number,
  298. int power)
  299. {
  300. int i;
  301. int index;
  302. for (i = 0; i < digits; i++)
  303. number[i] = 0;
  304. i = digits - 1 - power / DD_APA_BITS;
  305. if (i < 0) return;
  306. index = power & (DD_APA_BITS - 1);
  307. number[i] = 1 << index;
  308. } /* end of Cudd_ApaPowerOfTwo */
  309. /**Function********************************************************************
  310. Synopsis [Compares two arbitrary precision integers.]
  311. Description [Compares two arbitrary precision integers. Returns 1 if
  312. the first number is larger; 0 if they are equal; -1 if the second
  313. number is larger.]
  314. SideEffects [None]
  315. SeeAlso []
  316. ******************************************************************************/
  317. int
  318. Cudd_ApaCompare(
  319. int digitsFirst,
  320. DdApaNumber first,
  321. int digitsSecond,
  322. DdApaNumber second)
  323. {
  324. int i;
  325. int firstNZ, secondNZ;
  326. /* Find first non-zero in both numbers. */
  327. for (firstNZ = 0; firstNZ < digitsFirst; firstNZ++)
  328. if (first[firstNZ] != 0) break;
  329. for (secondNZ = 0; secondNZ < digitsSecond; secondNZ++)
  330. if (second[secondNZ] != 0) break;
  331. if (digitsFirst - firstNZ > digitsSecond - secondNZ) return(1);
  332. else if (digitsFirst - firstNZ < digitsSecond - secondNZ) return(-1);
  333. for (i = 0; i < digitsFirst - firstNZ; i++) {
  334. if (first[firstNZ + i] > second[secondNZ + i]) return(1);
  335. else if (first[firstNZ + i] < second[secondNZ + i]) return(-1);
  336. }
  337. return(0);
  338. } /* end of Cudd_ApaCompare */
  339. /**Function********************************************************************
  340. Synopsis [Compares the ratios of two arbitrary precision integers to two
  341. unsigned ints.]
  342. Description [Compares the ratios of two arbitrary precision integers
  343. to two unsigned ints. Returns 1 if the first number is larger; 0 if
  344. they are equal; -1 if the second number is larger.]
  345. SideEffects [None]
  346. SeeAlso []
  347. ******************************************************************************/
  348. int
  349. Cudd_ApaCompareRatios(
  350. int digitsFirst,
  351. DdApaNumber firstNum,
  352. unsigned int firstDen,
  353. int digitsSecond,
  354. DdApaNumber secondNum,
  355. unsigned int secondDen)
  356. {
  357. int result;
  358. DdApaNumber first, second;
  359. unsigned int firstRem, secondRem;
  360. first = Cudd_NewApaNumber(digitsFirst);
  361. firstRem = Cudd_ApaIntDivision(digitsFirst,firstNum,firstDen,first);
  362. second = Cudd_NewApaNumber(digitsSecond);
  363. secondRem = Cudd_ApaIntDivision(digitsSecond,secondNum,secondDen,second);
  364. result = Cudd_ApaCompare(digitsFirst,first,digitsSecond,second);
  365. FREE(first);
  366. FREE(second);
  367. if (result == 0) {
  368. if ((double)firstRem/firstDen > (double)secondRem/secondDen)
  369. return(1);
  370. else if ((double)firstRem/firstDen < (double)secondRem/secondDen)
  371. return(-1);
  372. }
  373. return(result);
  374. } /* end of Cudd_ApaCompareRatios */
  375. /**Function********************************************************************
  376. Synopsis [Prints an arbitrary precision integer in hexadecimal format.]
  377. Description [Prints an arbitrary precision integer in hexadecimal format.
  378. Returns 1 if successful; 0 otherwise.]
  379. SideEffects [None]
  380. SeeAlso [Cudd_ApaPrintDecimal Cudd_ApaPrintExponential]
  381. ******************************************************************************/
  382. int
  383. Cudd_ApaPrintHex(
  384. FILE * fp,
  385. int digits,
  386. DdApaNumber number)
  387. {
  388. int i, result;
  389. for (i = 0; i < digits; i++) {
  390. result = fprintf(fp,DD_APA_HEXPRINT,number[i]);
  391. if (result == EOF)
  392. return(0);
  393. }
  394. return(1);
  395. } /* end of Cudd_ApaPrintHex */
  396. /**Function********************************************************************
  397. Synopsis [Prints an arbitrary precision integer in decimal format.]
  398. Description [Prints an arbitrary precision integer in decimal format.
  399. Returns 1 if successful; 0 otherwise.]
  400. SideEffects [None]
  401. SeeAlso [Cudd_ApaPrintHex Cudd_ApaPrintExponential]
  402. ******************************************************************************/
  403. int
  404. Cudd_ApaPrintDecimal(
  405. FILE * fp,
  406. int digits,
  407. DdApaNumber number)
  408. {
  409. int i, result;
  410. DdApaDigit remainder;
  411. DdApaNumber work;
  412. unsigned char *decimal;
  413. int leadingzero;
  414. int decimalDigits = (int) (digits * log10((double) DD_APA_BASE)) + 1;
  415. work = Cudd_NewApaNumber(digits);
  416. if (work == NULL)
  417. return(0);
  418. decimal = ALLOC(unsigned char, decimalDigits);
  419. if (decimal == NULL) {
  420. FREE(work);
  421. return(0);
  422. }
  423. Cudd_ApaCopy(digits,number,work);
  424. for (i = decimalDigits - 1; i >= 0; i--) {
  425. remainder = Cudd_ApaShortDivision(digits,work,(DdApaDigit) 10,work);
  426. decimal[i] = (unsigned char) remainder;
  427. }
  428. FREE(work);
  429. leadingzero = 1;
  430. for (i = 0; i < decimalDigits; i++) {
  431. leadingzero = leadingzero && (decimal[i] == 0);
  432. if ((!leadingzero) || (i == (decimalDigits - 1))) {
  433. result = fprintf(fp,"%1d",decimal[i]);
  434. if (result == EOF) {
  435. FREE(decimal);
  436. return(0);
  437. }
  438. }
  439. }
  440. FREE(decimal);
  441. return(1);
  442. } /* end of Cudd_ApaPrintDecimal */
  443. /**Function********************************************************************
  444. Synopsis [Prints an arbitrary precision integer in exponential format.]
  445. Description [Prints an arbitrary precision integer in exponential format.
  446. Returns 1 if successful; 0 otherwise.]
  447. SideEffects [None]
  448. SeeAlso [Cudd_ApaPrintHex Cudd_ApaPrintDecimal]
  449. ******************************************************************************/
  450. int
  451. Cudd_ApaPrintExponential(
  452. FILE * fp,
  453. int digits,
  454. DdApaNumber number,
  455. int precision)
  456. {
  457. int i, first, last, result;
  458. DdApaDigit remainder;
  459. DdApaNumber work;
  460. unsigned char *decimal;
  461. int decimalDigits = (int) (digits * log10((double) DD_APA_BASE)) + 1;
  462. work = Cudd_NewApaNumber(digits);
  463. if (work == NULL)
  464. return(0);
  465. decimal = ALLOC(unsigned char, decimalDigits);
  466. if (decimal == NULL) {
  467. FREE(work);
  468. return(0);
  469. }
  470. Cudd_ApaCopy(digits,number,work);
  471. first = decimalDigits - 1;
  472. for (i = decimalDigits - 1; i >= 0; i--) {
  473. remainder = Cudd_ApaShortDivision(digits,work,(DdApaDigit) 10,work);
  474. decimal[i] = (unsigned char) remainder;
  475. if (remainder != 0) first = i; /* keep track of MS non-zero */
  476. }
  477. FREE(work);
  478. last = ddMin(first + precision, decimalDigits);
  479. for (i = first; i < last; i++) {
  480. result = fprintf(fp,"%s%1d",i == first+1 ? "." : "", decimal[i]);
  481. if (result == EOF) {
  482. FREE(decimal);
  483. return(0);
  484. }
  485. }
  486. FREE(decimal);
  487. result = fprintf(fp,"e+%d",decimalDigits - first - 1);
  488. if (result == EOF) {
  489. return(0);
  490. }
  491. return(1);
  492. } /* end of Cudd_ApaPrintExponential */
  493. /**Function********************************************************************
  494. Synopsis [Counts the number of minterms of a DD.]
  495. Description [Counts the number of minterms of a DD. The function is
  496. assumed to depend on nvars variables. The minterm count is
  497. represented as an arbitrary precision unsigned integer, to allow for
  498. any number of variables CUDD supports. Returns a pointer to the
  499. array representing the number of minterms of the function rooted at
  500. node if successful; NULL otherwise.]
  501. SideEffects [The number of digits of the result is returned in
  502. parameter <code>digits</code>.]
  503. SeeAlso [Cudd_CountMinterm]
  504. ******************************************************************************/
  505. DdApaNumber
  506. Cudd_ApaCountMinterm(
  507. DdManager * manager,
  508. DdNode * node,
  509. int nvars,
  510. int * digits)
  511. {
  512. DdApaNumber max, min;
  513. st_table *table;
  514. DdApaNumber i,count;
  515. background = manager->background;
  516. zero = Cudd_Not(manager->one);
  517. *digits = Cudd_ApaNumberOfDigits(nvars+1);
  518. max = Cudd_NewApaNumber(*digits);
  519. if (max == NULL) {
  520. return(NULL);
  521. }
  522. Cudd_ApaPowerOfTwo(*digits,max,nvars);
  523. min = Cudd_NewApaNumber(*digits);
  524. if (min == NULL) {
  525. FREE(max);
  526. return(NULL);
  527. }
  528. Cudd_ApaSetToLiteral(*digits,min,0);
  529. table = st_init_table(st_ptrcmp,st_ptrhash);
  530. if (table == NULL) {
  531. FREE(max);
  532. FREE(min);
  533. return(NULL);
  534. }
  535. i = cuddApaCountMintermAux(Cudd_Regular(node),*digits,max,min,table);
  536. if (i == NULL) {
  537. FREE(max);
  538. FREE(min);
  539. st_foreach(table, cuddApaStCountfree, NULL);
  540. st_free_table(table);
  541. return(NULL);
  542. }
  543. count = Cudd_NewApaNumber(*digits);
  544. if (count == NULL) {
  545. FREE(max);
  546. FREE(min);
  547. st_foreach(table, cuddApaStCountfree, NULL);
  548. st_free_table(table);
  549. if (Cudd_Regular(node)->ref == 1) FREE(i);
  550. return(NULL);
  551. }
  552. if (Cudd_IsComplement(node)) {
  553. (void) Cudd_ApaSubtract(*digits,max,i,count);
  554. } else {
  555. Cudd_ApaCopy(*digits,i,count);
  556. }
  557. FREE(max);
  558. FREE(min);
  559. st_foreach(table, cuddApaStCountfree, NULL);
  560. st_free_table(table);
  561. if (Cudd_Regular(node)->ref == 1) FREE(i);
  562. return(count);
  563. } /* end of Cudd_ApaCountMinterm */
  564. /**Function********************************************************************
  565. Synopsis [Prints the number of minterms of a BDD or ADD using
  566. arbitrary precision arithmetic.]
  567. Description [Prints the number of minterms of a BDD or ADD using
  568. arbitrary precision arithmetic. Returns 1 if successful; 0 otherwise.]
  569. SideEffects [None]
  570. SeeAlso [Cudd_ApaPrintMintermExp]
  571. ******************************************************************************/
  572. int
  573. Cudd_ApaPrintMinterm(
  574. FILE * fp,
  575. DdManager * dd,
  576. DdNode * node,
  577. int nvars)
  578. {
  579. int digits;
  580. int result;
  581. DdApaNumber count;
  582. count = Cudd_ApaCountMinterm(dd,node,nvars,&digits);
  583. if (count == NULL)
  584. return(0);
  585. result = Cudd_ApaPrintDecimal(fp,digits,count);
  586. FREE(count);
  587. if (fprintf(fp,"\n") == EOF) {
  588. return(0);
  589. }
  590. return(result);
  591. } /* end of Cudd_ApaPrintMinterm */
  592. /**Function********************************************************************
  593. Synopsis [Prints the number of minterms of a BDD or ADD in exponential
  594. format using arbitrary precision arithmetic.]
  595. Description [Prints the number of minterms of a BDD or ADD in
  596. exponential format using arbitrary precision arithmetic. Parameter
  597. precision controls the number of signficant digits printed. Returns
  598. 1 if successful; 0 otherwise.]
  599. SideEffects [None]
  600. SeeAlso [Cudd_ApaPrintMinterm]
  601. ******************************************************************************/
  602. int
  603. Cudd_ApaPrintMintermExp(
  604. FILE * fp,
  605. DdManager * dd,
  606. DdNode * node,
  607. int nvars,
  608. int precision)
  609. {
  610. int digits;
  611. int result;
  612. DdApaNumber count;
  613. count = Cudd_ApaCountMinterm(dd,node,nvars,&digits);
  614. if (count == NULL)
  615. return(0);
  616. result = Cudd_ApaPrintExponential(fp,digits,count,precision);
  617. FREE(count);
  618. if (fprintf(fp,"\n") == EOF) {
  619. return(0);
  620. }
  621. return(result);
  622. } /* end of Cudd_ApaPrintMintermExp */
  623. /**Function********************************************************************
  624. Synopsis [Prints the density of a BDD or ADD using
  625. arbitrary precision arithmetic.]
  626. Description [Prints the density of a BDD or ADD using
  627. arbitrary precision arithmetic. Returns 1 if successful; 0 otherwise.]
  628. SideEffects [None]
  629. SeeAlso []
  630. ******************************************************************************/
  631. int
  632. Cudd_ApaPrintDensity(
  633. FILE * fp,
  634. DdManager * dd,
  635. DdNode * node,
  636. int nvars)
  637. {
  638. int digits;
  639. int result;
  640. DdApaNumber count,density;
  641. unsigned int size, remainder, fractional;
  642. count = Cudd_ApaCountMinterm(dd,node,nvars,&digits);
  643. if (count == NULL)
  644. return(0);
  645. size = Cudd_DagSize(node);
  646. density = Cudd_NewApaNumber(digits);
  647. remainder = Cudd_ApaIntDivision(digits,count,size,density);
  648. result = Cudd_ApaPrintDecimal(fp,digits,density);
  649. FREE(count);
  650. FREE(density);
  651. fractional = (unsigned int)((double)remainder / size * 1000000);
  652. if (fprintf(fp,".%u\n", fractional) == EOF) {
  653. return(0);
  654. }
  655. return(result);
  656. } /* end of Cudd_ApaPrintDensity */
  657. /*---------------------------------------------------------------------------*/
  658. /* Definition of internal functions */
  659. /*---------------------------------------------------------------------------*/
  660. /*---------------------------------------------------------------------------*/
  661. /* Definition of static functions */
  662. /*---------------------------------------------------------------------------*/
  663. /**Function********************************************************************
  664. Synopsis [Performs the recursive step of Cudd_ApaCountMinterm.]
  665. Description [Performs the recursive step of Cudd_ApaCountMinterm.
  666. It is based on the following identity. Let |f| be the
  667. number of minterms of f. Then:
  668. <xmp>
  669. |f| = (|f0|+|f1|)/2
  670. </xmp>
  671. where f0 and f1 are the two cofactors of f.
  672. Uses the identity <code>|f'| = max - |f|</code>.
  673. The procedure expects the argument "node" to be a regular pointer, and
  674. guarantees this condition is met in the recursive calls.
  675. For efficiency, the result of a call is cached only if the node has
  676. a reference count greater than 1.
  677. Returns the number of minterms of the function rooted at node.]
  678. SideEffects [None]
  679. ******************************************************************************/
  680. static DdApaNumber
  681. cuddApaCountMintermAux(
  682. DdNode * node,
  683. int digits,
  684. DdApaNumber max,
  685. DdApaNumber min,
  686. st_table * table)
  687. {
  688. DdNode *Nt, *Ne;
  689. DdApaNumber mint, mint1, mint2;
  690. DdApaDigit carryout;
  691. if (cuddIsConstant(node)) {
  692. if (node == background || node == zero) {
  693. return(min);
  694. } else {
  695. return(max);
  696. }
  697. }
  698. if (node->ref > 1 && st_lookup(table, node, &mint)) {
  699. return(mint);
  700. }
  701. Nt = cuddT(node); Ne = cuddE(node);
  702. mint1 = cuddApaCountMintermAux(Nt, digits, max, min, table);
  703. if (mint1 == NULL) return(NULL);
  704. mint2 = cuddApaCountMintermAux(Cudd_Regular(Ne), digits, max, min, table);
  705. if (mint2 == NULL) {
  706. if (Nt->ref == 1) FREE(mint1);
  707. return(NULL);
  708. }
  709. mint = Cudd_NewApaNumber(digits);
  710. if (mint == NULL) {
  711. if (Nt->ref == 1) FREE(mint1);
  712. if (Cudd_Regular(Ne)->ref == 1) FREE(mint2);
  713. return(NULL);
  714. }
  715. if (Cudd_IsComplement(Ne)) {
  716. (void) Cudd_ApaSubtract(digits,max,mint2,mint);
  717. carryout = Cudd_ApaAdd(digits,mint1,mint,mint);
  718. } else {
  719. carryout = Cudd_ApaAdd(digits,mint1,mint2,mint);
  720. }
  721. Cudd_ApaShiftRight(digits,carryout,mint,mint);
  722. /* If the refernce count of a child is 1, its minterm count
  723. ** hasn't been stored in table. Therefore, it must be explicitly
  724. ** freed here. */
  725. if (Nt->ref == 1) FREE(mint1);
  726. if (Cudd_Regular(Ne)->ref == 1) FREE(mint2);
  727. if (node->ref > 1) {
  728. if (st_insert(table, (char *)node, (char *)mint) == ST_OUT_OF_MEM) {
  729. FREE(mint);
  730. return(NULL);
  731. }
  732. }
  733. return(mint);
  734. } /* end of cuddApaCountMintermAux */
  735. /**Function********************************************************************
  736. Synopsis [Frees the memory used to store the minterm counts recorded
  737. in the visited table.]
  738. Description [Frees the memory used to store the minterm counts
  739. recorded in the visited table. Returns ST_CONTINUE.]
  740. SideEffects [None]
  741. ******************************************************************************/
  742. static enum st_retval
  743. cuddApaStCountfree(
  744. char * key,
  745. char * value,
  746. char * arg)
  747. {
  748. DdApaNumber d;
  749. d = (DdApaNumber) value;
  750. FREE(d);
  751. return(ST_CONTINUE);
  752. } /* end of cuddApaStCountfree */