luckySwap.c 12.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/**CFile****************************************************************

  FileName    [luckySwap.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Semi-canonical form computation package.]

  Synopsis    [Swapping variables in the truth table.]

  Author      [Jake]

  Date        [Started - August 2012]

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

#include "luckyInt.h"

19

20 21 22
ABC_NAMESPACE_IMPL_START


23
static word mask0[6] = { ABC_CONST(0x5555555555555555),ABC_CONST(0x3333333333333333), ABC_CONST(0x0F0F0F0F0F0F0F0F),ABC_CONST(0x00FF00FF00FF00FF),ABC_CONST(0x0000FFFF0000FFFF), ABC_CONST(0x00000000FFFFFFFF)};  
Alan Mishchenko committed
24 25 26 27 28 29 30 31 32 33 34 35
/*
static word mask1[6] = { 0xAAAAAAAAAAAAAAAA,0xCCCCCCCCCCCCCCCC, 0xF0F0F0F0F0F0F0F0,0xFF00FF00FF00FF00,0xFFFF0000FFFF0000, 0xFFFFFFFF00000000 };
static word mask[6][2] =   {
    {0x5555555555555555,0xAAAAAAAAAAAAAAAA},
    {0x3333333333333333,0xCCCCCCCCCCCCCCCC},
    {0x0F0F0F0F0F0F0F0F,0xF0F0F0F0F0F0F0F0},
    {0x00FF00FF00FF00FF,0xFF00FF00FF00FF00},
    {0x0000FFFF0000FFFF,0xFFFF0000FFFF0000},
    {0x00000000FFFFFFFF,0xFFFFFFFF00000000}
};
*/

36
int Kit_TruthWordNum_64bit( int nVars )  { return nVars <= 6 ? 1 : (1 << (nVars - 6));}
37

38
int Kit_WordCountOnes_64bit(word x)
39
{
40 41 42
    x = x - ((x >> 1) & ABC_CONST(0x5555555555555555));   
    x = (x & ABC_CONST(0x3333333333333333)) + ((x >> 2) & ABC_CONST(0x3333333333333333));    
    x = (x + (x >> 4)) & ABC_CONST(0x0F0F0F0F0F0F0F0F);    
43 44 45 46 47 48
    x = x + (x >> 8);
    x = x + (x >> 16);
    x = x + (x >> 32); 
    return (int)(x & 0xFF);
}

49
int Kit_TruthCountOnes_64bit( word* pIn, int nVars )
50 51 52 53 54 55 56
{
    int w, Counter = 0;
    for ( w = Kit_TruthWordNum_64bit(nVars)-1; w >= 0; w-- )
        Counter += Kit_WordCountOnes_64bit(pIn[w]);
    return Counter;
}

57
void Kit_TruthCountOnesInCofs_64bit( word * pTruth, int nVars, int * pStore )
58
{    
59 60 61 62 63 64
    int nWords = Kit_TruthWordNum_64bit( nVars );
    int i, k, Counter;
    memset( pStore, 0, sizeof(int) * nVars );
    if ( nVars <= 6 )
    {
        if ( nVars > 0 )        
65
            pStore[0] = Kit_WordCountOnes_64bit( pTruth[0] & ABC_CONST(0x5555555555555555) );  
66
        if ( nVars > 1 )       
67
            pStore[1] = Kit_WordCountOnes_64bit( pTruth[0] & ABC_CONST(0x3333333333333333) );     
68
        if ( nVars > 2 )       
69
            pStore[2] = Kit_WordCountOnes_64bit( pTruth[0] & ABC_CONST(0x0F0F0F0F0F0F0F0F) );   
70
        if ( nVars > 3 )       
71
            pStore[3] = Kit_WordCountOnes_64bit( pTruth[0] & ABC_CONST(0x00FF00FF00FF00FF) );     
72
        if ( nVars > 4 )       
73
            pStore[4] = Kit_WordCountOnes_64bit( pTruth[0] & ABC_CONST(0x0000FFFF0000FFFF) ); 
74
        if ( nVars > 5 )       
75
            pStore[5] = Kit_WordCountOnes_64bit( pTruth[0] & ABC_CONST(0x00000000FFFFFFFF) );      
76 77 78 79 80 81 82 83 84 85 86 87 88 89
        return;
    }
    // nVars > 6
    // count 1's for all other variables
    for ( k = 0; k < nWords; k++ )
    {
        Counter = Kit_WordCountOnes_64bit( pTruth[k] );
        for ( i = 6; i < nVars; i++ )
            if ( (k & (1 << (i-6))) == 0)
                pStore[i] += Counter;
    }
    // count 1's for the first six variables
    for ( k = nWords/2; k>0; k-- )
    {
90 91 92 93 94 95
        pStore[0] += Kit_WordCountOnes_64bit( (pTruth[0] & ABC_CONST(0x5555555555555555)) | ((pTruth[1] & ABC_CONST(0x5555555555555555)) <<  1) );
        pStore[1] += Kit_WordCountOnes_64bit( (pTruth[0] & ABC_CONST(0x3333333333333333)) | ((pTruth[1] & ABC_CONST(0x3333333333333333)) <<  2) );
        pStore[2] += Kit_WordCountOnes_64bit( (pTruth[0] & ABC_CONST(0x0F0F0F0F0F0F0F0F)) | ((pTruth[1] & ABC_CONST(0x0F0F0F0F0F0F0F0F)) <<  4) );
        pStore[3] += Kit_WordCountOnes_64bit( (pTruth[0] & ABC_CONST(0x00FF00FF00FF00FF)) | ((pTruth[1] & ABC_CONST(0x00FF00FF00FF00FF)) <<  8) );
        pStore[4] += Kit_WordCountOnes_64bit( (pTruth[0] & ABC_CONST(0x0000FFFF0000FFFF)) | ((pTruth[1] & ABC_CONST(0x0000FFFF0000FFFF)) <<  16) );
        pStore[5] += Kit_WordCountOnes_64bit( (pTruth[0] & ABC_CONST(0x00000000FFFFFFFF)) | ((pTruth[1] & ABC_CONST(0x00000000FFFFFFFF)) <<  32) );  
96 97 98 99
        pTruth += 2;
    }
}

