abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
abcNpn.c File Reference
#include "misc/extra/extra.h"
#include "misc/vec/vec.h"
#include "bool/kit/kit.h"
#include "bool/lucky/lucky.h"
#include "opt/dau/dau.h"

Go to the source code of this file.

Data Structures

struct  Abc_TtStore_t_
 

Typedefs

typedef
typedefABC_NAMESPACE_IMPL_START
struct Abc_TtStore_t_ 
Abc_TtStore_t
 DECLARATIONS ///. More...
 

Functions

Abc_TtStore_tAbc_TtStoreLoad (char *pFileName, int nVarNum)
 
void Abc_TtStoreFree (Abc_TtStore_t *p, int nVarNum)
 
void Abc_TtStoreWrite (char *pFileName, Abc_TtStore_t *p, int fBinary)
 
static int Abc_TruthHashKey (word *pFunc, int nWords, int nTableSize)
 FUNCTION DEFINITIONS ///. More...
 
static int Abc_TruthHashLookup (word **pFuncs, int iThis, int nWords, int *pTable, int *pNexts, int Key)
 
int Abc_TruthNpnCountUnique (Abc_TtStore_t *p)
 
int Abc_TruthCompare (word **p1, word **p2)
 
int Abc_TruthNpnCountUniqueSort (Abc_TtStore_t *p)
 
void Abc_TruthNpnPrint (char *pCanonPermInit, unsigned uCanonPhase, int nVars)
 
void Abc_TruthNpnPerform (Abc_TtStore_t *p, int NpnType, int fVerbose)
 
void Abc_TruthNpnTest (char *pFileName, int NpnType, int nVarNum, int fDumpRes, int fBinary, int fVerbose)
 
int Abc_NpnTest (char *pFileName, int NpnType, int nVarNum, int fDumpRes, int fBinary, int fVerbose)
 

Variables

int nWords = 0
 

Typedef Documentation

typedef typedefABC_NAMESPACE_IMPL_START struct Abc_TtStore_t_ Abc_TtStore_t

DECLARATIONS ///.

CFile****************************************************************

FileName [abcNpn.c]

SystemName [ABC: Logic synthesis and verification system.]

PackageName [Network and node package.]

Synopsis [Procedures for testing and comparing semi-canonical forms.]

Author [Alan Mishchenko]

Affiliation [UC Berkeley]

Date [Ver. 1.0. Started - June 20, 2005.]

Revision [

Id:
abcNpn.c,v 1.00 2005/06/20 00:00:00 alanmi Exp

]

Definition at line 42 of file abcNpn.c.

Function Documentation

int Abc_NpnTest ( char *  pFileName,
int  NpnType,
int  nVarNum,
int  fDumpRes,
int  fBinary,
int  fVerbose 
)

Function*************************************************************

Synopsis [Testbench for decomposition algorithms.]

Description []

SideEffects []

SeeAlso []

Definition at line 351 of file abcNpn.c.

352 {
353  if ( fVerbose )
354  printf( "Using truth tables from file \"%s\"...\n", pFileName );
355  if ( NpnType >= 0 && NpnType <= 6 )
356  Abc_TruthNpnTest( pFileName, NpnType, nVarNum, fDumpRes, fBinary, fVerbose );
357  else
358  printf( "Unknown canonical form value (%d).\n", NpnType );
359  fflush( stdout );
360  return 0;
361 }
void Abc_TruthNpnTest(char *pFileName, int NpnType, int nVarNum, int fDumpRes, int fBinary, int fVerbose)
Definition: abcNpn.c:309
int Abc_TruthCompare ( word **  p1,
word **  p2 
)

Definition at line 128 of file abcNpn.c.

128 { return memcmp(*p1, *p2, sizeof(word) * nWords); }
int nWords
Definition: abcNpn.c:127
unsigned __int64 word
DECLARATIONS ///.
Definition: kitPerm.c:36
int memcmp()
static int Abc_TruthHashKey ( word pFunc,
int  nWords,
int  nTableSize 
)
inlinestatic

FUNCTION DEFINITIONS ///.

Function*************************************************************

