kitIsop.c 10.8 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 62
    assert( nVars >= 0 && nVars < 16 );
    // 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 ) );
Alan Mishchenko committed
90 91 92 93 94 95
            if ( pcRes->nCubes > pcRes2->nCubes )
            {
                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 105 106
}

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

Alan Mishchenko committed
107
  Synopsis    [Computes ISOP 6 variables or more.]
Alan Mishchenko committed
108 109 110 111 112 113 114 115

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
116
unsigned * Kit_TruthIsop_rec( unsigned * puOn, unsigned * puOnDc, int nVars, Kit_Sop_t * pcRes, Vec_Int_t * vStore )
Alan Mishchenko committed
117
{
Alan Mishchenko committed
118 119
    Kit_Sop_t cRes0, cRes1, cRes2;
    Kit_Sop_t * pcRes0 = &cRes0, * pcRes1 = &cRes1, * pcRes2 = &cRes2;
Alan Mishchenko committed
120
    unsigned * puRes0, * puRes1, * puRes2;
Alan Mishchenko committed
121 122
    unsigned * puOn0, * puOn1, * puOnDc0, * puOnDc1, * pTemp, * pTemp0, * pTemp1;
    int i, k, Var, nWords, nWordsAll;
Alan Mishchenko committed
123
//    assert( Kit_TruthIsImply( puOn, puOnDc, nVars ) );
Alan Mishchenko committed
124
    // allocate room for the resulting truth table
Alan Mishchenko committed
125
    nWordsAll = Kit_TruthWordNum( nVars );
Alan Mishchenko committed
126 127 128 129 130 131
    pTemp = Vec_IntFetch( vStore, nWordsAll );
    if ( pTemp == NULL )
    {
        pcRes->nCubes = -1;
        return NULL;
    }
Alan Mishchenko committed
132
    // check for constants
Alan Mishchenko committed
133
    if ( Kit_TruthIsConst0( puOn, nVars ) )
Alan Mishchenko committed
134 135 136
    {
        pcRes->nCubes = 0;
        pcRes->pCubes = NULL;
Alan Mishchenko committed
137
        Kit_TruthClear( pTemp, nVars );
Alan Mishchenko committed
138
        return pTemp;
Alan Mishchenko committed
139
    }
Alan Mishchenko committed
140
    if ( Kit_TruthIsConst1( puOnDc, nVars ) )
Alan Mishchenko committed
141 142
    {
        pcRes->nCubes = 1;
Alan Mishchenko committed
143 144 145 146 147 148
        pcRes->pCubes = Vec_IntFetch( vStore, 1 );
        if ( pcRes->pCubes == NULL )
        {
            pcRes->nCubes = -1;
            return NULL;
        }
Alan Mishchenko committed
149
        pcRes->pCubes[0] = 0;
Alan Mishchenko committed
150
        Kit_TruthFill( pTemp, nVars );
Alan Mishchenko committed
151
        return pTemp;
Alan Mishchenko committed
152
    }
Alan Mishchenko committed
153
    assert( nVars > 0 );
Alan Mishchenko committed
154 155
    // find the topmost var
    for ( Var = nVars-1; Var >= 0; Var-- )
Alan Mishchenko committed
156 157
        if ( Kit_TruthVarInSupport( puOn, nVars, Var ) || 
             Kit_TruthVarInSupport( puOnDc, nVars, Var ) )
Alan Mishchenko committed
158 159
             break;
    assert( Var >= 0 );
Alan Mishchenko committed
160
    // consider a simple case when one-word computation can be used
Alan Mishchenko committed
161 162
    if ( Var < 5 )
    {
Alan Mishchenko committed
163
        unsigned uRes = Kit_TruthIsop5_rec( puOn[0], puOnDc[0], Var+1, pcRes, vStore );
Alan Mishchenko committed
164 165 166
        for ( i = 0; i < nWordsAll; i++ )
            pTemp[i] = uRes;
        return pTemp;
Alan Mishchenko committed
167
    }
Alan Mishchenko committed
168
    assert( Var >= 5 );
Alan Mishchenko committed
169
    nWords = Kit_TruthWordNum( Var );
Alan Mishchenko committed
170
    // cofactor
Alan Mishchenko committed
171 172 173
    puOn0   = puOn;    puOn1   = puOn + nWords;
    puOnDc0 = puOnDc;  puOnDc1 = puOnDc + nWords;
    pTemp0  = pTemp;   pTemp1  = pTemp + nWords;
Alan Mishchenko committed
174
    // solve for cofactors
Alan Mishchenko committed
175
    Kit_TruthSharp( pTemp0, puOn0, puOnDc1, Var );
Alan Mishchenko committed
176
    puRes0 = Kit_TruthIsop_rec( pTemp0, puOnDc0, Var, pcRes0, vStore );
Alan Mishchenko committed
177 178 179 180 181
    if ( pcRes0->nCubes == -1 )
    {
        pcRes->nCubes = -1;
        return NULL;
    }
Alan Mishchenko committed
182
    Kit_TruthSharp( pTemp1, puOn1, puOnDc0, Var );
Alan Mishchenko committed
183
    puRes1 = Kit_TruthIsop_rec( pTemp1, puOnDc1, Var, pcRes1, vStore );
Alan Mishchenko committed
184 185 186 187 188
    if ( pcRes1->nCubes == -1 )
    {
        pcRes->nCubes = -1;
        return NULL;
    }
Alan Mishchenko committed
189 190 191 192
    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
193
    puRes2 = Kit_TruthIsop_rec( pTemp0, pTemp1, Var, pcRes2, vStore );
Alan Mishchenko committed
194 195 196 197 198
    if ( pcRes2->nCubes == -1 )
    {
        pcRes->nCubes = -1;
        return NULL;
    }
Alan Mishchenko committed
199 200
    // create the resulting cover
    pcRes->nCubes = pcRes0->nCubes + pcRes1->nCubes + pcRes2->nCubes;
Alan Mishchenko committed
201 202 203 204 205 206
    pcRes->pCubes = Vec_IntFetch( vStore, pcRes->nCubes );
    if ( pcRes->pCubes == NULL )
    {
        pcRes->nCubes = -1;
        return NULL;
    }
Alan Mishchenko committed
207 208
    k = 0;
    for ( i = 0; i < pcRes0->nCubes; i++ )
Alan Mishchenko committed
209
        pcRes->pCubes[k++] = pcRes0->pCubes[i] | (1 << ((Var<<1)+0));
Alan Mishchenko committed
210
    for ( i = 0; i < pcRes1->nCubes; i++ )
Alan Mishchenko committed
211
        pcRes->pCubes[k++] = pcRes1->pCubes[i] | (1 << ((Var<<1)+1));
Alan Mishchenko committed
212
    for ( i = 0; i < pcRes2->nCubes; i++ )
Alan Mishchenko committed
213 214 215
        pcRes->pCubes[k++] = pcRes2->pCubes[i];
    assert( k == pcRes->nCubes );
    // create the resulting truth table
Alan Mishchenko committed
216 217
    Kit_TruthOr( pTemp0, puRes0, puRes2, Var );
    Kit_TruthOr( pTemp1, puRes1, puRes2, Var );
Alan Mishchenko committed
218 219 220 221 222 223
    // 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
224 225
//    assert( Kit_TruthIsImply( puOn, pTemp, nVars ) );
//    assert( Kit_TruthIsImply( pTemp, puOnDc, nVars ) );
Alan Mishchenko committed
226
    return pTemp;
Alan Mishchenko committed
227 228 229 230 231 232 233 234 235 236 237 238 239
}

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

  Synopsis    [Computes ISOP for 5 variables or less.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
240
unsigned Kit_TruthIsop5_rec( unsigned uOn, unsigned uOnDc, int nVars, Kit_Sop_t * pcRes, Vec_Int_t * vStore )
Alan Mishchenko committed
241 242
{
    unsigned uMasks[5] = { 0xAAAAAAAA, 0xCCCCCCCC, 0xF0F0F0F0, 0xFF00FF00, 0xFFFF0000 };
Alan Mishchenko committed
243 244
    Kit_Sop_t cRes0, cRes1, cRes2;
    Kit_Sop_t * pcRes0 = &cRes0, * pcRes1 = &cRes1, * pcRes2 = &cRes2;
Alan Mishchenko committed
245
    unsigned uOn0, uOn1, uOnDc0, uOnDc1, uRes0, uRes1, uRes2;
Alan Mishchenko committed
246 247
    int i, k, Var;
    assert( nVars <= 5 );
Alan Mishchenko committed
248 249
    assert( (uOn & ~uOnDc) == 0 );
    if ( uOn == 0 )
Alan Mishchenko committed
250 251 252 253 254 255 256 257
    {
        pcRes->nCubes = 0;
        pcRes->pCubes = NULL;
        return 0;
    }
    if ( uOnDc == 0xFFFFFFFF )
    {
        pcRes->nCubes = 1;
Alan Mishchenko committed
258 259 260 261 262 263
        pcRes->pCubes = Vec_IntFetch( vStore, 1 );
        if ( pcRes->pCubes == NULL )
        {
            pcRes->nCubes = -1;
            return 0;
        }
Alan Mishchenko committed
264 265 266
        pcRes->pCubes[0] = 0;
        return 0xFFFFFFFF;
    }
Alan Mishchenko committed
267
    assert( nVars > 0 );
Alan Mishchenko committed
268 269
    // find the topmost var
    for ( Var = nVars-1; Var >= 0; Var-- )
Alan Mishchenko committed
270 271
        if ( Kit_TruthVarInSupport( &uOn, 5, Var ) || 
             Kit_TruthVarInSupport( &uOnDc, 5, Var ) )
Alan Mishchenko committed
272 273 274
             break;
    assert( Var >= 0 );
    // cofactor
Alan Mishchenko committed
275
    uOn0   = uOn1   = uOn;
Alan Mishchenko committed
276
    uOnDc0 = uOnDc1 = uOnDc;
Alan Mishchenko committed
277 278 279 280
    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
281
    // solve for cofactors
Alan Mishchenko committed
282
    uRes0 = Kit_TruthIsop5_rec( uOn0 & ~uOnDc1, uOnDc0, Var, pcRes0, vStore );
Alan Mishchenko committed
283 284 285 286 287
    if ( pcRes0->nCubes == -1 )
    {
        pcRes->nCubes = -1;
        return 0;
    }
Alan Mishchenko committed
288
    uRes1 = Kit_TruthIsop5_rec( uOn1 & ~uOnDc0, uOnDc1, Var, pcRes1, vStore );
Alan Mishchenko committed
289 290 291 292 293
    if ( pcRes1->nCubes == -1 )
    {
        pcRes->nCubes = -1;
        return 0;
    }
Alan Mishchenko committed
294
    uRes2 = Kit_TruthIsop5_rec( (uOn0 & ~uRes0) | (uOn1 & ~uRes1), uOnDc0 & uOnDc1, Var, pcRes2, vStore );
Alan Mishchenko committed
295 296 297 298 299
    if ( pcRes2->nCubes == -1 )
    {
        pcRes->nCubes = -1;
        return 0;
    }
Alan Mishchenko committed
300 301
    // create the resulting cover
    pcRes->nCubes = pcRes0->nCubes + pcRes1->nCubes + pcRes2->nCubes;
Alan Mishchenko committed
302 303 304 305 306 307
    pcRes->pCubes = Vec_IntFetch( vStore, pcRes->nCubes );
    if ( pcRes->pCubes == NULL )
    {
        pcRes->nCubes = -1;
        return 0;
    }
Alan Mishchenko committed
308 309
    k = 0;
    for ( i = 0; i < pcRes0->nCubes; i++ )
Alan Mishchenko committed
310
        pcRes->pCubes[k++] = pcRes0->pCubes[i] | (1 << ((Var<<1)+0));
Alan Mishchenko committed
311
    for ( i = 0; i < pcRes1->nCubes; i++ )
Alan Mishchenko committed
312
        pcRes->pCubes[k++] = pcRes1->pCubes[i] | (1 << ((Var<<1)+1));
Alan Mishchenko committed
313
    for ( i = 0; i < pcRes2->nCubes; i++ )
Alan Mishchenko committed
314 315
        pcRes->pCubes[k++] = pcRes2->pCubes[i];
    assert( k == pcRes->nCubes );
Alan Mishchenko committed
316 317 318 319 320
    // 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
321 322 323 324 325 326 327 328
}


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


329 330
ABC_NAMESPACE_IMPL_END