summaryrefslogtreecommitdiffstats
path: root/src/bool/lucky/luckySimple.c
blob: f9128f3f1c0ed12ab9778d36e0834e7e3f551f17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**CFile****************************************************************

  FileName    [luckySimple.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Semi-canonical form computation package.]

  Synopsis    [Truth table minimization procedures.]

  Author      [Jake]

  Date        [Started - August 2012]

***********************************************************************/

#include "luckyInt.h"

ABC_NAMESPACE_IMPL_START

static swapInfo* setSwapInfoPtr(int varsN)
{
    int i;
    swapInfo* x = (swapInfo*) malloc(sizeof(swapInfo));
    x->posArray = (varInfo*) malloc (sizeof(varInfo)*(varsN+2));
    x->realArray = (int*) malloc (sizeof(int)*(varsN+2));
    x->varN = varsN;
    x->realArray[0]=varsN+100;
    for(i=1;i<=varsN;i++)
    {
        x->posArray[i].position=i;
        x->posArray[i].direction=-1;
        x->realArray[i]=i;
    }    
    x->realArray[varsN+1]=varsN+10;
    return x;
}


static void freeSwapInfoPtr(swapInfo* x)
{
    free(x->posArray);
    free(x->realArray);
    free(x);
}

int nextSwap(swapInfo* x)
{
    int i,j,temp;
    for(i=x->varN;i>1;i--)
    {
        if( i > x->realArray[x->posArray[i].position + x->posArray[i].direction] )
        {
            x->posArray[i].position = x->posArray[i].position + x->posArray[i].direction;
            temp = x->realArray[x->posArray[i].position];
            x->realArray[x->posArray[i].position] = i; 
            x->realArray[x->posArray[i].position - x->posArray[i].direction] = temp;
            x->posArray[temp].position = x->posArray[i].position - x->posArray[i].direction; 
            for(j=x->varN;j>i;j--)
            {
                x->posArray[j].direction =     x->posArray[j].direction * -1;
            }
            x->positionToSwap1 = x->posArray[temp].position - 1;
            x->positionToSwap2 = x->posArray[i].position - 1;            
            return 1;
        }
        
    }
    return 0;    
}

void fillInSwapArray(permInfo* pi)
{
    int counter=pi->totalSwaps-1;
    swapInfo* x= setSwapInfoPtr(pi->varN);
    while(nextSwap(x)==1)
    {
        if(x->positionToSwap1<x->positionToSwap2)
            pi->swapArray[counter--]=x->positionToSwap1;
        else
            pi->swapArray[counter--]=x->positionToSwap2;
    }
    
    freeSwapInfoPtr(x);    
}
int oneBitPosition(int x, int size)
{
    int i;
    for(i=0;i<size;i++)
        if((x>>i)&1)
            return i;
    return -1;
}
void fillInFlipArray(permInfo* pi)
{
    int i, temp=0, grayNumber;
    for(i=1;i<=pi->totalFlips;i++)
    {
        grayNumber = i^(i>>1);
        pi->flipArray[pi->totalFlips-i]=oneBitPosition(temp^grayNumber, pi->varN);
        temp = grayNumber;        
    }
    
    
}
static inline int factorial(int n)
{
    return (n == 1 || n == 0) ? 1 : factorial(n - 1) * n;
}
permInfo* setPermInfoPtr(int var)
{
    permInfo* x;
    x = (permInfo*) malloc(sizeof(permInfo));
    x->flipCtr=0;
    x->varN = var; 
    x->totalFlips=(1<<var)-1;
    x->swapCtr=0;
    x->totalSwaps=factorial(var)-1;
    x->flipArray = (int*) malloc(sizeof(int)*x->totalFlips);
    x->swapArray = (int*) malloc(sizeof(int)*x->totalSwaps);
    fillInSwapArray(x);
    fillInFlipArray(x);
    return x;
}

void freePermInfoPtr(permInfo* x)
{
    free(x->flipArray);
    free(x->swapArray);
    free(x);
}
static inline void minWord(word* a, word* b, word* minimal, int nVars)
{
    if(memCompare(a, b, nVars) == -1)
        Kit_TruthCopy_64bit( minimal, a, nVars );
    else
        Kit_TruthCopy_64bit( minimal, b, nVars );
}
static inline void minWord3(word* a, word* b, word* minimal, int nVars)
{ 
    if (memCompare(a, b, nVars) <= 0)
    {
        if (memCompare(a, minimal, nVars) < 0) 
            Kit_TruthCopy_64bit( minimal, a, nVars ); 
        else 
            return ;
    }    
    if (memCompare(b, minimal, nVars) <= 0)
        Kit_TruthCopy_64bit( minimal, b, nVars );
}
void simpleMinimal(word* x, word* pAux,word* minimal, permInfo* pi, int nVars)
{
    int i,j=0;
    Kit_TruthCopy_64bit( pAux, x, nVars );
    Kit_TruthNot_64bit( x, nVars );
    
    minWord(x, pAux, minimal, nVars);
    
    for(i=pi->totalSwaps-1;i>=0;i--)
    {
        Kit_TruthSwapAdjacentVars_64bit(x, nVars, pi->swapArray[i]);
        Kit_TruthSwapAdjacentVars_64bit(pAux, nVars, pi->swapArray[i]);
        minWord3(x, pAux, minimal, nVars);
    }
    for(j=pi->totalFlips-1;j>=0;j--)
    {
        Kit_TruthSwapAdjacentVars_64bit(x, nVars, 0);
        Kit_TruthSwapAdjacentVars_64bit(pAux, nVars, 0);
        Kit_TruthChangePhase_64bit(x, nVars, pi->flipArray[j]);
        Kit_TruthChangePhase_64bit(pAux, nVars, pi->flipArray[j]);
        minWord3(x, pAux, minimal, nVars);
        for(i=pi->totalSwaps-1;i>=0;i--)
        {
            Kit_TruthSwapAdjacentVars_64bit(x, nVars, pi->swapArray[i]);
            Kit_TruthSwapAdjacentVars_64bit(pAux, nVars, pi->swapArray[i]);
            minWord3(x, pAux, minimal, nVars);
        }
    } 
    Kit_TruthCopy_64bit( x, minimal, nVars );    
}


ABC_NAMESPACE_IMPL_END