100
void Kit_TruthChangePhase_64bit( word * pInOut, int nVars, int iVar )
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
{
    int nWords = Kit_TruthWordNum_64bit( nVars );
    int i, Step,SizeOfBlock;
    word Temp[512];
    
    assert( iVar < nVars );
    if(iVar<=5)
    {
        for ( i = 0; i < nWords; i++ )
            pInOut[i] = ((pInOut[i] & mask0[iVar]) << (1<<(iVar))) | ((pInOut[i] & ~mask0[iVar]) >> (1<<(iVar)));
    }
    else
    {
        Step = (1 << (iVar - 6));
        SizeOfBlock = sizeof(word)*Step;
        for ( i = 0; i < nWords; i += 2*Step )
117
        {   
118 119 120
            memcpy(Temp,pInOut,SizeOfBlock);
            memcpy(pInOut,pInOut+Step,SizeOfBlock);
            memcpy(pInOut+Step,Temp,SizeOfBlock);
121 122 123
            //          Temp = pInOut[i];
            //          pInOut[i] = pInOut[Step+i];
            //          pInOut[Step+i] = Temp;          
124 125 126 127 128 129
            pInOut += 2*Step;
        }
    }
    
}

130
void Kit_TruthNot_64bit(word * pIn, int nVars )
131 132 133 134 135
{
    int w;
    for ( w = Kit_TruthWordNum_64bit(nVars)-1; w >= 0; w-- )
        pIn[w] = ~pIn[w];
}
136
void Kit_TruthCopy_64bit( word * pOut, word * pIn, int nVars )
137 138 139 140
{ 
    memcpy(pOut,pIn,Kit_TruthWordNum_64bit(nVars)*sizeof(word));
}

