summaryrefslogtreecommitdiffstats
path: root/src/map/amap/amapUniq.c
blob: e2be4343a4e2b2d180d4d276f06adca7c7a866d1 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/**CFile****************************************************************

  FileName    [amapUniq.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Technology mapper for standard cells.]

  Synopsis    [Checks if the structural node already exists.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - June 20, 2005.]

  Revision    [$Id: amapUniq.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]

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

#include "amapInt.h"

ABC_NAMESPACE_IMPL_START


////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

/**Function*************************************************************

  Synopsis    [Checks if the entry exists and returns value.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Vec_IntCheckWithMask( Vec_Int_t * p, int Entry )
{
    int i;
    for ( i = 0; i < p->nSize; i++ )
        if ( (0xffff & p->pArray[i]) == (0xffff & Entry) )
            return p->pArray[i] >> 16;
    return -1;
}

/**Function*************************************************************

  Synopsis    [Pushes entry in the natural order.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Vec_IntPushOrderWithMask( Vec_Int_t * p, int Entry )
{
    int i;
    if ( p->nSize == p->nCap )
        Vec_IntGrow( p, 2 * p->nCap );
    p->nSize++;
    for ( i = p->nSize-2; i >= 0; i-- )
        if ( (0xffff & p->pArray[i]) > (0xffff & Entry) )
            p->pArray[i+1] = p->pArray[i];
        else
            break;
    p->pArray[i+1] = Entry;
}

/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Amap_LibFindNode( Amap_Lib_t * pLib, int iFan0, int iFan1, int fXor )
{
    if ( fXor )
        return Vec_IntCheckWithMask( (Vec_Int_t *)Vec_PtrEntry(pLib->vRulesX, iFan0), iFan1 );
    else
        return Vec_IntCheckWithMask( (Vec_Int_t *)Vec_PtrEntry(pLib->vRules, iFan0), iFan1 );
}

/**Function*************************************************************

  Synopsis    [Checks if the three-argument rule exist.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Amap_LibFindMux( Amap_Lib_t * p, int iFan0, int iFan1, int iFan2 )
{
    int x;
    for ( x = 0; x < Vec_IntSize(p->vRules3); x += 4 )
    {
        if ( Vec_IntEntry(p->vRules3, x)   == iFan0 &&
             Vec_IntEntry(p->vRules3, x+1) == iFan1 &&
             Vec_IntEntry(p->vRules3, x+2) == iFan2 )
        {
            return Vec_IntEntry(p->vRules3, x+3);
        }
    }
    return -1;
}

/**Function*************************************************************

  Synopsis    [Creates a new node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Amap_Nod_t * Amap_LibCreateObj( Amap_Lib_t * p )
{
    Amap_Nod_t * pNode;
    if ( p->nNodes == p->nNodesAlloc )
    {
        p->pNodes = ABC_REALLOC( Amap_Nod_t, p->pNodes, 2*p->nNodesAlloc );
        p->nNodesAlloc *= 2;
    }
    pNode = Amap_LibNod( p, p->nNodes );
    memset( pNode, 0, sizeof(Amap_Nod_t) );
    pNode->Id = p->nNodes++;
    Vec_PtrPush( p->vRules, Vec_IntAlloc(8) );
    Vec_PtrPush( p->vRules, Vec_IntAlloc(8) );
    Vec_PtrPush( p->vRulesX, Vec_IntAlloc(8) );
    Vec_PtrPush( p->vRulesX, Vec_IntAlloc(8) );
    return pNode;
}

/**Function*************************************************************

  Synopsis    [Creates a new node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Amap_LibCreateVar( Amap_Lib_t * p )
{
    Amap_Nod_t * pNode;
    // start the manager
    assert( p->pNodes == NULL );
    p->nNodesAlloc = 256;
    p->pNodes = ABC_ALLOC( Amap_Nod_t, p->nNodesAlloc );
    // create the first node
    pNode = Amap_LibCreateObj( p );
    p->pNodes->Type = AMAP_OBJ_PI;
    p->pNodes->nSuppSize = 1;
    return 0;
}

/**Function*************************************************************

  Synopsis    [Creates a new node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Amap_LibCreateNode( Amap_Lib_t * p, int iFan0, int iFan1, int fXor )
{
    Amap_Nod_t * pNode;
    int iFan;
    if ( iFan0 < iFan1 )
    {
        iFan  = iFan0;
        iFan0 = iFan1;
        iFan1 = iFan;
    }
    pNode = Amap_LibCreateObj( p );
    pNode->Type  = fXor? AMAP_OBJ_XOR : AMAP_OBJ_AND;
    pNode->nSuppSize = p->pNodes[Abc_Lit2Var(iFan0)].nSuppSize + p->pNodes[Abc_Lit2Var(iFan1)].nSuppSize;
    pNode->iFan0 = iFan0;
    pNode->iFan1 = iFan1;
if ( p->fVerbose )
printf( "Creating node %5d %c :  iFan0 = %5d%c  iFan1 = %5d%c\n", 
pNode->Id, (fXor?'x':' '), 
Abc_Lit2Var(iFan0), (Abc_LitIsCompl(iFan0)?'-':'+'), 
Abc_Lit2Var(iFan1), (Abc_LitIsCompl(iFan1)?'-':'+') );

    if ( fXor )
    {
        if ( iFan0 == iFan1 )
            Vec_IntPushOrderWithMask( (Vec_Int_t *)Vec_PtrEntry(p->vRulesX, iFan0), (pNode->Id << 16) | iFan1 );
        else
        {
            Vec_IntPushOrderWithMask( (Vec_Int_t *)Vec_PtrEntry(p->vRulesX, iFan0), (pNode->Id << 16) | iFan1 );
            Vec_IntPushOrderWithMask( (Vec_Int_t *)Vec_PtrEntry(p->vRulesX, iFan1), (pNode->Id << 16) | iFan0 );
        }
    }
    else
    {
        if ( iFan0 == iFan1 )
            Vec_IntPushOrderWithMask( (Vec_Int_t *)Vec_PtrEntry(p->vRules, iFan0), (pNode->Id << 16) | iFan1 );
        else
        {
            Vec_IntPushOrderWithMask( (Vec_Int_t *)Vec_PtrEntry(p->vRules, iFan0), (pNode->Id << 16) | iFan1 );
            Vec_IntPushOrderWithMask( (Vec_Int_t *)Vec_PtrEntry(p->vRules, iFan1), (pNode->Id << 16) | iFan0 );
        }
    }
    return pNode->Id;
}

/**Function*************************************************************

  Synopsis    [Creates a new node.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Amap_LibCreateMux( Amap_Lib_t * p, int iFan0, int iFan1, int iFan2 )
{
    Amap_Nod_t * pNode;
    pNode = Amap_LibCreateObj( p );
    pNode->Type  = AMAP_OBJ_MUX;
    pNode->nSuppSize = p->pNodes[Abc_Lit2Var(iFan0)].nSuppSize + p->pNodes[Abc_Lit2Var(iFan1)].nSuppSize + p->pNodes[Abc_Lit2Var(iFan2)].nSuppSize;
    pNode->iFan0 = iFan0;
    pNode->iFan1 = iFan1;
    pNode->iFan2 = iFan2;
if ( p->fVerbose )
printf( "Creating node %5d %c :  iFan0 = %5d%c  iFan1 = %5d%c  iFan2 = %5d%c\n", 
pNode->Id, 'm', 
Abc_Lit2Var(iFan0), (Abc_LitIsCompl(iFan0)?'-':'+'), 
Abc_Lit2Var(iFan1), (Abc_LitIsCompl(iFan1)?'-':'+'), 
Abc_Lit2Var(iFan2), (Abc_LitIsCompl(iFan2)?'-':'+') );

    Vec_IntPush( p->vRules3, iFan0 );
    Vec_IntPush( p->vRules3, iFan1 );
    Vec_IntPush( p->vRules3, iFan2 );
    Vec_IntPush( p->vRules3, pNode->Id );
    return pNode->Id;
}

/**Function*************************************************************

  Synopsis    [Allocates triangular lookup table.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int ** Amap_LibLookupTableAlloc( Vec_Ptr_t * vVec, int fVerbose )
{
    Vec_Int_t * vOne;
    int ** pRes;
    int i, k, nTotal, nSize, nEntries, Value;
    // count the total size
    nEntries = nSize = Vec_PtrSize( vVec );
    Vec_PtrForEachEntry( Vec_Int_t *, vVec, vOne, i )
        nEntries += Vec_IntSize(vOne);
    pRes = (int **)ABC_ALLOC( char, nSize * sizeof(void *) + nEntries * sizeof(int) );
    pRes[0] = (int *)((char *)pRes + nSize * sizeof(void *));
    nTotal = 0;
    Vec_PtrForEachEntry( Vec_Int_t *, vVec, vOne, i )
    {
        pRes[i] = pRes[0] + nTotal;
        nTotal += Vec_IntSize(vOne) + 1;
        if ( fVerbose )
        printf( "%d : ", i );
        Vec_IntForEachEntry( vOne, Value, k )
        {
            pRes[i][k] = Value;
            if ( fVerbose )
            printf( "%d(%d) ", Value&0xffff, Value>>16 );
        }
        if ( fVerbose )
        printf( "\n" );
        pRes[i][k] = 0;
    }
    assert( nTotal == nEntries );
    return pRes;
}

////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


ABC_NAMESPACE_IMPL_END