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.

216 lines
7.0 KiB

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