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