summaryrefslogtreecommitdiffstats
path: root/src/bool/lucky/luckySwapIJ.c
blob: 4414456420ce76232e04bc1cf529ad6c7dc49d24 (plain)
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
/**CFile****************************************************************

  FileName    [luckySwapIJ.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Semi-canonical form computation package.]

  Synopsis    [just for support of swap_ij() function]

  Author      [Jake]

  Date        [Started - September 2012]

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

#include "luckyInt.h"


ABC_NAMESPACE_IMPL_START


void swap_ij_case1( word* f,int totalVars, int i, int j)
{
    int e,wordsNumber,n,shift;
    word maskArray[45]=
    {   ABC_CONST(0x9999999999999999), ABC_CONST(0x2222222222222222), ABC_CONST(0x4444444444444444) ,ABC_CONST(0xA5A5A5A5A5A5A5A5), ABC_CONST(0x0A0A0A0A0A0A0A0A), ABC_CONST(0x5050505050505050),
        ABC_CONST(0xAA55AA55AA55AA55), ABC_CONST(0x00AA00AA00AA00AA), ABC_CONST(0x5500550055005500) ,ABC_CONST(0xAAAA5555AAAA5555), ABC_CONST(0x0000AAAA0000AAAA), ABC_CONST(0x5555000055550000),
        ABC_CONST(0xAAAAAAAA55555555), ABC_CONST(0x00000000AAAAAAAA), ABC_CONST(0x5555555500000000) ,ABC_CONST(0xC3C3C3C3C3C3C3C3), ABC_CONST(0x0C0C0C0C0C0C0C0C), ABC_CONST(0x3030303030303030),
        ABC_CONST(0xCC33CC33CC33CC33), ABC_CONST(0x00CC00CC00CC00CC), ABC_CONST(0x3300330033003300) ,ABC_CONST(0xCCCC3333CCCC3333), ABC_CONST(0x0000CCCC0000CCCC), ABC_CONST(0x3333000033330000),
        ABC_CONST(0xCCCCCCCC33333333), ABC_CONST(0x00000000CCCCCCCC), ABC_CONST(0x3333333300000000) ,ABC_CONST(0xF00FF00FF00FF00F), ABC_CONST(0x00F000F000F000F0), ABC_CONST(0x0F000F000F000F00),
        ABC_CONST(0xF0F00F0FF0F00F0F), ABC_CONST(0x0000F0F00000F0F0), ABC_CONST(0x0F0F00000F0F0000) ,ABC_CONST(0xF0F0F0F00F0F0F0F), ABC_CONST(0x00000000F0F0F0F0), ABC_CONST(0x0F0F0F0F00000000),
        ABC_CONST(0xFF0000FFFF0000FF), ABC_CONST(0x0000FF000000FF00), ABC_CONST(0x00FF000000FF0000) ,ABC_CONST(0xFF00FF0000FF00FF), ABC_CONST(0x00000000FF00FF00), ABC_CONST(0x00FF00FF00000000),
        ABC_CONST(0xFFFF00000000FFFF), ABC_CONST(0x00000000FFFF0000), ABC_CONST(0x0000FFFF00000000)
    };
    e = 3*((9*i - i*i -2)/2 + j);          //  Exact formula for index in maskArray
    wordsNumber = Kit_TruthWordNum_64bit(totalVars);
    shift = (1<<j)-(1<<i);
    for(n = 0; n < wordsNumber; n++)
        f[n] = (f[n]&maskArray[e])+((f[n]&(maskArray[e+1]))<< shift)+((f[n]&(maskArray[e+2]))>> shift);
}
//  "width" - how many "Words" in a row have "1s" (or "0s")in position "i"
//   wi - width of i
//   wj - width of j
//   wwi = 2*wi; wwj = 2*wj;

void swap_ij_case2( word* f,int totalVars, int i, int j)
{
    word mask[] = { ABC_CONST(0xAAAAAAAAAAAAAAAA), ABC_CONST(0xCCCCCCCCCCCCCCCC), ABC_CONST(0xF0F0F0F0F0F0F0F0),
                    ABC_CONST(0xFF00FF00FF00FF00), ABC_CONST(0xFFFF0000FFFF0000), ABC_CONST(0xFFFFFFFF00000000) };
    word temp;
    int x,y,wj;
    int WORDS_IN_TT = Kit_TruthWordNum_64bit(totalVars);
    //  int forShift = ((Word)1)<<i;
    int forShift = (1<<i);
    wj = 1 << (j - 6);
    x = 0;
    y = wj;
    for(y=wj; y<WORDS_IN_TT;y+=2*wj)
        for(x=y-wj; x < y; x++)
        {
            temp = f[x+wj];
            f[x+wj] = ((f[x+wj])&(mask[i])) + (((f[x]) & (mask[i])) >> forShift);
            f[x] = ((f[x])&(~mask[i])) + ((temp&(~mask[i])) << forShift);
        }
}

void swap_ij_case3( word* f,int totalVars, int i, int j)
{
    int x,y,wwi,wwj,shift;
    int WORDS_IN_TT;
    int SizeOfBlock;
    word* temp;
    wwi = 1 << (i - 5);
    wwj = 1 << (j - 5);
    shift = (wwj - wwi)/2;
    WORDS_IN_TT = Kit_TruthWordNum_64bit(totalVars);
    SizeOfBlock = sizeof(word)*wwi/2;
    temp = (word *)malloc(SizeOfBlock);
    for(y=wwj/2; y<WORDS_IN_TT; y+=wwj)
        for(x=y-shift; x<y; x+=wwi)
        {
            memcpy(temp,&f[x],SizeOfBlock);
            memcpy(&f[x],&f[x+shift],SizeOfBlock);
            memcpy(&f[x+shift],temp,SizeOfBlock);
        }
}
void swap_ij( word* f,int totalVars, int varI, int varJ)
{
    if (varI == varJ)
        return;
    else if(varI>varJ)
        swap_ij( f,totalVars,varJ,varI);
    else if((varI <= 4) && (varJ <= 5))
        swap_ij_case1(f,totalVars, varI, varJ);
    else if((varI <= 5) && (varJ > 5))
        swap_ij_case2(f,totalVars, varI, varJ);
    else if((varI > 5) && (varJ > 5))
        swap_ij_case3(f,totalVars,varI,varJ);
}

ABC_NAMESPACE_IMPL_END