VIS

src/maig/maigUtil.c

Go to the documentation of this file.
00001 
00018 #include "maigInt.h"
00019 #include "mvfaig.h"
00020 #include "mvfaigInt.h"
00021 #include "ctlspInt.h"
00022 #include "ctlsp.h"
00023 #include "ntk.h"
00024 
00025 static char rcsid[] UNUSED = "$Id: maigUtil.c,v 1.20 2005/05/18 18:11:59 jinh Exp $";
00026 
00027 /*---------------------------------------------------------------------------*/
00028 /* Constant declarations                                                     */
00029 /*---------------------------------------------------------------------------*/
00030 
00031 /*---------------------------------------------------------------------------*/
00032 /* Stucture declarations                                                     */
00033 /*---------------------------------------------------------------------------*/
00034 
00035 
00036 /*---------------------------------------------------------------------------*/
00037 /* Type declarations                                                         */
00038 /*---------------------------------------------------------------------------*/
00039 
00040 
00041 /*---------------------------------------------------------------------------*/
00042 /* Variable declarations                                                     */
00043 /*---------------------------------------------------------------------------*/
00044 
00045 
00046 /*---------------------------------------------------------------------------*/
00047 /* Macro declarations                                                        */
00048 /*---------------------------------------------------------------------------*/
00049 
00050 
00053 /*---------------------------------------------------------------------------*/
00054 /* Static function prototypes                                                */
00055 /*---------------------------------------------------------------------------*/
00056 
00057 static int NoOfBitEncode(int n);
00058 static bAigEdge_t Case(mAig_Manager_t * mgr, array_t * encodeList, int index);
00059 static int mCommandmAigTest(void);
00060 
00064 /*---------------------------------------------------------------------------*/
00065 /* Definition of exported functions                                          */
00066 /*---------------------------------------------------------------------------*/
00067 
00077 void
00078 mAig_Init(void)
00079 {
00080   Cmd_CommandAdd("_mAig_test",
00081                  (int(*)(Hrc_Manager_t **, int, char **))  mCommandmAigTest,
00082                  0);
00083 }
00084 
00085 
00095 void
00096 mAig_End(void)
00097 {
00098 }
00099 
00109 array_t *
00110 mAig_ArrayDuplicate(
00111   array_t *mAigArray)
00112 {
00113   int      i;
00114   int      length = array_n(mAigArray);
00115   array_t *result = array_alloc(mAigEdge_t *, length);
00116   for (i = 0; i < length; i++) {
00117     mAigEdge_t *tempMAig = array_fetch(mAigEdge_t *, mAigArray, i);
00118     array_insert(mAigEdge_t *, result, i, tempMAig);
00119   }
00120 
00121   return (result);
00122 }
00123 
00133 mAig_Manager_t *
00134 mAig_initAig(void)
00135 {
00136   return bAig_initAig(5000); /* Initial number of nodes in the bAIG greaph*/
00137 }
00138 
00148 void
00149 mAig_quit(
00150   mAig_Manager_t *manager)
00151 {
00152  bAig_quit(manager);
00153 }
00154 
00168 mAigEdge_t
00169 mAig_EquC(
00170   mAig_Manager_t * mgr,
00171   mAigEdge_t     mVarId,
00172   int            constant)
00173 {
00174   array_t *mVarList    = mAigReadMulVarList(mgr);
00175   mAigMvar_t mVar      = array_fetch(mAigMvar_t, mVarList, mVarId);
00176   array_t  *encodeList = array_alloc(bAigEdge_t, 0);
00177   int i;
00178   bAigEdge_t bVarId;
00179   for(i=0; i< mVar.values; i++){
00180     if( i == constant){
00181       array_insert_last(bAigEdge_t, encodeList, mAig_One);      
00182     }
00183     else{
00184       array_insert_last(bAigEdge_t, encodeList, mAig_Zero);
00185     } /* if */
00186   } /* for */
00187   bVarId = Case(mgr, encodeList, mVar.bVars + mVar.encodeLength-1); /* Build the bAig grpah */
00188   array_free(encodeList);
00189   return bVarId;  
00190 }
00191 
00205 mAigEdge_t
00206 mAig_CreateVar(
00207   mAig_Manager_t * mgr,
00208   char           * name,
00209   int            value)
00210 {
00211   array_t *mVarList    = mAigReadMulVarList(mgr);
00212   array_t *bVarList    = mAigReadBinVarList(mgr);
00213 
00214   int         noBits      = NoOfBitEncode(value);
00215   int         i, startBvar, startMvar;
00216   /*
00217   char        bName[100];
00218   */
00219   char        *bName = ALLOC(char, strlen(name) + 10);
00220   mAigMvar_t  mVar;   
00221   mAigBvar_t  bVar; 
00222 
00223   assert(mVarList != NIL(array_t));
00224   assert(bVarList != NIL(array_t));
00225 
00226   startBvar = array_n(bVarList);
00227   startMvar = array_n(mVarList);
00228 
00229   /* Create Multi-valued variable */
00230   mVar.mVarId       = startMvar;
00231   mVar.bVars        = startBvar;
00232   mVar.values       = value;
00233   mVar.name         = name;
00234   mVar.encodeLength = noBits;
00235  
00236   array_insert_last(mAigMvar_t, mVarList, mVar);
00237 
00238   /* Create binary Variables of the Multi-valued variable */
00239   for (i=0; i<noBits; i++){
00240     sprintf(bName, "%s_%d", name, i);
00241     bVar.node = bAig_CreateVarNode(mgr,  util_strsav(bName));
00242 
00243     bVar.mVarId = mVar.mVarId; 
00244     array_insert_last(mAigBvar_t, bVarList, bVar);
00245   } /* for */
00246   return (mVar.mVarId);
00247 }
00248 
00262 mAigEdge_t
00263 mAig_CreateVarFromAig(
00264   mAig_Manager_t * mgr,
00265   char           * name,
00266   array_t        *mvfAig)
00267 {
00268   array_t *mVarList    = mAigReadMulVarList(mgr);
00269   array_t *bVarList    = mAigReadBinVarList(mgr);
00270 
00271   int noBits, value, index1;
00272   int startBvar, startMvar;
00273   int i, j, divider, orFlag;
00274   bAigEdge_t v, resultOn, resultOff;
00275   mAigMvar_t  mVar;   
00276   mAigBvar_t  bVar; 
00277 
00278   assert(mVarList != NIL(array_t));
00279   assert(bVarList != NIL(array_t));
00280 
00281   value = array_n(mvfAig);
00282   noBits = NoOfBitEncode(value);
00283   startBvar = array_n(bVarList);
00284   startMvar = array_n(mVarList);
00285 
00286   /* Create Multi-valued variable */
00287   mVar.mVarId       = startMvar;
00288   mVar.bVars        = startBvar;
00289   mVar.values       = value;
00290   mVar.name         = name;
00291   mVar.encodeLength = noBits;
00292  
00293   array_insert_last(mAigMvar_t, mVarList, mVar);
00294 
00295   for(i=0, index1=mVar.bVars; i<mVar.encodeLength; i++) {
00296 
00297     resultOn = bAig_Zero;
00298     resultOff = bAig_Zero;
00299 /*    divider = (int)pow(2.0, (double)(i)); */
00300     divider = (int)pow(2.0, (double)(mVar.encodeLength-i-1));
00301     orFlag = 1;
00302     for(j=0; j<value; j++) {
00303       if(j%divider == 0)orFlag = !orFlag;
00304       v = array_fetch(bAigEdge_t, mvfAig, j);
00305       if(orFlag){
00306         resultOn = bAig_Or(mgr, resultOn, v);
00307       }
00308       else {
00309         resultOff = bAig_Or(mgr, resultOff, v);
00310       }
00311     }
00312     bVar.node = resultOn;
00313     bVar.mVarId = mVar.mVarId; 
00314     array_insert_last(mAigBvar_t, bVarList, bVar);
00315     index1++;
00316   }
00317   return (mVar.mVarId);
00318 }
00319 
00320 /*---------------------------------------------------------------------------*/
00321 /* Definition of internal functions                                          */
00322 /*---------------------------------------------------------------------------*/
00323 
00335 array_t * 
00336 mAigReadBinVarList(
00337     mAig_Manager_t * manager)
00338 {
00339   return (manager->bVarList);
00340 }
00341 
00353 array_t *
00354 mAigReadMulVarList(
00355     mAig_Manager_t * manager)
00356 {
00357   return (manager->mVarList);
00358 }
00359 
00360 /*---------------------------------------------------------------------------*/
00361 /* Definition of static functions                                            */
00362 /*---------------------------------------------------------------------------*/
00363 
00364 
00376 static int
00377 NoOfBitEncode(
00378    int n)
00379 {
00380     int i = 0;
00381     int j = 1;
00382 
00383     if (n < 2) return 1; /* Takes care of mv.values <= 1 */
00384 
00385     while (j < n) {
00386         j = j * 2;
00387         i++;
00388     }
00389     return i;
00390 }
00391 
00392 
00407 static bAigEdge_t
00408 Case(
00409   mAig_Manager_t * mgr,
00410   array_t *       encodeList,
00411   int             index)
00412 {
00413   array_t       *bVarList    = mAigReadBinVarList(mgr);
00414   mAigBvar_t    bVar; 
00415   array_t       *newEncodeList;
00416   int           encodeLen = array_n(encodeList);  
00417   bAigEdge_t    andResult1, andResult2, orResult, result;
00418   bAigEdge_t    node1, node2;
00419   int i; 
00420   int count=0;
00421 
00422   if (encodeLen == 1){
00423     return array_fetch(bAigEdge_t, encodeList, 0);
00424   }
00425   bVar   = array_fetch(mAigBvar_t, bVarList, index); 
00426   newEncodeList =  array_alloc(bAigEdge_t, 0);
00427 
00428   for (i=0; i< (encodeLen/2); i++){
00429      node1 = array_fetch(bAigEdge_t, encodeList, count++);
00430      node2 = array_fetch(bAigEdge_t, encodeList, count++);
00431   
00432      /*  performs ITE operator */
00433      andResult1 = bAig_And(mgr, bVar.node,           node2);
00434      andResult2 = bAig_And(mgr, bAig_Not(bVar.node), node1);
00435      orResult   = bAig_Or (mgr, andResult1,          andResult2);
00436     
00437      array_insert_last(bAigEdge_t, newEncodeList, orResult);
00438     
00439   }
00440   if (encodeLen %2){ /* Odd */
00441      node1 = array_fetch(bAigEdge_t, encodeList, count);
00442      array_insert_last(bAigEdge_t, newEncodeList, node1);
00443   }
00444   result = Case(mgr, newEncodeList, index-1);
00445   array_free(newEncodeList);
00446   return result;
00447 }
00448 
00449 
00459 static int
00460 mCommandmAigTest(void)
00461 {
00462   array_t *mVarList;
00463   array_t *bVarList;
00464 
00465   MvfAig_Function_t * aa;
00466   MvfAig_Function_t * bb;
00467   MvfAig_Function_t * cc;
00468 
00469   mAigMvar_t mVar;   
00470   mAigBvar_t bVar; 
00471 
00472   int i;
00473 
00474   mAig_Manager_t *manager = mAig_initAig();
00475   mAigEdge_t      node1, node2, node3, node4, node5, node6;
00476 
00477   node1 = mAig_CreateVar(manager,"a",5); /* var a of 5 values */
00478   node2 = mAig_CreateVar(manager,"b",6); /* var b of 6 values */
00479   node3 = mAig_CreateVar(manager,"c",2); /* var c of 2 values */
00480 
00481   aa = MvfAig_FunctionCreateFromVariable(manager, node1);
00482   bb = MvfAig_FunctionCreateFromVariable(manager, node2);
00483   cc = MvfAig_FunctionCreateFromVariable(manager, node3);
00484 
00485   mVarList    = mAigReadMulVarList(manager);
00486   bVarList    = mAigReadBinVarList(manager);  
00487   fprintf(vis_stdout,"mValist: \n");
00488   for (i=0; i< array_n(mVarList); i++){
00489     mVar   = array_fetch(mAigMvar_t, mVarList, i);
00490     fprintf(vis_stdout,"mVarId=%d name=%s bVarId=%d values:%d\n",  mVar.mVarId, mVar.name, mVar.bVars, mVar.values);
00491   }
00492 
00493   fprintf(vis_stdout,"bValist: \n");
00494   for (i=0; i< array_n(bVarList); i++){
00495     bVar   = array_fetch(mAigBvar_t, bVarList, i);
00496     fprintf(vis_stdout,"mVar ID=%d bVar ID=%ld \n",  bVar.mVarId, bVar.node);
00497   }
00498 
00499   node4 = array_fetch(bAigEdge_t, aa, 3);
00500   node5 = array_fetch(bAigEdge_t, bb, 2);
00501   node6 = mAig_Or(manager, node4, node5);
00502 
00503   node4 = array_fetch(bAigEdge_t, cc, 0);
00504   node5 = bAig_Eq(manager, node6, node4);
00505 
00506   fprintf(vis_stdout,"%ld %ld %ld %ld %ld %ld\n",node1, node2, node3, node4, node5, node6);
00507   fprintf(vis_stdout,"Nodes Array:\n\n");
00508   for (i=0; i< manager->nodesArraySize ; i++)
00509     fprintf(vis_stdout,"Node #%d  value=%ld \n",i,manager->NodesArray[i]);
00510 
00511   fprintf(vis_stdout,"\n\nName List:\n");
00512   for (i=0; i< 20; i++)
00513     fprintf(vis_stdout,"index %d  Name %s \n",i, manager->nameList[i]);
00514 
00515   bAig_PrintDot(vis_stdout, manager);
00516 
00517   return 0;
00518 
00519 }