141
void Kit_TruthSwapAdjacentVars_64bit( word * pInOut, int nVars, int iVar )
142 143 144 145
{
    int i, Step, Shift, SizeOfBlock;                   //
    word temp[256];                   // to make only pInOut possible
    static word PMasks[5][3] = {
146 147 148 149 150
        { ABC_CONST(0x9999999999999999), ABC_CONST(0x2222222222222222), ABC_CONST(0x4444444444444444) },
        { ABC_CONST(0xC3C3C3C3C3C3C3C3), ABC_CONST(0x0C0C0C0C0C0C0C0C), ABC_CONST(0x3030303030303030) },
        { ABC_CONST(0xF00FF00FF00FF00F), ABC_CONST(0x00F000F000F000F0), ABC_CONST(0x0F000F000F000F00) },
        { ABC_CONST(0xFF0000FFFF0000FF), ABC_CONST(0x0000FF000000FF00), ABC_CONST(0x00FF000000FF0000) },
        { ABC_CONST(0xFFFF00000000FFFF), ABC_CONST(0x00000000FFFF0000), ABC_CONST(0x0000FFFF00000000) }
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    };
    int nWords = Kit_TruthWordNum_64bit( nVars ); 
    
    assert( iVar < nVars - 1 );
    if ( iVar < 5 )
    {
        Shift = (1 << iVar);
        for ( i = 0; i < nWords; i++ )
            pInOut[i] = (pInOut[i] & PMasks[iVar][0]) | ((pInOut[i] & PMasks[iVar][1]) << Shift) | ((pInOut[i] & PMasks[iVar][2]) >> Shift);
    }
    else if ( iVar > 5 )
    {
        Step = 1 << (iVar - 6);
        SizeOfBlock = sizeof(word)*Step;
        pInOut += 2*Step;
        for(i=2*Step; i<nWords; i+=4*Step)
167
        {           
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
            memcpy(temp,pInOut-Step,SizeOfBlock);
            memcpy(pInOut-Step,pInOut,SizeOfBlock);
            memcpy(pInOut,temp,SizeOfBlock);
            pInOut += 4*Step;
        }
    }
    else // if ( iVar == 5 )
    {
        for ( i = 0; i < nWords; i += 2 )
        {
            temp[0] = pInOut[i+1] << 32;
            pInOut[i+1] ^= (temp[0] ^ pInOut[i]) >> 32;
            pInOut[i] = (pInOut[i] & 0x00000000FFFFFFFF) | temp[0];
            
        }
    }
}

186
unsigned  Kit_TruthSemiCanonicize_Yasha( word* pInOut, int nVars, char * pCanonPerm )
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
{
    int pStore[16];
    int nWords = Kit_TruthWordNum_64bit( nVars );
    int i, Temp, fChange, nOnes;
    unsigned  uCanonPhase=0;
    assert( nVars <= 16 );
    
    nOnes = Kit_TruthCountOnes_64bit(pInOut, nVars);
    
    if ( (nOnes > nWords * 32) )
    {
        uCanonPhase |= (1 << nVars);
        Kit_TruthNot_64bit( pInOut, nVars );
        nOnes = nWords*64 - nOnes;
    }
    
    // collect the minterm counts
    Kit_TruthCountOnesInCofs_64bit( pInOut, nVars, pStore );
    
    // canonicize phase
    for ( i = 0; i < nVars; i++ )
    {
209
        if ( pStore[i] >= nOnes-pStore[i])
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
            continue;
        uCanonPhase |= (1 << i);
        pStore[i] = nOnes-pStore[i]; 
        Kit_TruthChangePhase_64bit( pInOut, nVars, i );
    }  
    
    do {
        fChange = 0;
        for ( i = 0; i < nVars-1; i++ )
        {
            if ( pStore[i] <= pStore[i+1] )
                continue;            
            fChange = 1;
            
            Temp = pCanonPerm[i];
            pCanonPerm[i] = pCanonPerm[i+1];
            pCanonPerm[i+1] = Temp;
            
            Temp = pStore[i];
            pStore[i] = pStore[i+1];
            pStore[i+1] = Temp;
            
            // if the polarity of variables is different, swap them
            if ( ((uCanonPhase & (1 << i)) > 0) != ((uCanonPhase & (1 << (i+1))) > 0) )
            {
                uCanonPhase ^= (1 << i);
                uCanonPhase ^= (1 << (i+1));
            }
            
239
            Kit_TruthSwapAdjacentVars_64bit( pInOut, nVars, i );            
240 241 242 243
        }
    } while ( fChange );
    return uCanonPhase;
}
244

245
unsigned  Kit_TruthSemiCanonicize_Yasha1( word* pInOut, int nVars, char * pCanonPerm, int * pStore )
246 247 248 249 250 251 252 253
{
    int nWords = Kit_TruthWordNum_64bit( nVars );
    int i, fChange, nOnes;
    int Temp;
    unsigned  uCanonPhase=0;
    assert( nVars <= 16 );
    
    nOnes = Kit_TruthCountOnes_64bit(pInOut, nVars);
254 255
    if ( nOnes == nWords * 32 )
        uCanonPhase |= (1 << (nVars+2));
256
    
257
    else if ( (nOnes > nWords * 32) )
258 259 260 261 262 263 264 265 266 267 268 269
    {
        uCanonPhase |= (1 << nVars);
        Kit_TruthNot_64bit( pInOut, nVars );
        nOnes = nWords*64 - nOnes;
    }
    
    // collect the minterm counts
    Kit_TruthCountOnesInCofs_64bit( pInOut, nVars, pStore );
    
    // canonicize phase
    for ( i = 0; i < nVars; i++ )
    {
270 271 272 273 274 275
        if (  2*pStore[i]  == nOnes)
        {
            uCanonPhase |= (1 << (nVars+1));
            continue;
        }
        if ( pStore[i] > nOnes-pStore[i])
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
            continue;
        uCanonPhase |= (1 << i);
        pStore[i] = nOnes-pStore[i]; 
        Kit_TruthChangePhase_64bit( pInOut, nVars, i );
    }  
    
    do {
        fChange = 0;
        for ( i = 0; i < nVars-1; i++ )
        {
            if ( pStore[i] <= pStore[i+1] )
                continue;            
            fChange = 1;
            
            Temp = pCanonPerm[i];
            pCanonPerm[i] = pCanonPerm[i+1];
            pCanonPerm[i+1] = Temp;
            
            Temp = pStore[i];
            pStore[i] = pStore[i+1];
            pStore[i+1] = Temp;
            
            // if the polarity of variables is different, swap them
            if ( ((uCanonPhase & (1 << i)) > 0) != ((uCanonPhase & (1 << (i+1))) > 0) )
            {
                uCanonPhase ^= (1 << i);
                uCanonPhase ^= (1 << (i+1));
            }
            
305
            Kit_TruthSwapAdjacentVars_64bit( pInOut, nVars, i );            
306 307 308 309 310
        }
    } while ( fChange );
    return uCanonPhase;
}

