dauArray.c 6.87 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 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 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 210 211 212 213 214 215 216 217 218 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
/**CFile****************************************************************

  FileName    [dauArray.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [DAG-aware unmapping.]

  Synopsis    [Array representation of DSD.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

#include "dauInt.h"

ABC_NAMESPACE_IMPL_START

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

typedef struct Dau_Dsd_t_ Dau_Dsd_t;
struct Dau_Dsd_t_
{
    unsigned       iVar    :   5;  // variable
    unsigned       nFans   :   5;  // fanin count
    unsigned       Depth   :   5;  // tree depth
    unsigned       Offset  :   5;  // the diff between this and other node
    unsigned       Data    :   5;  // user data
    unsigned       Type    :   3;  // node type
    unsigned       fCompl  :   1;  // the complemented attribute
    unsigned       fUnused :   1;  // this vertice is unused
};

static inline void Dau_DsdClean( Dau_Dsd_t * p ) { *((int *)p) = 0; }

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////
 
int Dau_DsdCountAnd( Dau_Dsd_t * p )
{
    int Count, Costs[7] = {0, 0, 0, 1, 3, 3, 3};
    for ( Count = 0; p->Type; p++ )
        Count += Costs[p->Type];
    return Count;
}

/*
void Dau_DsdMark( Dau_Dsd_t * p, int nSize, int * pMarks )
{
    int pStore[DAU_MAX_VAR] = {0};
    Dau_Dsd_t * q;
    if ( p->Type == DAU_DSD_CONST || p->Type == DAU_DSD_VAR )
        return;
    for ( q = p + nSize - 1; q >= p; q-- )
    {
        if ( q->Type == DAU_DSD_VAR )
            pStore[q->Depth] += pMarks[q->iVar];
        else 
        {
            q->Data = pStore[q->Depth+1]; pStore[q->Depth+1] = 0;
            pStore[q->Depth] += (q->Data == q->nFans);            
        }
    }
}
*/

int Dau_DsdConstruct( char * pDsd, Dau_Dsd_t * pStore )
{
    Dau_Dsd_t * pLevel[DAU_MAX_VAR];
    Dau_Dsd_t * q = pStore;
    int d = -1, fCompl = 0;
    if ( Dau_DsdIsConst(pDsd) )
    {
        Dau_DsdClean( q );
        q->Type   = DAU_DSD_CONST0;
        q->fCompl = Dau_DsdIsConst1(pDsd);
        return 1;
    }
    for ( --q; *pDsd; pDsd++ )
    {
        if ( *pDsd == '!' )
        {
            fCompl ^= 1;
            continue;
        }
        if ( *pDsd == ')' || *pDsd == ']' || *pDsd == '>' || *pDsd == '}' )
        {
            assert( fCompl == 0 );
            if ( --d >= 0 )
            {
                pLevel[d]->nFans++;
                if ( pLevel[d]->Data > pLevel[d+1]->Data )
                    pLevel[d]->Data = pLevel[d+1]->Data;
            }
            continue;
        }
        Dau_DsdClean( ++q );
        q->Data = 31;
        q->fCompl = fCompl;
        fCompl = 0;
        if ( *pDsd >= 'a' && *pDsd <= 'z' )
        {
            q->Type   = DAU_DSD_VAR;
            q->iVar   = *pDsd - 'a';
            q->Depth  = d + 1;
            if ( d >= 0 )
            {
                pLevel[d]->nFans++;
                if ( pLevel[d]->Data > q->iVar )
                    pLevel[d]->Data = q->iVar;
            }
            continue;
        }
        if ( *pDsd == '(' )
            q->Type = DAU_DSD_AND;
        else if ( *pDsd == '[' )
            q->Type = DAU_DSD_XOR;
        else if ( *pDsd == '<' )
            q->Type = DAU_DSD_MUX;
        else if ( *pDsd == '{' )
            q->Type = DAU_DSD_PRIME;
        else assert( 0 );
        pLevel[++d] = q;
        q->Depth = d;
    }
    assert( d == -1 );
    Dau_DsdClean( ++q );
    return q - pStore;
}

