56static inline int Ivy_DecToInt(
Ivy_Dec_t m ) {
union {
Ivy_Dec_t x;
int y; } v; v.x = m;
return v.y; }
57static inline Ivy_Dec_t Ivy_IntToDec(
int m ) {
union {
Ivy_Dec_t x;
int y; } v; v.y = m;
return v.x; }
58static inline void Ivy_DecClear(
Ivy_Dec_t * pNode ) { *pNode = Ivy_IntToDec(0); }
65static unsigned s_Masks[6][2] = {
66 { 0x55555555, 0xAAAAAAAA },
67 { 0x33333333, 0xCCCCCCCC },
68 { 0x0F0F0F0F, 0xF0F0F0F0 },
69 { 0x00FF00FF, 0xFF00FF00 },
70 { 0x0000FFFF, 0xFFFF0000 },
71 { 0x00000000, 0xFFFFFFFF }
74static inline int Ivy_TruthWordCountOnes(
unsigned uWord )
76 uWord = (uWord & 0x55555555) + ((uWord>>1) & 0x55555555);
77 uWord = (uWord & 0x33333333) + ((uWord>>2) & 0x33333333);
78 uWord = (uWord & 0x0F0F0F0F) + ((uWord>>4) & 0x0F0F0F0F);
79 uWord = (uWord & 0x00FF00FF) + ((uWord>>8) & 0x00FF00FF);
80 return (uWord & 0x0000FFFF) + (uWord>>16);
83static inline int Ivy_TruthCofactorIsConst(
unsigned uTruth,
int Var,
int Cof,
int Const )
86 return (uTruth & s_Masks[
Var][Cof]) == 0;
88 return (uTruth & s_Masks[
Var][Cof]) == s_Masks[
Var][Cof];
91static inline int Ivy_TruthCofactorIsOne(
unsigned uTruth,
int Var )
93 return (uTruth & s_Masks[
Var][0]) == 0;
96static inline unsigned Ivy_TruthCofactor(
unsigned uTruth,
int Var )
98 unsigned uCofactor = uTruth & s_Masks[
Var >> 1][(
Var & 1) == 0];
99 int Shift = (1 << (
Var >> 1));
101 return uCofactor | (uCofactor << Shift);
102 return uCofactor | (uCofactor >> Shift);
105static inline unsigned Ivy_TruthCofactor2(
unsigned uTruth,
int Var0,
int Var1 )
107 return Ivy_TruthCofactor( Ivy_TruthCofactor(uTruth,
Var0),
Var1 );
111static inline int Ivy_TruthDepends(
unsigned uTruth,
int Var )
113 return Ivy_TruthCofactor(uTruth,
Var << 1) != Ivy_TruthCofactor(uTruth, (
Var << 1) | 1);
116static inline void Ivy_DecSetVar(
Ivy_Dec_t * pNode,
int iNum,
unsigned Var )
118 assert( iNum >= 0 && iNum <= 5 );
121 case 0: pNode->
Fan0 =
Var;
break;
122 case 1: pNode->
Fan1 =
Var;
break;
123 case 2: pNode->
Fan2 =
Var;
break;
124 case 3: pNode->
Fan3 =
Var;
break;
125 case 4: pNode->
Fan4 =
Var;
break;
126 case 5: pNode->
Fan5 =
Var;
break;
130static inline unsigned Ivy_DecGetVar(
Ivy_Dec_t * pNode,
int iNum )
132 assert( iNum >= 0 && iNum <= 5 );
135 case 0:
return pNode->
Fan0;
136 case 1:
return pNode->
Fan1;
137 case 2:
return pNode->
Fan2;
138 case 3:
return pNode->
Fan3;
139 case 4:
return pNode->
Fan4;
140 case 5:
return pNode->
Fan5;
145static int Ivy_TruthDecompose_rec(
unsigned uTruth,
Vec_Int_t * vTree );
146static int Ivy_TruthRecognizeMuxMaj(
unsigned uTruth,
int * pSupp,
int nSupp,
Vec_Int_t * vTree );
171 Vec_IntClear( vTree );
172 for ( i = 0; i < 5; i++ )
173 Vec_IntPush( vTree, 0 );
175 if ( uTruth == 0 || ~uTruth == 0 )
177 Ivy_DecClear( &Node );
179 Node.
fCompl = (uTruth == 0);
180 Vec_IntPush( vTree, Ivy_DecToInt(Node) );
184 RetValue = Ivy_TruthDecompose_rec( uTruth, vTree );
185 if ( RetValue == -1 )
188 if ( (RetValue >> 1) < 5 )
190 Ivy_DecClear( &Node );
192 Node.
fCompl = (RetValue & 1);
193 Node.
Fan0 = ((RetValue >> 1) << 1);
194 Vec_IntPush( vTree, Ivy_DecToInt(Node) );
196 else if ( RetValue & 1 )
198 Node = Ivy_IntToDec( Vec_IntPop(vTree) );
200 Node.
fCompl = (RetValue & 1);
201 Vec_IntPush( vTree, Ivy_DecToInt(Node) );
204 printf(
"Verification failed.\n" );
219int Ivy_TruthDecompose_rec(
unsigned uTruth,
Vec_Int_t * vTree )
222 int Supp[5], Vars0[5], Vars1[5], Vars2[5], * pVars = NULL;
223 int nSupp, Count0, Count1, Count2, nVars = 0, RetValue, fCompl = 0, i;
224 unsigned uTruthCof, uCof0, uCof1;
227 Count0 = Count1 = Count2 = nSupp = 0;
228 for ( i = 0; i < 5; i++ )
230 if ( Ivy_TruthCofactorIsConst(uTruth, i, 0, 0) )
231 Vars0[Count0++] = (i << 1) | 0;
232 else if ( Ivy_TruthCofactorIsConst(uTruth, i, 1, 0) )
233 Vars0[Count0++] = (i << 1) | 1;
234 else if ( Ivy_TruthCofactorIsConst(uTruth, i, 0, 1) )
235 Vars1[Count1++] = (i << 1) | 0;
236 else if ( Ivy_TruthCofactorIsConst(uTruth, i, 1, 1) )
237 Vars1[Count1++] = (i << 1) | 1;
240 uCof0 = Ivy_TruthCofactor( uTruth, (i << 1) | 1 );
241 uCof1 = Ivy_TruthCofactor( uTruth, (i << 1) | 0 );
242 if ( uCof0 == ~uCof1 )
243 Vars2[Count2++] = (i << 1) | 0;
244 else if ( uCof0 != uCof1 )
248 assert( Count0 == 0 || Count1 == 0 );
249 assert( Count0 == 0 || Count2 == 0 );
250 assert( Count1 == 0 || Count2 == 0 );
253 if ( Count0 == 1 && nSupp == 0 )
257 if ( Count0 == 0 && Count1 == 0 && Count2 == 0 )
258 return Ivy_TruthRecognizeMuxMaj( uTruth, Supp, nSupp, vTree );
261 Ivy_DecClear( &Node );
264 else if ( Count1 > 0 )
265 nVars = Count1, pVars = Vars1, Node.
Type =
IVY_DEC_AND, fCompl = 1, uTruth = ~uTruth;
266 else if ( Count2 > 0 )
270 Node.
nFans = nVars+(nSupp>0);
274 for ( i = 0; i < nVars; i++ )
276 uTruthCof = Ivy_TruthCofactor( uTruthCof, pVars[i] );
277 Ivy_DecSetVar( &Node, i, pVars[i] );
281 fCompl ^= ((Node.
nFans & 1) == 0);
285 assert( uTruthCof != 0 && ~uTruthCof != 0 );
287 RetValue = Ivy_TruthDecompose_rec( uTruthCof, vTree );
289 if ( RetValue == -1 )
298 Ivy_DecSetVar( &Node, nVars, RetValue );
301 fCompl ^= (uTruthCof == 0);
303 Vec_IntPush( vTree, Ivy_DecToInt(Node) );
304 return ((Vec_IntSize(vTree)-1) << 1) | fCompl;
319int Ivy_TruthRecognizeMuxMaj(
unsigned uTruth,
int * pSupp,
int nSupp,
Vec_Int_t * vTree )
322 int i, k, RetValue0, RetValue1;
323 unsigned uCof0, uCof1, Num;
327 Ivy_DecClear( &Node );
331 for ( i = 0; i < nSupp; i++ )
334 uCof0 = Ivy_TruthCofactor( uTruth, (pSupp[i] << 1) | 1 );
335 uCof1 = Ivy_TruthCofactor( uTruth, pSupp[i] << 1 );
338 for ( k = 0; k < nSupp; k++ )
342 if ( Ivy_TruthDepends(uCof0, pSupp[k]) && Ivy_TruthDepends(uCof1, pSupp[k]) )
348 RetValue0 = Ivy_TruthDecompose_rec( uCof0, vTree );
349 if ( RetValue0 == -1 )
351 RetValue1 = Ivy_TruthDecompose_rec( uCof1, vTree );
352 if ( RetValue1 == -1 )
355 Ivy_DecSetVar( &Node, 0, pSupp[i] << 1 );
356 Ivy_DecSetVar( &Node, 1, RetValue1 );
357 Ivy_DecSetVar( &Node, 2, RetValue0 );
358 Vec_IntPush( vTree, Ivy_DecToInt(Node) );
359 return ((Vec_IntSize(vTree)-1) << 1) | 0;
364 if ( Ivy_TruthWordCountOnes(uTruth) != 16 )
368 Count[0] = Count[1] = Count[2] = 0;
369 for ( i = 0; i < 8; i++ )
372 for ( k = 0; k < 3; k++ )
374 Num |= (1 << pSupp[k]);
376 if ( (uTruth & (1 << Num)) == 0 )
378 for ( k = 0; k < 3; k++ )
382 assert( Count[0] == 1 || Count[0] == 3 );
383 assert( Count[1] == 1 || Count[1] == 3 );
384 assert( Count[2] == 1 || Count[2] == 3 );
385 Ivy_DecSetVar( &Node, 0, (pSupp[0] << 1)|(Count[0] == 1) );
386 Ivy_DecSetVar( &Node, 1, (pSupp[1] << 1)|(Count[1] == 1) );
387 Ivy_DecSetVar( &Node, 2, (pSupp[2] << 1)|(Count[2] == 1) );
388 Vec_IntPush( vTree, Ivy_DecToInt(Node) );
389 return ((Vec_IntSize(vTree)-1) << 1) | 0;
406 unsigned uTruthChild, uTruthTotal;
409 Ivy_Dec_t Node = Ivy_IntToDec( Vec_IntEntry(vTree, iNode) );
412 return s_Masks[5][ !Node.
fCompl ];
414 return s_Masks[iNode][ !Node.
fCompl ];
418 return Node.
fCompl? ~uTruthTotal : uTruthTotal;
422 uTruthTotal = s_Masks[5][1];
423 for ( i = 0; i < (int)Node.
nFans; i++ )
425 Var = Ivy_DecGetVar( &Node, i );
427 uTruthTotal = (
Var & 1)? uTruthTotal & ~uTruthChild : uTruthTotal & uTruthChild;
429 return Node.
fCompl? ~uTruthTotal : uTruthTotal;
434 for ( i = 0; i < (int)Node.
nFans; i++ )
436 Var = Ivy_DecGetVar( &Node, i );
440 return Node.
fCompl? ~uTruthTotal : uTruthTotal;
445 unsigned uTruthChildC, uTruthChild1, uTruthChild0;
447 VarC = Ivy_DecGetVar( &Node, 0 );
448 Var1 = Ivy_DecGetVar( &Node, 1 );
449 Var0 = Ivy_DecGetVar( &Node, 2 );
454 uTruthChildC = (VarC & 1)? ~uTruthChildC : uTruthChildC;
455 uTruthChild1 = (
Var1 & 1)? ~uTruthChild1 : uTruthChild1;
456 uTruthChild0 = (
Var0 & 1)? ~uTruthChild0 : uTruthChild0;
458 return (uTruthChildC & uTruthChild1) | (~uTruthChildC & uTruthChild0);
460 return (uTruthChildC & uTruthChild1) | (uTruthChildC & uTruthChild0) | (uTruthChild1 & uTruthChild0);
498 Ivy_Dec_t Node = Ivy_IntToDec( Vec_IntEntry(vTree, iNode) );
501 fprintf( pFile,
"Const1%s", (Node.
fCompl?
"\'" :
"") );
503 fprintf( pFile,
"%c%s",
'a' + iNode, (Node.
fCompl?
"\'" :
"") );
507 fprintf( pFile,
"%s", (Node.
fCompl?
"\'" :
"") );
511 fprintf( pFile,
"AND(" );
512 for ( i = 0; i < (int)Node.
nFans; i++ )
514 Var = Ivy_DecGetVar( &Node, i );
516 fprintf( pFile,
"%s", (
Var & 1)?
"\'" :
"" );
517 if ( i != (
int)Node.
nFans-1 )
518 fprintf( pFile,
"," );
520 fprintf( pFile,
")%s", (Node.
fCompl?
"\'" :
"") );
524 fprintf( pFile,
"EXOR(" );
525 for ( i = 0; i < (int)Node.
nFans; i++ )
527 Var = Ivy_DecGetVar( &Node, i );
529 if ( i != (
int)Node.
nFans-1 )
530 fprintf( pFile,
"," );
533 fprintf( pFile,
")%s", (Node.
fCompl?
"\'" :
"") );
539 VarC = Ivy_DecGetVar( &Node, 0 );
540 Var1 = Ivy_DecGetVar( &Node, 1 );
541 Var0 = Ivy_DecGetVar( &Node, 2 );
544 fprintf( pFile,
"%s", (VarC & 1)?
"\'" :
"" );
545 fprintf( pFile,
"," );
547 fprintf( pFile,
"%s", (
Var1 & 1)?
"\'" :
"" );
548 fprintf( pFile,
"," );
550 fprintf( pFile,
"%s", (
Var0 & 1)?
"\'" :
"" );
551 fprintf( pFile,
")" );
570 fprintf( pFile,
"F = " );
572 fprintf( pFile,
"\n" );
588 Ivy_Obj_t * pResult, * pChild, * pNodes[16];
591 Ivy_Dec_t Node = Ivy_IntToDec( Vec_IntEntry(vTree, iNode) );
594 return Ivy_NotCond( Ivy_ManConst1(
p), Node.
fCompl );
597 pResult = Ivy_ManObj(
p, Vec_IntEntry(vFront, iNode) );
598 return Ivy_NotCond( pResult, Node.
fCompl );
603 return Ivy_NotCond( pResult, Node.
fCompl );
607 for ( i = 0; i < (int)Node.
nFans; i++ )
609 Var = Ivy_DecGetVar( &Node, i );
612 pChild = Ivy_NotCond( pChild, (
Var & 1) );
619 return Ivy_NotCond( pResult, Node.
fCompl );
625 VarC = Ivy_DecGetVar( &Node, 0 );
626 Var1 = Ivy_DecGetVar( &Node, 1 );
627 Var0 = Ivy_DecGetVar( &Node, 2 );
632 pNodes[0] = Ivy_NotCond( pNodes[0], (VarC & 1) );
633 pNodes[1] = Ivy_NotCond( pNodes[1], (
Var1 & 1) );
634 pNodes[2] = Ivy_NotCond( pNodes[2], (
Var0 & 1) );
636 return Ivy_Mux(
p, pNodes[0], pNodes[1], pNodes[2] );
638 return Ivy_Maj(
p, pNodes[0], pNodes[1], pNodes[2] );
660 Vec_IntWriteEntry( vFront, i, Ivy_LeafId(Entry) );
682 vTree = Vec_IntAlloc( 12 );
686 printf(
"Undecomposable\n" );
702 static int Counter = 0;
706 vTree = Vec_IntAlloc( 12 );
715 printf(
"%5d : ", Counter++ );
720 printf(
"Verification failed.\n" );
745 pFile = fopen(
"npn4.txt",
"r" );
746 for ( i = 0; i < 222; i++ )
750 fscanf( pFile,
"%s", Buffer );
753 uTruth |= (uTruth << 16);
778 pFile = fopen(
"npn3.txt",
"r" );
779 for ( i = 0; i < 14; i++ )
781 fscanf( pFile,
"%s", Buffer );
783 uTruth = uTruth | (uTruth << 8) | (uTruth << 16) | (uTruth << 24);
809 pFile = fopen(
"npn5.txt",
"r" );
810 for ( i = 0; i < 616126; i++ )
812 fscanf( pFile,
"%s", Buffer );
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
void Extra_PrintBinary(FILE *pFile, unsigned Sign[], int nBits)
void Ivy_TruthTestOne(unsigned uTruth)
void Ivy_TruthDsdPrint(FILE *pFile, Vec_Int_t *vTree)
Ivy_DecType_t
DECLARATIONS ///.
struct Ivy_Dec_t_ Ivy_Dec_t
unsigned Ivy_TruthDsdCompute_rec(int iNode, Vec_Int_t *vTree)
int Ivy_TruthDsd(unsigned uTruth, Vec_Int_t *vTree)
FUNCTION DEFINITIONS ///.
Ivy_Obj_t * Ivy_ManDsdConstruct(Ivy_Man_t *p, Vec_Int_t *vFront, Vec_Int_t *vTree)
Ivy_Obj_t * Ivy_ManDsdConstruct_rec(Ivy_Man_t *p, Vec_Int_t *vFront, int iNode, Vec_Int_t *vTree)
void Ivy_TruthDsdPrint_rec(FILE *pFile, int iNode, Vec_Int_t *vTree)
unsigned Ivy_TruthDsdCompute(Vec_Int_t *vTree)
void Ivy_TruthDsdComputePrint(unsigned uTruth)
typedefABC_NAMESPACE_HEADER_START struct Ivy_Man_t_ Ivy_Man_t
INCLUDES ///.
Ivy_Obj_t * Ivy_Multi(Ivy_Man_t *p, Ivy_Obj_t **pArgs, int nArgs, Ivy_Type_t Type)
Ivy_Obj_t * Ivy_Mux(Ivy_Man_t *p, Ivy_Obj_t *pC, Ivy_Obj_t *p1, Ivy_Obj_t *p0)
Ivy_Obj_t * Ivy_Maj(Ivy_Man_t *p, Ivy_Obj_t *pA, Ivy_Obj_t *pB, Ivy_Obj_t *pC)
struct Ivy_Obj_t_ Ivy_Obj_t
#define Vec_IntForEachEntry(vVec, Entry, i)
MACRO DEFINITIONS ///.