311

312
// unsigned  Kit_TruthSemiCanonicize_Yasha_simple( word* pInOut, int nVars, char * pCanonPerm )
313 314 315 316 317
// {
//     unsigned uCanonPhase = 0;
//     int pStore[16];
//     int nWords = Kit_TruthWordNum_64bit( nVars );
//     int i, Temp, fChange, nOnes; 
318 319
//  assert( nVars <= 16 );
//  
320
//     nOnes = Kit_TruthCountOnes_64bit(pInOut, nVars);
321
//  
322 323 324
//     if ( (nOnes > nWords * 32) )
//     { 
//         Kit_TruthNot_64bit( pInOut, nVars );
325
//      nOnes = nWords*64 - nOnes;
326
//     }
327
//  
328 329
//     // collect the minterm counts
//     Kit_TruthCountOnesInCofs_64bit( pInOut, nVars, pStore );
330
//  
331 332 333 334 335 336 337 338
//     // canonicize phase
//     for ( i = 0; i < nVars; i++ )
//     {
//         if ( pStore[i] >= nOnes-pStore[i])
//             continue;        
//         pStore[i] = nOnes-pStore[i]; 
//         Kit_TruthChangePhase_64bit( pInOut, nVars, i );
//     }  
339
//  
340 341 342 343 344 345
//     do {
//         fChange = 0;
//         for ( i = 0; i < nVars-1; i++ )
//         {
//             if ( pStore[i] <= pStore[i+1] )
//                 continue;            
346 347
//             fChange = 1;         
//          
348 349 350
//             Temp = pStore[i];
//             pStore[i] = pStore[i+1];
//             pStore[i+1] = Temp;
351
//          
352 353 354 355 356 357
//             Kit_TruthSwapAdjacentVars_64bit( pInOut, nVars, i );             
//         }
//     } while ( fChange ); 
//     return uCanonPhase;
// }

358
void  Kit_TruthSemiCanonicize_Yasha_simple( word* pInOut, int nVars, int * pStore )
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
{
    int nWords = Kit_TruthWordNum_64bit( nVars );
    int i, Temp, fChange, nOnes; 
    assert( nVars <= 16 );
    
    nOnes = Kit_TruthCountOnes_64bit(pInOut, nVars);
    
    if ( (nOnes > nWords * 32) )
    { 
        Kit_TruthNot_64bit( pInOut, nVars );
        nOnes = nWords*64 - nOnes;
    }
    
    // collect the minterm counts
    Kit_TruthCountOnesInCofs_64bit( pInOut, nVars, pStore );
    
    // canonicize phase
    for ( i = 0; i < nVars; i++ )
    {
        if ( pStore[i] >= nOnes-pStore[i])
            continue;        
        pStore[i] = nOnes-pStore[i]; 
        Kit_TruthChangePhase_64bit( pInOut, nVars, i );
    }  
    
    do {
        fChange = 0;
        for ( i = 0; i < nVars-1; i++ )
        {
            if ( pStore[i] <= pStore[i+1] )
                continue;            
            fChange = 1;            
            
            Temp = pStore[i];
            pStore[i] = pStore[i+1];
            pStore[i+1] = Temp;
            
396
            Kit_TruthSwapAdjacentVars_64bit( pInOut, nVars, i );            
397 398 399 400 401 402
        }
    } while ( fChange ); 
}


ABC_NAMESPACE_IMPL_END