1 /**CFile****************************************************************
2 
3   FileName    [pla.h]
4 
5   SystemName  [ABC: Logic synthesis and verification system.]
6 
7   PackageName [SOP manager.]
8 
9   Synopsis    [Scalable SOP transformations.]
10 
11   Author      [Alan Mishchenko]
12 
13   Affiliation [UC Berkeley]
14 
15   Date        [Ver. 1.0. Started - March 18, 2015.]
16 
17   Revision    [$Id: pla.h,v 1.00 2014/09/12 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #ifndef ABC__base__wlc__wlc_h
22 #define ABC__base__wlc__wlc_h
23 
24 ////////////////////////////////////////////////////////////////////////
25 ///                          INCLUDES                                ///
26 ////////////////////////////////////////////////////////////////////////
27 
28 #include "aig/gia/gia.h"
29 #include "misc/extra/extra.h"
30 #include "base/main/mainInt.h"
31 //#include "misc/util/utilTruth.h"
32 
33 ////////////////////////////////////////////////////////////////////////
34 ///                         PARAMETERS                               ///
35 ////////////////////////////////////////////////////////////////////////
36 
37 ABC_NAMESPACE_HEADER_START
38 
39 #define MASK55 ABC_CONST(0x5555555555555555)
40 
41 ////////////////////////////////////////////////////////////////////////
42 ///                         BASIC TYPES                              ///
43 ////////////////////////////////////////////////////////////////////////
44 
45 // file types
46 typedef enum {
47     PLA_FILE_FD = 0,
48     PLA_FILE_F,
49     PLA_FILE_FR,
50     PLA_FILE_FDR,
51     PLA_FILE_NONE
52 } Pla_File_t;
53 
54 // literal types
55 typedef enum {
56     PLA_LIT_DASH = 0,
57     PLA_LIT_ZERO,
58     PLA_LIT_ONE,
59     PLA_LIT_FULL
60 } Pla_Lit_t;
61 
62 
63 typedef struct Pla_Man_t_ Pla_Man_t;
64 struct Pla_Man_t_
65 {
66     char *           pName;      // model name
67     char *           pSpec;      // input file
68     Pla_File_t       Type;       // file type
69     int              nIns;       // inputs
70     int              nOuts;      // outputs
71     int              nInWords;   // words of input data
72     int              nOutWords;  // words of output data
73     Vec_Int_t        vCubes;     // cubes
74     Vec_Int_t        vHashes;    // hash values
75     Vec_Wrd_t        vInBits;    // input bits
76     Vec_Wrd_t        vOutBits;   // output bits
77     Vec_Wec_t        vCubeLits;  // cubes as interger arrays
78     Vec_Wec_t        vOccurs;    // occurence counters for the literals
79     Vec_Int_t        vDivs;      // divisor definitions
80 };
81 
Pla_ManName(Pla_Man_t * p)82 static inline char * Pla_ManName( Pla_Man_t * p )                    { return p->pName;                   }
Pla_ManInNum(Pla_Man_t * p)83 static inline int    Pla_ManInNum( Pla_Man_t * p )                   { return p->nIns;                    }
Pla_ManOutNum(Pla_Man_t * p)84 static inline int    Pla_ManOutNum( Pla_Man_t * p )                  { return p->nOuts;                   }
Pla_ManCubeNum(Pla_Man_t * p)85 static inline int    Pla_ManCubeNum( Pla_Man_t * p )                 { return Vec_IntSize( &p->vCubes );  }
Pla_ManDivNum(Pla_Man_t * p)86 static inline int    Pla_ManDivNum( Pla_Man_t * p )                  { return Vec_IntSize( &p->vDivs );   }
87 
Pla_CubeIn(Pla_Man_t * p,int i)88 static inline word * Pla_CubeIn( Pla_Man_t * p, int i )              { return Vec_WrdEntryP(&p->vInBits,  i * p->nInWords);  }
Pla_CubeOut(Pla_Man_t * p,int i)89 static inline word * Pla_CubeOut( Pla_Man_t * p, int i )             { return Vec_WrdEntryP(&p->vOutBits, i * p->nOutWords); }
90 
Pla_CubeNum(int hCube)91 static inline int    Pla_CubeNum( int hCube )                        { return hCube >> 8;    }
Pla_CubeLit(int hCube)92 static inline int    Pla_CubeLit( int hCube )                        { return hCube & 0xFF;  }
Pla_CubeHandle(int iCube,int iLit)93 static inline int    Pla_CubeHandle( int iCube, int iLit )           { assert( !(iCube >> 24) && !(iLit >> 8) ); return iCube << 8 | iLit; }
94 
95 // read/write/flip i-th bit of a bit string table
Pla_TtGetBit(word * p,int i)96 static inline int    Pla_TtGetBit( word * p, int i )                 { return (int)(p[i>>6] >> (i & 63)) & 1;      }
Pla_TtSetBit(word * p,int i)97 static inline void   Pla_TtSetBit( word * p, int i )                 { p[i>>6] |= (((word)1)<<(i & 63));           }
Pla_TtXorBit(word * p,int i)98 static inline void   Pla_TtXorBit( word * p, int i )                 { p[i>>6] ^= (((word)1)<<(i & 63));           }
99 
100 // read/write/flip i-th literal in a cube
Pla_CubeGetLit(word * p,int i)101 static inline int    Pla_CubeGetLit( word * p, int i )               { return (int)(p[i>>5] >> ((i<<1) & 63)) & 3; }
Pla_CubeSetLit(word * p,int i,Pla_Lit_t d)102 static inline void   Pla_CubeSetLit( word * p, int i, Pla_Lit_t d )  { p[i>>5] |= (((word)d)<<((i<<1) & 63));      }
Pla_CubeXorLit(word * p,int i,Pla_Lit_t d)103 static inline void   Pla_CubeXorLit( word * p, int i, Pla_Lit_t d )  { p[i>>5] ^= (((word)d)<<((i<<1) & 63));      }
104 
105 
106 ////////////////////////////////////////////////////////////////////////
107 ///                      MACRO DEFINITIONS                           ///
108 ////////////////////////////////////////////////////////////////////////
109 
110 ////////////////////////////////////////////////////////////////////////
111 ///                             ITERATORS                            ///
112 ////////////////////////////////////////////////////////////////////////
113 
114 #define Pla_ForEachCubeIn( p, pCube, i )                            \
115     for ( i = 0; (i < Pla_ManCubeNum(p))  && (((pCube) = Pla_CubeIn(p, i)), 1); i++ )
116 #define Pla_ForEachCubeInStart( p, pCube, i, Start )                \
117     for ( i = Start; (i < Pla_ManCubeNum(p))  && (((pCube) = Pla_CubeIn(p, i)), 1); i++ )
118 
119 #define Pla_ForEachCubeOut( p, pCube, i )                           \
120     for ( i = 0; (i < Pla_ManCubeNum(p))  && (((pCube) = Pla_CubeOut(p, i)), 1); i++ )
121 #define Pla_ForEachCubeInOut( p, pCubeIn, pCubeOut, i )             \
122     for ( i = 0; (i < Pla_ManCubeNum(p))  && (((pCubeIn) = Pla_CubeIn(p, i)), 1)  && (((pCubeOut) = Pla_CubeOut(p, i)), 1); i++ )
123 
124 #define Pla_CubeForEachLit( nVars, pCube, Lit, i )                  \
125     for ( i = 0; (i < nVars)  && (((Lit) = Pla_CubeGetLit(pCube, i)), 1); i++ )
126 #define Pla_CubeForEachLitIn( p, pCube, Lit, i )                    \
127     Pla_CubeForEachLit( Pla_ManInNum(p), pCube, Lit, i )
128 #define Pla_CubeForEachLitOut( p, pCube, Lit, i )                   \
129     Pla_CubeForEachLit( Pla_ManOutNum(p), pCube, Lit, i )
130 
131 
132 ////////////////////////////////////////////////////////////////////////
133 ///                    FUNCTION DECLARATIONS                         ///
134 ////////////////////////////////////////////////////////////////////////
135 
136 /**Function*************************************************************
137 
138   Synopsis    [Checks if cubes are distance-1.]
139 
140   Description []
141 
142   SideEffects []
143 
144   SeeAlso     []
145 
146 ***********************************************************************/
Pla_OnlyOneOne(word t)147 static inline int Pla_OnlyOneOne( word t )
148 {
149     return t ? ((t & (t-1)) == 0) : 0;
150 }
Pla_CubesAreDistance1(word * p,word * q,int nWords)151 static inline int Pla_CubesAreDistance1( word * p, word * q, int nWords )
152 {
153     word Test; int c, fFound = 0;
154     for ( c = 0; c < nWords; c++ )
155     {
156         if ( p[c] == q[c] )
157             continue;
158         if ( fFound )
159             return 0;
160         // check if the number of 1s is one, which means exactly one different literal (0/1, -/1, -/0)
161         Test = ((p[c] ^ q[c]) | ((p[c] ^ q[c]) >> 1)) & MASK55;
162         if ( !Pla_OnlyOneOne(Test) )
163             return 0;
164         fFound = 1;
165     }
166     return fFound;
167 }
Pla_CubesAreConsensus(word * p,word * q,int nWords,int * piVar)168 static inline int Pla_CubesAreConsensus( word * p, word * q, int nWords, int * piVar )
169 {
170     word Test; int c, fFound = 0;
171     for ( c = 0; c < nWords; c++ )
172     {
173         if ( p[c] == q[c] )
174             continue;
175         if ( fFound )
176             return 0;
177         // check if the number of 1s is one, which means exactly one opposite literal (0/1) but may have other diffs (-/0 or -/1)
178         Test = ((p[c] ^ q[c]) & ((p[c] ^ q[c]) >> 1)) & MASK55;
179         if ( !Pla_OnlyOneOne(Test) )
180             return 0;
181         fFound = 1;
182         if ( piVar ) *piVar = c * 32 + Abc_Tt6FirstBit(Test)/2;
183     }
184     return fFound;
185 }
Pla_TtCountOnesOne(word x)186 static inline int Pla_TtCountOnesOne( word x )
187 {
188     x = x - ((x >> 1) & ABC_CONST(0x5555555555555555));
189     x = (x & ABC_CONST(0x3333333333333333)) + ((x >> 2) & ABC_CONST(0x3333333333333333));
190     x = (x + (x >> 4)) & ABC_CONST(0x0F0F0F0F0F0F0F0F);
191     x = x + (x >> 8);
192     x = x + (x >> 16);
193     x = x + (x >> 32);
194     return (int)(x & 0xFF);
195 }
Pla_TtCountOnes(word * p,int nWords)196 static inline int Pla_TtCountOnes( word * p, int nWords )
197 {
198     int i, Count = 0;
199     for ( i = 0; i < nWords; i++ )
200         Count += Pla_TtCountOnesOne( p[i] );
201     return Count;
202 }
203 
204 /**Function*************************************************************
205 
206   Synopsis    [Manager APIs.]
207 
208   Description []
209 
210   SideEffects []
211 
212   SeeAlso     []
213 
214 ***********************************************************************/
Pla_ManAlloc(char * pFileName,int nIns,int nOuts,int nCubes)215 static inline Pla_Man_t * Pla_ManAlloc( char * pFileName, int nIns, int nOuts, int nCubes )
216 {
217     Pla_Man_t * p = ABC_CALLOC( Pla_Man_t, 1 );
218     p->pName = Extra_FileDesignName( pFileName );
219     p->pSpec = Abc_UtilStrsav( pFileName );
220     p->nIns  = nIns;
221     p->nOuts = nOuts;
222     p->nInWords  = Abc_Bit6WordNum( 2*nIns );
223     p->nOutWords = Abc_Bit6WordNum( 2*nOuts );
224     Vec_IntFillNatural( &p->vCubes, nCubes );
225     Vec_WrdFill( &p->vInBits,  Pla_ManCubeNum(p) * p->nInWords,  0 );
226     Vec_WrdFill( &p->vOutBits, Pla_ManCubeNum(p) * p->nOutWords, 0 );
227     return p;
228 }
Pla_ManFree(Pla_Man_t * p)229 static inline void Pla_ManFree( Pla_Man_t * p )
230 {
231     Vec_IntErase( &p->vCubes );
232     Vec_IntErase( &p->vHashes );
233     Vec_WrdErase( &p->vInBits );
234     Vec_WrdErase( &p->vOutBits );
235     Vec_WecErase( &p->vCubeLits );
236     Vec_WecErase( &p->vOccurs );
237     Vec_IntErase( &p->vDivs );
238     ABC_FREE( p->pName );
239     ABC_FREE( p->pSpec );
240     ABC_FREE( p );
241 }
Pla_ManLitInNum(Pla_Man_t * p)242 static inline int Pla_ManLitInNum( Pla_Man_t * p )
243 {
244     word * pCube; int i, k, Lit, Count = 0;
245     Pla_ForEachCubeIn( p, pCube, i )
246         Pla_CubeForEachLitIn( p, pCube, Lit, k )
247             Count += Lit != PLA_LIT_DASH;
248     return Count;
249 }
Pla_ManLitOutNum(Pla_Man_t * p)250 static inline int Pla_ManLitOutNum( Pla_Man_t * p )
251 {
252     word * pCube; int i, k, Lit, Count = 0;
253     Pla_ForEachCubeOut( p, pCube, i )
254         Pla_CubeForEachLitOut( p, pCube, Lit, k )
255             Count += Lit == PLA_LIT_ONE;
256     return Count;
257 }
Pla_ManPrintStats(Pla_Man_t * p,int fVerbose)258 static inline void Pla_ManPrintStats( Pla_Man_t * p, int fVerbose )
259 {
260     printf( "%-16s :  ",     Pla_ManName(p) );
261     printf( "In =%4d  ",     Pla_ManInNum(p) );
262     printf( "Out =%4d  ",    Pla_ManOutNum(p) );
263     printf( "Cube =%8d  ",   Pla_ManCubeNum(p) );
264     printf( "LitIn =%8d  ",  Pla_ManLitInNum(p) );
265     printf( "LitOut =%8d  ", Pla_ManLitOutNum(p) );
266     printf( "Div =%6d  ",    Pla_ManDivNum(p) );
267     printf( "\n" );
268 }
269 
270 
271 
272 /*=== plaHash.c ========================================================*/
273 extern int                 Pla_ManHashDist1NumTest( Pla_Man_t * p );
274 extern void                Pla_ManComputeDist1Test( Pla_Man_t * p );
275 /*=== plaMan.c ========================================================*/
276 extern Vec_Bit_t *         Pla_ManPrimesTable( int nVars );
277 extern Pla_Man_t *         Pla_ManPrimesDetector( int nVars );
278 extern Pla_Man_t *         Pla_ManGenerate( int nIns, int nOuts, int nCubes, int fVerbose );
279 extern void                Pla_ManConvertFromBits( Pla_Man_t * p );
280 extern void                Pla_ManConvertToBits( Pla_Man_t * p );
281 extern int                 Pla_ManDist1NumTest( Pla_Man_t * p );
282 /*=== plaMerge.c ========================================================*/
283 extern int                 Pla_ManDist1Merge( Pla_Man_t * p );
284 /*=== plaSimple.c ========================================================*/
285 extern int                 Pla_ManFxPerformSimple( int nVars );
286 /*=== plaRead.c ========================================================*/
287 extern Pla_Man_t *         Pla_ReadPla( char * pFileName );
288 /*=== plaWrite.c ========================================================*/
289 extern void                Pla_WritePla( Pla_Man_t * p, char * pFileName );
290 
291 ABC_NAMESPACE_HEADER_END
292 
293 #endif
294 
295 ////////////////////////////////////////////////////////////////////////
296 ///                       END OF FILE                                ///
297 ////////////////////////////////////////////////////////////////////////
298