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.

235 lines
7.9 KiB

  1. /**CFile***********************************************************************
  2. FileName [chkMterm.c]
  3. PackageName [ntr]
  4. Synopsis [Functions to check that the minterm counts have not
  5. changed.]
  6. Description [Functions to check that the minterm counts have not
  7. changed during reordering.<p>
  8. Internal procedures included in this module:
  9. <ul>
  10. <li> check_minterms()
  11. </ul>
  12. Static procedures included in this module:
  13. <ul>
  14. <li> stFree()
  15. </ul>]
  16. SeeAlso []
  17. Author [Fabio Somenzi]
  18. Copyright [Copyright (c) 1995-2012, Regents of the University of Colorado
  19. All rights reserved.
  20. Redistribution and use in source and binary forms, with or without
  21. modification, are permitted provided that the following conditions
  22. are met:
  23. Redistributions of source code must retain the above copyright
  24. notice, this list of conditions and the following disclaimer.
  25. Redistributions in binary form must reproduce the above copyright
  26. notice, this list of conditions and the following disclaimer in the
  27. documentation and/or other materials provided with the distribution.
  28. Neither the name of the University of Colorado nor the names of its
  29. contributors may be used to endorse or promote products derived from
  30. this software without specific prior written permission.
  31. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  32. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  33. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  34. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  35. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  36. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  37. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  40. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  41. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  42. POSSIBILITY OF SUCH DAMAGE.]
  43. ******************************************************************************/
  44. #include "ntr.h"
  45. /*---------------------------------------------------------------------------*/
  46. /* Constant declarations */
  47. /*---------------------------------------------------------------------------*/
  48. /*---------------------------------------------------------------------------*/
  49. /* Stucture declarations */
  50. /*---------------------------------------------------------------------------*/
  51. /*---------------------------------------------------------------------------*/
  52. /* Type declarations */
  53. /*---------------------------------------------------------------------------*/
  54. /*---------------------------------------------------------------------------*/
  55. /* Variable declarations */
  56. /*---------------------------------------------------------------------------*/
  57. #ifndef lint
  58. static char rcsid[] UTIL_UNUSED = "$Id: chkMterm.c,v 1.9 2012/02/05 01:53:01 fabio Exp fabio $";
  59. #endif
  60. /*---------------------------------------------------------------------------*/
  61. /* Macro declarations */
  62. /*---------------------------------------------------------------------------*/
  63. /**AutomaticStart*************************************************************/
  64. /*---------------------------------------------------------------------------*/
  65. /* Static function prototypes */
  66. /*---------------------------------------------------------------------------*/
  67. static enum st_retval stFree (char *key, char *value, char *arg);
  68. /**AutomaticEnd***************************************************************/
  69. /*---------------------------------------------------------------------------*/
  70. /* Definition of exported functions */
  71. /*---------------------------------------------------------------------------*/
  72. /*---------------------------------------------------------------------------*/
  73. /* Definition of internal functions */
  74. /*---------------------------------------------------------------------------*/
  75. /**Function********************************************************************
  76. Synopsis [Check that minterm counts have not changed.]
  77. Description [Counts the minterms in the global functions of the
  78. primary outputs of the network passed as argument.
  79. When it is calld with the second argument set to NULL, it allocates
  80. a symbol table and stores, for each output, the minterm count. If
  81. an output does not have a BDD, it stores a NULL pointer for it.
  82. If it is called with a non-null second argument, it assumes that
  83. the symbol table contains the minterm counts measured previously
  84. and it compares the new counts to the old ones. Finally, it frees
  85. the symbol table.
  86. check_minterms is designed so that it can be called twice: once before
  87. reordering, and once after reordering.
  88. Returns a pointer to the symbol table on the first invocation and NULL
  89. on the second invocation.]
  90. SideEffects [None]
  91. SeeAlso []
  92. ******************************************************************************/
  93. st_table *
  94. checkMinterms(
  95. BnetNetwork * net,
  96. DdManager * dd,
  97. st_table * previous)
  98. {
  99. BnetNode *po;
  100. int numPi;
  101. char *name;
  102. double *count, newcount, *oldcount;
  103. int flag,err,i;
  104. numPi = net->ninputs;
  105. if (previous == NULL) {
  106. previous = st_init_table(strcmp,st_strhash);
  107. if (previous == NULL) {
  108. (void) printf("checkMinterms out-of-memory\n");
  109. return(NULL);
  110. }
  111. for (i = 0; i < net->noutputs; i++) {
  112. if (!st_lookup(net->hash,net->outputs[i],&po)) {
  113. exit(2);
  114. }
  115. name = net->outputs[i];
  116. if (po->dd != NULL) {
  117. count = ALLOC(double,1);
  118. *count = Cudd_CountMinterm(dd,po->dd,numPi);
  119. err = st_insert(previous, name, (char *) count);
  120. } else {
  121. err = st_insert(previous, name, NULL);
  122. }
  123. if (err) {
  124. (void) printf("Duplicate input name (%s)\n",name);
  125. return(NULL);
  126. }
  127. }
  128. return(previous);
  129. } else {
  130. flag = 0;
  131. if (st_count(previous) != net->noutputs) {
  132. (void) printf("Number of outputs has changed from %d to %d\n",
  133. st_count(previous), net->noutputs);
  134. flag = 1;
  135. }
  136. for (i = 0; i < net->noutputs; i++) {
  137. if (!st_lookup(net->hash,net->outputs[i],&po)) {
  138. exit(2);
  139. }
  140. name = net->outputs[i];
  141. if (st_lookup(previous,name,&oldcount)) {
  142. if (po->dd != NULL) {
  143. newcount = Cudd_CountMinterm(dd,po->dd,numPi);
  144. if (newcount != *oldcount) {
  145. (void) printf("Number of minterms of %s has changed from %g to %g\n",name,*oldcount,newcount);
  146. flag = 1;
  147. }
  148. } else {
  149. if (oldcount != NULL) {
  150. (void) printf("Output %s lost its BDD!\n",name);
  151. flag = 1;
  152. }
  153. }
  154. } else {
  155. (void) printf("Output %s is new!\n",name);
  156. flag = 1;
  157. }
  158. }
  159. /*st_foreach(previous,(enum st_retval)stFree,NULL);*/
  160. st_foreach(previous,(ST_PFSR)stFree,NULL);
  161. st_free_table(previous);
  162. if (flag) {
  163. return((st_table *) 1);
  164. } else {
  165. return(NULL);
  166. }
  167. }
  168. } /* end of checkMinterms */
  169. /*---------------------------------------------------------------------------*/
  170. /* Definition of static functions */
  171. /*---------------------------------------------------------------------------*/
  172. /**Function********************************************************************
  173. Synopsis [Frees the data of the symbol table.]
  174. Description []
  175. SideEffects [None]
  176. SeeAlso []
  177. *****************************************************************************/
  178. static enum st_retval
  179. stFree(
  180. char *key,
  181. char *value,
  182. char *arg)
  183. {
  184. if (value != NULL) {
  185. FREE(value);
  186. }
  187. return(ST_CONTINUE);
  188. } /* end of stFree */