dauDsd.c 70.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/**CFile****************************************************************

  FileName    [dauDsd.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [DAG-aware unmapping.]

  Synopsis    [Disjoint-support decomposition.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "dauInt.h"
22
#include "misc/util/utilTruth.h"
23 24 25 26 27 28 29

ABC_NAMESPACE_IMPL_START

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

30 31 32 33 34 35 36
/* 
    This code performs truth-table-based decomposition for 6-variable functions.
    Representation of operations:
    ! = not; 
    (ab) = a and b;  
    [ab] = a xor b;  
    <abc> = ITE( a, b, c )
Alan Mishchenko committed
37
    FUNCTION{abc} = FUNCTION( a, b, c )
38
*/
39

Alan Mishchenko committed
40

41 42 43 44 45 46
////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

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

Alan Mishchenko committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
  Synopsis    [Elementary truth tables.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline word ** Dau_DsdTtElems()
{
    static word TtElems[DAU_MAX_VAR+1][DAU_MAX_WORD], * pTtElems[DAU_MAX_VAR+1] = {NULL};
    if ( pTtElems[0] == NULL )
    {
        int v;
        for ( v = 0; v <= DAU_MAX_VAR; v++ )
            pTtElems[v] = TtElems[v];
        Abc_TtElemInit( pTtElems, DAU_MAX_VAR );
    }
    return pTtElems;
}

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

Alan Mishchenko committed
71
  Synopsis    [DSD formula manipulation.]
72 73 74 75 76 77 78 79

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
80 81 82 83 84 85 86 87 88 89 90 91
int * Dau_DsdComputeMatches( char * p )
{
    static int pMatches[DAU_MAX_STR];
    int pNested[DAU_MAX_VAR];
    int v, nNested = 0;
    for ( v = 0; p[v]; v++ )
    {
        pMatches[v] = 0;
        if ( p[v] == '(' || p[v] == '[' || p[v] == '<' || p[v] == '{' )
            pNested[nNested++] = v;
        else if ( p[v] == ')' || p[v] == ']' || p[v] == '>' || p[v] == '}' )
            pMatches[pNested[--nNested]] = v;
Alan Mishchenko committed
92
        assert( nNested < DAU_MAX_VAR );
Alan Mishchenko committed
93 94 95 96
    }
    assert( nNested == 0 );
    return pMatches;
}
Alan Mishchenko committed
97

Alan Mishchenko committed
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
/**Function*************************************************************

  Synopsis    [Generate random permutation.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Dau_DsdFindVarNum( char * pDsd )
{
    int vMax = 0;
    pDsd--;
    while ( *++pDsd )
        if ( *pDsd >= 'a' && *pDsd <= 'z' )
            vMax = Abc_MaxInt( vMax, *pDsd - 'a' );
    return vMax + 1;
}
void Dau_DsdGenRandPerm( int * pPerm, int nVars )
{
    int v, vNew;
    for ( v = 0; v < nVars; v++ )
        pPerm[v] = v;
    for ( v = 0; v < nVars; v++ )
    {
        vNew = rand() % nVars;
        ABC_SWAP( int, pPerm[v], pPerm[vNew] );
    }
}
void Dau_DsdPermute( char * pDsd )
{
    int pPerm[16];
    int nVars = Dau_DsdFindVarNum( pDsd );
    Dau_DsdGenRandPerm( pPerm, nVars );
    pDsd--;
    while ( *++pDsd )
        if ( *pDsd >= 'a' && *pDsd < 'a' + nVars )
            *pDsd = 'a' + pPerm[*pDsd - 'a'];
}

Alan Mishchenko committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
/**Function*************************************************************

  Synopsis    [Normalize the ordering of components.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
char * Dau_DsdNormalizeCopy( char * pDest, char * pSour, int * pMarks, int i )
{
    int s;
    for ( s = pMarks[i]; s < pMarks[i+1]; s++ )
        *pDest++ = pSour[s];
    return pDest;
}
int Dau_DsdNormalizeCompare( char * pStr, int * pMarks, int i, int j )
{
    char * pStr1 = pStr + pMarks[i];
    char * pStr2 = pStr + pMarks[j];
    char * pLimit1 = pStr + pMarks[i+1];
    char * pLimit2 = pStr + pMarks[j+1];
    for ( ; pStr1 < pLimit1 && pStr2 < pLimit2; pStr1++, pStr2++ )
    {
        if ( !(*pStr1 >= 'a' &&  *pStr1 <= 'z') )
        {
            pStr2--;
            continue;
        }
        if ( !(*pStr2 >= 'a' &&  *pStr2 <= 'z') )
        {
            pStr1--;
            continue;
        }
        if ( *pStr1 < *pStr2 )
            return -1;
        if ( *pStr1 > *pStr2 )
            return 1;
    }
    assert( pStr1 < pLimit1 || pStr2 < pLimit2 );
    if ( pStr1 == pLimit1 )
        return -1;
    if ( pStr2 == pLimit2 )
        return 1;
    assert( 0 );
    return 0;
}
int * Dau_DsdNormalizePerm( char * pStr, int * pMarks, int nMarks )
{
    static int pPerm[DAU_MAX_VAR];
    int i, k;
    for ( i = 0; i < nMarks; i++ )
        pPerm[i] = i;
    for ( i = 0; i < nMarks; i++ )
    {
        int iBest = i;
        for ( k = i + 1; k < nMarks; k++ )
            if ( Dau_DsdNormalizeCompare( pStr, pMarks, pPerm[iBest], pPerm[k] ) == 1 )
                iBest = k;
        ABC_SWAP( int, pPerm[i], pPerm[iBest] );
    }
    return pPerm;
}
void Dau_DsdNormalize_rec( char * pStr, char ** p, int * pMatches )
{
    static char pBuffer[DAU_MAX_STR];
    if ( **p == '!' )
        (*p)++;
Alan Mishchenko committed
210 211 212 213 214 215 216 217
    while ( (**p >= 'A' && **p <= 'F') || (**p >= '0' && **p <= '9') )
        (*p)++;
    if ( **p == '<' )
    {
        char * q = pStr + pMatches[*p - pStr];
        if ( *(q+1) == '{' )
            *p = q+1;
    }
Alan Mishchenko committed
218
    if ( **p >= 'a' && **p <= 'z' ) // var
Alan Mishchenko committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
        return;
    if ( **p == '(' || **p == '[' ) // and/or/xor
    {
        char * pStore, * pOld = *p + 1;
        char * q = pStr + pMatches[ *p - pStr ];
        int i, * pPerm, nMarks = 0, pMarks[DAU_MAX_VAR+1];
        assert( *q == **p + 1 + (**p != '(') );
        for ( (*p)++; *p < q; (*p)++ )
        {
            pMarks[nMarks++] = *p - pStr;
            Dau_DsdNormalize_rec( pStr, p, pMatches );
        }
        pMarks[nMarks] = *p - pStr;
        assert( *p == q );
        // add to buffer in good order
        pPerm = Dau_DsdNormalizePerm( pStr, pMarks, nMarks );
        // copy to the buffer
        pStore = pBuffer;
        for ( i = 0; i < nMarks; i++ )
            pStore = Dau_DsdNormalizeCopy( pStore, pStr, pMarks, pPerm[i] );
        assert( pStore - pBuffer == *p - pOld );
        memcpy( pOld, pBuffer, pStore - pBuffer );
        return;
    }
    if ( **p == '<' || **p == '{' ) // mux
    {
        char * q = pStr + pMatches[ *p - pStr ];
        assert( *q == **p + 1 + (**p != '(') );
        if ( (**p == '<') && (*(q+1) == '{') )
        {
            *p = q+1;
            Dau_DsdNormalize_rec( pStr, p, pMatches );
            return;
        }
        for ( (*p)++; *p < q; (*p)++ )
            Dau_DsdNormalize_rec( pStr, p, pMatches );
        assert( *p == q );
        return;
    }
    assert( 0 );
}
void Dau_DsdNormalize( char * pDsd )
{
    if ( pDsd[1] != 0 )
        Dau_DsdNormalize_rec( pDsd, &pDsd, Dau_DsdComputeMatches(pDsd) );
}


Alan Mishchenko committed
267 268 269

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

Alan Mishchenko committed
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Dau_DsdCountAnds_rec( char * pStr, char ** p, int * pMatches )
{
    if ( **p == '!' )
        (*p)++;
    while ( (**p >= 'A' && **p <= 'F') || (**p >= '0' && **p <= '9') )
        (*p)++;
    if ( **p == '<' )
    {
        char * q = pStr + pMatches[*p - pStr];
        if ( *(q+1) == '{' )
            *p = q+1;
    }
    if ( **p >= 'a' && **p <= 'z' ) // var
        return 0;
    if ( **p == '(' || **p == '[' ) // and/or/xor
    {
        int Counter = 0, AddOn = (**p == '(')? 1 : 3;
        char * q = pStr + pMatches[ *p - pStr ];
        assert( *q == **p + 1 + (**p != '(') );
        for ( (*p)++; *p < q; (*p)++ )
            Counter += AddOn + Dau_DsdCountAnds_rec( pStr, p, pMatches );
        assert( *p == q );
        return Counter - AddOn;
    }
    if ( **p == '<' || **p == '{' ) // mux
    {
        int Counter = 3;
        char * q = pStr + pMatches[ *p - pStr ];
        assert( *q == **p + 1 + (**p != '(') );
        for ( (*p)++; *p < q; (*p)++ )
            Counter += Dau_DsdCountAnds_rec( pStr, p, pMatches );
        assert( *p == q );
        return Counter;
    }
    assert( 0 );
    return 0;
}
int Dau_DsdCountAnds( char * pDsd )
{
    if ( pDsd[1] == 0 )
        return 0;
    return Dau_DsdCountAnds_rec( pDsd, &pDsd, Dau_DsdComputeMatches(pDsd) );
}

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

Alan Mishchenko committed
325 326 327 328 329 330 331 332 333 334
  Synopsis    [Computes truth table for the DSD formula.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
word Dau_Dsd6TruthCompose_rec( word Func, word * pFanins, int nVars )
Alan Mishchenko committed
335 336 337 338 339 340 341 342
{
    word t0, t1;
    if ( Func == 0 )
        return 0;
    if ( Func == ~(word)0 )
        return ~(word)0;
    assert( nVars > 0 );
    if ( --nVars == 0 )
Alan Mishchenko committed
343 344
    {
        assert( Func == s_Truths6[0] || Func == s_Truths6Neg[0] );
Alan Mishchenko committed
345
        return (Func == s_Truths6[0]) ? pFanins[0] : ~pFanins[0];
Alan Mishchenko committed
346
    }
Alan Mishchenko committed
347
    if ( !Abc_Tt6HasVar(Func, nVars) )
Alan Mishchenko committed
348 349 350
        return Dau_Dsd6TruthCompose_rec( Func, pFanins, nVars );
    t0 = Dau_Dsd6TruthCompose_rec( Abc_Tt6Cofactor0(Func, nVars), pFanins, nVars );
    t1 = Dau_Dsd6TruthCompose_rec( Abc_Tt6Cofactor1(Func, nVars), pFanins, nVars );
Alan Mishchenko committed
351 352
    return (~pFanins[nVars] & t0) | (pFanins[nVars] & t1);
}
Alan Mishchenko committed
353
word Dau_Dsd6ToTruth_rec( char * pStr, char ** p, int * pMatches, word * pTruths )
354 355 356 357
{
    int fCompl = 0;
    if ( **p == '!' )
        (*p)++, fCompl = 1;
Alan Mishchenko committed
358
    if ( **p >= 'a' && **p <= 'f' ) // var
Alan Mishchenko committed
359 360
    {
        assert( **p - 'a' >= 0 && **p - 'a' < 6 );
Alan Mishchenko committed
361
        return fCompl ? ~pTruths[**p - 'a'] : pTruths[**p - 'a'];
Alan Mishchenko committed
362
    }
Alan Mishchenko committed
363
    if ( **p == '(' ) // and/or
364
    {
Alan Mishchenko committed
365 366 367 368
        char * q = pStr + pMatches[ *p - pStr ];
        word Res = ~(word)0;
        assert( **p == '(' && *q == ')' );
        for ( (*p)++; *p < q; (*p)++ )
Alan Mishchenko committed
369
            Res &= Dau_Dsd6ToTruth_rec( pStr, p, pMatches, pTruths );
Alan Mishchenko committed
370 371 372 373 374 375
        assert( *p == q );
        return fCompl ? ~Res : Res;
    }
    if ( **p == '[' ) // xor
    {
        char * q = pStr + pMatches[ *p - pStr ];
376
        word Res = 0;
Alan Mishchenko committed
377 378
        assert( **p == '[' && *q == ']' );
        for ( (*p)++; *p < q; (*p)++ )
Alan Mishchenko committed
379
            Res ^= Dau_Dsd6ToTruth_rec( pStr, p, pMatches, pTruths );
Alan Mishchenko committed
380 381 382 383 384
        assert( *p == q );
        return fCompl ? ~Res : Res;
    }
    if ( **p == '<' ) // mux
    {
Alan Mishchenko committed
385
        int nVars = 0;
Alan Mishchenko committed
386
        word Temp[3], * pTemp = Temp, Res;
Alan Mishchenko committed
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
        word Fanins[6], * pTruths2;
        char * pOld = *p;
        char * q = pStr + pMatches[ *p - pStr ];
        // read fanins
        if ( *(q+1) == '{' )
        {
            char * q2;
            *p = q+1;
            q2 = pStr + pMatches[ *p - pStr ];
            assert( **p == '{' && *q2 == '}' );
            for ( nVars = 0, (*p)++; *p < q2; (*p)++, nVars++ )
                Fanins[nVars] = Dau_Dsd6ToTruth_rec( pStr, p, pMatches, pTruths );
            assert( *p == q2 );
            pTruths2 = Fanins;
        }
        else
            pTruths2 = pTruths;
        // read MUX
        *p = pOld;
        q = pStr + pMatches[ *p - pStr ];
Alan Mishchenko committed
407
        assert( **p == '<' && *q == '>' );
Alan Mishchenko committed
408 409 410 411 412 413
        // verify internal variables
        if ( nVars )
            for ( ; pOld < q; pOld++ )
                if ( *pOld >= 'a' && *pOld <= 'z' )
                    assert( *pOld - 'a' < nVars );
        // derive MAX components
Alan Mishchenko committed
414
        for ( (*p)++; *p < q; (*p)++ )
Alan Mishchenko committed
415
            *pTemp++ = Dau_Dsd6ToTruth_rec( pStr, p, pMatches, pTruths2 );
Alan Mishchenko committed
416
        assert( pTemp == Temp + 3 );
417
        assert( *p == q );
Alan Mishchenko committed
418 419 420 421 422 423
        if ( *(q+1) == '{' ) // and/or
        {
            char * q = pStr + pMatches[ ++(*p) - pStr ];
            assert( **p == '{' && *q == '}' );
            *p = q;
        }
Alan Mishchenko committed
424
        Res = (Temp[0] & Temp[1]) | (~Temp[0] & Temp[2]);
425 426
        return fCompl ? ~Res : Res;
    }
Alan Mishchenko committed
427 428
    if ( (**p >= 'A' && **p <= 'F') || (**p >= '0' && **p <= '9') )
    {
Alan Mishchenko committed
429
        word Func, Fanins[6], Res;
Alan Mishchenko committed
430 431 432 433 434 435
        char * q;
        int i, nVars = Abc_TtReadHex( &Func, *p );
        *p += Abc_TtHexDigitNum( nVars );
        q = pStr + pMatches[ *p - pStr ];
        assert( **p == '{' && *q == '}' );
        for ( i = 0, (*p)++; *p < q; (*p)++, i++ )
Alan Mishchenko committed
436
            Fanins[i] = Dau_Dsd6ToTruth_rec( pStr, p, pMatches, pTruths );
Alan Mishchenko committed
437
        assert( i == nVars );
Alan Mishchenko committed
438 439 440
        assert( *p == q );
        Res = Dau_Dsd6TruthCompose_rec( Func, Fanins, nVars );
        return fCompl ? ~Res : Res;
Alan Mishchenko committed
441
    }
442 443 444
    assert( 0 );
    return 0;
}
Alan Mishchenko committed
445
word Dau_Dsd6ToTruth( char * p )
446 447
{
    word Res;
Alan Mishchenko committed
448
    if ( *p == '0' && *(p+1) == 0 )
449
        Res = 0;
Alan Mishchenko committed
450
    else if ( *p == '1' && *(p+1) == 0 )
451
        Res = ~(word)0;
452
    else
Alan Mishchenko committed
453
        Res = Dau_Dsd6ToTruth_rec( p, &p, Dau_DsdComputeMatches(p), s_Truths6 );
454 455 456 457 458 459
    assert( *++p == 0 );
    return Res;
}

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

Alan Mishchenko committed
460 461 462 463 464 465 466 467 468
  Synopsis    [Computes truth table for the DSD formula.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
469
void Dau_DsdTruth6Compose_rec( word Func, word pFanins[DAU_MAX_VAR][DAU_MAX_WORD], word * pRes, int nVars, int nWordsR )
Alan Mishchenko committed
470 471 472
{
    if ( Func == 0 )
    {
Alan Mishchenko committed
473
        Abc_TtConst0( pRes, nWordsR );
Alan Mishchenko committed
474 475 476 477
        return;
    }
    if ( Func == ~(word)0 )
    {
Alan Mishchenko committed
478
        Abc_TtConst1( pRes, nWordsR );
Alan Mishchenko committed
479 480 481 482 483 484
        return;
    }
    assert( nVars > 0 );
    if ( --nVars == 0 )
    {
        assert( Func == s_Truths6[0] || Func == s_Truths6Neg[0] );
Alan Mishchenko committed
485
        Abc_TtCopy( pRes, pFanins[0], nWordsR, Func == s_Truths6Neg[0] );
Alan Mishchenko committed
486 487 488 489
        return;
    }
    if ( !Abc_Tt6HasVar(Func, nVars) )
    {
Alan Mishchenko committed
490
        Dau_DsdTruth6Compose_rec( Func, pFanins, pRes, nVars, nWordsR );
Alan Mishchenko committed
491 492 493 494
        return;
    }
    {
        word pTtTemp[2][DAU_MAX_WORD];
Alan Mishchenko committed
495 496 497
        Dau_DsdTruth6Compose_rec( Abc_Tt6Cofactor0(Func, nVars), pFanins, pTtTemp[0], nVars, nWordsR );
        Dau_DsdTruth6Compose_rec( Abc_Tt6Cofactor1(Func, nVars), pFanins, pTtTemp[1], nVars, nWordsR );
        Abc_TtMux( pRes, pFanins[nVars], pTtTemp[1], pTtTemp[0], nWordsR );
Alan Mishchenko committed
498 499 500
        return;
    }
}
Alan Mishchenko committed
501
void Dau_DsdTruthCompose_rec( word * pFunc, word pFanins[DAU_MAX_VAR][DAU_MAX_WORD], word * pRes, int nVars, int nWordsR )
Alan Mishchenko committed
502
{
Alan Mishchenko committed
503
    int nWordsF;
Alan Mishchenko committed
504 505
    if ( nVars <= 6 )
    {
Alan Mishchenko committed
506
        Dau_DsdTruth6Compose_rec( pFunc[0], pFanins, pRes, nVars, nWordsR );
Alan Mishchenko committed
507 508
        return;
    }
Alan Mishchenko committed
509 510 511
    nWordsF = Abc_TtWordNum( nVars );
    assert( nWordsF > 1 );
    if ( Abc_TtIsConst0(pFunc, nWordsF) )
Alan Mishchenko committed
512
    {
Alan Mishchenko committed
513
        Abc_TtConst0( pRes, nWordsR );
Alan Mishchenko committed
514 515
        return;
    }
Alan Mishchenko committed
516
    if ( Abc_TtIsConst1(pFunc, nWordsF) )
Alan Mishchenko committed
517
    {
Alan Mishchenko committed
518
        Abc_TtConst1( pRes, nWordsR );
Alan Mishchenko committed
519 520 521 522
        return;
    }
    if ( !Abc_TtHasVar( pFunc, nVars, nVars-1 ) )
    {
Alan Mishchenko committed
523
        Dau_DsdTruthCompose_rec( pFunc, pFanins, pRes, nVars-1, nWordsR );
Alan Mishchenko committed
524 525 526 527 528
        return;
    }
    {
        word pTtTemp[2][DAU_MAX_WORD];
        nVars--;
Alan Mishchenko committed
529 530 531
        Dau_DsdTruthCompose_rec( pFunc,             pFanins, pTtTemp[0], nVars, nWordsR );
        Dau_DsdTruthCompose_rec( pFunc + nWordsF/2, pFanins, pTtTemp[1], nVars, nWordsR );
        Abc_TtMux( pRes, pFanins[nVars], pTtTemp[1], pTtTemp[0], nWordsR );
Alan Mishchenko committed
532 533 534 535 536 537 538 539 540
        return;
    }
}
void Dau_DsdToTruth_rec( char * pStr, char ** p, int * pMatches, word ** pTtElems, word * pRes, int nVars )
{
    int nWords = Abc_TtWordNum( nVars );
    int fCompl = 0;
    if ( **p == '!' )
        (*p)++, fCompl = 1;
Alan Mishchenko committed
541
    if ( **p >= 'a' && **p <= 'z' ) // var
Alan Mishchenko committed
542
    {
Alan Mishchenko committed
543
        assert( **p - 'a' >= 0 && **p - 'a' < nVars );
Alan Mishchenko committed
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
        Abc_TtCopy( pRes, pTtElems[**p - 'a'], nWords, fCompl );
        return;
    }
    if ( **p == '(' ) // and/or
    {
        char * q = pStr + pMatches[ *p - pStr ];
        word pTtTemp[DAU_MAX_WORD];
        assert( **p == '(' && *q == ')' );
        Abc_TtConst1( pRes, nWords );
        for ( (*p)++; *p < q; (*p)++ )
        {
            Dau_DsdToTruth_rec( pStr, p, pMatches, pTtElems, pTtTemp, nVars );
            Abc_TtAnd( pRes, pRes, pTtTemp, nWords, 0 );
        }
        assert( *p == q );
        if ( fCompl ) Abc_TtNot( pRes, nWords );
        return;
    }
    if ( **p == '[' ) // xor
    {
        char * q = pStr + pMatches[ *p - pStr ];
        word pTtTemp[DAU_MAX_WORD];
        assert( **p == '[' && *q == ']' );
        Abc_TtConst0( pRes, nWords );
        for ( (*p)++; *p < q; (*p)++ )
        {
            Dau_DsdToTruth_rec( pStr, p, pMatches, pTtElems, pTtTemp, nVars );
            Abc_TtXor( pRes, pRes, pTtTemp, nWords, 0 );
        }
        assert( *p == q );
        if ( fCompl ) Abc_TtNot( pRes, nWords );
        return;
    }
    if ( **p == '<' ) // mux
    {
        char * q = pStr + pMatches[ *p - pStr ];
        word pTtTemp[3][DAU_MAX_WORD];
        int i;
        assert( **p == '<' && *q == '>' );
        for ( i = 0, (*p)++; *p < q; (*p)++, i++ )
            Dau_DsdToTruth_rec( pStr, p, pMatches, pTtElems, pTtTemp[i], nVars );
        assert( i == 3 );
        Abc_TtMux( pRes, pTtTemp[0], pTtTemp[1], pTtTemp[2], nWords );
        assert( *p == q );
        if ( fCompl ) Abc_TtNot( pRes, nWords );
        return;
    }
    if ( (**p >= 'A' && **p <= 'F') || (**p >= '0' && **p <= '9') )
    {
        word pFanins[DAU_MAX_VAR][DAU_MAX_WORD], pFunc[DAU_MAX_WORD];
        char * q;
Alan Mishchenko committed
595 596
        int i, nVarsF = Abc_TtReadHex( pFunc, *p );
        *p += Abc_TtHexDigitNum( nVarsF );
Alan Mishchenko committed
597 598 599 600
        q = pStr + pMatches[ *p - pStr ];
        assert( **p == '{' && *q == '}' );
        for ( i = 0, (*p)++; *p < q; (*p)++, i++ )
            Dau_DsdToTruth_rec( pStr, p, pMatches, pTtElems, pFanins[i], nVars );
Alan Mishchenko committed
601
        assert( i == nVarsF );
Alan Mishchenko committed
602
        assert( *p == q );
Alan Mishchenko committed
603
        Dau_DsdTruthCompose_rec( pFunc, pFanins, pRes, nVarsF, nWords );
Alan Mishchenko committed
604 605 606 607 608 609 610
        if ( fCompl ) Abc_TtNot( pRes, nWords );
        return;
    }
    assert( 0 );
}
word * Dau_DsdToTruth( char * p, int nVars )
{
Alan Mishchenko committed
611 612 613 614 615
    int nWords = Abc_TtWordNum( nVars );
    word ** pTtElems = Dau_DsdTtElems();
    word * pRes = pTtElems[DAU_MAX_VAR];
    assert( nVars <= DAU_MAX_VAR );
    if ( Dau_DsdIsConst0(p) )
Alan Mishchenko committed
616
        Abc_TtConst0( pRes, nWords );
Alan Mishchenko committed
617
    else if ( Dau_DsdIsConst1(p) )
Alan Mishchenko committed
618 619 620 621 622 623 624 625 626 627
        Abc_TtConst1( pRes, nWords );
    else
        Dau_DsdToTruth_rec( p, &p, Dau_DsdComputeMatches(p), pTtElems, pRes, nVars );
    assert( *++p == 0 );
    return pRes;
}


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

628 629 630 631 632 633 634 635 636 637 638 639
  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Dau_DsdTest2()
{
//    char * p = Abc_UtilStrsav( "!(ab!(de[cf]))" );
640
//    char * p = Abc_UtilStrsav( "!(a![d<ecf>]b)" );
Alan Mishchenko committed
641
//    word t = Dau_Dsd6ToTruth( p );
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
}



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

  Synopsis    [Performs DSD.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Dau_DsdPerformReplace( char * pBuffer, int PosStart, int Pos, int Symb, char * pNext )
{
Alan Mishchenko committed
659
    static char pTemp[DAU_MAX_STR];
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
    char * pCur = pTemp;
    int i, k, RetValue;
    for ( i = PosStart; i < Pos; i++ )
        if ( pBuffer[i] != Symb )
            *pCur++ = pBuffer[i];
        else
            for ( k = 0; pNext[k]; k++ )
                *pCur++ = pNext[k];
    RetValue = PosStart + (pCur - pTemp);
    for ( i = PosStart; i < RetValue; i++ )
        pBuffer[i] = pTemp[i-PosStart];
    return RetValue;
}
int Dau_DsdPerform_rec( word t, char * pBuffer, int Pos, int * pVars, int nVars )
{
    char pNest[10];
    word Cof0[6], Cof1[6], Cof[4];
    int pVarsNew[6], nVarsNew, PosStart;
    int v, u, vBest, CountBest;
679
    assert( Pos < DAU_MAX_STR );
680
    // perform support minimization
681 682
    nVarsNew = 0;
    for ( v = 0; v < nVars; v++ )
683
        if ( Abc_Tt6HasVar( t, pVars[v] ) )
684 685
            pVarsNew[ nVarsNew++ ] = pVars[v];
    assert( nVarsNew > 0 );
686
    // special case when function is a var
687 688
    if ( nVarsNew == 1 )
    {
689
        if ( t == s_Truths6[ pVarsNew[0] ] )
690 691 692 693
        {
            pBuffer[Pos++] = 'a' +  pVarsNew[0];
            return Pos;
        }
694
        if ( t == ~s_Truths6[ pVarsNew[0] ] )
695 696 697 698 699 700 701 702
        {
            pBuffer[Pos++] = '!';
            pBuffer[Pos++] = 'a' +  pVarsNew[0];
            return Pos;
        }
        assert( 0 );
        return Pos;
    }
703
    // decompose on the output side
704 705
    for ( v = 0; v < nVarsNew; v++ )
    {
706 707
        Cof0[v] = Abc_Tt6Cofactor0( t, pVarsNew[v] );
        Cof1[v] = Abc_Tt6Cofactor1( t, pVarsNew[v] );
708
        assert( Cof0[v] != Cof1[v] );
709
        if ( Cof0[v] == 0 ) // ax
710 711 712 713 714 715 716
        {
            pBuffer[Pos++] = '(';
            pBuffer[Pos++] = 'a' + pVarsNew[v];
            Pos = Dau_DsdPerform_rec( Cof1[v], pBuffer, Pos, pVarsNew, nVarsNew );
            pBuffer[Pos++] = ')';
            return Pos;
        }
717
        if ( Cof0[v] == ~(word)0 ) // !(ax)
718 719 720 721 722 723 724 725
        {
            pBuffer[Pos++] = '!';
            pBuffer[Pos++] = '(';
            pBuffer[Pos++] = 'a' + pVarsNew[v];
            Pos = Dau_DsdPerform_rec( ~Cof1[v], pBuffer, Pos, pVarsNew, nVarsNew );
            pBuffer[Pos++] = ')';
            return Pos;
        }
726
        if ( Cof1[v] == 0 ) // !ax
727 728 729 730 731 732 733 734
        {
            pBuffer[Pos++] = '(';
            pBuffer[Pos++] = '!';
            pBuffer[Pos++] = 'a' + pVarsNew[v];
            Pos = Dau_DsdPerform_rec( Cof0[v], pBuffer, Pos, pVarsNew, nVarsNew );
            pBuffer[Pos++] = ')';
            return Pos;
        }
735
        if ( Cof1[v] == ~(word)0 ) // !(!ax)
736 737 738 739 740 741 742 743 744
        {
            pBuffer[Pos++] = '!';
            pBuffer[Pos++] = '(';
            pBuffer[Pos++] = '!';
            pBuffer[Pos++] = 'a' + pVarsNew[v];
            Pos = Dau_DsdPerform_rec( ~Cof0[v], pBuffer, Pos, pVarsNew, nVarsNew );
            pBuffer[Pos++] = ')';
            return Pos;
        }
745
        if ( Cof0[v] == ~Cof1[v] ) // a^x
746 747 748 749 750 751 752 753
        {
            pBuffer[Pos++] = '[';
            pBuffer[Pos++] = 'a' + pVarsNew[v];
            Pos = Dau_DsdPerform_rec( Cof0[v], pBuffer, Pos, pVarsNew, nVarsNew );
            pBuffer[Pos++] = ']';
            return Pos;
        }
    }
754
    // decompose on the input side
755 756 757
    for ( v = 0; v < nVarsNew; v++ )
    for ( u = v+1; u < nVarsNew; u++ )
    {
758 759 760 761
        Cof[0] = Abc_Tt6Cofactor0( Cof0[v], pVarsNew[u] );
        Cof[1] = Abc_Tt6Cofactor1( Cof0[v], pVarsNew[u] );
        Cof[2] = Abc_Tt6Cofactor0( Cof1[v], pVarsNew[u] );
        Cof[3] = Abc_Tt6Cofactor1( Cof1[v], pVarsNew[u] );
762 763 764 765
        if ( Cof[0] == Cof[1] && Cof[0] == Cof[2] ) // vu
        {
            PosStart = Pos;
            sprintf( pNest, "(%c%c)", 'a' + pVarsNew[v], 'a' + pVarsNew[u] );
766
            Pos = Dau_DsdPerform_rec( (s_Truths6[pVarsNew[u]] & Cof[3]) | (~s_Truths6[pVarsNew[u]] & Cof[0]), pBuffer, Pos, pVarsNew, nVarsNew );
767 768 769 770 771 772 773
            Pos = Dau_DsdPerformReplace( pBuffer, PosStart, Pos, 'a' + pVarsNew[u], pNest );
            return Pos;
        }
        if ( Cof[0] == Cof[1] && Cof[0] == Cof[3] ) // v!u
        {
            PosStart = Pos;
            sprintf( pNest, "(%c!%c)", 'a' + pVarsNew[v], 'a' + pVarsNew[u] );
774
            Pos = Dau_DsdPerform_rec( (s_Truths6[pVarsNew[u]] & Cof[2]) | (~s_Truths6[pVarsNew[u]] & Cof[0]), pBuffer, Pos, pVarsNew, nVarsNew );
775 776 777 778 779 780 781
            Pos = Dau_DsdPerformReplace( pBuffer, PosStart, Pos, 'a' + pVarsNew[u], pNest );
            return Pos;
        }
        if ( Cof[0] == Cof[2] && Cof[0] == Cof[3] ) // !vu
        {
            PosStart = Pos;
            sprintf( pNest, "(!%c%c)", 'a' + pVarsNew[v], 'a' + pVarsNew[u] );
782
            Pos = Dau_DsdPerform_rec( (s_Truths6[pVarsNew[u]] & Cof[1]) | (~s_Truths6[pVarsNew[u]] & Cof[0]), pBuffer, Pos, pVarsNew, nVarsNew );
783 784 785 786 787 788 789
            Pos = Dau_DsdPerformReplace( pBuffer, PosStart, Pos, 'a' + pVarsNew[u], pNest );
            return Pos;
        }
        if ( Cof[1] == Cof[2] && Cof[1] == Cof[3] ) // !v!u
        {
            PosStart = Pos;
            sprintf( pNest, "(!%c!%c)", 'a' + pVarsNew[v], 'a' + pVarsNew[u] );
790
            Pos = Dau_DsdPerform_rec( (s_Truths6[pVarsNew[u]] & Cof[0]) | (~s_Truths6[pVarsNew[u]] & Cof[1]), pBuffer, Pos, pVarsNew, nVarsNew );
791 792 793 794 795 796 797
            Pos = Dau_DsdPerformReplace( pBuffer, PosStart, Pos, 'a' + pVarsNew[u], pNest );
            return Pos;
        }
        if ( Cof[0] == Cof[3] && Cof[1] == Cof[2] ) // v+u
        {
            PosStart = Pos;
            sprintf( pNest, "[%c%c]", 'a' + pVarsNew[v], 'a' + pVarsNew[u] );
798
            Pos = Dau_DsdPerform_rec( (s_Truths6[pVarsNew[u]] & Cof[1]) | (~s_Truths6[pVarsNew[u]] & Cof[0]), pBuffer, Pos, pVarsNew, nVarsNew );
799 800 801
            Pos = Dau_DsdPerformReplace( pBuffer, PosStart, Pos, 'a' + pVarsNew[u], pNest );
            return Pos;
        }
Alan Mishchenko committed
802
    } 
803
    // find best variable for MUX decomposition
804 805 806 807 808 809
    vBest = -1;
    CountBest = 10;
    for ( v = 0; v < nVarsNew; v++ )
    {
        int CountCur = 0;
        for ( u = 0; u < nVarsNew; u++ )
810
            if ( u != v && Abc_Tt6HasVar(Cof0[v], pVarsNew[u]) && Abc_Tt6HasVar(Cof1[v], pVarsNew[u]) )
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
                CountCur++;
        if ( CountBest > CountCur )
        {
            CountBest = CountCur;
            vBest = v;
        }
        if ( CountCur == 0 )
            break;
    }
    // perform MUX decomposition
    pBuffer[Pos++] = '<';
    pBuffer[Pos++] = 'a' + pVarsNew[vBest];
    Pos = Dau_DsdPerform_rec( Cof1[vBest], pBuffer, Pos, pVarsNew, nVarsNew );
    Pos = Dau_DsdPerform_rec( Cof0[vBest], pBuffer, Pos, pVarsNew, nVarsNew );
    pBuffer[Pos++] = '>';
    return Pos;
}
char * Dau_DsdPerform( word t )
{
Alan Mishchenko committed
830
    static char pBuffer[DAU_MAX_STR];
831 832 833 834
    int pVarsNew[6] = {0, 1, 2, 3, 4, 5};
    int Pos = 0;
    if ( t == 0 )
        pBuffer[Pos++] = '0';
835
    else if ( t == ~(word)0 )
836 837 838 839
        pBuffer[Pos++] = '1';
    else
        Pos = Dau_DsdPerform_rec( t, pBuffer, Pos, pVarsNew, 6 );
    pBuffer[Pos++] = 0;
840 841
//    printf( "%d ", strlen(pBuffer) );
//    printf( "%s ->", pBuffer );
Alan Mishchenko committed
842
    Dau_DsdRemoveBraces( pBuffer, Dau_DsdComputeMatches(pBuffer) );
843
//    printf( " %s\n", pBuffer );
844 845 846 847 848 849 850 851 852 853 854 855 856 857
    return pBuffer;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
858
void Dau_DsdTest3()
859
{
860 861 862 863 864 865 866
//    word t = s_Truths6[0] & s_Truths6[1] & s_Truths6[2];
//    word t = ~s_Truths6[0] | (s_Truths6[1] ^ ~s_Truths6[2]);
//    word t = (s_Truths6[1] & s_Truths6[2]) | (s_Truths6[0] & s_Truths6[3]);
//    word t = (~s_Truths6[1] & ~s_Truths6[2]) | (s_Truths6[0] ^ s_Truths6[3]);
//    word t = ((~s_Truths6[1] & ~s_Truths6[2]) | (s_Truths6[0] ^ s_Truths6[3])) ^ s_Truths6[5];
//    word t = ((s_Truths6[1] & ~s_Truths6[2]) ^ (s_Truths6[0] & s_Truths6[3])) & s_Truths6[5];
//    word t = (~(~s_Truths6[0] & ~s_Truths6[4]) & s_Truths6[2]) | (~s_Truths6[1] & ~s_Truths6[0] & ~s_Truths6[4]);
867 868
//    word t = 0x0000000000005F3F;
//    word t = 0xF3F5030503050305;
869
//    word t = (s_Truths6[0] & s_Truths6[1] & (s_Truths6[2] ^ s_Truths6[4])) | (~s_Truths6[0] & ~s_Truths6[1] & ~(s_Truths6[2] ^ s_Truths6[4]));
870
//    word t = 0x05050500f5f5f5f3;
871
    word t = ABC_CONST(0x9ef7a8d9c7193a0f);
872
    char * p = Dau_DsdPerform( t );
Alan Mishchenko committed
873
    word t2 = Dau_Dsd6ToTruth( p );
874 875 876 877
    if ( t != t2 )
        printf( "Verification failed.\n" );
}

878

879 880


Alan Mishchenko committed
881 882 883 884 885 886 887 888 889 890 891 892
/**Function*************************************************************

  Synopsis    [Find the best cofactoring variable.]

  Description [Return -2 if non-DSD; -1 if full DSD; otherwise,
  returns cofactoring variables i (i >= 0).]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
893
int Dau_DsdCheck1Step( void * p, word * pTruth, int nVarsInit, int * pVarLevels )
Alan Mishchenko committed
894 895
{
    word pCofTemp[DAU_MAX_WORD];
896
    int pVarPrios[DAU_MAX_VAR];
Alan Mishchenko committed
897 898
    int nWords = Abc_TtWordNum(nVarsInit);
    int nSizeNonDec, nSizeNonDec0, nSizeNonDec1;
899
    int i, vBest = -2, nSumCofsBest = ABC_INFINITY, nSumCofs;
Alan Mishchenko committed
900
    nSizeNonDec = Dau_DsdDecompose( pTruth, nVarsInit, 0, 0, NULL );
Alan Mishchenko committed
901 902 903
    if ( nSizeNonDec == 0 )
        return -1;
    assert( nSizeNonDec > 0 );
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
    // find variable priority
    for ( i = 0; i < nVarsInit; i++ )
        pVarPrios[i] = i;
    if ( pVarLevels )
    {
        extern int Dau_DsdLevelVar( void * pMan, int iVar );
        int pVarLevels[DAU_MAX_VAR];
        for ( i = 0; i < nVarsInit; i++ )
            pVarLevels[i] = -Dau_DsdLevelVar( p, i );
//        for ( i = 0; i < nVarsInit; i++ )
//            printf( "%d ", -pVarLevels[i] );
//        printf( "\n" );
        Vec_IntSelectSortCost2( pVarPrios, nVarsInit, pVarLevels );
//        for ( i = 0; i < nVarsInit; i++ )
//            printf( "%d ", pVarPrios[i] );
//        printf( "\n\n" );
    }
    for ( i = 0; i < nVarsInit; i++ )
Alan Mishchenko committed
922
    {
923
        assert( pVarPrios[i] >= 0 && pVarPrios[i] < nVarsInit );
Alan Mishchenko committed
924
        // try first cofactor
925
        Abc_TtCofactor0p( pCofTemp, pTruth, nWords, pVarPrios[i] );
Alan Mishchenko committed
926
        nSumCofs = Abc_TtSupportSize( pCofTemp, nVarsInit );
Alan Mishchenko committed
927
        nSizeNonDec0 = Dau_DsdDecompose( pCofTemp, nVarsInit, 0, 0, NULL );
Alan Mishchenko committed
928
        // try second cofactor
929
        Abc_TtCofactor1p( pCofTemp, pTruth, nWords, pVarPrios[i] );
Alan Mishchenko committed
930
        nSumCofs += Abc_TtSupportSize( pCofTemp, nVarsInit );
Alan Mishchenko committed
931
        nSizeNonDec1 = Dau_DsdDecompose( pCofTemp, nVarsInit, 0, 0, NULL );
Alan Mishchenko committed
932 933 934 935 936
        // compare cofactors
        if ( nSizeNonDec0 || nSizeNonDec1 )
            continue;
        if ( nSumCofsBest > nSumCofs )
        {
937
            vBest = pVarPrios[i];
Alan Mishchenko committed
938 939 940 941 942
            nSumCofsBest = nSumCofs;
        }
    }
    return vBest;
}
943 944 945

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

Alan Mishchenko committed
946 947 948 949 950 951 952 953 954
  Synopsis    [Data-structure to store DSD information.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
955 956 957 958 959 960
typedef struct Dau_Dsd_t_ Dau_Dsd_t;
struct Dau_Dsd_t_
{
    int      nVarsInit;            // the initial number of variables
    int      nVarsUsed;            // the current number of variables
    int      nPos;                 // writing position
Alan Mishchenko committed
961
    int      nSizeNonDec;          // size of the largest non-decomposable block
Alan Mishchenko committed
962 963
    int      nConsts;              // the number of constant decompositions
    int      uConstMask;           // constant decomposition mask
Alan Mishchenko committed
964
    int      fSplitPrime;          // represent prime function as 1-step DSD
Alan Mishchenko committed
965
    int      fWriteTruth;          // writing truth table as a hex string
966
    int *    pVarLevels;           // variable levels
Alan Mishchenko committed
967 968 969
    char     pVarDefs[32][8];      // variable definitions
    char     Cache[32][32];        // variable cache
    char     pOutput[DAU_MAX_STR]; // output stream
970 971
};

972
static abctime s_Times[3] = {0};
Alan Mishchenko committed
973

974 975
/**Function*************************************************************

Alan Mishchenko committed
976
  Synopsis    [Manipulation of DSD data-structure.]
977 978 979 980 981 982 983 984

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
985
static inline void Dau_DsdInitialize( Dau_Dsd_t * p, int nVarsInit )
986
{
Alan Mishchenko committed
987 988 989 990 991 992 993 994
    int i, v, u;
    assert( nVarsInit >= 0 && nVarsInit <= 16 );
    p->nVarsInit   = nVarsInit;
    p->nVarsUsed   = nVarsInit;
    p->nPos        = 0;
    p->nSizeNonDec = 0;
    p->nConsts     = 0;
    p->uConstMask  = 0;
995
    for ( i = 0; i < nVarsInit; i++ )
Alan Mishchenko committed
996 997 998 999 1000
        p->pVarDefs[i][0] = 'a' + i, p->pVarDefs[i][1] = 0;
    for ( v = 0; v < nVarsInit; v++ )
    for ( u = 0; u < nVarsInit; u++ )
        p->Cache[v][u] = 0;

1001
}
Alan Mishchenko committed
1002
static inline void Dau_DsdWriteString( Dau_Dsd_t * p, char * pStr )
1003 1004 1005 1006
{
    while ( *pStr )
        p->pOutput[ p->nPos++ ] = *pStr++;
}
Alan Mishchenko committed
1007
static inline void Dau_DsdWriteVar( Dau_Dsd_t * p, int iVar, int fInv )
1008 1009
{
    char * pStr;
Alan Mishchenko committed
1010 1011
    if ( fInv )
        p->pOutput[ p->nPos++ ] = '!';
1012 1013
    for ( pStr = p->pVarDefs[iVar]; *pStr; pStr++ )
        if ( *pStr >= 'a' + p->nVarsInit && *pStr < 'a' + p->nVarsUsed )
Alan Mishchenko committed
1014 1015 1016 1017
            Dau_DsdWriteVar( p, *pStr - 'a', 0 );
        else
            p->pOutput[ p->nPos++ ] = *pStr;
}
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
int Dau_DsdLevelVar( void * pMan, int iVar )
{
    Dau_Dsd_t * p = (Dau_Dsd_t *)pMan;
    char * pStr;
    int LevelMax = 0, Level;
    for ( pStr = p->pVarDefs[iVar]; *pStr; pStr++ )
    {
        if ( *pStr >= 'a' + p->nVarsInit && *pStr < 'a' + p->nVarsUsed )
            Level = 1 + Dau_DsdLevelVar( p, *pStr - 'a' );
        else
            Level = p->pVarLevels[*pStr - 'a'];
        LevelMax = Abc_MaxInt( LevelMax, Level );
    }
    return LevelMax;
}
Alan Mishchenko committed
1033 1034 1035
static inline void Dau_DsdTranslate( Dau_Dsd_t * p, int * pVars, int nVars, char * pStr )
{
    for ( ; *pStr; pStr++ )
Alan Mishchenko committed
1036
        if ( *pStr >= 'a' && *pStr < 'a' + nVars )
Alan Mishchenko committed
1037
            Dau_DsdWriteVar( p, pVars[*pStr - 'a'], 0 );
1038 1039 1040
        else
            p->pOutput[ p->nPos++ ] = *pStr;
}
Alan Mishchenko committed
1041
static inline int Dau_DsdWritePrime( Dau_Dsd_t * p, word * pTruth, int * pVars, int nVars )
Alan Mishchenko committed
1042
{
Alan Mishchenko committed
1043
    int v, RetValue = 2;
Alan Mishchenko committed
1044
    assert( nVars > 2 );
Alan Mishchenko committed
1045 1046 1047 1048
    if ( p->fSplitPrime )
    {
        word pCofTemp[DAU_MAX_WORD];
        int nWords = Abc_TtWordNum(nVars);
1049
        int vBest = Dau_DsdCheck1Step( p, pTruth, nVars, p->pVarLevels );
Alan Mishchenko committed
1050 1051 1052 1053 1054 1055 1056 1057 1058
        assert( vBest != -1 );
        if ( vBest == -2 ) // non-dec
            p->nPos += Abc_TtWriteHexRev( p->pOutput + p->nPos, pTruth, nVars );
        else 
        {
            char pRes[DAU_MAX_STR];
            int nNonDecSize;
            // compose the result
            Dau_DsdWriteString( p, "<" );
1059
            Dau_DsdWriteVar( p, vBest, 0 );
Alan Mishchenko committed
1060
            // split decomposition
Alan Mishchenko committed
1061
            Abc_TtCofactor1p( pCofTemp, pTruth, nWords, vBest );
Alan Mishchenko committed
1062
            nNonDecSize = Dau_DsdDecompose( pCofTemp, nVars, 0, p->fWriteTruth, pRes );
Alan Mishchenko committed
1063 1064 1065
            assert( nNonDecSize == 0 );
            Dau_DsdWriteString( p, pRes );
            // split decomposition
Alan Mishchenko committed
1066
            Abc_TtCofactor0p( pCofTemp, pTruth, nWords, vBest );
Alan Mishchenko committed
1067
            nNonDecSize = Dau_DsdDecompose( pCofTemp, nVars, 0, p->fWriteTruth, pRes );
Alan Mishchenko committed
1068 1069 1070 1071 1072 1073
            assert( nNonDecSize == 0 );
            Dau_DsdWriteString( p, pRes );
            Dau_DsdWriteString( p, ">" );
            RetValue = 1;
        }
    }
Alan Mishchenko committed
1074
    else if ( p->fWriteTruth )
Alan Mishchenko committed
1075
        p->nPos += Abc_TtWriteHexRev( p->pOutput + p->nPos, pTruth, nVars );
Alan Mishchenko committed
1076 1077 1078 1079 1080
    Dau_DsdWriteString( p, "{" );
    for ( v = 0; v < nVars; v++ )
        Dau_DsdWriteVar( p, pVars[v], 0 );
    Dau_DsdWriteString( p, "}" );
    p->nSizeNonDec = nVars;
Alan Mishchenko committed
1081
    return RetValue;
Alan Mishchenko committed
1082 1083
}
static inline void Dau_DsdFinalize( Dau_Dsd_t * p )
1084
{
Alan Mishchenko committed
1085 1086
    int i;
    for ( i = 0; i < p->nConsts; i++ )
Alan Mishchenko committed
1087
        p->pOutput[ p->nPos++ ] = ((p->uConstMask >> (p->nConsts-1-i)) & 1) ? ']' : ')';
1088 1089
    p->pOutput[ p->nPos++ ] = 0;
}
Alan Mishchenko committed
1090
static inline int Dau_DsdAddVarDef( Dau_Dsd_t * p, char * pStr )
Alan Mishchenko committed
1091
{
Alan Mishchenko committed
1092
    int u;
Alan Mishchenko committed
1093 1094
    assert( strlen(pStr) < 8 );
    assert( p->nVarsUsed < 32 );
Alan Mishchenko committed
1095 1096 1097 1098
    for ( u = 0; u < p->nVarsUsed; u++ )
        p->Cache[p->nVarsUsed][u] = 0;
    for ( u = 0; u < p->nVarsUsed; u++ )
        p->Cache[u][p->nVarsUsed] = 0;
Alan Mishchenko committed
1099 1100 1101
    sprintf( p->pVarDefs[p->nVarsUsed++], "%s", pStr );
    return p->nVarsUsed - 1;
}
Alan Mishchenko committed
1102 1103 1104 1105 1106 1107 1108 1109 1110
static inline int Dau_DsdFindVarDef( int * pVars, int nVars, int VarDef )
{
    int v;
    for ( v = 0; v < nVars; v++ )
        if ( pVars[v] == VarDef )
            break;
    assert( v < nVars );
    return v;
}
Alan Mishchenko committed
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
static inline void Dau_DsdInsertVarCache( Dau_Dsd_t * p, int v, int u, int Status )
{
    assert( v != u );
    assert( Status > 0 && Status < 4 );
    assert( p->Cache[v][u] == 0 );
    p->Cache[v][u] = Status;
}
static inline int Dau_DsdLookupVarCache( Dau_Dsd_t * p, int v, int u )
{
    return p->Cache[v][u];
}
1122 1123 1124

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

Alan Mishchenko committed
1125
  Synopsis    [Procedures specialized for 6-variable functions.]
1126 1127 1128 1129 1130 1131 1132 1133

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1134
static inline int Dau_Dsd6DecomposeSingleVarOne( Dau_Dsd_t * p, word * pTruth, int * pVars, int nVars, int v )
Alan Mishchenko committed
1135 1136 1137 1138 1139 1140
{
    // consider negative cofactors
    if ( pTruth[0] & 1 )
    {        
        if ( Abc_Tt6Cof0IsConst1( pTruth[0], v ) ) // !(ax)
        {
Alan Mishchenko committed
1141
            Dau_DsdWriteString( p, "!(" );
Alan Mishchenko committed
1142
            pTruth[0] = ~Abc_Tt6Cofactor1( pTruth[0], v );
Alan Mishchenko committed
1143
            goto finish;            
Alan Mishchenko committed
1144 1145 1146 1147 1148 1149
        }
    }
    else
    {
        if ( Abc_Tt6Cof0IsConst0( pTruth[0], v ) ) // ax
        {
Alan Mishchenko committed
1150
            Dau_DsdWriteString( p, "(" );
Alan Mishchenko committed
1151
            pTruth[0] = Abc_Tt6Cofactor1( pTruth[0], v );
Alan Mishchenko committed
1152
            goto finish;            
Alan Mishchenko committed
1153 1154 1155 1156 1157 1158 1159
        }
    }
    // consider positive cofactors
    if ( pTruth[0] >> 63 )
    {        
        if ( Abc_Tt6Cof1IsConst1( pTruth[0], v ) ) // !(!ax)
        {
Alan Mishchenko committed
1160
            Dau_DsdWriteString( p, "!(!" );
Alan Mishchenko committed
1161
            pTruth[0] = ~Abc_Tt6Cofactor0( pTruth[0], v );
Alan Mishchenko committed
1162
            goto finish;            
Alan Mishchenko committed
1163 1164 1165 1166 1167 1168
        }
    }
    else
    {
        if ( Abc_Tt6Cof1IsConst0( pTruth[0], v ) ) // !ax
        {
Alan Mishchenko committed
1169
            Dau_DsdWriteString( p, "(!" );
Alan Mishchenko committed
1170
            pTruth[0] = Abc_Tt6Cofactor0( pTruth[0], v );
Alan Mishchenko committed
1171
            goto finish;            
Alan Mishchenko committed
1172 1173 1174 1175 1176
        }
    }
    // consider equal cofactors
    if ( Abc_Tt6CofsOpposite( pTruth[0], v ) ) // [ax]
    {
Alan Mishchenko committed
1177
        Dau_DsdWriteString( p, "[" );
Alan Mishchenko committed
1178
        pTruth[0] = Abc_Tt6Cofactor0( pTruth[0], v );
Alan Mishchenko committed
1179 1180
        p->uConstMask |= (1 << p->nConsts);
        goto finish;            
Alan Mishchenko committed
1181
    }
Alan Mishchenko committed
1182 1183 1184 1185
    return 0;

finish:
    p->nConsts++;
Alan Mishchenko committed
1186
    Dau_DsdWriteVar( p, pVars[v], 0 );
Alan Mishchenko committed
1187
    pVars[v] = pVars[nVars-1];
Alan Mishchenko committed
1188
    Abc_TtSwapVars( pTruth, nVars, v, nVars-1 );
Alan Mishchenko committed
1189 1190
    return 1;
}
Alan Mishchenko committed
1191
int Dau_Dsd6DecomposeSingleVar( Dau_Dsd_t * p, word * pTruth, int * pVars, int nVars )
Alan Mishchenko committed
1192
{
1193
    abctime clk = Abc_Clock();
Alan Mishchenko committed
1194
    assert( nVars > 1 );
Alan Mishchenko committed
1195 1196
    while ( 1 )
    {
Alan Mishchenko committed
1197
        int v;
Alan Mishchenko committed
1198
        for ( v = nVars - 1; v >= 0 && nVars > 1; v-- )
Alan Mishchenko committed
1199 1200
            if ( Dau_Dsd6DecomposeSingleVarOne( p, pTruth, pVars, nVars, v ) )
            {
Alan Mishchenko committed
1201
                nVars--;
Alan Mishchenko committed
1202 1203 1204
                break;
            }
        if ( v == -1 || nVars == 1 )
Alan Mishchenko committed
1205 1206
            break;
    }
Alan Mishchenko committed
1207
    if ( nVars == 1 )
Alan Mishchenko committed
1208
        Dau_DsdWriteVar( p, pVars[--nVars], (int)(pTruth[0] & 1) );
1209
    s_Times[0] += Abc_Clock() - clk;
Alan Mishchenko committed
1210 1211
    return nVars;
}
Alan Mishchenko committed
1212
static inline int Dau_Dsd6FindSupportOne( Dau_Dsd_t * p, word tCof0, word tCof1, int * pVars, int nVars, int v, int u )
Alan Mishchenko committed
1213
{
Alan Mishchenko committed
1214
    int Status = p ? Dau_DsdLookupVarCache( p, pVars[v], pVars[u] ) : 0;
Alan Mishchenko committed
1215
    if ( Status == 0 )
Alan Mishchenko committed
1216
    {
Alan Mishchenko committed
1217
        Status = (Abc_Tt6HasVar(tCof1, u) << 1) | Abc_Tt6HasVar(tCof0, u);
Alan Mishchenko committed
1218 1219
        if ( p )
            Dau_DsdInsertVarCache( p, pVars[v], pVars[u], Status );
Alan Mishchenko committed
1220 1221 1222 1223 1224 1225 1226 1227 1228
    }
    return Status;
}
static inline unsigned Dau_Dsd6FindSupports( Dau_Dsd_t * p, word  * pTruth, int * pVars, int nVars, int v )
{
    int u;
    unsigned uSupports = 0;
    word tCof0 = Abc_Tt6Cofactor0( pTruth[0], v );
    word tCof1 = Abc_Tt6Cofactor1( pTruth[0], v );
Alan Mishchenko committed
1229 1230
//Kit_DsdPrintFromTruth( (unsigned *)&tCof0, 6 );printf( "\n" );
//Kit_DsdPrintFromTruth( (unsigned *)&tCof1, 6 );printf( "\n" );
Alan Mishchenko committed
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
    for ( u = 0; u < nVars; u++ )
        if ( u != v )
            uSupports |= (Dau_Dsd6FindSupportOne( p, tCof0, tCof1, pVars, nVars, v, u ) << (2 * u));
    return uSupports;
}
static inline void Dau_DsdPrintSupports( unsigned uSupp, int nVars )
{
    int v, Value;
    printf( "Cofactor supports: " );
    for ( v = nVars - 1; v >= 0; v-- )
    {
        Value = ((uSupp >> (2*v)) & 3);
        if ( Value == 1 )
            printf( "01" );
        else if ( Value == 2 )
            printf( "10" );
        else if ( Value == 3 )
            printf( "11" );
        else 
            printf( "00" );
        if ( v )
            printf( "-" );
    }
    printf( "\n" );
}
// checks decomposability with respect to the pair (v, u)
static inline int Dau_Dsd6DecomposeDoubleVarsOne( Dau_Dsd_t * p, word  * pTruth, int * pVars, int nVars, int v, int u )
{
    char pBuffer[10] = { 0 }; 
    word tCof0 = Abc_Tt6Cofactor0( pTruth[0], v );
    word tCof1 = Abc_Tt6Cofactor1( pTruth[0], v );
    int Status = Dau_Dsd6FindSupportOne( p, tCof0, tCof1, pVars, nVars, v, u );
    assert( v > u );
Alan Mishchenko committed
1264 1265
//printf( "Checking %s and %s.\n", p->pVarDefs[pVars[v]], p->pVarDefs[pVars[u]] );

Alan Mishchenko committed
1266 1267 1268 1269 1270
//    Kit_DsdPrintFromTruth( (unsigned *)pTruth, 6 );printf( "\n" );
    if ( Status == 3 )
    {
        // both F(v=0) and F(v=1) depend on u
        if ( Abc_Tt6Cof0EqualCof1(tCof0, tCof1, u) && Abc_Tt6Cof0EqualCof1(tCof1, tCof0, u) ) // v+u
Alan Mishchenko committed
1271
        {
Alan Mishchenko committed
1272 1273 1274
            pTruth[0] = (s_Truths6[u] & Abc_Tt6Cofactor1(tCof0, u)) | (~s_Truths6[u] & Abc_Tt6Cofactor0(tCof0, u));
            sprintf( pBuffer, "[%c%c]", 'a' + pVars[v], 'a' + pVars[u] );
            goto finish;
Alan Mishchenko committed
1275
        }
Alan Mishchenko committed
1276 1277 1278 1279 1280
    }
    else if ( Status == 2 )
    {
        // F(v=0) does not depend on u; F(v=1) depends on u
        if ( Abc_Tt6Cof0EqualCof0(tCof0, tCof1, u) ) // vu
Alan Mishchenko committed
1281
        {
Alan Mishchenko committed
1282 1283 1284
            sprintf( pBuffer, "(%c%c)", 'a' + pVars[v], 'a' + pVars[u] );
            pTruth[0] = (s_Truths6[u] & Abc_Tt6Cofactor1(tCof1, u)) | (~s_Truths6[u] & Abc_Tt6Cofactor0(tCof0, u));
            goto finish;
Alan Mishchenko committed
1285
        }
Alan Mishchenko committed
1286
        if ( Abc_Tt6Cof0EqualCof1(tCof0, tCof1, u) ) // v!u
Alan Mishchenko committed
1287
        {
Alan Mishchenko committed
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
            sprintf( pBuffer, "(%c!%c)", 'a' + pVars[v], 'a' + pVars[u] );
            pTruth[0] = (s_Truths6[u] & Abc_Tt6Cofactor0(tCof1, u)) | (~s_Truths6[u] & Abc_Tt6Cofactor0(tCof0, u));
            goto finish;
        }
    }
    else if ( Status == 1 )
    {
        // F(v=0) depends on u; F(v=1) does not depend on u
        if ( Abc_Tt6Cof0EqualCof1(tCof0, tCof1, u) ) // !vu
        {
            sprintf( pBuffer, "(!%c%c)", 'a' + pVars[v], 'a' + pVars[u] );
            pTruth[0] = (s_Truths6[u] & Abc_Tt6Cofactor1(tCof0, u)) | (~s_Truths6[u] & Abc_Tt6Cofactor0(tCof0, u));
            goto finish;
        }
        if ( Abc_Tt6Cof1EqualCof1(tCof0, tCof1, u) ) // !v!u
        {
            sprintf( pBuffer, "(!%c!%c)", 'a' + pVars[v], 'a' + pVars[u] );
            pTruth[0] = (s_Truths6[u] & Abc_Tt6Cofactor0(tCof0, u)) | (~s_Truths6[u] & Abc_Tt6Cofactor1(tCof1, u));
            goto finish;
        }
    }
    return nVars;
finish: 
    // finalize decomposition
    assert( pBuffer[0] );
    pVars[u] = Dau_DsdAddVarDef( p, pBuffer );
    pVars[v] = pVars[nVars-1];
Alan Mishchenko committed
1315
    Abc_TtSwapVars( pTruth, nVars, v, nVars-1 );
Alan Mishchenko committed
1316 1317 1318 1319 1320 1321
    if ( Dau_Dsd6DecomposeSingleVarOne( p, pTruth, pVars, --nVars, u ) )
        nVars = Dau_Dsd6DecomposeSingleVar( p, pTruth, pVars, --nVars );
    return nVars;
}
int Dau_Dsd6DecomposeDoubleVars( Dau_Dsd_t * p, word  * pTruth, int * pVars, int nVars )
{
1322
    abctime clk = Abc_Clock();
Alan Mishchenko committed
1323 1324 1325 1326 1327 1328
    while ( 1 )
    {
        int v, u, nVarsOld;
        for ( v = nVars - 1; v > 0; v-- )
        {
            for ( u = v - 1; u >= 0; u-- )
Alan Mishchenko committed
1329
            {
Alan Mishchenko committed
1330
                if ( Dau_DsdLookupVarCache( p, pVars[v], pVars[u] ) )
Alan Mishchenko committed
1331 1332 1333
                    continue;
                nVarsOld = nVars;
                nVars = Dau_Dsd6DecomposeDoubleVarsOne( p, pTruth, pVars, nVars, v, u );
Alan Mishchenko committed
1334
                if ( nVars == 0 )
Alan Mishchenko committed
1335
                {
1336
                    s_Times[1] += Abc_Clock() - clk;
Alan Mishchenko committed
1337
                    return 0;
Alan Mishchenko committed
1338
                }
Alan Mishchenko committed
1339 1340
                if ( nVarsOld > nVars )
                    break;
Alan Mishchenko committed
1341
            }
Alan Mishchenko committed
1342 1343
            if ( u >= 0 ) // found
                break;
Alan Mishchenko committed
1344
        }
Alan Mishchenko committed
1345 1346 1347
        if ( v == 0 ) // not found
            break;
    }
1348
    s_Times[1] += Abc_Clock() - clk;
Alan Mishchenko committed
1349 1350
    return nVars;
}
Alan Mishchenko committed
1351

Alan Mishchenko committed
1352 1353 1354
// look for MUX-decomposable variable on top or at the bottom
static inline int Dau_Dsd6DecomposeTripleVarsOuter( Dau_Dsd_t * p, word  * pTruth, int * pVars, int nVars, int v )
{
Alan Mishchenko committed
1355
    extern int Dau_DsdDecomposeInt( Dau_Dsd_t * p, word * pTruth, int nVarsInit );
Alan Mishchenko committed
1356 1357
    Dau_Dsd_t P1, * p1 = &P1;
    word tCof0, tCof1;
Alan Mishchenko committed
1358
    p1->fSplitPrime = 0;
1359
    p1->fWriteTruth = p->fWriteTruth;
Alan Mishchenko committed
1360
    // move this variable to the top
Alan Mishchenko committed
1361
    ABC_SWAP( int, pVars[v], pVars[nVars-1] );
Alan Mishchenko committed
1362
    Abc_TtSwapVars( pTruth, nVars, v, nVars-1 );
Alan Mishchenko committed
1363 1364 1365 1366 1367 1368
    // cofactor w.r.t the last variable
    tCof0 = Abc_Tt6Cofactor0( pTruth[0], nVars - 1 );
    tCof1 = Abc_Tt6Cofactor1( pTruth[0], nVars - 1 );
    // compose the result
    Dau_DsdWriteString( p, "<" );
    Dau_DsdWriteVar( p, pVars[nVars - 1], 0 );
Alan Mishchenko committed
1369 1370 1371 1372
    // split decomposition
    Dau_DsdDecomposeInt( p1, &tCof1, nVars - 1 );
    Dau_DsdTranslate( p, pVars, nVars - 1, p1->pOutput );
    p->nSizeNonDec = p1->nSizeNonDec;
Alan Mishchenko committed
1373 1374
    if ( p1->nSizeNonDec )
        *pTruth = tCof1;
Alan Mishchenko committed
1375 1376
    // split decomposition
    Dau_DsdDecomposeInt( p1, &tCof0, nVars - 1 );
Alan Mishchenko committed
1377 1378
    Dau_DsdTranslate( p, pVars, nVars - 1, p1->pOutput );
    Dau_DsdWriteString( p, ">" );
Alan Mishchenko committed
1379
    p->nSizeNonDec = Abc_MaxInt( p->nSizeNonDec, p1->nSizeNonDec );
Alan Mishchenko committed
1380 1381
    if ( p1->nSizeNonDec )
        *pTruth = tCof0;
Alan Mishchenko committed
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
    return 0;
}
static inline int Dau_Dsd6DecomposeTripleVarsInner( Dau_Dsd_t * p, word  * pTruth, int * pVars, int nVars, int v, unsigned uSupports )
{
    int iVar0 = Abc_TtSuppFindFirst(  uSupports & (~uSupports >> 1) & 0x55555555 ) >> 1;
    int iVar1 = Abc_TtSuppFindFirst( ~uSupports & ( uSupports >> 1) & 0x55555555 ) >> 1;
    word tCof0 = Abc_Tt6Cofactor0( pTruth[0], v );
    word tCof1 = Abc_Tt6Cofactor1( pTruth[0], v );
    word C00 = Abc_Tt6Cofactor0( tCof0, iVar0 );
    word C01 = Abc_Tt6Cofactor1( tCof0, iVar0 );
    word C10 = Abc_Tt6Cofactor0( tCof1, iVar1 );
    word C11 = Abc_Tt6Cofactor1( tCof1, iVar1 );
    int fEqual0 = (C00 == C10) && (C01 == C11);
    int fEqual1 = (C00 == C11) && (C01 == C10);
    if ( fEqual0 || fEqual1 )
    {
        char pBuffer[10];
Alan Mishchenko committed
1399 1400 1401 1402 1403 1404
        int VarId = pVars[iVar0];
        pTruth[0] = (s_Truths6[v] & C11) | (~s_Truths6[v] & C10);
        sprintf( pBuffer, "<%c%c%s%c>", 'a' + pVars[v], 'a' + pVars[iVar1], fEqual1 ? "!":"", 'a' + pVars[iVar0] );
        pVars[v] = Dau_DsdAddVarDef( p, pBuffer );
        // remove iVar1
        ABC_SWAP( int, pVars[iVar1], pVars[nVars-1] );
Alan Mishchenko committed
1405
        Abc_TtSwapVars( pTruth, nVars, iVar1, nVars-1 ); nVars--;
Alan Mishchenko committed
1406 1407 1408
        // remove iVar0
        iVar0 = Dau_DsdFindVarDef( pVars, nVars, VarId );
        ABC_SWAP( int, pVars[iVar0], pVars[nVars-1] );
Alan Mishchenko committed
1409
        Abc_TtSwapVars( pTruth, nVars, iVar0, nVars-1 ); nVars--;
Alan Mishchenko committed
1410 1411 1412 1413 1414 1415
        // find the new var
        v = Dau_DsdFindVarDef( pVars, nVars, p->nVarsUsed-1 );
        // remove single variables if possible
        if ( Dau_Dsd6DecomposeSingleVarOne( p, pTruth, pVars, nVars, v ) )
            nVars = Dau_Dsd6DecomposeSingleVar( p, pTruth, pVars, --nVars );
        return nVars;
Alan Mishchenko committed
1416 1417 1418 1419 1420
    }
    return nVars;
}
int Dau_Dsd6DecomposeTripleVars( Dau_Dsd_t * p, word  * pTruth, int * pVars, int nVars )
{
1421
    abctime clk = Abc_Clock();
Alan Mishchenko committed
1422 1423 1424
    while ( 1 )
    {
        int v;
Alan Mishchenko committed
1425
//        Kit_DsdPrintFromTruth( (unsigned *)pTruth, 6 ); printf( "\n" );
Alan Mishchenko committed
1426 1427 1428
        for ( v = nVars - 1; v >= 0; v-- )
        {
            unsigned uSupports = Dau_Dsd6FindSupports( p, pTruth, pVars, nVars, v );
Alan Mishchenko committed
1429
//            Dau_DsdPrintSupports( uSupports, nVars );
Alan Mishchenko committed
1430 1431
            if ( (uSupports & (uSupports >> 1) & 0x55555555) == 0 ) // non-overlapping supports
                return Dau_Dsd6DecomposeTripleVarsOuter( p, pTruth, pVars, nVars, v );
Alan Mishchenko committed
1432
            if ( Abc_TtSuppOnlyOne( uSupports & (~uSupports >> 1) & 0x55555555) &&
Alan Mishchenko committed
1433
                 Abc_TtSuppOnlyOne(~uSupports & ( uSupports >> 1) & 0x55555555) ) // one unique variable in each cofactor
Alan Mishchenko committed
1434
            {
Alan Mishchenko committed
1435 1436 1437 1438
                int nVarsNew = Dau_Dsd6DecomposeTripleVarsInner( p, pTruth, pVars, nVars, v, uSupports );
                if ( nVarsNew == nVars )
                    continue;
                if ( nVarsNew == 0 )
Alan Mishchenko committed
1439
                {
1440
                    s_Times[2] += Abc_Clock() - clk;
Alan Mishchenko committed
1441
                    return 0;
Alan Mishchenko committed
1442
                }
Alan Mishchenko committed
1443
                nVars = Dau_Dsd6DecomposeDoubleVars( p, pTruth, pVars, nVarsNew );
Alan Mishchenko committed
1444
                if ( nVars == 0 )
Alan Mishchenko committed
1445
                {
1446
                    s_Times[2] += Abc_Clock() - clk;
Alan Mishchenko committed
1447
                    return 0;
Alan Mishchenko committed
1448
                }
Alan Mishchenko committed
1449
                break;
Alan Mishchenko committed
1450
            }
Alan Mishchenko committed
1451 1452
        }
        if ( v == -1 )
Alan Mishchenko committed
1453
        {
1454
            s_Times[2] += Abc_Clock() - clk;
Alan Mishchenko committed
1455
            return nVars;
Alan Mishchenko committed
1456
        }
Alan Mishchenko committed
1457 1458 1459 1460
    }
    assert( 0 );
    return -1;
}
Alan Mishchenko committed
1461
int Dau_Dsd6DecomposeInternal( Dau_Dsd_t * p, word  * pTruth, int * pVars, int nVars )
Alan Mishchenko committed
1462 1463 1464 1465
{
    // decompose single variales on the output side
    nVars = Dau_Dsd6DecomposeSingleVar( p, pTruth, pVars, nVars );
    if ( nVars == 0 )
Alan Mishchenko committed
1466
        return 0;
Alan Mishchenko committed
1467 1468 1469
    // decompose double variables on the input side
    nVars = Dau_Dsd6DecomposeDoubleVars( p, pTruth, pVars, nVars );
    if ( nVars == 0 )
Alan Mishchenko committed
1470
        return 0;
Alan Mishchenko committed
1471 1472 1473
    // decompose MUX on the output/input side
    nVars = Dau_Dsd6DecomposeTripleVars( p, pTruth, pVars, nVars );
    if ( nVars == 0 )
Alan Mishchenko committed
1474
        return 0;
Alan Mishchenko committed
1475
    // write non-decomposable function
Alan Mishchenko committed
1476
    return Dau_DsdWritePrime( p, pTruth, pVars, nVars );
Alan Mishchenko committed
1477
}
Alan Mishchenko committed
1478

Alan Mishchenko committed
1479
/**Function*************************************************************
Alan Mishchenko committed
1480

Alan Mishchenko committed
1481
  Synopsis    [Procedures specialized for 6-variable functions.]
Alan Mishchenko committed
1482

Alan Mishchenko committed
1483 1484 1485 1486 1487 1488 1489
  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1490
static inline int Dau_DsdDecomposeSingleVarOne( Dau_Dsd_t * p, word * pTruth, int * pVars, int nVars, int v )
Alan Mishchenko committed
1491
{
Alan Mishchenko committed
1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540
    int nWords = Abc_TtWordNum(nVars);
    // consider negative cofactors
    if ( pTruth[0] & 1 )
    {        
        if ( Abc_TtCof0IsConst1( pTruth, nWords, v ) ) // !(ax)
        {
            Dau_DsdWriteString( p, "!(" );
            Abc_TtCofactor1( pTruth, nWords, v );
            Abc_TtNot( pTruth, nWords );
            goto finish;            
        }
    }
    else
    {
        if ( Abc_TtCof0IsConst0( pTruth, nWords, v ) ) // ax
        {
            Dau_DsdWriteString( p, "(" );
            Abc_TtCofactor1( pTruth, nWords, v );
            goto finish;            
        }
    }
    // consider positive cofactors
    if ( pTruth[nWords-1] >> 63 )
    {        
        if ( Abc_TtCof1IsConst1( pTruth, nWords, v ) ) // !(!ax)
        {
            Dau_DsdWriteString( p, "!(!" );
            Abc_TtCofactor0( pTruth, nWords, v );
            Abc_TtNot( pTruth, nWords );
            goto finish;            
        }
    }
    else
    {
        if ( Abc_TtCof1IsConst0( pTruth, nWords, v ) ) // !ax
        {
            Dau_DsdWriteString( p, "(!" );
            Abc_TtCofactor0( pTruth, nWords, v );
            goto finish;            
        }
    }
    // consider equal cofactors
    if ( Abc_TtCofsOpposite( pTruth, nWords, v ) ) // [ax]
    {
        Dau_DsdWriteString( p, "[" );
        Abc_TtCofactor0( pTruth, nWords, v );
        p->uConstMask |= (1 << p->nConsts);
        goto finish;            
    }
Alan Mishchenko committed
1541
    return 0;
Alan Mishchenko committed
1542 1543 1544 1545 1546 1547 1548 1549 1550 1551

finish:
    p->nConsts++;
    Dau_DsdWriteVar( p, pVars[v], 0 );
    pVars[v] = pVars[nVars-1];
    Abc_TtSwapVars( pTruth, nVars, v, nVars-1 );
    return 1;
}
int Dau_DsdDecomposeSingleVar( Dau_Dsd_t * p, word * pTruth, int * pVars, int nVars )
{
1552
    abctime clk = Abc_Clock();
Alan Mishchenko committed
1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567
    assert( nVars > 1 );
    while ( 1 )
    {
        int v;
        for ( v = nVars - 1; v >= 0 && nVars > 1; v-- )
            if ( Dau_DsdDecomposeSingleVarOne( p, pTruth, pVars, nVars, v ) )
            {
                nVars--;
                break;
            }
        if ( v == -1 || nVars == 1 )
            break;
    }
    if ( nVars == 1 )
        Dau_DsdWriteVar( p, pVars[--nVars], (int)(pTruth[0] & 1) );
1568
    s_Times[0] += Abc_Clock() - clk;
Alan Mishchenko committed
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695
    return nVars;
}

static inline int Dau_DsdFindSupportOne( Dau_Dsd_t * p, word * pTruth, int * pVars, int nVars, int v, int u )
{
    int nWords = Abc_TtWordNum(nVars);
    int Status = p ? Dau_DsdLookupVarCache( p, pVars[v], pVars[u] ) : 0;
    if ( Status == 0 )
    {
//        Status = (Abc_Tt6HasVar(tCof1, u) << 1) | Abc_Tt6HasVar(tCof0, u);
        if ( v < u )
            Status = (!Abc_TtCheckEqualCofs(pTruth, nWords, v, u, 1, 3) << 1) | !Abc_TtCheckEqualCofs(pTruth, nWords, v, u, 0, 2);
        else // if ( v > u )
            Status = (!Abc_TtCheckEqualCofs(pTruth, nWords, u, v, 2, 3) << 1) | !Abc_TtCheckEqualCofs(pTruth, nWords, u, v, 0, 1);
        assert( Status != 0 );
        if ( p )
            Dau_DsdInsertVarCache( p, pVars[v], pVars[u], Status );
    }
    return Status;
}
static inline unsigned Dau_DsdFindSupports( Dau_Dsd_t * p, word  * pTruth, int * pVars, int nVars, int v )
{
    int u;
    unsigned uSupports = 0;
//Kit_DsdPrintFromTruth( (unsigned *)&tCof0, 6 );printf( "\n" );
//Kit_DsdPrintFromTruth( (unsigned *)&tCof1, 6 );printf( "\n" );
    for ( u = 0; u < nVars; u++ )
        if ( u != v )
            uSupports |= (Dau_DsdFindSupportOne( p, pTruth, pVars, nVars, v, u ) << (2 * u));
    return uSupports;
}

// checks decomposability with respect to the pair (v, u)
static inline int Dau_DsdDecomposeDoubleVarsOne( Dau_Dsd_t * p, word  * pTruth, int * pVars, int nVars, int v, int u )
{
    char pBuffer[10] = { 0 }; 
    int nWords = Abc_TtWordNum(nVars);
    int Status = Dau_DsdFindSupportOne( p, pTruth, pVars, nVars, v, u );
    assert( v > u );
//printf( "Checking %s and %s.\n", p->pVarDefs[pVars[v]], p->pVarDefs[pVars[u]] );
    if ( Status == 3 )
    {
        // both F(v=0) and F(v=1) depend on u
//        if ( Abc_Tt6Cof0EqualCof1(tCof0, tCof1, u) && Abc_Tt6Cof0EqualCof1(tCof1, tCof0, u) ) // v+u
        if ( Abc_TtCheckEqualCofs(pTruth, nWords, u, v, 0, 3) && Abc_TtCheckEqualCofs(pTruth, nWords, u, v, 1, 2) ) // 00=11 01=10 v+u
        {
            word pTtTemp[2][DAU_MAX_WORD];
            sprintf( pBuffer, "[%c%c]", 'a' + pVars[v], 'a' + pVars[u] );
//            pTruth[0] = (s_Truths6[u] & Abc_Tt6Cofactor1(tCof0, u)) | (~s_Truths6[u] & Abc_Tt6Cofactor0(tCof0, u));
            Abc_TtCofactor0p( pTtTemp[0], pTruth, nWords, v );
            Abc_TtCofactor0( pTtTemp[0], nWords, u );
            Abc_TtCofactor0p( pTtTemp[1], pTruth, nWords, v );
            Abc_TtCofactor1( pTtTemp[1], nWords, u );
            Abc_TtMux( pTruth, Dau_DsdTtElems()[u], pTtTemp[1], pTtTemp[0], nWords );
            goto finish;
        }
    }
    else if ( Status == 2 )
    {
        // F(v=0) does not depend on u; F(v=1) depends on u
//        if ( Abc_Tt6Cof0EqualCof0(tCof0, tCof1, u) ) // vu
        if ( Abc_TtCheckEqualCofs(pTruth, nWords, u, v, 0, 2) ) // 00=10 vu
        {
            word pTtTemp[2][DAU_MAX_WORD];
            sprintf( pBuffer, "(%c%c)", 'a' + pVars[v], 'a' + pVars[u] );
//            pTruth[0] = (s_Truths6[u] & Abc_Tt6Cofactor1(tCof1, u)) | (~s_Truths6[u] & Abc_Tt6Cofactor0(tCof0, u));
            Abc_TtCofactor0p( pTtTemp[0], pTruth, nWords, v );
            Abc_TtCofactor0( pTtTemp[0], nWords, u );
            Abc_TtCofactor1p( pTtTemp[1], pTruth, nWords, v );
            Abc_TtCofactor1( pTtTemp[1], nWords, u );
            Abc_TtMux( pTruth, Dau_DsdTtElems()[u], pTtTemp[1], pTtTemp[0], nWords );
            goto finish;
        }
//        if ( Abc_Tt6Cof0EqualCof1(tCof0, tCof1, u) ) // v!u
        if ( Abc_TtCheckEqualCofs(pTruth, nWords, u, v, 0, 3) ) // 00=11 v!u
        {
            word pTtTemp[2][DAU_MAX_WORD];
            sprintf( pBuffer, "(%c!%c)", 'a' + pVars[v], 'a' + pVars[u] );
//            pTruth[0] = (s_Truths6[u] & Abc_Tt6Cofactor0(tCof1, u)) | (~s_Truths6[u] & Abc_Tt6Cofactor0(tCof0, u));
            Abc_TtCofactor0p( pTtTemp[0], pTruth, nWords, v );
            Abc_TtCofactor0( pTtTemp[0], nWords, u );
            Abc_TtCofactor1p( pTtTemp[1], pTruth, nWords, v );
            Abc_TtCofactor0( pTtTemp[1], nWords, u );
            Abc_TtMux( pTruth, Dau_DsdTtElems()[u], pTtTemp[1], pTtTemp[0], nWords );
            goto finish;
        }
    }
    else if ( Status == 1 )
    {
        // F(v=0) depends on u; F(v=1) does not depend on u
//        if ( Abc_Tt6Cof0EqualCof1(tCof0, tCof1, u) ) // !vu
        if ( Abc_TtCheckEqualCofs(pTruth, nWords, u, v, 0, 3) ) // 00=11 !vu
        {
            word pTtTemp[2][DAU_MAX_WORD];
            sprintf( pBuffer, "(!%c%c)", 'a' + pVars[v], 'a' + pVars[u] );
//            pTruth[0] = (s_Truths6[u] & Abc_Tt6Cofactor1(tCof0, u)) | (~s_Truths6[u] & Abc_Tt6Cofactor0(tCof0, u));
            Abc_TtCofactor0p( pTtTemp[0], pTruth, nWords, v );
            Abc_TtCofactor0( pTtTemp[0], nWords, u );
            Abc_TtCofactor0p( pTtTemp[1], pTruth, nWords, v );
            Abc_TtCofactor1( pTtTemp[1], nWords, u );
            Abc_TtMux( pTruth, Dau_DsdTtElems()[u], pTtTemp[1], pTtTemp[0], nWords );
            goto finish;
        }
//        if ( Abc_Tt6Cof1EqualCof1(tCof0, tCof1, u) ) // !v!u
        if ( Abc_TtCheckEqualCofs(pTruth, nWords, u, v, 1, 3) ) // 01=11 !v!u
        {
            word pTtTemp[2][DAU_MAX_WORD];
            sprintf( pBuffer, "(!%c!%c)", 'a' + pVars[v], 'a' + pVars[u] );
//            pTruth[0] = (s_Truths6[u] & Abc_Tt6Cofactor0(tCof0, u)) | (~s_Truths6[u] & Abc_Tt6Cofactor1(tCof1, u));
            Abc_TtCofactor1p( pTtTemp[0], pTruth, nWords, v );
            Abc_TtCofactor1( pTtTemp[0], nWords, u );
            Abc_TtCofactor0p( pTtTemp[1], pTruth, nWords, v );
            Abc_TtCofactor0( pTtTemp[1], nWords, u );
            Abc_TtMux( pTruth, Dau_DsdTtElems()[u], pTtTemp[1], pTtTemp[0], nWords );
            goto finish;
        }
    }
    return nVars;
finish: 
    // finalize decomposition
    assert( pBuffer[0] );
    pVars[u] = Dau_DsdAddVarDef( p, pBuffer );
    pVars[v] = pVars[nVars-1];
    Abc_TtSwapVars( pTruth, nVars, v, nVars-1 );
    if ( Dau_DsdDecomposeSingleVarOne( p, pTruth, pVars, --nVars, u ) )
        nVars = Dau_DsdDecomposeSingleVar( p, pTruth, pVars, --nVars );
    return nVars;
Alan Mishchenko committed
1696 1697 1698
}
int Dau_DsdDecomposeDoubleVars( Dau_Dsd_t * p, word  * pTruth, int * pVars, int nVars )
{
1699
    abctime clk = Abc_Clock();
Alan Mishchenko committed
1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712
    while ( 1 )
    {
        int v, u, nVarsOld;
        for ( v = nVars - 1; v > 0; v-- )
        {
            for ( u = v - 1; u >= 0; u-- )
            {
                if ( Dau_DsdLookupVarCache( p, pVars[v], pVars[u] ) )
                    continue;
                nVarsOld = nVars;
                nVars = Dau_DsdDecomposeDoubleVarsOne( p, pTruth, pVars, nVars, v, u );
                if ( nVars == 0 )
                {
1713
                    s_Times[1] += Abc_Clock() - clk;
Alan Mishchenko committed
1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724
                    return 0;
                }
                if ( nVarsOld > nVars )
                    break;
            }
            if ( u >= 0 ) // found
                break;
        }
        if ( v == 0 ) // not found
            break;
    }
1725
    s_Times[1] += Abc_Clock() - clk;
Alan Mishchenko committed
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736
    return nVars;
}

// look for MUX-decomposable variable on top or at the bottom
static inline int Dau_DsdDecomposeTripleVarsOuter( Dau_Dsd_t * p, word  * pTruth, int * pVars, int nVars, int v )
{
    extern int Dau_DsdDecomposeInt( Dau_Dsd_t * p, word * pTruth, int nVarsInit );
    Dau_Dsd_t P1, * p1 = &P1;
    word pTtCof[2][DAU_MAX_WORD];
    int nWords = Abc_TtWordNum(nVars);
    p1->fSplitPrime = 0;
Alan Mishchenko committed
1737
    p1->fWriteTruth = p->fWriteTruth;
Alan Mishchenko committed
1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752
    // move this variable to the top
    ABC_SWAP( int, pVars[v], pVars[nVars-1] );
    Abc_TtSwapVars( pTruth, nVars, v, nVars-1 );
    // cofactor w.r.t the last variable
//    tCof0 = Abc_Tt6Cofactor0( pTruth[0], nVars - 1 );
//    tCof1 = Abc_Tt6Cofactor1( pTruth[0], nVars - 1 );
    Abc_TtCofactor0p( pTtCof[0], pTruth, nWords, nVars - 1 );
    Abc_TtCofactor1p( pTtCof[1], pTruth, nWords, nVars - 1 );
    // compose the result
    Dau_DsdWriteString( p, "<" );
    Dau_DsdWriteVar( p, pVars[nVars - 1], 0 );
    // split decomposition
    Dau_DsdDecomposeInt( p1, pTtCof[1], nVars - 1 );
    Dau_DsdTranslate( p, pVars, nVars - 1, p1->pOutput );
    p->nSizeNonDec = p1->nSizeNonDec;
Alan Mishchenko committed
1753 1754
    if ( p1->nSizeNonDec )
        Abc_TtCopy( pTruth, pTtCof[1], Abc_TtWordNum(p1->nSizeNonDec), 0 );
Alan Mishchenko committed
1755 1756 1757 1758 1759
    // split decomposition
    Dau_DsdDecomposeInt( p1, pTtCof[0], nVars - 1 );
    Dau_DsdTranslate( p, pVars, nVars - 1, p1->pOutput );
    Dau_DsdWriteString( p, ">" );
    p->nSizeNonDec = Abc_MaxInt( p->nSizeNonDec, p1->nSizeNonDec );
Alan Mishchenko committed
1760 1761
    if ( p1->nSizeNonDec )
        Abc_TtCopy( pTruth, pTtCof[0], Abc_TtWordNum(p1->nSizeNonDec), 0 );
Alan Mishchenko committed
1762 1763
    return 0;
}
Alan Mishchenko committed
1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811
static inline int Dau_DsdDecomposeTripleVarsInner( Dau_Dsd_t * p, word  * pTruth, int * pVars, int nVars, int v, unsigned uSupports )
{
    int nWords = Abc_TtWordNum(nVars);
    int iVar0 = Abc_TtSuppFindFirst(  uSupports & (~uSupports >> 1) & 0x55555555 ) >> 1;
    int iVar1 = Abc_TtSuppFindFirst( ~uSupports & ( uSupports >> 1) & 0x55555555 ) >> 1;
    int fEqual0, fEqual1;
//    word tCof0 = Abc_Tt6Cofactor0( pTruth[0], v );
//    word tCof1 = Abc_Tt6Cofactor1( pTruth[0], v );
//    word C00 = Abc_Tt6Cofactor0( tCof0, iVar0 );
//    word C01 = Abc_Tt6Cofactor1( tCof0, iVar0 );
//    word C10 = Abc_Tt6Cofactor0( tCof1, iVar1 );
//    word C11 = Abc_Tt6Cofactor1( tCof1, iVar1 );
//    int fEqual0 = (C00 == C10) && (C01 == C11);
//    int fEqual1 = (C00 == C11) && (C01 == C10);
    word pTtCof[2][DAU_MAX_WORD];
    word pTtFour[2][2][DAU_MAX_WORD];
    Abc_TtCofactor0p( pTtCof[0], pTruth, nWords, v );
    Abc_TtCofactor1p( pTtCof[1], pTruth, nWords, v );
    Abc_TtCofactor0p( pTtFour[0][0], pTtCof[0], nWords, iVar0 );
    Abc_TtCofactor1p( pTtFour[0][1], pTtCof[0], nWords, iVar0 );
    Abc_TtCofactor0p( pTtFour[1][0], pTtCof[1], nWords, iVar1 );
    Abc_TtCofactor1p( pTtFour[1][1], pTtCof[1], nWords, iVar1 );
    fEqual0 = Abc_TtEqual(pTtFour[0][0], pTtFour[1][0], nWords) && Abc_TtEqual(pTtFour[0][1], pTtFour[1][1], nWords);
    fEqual1 = Abc_TtEqual(pTtFour[0][0], pTtFour[1][1], nWords) && Abc_TtEqual(pTtFour[0][1], pTtFour[1][0], nWords);
    if ( fEqual0 || fEqual1 )
    {
        char pBuffer[10];
        int VarId = pVars[iVar0];
//        pTruth[0] = (s_Truths6[v] & C11) | (~s_Truths6[v] & C10);
        Abc_TtMux( pTruth, Dau_DsdTtElems()[v], pTtFour[1][1], pTtFour[1][0], nWords );
        sprintf( pBuffer, "<%c%c%s%c>", 'a' + pVars[v], 'a' + pVars[iVar1], fEqual1 ? "!":"", 'a' + pVars[iVar0] );
        pVars[v] = Dau_DsdAddVarDef( p, pBuffer );
        // remove iVar1
        ABC_SWAP( int, pVars[iVar1], pVars[nVars-1] );
        Abc_TtSwapVars( pTruth, nVars, iVar1, nVars-1 ); nVars--;
        // remove iVar0
        iVar0 = Dau_DsdFindVarDef( pVars, nVars, VarId );
        ABC_SWAP( int, pVars[iVar0], pVars[nVars-1] );
        Abc_TtSwapVars( pTruth, nVars, iVar0, nVars-1 ); nVars--;
        // find the new var
        v = Dau_DsdFindVarDef( pVars, nVars, p->nVarsUsed-1 );
        // remove single variables if possible
        if ( Dau_DsdDecomposeSingleVarOne( p, pTruth, pVars, nVars, v ) )
            nVars = Dau_DsdDecomposeSingleVar( p, pTruth, pVars, --nVars );
        return nVars;
    }
    return nVars;
}
Alan Mishchenko committed
1812 1813
int Dau_DsdDecomposeTripleVars( Dau_Dsd_t * p, word  * pTruth, int * pVars, int nVars )
{
1814
    abctime clk = Abc_Clock();
Alan Mishchenko committed
1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832
    while ( 1 )
    {
        int v;
//        Kit_DsdPrintFromTruth( (unsigned *)pTruth, 6 ); printf( "\n" );
        for ( v = nVars - 1; v >= 0; v-- )
        {
            unsigned uSupports = Dau_DsdFindSupports( p, pTruth, pVars, nVars, v );
//            Dau_DsdPrintSupports( uSupports, nVars );
            if ( (uSupports & (uSupports >> 1) & 0x55555555) == 0 ) // non-overlapping supports
                return Dau_DsdDecomposeTripleVarsOuter( p, pTruth, pVars, nVars, v );
            if ( Abc_TtSuppOnlyOne( uSupports & (~uSupports >> 1) & 0x55555555) &&
                 Abc_TtSuppOnlyOne(~uSupports & ( uSupports >> 1) & 0x55555555) ) // one unique variable in each cofactor
            {
                int nVarsNew = Dau_DsdDecomposeTripleVarsInner( p, pTruth, pVars, nVars, v, uSupports );
                if ( nVarsNew == nVars )
                    continue;
                if ( nVarsNew == 0 )
                {
1833
                    s_Times[2] += Abc_Clock() - clk;
Alan Mishchenko committed
1834 1835 1836 1837 1838
                    return 0;
                }
                nVars = Dau_DsdDecomposeDoubleVars( p, pTruth, pVars, nVarsNew );
                if ( nVars == 0 )
                {
1839
                    s_Times[2] += Abc_Clock() - clk;
Alan Mishchenko committed
1840 1841 1842 1843 1844 1845 1846
                    return 0;
                }
                break;
            }
        }
        if ( v == -1 )
        {
1847
            s_Times[2] += Abc_Clock() - clk;
Alan Mishchenko committed
1848 1849 1850 1851 1852
            return nVars;
        }
    }
    assert( 0 );
    return -1;
Alan Mishchenko committed
1853
}
Alan Mishchenko committed
1854
int Dau_DsdDecomposeInternal( Dau_Dsd_t * p, word  * pTruth, int * pVars, int nVars )
1855
{
Alan Mishchenko committed
1856 1857 1858
    // decompose single variales on the output side
    nVars = Dau_DsdDecomposeSingleVar( p, pTruth, pVars, nVars );
    if ( nVars == 0 )
Alan Mishchenko committed
1859
        return 0;
Alan Mishchenko committed
1860 1861 1862
    // decompose double variables on the input side
    nVars = Dau_DsdDecomposeDoubleVars( p, pTruth, pVars, nVars );
    if ( nVars == 0 )
Alan Mishchenko committed
1863
        return 0;
Alan Mishchenko committed
1864 1865 1866
    // decompose MUX on the output/input side
    nVars = Dau_DsdDecomposeTripleVars( p, pTruth, pVars, nVars );
    if ( nVars == 0 )
Alan Mishchenko committed
1867
        return 0;
Alan Mishchenko committed
1868
    // write non-decomposable function
Alan Mishchenko committed
1869
    return Dau_DsdWritePrime( p, pTruth, pVars, nVars );
1870
}
Alan Mishchenko committed
1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882

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

  Synopsis    [Fast DSD for truth tables.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896
int Dau_DsdMinBase( word * pTruth, int nVars, int * pVarsNew )
{
    int v;
    for ( v = 0; v < nVars; v++ )
        pVarsNew[v] = v;
    for ( v = nVars - 1; v >= 0; v-- )
    {
        if ( Abc_TtHasVar( pTruth, nVars, v ) )
            continue;
        Abc_TtSwapVars( pTruth, nVars, v, nVars-1 );
        pVarsNew[v] = pVarsNew[--nVars];
    }
    return nVars;
}
Alan Mishchenko committed
1897
int Dau_DsdDecomposeInt( Dau_Dsd_t * p, word * pTruth, int nVarsInit )
1898
{
Alan Mishchenko committed
1899
    int Status = 0, nVars, pVars[16];
Alan Mishchenko committed
1900 1901
    Dau_DsdInitialize( p, nVarsInit );
    nVars = Dau_DsdMinBase( pTruth, nVarsInit, pVars );
Alan Mishchenko committed
1902
    assert( nVars > 0 && nVars <= nVarsInit );
Alan Mishchenko committed
1903 1904 1905
    if ( nVars == 1 )
        Dau_DsdWriteVar( p, pVars[0], (int)(pTruth[0] & 1) );
    else if ( nVars <= 6 )
Alan Mishchenko committed
1906
        Status = Dau_Dsd6DecomposeInternal( p, pTruth, pVars, nVars );
Alan Mishchenko committed
1907
    else
Alan Mishchenko committed
1908
        Status = Dau_DsdDecomposeInternal( p, pTruth, pVars, nVars );
Alan Mishchenko committed
1909
    Dau_DsdFinalize( p );
Alan Mishchenko committed
1910
    return Status;
Alan Mishchenko committed
1911
}
Alan Mishchenko committed
1912
int Dau_DsdDecompose( word * pTruth, int nVarsInit, int fSplitPrime, int fWriteTruth, char * pRes )
Alan Mishchenko committed
1913
{
Alan Mishchenko committed
1914 1915
    Dau_Dsd_t P, * p = &P;
    p->fSplitPrime = fSplitPrime;
Alan Mishchenko committed
1916
    p->fWriteTruth = fWriteTruth;
1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941
    p->pVarLevels  = NULL;
    p->nSizeNonDec = 0;
    if ( (pTruth[0] & 1) == 0 && Abc_TtIsConst0(pTruth, Abc_TtWordNum(nVarsInit)) )
        { if ( pRes ) pRes[0] = '0', pRes[1] = 0; }
    else if ( (pTruth[0] & 1) && Abc_TtIsConst1(pTruth, Abc_TtWordNum(nVarsInit)) )
        { if ( pRes ) pRes[0] = '1', pRes[1] = 0; }
    else 
    {
        int Status = Dau_DsdDecomposeInt( p, pTruth, nVarsInit );
        Dau_DsdRemoveBraces( p->pOutput, Dau_DsdComputeMatches(p->pOutput) );
        if ( pRes )
            strcpy( pRes, p->pOutput );
        assert( fSplitPrime || Status != 1 );
        if ( fSplitPrime && Status == 2 )
            return -1;
    }
//    assert( p->nSizeNonDec == 0 );
    return p->nSizeNonDec;
}
int Dau_DsdDecomposeLevel( word * pTruth, int nVarsInit, int fSplitPrime, int fWriteTruth, char * pRes, int * pVarLevels )
{
    Dau_Dsd_t P, * p = &P;
    p->fSplitPrime = fSplitPrime;
    p->fWriteTruth = fWriteTruth;
    p->pVarLevels  = pVarLevels;
Alan Mishchenko committed
1942
    p->nSizeNonDec = 0;
Alan Mishchenko committed
1943
    if ( (pTruth[0] & 1) == 0 && Abc_TtIsConst0(pTruth, Abc_TtWordNum(nVarsInit)) )
Alan Mishchenko committed
1944
        { if ( pRes ) pRes[0] = '0', pRes[1] = 0; }
Alan Mishchenko committed
1945
    else if ( (pTruth[0] & 1) && Abc_TtIsConst1(pTruth, Abc_TtWordNum(nVarsInit)) )
Alan Mishchenko committed
1946
        { if ( pRes ) pRes[0] = '1', pRes[1] = 0; }
Alan Mishchenko committed
1947
    else 
1948
    {
Alan Mishchenko committed
1949 1950 1951 1952 1953 1954 1955
        int Status = Dau_DsdDecomposeInt( p, pTruth, nVarsInit );
        Dau_DsdRemoveBraces( p->pOutput, Dau_DsdComputeMatches(p->pOutput) );
        if ( pRes )
            strcpy( pRes, p->pOutput );
        assert( fSplitPrime || Status != 1 );
        if ( fSplitPrime && Status == 2 )
            return -1;
1956
    }
Alan Mishchenko committed
1957
//    assert( p->nSizeNonDec == 0 );
Alan Mishchenko committed
1958
    return p->nSizeNonDec;
1959
}
Alan Mishchenko committed
1960
void Dau_DsdPrintFromTruthFile( FILE * pFile, word * pTruth, int nVarsInit )
Alan Mishchenko committed
1961 1962
{
    char pRes[DAU_MAX_STR];
1963 1964 1965
    word pTemp[DAU_MAX_WORD];
    Abc_TtCopy( pTemp, pTruth, Abc_TtWordNum(nVarsInit), 0 );
    Dau_DsdDecompose( pTemp, nVarsInit, 0, 1, pRes );
Alan Mishchenko committed
1966 1967
    fprintf( pFile, "%s\n", pRes );
}
Alan Mishchenko committed
1968 1969 1970 1971 1972 1973 1974 1975
void Dau_DsdPrintFromTruth( word * pTruth, int nVarsInit )
{
    char pRes[DAU_MAX_STR];
    word pTemp[DAU_MAX_WORD];
    Abc_TtCopy( pTemp, pTruth, Abc_TtWordNum(nVarsInit), 0 );
    Dau_DsdDecompose( pTemp, nVarsInit, 0, 1, pRes );
    fprintf( stdout, "%s\n", pRes );
}
Alan Mishchenko committed
1976 1977

void Dau_DsdTest44()
Alan Mishchenko committed
1978
{
Alan Mishchenko committed
1979
    char pRes[DAU_MAX_STR];
Alan Mishchenko committed
1980
//    char * pStr = "(!(!a<bcd>)!(!fe))";
Alan Mishchenko committed
1981
//    char * pStr = "([acb]<!edf>)";
Alan Mishchenko committed
1982
//    char * pStr = "!(f!(b!c!(d[ea])))";
Alan Mishchenko committed
1983
//    char * pStr = "[!(a[be])!(c!df)]";
Alan Mishchenko committed
1984 1985 1986 1987
//    char * pStr = "<(e<bca>)fd>";
//    char * pStr = "[d8001{abef}c]";
    char * pStr = "[dc<a(cbd)(!c!b!d)>{abef}]";
//    char * pStr3;
Alan Mishchenko committed
1988
    word t = Dau_Dsd6ToTruth( pStr );
Alan Mishchenko committed
1989
//    return;
Alan Mishchenko committed
1990
    int nNonDec = Dau_DsdDecompose( &t, 6, 1, 1, pRes );
Alan Mishchenko committed
1991 1992
//    Dau_DsdNormalize( pStr2 );
//    Dau_DsdExtract( pStr, 2, 0 );
Alan Mishchenko committed
1993
    t = 0; 
Alan Mishchenko committed
1994
    nNonDec = 0;
Alan Mishchenko committed
1995
}
1996

Alan Mishchenko committed
1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027


void Dau_DsdTest888()
{
    char pDsd[DAU_MAX_STR];
    int nVars = 9;
//    char * pStr = "[(abc)(def)(ghi)]";
//    char * pStr = "[a!b!(c!d[e(fg)hi])]";
//    char * pStr = "[(abc)(def)]";
//    char * pStr = "[(abc)(def)]";
//    char * pStr = "[abcdefg]";
//    char * pStr = "[<abc>(de[ghi])]";
    char * pStr = "(<abc>(<def><ghi>))";
    word * pTruth = Dau_DsdToTruth( pStr, 9 );
    int i, Status;
//    Kit_DsdPrintFromTruth( (unsigned *)pTruth, 9 ); printf( "\n" );
/*
    for ( i = 0; i < 6; i++ )
    {
        unsigned uSupp = Dau_Dsd6FindSupports( NULL, pTruth, NULL, 6, i );
        Dau_DsdPrintSupports( uSupp, 6 );
    }
*/
/*
    printf( "\n" );
    for ( i = 0; i < nVars; i++ )
    {
        unsigned uSupp = Dau_DsdFindSupports( NULL, pTruth, NULL, nVars, i );
        Dau_DsdPrintSupports( uSupp, nVars );
    }
*/
Alan Mishchenko committed
2028
    Status = Dau_DsdDecompose( pTruth, nVars, 0, 0, pDsd );
Alan Mishchenko committed
2029 2030 2031
    i = 0;
}

