33 #define BAL_LEAF_MAX 6
36 #define BAL_NO_LEAF 31
37 #define BAL_NO_FUNC 134217727 // (1<<27)-1
123 i = i - ((i >> 1) & 0x5555555555555555);
124 i = (i & 0x3333333333333333) + ((i >> 2) & 0x3333333333333333);
125 i = ((i + (i >> 4)) & 0x0F0F0F0F0F0F0F0F);
126 return (i*(0x0101010101010101))>>56;
130 word Sign = 0;
int i;
131 for ( i = 0; i < nLeaves; i++ )
132 Sign |= ((
word)1) << (pLeaves[i] & 0x3F);
150 for ( i = 0; i < p->
nCutNum; i++ )
174 for ( i = 0; i < nSizeC; i++ )
176 for ( k = 0; k < nSizeB; k++ )
177 if ( pC[i] == pB[k] )
187 int i, k, m, n, Value;
189 for ( i = 0; i < nCuts; i++ )
195 for ( m = 0; m < (int)pCut0->
nLeaves; m++ )
196 for ( n = m + 1; n < (int)pCut0->
nLeaves; n++ )
199 for ( k = 0; k < nCuts; k++ )
202 if ( pCut0 == pCut1 )
231 if ( nSize0 == nLutSize && nSize1 == nLutSize )
233 for ( i = 0; i < nSize0; i++ )
235 if ( pC0[i] != pC1[i] )
return 0;
248 if ( c == nLutSize )
return 0;
249 if ( pC0[i] < pC1[k] )
252 if ( i >= nSize0 )
goto FlushCut1;
254 else if ( pC0[i] > pC1[k] )
257 if ( k >= nSize1 )
goto FlushCut0;
261 pC[c++] = pC0[i++]; k++;
262 if ( i >= nSize0 )
goto FlushCut1;
263 if ( k >= nSize1 )
goto FlushCut0;
268 if ( c + nSize0 > nLutSize + i )
return 0;
278 if ( c + nSize1 > nLutSize + k )
return 0;
292 int xMin, c = 0, * pC = pCut->
pLeaves;
300 if ( c == nLutSize )
return 0;
302 if (x0 == xMin) i0++;
303 if (x1 == xMin) i1++;
304 if (x2 == xMin) i2++;
314 int i, nSizeB = pBase->
nLeaves;
316 if ( nSizeB == nSizeC )
318 for ( i = 0; i < nSizeB; i++ )
323 assert( nSizeB > nSizeC );
326 for ( i = k = 0; i < nSizeB; i++ )
341 for ( i = 0; i < nCuts; i++ )
342 if ( pCuts[i]->nLeaves <= pCuts[nCuts]->nLeaves && (pCuts[i]->Sign & pCuts[nCuts]->Sign) == pCuts[i]->Sign &&
Bal_SetCutIsContainedOrder(pCuts[nCuts], pCuts[i]) )
348 int i, k, fChanges = 0;
349 for ( i = 0; i < nCuts; i++ )
350 if ( pCuts[nCuts]->nLeaves < pCuts[i]->nLeaves && (pCuts[nCuts]->Sign & pCuts[i]->Sign) == pCuts[nCuts]->Sign &&
Bal_SetCutIsContainedOrder(pCuts[i], pCuts[nCuts]) )
354 for ( i = k = 0; i <= nCuts; i++ )
375 for ( i = nCuts; i > 0; i-- )
402 int Bal_ManDeriveCuts(
Bal_Man_t *
p,
int iFan0,
int iFan1,
int iFan2,
int fCompl0,
int fCompl1,
int fCompl2,
int fUnit0,
int fUnit1,
int fUnit2,
int fIsXor,
int Target,
int fSave )
405 Bal_Cut_t * pCutSet0, * pCutSet1, * pCutSet2;
408 Bal_Cut_t * pCut0, * pCut0Lim = pCutSet0 + nCuts0;
409 Bal_Cut_t * pCut1, * pCut1Lim = pCutSet1 + nCuts1;
410 int i, Cost, nCutsR = 0;
412 for ( i = 0; i < p->
nCutNum; i++ )
413 pCutsR[i] = pCutSet + i;
418 Bal_Cut_t * pCut2, * pCut2Lim = pCutSet2 + nCuts2;
419 for ( pCut0 = pCutSet0; pCut0 < pCut0Lim; pCut0++ )
420 for ( pCut1 = pCutSet1; pCut1 < pCut1Lim; pCut1++ )
421 for ( pCut2 = pCutSet2; pCut2 < pCut2Lim; pCut2++ )
427 assert( pCutsR[nCutsR]->Delay == Target );
437 for ( pCut0 = pCutSet0; pCut0 < pCut0Lim; pCut0++ )
438 for ( pCut1 = pCutSet1; pCut1 < pCut1Lim; pCut1++ )
444 assert( pCutsR[nCutsR]->Delay == Target );
455 Cost = ((pCutsR[0]->
Delay << 4) | pCutsR[0]->nLeaves);
457 assert( nCutsR > 0 && nCutsR < p->nCutNum );
460 if ( fSave && Cost >= 0 )
465 for ( i = 0; i < nCutsR; i++ )
466 pCutSet0[i] = *pCutsR[i];
488 int iFan0, iFan1, iFan2, Cost;
489 int fCompl0, fCompl1, fCompl2;
490 int fUnit0, fUnit1, fUnit2;
491 int Delay0, Delay1, Delay2, DelayMax;
507 fUnit0 = (int)(Delay0 != DelayMax);
508 fUnit1 = (int)(Delay1 != DelayMax);
509 fUnit2 = (int)(Delay2 != DelayMax);
513 Cost =
Bal_ManDeriveCuts(p, iFan0, iFan1, iFan2, fCompl0, fCompl1, fCompl2, fUnit0, fUnit1, fUnit2,
Gia_ObjIsXor(pObjNew), DelayMax, 1 );
519 fUnit0 = fUnit1 = fUnit2 = 1;
521 Cost =
Bal_ManDeriveCuts(p, iFan0, iFan1, iFan2, fCompl0, fCompl1, fCompl2, fUnit0, fUnit1, fUnit2,
Gia_ObjIsXor(pObjNew), DelayMax, 1 );
538 int fUnit0 = (int)(Delay0 != DelayMax);
539 int fUnit1 = (int)(Delay1 != DelayMax);
540 int fUnit2 = (int)(Delay2 != DelayMax);
543 return Bal_ManDeriveCuts(p, iFan0, iFan1, iFan2, fCompl0, fCompl1, fCompl2, fUnit0, fUnit1, fUnit2, fIsXor, DelayMax, 0 );
562 for ( i = 0; i < nSize-1; i++ )
565 for ( j = i+1; j < nSize; j++ )
568 ABC_SWAP(
int, pArray[i], pArray[best_i] );
573 int i, nSize, * pArray;
577 for ( i = nSize-1; i > 0; i-- )
581 ABC_SWAP(
int, pArray[i], pArray[i - 1] );
591 assert( DelayCur >= Delay );
592 if ( DelayCur > Delay )
615 int i, k, iBest = -1, kBest = -1, BestCost =
ABC_INFINITY, Cost;
618 for ( k = iBeg-1; k >= 0; k-- )
619 for ( i = iEnd; i >= iBeg; i-- )
629 if ( BestCost > Cost )
630 BestCost = Cost, iBest = i, kBest = k;
635 return (kBest << 16)|iBest;
639 for ( i = iBeg; i <= iEnd; i++ )
640 for ( k = i+1; k <= iEnd; k++ )
650 if ( BestCost > Cost )
651 BestCost = Cost, iBest = i, kBest = k;
656 return (kBest << 16)|iBest;
660 return (iEnd << 16)|(iEnd-1);
676 int i, k = 0, Prev = -1, This, fCompl = 0;
683 else if ( Prev != This )
696 int i, k = 0, Prev = -1, This;
705 else if ( Prev != This )
830 else if ( nLits == 2 )
836 else if ( nLits > 2 )
839 for ( i = 0; i < nLits; i++ )
877 int i, iLit, iBeg, iEnd;
931 pMan =
Bal_ManAlloc( p, pNew, nLutSize, nCutNum, fVerbose );
946 printf(
"Best delay = %d\n", nLevelMax );
static int Gia_ObjFanin2Copy(Gia_Man_t *p, Gia_Obj_t *pObj)
static int Gia_ObjToLit(Gia_Man_t *p, Gia_Obj_t *pObj)
static int Gia_ObjLevelId(Gia_Man_t *p, int Id)
static int * Vec_IntArray(Vec_Int_t *p)
void Gia_ManCreateRefs(Gia_Man_t *p)
static Gia_Obj_t * Gia_ObjChild0(Gia_Obj_t *pObj)
static int Gia_ObjFaninC2(Gia_Man_t *p, Gia_Obj_t *pObj)
static int Bal_SetCutIsContainedOrder(Bal_Cut_t *pBase, Bal_Cut_t *pCut)
typedefABC_NAMESPACE_HEADER_START struct Vec_Ptr_t_ Vec_Ptr_t
INCLUDES ///.
static int Gia_ObjIsAndReal(Gia_Man_t *p, Gia_Obj_t *pObj)
#define BAL_LEAF_MAX
DECLARATIONS ///.
void Gia_ManStop(Gia_Man_t *p)
#define Gia_ManForEachCo(p, pObj, i)
void Gia_ManPrintStats(Gia_Man_t *p, Gps_Par_t *pPars)
static int Bal_LitCost(Bal_Man_t *p, int i)
static int Gia_ObjFaninC1(Gia_Obj_t *pObj)
static word Bal_CutGetSign(int *pLeaves, int nLeaves)
static int Gia_ManAppendCo(Gia_Man_t *p, int iLit0)
int Gia_ManHashXor(Gia_Man_t *p, int iLit0, int iLit1)
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
int Bal_ManEvalTwo(Bal_Man_t *p, int iLitNew0, int iLitNew1, int iLitNew2, int fIsXor)
static void Vec_PtrFill(Vec_Ptr_t *p, int nSize, void *Entry)
static void Gia_ManBalance_rec(Gia_Man_t *pNew, Gia_Man_t *p, Gia_Obj_t *pObj)
static void Gia_ManSimplifyXor(Vec_Int_t *vSuper)
static int Bal_ManFindBestPair(Bal_Man_t *p, Vec_Int_t *vSuper, Gia_Obj_t *pObj)
static int Bal_ObjDelay(Bal_Man_t *p, int i)
static int Bal_CutCountBits(word i)
static int Gia_ManAppendCi(Gia_Man_t *p)
Gia_Man_t * Gia_ManBalanceLut(Gia_Man_t *p, int nLutSize, int nCutNum, int fVerbose)
static void Vec_PtrFreeFree(Vec_Ptr_t *p)
static int Bal_ManPrepareSet(Bal_Man_t *p, int iObj, int Index, int fUnit, Bal_Cut_t **ppCutSet)
int Bal_ManDeriveCuts(Bal_Man_t *p, int iFan0, int iFan1, int iFan2, int fCompl0, int fCompl1, int fCompl2, int fUnit0, int fUnit1, int fUnit2, int fIsXor, int Target, int fSave)
static int Bal_CutMergeOrder(Bal_Cut_t *pCut0, Bal_Cut_t *pCut1, Bal_Cut_t *pCut, int nLutSize)
static void Vec_IntPushOrderCost(Vec_Int_t *vSuper, Vec_Int_t *vCosts, int iLit)
static int Gia_ObjRefNum(Gia_Man_t *p, Gia_Obj_t *pObj)
static void Vec_PtrPush(Vec_Ptr_t *p, void *Entry)
void Gia_ManSetRegNum(Gia_Man_t *p, int nRegs)
static abctime Abc_Clock()
static int Abc_MaxInt(int a, int b)
static int Vec_PtrSize(Vec_Ptr_t *p)
static void Gia_ManSuperCollectXor_rec(Gia_Man_t *p, Gia_Obj_t *pObj)
static void Vec_IntSort(Vec_Int_t *p, int fReverse)
static Gia_Obj_t * Gia_ManObj(Gia_Man_t *p, int v)
static int Abc_LitNotCond(int Lit, int c)
#define ABC_SWAP(Type, a, b)
Gia_Man_t * Gia_ManDupNoMuxes(Gia_Man_t *p)
static Gia_Obj_t * Gia_ObjChild1(Gia_Obj_t *pObj)
#define Gia_ManForEachCi(p, pObj, i)
static int Gia_ManBalanceGate(Gia_Man_t *pNew, Gia_Obj_t *pObj, Vec_Int_t *vSuper, int *pLits, int nLits)
static void Vec_IntWriteEntry(Vec_Int_t *p, int i, int Entry)
static Gia_Man_t * Gia_ManBalanceInt(Gia_Man_t *p, int nLutSize, int nCutNum, int fVerbose)
static void Gia_ManSuperCollect(Gia_Man_t *p, Gia_Obj_t *pObj)
static int Abc_MinInt(int a, int b)
static Vec_Int_t * Vec_IntStart(int nSize)
static int Abc_LitIsCompl(int Lit)
static int Bal_SetCheckArray(Bal_Cut_t **ppCuts, int nCuts)
static Gia_Obj_t * Gia_ObjFanin0(Gia_Obj_t *pObj)
static Vec_Int_t * Vec_IntAlloc(int nCap)
FUNCTION DEFINITIONS ///.
static void Gia_ManSuperCollectAnd_rec(Gia_Man_t *p, Gia_Obj_t *pObj)
static int Bal_SetLastCutIsContained(Bal_Cut_t **pCuts, int nCuts)
static int Gia_ObjFanin1Copy(Gia_Obj_t *pObj)
static int Bal_CutCheck(Bal_Cut_t *pBase, Bal_Cut_t *pCut)
static int Vec_IntEntry(Vec_Int_t *p, int i)
unsigned __int64 word
DECLARATIONS ///.
#define ABC_NAMESPACE_IMPL_END
static void Vec_IntFill(Vec_Int_t *p, int nSize, int Fill)
void Gia_ManHashStart(Gia_Man_t *p)
static void Vec_IntSelectSortCostLit(Vec_Int_t *vSuper, Vec_Int_t *vCosts)
Gia_Man_t * Gia_ManStart(int nObjsMax)
DECLARATIONS ///.
Gia_Man_t * Gia_ManDupMuxes(Gia_Man_t *p, int Limit)
static int Gia_ObjIsXor(Gia_Obj_t *pObj)
static int Bal_CutMergeOrderMux(Bal_Cut_t *pCut0, Bal_Cut_t *pCut1, Bal_Cut_t *pCut2, Bal_Cut_t *pCut, int nLutSize)
int Gia_ManHashXorReal(Gia_Man_t *p, int iLit0, int iLit1)
static void Vec_IntPush(Vec_Int_t *p, int Entry)
void Gia_ManFillValue(Gia_Man_t *p)
static int Bal_LitDelay(Bal_Man_t *p, int i)
static int Bal_CutCreateUnit(Bal_Cut_t *p, int i, int Delay)
static void Bal_SetSortByDelay(Bal_Cut_t **pCuts, int nCuts)
static Bal_Man_t * Bal_GiaMan(Gia_Man_t *p)
static int Vec_IntRemove(Vec_Int_t *p, int Entry)
static void Gia_ManSimplifyAnd(Vec_Int_t *vSuper)
static int Gia_ObjFanin0Copy(Gia_Obj_t *pObj)
int pLeaves[BAL_LEAF_MAX]
static Gia_Obj_t * Gia_ObjFanin2(Gia_Man_t *p, Gia_Obj_t *pObj)
#define ABC_NAMESPACE_IMPL_START
static void * Vec_PtrEntry(Vec_Ptr_t *p, int i)
static int Bal_SetLastCutContains(Bal_Cut_t **pCuts, int nCuts)
static int Vec_IntEntryLast(Vec_Int_t *p)
static int Abc_LitNot(int Lit)
Bal_Man_t * Bal_ManAlloc(Gia_Man_t *pGia, Gia_Man_t *pNew, int nLutSize, int nCutNum, int fVerbose)
FUNCTION DEFINITIONS ///.
static int Vec_IntFindFirstSameDelayAsLast(Bal_Man_t *p, Vec_Int_t *vSuper)
static int Vec_IntSize(Vec_Int_t *p)
static int Bal_CutCompareArea(Bal_Cut_t *pCut0, Bal_Cut_t *pCut1)
static int Gia_IsComplement(Gia_Obj_t *p)
static Vec_Ptr_t * Vec_PtrAlloc(int nCap)
FUNCTION DEFINITIONS ///.
static void Vec_IntShrink(Vec_Int_t *p, int nSizeNew)
static int Bal_SetAddCut(Bal_Cut_t **pCuts, int nCuts, int nCutNum)
static int Gia_ObjIsAnd(Gia_Obj_t *pObj)
static Gia_Obj_t * Gia_ManConst0(Gia_Man_t *p)
void Bal_ManFree(Bal_Man_t *p)
#define ABC_CALLOC(type, num)
static int Abc_Lit2Var(int Lit)
static int Gia_ObjFaninId0p(Gia_Man_t *p, Gia_Obj_t *pObj)
static Gia_Obj_t * Gia_ObjFanin1(Gia_Obj_t *pObj)
#define ABC_INFINITY
MACRO DEFINITIONS ///.
static void Gia_ManCreateGate(Gia_Man_t *pNew, Gia_Obj_t *pObj, Vec_Int_t *vSuper)
static int Bal_ObjCost(Bal_Man_t *p, int i)
static int * Vec_IntEntryP(Vec_Int_t *p, int i)
#define Vec_IntForEachEntryStartStop(vVec, Entry, i, Start, Stop)
static int Gia_ObjFaninId2(Gia_Man_t *p, int ObjId)
int Gia_ManHashMuxReal(Gia_Man_t *p, int iLitC, int iLit1, int iLit0)
int Bal_ManSetGateLevel(Bal_Man_t *p, Gia_Obj_t *pObjOld, int iLitNew)
static int Gia_ObjFaninC0(Gia_Obj_t *pObj)
static void Vec_IntFree(Vec_Int_t *p)
static void Vec_IntClear(Vec_Int_t *p)
Gia_Man_t * Gia_ManCleanup(Gia_Man_t *p)
char * Abc_UtilStrsav(char *s)
#define Vec_IntForEachEntry(vVec, Entry, i)
MACRO DEFINITIONS ///.
static int Gia_ObjFaninId1(Gia_Obj_t *pObj, int ObjId)
static int Gia_ObjIsMux(Gia_Man_t *p, Gia_Obj_t *pObj)
int Gia_ManHashAnd(Gia_Man_t *p, int iLit0, int iLit1)
static int Gia_ManObjNum(Gia_Man_t *p)
void Gia_ManHashStop(Gia_Man_t *p)
static int Gia_ObjFaninId0(Gia_Obj_t *pObj, int ObjId)
static int Gia_ManRegNum(Gia_Man_t *p)