ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
extraLutCas.h
Go to the documentation of this file.
1
22
23#ifndef ABC__misc__extra__extra_lutcas_h
24#define ABC__misc__extra__extra_lutcas_h
25
26#ifdef _WIN32
27#define inline __inline // compatible with MS VS 6.0
28#endif
29
30#ifdef _MSC_VER
31# include <intrin.h>
32# define __builtin_popcount __popcnt
33#endif
34
35#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38#include <assert.h>
39
40#include "aig/miniaig/miniaig.h"
41#include "bdd/cudd/cuddInt.h"
42#include "bdd/dsd/dsd.h"
43
45
46/*
47 The decomposed structure of the LUT cascade is represented as an array of 64-bit integers (words).
48 The first word in the record is the number of LUT info blocks in the record, which follow one by one.
49 Each LUT info block contains the following:
50 - the number of words in this block
51 - the number of fanins
52 - the list of fanins
53 - the variable ID of the output (can be one of the fanin variables)
54 - truth tables (one word for 6 vars or less; more words as needed for more than 6 vars)
55 For a 6-input node, the LUT info block takes 10 words (block size, fanin count, 6 fanins, output ID, truth table).
56 For a 4-input node, the LUT info block takes 8 words (block size, fanin count, 4 fanins, output ID, truth table).
57 If the LUT cascade contains a 6-LUT followed by a 4-LUT, the record contains 1+10+8=19 words.
58*/
59
60#define Mini_AigForEachNonPo( p, i ) for (i = 1; i < Mini_AigNodeNum(p); i++) if ( Mini_AigNodeIsPo(p, i) ) {} else
61
73void Abc_LutCasCollapseDeref( DdManager * dd, Vec_Ptr_t * vFuncs )
74{
75 DdNode * bFunc; int i;
76 Vec_PtrForEachEntry( DdNode *, vFuncs, bFunc, i )
77 if ( bFunc )
78 Cudd_RecursiveDeref( dd, bFunc );
79 Vec_PtrFree( vFuncs );
80}
81Vec_Ptr_t * Abc_LutCasCollapse( Mini_Aig_t * p, DdManager * dd, int nBddLimit, int fVerbose )
82{
83 DdNode * bFunc0, * bFunc1, * bFunc; int Id, nOuts = 0;
84 Vec_Ptr_t * vFuncs = Vec_PtrStart( Mini_AigNodeNum(p) );
85 Vec_PtrWriteEntry( vFuncs, 0, Cudd_ReadLogicZero(dd) ), Cudd_Ref(Cudd_ReadLogicZero(dd));
86 Mini_AigForEachPi( p, Id )
87 Vec_PtrWriteEntry( vFuncs, Id, Cudd_bddIthVar(dd,Id-1) ), Cudd_Ref(Cudd_bddIthVar(dd,Id-1));
88 Mini_AigForEachAnd( p, Id ) {
89 bFunc0 = Cudd_NotCond( (DdNode *)Vec_PtrEntry(vFuncs, Mini_AigLit2Var(Mini_AigNodeFanin0(p, Id))), Mini_AigLitIsCompl(Mini_AigNodeFanin0(p, Id)) );
90 bFunc1 = Cudd_NotCond( (DdNode *)Vec_PtrEntry(vFuncs, Mini_AigLit2Var(Mini_AigNodeFanin1(p, Id))), Mini_AigLitIsCompl(Mini_AigNodeFanin1(p, Id)) );
91 bFunc = Cudd_bddAndLimit( dd, bFunc0, bFunc1, nBddLimit );
92 if ( bFunc == NULL )
93 {
94 Abc_LutCasCollapseDeref( dd, vFuncs );
95 return NULL;
96 }
97 Cudd_Ref( bFunc );
98 Vec_PtrWriteEntry( vFuncs, Id, bFunc );
99 }
100 Mini_AigForEachPo( p, Id ) {
101 bFunc0 = Cudd_NotCond( (DdNode *)Vec_PtrEntry(vFuncs, Mini_AigLit2Var(Mini_AigNodeFanin0(p, Id))), Mini_AigLitIsCompl(Mini_AigNodeFanin0(p, Id)) );
102 Vec_PtrWriteEntry( vFuncs, Id, bFunc0 ); Cudd_Ref( bFunc0 );
103 }
104 Mini_AigForEachNonPo( p, Id ) {
105 bFunc = (DdNode *)Vec_PtrEntry(vFuncs, Id);
106 Cudd_RecursiveDeref( dd, bFunc );
107 Vec_PtrWriteEntry( vFuncs, Id, NULL );
108 }
109 Mini_AigForEachPo( p, Id )
110 Vec_PtrWriteEntry( vFuncs, nOuts++, Vec_PtrEntry(vFuncs, Id) );
111 Vec_PtrShrink( vFuncs, nOuts );
112 return vFuncs;
113}
114
126Vec_Ptr_t * Abc_LutBddScan( DdManager * dd, DdNode * bFunc, int nVars )
127{
128 Vec_Ptr_t * vRes = Vec_PtrAlloc( 1 << nVars );
129 Vec_Ptr_t * vRes2 = Vec_PtrAlloc( 1 << nVars );
130 Vec_PtrPush( vRes, bFunc );
131 int v, Level = Cudd_ReadPerm( dd, Cudd_NodeReadIndex(bFunc) );
132 for ( v = 0; v < dd->size; v++ )
133 printf( "%2d : perm = %d invperm = %d\n", v, dd->perm[v], dd->invperm[v] );
134 for ( v = 0; v < nVars; v++ )
135 {
136 int i, LevelCur = Level + v;
137 Vec_PtrClear( vRes2 );
138 DdNode * bTemp;
139 Vec_PtrForEachEntry( DdNode *, vRes, bTemp, i ) {
140 int LevelTemp = Cudd_ReadPerm( dd, Cudd_NodeReadIndex(bTemp) );
141 if ( LevelTemp == LevelCur ) {
142 Vec_PtrPush( vRes2, Cudd_NotCond(Cudd_E(bTemp), Cudd_IsComplement(bTemp)) );
143 Vec_PtrPush( vRes2, Cudd_NotCond(Cudd_T(bTemp), Cudd_IsComplement(bTemp)) );
144 }
145 else if ( LevelTemp > LevelCur ) {
146 Vec_PtrPush( vRes2, bTemp );
147 Vec_PtrPush( vRes2, bTemp );
148 }
149 else assert( 0 );
150 }
151 ABC_SWAP( Vec_Ptr_t *, vRes, vRes2 );
152 //Vec_PtrForEachEntry( DdNode *, vRes, bTemp, i )
153 // printf( "%p ", bTemp );
154 //printf( "\n" );
155 }
156 Vec_PtrFree( vRes2 );
157 assert( Vec_PtrSize(vRes) == (1 << nVars) );
158 return vRes;
159}
160char * Abc_LutBddToTruth( Vec_Ptr_t * vFuncs )
161{
162 assert( Vec_PtrSize(vFuncs) <= 256 );
163 char * pRes = ABC_CALLOC( char, Vec_PtrSize(vFuncs)+1 );
164 void * pTemp, * pStore[256] = {Vec_PtrEntry(vFuncs, 0)};
165 int i, k, nStore = 1; pRes[0] = 'a';
166 Vec_PtrForEachEntryStart( void *, vFuncs, pTemp, i, 1 ) {
167 for ( k = 0; k < nStore; k++ )
168 if ( pStore[k] == pTemp )
169 break;
170 if ( k == nStore )
171 pStore[nStore++] = pTemp;
172 pRes[i] = 'a' + (char)k;
173 }
174 return pRes;
175}
176
189{
190 char * pRes = ABC_CALLOC( char, 1 << 16 );
191 int i, k, b;
192 for ( i = 0; i < 256; i++ ) {
193 int nOnes = __builtin_popcount(i);
194 char * pTemp = pRes + 256*i;
195 for ( k = 0; k < 256; k++ ) {
196 int iMask = 0, Counts[2] = {nOnes, 0};
197 for ( b = 0; b < 8; Counts[(i >> b++)&1]++ )
198 if ( (k >> b) & 1 )
199 iMask |= 1 << Counts[(i >> b)&1];
200 pTemp[iMask] = (char)k;
201 assert( Counts[1] == nOnes );
202 }
203 }
204 for ( i = 0; i < 16; i++, printf("\n") )
205 for ( printf("%x : ", i), k = 0; k < 16; k++ )
206 printf( "%x=%x ", k, (int)pRes[i*256+k] );
207 return pRes;
208}
209
210int Abc_NtkDecPatCount( int iFirst, int nStep, int MyuMax, char * pDecPat, char * pPermInfo )
211{
212 int s, k, nPats = 1;
213 char Pats[256] = { pDecPat[(int)pPermInfo[iFirst]] };
214 assert( MyuMax <= 256 );
215 for ( s = 1; s < nStep; s++ ) {
216 char Entry = pDecPat[(int)pPermInfo[iFirst+s]];
217 for ( k = 0; k < nPats; k++ )
218 if ( Pats[k] == Entry )
219 break;
220 if ( k < nPats )
221 continue;
222 if ( nPats == MyuMax )
223 return MyuMax + 1;
224 assert( nPats < 256 );
225 Pats[nPats++] = Entry;
226 }
227 return nPats;
228}
229int Abc_NtkDecPatDecompose_rec( int Mask, int nMaskVars, int iStart, int nVars, int nDiffs, int nRails, char * pDecPat, char * pPermInfo )
230{
231 if ( nDiffs == 0 || iStart == nVars )
232 return 0;
233 int v, m, nMints = 1 << nVars;
234 for ( v = iStart; v < nVars; v++ ) {
235 int MaskThis = Mask & ~(1 << v);
236 int nStep = 1 << (nMaskVars - 1);
237 int MyuMax = 0;
238 for ( m = 0; m < nMints; m += nStep ) {
239 int MyuCur = Abc_NtkDecPatCount( m, nStep, 1 << nDiffs, pDecPat, pPermInfo+256*MaskThis );
240 MyuMax = Abc_MaxInt( MyuMax, MyuCur );
241 }
242 if ( MyuMax > (1 << nDiffs) )
243 continue;
244 if ( MyuMax <= (1 << nRails) )
245 return MaskThis;
246 MaskThis = Abc_NtkDecPatDecompose_rec( MaskThis, nMaskVars-1, v+1, nVars, nDiffs-1, nRails, pDecPat, pPermInfo );
247 if ( MaskThis )
248 return MaskThis;
249 }
250 return 0;
251}
252int Abc_NtkDecPatDecompose( int nVars, int nRails, char * pDecPat, char * pPermInfo )
253{
254 int BoundSet = ~(~0 << nVars);
255 int Myu = Abc_NtkDecPatCount( 0, 1 << nVars, 256, pDecPat, pPermInfo + 256*BoundSet );
256 int Log2 = Abc_Base2Log( Myu );
257 if ( Log2 <= nRails )
258 return BoundSet;
259 return Abc_NtkDecPatDecompose_rec( BoundSet, nVars, 0, nVars, Log2 - nRails, nRails, pDecPat, pPermInfo );
260}
261
262int Abc_NtkCascadeDecompose( int nVars, int nRails, char * pDecPat, char * pPermInfo )
263{
264 return 0;
265}
266
267
268
281{
282 Vec_Ptr_t * vNames;
283 char Buffer[5];
284 int i;
285
286 vNames = Vec_PtrAlloc( nNames );
287 for ( i = 0; i < nNames; i++ )
288 {
289 if ( nNames < 26 )
290 {
291 Buffer[0] = 'a' + i;
292 Buffer[1] = 0;
293 }
294 else
295 {
296 Buffer[0] = 'a' + i%26;
297 Buffer[1] = '0' + i/26;
298 Buffer[2] = 0;
299 }
300 Vec_PtrPush( vNames, Extra_UtilStrsav(Buffer) );
301 }
302 return vNames;
303}
304void Abc_LutCasPrintDsd( DdManager * dd, DdNode * bFunc, int fVerbose )
305{
306 Dsd_Manager_t * pManDsd = Dsd_ManagerStart( dd, dd->size, 0 );
307 Dsd_Decompose( pManDsd, &bFunc, 1 );
308 if ( fVerbose )
309 {
310 Vec_Ptr_t * vNamesCi = Abc_LutCasFakeNames( dd->size );
311 Vec_Ptr_t * vNamesCo = Abc_LutCasFakeNames( 1 );
312 char ** ppNamesCi = (char **)Vec_PtrArray( vNamesCi );
313 char ** ppNamesCo = (char **)Vec_PtrArray( vNamesCo );
314 Dsd_TreePrint( stdout, pManDsd, ppNamesCi, ppNamesCo, 0, -1, 0 );
315 Vec_PtrFreeFree( vNamesCi );
316 Vec_PtrFreeFree( vNamesCo );
317 }
318 Dsd_ManagerStop( pManDsd );
319}
320DdNode * Abc_LutCasBuildBdds( Mini_Aig_t * p, DdManager ** pdd, int fReorder )
321{
322 DdManager * dd = Cudd_Init( Mini_AigPiNum(p), 0, CUDD_UNIQUE_SLOTS, CUDD_CACHE_SLOTS, 0 );
323 if ( fReorder ) Cudd_AutodynEnable( dd, CUDD_REORDER_SYMM_SIFT );
324 Vec_Ptr_t * vFuncs = Abc_LutCasCollapse( p, dd, 10000, 0 );
325 if ( fReorder ) Cudd_AutodynDisable( dd );
326 if ( vFuncs == NULL ) {
327 Extra_StopManager( dd );
328 return NULL;
329 }
330 DdNode * bNode = (DdNode *)Vec_PtrEntry(vFuncs, 0);
331 Vec_PtrFree(vFuncs);
332 *pdd = dd;
333 return bNode;
334}
335static inline word * Abc_LutCascade( Mini_Aig_t * p, int nLutSize, int nLuts, int nRails, int nIters, int fVerbose )
336{
337 DdManager * dd = NULL;
338 DdNode * bFunc = Abc_LutCasBuildBdds( p, &dd, 0 );
339 if ( bFunc == NULL ) return NULL;
340 char * pPermInfo = Abc_NtkPrecomputeData();
341 Abc_LutCasPrintDsd( dd, bFunc, 1 );
342
343 Vec_Ptr_t * vTemp = Abc_LutBddScan( dd, bFunc, nLutSize );
344 char * pTruth = Abc_LutBddToTruth( vTemp );
345
346 int BoundSet = Abc_NtkDecPatDecompose( nLutSize, nRails, pTruth, pPermInfo );
347 printf( "Pattern %s : Bound set = %d\n", pTruth, BoundSet );
348
349 ABC_FREE( pTruth );
350 Vec_PtrFree( vTemp );
351
352 Cudd_RecursiveDeref( dd, bFunc );
353 Extra_StopManager( dd );
354 ABC_FREE( pPermInfo );
355
356 printf( "\n" );
357 word * pLuts = NULL;
358 return pLuts;
359}
360
362
363#endif /* ABC__misc__extra__extra_lutcas_h */
word * Abc_LutCascade(Mini_Aig_t *p, int nLutSize, int nStages, int nRails, int nIters, int fVerbose)
Definition abcCas.c:335
#define ABC_SWAP(Type, a, b)
Definition abc_global.h:253
#define ABC_CALLOC(type, num)
Definition abc_global.h:265
#define ABC_FREE(obj)
Definition abc_global.h:267
#define ABC_NAMESPACE_HEADER_END
#define ABC_NAMESPACE_HEADER_START
NAMESPACES ///.
Dsd_Manager_t * Dsd_ManagerStart(DdManager *dd, int nSuppMax, int fVerbose)
FUNCTION DECLARATIONS ///.
Definition dsdMan.c:47
void Dsd_TreePrint(FILE *pFile, Dsd_Manager_t *dMan, char *pInputNames[], char *pOutputNames[], int fShortNames, int Output, int OffSet)
Definition dsdTree.c:830
struct Dsd_Manager_t_ Dsd_Manager_t
TYPEDEF DEFINITIONS ///.
Definition dsd.h:59
void Dsd_ManagerStop(Dsd_Manager_t *dMan)
Definition dsdMan.c:100
void Dsd_Decompose(Dsd_Manager_t *dMan, DdNode **pbFuncs, int nFuncs)
DECOMPOSITION FUNCTIONS ///.
Definition dsdProc.c:113
Cube * p
Definition exorList.c:222
void Extra_StopManager(DdManager *dd)
char * Abc_LutBddToTruth(Vec_Ptr_t *vFuncs)
int Abc_NtkCascadeDecompose(int nVars, int nRails, char *pDecPat, char *pPermInfo)
Vec_Ptr_t * Abc_LutCasCollapse(Mini_Aig_t *p, DdManager *dd, int nBddLimit, int fVerbose)
Definition extraLutCas.h:81
Vec_Ptr_t * Abc_LutBddScan(DdManager *dd, DdNode *bFunc, int nVars)
int Abc_NtkDecPatDecompose_rec(int Mask, int nMaskVars, int iStart, int nVars, int nDiffs, int nRails, char *pDecPat, char *pPermInfo)
DdNode * Abc_LutCasBuildBdds(Mini_Aig_t *p, DdManager **pdd, int fReorder)
void Abc_LutCasCollapseDeref(DdManager *dd, Vec_Ptr_t *vFuncs)
Definition extraLutCas.h:73
#define Mini_AigForEachNonPo(p, i)
Definition extraLutCas.h:60
char * Abc_NtkPrecomputeData()
Vec_Ptr_t * Abc_LutCasFakeNames(int nNames)
void Abc_LutCasPrintDsd(DdManager *dd, DdNode *bFunc, int fVerbose)
int Abc_NtkDecPatCount(int iFirst, int nStep, int MyuMax, char *pDecPat, char *pPermInfo)
int Abc_NtkDecPatDecompose(int nVars, int nRails, char *pDecPat, char *pPermInfo)
char * Extra_UtilStrsav(const char *s)
unsigned __int64 word
DECLARATIONS ///.
Definition kitPerm.c:36
struct Mini_Aig_t_ Mini_Aig_t
BASIC TYPES ///.
Definition miniaig.h:48
#define Mini_AigForEachPi(p, i)
Definition miniaig.h:138
#define Mini_AigForEachPo(p, i)
Definition miniaig.h:139
#define Mini_AigForEachAnd(p, i)
Definition miniaig.h:140
#define assert(ex)
Definition util_old.h:213
#define Vec_PtrForEachEntryStart(Type, vVec, pEntry, i, Start)
Definition vecPtr.h:57
typedefABC_NAMESPACE_HEADER_START struct Vec_Ptr_t_ Vec_Ptr_t
INCLUDES ///.
Definition vecPtr.h:42
#define Vec_PtrForEachEntry(Type, vVec, pEntry, i)
MACRO DEFINITIONS ///.
Definition vecPtr.h:55