utilIsop.c 43.4 KB
Newer Older
Alan Mishchenko committed
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
/**CFile****************************************************************

  FileName    [utilIsop.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [ISOP computation.]

  Synopsis    [ISOP computation.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - October 4, 2014.]

  Revision    [$Id: utilIsop.c,v 1.00 2014/10/04 00:00:00 alanmi Exp $]

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

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

#include "misc/vec/vec.h"
#include "misc/util/utilTruth.h"

ABC_NAMESPACE_IMPL_START

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

Alan Mishchenko committed
35 36 37 38 39
#define ABC_ISOP_MAX_VAR  16
#define ABC_ISOP_MAX_WORD (ABC_ISOP_MAX_VAR > 6 ? (1 << (ABC_ISOP_MAX_VAR-6)) : 1)
#define ABC_ISOP_MAX_CUBE  0xFFFF

typedef word FUNC_ISOP( word *, word *, word *, word, int * );
Alan Mishchenko committed
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

static FUNC_ISOP Abc_Isop7Cover; 
static FUNC_ISOP Abc_Isop8Cover;
static FUNC_ISOP Abc_Isop9Cover; 
static FUNC_ISOP Abc_Isop10Cover;
static FUNC_ISOP Abc_Isop11Cover;
static FUNC_ISOP Abc_Isop12Cover;
static FUNC_ISOP Abc_Isop13Cover;
static FUNC_ISOP Abc_Isop14Cover;
static FUNC_ISOP Abc_Isop15Cover;
static FUNC_ISOP Abc_Isop16Cover;

static FUNC_ISOP * s_pFuncIsopCover[17] =
{
    NULL, // 0
    NULL, // 1
    NULL, // 2
    NULL, // 3
    NULL, // 4
    NULL, // 5
    NULL, // 6
    Abc_Isop7Cover,  // 7
    Abc_Isop8Cover,  // 8
    Abc_Isop9Cover,  // 9
    Abc_Isop10Cover, // 10
    Abc_Isop11Cover, // 11
    Abc_Isop12Cover, // 12
    Abc_Isop13Cover, // 13
    Abc_Isop14Cover, // 14
    Abc_Isop15Cover, // 15
    Abc_Isop16Cover  // 16
};

Alan Mishchenko committed
73 74 75 76 77 78
extern word Abc_IsopCheck( word * pOn, word * pOnDc, word * pRes, int nVars, word CostLim, int * pCover );
extern word Abc_EsopCheck( word * pOn,                            int nVars, word CostLim, int * pCover );

static inline word Abc_Cube2Cost( int nCubes )    { return (word)nCubes << 32;       }
static inline int  Abc_CostCubes( word Cost )     { return (int)(Cost >> 32);        }
static inline int  Abc_CostLits( word Cost )      { return (int)(Cost & 0xFFFFFFFF); }
Alan Mishchenko committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

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

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

  Synopsis    [These procedures assume that function has exact support.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
95
static inline void Abc_IsopAddLits( int * pCover, word Cost0, word Cost1, int Var )
Alan Mishchenko committed
96
{
Alan Mishchenko committed
97 98 99 100 101 102 103 104 105 106
    if ( pCover ) 
    {
        int c, nCubes0, nCubes1;
        nCubes0 = Abc_CostCubes( Cost0 );
        nCubes1 = Abc_CostCubes( Cost1 );
        for ( c = 0; c < nCubes0; c++ )
            pCover[c] |= (1 << Abc_Var2Lit(Var,0));
        for ( c = 0; c < nCubes1; c++ )
            pCover[nCubes0+c] |= (1 << Abc_Var2Lit(Var,1));
    }
Alan Mishchenko committed
107
}
Alan Mishchenko committed
108
word Abc_Isop6Cover( word uOn, word uOnDc, word * pRes, int nVars, word CostLim, int * pCover )
Alan Mishchenko committed
109 110
{
    word uOn0, uOn1, uOnDc0, uOnDc1, uRes0, uRes1, uRes2;
Alan Mishchenko committed
111
    word Cost0, Cost1, Cost2;  int Var; 
Alan Mishchenko committed
112 113 114 115 116 117 118 119 120 121 122
    assert( nVars <= 6 );
    assert( (uOn & ~uOnDc) == 0 );
    if ( uOn == 0 )
    {
        pRes[0] = 0;
        return 0;
    }
    if ( uOnDc == ~(word)0 )
    {
        pRes[0] = ~(word)0;
        if ( pCover ) pCover[0] = 0;
Alan Mishchenko committed
123
        return Abc_Cube2Cost(1);
Alan Mishchenko committed
124 125 126 127 128 129 130 131 132 133 134 135 136
    }
    assert( nVars > 0 );
    // find the topmost var
    for ( Var = nVars-1; Var >= 0; Var-- )
        if ( Abc_Tt6HasVar( uOn, Var ) || Abc_Tt6HasVar( uOnDc, Var ) )
             break;
    assert( Var >= 0 );
    // cofactor
    uOn0   = Abc_Tt6Cofactor0( uOn,   Var );
    uOn1   = Abc_Tt6Cofactor1( uOn  , Var );
    uOnDc0 = Abc_Tt6Cofactor0( uOnDc, Var );
    uOnDc1 = Abc_Tt6Cofactor1( uOnDc, Var );
    // solve for cofactors
Alan Mishchenko committed
137 138 139 140 141 142
    Cost0 = Abc_Isop6Cover( uOn0 & ~uOnDc1, uOnDc0, &uRes0, Var, CostLim, pCover );
    if ( Cost0 >= CostLim ) return CostLim;
    Cost1 = Abc_Isop6Cover( uOn1 & ~uOnDc0, uOnDc1, &uRes1, Var, CostLim, pCover ? pCover + Abc_CostCubes(Cost0) : NULL );
    if ( Cost0 + Cost1 >= CostLim ) return CostLim;
    Cost2 = Abc_Isop6Cover( (uOn0 & ~uRes0) | (uOn1 & ~uRes1), uOnDc0 & uOnDc1, &uRes2, Var, CostLim, pCover ? pCover + Abc_CostCubes(Cost0) + Abc_CostCubes(Cost1) : NULL );
    if ( Cost0 + Cost1 + Cost2 >= CostLim ) return CostLim;
Alan Mishchenko committed
143 144 145
    // derive the final truth table
    *pRes = uRes2 | (uRes0 & s_Truths6Neg[Var]) | (uRes1 & s_Truths6[Var]);
    assert( (uOn & ~*pRes) == 0 && (*pRes & ~uOnDc) == 0 );
Alan Mishchenko committed
146 147
    Abc_IsopAddLits( pCover, Cost0, Cost1, Var );
    return Cost0 + Cost1 + Cost2 + Abc_CostCubes(Cost0) + Abc_CostCubes(Cost1);
Alan Mishchenko committed
148
}
Alan Mishchenko committed
149
word Abc_Isop7Cover( word * pOn, word * pOnDc, word * pRes, word CostLim, int * pCover )
Alan Mishchenko committed
150 151
{
    word uOn0, uOn1, uOn2, uOnDc2, uRes0, uRes1, uRes2;
Alan Mishchenko committed
152
    word Cost0, Cost1, Cost2;  int nVars = 6; 
Alan Mishchenko committed
153 154
    assert( (pOn[0] & ~pOnDc[0]) == 0 );
    assert( (pOn[1] & ~pOnDc[1]) == 0 );
Alan Mishchenko committed
155 156 157 158
    // cofactor
    uOn0 = pOn[0] & ~pOnDc[1];
    uOn1 = pOn[1] & ~pOnDc[0];
    // solve for cofactors
Alan Mishchenko committed
159 160 161 162
    Cost0 = Abc_IsopCheck( &uOn0, pOnDc,   &uRes0, nVars, CostLim, pCover );
    if ( Cost0 >= CostLim ) return CostLim;
    Cost1 = Abc_IsopCheck( &uOn1, pOnDc+1, &uRes1, nVars, CostLim, pCover ? pCover + Abc_CostCubes(Cost0) : NULL );
    if ( Cost0 + Cost1 >= CostLim ) return CostLim;
Alan Mishchenko committed
163 164
    uOn2 = (pOn[0] & ~uRes0) | (pOn[1] & ~uRes1);
    uOnDc2 = pOnDc[0] & pOnDc[1];
Alan Mishchenko committed
165 166
    Cost2 = Abc_IsopCheck( &uOn2, &uOnDc2,  &uRes2, nVars, CostLim, pCover ? pCover + Abc_CostCubes(Cost0) + Abc_CostCubes(Cost1) : NULL );
    if ( Cost0 + Cost1 + Cost2 >= CostLim ) return CostLim;
Alan Mishchenko committed
167 168 169 170 171
    // derive the final truth table
    pRes[0] = uRes2 | uRes0;
    pRes[1] = uRes2 | uRes1;
    assert( (pOn[0] & ~pRes[0]) == 0 && (pRes[0] & ~pOnDc[0]) == 0 );
    assert( (pOn[1] & ~pRes[1]) == 0 && (pRes[1] & ~pOnDc[1]) == 0 );
Alan Mishchenko committed
172 173
    Abc_IsopAddLits( pCover, Cost0, Cost1, nVars );
    return Cost0 + Cost1 + Cost2 + Abc_CostCubes(Cost0) + Abc_CostCubes(Cost1);
Alan Mishchenko committed
174
}
Alan Mishchenko committed
175
word Abc_Isop8Cover( word * pOn, word * pOnDc, word * pRes, word CostLim, int * pCover )
Alan Mishchenko committed
176
{
Alan Mishchenko committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
    word uOn2[2], uOnDc2[2], uRes0[2], uRes1[2], uRes2[2];
    word Cost0, Cost1, Cost2;  int nVars = 7;
    // negative cofactor
    uOn2[0] = pOn[0] & ~pOnDc[2];
    uOn2[1] = pOn[1] & ~pOnDc[3];
    Cost0 = Abc_IsopCheck( uOn2, pOnDc,   uRes0, nVars, CostLim, pCover );
    if ( Cost0 >= CostLim ) return CostLim;
    // positive cofactor
    uOn2[0] = pOn[2] & ~pOnDc[0];
    uOn2[1] = pOn[3] & ~pOnDc[1];
    Cost1 = Abc_IsopCheck( uOn2, pOnDc+2, uRes1, nVars, CostLim, pCover ? pCover + Abc_CostCubes(Cost0) : NULL );
    if ( Cost0 + Cost1 >= CostLim ) return CostLim;
    // middle cofactor
    uOn2[0] = (pOn[0] & ~uRes0[0]) | (pOn[2] & ~uRes1[0]); uOnDc2[0] = pOnDc[0] & pOnDc[2];
    uOn2[1] = (pOn[1] & ~uRes0[1]) | (pOn[3] & ~uRes1[1]); uOnDc2[1] = pOnDc[1] & pOnDc[3];
    Cost2 = Abc_IsopCheck( uOn2, uOnDc2,  uRes2, nVars, CostLim, pCover ? pCover + Abc_CostCubes(Cost0) + Abc_CostCubes(Cost1) : NULL );
    if ( Cost0 + Cost1 + Cost2 >= CostLim ) return CostLim;
Alan Mishchenko committed
194 195 196 197 198 199 200
    // derive the final truth table
    pRes[0] = uRes2[0] | uRes0[0];
    pRes[1] = uRes2[1] | uRes0[1];
    pRes[2] = uRes2[0] | uRes1[0];
    pRes[3] = uRes2[1] | uRes1[1];
    assert( (pOn[0] & ~pRes[0]) == 0 && (pOn[1] & ~pRes[1]) == 0 && (pOn[2] & ~pRes[2]) == 0 && (pOn[3] & ~pRes[3]) == 0 );
    assert( (pRes[0] & ~pOnDc[0])==0 && (pRes[1] & ~pOnDc[1])==0 && (pRes[2] & ~pOnDc[2])==0 && (pRes[3] & ~pOnDc[3])==0 );
Alan Mishchenko committed
201 202
    Abc_IsopAddLits( pCover, Cost0, Cost1, nVars );
    return Cost0 + Cost1 + Cost2 + Abc_CostCubes(Cost0) + Abc_CostCubes(Cost1);
Alan Mishchenko committed
203
}
Alan Mishchenko committed
204
word Abc_Isop9Cover( word * pOn, word * pOnDc, word * pRes, word CostLim, int * pCover )
Alan Mishchenko committed
205
{
Alan Mishchenko committed
206 207 208
    word uOn2[4], uOnDc2[4], uRes0[4], uRes1[4], uRes2[4];
    word Cost0, Cost1, Cost2; int c, nVars = 8, nWords = 4;
    // negative cofactor
Alan Mishchenko committed
209
    for ( c = 0; c < nWords; c++ )
Alan Mishchenko committed
210 211 212 213 214 215 216 217 218
        uOn2[c] = pOn[c] & ~pOnDc[c+nWords];
    Cost0 = Abc_IsopCheck( uOn2, pOnDc,        uRes0, nVars, CostLim, pCover );
    if ( Cost0 >= CostLim ) return CostLim;
    // positive cofactor
    for ( c = 0; c < nWords; c++ )
        uOn2[c] = pOn[c+nWords] & ~pOnDc[c];
    Cost1 = Abc_IsopCheck( uOn2, pOnDc+nWords, uRes1, nVars, CostLim, pCover ? pCover + Abc_CostCubes(Cost0) : NULL );
    if ( Cost0 + Cost1 >= CostLim ) return CostLim;
    // middle cofactor
Alan Mishchenko committed
219 220
    for ( c = 0; c < nWords; c++ )
        uOn2[c] = (pOn[c] & ~uRes0[c]) | (pOn[c+nWords] & ~uRes1[c]), uOnDc2[c] = pOnDc[c] & pOnDc[c+nWords];
Alan Mishchenko committed
221 222
    Cost2 = Abc_IsopCheck( uOn2, uOnDc2,       uRes2, nVars, CostLim, pCover ? pCover + Abc_CostCubes(Cost0) + Abc_CostCubes(Cost1) : NULL );
    if ( Cost0 + Cost1 + Cost2 >= CostLim ) return CostLim;
Alan Mishchenko committed
223 224 225 226 227 228
    // derive the final truth table
    for ( c = 0; c < nWords; c++ )
        pRes[c] = uRes2[c] | uRes0[c], pRes[c+nWords] = uRes2[c] | uRes1[c];
    // verify
    for ( c = 0; c < (nWords<<1); c++ )
        assert( (pOn[c]  & ~pRes[c] ) == 0 && (pRes[c] & ~pOnDc[c]) == 0 );
Alan Mishchenko committed
229 230
    Abc_IsopAddLits( pCover, Cost0, Cost1, nVars );
    return Cost0 + Cost1 + Cost2 + Abc_CostCubes(Cost0) + Abc_CostCubes(Cost1);
Alan Mishchenko committed
231
}
Alan Mishchenko committed
232
word Abc_Isop10Cover( word * pOn, word * pOnDc, word * pRes, word CostLim, int * pCover )
Alan Mishchenko committed
233
{
Alan Mishchenko committed
234 235 236
    word uOn2[8], uOnDc2[8], uRes0[8], uRes1[8], uRes2[8];
    word Cost0, Cost1, Cost2; int c, nVars = 9, nWords = 8;
    // negative cofactor
Alan Mishchenko committed
237
    for ( c = 0; c < nWords; c++ )
Alan Mishchenko committed
238 239 240 241 242 243 244 245 246
        uOn2[c] = pOn[c] & ~pOnDc[c+nWords];
    Cost0 = Abc_IsopCheck( uOn2, pOnDc,        uRes0, nVars, CostLim, pCover );
    if ( Cost0 >= CostLim ) return CostLim;
    // positive cofactor
    for ( c = 0; c < nWords; c++ )
        uOn2[c] = pOn[c+nWords] & ~pOnDc[c];
    Cost1 = Abc_IsopCheck( uOn2, pOnDc+nWords, uRes1, nVars, CostLim, pCover ? pCover + Abc_CostCubes(Cost0) : NULL );
    if ( Cost0 + Cost1 >= CostLim ) return CostLim;
    // middle cofactor
Alan Mishchenko committed
247 248
    for ( c = 0; c < nWords; c++ )
        uOn2[c] = (pOn[c] & ~uRes0[c]) | (pOn[c+nWords] & ~uRes1[c]), uOnDc2[c] = pOnDc[c] & pOnDc[c+nWords];
Alan Mishchenko committed
249 250
    Cost2 = Abc_IsopCheck( uOn2, uOnDc2,       uRes2, nVars, CostLim, pCover ? pCover + Abc_CostCubes(Cost0) + Abc_CostCubes(Cost1) : NULL );
    if ( Cost0 + Cost1 + Cost2 >= CostLim ) return CostLim;
Alan Mishchenko committed
251 252 253 254 255 256
    // derive the final truth table
    for ( c = 0; c < nWords; c++ )
        pRes[c] = uRes2[c] | uRes0[c], pRes[c+nWords] = uRes2[c] | uRes1[c];
    // verify
    for ( c = 0; c < (nWords<<1); c++ )
        assert( (pOn[c]  & ~pRes[c] ) == 0 && (pRes[c] & ~pOnDc[c]) == 0 );
Alan Mishchenko committed
257 258
    Abc_IsopAddLits( pCover, Cost0, Cost1, nVars );
    return Cost0 + Cost1 + Cost2 + Abc_CostCubes(Cost0) + Abc_CostCubes(Cost1);
Alan Mishchenko committed
259
}
Alan Mishchenko committed
260
word Abc_Isop11Cover( word * pOn, word * pOnDc, word * pRes, word CostLim, int * pCover )
Alan Mishchenko committed
261
{
Alan Mishchenko committed
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
    word uOn2[16], uOnDc2[16], uRes0[16], uRes1[16], uRes2[16];
    word Cost0, Cost1, Cost2; int c, nVars = 10, nWords = 16;
    // negative cofactor
    for ( c = 0; c < nWords; c++ )
        uOn2[c] = pOn[c] & ~pOnDc[c+nWords];
    Cost0 = Abc_IsopCheck( uOn2, pOnDc,        uRes0, nVars, CostLim, pCover );
    if ( Cost0 >= CostLim ) return CostLim;
    // positive cofactor
    for ( c = 0; c < nWords; c++ )
        uOn2[c] = pOn[c+nWords] & ~pOnDc[c];
    Cost1 = Abc_IsopCheck( uOn2, pOnDc+nWords, uRes1, nVars, CostLim, pCover ? pCover + Abc_CostCubes(Cost0) : NULL );
    if ( Cost0 + Cost1 >= CostLim ) return CostLim;
    // middle cofactor
    for ( c = 0; c < nWords; c++ )
        uOn2[c] = (pOn[c] & ~uRes0[c]) | (pOn[c+nWords] & ~uRes1[c]), uOnDc2[c] = pOnDc[c] & pOnDc[c+nWords];
    Cost2 = Abc_IsopCheck( uOn2, uOnDc2,       uRes2, nVars, CostLim, pCover ? pCover + Abc_CostCubes(Cost0) + Abc_CostCubes(Cost1) : NULL );
    if ( Cost0 + Cost1 + Cost2 >= CostLim ) return CostLim;
    // derive the final truth table
    for ( c = 0; c < nWords; c++ )
        pRes[c] = uRes2[c] | uRes0[c], pRes[c+nWords] = uRes2[c] | uRes1[c];
    // verify
    for ( c = 0; c < (nWords<<1); c++ )
        assert( (pOn[c]  & ~pRes[c] ) == 0 && (pRes[c] & ~pOnDc[c]) == 0 );
    Abc_IsopAddLits( pCover, Cost0, Cost1, nVars );
    return Cost0 + Cost1 + Cost2 + Abc_CostCubes(Cost0) + Abc_CostCubes(Cost1);
Alan Mishchenko committed
287
}
Alan Mishchenko committed
288
word Abc_Isop12Cover( word * pOn, word * pOnDc, word * pRes, word CostLim, int * pCover )
Alan Mishchenko committed
289
{
Alan Mishchenko committed
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
    word uOn2[32], uOnDc2[32], uRes0[32], uRes1[32], uRes2[32];
    word Cost0, Cost1, Cost2; int c, nVars = 11, nWords = 32;
    // negative cofactor
    for ( c = 0; c < nWords; c++ )
        uOn2[c] = pOn[c] & ~pOnDc[c+nWords];
    Cost0 = Abc_IsopCheck( uOn2, pOnDc,        uRes0, nVars, CostLim, pCover );
    if ( Cost0 >= CostLim ) return CostLim;
    // positive cofactor
    for ( c = 0; c < nWords; c++ )
        uOn2[c] = pOn[c+nWords] & ~pOnDc[c];
    Cost1 = Abc_IsopCheck( uOn2, pOnDc+nWords, uRes1, nVars, CostLim, pCover ? pCover + Abc_CostCubes(Cost0) : NULL );
    if ( Cost0 + Cost1 >= CostLim ) return CostLim;
    // middle cofactor
    for ( c = 0; c < nWords; c++ )
        uOn2[c] = (pOn[c] & ~uRes0[c]) | (pOn[c+nWords] & ~uRes1[c]), uOnDc2[c] = pOnDc[c] & pOnDc[c+nWords];
    Cost2 = Abc_IsopCheck( uOn2, uOnDc2,       uRes2, nVars, CostLim, pCover ? pCover + Abc_CostCubes(Cost0) + Abc_CostCubes(Cost1) : NULL );
    if ( Cost0 + Cost1 + Cost2 >= CostLim ) return CostLim;
    // derive the final truth table
    for ( c = 0; c < nWords; c++ )
        pRes[c] = uRes2[c] | uRes0[c], pRes[c+nWords] = uRes2[c] | uRes1[c];
    // verify
    for ( c = 0; c < (nWords<<1); c++ )
        assert( (pOn[c]  & ~pRes[c] ) == 0 && (pRes[c] & ~pOnDc[c]) == 0 );
    Abc_IsopAddLits( pCover, Cost0, Cost1, nVars );
    return Cost0 + Cost1 + Cost2 + Abc_CostCubes(Cost0) + Abc_CostCubes(Cost1);
Alan Mishchenko committed
315
}
Alan Mishchenko committed
316
word Abc_Isop13Cover( word * pOn, word * pOnDc, word * pRes, word CostLim, int * pCover )
Alan Mishchenko committed
317
{
Alan Mishchenko committed
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
    word uOn2[64], uOnDc2[64], uRes0[64], uRes1[64], uRes2[64];
    word Cost0, Cost1, Cost2; int c, nVars = 12, nWords = 64;
    // negative cofactor
    for ( c = 0; c < nWords; c++ )
        uOn2[c] = pOn[c] & ~pOnDc[c+nWords];
    Cost0 = Abc_IsopCheck( uOn2, pOnDc,        uRes0, nVars, CostLim, pCover );
    if ( Cost0 >= CostLim ) return CostLim;
    // positive cofactor
    for ( c = 0; c < nWords; c++ )
        uOn2[c] = pOn[c+nWords] & ~pOnDc[c];
    Cost1 = Abc_IsopCheck( uOn2, pOnDc+nWords, uRes1, nVars, CostLim, pCover ? pCover + Abc_CostCubes(Cost0) : NULL );
    if ( Cost0 + Cost1 >= CostLim ) return CostLim;
    // middle cofactor
    for ( c = 0; c < nWords; c++ )
        uOn2[c] = (pOn[c] & ~uRes0[c]) | (pOn[c+nWords] & ~uRes1[c]), uOnDc2[c] = pOnDc[c] & pOnDc[c+nWords];
    Cost2 = Abc_IsopCheck( uOn2, uOnDc2,       uRes2, nVars, CostLim, pCover ? pCover + Abc_CostCubes(Cost0) + Abc_CostCubes(Cost1) : NULL );
    if ( Cost0 + Cost1 + Cost2 >= CostLim ) return CostLim;
    // derive the final truth table
    for ( c = 0; c < nWords; c++ )
        pRes[c] = uRes2[c] | uRes0[c], pRes[c+nWords] = uRes2[c] | uRes1[c];
    // verify
    for ( c = 0; c < (nWords<<1); c++ )
        assert( (pOn[c]  & ~pRes[c] ) == 0 && (pRes[c] & ~pOnDc[c]) == 0 );
    Abc_IsopAddLits( pCover, Cost0, Cost1, nVars );
    return Cost0 + Cost1 + Cost2 + Abc_CostCubes(Cost0) + Abc_CostCubes(Cost1);
Alan Mishchenko committed
343
}
Alan Mishchenko committed
344
word Abc_Isop14Cover( word * pOn, word * pOnDc, word * pRes, word CostLim, int * pCover )
Alan Mishchenko committed
345
{
Alan Mishchenko committed
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
    word uOn2[128], uOnDc2[128], uRes0[128], uRes1[128], uRes2[128];
    word Cost0, Cost1, Cost2; int c, nVars = 13, nWords = 128;
    // negative cofactor
    for ( c = 0; c < nWords; c++ )
        uOn2[c] = pOn[c] & ~pOnDc[c+nWords];
    Cost0 = Abc_IsopCheck( uOn2, pOnDc,        uRes0, nVars, CostLim, pCover );
    if ( Cost0 >= CostLim ) return CostLim;
    // positive cofactor
    for ( c = 0; c < nWords; c++ )
        uOn2[c] = pOn[c+nWords] & ~pOnDc[c];
    Cost1 = Abc_IsopCheck( uOn2, pOnDc+nWords, uRes1, nVars, CostLim, pCover ? pCover + Abc_CostCubes(Cost0) : NULL );
    if ( Cost0 + Cost1 >= CostLim ) return CostLim;
    // middle cofactor
    for ( c = 0; c < nWords; c++ )
        uOn2[c] = (pOn[c] & ~uRes0[c]) | (pOn[c+nWords] & ~uRes1[c]), uOnDc2[c] = pOnDc[c] & pOnDc[c+nWords];
    Cost2 = Abc_IsopCheck( uOn2, uOnDc2,       uRes2, nVars, CostLim, pCover ? pCover + Abc_CostCubes(Cost0) + Abc_CostCubes(Cost1) : NULL );
    if ( Cost0 + Cost1 + Cost2 >= CostLim ) return CostLim;
    // derive the final truth table
    for ( c = 0; c < nWords; c++ )
        pRes[c] = uRes2[c] | uRes0[c], pRes[c+nWords] = uRes2[c] | uRes1[c];
    // verify
    for ( c = 0; c < (nWords<<1); c++ )
        assert( (pOn[c]  & ~pRes[c] ) == 0 && (pRes[c] & ~pOnDc[c]) == 0 );
    Abc_IsopAddLits( pCover, Cost0, Cost1, nVars );
    return Cost0 + Cost1 + Cost2 + Abc_CostCubes(Cost0) + Abc_CostCubes(Cost1);
Alan Mishchenko committed
371
}
Alan Mishchenko committed
372
word Abc_Isop15Cover( word * pOn, word * pOnDc, word * pRes, word CostLim, int * pCover )
Alan Mishchenko committed
373
{
Alan Mishchenko committed
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
    word uOn2[256], uOnDc2[256], uRes0[256], uRes1[256], uRes2[256];
    word Cost0, Cost1, Cost2; int c, nVars = 14, nWords = 256;
    // negative cofactor
    for ( c = 0; c < nWords; c++ )
        uOn2[c] = pOn[c] & ~pOnDc[c+nWords];
    Cost0 = Abc_IsopCheck( uOn2, pOnDc,        uRes0, nVars, CostLim, pCover );
    if ( Cost0 >= CostLim ) return CostLim;
    // positive cofactor
    for ( c = 0; c < nWords; c++ )
        uOn2[c] = pOn[c+nWords] & ~pOnDc[c];
    Cost1 = Abc_IsopCheck( uOn2, pOnDc+nWords, uRes1, nVars, CostLim, pCover ? pCover + Abc_CostCubes(Cost0) : NULL );
    if ( Cost0 + Cost1 >= CostLim ) return CostLim;
    // middle cofactor
    for ( c = 0; c < nWords; c++ )
        uOn2[c] = (pOn[c] & ~uRes0[c]) | (pOn[c+nWords] & ~uRes1[c]), uOnDc2[c] = pOnDc[c] & pOnDc[c+nWords];
    Cost2 = Abc_IsopCheck( uOn2, uOnDc2,       uRes2, nVars, CostLim, pCover ? pCover + Abc_CostCubes(Cost0) + Abc_CostCubes(Cost1) : NULL );
    if ( Cost0 + Cost1 + Cost2 >= CostLim ) return CostLim;
    // derive the final truth table
    for ( c = 0; c < nWords; c++ )
        pRes[c] = uRes2[c] | uRes0[c], pRes[c+nWords] = uRes2[c] | uRes1[c];
    // verify
    for ( c = 0; c < (nWords<<1); c++ )
        assert( (pOn[c]  & ~pRes[c] ) == 0 && (pRes[c] & ~pOnDc[c]) == 0 );
    Abc_IsopAddLits( pCover, Cost0, Cost1, nVars );
    return Cost0 + Cost1 + Cost2 + Abc_CostCubes(Cost0) + Abc_CostCubes(Cost1);
Alan Mishchenko committed
399
}
Alan Mishchenko committed
400
word Abc_Isop16Cover( word * pOn, word * pOnDc, word * pRes, word CostLim, int * pCover )
Alan Mishchenko committed
401
{
Alan Mishchenko committed
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
    word uOn2[512], uOnDc2[512], uRes0[512], uRes1[512], uRes2[512];
    word Cost0, Cost1, Cost2; int c, nVars = 15, nWords = 512;
    // negative cofactor
    for ( c = 0; c < nWords; c++ )
        uOn2[c] = pOn[c] & ~pOnDc[c+nWords];
    Cost0 = Abc_IsopCheck( uOn2, pOnDc,        uRes0, nVars, CostLim, pCover );
    if ( Cost0 >= CostLim ) return CostLim;
    // positive cofactor
    for ( c = 0; c < nWords; c++ )
        uOn2[c] = pOn[c+nWords] & ~pOnDc[c];
    Cost1 = Abc_IsopCheck( uOn2, pOnDc+nWords, uRes1, nVars, CostLim, pCover ? pCover + Abc_CostCubes(Cost0) : NULL );
    if ( Cost0 + Cost1 >= CostLim ) return CostLim;
    // middle cofactor
    for ( c = 0; c < nWords; c++ )
        uOn2[c] = (pOn[c] & ~uRes0[c]) | (pOn[c+nWords] & ~uRes1[c]), uOnDc2[c] = pOnDc[c] & pOnDc[c+nWords];
    Cost2 = Abc_IsopCheck( uOn2, uOnDc2,       uRes2, nVars, CostLim, pCover ? pCover + Abc_CostCubes(Cost0) + Abc_CostCubes(Cost1) : NULL );
    if ( Cost0 + Cost1 + Cost2 >= CostLim ) return CostLim;
    // derive the final truth table
    for ( c = 0; c < nWords; c++ )
        pRes[c] = uRes2[c] | uRes0[c], pRes[c+nWords] = uRes2[c] | uRes1[c];
    // verify
    for ( c = 0; c < (nWords<<1); c++ )
        assert( (pOn[c]  & ~pRes[c] ) == 0 && (pRes[c] & ~pOnDc[c]) == 0 );
    Abc_IsopAddLits( pCover, Cost0, Cost1, nVars );
    return Cost0 + Cost1 + Cost2 + Abc_CostCubes(Cost0) + Abc_CostCubes(Cost1);
Alan Mishchenko committed
427
}
Alan Mishchenko committed
428
word Abc_IsopCheck( word * pOn, word * pOnDc, word * pRes, int nVars, word CostLim, int * pCover )
Alan Mishchenko committed
429
{
Alan Mishchenko committed
430
    int nVarsNew; word Cost;
Alan Mishchenko committed
431
    if ( nVars <= 6 )
Alan Mishchenko committed
432
        return Abc_Isop6Cover( *pOn, *pOnDc, pRes, nVars, CostLim, pCover );
Alan Mishchenko committed
433 434 435 436
    for ( nVarsNew = nVars; nVarsNew > 6; nVarsNew-- )
        if ( Abc_TtHasVar( pOn, nVars, nVarsNew-1 ) || Abc_TtHasVar( pOnDc, nVars, nVarsNew-1 ) )
            break;
    if ( nVarsNew == 6 )
Alan Mishchenko committed
437
        Cost = Abc_Isop6Cover( *pOn, *pOnDc, pRes, nVarsNew, CostLim, pCover );
Alan Mishchenko committed
438
    else
Alan Mishchenko committed
439
        Cost = s_pFuncIsopCover[nVarsNew]( pOn, pOnDc, pRes, CostLim, pCover );
Alan Mishchenko committed
440 441
    Abc_TtStretch6( pRes, nVarsNew, nVars );
    return Cost;
Alan Mishchenko committed
442 443 444 445
}

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

Alan Mishchenko committed
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
  Synopsis    [Create truth table for the given cover.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline word ** Abc_IsopTtElems()
{
    static word TtElems[ABC_ISOP_MAX_VAR+1][ABC_ISOP_MAX_WORD], * pTtElems[ABC_ISOP_MAX_VAR+1] = {NULL};
    if ( pTtElems[0] == NULL )
    {
        int v;
        for ( v = 0; v <= ABC_ISOP_MAX_VAR; v++ )
            pTtElems[v] = TtElems[v];
        Abc_TtElemInit( pTtElems, ABC_ISOP_MAX_VAR );
    }
    return pTtElems;
}
void Abc_IsopBuildTruth( Vec_Int_t * vCover, int nVars, word * pRes, int fXor, int fCompl )
{
    word ** pTtElems = Abc_IsopTtElems();
    word pCube[ABC_ISOP_MAX_WORD];
    int nWords = Abc_TtWordNum( nVars );
    int c, v, Cube;
    assert( nVars <= ABC_ISOP_MAX_VAR );
    Abc_TtClear( pRes, nWords );
    Vec_IntForEachEntry( vCover, Cube, c )
    {
        Abc_TtFill( pCube, nWords );
        for ( v = 0; v < nVars; v++ )
            if ( ((Cube >> (v << 1)) & 3) == 1 )
                Abc_TtSharp( pCube, pCube, pTtElems[v], nWords );
            else if ( ((Cube >> (v << 1)) & 3) == 2 )
                Abc_TtAnd( pCube, pCube, pTtElems[v], nWords, 0 );
        if ( fXor )
            Abc_TtXor( pRes, pRes, pCube, nWords, 0 );
        else
            Abc_TtOr( pRes, pRes, pCube, nWords );
    }
    if ( fCompl )
        Abc_TtNot( pRes, nWords );
}
static inline void Abc_IsopVerify( word * pFunc, int nVars, word * pRes, Vec_Int_t * vCover, int fXor, int fCompl )
{
    Abc_IsopBuildTruth( vCover, nVars, pRes, fXor, fCompl );
    if ( !Abc_TtEqual( pFunc, pRes, Abc_TtWordNum(nVars) ) )
        printf( "Verification failed.\n" );
//    else
//        printf( "Verification succeeded.\n" );
}

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

  Synopsis    [This procedure assumes that function has exact support.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_Isop( word * pFunc, int nVars, int nCubeLim, Vec_Int_t * vCover, int fTryBoth )
{
    word pRes[ABC_ISOP_MAX_WORD];
    word Cost0, Cost1, Cost, CostInit = Abc_Cube2Cost(nCubeLim);
    assert( nVars <= ABC_ISOP_MAX_VAR );
    Vec_IntGrow( vCover, 1 << (nVars-1) );
    if ( fTryBoth )
    {
        Cost0 = Abc_IsopCheck( pFunc, pFunc, pRes, nVars, CostInit, NULL );
        Abc_TtNot( pFunc, Abc_TtWordNum(nVars) );
        Cost1 = Abc_IsopCheck( pFunc, pFunc, pRes, nVars, Cost0, NULL );
        Cost = Abc_MinWord( Cost0, Cost1 );
        if ( Cost == CostInit )
        {
            Abc_TtNot( pFunc, Abc_TtWordNum(nVars) );
            return -1;
        }
        if ( Cost == Cost0 )
        {
            Abc_TtNot( pFunc, Abc_TtWordNum(nVars) );
            Abc_IsopCheck( pFunc, pFunc, pRes, nVars, CostInit, Vec_IntArray(vCover) );
        }
        else // if ( Cost == Cost1 )
        {
            Abc_IsopCheck( pFunc, pFunc, pRes, nVars, CostInit, Vec_IntArray(vCover) );
            Abc_TtNot( pFunc, Abc_TtWordNum(nVars) );
        }
    }
    else
    {
        Cost = Cost0 = Abc_IsopCheck( pFunc, pFunc, pRes, nVars, CostInit, Vec_IntArray(vCover) );
        if ( Cost == CostInit )
            return -1;
    }
    vCover->nSize = Abc_CostCubes(Cost);
    assert( vCover->nSize <= vCover->nCap );
//    Abc_IsopVerify( pFunc, nVars, pRes, vCover, 0, Cost != Cost0 );
    return Cost != Cost0;
}

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

Alan Mishchenko committed
553 554 555 556 557 558 559 560 561 562 563
  Synopsis    [Compute CNF assuming it does not exceed the limit.]

  Description [Please note that pCover should have at least 32 extra entries!]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_IsopCnf( word * pFunc, int nVars, int nCubeLim, int * pCover )
{
Alan Mishchenko committed
564 565 566 567
    word pRes[ABC_ISOP_MAX_WORD];
    word Cost0, Cost1, CostInit = Abc_Cube2Cost(nCubeLim);
    int c, nCubes0, nCubes1;
    assert( nVars <= ABC_ISOP_MAX_VAR );
Alan Mishchenko committed
568 569
    assert( Abc_TtHasVar( pFunc, nVars, nVars - 1 ) );
    if ( nVars > 6 )
Alan Mishchenko committed
570
        Cost0 = s_pFuncIsopCover[nVars]( pFunc, pFunc, pRes, CostInit, pCover );
Alan Mishchenko committed
571
    else
Alan Mishchenko committed
572 573 574
        Cost0 = Abc_Isop6Cover( *pFunc, *pFunc, pRes, nVars, CostInit, pCover );
    if ( Cost0 >= CostInit )
        return CostInit;
Alan Mishchenko committed
575 576
    Abc_TtNot( pFunc, Abc_TtWordNum(nVars) );
    if ( nVars > 6 )
Alan Mishchenko committed
577
        Cost1 = s_pFuncIsopCover[nVars]( pFunc, pFunc, pRes, CostInit, pCover ? pCover + Abc_CostCubes(Cost0) : NULL );
Alan Mishchenko committed
578
    else
Alan Mishchenko committed
579
        Cost1 = Abc_Isop6Cover( *pFunc, *pFunc, pRes, nVars, CostInit, pCover ? pCover + Abc_CostCubes(Cost0) : NULL );
Alan Mishchenko committed
580
    Abc_TtNot( pFunc, Abc_TtWordNum(nVars) );
Alan Mishchenko committed
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
    if ( Cost0 + Cost1 >= CostInit )
        return CostInit;
    nCubes0 = Abc_CostCubes(Cost0);
    nCubes1 = Abc_CostCubes(Cost1);
    if ( pCover )
    {
        for ( c = 0; c < nCubes0; c++ )
            pCover[c] |= (1 << Abc_Var2Lit(nVars, 0));
        for ( c = 0; c < nCubes1; c++ )
            pCover[c+nCubes0] |= (1 << Abc_Var2Lit(nVars, 1));
    }
    return nCubes0 + nCubes1;
}


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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_IsopCountLits( Vec_Int_t * vCover, int nVars )
{
    int i, k, Entry, Literal, nLits = 0;
    if ( Vec_IntSize(vCover) == 0 || (Vec_IntSize(vCover) == 1 && Vec_IntEntry(vCover, 0) == 0) )
        return 0;
    Vec_IntForEachEntry( vCover, Entry, i )
    { 
        for ( k = 0; k < nVars; k++ )
        {
            Literal = 3 & (Entry >> (k << 1));
            if ( Literal == 1 ) // neg literal
                nLits++;
            else if ( Literal == 2 ) // pos literal
                nLits++;
            else if ( Literal != 0 ) 
                assert( 0 );
        }
    }
    return nLits;
}
void Abc_IsopPrintCover( Vec_Int_t * vCover, int nVars, int fCompl )
{
    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 );
        }
        printf( " %d\n", !fCompl );
    }
}
void Abc_IsopPrint( word * t, int nVars, Vec_Int_t * vCover, int fTryBoth )
{
    int fCompl = Abc_Isop( t, nVars, ABC_ISOP_MAX_CUBE, vCover, fTryBoth );
    Abc_IsopPrintCover( vCover, nVars, fCompl );
Alan Mishchenko committed
655 656 657 658 659 660 661 662 663 664 665 666 667 668
}


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

  Synopsis    [These procedures assume that function has exact support.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
669
static inline int Abc_EsopAddLits( int * pCover, word r0, word r1, word r2, word Max, int Var )
Alan Mishchenko committed
670
{
Alan Mishchenko committed
671
    int i, c0, c1, c2;
Alan Mishchenko committed
672 673
    if ( Max == r0 )
    {
Alan Mishchenko committed
674
        c2 = Abc_CostCubes(r2);
Alan Mishchenko committed
675 676
        if ( pCover )
        {
Alan Mishchenko committed
677 678 679 680 681 682
            c0 = Abc_CostCubes(r0);
            c1 = Abc_CostCubes(r1);
            for ( i = 0; i < c1; i++ )
                pCover[i] = pCover[c0+i];
            for ( i = 0; i < c2; i++ )
                pCover[c1+i] = pCover[c0+c1+i] | (1 << Abc_Var2Lit(Var,0));
Alan Mishchenko committed
683
        }
Alan Mishchenko committed
684
        return c2;
Alan Mishchenko committed
685 686 687
    }
    else if ( Max == r1 )
    {
Alan Mishchenko committed
688
        c2 = Abc_CostCubes(r2);
Alan Mishchenko committed
689 690
        if ( pCover )
        {
Alan Mishchenko committed
691 692 693 694
            c0 = Abc_CostCubes(r0);
            c1 = Abc_CostCubes(r1);
            for ( i = 0; i < c2; i++ )
                pCover[c0+i] = pCover[c0+c1+i] | (1 << Abc_Var2Lit(Var,1));
Alan Mishchenko committed
695
        }
Alan Mishchenko committed
696
        return c2;
Alan Mishchenko committed
697 698 699
    }
    else
    {
Alan Mishchenko committed
700 701
        c0 = Abc_CostCubes(r0);
        c1 = Abc_CostCubes(r1);
Alan Mishchenko committed
702 703
        if ( pCover )
        {
Alan Mishchenko committed
704 705
            c2 = Abc_CostCubes(r2);
            for ( i = 0; i < c0; i++ )
Alan Mishchenko committed
706
                pCover[i] |= (1 << Abc_Var2Lit(Var,0));
Alan Mishchenko committed
707 708
            for ( i = 0; i < c1; i++ )
                pCover[c0+i] |= (1 << Abc_Var2Lit(Var,1));
Alan Mishchenko committed
709
        }
Alan Mishchenko committed
710
        return c0 + c1;
Alan Mishchenko committed
711 712
    }
}
Alan Mishchenko committed
713
word Abc_Esop6Cover( word t, int nVars, word CostLim, int * pCover )
Alan Mishchenko committed
714 715
{
    word c0, c1;
Alan Mishchenko committed
716 717
    word r0, r1, r2, Max;
    int Var;
Alan Mishchenko committed
718 719 720 721 722 723
    assert( nVars <= 6 );
    if ( t == 0 )
        return 0;
    if ( t == ~(word)0 )
    {
        if ( pCover ) *pCover = 0;
Alan Mishchenko committed
724
        return Abc_Cube2Cost(1);
Alan Mishchenko committed
725 726 727 728 729 730 731 732 733 734 735
    }
    assert( nVars > 0 );
    // find the topmost var
    for ( Var = nVars-1; Var >= 0; Var-- )
        if ( Abc_Tt6HasVar( t, Var ) )
             break;
    assert( Var >= 0 );
    // cofactor
    c0 = Abc_Tt6Cofactor0( t, Var );
    c1 = Abc_Tt6Cofactor1( t, Var );
    // call recursively
Alan Mishchenko committed
736 737 738 739 740 741 742 743
    r0 = Abc_Esop6Cover( c0,      Var, CostLim, pCover ? pCover : NULL );
    if ( r0 >= CostLim ) return CostLim;
    r1 = Abc_Esop6Cover( c1,      Var, CostLim, pCover ? pCover + Abc_CostCubes(r0) : NULL );
    if ( r1 >= CostLim ) return CostLim;
    r2 = Abc_Esop6Cover( c0 ^ c1, Var, CostLim, pCover ? pCover + Abc_CostCubes(r0) + Abc_CostCubes(r1) : NULL );
    if ( r2 >= CostLim ) return CostLim;
    Max = Abc_MaxWord( r0, Abc_MaxWord(r1, r2) );
    if ( r0 + r1 + r2 - Max >= CostLim ) return CostLim;
Alan Mishchenko committed
744
    return r0 + r1 + r2 - Max + Abc_EsopAddLits( pCover, r0, r1, r2, Max, Var );
Alan Mishchenko committed
745
}
Alan Mishchenko committed
746
word Abc_EsopCover( word * pOn, int nVars, word CostLim, int * pCover )
Alan Mishchenko committed
747
{
Alan Mishchenko committed
748 749
    word r0, r1, r2, Max;
    int c, nWords = (1 << (nVars - 7));
Alan Mishchenko committed
750
    assert( nVars > 6 );
Alan Mishchenko committed
751
    assert( Abc_TtHasVar( pOn, nVars, nVars - 1 ) );
Alan Mishchenko committed
752 753 754 755
    r0 = Abc_EsopCheck( pOn,        nVars-1, CostLim, pCover );
    if ( r0 >= CostLim ) return CostLim;
    r1 = Abc_EsopCheck( pOn+nWords, nVars-1, CostLim, pCover ? pCover + Abc_CostCubes(r0) : NULL );
    if ( r1 >= CostLim ) return CostLim;
Alan Mishchenko committed
756 757
    for ( c = 0; c < nWords; c++ )
        pOn[c] ^= pOn[nWords+c];
Alan Mishchenko committed
758
    r2 = Abc_EsopCheck( pOn, nVars-1, CostLim, pCover ? pCover + Abc_CostCubes(r0) + Abc_CostCubes(r1) : NULL );
Alan Mishchenko committed
759 760
    for ( c = 0; c < nWords; c++ )
        pOn[c] ^= pOn[nWords+c];
Alan Mishchenko committed
761 762 763
    if ( r2 >= CostLim ) return CostLim;
    Max = Abc_MaxWord( r0, Abc_MaxWord(r1, r2) );
    if ( r0 + r1 + r2 - Max >= CostLim ) return CostLim;
Alan Mishchenko committed
764
    return r0 + r1 + r2 - Max + Abc_EsopAddLits( pCover, r0, r1, r2, Max, nVars-1 );
Alan Mishchenko committed
765
}
Alan Mishchenko committed
766
word Abc_EsopCheck( word * pOn, int nVars, word CostLim, int * pCover )
Alan Mishchenko committed
767
{
Alan Mishchenko committed
768
    int nVarsNew; word Cost;
Alan Mishchenko committed
769
    if ( nVars <= 6 )
Alan Mishchenko committed
770
        return Abc_Esop6Cover( *pOn, nVars, CostLim, pCover );
Alan Mishchenko committed
771 772 773 774
    for ( nVarsNew = nVars; nVarsNew > 6; nVarsNew-- )
        if ( Abc_TtHasVar( pOn, nVars, nVarsNew-1 ) )
            break;
    if ( nVarsNew == 6 )
Alan Mishchenko committed
775
        Cost = Abc_Esop6Cover( *pOn, nVarsNew, CostLim, pCover );
Alan Mishchenko committed
776
    else
Alan Mishchenko committed
777
        Cost = Abc_EsopCover( pOn, nVarsNew, CostLim, pCover );
Alan Mishchenko committed
778
    return Cost;
Alan Mishchenko committed
779 780
}

Alan Mishchenko committed
781

Alan Mishchenko committed
782 783
/**Function*************************************************************

Alan Mishchenko committed
784
  Synopsis    [Perform ISOP computation by subtracting cubes.]
Alan Mishchenko committed
785 786 787 788 789 790 791 792

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
793
static inline int Abc_TtIntersect( word * pIn1, word * pIn2, int nWords )
Alan Mishchenko committed
794
{
Alan Mishchenko committed
795 796 797 798 799 800 801 802 803
    int w;
    for ( w = 0; w < nWords; w++ )
        if ( pIn1[w] & pIn2[w] )
            return 1;
    return 0;
}
static inline int Abc_TtCheckWithCubePos2Neg( word * t, word * c, int nWords, int iVar )
{
    if ( iVar < 6 )
Alan Mishchenko committed
804
    {
Alan Mishchenko committed
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
        int i, Shift = (1 << iVar);
        for ( i = 0; i < nWords; i++ )
            if ( t[i] & (c[i] >> Shift) )
                return 0;
        return 1;
    }
    else
    {
        int i, Step = (1 << (iVar - 6));
        word * tLimit = t + nWords;
        for ( ; t < tLimit; t += 2*Step )
            for ( i = 0; i < Step; i++ )
                if ( t[Step+i] & c[i] )
                    return 0;
        return 1;
Alan Mishchenko committed
820 821
    }
}
Alan Mishchenko committed
822
static inline int Abc_TtCheckWithCubeNeg2Pos( word * t, word * c, int nWords, int iVar )
Alan Mishchenko committed
823
{
Alan Mishchenko committed
824
    if ( iVar < 6 )
Alan Mishchenko committed
825
    {
Alan Mishchenko committed
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
        int i, Shift = (1 << iVar);
        for ( i = 0; i < nWords; i++ )
            if ( t[i] & (c[i] << Shift) )
                return 0;
        return 1;
    }
    else
    {
        int i, Step = (1 << (iVar - 6));
        word * tLimit = t + nWords;
        for ( ; t < tLimit; t += 2*Step )
            for ( i = 0; i < Step; i++ )
                if ( t[i] & c[Step+i] )
                    return 0;
        return 1;
Alan Mishchenko committed
841 842
    }
}
Alan Mishchenko committed
843
static inline void Abc_TtExpandCubePos2Neg( word * t, int nWords, int iVar )
Alan Mishchenko committed
844
{
Alan Mishchenko committed
845 846 847 848 849 850 851 852 853 854 855 856 857 858
    if ( iVar < 6 )
    {
        int i, Shift = (1 << iVar);
        for ( i = 0; i < nWords; i++ )
            t[i] |= (t[i] >> Shift);
    }
    else
    {
        int i, Step = (1 << (iVar - 6));
        word * tLimit = t + nWords;
        for ( ; t < tLimit; t += 2*Step )
            for ( i = 0; i < Step; i++ )
                t[i] = t[Step+i];
    }
Alan Mishchenko committed
859
}
Alan Mishchenko committed
860
static inline void Abc_TtExpandCubeNeg2Pos( word * t, int nWords, int iVar )
Alan Mishchenko committed
861
{
Alan Mishchenko committed
862
    if ( iVar < 6 )
Alan Mishchenko committed
863
    {
Alan Mishchenko committed
864 865 866
        int i, Shift = (1 << iVar);
        for ( i = 0; i < nWords; i++ )
            t[i] |= (t[i] << Shift);
Alan Mishchenko committed
867
    }
Alan Mishchenko committed
868
    else
Alan Mishchenko committed
869
    {
Alan Mishchenko committed
870 871 872 873 874
        int i, Step = (1 << (iVar - 6));
        word * tLimit = t + nWords;
        for ( ; t < tLimit; t += 2*Step )
            for ( i = 0; i < Step; i++ )
                t[Step+i] = t[i];
Alan Mishchenko committed
875
    }
Alan Mishchenko committed
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
}
word Abc_IsopNew( word * pOn, word * pOnDc, word * pRes, int nVars, word CostLim, int * pCover )
{
    word pCube[ABC_ISOP_MAX_WORD];
    word pOnset[ABC_ISOP_MAX_WORD];
    word pOffset[ABC_ISOP_MAX_WORD];
    int pBlocks[16], nBlocks, vTwo, uTwo;
    int nWords = Abc_TtWordNum(nVars);
    int c, v, u, iMint, Cube, nLits = 0;
    assert( nVars <= ABC_ISOP_MAX_VAR );
    Abc_TtClear( pRes, nWords );
    Abc_TtCopy( pOnset, pOn, nWords, 0 ); 
    Abc_TtCopy( pOffset, pOnDc, nWords, 1 ); 
    if ( nVars < 6 )
        pOnset[0] >>= (64 - (1 << nVars));
    for ( c = 0; !Abc_TtIsConst0(pOnset, nWords); c++ )
Alan Mishchenko committed
892
    {
Alan Mishchenko committed
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
        // pick one minterm
        iMint = Abc_TtFindFirstBit(pOnset, nVars);
        for ( Cube = v = 0; v < nVars; v++ )
            Cube |= (1 << Abc_Var2Lit(v, (iMint >> v) & 1));
        // check expansion for the minterm
        nBlocks = 0;
        for ( v = 0; v < nVars; v++ )
            if ( (pBlocks[v] = Abc_TtGetBit(pOffset, iMint ^ (1 << v))) )
                nBlocks++;
        // check second step
        if ( nBlocks == nVars ) // cannot expand
        {
            Abc_TtSetBit( pRes, iMint );
            Abc_TtXorBit( pOnset, iMint );
            pCover[c] = Cube;
            nLits += nVars;
            continue;
        }
        // check dual expansion
        vTwo = uTwo = -1;
        if ( nBlocks < nVars - 1 )
        {
            for ( v = 0; v < nVars && vTwo == -1; v++ )
            if ( !pBlocks[v] )
            for ( u = v + 1; u < nVars; u++ )
            if ( !pBlocks[u] )
            {
                if ( Abc_TtGetBit( pOffset, iMint ^ (1 << v) ^ (1 << u) ) )
                    continue;
                // can expand both directions
                vTwo = v;
                uTwo = u;
                break;
            }
        }
        if ( vTwo == -1 ) // can expand only one
        {
            for ( v = 0; v < nVars; v++ )
                if ( !pBlocks[v] )
                    break;
            Abc_TtSetBit( pRes, iMint );
            Abc_TtSetBit( pRes, iMint ^ (1 << v) );
            Abc_TtXorBit( pOnset, iMint );
            if ( Abc_TtGetBit(pOnset, iMint ^ (1 << v)) )
                Abc_TtXorBit( pOnset, iMint ^ (1 << v) );
            pCover[c] = Cube & ~(3 << Abc_Var2Lit(v, 0));
            nLits += nVars - 1;
            continue;
        }
        if ( nBlocks == nVars - 2 && vTwo >= 0 ) // can expand only these two
        {
            Abc_TtSetBit( pRes, iMint );
            Abc_TtSetBit( pRes, iMint ^ (1 << vTwo) );
            Abc_TtSetBit( pRes, iMint ^ (1 << uTwo) );
            Abc_TtSetBit( pRes, iMint ^ (1 << vTwo) ^ (1 << uTwo) );
            Abc_TtXorBit( pOnset, iMint );
            if ( Abc_TtGetBit(pOnset, iMint ^ (1 << vTwo)) )
                Abc_TtXorBit( pOnset, iMint ^ (1 << vTwo) );
            if ( Abc_TtGetBit(pOnset, iMint ^ (1 << uTwo)) )
                Abc_TtXorBit( pOnset, iMint ^ (1 << uTwo) );
            if ( Abc_TtGetBit(pOnset, iMint ^ (1 << vTwo) ^ (1 << uTwo)) )
                Abc_TtXorBit( pOnset, iMint ^ (1 << vTwo) ^ (1 << uTwo) );
            pCover[c] = Cube & ~(3 << Abc_Var2Lit(vTwo, 0)) & ~(3 << Abc_Var2Lit(uTwo, 0));
            nLits += nVars - 2;
            continue;
        }
        // can expand others as well
        Abc_TtClear( pCube, nWords );
        Abc_TtSetBit( pCube, iMint );
        Abc_TtSetBit( pCube, iMint ^ (1 << vTwo) );
        Abc_TtSetBit( pCube, iMint ^ (1 << uTwo) );
        Abc_TtSetBit( pCube, iMint ^ (1 << vTwo) ^ (1 << uTwo) );
        Cube &= ~(3 << Abc_Var2Lit(vTwo, 0)) & ~(3 << Abc_Var2Lit(uTwo, 0));
        assert( !Abc_TtIntersect(pCube, pOffset, nWords) );        
        // expand against offset
        for ( v = 0; v < nVars; v++ )
        if ( v != vTwo && v != uTwo )
        {
            int Shift = Abc_Var2Lit( v, 0 );
            if ( (Cube >> Shift) == 2 && Abc_TtCheckWithCubePos2Neg(pOffset, pCube, nWords, v) ) // pos literal
            {
                Abc_TtExpandCubePos2Neg( pCube, nWords, v );
                Cube &= ~(3 << Shift);
            }
            else if ( (Cube >> Shift) == 1 && Abc_TtCheckWithCubeNeg2Pos(pOffset, pCube, nWords, v) ) // neg literal
            {
                Abc_TtExpandCubeNeg2Pos( pCube, nWords, v );
                Cube &= ~(3 << Shift);
            }
            else 
                nLits++;
        }
        // add cube to solution
        Abc_TtOr( pRes, pRes, pCube, nWords );
        Abc_TtSharp( pOnset, pOnset, pCube, nWords );
        pCover[c] = Cube;
Alan Mishchenko committed
989
    }
Alan Mishchenko committed
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
    pRes[0] = Abc_Tt6Stretch( pRes[0], nVars );
    return Abc_Cube2Cost(c) | nLits;
}
void Abc_IsopTestNew()
{
    int nVars = 4;
    Vec_Int_t * vCover = Vec_IntAlloc( 1000 );
    word r, t = (s_Truths6[0] & s_Truths6[1]) ^ (s_Truths6[2] & s_Truths6[3]), copy = t;
//    word r, t = ~s_Truths6[0] | (s_Truths6[1] & s_Truths6[2] & s_Truths6[3]), copy = t;
//    word r, t = 0xABCDABCDABCDABCD, copy = t;
//    word r, t = 0x6996000000006996, copy = t;
//    word Cost = Abc_IsopNew( &t, &t, &r, nVars, Abc_Cube2Cost(ABC_ISOP_MAX_CUBE), Vec_IntArray(vCover) );
    word Cost = Abc_EsopCheck( &t, nVars, Abc_Cube2Cost(ABC_ISOP_MAX_CUBE), Vec_IntArray(vCover) );
    vCover->nSize = Abc_CostCubes(Cost);
    assert( vCover->nSize <= vCover->nCap );
    printf( "Cubes = %d.  Lits = %d.\n", Abc_CostCubes(Cost), Abc_CostLits(Cost) );
    Abc_IsopPrintCover( vCover, nVars, 0 );
    Abc_IsopVerify( &copy, nVars, &r, vCover, 1, 0 );
    Vec_IntFree( vCover );
Alan Mishchenko committed
1009 1010
}

Alan Mishchenko committed
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
/**Function*************************************************************

  Synopsis    [Compute CNF assuming it does not exceed the limit.]

  Description [Please note that pCover should have at least 32 extra entries!]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_IsopTest( word * pFunc, int nVars, Vec_Int_t * vCover )
{
Alan Mishchenko committed
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
    int fVerbose = 0;    
    static word TotalCost[6] = {0};
    static abctime TotalTime[6] = {0};
    static int Counter;
    word pRes[ABC_ISOP_MAX_WORD];
    word Cost;
    abctime clk;
    Counter++;
    if ( Counter == 9999 )
    {
        Abc_PrintTime( 1, "0", TotalTime[0] );
        Abc_PrintTime( 1, "1", TotalTime[1] );
        Abc_PrintTime( 1, "2", TotalTime[2] );
        Abc_PrintTime( 1, "3", TotalTime[3] );
        Abc_PrintTime( 1, "4", TotalTime[4] );
        Abc_PrintTime( 1, "5", TotalTime[5] );
    }
    assert( nVars <= ABC_ISOP_MAX_VAR );
Alan Mishchenko committed
1042 1043 1044
//    if ( fVerbose )
//    Dau_DsdPrintFromTruth( pFunc, nVars ), printf( "         " );

Alan Mishchenko committed
1045 1046 1047
    clk = Abc_Clock();
    Cost = Abc_IsopCheck( pFunc, pFunc, pRes, nVars, Abc_Cube2Cost(ABC_ISOP_MAX_CUBE), Vec_IntArray(vCover) );
    vCover->nSize = Abc_CostCubes(Cost);
Alan Mishchenko committed
1048 1049
    assert( vCover->nSize <= vCover->nCap );
    if ( fVerbose )
Alan Mishchenko committed
1050 1051 1052 1053
    printf( "%5d %7d  ", Abc_CostCubes(Cost), Abc_CostLits(Cost) );
//    Abc_IsopVerify( pFunc, nVars, pRes, vCover, 0, 0 );
    TotalCost[0] += Abc_CostCubes(Cost);
    TotalTime[0] += Abc_Clock() - clk;
Alan Mishchenko committed
1054 1055


Alan Mishchenko committed
1056
    clk = Abc_Clock();
Alan Mishchenko committed
1057
    Abc_TtNot( pFunc, Abc_TtWordNum(nVars) );
Alan Mishchenko committed
1058
    Cost = Abc_IsopCheck( pFunc, pFunc, pRes, nVars, Abc_Cube2Cost(ABC_ISOP_MAX_CUBE), Vec_IntArray(vCover) );
Alan Mishchenko committed
1059
    Abc_TtNot( pFunc, Abc_TtWordNum(nVars) );
Alan Mishchenko committed
1060
    vCover->nSize = Abc_CostCubes(Cost);
Alan Mishchenko committed
1061 1062
    assert( vCover->nSize <= vCover->nCap );
    if ( fVerbose )
Alan Mishchenko committed
1063 1064 1065 1066 1067 1068 1069 1070 1071
    printf( "%5d %7d  ", Abc_CostCubes(Cost), Abc_CostLits(Cost) );
//    Abc_IsopVerify( pFunc, nVars, pRes, vCover, 0, 1 );
    TotalCost[1] += Abc_CostCubes(Cost);
    TotalTime[1] += Abc_Clock() - clk;

/*
    clk = Abc_Clock();
    Cost = Abc_EsopCheck( pFunc, nVars, Abc_Cube2Cost(ABC_ISOP_MAX_CUBE), Vec_IntArray(vCover) );
    vCover->nSize = Abc_CostCubes(Cost);
Alan Mishchenko committed
1072 1073
    assert( vCover->nSize <= vCover->nCap );
    if ( fVerbose )
Alan Mishchenko committed
1074 1075 1076 1077
    printf( "%5d %7d   ", Abc_CostCubes(Cost), Abc_CostLits(Cost) );
//    Abc_IsopVerify( pFunc, nVars, pRes, vCover, 1, 0 );
    TotalCost[2] += Abc_CostCubes(Cost);
    TotalTime[2] += Abc_Clock() - clk;
Alan Mishchenko committed
1078 1079


Alan Mishchenko committed
1080
    clk = Abc_Clock();
Alan Mishchenko committed
1081
    Abc_TtNot( pFunc, Abc_TtWordNum(nVars) );
Alan Mishchenko committed
1082
    Cost = Abc_EsopCheck( pFunc, nVars, Abc_Cube2Cost(ABC_ISOP_MAX_CUBE), Vec_IntArray(vCover) );
Alan Mishchenko committed
1083
    Abc_TtNot( pFunc, Abc_TtWordNum(nVars) );
Alan Mishchenko committed
1084
    vCover->nSize = Abc_CostCubes(Cost);
Alan Mishchenko committed
1085 1086
    assert( vCover->nSize <= vCover->nCap );
    if ( fVerbose )
Alan Mishchenko committed
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
    printf( "%5d %7d   ", Abc_CostCubes(Cost), Abc_CostLits(Cost) );
//    Abc_IsopVerify( pFunc, nVars, pRes, vCover, 1, 1 );
    TotalCost[3] += Abc_CostCubes(Cost);
    TotalTime[3] += Abc_Clock() - clk;
*/

