VIS

src/synth/synthCount.c

Go to the documentation of this file.
00001 
00017 #include "synthInt.h"
00018 
00019 static char rcsid[] UNUSED = "$Id: synthCount.c,v 1.14 1998/08/31 19:22:12 mooni Exp $";
00020 
00021 /*---------------------------------------------------------------------------*/
00022 /* Constant declarations                                                     */
00023 /*---------------------------------------------------------------------------*/
00024 
00025 
00026 /*---------------------------------------------------------------------------*/
00027 /* Type declarations                                                         */
00028 /*---------------------------------------------------------------------------*/
00029 
00030 
00031 /*---------------------------------------------------------------------------*/
00032 /* Structure declarations                                                    */
00033 /*---------------------------------------------------------------------------*/
00034 
00035 
00036 /*---------------------------------------------------------------------------*/
00037 /* Variable declarations                                                     */
00038 /*---------------------------------------------------------------------------*/
00039 
00042 /*---------------------------------------------------------------------------*/
00043 /* Static function prototypes                                                */
00044 /*---------------------------------------------------------------------------*/
00045 
00046 static int CountMultiLevelFactor(bdd_manager *dd, MlTree *top, MlTree *tree, int ref);
00047 static int GetLiteralCount(bdd_manager *dd, bdd_node *node, int nLiterals);
00048 
00052 /*---------------------------------------------------------------------------*/
00053 /* Definition of exported functions                                          */
00054 /*---------------------------------------------------------------------------*/
00055 
00056 
00057 /*---------------------------------------------------------------------------*/
00058 /* Definition of internal functions                                          */
00059 /*---------------------------------------------------------------------------*/
00060 
00074 int
00075 SynthCountMlLiteral(bdd_manager *dd,
00076                     MlTree *tree,
00077                     int ref)
00078 {
00079   bdd_node      *one = bdd_read_one(dd);
00080   bdd_node      *zero = bdd_read_zero(dd);
00081   int           count = 0;
00082 
00083   if (tree->top || tree->shared) {
00084     if (ref && tree->ref) {
00085       if (tree->node != zero && tree->node != one)
00086         count = 1;
00087       return(count);
00088     }
00089     count += CountMultiLevelFactor(dd, tree, tree, ref);
00090   }
00091 
00092   if (tree->leaf)
00093     return(count);
00094 
00095   if (tree->q_ref == 0)
00096     count += SynthCountMlLiteral(dd, tree->q, ref);
00097   if (tree->d_ref == 0)
00098     count += SynthCountMlLiteral(dd, tree->d, ref);
00099   if (tree->r_ref == 0)
00100     count += SynthCountMlLiteral(dd, tree->r, ref);
00101 
00102   return(count);
00103 }
00104 
00105 
00119 int
00120 SynthGetSupportMask(bdd_manager *dd,
00121                     bdd_node *node,
00122                     unsigned int *mask)
00123 {
00124   int           i, pos, count;
00125   int           word, size;
00126   int           bit, bit_mask;
00127   int           *support;
00128   int           zddSize = bdd_num_zdd_vars(dd);
00129 
00130   count = 0;
00131   word = sizeof(int) * 8;
00132   size = zddSize / word + 1;
00133 
00134   support = ALLOC(int, zddSize);
00135   (void)memset((void *)support, 0, sizeof(int) * zddSize);
00136   (void)memset((void *)mask, 0, size * sizeof(int));
00137   SynthZddSupportStep(bdd_regular(node), support);
00138   SynthZddClearFlag(bdd_regular(node));
00139   for (i = 0; i < zddSize; i++) {
00140     if (support[i]) {
00141       pos = i / word;
00142       bit = i % word;
00143       bit_mask = 01 << bit;
00144       mask[pos] |= bit_mask;
00145       count++;
00146     }
00147   }
00148   FREE(support);
00149   return(count);
00150 }
00151 
00152 
00164 int
00165 SynthGetSupportCount(bdd_manager *dd,
00166                      bdd_node *node)
00167 {
00168   int           i, count;
00169   int           *support;
00170   int           zddSize = bdd_num_zdd_vars(dd);
00171 
00172   count = 0;
00173   support = ALLOC(int, zddSize);
00174   (void)memset((void *)support, 0, sizeof(int) * zddSize);
00175   SynthZddSupportStep(bdd_regular(node), support);
00176   SynthZddClearFlag(bdd_regular(node));
00177   for (i = 0; i < zddSize; i++) {
00178     if (support[i])
00179       count++;
00180   }
00181   FREE(support);
00182   return(count);
00183 }
00184 
00185 
00201 void
00202 SynthZddSupportStep(bdd_node *f, int *support)
00203 {
00204   if (bdd_is_constant(f) || bdd_is_complement(bdd_read_next(f))) {
00205     return;
00206   }
00207 
00208   support[bdd_node_read_index(f)] = 1;
00209   SynthZddSupportStep(bdd_bdd_T(f),support);
00210   SynthZddSupportStep(bdd_regular(bdd_bdd_E(f)),support);
00211   /* Mark as visited. */
00212   bdd_set_next(f, bdd_not_bdd_node(bdd_read_next(f)));
00213   return;
00214 }
00215 
00216 
00228 void
00229 SynthZddClearFlag(bdd_node *f)
00230 {
00231   bdd_node *temp;
00232 
00233   temp = bdd_read_next(f);
00234 
00235   if (!bdd_is_complement(temp)) {
00236     return;
00237   }
00238   /* Clear visited flag. */
00239   bdd_set_next(f, bdd_regular(temp));
00240   if (bdd_is_constant(f)) {
00241     return;
00242   }
00243   SynthZddClearFlag(bdd_bdd_T(f));
00244   SynthZddClearFlag(bdd_regular(bdd_bdd_E(f)));
00245   return;
00246 }
00247 
00248 
00260 int
00261 SynthGetLiteralCount(bdd_manager *dd,
00262                      bdd_node *node)
00263 {
00264   return(GetLiteralCount(dd, node, 0));
00265 }
00266 
00267 
00268 /*---------------------------------------------------------------------------*/
00269 /* Definition of static functions                                            */
00270 /*---------------------------------------------------------------------------*/
00271 
00283 static int
00284 CountMultiLevelFactor(bdd_manager *dd,
00285                       MlTree *top,
00286                       MlTree *tree,
00287                       int ref)
00288 {
00289   bdd_node      *one = bdd_read_one(dd);
00290   bdd_node      *zero = bdd_read_zero(dd);
00291   bdd_node      *f;
00292   int           i, *support;
00293   int           count = 0;
00294   int           sizeZ = bdd_num_zdd_vars(dd);
00295 
00296   if (ref && tree != top && tree->shared)
00297     return(1);
00298 
00299   f = tree->node;
00300   if (f == one || f == zero)
00301     return(0);
00302 
00303   if (tree->leaf) {
00304     support = ALLOC(int, sizeZ);
00305     if (!support)
00306       return(0);
00307     (void)memset((void *)support, 0, sizeof(int) * sizeZ);
00308     SynthZddSupportStep(bdd_regular(tree->node), support);
00309     SynthZddClearFlag(bdd_regular(tree->node));
00310     for (i = 0; i < sizeZ; i++) {
00311       if (support[i])
00312         count++;
00313     }
00314     FREE(support);
00315     return(count);
00316   }
00317 
00318   count += CountMultiLevelFactor(dd, top, tree->q, ref);
00319   count += CountMultiLevelFactor(dd, top, tree->d, ref);
00320   count += CountMultiLevelFactor(dd, top, tree->r, ref);
00321 
00322   return(count);
00323 }
00324 
00325 
00339 static int
00340 GetLiteralCount(bdd_manager *dd,
00341                 bdd_node *node,
00342                 int nLiterals)
00343 {
00344   bdd_node      *one = bdd_read_one(dd);
00345   bdd_node      *zero = bdd_read_zero(dd);
00346   int           count = 0;
00347 
00348   if (node == zero)
00349     return(0);
00350   if (node == one)
00351     return(nLiterals);
00352 
00353   count = GetLiteralCount(dd, bdd_bdd_T(node), nLiterals + 1);
00354   count += GetLiteralCount(dd, bdd_bdd_E(node), nLiterals);
00355 
00356   return(count);
00357 }