61 return (t &
ABC_CONST(0x00000000FFFFFFFF)) + (t>>32);
70 return n00 * n11 + n10 * n01;
75 assert( iVar >= 0 && iVar < 6 );
77 return (t &
Truths[iVar]) | ((t &
Truths[iVar]) >> (1<<iVar));
79 return (t &~
Truths[iVar]) | ((t &~
Truths[iVar]) << (1<<iVar));
86 for ( v = 0; v < 6; v++ )
118 if ( pNode->Type & 1 )
120 if ( pNode->iFan0g == 0 )
121 printf(
"%c",
'a' + pNode->iFan0n );
128 if ( pNode->Type & 4 )
133 if ( pNode->Type & 2 )
135 if ( pNode->iFan1g == 0 )
136 printf(
"%c",
'a' + pNode->iFan1n );
159 word Diff = Truth ^ pNode->Truth;
160 Extra_PrintHex( stdout, (
unsigned *)&pNode->Truth, 6 ); printf(
" " );
163 printf(
" %d\n", pNode->Weight );
179 int nSize = nCands * nCands * (nGatesMax + 1) * 5;
182 Bdc_Nod_t * pNode, * pNode0, * pNode1, * pNode2;
183 int Count0, Count1, *
pPerm;
186 assert( nGatesMax < (1<<8) );
187 assert( nCands < (1<<12) );
188 assert( (1<<(nVars-1))*(1<<(nVars-1)) < (1<<12) );
190 printf(
"Storage size = %d (%d * %d * %d * %d).\n", nSize, nCands, nCands, nGatesMax + 1, 5 );
195 if ( Truth == 0 || Truth == ~0 )
197 printf(
"Function is a constant.\n" );
200 for ( i = 0; i < nVars; i++ )
203 printf(
"Function is an elementary variable.\n" );
214 for ( i = 0; i < nVars; i++ )
215 pNode[i].Truth =
Truths[i];
216 for ( i = 0; i < nVars; i++ )
225 for ( c = i = 0; i < nVars; i++ )
226 for ( j = i+1; j < nVars; j++ )
228 pNode[c].Truth = pNode0[i].Truth & pNode0[j].Truth; pNode[c].iFan0n = i; pNode[c].iFan1n = j; pNode[c++].Type = 0;
229 pNode[c].Truth = ~pNode0[i].Truth & pNode0[j].Truth; pNode[c].iFan0n = i; pNode[c].iFan1n = j; pNode[c++].Type = 1;
230 pNode[c].Truth = pNode0[i].Truth & ~pNode0[j].Truth; pNode[c].iFan0n = i; pNode[c].iFan1n = j; pNode[c++].Type = 2;
231 pNode[c].Truth = ~pNode0[i].Truth & ~pNode0[j].Truth; pNode[c].iFan0n = i; pNode[c].iFan1n = j; pNode[c++].Type = 3;
232 pNode[c].Truth = pNode0[i].Truth ^ pNode0[j].Truth; pNode[c].iFan0n = i; pNode[c].iFan1n = j; pNode[c++].Type = 4;
234 assert( c == 5 * nVars * (nVars - 1) / 2 );
237 for ( i = 0; i < c; i++ )
241 if ( Truth == pNode[i].Truth || Truth == ~pNode[i].Truth )
243 printf(
"Function can be implemented using 1 gate.\n" );
248 printf(
"Selected %6d gates on level %2d. ", c, 1 );
254 for ( n = 2; n <= nGatesMax; n++ )
261 for ( k = 0; k < n-1; k++ )
265 for ( i = 0; i < Count0; i++ )
266 for ( j = 0; j < Count1; j++ )
268 pNode[c].Truth = pNode0[i].Truth & pNode1[j].Truth; pNode[c].iFan0g = k; pNode[c].iFan1g = n-1; pNode[c].iFan0n = i; pNode[c].iFan1n = j; pNode[c++].Type = 0;
269 pNode[c].Truth = ~pNode0[i].Truth & pNode1[j].Truth; pNode[c].iFan0g = k; pNode[c].iFan1g = n-1; pNode[c].iFan0n = i; pNode[c].iFan1n = j; pNode[c++].Type = 1;
270 pNode[c].Truth = pNode0[i].Truth & ~pNode1[j].Truth; pNode[c].iFan0g = k; pNode[c].iFan1g = n-1; pNode[c].iFan0n = i; pNode[c].iFan1n = j; pNode[c++].Type = 2;
271 pNode[c].Truth = ~pNode0[i].Truth & ~pNode1[j].Truth; pNode[c].iFan0g = k; pNode[c].iFan1g = n-1; pNode[c].iFan0n = i; pNode[c].iFan1n = j; pNode[c++].Type = 3;
272 pNode[c].Truth = pNode0[i].Truth ^ pNode1[j].Truth; pNode[c].iFan0g = k; pNode[c].iFan1g = n-1; pNode[c].iFan0n = i; pNode[c].iFan1n = j; pNode[c++].Type = 4;
277 for ( i = 0; i < Count1; i++ )
278 for ( j = i+1; j < Count1; j++ )
280 pNode[c].Truth = pNode1[i].Truth & pNode1[j].Truth; pNode[c].iFan0g = n-1; pNode[c].iFan1g = n-1; pNode[c].iFan0n = i; pNode[c].iFan1n = j; pNode[c++].Type = 0;
281 pNode[c].Truth = ~pNode1[i].Truth & pNode1[j].Truth; pNode[c].iFan0g = n-1; pNode[c].iFan1g = n-1; pNode[c].iFan0n = i; pNode[c].iFan1n = j; pNode[c++].Type = 1;
282 pNode[c].Truth = pNode1[i].Truth & ~pNode1[j].Truth; pNode[c].iFan0g = n-1; pNode[c].iFan1g = n-1; pNode[c].iFan0n = i; pNode[c].iFan1n = j; pNode[c++].Type = 2;
283 pNode[c].Truth = ~pNode1[i].Truth & ~pNode1[j].Truth; pNode[c].iFan0g = n-1; pNode[c].iFan1g = n-1; pNode[c].iFan0n = i; pNode[c].iFan1n = j; pNode[c++].Type = 3;
284 pNode[c].Truth = pNode1[i].Truth ^ pNode1[j].Truth; pNode[c].iFan0g = n-1; pNode[c].iFan1g = n-1; pNode[c].iFan0n = i; pNode[c].iFan1n = j; pNode[c++].Type = 4;
289 for ( i = 0; i < c; i++ )
292 if ( pNode[i].Weight > 300 )
296 if ( Truth == pNode[i].Truth || Truth == ~pNode[i].Truth )
298 printf(
"Function can be implemented using %d gates.\n", n );
306 printf(
"Best SPFD = %d.\n",
Vec_IntEntry(vWeight, pPerm[c-1]) );
312 for ( j = 0, i = c-1; i >= 0; i-- )
314 pNode2[j++] = pNode[pPerm[i]];
322 printf(
"Selected %6d gates (out of %6d) on level %2d. ", j, c, n );
325 for ( i = 0; i < 10; i++ )
414 #define BDC_TERM 0x1FFFFFFF
530 static unsigned BigPrimes[8] = {12582917, 25165843, 50331653, 100663319, 201326611, 402653189, 805306457, 1610612741};
531 unsigned char * s = (
unsigned char *)&t;
532 unsigned i, Value = 0;
533 for ( i = 0; i < 8; i++ )
534 Value ^= BigPrimes[i] * s[i];
552 if ( pBin->
iList == 0 )
554 for ( pBin = p + pBin->
iList; ; pBin = p + pBin->
iNext )
556 if ( pBin->
Truth == t )
558 if ( pBin->
iNext == 0 )
586 int nFuncs = 250000000;
587 int nSize = 201326611;
590 int * pPlace, i, n, m, k, s, fCompl;
595 Bdc_Ent_t *
p, * q, * pBeg0, * pEnd0, * pBeg1, * pEnd1, * pThis0, * pThis1;
597 assert( nSize <= nFuncs );
599 printf(
"Allocating %.2f MB of internal memory.\n", 1.0*
sizeof(
Bdc_Ent_t)*nFuncs/(1<<20) );
604 for ( q = p; q < p+nFuncs; q++ )
607 printf(
"Added %d + %d + 0 = %d. Total = %8d.\n", 0, 0, 0, (
int)(q-p) );
617 for ( i = 0; i < 6; i++ )
629 printf(
"Added %d + %d + 0 = %d. Total = %8d.\n", 0, 0, 0, (
int)(q-p) );
632 for ( n = 0; n < Limit; n++ )
635 for ( k = 0; k < Limit; k++ )
636 for ( m = 0; m < Limit; m++ )
638 if ( k + m != n || k > m )
648 printf(
"Trying %7d x %7d. ", (
int)(pEnd0-pBeg0), (
int)(pEnd1-pBeg1) );
649 for ( pThis0 = pBeg0; pThis0 < pEnd0; pThis0++ )
650 for ( pThis1 = pBeg1; pThis1 < pEnd1; pThis1++ )
651 if ( k < m || pThis1 > pThis0 )
653 for ( s = 0; s < 5; s++ )
655 t0 = (s&1) ? ~pThis0->Truth : pThis0->Truth;
656 t1 = ((s>>1)&1) ? ~pThis1->
Truth : pThis1->
Truth;
657 t = ((s>>2)&1) ? t0 ^ t1 : t0 & t1;
664 if ( pPlace == NULL )
680 printf(
"Reached limit of %d functions.\n", nFuncs );
684 printf(
"Added %d + %d + 1 = %d. Total = %8d. ", k, m, n+1, (
int)(q-p) );
693 FILE * pFile = fopen(
"func6v6n_bin.txt",
"wb" );
698 FILE * pFile = fopen(
"func6v6nW_bin.txt",
"wb" );
708 *pvWeights = vWeights;
733 pFile = fopen(
"func6v5n_bin.txt",
"rb" );
738 pFile = fopen(
"func6v5nW_bin.txt",
"rb" );
742 *pvWeights = vWeights;
761 FILE * pFile = fopen(
"func6v6n_bin.txt",
"rb" );
767 pFile = fopen(
"func6v6nW_bin.txt",
"rb" );
771 *pvWeights = vWeights;
809 int i, Cost, CostBest = -1, NumBest = -1;
812 if ( (Func & F0) == 0 )
815 if ( CostBest < Cost )
822 if ( (Func & F1) == 0 )
825 if ( CostBest < Cost )
832 if ( (~Func & F0) == 0 )
835 if ( CostBest < Cost )
842 if ( (~Func & F1) == 0 )
845 if ( CostBest < Cost )
855 printf(
"Selected %8d with cost %2d and weight %d: ", NumBest, 0,
Vec_IntEntry(vWeights, NumBest) );
856 Extra_PrintHex( stdout, (
unsigned *)&FuncBest, 6 ); printf(
"\n" );
877 printf(
"Trying: " );
880 for ( i = 0; F0 && F1; i++ )
882 printf(
"*** ITER %2d ", i );
889 printf(
"Produce solution with cost %2d (with adj cost %4d).\n", Cost,
Bdc_SpfdAdjCost(t) );
922 word c0, c1, s, tt, tbest;
923 int i, j, Cost, CostBest = 100000;
938 if ( CostBest > Cost )
946 for ( i = 0; i < 6; i++ )
950 if ( CostBest > Cost )
959 for ( i = 0; i < 6; i++ )
960 for ( j = 0; j < 6; j++ )
967 tt = (~s & c0) | (s & c1);
970 if ( CostBest > Cost )
1008 printf(
"Best solution found with cost %d. ", CostBest );
1029 int nSizeM = (1 << 26);
1030 int nSizeK = (1 << 3);
1039 for ( i = 0; i < nSizeM; i++ )
1043 for ( i = 0; i < nSizeK; i++ )
1048 for ( i = 0; i < nSizeM; i++ )
1049 for ( k = 0; k < nSizeK; k++ )
1050 Counter += ((v1M->pArray[i] & v1K->pArray[k]) == v1K->pArray[k]);
1055 printf(
"Total = %8d. ", Counter );
1060 for ( k = 0; k < nSizeK; k++ )
1061 for ( i = 0; i < nSizeM; i++ )
1062 Counter += ((v1M->pArray[i] & v1K->pArray[k]) == v1K->pArray[k]);
1063 printf(
"Total = %8d. ", Counter );
1087 word Func, FuncBest;
1103 if ( CostBest > Cost )
1110 printf(
"Best cost = %4d. ", CostBest );
1119 Extra_PrintHex( stdout, (
unsigned *)&FuncBest, 6 ); printf(
"\n" );
1136 int nSizeM = (1 << 26);
1137 int nSizeK = (1 << 3);
1146 for ( i = 0; i < nSizeM; i++ )
1150 for ( i = 0; i < nSizeK; i++ )
1160 Counter += ((EntryM & EntryK) == EntryK);
1161 printf(
"Total = %8d. ", Counter );
1171 Counter += ((EntryM & EntryK) == EntryK);
1172 printf(
"Total = %8d. ", Counter );
static int * Vec_IntArray(Vec_Int_t *p)
void Bdc_SpfdUnmark1(Bdc_Ent_t *p, Bdc_Ent_t *pEnt)
typedefABC_NAMESPACE_HEADER_START struct Vec_Ptr_t_ Vec_Ptr_t
INCLUDES ///.
static int Bdc_CountSpfd(word t, word f)
static word Bdc_Cof6(word t, int iVar, int fCof1)
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
void Bdc_SpfdPrint(Bdc_Nod_t *pNode, int Level, Vec_Ptr_t *vLevels, word Truth)
static void Vec_WrdPush(Vec_Wrd_t *p, word Entry)
void Bdc_SpfdDecomposeTest()
int Bdc_SpfdHashValue(word t, int Size)
void Bdc_SpfdPrint_rec(Bdc_Nod_t *pNode, int Level, Vec_Ptr_t *vLevels)
FUNCTION DEFINITIONS ///.
static void Vec_PtrPush(Vec_Ptr_t *p, void *Entry)
word Aig_ManRandom64(int fReset)
Vec_Wrd_t * Bdc_SpfdReadFiles5(Vec_Int_t **pvWeights)
int * Bdc_SpfdHashLookup(Bdc_Ent_t *p, int Size, word t)
static abctime Abc_Clock()
static int Vec_WrdSize(Vec_Wrd_t *p)
#define Vec_WrdForEachEntry(vVec, Entry, i)
MACRO DEFINITIONS ///.
int Bdc_SpfdMark1(Bdc_Ent_t *p, Bdc_Ent_t *pEnt)
void Abc_Show6VarFunc(word F0, word F1)
void Bdc_SpfdDecompose(word Truth, int nVars, int nCands, int nGatesMax)
void Bdc_SpfdDecomposeTest44()
void Bdc_SpfdDecomposeTest3()
static int Bdc_CountOnes(word t)
static void Abc_PrintTime(int level, const char *pStr, abctime time)
int * Abc_MergeSortCost(int *pCosts, int nSize)
static Vec_Int_t * Vec_IntStart(int nSize)
static Vec_Int_t * Vec_IntAlloc(int nCap)
FUNCTION DEFINITIONS ///.
int Bdc_SpfdComputeCost(word f, int i, Vec_Int_t *vWeights)
static void Vec_WrdClear(Vec_Wrd_t *p)
static int Vec_IntEntry(Vec_Int_t *p, int i)
void Bdc_SpfdDecomposeTest8()
unsigned __int64 word
DECLARATIONS ///.
#define ABC_NAMESPACE_IMPL_END
static void Vec_WrdFree(Vec_Wrd_t *p)
int Bdc_SpfdDecomposeTestOne(word t, Vec_Wrd_t *vDivs, Vec_Int_t *vWeights)
int Bdc_SpfdCheckOverlap(Bdc_Ent_t *p, Bdc_Ent_t *pEnt0, Bdc_Ent_t *pEnt1)
void Bdc_SpfdUnmark0(Bdc_Ent_t *p, Bdc_Ent_t *pEnt)
static void Vec_IntPush(Vec_Int_t *p, int Entry)
void Bdc_SpfdDecomposeTest_()
static Vec_Wrd_t * Vec_WrdAlloc(int nCap)
FUNCTION DEFINITIONS ///.
#define ABC_NAMESPACE_IMPL_START
int Bdc_SpfdMark0(Bdc_Ent_t *p, Bdc_Ent_t *pEnt)
static void * Vec_PtrEntry(Vec_Ptr_t *p, int i)
static Vec_Wrd_t * Vec_WrdStart(int nSize)
int Bdc_SpfdAdjCost(word t)
static ABC_NAMESPACE_IMPL_START word Truth[8]
DECLARATIONS ///.
static int Vec_IntSize(Vec_Int_t *p)
word Bdc_SpfdFindBest(Vec_Wrd_t *vDivs, Vec_Int_t *vWeights, word F0, word F1, int *pCost)
static word * Vec_WrdArray(Vec_Wrd_t *p)
static Vec_Ptr_t * Vec_PtrAlloc(int nCap)
FUNCTION DEFINITIONS ///.
#define ABC_CONST(number)
PARAMETERS ///.
#define ABC_CALLOC(type, num)
Vec_Wrd_t * Bdc_SpfdDecomposeTest__(Vec_Int_t **pvWeights)
#define ABC_INFINITY
MACRO DEFINITIONS ///.
Vec_Wrd_t * Bdc_SpfdReadFiles6(Vec_Int_t **pvWeights)
static void Vec_IntFree(Vec_Int_t *p)
#define Vec_PtrForEachEntry(Type, vVec, pEntry, i)
MACRO DEFINITIONS ///.
static void Vec_IntClear(Vec_Int_t *p)
typedefABC_NAMESPACE_HEADER_START struct Vec_Wrd_t_ Vec_Wrd_t
INCLUDES ///.
typedefABC_NAMESPACE_IMPL_START struct Bdc_Nod_t_ Bdc_Nod_t
DECLARATIONS ///.
static void Vec_PtrFree(Vec_Ptr_t *p)