32# define __builtin_popcount __popcnt
77 int i, Res = 0, nDigits = 1 << (nVars - 1);
78 for ( i = 0; i < nDigits; i++ )
79 Counts[Abc_TtGetQua(
p, i)]++;
80 for ( i = 0; i < 4; i++ )
87 int i, Res = 0, nDigits = 1 << (nVars - 2);
88 for ( i = 0; i < nDigits; i++ )
89 Counts[Abc_TtGetHex(
p, i)]++;
90 for ( i = 0; i < 16; i++ )
96 unsigned char * q = (
unsigned char *)
p;
97 int i, Digit, nDigits = 1 << (nVars - 3);
98 Vec_IntClear( vUsed );
99 for ( i = 0; i < nDigits; i++ ) {
100 if ( pCounts[q[i]] == 1 )
103 Vec_IntPush(vUsed, q[i]);
107 return Vec_IntSize(vUsed);
111 unsigned short * q = (
unsigned short *)
p;
112 int i, Digit, nDigits = 1 << (nVars - 4);
113 Vec_IntClear( vUsed );
114 for ( i = 0; i < nDigits; i++ ) {
115 if ( pCounts[q[i]] == 1 )
118 Vec_IntPush(vUsed, q[i]);
122 return Vec_IntSize(vUsed);
128 int i = 0;
unsigned hash = 0;
138 return (
int)(hash % nTableSize);
142 int Key =
Abc_TtGetKey( (
unsigned char*)&Entry, 4, Vec_IntSize(vTable) );
143 int * pTable = Vec_IntArray(vTable);
144 for ( ; pTable[Key] >= 0; Key = (Key + 1) % Vec_IntSize(vTable) )
145 if ( Entry == (
int)Vec_WrdEntry(vStore, pTable[Key]) )
147 assert( pTable[Key] == -1 );
148 pTable[Key] = Vec_WrdSize(vStore);
149 Vec_WrdPush( vStore, (
word)Entry );
150 Vec_IntPush( vUsed, Key );
155 unsigned * q = (
unsigned *)
p;
156 int i, Item, nDigits = 1 << (nVars - 5);
157 Vec_WrdClear( vStore );
158 Vec_IntClear( vUsed );
159 for ( i = 0; i < nDigits; i++ )
162 Vec_IntWriteEntry( vTable, Item, -1 );
163 return Vec_IntSize(vUsed);
168 int * pTable = Vec_IntArray(vTable);
169 for ( ; pTable[Key] >= 0; Key = (Key + 1) % Vec_IntSize(vTable) )
172 assert( pTable[Key] == -1 );
173 pTable[Key] = Vec_WrdSize(vStore)/
nWords;
174 for ( i = 0; i <
nWords; i++ )
175 Vec_WrdPush( vStore, pEntry[i] );
176 Vec_IntPush( vUsed, Key );
181 assert( nFVars >= 6 && nFVars < nVars );
182 int i, Item, nDigits = 1 << (nVars - nFVars),
nWords = 1 << (nFVars - 6);
183 Vec_WrdClear( vStore );
184 Vec_IntClear( vUsed );
186 for ( i = 0; i < nDigits; i++ )
189 Vec_IntWriteEntry( vTable, Item, -1 );
190 return Vec_IntSize(vUsed);
199 return Abc_TtGetCM3(
p, nVars, Vec_IntArray(vCounts), vUsed );
201 return Abc_TtGetCM4(
p, nVars, Vec_IntArray(vCounts), vUsed );
205 return Abc_TtGetCM6(
p, nVars, nFVars, vTable, vStore, vUsed );
223 int nUsed = 0, pUsed[4], pMap[4];
224 int i, nDigits = 1 << (nVars - 1), nWordsBS = Abc_TtWordNum(nVars - 1);
226 for ( i = 0; i < nDigits; i++ ) {
227 int Digit = Abc_TtGetQua(
p, i);
228 if ( pMap[Digit] == -1 ) {
230 pUsed[nUsed++] = Digit;
232 if ( pPat ) Abc_TtSetBit( pPat + nWordsBS*pMap[Digit], i );
238 int nUsed = 0, pUsed[16], pMap[16];
239 int i, nDigits = 1 << (nVars - 2), nWordsBS = Abc_TtWordNum(nVars - 2);
241 for ( i = 0; i < nDigits; i++ ) {
242 int Digit = Abc_TtGetHex(
p, i);
243 if ( pMap[Digit] == -1 ) {
245 pUsed[nUsed++] = Digit;
247 if ( pPat ) Abc_TtSetBit( pPat + nWordsBS*pMap[Digit], i );
253 unsigned char * q = (
unsigned char *)
p;
254 int i, Digit, nDigits = 1 << (nVars - 3), nWordsBS = Abc_TtWordNum(nVars - 3);
255 Vec_IntClear( vUsed );
256 for ( i = 0; i < nDigits; i++ ) {
257 if ( pMap[q[i]] == -1 ) {
258 pMap[q[i]] = Vec_IntSize(vUsed);
259 Vec_IntPush(vUsed, q[i]);
261 if ( pPat ) Abc_TtSetBit( pPat + nWordsBS*pMap[q[i]], i );
265 return Vec_IntSize(vUsed);
269 unsigned short * q = (
unsigned short *)
p;
270 int i, Digit, nDigits = 1 << (nVars - 4), nWordsBS = Abc_TtWordNum(nVars - 4);
271 Vec_IntClear( vUsed );
272 for ( i = 0; i < nDigits; i++ ) {
273 if ( pMap[q[i]] == -1 ) {
274 pMap[q[i]] = Vec_IntSize(vUsed);
275 Vec_IntPush(vUsed, q[i]);
277 if ( pPat ) Abc_TtSetBit( pPat + nWordsBS*pMap[q[i]], i );
281 return Vec_IntSize(vUsed);
285 unsigned * q = (
unsigned *)
p;
286 int i, Item, nDigits = 1 << (nVars - 5), nWordsBS = Abc_TtWordNum(nVars - 5);
287 Vec_WrdClear( vStore );
288 Vec_IntClear( vUsed );
290 for ( i = 0; i < nDigits; i++ )
291 Abc_TtSetBit( pPat + nWordsBS*
Abc_TtHashLookup5(q[i], vTable, vStore, vUsed), i );
293 for ( i = 0; i < nDigits; i++ )
296 Vec_IntWriteEntry( vTable, Item, -1 );
297 return Vec_IntSize(vUsed);
301 assert( nFVars >= 6 && nFVars < nVars );
302 int i, Item, nDigits = 1 << (nVars - nFVars),
nWords = 1 << (nFVars - 6), nWordsBS = Abc_TtWordNum(nVars - nFVars);
303 Vec_WrdClear( vStore );
304 Vec_IntClear( vUsed );
306 for ( i = 0; i < nDigits; i++ )
309 for ( i = 0; i < nDigits; i++ )
312 Vec_IntWriteEntry( vTable, Item, -1 );
313 return Vec_IntSize(vUsed);
318 printf(
"ACD i-sets with %d variables and column multiplicity %d:\n", nVars, nMyu );
319 for (
int m = 0; m < nMyu; m++ )
327 int nRails = Abc_Base2Log(nMyu);
328 int nMyuMax = 1 << (nRails - 1);
329 for (
int v = 0; v < nVars - nFVars; v++ ) {
330 int m, n, Counts[2] = {0};
331 for ( n = 0; n < 2; n++ ) {
332 for ( m = 0; m < nMyu; m++ )
333 if ( (Counts[n] += ((s_Truth26[n][v] & pPat[m]) != 0)) > nMyuMax )
338 if ( fVerbose ) printf(
"%d : %2d %2d %2d\n", v, Counts[0], Counts[1], nMyuMax );
345 if ( fVerbose ) printf(
"Not found\n" );
350 int nMintsBS = 1 << (nVars - nFVars);
351 int nWordsBS = Abc_TtWordNum(nVars - nFVars);
353 memset( pPat, 0, 8 * nMintsBS * nWordsBS );
357 else if ( nFVars == 2 )
359 else if ( nFVars == 3 )
361 else if ( nFVars == 4 )
363 else if ( nFVars == 5 )
365 else if ( nFVars >= 6 )
373 int nRails, nMyu =
Abc_TtGetCMInt(
p, nVars, nFVars, vCounts, vTable, vStore, vUsed, NULL );
383 return Abc_TtGetCMPat(
p, nVars, nFVars, vCounts, vTable, vStore, vUsed );
399static void Abc_TtPermGen(
int * currPerm,
int nVars,
word * pT,
int nTtVars )
402 while ( i >= 0 && currPerm[i - 1] >= currPerm[i] )
407 while ( j > i && currPerm[j - 1] <= currPerm[i - 1 ])
409 ABC_SWAP(
int, currPerm[i - 1], currPerm[j - 1] )
410 if ( pT ) Abc_TtSwapVars( pT, nTtVars, i-1, j-1 );
415 ABC_SWAP(
int, currPerm[i - 1], currPerm[j - 1] )
416 if ( pT ) Abc_TtSwapVars( pT, nTtVars, i-1, j-1 );
422static
int Abc_TtFactorial(
int nVars)
425 for ( i = 1; i <= nVars; i++ )
431 int i, k, nVars = 5, currPerm[5] = {0};
432 for ( i = 0; i < nVars; i++ )
434 int fact = Abc_TtFactorial( nVars );
435 for ( i = 0; i < fact; i++ )
437 printf(
"%3d :", i );
438 for ( k = 0; k < nVars; k++ )
439 printf(
" %d", currPerm[k] );
441 Abc_TtPermGen( currPerm, nVars, NULL, 0 );
460 for (j = *r; !w[j]; j++) {
463 if (b < (w[j + 1] ? n - (2 - (n & 1)) : n)) {
464 if ((b & 1) == 0 && b + 1 < n) b++;
466 if (!found_r) *r = j > 1 ? j - 1 : 0;
469 w[j] = a[j] - 1 >= j;
470 if (w[j] && !found_r) {
476 if ((b & 1) != 0 && b - 1 >= j) b--;
479 if (!found_r) *r = j;
483 Vec_Int_t * vPairs = Vec_IntAlloc( 100 );
484 int j, z, r = 0, Count = 0, a[32], b[32], w[32];
485 for ( j = 0; j < T + 1; j++ ) {
490 for ( z = 0; z <= T; z++ )
493 for ( z = 0; z < T; z++ ) {
496 Vec_IntPushTwo( vPairs, b[z], a[z] );
498 printf(
"%3d : ", Count++ );
499 for (j = T - 1; j > -1; j--) printf(
"%x", b[j]);
500 printf(
" %d <-> %d ", b[z], a[z] );
501 for (j = T - 1; j > -1; j--) printf(
"%x", a[j]);
507 Vec_IntPushTwo( vPairs, 0, 0 );
513 printf(
"%3d : ", Count++ );
514 for ( k = nVars-1; k >= nFVars; k-- )
515 printf(
"%d", pPerm[k] );
517 for ( k = nFVars-1; k >= 0; k-- )
518 printf(
"%d", pPerm[k] );
519 printf(
" %d <-> %d\n",
Var0,
Var1 );
526 int i,
Var0,
Var1, Pla2Var[32], Var2Pla[32];
527 for ( i = 0; i < nVars; i++ )
528 Pla2Var[i] = Var2Pla[i] = i;
532 int iPlace0 = Var2Pla[
Var0];
533 int iPlace1 = Var2Pla[
Var1];
535 Var2Pla[Pla2Var[iPlace0]] = iPlace1;
536 Var2Pla[Pla2Var[iPlace1]] = iPlace0;
537 Pla2Var[iPlace0] ^= Pla2Var[iPlace1];
538 Pla2Var[iPlace1] ^= Pla2Var[iPlace0];
539 Pla2Var[iPlace0] ^= Pla2Var[iPlace1];
541 Vec_IntFree( vPairs );
559 p->vCounts = Vec_IntStartFull( 1 << 16 );
560 p->vTable = Vec_IntStartFull( 997 );
561 p->vUsed = Vec_IntAlloc(100);
562 p->vStore = Vec_WrdAlloc(1000);
569 Vec_IntFreeP( &
p->vPairs[i][k] );
570 Vec_IntFree(
p->vCounts );
571 Vec_IntFree(
p->vTable );
572 Vec_IntFree(
p->vUsed );
573 Vec_WrdFree(
p->vStore );
575 Vec_WecFreeP( &
p->vSets[i] );
577 Vec_WrdFreeP( &
p->vCofs[i] );
585 if (
p->nVars != nVars ||
p->nLVars != nLVars ) {
588 if (
p->vPairs[
p->nVars][
p->nLVars] == NULL )
591 int Best =
Abc_TtGetCM( pT, nVars, nVars-nLVars,
p->vCounts,
p->vTable,
p->vStore,
p->vUsed, 0 );
592 printf(
"Function: " );
Extra_PrintHex( stdout, (
unsigned *)pT, nVars ); printf(
"\n" );
593 printf(
"The column multiplicity of the %d-var function with bound-sets of size %d is %d.\n", nVars, nLVars, Best );
596int Abc_BSEvalBest(
Abc_BSEval_t *
p,
word * pIn,
word * pBest,
word * pBest2,
int nVars,
int nCVars,
int nFVars,
int fVerbose,
int * pPermBest,
int * pPermBest2,
int fShared,
int nJRatio )
598 int i, k,
Var0,
Var1, Pla2Var[32], Var2Pla[32];
599 int nPermVars = nVars-nCVars, Count = 0;
600 assert(
p->nVars == nPermVars &&
p->nLVars == nVars-nFVars );
601 for ( i = 0; i < nVars; i++ )
602 Pla2Var[i] = Var2Pla[i] = i;
604 for ( i = 0; i < nVars; i++ )
606 int CostBest = 1 << nVars;
607 int CostBest2 = 1 << nVars;
608 int iSave = nJRatio ? (
Abc_Random(0) % Vec_IntSize(
p->vPairs[
p->nVars][
p->nLVars])/2) : -1;
613 int CostThis =
Abc_TtGetCM( pIn, nVars, nFVars,
p->vCounts,
p->vTable,
p->vStore,
p->vUsed, fShared );
614 if ( iSave == i/2 ) {
615 CostBest2 = CostThis;
616 if ( pBest2 ) Abc_TtCopy( pBest2, pIn, Abc_Truth6WordNum(nVars), 0 );
617 if ( pPermBest2 )
memcpy( pPermBest2, Pla2Var,
sizeof(
int)*nVars );
619 if ( CostBest > CostThis ) {
621 if ( pBest ) Abc_TtCopy( pBest, pIn, Abc_Truth6WordNum(nVars), 0 );
622 if ( pPermBest )
memcpy( pPermBest, Pla2Var,
sizeof(
int)*nVars );
625 else if ( CostBest == CostThis && (
Abc_Random(0) % ++Count) == 0 ) {
626 if ( pBest ) Abc_TtCopy( pBest, pIn, Abc_Truth6WordNum(nVars), 0 );
627 if ( pPermBest )
memcpy( pPermBest, Pla2Var,
sizeof(
int)*nVars );
631 printf(
"%3d : ", i/2 );
632 for ( k = nCVars-1; k >= 0; k-- )
633 printf(
" %d", nVars-nCVars+k );
635 for ( k = nPermVars-1; k >= nFVars; k-- )
636 printf(
" %d", Pla2Var[k] );
638 for ( k = nFVars-1; k >= 0; k-- )
639 printf(
" %d", Pla2Var[k] );
640 printf(
" : Myu = %3d", CostThis );
645 int nRails = 1, Shared = 0, nSetSize = 0;
646 if ( CostThis > (1 << nRails) ) {
647 extern int Abc_SharedEvalBest(
Abc_BSEval_t *
p,
word * pTruth,
int nVars,
int nCVars,
int nFVars,
int MyuMin,
int nRails,
int fVerbose,
int * pSetShared,
int * pSetSize,
word * pPat );
648 int nRailsMin =
Abc_SharedEvalBest(
p, pIn, nVars, nCVars, nFVars, CostThis, nRails, 0, &Shared, &nSetSize,
p->pPat );
649 printf(
" RailMin = %d. Shared = %2d. ", nRailsMin, Shared );
655 int iPlace0 = Var2Pla[
Var0];
656 int iPlace1 = Var2Pla[
Var1];
657 if ( iPlace0 == iPlace1 )
659 Abc_TtSwapVars( pIn, nVars, iPlace0, iPlace1 );
660 Var2Pla[Pla2Var[iPlace0]] = iPlace1;
661 Var2Pla[Pla2Var[iPlace1]] = iPlace0;
662 Pla2Var[iPlace0] ^= Pla2Var[iPlace1];
663 Pla2Var[iPlace1] ^= Pla2Var[iPlace0];
664 Pla2Var[iPlace0] ^= Pla2Var[iPlace1];
666 for ( i = 0; i < nPermVars; i++ )
669 int iPlace1 = Var2Pla[i];
670 if ( iPlace0 == iPlace1 )
672 Abc_TtSwapVars( pIn, nVars, iPlace0, iPlace1 );
673 Var2Pla[Pla2Var[iPlace0]] = iPlace1;
674 Var2Pla[Pla2Var[iPlace1]] = iPlace0;
675 Pla2Var[iPlace0] ^= Pla2Var[iPlace1];
676 Pla2Var[iPlace1] ^= Pla2Var[iPlace0];
677 Pla2Var[iPlace0] ^= Pla2Var[iPlace1];
679 if ( nJRatio &&
Abc_Random(0) % nJRatio == 0 ) {
680 CostBest = CostBest2;
681 if ( pBest2 ) Abc_TtCopy( pBest, pBest2, Abc_Truth6WordNum(nVars), 0 );
682 if ( pPermBest2 )
memcpy( pPermBest, pPermBest2,
sizeof(
int)*nVars );
690 if (
p->nVars != nVars ||
p->nLVars != nLVars ) {
693 if (
p->vPairs[
p->nVars][
p->nLVars] == NULL )
698 int Best =
Abc_BSEvalBest(
p, pIn, pFun, pFun2, nVars, 0, nVars-nLVars, fVerbose, pPerm, pPerm2, fShared, 0 );
699 printf(
"The minimum %s of the %d-var function with bound-sets of size %d is %d.\n",
700 fShared ?
"number of rails" :
"column multiplicity", nVars, nLVars, Best );
701 printf(
"Original: " );
Extra_PrintHex( stdout, (
unsigned *)pIn, nVars ); printf(
"\n" );
702 printf(
"Permuted: " );
Extra_PrintHex( stdout, (
unsigned *)pFun, nVars ); printf(
"\n" );
703 printf(
"Permutation is " );
704 for ( i = 0; i < nVars; i++ )
705 printf(
"%d ", pPerm[i] );
723void Abc_BSEvalBestGen(
int nVars,
int nLVars,
int nFuncs,
int nMints,
int fTryAll,
int fShared,
int fVerbose )
726 abctime clkTotal = Abc_Clock();
728 Vec_Int_t * vCounts[2] = { Vec_IntStart(1 << nVars), Vec_IntStart(1 << nVars) };
729 int i, k, Count,
nWords = Abc_TtWordNum(nVars);
732 if (
p->nVars != nVars ||
p->nLVars != nLVars ) {
735 if (
p->vPairs[
p->nVars][
p->nLVars] == NULL )
739 for ( i = 0; i < nFuncs; i++ ) {
741 for ( k = 0; k <
nWords; k++ )
744 Abc_TtClear( pFun,
nWords );
745 for ( k = 0; k < nMints; k++ ) {
748 while ( Abc_TtGetBit(pFun, iMint) );
749 Abc_TtSetBit( pFun, iMint );
753 printf(
"Function %5d ", i );
755 printf(
"with %d positive minterms ", nMints );
757 printf(
"has truth table: " );
767 Count =
Abc_BSEvalBest(
p, pFun, pFun2, NULL, nVars, 0, nVars-nLVars, fVerbose, NULL, NULL, fShared, 0 );
769 Count =
Abc_TtGetCM( pFun, nVars, nVars-nLVars,
p->vCounts,
p->vTable,
p->vStore,
p->vUsed, fShared );
771 printf(
"Myu = %d\n", Count );
772 Vec_IntAddToEntry( vCounts[0], Count, 1 );
773 Vec_IntAddToEntry( vCounts[1], Abc_Base2Log(Count), 1 );
779 printf(
"Generated %d random %d-var functions with %d positive minterms.\n", nFuncs, nVars, nMints );
781 printf(
"Generated %d random %d-var functions.\n", nFuncs, nVars );
783 printf(
"Distribution of the %s number of rails for bound set size %d with one shared variable:\n", fTryAll ?
"MINIMUM":
"ORIGINAL", nLVars );
784 assert( Vec_IntSum(vCounts[0]) == nFuncs );
786 if ( Count ) printf(
"%d=%d (%.2f %%) ", i, Count, 100.0*Count/nFuncs );
790 printf(
"Distribution of the %s column multiplicity for bound set size %d with no shared variables:\n", fTryAll ?
"MINIMUM":
"ORIGINAL", nLVars );
791 assert( Vec_IntSum(vCounts[0]) == nFuncs );
793 if ( Count ) printf(
"%d=%d (%.2f %%) ", i, Count, 100.0*Count/nFuncs );
795 printf(
"Distribution of the %s number of rails for bound set size %d with no shared variables:\n", fTryAll ?
"MINIMUM":
"ORIGINAL", nLVars );
796 assert( Vec_IntSum(vCounts[1]) == nFuncs );
798 if ( Count ) printf(
"%d=%d (%.2f %%) ", i, Count, 100.0*Count/nFuncs );
801 Vec_IntFree( vCounts[0] );
802 Vec_IntFree( vCounts[1] );
803 Abc_PrintTime( 1,
"Total runtime", Abc_Clock() - clkTotal );
820 int nWords = Abc_Truth6WordNum(nVars);
821 int m, i, nUsed = 0, pUsed[32], Start = Vec_WrdSize(vCofs);
822 for ( i = 0; i < nVars; i++ )
823 if ( (iSet >> i) & 1 )
825 Vec_WrdFillExtra( vCofs, Start +
nWords*(1 << nUsed), ~(
word)0 );
826 for ( m = 0; m < (1 << nUsed); m++ )
828 word * pCof = Vec_WrdEntryP(vCofs, Start + m*
nWords);
829 for ( i = 0; i < nUsed; i++ )
830 Abc_TtAndSharp( pCof, pCof, Vec_WrdEntryP(vElems,
nWords*pUsed[i]),
nWords, ((m >> i) & 1) == 0 );
835 Vec_Wrd_t * vElems = Vec_WrdStartTruthTables6( nVars );
836 Vec_Wrd_t * vCofs = Vec_WrdAlloc( 1000 );
837 Vec_Wec_t * vSets = Vec_WecStart( nVars+1 );
838 int m, nMints = 1 << nVars;
839 for ( m = 0; m < nMints; m++ )
841 int nOnes = __builtin_popcount(m);
842 Vec_WecPushTwo( vSets, nOnes, m, Vec_WrdSize(vCofs) );
845 Vec_WrdFree( vElems );
850static inline int Abc_BSEvalCountUnique(
word * pISets,
int nISets,
int nBSWords,
word * pCof )
853 for ( i = 0; i < nISets; i++ )
854 Count += Abc_TtIntersect(pISets + i*nBSWords, pCof, nBSWords, 0);
857static inline int Abc_BSEvalCountUniqueMax(
word * pISets,
int nISets,
int nBSWords,
word * pCofs,
int nOnes,
int nISetsMaxHave )
859 int m, nMints = (1 << nOnes), CountMax = 0;
861 for ( m = 0; m < nMints; m++ )
863 int Count = Abc_BSEvalCountUnique( pISets, nISets, nBSWords, pCofs + m * nBSWords );
864 if ( Count > nISetsMaxHave )
866 CountMax = Abc_MaxInt( CountMax, Count );
883int Abc_SharedEvalBest(
Abc_BSEval_t *
p,
word * pTruth,
int nVars,
int nCVars,
int nFVars,
int MyuMin,
int nRails,
int fVerbose,
int * pSetShared,
int * pnSetSize,
word * pPat )
885 int nBSWords = Abc_Truth6WordNum( nVars - nFVars ), CVarMask = nCVars ? Abc_InfoMask(nCVars) << (nVars - nCVars - nFVars) : 0;
886 int MyuCur, Myu =
Abc_TtGetCMInt( pTruth, nVars, nFVars,
p->vCounts,
p->vTable,
p->vStore,
p->vUsed, pPat );
887 int nRailsCur = Abc_Base2Log( Myu );
Vec_Int_t * vLevel;
888 assert( Myu == MyuMin && nRailsCur >= nRails );
889 int i, k, iSet, iStart, nSharedMax = nVars - nFVars - nRails, nRailsMin = 100;
892 if ( iSet & CVarMask )
895 MyuCur = Abc_BSEvalCountUniqueMax( pPat, Myu, nBSWords, Vec_WrdEntryP(
p->vCofs[
p->nBVars], iStart), i, 1 << nRails );
897 if ( MyuCur == 0 || MyuCur > (1 << nRails) )
899 nRailsCur = Abc_Base2Log(MyuCur);
900 if ( nRailsCur > nRails )
902 if ( nRailsMin > nRailsCur ) {
903 nRailsMin = nRailsCur;
908 if ( nRailsMin <= nRails )
928 int nPermVars = nVars-nRVars;
929 if (
p->nVars != nPermVars ) {
930 p->nVars = nPermVars;
931 p->nLVars = nLutSize-nRVars;
932 if (
p->vPairs[
p->nVars][
p->nLVars] == NULL )
935 if (
p->nBVars != nLutSize ) {
936 if (
p->vCofs[nLutSize] == NULL )
938 if (
p->nBVars < nLutSize ) {
940 p->pPat =
ABC_ALLOC(
word, (1 << nLutSize)*Abc_TtWordNum(nLutSize) );
942 p->nBVars = nLutSize;
945 int v, r,
nWords = Abc_TtWordNum(nVars);
947 Abc_TtCopy( pCopy, pTruth,
nWords, 0 );
950 int pPermBest[32] = {0};
951 int pPermBest2[32] = {0};
955 int MyuMin =
Abc_BSEvalBest(
p, pCopy, pBest, pBest2, nVars, nRVars, nVars-nLutSize, 0, pPermBest, pPermBest2, 0, nJRatio );
957 if ( pMyu ) *pMyu = MyuMin;
960 printf(
"Best perm: " );
961 for ( v = 0; v < nVars; v++ )
962 printf(
"%d ", pPermBest[v] );
963 printf(
" Myu = %d. ", MyuMin );
966 int Shared = 0, nSetSize = 0, nRailsMin = Abc_Base2Log( MyuMin );
967 for ( r = 1; r <= nRails && nRailsMin > r; r++ ) {
968 int nRailsMinNew =
Abc_SharedEvalBest(
p, pBest, nVars, nRVars, nVars-nLutSize, MyuMin, r, 0, &Shared, &nSetSize,
p->pPat );
969 if ( nRailsMinNew < 100 )
970 nRailsMin = nRailsMinNew;
972 MyuMin = 1 << nRailsMin;
980 printf(
"Myu min = %d. Rail min = %d. Shared = %x.\n", MyuMin, nRailsMin, Shared );
982 if ( nRailsMin > nRails )
986 for ( v = 0; v < nLutSize; v++ )
987 mBVars |= (
word)1 << pPermBest[nVars-nLutSize+v];
990 for ( v = 0; v < nLutSize; v++ )
991 if ( (Shared >> v) & 1 )
992 mSVars |= (
word)1 << (nVars-nLutSize+v);
994 return ((
word)MyuMin << 48) | (mSVars << 24) | mBVars;
1001 for ( v = 0; v < nLutSize; v++ )
1002 mBVars |= (
word)1 << pPermBest[nVars-nLutSize+v];
1004 for ( v = 0; v < nLutSize; v++ )
1005 if ( (Shared >> v) & 1 )
1006 mSVars |= (
word)1 << (nVars-nLutSize+v);
1007 return ((
word)MyuMin << 48) | (mSVars << 24) | mBVars;
1012 int nFVars = nVars-nLutSize;
1013 int nPermVars = nVars-nCVars;
1015 if (
p->nVars != nPermVars ) {
1016 p->nVars = nPermVars;
1017 p->nLVars = nLutSize-nCVars;
1018 if (
p->vPairs[
p->nVars][
p->nLVars] == NULL )
1021 if (
p->nBVars != nLutSize ) {
1022 if (
p->vCofs[nLutSize] == NULL )
1024 if (
p->nBVars < nLutSize ) {
1026 p->pPat =
ABC_ALLOC(
word, (1 << nLutSize)*Abc_TtWordNum(nLutSize) );
1028 p->nBVars = nLutSize;
1031 int nWords = Abc_TtWordNum(nVars);
1033 Abc_TtCopy( pCopy, pTruth,
nWords, 0 );
1036 int i, k,
Var0,
Var1, Pla2Var[32], Var2Pla[32];
1037 for ( i = 0; i < nVars; i++ )
1038 Pla2Var[i] = Var2Pla[i] = i;
1039 int MyuOrigBest = 1 << nVars;
1040 int MyuBest = 1 << nVars;
1041 int nSetSizeBest = nVars;
1043 if ( pMyu ) *pMyu = 1 << nVars;
1045 int MyuThis =
Abc_TtGetCM( pCopy, nVars, nFVars,
p->vCounts,
p->vTable,
p->vStore,
p->vUsed, 0 );
1046 MyuOrigBest = Abc_MinInt( MyuOrigBest, MyuThis );
1047 if ( pMyu ) *pMyu = Abc_MinInt( *pMyu, MyuThis );
1050 printf(
"%3d : ", i/2 );
1051 for ( k = nCVars-1; k >= 0; k-- )
1052 printf(
" %d", nVars-nCVars+k );
1054 for ( k = nPermVars-1; k >= nFVars; k-- )
1055 printf(
" %d", Pla2Var[k] );
1057 for ( k = nFVars-1; k >= 0; k-- )
1058 printf(
" %d", Pla2Var[k] );
1059 printf(
" : Myu = %3d", MyuThis );
1061 if ( MyuThis <= MyuOrigBest + nMyuIncrease ) {
1062 int Shared = 0, nSetSize = 0;
1063 if ( MyuThis > 2 ) {
1064 int SharedThis = 0, nSetSizeThis = 0;
1065 int nRailsMin = 100;
1066 for (
int r = 1; r <= nRails && nRailsMin > r; r++ ) {
1067 int nRailsMinNew =
Abc_SharedEvalBest(
p, pCopy, nVars, nCVars, nFVars, MyuThis, r, 0, &SharedThis, &nSetSizeThis,
p->pPat );
1068 if ( nRailsMinNew < 100 )
1069 nRailsMin = nRailsMinNew;
1072 printf(
" RailsMyu = %3d. RailsMin = %3d. Shared = %2d. SetSize = %d.", Abc_Base2Log(MyuThis), nRailsMin, SharedThis, nSetSizeThis );
1073 if ( nRailsMin <= nRails ) {
1074 MyuThis = 1 << nRailsMin;
1075 Shared = SharedThis;
1076 nSetSize = nSetSizeThis;
1079 if ( MyuBest > MyuThis || (MyuBest == MyuThis && nSetSizeBest >= nSetSize) ) {
1080 int fSave = (MyuBest == MyuThis && nSetSizeBest == nSetSize);
1082 nSetSizeBest = nSetSize;
1085 Vec_WrdPush( vRes, Result );
1087 Vec_WrdFill( vRes, 1, Result );
1089 printf(
" <== best" );
1094 int iPlace0 = Var2Pla[
Var0];
1095 int iPlace1 = Var2Pla[
Var1];
1096 if ( iPlace0 == iPlace1 )
1098 Abc_TtSwapVars( pCopy, nVars, iPlace0, iPlace1 );
1099 Var2Pla[Pla2Var[iPlace0]] = iPlace1;
1100 Var2Pla[Pla2Var[iPlace1]] = iPlace0;
1101 Pla2Var[iPlace0] ^= Pla2Var[iPlace1];
1102 Pla2Var[iPlace1] ^= Pla2Var[iPlace0];
1103 Pla2Var[iPlace0] ^= Pla2Var[iPlace1];
1105 for ( i = 0; i < nPermVars; i++ )
1108 int iPlace1 = Var2Pla[i];
1109 if ( iPlace0 == iPlace1 )
1111 Abc_TtSwapVars( pCopy, nVars, iPlace0, iPlace1 );
1112 Var2Pla[Pla2Var[iPlace0]] = iPlace1;
1113 Var2Pla[Pla2Var[iPlace1]] = iPlace0;
1114 Pla2Var[iPlace0] ^= Pla2Var[iPlace1];
1115 Pla2Var[iPlace1] ^= Pla2Var[iPlace0];
1116 Pla2Var[iPlace0] ^= Pla2Var[iPlace1];
1118 if ( !Abc_TtEqual(pCopy, pTruth,
nWords) )
1119 printf(
"Internal truth table check failed.\n" );
1122 if ( MyuBest > (1 << nRails) ) {
1127 printf(
"COllected %d solutions with MyuMin = %d and SharedSize = %d.\n", Vec_WrdSize(vRes), MyuBest, nSetSizeBest );
struct Abc_BSEval_t_ Abc_BSEval_t
word Abc_RandomW(int fReset)
#define ABC_SWAP(Type, a, b)
#define ABC_ALLOC(type, num)
unsigned Abc_Random(int fReset)
#define ABC_CALLOC(type, num)
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
void Extra_PrintHex(FILE *pFile, unsigned *pTruth, int nVars)
void Extra_PrintBinary(FILE *pFile, unsigned Sign[], int nBits)
unsigned __int64 word
DECLARATIONS ///.
Vec_Wrd_t * vCofs[MAX_TT_SIZE]
Vec_Int_t * vPairs[MAX_TT_SIZE][MAX_TT_SIZE]
Vec_Wec_t * vSets[MAX_TT_SIZE]
int Abc_TtGetCM2Pat(word *p, int nVars, word *pPat)
int Abc_TtGetCM4Pat(word *p, int nVars, int *pMap, Vec_Int_t *vUsed, word *pPat)
int Abc_TtCheck1Shared(word *pPat, int nVars, int nFVars, int nMyu)
word Abc_TtFindBVarsSVars(word *pTruth, int nVars, int nRVars, int nRails, int nLutSize, int fVerbose, int *pMyu, int nJRatio)
int Abc_TtGetCM4(word *p, int nVars, int *pCounts, Vec_Int_t *vUsed)
int Abc_TtHashLookup5(int Entry, Vec_Int_t *vTable, Vec_Wrd_t *vStore, Vec_Int_t *vUsed)
int Abc_TtGetCM(word *p, int nVars, int nFVars, Vec_Int_t *vCounts, Vec_Int_t *vTable, Vec_Wrd_t *vStore, Vec_Int_t *vUsed, int fShared)
void Abc_BSEvalBestGen(int nVars, int nLVars, int nFuncs, int nMints, int fTryAll, int fShared, int fVerbose)
void Abc_BSEvalBestTest(word *pIn, int nVars, int nLVars, int fShared, int fVerbose)
int Abc_TtHashLookup6(word *pEntry, int nWords, Vec_Int_t *vTable, Vec_Wrd_t *vStore, Vec_Int_t *vUsed)
void Abc_BSEvalOneTest(word *pT, int nVars, int nLVars, int fVerbose)
int Abc_TtGetCM6Pat(word *p, int nVars, int nFVars, Vec_Int_t *vTable, Vec_Wrd_t *vStore, Vec_Int_t *vUsed, word *pPat)
void Abc_BSEvalFree(Abc_BSEval_t *p)
int Abc_TtGetCMInt(word *p, int nVars, int nFVars, Vec_Int_t *vCounts, Vec_Int_t *vTable, Vec_Wrd_t *vStore, Vec_Int_t *vUsed, word *pPat)
int Abc_TtGetCM2(word *p, int nVars)
Abc_BSEval_t * Abc_BSEvalAlloc()
void Abc_BSEvalCreateCofs(int iSet, int nVars, Vec_Wrd_t *vCofs, Vec_Wrd_t *vElems)
word Abc_BSEvalEncode(int *pPermBest, int nVars, int nLutSize, int Shared, int MyuMin, int SharedSize)
Vec_Wrd_t * Abc_TtFindBVarsSVars2(Abc_BSEval_t *p, word *pTruth, int nVars, int nCVars, int nRails, int nLutSize, int fVerbose, int *pMyu, int nMyuIncrease)
int Abc_TtGetCM1(word *p, int nVars)
FUNCTION DEFINITIONS ///.
#define MAX_TT_SIZE
DECLARATIONS ///.
int Abc_TtGetCM3Pat(word *p, int nVars, int *pMap, Vec_Int_t *vUsed, word *pPat)
int Abc_TtGetCM5(word *p, int nVars, Vec_Int_t *vTable, Vec_Wrd_t *vStore, Vec_Int_t *vUsed)
int Abc_TtGetCM6(word *p, int nVars, int nFVars, Vec_Int_t *vTable, Vec_Wrd_t *vStore, Vec_Int_t *vUsed)
void Abc_TtPrintPat(word *pPat, int nVars, int nMyu)
int Abc_TtGetKey(unsigned char *p, int nSize, int nTableSize)
int Abc_TtGetCMPat(word *p, int nVars, int nFVars, Vec_Int_t *vCounts, Vec_Int_t *vTable, Vec_Wrd_t *vStore, Vec_Int_t *vUsed)
void Abc_GenChasePrint(int Count, int *pPerm, int nVars, int nFVars, int Var0, int Var1)
int Abc_SharedEvalBest(Abc_BSEval_t *p, word *pTruth, int nVars, int nCVars, int nFVars, int MyuMin, int nRails, int fVerbose, int *pSetShared, int *pnSetSize, word *pPat)
int Abc_BSEvalBest(Abc_BSEval_t *p, word *pIn, word *pBest, word *pBest2, int nVars, int nCVars, int nFVars, int fVerbose, int *pPermBest, int *pPermBest2, int fShared, int nJRatio)
int Abc_TtGetCM1Pat(word *p, int nVars, word *pPat)
Vec_Int_t * Abc_GenChasePairs(int N, int T)
void Abc_GenChaseNext(int a[], int w[], int *r)
Vec_Wrd_t * Abc_BSEvalCreateCofactorSets(int nVars, Vec_Wec_t **pvSets)
int Abc_TtGetCM3(word *p, int nVars, int *pCounts, Vec_Int_t *vUsed)
int Abc_TtGetCM5Pat(word *p, int nVars, Vec_Int_t *vTable, Vec_Wrd_t *vStore, Vec_Int_t *vUsed, word *pPat)
int Abc_TtGetCMCount(word *p, int nVars, int nFVars, Vec_Int_t *vCounts, Vec_Int_t *vTable, Vec_Wrd_t *vStore, Vec_Int_t *vUsed)
#define Vec_IntForEachEntryDouble(vVec, Entry1, Entry2, i)
#define Vec_IntForEachEntry(vVec, Entry, i)
MACRO DEFINITIONS ///.
#define Vec_WecForEachLevelStartStop(vGlob, vVec, i, LevelStart, LevelStop)
typedefABC_NAMESPACE_HEADER_START struct Vec_Wec_t_ Vec_Wec_t
INCLUDES ///.
typedefABC_NAMESPACE_HEADER_START struct Vec_Wrd_t_ Vec_Wrd_t
INCLUDES ///.