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.

318 lines
10 KiB

  1. /**CFile***********************************************************************
  2. FileName [cuddSign.c]
  3. PackageName [cudd]
  4. Synopsis [Computation of signatures.]
  5. Description [External procedures included in this module:
  6. <ul>
  7. <li> Cudd_CofMinterm();
  8. </ul>
  9. Static procedures included in this module:
  10. <ul>
  11. <li> ddCofMintermAux()
  12. </ul>
  13. ]
  14. Author [Fabio Somenzi]
  15. Copyright [Copyright (c) 1995-2012, Regents of the University of Colorado
  16. All rights reserved.
  17. Redistribution and use in source and binary forms, with or without
  18. modification, are permitted provided that the following conditions
  19. are met:
  20. Redistributions of source code must retain the above copyright
  21. notice, this list of conditions and the following disclaimer.
  22. Redistributions in binary form must reproduce the above copyright
  23. notice, this list of conditions and the following disclaimer in the
  24. documentation and/or other materials provided with the distribution.
  25. Neither the name of the University of Colorado nor the names of its
  26. contributors may be used to endorse or promote products derived from
  27. this software without specific prior written permission.
  28. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  29. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  30. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  31. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  32. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  33. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  34. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  35. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  36. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  37. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  38. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  39. POSSIBILITY OF SUCH DAMAGE.]
  40. ******************************************************************************/
  41. #include "util.h"
  42. #include "cuddInt.h"
  43. /*---------------------------------------------------------------------------*/
  44. /* Constant declarations */
  45. /*---------------------------------------------------------------------------*/
  46. /*---------------------------------------------------------------------------*/
  47. /* Stucture declarations */
  48. /*---------------------------------------------------------------------------*/
  49. /*---------------------------------------------------------------------------*/
  50. /* Type declarations */
  51. /*---------------------------------------------------------------------------*/
  52. /*---------------------------------------------------------------------------*/
  53. /* Variable declarations */
  54. /*---------------------------------------------------------------------------*/
  55. #ifndef lint
  56. static char rcsid[] DD_UNUSED = "$Id: cuddSign.c,v 1.24 2012/02/05 01:07:19 fabio Exp $";
  57. #endif
  58. static int size;
  59. #ifdef DD_STATS
  60. static int num_calls; /* should equal 2n-1 (n is the # of nodes) */
  61. static int table_mem;
  62. #endif
  63. /*---------------------------------------------------------------------------*/
  64. /* Macro declarations */
  65. /*---------------------------------------------------------------------------*/
  66. /**AutomaticStart*************************************************************/
  67. /*---------------------------------------------------------------------------*/
  68. /* Static function prototypes */
  69. /*---------------------------------------------------------------------------*/
  70. static double * ddCofMintermAux (DdManager *dd, DdNode *node, st_table *table);
  71. /**AutomaticEnd***************************************************************/
  72. /*---------------------------------------------------------------------------*/
  73. /* Definition of exported functions */
  74. /*---------------------------------------------------------------------------*/
  75. /**Function********************************************************************
  76. Synopsis [Computes the fraction of minterms in the on-set of all the
  77. positive cofactors of a BDD or ADD.]
  78. Description [Computes the fraction of minterms in the on-set of all
  79. the positive cofactors of DD. Returns the pointer to an array of
  80. doubles if successful; NULL otherwise. The array has as many
  81. positions as there are BDD variables in the manager plus one. The
  82. last position of the array contains the fraction of the minterms in
  83. the ON-set of the function represented by the BDD or ADD. The other
  84. positions of the array hold the variable signatures.]
  85. SideEffects [None]
  86. ******************************************************************************/
  87. double *
  88. Cudd_CofMinterm(
  89. DdManager * dd,
  90. DdNode * node)
  91. {
  92. st_table *table;
  93. double *values;
  94. double *result = NULL;
  95. int i, firstLevel;
  96. #ifdef DD_STATS
  97. unsigned long startTime;
  98. startTime = util_cpu_time();
  99. num_calls = 0;
  100. table_mem = sizeof(st_table);
  101. #endif
  102. table = st_init_table(st_ptrcmp, st_ptrhash);
  103. if (table == NULL) {
  104. (void) fprintf(dd->err,
  105. "out-of-memory, couldn't measure DD cofactors.\n");
  106. dd->errorCode = CUDD_MEMORY_OUT;
  107. return(NULL);
  108. }
  109. size = dd->size;
  110. values = ddCofMintermAux(dd, node, table);
  111. if (values != NULL) {
  112. result = ALLOC(double,size + 1);
  113. if (result != NULL) {
  114. #ifdef DD_STATS
  115. table_mem += (size + 1) * sizeof(double);
  116. #endif
  117. if (Cudd_IsConstant(node))
  118. firstLevel = 1;
  119. else
  120. firstLevel = cuddI(dd,Cudd_Regular(node)->index);
  121. for (i = 0; i < size; i++) {
  122. if (i >= cuddI(dd,Cudd_Regular(node)->index)) {
  123. result[dd->invperm[i]] = values[i - firstLevel];
  124. } else {
  125. result[dd->invperm[i]] = values[size - firstLevel];
  126. }
  127. }
  128. result[size] = values[size - firstLevel];
  129. } else {
  130. dd->errorCode = CUDD_MEMORY_OUT;
  131. }
  132. }
  133. #ifdef DD_STATS
  134. table_mem += table->num_bins * sizeof(st_table_entry *);
  135. #endif
  136. if (Cudd_Regular(node)->ref == 1) FREE(values);
  137. st_foreach(table, cuddStCountfree, NULL);
  138. st_free_table(table);
  139. #ifdef DD_STATS
  140. (void) fprintf(dd->out,"Number of calls: %d\tTable memory: %d bytes\n",
  141. num_calls, table_mem);
  142. (void) fprintf(dd->out,"Time to compute measures: %s\n",
  143. util_print_time(util_cpu_time() - startTime));
  144. #endif
  145. if (result == NULL) {
  146. (void) fprintf(dd->out,
  147. "out-of-memory, couldn't measure DD cofactors.\n");
  148. dd->errorCode = CUDD_MEMORY_OUT;
  149. }
  150. return(result);
  151. } /* end of Cudd_CofMinterm */
  152. /*---------------------------------------------------------------------------*/
  153. /* Definition of internal functions */
  154. /*---------------------------------------------------------------------------*/
  155. /*---------------------------------------------------------------------------*/
  156. /* Definition of static functions */
  157. /*---------------------------------------------------------------------------*/
  158. /**Function********************************************************************
  159. Synopsis [Recursive Step for Cudd_CofMinterm function.]
  160. Description [Traverses the DD node and computes the fraction of
  161. minterms in the on-set of all positive cofactors simultaneously.
  162. It allocates an array with two more entries than there are
  163. variables below the one labeling the node. One extra entry (the
  164. first in the array) is for the variable labeling the node. The other
  165. entry (the last one in the array) holds the fraction of minterms of
  166. the function rooted at node. Each other entry holds the value for
  167. one cofactor. The array is put in a symbol table, to avoid repeated
  168. computation, and its address is returned by the procedure, for use
  169. by the caller. Returns a pointer to the array of cofactor measures.]
  170. SideEffects [None]
  171. SeeAlso []
  172. ******************************************************************************/
  173. static double *
  174. ddCofMintermAux(
  175. DdManager * dd,
  176. DdNode * node,
  177. st_table * table)
  178. {
  179. DdNode *N; /* regular version of node */
  180. DdNode *Nv, *Nnv;
  181. double *values;
  182. double *valuesT, *valuesE;
  183. int i;
  184. int localSize, localSizeT, localSizeE;
  185. double vT, vE;
  186. statLine(dd);
  187. #ifdef DD_STATS
  188. num_calls++;
  189. #endif
  190. if (st_lookup(table, node, &values)) {
  191. return(values);
  192. }
  193. N = Cudd_Regular(node);
  194. if (cuddIsConstant(N)) {
  195. localSize = 1;
  196. } else {
  197. localSize = size - cuddI(dd,N->index) + 1;
  198. }
  199. values = ALLOC(double, localSize);
  200. if (values == NULL) {
  201. dd->errorCode = CUDD_MEMORY_OUT;
  202. return(NULL);
  203. }
  204. if (cuddIsConstant(N)) {
  205. if (node == DD_ZERO(dd) || node == Cudd_Not(DD_ONE(dd))) {
  206. values[0] = 0.0;
  207. } else {
  208. values[0] = 1.0;
  209. }
  210. } else {
  211. Nv = Cudd_NotCond(cuddT(N),N!=node);
  212. Nnv = Cudd_NotCond(cuddE(N),N!=node);
  213. valuesT = ddCofMintermAux(dd, Nv, table);
  214. if (valuesT == NULL) return(NULL);
  215. valuesE = ddCofMintermAux(dd, Nnv, table);
  216. if (valuesE == NULL) return(NULL);
  217. if (Cudd_IsConstant(Nv)) {
  218. localSizeT = 1;
  219. } else {
  220. localSizeT = size - cuddI(dd,Cudd_Regular(Nv)->index) + 1;
  221. }
  222. if (Cudd_IsConstant(Nnv)) {
  223. localSizeE = 1;
  224. } else {
  225. localSizeE = size - cuddI(dd,Cudd_Regular(Nnv)->index) + 1;
  226. }
  227. values[0] = valuesT[localSizeT - 1];
  228. for (i = 1; i < localSize; i++) {
  229. if (i >= cuddI(dd,Cudd_Regular(Nv)->index) - cuddI(dd,N->index)) {
  230. vT = valuesT[i - cuddI(dd,Cudd_Regular(Nv)->index) +
  231. cuddI(dd,N->index)];
  232. } else {
  233. vT = valuesT[localSizeT - 1];
  234. }
  235. if (i >= cuddI(dd,Cudd_Regular(Nnv)->index) - cuddI(dd,N->index)) {
  236. vE = valuesE[i - cuddI(dd,Cudd_Regular(Nnv)->index) +
  237. cuddI(dd,N->index)];
  238. } else {
  239. vE = valuesE[localSizeE - 1];
  240. }
  241. values[i] = (vT + vE) / 2.0;
  242. }
  243. if (Cudd_Regular(Nv)->ref == 1) FREE(valuesT);
  244. if (Cudd_Regular(Nnv)->ref == 1) FREE(valuesE);
  245. }
  246. if (N->ref > 1) {
  247. if (st_add_direct(table, (char *) node, (char *) values) == ST_OUT_OF_MEM) {
  248. FREE(values);
  249. return(NULL);
  250. }
  251. #ifdef DD_STATS
  252. table_mem += localSize * sizeof(double) + sizeof(st_table_entry);
  253. #endif
  254. }
  255. return(values);
  256. } /* end of ddCofMintermAux */