/**Function******************************************************************** Synopsis [Integer and floating point multiplication.] Description [Integer and floating point multiplication. Returns NULL if not a terminal case; f * g otherwise. This function can be used also to take the AND of two 0-1 ADDs.] SideEffects [None] SeeAlso [Cudd_addApply] ******************************************************************************/ DdNode * Cudd_addTimes( DdManager * dd, DdNode ** f, DdNode ** g) { DdNode *res; DdNode *F, *G; CUDD_VALUE_TYPE value; F = *f; G = *g; if (F == DD_ZERO(dd) || G == DD_ZERO(dd)) return(DD_ZERO(dd)); if (F == DD_ONE(dd)) return(G); if (G == DD_ONE(dd)) return(F); if (cuddIsConstant(F) && cuddIsConstant(G)) { value = cuddV(F)*cuddV(G); res = cuddUniqueConst(dd,value); return(res); } if (F > G) { /* swap f and g */ *f = G; *g = F; } return(NULL); } /* end of Cudd_addTimes */
/**Function******************************************************************** Synopsis [Comparison of a pair of functions to the i-th ADD variable.] Description [Comparison of a pair of functions to the i-th ADD variable. Returns 1 if the functions are the i-th ADD variable and its complement; 0 otherwise.] SideEffects [None] SeeAlso [] ******************************************************************************/ DD_INLINE static int ddIsIthAddVarPair( DdManager * dd, DdNode * f, DdNode * g, unsigned int i) { return(f->index == i && g->index == i && cuddT(f) == DD_ONE(dd) && cuddE(f) == DD_ZERO(dd) && cuddT(g) == DD_ZERO(dd) && cuddE(g) == DD_ONE(dd)); } /* end of ddIsIthAddVarPair */
/**Function******************************************************************** Synopsis [Checks whether cube is an BDD representing the product of positive literals.] Description [Returns 1 in case of success; 0 otherwise.] SideEffects [None] ******************************************************************************/ static int bddCheckPositiveCube( DdManager * manager, DdNode * cube) { if (Cudd_IsComplement(cube)) return(0); if (cube == DD_ONE(manager)) return(1); if (cuddIsConstant(cube)) return(0); if (cuddE(cube) == Cudd_Not(DD_ONE(manager))) { return(bddCheckPositiveCube(manager, cuddT(cube))); } return(0); } /* end of bddCheckPositiveCube */
/**Function******************************************************************** Synopsis [Determines whether f is less than or equal to g.] Description [Returns 1 if f is less than or equal to g; 0 otherwise. No new nodes are created. This procedure works for arbitrary ADDs. For 0-1 ADDs Cudd_addEvalConst is more efficient.] SideEffects [None] SeeAlso [Cudd_addIteConstant Cudd_addEvalConst Cudd_bddLeq] ******************************************************************************/ int Cudd_addLeq( DdManager * dd, DdNode * f, DdNode * g) { DdNode *tmp, *fv, *fvn, *gv, *gvn; unsigned int topf, topg, res; /* Terminal cases. */ if (f == g) return(1); statLine(dd); if (cuddIsConstant(f)) { if (cuddIsConstant(g)) return(cuddV(f) <= cuddV(g)); if (f == DD_MINUS_INFINITY(dd)) return(1); if (f == DD_PLUS_INFINITY(dd)) return(0); /* since f != g */ } if (g == DD_PLUS_INFINITY(dd)) return(1); if (g == DD_MINUS_INFINITY(dd)) return(0); /* since f != g */ /* Check cache. */ tmp = cuddCacheLookup2(dd,(DD_CTFP)Cudd_addLeq,f,g); if (tmp != NULL) { return(tmp == DD_ONE(dd)); } /* Compute cofactors. One of f and g is not constant. */ topf = cuddI(dd,f->index); topg = cuddI(dd,g->index); if (topf <= topg) { fv = cuddT(f); fvn = cuddE(f); } else { fv = fvn = f; } if (topg <= topf) { gv = cuddT(g); gvn = cuddE(g); } else { gv = gvn = g; } res = Cudd_addLeq(dd,fvn,gvn) && Cudd_addLeq(dd,fv,gv); /* Store result in cache and return. */ cuddCacheInsert2(dd,(DD_CTFP) Cudd_addLeq,f,g, Cudd_NotCond(DD_ONE(dd),res==0)); return(res); } /* end of Cudd_addLeq */
/**Function******************************************************************** Synopsis [Performs the recursive step of Cuddaux_Support.] Description [Performs the recursive step of Cuddaux_Support.] SideEffects [None] SeeAlso [] ******************************************************************************/ DdNode* cuddauxSupportRecur(DdManager* dd, DdNode * f) { DdNode *one, *fv, *fvn, *T,*E, *res, *res1; one = DD_ONE(dd); if (cuddIsConstant(f)) { return one; } fv = cuddT(f); fvn = Cudd_Regular(cuddE(f)); if (cuddIsConstant(fv) && cuddIsConstant(fvn)){ return dd->vars[f->index]; } /* Look in the cache */ res = cuddCacheLookup1(dd,Cuddaux_Support,f); if (res != NULL) return(res); T = cuddIsConstant(fv) ? one : cuddauxSupportRecur(dd,fv); if (T == NULL) return(NULL); cuddRef(T); E = cuddIsConstant(fvn) ? one : cuddauxSupportRecur(dd,fvn); if (E == NULL){ Cudd_IterDerefBdd(dd,T); return(NULL); } if (T==E){ res = cuddUniqueInter(dd,f->index,T,Cudd_Not(one)); if (res == NULL){ Cudd_IterDerefBdd(dd,T); return NULL; } cuddDeref(T); } else { cuddRef(E); res1 = cuddBddAndRecur(dd,T,E); if (res1 == NULL){ Cudd_IterDerefBdd(dd,T); Cudd_IterDerefBdd(dd,E); return(NULL); } cuddRef(res1); Cudd_IterDerefBdd(dd,T); Cudd_IterDerefBdd(dd,E); res = cuddUniqueInter(dd,f->index,res1,Cudd_Not(one)); if (res == NULL){ Cudd_IterDerefBdd(dd,T); Cudd_IterDerefBdd(dd,E); Cudd_IterDerefBdd(dd,res1); return(NULL); } cuddDeref(res1); } cuddCacheInsert1(dd,Cuddaux_Support,f,res); return(res); } /* end of cuddauxSupportRecur */
/**Function******************************************************************** Synopsis [Computes the negative cofactor of a ZDD w.r.t. a variable.] Description [Computes the negative cofactor of a ZDD w.r.t. a variable. In terms of combinations, the result is the set of all combinations in which the variable is negated. Returns a pointer to the result if successful; NULL otherwise. cuddZddSubset0 performs the same function as Cudd_zddSubset0, but does not restart if reordering has taken place. Therefore it can be called from within a recursive procedure.] SideEffects [None] SeeAlso [cuddZddSubset1 Cudd_zddSubset0] ******************************************************************************/ DdNode * cuddZddSubset0( DdManager * dd, DdNode * P, int var) { DdNode *zvar, *r; DdNode *base, *empty; base = DD_ONE(dd); empty = DD_ZERO(dd); zvar = cuddUniqueInterZdd(dd, var, base, empty); if (zvar == NULL) { return(NULL); } else { cuddRef(zvar); r = zdd_subset0_aux(dd, P, zvar); if (r == NULL) { Cudd_RecursiveDerefZdd(dd, zvar); return(NULL); } cuddRef(r); Cudd_RecursiveDerefZdd(dd, zvar); } cuddDeref(r); return(r); } /* end of cuddZddSubset0 */
/**Function******************************************************************** Synopsis [Performs the recursive step of Cuddaux_IsVarIn.] Description [Performs the recursive step of Cuddaux_IsVarIn. var is supposed to be a BDD projection function. Returns the logical one or zero.] SideEffects [None] SeeAlso [] ******************************************************************************/ DdNode* cuddauxIsVarInRecur(DdManager* manager, DdNode* f, DdNode* Var) { DdNode *zero,*one, *F, *res; int topV,topF; one = DD_ONE(manager); zero = Cudd_Not(one); F = Cudd_Regular(f); if (cuddIsConstant(F)) return zero; if (Var==F) return(one); topV = Var->index; topF = F->index; if (topF == topV) return(one); if (cuddI(manager,topV) < cuddI(manager,topF)) return(zero); res = cuddCacheLookup2(manager,cuddauxIsVarInRecur, F, Var); if (res != NULL) return(res); res = cuddauxIsVarInRecur(manager,cuddT(F),Var); if (res==zero){ res = cuddauxIsVarInRecur(manager,cuddE(F),Var); } cuddCacheInsert2(manager,cuddauxIsVarInRecur,F,Var,res); return(res); }
/** @brief Computes the boolean difference of f with respect to x. @details Computes the boolean difference of f with respect to the variable with index x. @return the %BDD of the boolean difference if successful; NULL otherwise. @sideeffect None */ DdNode * Cudd_bddBooleanDiff( DdManager * manager, DdNode * f, int x) { DdNode *res, *var; /* If the variable is not currently in the manager, f cannot ** depend on it. */ if (x >= manager->size) return(Cudd_Not(DD_ONE(manager))); var = manager->vars[x]; do { manager->reordered = 0; res = cuddBddBooleanDiffRecur(manager, Cudd_Regular(f), var); } while (manager->reordered == 1); if (manager->errorCode == CUDD_TIMEOUT_EXPIRED && manager->timeoutHandler) { manager->timeoutHandler(manager, manager->tohArg); } return(res); } /* end of Cudd_bddBooleanDiff */
/**Function******************************************************************** Synopsis [Performs the recursive step of Cudd_zddPrintCover.] Description [] SideEffects [None] SeeAlso [] ******************************************************************************/ static void zddPrintCoverAux( DdManager * zdd /* manager */, DdNode * node /* current node */, int level /* depth in the recursion */, int * list /* current recursion path */) { DdNode *Nv, *Nnv; int i, v; DdNode *base = DD_ONE(zdd); if (Cudd_IsConstant(node)) { if (node == base) { /* Check for missing variable. */ if (level != zdd->sizeZ) { list[zdd->invpermZ[level]] = 0; zddPrintCoverAux(zdd, node, level + 1, list); return; } /* Terminal case: Print one cube based on the current recursion ** path. */ for (i = 0; i < zdd->sizeZ; i += 2) { v = list[i] * 4 + list[i+1]; if (v == 0) (void) fprintf(zdd->out,"-"); else if (v == 4) (void) fprintf(zdd->out,"1"); else if (v == 1) (void) fprintf(zdd->out,"0"); else (void) fprintf(zdd->out,"@"); /* should never happen */ } (void) fprintf(zdd->out," 1\n"); } } else { /* Check for missing variable. */ if (level != cuddIZ(zdd,node->index)) { list[zdd->invpermZ[level]] = 0; zddPrintCoverAux(zdd, node, level + 1, list); return; } Nnv = cuddE(node); Nv = cuddT(node); if (Nv == Nnv) { list[node->index] = 2; zddPrintCoverAux(zdd, Nnv, level + 1, list); return; } list[node->index] = 1; zddPrintCoverAux(zdd, Nv, level + 1, list); list[node->index] = 0; zddPrintCoverAux(zdd, Nnv, level + 1, list); } return; } /* end of zddPrintCoverAux */
/**Function******************************************************************** Synopsis [Annotates every node in the BDD node with its minterm count.] Description [Annotates every node in the BDD node with its minterm count. In this function, every node and the minterm count represented by it are stored in a hash table.] SideEffects [Fills up 'table' with the pair <node,minterm_count>.] ******************************************************************************/ static double bddAnnotateMintermCount( DdManager * manager, DdNode * node, double max, st_table * table) { DdNode *N,*Nv,*Nnv; register double min_v,min_nv; register double min_N; double *pmin; double *dummy; statLine(manager); N = Cudd_Regular(node); if (cuddIsConstant(N)) { if (node == DD_ONE(manager)) { return(max); } else { return(0.0); } } if (st_lookup(table, node, &dummy)) { return(*dummy); } Nv = cuddT(N); Nnv = cuddE(N); if (N != node) { Nv = Cudd_Not(Nv); Nnv = Cudd_Not(Nnv); } /* Recur on the two branches. */ min_v = bddAnnotateMintermCount(manager,Nv,max,table) / 2.0; if (min_v == (double)CUDD_OUT_OF_MEM) return ((double)CUDD_OUT_OF_MEM); min_nv = bddAnnotateMintermCount(manager,Nnv,max,table) / 2.0; if (min_nv == (double)CUDD_OUT_OF_MEM) return ((double)CUDD_OUT_OF_MEM); min_N = min_v + min_nv; pmin = ALLOC(double,1); if (pmin == NULL) { manager->errorCode = CUDD_MEMORY_OUT; return((double)CUDD_OUT_OF_MEM); } *pmin = min_N; if (st_insert(table,(char *)node, (char *)pmin) == ST_OUT_OF_MEM) { FREE(pmin); return((double)CUDD_OUT_OF_MEM); } return(min_N); } /* end of bddAnnotateMintermCount */
DdNode* cuddauxAddGuardOfNodeRecur(DdManager* manager, DdNode* f, DdNode* h) { DdNode *one, *res, *T, *E; int topf, toph; /* Handle terminal cases */ one = DD_ONE(manager); if (f==h){ return(one); } topf = cuddI(manager,f->index); toph = cuddI(manager,h->index); if (topf >= toph){ return Cudd_Not(one); } /* Look in the cache */ res = cuddCacheLookup2(manager,Cuddaux_addGuardOfNode,f,h); if (res != NULL) return(res); T = cuddauxAddGuardOfNodeRecur(manager,cuddT(f),h); if (T == NULL) return(NULL); cuddRef(T); E = cuddauxAddGuardOfNodeRecur(manager,cuddE(f),h); if (E == NULL){ Cudd_IterDerefBdd(manager, T); return(NULL); } cuddRef(E); if (T == E){ res = T; } else { if (Cudd_IsComplement(T)){ res = cuddUniqueInter(manager,f->index,Cudd_Not(T),Cudd_Not(E)); if (res == NULL) { Cudd_IterDerefBdd(manager, T); Cudd_IterDerefBdd(manager, E); return(NULL); } res = Cudd_Not(res); } else { res = cuddUniqueInter(manager,f->index,T,E); if (res == NULL) { Cudd_IterDerefBdd(manager, T); Cudd_IterDerefBdd(manager, E); return(NULL); } } } cuddDeref(T); cuddDeref(E); cuddCacheInsert2(manager,Cuddaux_addGuardOfNode,f,h,res); return(res); }
/**Function******************************************************************** Synopsis [Reads in a sparse matrix.] Description [Reads in a sparse matrix specified in a simple format. The first line of the input contains the numbers of rows and columns. The remaining lines contain the elements of the matrix, one per line. Given a background value (specified by the background field of the manager), only the values different from it are explicitly listed. Each foreground element is described by two integers, i.e., the row and column number, and a real number, i.e., the value.<p> Cudd_addRead produces an ADD that depends on two sets of variables: x and y. The x variables (x\[0\] ... x\[nx-1\]) encode the row index and the y variables (y\[0\] ... y\[ny-1\]) encode the column index. x\[0\] and y\[0\] are the most significant bits in the indices. The variables may already exist or may be created by the function. The index of x\[i\] is bx+i*sx, and the index of y\[i\] is by+i*sy.<p> On input, nx and ny hold the numbers of row and column variables already in existence. On output, they hold the numbers of row and column variables actually used by the matrix. When Cudd_addRead creates the variable arrays, the index of x\[i\] is bx+i*sx, and the index of y\[i\] is by+i*sy. When some variables already exist Cudd_addRead expects the indices of the existing x variables to be bx+i*sx, and the indices of the existing y variables to be by+i*sy.<p> m and n are set to the numbers of rows and columns of the matrix. Their values on input are immaterial. The ADD for the sparse matrix is returned in E, and its reference count is > 0. Cudd_addRead returns 1 in case of success; 0 otherwise.] SideEffects [nx and ny are set to the numbers of row and column variables. m and n are set to the numbers of rows and columns. x and y are possibly extended to represent the array of row and column variables. Similarly for xn and yn_, which hold on return from Cudd_addRead the complements of the row and column variables.] SeeAlso [Cudd_addHarwell Cudd_bddRead] ******************************************************************************/ int Cudd_addRead( FILE * fp /* input file pointer */, DdManager * dd /* DD manager */, DdNode ** E /* characteristic function of the graph */, DdNode *** x /* array of row variables */, DdNode *** y /* array of column variables */, DdNode *** xn /* array of complemented row variables */, DdNode *** yn_ /* array of complemented column variables */, int * nx /* number or row variables */, int * ny /* number or column variables */, int * m /* number of rows */, int * n /* number of columns */, int bx /* first index of row variables */, int sx /* step of row variables */, int by /* first index of column variables */, int sy /* step of column variables */) { DdNode *one, *zero; DdNode *w, *neW; DdNode *minterm1; int u, v, err, i, nv; int lnx, lny; CUDD_VALUE_TYPE val; DdNode **lx, **ly, **lxn, **lyn; one = DD_ONE(dd); zero = DD_ZERO(dd); err = fscanf(fp, "%d %d", &u, &v); if (err == EOF) { return(0); } else if (err != 2) { return(0); } *m = u; /* Compute the number of x variables. */ lx = *x; lxn = *xn; u--; /* row and column numbers start from 0 */ for (lnx=0; u > 0; lnx++) { u >>= 1; } /* Here we rely on the fact that REALLOC of a null pointer is ** translates to an ALLOC. */ if (lnx > *nx) { *x = lx = REALLOC(DdNode *, *x, lnx); if (lx == NULL) { dd->errorCode = CUDD_MEMORY_OUT; return(0); } *xn = lxn = REALLOC(DdNode *, *xn, lnx); if (lxn == NULL) { dd->errorCode = CUDD_MEMORY_OUT; return(0); } }
/**Function******************************************************************** Synopsis [Recursive procedure to extract n mintems from constant 1.] Description [Recursive procedure to extract n mintems from constant 1.] SideEffects [None] ******************************************************************************/ static DdNode * mintermsFromUniverse( DdManager * manager, DdNode ** vars, int numVars, double n, int index) { DdNode *one, *zero; DdNode *q, *result; double max, max2; statLine(manager); one = DD_ONE(manager); zero = Cudd_Not(one); max = pow(2.0, (double)numVars); max2 = max / 2.0; if (n == max) return(one); if (n == 0.0) return(zero); /* if n == 2^(numVars-1), return a single variable */ if (n == max2) return vars[index]; else if (n > max2) { /* When n > 2^(numVars-1), a single variable vars[index] ** contains 2^(numVars-1) minterms. The rest are extracted ** from a constant with 1 less variable. */ q = mintermsFromUniverse(manager,vars,numVars-1,(n-max2),index+1); if (q == NULL) return(NULL); cuddRef(q); result = cuddBddIteRecur(manager,vars[index],one,q); } else { /* When n < 2^(numVars-1), a literal of variable vars[index] ** is selected. The required n minterms are extracted from a ** constant with 1 less variable. */ q = mintermsFromUniverse(manager,vars,numVars-1,n,index+1); if (q == NULL) return(NULL); cuddRef(q); result = cuddBddAndRecur(manager,vars[index],q); } if (result == NULL) { Cudd_RecursiveDeref(manager,q); return(NULL); } cuddRef(result); Cudd_RecursiveDeref(manager,q); cuddDeref(result); return(result); } /* end of mintermsFromUniverse */
/**Function******************************************************************** Synopsis [NOR of two 0-1 ADDs.] Description [NOR of two 0-1 ADDs. Returns NULL if not a terminal case; f NOR g otherwise.] SideEffects [None] SeeAlso [Cudd_addApply] ******************************************************************************/ DdNode * Cudd_addNor( DdManager * dd, DdNode ** f, DdNode ** g) { DdNode *F, *G; F = *f; G = *g; if (F == DD_ONE(dd) || G == DD_ONE(dd)) return(DD_ZERO(dd)); if (cuddIsConstant(F) && cuddIsConstant(G)) return(DD_ONE(dd)); if (F > G) { /* swap f and g */ *f = G; *g = F; } return(NULL); } /* end of Cudd_addNor */
/**Function******************************************************************** Synopsis [Performs the recursive steps of Cudd_bddBoleanDiff.] Description [Performs the recursive steps of Cudd_bddBoleanDiff. Returns the BDD obtained by XORing the cofactors of f with respect to var if successful; NULL otherwise. Exploits the fact that dF/dx = dF'/dx.] SideEffects [None] SeeAlso [] ******************************************************************************/ DdNode * cuddBddBooleanDiffRecur( DdManager * manager, DdNode * f, DdNode * var) { DdNode *T, *E, *res, *res1, *res2; statLine(manager); if (cuddI(manager,f->index) > manager->perm[var->index]) { /* f does not depend on var. */ return(Cudd_Not(DD_ONE(manager))); } /* From now on, f is non-constant. */ /* If the two indices are the same, so are their levels. */ if (f->index == var->index) { res = cuddBddXorRecur(manager, cuddT(f), cuddE(f)); return(res); } /* From now on, cuddI(manager,f->index) < cuddI(manager,cube->index). */ /* Check the cache. */ res = cuddCacheLookup2(manager, cuddBddBooleanDiffRecur, f, var); if (res != NULL) { return(res); } /* Compute the cofactors of f. */ T = cuddT(f); E = cuddE(f); res1 = cuddBddBooleanDiffRecur(manager, T, var); if (res1 == NULL) return(NULL); cuddRef(res1); res2 = cuddBddBooleanDiffRecur(manager, Cudd_Regular(E), var); if (res2 == NULL) { Cudd_IterDerefBdd(manager, res1); return(NULL); } cuddRef(res2); /* ITE takes care of possible complementation of res1 and of the ** case in which res1 == res2. */ res = cuddBddIteRecur(manager, manager->vars[f->index], res1, res2); if (res == NULL) { Cudd_IterDerefBdd(manager, res1); Cudd_IterDerefBdd(manager, res2); return(NULL); } cuddDeref(res1); cuddDeref(res2); cuddCacheInsert2(manager, cuddBddBooleanDiffRecur, f, var, res); return(res); } /* end of cuddBddBooleanDiffRecur */
/**Function******************************************************************** Synopsis [Performs the recursive step of Cudd_zddChange.] Description [] SideEffects [None] SeeAlso [] ******************************************************************************/ DdNode * cuddZddChangeAux( DdManager * zdd, DdNode * P, DdNode * zvar) { int top_var, level; DdNode *res, *t, *e; DdNode *base = DD_ONE(zdd); DdNode *empty = DD_ZERO(zdd); statLine(zdd); if (P == empty) return(empty); if (P == base) return(zvar); /* Check cache. */ res = cuddCacheLookup2Zdd(zdd, cuddZddChangeAux, P, zvar); if (res != NULL) return(res); top_var = zdd->permZ[P->index]; level = zdd->permZ[zvar->index]; if (top_var > level) { res = cuddZddGetNode(zdd, zvar->index, P, DD_ZERO(zdd)); if (res == NULL) return(NULL); } else if (top_var == level) { res = cuddZddGetNode(zdd, zvar->index, cuddE(P), cuddT(P)); if (res == NULL) return(NULL); } else { t = cuddZddChangeAux(zdd, cuddT(P), zvar); if (t == NULL) return(NULL); cuddRef(t); e = cuddZddChangeAux(zdd, cuddE(P), zvar); if (e == NULL) { Cudd_RecursiveDerefZdd(zdd, t); return(NULL); } cuddRef(e); res = cuddZddGetNode(zdd, P->index, t, e); if (res == NULL) { Cudd_RecursiveDerefZdd(zdd, t); Cudd_RecursiveDerefZdd(zdd, e); return(NULL); } cuddDeref(t); cuddDeref(e); } cuddCacheInsert2(zdd, cuddZddChangeAux, P, zvar, res); return(res); } /* end of cuddZddChangeAux */
/**Function******************************************************************** Synopsis [Performs the recursive step for Cudd_addIthBit.] Description [Performs the recursive step for Cudd_addIthBit. Returns a pointer to the BDD if successful; NULL otherwise.] SideEffects [None] SeeAlso [] ******************************************************************************/ static DdNode * addDoIthBit( DdManager * dd, DdNode * f, DdNode * index) { DdNode *res, *T, *E; DdNode *fv, *fvn; int mask, value; int v; statLine(dd); /* Check terminal case. */ if (cuddIsConstant(f)) { mask = 1 << ((int) cuddV(index)); value = (int) cuddV(f); return((value & mask) == 0 ? DD_ZERO(dd) : DD_ONE(dd)); } /* Check cache. */ res = cuddCacheLookup2(dd,addDoIthBit,f,index); if (res != NULL) return(res); /* Recursive step. */ v = f->index; fv = cuddT(f); fvn = cuddE(f); T = addDoIthBit(dd,fv,index); if (T == NULL) return(NULL); cuddRef(T); E = addDoIthBit(dd,fvn,index); if (E == NULL) { Cudd_RecursiveDeref(dd, T); return(NULL); } cuddRef(E); res = (T == E) ? T : cuddUniqueInter(dd,v,T,E); if (res == NULL) { Cudd_RecursiveDeref(dd, T); Cudd_RecursiveDeref(dd, E); return(NULL); } cuddDeref(T); cuddDeref(E); /* Store result. */ cuddCacheInsert2(dd,addDoIthBit,f,index,res); return(res); } /* end of addDoIthBit */
/**Function******************************************************************** Synopsis [This function prepares an array of variables which have not been encountered so far when traversing the procedure cuddSplitSetRecur.] Description [This function prepares an array of variables which have not been encountered so far when traversing the procedure cuddSplitSetRecur. This array is then used to extract the required number of minterms from a constant 1. The algorithm guarantees that the size of BDD will be utmost \log(n).] SideEffects [None] ******************************************************************************/ static DdNode * selectMintermsFromUniverse( DdManager * manager, int * varSeen, double n) { int numVars; int i, size, j; DdNode *one, *zero, *result; DdNode **vars; numVars = 0; size = manager->size; one = DD_ONE(manager); zero = Cudd_Not(one); /* Count the number of variables not encountered so far in procedure ** cuddSplitSetRecur. */ for (i = size-1; i >= 0; i--) { if(varSeen[i] == 0) numVars++; } vars = ALLOC(DdNode *, numVars); if (!vars) { manager->errorCode = CUDD_MEMORY_OUT; return(NULL); } j = 0; for (i = size-1; i >= 0; i--) { if(varSeen[i] == 0) { vars[j] = cuddUniqueInter(manager,manager->perm[i],one,zero); cuddRef(vars[j]); j++; } } /* Compute a function which has n minterms and depends on at most ** numVars variables. */ result = mintermsFromUniverse(manager,vars,numVars,n, 0); if (result) cuddRef(result); for (i = 0; i < numVars; i++) Cudd_RecursiveDeref(manager,vars[i]); FREE(vars); return(result); } /* end of selectMintermsFromUniverse */
/**Function******************************************************************** Synopsis [Checks whether a variable is dependent on others in a function.] Description [Checks whether a variable is dependent on others in a function. Returns 1 if the variable is dependent; 0 otherwise. No new nodes are created.] SideEffects [None] SeeAlso [] ******************************************************************************/ int Cudd_bddVarIsDependent( DdManager *dd, /* manager */ DdNode *f, /* function */ DdNode *var /* variable */) { DdNode *F, *res, *zero, *ft, *fe; unsigned topf, level; DdNode *(*cacheOp)(DdManager *, DdNode *, DdNode *); int retval; zero = Cudd_Not(DD_ONE(dd)); if (Cudd_IsConstant(f)) return(f == zero); /* From now on f is not constant. */ F = Cudd_Regular(f); topf = (unsigned) dd->perm[F->index]; level = (unsigned) dd->perm[var->index]; /* Check terminal case. If topf > index of var, f does not depend on var. ** Therefore, var is not dependent in f. */ if (topf > level) { return(0); } cacheOp = (DdNode *(*)(DdManager *, DdNode *, DdNode *)) Cudd_bddVarIsDependent; res = cuddCacheLookup2(dd,cacheOp,f,var); if (res != NULL) { return(res != zero); } /* Compute cofactors. */ ft = Cudd_NotCond(cuddT(F), f != F); fe = Cudd_NotCond(cuddE(F), f != F); if (topf == level) { retval = Cudd_bddLeq(dd,ft,Cudd_Not(fe)); } else { retval = Cudd_bddVarIsDependent(dd,ft,var) && Cudd_bddVarIsDependent(dd,fe,var); } cuddCacheInsert2(dd,cacheOp,f,var,Cudd_NotCond(zero,retval)); return(retval); } /* Cudd_bddVarIsDependent */
/**Function******************************************************************** Synopsis [Performs the recursive step of Cudd_addCmpl.] Description [Performs the recursive step of Cudd_addCmpl. Returns a pointer to the resulting ADD if successful; NULL otherwise.] SideEffects [None] SeeAlso [Cudd_addCmpl] ******************************************************************************/ DdNode * cuddAddCmplRecur( DdManager * dd, DdNode * f) { DdNode *one,*zero; DdNode *r,*Fv,*Fnv,*t,*e; statLine(dd); one = DD_ONE(dd); zero = DD_ZERO(dd); if (cuddIsConstant(f)) { if (f == zero) { return(one); } else { return(zero); } } r = cuddCacheLookup1(dd,Cudd_addCmpl,f); if (r != NULL) { return(r); } Fv = cuddT(f); Fnv = cuddE(f); t = cuddAddCmplRecur(dd,Fv); if (t == NULL) return(NULL); cuddRef(t); e = cuddAddCmplRecur(dd,Fnv); if (e == NULL) { Cudd_RecursiveDeref(dd,t); return(NULL); } cuddRef(e); r = (t == e) ? t : cuddUniqueInter(dd,(int)f->index,t,e); if (r == NULL) { Cudd_RecursiveDeref(dd, t); Cudd_RecursiveDeref(dd, e); return(NULL); } cuddDeref(t); cuddDeref(e); cuddCacheInsert1(dd,Cudd_addCmpl,f,r); return(r); } /* end of cuddAddCmplRecur */
/**Function******************************************************************** Synopsis [Initializes the ZDD universe.] Description [Initializes the ZDD universe. Returns 1 if successful; 0 otherwise.] SideEffects [None] SeeAlso [cuddZddFreeUniv] ******************************************************************************/ int cuddZddInitUniv( DdManager * zdd) { DdNode *p, *res; int i; #ifdef __osf__ #pragma pointer_size save #pragma pointer_size short #endif zdd->univ = ALLOC(DdNode *, zdd->sizeZ); #ifdef __osf__ #pragma pointer_size restore #endif if (zdd->univ == NULL) { zdd->errorCode = CUDD_MEMORY_OUT; return(0); } res = DD_ONE(zdd); cuddRef(res); for (i = zdd->sizeZ - 1; i >= 0; i--) { unsigned int index = zdd->invpermZ[i]; p = res; res = cuddUniqueInterZdd(zdd, index, p, p); if (res == NULL) { Cudd_RecursiveDerefZdd(zdd,p); FREE(zdd->univ); return(0); } cuddRef(res); cuddDeref(p); zdd->univ[i] = res; } #ifdef DD_VERBOSE cuddZddP(zdd, zdd->univ[0]); #endif return(1); } /* end of cuddZddInitUniv */
/**Function******************************************************************** Synopsis [Returns 1 if f > g and 0 otherwise.] Description [Returns 1 if f > g (both should be terminal cases) and 0 otherwise. Used in conjunction with Cudd_addApply. Returns NULL if not a terminal case.] SideEffects [None] SeeAlso [Cudd_addApply] ******************************************************************************/ DdNode * Cudd_addOneZeroMaximum( DdManager * dd, DdNode ** f, DdNode ** g) { if (*g == DD_PLUS_INFINITY(dd)) return DD_ZERO(dd); if (cuddIsConstant(*f) && cuddIsConstant(*g)) { if (cuddV(*f) > cuddV(*g)) { return(DD_ONE(dd)); } else { return(DD_ZERO(dd)); } } return(NULL); } /* end of Cudd_addOneZeroMaximum */
/**Function******************************************************************** Synopsis [Computes the cofactor of f with respect to g.] Description [Computes the cofactor of f with respect to g; g must be the BDD or the ADD of a cube. Returns a pointer to the cofactor if successful; NULL otherwise.] SideEffects [None] SeeAlso [Cudd_bddConstrain Cudd_bddRestrict] ******************************************************************************/ DdNode * Cudd_Cofactor( DdManager * dd, DdNode * f, DdNode * g) { DdNode *res,*zero; zero = Cudd_Not(DD_ONE(dd)); if (g == zero || g == DD_ZERO(dd)) { (void) fprintf(stdout,"Cudd_Cofactor: Invalid restriction 1\n"); return(NULL); } do { dd->reordered = 0; res = cuddCofactorRecur(dd,f,g); } while (dd->reordered == 1); return(res); } /* end of Cudd_Cofactor */
/**Function******************************************************************** Synopsis [Integer and floating point division.] Description [Integer and floating point division. Returns NULL if not a terminal case; f / g otherwise.] SideEffects [None] SeeAlso [Cudd_addApply] ******************************************************************************/ DdNode * Cudd_addDivide( DdManager * dd, DdNode ** f, DdNode ** g) { DdNode *res; DdNode *F, *G; CUDD_VALUE_TYPE value; F = *f; G = *g; if (F == DD_ZERO(dd)) return(DD_ZERO(dd)); if (G == DD_ONE(dd)) return(F); if (cuddIsConstant(F) && cuddIsConstant(G)) { value = cuddV(F)/cuddV(G); res = cuddUniqueConst(dd,value); return(res); } return(NULL); } /* end of Cudd_addDivide */
/**Function******************************************************************** Synopsis [Computes the boolean difference of f with respect to x.] Description [Computes the boolean difference of f with respect to the variable with index x. Returns the BDD of the boolean difference if successful; NULL otherwise.] SideEffects [None] SeeAlso [] ******************************************************************************/ DdNode * Cudd_bddBooleanDiff( DdManager * manager, DdNode * f, int x) { DdNode *res, *var; /* If the variable is not currently in the manager, f cannot ** depend on it. */ if (x >= manager->size) return(Cudd_Not(DD_ONE(manager))); var = manager->vars[x]; do { manager->reordered = 0; res = cuddBddBooleanDiffRecur(manager, Cudd_Regular(f), var); } while (manager->reordered == 1); return(res); } /* end of Cudd_bddBooleanDiff */
/**Function******************************************************************** Synopsis [Substitutes a variable with its complement in a ZDD.] Description [Substitutes a variable with its complement in a ZDD. returns a pointer to the result if successful; NULL otherwise. cuddZddChange performs the same function as Cudd_zddChange, but does not restart if reordering has taken place. Therefore it can be called from within a recursive procedure.] SideEffects [None] SeeAlso [Cudd_zddChange] ******************************************************************************/ DdNode * cuddZddChange( DdManager * dd, DdNode * P, int var) { DdNode *zvar, *res; zvar = cuddUniqueInterZdd(dd, var, DD_ONE(dd), DD_ZERO(dd)); if (zvar == NULL) return(NULL); cuddRef(zvar); res = cuddZddChangeAux(dd, P, zvar); if (res == NULL) { Cudd_RecursiveDerefZdd(dd,zvar); return(NULL); } cuddRef(res); Cudd_RecursiveDerefZdd(dd,zvar); cuddDeref(res); return(res); } /* end of cuddZddChange */
/**Function******************************************************************** Synopsis [Counts the number of minterms in a ZDD.] Description [Returns an integer representing the number of minterms in a ZDD.] SideEffects [None] SeeAlso [Cudd_zddCountDouble] ******************************************************************************/ int Cudd_zddCount( DdManager * zdd, DdNode * P) { st_table *table; int res; DdNode *base, *empty; base = DD_ONE(zdd); empty = DD_ZERO(zdd); table = st_init_table(st_ptrcmp, st_ptrhash); if (table == NULL) return(CUDD_OUT_OF_MEM); res = cuddZddCountStep(P, table, base, empty); if (res == CUDD_OUT_OF_MEM) { zdd->errorCode = CUDD_MEMORY_OUT; } st_foreach(table, st_zdd_countfree, NIL(char)); st_free_table(table); return(res); } /* end of Cudd_zddCount */
/**Function******************************************************************** Synopsis [Checks whether g is the BDD of a cube.] Description [Checks whether g is the BDD of a cube. Returns 1 in case of success; 0 otherwise. The constant 1 is a valid cube, but all other constant functions cause cuddCheckCube to return 0.] SideEffects [None] SeeAlso [] ******************************************************************************/ int cuddCheckCube( DdManager * dd, DdNode * g) { DdNode *g1,*g0,*one,*zero; one = DD_ONE(dd); if (g == one) return(1); if (Cudd_IsConstant(g)) return(0); zero = Cudd_Not(one); cuddGetBranches(g,&g1,&g0); if (g0 == zero) { return(cuddCheckCube(dd, g1)); } if (g1 == zero) { return(cuddCheckCube(dd, g0)); } return(0); } /* end of cuddCheckCube */
/**Function******************************************************************** Synopsis [Integer and floating point division.] Description [Integer and floating point division. Returns NULL if not a terminal case; f / g otherwise.] SideEffects [None] SeeAlso [Cudd_addApply] ******************************************************************************/ DdNode * Cudd_addDivide( DdManager * dd, DdNode ** f, DdNode ** g) { DdNode *res; DdNode *F, *G; CUDD_VALUE_TYPE value; F = *f; G = *g; /* We would like to use F == G -> F/G == 1, but F and G may ** contain zeroes. */ if (F == DD_ZERO(dd)) return(DD_ZERO(dd)); if (G == DD_ONE(dd)) return(F); if (cuddIsConstant(F) && cuddIsConstant(G)) { value = cuddV(F)/cuddV(G); res = cuddUniqueConst(dd,value); return(res); } return(NULL); } /* end of Cudd_addDivide */
/**Function******************************************************************** Synopsis [Calculates the product of two matrices represented as ADDs.] Description [Calculates the product of two matrices, A and B, represented as ADDs, using the CMU matrix by matrix multiplication procedure by Clarke et al.. Matrix A has x's as row variables and z's as column variables, while matrix B has z's as row variables and y's as column variables. Returns the pointer to the result if successful; NULL otherwise. The resulting matrix has x's as row variables and y's as column variables.] SideEffects [None] SeeAlso [Cudd_addMatrixMultiply] ******************************************************************************/ DdNode * Cudd_addTimesPlus( DdManager * dd, DdNode * A, DdNode * B, DdNode ** z, int nz) { DdNode *w, *cube, *tmp, *res; int i; tmp = Cudd_addApply(dd,Cudd_addTimes,A,B); if (tmp == NULL) return(NULL); Cudd_Ref(tmp); Cudd_Ref(cube = DD_ONE(dd)); for (i = nz-1; i >= 0; i--) { w = Cudd_addIte(dd,z[i],cube,DD_ZERO(dd)); if (w == NULL) { Cudd_RecursiveDeref(dd,tmp); return(NULL); } Cudd_Ref(w); Cudd_RecursiveDeref(dd,cube); cube = w; } res = Cudd_addExistAbstract(dd,tmp,cube); if (res == NULL) { Cudd_RecursiveDeref(dd,tmp); Cudd_RecursiveDeref(dd,cube); return(NULL); } Cudd_Ref(res); Cudd_RecursiveDeref(dd,cube); Cudd_RecursiveDeref(dd,tmp); Cudd_Deref(res); return(res); } /* end of Cudd_addTimesPlus */