48 word Cof[2][64], Value;
49 word MaskFF = (((
word)1) << (1 << nVarsF)) - 1;
50 int ShiftF = 6 - nVarsF, MaskF = (1 << ShiftF) - 1;
51 int pVarsS[16], pVarsB[16];
52 int nMints = (1 << nVarsB);
53 int nMintsB = (1 <<(nVarsB-nVarsS));
54 int nMintsS = (1 << nVarsS);
55 int s, b, v, m, Mint, MintB, MintS;
56 assert( nVars == nVarsB + nVarsF );
59 assert( nVarsF >= 1 && nVarsF <= 5 );
61 for ( s = b = v = 0; v < nVarsB; v++ )
62 if ( (uMaskS >> v) & 1 )
63 pVarsB[v] = -1, pVarsS[v] = s++;
65 pVarsS[v] = -1, pVarsB[v] = b++;
67 assert( b == nVarsB-nVarsS );
69 for ( s = 0; s < nMintsS; s++ )
70 Cof[0][s] = Cof[1][s] = ~(
word)0;
72 for ( MintS = MintB = Mint = m = 0; m < nMints; m++ )
75 Value = (p[Mint>>ShiftF] >> ((Mint&MaskF)<<nVarsF)) & MaskFF;
77 if ( !~Cof[0][MintS] || Cof[0][MintS] == Value )
78 Cof[0][MintS] = Value;
79 else if ( !~Cof[1][MintS] || Cof[1][MintS] == Value )
81 Cof[1][MintS] = Value;
84 int iMintB = MintS * nMintsB + MintB;
85 pDec[iMintB>>6] |= (((
word)1)<<(iMintB & 63));
93 if ( (uMaskS >> v) & 1 )
94 MintS ^= (1 << pVarsS[v]);
96 MintB ^= (1 << pVarsB[v]);
101 for ( s = 0; s < nMintsS; s++ )
103 pComp[s>>ShiftF] |= (Cof[0][s] << ((s&MaskF) << nVarsF));
105 pComp[(s+nMintsS)>>ShiftF] |= (Cof[1][s] << (((s+nMintsS)&MaskF) << nVarsF));
107 pComp[(s+nMintsS)>>ShiftF] |= (Cof[0][s] << (((s+nMintsS)&MaskF) << nVarsF));
109 if ( nVarsF + nVarsS + 1 < 6 )
112 if ( pDec && nVarsB < 6 )
120 int pVarsS[16], pVarsB[16];
121 int nMints = (1 << nVarsB);
122 int nMintsB = (1 <<(nVarsB-nVarsS));
123 int nMintsS = (1 << nVarsS);
124 int s, b, v, m, Mint, MintB, MintS;
125 assert( nVars == nVarsB + nVarsF );
130 for ( s = b = v = 0; v < nVarsB; v++ )
131 if ( (uMaskS >> v) & 1 )
132 pVarsB[v] = -1, pVarsS[v] = s++;
134 pVarsS[v] = -1, pVarsB[v] = b++;
136 assert( b == nVarsB-nVarsS );
138 for ( s = 0; s < nMintsS; s++ )
139 Cof[0][s] = Cof[1][s] = NULL;
141 for ( MintS = MintB = Mint = m = 0; m < nMints; m++ )
144 if ( !Cof[0][MintS] || !
memcmp(Cof[0][MintS], p + Mint * nWordsF,
sizeof(
word) * nWordsF) )
145 Cof[0][MintS] = p + Mint * nWordsF;
146 else if ( !Cof[1][MintS] || !
memcmp(Cof[1][MintS], p + Mint * nWordsF,
sizeof(
word) * nWordsF) )
148 Cof[1][MintS] = p + Mint * nWordsF;
151 int iMintB = MintS * nMintsB + MintB;
152 pDec[iMintB>>6] |= (((
word)1)<<(iMintB & 63));
160 if ( (uMaskS >> v) & 1 )
161 MintS ^= (1 << pVarsS[v]);
163 MintB ^= (1 << pVarsB[v]);
168 for ( s = 0; s < nMintsS; s++ )
170 memcpy( pComp + s * nWordsF, Cof[0][s],
sizeof(
word) * nWordsF );
172 memcpy( pComp + (s+nMintsS) * nWordsF, Cof[1][s],
sizeof(
word) * nWordsF );
174 memcpy( pComp + (s+nMintsS) * nWordsF, Cof[0][s],
sizeof(
word) * nWordsF );
177 if ( pDec && nVarsB < 6 )
184 return Dau_DecCheckSetTop5( p, nVars, nVarsF, nVarsB, nVarsS, uMaskS, pSched, pDec, pComp );
186 return Dau_DecCheckSetTop6( p, nVars, nVarsF, nVarsB, nVarsS, uMaskS, pSched, pDec, pComp );
203 for ( m = c = v = 0; v < nVarsS; v++ )
204 if ( !((uMaskAll >> v) & 1) )
211 p[m>>6] |= (((
word)1)<<(m & 63));
215 int fFound0 = 0, fFound1 = 0;
216 int g, gMax = (1 << (nVars - nVarsF));
217 int Shift = 6 - nVarsF, Mask = (1 << Shift) - 1;
218 word Mask2 = (((
word)1) << (1 << nVarsF)) - 1;
219 word Cof0 = 0, Cof1 = 0, Value;
220 assert( nVarsF >= 1 && nVarsF <= 5 );
221 if ( pDec ) *pDec = 0;
222 for ( g = 0; g < gMax; g++ )
223 if ( (g & uMaskAll) == uMaskValue )
225 Value = (p[g>>Shift] >> ((g&Mask)<<nVarsF)) & Mask2;
227 Cof0 = Value, fFound0 = 1;
228 else if ( Cof0 == Value )
232 Cof1 = Value, fFound1 = 1;
235 else if ( Cof1 == Value )
246 Cof1 = fFound1 ? Cof1 : Cof0;
254 int fFound0 = 0, fFound1 = 0;
255 int g, gMax = (1 << (nVars - nVarsF));
257 word * Cof0 = NULL, * Cof1 = NULL;
258 assert( nVarsF >= 6 && nVarsF <= nVars - 2 );
259 if ( pDec ) *pDec = 0;
260 for ( g = 0; g < gMax; g++ )
261 if ( (g & uMaskAll) == uMaskValue )
264 Cof0 = p + g *
nWords, fFound0 = 1;
265 else if ( !
memcmp(Cof0, p + g * nWords,
sizeof(
word) * nWords) )
269 Cof1 = p + g *
nWords, fFound1 = 1;
272 else if ( !
memcmp(Cof1, p + g * nWords,
sizeof(
word) * nWords) )
283 Cof1 = fFound1 ? Cof1 : Cof0;
291 assert( nVarsF >= 1 && nVarsF <= nVars - 2 );
293 return Dau_DecCheckSet5( p, nVars, nVarsF, uMaskAll, uMaskValue, pCof0, pCof1, pDec );
295 return Dau_DecCheckSet6( p, nVars, nVarsF, uMaskAll, uMaskValue, pCof0, pCof1, pDec );
300 int v, m, mMax = (1 << nVarsS), uMaskValue;
301 assert( nVars >= 3 && nVars <= 16 );
302 assert( nVars == nVarsF + nVarsB );
303 assert( nVarsF >= 1 && nVarsF <= nVars - 2 );
304 assert( nVarsB >= 2 && nVarsB <= nVars - 1 );
305 assert( nVarsS >= 0 && nVarsS <= nVarsB - 2 );
307 return Dau_DecCheckSetAny( p, nVars, nVarsF, 0, 0, pCof0? pCof0[0] : 0, pCof1? pCof1[0] : 0, pDec? pDec[0] : 0 );
309 assert( maskS > 0 && maskS < (1 << nVarsB) );
310 for ( i = 0, v = 0; v < nVarsB; v++ )
311 if ( (maskS >> v) & 1 )
315 for ( m = 0; m < mMax; m++ )
319 for ( v = 0; v < nVarsS; v++ )
321 uMaskValue |= (1 << pVarsS[v]);
322 assert( (maskS & uMaskValue) == uMaskValue );
324 if ( !
Dau_DecCheckSetAny( p, nVars, nVarsF, maskS, uMaskValue, pCof0? pCof0[m] : 0, pCof1? pCof1[m] : 0, pDec? pDec[m] : 0 ) )
344 unsigned uSet = 0;
int v;
345 for ( v = 0; v < sizeB; v++ )
347 uSet |= (1 << (pVarsB[v] << 1));
348 if ( (maskS >> v) & 1 )
349 uSet |= (1 << ((pVarsB[v] << 1)+1));
355 return (Mask & ((~Mask) >> 1) & 0x55555555);
365 Old = (unsigned)Entry;
371 void Dau_DecSortSet(
unsigned set,
int nVars,
int * pnUnique,
int * pnShared,
int * pnFree )
374 int nUnique = 0, nShared = 0, nFree = 0;
375 for ( v = 0; v < nVars; v++ )
377 int Value = ((set >> (v << 1)) & 3);
380 else if ( Value == 3 )
382 else if ( Value == 0 )
393 int nUnique = 0, nShared = 0, nFree = 0;
395 printf(
"S =%2d D =%2d C =%2d ", nShared, nUnique+nShared, nShared+nFree+1 );
398 for ( v = 0; v < nVars; v++ )
400 int Value = ((set >> (v << 1)) & 3);
402 printf(
"%c",
'a' + v ), Counter++;
403 else if ( Value == 3 )
404 printf(
"%c",
'A' + v ), Counter++;
405 else assert( Value == 0 );
408 for ( v = 0; v < nVars; v++ )
410 int Value = ((set >> (v << 1)) & 3);
412 printf(
"%c",
'a' + v ), Counter++;
413 else if ( Value == 3 )
414 printf(
"%c",
'A' + v ), Counter++;
416 for ( ; Counter < 15; Counter++ )
423 unsigned uSet = 0;
int v;
424 for ( v = 0; pStr[v]; v++ )
426 if ( pStr[v] >=
'a' && pStr[v] <=
'z' )
427 uSet |= (1 << ((pStr[v] -
'a') << 1));
428 else if ( pStr[v] >=
'A' && pStr[v] <=
'Z' )
429 uSet |= (1 << ((pStr[v] -
'a') << 1)) | (1 << (((pStr[v] -
'a') << 1)+1));
437 printf(
"The %d-variable set family contains %d sets:\n", nVars,
Vec_IntSize(vSets) );
457 for ( v = 0; v < nVars; v++ )
458 if ( !((maskB >> v) & 1) )
460 assert( c == nVars - sizeB );
465 int V2P[16], P2V[16], pVarsB[16];
466 int Limit = (1 << nVars);
467 int c, v, sizeB, sizeS, maskB, maskS;
471 for ( v = 0; v < nVars; v++ )
474 for ( v = 0; v < nVars; v++ )
477 for ( sizeB = 2; sizeB < nVars; sizeB++ )
478 for ( maskB = 0; maskB < Limit; maskB++ )
484 for ( c = 0; c < sizeB; c++ )
485 pVarsB[c] = P2V[nVars-sizeB+c];
488 if (
Dau_DecCheckSetTop(p, nVars, nVars-sizeB, sizeB, 0, 0, pSched[sizeB], NULL, NULL) )
496 for ( sizeS = 1; sizeS <= sizeB - 2; sizeS++ )
499 for ( maskS = 0; maskS < (1 << sizeB); maskS++ )
510 if (
Dau_DecCheckSetTop(p, nVars, nVars-sizeB, sizeB, sizeS, maskS, pSched[sizeB], NULL, NULL) )
519 int v, * pSched[16] = {NULL};
520 for ( v = 2; v < nVars; v++ )
523 for ( v = 2; v < nVars; v++ )
531 word a1 = (~s_Truths6[1] & s_Truths6[4]) | (s_Truths6[1] & s_Truths6[5]);
532 word t = (~s_Truths6[0] &
a0) | (s_Truths6[0] & a1);
560 for ( v = 0; pStr[v]; v++ )
561 if ( pStr[v] >=
'a' && pStr[v] <=
'z' )
563 assert( pStr[v] -
'a' < nVars );
564 pStr[v] =
'a' + pPerm[pStr[v] -
'a'];
581 word p[1<<13], Cof[64], Cof0[64], Cof1[64], Decs[64];
582 word * pCof0[64], * pCof1[64], * pDecs[64], MintC, MintD;
583 int V2P[16], P2V[16], pVarsU[16], pVarsS[16], pVarsF[16];
584 int nVarsU = 0, nVarsS = 0, nVarsF = 0;
586 int v, d, c, Status, nDecs;
588 for ( v = 0; v < nVars; v++ )
592 for ( v = 0; v < nVars; v++ )
594 int Value = (uSet >> (v<<1)) & 3;
596 pVarsF[nVarsF++] = v;
597 else if ( Value == 1 )
598 pVarsU[nVarsU++] = v;
599 else if ( Value == 3 )
600 pVarsS[nVarsS++] = v;
603 assert( nVarsS >= 0 && nVarsS <= 6 );
604 assert( nVarsF + nVarsS + 1 <= 6 );
605 assert( nVarsU + nVarsS <= 6 );
607 nDecs = (1 << nVarsS);
608 for ( d = 0; d < nDecs; d++ )
616 for ( v = 0; v < nVarsF; v++ )
618 for ( v = 0; v < nVarsS; v++ )
620 for ( v = 0; v < nVarsU; v++ )
628 assert( nVarsF + nVarsS < 6 );
629 for ( d = 0; d < nDecs; d++ )
631 Cof[d] = (pCof1[d][0] &
s_Truths6[nVarsF + nVarsS]) | (pCof0[d][0] & ~
s_Truths6[nVarsF + nVarsS]);
637 for ( d = 0; d < nDecs; d++ )
640 MintC = MintD = ~((
word)0);
641 for ( v = 0; v < nVarsS; v++ )
647 pComp[0] |= MintC & Cof[d];
648 pDec[0] |= MintD & pDecs[d][0];
653 for ( v = 0; v < nVarsF; v++ )
654 pPermC[v] = pVarsF[v];
655 for ( v = 0; v < nVarsS; v++ )
656 pPermC[nVarsF+v] = pVarsS[v];
657 pPermC[nVarsF + nVarsS] = nVars;
661 for ( v = 0; v < nVarsU; v++ )
662 pPermD[v] = pVarsU[v];
663 for ( v = 0; v < nVarsS; v++ )
664 pPermD[nVarsU+v] = pVarsS[v];
667 *pnVarsC = nVarsF + nVarsS + 1;
669 *pnVarsD = nVarsU + nVarsS;
689 word pC[1<<13], pD[1<<13], pRes[1<<13];
699 Abc_TtMux( pRes, pD, pC + nWordsD, pC, nWordsD );
705 Abc_TtMux( pRes, pD, &pC1, &pC0, nWordsD );
708 printf(
" Verification failed" );
716 word tComp = 0, tDec = 0, tDec0, tComp0, tComp1, FuncC, FuncD;
717 char pDsdC[1000], pDsdD[1000];
718 int pPermC[16], pPermD[16];
719 int nVarsC, nVarsD, nVarsS, nVarsU, nVarsF, nPairs;
720 int i, m, v, status, ResC, ResD,
Counter = 0;
721 status =
Dau_DecDecomposeSet( p, nVars, uSet, &tComp, &tDec0, pPermC, pPermD, &nVarsC, &nVarsD, &nVarsS );
724 printf(
" Decomposition does not exist\n" );
727 nVarsU = nVarsD - nVarsS;
728 nVarsF = nVarsC - nVarsS - 1;
731 nPairs = 1 << (1 << nVarsS);
732 for ( i = 0; i < nPairs; i++ )
738 for ( m = 0; m < (1 << nVarsS); m++ )
741 if ( !((i >> m) & 1) )
743 MintC = MintD = ~(
word)0;
744 for ( v = 0; v < nVarsS; v++ )
753 tComp = (~
s_Truths6[nVarsF + nVarsS] & ((tComp0 & ~FuncC) | (tComp1 & FuncC))) | (
s_Truths6[nVarsF + nVarsS] & ((tComp1 & ~FuncC) | (tComp0 & FuncC)));
754 tDec = tDec0 ^ FuncD;
763 printf(
"%3d : ", Counter++ );
764 printf(
"%24s ", pDsdD );
765 printf(
"%24s ", pDsdC );
773 word p[1<<10], pDec[1<<10], pComp[1<<10];
774 char pDsdC[5000], pDsdD[5000];
775 int nVarsU, nVarsS, nVarsF, nVarsC = 0, nVarsD = 0;
776 int V2P[16], P2V[16], pPermC[16], pPermD[16], * pSched;
777 int v, i, status, ResC, ResD;
785 for ( v = 0; v < nVars; v++ )
787 for ( i = v = 0; v < nVars; v++ )
788 if ( ((uSet >> (v<<1)) & 3) == 0 )
789 Abc_TtMoveVar( p, nVars, V2P, P2V, v, i++ ), pPermC[nVarsC++] = v;
790 for ( v = 0; v < nVars; v++ )
791 if ( ((uSet >> (v<<1)) & 3) == 3 )
792 Abc_TtMoveVar( p, nVars, V2P, P2V, v, i++ ), pPermC[nVarsC++] = v;
793 pPermC[nVarsC++] = nVars;
794 for ( v = 0; v < nVars; v++ )
795 if ( ((uSet >> (v<<1)) & 3) == 1 )
796 Abc_TtMoveVar( p, nVars, V2P, P2V, v, i++ ), pPermD[nVarsD++] = v;
797 for ( v = 0; v < nVarsS; v++ )
798 pPermD[nVarsD++] = pPermC[nVarsF+v];
799 assert( nVarsD == nVarsU + nVarsS );
800 assert( nVarsC == nVarsF + nVarsS + 1 );
810 printf(
" Decomposition does not exist\n" );
825 printf(
"%3d : ", 0 );
826 printf(
"%24s ", pDsdD );
827 printf(
"%24s ", pDsdC );
843 printf(
"This %d-variable function has %d decomposable variable sets:\n", nVars,
Vec_IntSize(vSets) );
846 unsigned uSet = (unsigned)Entry;
847 printf(
"Set %4d : ", i );
866 word a1 = (~s_Truths6[1] & s_Truths6[4]) | (s_Truths6[1] & s_Truths6[5]);
867 word t = (~s_Truths6[0] &
a0) | (s_Truths6[0] & a1);
static unsigned Abc_InfoMask(int nVar)
void Dau_DecPrintSet(unsigned set, int nVars, int fNewLine)
int Dau_DecPerform(word *pInit, int nVars, unsigned uSet)
int Dau_DecCheckSetTop6(word *p, int nVars, int nVarsF, int nVarsB, int nVarsS, int uMaskS, int *pSched, word *pDec, word *pComp)
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
static void Dau_DecGetMinterm(word *p, int g, int nVarsS, int uMaskAll)
int Dau_DecDecomposeSet(word *pInit, int nVars, unsigned uSet, word *pComp, word *pDec, int *pPermC, int *pPermD, int *pnVarsC, int *pnVarsD, int *pnVarsS)
int Dau_DecCheckSetTopOld(word *p, int nVars, int nVarsF, int nVarsB, int nVarsS, int maskS, word **pCof0, word **pCof1, word **pDec)
Vec_Int_t * Dau_DecFindSets_int(word *pInit, int nVars, int *pSched[16])
void Dau_DsdPrintFromTruth(word *pTruth, int nVarsInit)
int Dau_DsdDecompose(word *pTruth, int nVarsInit, int fSplitPrime, int fWriteTruth, char *pRes)
void Dau_DecFindSetsTest3()
word * Dau_DsdToTruth(char *p, int nVars)
ABC_NAMESPACE_IMPL_START int Dau_DecCheckSetTop5(word *p, int nVars, int nVarsF, int nVarsB, int nVarsS, int uMaskS, int *pSched, word *pDec, word *pComp)
DECLARATIONS ///.
int Dau_DecPerform6(word *p, int nVars, unsigned uSet)
static word Abc_Tt6Cofactor0(word t, int iVar)
void Dau_DecTrySets(word *pInit, int nVars, int fVerbose)
void Dau_DecSortSet(unsigned set, int nVars, int *pnUnique, int *pnShared, int *pnFree)
static int Abc_TtBitCount16(int i)
static unsigned Dau_DecCreateSet(int *pVarsB, int sizeB, int maskS)
static int Dau_DecCheckSetAny(word *p, int nVars, int nVarsF, int uMaskAll, int uMaskValue, word *pCof0, word *pCof1, word *pDec)
static word Abc_Tt6Cofactor1(word t, int iVar)
static Vec_Int_t * Vec_IntAlloc(int nCap)
FUNCTION DEFINITIONS ///.
static int Dau_DecSetHas01(unsigned Mask)
static int Dau_DecCheckSet5(word *p, int nVars, int nVarsF, int uMaskAll, int uMaskValue, word *pCof0, word *pCof1, word *pDec)
unsigned __int64 word
DECLARATIONS ///.
#define ABC_NAMESPACE_IMPL_END
static word Abc_Tt6Stretch(word t, int nVars)
int Dau_DecVerify(word *pInit, int nVars, char *pDsdC, char *pDsdD)
static void Vec_IntPush(Vec_Int_t *p, int Entry)
static int Abc_TtHasVar(word *t, int nVars, int iVar)
static int Dau_DecCheckSet6(word *p, int nVars, int nVarsF, int uMaskAll, int uMaskValue, word *pCof0, word *pCof1, word *pDec)
void Dau_DecFindSetsTest2()
void Dau_DecPrintSets(Vec_Int_t *vSets, int nVars)
void Dau_DecFindSetsTest()
#define ABC_NAMESPACE_IMPL_START
Vec_Int_t * Dau_DecFindSets(word *pInit, int nVars)
static int Vec_IntSize(Vec_Int_t *p)
static int Abc_TtWordNum(int nVars)
#define ABC_CONST(number)
PARAMETERS ///.
static void Abc_TtMux(word *pOut, word *pCtrl, word *pIn1, word *pIn0, int nWords)
static int Dau_DecSetIsContained(Vec_Int_t *vSets, unsigned New)
static int Abc_TtEqual(word *pIn1, word *pIn2, int nWords)
static int Dau_DecCheckSetTop(word *p, int nVars, int nVarsF, int nVarsB, int nVarsS, int uMaskS, int *pSched, word *pDec, word *pComp)
static void Vec_IntFree(Vec_Int_t *p)
#define Vec_IntForEachEntry(vVec, Entry, i)
MACRO DEFINITIONS ///.
unsigned Dau_DecReadSet(char *pStr)
void Dau_DecMoveFreeToLSB(word *p, int nVars, int *V2P, int *P2V, int maskB, int sizeB)
static void Abc_TtMoveVar(word *pF, int nVars, int *V2P, int *P2V, int v, int p)
void Dau_DecVarReplace(char *pStr, int *pPerm, int nVars)