kitIsop.c 12.1 KB
Newer Older
Alan Mishchenko committed
1 2
/**CFile****************************************************************

Alan Mishchenko committed
3
  FileName    [kitIsop.c]
Alan Mishchenko committed
4 5 6

  SystemName  [ABC: Logic synthesis and verification system.]

Alan Mishchenko committed
7
  PackageName [Computation kit.]
Alan Mishchenko committed
8

Alan Mishchenko committed
9
  Synopsis    [ISOP computation based on Morreale's algorithm.]
Alan Mishchenko committed
10 11 12 13 14

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

Alan Mishchenko committed
15
  Date        [Ver. 1.0. Started - Dec 6, 2006.]
Alan Mishchenko committed
16

Alan Mishchenko committed
17
  Revision    [$Id: kitIsop.c,v 1.00 2006/12/06 00:00:00 alanmi Exp $]
Alan Mishchenko committed
18 19 20

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

Alan Mishchenko committed
21
#include "kit.h"
Alan Mishchenko committed
22

23 24 25
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
26 27 28 29
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

Alan Mishchenko committed
30
// ISOP computation fails if intermediate memory usage exceed this limit
31
#define KIT_ISOP_MEM_LIMIT  (1<<20)
Alan Mishchenko committed
32

Alan Mishchenko committed
33
// static procedures to compute ISOP
Alan Mishchenko committed
34 35
static unsigned * Kit_TruthIsop_rec( unsigned * puOn, unsigned * puOnDc, int nVars, Kit_Sop_t * pcRes, Vec_Int_t * vStore );
static unsigned   Kit_TruthIsop5_rec( unsigned uOn, unsigned uOnDc, int nVars, Kit_Sop_t * pcRes, Vec_Int_t * vStore );
Alan Mishchenko committed
36 37 38 39 40 41 42

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

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

Alan Mishchenko committed
43
  Synopsis    [Computes ISOP from TT.]
Alan Mishchenko committed
44

Alan Mishchenko committed
45
  Description [Returns the cover in vMemory. Uses the rest of array in vMemory
Alan Mishchenko committed
46
  as an intermediate memory storage. Returns the cover with -1 cubes, if the
Alan Mishchenko committed
47
  the computation exceeded the memory limit (KIT_ISOP_MEM_LIMIT words of
Alan Mishchenko committed
48
  intermediate data).]
Alan Mishchenko committed
49 50 51 52 53 54
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
55
int Kit_TruthIsop( unsigned * puTruth, int nVars, Vec_Int_t * vMemory, int fTryBoth )
Alan Mishchenko committed
56
{
Alan Mishchenko committed
57 58
    Kit_Sop_t cRes, * pcRes = &cRes;
    Kit_Sop_t cRes2, * pcRes2 = &cRes2;
Alan Mishchenko committed
59
    unsigned * pResult;
Alan Mishchenko committed
60
    int RetValue = 0;
Alan Mishchenko committed
61
    assert( nVars >= 0 && nVars <= 16 );
Alan Mishchenko committed
62
    // if nVars < 5, make sure it does not depend on those vars
Alan Mishchenko committed
63
//    for ( i = nVars; i < 5; i++ )
Alan Mishchenko committed
64
//        assert( !Kit_TruthVarInSupport(puTruth, 5, i) );
Alan Mishchenko committed
65
    // prepare memory manager
Alan Mishchenko committed
66 67
    Vec_IntClear( vMemory );
    Vec_IntGrow( vMemory, KIT_ISOP_MEM_LIMIT );
Alan Mishchenko committed
68
    // compute ISOP for the direct polarity
Alan Mishchenko committed
69
    pResult = Kit_TruthIsop_rec( puTruth, puTruth, nVars, pcRes, vMemory );
Alan Mishchenko committed
70 71
    if ( pcRes->nCubes == -1 )
    {
Alan Mishchenko committed
72
        vMemory->nSize = -1;
Alan Mishchenko committed
73
        return -1;
Alan Mishchenko committed
74
    }
Alan Mishchenko committed
75
    assert( Kit_TruthIsEqual( puTruth, pResult, nVars ) );
Alan Mishchenko committed
76 77
    if ( pcRes->nCubes == 0 || (pcRes->nCubes == 1 && pcRes->pCubes[0] == 0) )
    {
Alan Mishchenko committed
78
        vMemory->pArray[0] = 0;
Alan Mishchenko committed
79 80 81
        Vec_IntShrink( vMemory, pcRes->nCubes );
        return 0;
    }
Alan Mishchenko committed
82 83 84
    if ( fTryBoth )
    {
        // compute ISOP for the complemented polarity
Alan Mishchenko committed
85
        Kit_TruthNot( puTruth, puTruth, nVars );
Alan Mishchenko committed
86
        pResult = Kit_TruthIsop_rec( puTruth, puTruth, nVars, pcRes2, vMemory );
Alan Mishchenko committed
87 88
        if ( pcRes2->nCubes >= 0 )
        {
Alan Mishchenko committed
89
            assert( Kit_TruthIsEqual( puTruth, pResult, nVars ) );
90
            if ( pcRes->nCubes > pcRes2->nCubes || (pcRes->nCubes == pcRes2->nCubes && pcRes->nLits > pcRes2->nLits) )
Alan Mishchenko committed
91 92 93 94 95
            {
                RetValue = 1;
                pcRes = pcRes2;
            }
        }
Alan Mishchenko committed
96
        Kit_TruthNot( puTruth, puTruth, nVars );
Alan Mishchenko committed
97
    }
Alan Mishchenko committed
98
//    printf( "%d ", vMemory->nSize );
Alan Mishchenko committed
99
    // move the cover representation to the beginning of the memory buffer
Alan Mishchenko committed
100 101
    memmove( vMemory->pArray, pcRes->pCubes, pcRes->nCubes * sizeof(unsigned) );
    Vec_IntShrink( vMemory, pcRes->nCubes );
Alan Mishchenko committed
102
    return RetValue;
Alan Mishchenko committed
103
}
104
void Kit_TruthIsopPrintCover( Vec_Int_t * vCover, int nVars, int fCompl )
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
{
    int i, k, Entry, Literal;
    if ( Vec_IntSize(vCover) == 0 || (Vec_IntSize(vCover) == 1 && Vec_IntEntry(vCover, 0) == 0) )
    {
        printf( "Constant %d\n", Vec_IntSize(vCover) );
        return;
    }
    Vec_IntForEachEntry( vCover, Entry, i )
    { 
        for ( k = 0; k < nVars; k++ )
        {
            Literal = 3 & (Entry >> (k << 1));
            if ( Literal == 1 ) // neg literal
                printf( "0" );
            else if ( Literal == 2 ) // pos literal
                printf( "1" );
            else if ( Literal == 0 ) 
                printf( "-" );
            else assert( 0 );
        }
125
        printf( " %d\n", !fCompl );
126 127
    }
}
128 129 130 131 132
void Kit_TruthIsopPrint( unsigned * puTruth, int nVars, Vec_Int_t * vCover, int fTryBoth )
{
    int fCompl = Kit_TruthIsop( puTruth, nVars, vCover, fTryBoth );
    Kit_TruthIsopPrintCover( vCover, nVars, fCompl );
}
Alan Mishchenko committed
133 134 135

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

Alan Mishchenko committed
136
  Synopsis    [Computes ISOP 6 variables or more.]
Alan Mishchenko committed
137 138 139 140 141 142 143 144

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
145
unsigned * Kit_TruthIsop_rec( unsigned * puOn, unsigned * puOnDc, int nVars, Kit_Sop_t * pcRes, Vec_Int_t * vStore )
Alan Mishchenko committed
146
{
Alan Mishchenko committed
147 148
    Kit_Sop_t cRes0, cRes1, cRes2;
    Kit_Sop_t * pcRes0 = &cRes0, * pcRes1 = &cRes1, * pcRes2 = &cRes2;
Alan Mishchenko committed
149
    unsigned * puRes0, * puRes1, * puRes2;
Alan Mishchenko committed
150 151
    unsigned * puOn0, * puOn1, * puOnDc0, * puOnDc1, * pTemp, * pTemp0, * pTemp1;
    int i, k, Var, nWords, nWordsAll;
Alan Mishchenko committed
152
//    assert( Kit_TruthIsImply( puOn, puOnDc, nVars ) );
Alan Mishchenko committed
153
    // allocate room for the resulting truth table
Alan Mishchenko committed
154
    nWordsAll = Kit_TruthWordNum( nVars );
Alan Mishchenko committed
155 156 157 158 159 160
    pTemp = Vec_IntFetch( vStore, nWordsAll );
    if ( pTemp == NULL )
    {
        pcRes->nCubes = -1;
        return NULL;
    }
Alan Mishchenko committed
161
    // check for constants
Alan Mishchenko committed
162
    if ( Kit_TruthIsConst0( puOn, nVars ) )
Alan Mishchenko committed
163
    {
164
        pcRes->nLits  = 0;
Alan Mishchenko committed
165 166
        pcRes->nCubes = 0;
        pcRes->pCubes = NULL;
Alan Mishchenko committed
167
        Kit_TruthClear( pTemp, nVars );
Alan Mishchenko committed
168
        return pTemp;
Alan Mishchenko committed
169
    }
Alan Mishchenko committed
170
    if ( Kit_TruthIsConst1( puOnDc, nVars ) )
Alan Mishchenko committed
171
    {
172
        pcRes->nLits  = 0;
Alan Mishchenko committed
173
        pcRes->nCubes = 1;
Alan Mishchenko committed
174 175 176 177 178 179
        pcRes->pCubes = Vec_IntFetch( vStore, 1 );
        if ( pcRes->pCubes == NULL )
        {
            pcRes->nCubes = -1;
            return NULL;
        }
Alan Mishchenko committed
180
        pcRes->pCubes[0] = 0;
Alan Mishchenko committed
181
        Kit_TruthFill( pTemp, nVars );
Alan Mishchenko committed
182
        return pTemp;
Alan Mishchenko committed
183
    }
Alan Mishchenko committed
184
    assert( nVars > 0 );
Alan Mishchenko committed
185 186
    // find the topmost var
    for ( Var = nVars-1; Var >= 0; Var-- )
Alan Mishchenko committed
187 188
        if ( Kit_TruthVarInSupport( puOn, nVars, Var ) || 
             Kit_TruthVarInSupport( puOnDc, nVars, Var ) )
Alan Mishchenko committed
189 190
             break;
    assert( Var >= 0 );
Alan Mishchenko committed
191
    // consider a simple case when one-word computation can be used
Alan Mishchenko committed
192 193
    if ( Var < 5 )
    {
Alan Mishchenko committed
194
        unsigned uRes = Kit_TruthIsop5_rec( puOn[0], puOnDc[0], Var+1, pcRes, vStore );
Alan Mishchenko committed
195 196 197
        for ( i = 0; i < nWordsAll; i++ )
            pTemp[i] = uRes;
        return pTemp;
Alan Mishchenko committed
198
    }
Alan Mishchenko committed
199
    assert( Var >= 5 );
Alan Mishchenko committed
200
    nWords = Kit_TruthWordNum( Var );
Alan Mishchenko committed
201
    // cofactor
Alan Mishchenko committed
202 203 204
    puOn0   = puOn;    puOn1   = puOn + nWords;
    puOnDc0 = puOnDc;  puOnDc1 = puOnDc + nWords;
    pTemp0  = pTemp;   pTemp1  = pTemp + nWords;
Alan Mishchenko committed
205
    // solve for cofactors
Alan Mishchenko committed
206
    Kit_TruthSharp( pTemp0, puOn0, puOnDc1, Var );
Alan Mishchenko committed
207
    puRes0 = Kit_TruthIsop_rec( pTemp0, puOnDc0, Var, pcRes0, vStore );
Alan Mishchenko committed
208 209 210 211 212
    if ( pcRes0->nCubes == -1 )
    {
        pcRes->nCubes = -1;
        return NULL;
    }
Alan Mishchenko committed
213
    Kit_TruthSharp( pTemp1, puOn1, puOnDc0, Var );
Alan Mishchenko committed
214
    puRes1 = Kit_TruthIsop_rec( pTemp1, puOnDc1, Var, pcRes1, vStore );
Alan Mishchenko committed
215 216 217 218 219
    if ( pcRes1->nCubes == -1 )
    {
        pcRes->nCubes = -1;
        return NULL;
    }
Alan Mishchenko committed
220 221 222 223
    Kit_TruthSharp( pTemp0, puOn0, puRes0, Var );
    Kit_TruthSharp( pTemp1, puOn1, puRes1, Var );
    Kit_TruthOr( pTemp0, pTemp0, pTemp1, Var );
    Kit_TruthAnd( pTemp1, puOnDc0, puOnDc1, Var );
Alan Mishchenko committed
224
    puRes2 = Kit_TruthIsop_rec( pTemp0, pTemp1, Var, pcRes2, vStore );
Alan Mishchenko committed
225 226 227 228 229
    if ( pcRes2->nCubes == -1 )
    {
        pcRes->nCubes = -1;
        return NULL;
    }
Alan Mishchenko committed
230
    // create the resulting cover
231
    pcRes->nLits  = pcRes0->nLits  + pcRes1->nLits  + pcRes2->nLits + pcRes0->nCubes + pcRes1->nCubes;
Alan Mishchenko committed
232
    pcRes->nCubes = pcRes0->nCubes + pcRes1->nCubes + pcRes2->nCubes;
Alan Mishchenko committed
233 234 235 236 237 238
    pcRes->pCubes = Vec_IntFetch( vStore, pcRes->nCubes );
    if ( pcRes->pCubes == NULL )
    {
        pcRes->nCubes = -1;
        return NULL;
    }
Alan Mishchenko committed
239 240
    k = 0;
    for ( i = 0; i < pcRes0->nCubes; i++ )
Alan Mishchenko committed
241
        pcRes->pCubes[k++] = pcRes0->pCubes[i] | (1 << ((Var<<1)+0));
Alan Mishchenko committed
242
    for ( i = 0; i < pcRes1->nCubes; i++ )
Alan Mishchenko committed
243
        pcRes->pCubes[k++] = pcRes1->pCubes[i] | (1 << ((Var<<1)+1));
Alan Mishchenko committed
244
    for ( i = 0; i < pcRes2->nCubes; i++ )
Alan Mishchenko committed
245 246 247
        pcRes->pCubes[k++] = pcRes2->pCubes[i];
    assert( k == pcRes->nCubes );
    // create the resulting truth table
Alan Mishchenko committed
248 249
    Kit_TruthOr( pTemp0, puRes0, puRes2, Var );
    Kit_TruthOr( pTemp1, puRes1, puRes2, Var );
Alan Mishchenko committed
250 251 252 253 254 255
    // copy the table if needed
    nWords <<= 1;
    for ( i = 1; i < nWordsAll/nWords; i++ )
        for ( k = 0; k < nWords; k++ )
            pTemp[i*nWords + k] = pTemp[k];
    // verify in the end
Alan Mishchenko committed
256 257
//    assert( Kit_TruthIsImply( puOn, pTemp, nVars ) );
//    assert( Kit_TruthIsImply( pTemp, puOnDc, nVars ) );
Alan Mishchenko committed
258
    return pTemp;
Alan Mishchenko committed
259 260 261 262 263 264 265 266 267 268 269 270 271
}

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

  Synopsis    [Computes ISOP for 5 variables or less.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
272
unsigned Kit_TruthIsop5_rec( unsigned uOn, unsigned uOnDc, int nVars, Kit_Sop_t * pcRes, Vec_Int_t * vStore )
Alan Mishchenko committed
273 274
{
    unsigned uMasks[5] = { 0xAAAAAAAA, 0xCCCCCCCC, 0xF0F0F0F0, 0xFF00FF00, 0xFFFF0000 };
Alan Mishchenko committed
275 276
    Kit_Sop_t cRes0, cRes1, cRes2;
    Kit_Sop_t * pcRes0 = &cRes0, * pcRes1 = &cRes1, * pcRes2 = &cRes2;
Alan Mishchenko committed
277
    unsigned uOn0, uOn1, uOnDc0, uOnDc1, uRes0, uRes1, uRes2;
Alan Mishchenko committed
278 279
    int i, k, Var;
    assert( nVars <= 5 );
Alan Mishchenko committed
280 281
    assert( (uOn & ~uOnDc) == 0 );
    if ( uOn == 0 )
Alan Mishchenko committed
282
    {
283
        pcRes->nLits  = 0;
Alan Mishchenko committed
284 285 286 287 288 289
        pcRes->nCubes = 0;
        pcRes->pCubes = NULL;
        return 0;
    }
    if ( uOnDc == 0xFFFFFFFF )
    {
290
        pcRes->nLits  = 0;
Alan Mishchenko committed
291
        pcRes->nCubes = 1;
Alan Mishchenko committed
292 293 294 295 296 297
        pcRes->pCubes = Vec_IntFetch( vStore, 1 );
        if ( pcRes->pCubes == NULL )
        {
            pcRes->nCubes = -1;
            return 0;
        }
Alan Mishchenko committed
298 299 300
        pcRes->pCubes[0] = 0;
        return 0xFFFFFFFF;
    }
Alan Mishchenko committed
301
    assert( nVars > 0 );
Alan Mishchenko committed
302 303
    // find the topmost var
    for ( Var = nVars-1; Var >= 0; Var-- )
Alan Mishchenko committed
304 305
        if ( Kit_TruthVarInSupport( &uOn, 5, Var ) || 
             Kit_TruthVarInSupport( &uOnDc, 5, Var ) )
Alan Mishchenko committed
306 307 308
             break;
    assert( Var >= 0 );
    // cofactor
Alan Mishchenko committed
309
    uOn0   = uOn1   = uOn;
Alan Mishchenko committed
310
    uOnDc0 = uOnDc1 = uOnDc;
Alan Mishchenko committed
311 312 313 314
    Kit_TruthCofactor0( &uOn0, Var + 1, Var );
    Kit_TruthCofactor1( &uOn1, Var + 1, Var );
    Kit_TruthCofactor0( &uOnDc0, Var + 1, Var );
    Kit_TruthCofactor1( &uOnDc1, Var + 1, Var );
Alan Mishchenko committed
315
    // solve for cofactors
Alan Mishchenko committed
316
    uRes0 = Kit_TruthIsop5_rec( uOn0 & ~uOnDc1, uOnDc0, Var, pcRes0, vStore );
Alan Mishchenko committed
317 318 319 320 321
    if ( pcRes0->nCubes == -1 )
    {
        pcRes->nCubes = -1;
        return 0;
    }
Alan Mishchenko committed
322
    uRes1 = Kit_TruthIsop5_rec( uOn1 & ~uOnDc0, uOnDc1, Var, pcRes1, vStore );
Alan Mishchenko committed
323 324 325 326 327
    if ( pcRes1->nCubes == -1 )
    {
        pcRes->nCubes = -1;
        return 0;
    }
Alan Mishchenko committed
328
    uRes2 = Kit_TruthIsop5_rec( (uOn0 & ~uRes0) | (uOn1 & ~uRes1), uOnDc0 & uOnDc1, Var, pcRes2, vStore );
Alan Mishchenko committed
329 330 331 332 333
    if ( pcRes2->nCubes == -1 )
    {
        pcRes->nCubes = -1;
        return 0;
    }
Alan Mishchenko committed
334
    // create the resulting cover
335
    pcRes->nLits  = pcRes0->nLits  + pcRes1->nLits  + pcRes2->nLits + pcRes0->nCubes + pcRes1->nCubes;
Alan Mishchenko committed
336
    pcRes->nCubes = pcRes0->nCubes + pcRes1->nCubes + pcRes2->nCubes;
Alan Mishchenko committed
337 338 339 340 341 342
    pcRes->pCubes = Vec_IntFetch( vStore, pcRes->nCubes );
    if ( pcRes->pCubes == NULL )
    {
        pcRes->nCubes = -1;
        return 0;
    }
Alan Mishchenko committed
343 344
    k = 0;
    for ( i = 0; i < pcRes0->nCubes; i++ )
Alan Mishchenko committed
345
        pcRes->pCubes[k++] = pcRes0->pCubes[i] | (1 << ((Var<<1)+0));
Alan Mishchenko committed
346
    for ( i = 0; i < pcRes1->nCubes; i++ )
Alan Mishchenko committed
347
        pcRes->pCubes[k++] = pcRes1->pCubes[i] | (1 << ((Var<<1)+1));
Alan Mishchenko committed
348
    for ( i = 0; i < pcRes2->nCubes; i++ )
Alan Mishchenko committed
349 350
        pcRes->pCubes[k++] = pcRes2->pCubes[i];
    assert( k == pcRes->nCubes );
Alan Mishchenko committed
351 352 353 354 355
    // derive the final truth table
    uRes2 |= (uRes0 & ~uMasks[Var]) | (uRes1 & uMasks[Var]);
//    assert( (uOn & ~uRes2) == 0 );
//    assert( (uRes2 & ~uOnDc) == 0 );
    return uRes2;
Alan Mishchenko committed
356 357 358 359 360 361 362 363
}


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


364 365
ABC_NAMESPACE_IMPL_END