Synopsis [Counts the number of unique truth tables.]

Description []

SideEffects []

SeeAlso []

Definition at line 71 of file abcNpn.c.

72 {
73  static unsigned s_BigPrimes[7] = {12582917, 25165843, 50331653, 100663319, 201326611, 402653189, 805306457};
74  int w;
75  word Key = 0;
76  for ( w = 0; w < nWords; w++ )
77  Key += pFunc[w] * s_BigPrimes[w % 7];
78  return (int)(Key % nTableSize);
79 }
int nWords
Definition: abcNpn.c:127
unsigned __int64 word
DECLARATIONS ///.
Definition: kitPerm.c:36
static int Abc_TruthHashLookup ( word **  pFuncs,
int  iThis,
int  nWords,
int *  pTable,
int *  pNexts,
int  Key 
)
inlinestatic

Definition at line 81 of file abcNpn.c.

82 {
83  int iThat;
84  for ( iThat = pTable[Key]; iThat != -1; iThat = pNexts[iThat] )
85  if ( !memcmp( pFuncs[iThat], pFuncs[iThis], sizeof(word) * nWords ) )
86  return 1;
87  return 0;
88 }
int nWords
Definition: abcNpn.c:127
unsigned __int64 word
DECLARATIONS ///.
Definition: kitPerm.c:36
int memcmp()
int Abc_TruthNpnCountUnique ( Abc_TtStore_t p)

Definition at line 90 of file abcNpn.c.

91 {
92  // allocate hash table
93  int nTableSize = Abc_PrimeCudd(p->nFuncs);
94  int * pTable = ABC_FALLOC( int, nTableSize );
95  int * pNexts = ABC_FALLOC( int, nTableSize );
96  // hash functions
97  int i, k, Key;
98  for ( i = 0; i < p->nFuncs; i++ )
99  {
100  Key = Abc_TruthHashKey( p->pFuncs[i], p->nWords, nTableSize );
101  if ( Abc_TruthHashLookup( p->pFuncs, i, p->nWords, pTable, pNexts, Key ) ) // found equal
102  p->pFuncs[i] = NULL;
103  else // there is no equal (the first time this one occurs so far)
104  pNexts[i] = pTable[Key], pTable[Key] = i;
105  }
106  ABC_FREE( pTable );
107  ABC_FREE( pNexts );
108  // count the number of unqiue functions
109  assert( p->pFuncs[0] != NULL );
110  for ( i = k = 1; i < p->nFuncs; i++ )
111  if ( p->pFuncs[i] != NULL )
112  p->pFuncs[k++] = p->pFuncs[i];
113  return (p->nFuncs = k);
114 }
static int Abc_PrimeCudd(unsigned int p)
Definition: abc_global.h:383
word ** pFuncs
Definition: luckyInt.h:70
#define ABC_FREE(obj)
Definition: abc_global.h:232
#define assert(ex)
Definition: util_old.h:213
static int Abc_TruthHashKey(word *pFunc, int nWords, int nTableSize)
FUNCTION DEFINITIONS ///.
Definition: abcNpn.c:71
#define ABC_FALLOC(type, num)
Definition: abc_global.h:231
static int Abc_TruthHashLookup(word **pFuncs, int iThis, int nWords, int *pTable, int *pNexts, int Key)
Definition: abcNpn.c:81
int Abc_TruthNpnCountUniqueSort ( Abc_TtStore_t p)

Definition at line 129 of file abcNpn.c.

130 {
131  int i, k;
132  // sort them by value
133  nWords = p->nWords;
134  assert( nWords > 0 );
135  qsort( (void *)p->pFuncs, p->nFuncs, sizeof(word *), (int(*)(const void *,const void *))Abc_TruthCompare );
136  // count the number of unqiue functions
137  for ( i = k = 1; i < p->nFuncs; i++ )
138  if ( memcmp( p->pFuncs[i-1], p->pFuncs[i], sizeof(word) * nWords ) )
139  p->pFuncs[k++] = p->pFuncs[i];
140  return (p->nFuncs = k);
141 }
int Abc_TruthCompare(word **p1, word **p2)
Definition: abcNpn.c:128
word ** pFuncs
Definition: luckyInt.h:70
int nWords
Definition: abcNpn.c:127
unsigned __int64 word
DECLARATIONS ///.
Definition: kitPerm.c:36
int memcmp()
#define assert(ex)
Definition: util_old.h:213
void Abc_TruthNpnPerform ( Abc_TtStore_t p,
int  NpnType,
int  fVerbose 
)

