acecPolyn.c 15.1 KB
Newer Older
1 2
/**CFile****************************************************************

3
  FileName    [acecPolyn.c]
4 5 6

  SystemName  [ABC: Logic synthesis and verification system.]

7
  PackageName [CEC for arithmetic circuits.]
8

9
  Synopsis    [Polynomial extraction.]
10 11 12 13 14 15 16

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

17
  Revision    [$Id: acecPolyn.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
18 19 20

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

21
#include "acecInt.h"
22 23
#include "misc/vec/vecWec.h"
#include "misc/vec/vecHsh.h"
24
#include "misc/vec/vecQue.h"
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

ABC_NAMESPACE_IMPL_START


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

/*
!a            ->   1 - a
a & b         ->   ab
a | b         ->   a + b - ab
a ^ b         ->   a + b - 2ab
MUX(a, b, c)  ->   ab | (1 - a)c = ab + (1-a)c - ab(1-a)c = ab + c - ac

!a & b        ->   (1 - a)b = b - ab
 a & !b       ->   a(1 - b) = a - ab
!a & !b       ->   1 - a - b + ab
*/

45 46 47 48 49 50 51 52 53 54 55
typedef struct Pln_Man_t_ Pln_Man_t;
struct Pln_Man_t_
{
    Gia_Man_t *    pGia;      // AIG manager
    Hsh_VecMan_t * pHashC;    // hash table for constants
    Hsh_VecMan_t * pHashM;    // hash table for monomials
    Vec_Que_t *    vQue;      // queue by largest node
    Vec_Flt_t *    vCounts;   // largest node
    Vec_Int_t *    vCoefs;    // coefficients for each monomial
    Vec_Int_t *    vTempC[2]; // polynomial representation
    Vec_Int_t *    vTempM[4]; // polynomial representation
56
    Vec_Int_t *    vOrder;    // order of collapsing
57 58
    int            nBuilds;   // built monomials
    int            nUsed;     // used monomials
59
};
60 61 62 63 64

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

