nysol-zdd 3.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/ext/zdd_so/BDD.cc +495 -0
- data/ext/zdd_so/BDD.h +356 -0
- data/ext/zdd_so/BDDDG.cc +1818 -0
- data/ext/zdd_so/BDDDG.h +107 -0
- data/ext/zdd_so/BDDHASH.cc +91 -0
- data/ext/zdd_so/BtoI.cc +503 -0
- data/ext/zdd_so/BtoI.h +144 -0
- data/ext/zdd_so/CtoI.cc +1072 -0
- data/ext/zdd_so/CtoI.h +186 -0
- data/ext/zdd_so/MLZBDDV.cc +153 -0
- data/ext/zdd_so/MLZBDDV.h +42 -0
- data/ext/zdd_so/SOP.cc +608 -0
- data/ext/zdd_so/SOP.h +199 -0
- data/ext/zdd_so/ZBDD.cc +1035 -0
- data/ext/zdd_so/ZBDD.h +243 -0
- data/ext/zdd_so/ZBDDDG.cc +1834 -0
- data/ext/zdd_so/ZBDDDG.h +105 -0
- data/ext/zdd_so/ZBDDHASH.cc +91 -0
- data/ext/zdd_so/bddc.c +2816 -0
- data/ext/zdd_so/bddc.h +132 -0
- data/ext/zdd_so/extconf.rb +25 -0
- data/ext/zdd_so/include/aheap.c +211 -0
- data/ext/zdd_so/include/aheap.h +111 -0
- data/ext/zdd_so/include/base.c +93 -0
- data/ext/zdd_so/include/base.h +60 -0
- data/ext/zdd_so/include/itemset.c +473 -0
- data/ext/zdd_so/include/itemset.h +153 -0
- data/ext/zdd_so/include/problem.c +371 -0
- data/ext/zdd_so/include/problem.h +160 -0
- data/ext/zdd_so/include/queue.c +518 -0
- data/ext/zdd_so/include/queue.h +177 -0
- data/ext/zdd_so/include/sgraph.c +331 -0
- data/ext/zdd_so/include/sgraph.h +170 -0
- data/ext/zdd_so/include/stdlib2.c +832 -0
- data/ext/zdd_so/include/stdlib2.h +746 -0
- data/ext/zdd_so/include/trsact.c +723 -0
- data/ext/zdd_so/include/trsact.h +167 -0
- data/ext/zdd_so/include/vec.c +583 -0
- data/ext/zdd_so/include/vec.h +159 -0
- data/ext/zdd_so/lcm-vsop.cc +596 -0
- data/ext/zdd_so/print.cc +683 -0
- data/ext/zdd_so/table.cc +330 -0
- data/ext/zdd_so/vsop.h +88 -0
- data/ext/zdd_so/zdd_so.cpp +3277 -0
- data/lib/nysol/zdd.rb +31 -0
- metadata +131 -0
data/ext/zdd_so/BDDDG.h
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
/*****************************************
|
2
|
+
* BDDDG - Decomposition Graph *
|
3
|
+
* (SAPPORO-1.01) - Header *
|
4
|
+
* (C) Shin-ichi MINATO (July 4, 2005) *
|
5
|
+
*****************************************/
|
6
|
+
|
7
|
+
#ifndef _BDDDG_
|
8
|
+
#define _BDDDG_
|
9
|
+
|
10
|
+
#include "BDD.h"
|
11
|
+
|
12
|
+
#define BDDDG_InitSize 4
|
13
|
+
|
14
|
+
#define BDDDG_NIL BDD_MaxNode
|
15
|
+
|
16
|
+
#define BDDDG_C1 1
|
17
|
+
#define BDDDG_LIT 2
|
18
|
+
#define BDDDG_OR 3
|
19
|
+
#define BDDDG_XOR 4
|
20
|
+
#define BDDDG_OTHER 5
|
21
|
+
|
22
|
+
#define BDDDG_PackIdx(ndx,inv) (((ndx)==BDDDG_NIL)? BDDDG_NIL:(((ndx)<<1)|inv))
|
23
|
+
#define BDDDG_Ndx(idx) (((idx)==BDDDG_NIL)? BDDDG_NIL:((idx)>>1))
|
24
|
+
#define BDDDG_Inv(idx) ((idx)&1)
|
25
|
+
#define BDDDG_InvSet(idx) ((idx)|1)
|
26
|
+
#define BDDDG_InvReset(idx) ((idx)&(~1))
|
27
|
+
#define BDDDG_InvAlt(idx) ((idx)^1)
|
28
|
+
|
29
|
+
class BDDDG
|
30
|
+
{
|
31
|
+
struct Node;
|
32
|
+
struct NodeLink
|
33
|
+
{
|
34
|
+
bddword _idx;
|
35
|
+
bddword _nxt;
|
36
|
+
NodeLink(void){ _idx = BDDDG_NIL; _nxt = BDDDG_NIL; }
|
37
|
+
};
|
38
|
+
|
39
|
+
bddword _nodeSize;
|
40
|
+
bddword _nodeUsed;
|
41
|
+
bddword _linkSize;
|
42
|
+
bddword _linkUsed;
|
43
|
+
bddword* _hashWheel;
|
44
|
+
Node* _nodeA;
|
45
|
+
NodeLink* _linkA;
|
46
|
+
bddword _c1;
|
47
|
+
|
48
|
+
bddword HashIndex(BDD);
|
49
|
+
bddword NewNdx(BDD, char);
|
50
|
+
int EnlargeNode(void);
|
51
|
+
bddword NewLkx(bddword);
|
52
|
+
int EnlargeLink(void);
|
53
|
+
int LinkNodes(bddword, bddword);
|
54
|
+
int Merge3(bddword, bddword, bddword);
|
55
|
+
BDD Func0(BDD, BDD);
|
56
|
+
int LinkNodesC3(bddword, bddword);
|
57
|
+
void Print0(bddword);
|
58
|
+
bddword Merge(BDD, bddword, bddword);
|
59
|
+
bddword ReferIdx(BDD);
|
60
|
+
void PhaseSweep(bddword);
|
61
|
+
void MarkSweep(bddword);
|
62
|
+
void MarkSweepR(bddword);
|
63
|
+
int Mark1(bddword);
|
64
|
+
void Mark2R(bddword);
|
65
|
+
int MarkChkR(bddword);
|
66
|
+
void Mark3R(bddword);
|
67
|
+
|
68
|
+
struct Node
|
69
|
+
{
|
70
|
+
bddword _lkx;
|
71
|
+
BDD _f;
|
72
|
+
bddword _ndxP;
|
73
|
+
char _invP;
|
74
|
+
char _type; // NULL, C1, LIT, OR, XOR, OTHER
|
75
|
+
char _mark;
|
76
|
+
Node(void);
|
77
|
+
Node(BDD, char);
|
78
|
+
};
|
79
|
+
|
80
|
+
public:
|
81
|
+
BDDDG(void);
|
82
|
+
~BDDDG(void);
|
83
|
+
void Clear(void);
|
84
|
+
bddword NodeUsed(void);
|
85
|
+
int PrintDecomp(BDD);
|
86
|
+
bddword Decomp(BDD);
|
87
|
+
bddword NodeIdx(BDD);
|
88
|
+
|
89
|
+
friend class BDDDG_Tag;
|
90
|
+
};
|
91
|
+
|
92
|
+
class BDDDG_Tag
|
93
|
+
{
|
94
|
+
BDDDG* _dg;
|
95
|
+
bddword _ndx;
|
96
|
+
bddword _lkx;
|
97
|
+
public:
|
98
|
+
BDDDG_Tag(void);
|
99
|
+
int Set(BDDDG *, bddword);
|
100
|
+
bddword TopIdx(void);
|
101
|
+
bddword NextIdx(void);
|
102
|
+
char Type(void);
|
103
|
+
BDD Func(void);
|
104
|
+
};
|
105
|
+
|
106
|
+
#endif // _BDDDG_
|
107
|
+
|
@@ -0,0 +1,91 @@
|
|
1
|
+
/****************************************
|
2
|
+
* BDD+ Manipulator (SAPPORO-1.59) *
|
3
|
+
* (Hash table methods) *
|
4
|
+
* (C) Shin-ichi MINATO (Dec. 10, 2013) *
|
5
|
+
****************************************/
|
6
|
+
|
7
|
+
#include "BDD.h"
|
8
|
+
|
9
|
+
using std::cerr;
|
10
|
+
|
11
|
+
BDD_Hash::BDD_Hash()
|
12
|
+
{
|
13
|
+
_hashSize = 16;
|
14
|
+
_wheel = new BDD_Entry[_hashSize];
|
15
|
+
_amount = 0;
|
16
|
+
}
|
17
|
+
|
18
|
+
BDD_Hash::~BDD_Hash() { delete[] _wheel; }
|
19
|
+
|
20
|
+
void BDD_Hash::Clear()
|
21
|
+
{
|
22
|
+
if(_hashSize != 0) delete[] _wheel;
|
23
|
+
_hashSize = 16;
|
24
|
+
_wheel = new BDD_Entry[_hashSize];
|
25
|
+
_amount = 0;
|
26
|
+
}
|
27
|
+
|
28
|
+
BDD_Hash::BDD_Entry* BDD_Hash::GetEntry(BDD key)
|
29
|
+
{
|
30
|
+
bddword id = key.GetID();
|
31
|
+
bddword hash = (id+(id>>10)+(id>>20)) & (_hashSize - 1);
|
32
|
+
bddword i = hash;
|
33
|
+
while(_wheel[i]._key != -1)
|
34
|
+
{
|
35
|
+
if(key == _wheel[i]._key) return & _wheel[i];
|
36
|
+
i++;
|
37
|
+
i &= (_hashSize -1);
|
38
|
+
}
|
39
|
+
i = hash;
|
40
|
+
while(_wheel[i]._key != -1)
|
41
|
+
{
|
42
|
+
if(_wheel[i]._ptr == 0) break;
|
43
|
+
i++;
|
44
|
+
i &= (_hashSize -1);
|
45
|
+
}
|
46
|
+
return & _wheel[i];
|
47
|
+
}
|
48
|
+
|
49
|
+
void BDD_Hash::Enlarge()
|
50
|
+
{
|
51
|
+
bddword oldSize = _hashSize;
|
52
|
+
BDD_Entry* oldWheel = _wheel;
|
53
|
+
|
54
|
+
_hashSize <<= 2;
|
55
|
+
_wheel = new BDD_Entry[_hashSize];
|
56
|
+
if(_wheel == 0)
|
57
|
+
{
|
58
|
+
cerr << "<ERROR> BDD_Hash::Enlarge(): Memory overflow (";
|
59
|
+
cerr << _hashSize << ")\n";
|
60
|
+
exit(1);
|
61
|
+
}
|
62
|
+
_amount = 0;
|
63
|
+
for(bddword i=0; i<oldSize; i++)
|
64
|
+
if(oldWheel[i]._ptr != 0)
|
65
|
+
if(oldWheel[i]._key != -1)
|
66
|
+
Enter(oldWheel[i]._key, oldWheel[i]._ptr);
|
67
|
+
delete[] oldWheel;
|
68
|
+
}
|
69
|
+
|
70
|
+
void BDD_Hash::Enter(BDD key, void* ptr)
|
71
|
+
// ptr = 0 means deleting.
|
72
|
+
{
|
73
|
+
BDD_Entry* ent = GetEntry(key);
|
74
|
+
if(ent -> _key == -1) _amount++;
|
75
|
+
else if(ent -> _ptr == 0) _amount++;
|
76
|
+
if(ptr == 0) _amount--;
|
77
|
+
ent -> _key = key;
|
78
|
+
ent -> _ptr = ptr;
|
79
|
+
if(_amount >= (_hashSize>>1)) Enlarge();
|
80
|
+
}
|
81
|
+
|
82
|
+
void* BDD_Hash::Refer(BDD key)
|
83
|
+
// returns 0 if not found.
|
84
|
+
{
|
85
|
+
BDD_Entry* ent = GetEntry(key);
|
86
|
+
if(ent -> _key == -1) return 0;
|
87
|
+
return ent -> _ptr;
|
88
|
+
}
|
89
|
+
|
90
|
+
bddword BDD_Hash::Amount() { return _amount; }
|
91
|
+
|
data/ext/zdd_so/BtoI.cc
ADDED
@@ -0,0 +1,503 @@
|
|
1
|
+
/******************************************
|
2
|
+
* Binary-to-Integer function class *
|
3
|
+
* (SAPPORO-1.55) - Body *
|
4
|
+
* (C) Shin-ichi MINATO (Dec. 11, 2012) *
|
5
|
+
******************************************/
|
6
|
+
|
7
|
+
#include "BtoI.h"
|
8
|
+
|
9
|
+
BtoI::BtoI(int n)
|
10
|
+
{
|
11
|
+
int k = n;
|
12
|
+
if(k < 0)
|
13
|
+
{
|
14
|
+
k = -k;
|
15
|
+
if(k < 0) BDDerr("BtoI::BtoI(): overflow.", n);
|
16
|
+
}
|
17
|
+
while(k != 0)
|
18
|
+
{
|
19
|
+
if((k & 1) != 0) _bddv = _bddv || BDD(1);
|
20
|
+
else _bddv = _bddv || BDD(0);
|
21
|
+
k >>= 1;
|
22
|
+
}
|
23
|
+
_bddv = _bddv || BDD(0);
|
24
|
+
if(n < 0) *this = - (*this);
|
25
|
+
}
|
26
|
+
|
27
|
+
BtoI& BtoI::operator+=(const BtoI& fv) { return *this = *this + fv; }
|
28
|
+
BtoI& BtoI::operator-=(const BtoI& fv) { return *this = *this - fv; }
|
29
|
+
BtoI& BtoI::operator*=(const BtoI& fv) { return *this = *this * fv; }
|
30
|
+
BtoI& BtoI::operator/=(const BtoI& fv) { return *this = *this / fv; }
|
31
|
+
BtoI& BtoI::operator%=(const BtoI& fv) { return *this = *this % fv; }
|
32
|
+
BtoI& BtoI::operator&=(const BtoI& fv) { return *this = *this & fv; }
|
33
|
+
BtoI& BtoI::operator|=(const BtoI& fv) { return *this = *this | fv; }
|
34
|
+
BtoI& BtoI::operator^=(const BtoI& fv) { return *this = *this ^ fv; }
|
35
|
+
BtoI& BtoI::operator<<=(const BtoI& fv) { return *this = *this << fv; }
|
36
|
+
BtoI& BtoI::operator>>=(const BtoI& fv) { return *this = *this >> fv; }
|
37
|
+
|
38
|
+
BtoI BtoI::Shift(int power) const
|
39
|
+
{
|
40
|
+
if(power == 0) return *this;
|
41
|
+
if(power > 0)
|
42
|
+
{
|
43
|
+
if(*this == 0) return *this;
|
44
|
+
return BtoI(BDDV(0, power) || _bddv);
|
45
|
+
}
|
46
|
+
int p = -power;
|
47
|
+
int len = Len();
|
48
|
+
if(p >= len) p = len-1;
|
49
|
+
return BtoI(_bddv.Part(p, len - p));
|
50
|
+
}
|
51
|
+
|
52
|
+
BtoI BtoI::operator<<(const BtoI& fv) const
|
53
|
+
{
|
54
|
+
if(_bddv == BDDV(-1)) return *this;
|
55
|
+
if(fv == BtoI(BDD(-1))) return fv;
|
56
|
+
BtoI ffv = fv;
|
57
|
+
BDD sign = ffv.GetSignBDD();
|
58
|
+
BtoI p1 = BtoI_ITE(sign, 0, *this);
|
59
|
+
BtoI n1 = BtoI_ITE(sign, *this, 0);
|
60
|
+
BtoI p2 = BtoI_ITE(sign, 0, ffv);
|
61
|
+
BtoI n2 = BtoI_ITE(sign, -ffv, 0);
|
62
|
+
|
63
|
+
for(int i=0; i<ffv.Len(); i++)
|
64
|
+
{
|
65
|
+
BDD f = p2.GetBDD(i);
|
66
|
+
if(f != 0) p1 = BtoI_ITE(f, p1.Shift(1 << i), p1);
|
67
|
+
f = n2.GetBDD(i);
|
68
|
+
if(f != 0) n1 = BtoI_ITE(f, n1.Shift(-(1 << i)), n1);
|
69
|
+
}
|
70
|
+
return p1 | n1;
|
71
|
+
}
|
72
|
+
|
73
|
+
BtoI BtoI::Sup() const
|
74
|
+
{
|
75
|
+
BDDV fv = _bddv;
|
76
|
+
int len = fv.Len();
|
77
|
+
while(len > 1)
|
78
|
+
{
|
79
|
+
if(fv.GetBDD(len-1) != fv.GetBDD(len-2))
|
80
|
+
break;
|
81
|
+
fv = fv.Part(0, --len);
|
82
|
+
}
|
83
|
+
return BtoI(fv);
|
84
|
+
}
|
85
|
+
|
86
|
+
BtoI BtoI::UpperBound() const
|
87
|
+
{
|
88
|
+
if(_bddv == BDDV(-1)) return *this;
|
89
|
+
BDD d = GetSignBDD();
|
90
|
+
BDD c0 = BDD(d==1);
|
91
|
+
BDDV ub = c0;
|
92
|
+
BDD cond = c0 | ~d;
|
93
|
+
for(int i=Len()-2; i>=0; i--)
|
94
|
+
{
|
95
|
+
d = GetBDD(i);
|
96
|
+
c0 = BDD((cond & d) != 0);
|
97
|
+
if(c0 == -1) return BtoI(BDD(-1));
|
98
|
+
ub = c0 || ub;
|
99
|
+
cond &= ~c0 | d;
|
100
|
+
}
|
101
|
+
return BtoI(ub).Sup();
|
102
|
+
}
|
103
|
+
|
104
|
+
BtoI BtoI::UpperBound(const BDD& f) const
|
105
|
+
{
|
106
|
+
if(_bddv == BDDV(-1)) return *this;
|
107
|
+
if(f == -1) return BtoI(BDDV(-1));
|
108
|
+
BDD d = GetSignBDD();
|
109
|
+
BDD c0 = d.Univ(f);
|
110
|
+
BDDV ub = c0;
|
111
|
+
BDD cond = c0 | ~d;
|
112
|
+
for(int i=Len()-2; i>=0; i--)
|
113
|
+
{
|
114
|
+
d = GetBDD(i);
|
115
|
+
c0 = (cond & d).Exist(f);
|
116
|
+
if(c0 == -1) return BtoI(BDD(-1));
|
117
|
+
ub = c0 || ub;
|
118
|
+
cond &= ~c0 | d;
|
119
|
+
}
|
120
|
+
return BtoI(ub).Sup();
|
121
|
+
}
|
122
|
+
|
123
|
+
int BtoI::GetInt() const
|
124
|
+
{
|
125
|
+
if(Top() > 0)
|
126
|
+
{
|
127
|
+
if(_bddv == BDDV(-1)) return 0;
|
128
|
+
return At0(Top()).GetInt();
|
129
|
+
}
|
130
|
+
if(GetSignBDD() != 0) return - (- *this).GetInt();
|
131
|
+
int len = Len();
|
132
|
+
if(len > 32) len = 32; // Assuming 32-bit int.
|
133
|
+
int n = 0;
|
134
|
+
for(int i=len-2; i>=0; i--)
|
135
|
+
{
|
136
|
+
n <<= 1;
|
137
|
+
if(GetBDD(i) != 0) n |= 1;
|
138
|
+
}
|
139
|
+
return n;
|
140
|
+
}
|
141
|
+
|
142
|
+
static const char table[] = "0123456789ABCDEF";
|
143
|
+
|
144
|
+
int BtoI::StrNum10(char* s) const
|
145
|
+
{
|
146
|
+
if(*this == BtoI(BDD(-1)))
|
147
|
+
{
|
148
|
+
sprintf(s, "0");
|
149
|
+
return 1;
|
150
|
+
}
|
151
|
+
const int width = 9;
|
152
|
+
BtoI fv0 = *this;
|
153
|
+
while(fv0.Top() > 0) fv0 = fv0.At0(fv0.Top());
|
154
|
+
int i=0;
|
155
|
+
int sign = 0;
|
156
|
+
if(fv0.GetSignBDD() == 1)
|
157
|
+
{
|
158
|
+
sign = 1;
|
159
|
+
fv0 = -fv0;
|
160
|
+
}
|
161
|
+
BtoI a;
|
162
|
+
char s0[12];
|
163
|
+
if(fv0.Len() > 29)
|
164
|
+
{
|
165
|
+
BtoI fvb = BtoI(1000000000); // 10 ** width
|
166
|
+
while(1)
|
167
|
+
{
|
168
|
+
BtoI t = BtoI_GE(fv0, fvb);
|
169
|
+
if(t == BtoI(BDD(-1)))
|
170
|
+
{
|
171
|
+
sprintf(s, "0");
|
172
|
+
return 1;
|
173
|
+
}
|
174
|
+
if(t == BtoI(0)) break;
|
175
|
+
a = fv0 % fvb;
|
176
|
+
if(a == BtoI(BDD(-1)))
|
177
|
+
{
|
178
|
+
sprintf(s, "0");
|
179
|
+
return 1;
|
180
|
+
}
|
181
|
+
sprintf(s0, "%09d", a.GetInt());
|
182
|
+
for(int j=i-1; j>=0; j--) s[j+width] = s[j];
|
183
|
+
for(int j=0; j<width; j++) s[j] = s0[j];
|
184
|
+
i += width;
|
185
|
+
fv0 /= fvb;
|
186
|
+
}
|
187
|
+
}
|
188
|
+
sprintf(s0, "%d", fv0.GetInt());
|
189
|
+
int len = strlen(s0);
|
190
|
+
for(int j=i-1; j>=0; j--) s[j+len] = s[j];
|
191
|
+
for(int j=0; j<len; j++) s[j] = s0[j];
|
192
|
+
i += len;
|
193
|
+
if(sign == 1)
|
194
|
+
{
|
195
|
+
for(int j=i-1; j>=0; j--) s[j+1] = s[j];
|
196
|
+
s[0] = '-';
|
197
|
+
i++;
|
198
|
+
}
|
199
|
+
s[i] = 0;
|
200
|
+
return 0;
|
201
|
+
}
|
202
|
+
|
203
|
+
int BtoI::StrNum16(char* s) const
|
204
|
+
{
|
205
|
+
if(*this == BtoI(BDD(-1)))
|
206
|
+
{
|
207
|
+
sprintf(s, "0");
|
208
|
+
return 1;
|
209
|
+
}
|
210
|
+
const int width = 7;
|
211
|
+
BtoI fv0 = *this;
|
212
|
+
while(fv0.Top() > 0) fv0 = fv0.At0(fv0.Top());
|
213
|
+
int i=0;
|
214
|
+
int sign = 0;
|
215
|
+
if(fv0.GetSignBDD() == 1)
|
216
|
+
{
|
217
|
+
sign = 1;
|
218
|
+
fv0 = -fv0;
|
219
|
+
}
|
220
|
+
BtoI a;
|
221
|
+
char s0[10];
|
222
|
+
if(fv0.Len() > 27)
|
223
|
+
{
|
224
|
+
BtoI fvb = BtoI(1<<(width*4));
|
225
|
+
while(1)
|
226
|
+
{
|
227
|
+
BtoI t = BtoI_GE(fv0, fvb);
|
228
|
+
if(t == BtoI(BDD(-1)))
|
229
|
+
{
|
230
|
+
sprintf(s, "0");
|
231
|
+
return 1;
|
232
|
+
}
|
233
|
+
if(t == BtoI(0)) break;
|
234
|
+
a = fv0 & (fvb-1);
|
235
|
+
if(a == BtoI(BDD(-1)))
|
236
|
+
{
|
237
|
+
sprintf(s, "0");
|
238
|
+
return 1;
|
239
|
+
}
|
240
|
+
sprintf(s0, "%07X", a.GetInt());
|
241
|
+
for(int j=i-1; j>=0; j--) s[j+width] = s[j];
|
242
|
+
for(int j=0; j<width; j++) s[j] = s0[j];
|
243
|
+
i += width;
|
244
|
+
fv0 >>= width*4;
|
245
|
+
}
|
246
|
+
}
|
247
|
+
sprintf(s0, "%X", fv0.GetInt());
|
248
|
+
int len = strlen(s0);
|
249
|
+
for(int j=i-1; j>=0; j--) s[j+len] = s[j];
|
250
|
+
for(int j=0; j<len; j++) s[j] = s0[j];
|
251
|
+
i += len;
|
252
|
+
if(sign == 1)
|
253
|
+
{
|
254
|
+
for(int j=i-1; j>=0; j--) s[j+1] = s[j];
|
255
|
+
s[0] = '-';
|
256
|
+
i++;
|
257
|
+
}
|
258
|
+
s[i] = 0;
|
259
|
+
return 0;
|
260
|
+
}
|
261
|
+
|
262
|
+
bddword BtoI::Size() const { return _bddv.Size(); }
|
263
|
+
|
264
|
+
void BtoI::Print() const { _bddv.Print(); }
|
265
|
+
|
266
|
+
BtoI operator+(const BtoI& a, const BtoI& b)
|
267
|
+
{
|
268
|
+
if(a == 0) return b;
|
269
|
+
if(b == 0) return a;
|
270
|
+
if(a == BtoI(BDD(-1))) return a;
|
271
|
+
if(b == BtoI(BDD(-1))) return b;
|
272
|
+
BDD a0;
|
273
|
+
BDD b0;
|
274
|
+
BDD c = 0;
|
275
|
+
BDD c0 = 0;
|
276
|
+
BDDV fv;
|
277
|
+
int len = a.Len();
|
278
|
+
if(len < b.Len()) len = b.Len();
|
279
|
+
for(int i=0; i<len; i++)
|
280
|
+
{
|
281
|
+
a0 = a.GetBDD(i);
|
282
|
+
b0 = b.GetBDD(i);
|
283
|
+
c0 = c;
|
284
|
+
fv = fv || (a0 ^ b0 ^ c0);
|
285
|
+
c = (a0 & b0)|(b0 & c0)|(c0 & a0);
|
286
|
+
}
|
287
|
+
if(c != c0) fv = fv || (a0 ^ b0 ^ c);
|
288
|
+
return BtoI(fv).Sup();
|
289
|
+
}
|
290
|
+
|
291
|
+
BtoI operator-(const BtoI& a, const BtoI& b)
|
292
|
+
{
|
293
|
+
if(b == 0) return a;
|
294
|
+
if(b == BtoI(BDD(-1))) return b;
|
295
|
+
if(a == b) return 0;
|
296
|
+
if(a == BtoI(BDD(-1))) return a;
|
297
|
+
BDD a0;
|
298
|
+
BDD b0;
|
299
|
+
BDD c = 0;
|
300
|
+
BDD c0 = 0;
|
301
|
+
BDDV fv;
|
302
|
+
int len = a.Len();
|
303
|
+
if(len < b.Len()) len = b.Len();
|
304
|
+
for(int i=0; i<len; i++)
|
305
|
+
{
|
306
|
+
a0 = a.GetBDD(i);
|
307
|
+
b0 = b.GetBDD(i);
|
308
|
+
c0 = c;
|
309
|
+
fv = fv || (a0 ^ b0 ^ c0);
|
310
|
+
c = (~a0 & b0)|(b0 & c0)|(c0 & ~a0);
|
311
|
+
}
|
312
|
+
if(c != c0) fv = fv || (a0 ^ b0 ^ c);
|
313
|
+
return BtoI(fv).Sup();
|
314
|
+
}
|
315
|
+
|
316
|
+
BtoI operator*(const BtoI& a, const BtoI& b)
|
317
|
+
{
|
318
|
+
if(a == 1) return b;
|
319
|
+
if(b == 1) return a;
|
320
|
+
if(a == BtoI(BDD(-1))) return a;
|
321
|
+
if(b == BtoI(BDD(-1))) return b;
|
322
|
+
if(a == 0) return 0;
|
323
|
+
if(b == 0) return 0;
|
324
|
+
BDD sign = b.GetSignBDD();
|
325
|
+
BtoI a0 = BtoI_ITE(sign, -a, a);
|
326
|
+
BtoI b0 = BtoI_ITE(sign, -b, b);
|
327
|
+
BtoI s = BtoI_ITE(b.GetBDD(0), a0, 0);
|
328
|
+
while(b0 != 0)
|
329
|
+
{
|
330
|
+
a0 <<= 1;
|
331
|
+
b0 >>= 1;
|
332
|
+
s += BtoI_ITE(b0.GetBDD(0), a0, 0);
|
333
|
+
if(s == BtoI(BDD(-1))) break;
|
334
|
+
}
|
335
|
+
return s;
|
336
|
+
}
|
337
|
+
|
338
|
+
BtoI operator/(const BtoI& a, const BtoI& b)
|
339
|
+
{
|
340
|
+
if(b == 1) return a;
|
341
|
+
if(a == BtoI(BDD(-1))) return a;
|
342
|
+
if(b == BtoI(BDD(-1))) return b;
|
343
|
+
if(a == 0) return 0;
|
344
|
+
if(a == b) return 1;
|
345
|
+
if(BtoI_EQ(b, 0) != 0)
|
346
|
+
BDDerr("BtoI::operator/(): Divided by 0.");
|
347
|
+
BDD sign = a.GetSignBDD() ^ b.GetSignBDD();
|
348
|
+
BtoI a0 = BtoI_ITE(a.GetSignBDD(), -a, a);
|
349
|
+
if(a0 == BtoI(BDD(-1))) return a0;
|
350
|
+
BtoI b0 = BtoI_ITE(b.GetSignBDD(), -b, b);
|
351
|
+
if(b0 == BtoI(BDD(-1))) return b0;
|
352
|
+
int len = a0.Len();
|
353
|
+
b0 <<= len - 2;
|
354
|
+
BtoI s = 0;
|
355
|
+
for(int i=0; i<len-1; i++)
|
356
|
+
{
|
357
|
+
s <<= 1;
|
358
|
+
BtoI cond = BtoI_GE(a0, b0);
|
359
|
+
a0 = BtoI_ITE(cond, a0 - b0, a0);
|
360
|
+
s += cond;
|
361
|
+
b0 >>= 1;
|
362
|
+
}
|
363
|
+
return BtoI_ITE(sign, -s, s);
|
364
|
+
}
|
365
|
+
|
366
|
+
BtoI operator&(const BtoI& a, const BtoI& b)
|
367
|
+
{
|
368
|
+
int len = a.Len();
|
369
|
+
if(len < b.Len()) len = b.Len();
|
370
|
+
BDDV fv;
|
371
|
+
for(int i=0; i<len; i++)
|
372
|
+
fv = fv || (a.GetBDD(i) & b.GetBDD(i));
|
373
|
+
return BtoI(fv).Sup();
|
374
|
+
}
|
375
|
+
|
376
|
+
BtoI operator|(const BtoI& a, const BtoI& b)
|
377
|
+
{
|
378
|
+
int len = a.Len();
|
379
|
+
if(len < b.Len()) len = b.Len();
|
380
|
+
BDDV fv;
|
381
|
+
for(int i=0; i<len; i++)
|
382
|
+
fv = fv || (a.GetBDD(i) | b.GetBDD(i));
|
383
|
+
return BtoI(fv).Sup();
|
384
|
+
}
|
385
|
+
|
386
|
+
BtoI operator^(const BtoI& a, const BtoI& b)
|
387
|
+
{
|
388
|
+
int len = a.Len();
|
389
|
+
if(len < b.Len()) len = b.Len();
|
390
|
+
BDDV fv;
|
391
|
+
for(int i=0; i<len; i++)
|
392
|
+
fv = fv || (a.GetBDD(i) ^ b.GetBDD(i));
|
393
|
+
return BtoI(fv).Sup();
|
394
|
+
}
|
395
|
+
|
396
|
+
BtoI BtoI_ITE(const BDD& f, const BtoI& a, const BtoI& b)
|
397
|
+
{
|
398
|
+
if(a == b) return a;
|
399
|
+
int len = a.Len();
|
400
|
+
if(len < b.Len()) len = b.Len();
|
401
|
+
BDDV fv;
|
402
|
+
for(int i=0; i<len; i++)
|
403
|
+
{
|
404
|
+
BDD g = (f & a.GetBDD(i)) | (~f & b.GetBDD(i));
|
405
|
+
fv = fv || g;
|
406
|
+
}
|
407
|
+
return BtoI(fv).Sup();
|
408
|
+
}
|
409
|
+
|
410
|
+
BtoI BtoI_EQ(const BtoI& a, const BtoI& b)
|
411
|
+
{
|
412
|
+
BtoI c = a - b;
|
413
|
+
BDD cond = 0;
|
414
|
+
for(int i=0; i<c.Len(); i++)
|
415
|
+
{
|
416
|
+
cond |= c.GetBDD(i);
|
417
|
+
if(cond == 1) break;
|
418
|
+
}
|
419
|
+
return BtoI(~cond);
|
420
|
+
}
|
421
|
+
|
422
|
+
static BtoI atoi16(char *);
|
423
|
+
static BtoI atoi16(char* s)
|
424
|
+
{
|
425
|
+
const int width = 6;
|
426
|
+
int p = 0;
|
427
|
+
int len = strlen(s);
|
428
|
+
char a[width + 1];
|
429
|
+
BtoI fv = 0;
|
430
|
+
while(len - p > width)
|
431
|
+
{
|
432
|
+
fv <<= BtoI(width*4);
|
433
|
+
strncpy(a, s+p, width);
|
434
|
+
fv += BtoI((int)strtol(a, 0, 16));
|
435
|
+
p += width;
|
436
|
+
}
|
437
|
+
if(len > width) fv <<= BtoI((len-p)*4);
|
438
|
+
strncpy(a, s+p, width);
|
439
|
+
fv += BtoI((int)strtol(a, 0, 16));
|
440
|
+
return fv;
|
441
|
+
}
|
442
|
+
|
443
|
+
static BtoI atoi2(char *);
|
444
|
+
static BtoI atoi2(char* s)
|
445
|
+
{
|
446
|
+
int p = 0;
|
447
|
+
int len = strlen(s);
|
448
|
+
BtoI fv = 0;
|
449
|
+
while(p < len)
|
450
|
+
{
|
451
|
+
fv <<= BtoI(1);
|
452
|
+
if(s[p] == '1') fv += 1;
|
453
|
+
p++;
|
454
|
+
}
|
455
|
+
return fv;
|
456
|
+
}
|
457
|
+
|
458
|
+
static BtoI atoi10(char *);
|
459
|
+
static BtoI atoi10(char* s)
|
460
|
+
{
|
461
|
+
const int width = 9;
|
462
|
+
int times = 1000000000; // 10 ** width
|
463
|
+
int p = 0;
|
464
|
+
int len = strlen(s);
|
465
|
+
char a[width + 1];
|
466
|
+
BtoI fv = 0;
|
467
|
+
while(len - p > width)
|
468
|
+
{
|
469
|
+
fv *= BtoI(times);
|
470
|
+
strncpy(a, s+p, width);
|
471
|
+
fv += BtoI((int)strtol(a, 0, 10));
|
472
|
+
p += width;
|
473
|
+
}
|
474
|
+
if(len > width)
|
475
|
+
{
|
476
|
+
times = 1;
|
477
|
+
for(int i=p; i<len; i++) times *= 10;
|
478
|
+
fv *= BtoI(times);
|
479
|
+
}
|
480
|
+
strncpy(a, s+p, width);
|
481
|
+
fv += BtoI((int)strtol(a, 0, 10));
|
482
|
+
return fv;
|
483
|
+
}
|
484
|
+
|
485
|
+
BtoI BtoI_atoi(char* s)
|
486
|
+
{
|
487
|
+
if(s[0] == '0')
|
488
|
+
{
|
489
|
+
switch(s[1])
|
490
|
+
{
|
491
|
+
case 'x':
|
492
|
+
case 'X':
|
493
|
+
return atoi16(s+2);
|
494
|
+
case 'b':
|
495
|
+
case 'B':
|
496
|
+
return atoi2(s+2);
|
497
|
+
default:
|
498
|
+
;
|
499
|
+
}
|
500
|
+
}
|
501
|
+
return atoi10(s);
|
502
|
+
}
|
503
|
+
|