/**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 $] ***********************************************************************/ #include "extra.h" /*---------------------------------------------------------------------------*/ /* Constant declarations */ /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ /* Stucture declarations */ /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ /* Type declarations */ /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ /* Variable declarations */ /*---------------------------------------------------------------------------*/ static unsigned s_Truths3[256]; static char s_Phases3[256][9]; /*---------------------------------------------------------------------------*/ /* Macro declarations */ /*---------------------------------------------------------------------------*/ /**AutomaticStart*************************************************************/ /*---------------------------------------------------------------------------*/ /* Static function prototypes */ /*---------------------------------------------------------------------------*/ static int Extra_TruthCanonN_rec( int nVars, unsigned char * pt, unsigned ** pptRes, char ** ppfRes, int Flag ); /**AutomaticEnd***************************************************************/ /*---------------------------------------------------------------------------*/ /* Definition of exported functions */ /*---------------------------------------------------------------------------*/ /**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 [] ******************************************************************************/ int Extra_TruthCanonFastN( int nVarsMax, int nVarsReal, unsigned * pt, unsigned ** pptRes, char ** ppfRes ) { static unsigned uTruthStore6[2]; int RetValue; assert( nVarsMax <= 6 ); assert( nVarsReal <= nVarsMax ); RetValue = Extra_TruthCanonN_rec( nVarsReal <= 3? 3: nVarsReal, (unsigned char *)pt, pptRes, ppfRes, 0 ); if ( nVarsMax == 6 && nVarsReal < nVarsMax ) { uTruthStore6[0] = **pptRes; uTruthStore6[1] = **pptRes; *pptRes = uTruthStore6; } return RetValue; } /*---------------------------------------------------------------------------*/ /* Definition of internal functions */ /*---------------------------------------------------------------------------*/ /**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 [] ***********************************************************************/ int Extra_TruthCanonN_rec( int nVars, unsigned char * pt, unsigned ** pptRes, char ** ppfRes, int Flag ) { static unsigned uTruthStore[7][2][2]; static char uPhaseStore[7][2][64]; unsigned char * pt0, * pt1; unsigned * ptRes0, * ptRes1, * ptRes; unsigned uInit0, uInit1, uTruth0, uTruth1, uTemp; char * pfRes0, * pfRes1, * pfRes; int nf0, nf1, nfRes, i, nVarsN; // table lookup for three vars if ( nVars == 3 ) { *pptRes = &s_Truths3[*pt]; *ppfRes = s_Phases3[*pt]+1; return s_Phases3[*pt][0]; } // number of vars for the next call nVarsN = nVars-1; // truth table for the next call pt0 = pt; pt1 = pt + (1 << nVarsN) / 8; // 5-var truth tables for this call // uInit0 = *((unsigned *)pt0); // uInit1 = *((unsigned *)pt1); if ( nVarsN == 3 ) { uInit0 = (pt0[0] << 24) | (pt0[0] << 16) | (pt0[0] << 8) | pt0[0]; uInit1 = (pt1[0] << 24) | (pt1[0] << 16) | (pt1[0] << 8) | pt1[0]; } else if ( nVarsN == 4 ) { uInit0 = (pt0[1] << 24) | (pt0[0] << 16) | (pt0[1] << 8) | pt0[0]; uInit1 = (pt1[1] << 24) | (pt1[0] << 16) | (pt1[1] << 8) | pt1[0]; } else { uInit0 = (pt0[3] << 24) | (pt0[2] << 16) | (pt0[1] << 8) | pt0[0]; uInit1 = (pt1[3] << 24) | (pt1[2] << 16) | (pt1[1] << 8) | pt1[0]; } // storage for truth tables and phases ptRes = uTruthStore[nVars][Flag]; pfRes = uPhaseStore[nVars][Flag]; // solve trivial cases if ( uInit1 == 0 ) { nf0 = Extra_TruthCanonN_rec( nVarsN, pt0, &ptRes0, &pfRes0, 0 ); uTruth1 = uInit1; uTruth0 = *ptRes0; nfRes = 0; for ( i = 0; i < nf0; i++ ) pfRes[nfRes++] = pfRes0[i]; goto finish; } if ( uInit0 == 0 ) { nf1 = Extra_TruthCanonN_rec( nVarsN, pt1, &ptRes1, &pfRes1, 1 ); uTruth1 = uInit0; uTruth0 = *ptRes1; nfRes = 0; for ( i = 0; i < nf1; i++ ) pfRes[nfRes++] = pfRes1[i] | (1< uTemp ) { nfRes = 0; uTruth0 = uTemp; pfRes[nfRes++] = pfRes1[i]; } else if ( uTruth0 == uTemp ) pfRes[nfRes++] = pfRes1[i]; } uTruth1 = *ptRes1; } else if ( *ptRes1 > *ptRes0 ) { uTruth0 = 0xFFFFFFFF; nfRes = 0; for ( i = 0; i < nf0; i++ ) { uTemp = Extra_TruthPolarize( uInit1, pfRes0[i], nVarsN ); if ( uTruth0 > uTemp ) { nfRes = 0; uTruth0 = uTemp; pfRes[nfRes++] = pfRes0[i] | (1<