abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
luckySimple.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [luckySimple.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [Semi-canonical form computation package.]
8 
9  Synopsis [Truth table minimization procedures.]
10 
11  Author [Jake]
12 
13  Date [Started - August 2012]
14 
15 ***********************************************************************/
16 
17 #include "luckyInt.h"
18 
20 
21 static swapInfo* setSwapInfoPtr(int varsN)
22 {
23  int i;
24  swapInfo* x = (swapInfo*) malloc(sizeof(swapInfo));
25  x->posArray = (varInfo*) malloc (sizeof(varInfo)*(varsN+2));
26  x->realArray = (int*) malloc (sizeof(int)*(varsN+2));
27  x->varN = varsN;
28  x->realArray[0]=varsN+100;
29  for(i=1;i<=varsN;i++)
30  {
31  x->posArray[i].position=i;
32  x->posArray[i].direction=-1;
33  x->realArray[i]=i;
34  }
35  x->realArray[varsN+1]=varsN+10;
36  return x;
37 }
38 
39 
40 static void freeSwapInfoPtr(swapInfo* x)
41 {
42  free(x->posArray);
43  free(x->realArray);
44  free(x);
45 }
46 
48 {
49  int i,j,temp;
50  for(i=x->varN;i>1;i--)
51  {
52  if( i > x->realArray[x->posArray[i].position + x->posArray[i].direction] )
53  {
54  x->posArray[i].position = x->posArray[i].position + x->posArray[i].direction;
55  temp = x->realArray[x->posArray[i].position];
56  x->realArray[x->posArray[i].position] = i;
57  x->realArray[x->posArray[i].position - x->posArray[i].direction] = temp;
58  x->posArray[temp].position = x->posArray[i].position - x->posArray[i].direction;
59  for(j=x->varN;j>i;j--)
60  {
61  x->posArray[j].direction = x->posArray[j].direction * -1;
62  }
63  x->positionToSwap1 = x->posArray[temp].position - 1;
64  x->positionToSwap2 = x->posArray[i].position - 1;
65  return 1;
66  }
67 
68  }
69  return 0;
70 }
71 
73 {
74  int counter=pi->totalSwaps-1;
75  swapInfo* x= setSwapInfoPtr(pi->varN);
76  while(nextSwap(x)==1)
77  {
79  pi->swapArray[counter--]=x->positionToSwap1;
80  else
81  pi->swapArray[counter--]=x->positionToSwap2;
82  }
83 
84  freeSwapInfoPtr(x);
85 }
86 int oneBitPosition(int x, int size)
87 {
88  int i;
89  for(i=0;i<size;i++)
90  if((x>>i)&1)
91  return i;
92  return -1;
93 }
95 {
96  int i, temp=0, grayNumber;
97  for(i=1;i<=pi->totalFlips;i++)
98  {
99  grayNumber = i^(i>>1);
100  pi->flipArray[pi->totalFlips-i]=oneBitPosition(temp^grayNumber, pi->varN);
101  temp = grayNumber;
102  }
103 
104 
105 }
106 static inline int factorial(int n)
107 {
108  return (n == 1 || n == 0) ? 1 : factorial(n - 1) * n;
109 }
111 {
112  permInfo* x;
113  x = (permInfo*) malloc(sizeof(permInfo));
114  x->flipCtr=0;
115  x->varN = var;
116  x->totalFlips=(1<<var)-1;
117  x->swapCtr=0;
118  x->totalSwaps=factorial(var)-1;
119  x->flipArray = (int*) malloc(sizeof(int)*x->totalFlips);
120  x->swapArray = (int*) malloc(sizeof(int)*x->totalSwaps);
121  fillInSwapArray(x);
122  fillInFlipArray(x);
123  return x;
124 }
125 
127 {
128  free(x->flipArray);
129  free(x->swapArray);
130  free(x);
131 }
132 static inline void minWord(word* a, word* b, word* minimal, int nVars)
133 {
134  if(memCompare(a, b, nVars) == -1)
135  Kit_TruthCopy_64bit( minimal, a, nVars );
136  else
137  Kit_TruthCopy_64bit( minimal, b, nVars );
138 }
139 static inline void minWord3(word* a, word* b, word* minimal, int nVars)
140 {
141  if (memCompare(a, b, nVars) <= 0)
142  {
143  if (memCompare(a, minimal, nVars) < 0)
144  Kit_TruthCopy_64bit( minimal, a, nVars );
145  else
146  return ;
147  }
148  if (memCompare(b, minimal, nVars) <= 0)
149  Kit_TruthCopy_64bit( minimal, b, nVars );
150 }
151 void simpleMinimal(word* x, word* pAux,word* minimal, permInfo* pi, int nVars)
152 {
153  int i,j=0;
154  Kit_TruthCopy_64bit( pAux, x, nVars );
155  Kit_TruthNot_64bit( x, nVars );
156 
157  minWord(x, pAux, minimal, nVars);
158 
159  for(i=pi->totalSwaps-1;i>=0;i--)
160  {
161  Kit_TruthSwapAdjacentVars_64bit(x, nVars, pi->swapArray[i]);
162  Kit_TruthSwapAdjacentVars_64bit(pAux, nVars, pi->swapArray[i]);
163  minWord3(x, pAux, minimal, nVars);
164  }
165  for(j=pi->totalFlips-1;j>=0;j--)
166  {
167  Kit_TruthSwapAdjacentVars_64bit(x, nVars, 0);
168  Kit_TruthSwapAdjacentVars_64bit(pAux, nVars, 0);
169  Kit_TruthChangePhase_64bit(x, nVars, pi->flipArray[j]);
170  Kit_TruthChangePhase_64bit(pAux, nVars, pi->flipArray[j]);
171  minWord3(x, pAux, minimal, nVars);
172  for(i=pi->totalSwaps-1;i>=0;i--)
173  {
174  Kit_TruthSwapAdjacentVars_64bit(x, nVars, pi->swapArray[i]);
175  Kit_TruthSwapAdjacentVars_64bit(pAux, nVars, pi->swapArray[i]);
176  minWord3(x, pAux, minimal, nVars);
177  }
178  }
179  Kit_TruthCopy_64bit( x, minimal, nVars );
180 }
181 
182 
char * malloc()
static void minWord3(word *a, word *b, word *minimal, int nVars)
Definition: luckySimple.c:139
VOID_HACK free()
void Kit_TruthCopy_64bit(word *pOut, word *pIn, int nVars)
Definition: luckySwap.c:136
static void freeSwapInfoPtr(swapInfo *x)
Definition: luckySimple.c:40
permInfo * setPermInfoPtr(int var)
Definition: luckySimple.c:110
int totalSwaps
Definition: lucky.h:28
int var(Lit p)
Definition: SolverTypes.h:62
void Kit_TruthSwapAdjacentVars_64bit(word *pInOut, int nVars, int iVar)
Definition: luckySwap.c:141
int totalFlips
Definition: lucky.h:31
int flipCtr
Definition: lucky.h:30
int * flipArray
Definition: lucky.h:29
static void minWord(word *a, word *b, word *minimal, int nVars)
Definition: luckySimple.c:132
void fillInFlipArray(permInfo *pi)
Definition: luckySimple.c:94
static int factorial(int n)
Definition: luckySimple.c:106
int positionToSwap1
Definition: luckyInt.h:85
int positionToSwap2
Definition: luckyInt.h:86
void fillInSwapArray(permInfo *pi)
Definition: luckySimple.c:72
unsigned __int64 word
DECLARATIONS ///.
Definition: kitPerm.c:36
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
varInfo * posArray
Definition: luckyInt.h:82
static int size
Definition: cuddSign.c:86
int swapCtr
Definition: lucky.h:27
int * swapArray
Definition: lucky.h:26
void Kit_TruthNot_64bit(word *pIn, int nVars)
Definition: luckySwap.c:130
int position
Definition: luckyInt.h:76
int oneBitPosition(int x, int size)
Definition: luckySimple.c:86
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
static ABC_NAMESPACE_IMPL_START swapInfo * setSwapInfoPtr(int varsN)
Definition: luckySimple.c:21
int varN
Definition: lucky.h:25
int * realArray
Definition: luckyInt.h:83
void freePermInfoPtr(permInfo *x)
Definition: luckySimple.c:126
int direction
Definition: luckyInt.h:75
int varN
Definition: luckyInt.h:84
ABC_NAMESPACE_IMPL_START int memCompare(word *x, word *y, int nVars)
Definition: lucky.c:22
void simpleMinimal(word *x, word *pAux, word *minimal, permInfo *pi, int nVars)
Definition: luckySimple.c:151
Definition: lucky.h:23
void Kit_TruthChangePhase_64bit(word *pInOut, int nVars, int iVar)
Definition: luckySwap.c:100
int nextSwap(swapInfo *x)
Definition: luckySimple.c:47