void Dau_DsdPrint( Dau_Dsd_t * p )
{
    char OpenType[7]  = {0, 0, 0, '(', '[', '<', '{'};
    char CloseType[7] = {0, 0, 0, ')', ']', '>', '}'};
    char pTypes[DAU_MAX_VAR];
    int d, pVisits[DAU_MAX_VAR];
    if ( p->Type == DAU_DSD_CONST0 )
    {
        printf( "%d\n", p->fCompl );
        return;
    }
    pVisits[0] = 1;
    for ( d = 0; p->Type; p++ )
    {
        if ( p->fCompl )
            printf( "!" );
        if ( p->Type == DAU_DSD_VAR )
        {
            printf( "%c", 'a' + p->iVar );
            while ( d > 0 && --pVisits[d] == 0 )
                printf( "%c", pTypes[d--] );
        }
        else
        {
            pVisits[++d] = p->nFans;
            printf( "%c", OpenType[p->Type] );
            printf( "%c", 'a' + p->Data );
            printf( "%d", p->Depth );
            pTypes[d]  = CloseType[p->Type];
        }
    }
    assert( d == 0 );
    printf( "\n" );
}

void Dau_DsdDepth( Dau_Dsd_t * p )
{
    int d, pVisits[DAU_MAX_VAR];
    if ( p->Type == DAU_DSD_CONST0 )
        return;
    pVisits[0] = 1;
    for ( d = 0; p->Type; p++ )
    {
        p->Depth = d;
        if ( p->Type == DAU_DSD_VAR )
            while ( d > 0 && --pVisits[d] == 0 )
                d--;
        else
            pVisits[++d] = p->nFans;
    }
    assert( d == 0 );
}

void Dau_DsdRemoveUseless( Dau_Dsd_t * p )
{
    Dau_Dsd_t * q = p, * pLevel[DAU_MAX_VAR];
    int d, fChange = 0, pVisits[DAU_MAX_VAR];
    if ( p->Type == DAU_DSD_CONST0 )
        return;
    pVisits[0] = 1;
    for ( d = 0; p->Type; p++ )
    {
        p->Depth = d;
        if ( p->Type == DAU_DSD_VAR )
            while ( d > 0 && --pVisits[d] == 0 )
                d--;
        else
        {
            if ( d > 0 && (pLevel[d-1]->Type == DAU_DSD_XOR && p->Type == DAU_DSD_XOR ||
                           pLevel[d-1]->Type == DAU_DSD_AND && p->Type == DAU_DSD_AND && !p->fCompl) )
            {
                pLevel[d-1]->nFans += p->nFans - 1;
                pVisits[d] += p->nFans - 1;
                p->fUnused = 1;
                fChange = 1;
            }
            else
            {
                pLevel[d++] = p;
                pVisits[d] = p->nFans;
            }
        }
    }
    assert( d == 0 );
    // compact
    if ( fChange )
    {
        for ( p = q; p->Type; p++ )
            if ( !p->fUnused )
                *q++ = *p;
        Dau_DsdClean( q );
    }
}

void Dau_DsdTest22()
{
    Dau_Dsd_t pStore[2 * DAU_MAX_VAR];
//    char * pDsd = "[(ab)c(f!(he))]";
//    char * pDsd = "[(abd)cf(f!{she})]";
    char * pDsd = "[(abd)[cf](f(sg(he)))]";
//    char * pDsd = "[(ab)[cf]]";
    int i, nSize = Dau_DsdConstruct( pDsd, pStore );
//    Dau_DsdDepth( pStore );
    Dau_DsdPrint( pStore );

    Dau_DsdRemoveUseless( pStore );

    Dau_DsdPrint( pStore );
    i = 0;
}

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


ABC_NAMESPACE_IMPL_END