Function*************************************************************

Synopsis [Apply decomposition to the truth table.]

Description [Returns the number of AIG nodes.]

SideEffects []

SeeAlso []

Definition at line 177 of file abcNpn.c.

178 {
179  unsigned pAux[2048];
180  word pAuxWord[1024], pAuxWord1[1024];
181  char pCanonPerm[16];
182  unsigned uCanonPhase=0;
183  abctime clk = Abc_Clock();
184  int i;
185 
186  char * pAlgoName = NULL;
187  if ( NpnType == 0 )
188  pAlgoName = "uniqifying ";
189  else if ( NpnType == 1 )
190  pAlgoName = "exact NPN ";
191  else if ( NpnType == 2 )
192  pAlgoName = "counting 1s ";
193  else if ( NpnType == 3 )
194  pAlgoName = "Jake's hybrid fast ";
195  else if ( NpnType == 4 )
196  pAlgoName = "Jake's hybrid good ";
197  else if ( NpnType == 5 )
198  pAlgoName = "new hybrid fast ";
199  else if ( NpnType == 6 )
200  pAlgoName = "new phase flipping ";
201 
202  assert( p->nVars <= 16 );
203  if ( pAlgoName )
204  printf( "Applying %-20s to %8d func%s of %2d vars... ",
205  pAlgoName, p->nFuncs, (p->nFuncs == 1 ? "":"s"), p->nVars );
206  if ( fVerbose )
207  printf( "\n" );
208 
209  if ( NpnType == 0 )
210  {
211  for ( i = 0; i < p->nFuncs; i++ )
212  {
213  if ( fVerbose )
214  printf( "%7d : ", i );
215  if ( fVerbose )
216  Extra_PrintHex( stdout, (unsigned *)p->pFuncs[i], p->nVars ), printf( "\n" );
217  }
218  }
219  else if ( NpnType == 1 )
220  {
221  permInfo* pi;
223  pi = setPermInfoPtr(p->nVars);
224  for ( i = 0; i < p->nFuncs; i++ )
225  {
226  if ( fVerbose )
227  printf( "%7d : ", i );
228  simpleMinimal(p->pFuncs[i], pAuxWord, pAuxWord1, pi, p->nVars);
229  if ( fVerbose )
230  Extra_PrintHex( stdout, (unsigned *)p->pFuncs[i], p->nVars ), Abc_TruthNpnPrint(pCanonPerm, uCanonPhase, p->nVars), printf( "\n" );
231  }
232  freePermInfoPtr(pi);
233  }
234  else if ( NpnType == 2 )
235  {
236  for ( i = 0; i < p->nFuncs; i++ )
237  {
238  if ( fVerbose )
239  printf( "%7d : ", i );
240  resetPCanonPermArray(pCanonPerm, p->nVars);
241  uCanonPhase = Kit_TruthSemiCanonicize( (unsigned *)p->pFuncs[i], pAux, p->nVars, pCanonPerm );
242  if ( fVerbose )
243  Extra_PrintHex( stdout, (unsigned *)p->pFuncs[i], p->nVars ), Abc_TruthNpnPrint(pCanonPerm, uCanonPhase, p->nVars), printf( "\n" );
244  }
245  }
246  else if ( NpnType == 3 )
247  {
248  for ( i = 0; i < p->nFuncs; i++ )
249  {
250  if ( fVerbose )
251  printf( "%7d : ", i );
252  resetPCanonPermArray(pCanonPerm, p->nVars);
253  uCanonPhase = luckyCanonicizer_final_fast( p->pFuncs[i], p->nVars, pCanonPerm );
254  if ( fVerbose )
255  Extra_PrintHex( stdout, (unsigned *)p->pFuncs[i], p->nVars ), Abc_TruthNpnPrint(pCanonPerm, uCanonPhase, p->nVars), printf( "\n" );
256  }
257  }
258  else if ( NpnType == 4 )
259  {
260  for ( i = 0; i < p->nFuncs; i++ )
261  {
262  if ( fVerbose )
263  printf( "%7d : ", i );
264  resetPCanonPermArray(pCanonPerm, p->nVars);
265  uCanonPhase = luckyCanonicizer_final_fast1( p->pFuncs[i], p->nVars, pCanonPerm );
266  if ( fVerbose )
267  Extra_PrintHex( stdout, (unsigned *)p->pFuncs[i], p->nVars ), Abc_TruthNpnPrint(pCanonPerm, uCanonPhase, p->nVars), printf( "\n" );
268  }
269  }
270  else if ( NpnType == 5 )
271  {
272  for ( i = 0; i < p->nFuncs; i++ )
273  {
274  if ( fVerbose )
275  printf( "%7d : ", i );
276  uCanonPhase = Abc_TtCanonicize( p->pFuncs[i], p->nVars, pCanonPerm );
277  if ( fVerbose )
278  Extra_PrintHex( stdout, (unsigned *)p->pFuncs[i], p->nVars ), Abc_TruthNpnPrint(pCanonPerm, uCanonPhase, p->nVars), printf( "\n" );
279  }
280  }
281  else if ( NpnType == 6 )
282  {
283  for ( i = 0; i < p->nFuncs; i++ )
284  {
285  if ( fVerbose )
286  printf( "%7d : ", i );
287  uCanonPhase = Abc_TtCanonicizePhase( p->pFuncs[i], p->nVars );
288  if ( fVerbose )
289  Extra_PrintHex( stdout, (unsigned *)p->pFuncs[i], p->nVars ), Abc_TruthNpnPrint(NULL, uCanonPhase, p->nVars), printf( "\n" );
290  }
291  }
292  else assert( 0 );
293  clk = Abc_Clock() - clk;
294  printf( "Classes =%9d ", Abc_TruthNpnCountUnique(p) );
295  Abc_PrintTime( 1, "Time", clk );
296 }
word ** pFuncs
Definition: luckyInt.h:70
static abctime Abc_Clock()
Definition: abc_global.h:279
void resetPCanonPermArray(char *x, int nVars)
Definition: luckyFast6.c:30
void Abc_TruthNpnPrint(char *pCanonPermInit, unsigned uCanonPhase, int nVars)
Definition: abcNpn.c:154
static void Abc_PrintTime(int level, const char *pStr, abctime time)
Definition: abc_global.h:367
unsigned Abc_TtCanonicizePhase(word *pTruth, int nVars)
Definition: dauCanon.c:999
void Extra_PrintHex(FILE *pFile, unsigned *pTruth, int nVars)
int Abc_TruthNpnCountUnique(Abc_TtStore_t *p)
Definition: abcNpn.c:90
unsigned __int64 word
DECLARATIONS ///.
Definition: kitPerm.c:36
permInfo * setPermInfoPtr(int var)
Definition: luckySimple.c:110
unsigned Kit_TruthSemiCanonicize(unsigned *pInOut, unsigned *pAux, int nVars, char *pCanonPerm)
Definition: kitTruth.c:1657
unsigned luckyCanonicizer_final_fast(word *pInOut, int nVars, char *pCanonPerm)
Definition: luckyFast16.c:818
void freePermInfoPtr(permInfo *x)
Definition: luckySimple.c:126
unsigned Abc_TtCanonicize(word *pTruth, int nVars, char *pCanonPerm)
FUNCTION DECLARATIONS ///.
Definition: dauCanon.c:895
#define assert(ex)
Definition: util_old.h:213
void simpleMinimal(word *x, word *pAux, word *minimal, permInfo *pi, int nVars)
Definition: luckySimple.c:151
ABC_INT64_T abctime
Definition: abc_global.h:278
unsigned luckyCanonicizer_final_fast1(word *pInOut, int nVars, char *pCanonPerm)
Definition: luckyFast16.c:843
Definition: lucky.h:23
void Abc_TruthNpnPrint ( char *  pCanonPermInit,
unsigned  uCanonPhase,
int  nVars 
)

Function*************************************************************

Synopsis [Prints out one NPN transform.]

Description []

SideEffects []

SeeAlso []

Definition at line 154 of file abcNpn.c.

155 {
156  char pCanonPerm[16]; int i;
157  assert( nVars <= 16 );
158  for ( i = 0; i < nVars; i++ )
159  pCanonPerm[i] = pCanonPermInit ? pCanonPermInit[i] : 'a' + i;
160  printf( " %c = ( ", Abc_InfoHasBit(&uCanonPhase, nVars) ? 'Z':'z' );
161  for ( i = 0; i < nVars; i++ )
162  printf( "%c%s", pCanonPerm[i] + ('A'-'a') * Abc_InfoHasBit(&uCanonPhase, pCanonPerm[i]-'a'), i == nVars-1 ? "":"," );
163  printf( " ) " );
164 }
static int Abc_InfoHasBit(unsigned *p, int i)
Definition: abc_global.h:258
#define assert(ex)
Definition: util_old.h:213
void Abc_TruthNpnTest ( char *  pFileName,
int  NpnType,
int  nVarNum,
int  fDumpRes,
int  fBinary,
int  fVerbose 
)

Function*************************************************************

Synopsis [Apply decomposition to truth tables.]

Description []

SideEffects []

SeeAlso []

Definition at line 309 of file abcNpn.c.

310 {
311  Abc_TtStore_t * p;
312  char * pFileNameOut;
313 
314  // read info from file
315  p = Abc_TtStoreLoad( pFileName, nVarNum );
316  if ( p == NULL )
317  return;
318 
319  // consider functions from the file
320  Abc_TruthNpnPerform( p, NpnType, fVerbose );
321 
322  // write the result
323  if ( fDumpRes )
324  {
325  if ( fBinary )
326  pFileNameOut = Extra_FileNameGenericAppend( pFileName, "_out.tt" );
327  else
328  pFileNameOut = Extra_FileNameGenericAppend( pFileName, "_out.txt" );
329  Abc_TtStoreWrite( pFileNameOut, p, fBinary );
330  if ( fVerbose )
331  printf( "The resulting functions are written into file \"%s\".\n", pFileNameOut );
332  }
333 
334  // delete data-structure
335  Abc_TtStoreFree( p, nVarNum );
336 // printf( "Finished computing canonical forms for functions from file \"%s\".\n", pFileName );
337 }
void Abc_TtStoreFree(Abc_TtStore_t *p, int nVarNum)
Definition: abcDec.c:175
static Llb_Mgr_t * p
Definition: llb3Image.c:950
Abc_TtStore_t * Abc_TtStoreLoad(char *pFileName, int nVarNum)
Definition: abcDec.c:395
void Abc_TtStoreWrite(char *pFileName, Abc_TtStore_t *p, int fBinary)
Definition: abcDec.c:358
void Abc_TruthNpnPerform(Abc_TtStore_t *p, int NpnType, int fVerbose)
Definition: abcNpn.c:177
char * Extra_FileNameGenericAppend(char *pBase, char *pSuffix)
void Abc_TtStoreFree ( Abc_TtStore_t p,
int  nVarNum 
)

Definition at line 175 of file abcDec.c.

176 {
177  if ( nVarNum >= 0 )
178  ABC_FREE( p->pFuncs[0] );
179  ABC_FREE( p->pFuncs );
180  ABC_FREE( p );
181 }
word ** pFuncs
Definition: luckyInt.h:70
#define ABC_FREE(obj)
Definition: abc_global.h:232
Abc_TtStore_t* Abc_TtStoreLoad ( char *  pFileName,
int  nVarNum 
)

Function*************************************************************

Synopsis [Read truth tables from input file and write them into output file.]

Description []

SideEffects []

SeeAlso []

Definition at line 395 of file abcDec.c.