65
/**Function*************************************************************
66

67 68 69 70 71 72 73 74 75
  Synopsis    [Computation manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
76
Pln_Man_t * Pln_ManAlloc( Gia_Man_t * pGia, Vec_Int_t * vOrder )
77 78 79 80 81 82 83 84 85 86 87 88 89 90
{
    Pln_Man_t * p = ABC_CALLOC( Pln_Man_t, 1 );
    p->pGia      = pGia;
    p->pHashC    = Hsh_VecManStart( 1000 );
    p->pHashM    = Hsh_VecManStart( 1000 );
    p->vQue      = Vec_QueAlloc( 1000 );
    p->vCounts   = Vec_FltAlloc( 1000 );
    p->vCoefs    = Vec_IntAlloc( 1000 );
    p->vTempC[0] = Vec_IntAlloc( 100 );
    p->vTempC[1] = Vec_IntAlloc( 100 );
    p->vTempM[0] = Vec_IntAlloc( 100 );
    p->vTempM[1] = Vec_IntAlloc( 100 );
    p->vTempM[2] = Vec_IntAlloc( 100 );
    p->vTempM[3] = Vec_IntAlloc( 100 );
91
    p->vOrder    = vOrder ? Vec_IntDup(vOrder) : Vec_IntStartNatural( Gia_ManObjNum(pGia) );
92
    assert( Vec_IntSize(p->vOrder) == Gia_ManObjNum(pGia) );
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    Vec_QueSetPriority( p->vQue, Vec_FltArrayP(p->vCounts) );
    // add 0-constant and 1-monomial
    Hsh_VecManAdd( p->pHashC, p->vTempC[0] );
    Hsh_VecManAdd( p->pHashM, p->vTempM[0] );
    Vec_FltPush( p->vCounts, 0 );
    Vec_IntPush( p->vCoefs, 0 );
    return p;
}
void Pln_ManStop( Pln_Man_t * p )
{
    Hsh_VecManStop( p->pHashC );
    Hsh_VecManStop( p->pHashM );
    Vec_QueFree( p->vQue );
    Vec_FltFree( p->vCounts );
    Vec_IntFree( p->vCoefs );
    Vec_IntFree( p->vTempC[0] );
    Vec_IntFree( p->vTempC[1] );
    Vec_IntFree( p->vTempM[0] );
    Vec_IntFree( p->vTempM[1] );
    Vec_IntFree( p->vTempM[2] );
    Vec_IntFree( p->vTempM[3] );
114
    Vec_IntFree( p->vOrder );
115 116
    ABC_FREE( p );
}
117 118 119 120 121 122 123 124
int Pln_ManCompare3( int * pData0, int * pData1 )
{
    if ( pData0[0] < pData1[0] ) return -1;
    if ( pData0[0] > pData1[0] ) return  1;
    if ( pData0[1] < pData1[1] ) return -1;
    if ( pData0[1] > pData1[1] ) return  1;
    return 0;
}
125
void Pln_ManPrintFinal( Pln_Man_t * p, int fVerbose, int fVeryVerbose )
126 127
{
    Vec_Int_t * vArray;
128 129 130
    int i, k, Entry, iMono, iConst;
    // collect triples
    Vec_Int_t * vPairs = Vec_IntAlloc( 100 );
131 132 133 134
    Vec_IntForEachEntry( p->vCoefs, iConst, iMono )
    {
        if ( iConst == 0 ) 
            continue;
135 136 137 138 139 140 141
        vArray = Hsh_VecReadEntry( p->pHashC, iConst );
        Vec_IntPush( vPairs, Vec_IntEntry(vArray, 0) );
        vArray = Hsh_VecReadEntry( p->pHashM, iMono );
        Vec_IntPush( vPairs, Vec_IntSize(vArray) ? Vec_IntEntry(vArray, 0) : 0 );
        Vec_IntPushTwo( vPairs, iConst, iMono );
    }
    // sort triples
142
    qsort( Vec_IntArray(vPairs), (size_t)(Vec_IntSize(vPairs)/4), 16, (int (*)(const void *, const void *))Pln_ManCompare3 );
143 144 145 146 147
    // print
    if ( fVerbose )
    Vec_IntForEachEntryDouble( vPairs, iConst, iMono, i )
    {
        if ( i % 4 == 0 )
148
            continue;
149
        printf( "%-6d : ", i/4 );
150 151
        vArray = Hsh_VecReadEntry( p->pHashC, iConst );
        Vec_IntForEachEntry( vArray, Entry, k )
152
            printf( "%s%d", Entry < 0 ? "-" : "+", (1 << (Abc_AbsInt(Entry)-1)) );
153 154 155 156 157
        vArray = Hsh_VecReadEntry( p->pHashM, iMono );
        Vec_IntForEachEntry( vArray, Entry, k )
            printf( " * %d", Entry );
        printf( "\n" );
    }
158 159
    printf( "HashC = %d. HashM = %d.  Total = %d. Used = %d.  ", Hsh_VecSize(p->pHashC), Hsh_VecSize(p->pHashM), p->nBuilds, Vec_IntSize(vPairs)/4 );
    Vec_IntFree( vPairs );
160 161 162 163 164 165 166 167 168 169 170 171 172
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
static inline void Vec_IntAppendMinus2x( Vec_Int_t * vVec1, Vec_Int_t * vVec2 )
{
    int Entry, i;
    Vec_IntClear( vVec1 );
    Vec_IntForEachEntry( vVec2, Entry, i )
        Vec_IntPush( vVec1, Entry > 0 ? -Entry-1 : -Entry+1 );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
static inline void Gia_PolynMergeConstOne( Vec_Int_t * vConst, int New )
{
    int i, Old;
    assert( New != 0 );
    Vec_IntForEachEntry( vConst, Old, i )
    {
        assert( Old != 0 );
        if ( Old == New ) // A == B
        {
            Vec_IntDrop( vConst, i );
            Gia_PolynMergeConstOne( vConst, New > 0 ? New + 1 : New - 1 );
            return;
        }
        if ( Abc_AbsInt(Old) == Abc_AbsInt(New) ) // A == -B
        {
            Vec_IntDrop( vConst, i );
            return;
        }
        if ( Old + New == 1 || Old + New == -1 )  // sign(A) != sign(B)  &&  abs(abs(A)-abs(B)) == 1 
        {
            int Value = Abc_MinInt( Abc_AbsInt(Old), Abc_AbsInt(New) );
            Vec_IntDrop( vConst, i );
            Gia_PolynMergeConstOne( vConst, (Old + New == 1) ? Value : -Value );
            return;
        }
    }
    Vec_IntPushUniqueOrder( vConst, New );
}
static inline void Gia_PolynMergeConst( Vec_Int_t * vConst, Pln_Man_t * p, int iConstAdd )
{
    int i, New;
    Vec_Int_t * vConstAdd = Hsh_VecReadEntry( p->pHashC, iConstAdd );
    Vec_IntForEachEntry( vConstAdd, New, i )
    {
        Gia_PolynMergeConstOne( vConst, New );
        vConstAdd = Hsh_VecReadEntry( p->pHashC, iConstAdd );
    }
}
static inline void Gia_PolynBuildAdd( Pln_Man_t * p, Vec_Int_t * vTempC, Vec_Int_t * vTempM )
{
232
    int iConst, iConstNew, iMono = vTempM ? Hsh_VecManAdd(p->pHashM, vTempM) : 0;
233 234 235 236 237
    p->nBuilds++;
    if ( iMono == Vec_IntSize(p->vCoefs) ) // new monomial
    {
        iConst = Hsh_VecManAdd( p->pHashC, vTempC );
        Vec_IntPush( p->vCoefs, iConst );
238 239
//        Vec_FltPush( p->vCounts, Vec_IntEntryLast(vTempM) );
        Vec_FltPush( p->vCounts, (float)Vec_IntEntry(p->vOrder, Vec_IntEntryLast(vTempM)) );
240 241
        Vec_QuePush( p->vQue, iMono );
//        Vec_QueUpdate( p->vQue, iMono );
242 243
        if ( iConst )
            p->nUsed++;
244 245 246 247 248 249
        return;
    }
    // this monomial exists
    iConst = Vec_IntEntry( p->vCoefs, iMono );
    if ( iConst )
        Gia_PolynMergeConst( vTempC, p, iConst );
250 251 252 253 254 255 256
    iConstNew = Hsh_VecManAdd( p->pHashC, vTempC );
    Vec_IntWriteEntry( p->vCoefs, iMono, iConstNew );
    if ( iConst && !iConstNew )
        p->nUsed--;
    else if ( !iConst && iConstNew )
        p->nUsed++;
    //assert( p->nUsed == Vec_IntSize(p->vCoefs) - Vec_IntCountZero(p->vCoefs) );
257 258 259 260 261 262 263 264 265 266 267 268 269 270
}
void Gia_PolynBuildOne( Pln_Man_t * p, int iMono )
{
    Gia_Obj_t * pObj; 
    Vec_Int_t * vArray, * vConst;
    int iFan0, iFan1;
    int k, iConst, iDriver;

    assert( Vec_IntSize(p->vCoefs) == Hsh_VecSize(p->pHashM) );
    vArray  = Hsh_VecReadEntry( p->pHashM, iMono );
    iDriver = Vec_IntEntryLast( vArray );
    pObj    = Gia_ManObj( p->pGia, iDriver );
    if ( !Gia_ObjIsAnd(pObj) )
        return;
271
    assert( !Gia_ObjIsMux(p->pGia, pObj) );
272 273 274 275 276

    iConst = Vec_IntEntry( p->vCoefs, iMono );
    if ( iConst == 0 )
        return;
    Vec_IntWriteEntry( p->vCoefs, iMono, 0 );
277
    p->nUsed--;
278 279 280 281 282 283 284 285 286

    iFan0 = Gia_ObjFaninId0p(p->pGia, pObj);
    iFan1 = Gia_ObjFaninId1p(p->pGia, pObj);
    for ( k = 0; k < 4; k++ )
    {
        Vec_IntClear( p->vTempM[k] );
        Vec_IntAppend( p->vTempM[k], vArray );
        Vec_IntPop( p->vTempM[k] );
        if ( k == 1 || k == 3 )
287 288
            Vec_IntPushUniqueOrderCost( p->vTempM[k], iFan0, p->vOrder );    // x
//            Vec_IntPushUniqueOrder( p->vTempM[k], iFan0 );    // x
289
        if ( k == 2 || k == 3 )
290 291
            Vec_IntPushUniqueOrderCost( p->vTempM[k], iFan1, p->vOrder );    // y
//            Vec_IntPushUniqueOrder( p->vTempM[k], iFan1 );    // y
292 293 294 295
    }

    vConst = Hsh_VecReadEntry( p->pHashC, iConst );

296 297 298 299 300 301 302 303 304 305 306 307 308 309
    if ( !Gia_ObjIsXor(pObj) )
        for ( k = 0; k < 2; k++ )
            Vec_IntAppendMinus( p->vTempC[k], vConst, k );

    if ( Gia_ObjIsXor(pObj) )
    {
        vConst = Hsh_VecReadEntry( p->pHashC, iConst );
        Vec_IntAppendMinus( p->vTempC[0], vConst, 0 );
        Gia_PolynBuildAdd( p, p->vTempC[0], p->vTempM[1] );   //  C * x 

        vConst = Hsh_VecReadEntry( p->pHashC, iConst );
        Vec_IntAppendMinus( p->vTempC[0], vConst, 0 );
        Gia_PolynBuildAdd( p, p->vTempC[0], p->vTempM[2] );   //  C * y 

310 311
        //if ( !p->pGia->vXors || Vec_IntFind(p->pGia->vXors, iDriver) == -1 || Vec_IntFind(p->pGia->vXors, iDriver) == 5 )
        { 
312 313 314 315 316 317
            vConst = Hsh_VecReadEntry( p->pHashC, iConst );
            Vec_IntAppendMinus2x( p->vTempC[0], vConst );
            Gia_PolynBuildAdd( p, p->vTempC[0], p->vTempM[3] );   // -C * x * y 
        }
    }
    else if ( Gia_ObjFaninC0(pObj) && Gia_ObjFaninC1(pObj) )  //  C * (1 - x) * (1 - y)
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
    {
        Gia_PolynBuildAdd( p, p->vTempC[0], p->vTempM[0] );   //  C * 1
        Gia_PolynBuildAdd( p, p->vTempC[1], p->vTempM[1] );   // -C * x
        vConst = Hsh_VecReadEntry( p->pHashC, iConst );
        for ( k = 0; k < 2; k++ )
            Vec_IntAppendMinus( p->vTempC[k], vConst, k );
        Gia_PolynBuildAdd( p, p->vTempC[1], p->vTempM[2] );   // -C * y 
        Gia_PolynBuildAdd( p, p->vTempC[0], p->vTempM[3] );   //  C * x * y
    }
    else if ( Gia_ObjFaninC0(pObj) && !Gia_ObjFaninC1(pObj) ) //  C * (1 - x) * y
    {
        Gia_PolynBuildAdd( p, p->vTempC[0], p->vTempM[2] );   //  C * y 
        Gia_PolynBuildAdd( p, p->vTempC[1], p->vTempM[3] );   // -C * x * y
    }
    else if ( !Gia_ObjFaninC0(pObj) && Gia_ObjFaninC1(pObj) ) //  C * x * (1 - y)
    {
        Gia_PolynBuildAdd( p, p->vTempC[0], p->vTempM[1] );   //  C * x 
        Gia_PolynBuildAdd( p, p->vTempC[1], p->vTempM[3] );   // -C * x * y
    }
    else   
        Gia_PolynBuildAdd( p, p->vTempC[0], p->vTempM[3] );   //  C * x * y
}
340
void Gia_PolynBuild( Gia_Man_t * pGia, Vec_Int_t * vOrder, int fSigned, int fVerbose, int fVeryVerbose )
341 342 343
{
    abctime clk = Abc_Clock();//, clk2 = 0;
    Gia_Obj_t * pObj; 
344 345 346
    Vec_Bit_t * vPres = Vec_BitStart( Gia_ManObjNum(pGia) );
    int i, iMono, iDriver, LevPrev, LevCur, Iter, Line = 0;
    Pln_Man_t * p = Pln_ManAlloc( pGia, vOrder );
347 348 349 350 351 352 353 354
    Gia_ManForEachCoReverse( pGia, pObj, i )
    {
        Vec_IntFill( p->vTempC[0], 1,  i+1 );      //  2^i
        Vec_IntFill( p->vTempC[1], 1, -i-1 );      // -2^i

        iDriver = Gia_ObjFaninId0p( pGia, pObj );
        Vec_IntFill( p->vTempM[0], 1, iDriver );   //  Driver

355
        if ( fSigned && i == Gia_ManCoNum(pGia)-1 )
356
        {
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
            if ( Gia_ObjFaninC0(pObj) )
            {
                Gia_PolynBuildAdd( p, p->vTempC[1], NULL );           // -C
                Gia_PolynBuildAdd( p, p->vTempC[0], p->vTempM[0] );   //  C * Driver
            }
            else
                Gia_PolynBuildAdd( p, p->vTempC[1], p->vTempM[0] );   // -C * Driver
        }
        else 
        {
            if ( Gia_ObjFaninC0(pObj) )
            {
                Gia_PolynBuildAdd( p, p->vTempC[0], NULL );           //  C
                Gia_PolynBuildAdd( p, p->vTempC[1], p->vTempM[0] );   // -C * Driver
            }
            else
                Gia_PolynBuildAdd( p, p->vTempC[0], p->vTempM[0] );   //  C * Driver
374 375
        }
    }
376 377
    LevPrev = -1;
    for ( Iter = 0; ; Iter++ )
378
    {
379
        Vec_Int_t * vTempM;
380
        //abctime temp = Abc_Clock();
381
        if ( Vec_QueSize(p->vQue) == 0 )
382
            break;
383
        iMono = Vec_QuePop(p->vQue);
384 385 386 387

        // report
        vTempM = Hsh_VecReadEntry( p->pHashM, iMono );
        //printf( "Removing var %d\n", Vec_IntEntryLast(vTempM) );
388 389 390
        LevCur = Vec_IntEntryLast(vTempM);
        if ( !Gia_ObjIsAnd(Gia_ManObj(pGia, LevCur)) )
            continue;
391 392 393

        if ( LevPrev != LevCur )
        {
394 395
            if ( Vec_BitEntry( vPres, LevCur ) && fVerbose )
                printf( "Repeating entry %d\n", LevCur );
396
            else
397
                Vec_BitSetEntry( vPres, LevCur, 1 );
398

399
            if ( fVeryVerbose )
400 401
                printf( "Line%5d   Iter%10d : Obj =%6d.  Order =%6d.  HashC =%6d. HashM =%10d.  Total =%10d. Used =%10d.\n", 
                    Line++, Iter, LevCur, Vec_IntEntry(p->vOrder, LevCur), Hsh_VecSize(p->pHashC), Hsh_VecSize(p->pHashM), p->nBuilds, p->nUsed );
402 403 404
        }
        LevPrev = LevCur;

405 406 407 408
        Gia_PolynBuildOne( p, iMono );
        //clk2 += Abc_Clock() - temp;
    }
    //Abc_PrintTime( 1, "Time2", clk2 );
409
    Pln_ManPrintFinal( p, fVerbose, fVeryVerbose );
410
    Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
411
    Pln_ManStop( p );
412
    Vec_BitFree( vPres );
413 414 415
}


416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_PolynBuild2( Gia_Man_t * pGia, int fSigned, int fVerbose, int fVeryVerbose )
{
    Hsh_VecMan_t * pHashC = Hsh_VecManStart( 1000 );    // hash table for constants
    Hsh_VecMan_t * pHashM = Hsh_VecManStart( 1000 );    // hash table for monomials
Alan Mishchenko committed
432
    //Vec_Wec_t * vLit2Mono = Vec_WecStart( Gia_ManObjNum(pGia) * 2 );
433 434 435 436 437 438

    Hsh_VecManStop( pHashC );
    Hsh_VecManStop( pHashM );
}


439 440 441 442 443 444 445
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


ABC_NAMESPACE_IMPL_END