summaryrefslogtreecommitdiffstats
path: root/src/misc/extra/extraUtilMaj.c
blob: 8bdb08186da2a92eb2ab34bac0f7eb6e7cc3c81a (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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/**CFile****************************************************************

  FileName    [extraUtilMaj.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [extra]

  Synopsis    [Path enumeration.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

  Revision    [$Id: extraUtilMaj.c,v 1.0 2003/02/01 00:00:00 alanmi Exp $]

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "misc/vec/vec.h"
#include "misc/vec/vecMem.h"
#include "misc/extra/extra.h"
#include "misc/util/utilTruth.h"
#include "opt/dau/dau.h"

ABC_NAMESPACE_IMPL_START

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

typedef struct Gem_Man_t_  Gem_Man_t;
typedef struct Gem_Obj_t_  Gem_Obj_t;

struct Gem_Man_t_
{
    int          nVars;         // max variable count
    int          nWords;        // truth tabel word count
    int          nObjsAlloc;    // allocated objects
    int          nObjs;         // used objects
    Gem_Obj_t *  pObjs;         // function objects
    Vec_Mem_t *  vTtMem;        // truth table memory and hash table
    word **      pTtElems;      // elementary truth tables
    int          fVerbose;
};

struct Gem_Obj_t_ // 8 bytes
{
    unsigned     nVars   :  4;  // variable count
    unsigned     nNodes  :  4;  // node count
    unsigned     History :  8;  // (i < j) ? {vi, vj} : {vi, 0}
    unsigned     Groups  : 16;  // mask with last vars in each symmetric group
    int          Predec;        // predecessor 
};

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

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gem_PrintNode( Gem_Man_t * p, int f, char * pLabel, int fUpdate )
{
    Gem_Obj_t * pObj = p->pObjs + f; 
    int GroupsMod = pObj->Groups;
    if ( !p->fVerbose )
        return;
    printf( "Node %6d : %s  Pred = %6d  Vars = %d  Nodes = %d  History = %d%d  Profile: ", 
        f, pLabel, pObj->Predec, pObj->nVars, pObj->nNodes, pObj->History & 0xF, pObj->History >> 4 );
    Extra_PrintBinary2( stdout, (unsigned*)&GroupsMod, p->nVars ); printf("%s\n", fUpdate?" *":"");
}
Gem_Man_t * Gem_ManAlloc( int nVars, int fVerbose )
{
    Gem_Man_t * p;
    assert( nVars <= 16 );
    p = ABC_CALLOC( Gem_Man_t, 1 );
    p->nVars      = nVars;
    p->nWords     = Abc_TtWordNum( nVars );
    p->nObjsAlloc = 10000000;
    p->nObjs      = 2;
    p->pObjs      = ABC_CALLOC( Gem_Obj_t, p->nObjsAlloc );
    p->pObjs[1].nVars = p->pObjs[1].Groups = 1; // buffer
    p->vTtMem     = Vec_MemAllocForTT( nVars, 0 );
    p->pTtElems   = (word **)Extra_ArrayAlloc( nVars + 4, p->nWords, sizeof(word) );
    p->fVerbose   = fVerbose;
    Abc_TtElemInit( p->pTtElems, nVars );
    Gem_PrintNode( p, 1, "Original", 0 );
    return p;
}
int Gem_ManFree( Gem_Man_t * p )
{
    Vec_MemHashFree( p->vTtMem );
    Vec_MemFree( p->vTtMem );
    ABC_FREE( p->pTtElems );
    ABC_FREE( p->pObjs );
    ABC_FREE( p );
    return 1;
}
void Gem_ManRealloc( Gem_Man_t * p )
{
    int nObjNew = Abc_MinInt( 2 * p->nObjsAlloc, 0x7FFFFFFF );
    assert( p->nObjs == p->nObjsAlloc );
    if ( p->nObjs == 0x7FFFFFFF )
        printf( "Hard limit on the number of nodes (0x7FFFFFFF) is reached. Quitting...\n" ), exit(1);
    assert( p->nObjs < nObjNew );
    printf("Extending object storage: %d -> %d.\n", p->nObjsAlloc, nObjNew );
    p->pObjs = ABC_REALLOC( Gem_Obj_t, p->pObjs, nObjNew );
    memset( p->pObjs + p->nObjsAlloc, 0, sizeof(Gem_Obj_t) * (nObjNew - p->nObjsAlloc) );
    p->nObjsAlloc = nObjNew;
}

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

  Synopsis    [Derive groups using symmetry info.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Gem_GroupsDerive( word * pTruth, int nVars, word * pCof0, word * pCof1 )
{
    int v, Res = 1 << (nVars-1);
    for ( v = 0; v < nVars-1; v++ )
        if ( !Abc_TtVarsAreSymmetric(pTruth, nVars, v, v+1, pCof0, pCof1) )
            Res |= (1 << v);
    return Res;
}

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

  Synopsis    [Extends function f by replacing var i with a new gate.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Gem_GroupVarRemove( int Groups, int i ) // remove i-th var
{
    int Mask = i ? Abc_InfoMask( i ) : 0;
    assert( i >= 0 );
    assert( (Groups >> i) & 1 );
    return (Groups & Mask) | ((Groups & ~Mask) >> 1);
}
int Gem_GroupVarsInsert1( int Groups, int i, int fGroup ) // insert one bit after i
{
    int Mask = i+1 ? Abc_InfoMask( i+1 ) : 0;
    assert( i+1 >= 0 );
    assert( !fGroup || i == -1 || ((Groups >> i) & 1) );
    assert( fGroup == 0 || fGroup == 1 );
    return (Groups & Mask) | ((Groups & ~Mask) << 1) | (fGroup << (i+1));
}
int Gem_GroupVarsInsert3( int Groups, int i ) // insert group of 3 bits after i
{
    int Mask = i+1 ? Abc_InfoMask( i+1 ) : 0;
    assert( i+1 >= 0 );
    assert( i == -1 || (Groups >> i) & 1 );
    return (Groups & Mask) | ((Groups & ~Mask) << 3) | (0x4 << (i+1));
}
int Gem_GroupUnpack( int Groups, int * pVars )
{
    int v, nGroups = 0;
    for ( v = 0; Groups; v++, Groups >>= 1 )
        if ( Groups & 1 )
            pVars[nGroups++] = v;
    return nGroups;
}
int Gem_FuncFindPlace( word * pTruth, int nWords, int Groups, word * pBest, int fOneVar )
{
    int pLast[16], nGroups = Gem_GroupUnpack( Groups, pLast );
    int g, v, Value, BestPlace = nGroups ? pLast[nGroups - 1] : -1;
    assert( nGroups >= 0 );
    Abc_TtCopy( pBest, pTruth, nWords, 0 );
    for ( g = nGroups - 1; g >= 0; g-- )
    {
        int Limit = g ? pLast[g-1] : -1;
        for ( v = pLast[g]; v > Limit; v-- )
        {
            Abc_TtSwapAdjacent( pTruth, nWords, v+0 );
            if ( fOneVar ) 
                continue;
            Abc_TtSwapAdjacent( pTruth, nWords, v+1 );
            Abc_TtSwapAdjacent( pTruth, nWords, v+2 );
        }
        Value = memcmp(pBest, pTruth, sizeof(word)*nWords);
        if ( Value < 0 )
        {
            Abc_TtCopy( pBest, pTruth, nWords, 0 );
            BestPlace = Limit;
        }
    }
    return BestPlace;
}
void Gem_FuncExpand( Gem_Man_t * p, int f, int i )
{
    Gem_Obj_t * pNew = p->pObjs + p->nObjs, * pObj = p->pObjs + f; 
    word * pTruth   = Vec_MemReadEntry( p->vTtMem, f );
    word * pResult  = p->pTtElems[p->nVars];
    word * pCofs[2] = { p->pTtElems[p->nVars+2], p->pTtElems[p->nVars+3] };
    int v, iFunc;
    char pCanonPermC[16];
    assert( i < (int)pObj->nVars );
    assert( (int)pObj->nVars + 2 <= p->nVars );
    Abc_TtCopy( pResult, pTruth, p->nWords, 0 );
    // move i variable to the end
    for ( v = i; v < (int)pObj->nVars-1; v++ )
        Abc_TtSwapAdjacent( pResult, p->nWords, v );
    // create new symmetric group
    assert( v == (int)pObj->nVars-1 );
    Abc_TtCofactor0p( pCofs[0], pResult, p->nWords, v );
    Abc_TtCofactor1p( pCofs[1], pResult, p->nWords, v );
    Abc_TtMaj( pResult, p->pTtElems[v], p->pTtElems[v+1], p->pTtElems[v+2], p->nWords );
    Abc_TtMux( pResult, pResult, pCofs[1], pCofs[0], p->nWords );
    // canonicize
    //Extra_PrintHex( stdout, (unsigned*)pResult, pObj->nVars + 2 ); printf("\n");
    Abc_TtCanonicizePerm( pResult, pObj->nVars + 2, pCanonPermC );
    Abc_TtStretch6( pResult, Abc_MaxInt(6, pObj->nVars+2), p->nVars );
    //Extra_PrintHex( stdout, (unsigned*)pResult, pObj->nVars + 2 ); printf("\n\n");
    iFunc = Vec_MemHashInsert( p->vTtMem, pResult );
    if ( iFunc < p->nObjs )
        return;
    assert( iFunc == p->nObjs );
    pNew->nVars   = pObj->nVars + 2;
    pNew->nNodes  = pObj->nNodes + 1;
    pNew->Groups  = Gem_GroupsDerive( pResult, pNew->nVars, pCofs[0], pCofs[1] );
    pNew->Predec  = f;
    pNew->History = i; 
    Gem_PrintNode( p, iFunc, "Expand  ", 0 );

    assert( p->nObjs < p->nObjsAlloc );
    if ( ++p->nObjs == p->nObjsAlloc )
        Gem_ManRealloc( p );
}

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

  Synopsis    [Reduces function f by crossbaring variables i and j.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Gem_FuncCheckMajority( Gem_Man_t * p, int f )
{
    Gem_Obj_t * pObj = p->pObjs + f;
    word * pTruth = Vec_MemReadEntry( p->vTtMem, f );
    int Polar = Abc_TtIsFullySymmetric( pTruth, pObj->nVars );
    if ( Polar != -1 )
    {
        int nHalfVars = (pObj->nVars+1) >> 1;
        int Mask = Abc_Tt6Mask( nHalfVars );
        printf( "Found symmetric %d-variable function: ", pObj->nVars );
        Extra_PrintBinary2( stdout, (unsigned *)&Polar, pObj->nVars + 1 );
        printf( "   " );
        if ( (pObj->nVars & 1) && Polar == (Mask << nHalfVars) )
        {
            printf( "This is majority-%d.\n", pObj->nVars );
            return 0;
        }
        printf( "\n" );
    }
    return 0;
}
int Gem_FuncReduce( Gem_Man_t * p, int f, int i, int j )
{
    Gem_Obj_t * pNew = p->pObjs + p->nObjs, * pObj = p->pObjs + f; 
    word * pTruth   = Vec_MemReadEntry( p->vTtMem, f );
    word * pResult  = p->pTtElems[p->nVars];
    word * pCofs[2] = { p->pTtElems[p->nVars+2], p->pTtElems[p->nVars+3] };
    int v, iFunc;
    char pCanonPermC[16];
    assert( i < j && j < 16 );
    Abc_TtCopy( pResult, pTruth, p->nWords, 0 );
    // move j variable to the end
    for ( v = j; v < (int)pObj->nVars-1; v++ )
        Abc_TtSwapAdjacent( pResult, p->nWords, v );
    assert( v == (int)pObj->nVars-1 );
    // move i variable to the end
    for ( v = i; v < (int)pObj->nVars-2; v++ )
        Abc_TtSwapAdjacent( pResult, p->nWords, v );
    assert( v == (int)pObj->nVars-2 );
    // create new variable
    Abc_TtCofactor0p( pCofs[0], pResult, p->nWords, v+1 );
    Abc_TtCofactor1p( pCofs[1], pResult, p->nWords, v+1 );
    Abc_TtCofactor0( pCofs[0], p->nWords, v );
    Abc_TtCofactor1( pCofs[1], p->nWords, v );
    Abc_TtMux( pResult, p->pTtElems[v], pCofs[1], pCofs[0], p->nWords );
    // canonicize
    //Extra_PrintHex( stdout, (unsigned*)pResult, pObj->nVars - 1 ); printf("\n");
    Abc_TtCanonicizePerm( pResult, pObj->nVars - 1, pCanonPermC );
    Abc_TtStretch6( pResult, Abc_MaxInt(6, pObj->nVars-1), p->nVars );
    //Extra_PrintHex( stdout, (unsigned*)pResult, pObj->nVars - 1 ); printf("\n\n");
    iFunc = Vec_MemHashInsert( p->vTtMem, pResult );
    if ( iFunc < p->nObjs )
        return 0;
    assert( iFunc == p->nObjs );
    pNew->nVars   = pObj->nVars - 1;
    pNew->nNodes  = pObj->nNodes;
    pNew->Groups  = Gem_GroupsDerive( pResult, pNew->nVars, pCofs[0], pCofs[1] );
    pNew->Predec  = f;
    pNew->History = (j << 4) | i;
    Gem_PrintNode( p, iFunc, "Crossbar", 0 );
    Gem_FuncCheckMajority( p, iFunc );

    assert( p->nObjs < p->nObjsAlloc );
    if ( ++p->nObjs == p->nObjsAlloc )
        Gem_ManRealloc( p );
    return 0;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Gem_Enumerate( int nVars, int fDump, int fVerbose )
{
    abctime clk = Abc_Clock();
    Gem_Man_t * p = Gem_ManAlloc( nVars, fVerbose );
    int v, f, i, j, nObjsStop = 1;
    for ( v = 1; v <= nVars-2; v++ )
    {
        // expand functions by adding a gate
        int nObjsStopPrev = nObjsStop;
        nObjsStop = p->nObjs;
        printf( "Expanding  var %2d (functions = %10d)  ", v, p->nObjs );
        Abc_PrintTime( 0, "Time", Abc_Clock() - clk );
        for ( f = 0; f < nObjsStop; f++ )
            if ( v == (int)p->pObjs[f].nVars || (v > (int)p->pObjs[f].nVars && f >= nObjsStopPrev) )
                for ( i = 0; i < v; i++ )
                    if ( (int)p->pObjs[f].Groups & (1 << i) )
                        Gem_FuncExpand( p, f, i );
        // reduce functions by adding a crossbar
        printf( "Connecting var %2d (functions = %10d)  ", v, p->nObjs );
        Abc_PrintTime( 0, "Time", Abc_Clock() - clk );
        for ( f = nObjsStop; f < p->nObjs; f++ )
            for ( i = 0; i < (int)p->pObjs[f].nVars; i++ )
                if ( (int)p->pObjs[f].Groups & (1 << i) )
                    for ( j = i+1; j < (int)p->pObjs[f].nVars; j++ )
                        if ( (int)p->pObjs[f].Groups & (1 << j) )
                            if ( Gem_FuncReduce( p, f, i, j ) )
                                return Gem_ManFree( p );
    }
    printf( "Finished          (functions = %10d)  ", p->nObjs );
    Abc_PrintTime( 0, "Time", Abc_Clock() - clk );
    if ( fDump ) Vec_MemDumpTruthTables( p->vTtMem, "enum", nVars );
    Gem_ManFree( p );
    return 0;
}

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


ABC_NAMESPACE_IMPL_END