Commit 1d1872ab authored by Alexandre Duret-Lutz's avatar Alexandre Duret-Lutz
Browse files

[buddy] Inline the "is bdd constant" check performed in copies/constructors.

This avoids a library call to bdd_addref or bdd_delref.

* src/kernel.c (bdd_delref_nc, bdd_addref_nc): New function,
that work only on BDD that are not constant.
* src/cpext.cxx (bdd::operator=): Move...
* src/bdd.hh (bdd::operator=): ... here.
(bdd::bdd, bdd::~bdd, bdd::operator=): Inline the "is bdd constant"
check and call bdd_delref_nc/bdd_addref_nc only otherwise.
parent 2b58fb90
2011-04-30 Alexandre Duret-Lutz <adl@lrde.epita.fr>
Inline the "is bdd constant" check performed in copies/constructors.
This avoids a library call to bdd_addref or bdd_delref.
* src/kernel.c (bdd_delref_nc, bdd_addref_nc): New function,
that work only on BDD that are not constant.
* src/cpext.cxx (bdd::operator=): Move...
* src/bdd.hh (bdd::operator=): ... here.
(bdd::bdd, bdd::~bdd, bdd::operator=): Inline the "is bdd constant"
check and call bdd_delref_nc/bdd_addref_nc only otherwise.
2011-04-30 Alexandre Duret-Lutz <adl@lrde.epita.fr>
Hint gcc about likely/unlikely branches.
......
......@@ -272,7 +272,9 @@ extern int bdd_var(BDD) __purefn;
extern BDD bdd_low(BDD) __purefn;
extern BDD bdd_high(BDD) __purefn;
extern int bdd_varlevel(int) __purefn;
extern BDD bdd_addref_nc(BDD);
extern BDD bdd_addref(BDD);
extern BDD bdd_delref_nc(BDD);
extern BDD bdd_delref(BDD);
extern void bdd_gbc(void);
extern int bdd_scanset(BDD, int**, int*);
......@@ -453,8 +455,8 @@ class bdd
public:
bdd(void) { root=0; }
bdd(const bdd &r) { bdd_addref(root=r.root); }
~bdd(void) { bdd_delref(root); }
bdd(const bdd &r) { root=r.root; if (root > 1) bdd_addref_nc(root); }
~bdd(void) { if (root > 1) bdd_delref_nc(root); }
int id(void) const;
......@@ -481,7 +483,7 @@ class bdd
private:
BDD root;
bdd(BDD r) { bdd_addref(root=r); }
bdd(BDD r) { root=r; if (root > 1) bdd_addref_nc(root); }
bdd operator=(BDD r);
friend int bdd_init(int, int);
......@@ -849,6 +851,32 @@ inline int bdd::operator==(const bdd &r) const
inline int bdd::operator!=(const bdd &r) const
{ return r.root!=root; }
inline bdd bdd::operator=(const bdd &r)
{
if (__likely(root != r.root))
{
if (root > 1)
bdd_delref_nc(root);
root = r.root;
if (root > 1)
bdd_addref_nc(root);
}
return *this;
}
inline bdd bdd::operator=(int r)
{
if (__likely(root != r))
{
if (root > 1)
bdd_delref_nc(root);
root = r;
if (root > 1)
bdd_addref_nc(root);
}
return *this;
}
inline bdd bdd_true(void)
{ return 1; }
......
......@@ -81,7 +81,7 @@ static bddstrmhandler strmhandler_fdd;
int bdd_cpp_init(int n, int c)
{
int ok = bdd_init(n,c);
strmhandler_bdd = NULL;
strmhandler_fdd = NULL;
......@@ -115,11 +115,11 @@ int bdd_setbddpairs(bddPair *pair, int *oldvar, const bdd *newvar, int size)
{
if (pair == NULL)
return 0;
for (int n=0,e=0 ; n<size ; n++)
if ((e=bdd_setbddpair(pair, oldvar[n], newvar[n].root)) < 0)
return e;
return 0;
}
......@@ -135,39 +135,12 @@ int bdd_anodecountpp(const bdd *r, int num)
cpr[n] = r[n].root;
cou = bdd_anodecount(cpr,num);
free(cpr);
return cou;
}
/*************************************************************************
BDD class functions
*************************************************************************/
bdd bdd::operator=(const bdd &r)
{
if (root != r.root)
{
bdd_delref(root);
root = r.root;
bdd_addref(root);
}
return *this;
}
bdd bdd::operator=(int r)
{
if (root != r)
{
bdd_delref(root);
root = r;
bdd_addref(root);
}
return *this;
}
/*************************************************************************
C++ iostream operators
......@@ -216,14 +189,14 @@ ostream &operator<<(ostream &o, const bdd &r)
o << (r.root == 0 ? "F" : "T");
return o;
}
int *set = new int[bddvarnum];
if (set == NULL)
{
bdd_error(BDD_MEMORY);
return o;
}
memset(set, 0, sizeof(int) * bddvarnum);
bdd_printset_rec(o, r.root, set);
delete[] set;
......@@ -234,7 +207,7 @@ ostream &operator<<(ostream &o, const bdd &r)
o << "ROOT: " << r.root << "\n";
if (r.root < 2)
return o;
bdd_mark(r.root);
for (int n=0 ; n<bddnodesize ; n++)
......@@ -242,7 +215,7 @@ ostream &operator<<(ostream &o, const bdd &r)
if (LEVEL(n) & MARKON)
{
BddNode *node = &bddnodes[n];
LEVELp(node) &= MARKOFF;
o << "[" << setw(5) << n << "] ";
......@@ -263,9 +236,9 @@ ostream &operator<<(ostream &o, const bdd &r)
o << "digraph G {\n";
o << "0 [shape=box, label=\"0\", style=filled, shape=box, height=0.3, width=0.3];\n";
o << "1 [shape=box, label=\"1\", style=filled, shape=box, height=0.3, width=0.3];\n";
bdd_printdot_rec(o, r.root);
o << "}\n";
bdd_unmark(r.root);
......@@ -278,19 +251,19 @@ ostream &operator<<(ostream &o, const bdd &r)
o << (r == 0 ? "F" : "T");
return o;
}
int *set = new int[bddvarnum];
if (set == NULL)
{
bdd_error(BDD_MEMORY);
return o;
}
memset(set, 0, sizeof(int) * bddvarnum);
fdd_printset_rec(o, r.root, set);
delete[] set;
}
return o;
}
......@@ -333,7 +306,7 @@ ostream &operator<<(ostream &o, const bdd_ioformat &f)
for (int n=0 ; n<bddnodesize ; n++)
{
const BddNode *node = &bddnodes[n];
if (LOWp(node) != -1)
{
o << "[" << setw(5) << n << "] ";
......@@ -347,7 +320,7 @@ ostream &operator<<(ostream &o, const bdd_ioformat &f)
}
}
}
return o;
}
......@@ -356,7 +329,7 @@ static void bdd_printset_rec(ostream& o, int r, int* set)
{
int n;
int first;
if (r == 0)
return;
else
......@@ -364,7 +337,7 @@ static void bdd_printset_rec(ostream& o, int r, int* set)
{
o << "<";
first = 1;
for (n=0 ; n<bddvarnum ; n++)
{
if (set[n] > 0)
......@@ -386,10 +359,10 @@ static void bdd_printset_rec(ostream& o, int r, int* set)
{
set[LEVEL(r)] = 1;
bdd_printset_rec(o, LOW(r), set);
set[LEVEL(r)] = 2;
bdd_printset_rec(o, HIGH(r), set);
set[LEVEL(r)] = 0;
}
}
......@@ -410,7 +383,7 @@ static void bdd_printdot_rec(ostream& o, int r)
o << r << " -> " << HIGH(r) << "[style=filled];\n";
SETMARK(r);
bdd_printdot_rec(o, LOW(r));
bdd_printdot_rec(o, HIGH(r));
}
......@@ -422,7 +395,7 @@ static void fdd_printset_rec(ostream &o, int r, int *set)
int used = 0;
int *binval;
int ok, first;
if (r == 0)
return;
else
......@@ -431,18 +404,18 @@ static void fdd_printset_rec(ostream &o, int r, int *set)
o << "<";
first=1;
int fdvarnum = fdd_domainnum();
for (n=0 ; n<fdvarnum ; n++)
{
int firstval=1;
used = 0;
int binsize = fdd_varnum(n);
int *vars = fdd_vars(n);
for (m=0 ; m<binsize ; m++)
if (set[vars[m]] != 0)
used = 1;
if (used)
{
if (!first)
......@@ -458,7 +431,7 @@ static void fdd_printset_rec(ostream &o, int r, int *set)
{
binval = fdddec2bin(n, m);
ok=1;
for (i=0 ; i<binsize && ok ; i++)
if (set[vars[i]] == 1 && binval[i] != 0)
ok = 0;
......@@ -486,10 +459,10 @@ static void fdd_printset_rec(ostream &o, int r, int *set)
{
set[bddlevel2var[LEVEL(r)]] = 1;
fdd_printset_rec(o, LOW(r), set);
set[bddlevel2var[LEVEL(r)]] = 2;
fdd_printset_rec(o, HIGH(r), set);
set[bddlevel2var[LEVEL(r)]] = 0;
}
}
......@@ -581,7 +554,7 @@ bvec bvec_map2(const bvec &a, const bvec &b,
bdd_error(BVEC_SIZE);
return res;
}
res = bvec_false(a.bitnum());
for (n=0 ; n < a.bitnum() ; n++)
res.set(n, fun(a[n], b[n]));
......@@ -601,7 +574,7 @@ bvec bvec_map3(const bvec &a, const bvec &b, const bvec &c,
bdd_error(BVEC_SIZE);
return res;
}
res = bvec_false(a.bitnum());
for (n=0 ; n < a.bitnum() ; n++)
res.set(n, fun(a[n], b[n], c[n]) );
......
......@@ -1102,6 +1102,20 @@ void bdd_gbc(void)
}
BDD bdd_addref_nc(BDD root)
{
#ifndef NDEBUG
if (!bddrunning)
return root;
if (root < 2 || root >= bddnodesize)
return bdd_error(BDD_ILLBDD);
if (LOW(root) == -1)
return bdd_error(BDD_ILLBDD);
#endif
INCREF(root);
return root;
}
/*
NAME {* bdd\_addref *}
SECTION {* kernel *}
......@@ -1131,6 +1145,23 @@ BDD bdd_addref(BDD root)
return root;
}
// Non constant version
BDD bdd_delref_nc(BDD root)
{
#ifndef NDEBUG
if (root < 2 || !bddrunning)
return root;
if (root >= bddnodesize)
return bdd_error(BDD_ILLBDD);
if (LOW(root) == -1)
return bdd_error(BDD_ILLBDD);
/* if the following line is present, fails there much earlier */
if (!HASREF(root)) bdd_error(BDD_BREAK); /* distinctive */
#endif
DECREF(root);
return root;
}
/*
NAME {* bdd\_delref *}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment