src/misc/extra/extraUtilCanon.c File Reference

#include "extra.h"
Include dependency graph for extraUtilCanon.c:

Go to the source code of this file.

Functions

static int Extra_TruthCanonN_rec (int nVars, unsigned char *pt, unsigned **pptRes, char **ppfRes, int Flag)
int Extra_TruthCanonFastN (int nVarsMax, int nVarsReal, unsigned *pt, unsigned **pptRes, char **ppfRes)
void Map_Var3Print ()
void Map_Var3Test ()
void Map_Var4Test ()

Variables

static unsigned s_Truths3 [256]
static char s_Phases3 [256][9]

Function Documentation

int Extra_TruthCanonFastN ( int  nVarsMax,
int  nVarsReal,
unsigned *  pt,
unsigned **  pptRes,
char **  ppfRes 
)

AutomaticEnd Function********************************************************************

Synopsis [Computes the N-canonical form of the Boolean function up to 6 inputs.]

Description [The N-canonical form is defined as the truth table with the minimum integer value. This function exhaustively enumerates through the complete set of 2^N phase assignments. Returns pointers to the static storage to the truth table and phases. This data should be used before the function is called again.]

SideEffects []

SeeAlso []

Definition at line 76 of file extraUtilCanon.c.

00077 {
00078     static unsigned uTruthStore6[2];
00079     int RetValue;
00080     assert( nVarsMax <= 6 );
00081     assert( nVarsReal <= nVarsMax );
00082     RetValue = Extra_TruthCanonN_rec( nVarsReal <= 3? 3: nVarsReal, (unsigned char *)pt, pptRes, ppfRes, 0 );
00083     if ( nVarsMax == 6 && nVarsReal < nVarsMax )
00084     {
00085         uTruthStore6[0] = **pptRes;
00086         uTruthStore6[1] = **pptRes;
00087         *pptRes = uTruthStore6;
00088     }
00089     return RetValue;
00090 }

int Extra_TruthCanonN_rec ( int  nVars,
unsigned char *  pt,
unsigned **  pptRes,
char **  ppfRes,
int  Flag 
) [static]

AutomaticStart

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

Synopsis [Recursive implementation of the above.]

Description []

SideEffects [This procedure has a bug, which shows on Solaris. Most likely has something to do with the casts, i.g *((unsigned *)pt0)]

SeeAlso []

Definition at line 108 of file extraUtilCanon.c.

00109 {
00110     static unsigned uTruthStore[7][2][2];
00111     static char uPhaseStore[7][2][64];
00112 
00113     unsigned char * pt0, * pt1;
00114     unsigned * ptRes0, * ptRes1, * ptRes;
00115     unsigned uInit0, uInit1, uTruth0, uTruth1, uTemp;
00116     char * pfRes0, * pfRes1, * pfRes;
00117     int nf0, nf1, nfRes, i, nVarsN;
00118 
00119     // table lookup for three vars
00120     if ( nVars == 3 )
00121     {
00122         *pptRes = &s_Truths3[*pt];
00123         *ppfRes = s_Phases3[*pt]+1;
00124         return s_Phases3[*pt][0];
00125     }
00126 
00127     // number of vars for the next call
00128     nVarsN = nVars-1;
00129     // truth table for the next call
00130     pt0 = pt;
00131     pt1 = pt + (1 << nVarsN) / 8;
00132     // 5-var truth tables for this call
00133 //    uInit0 = *((unsigned *)pt0);
00134 //    uInit1 = *((unsigned *)pt1);
00135     if ( nVarsN == 3 )
00136     {
00137         uInit0 = (pt0[0] << 24) | (pt0[0] << 16) | (pt0[0] << 8) | pt0[0];
00138         uInit1 = (pt1[0] << 24) | (pt1[0] << 16) | (pt1[0] << 8) | pt1[0];
00139     }
00140     else if ( nVarsN == 4 )
00141     {
00142         uInit0 = (pt0[1] << 24) | (pt0[0] << 16) | (pt0[1] << 8) | pt0[0];
00143         uInit1 = (pt1[1] << 24) | (pt1[0] << 16) | (pt1[1] << 8) | pt1[0];
00144     }
00145     else
00146     {
00147         uInit0 = (pt0[3] << 24) | (pt0[2] << 16) | (pt0[1] << 8) | pt0[0];
00148         uInit1 = (pt1[3] << 24) | (pt1[2] << 16) | (pt1[1] << 8) | pt1[0];
00149     }
00150 
00151     // storage for truth tables and phases
00152     ptRes  = uTruthStore[nVars][Flag];
00153     pfRes  = uPhaseStore[nVars][Flag];
00154 
00155     // solve trivial cases
00156     if ( uInit1 == 0 )
00157     {
00158         nf0 = Extra_TruthCanonN_rec( nVarsN, pt0, &ptRes0, &pfRes0, 0 );
00159         uTruth1 = uInit1;
00160         uTruth0 = *ptRes0;
00161         nfRes   = 0;
00162         for ( i = 0; i < nf0; i++ )
00163             pfRes[nfRes++] = pfRes0[i];
00164         goto finish;
00165     }
00166     if ( uInit0 == 0 )
00167     {
00168         nf1 = Extra_TruthCanonN_rec( nVarsN, pt1, &ptRes1, &pfRes1, 1 );
00169         uTruth1 = uInit0;
00170         uTruth0 = *ptRes1;
00171         nfRes   = 0;
00172         for ( i = 0; i < nf1; i++ )
00173             pfRes[nfRes++] = pfRes1[i] | (1<<nVarsN);
00174         goto finish;
00175     }
00176 
00177     if ( uInit1 == 0xFFFFFFFF )
00178     {
00179         nf0 = Extra_TruthCanonN_rec( nVarsN, pt0, &ptRes0, &pfRes0, 0 );
00180         uTruth1 = *ptRes0;
00181         uTruth0 = uInit1;
00182         nfRes   = 0;
00183         for ( i = 0; i < nf0; i++ )
00184             pfRes[nfRes++] = pfRes0[i] | (1<<nVarsN);
00185         goto finish;
00186     }
00187     if ( uInit0 == 0xFFFFFFFF )
00188     {
00189         nf1 = Extra_TruthCanonN_rec( nVarsN, pt1, &ptRes1, &pfRes1, 1 );
00190         uTruth1 = *ptRes1;
00191         uTruth0 = uInit0;
00192         nfRes   = 0;
00193         for ( i = 0; i < nf1; i++ )
00194             pfRes[nfRes++] = pfRes1[i];
00195         goto finish;
00196     }
00197 
00198     // solve the problem for cofactors
00199     nf0 = Extra_TruthCanonN_rec( nVarsN, pt0, &ptRes0, &pfRes0, 0 );
00200     nf1 = Extra_TruthCanonN_rec( nVarsN, pt1, &ptRes1, &pfRes1, 1 );
00201 
00202     // combine the result
00203     if ( *ptRes1 < *ptRes0 )
00204     {
00205         uTruth0 = 0xFFFFFFFF;
00206         nfRes   = 0;
00207         for ( i = 0; i < nf1; i++ )
00208         {
00209             uTemp = Extra_TruthPolarize( uInit0, pfRes1[i], nVarsN );
00210             if ( uTruth0 > uTemp )
00211             {
00212                 nfRes          = 0;
00213                 uTruth0        = uTemp;
00214                 pfRes[nfRes++] = pfRes1[i];
00215             }
00216             else if ( uTruth0 == uTemp ) 
00217                 pfRes[nfRes++] = pfRes1[i];
00218         }
00219         uTruth1 = *ptRes1;
00220     }
00221     else if ( *ptRes1 > *ptRes0 )
00222     {
00223         uTruth0 = 0xFFFFFFFF;
00224         nfRes   = 0;
00225         for ( i = 0; i < nf0; i++ )
00226         {
00227             uTemp = Extra_TruthPolarize( uInit1, pfRes0[i], nVarsN );
00228             if ( uTruth0 > uTemp )
00229             {
00230                 nfRes          = 0;
00231                 uTruth0        = uTemp;
00232                 pfRes[nfRes++] = pfRes0[i] | (1<<nVarsN);
00233             }
00234             else if ( uTruth0 == uTemp ) 
00235                 pfRes[nfRes++] = pfRes0[i] | (1<<nVarsN);
00236         }
00237         uTruth1 = *ptRes0;
00238     }
00239     else 
00240     {
00241         assert( nf0 == nf1 );
00242         nfRes = 0; 
00243         for ( i = 0; i < nf1; i++ )
00244             pfRes[nfRes++] = pfRes1[i];
00245         for ( i = 0; i < nf0; i++ )
00246             pfRes[nfRes++] = pfRes0[i] | (1<<nVarsN);
00247         uTruth0 = Extra_TruthPolarize( uInit0, pfRes1[0], nVarsN );
00248         uTruth1 = *ptRes0;
00249     }
00250 
00251 finish :
00252     if ( nVarsN == 3 )
00253     {
00254         uTruth0 &= 0xFF;
00255         uTruth1 &= 0xFF;
00256         uTemp = (uTruth1 << 8) | uTruth0;
00257         *ptRes = (uTemp << 16) | uTemp;
00258     }
00259     else if ( nVarsN == 4 )
00260     {
00261         uTruth0 &= 0xFFFF;
00262         uTruth1 &= 0xFFFF;
00263         *ptRes = (uTruth1 << 16) | uTruth0;
00264     }
00265     else if ( nVarsN == 5 )
00266     {
00267         *(ptRes+0) = uTruth0;
00268         *(ptRes+1) = uTruth1;
00269     }
00270 
00271     *pptRes = ptRes;
00272     *ppfRes = pfRes;
00273     return nfRes;
00274 }

void Map_Var3Print (  ) 

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 287 of file extraUtilCanon.c.

00288 {
00289     extern void Extra_Truth3VarN( unsigned ** puCanons, char *** puPhases, char ** ppCounters );
00290 
00291     unsigned * uCanons;
00292     char ** uPhases;
00293     char * pCounters;
00294     int i, k;
00295 
00296     Extra_Truth3VarN( &uCanons, &uPhases, &pCounters );
00297 
00298     for ( i = 0; i < 256; i++ )
00299     {
00300         if ( i % 8 == 0 )
00301             printf( "\n" );
00302         Extra_PrintHex( stdout, uCanons[i], 5 ); 
00303         printf( ", " );
00304     }
00305     printf( "\n" );
00306 
00307     for ( i = 0; i < 256; i++ )
00308     {
00309         printf( "%3d */  { %2d,   ", i, pCounters[i] );
00310         for ( k = 0; k < pCounters[i]; k++ )            
00311             printf( "%s%d", k? ", ":"", uPhases[i][k] );
00312         printf( " }\n" );
00313     }
00314     printf( "\n" );
00315 }

void Map_Var3Test (  ) 

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 328 of file extraUtilCanon.c.

00329 {
00330     extern void Extra_Truth3VarN( unsigned ** puCanons, char *** puPhases, char ** ppCounters );
00331 
00332     unsigned * uCanons;
00333     char ** uPhases;
00334     char * pCounters;
00335     int i;
00336     unsigned * ptRes;
00337     char * pfRes;
00338     unsigned uTruth;
00339     int Count;
00340 
00341     Extra_Truth3VarN( &uCanons, &uPhases, &pCounters );
00342 
00343     for ( i = 0; i < 256; i++ )
00344     {
00345         uTruth = i;
00346         Count =  Extra_TruthCanonFastN( 5, 3, &uTruth, &ptRes, &pfRes );
00347         if ( *ptRes != uCanons[i] || Count != pCounters[i] )
00348         {
00349             int k = 0;
00350         }
00351     }
00352 }

void Map_Var4Test (  ) 

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

Synopsis []

Description []

SideEffects []

SeeAlso []

Definition at line 365 of file extraUtilCanon.c.

00366 {
00367     extern void Extra_Truth4VarN( unsigned short ** puCanons, char *** puPhases, char ** ppCounters, int PhaseMax );
00368 
00369     unsigned short * uCanons;
00370     char ** uPhases;
00371     char * pCounters;
00372     int i, k;
00373     unsigned * ptRes;
00374     char * pfRes;
00375     unsigned uTruth;
00376     int Count;
00377 
00378     Extra_Truth4VarN( &uCanons, &uPhases, &pCounters, 16 );
00379 
00380     for ( i = 0; i < 256*256; i++ )
00381     {
00382         uTruth = i;
00383         Count =  Extra_TruthCanonFastN( 5, 4, &uTruth, &ptRes, &pfRes );
00384         if ( (*ptRes & 0xFFFF) != uCanons[i] || Count != pCounters[i] )
00385         {
00386             int k = 0;
00387         }
00388         for ( k = 0; k < Count; k++ )
00389             if ( uPhases[i][k] != pfRes[k] )
00390             {
00391                 int v = 0;
00392             }
00393     }
00394 }


Variable Documentation

static char s_Phases3 [static]

Definition at line 40 of file extraUtilCanon.c.

static unsigned s_Truths3 [static]

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

FileName [extraUtilMisc.c]

SystemName [ABC: Logic synthesis and verification system.]

PackageName [extra]

Synopsis [Computing canonical forms of Boolean functions using truth tables.]

Author [Alan Mishchenko]

Affiliation [UC Berkeley]

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

Revision [

Id
extraUtilMisc.c,v 1.0 2003/09/01 00:00:00 alanmi Exp

]

Definition at line 39 of file extraUtilCanon.c.


Generated on Tue Jan 5 12:19:14 2010 for abc70930 by  doxygen 1.6.1