Alan Mishchenko committed
2032
void Dau_DsdTest555()
Alan Mishchenko committed
2033
{
Alan Mishchenko committed
2034 2035 2036
    int nVars = 10;
    int nWords = Abc_TtWordNum(nVars);
    char * pFileName = "_npn/npn/dsd10.txt";
Alan Mishchenko committed
2037
    FILE * pFile = fopen( pFileName, "rb" );
Alan Mishchenko committed
2038 2039
    word Tru[2][DAU_MAX_WORD], * pTruth;
    char pBuffer[DAU_MAX_STR];
Alan Mishchenko committed
2040
    char pRes[DAU_MAX_STR];
Alan Mishchenko committed
2041 2042
    int nSizeNonDec;
    int i, Counter = 0;
2043
    abctime clk = Abc_Clock(), clkDec = 0, clk2;
Alan Mishchenko committed
2044
//    return;
Alan Mishchenko committed
2045 2046

    while ( fgets( pBuffer, DAU_MAX_STR, pFile ) != NULL )
Alan Mishchenko committed
2047
    {
Alan Mishchenko committed
2048
        char * pStr2 = pBuffer + strlen(pBuffer)-1;
Alan Mishchenko committed
2049 2050 2051 2052 2053 2054
        if ( *pStr2 == '\n' )
            *pStr2-- = 0;
        if ( *pStr2 == '\r' )
            *pStr2-- = 0;
        if ( pBuffer[0] == 'V' || pBuffer[0] == 0 )
            continue;
Alan Mishchenko committed
2055
        Counter++; 
Alan Mishchenko committed
2056

Alan Mishchenko committed
2057
        for ( i = 0; i < 1; i++ )
Alan Mishchenko committed
2058
        {
Alan Mishchenko committed
2059
//            Dau_DsdPermute( pBuffer );
Alan Mishchenko committed
2060 2061 2062
            pTruth = Dau_DsdToTruth( pBuffer[0] == '*' ? pBuffer + 1 : pBuffer, nVars );
            Abc_TtCopy( Tru[0], pTruth, nWords, 0 );
            Abc_TtCopy( Tru[1], pTruth, nWords, 0 );
2063
            clk2 = Abc_Clock();
Alan Mishchenko committed
2064
            nSizeNonDec = Dau_DsdDecompose( Tru[1], nVars, 0, 1, pRes );
2065
            clkDec += Abc_Clock() - clk2;
Alan Mishchenko committed
2066
            Dau_DsdNormalize( pRes );
Alan Mishchenko committed
2067
//            pStr2 = Dau_DsdPerform( t ); nSizeNonDec = 0;
Alan Mishchenko committed
2068
            assert( nSizeNonDec == 0 );
Alan Mishchenko committed
2069 2070
            pTruth = Dau_DsdToTruth( pRes, nVars );
            if ( !Abc_TtEqual( pTruth, Tru[0], nWords ) )
Alan Mishchenko committed
2071 2072 2073 2074
            {
    //        Kit_DsdPrintFromTruth( (unsigned *)&t, 6 );
    //        printf( "  " );
    //        Kit_DsdPrintFromTruth( (unsigned *)&t2, 6 );
Alan Mishchenko committed
2075
                printf( "%s -> %s \n", pBuffer, pRes );
Alan Mishchenko committed
2076 2077 2078 2079
                printf( "Verification failed.\n" );
            }
        }
    }
Alan Mishchenko committed
2080
    printf( "Finished trying %d decompositions.  ", Counter );
Alan Mishchenko committed
2081
    Abc_PrintTime( 1, "Time", clkDec );
2082
    Abc_PrintTime( 1, "Total", Abc_Clock() - clk );
Alan Mishchenko committed
2083 2084 2085 2086 2087

    Abc_PrintTime( 1, "Time1", s_Times[0] );
    Abc_PrintTime( 1, "Time2", s_Times[1] );
    Abc_PrintTime( 1, "Time3", s_Times[2] );

Alan Mishchenko committed
2088 2089 2090
    fclose( pFile );
}

2091 2092 2093 2094 2095 2096 2097 2098
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


ABC_NAMESPACE_IMPL_END