summaryrefslogtreecommitdiffstats
path: root/src/misc/npn/npn.c
blob: ad4b4de64893697864578a285ed3ef9fa00488fd (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
/**CFile****************************************************************

  FileName    [npn.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName []

  Synopsis    []

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "extra.h"
#include "npn.h"
#include "vec.h"

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

static int Npn_CompareVecs( void ** p0, void ** p1 );
static Vec_Int_t * Npn_GetSignature( unsigned uTruth, int nVars );
static void Npn_VecPrint( FILE * pFile, Vec_Int_t * vVec );
static int Npn_VecProperty( Vec_Int_t * vVec );

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFITIONS                           ///
////////////////////////////////////////////////////////////////////////

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Npn_Experiment()
{
    Vec_Int_t ** pVecs;
    FILE * pFile;
    int nFuncs, Num, i;

    pFile = fopen( "npn5.txt", "r" );
    pVecs = ALLOC( Vec_Int_t *, 1000000 );
    for ( i = 0; ; i++ )
    {
        if ( fscanf( pFile, "%x", &Num ) != 1 )
            break;
        pVecs[i] = Npn_GetSignature( Num, 5 );
        if ( Npn_VecProperty( pVecs[i] ) )
        {
            printf( "1s = %2d. ", Extra_CountOnes((unsigned char *)&Num, (1 << 5) / 8) );
            Extra_PrintHex( stdout, Num, 5 ); fprintf( stdout, "\n" );
        }
        
    }
    nFuncs = i;
    printf( "Read %d numbers.\n", nFuncs );
    fclose( pFile );
    /*
    // sort the vectors
    qsort( (void *)pVecs, nFuncs, sizeof(void *), 
            (int (*)(const void *, const void *)) Npn_CompareVecs );
    pFile = fopen( "npn5ar.txt", "w" );
    for ( i = 0; i < nFuncs; i++ )
    {
        Npn_VecPrint( pFile, pVecs[i] );
        Vec_IntFree( pVecs[i] );
    }
    fclose( pFile );
    */

    free( pVecs );
}


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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Int_t * Npn_GetSignature( unsigned uTruth, int nVars )
{
    // elementary truth tables
    static unsigned Signs[5] = {
        0xAAAAAAAA,    // 1010 1010 1010 1010 1010 1010 1010 1010
        0xCCCCCCCC,    // 1010 1010 1010 1010 1010 1010 1010 1010
        0xF0F0F0F0,    // 1111 0000 1111 0000 1111 0000 1111 0000
        0xFF00FF00,    // 1111 1111 0000 0000 1111 1111 0000 0000
        0xFFFF0000     // 1111 1111 1111 1111 0000 0000 0000 0000
    };
    Vec_Int_t * vVec;
    unsigned uCofactor;
    int Count0, Count1, i;
    int nBytes = (1 << nVars) / 8;

    // collect the number of 1s in each cofactor
    vVec = Vec_IntAlloc( 5 );
    for ( i = 0; i < nVars; i++ )
    {
        uCofactor = uTruth & ~Signs[i];
        Count0 = Extra_CountOnes( (unsigned char *)&uCofactor, nBytes );
        uCofactor = uTruth &  Signs[i];
        Count1 = Extra_CountOnes( (unsigned char *)&uCofactor, nBytes );
        if ( Count0 < Count1 )
            Vec_IntPush( vVec, Count0 );
        else
            Vec_IntPush( vVec, Count1 );
    }
    // sort them
    Vec_IntSort( vVec, 0 );
    return vVec;
    
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Npn_CompareVecs( void ** p0, void ** p1 )
{
    Vec_Int_t * vVec0 = *p0;
    Vec_Int_t * vVec1 = *p1;
    int i;
    assert( vVec0->nSize == vVec1->nSize );
    for ( i = 0; i < vVec0->nSize; i++ )
        if ( vVec0->pArray[i] < vVec1->pArray[i] )
            return -1;
        else if ( vVec0->pArray[i] > vVec1->pArray[i] )
            return 1;
    return 0;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Npn_VecPrint( FILE * pFile, Vec_Int_t * vVec )
{
    int i;
    for ( i = 0; i < vVec->nSize; i++ )
        fprintf( pFile, "%2d ", vVec->pArray[i] );
    fprintf( pFile, "\n" );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Npn_VecProperty( Vec_Int_t * vVec )
{
    int i;
    for ( i = 0; i < vVec->nSize; i++ )
        if ( vVec->pArray[i] != i + 1 )
            return 0;
    return 1;
}

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