/*
    // try new computation
    clk = Abc_Clock();
    Cost = Abc_IsopNew( pFunc, pFunc, pRes, nVars, Abc_Cube2Cost(ABC_ISOP_MAX_CUBE), Vec_IntArray(vCover) );
    vCover->nSize = Abc_CostCubes(Cost);
    assert( vCover->nSize <= vCover->nCap );
    if ( fVerbose )
    printf( "%5d %7d   ", Abc_CostCubes(Cost), Abc_CostLits(Cost) );
    Abc_IsopVerify( pFunc, nVars, pRes, vCover, 0, 0 );
    TotalCost[4] += Abc_CostCubes(Cost);
    TotalTime[4] += Abc_Clock() - clk;
*/
1105
/*
Alan Mishchenko committed
1106 1107 1108 1109 1110 1111 1112 1113 1114
    // try old computation
    clk = Abc_Clock();
    Cost = Kit_TruthIsop( (unsigned *)pFunc, nVars, vCover, 1 );
    vCover->nSize = Vec_IntSize(vCover);
    assert( vCover->nSize <= vCover->nCap );
    if ( fVerbose )
    printf( "%5d %7d   ", Vec_IntSize(vCover), Abc_IsopCountLits(vCover, nVars) );
    TotalCost[4] += Vec_IntSize(vCover);
    TotalTime[4] += Abc_Clock() - clk;
1115
*/
Alan Mishchenko committed
1116

Alan Mishchenko committed
1117 1118 1119 1120 1121 1122 1123 1124 1125
    clk = Abc_Clock();
    Cost = Abc_Isop( pFunc, nVars, ABC_ISOP_MAX_CUBE, vCover, 1 );
    if ( fVerbose )
    printf( "%5d %7d   ", Vec_IntSize(vCover), Abc_IsopCountLits(vCover, nVars) );
    TotalCost[5] += Vec_IntSize(vCover);
    TotalTime[5] += Abc_Clock() - clk;

    if ( fVerbose )
    printf( "  | %8d %8d %8d %8d %8d %8d", (int)TotalCost[0], (int)TotalCost[1], (int)TotalCost[2], (int)TotalCost[3], (int)TotalCost[4], (int)TotalCost[5] );
Alan Mishchenko committed
1126 1127
    if ( fVerbose )
    printf( "\n" );
Alan Mishchenko committed
1128
//Abc_IsopPrintCover( vCover, nVars, 0 );
Alan Mishchenko committed
1129 1130 1131
    return 1; 
}

Alan Mishchenko committed
1132

Alan Mishchenko committed
1133 1134 1135 1136 1137 1138 1139
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


ABC_NAMESPACE_IMPL_END