396 {
397  Abc_TtStore_t * p;
398  if ( nVarNum < 0 )
399  {
400  int nVars, nTruths;
401  // figure out how many truth table and how many variables
402  Abc_TruthGetParams( pFileName, &nVars, &nTruths );
403  if ( nVars < 2 || nVars > 16 || nTruths == 0 )
404  return NULL;
405  // allocate data-structure
406  p = Abc_TruthStoreAlloc( nVars, nTruths );
407  // read info from file
408  Abc_TruthStoreRead( pFileName, p );
409  }
410  else
411  {
412  char * pBuffer;
413  int nFileSize = Abc_FileSize( pFileName );
414  int nBytes = (1 << (nVarNum-3));
415  int nTruths = nFileSize / nBytes;
416  if ( nFileSize == -1 )
417  return NULL;
418  assert( nVarNum >= 6 );
419  if ( nFileSize % nBytes != 0 )
420  Abc_Print( 0, "The file size (%d) is divided by the truth table size (%d) with remainder (%d).\n",
421  nFileSize, nBytes, nFileSize % nBytes );
422  // read file contents
423  pBuffer = Abc_FileRead( pFileName );
424  // allocate data-structure
425  p = Abc_TruthStoreAlloc2( nVarNum, nTruths, (word *)pBuffer );
426  }
427  return p;
428 }
static Llb_Mgr_t * p
Definition: llb3Image.c:950
Abc_TtStore_t * Abc_TruthStoreAlloc2(int nVars, int nFuncs, word *pBuffer)
Definition: abcDec.c:158
void Abc_TruthGetParams(char *pFileName, int *pnVars, int *pnTruths)
Definition: abcDec.c:262
Abc_TtStore_t * Abc_TruthStoreAlloc(int nVars, int nFuncs)
Definition: abcDec.c:140
unsigned __int64 word
DECLARATIONS ///.
Definition: kitPerm.c:36
static void Abc_Print(int level, const char *format,...)
Definition: abc_global.h:313
char * Abc_FileRead(char *pFileName)
Definition: abcDec.c:222
void Abc_TruthStoreRead(char *pFileName, Abc_TtStore_t *p)
Definition: abcDec.c:320
int Abc_FileSize(char *pFileName)
Definition: abcDec.c:194
#define assert(ex)
Definition: util_old.h:213
void Abc_TtStoreWrite ( char *  pFileName,
Abc_TtStore_t p,
int  fBinary 
)

Function*************************************************************

Synopsis [Write truth tables into file.]

Description []

SideEffects []

SeeAlso []

Definition at line 358 of file abcDec.c.

359 {
360  FILE * pFile;
361  char pBuffer[1000];
362  int i, nBytes = 8 * Abc_Truth6WordNum( p->nVars );
363  pFile = fopen( pFileName, "wb" );
364  if ( pFile == NULL )
365  {
366  printf( "Cannot open file \"%s\" for writing.\n", pFileName );
367  return;
368  }
369  for ( i = 0; i < p->nFuncs; i++ )
370  {
371  if ( fBinary )
372  fwrite( p->pFuncs[i], nBytes, 1, pFile );
373  else
374  {
375  Abc_TruthWriteHex( pFile, p->pFuncs[i], p->nVars ), fprintf( pFile, " " );
376  Dau_DsdDecompose( p->pFuncs[i], p->nVars, 0, (int)(p->nVars <= 10), pBuffer );
377  fprintf( pFile, "%s\n", pBuffer );
378  }
379  }
380  fclose( pFile );
381 }
word ** pFuncs
Definition: luckyInt.h:70
static int Abc_Truth6WordNum(int nVars)
Definition: abc_global.h:257
int Dau_DsdDecompose(word *pTruth, int nVarsInit, int fSplitPrime, int fWriteTruth, char *pRes)
Definition: dauDsd.c:1912
void Abc_TruthWriteHex(FILE *pFile, word *pTruth, int nVars)
Definition: abcDec.c:116

Variable Documentation

int nWords = 0

Function*************************************************************

Synopsis [Counts the number of unique truth tables.]

Description []

SideEffects []

SeeAlso []

Definition at line 127 of file abcNpn.c.