ifDec75.c 10.5 KB
Newer Older
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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
/**CFile****************************************************************

  FileName    [ifDec75.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [FPGA mapping based on priority cuts.]

  Synopsis    [Performs additional check.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - November 21, 2006.]

  Revision    [$Id: ifDec75.c,v 1.00 2006/11/21 00:00:00 alanmi Exp $]

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

#include "if.h"
#include "misc/extra/extra.h"
#include "bool/kit/kit.h"
#include "opt/dau/dau.h"

ABC_NAMESPACE_IMPL_START


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

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

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

  Synopsis    [Finds all boundsets for which decomposition exists.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Dau_DsdCheckDecExist_rec( char * pStr, char ** p, int * pMatches, int * pnSupp )
{
    if ( **p == '!' )
        (*p)++;
    while ( (**p >= 'A' && **p <= 'F') || (**p >= '0' && **p <= '9') )
        (*p)++;
    if ( **p >= 'a' && **p <= 'z' ) // var
    {
        (*pnSupp)++;
        return 0;
    }
    if ( **p == '(' || **p == '[' ) // and/xor
    {
        unsigned Mask = 0;
        int m, pSupps[8] = {0}, nParts = 0, nMints;
        char * q = pStr + pMatches[ *p - pStr ];
        assert( *q == **p + 1 + (**p != '(') );
        for ( (*p)++; *p < q; (*p)++ )
        {
            Mask |= Dau_DsdCheckDecExist_rec( pStr, p, pMatches, &pSupps[nParts] );
            *pnSupp += pSupps[nParts++];
        }
        assert( *p == q );
        assert( nParts > 1 );
        nMints = (1 << nParts);
        for ( m = 1; m < nMints; m++ )
        {
            int i, Sum = 0;
            for ( i = 0; i < nParts; i++ )
                if ( (m >> i) & 1 )
                    Sum += pSupps[i];
            assert( Sum > 0 && Sum <= 8 );
            if ( Sum >= 2 )
                Mask |= (1 << Sum);
        }
        return Mask;
    }
    if ( **p == '<' || **p == '{' ) // mux
    {
        int uSupp;
        unsigned Mask = 0;
        char * q = pStr + pMatches[ *p - pStr ];
        assert( *q == **p + 1 + (**p != '(') );
        for ( (*p)++; *p < q; (*p)++ )
        {
            uSupp = 0;
            Mask |= Dau_DsdCheckDecExist_rec( pStr, p, pMatches, &uSupp );
            *pnSupp += uSupp;
        }
        assert( *p == q );
        Mask |= (1 << *pnSupp);
        return Mask;
    }
    assert( 0 );
    return 0;
}
int Dau_DsdCheckDecExist( char * pDsd )
{
    int nSupp = 0;
    if ( pDsd[1] == 0 )
        return 0;
    return Dau_DsdCheckDecExist_rec( pDsd, &pDsd, Dau_DsdComputeMatches(pDsd), &nSupp );
}

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

  Synopsis    [Finds all boundsets for which AND-decomposition exists.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Dau_DsdCheckDecAndExist_rec( char * pStr, char ** p, int * pMatches, int * pnSupp )
{
    if ( **p == '!' )
        (*p)++;
    while ( (**p >= 'A' && **p <= 'F') || (**p >= '0' && **p <= '9') )
        (*p)++;
    if ( **p >= 'a' && **p <= 'z' ) // var
    {
        (*pnSupp)++;
        return 0;
    }
    if ( **p == '(' ) // and
    {
        unsigned Mask = 0;
        int m, i, pSupps[8] = {0}, nParts = 0, nSimple = 0, nMints;
        char * q = pStr + pMatches[ *p - pStr ];
        assert( *q == **p + 1 + (**p != '(') );
        for ( (*p)++; *p < q; (*p)++ )
        {
            Mask |= Dau_DsdCheckDecAndExist_rec( pStr, p, pMatches, &pSupps[nParts] );
            nSimple += (pSupps[nParts] == 1);
            *pnSupp += pSupps[nParts++];
        }
        assert( *p == q );
        assert( nParts > 1 );
        if ( nSimple > 0 )
        {
            nMints = (1 << nParts);
            for ( m = 1; m < nMints; m++ )
            {
                int Sum = 0;
                for ( i = 0; i < nParts; i++ )
                    if ( pSupps[i] > 1 && ((m >> i) & 1) )
                        Sum += pSupps[i];
                assert( Sum <= 8 );
                if ( Sum >= 2 )
                    for ( i = 0; i < nSimple; i++ )
                        Mask |= (1 << (Sum + i));
            }
            for ( i = 2; i < nSimple; i++ )
                Mask |= (1 << i);
        }
        return Mask;
    }
    if ( **p == '<' || **p == '{' || **p == '[' ) // mux/xor/nondec
    {
        int uSupp;
        unsigned Mask = 0;
        char * q = pStr + pMatches[ *p - pStr ];
        assert( *q == **p + 1 + (**p != '(') );
        for ( (*p)++; *p < q; (*p)++ )
        {
            uSupp = 0;
            Mask |= Dau_DsdCheckDecAndExist_rec( pStr, p, pMatches, &uSupp );
            *pnSupp += uSupp;
        }
        assert( *p == q );
        return Mask;
    }
    assert( 0 );
    return 0;
}
int Dau_DsdCheckDecAndExist( char * pDsd )
{
    int nSupp = 0;
    if ( pDsd[1] == 0 )
        return 1;
    return Dau_DsdCheckDecAndExist_rec( pDsd, &pDsd, Dau_DsdComputeMatches(pDsd), &nSupp );
}

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

  Synopsis    [Performs additional check.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int If_CutPerformCheck75__( If_Man_t * p, unsigned * pTruth, int nVars, int nLeaves, char * pStr )
{
    char pDsdStr[1000];
    int nSizeNonDec, nDecExists, nDecAndExists;
    static int Counter = 0;
    Counter++;
    if ( nLeaves < 6 )
        return 1;
    assert( nLeaves <= 8 );
    if ( nLeaves < 8 && If_CutPerformCheck16( p, pTruth, nVars, nLeaves, "44" ) )
        return 1;
    // check decomposability
    nSizeNonDec = Dau_DsdDecompose( (word *)pTruth, nLeaves, 0, 0, pDsdStr );
//    printf( "Vars = %d  %s", nLeaves, pDsdStr );    printf( "\n" );
//    Extra_PrintBinary( stdout, &nDecExists, 8 );    printf( "\n" );
//    Extra_PrintBinary( stdout, &nDecAndExists, 8 ); printf( "\n" );
    if ( nLeaves == 8 )
    {
        if ( nSizeNonDec >= 5 )
            return 0;
        nDecAndExists = Dau_DsdCheckDecAndExist( pDsdStr );
        if ( nDecAndExists & 0x10 ) // bit 4
            return 1;
        else
            return 0;
    }
    if ( nLeaves == 7 )
    {
        extern void If_Dec7MinimumBase( word uTruth[2], int * pSupp, int nVarsAll, int * pnVars );
        word * pT = (word *)pTruth;
        word pCof0[2], pCof1[2];
        int v, nVarsMin;
        if ( nSizeNonDec < 5 )
        {
            nDecExists = Dau_DsdCheckDecExist( pDsdStr );
            if ( nDecExists & 0x10 ) // bit 4
                return 1;
            nDecAndExists = Dau_DsdCheckDecAndExist( pDsdStr );
            if ( nDecAndExists & 0x18 ) // bit 4, 3
                return 1;
        }
        // check cofactors
        for ( v = 0; v < 7; v++ )
        {
            pCof0[0] = pCof1[0] = pT[0];
            pCof0[1] = pCof1[1] = pT[1];
            Abc_TtCofactor0( pCof0, 2, v );
            Abc_TtCofactor1( pCof1, 2, v );
            if ( Abc_TtSupportSize(pCof0, 7) < 4 )
            {
                If_Dec7MinimumBase( pCof1, NULL, 7, &nVarsMin );
                nSizeNonDec = Dau_DsdDecompose( pCof1, nVarsMin, 0, 0, pDsdStr );
                if ( nSizeNonDec >= 5 )
                    continue;
                nDecExists = Dau_DsdCheckDecExist( pDsdStr );
                if ( nDecExists & 0x18 ) // bit 4, 3
                    return 1;
            }
            else if ( Abc_TtSupportSize(pCof1, 7) < 4 )
            {
                If_Dec7MinimumBase( pCof0, NULL, 7, &nVarsMin );
                nSizeNonDec = Dau_DsdDecompose( pCof0, nVarsMin, 0, 0, pDsdStr );
                if ( nSizeNonDec >= 5 )
                    continue;
                nDecExists = Dau_DsdCheckDecExist( pDsdStr );
                if ( nDecExists & 0x18 ) // bit 4, 3
                    return 1;
            }
        }
        return 0;
    }
    if ( nLeaves == 6 )
    {
        if ( nSizeNonDec < 5 )
        {
            nDecExists = Dau_DsdCheckDecExist( pDsdStr );
            if ( nDecExists & 0x18 ) // bit 4, 3
                return 1;
            nDecAndExists = Dau_DsdCheckDecAndExist( pDsdStr );
            if ( nDecAndExists & 0x1C ) // bit 4, 3, 2
                return 1;
        }
        return If_CutPerformCheck07( p, pTruth, nVars, nLeaves, pStr );
    }
    assert( 0 );
    return 0;
}

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

  Synopsis    [Performs additional check.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int If_CutPerformCheck75( If_Man_t * p, unsigned * pTruth0, int nVars, int nLeaves, char * pStr )
{
    word * pTruthW = (word *)pTruth0;
    word pTruth[4] = { pTruthW[0], pTruthW[1], pTruthW[2], pTruthW[3] };
    assert( nLeaves <= 8 );
308 309
    if ( !p->pPars->fCutMin )
        Abc_TtMinimumBase( pTruth, NULL, nLeaves, &nLeaves );
310 311 312 313 314
    if ( nLeaves < 6 )
        return 1;
//    if ( nLeaves < 8 && If_CutPerformCheck07( p, (unsigned *)pTruth, nVars, nLeaves, "44" ) )
    if ( nLeaves < 8 && If_CutPerformCheck16( p, (unsigned *)pTruth, nVars, nLeaves, "44" ) )
        return 1;
315 316
    // this is faster but not compatible with -z
    if ( !p->pPars->fDeriveLuts && p->pPars->fEnableCheck75 && nLeaves == 8 )
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
    {
//        char pDsdStr[1000] = "(!(abd)!(c!([fe][gh])))";
        char pDsdStr[1000];
        int nSizeNonDec = Dau_DsdDecompose( (word *)pTruth, nLeaves, 0, 0, pDsdStr );
        if ( nSizeNonDec >= 5 )
            return 0;
        if ( Dau_DsdCheckDecAndExist(pDsdStr) & 0x10 ) // bit 4
            return 1;
        return 0;
    }
    if ( If_CutPerformCheck45( p, (unsigned *)pTruth, nVars, nLeaves, pStr ) )
        return 1;
    if ( If_CutPerformCheck54( p, (unsigned *)pTruth, nVars, nLeaves, pStr ) )
        return 1;
    return 0;
}


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


ABC_NAMESPACE_IMPL_END