You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4032 lines
107 KiB

  1. /**CFile***********************************************************************
  2. FileName [cuddUtil.c]
  3. PackageName [cudd]
  4. Synopsis [Utility functions.]
  5. Description [External procedures included in this module:
  6. <ul>
  7. <li> Cudd_PrintMinterm()
  8. <li> Cudd_bddPrintCover()
  9. <li> Cudd_PrintDebug()
  10. <li> Cudd_DagSize()
  11. <li> Cudd_EstimateCofactor()
  12. <li> Cudd_EstimateCofactorSimple()
  13. <li> Cudd_SharingSize()
  14. <li> Cudd_CountMinterm()
  15. <li> Cudd_EpdCountMinterm()
  16. <li> Cudd_CountPath()
  17. <li> Cudd_CountPathsToNonZero()
  18. <li> Cudd_SupportIndices()
  19. <li> Cudd_Support()
  20. <li> Cudd_SupportIndex()
  21. <li> Cudd_SupportSize()
  22. <li> Cudd_VectorSupportIndices()
  23. <li> Cudd_VectorSupport()
  24. <li> Cudd_VectorSupportIndex()
  25. <li> Cudd_VectorSupportSize()
  26. <li> Cudd_ClassifySupport()
  27. <li> Cudd_CountLeaves()
  28. <li> Cudd_bddPickOneCube()
  29. <li> Cudd_bddPickOneMinterm()
  30. <li> Cudd_bddPickArbitraryMinterms()
  31. <li> Cudd_SubsetWithMaskVars()
  32. <li> Cudd_FirstCube()
  33. <li> Cudd_NextCube()
  34. <li> Cudd_bddComputeCube()
  35. <li> Cudd_addComputeCube()
  36. <li> Cudd_FirstNode()
  37. <li> Cudd_NextNode()
  38. <li> Cudd_GenFree()
  39. <li> Cudd_IsGenEmpty()
  40. <li> Cudd_IndicesToCube()
  41. <li> Cudd_PrintVersion()
  42. <li> Cudd_AverageDistance()
  43. <li> Cudd_Random()
  44. <li> Cudd_Srandom()
  45. <li> Cudd_Density()
  46. </ul>
  47. Internal procedures included in this module:
  48. <ul>
  49. <li> cuddP()
  50. <li> cuddStCountfree()
  51. <li> cuddCollectNodes()
  52. <li> cuddNodeArray()
  53. </ul>
  54. Static procedures included in this module:
  55. <ul>
  56. <li> dp2()
  57. <li> ddPrintMintermAux()
  58. <li> ddDagInt()
  59. <li> ddCountMintermAux()
  60. <li> ddEpdCountMintermAux()
  61. <li> ddCountPathAux()
  62. <li> ddSupportStep()
  63. <li> ddClearFlag()
  64. <li> ddLeavesInt()
  65. <li> ddPickArbitraryMinterms()
  66. <li> ddPickRepresentativeCube()
  67. <li> ddEpdFree()
  68. <li> ddFindSupport()
  69. <li> ddClearVars()
  70. <li> indexCompare()
  71. </ul>]
  72. Author [Fabio Somenzi]
  73. Copyright [Copyright (c) 1995-2012, Regents of the University of Colorado
  74. All rights reserved.
  75. Redistribution and use in source and binary forms, with or without
  76. modification, are permitted provided that the following conditions
  77. are met:
  78. Redistributions of source code must retain the above copyright
  79. notice, this list of conditions and the following disclaimer.
  80. Redistributions in binary form must reproduce the above copyright
  81. notice, this list of conditions and the following disclaimer in the
  82. documentation and/or other materials provided with the distribution.
  83. Neither the name of the University of Colorado nor the names of its
  84. contributors may be used to endorse or promote products derived from
  85. this software without specific prior written permission.
  86. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  87. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  88. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  89. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  90. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  91. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  92. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  93. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  94. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  95. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  96. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  97. POSSIBILITY OF SUCH DAMAGE.]
  98. ******************************************************************************/
  99. #include "util.h"
  100. #include "cuddInt.h"
  101. /*---------------------------------------------------------------------------*/
  102. /* Constant declarations */
  103. /*---------------------------------------------------------------------------*/
  104. /* Random generator constants. */
  105. #define MODULUS1 2147483563
  106. #define LEQA1 40014
  107. #define LEQQ1 53668
  108. #define LEQR1 12211
  109. #define MODULUS2 2147483399
  110. #define LEQA2 40692
  111. #define LEQQ2 52774
  112. #define LEQR2 3791
  113. #define STAB_SIZE 64
  114. #define STAB_DIV (1 + (MODULUS1 - 1) / STAB_SIZE)
  115. /*---------------------------------------------------------------------------*/
  116. /* Stucture declarations */
  117. /*---------------------------------------------------------------------------*/
  118. /*---------------------------------------------------------------------------*/
  119. /* Type declarations */
  120. /*---------------------------------------------------------------------------*/
  121. /*---------------------------------------------------------------------------*/
  122. /* Variable declarations */
  123. /*---------------------------------------------------------------------------*/
  124. #ifndef lint
  125. static char rcsid[] DD_UNUSED = "$Id: cuddUtil.c,v 1.83 2012/02/05 01:07:19 fabio Exp $";
  126. #endif
  127. static DdNode *background, *zero;
  128. static long cuddRand = 0;
  129. static long cuddRand2;
  130. static long shuffleSelect;
  131. static long shuffleTable[STAB_SIZE];
  132. /*---------------------------------------------------------------------------*/
  133. /* Macro declarations */
  134. /*---------------------------------------------------------------------------*/
  135. #define bang(f) ((Cudd_IsComplement(f)) ? '!' : ' ')
  136. #ifdef __cplusplus
  137. extern "C" {
  138. #endif
  139. /**AutomaticStart*************************************************************/
  140. /*---------------------------------------------------------------------------*/
  141. /* Static function prototypes */
  142. /*---------------------------------------------------------------------------*/
  143. static int dp2 (DdManager *dd, DdNode *f, st_table *t);
  144. static void ddPrintMintermAux (DdManager *dd, DdNode *node, int *list);
  145. static int ddDagInt (DdNode *n);
  146. static int cuddNodeArrayRecur (DdNode *f, DdNodePtr *table, int index);
  147. static int cuddEstimateCofactor (DdManager *dd, st_table *table, DdNode * node, int i, int phase, DdNode ** ptr);
  148. static DdNode * cuddUniqueLookup (DdManager * unique, int index, DdNode * T, DdNode * E);
  149. static int cuddEstimateCofactorSimple (DdNode * node, int i);
  150. static double ddCountMintermAux (DdNode *node, double max, DdHashTable *table);
  151. static int ddEpdCountMintermAux (DdNode *node, EpDouble *max, EpDouble *epd, st_table *table);
  152. static double ddCountPathAux (DdNode *node, st_table *table);
  153. static double ddCountPathsToNonZero (DdNode * N, st_table * table);
  154. static void ddSupportStep (DdNode *f, int *support);
  155. static void ddClearFlag (DdNode *f);
  156. static int ddLeavesInt (DdNode *n);
  157. static int ddPickArbitraryMinterms (DdManager *dd, DdNode *node, int nvars, int nminterms, char **string);
  158. static int ddPickRepresentativeCube (DdManager *dd, DdNode *node, double *weight, char *string);
  159. static enum st_retval ddEpdFree (char * key, char * value, char * arg);
  160. static void ddFindSupport(DdManager *dd, DdNode *f, int *SP);
  161. static void ddClearVars(DdManager *dd, int SP);
  162. static int indexCompare(const void *a, const void *b);
  163. /**AutomaticEnd***************************************************************/
  164. #ifdef __cplusplus
  165. }
  166. #endif
  167. /*---------------------------------------------------------------------------*/
  168. /* Definition of exported functions */
  169. /*---------------------------------------------------------------------------*/
  170. /**Function********************************************************************
  171. Synopsis [Prints a disjoint sum of products.]
  172. Description [Prints a disjoint sum of product cover for the function
  173. rooted at node. Each product corresponds to a path from node to a
  174. leaf node different from the logical zero, and different from the
  175. background value. Uses the package default output file. Returns 1
  176. if successful; 0 otherwise.]
  177. SideEffects [None]
  178. SeeAlso [Cudd_PrintDebug Cudd_bddPrintCover]
  179. ******************************************************************************/
  180. int
  181. Cudd_PrintMinterm(
  182. DdManager * manager,
  183. DdNode * node)
  184. {
  185. int i, *list;
  186. background = manager->background;
  187. zero = Cudd_Not(manager->one);
  188. list = ALLOC(int,manager->size);
  189. if (list == NULL) {
  190. manager->errorCode = CUDD_MEMORY_OUT;
  191. return(0);
  192. }
  193. for (i = 0; i < manager->size; i++) list[i] = 2;
  194. ddPrintMintermAux(manager,node,list);
  195. FREE(list);
  196. return(1);
  197. } /* end of Cudd_PrintMinterm */
  198. /**Function********************************************************************
  199. Synopsis [Prints a sum of prime implicants of a BDD.]
  200. Description [Prints a sum of product cover for an incompletely
  201. specified function given by a lower bound and an upper bound. Each
  202. product is a prime implicant obtained by expanding the product
  203. corresponding to a path from node to the constant one. Uses the
  204. package default output file. Returns 1 if successful; 0 otherwise.]
  205. SideEffects [None]
  206. SeeAlso [Cudd_PrintMinterm]
  207. ******************************************************************************/
  208. int
  209. Cudd_bddPrintCover(
  210. DdManager *dd,
  211. DdNode *l,
  212. DdNode *u)
  213. {
  214. int *array;
  215. int q, result;
  216. DdNode *lb;
  217. #ifdef DD_DEBUG
  218. DdNode *cover;
  219. #endif
  220. array = ALLOC(int, Cudd_ReadSize(dd));
  221. if (array == NULL) return(0);
  222. lb = l;
  223. cuddRef(lb);
  224. #ifdef DD_DEBUG
  225. cover = Cudd_ReadLogicZero(dd);
  226. cuddRef(cover);
  227. #endif
  228. while (lb != Cudd_ReadLogicZero(dd)) {
  229. DdNode *implicant, *prime, *tmp;
  230. int length;
  231. implicant = Cudd_LargestCube(dd,lb,&length);
  232. if (implicant == NULL) {
  233. Cudd_RecursiveDeref(dd,lb);
  234. FREE(array);
  235. return(0);
  236. }
  237. cuddRef(implicant);
  238. prime = Cudd_bddMakePrime(dd,implicant,u);
  239. if (prime == NULL) {
  240. Cudd_RecursiveDeref(dd,lb);
  241. Cudd_RecursiveDeref(dd,implicant);
  242. FREE(array);
  243. return(0);
  244. }
  245. cuddRef(prime);
  246. Cudd_RecursiveDeref(dd,implicant);
  247. tmp = Cudd_bddAnd(dd,lb,Cudd_Not(prime));
  248. if (tmp == NULL) {
  249. Cudd_RecursiveDeref(dd,lb);
  250. Cudd_RecursiveDeref(dd,prime);
  251. FREE(array);
  252. return(0);
  253. }
  254. cuddRef(tmp);
  255. Cudd_RecursiveDeref(dd,lb);
  256. lb = tmp;
  257. result = Cudd_BddToCubeArray(dd,prime,array);
  258. if (result == 0) {
  259. Cudd_RecursiveDeref(dd,lb);
  260. Cudd_RecursiveDeref(dd,prime);
  261. FREE(array);
  262. return(0);
  263. }
  264. for (q = 0; q < dd->size; q++) {
  265. switch (array[q]) {
  266. case 0:
  267. (void) fprintf(dd->out, "0");
  268. break;
  269. case 1:
  270. (void) fprintf(dd->out, "1");
  271. break;
  272. case 2:
  273. (void) fprintf(dd->out, "-");
  274. break;
  275. default:
  276. (void) fprintf(dd->out, "?");
  277. }
  278. }
  279. (void) fprintf(dd->out, " 1\n");
  280. #ifdef DD_DEBUG
  281. tmp = Cudd_bddOr(dd,prime,cover);
  282. if (tmp == NULL) {
  283. Cudd_RecursiveDeref(dd,cover);
  284. Cudd_RecursiveDeref(dd,lb);
  285. Cudd_RecursiveDeref(dd,prime);
  286. FREE(array);
  287. return(0);
  288. }
  289. cuddRef(tmp);
  290. Cudd_RecursiveDeref(dd,cover);
  291. cover = tmp;
  292. #endif
  293. Cudd_RecursiveDeref(dd,prime);
  294. }
  295. (void) fprintf(dd->out, "\n");
  296. Cudd_RecursiveDeref(dd,lb);
  297. FREE(array);
  298. #ifdef DD_DEBUG
  299. if (!Cudd_bddLeq(dd,cover,u) || !Cudd_bddLeq(dd,l,cover)) {
  300. Cudd_RecursiveDeref(dd,cover);
  301. return(0);
  302. }
  303. Cudd_RecursiveDeref(dd,cover);
  304. #endif
  305. return(1);
  306. } /* end of Cudd_bddPrintCover */
  307. /**Function********************************************************************
  308. Synopsis [Prints to the standard output a DD and its statistics.]
  309. Description [Prints to the standard output a DD and its statistics.
  310. The statistics include the number of nodes, the number of leaves, and
  311. the number of minterms. (The number of minterms is the number of
  312. assignments to the variables that cause the function to be different
  313. from the logical zero (for BDDs) and from the background value (for
  314. ADDs.) The statistics are printed if pr &gt; 0. Specifically:
  315. <ul>
  316. <li> pr = 0 : prints nothing
  317. <li> pr = 1 : prints counts of nodes and minterms
  318. <li> pr = 2 : prints counts + disjoint sum of product
  319. <li> pr = 3 : prints counts + list of nodes
  320. <li> pr &gt; 3 : prints counts + disjoint sum of product + list of nodes
  321. </ul>
  322. For the purpose of counting the number of minterms, the function is
  323. supposed to depend on n variables. Returns 1 if successful; 0 otherwise.]
  324. SideEffects [None]
  325. SeeAlso [Cudd_DagSize Cudd_CountLeaves Cudd_CountMinterm
  326. Cudd_PrintMinterm]
  327. ******************************************************************************/
  328. int
  329. Cudd_PrintDebug(
  330. DdManager * dd,
  331. DdNode * f,
  332. int n,
  333. int pr)
  334. {
  335. DdNode *azero, *bzero;
  336. int nodes;
  337. int leaves;
  338. double minterms;
  339. int retval = 1;
  340. if (f == NULL) {
  341. (void) fprintf(dd->out,": is the NULL DD\n");
  342. (void) fflush(dd->out);
  343. return(0);
  344. }
  345. azero = DD_ZERO(dd);
  346. bzero = Cudd_Not(DD_ONE(dd));
  347. if ((f == azero || f == bzero) && pr > 0){
  348. (void) fprintf(dd->out,": is the zero DD\n");
  349. (void) fflush(dd->out);
  350. return(1);
  351. }
  352. if (pr > 0) {
  353. nodes = Cudd_DagSize(f);
  354. if (nodes == CUDD_OUT_OF_MEM) retval = 0;
  355. leaves = Cudd_CountLeaves(f);
  356. if (leaves == CUDD_OUT_OF_MEM) retval = 0;
  357. minterms = Cudd_CountMinterm(dd, f, n);
  358. if (minterms == (double)CUDD_OUT_OF_MEM) retval = 0;
  359. (void) fprintf(dd->out,": %d nodes %d leaves %g minterms\n",
  360. nodes, leaves, minterms);
  361. if (pr > 2) {
  362. if (!cuddP(dd, f)) retval = 0;
  363. }
  364. if (pr == 2 || pr > 3) {
  365. if (!Cudd_PrintMinterm(dd,f)) retval = 0;
  366. (void) fprintf(dd->out,"\n");
  367. }
  368. (void) fflush(dd->out);
  369. }
  370. return(retval);
  371. } /* end of Cudd_PrintDebug */
  372. /**Function********************************************************************
  373. Synopsis [Counts the number of nodes in a DD.]
  374. Description [Counts the number of nodes in a DD. Returns the number
  375. of nodes in the graph rooted at node.]
  376. SideEffects [None]
  377. SeeAlso [Cudd_SharingSize Cudd_PrintDebug]
  378. ******************************************************************************/
  379. int
  380. Cudd_DagSize(
  381. DdNode * node)
  382. {
  383. int i;
  384. i = ddDagInt(Cudd_Regular(node));
  385. ddClearFlag(Cudd_Regular(node));
  386. return(i);
  387. } /* end of Cudd_DagSize */
  388. /**Function********************************************************************
  389. Synopsis [Estimates the number of nodes in a cofactor of a DD.]
  390. Description [Estimates the number of nodes in a cofactor of a DD.
  391. Returns an estimate of the number of nodes in a cofactor of
  392. the graph rooted at node with respect to the variable whose index is i.
  393. In case of failure, returns CUDD_OUT_OF_MEM.
  394. This function uses a refinement of the algorithm of Cabodi et al.
  395. (ICCAD96). The refinement allows the procedure to account for part
  396. of the recombination that may occur in the part of the cofactor above
  397. the cofactoring variable. This procedure does not create any new node.
  398. It does keep a small table of results; therefore it may run out of memory.
  399. If this is a concern, one should use Cudd_EstimateCofactorSimple, which
  400. is faster, does not allocate any memory, but is less accurate.]
  401. SideEffects [None]
  402. SeeAlso [Cudd_DagSize Cudd_EstimateCofactorSimple]
  403. ******************************************************************************/
  404. int
  405. Cudd_EstimateCofactor(
  406. DdManager *dd /* manager */,
  407. DdNode * f /* function */,
  408. int i /* index of variable */,
  409. int phase /* 1: positive; 0: negative */
  410. )
  411. {
  412. int val;
  413. DdNode *ptr;
  414. st_table *table;
  415. table = st_init_table(st_ptrcmp,st_ptrhash);
  416. if (table == NULL) return(CUDD_OUT_OF_MEM);
  417. val = cuddEstimateCofactor(dd,table,Cudd_Regular(f),i,phase,&ptr);
  418. ddClearFlag(Cudd_Regular(f));
  419. st_free_table(table);
  420. return(val);
  421. } /* end of Cudd_EstimateCofactor */
  422. /**Function********************************************************************
  423. Synopsis [Estimates the number of nodes in a cofactor of a DD.]
  424. Description [Estimates the number of nodes in a cofactor of a DD.
  425. Returns an estimate of the number of nodes in the positive cofactor of
  426. the graph rooted at node with respect to the variable whose index is i.
  427. This procedure implements with minor changes the algorithm of Cabodi et al.
  428. (ICCAD96). It does not allocate any memory, it does not change the
  429. state of the manager, and it is fast. However, it has been observed to
  430. overestimate the size of the cofactor by as much as a factor of 2.]
  431. SideEffects [None]
  432. SeeAlso [Cudd_DagSize]
  433. ******************************************************************************/
  434. int
  435. Cudd_EstimateCofactorSimple(
  436. DdNode * node,
  437. int i)
  438. {
  439. int val;
  440. val = cuddEstimateCofactorSimple(Cudd_Regular(node),i);
  441. ddClearFlag(Cudd_Regular(node));
  442. return(val);
  443. } /* end of Cudd_EstimateCofactorSimple */
  444. /**Function********************************************************************
  445. Synopsis [Counts the number of nodes in an array of DDs.]
  446. Description [Counts the number of nodes in an array of DDs. Shared
  447. nodes are counted only once. Returns the total number of nodes.]
  448. SideEffects [None]
  449. SeeAlso [Cudd_DagSize]
  450. ******************************************************************************/
  451. int
  452. Cudd_SharingSize(
  453. DdNode ** nodeArray,
  454. int n)
  455. {
  456. int i,j;
  457. i = 0;
  458. for (j = 0; j < n; j++) {
  459. i += ddDagInt(Cudd_Regular(nodeArray[j]));
  460. }
  461. for (j = 0; j < n; j++) {
  462. ddClearFlag(Cudd_Regular(nodeArray[j]));
  463. }
  464. return(i);
  465. } /* end of Cudd_SharingSize */
  466. /**Function********************************************************************
  467. Synopsis [Counts the number of minterms of a DD.]
  468. Description [Counts the number of minterms of a DD. The function is
  469. assumed to depend on nvars variables. The minterm count is
  470. represented as a double, to allow for a larger number of variables.
  471. Returns the number of minterms of the function rooted at node if
  472. successful; (double) CUDD_OUT_OF_MEM otherwise.]
  473. SideEffects [None]
  474. SeeAlso [Cudd_PrintDebug Cudd_CountPath]
  475. ******************************************************************************/
  476. double
  477. Cudd_CountMinterm(
  478. DdManager * manager,
  479. DdNode * node,
  480. int nvars)
  481. {
  482. double max;
  483. DdHashTable *table;
  484. double res;
  485. CUDD_VALUE_TYPE epsilon;
  486. background = manager->background;
  487. zero = Cudd_Not(manager->one);
  488. max = pow(2.0,(double)nvars);
  489. table = cuddHashTableInit(manager,1,2);
  490. if (table == NULL) {
  491. return((double)CUDD_OUT_OF_MEM);
  492. }
  493. epsilon = Cudd_ReadEpsilon(manager);
  494. Cudd_SetEpsilon(manager,(CUDD_VALUE_TYPE)0.0);
  495. res = ddCountMintermAux(node,max,table);
  496. cuddHashTableQuit(table);
  497. Cudd_SetEpsilon(manager,epsilon);
  498. return(res);
  499. } /* end of Cudd_CountMinterm */
  500. /**Function********************************************************************
  501. Synopsis [Counts the number of paths of a DD.]
  502. Description [Counts the number of paths of a DD. Paths to all
  503. terminal nodes are counted. The path count is represented as a
  504. double, to allow for a larger number of variables. Returns the
  505. number of paths of the function rooted at node if successful;
  506. (double) CUDD_OUT_OF_MEM otherwise.]
  507. SideEffects [None]
  508. SeeAlso [Cudd_CountMinterm]
  509. ******************************************************************************/
  510. double
  511. Cudd_CountPath(
  512. DdNode * node)
  513. {
  514. st_table *table;
  515. double i;
  516. table = st_init_table(st_ptrcmp,st_ptrhash);
  517. if (table == NULL) {
  518. return((double)CUDD_OUT_OF_MEM);
  519. }
  520. i = ddCountPathAux(Cudd_Regular(node),table);
  521. st_foreach(table, cuddStCountfree, NULL);
  522. st_free_table(table);
  523. return(i);
  524. } /* end of Cudd_CountPath */
  525. /**Function********************************************************************
  526. Synopsis [Counts the number of minterms of a DD with extended precision.]
  527. Description [Counts the number of minterms of a DD with extended precision.
  528. The function is assumed to depend on nvars variables. The minterm count is
  529. represented as an EpDouble, to allow any number of variables.
  530. Returns 0 if successful; CUDD_OUT_OF_MEM otherwise.]
  531. SideEffects [None]
  532. SeeAlso [Cudd_PrintDebug Cudd_CountPath]
  533. ******************************************************************************/
  534. int
  535. Cudd_EpdCountMinterm(
  536. DdManager * manager,
  537. DdNode * node,
  538. int nvars,
  539. EpDouble * epd)
  540. {
  541. EpDouble max, tmp;
  542. st_table *table;
  543. int status;
  544. background = manager->background;
  545. zero = Cudd_Not(manager->one);
  546. EpdPow2(nvars, &max);
  547. table = st_init_table(EpdCmp, st_ptrhash);
  548. if (table == NULL) {
  549. EpdMakeZero(epd, 0);
  550. return(CUDD_OUT_OF_MEM);
  551. }
  552. status = ddEpdCountMintermAux(Cudd_Regular(node),&max,epd,table);
  553. st_foreach(table, ddEpdFree, NULL);
  554. st_free_table(table);
  555. if (status == CUDD_OUT_OF_MEM) {
  556. EpdMakeZero(epd, 0);
  557. return(CUDD_OUT_OF_MEM);
  558. }
  559. if (Cudd_IsComplement(node)) {
  560. EpdSubtract3(&max, epd, &tmp);
  561. EpdCopy(&tmp, epd);
  562. }
  563. return(0);
  564. } /* end of Cudd_EpdCountMinterm */
  565. /**Function********************************************************************
  566. Synopsis [Counts the number of paths to a non-zero terminal of a DD.]
  567. Description [Counts the number of paths to a non-zero terminal of a
  568. DD. The path count is
  569. represented as a double, to allow for a larger number of variables.
  570. Returns the number of paths of the function rooted at node.]
  571. SideEffects [None]
  572. SeeAlso [Cudd_CountMinterm Cudd_CountPath]
  573. ******************************************************************************/
  574. double
  575. Cudd_CountPathsToNonZero(
  576. DdNode * node)
  577. {
  578. st_table *table;
  579. double i;
  580. table = st_init_table(st_ptrcmp,st_ptrhash);
  581. if (table == NULL) {
  582. return((double)CUDD_OUT_OF_MEM);
  583. }
  584. i = ddCountPathsToNonZero(node,table);
  585. st_foreach(table, cuddStCountfree, NULL);
  586. st_free_table(table);
  587. return(i);
  588. } /* end of Cudd_CountPathsToNonZero */
  589. /**Function********************************************************************
  590. Synopsis [Finds the variables on which a DD depends.]
  591. Description [Finds the variables on which a DD depends. Returns the
  592. number of variables if successful; CUDD_OUT_OF_MEM otherwise.]
  593. SideEffects [The indices of the support variables are returned as
  594. side effects. If the function is constant, no array is allocated.]
  595. SeeAlso [Cudd_Support Cudd_SupportIndex Cudd_VectorSupportIndices]
  596. ******************************************************************************/
  597. int
  598. Cudd_SupportIndices(
  599. DdManager * dd /* manager */,
  600. DdNode * f /* DD whose support is sought */,
  601. int **indices /* array containing (on return) the indices */)
  602. {
  603. int SP = 0;
  604. ddFindSupport(dd, Cudd_Regular(f), &SP);
  605. ddClearFlag(Cudd_Regular(f));
  606. ddClearVars(dd, SP);
  607. if (SP > 0) {
  608. int i;
  609. *indices = ALLOC(int, SP);
  610. if (*indices == NULL) {
  611. dd->errorCode = CUDD_MEMORY_OUT;
  612. return(CUDD_OUT_OF_MEM);
  613. }
  614. for (i = 0; i < SP; i++)
  615. (*indices)[i] = (int) (ptrint) dd->stack[i];
  616. qsort(*indices, SP, sizeof(int), indexCompare);
  617. } else {
  618. *indices = NULL;
  619. }
  620. return(SP);
  621. } /* end of Cudd_SupportIndices */
  622. /**Function********************************************************************
  623. Synopsis [Finds the variables on which a DD depends.]
  624. Description [Finds the variables on which a DD depends.
  625. Returns a BDD consisting of the product of the variables if
  626. successful; NULL otherwise.]
  627. SideEffects [None]
  628. SeeAlso [Cudd_VectorSupport Cudd_ClassifySupport]
  629. ******************************************************************************/
  630. DdNode *
  631. Cudd_Support(
  632. DdManager * dd /* manager */,
  633. DdNode * f /* DD whose support is sought */)
  634. {
  635. int *support;
  636. DdNode *res;
  637. int j;
  638. int size = Cudd_SupportIndices(dd, f, &support);
  639. if (size == CUDD_OUT_OF_MEM)
  640. return(NULL);
  641. /* Transform support from array of indices to cube. */
  642. res = DD_ONE(dd);
  643. cuddRef(res);
  644. for (j = size - 1; j >= 0; j--) { /* for each index bottom-up (almost) */
  645. int index = support[j];
  646. DdNode *var = dd->vars[index];
  647. DdNode *tmp = Cudd_bddAnd(dd,res,var);
  648. if (tmp == NULL) {
  649. Cudd_RecursiveDeref(dd,res);
  650. FREE(support);
  651. return(NULL);
  652. }
  653. cuddRef(tmp);
  654. Cudd_RecursiveDeref(dd,res);
  655. res = tmp;
  656. }
  657. FREE(support);
  658. cuddDeref(res);
  659. return(res);
  660. } /* end of Cudd_Support */
  661. /**Function********************************************************************
  662. Synopsis [Finds the variables on which a DD depends.]
  663. Description [Finds the variables on which a DD depends. Returns an
  664. index array of the variables if successful; NULL otherwise. The
  665. size of the array equals the number of variables in the manager.
  666. Each entry of the array is 1 if the corresponding variable is in the
  667. support of the DD and 0 otherwise.]
  668. SideEffects [None]
  669. SeeAlso [Cudd_Support Cudd_SupportIndices Cudd_ClassifySupport]
  670. ******************************************************************************/
  671. int *
  672. Cudd_SupportIndex(
  673. DdManager * dd /* manager */,
  674. DdNode * f /* DD whose support is sought */)
  675. {
  676. int *support;
  677. int i;
  678. int size;
  679. /* Allocate and initialize support array for ddSupportStep. */
  680. size = ddMax(dd->size, dd->sizeZ);
  681. support = ALLOC(int,size);
  682. if (support == NULL) {
  683. dd->errorCode = CUDD_MEMORY_OUT;
  684. return(NULL);
  685. }
  686. for (i = 0; i < size; i++) {
  687. support[i] = 0;
  688. }
  689. /* Compute support and clean up markers. */
  690. ddSupportStep(Cudd_Regular(f),support);
  691. ddClearFlag(Cudd_Regular(f));
  692. return(support);
  693. } /* end of Cudd_SupportIndex */
  694. /**Function********************************************************************
  695. Synopsis [Counts the variables on which a DD depends.]
  696. Description [Returns the variables on which a DD depends.]
  697. SideEffects [None]
  698. SeeAlso [Cudd_Support Cudd_SupportIndices]
  699. ******************************************************************************/
  700. int
  701. Cudd_SupportSize(
  702. DdManager * dd /* manager */,
  703. DdNode * f /* DD whose support size is sought */)
  704. {
  705. int SP = 0;
  706. ddFindSupport(dd, Cudd_Regular(f), &SP);
  707. ddClearFlag(Cudd_Regular(f));
  708. ddClearVars(dd, SP);
  709. return(SP);
  710. } /* end of Cudd_SupportSize */
  711. /**Function********************************************************************
  712. Synopsis [Finds the variables on which a set of DDs depends.]
  713. Description [Finds the variables on which a set of DDs depends. The
  714. set must contain either BDDs and ADDs, or ZDDs. Returns the number
  715. of variables if successful; CUDD_OUT_OF_MEM otherwise.]
  716. SideEffects [The indices of the support variables are returned as
  717. side effects. If the function is constant, no array is allocated.]
  718. SeeAlso [Cudd_Support Cudd_SupportIndex Cudd_VectorSupportIndices]
  719. ******************************************************************************/
  720. int
  721. Cudd_VectorSupportIndices(
  722. DdManager * dd /* manager */,
  723. DdNode ** F /* DD whose support is sought */,
  724. int n /* size of the array */,
  725. int **indices /* array containing (on return) the indices */)
  726. {
  727. int i;
  728. int SP = 0;
  729. /* Compute support and clean up markers. */
  730. for (i = 0; i < n; i++) {
  731. ddFindSupport(dd, Cudd_Regular(F[i]), &SP);
  732. }
  733. for (i = 0; i < n; i++) {
  734. ddClearFlag(Cudd_Regular(F[i]));
  735. }
  736. ddClearVars(dd, SP);
  737. if (SP > 0) {
  738. int i;
  739. *indices = ALLOC(int, SP);
  740. if (*indices == NULL) {
  741. dd->errorCode = CUDD_MEMORY_OUT;
  742. return(CUDD_OUT_OF_MEM);
  743. }
  744. for (i = 0; i < SP; i++)
  745. (*indices)[i] = (int) (ptrint) dd->stack[i];
  746. qsort(*indices, SP, sizeof(int), indexCompare);
  747. } else {
  748. *indices = NULL;
  749. }
  750. return(SP);
  751. } /* end of Cudd_VectorSupportIndices */
  752. /**Function********************************************************************
  753. Synopsis [Finds the variables on which a set of DDs depends.]
  754. Description [Finds the variables on which a set of DDs depends.
  755. The set must contain either BDDs and ADDs, or ZDDs.
  756. Returns a BDD consisting of the product of the variables if
  757. successful; NULL otherwise.]
  758. SideEffects [None]
  759. SeeAlso [Cudd_Support Cudd_ClassifySupport]
  760. ******************************************************************************/
  761. DdNode *
  762. Cudd_VectorSupport(
  763. DdManager * dd /* manager */,
  764. DdNode ** F /* array of DDs whose support is sought */,
  765. int n /* size of the array */)
  766. {
  767. int *support;
  768. DdNode *res;
  769. int j;
  770. int size = Cudd_VectorSupportIndices(dd, F, n, &support);
  771. if (size == CUDD_OUT_OF_MEM)
  772. return(NULL);
  773. /* Transform support from array of indices to cube. */
  774. res = DD_ONE(dd);
  775. cuddRef(res);
  776. for (j = size - 1; j >= 0; j--) { /* for each index bottom-up (almost) */
  777. int index = support[j];
  778. DdNode *var = dd->vars[index];
  779. DdNode *tmp = Cudd_bddAnd(dd,res,var);
  780. if (tmp == NULL) {
  781. Cudd_RecursiveDeref(dd,res);
  782. FREE(support);
  783. return(NULL);
  784. }
  785. cuddRef(tmp);
  786. Cudd_RecursiveDeref(dd,res);
  787. res = tmp;
  788. }
  789. FREE(support);
  790. cuddDeref(res);
  791. return(res);
  792. } /* end of Cudd_VectorSupport */
  793. /**Function********************************************************************
  794. Synopsis [Finds the variables on which a set of DDs depends.]
  795. Description [Finds the variables on which a set of DDs depends.
  796. The set must contain either BDDs and ADDs, or ZDDs.
  797. Returns an index array of the variables if successful; NULL otherwise.]
  798. SideEffects [None]
  799. SeeAlso [Cudd_SupportIndex Cudd_VectorSupport Cudd_VectorSupportIndices]
  800. ******************************************************************************/
  801. int *
  802. Cudd_VectorSupportIndex(
  803. DdManager * dd /* manager */,
  804. DdNode ** F /* array of DDs whose support is sought */,
  805. int n /* size of the array */)
  806. {
  807. int *support;
  808. int i;
  809. int size;
  810. /* Allocate and initialize support array for ddSupportStep. */
  811. size = ddMax(dd->size, dd->sizeZ);
  812. support = ALLOC(int,size);
  813. if (support == NULL) {
  814. dd->errorCode = CUDD_MEMORY_OUT;
  815. return(NULL);
  816. }
  817. for (i = 0; i < size; i++) {
  818. support[i] = 0;
  819. }
  820. /* Compute support and clean up markers. */
  821. for (i = 0; i < n; i++) {
  822. ddSupportStep(Cudd_Regular(F[i]),support);
  823. }
  824. for (i = 0; i < n; i++) {
  825. ddClearFlag(Cudd_Regular(F[i]));
  826. }
  827. return(support);
  828. } /* end of Cudd_VectorSupportIndex */
  829. /**Function********************************************************************
  830. Synopsis [Counts the variables on which a set of DDs depends.]
  831. Description [Returns the variables on which a set of DDs depends.
  832. The set must contain either BDDs and ADDs, or ZDDs.]
  833. SideEffects [None]
  834. SeeAlso [Cudd_VectorSupport Cudd_SupportSize]
  835. ******************************************************************************/
  836. int
  837. Cudd_VectorSupportSize(
  838. DdManager * dd /* manager */,
  839. DdNode ** F /* array of DDs whose support is sought */,
  840. int n /* size of the array */)
  841. {
  842. int i;
  843. int SP = 0;
  844. /* Compute support and clean up markers. */
  845. for (i = 0; i < n; i++) {
  846. ddFindSupport(dd, Cudd_Regular(F[i]), &SP);
  847. }
  848. for (i = 0; i < n; i++) {
  849. ddClearFlag(Cudd_Regular(F[i]));
  850. }
  851. ddClearVars(dd, SP);
  852. return(SP);
  853. } /* end of Cudd_VectorSupportSize */
  854. /**Function********************************************************************
  855. Synopsis [Classifies the variables in the support of two DDs.]
  856. Description [Classifies the variables in the support of two DDs
  857. <code>f</code> and <code>g</code>, depending on whther they appear
  858. in both DDs, only in <code>f</code>, or only in <code>g</code>.
  859. Returns 1 if successful; 0 otherwise.]
  860. SideEffects [The cubes of the three classes of variables are
  861. returned as side effects.]
  862. SeeAlso [Cudd_Support Cudd_VectorSupport]
  863. ******************************************************************************/
  864. int
  865. Cudd_ClassifySupport(
  866. DdManager * dd /* manager */,
  867. DdNode * f /* first DD */,
  868. DdNode * g /* second DD */,
  869. DdNode ** common /* cube of shared variables */,
  870. DdNode ** onlyF /* cube of variables only in f */,
  871. DdNode ** onlyG /* cube of variables only in g */)
  872. {
  873. int *supportF, *supportG;
  874. int fi, gi;
  875. int sizeF, sizeG;
  876. sizeF = Cudd_SupportIndices(dd, f, &supportF);
  877. if (sizeF == CUDD_OUT_OF_MEM)
  878. return(0);
  879. sizeG = Cudd_SupportIndices(dd, g, &supportG);
  880. if (sizeG == CUDD_OUT_OF_MEM) {
  881. FREE(supportF);
  882. return(0);
  883. }
  884. /* Classify variables and create cubes. This part of the procedure
  885. ** relies on the sorting of the indices in the two support arrays.
  886. */
  887. *common = *onlyF = *onlyG = DD_ONE(dd);
  888. cuddRef(*common); cuddRef(*onlyF); cuddRef(*onlyG);
  889. fi = sizeF - 1;
  890. gi = sizeG - 1;
  891. while (fi >= 0 || gi >= 0) {
  892. int indexF = fi >= 0 ? supportF[fi] : -1;
  893. int indexG = gi >= 0 ? supportG[gi] : -1;
  894. int index = ddMax(indexF, indexG);
  895. DdNode *var = dd->vars[index];
  896. #ifdef DD_DEBUG
  897. assert(index >= 0);
  898. #endif
  899. if (indexF == indexG) {
  900. DdNode *tmp = Cudd_bddAnd(dd,*common,var);
  901. if (tmp == NULL) {
  902. Cudd_RecursiveDeref(dd,*common);
  903. Cudd_RecursiveDeref(dd,*onlyF);
  904. Cudd_RecursiveDeref(dd,*onlyG);
  905. FREE(supportF); FREE(supportG);
  906. return(0);
  907. }
  908. cuddRef(tmp);
  909. Cudd_RecursiveDeref(dd,*common);
  910. *common = tmp;
  911. fi--;
  912. gi--;
  913. } else if (index == indexF) {
  914. DdNode *tmp = Cudd_bddAnd(dd,*onlyF,var);
  915. if (tmp == NULL) {
  916. Cudd_RecursiveDeref(dd,*common);
  917. Cudd_RecursiveDeref(dd,*onlyF);
  918. Cudd_RecursiveDeref(dd,*onlyG);
  919. FREE(supportF); FREE(supportG);
  920. return(0);
  921. }
  922. cuddRef(tmp);
  923. Cudd_RecursiveDeref(dd,*onlyF);
  924. *onlyF = tmp;
  925. fi--;
  926. } else { /* index == indexG */
  927. DdNode *tmp = Cudd_bddAnd(dd,*onlyG,var);
  928. if (tmp == NULL) {
  929. Cudd_RecursiveDeref(dd,*common);
  930. Cudd_RecursiveDeref(dd,*onlyF);
  931. Cudd_RecursiveDeref(dd,*onlyG);
  932. FREE(supportF); FREE(supportG);
  933. return(0);
  934. }
  935. cuddRef(tmp);
  936. Cudd_RecursiveDeref(dd,*onlyG);
  937. *onlyG = tmp;
  938. gi--;
  939. }
  940. }
  941. FREE(supportF); FREE(supportG);
  942. cuddDeref(*common); cuddDeref(*onlyF); cuddDeref(*onlyG);
  943. return(1);
  944. } /* end of Cudd_ClassifySupport */
  945. /**Function********************************************************************
  946. Synopsis [Counts the number of leaves in a DD.]
  947. Description [Counts the number of leaves in a DD. Returns the number
  948. of leaves in the DD rooted at node if successful; CUDD_OUT_OF_MEM
  949. otherwise.]
  950. SideEffects [None]
  951. SeeAlso [Cudd_PrintDebug]
  952. ******************************************************************************/
  953. int
  954. Cudd_CountLeaves(
  955. DdNode * node)
  956. {
  957. int i;
  958. i = ddLeavesInt(Cudd_Regular(node));
  959. ddClearFlag(Cudd_Regular(node));
  960. return(i);
  961. } /* end of Cudd_CountLeaves */
  962. /**Function********************************************************************
  963. Synopsis [Picks one on-set cube randomly from the given DD.]
  964. Description [Picks one on-set cube randomly from the given DD. The
  965. cube is written into an array of characters. The array must have at
  966. least as many entries as there are variables. Returns 1 if
  967. successful; 0 otherwise.]
  968. SideEffects [None]
  969. SeeAlso [Cudd_bddPickOneMinterm]
  970. ******************************************************************************/
  971. int
  972. Cudd_bddPickOneCube(
  973. DdManager * ddm,
  974. DdNode * node,
  975. char * string)
  976. {
  977. DdNode *N, *T, *E;
  978. DdNode *one, *bzero;
  979. char dir;
  980. int i;
  981. if (string == NULL || node == NULL) return(0);
  982. /* The constant 0 function has no on-set cubes. */
  983. one = DD_ONE(ddm);
  984. bzero = Cudd_Not(one);
  985. if (node == bzero) return(0);
  986. for (i = 0; i < ddm->size; i++) string[i] = 2;
  987. for (;;) {
  988. if (node == one) break;
  989. N = Cudd_Regular(node);
  990. T = cuddT(N); E = cuddE(N);
  991. if (Cudd_IsComplement(node)) {
  992. T = Cudd_Not(T); E = Cudd_Not(E);
  993. }
  994. if (T == bzero) {
  995. string[N->index] = 0;
  996. node = E;
  997. } else if (E == bzero) {
  998. string[N->index] = 1;
  999. node = T;
  1000. } else {
  1001. dir = (char) ((Cudd_Random() & 0x2000) >> 13);
  1002. string[N->index] = dir;
  1003. node = dir ? T : E;
  1004. }
  1005. }
  1006. return(1);
  1007. } /* end of Cudd_bddPickOneCube */
  1008. /**Function********************************************************************
  1009. Synopsis [Picks one on-set minterm randomly from the given DD.]
  1010. Description [Picks one on-set minterm randomly from the given
  1011. DD. The minterm is in terms of <code>vars</code>. The array
  1012. <code>vars</code> should contain at least all variables in the
  1013. support of <code>f</code>; if this condition is not met the minterm
  1014. built by this procedure may not be contained in
  1015. <code>f</code>. Builds a BDD for the minterm and returns a pointer
  1016. to it if successful; NULL otherwise. There are three reasons why the
  1017. procedure may fail:
  1018. <ul>
  1019. <li> It may run out of memory;
  1020. <li> the function <code>f</code> may be the constant 0;
  1021. <li> the minterm may not be contained in <code>f</code>.
  1022. </ul>]
  1023. SideEffects [None]
  1024. SeeAlso [Cudd_bddPickOneCube]
  1025. ******************************************************************************/
  1026. DdNode *
  1027. Cudd_bddPickOneMinterm(
  1028. DdManager * dd /* manager */,
  1029. DdNode * f /* function from which to pick one minterm */,
  1030. DdNode ** vars /* array of variables */,
  1031. int n /* size of <code>vars</code> */)
  1032. {
  1033. char *string;
  1034. int i, size;
  1035. int *indices;
  1036. int result;
  1037. DdNode *old, *neW;
  1038. size = dd->size;
  1039. string = ALLOC(char, size);
  1040. if (string == NULL) {
  1041. dd->errorCode = CUDD_MEMORY_OUT;
  1042. return(NULL);
  1043. }
  1044. indices = ALLOC(int,n);
  1045. if (indices == NULL) {
  1046. dd->errorCode = CUDD_MEMORY_OUT;
  1047. FREE(string);
  1048. return(NULL);
  1049. }
  1050. for (i = 0; i < n; i++) {
  1051. indices[i] = vars[i]->index;
  1052. }
  1053. result = Cudd_bddPickOneCube(dd,f,string);
  1054. if (result == 0) {
  1055. FREE(string);
  1056. FREE(indices);
  1057. return(NULL);
  1058. }
  1059. /* Randomize choice for don't cares. */
  1060. for (i = 0; i < n; i++) {
  1061. if (string[indices[i]] == 2)
  1062. string[indices[i]] = (char) ((Cudd_Random() & 0x20) >> 5);
  1063. }
  1064. /* Build result BDD. */
  1065. old = Cudd_ReadOne(dd);
  1066. cuddRef(old);
  1067. for (i = n-1; i >= 0; i--) {
  1068. neW = Cudd_bddAnd(dd,old,Cudd_NotCond(vars[i],string[indices[i]]==0));
  1069. if (neW == NULL) {
  1070. FREE(string);
  1071. FREE(indices);
  1072. Cudd_RecursiveDeref(dd,old);
  1073. return(NULL);
  1074. }
  1075. cuddRef(neW);
  1076. Cudd_RecursiveDeref(dd,old);
  1077. old = neW;
  1078. }
  1079. #ifdef DD_DEBUG
  1080. /* Test. */
  1081. if (Cudd_bddLeq(dd,old,f)) {
  1082. cuddDeref(old);
  1083. } else {
  1084. Cudd_RecursiveDeref(dd,old);
  1085. old = NULL;
  1086. }
  1087. #else
  1088. cuddDeref(old);
  1089. #endif
  1090. FREE(string);
  1091. FREE(indices);
  1092. return(old);
  1093. } /* end of Cudd_bddPickOneMinterm */
  1094. /**Function********************************************************************
  1095. Synopsis [Picks k on-set minterms evenly distributed from given DD.]
  1096. Description [Picks k on-set minterms evenly distributed from given DD.
  1097. The minterms are in terms of <code>vars</code>. The array
  1098. <code>vars</code> should contain at least all variables in the
  1099. support of <code>f</code>; if this condition is not met the minterms
  1100. built by this procedure may not be contained in
  1101. <code>f</code>. Builds an array of BDDs for the minterms and returns a
  1102. pointer to it if successful; NULL otherwise. There are three reasons
  1103. why the procedure may fail:
  1104. <ul>
  1105. <li> It may run out of memory;
  1106. <li> the function <code>f</code> may be the constant 0;
  1107. <li> the minterms may not be contained in <code>f</code>.
  1108. </ul>]
  1109. SideEffects [None]
  1110. SeeAlso [Cudd_bddPickOneMinterm Cudd_bddPickOneCube]
  1111. ******************************************************************************/
  1112. DdNode **
  1113. Cudd_bddPickArbitraryMinterms(
  1114. DdManager * dd /* manager */,
  1115. DdNode * f /* function from which to pick k minterms */,
  1116. DdNode ** vars /* array of variables */,
  1117. int n /* size of <code>vars</code> */,
  1118. int k /* number of minterms to find */)
  1119. {
  1120. char **string;
  1121. int i, j, l, size;
  1122. int *indices;
  1123. int result;
  1124. DdNode **old, *neW;
  1125. double minterms;
  1126. char *saveString;
  1127. int saveFlag, savePoint, isSame;
  1128. minterms = Cudd_CountMinterm(dd,f,n);
  1129. if ((double)k > minterms) {
  1130. return(NULL);
  1131. }
  1132. size = dd->size;
  1133. string = ALLOC(char *, k);
  1134. if (string == NULL) {
  1135. dd->errorCode = CUDD_MEMORY_OUT;
  1136. return(NULL);
  1137. }
  1138. for (i = 0; i < k; i++) {
  1139. string[i] = ALLOC(char, size + 1);
  1140. if (string[i] == NULL) {
  1141. for (j = 0; j < i; j++)
  1142. FREE(string[i]);
  1143. FREE(string);
  1144. dd->errorCode = CUDD_MEMORY_OUT;
  1145. return(NULL);
  1146. }
  1147. for (j = 0; j < size; j++) string[i][j] = '2';
  1148. string[i][size] = '\0';
  1149. }
  1150. indices = ALLOC(int,n);
  1151. if (indices == NULL) {
  1152. dd->errorCode = CUDD_MEMORY_OUT;
  1153. for (i = 0; i < k; i++)
  1154. FREE(string[i]);
  1155. FREE(string);
  1156. return(NULL);
  1157. }
  1158. for (i = 0; i < n; i++) {
  1159. indices[i] = vars[i]->index;
  1160. }
  1161. result = ddPickArbitraryMinterms(dd,f,n,k,string);
  1162. if (result == 0) {
  1163. for (i = 0; i < k; i++)
  1164. FREE(string[i]);
  1165. FREE(string);
  1166. FREE(indices);
  1167. return(NULL);
  1168. }
  1169. old = ALLOC(DdNode *, k);
  1170. if (old == NULL) {
  1171. dd->errorCode = CUDD_MEMORY_OUT;
  1172. for (i = 0; i < k; i++)
  1173. FREE(string[i]);
  1174. FREE(string);
  1175. FREE(indices);
  1176. return(NULL);
  1177. }
  1178. saveString = ALLOC(char, size + 1);
  1179. if (saveString == NULL) {
  1180. dd->errorCode = CUDD_MEMORY_OUT;
  1181. for (i = 0; i < k; i++)
  1182. FREE(string[i]);
  1183. FREE(string);
  1184. FREE(indices);
  1185. FREE(old);
  1186. return(NULL);
  1187. }
  1188. saveFlag = 0;
  1189. /* Build result BDD array. */
  1190. for (i = 0; i < k; i++) {
  1191. isSame = 0;
  1192. if (!saveFlag) {
  1193. for (j = i + 1; j < k; j++) {
  1194. if (strcmp(string[i], string[j]) == 0) {
  1195. savePoint = i;
  1196. strcpy(saveString, string[i]);
  1197. saveFlag = 1;
  1198. break;
  1199. }
  1200. }
  1201. } else {
  1202. if (strcmp(string[i], saveString) == 0) {
  1203. isSame = 1;
  1204. } else {
  1205. saveFlag = 0;
  1206. for (j = i + 1; j < k; j++) {
  1207. if (strcmp(string[i], string[j]) == 0) {
  1208. savePoint = i;
  1209. strcpy(saveString, string[i]);
  1210. saveFlag = 1;
  1211. break;
  1212. }
  1213. }
  1214. }
  1215. }
  1216. /* Randomize choice for don't cares. */
  1217. for (j = 0; j < n; j++) {
  1218. if (string[i][indices[j]] == '2')
  1219. string[i][indices[j]] =
  1220. (char) ((Cudd_Random() & 0x20) ? '1' : '0');
  1221. }
  1222. while (isSame) {
  1223. isSame = 0;
  1224. for (j = savePoint; j < i; j++) {
  1225. if (strcmp(string[i], string[j]) == 0) {
  1226. isSame = 1;
  1227. break;
  1228. }
  1229. }
  1230. if (isSame) {
  1231. strcpy(string[i], saveString);
  1232. /* Randomize choice for don't cares. */
  1233. for (j = 0; j < n; j++) {
  1234. if (string[i][indices[j]] == '2')
  1235. string[i][indices[j]] =
  1236. (char) ((Cudd_Random() & 0x20) ? '1' : '0');
  1237. }
  1238. }
  1239. }
  1240. old[i] = Cudd_ReadOne(dd);
  1241. cuddRef(old[i]);
  1242. for (j = 0; j < n; j++) {
  1243. if (string[i][indices[j]] == '0') {
  1244. neW = Cudd_bddAnd(dd,old[i],Cudd_Not(vars[j]));
  1245. } else {
  1246. neW = Cudd_bddAnd(dd,old[i],vars[j]);
  1247. }
  1248. if (neW == NULL) {
  1249. FREE(saveString);
  1250. for (l = 0; l < k; l++)
  1251. FREE(string[l]);
  1252. FREE(string);
  1253. FREE(indices);
  1254. for (l = 0; l <= i; l++)
  1255. Cudd_RecursiveDeref(dd,old[l]);
  1256. FREE(old);
  1257. return(NULL);
  1258. }
  1259. cuddRef(neW);
  1260. Cudd_RecursiveDeref(dd,old[i]);
  1261. old[i] = neW;
  1262. }
  1263. /* Test. */
  1264. if (!Cudd_bddLeq(dd,old[i],f)) {
  1265. FREE(saveString);
  1266. for (l = 0; l < k; l++)
  1267. FREE(string[l]);
  1268. FREE(string);
  1269. FREE(indices);
  1270. for (l = 0; l <= i; l++)
  1271. Cudd_RecursiveDeref(dd,old[l]);
  1272. FREE(old);
  1273. return(NULL);
  1274. }
  1275. }
  1276. FREE(saveString);
  1277. for (i = 0; i < k; i++) {
  1278. cuddDeref(old[i]);
  1279. FREE(string[i]);
  1280. }
  1281. FREE(string);
  1282. FREE(indices);
  1283. return(old);
  1284. } /* end of Cudd_bddPickArbitraryMinterms */
  1285. /**Function********************************************************************
  1286. Synopsis [Extracts a subset from a BDD.]
  1287. Description [Extracts a subset from a BDD in the following procedure.
  1288. 1. Compute the weight for each mask variable by counting the number of
  1289. minterms for both positive and negative cofactors of the BDD with
  1290. respect to each mask variable. (weight = #positive - #negative)
  1291. 2. Find a representative cube of the BDD by using the weight. From the
  1292. top variable of the BDD, for each variable, if the weight is greater
  1293. than 0.0, choose THEN branch, othereise ELSE branch, until meeting
  1294. the constant 1.
  1295. 3. Quantify out the variables not in maskVars from the representative
  1296. cube and if a variable in maskVars is don't care, replace the
  1297. variable with a constant(1 or 0) depending on the weight.
  1298. 4. Make a subset of the BDD by multiplying with the modified cube.]
  1299. SideEffects [None]
  1300. SeeAlso []
  1301. ******************************************************************************/
  1302. DdNode *
  1303. Cudd_SubsetWithMaskVars(
  1304. DdManager * dd /* manager */,
  1305. DdNode * f /* function from which to pick a cube */,
  1306. DdNode ** vars /* array of variables */,
  1307. int nvars /* size of <code>vars</code> */,
  1308. DdNode ** maskVars /* array of variables */,
  1309. int mvars /* size of <code>maskVars</code> */)
  1310. {
  1311. double *weight;
  1312. char *string;
  1313. int i, size;
  1314. int *indices, *mask;
  1315. int result;
  1316. DdNode *zero, *cube, *newCube, *subset;
  1317. DdNode *cof;
  1318. DdNode *support;
  1319. support = Cudd_Support(dd,f);
  1320. cuddRef(support);
  1321. Cudd_RecursiveDeref(dd,support);
  1322. zero = Cudd_Not(dd->one);
  1323. size = dd->size;
  1324. weight = ALLOC(double,size);
  1325. if (weight == NULL) {
  1326. dd->errorCode = CUDD_MEMORY_OUT;
  1327. return(NULL);
  1328. }
  1329. for (i = 0; i < size; i++) {
  1330. weight[i] = 0.0;
  1331. }
  1332. for (i = 0; i < mvars; i++) {
  1333. cof = Cudd_Cofactor(dd, f, maskVars[i]);
  1334. cuddRef(cof);
  1335. weight[i] = Cudd_CountMinterm(dd, cof, nvars);
  1336. Cudd_RecursiveDeref(dd,cof);
  1337. cof = Cudd_Cofactor(dd, f, Cudd_Not(maskVars[i]));
  1338. cuddRef(cof);
  1339. weight[i] -= Cudd_CountMinterm(dd, cof, nvars);
  1340. Cudd_RecursiveDeref(dd,cof);
  1341. }
  1342. string = ALLOC(char, size + 1);
  1343. if (string == NULL) {
  1344. dd->errorCode = CUDD_MEMORY_OUT;
  1345. FREE(weight);
  1346. return(NULL);
  1347. }
  1348. mask = ALLOC(int, size);
  1349. if (mask == NULL) {
  1350. dd->errorCode = CUDD_MEMORY_OUT;
  1351. FREE(weight);
  1352. FREE(string);
  1353. return(NULL);
  1354. }
  1355. for (i = 0; i < size; i++) {
  1356. string[i] = '2';
  1357. mask[i] = 0;
  1358. }
  1359. string[size] = '\0';
  1360. indices = ALLOC(int,nvars);
  1361. if (indices == NULL) {
  1362. dd->errorCode = CUDD_MEMORY_OUT;
  1363. FREE(weight);
  1364. FREE(string);
  1365. FREE(mask);
  1366. return(NULL);
  1367. }
  1368. for (i = 0; i < nvars; i++) {
  1369. indices[i] = vars[i]->index;
  1370. }
  1371. result = ddPickRepresentativeCube(dd,f,weight,string);
  1372. if (result == 0) {
  1373. FREE(weight);
  1374. FREE(string);
  1375. FREE(mask);
  1376. FREE(indices);
  1377. return(NULL);
  1378. }
  1379. cube = Cudd_ReadOne(dd);
  1380. cuddRef(cube);
  1381. zero = Cudd_Not(Cudd_ReadOne(dd));
  1382. for (i = 0; i < nvars; i++) {
  1383. if (string[indices[i]] == '0') {
  1384. newCube = Cudd_bddIte(dd,cube,Cudd_Not(vars[i]),zero);
  1385. } else if (string[indices[i]] == '1') {
  1386. newCube = Cudd_bddIte(dd,cube,vars[i],zero);
  1387. } else
  1388. continue;
  1389. if (newCube == NULL) {
  1390. FREE(weight);
  1391. FREE(string);
  1392. FREE(mask);
  1393. FREE(indices);
  1394. Cudd_RecursiveDeref(dd,cube);
  1395. return(NULL);
  1396. }
  1397. cuddRef(newCube);
  1398. Cudd_RecursiveDeref(dd,cube);
  1399. cube = newCube;
  1400. }
  1401. Cudd_RecursiveDeref(dd,cube);
  1402. for (i = 0; i < mvars; i++) {
  1403. mask[maskVars[i]->index] = 1;
  1404. }
  1405. for (i = 0; i < nvars; i++) {
  1406. if (mask[indices[i]]) {
  1407. if (string[indices[i]] == '2') {
  1408. if (weight[indices[i]] >= 0.0)
  1409. string[indices[i]] = '1';
  1410. else
  1411. string[indices[i]] = '0';
  1412. }
  1413. } else {
  1414. string[indices[i]] = '2';
  1415. }
  1416. }
  1417. cube = Cudd_ReadOne(dd);
  1418. cuddRef(cube);
  1419. zero = Cudd_Not(Cudd_ReadOne(dd));
  1420. /* Build result BDD. */
  1421. for (i = 0; i < nvars; i++) {
  1422. if (string[indices[i]] == '0') {
  1423. newCube = Cudd_bddIte(dd,cube,Cudd_Not(vars[i]),zero);
  1424. } else if (string[indices[i]] == '1') {
  1425. newCube = Cudd_bddIte(dd,cube,vars[i],zero);
  1426. } else
  1427. continue;
  1428. if (newCube == NULL) {
  1429. FREE(weight);
  1430. FREE(string);
  1431. FREE(mask);
  1432. FREE(indices);
  1433. Cudd_RecursiveDeref(dd,cube);
  1434. return(NULL);
  1435. }
  1436. cuddRef(newCube);
  1437. Cudd_RecursiveDeref(dd,cube);
  1438. cube = newCube;
  1439. }
  1440. subset = Cudd_bddAnd(dd,f,cube);
  1441. cuddRef(subset);
  1442. Cudd_RecursiveDeref(dd,cube);
  1443. /* Test. */
  1444. if (Cudd_bddLeq(dd,subset,f)) {
  1445. cuddDeref(subset);
  1446. } else {
  1447. Cudd_RecursiveDeref(dd,subset);
  1448. subset = NULL;
  1449. }
  1450. FREE(weight);
  1451. FREE(string);
  1452. FREE(mask);
  1453. FREE(indices);
  1454. return(subset);
  1455. } /* end of Cudd_SubsetWithMaskVars */
  1456. /**Function********************************************************************
  1457. Synopsis [Finds the first cube of a decision diagram.]
  1458. Description [Defines an iterator on the onset of a decision diagram
  1459. and finds its first cube. Returns a generator that contains the
  1460. information necessary to continue the enumeration if successful; NULL
  1461. otherwise.<p>
  1462. A cube is represented as an array of literals, which are integers in
  1463. {0, 1, 2}; 0 represents a complemented literal, 1 represents an
  1464. uncomplemented literal, and 2 stands for don't care. The enumeration
  1465. produces a disjoint cover of the function associated with the diagram.
  1466. The size of the array equals the number of variables in the manager at
  1467. the time Cudd_FirstCube is called.<p>
  1468. For each cube, a value is also returned. This value is always 1 for a
  1469. BDD, while it may be different from 1 for an ADD.
  1470. For BDDs, the offset is the set of cubes whose value is the logical zero.
  1471. For ADDs, the offset is the set of cubes whose value is the
  1472. background value. The cubes of the offset are not enumerated.]
  1473. SideEffects [The first cube and its value are returned as side effects.]
  1474. SeeAlso [Cudd_ForeachCube Cudd_NextCube Cudd_GenFree Cudd_IsGenEmpty
  1475. Cudd_FirstNode]
  1476. ******************************************************************************/
  1477. DdGen *
  1478. Cudd_FirstCube(
  1479. DdManager * dd,
  1480. DdNode * f,
  1481. int ** cube,
  1482. CUDD_VALUE_TYPE * value)
  1483. {
  1484. DdGen *gen;
  1485. DdNode *top, *treg, *next, *nreg, *prev, *preg;
  1486. int i;
  1487. int nvars;
  1488. /* Sanity Check. */
  1489. if (dd == NULL || f == NULL) return(NULL);
  1490. /* Allocate generator an initialize it. */
  1491. gen = ALLOC(DdGen,1);
  1492. if (gen == NULL) {
  1493. dd->errorCode = CUDD_MEMORY_OUT;
  1494. return(NULL);
  1495. }
  1496. gen->manager = dd;
  1497. gen->type = CUDD_GEN_CUBES;
  1498. gen->status = CUDD_GEN_EMPTY;
  1499. gen->gen.cubes.cube = NULL;
  1500. gen->gen.cubes.value = DD_ZERO_VAL;
  1501. gen->stack.sp = 0;
  1502. gen->stack.stack = NULL;
  1503. gen->node = NULL;
  1504. nvars = dd->size;
  1505. gen->gen.cubes.cube = ALLOC(int,nvars);
  1506. if (gen->gen.cubes.cube == NULL) {
  1507. dd->errorCode = CUDD_MEMORY_OUT;
  1508. FREE(gen);
  1509. return(NULL);
  1510. }
  1511. for (i = 0; i < nvars; i++) gen->gen.cubes.cube[i] = 2;
  1512. /* The maximum stack depth is one plus the number of variables.
  1513. ** because a path may have nodes at all levels, including the
  1514. ** constant level.
  1515. */
  1516. gen->stack.stack = ALLOC(DdNodePtr, nvars+1);
  1517. if (gen->stack.stack == NULL) {
  1518. dd->errorCode = CUDD_MEMORY_OUT;
  1519. FREE(gen->gen.cubes.cube);
  1520. FREE(gen);
  1521. return(NULL);
  1522. }
  1523. for (i = 0; i <= nvars; i++) gen->stack.stack[i] = NULL;
  1524. /* Find the first cube of the onset. */
  1525. gen->stack.stack[gen->stack.sp] = f; gen->stack.sp++;
  1526. while (1) {
  1527. top = gen->stack.stack[gen->stack.sp-1];
  1528. treg = Cudd_Regular(top);
  1529. if (!cuddIsConstant(treg)) {
  1530. /* Take the else branch first. */
  1531. gen->gen.cubes.cube[treg->index] = 0;
  1532. next = cuddE(treg);
  1533. if (top != treg) next = Cudd_Not(next);
  1534. gen->stack.stack[gen->stack.sp] = next; gen->stack.sp++;
  1535. } else if (top == Cudd_Not(DD_ONE(dd)) || top == dd->background) {
  1536. /* Backtrack */
  1537. while (1) {
  1538. if (gen->stack.sp == 1) {
  1539. /* The current node has no predecessor. */
  1540. gen->status = CUDD_GEN_EMPTY;
  1541. gen->stack.sp--;
  1542. goto done;
  1543. }
  1544. prev = gen->stack.stack[gen->stack.sp-2];
  1545. preg = Cudd_Regular(prev);
  1546. nreg = cuddT(preg);
  1547. if (prev != preg) {next = Cudd_Not(nreg);} else {next = nreg;}
  1548. if (next != top) { /* follow the then branch next */
  1549. gen->gen.cubes.cube[preg->index] = 1;
  1550. gen->stack.stack[gen->stack.sp-1] = next;
  1551. break;
  1552. }
  1553. /* Pop the stack and try again. */
  1554. gen->gen.cubes.cube[preg->index] = 2;
  1555. gen->stack.sp--;
  1556. top = gen->stack.stack[gen->stack.sp-1];
  1557. treg = Cudd_Regular(top);
  1558. }
  1559. } else {
  1560. gen->status = CUDD_GEN_NONEMPTY;
  1561. gen->gen.cubes.value = cuddV(top);
  1562. goto done;
  1563. }
  1564. }
  1565. done:
  1566. *cube = gen->gen.cubes.cube;
  1567. *value = gen->gen.cubes.value;
  1568. return(gen);
  1569. } /* end of Cudd_FirstCube */
  1570. /**Function********************************************************************
  1571. Synopsis [Generates the next cube of a decision diagram onset.]
  1572. Description [Generates the next cube of a decision diagram onset,
  1573. using generator gen. Returns 0 if the enumeration is completed; 1
  1574. otherwise.]
  1575. SideEffects [The cube and its value are returned as side effects. The
  1576. generator is modified.]
  1577. SeeAlso [Cudd_ForeachCube Cudd_FirstCube Cudd_GenFree Cudd_IsGenEmpty
  1578. Cudd_NextNode]
  1579. ******************************************************************************/
  1580. int
  1581. Cudd_NextCube(
  1582. DdGen * gen,
  1583. int ** cube,
  1584. CUDD_VALUE_TYPE * value)
  1585. {
  1586. DdNode *top, *treg, *next, *nreg, *prev, *preg;
  1587. DdManager *dd = gen->manager;
  1588. /* Backtrack from previously reached terminal node. */
  1589. while (1) {
  1590. if (gen->stack.sp == 1) {
  1591. /* The current node has no predecessor. */
  1592. gen->status = CUDD_GEN_EMPTY;
  1593. gen->stack.sp--;
  1594. goto done;
  1595. }
  1596. top = gen->stack.stack[gen->stack.sp-1];
  1597. treg = Cudd_Regular(top);
  1598. prev = gen->stack.stack[gen->stack.sp-2];
  1599. preg = Cudd_Regular(prev);
  1600. nreg = cuddT(preg);
  1601. if (prev != preg) {next = Cudd_Not(nreg);} else {next = nreg;}
  1602. if (next != top) { /* follow the then branch next */
  1603. gen->gen.cubes.cube[preg->index] = 1;
  1604. gen->stack.stack[gen->stack.sp-1] = next;
  1605. break;
  1606. }
  1607. /* Pop the stack and try again. */
  1608. gen->gen.cubes.cube[preg->index] = 2;
  1609. gen->stack.sp--;
  1610. }
  1611. while (1) {
  1612. top = gen->stack.stack[gen->stack.sp-1];
  1613. treg = Cudd_Regular(top);
  1614. if (!cuddIsConstant(treg)) {
  1615. /* Take the else branch first. */
  1616. gen->gen.cubes.cube[treg->index] = 0;
  1617. next = cuddE(treg);
  1618. if (top != treg) next = Cudd_Not(next);
  1619. gen->stack.stack[gen->stack.sp] = next; gen->stack.sp++;
  1620. } else if (top == Cudd_Not(DD_ONE(dd)) || top == dd->background) {
  1621. /* Backtrack */
  1622. while (1) {
  1623. if (gen->stack.sp == 1) {
  1624. /* The current node has no predecessor. */
  1625. gen->status = CUDD_GEN_EMPTY;
  1626. gen->stack.sp--;
  1627. goto done;
  1628. }
  1629. prev = gen->stack.stack[gen->stack.sp-2];
  1630. preg = Cudd_Regular(prev);
  1631. nreg = cuddT(preg);
  1632. if (prev != preg) {next = Cudd_Not(nreg);} else {next = nreg;}
  1633. if (next != top) { /* follow the then branch next */
  1634. gen->gen.cubes.cube[preg->index] = 1;
  1635. gen->stack.stack[gen->stack.sp-1] = next;
  1636. break;
  1637. }
  1638. /* Pop the stack and try again. */
  1639. gen->gen.cubes.cube[preg->index] = 2;
  1640. gen->stack.sp--;
  1641. top = gen->stack.stack[gen->stack.sp-1];
  1642. treg = Cudd_Regular(top);
  1643. }
  1644. } else {
  1645. gen->status = CUDD_GEN_NONEMPTY;
  1646. gen->gen.cubes.value = cuddV(top);
  1647. goto done;
  1648. }
  1649. }
  1650. done:
  1651. if (gen->status == CUDD_GEN_EMPTY) return(0);
  1652. *cube = gen->gen.cubes.cube;
  1653. *value = gen->gen.cubes.value;
  1654. return(1);
  1655. } /* end of Cudd_NextCube */
  1656. /**Function********************************************************************
  1657. Synopsis [Finds the first prime of a Boolean function.]
  1658. Description [Defines an iterator on a pair of BDDs describing a
  1659. (possibly incompletely specified) Boolean functions and finds the
  1660. first cube of a cover of the function. Returns a generator
  1661. that contains the information necessary to continue the enumeration
  1662. if successful; NULL otherwise.<p>
  1663. The two argument BDDs are the lower and upper bounds of an interval.
  1664. It is a mistake to call this function with a lower bound that is not
  1665. less than or equal to the upper bound.<p>
  1666. A cube is represented as an array of literals, which are integers in
  1667. {0, 1, 2}; 0 represents a complemented literal, 1 represents an
  1668. uncomplemented literal, and 2 stands for don't care. The enumeration
  1669. produces a prime and irredundant cover of the function associated
  1670. with the two BDDs. The size of the array equals the number of
  1671. variables in the manager at the time Cudd_FirstCube is called.<p>
  1672. This iterator can only be used on BDDs.]
  1673. SideEffects [The first cube is returned as side effect.]
  1674. SeeAlso [Cudd_ForeachPrime Cudd_NextPrime Cudd_GenFree Cudd_IsGenEmpty
  1675. Cudd_FirstCube Cudd_FirstNode]
  1676. ******************************************************************************/
  1677. DdGen *
  1678. Cudd_FirstPrime(
  1679. DdManager *dd,
  1680. DdNode *l,
  1681. DdNode *u,
  1682. int **cube)
  1683. {
  1684. DdGen *gen;
  1685. DdNode *implicant, *prime, *tmp;
  1686. int length, result;
  1687. /* Sanity Check. */
  1688. if (dd == NULL || l == NULL || u == NULL) return(NULL);
  1689. /* Allocate generator an initialize it. */
  1690. gen = ALLOC(DdGen,1);
  1691. if (gen == NULL) {
  1692. dd->errorCode = CUDD_MEMORY_OUT;
  1693. return(NULL);
  1694. }
  1695. gen->manager = dd;
  1696. gen->type = CUDD_GEN_PRIMES;
  1697. gen->status = CUDD_GEN_EMPTY;
  1698. gen->gen.primes.cube = NULL;
  1699. gen->gen.primes.ub = u;
  1700. gen->stack.sp = 0;
  1701. gen->stack.stack = NULL;
  1702. gen->node = l;
  1703. cuddRef(l);
  1704. gen->gen.primes.cube = ALLOC(int,dd->size);
  1705. if (gen->gen.primes.cube == NULL) {
  1706. dd->errorCode = CUDD_MEMORY_OUT;
  1707. FREE(gen);
  1708. return(NULL);
  1709. }
  1710. if (gen->node == Cudd_ReadLogicZero(dd)) {
  1711. gen->status = CUDD_GEN_EMPTY;
  1712. } else {
  1713. implicant = Cudd_LargestCube(dd,gen->node,&length);
  1714. if (implicant == NULL) {
  1715. Cudd_RecursiveDeref(dd,gen->node);
  1716. FREE(gen->gen.primes.cube);
  1717. FREE(gen);
  1718. return(NULL);
  1719. }
  1720. cuddRef(implicant);
  1721. prime = Cudd_bddMakePrime(dd,implicant,gen->gen.primes.ub);
  1722. if (prime == NULL) {
  1723. Cudd_RecursiveDeref(dd,gen->node);
  1724. Cudd_RecursiveDeref(dd,implicant);
  1725. FREE(gen->gen.primes.cube);
  1726. FREE(gen);
  1727. return(NULL);
  1728. }
  1729. cuddRef(prime);
  1730. Cudd_RecursiveDeref(dd,implicant);
  1731. tmp = Cudd_bddAnd(dd,gen->node,Cudd_Not(prime));
  1732. if (tmp == NULL) {
  1733. Cudd_RecursiveDeref(dd,gen->node);
  1734. Cudd_RecursiveDeref(dd,prime);
  1735. FREE(gen->gen.primes.cube);
  1736. FREE(gen);
  1737. return(NULL);
  1738. }
  1739. cuddRef(tmp);
  1740. Cudd_RecursiveDeref(dd,gen->node);
  1741. gen->node = tmp;
  1742. result = Cudd_BddToCubeArray(dd,prime,gen->gen.primes.cube);
  1743. if (result == 0) {
  1744. Cudd_RecursiveDeref(dd,gen->node);
  1745. Cudd_RecursiveDeref(dd,prime);
  1746. FREE(gen->gen.primes.cube);
  1747. FREE(gen);
  1748. return(NULL);
  1749. }
  1750. Cudd_RecursiveDeref(dd,prime);
  1751. gen->status = CUDD_GEN_NONEMPTY;
  1752. }
  1753. *cube = gen->gen.primes.cube;
  1754. return(gen);
  1755. } /* end of Cudd_FirstPrime */
  1756. /**Function********************************************************************
  1757. Synopsis [Generates the next prime of a Boolean function.]
  1758. Description [Generates the next cube of a Boolean function,
  1759. using generator gen. Returns 0 if the enumeration is completed; 1
  1760. otherwise.]
  1761. SideEffects [The cube and is returned as side effects. The
  1762. generator is modified.]
  1763. SeeAlso [Cudd_ForeachPrime Cudd_FirstPrime Cudd_GenFree Cudd_IsGenEmpty
  1764. Cudd_NextCube Cudd_NextNode]
  1765. ******************************************************************************/
  1766. int
  1767. Cudd_NextPrime(
  1768. DdGen *gen,
  1769. int **cube)
  1770. {
  1771. DdNode *implicant, *prime, *tmp;
  1772. DdManager *dd = gen->manager;
  1773. int length, result;
  1774. if (gen->node == Cudd_ReadLogicZero(dd)) {
  1775. gen->status = CUDD_GEN_EMPTY;
  1776. } else {
  1777. implicant = Cudd_LargestCube(dd,gen->node,&length);
  1778. if (implicant == NULL) {
  1779. gen->status = CUDD_GEN_EMPTY;
  1780. return(0);
  1781. }
  1782. cuddRef(implicant);
  1783. prime = Cudd_bddMakePrime(dd,implicant,gen->gen.primes.ub);
  1784. if (prime == NULL) {
  1785. Cudd_RecursiveDeref(dd,implicant);
  1786. gen->status = CUDD_GEN_EMPTY;
  1787. return(0);
  1788. }
  1789. cuddRef(prime);
  1790. Cudd_RecursiveDeref(dd,implicant);
  1791. tmp = Cudd_bddAnd(dd,gen->node,Cudd_Not(prime));
  1792. if (tmp == NULL) {
  1793. Cudd_RecursiveDeref(dd,prime);
  1794. gen->status = CUDD_GEN_EMPTY;
  1795. return(0);
  1796. }
  1797. cuddRef(tmp);
  1798. Cudd_RecursiveDeref(dd,gen->node);
  1799. gen->node = tmp;
  1800. result = Cudd_BddToCubeArray(dd,prime,gen->gen.primes.cube);
  1801. if (result == 0) {
  1802. Cudd_RecursiveDeref(dd,prime);
  1803. gen->status = CUDD_GEN_EMPTY;
  1804. return(0);
  1805. }
  1806. Cudd_RecursiveDeref(dd,prime);
  1807. gen->status = CUDD_GEN_NONEMPTY;
  1808. }
  1809. if (gen->status == CUDD_GEN_EMPTY) return(0);
  1810. *cube = gen->gen.primes.cube;
  1811. return(1);
  1812. } /* end of Cudd_NextPrime */
  1813. /**Function********************************************************************
  1814. Synopsis [Computes the cube of an array of BDD variables.]
  1815. Description [Computes the cube of an array of BDD variables. If
  1816. non-null, the phase argument indicates which literal of each
  1817. variable should appear in the cube. If phase\[i\] is nonzero, then the
  1818. positive literal is used. If phase is NULL, the cube is positive unate.
  1819. Returns a pointer to the result if successful; NULL otherwise.]
  1820. SideEffects [None]
  1821. SeeAlso [Cudd_addComputeCube Cudd_IndicesToCube Cudd_CubeArrayToBdd]
  1822. ******************************************************************************/
  1823. DdNode *
  1824. Cudd_bddComputeCube(
  1825. DdManager * dd,
  1826. DdNode ** vars,
  1827. int * phase,
  1828. int n)
  1829. {
  1830. DdNode *cube;
  1831. DdNode *fn;
  1832. int i;
  1833. cube = DD_ONE(dd);
  1834. cuddRef(cube);
  1835. for (i = n - 1; i >= 0; i--) {
  1836. if (phase == NULL || phase[i] != 0) {
  1837. fn = Cudd_bddAnd(dd,vars[i],cube);
  1838. } else {
  1839. fn = Cudd_bddAnd(dd,Cudd_Not(vars[i]),cube);
  1840. }
  1841. if (fn == NULL) {
  1842. Cudd_RecursiveDeref(dd,cube);
  1843. return(NULL);
  1844. }
  1845. cuddRef(fn);
  1846. Cudd_RecursiveDeref(dd,cube);
  1847. cube = fn;
  1848. }
  1849. cuddDeref(cube);
  1850. return(cube);
  1851. } /* end of Cudd_bddComputeCube */
  1852. /**Function********************************************************************
  1853. Synopsis [Computes the cube of an array of ADD variables.]
  1854. Description [Computes the cube of an array of ADD variables. If
  1855. non-null, the phase argument indicates which literal of each
  1856. variable should appear in the cube. If phase\[i\] is nonzero, then the
  1857. positive literal is used. If phase is NULL, the cube is positive unate.
  1858. Returns a pointer to the result if successful; NULL otherwise.]
  1859. SideEffects [none]
  1860. SeeAlso [Cudd_bddComputeCube]
  1861. ******************************************************************************/
  1862. DdNode *
  1863. Cudd_addComputeCube(
  1864. DdManager * dd,
  1865. DdNode ** vars,
  1866. int * phase,
  1867. int n)
  1868. {
  1869. DdNode *cube, *zero;
  1870. DdNode *fn;
  1871. int i;
  1872. cube = DD_ONE(dd);
  1873. cuddRef(cube);
  1874. zero = DD_ZERO(dd);
  1875. for (i = n - 1; i >= 0; i--) {
  1876. if (phase == NULL || phase[i] != 0) {
  1877. fn = Cudd_addIte(dd,vars[i],cube,zero);
  1878. } else {
  1879. fn = Cudd_addIte(dd,vars[i],zero,cube);
  1880. }
  1881. if (fn == NULL) {
  1882. Cudd_RecursiveDeref(dd,cube);
  1883. return(NULL);
  1884. }
  1885. cuddRef(fn);
  1886. Cudd_RecursiveDeref(dd,cube);
  1887. cube = fn;
  1888. }
  1889. cuddDeref(cube);
  1890. return(cube);
  1891. } /* end of Cudd_addComputeCube */
  1892. /**Function********************************************************************
  1893. Synopsis [Builds the BDD of a cube from a positional array.]
  1894. Description [Builds a cube from a positional array. The array must
  1895. have one integer entry for each BDD variable. If the i-th entry is
  1896. 1, the variable of index i appears in true form in the cube; If the
  1897. i-th entry is 0, the variable of index i appears complemented in the
  1898. cube; otherwise the variable does not appear in the cube. Returns a
  1899. pointer to the BDD for the cube if successful; NULL otherwise.]
  1900. SideEffects [None]
  1901. SeeAlso [Cudd_bddComputeCube Cudd_IndicesToCube Cudd_BddToCubeArray]
  1902. ******************************************************************************/
  1903. DdNode *
  1904. Cudd_CubeArrayToBdd(
  1905. DdManager *dd,
  1906. int *array)
  1907. {
  1908. DdNode *cube, *var, *tmp;
  1909. int i;
  1910. int size = Cudd_ReadSize(dd);
  1911. cube = DD_ONE(dd);
  1912. cuddRef(cube);
  1913. for (i = size - 1; i >= 0; i--) {
  1914. if ((array[i] & ~1) == 0) {
  1915. var = Cudd_bddIthVar(dd,i);
  1916. tmp = Cudd_bddAnd(dd,cube,Cudd_NotCond(var,array[i]==0));
  1917. if (tmp == NULL) {
  1918. Cudd_RecursiveDeref(dd,cube);
  1919. return(NULL);
  1920. }
  1921. cuddRef(tmp);
  1922. Cudd_RecursiveDeref(dd,cube);
  1923. cube = tmp;
  1924. }
  1925. }
  1926. cuddDeref(cube);
  1927. return(cube);
  1928. } /* end of Cudd_CubeArrayToBdd */
  1929. /**Function********************************************************************
  1930. Synopsis [Builds a positional array from the BDD of a cube.]
  1931. Description [Builds a positional array from the BDD of a cube.
  1932. Array must have one entry for each BDD variable. The positional
  1933. array has 1 in i-th position if the variable of index i appears in
  1934. true form in the cube; it has 0 in i-th position if the variable of
  1935. index i appears in complemented form in the cube; finally, it has 2
  1936. in i-th position if the variable of index i does not appear in the
  1937. cube. Returns 1 if successful (the BDD is indeed a cube); 0
  1938. otherwise.]
  1939. SideEffects [The result is in the array passed by reference.]
  1940. SeeAlso [Cudd_CubeArrayToBdd]
  1941. ******************************************************************************/
  1942. int
  1943. Cudd_BddToCubeArray(
  1944. DdManager *dd,
  1945. DdNode *cube,
  1946. int *array)
  1947. {
  1948. DdNode *scan, *t, *e;
  1949. int i;
  1950. int size = Cudd_ReadSize(dd);
  1951. DdNode *zero = Cudd_Not(DD_ONE(dd));
  1952. for (i = size-1; i >= 0; i--) {
  1953. array[i] = 2;
  1954. }
  1955. scan = cube;
  1956. while (!Cudd_IsConstant(scan)) {
  1957. int index = Cudd_Regular(scan)->index;
  1958. cuddGetBranches(scan,&t,&e);
  1959. if (t == zero) {
  1960. array[index] = 0;
  1961. scan = e;
  1962. } else if (e == zero) {
  1963. array[index] = 1;
  1964. scan = t;
  1965. } else {
  1966. return(0); /* cube is not a cube */
  1967. }
  1968. }
  1969. if (scan == zero) {
  1970. return(0);
  1971. } else {
  1972. return(1);
  1973. }
  1974. } /* end of Cudd_BddToCubeArray */
  1975. /**Function********************************************************************
  1976. Synopsis [Finds the first node of a decision diagram.]
  1977. Description [Defines an iterator on the nodes of a decision diagram
  1978. and finds its first node. Returns a generator that contains the
  1979. information necessary to continue the enumeration if successful;
  1980. NULL otherwise. The nodes are enumerated in a reverse topological
  1981. order, so that a node is always preceded in the enumeration by its
  1982. descendants.]
  1983. SideEffects [The first node is returned as a side effect.]
  1984. SeeAlso [Cudd_ForeachNode Cudd_NextNode Cudd_GenFree Cudd_IsGenEmpty
  1985. Cudd_FirstCube]
  1986. ******************************************************************************/
  1987. DdGen *
  1988. Cudd_FirstNode(
  1989. DdManager * dd,
  1990. DdNode * f,
  1991. DdNode ** node)
  1992. {
  1993. DdGen *gen;
  1994. int size;
  1995. /* Sanity Check. */
  1996. if (dd == NULL || f == NULL) return(NULL);
  1997. /* Allocate generator an initialize it. */
  1998. gen = ALLOC(DdGen,1);
  1999. if (gen == NULL) {
  2000. dd->errorCode = CUDD_MEMORY_OUT;
  2001. return(NULL);
  2002. }
  2003. gen->manager = dd;
  2004. gen->type = CUDD_GEN_NODES;
  2005. gen->status = CUDD_GEN_EMPTY;
  2006. gen->stack.sp = 0;
  2007. gen->node = NULL;
  2008. /* Collect all the nodes on the generator stack for later perusal. */
  2009. gen->stack.stack = cuddNodeArray(Cudd_Regular(f), &size);
  2010. if (gen->stack.stack == NULL) {
  2011. FREE(gen);
  2012. dd->errorCode = CUDD_MEMORY_OUT;
  2013. return(NULL);
  2014. }
  2015. gen->gen.nodes.size = size;
  2016. /* Find the first node. */
  2017. if (gen->stack.sp < gen->gen.nodes.size) {
  2018. gen->status = CUDD_GEN_NONEMPTY;
  2019. gen->node = gen->stack.stack[gen->stack.sp];
  2020. *node = gen->node;
  2021. }
  2022. return(gen);
  2023. } /* end of Cudd_FirstNode */
  2024. /**Function********************************************************************
  2025. Synopsis [Finds the next node of a decision diagram.]
  2026. Description [Finds the node of a decision diagram, using generator
  2027. gen. Returns 0 if the enumeration is completed; 1 otherwise.]
  2028. SideEffects [The next node is returned as a side effect.]
  2029. SeeAlso [Cudd_ForeachNode Cudd_FirstNode Cudd_GenFree Cudd_IsGenEmpty
  2030. Cudd_NextCube]
  2031. ******************************************************************************/
  2032. int
  2033. Cudd_NextNode(
  2034. DdGen * gen,
  2035. DdNode ** node)
  2036. {
  2037. /* Find the next node. */
  2038. gen->stack.sp++;
  2039. if (gen->stack.sp < gen->gen.nodes.size) {
  2040. gen->node = gen->stack.stack[gen->stack.sp];
  2041. *node = gen->node;
  2042. return(1);
  2043. } else {
  2044. gen->status = CUDD_GEN_EMPTY;
  2045. return(0);
  2046. }
  2047. } /* end of Cudd_NextNode */
  2048. /**Function********************************************************************
  2049. Synopsis [Frees a CUDD generator.]
  2050. Description [Frees a CUDD generator. Always returns 0, so that it can
  2051. be used in mis-like foreach constructs.]
  2052. SideEffects [None]
  2053. SeeAlso [Cudd_ForeachCube Cudd_ForeachNode Cudd_FirstCube Cudd_NextCube
  2054. Cudd_FirstNode Cudd_NextNode Cudd_IsGenEmpty]
  2055. ******************************************************************************/
  2056. int
  2057. Cudd_GenFree(
  2058. DdGen * gen)
  2059. {
  2060. if (gen == NULL) return(0);
  2061. switch (gen->type) {
  2062. case CUDD_GEN_CUBES:
  2063. case CUDD_GEN_ZDD_PATHS:
  2064. FREE(gen->gen.cubes.cube);
  2065. FREE(gen->stack.stack);
  2066. break;
  2067. case CUDD_GEN_PRIMES:
  2068. FREE(gen->gen.primes.cube);
  2069. Cudd_RecursiveDeref(gen->manager,gen->node);
  2070. break;
  2071. case CUDD_GEN_NODES:
  2072. FREE(gen->stack.stack);
  2073. break;
  2074. default:
  2075. return(0);
  2076. }
  2077. FREE(gen);
  2078. return(0);
  2079. } /* end of Cudd_GenFree */
  2080. /**Function********************************************************************
  2081. Synopsis [Queries the status of a generator.]
  2082. Description [Queries the status of a generator. Returns 1 if the
  2083. generator is empty or NULL; 0 otherswise.]
  2084. SideEffects [None]
  2085. SeeAlso [Cudd_ForeachCube Cudd_ForeachNode Cudd_FirstCube Cudd_NextCube
  2086. Cudd_FirstNode Cudd_NextNode Cudd_GenFree]
  2087. ******************************************************************************/
  2088. int
  2089. Cudd_IsGenEmpty(
  2090. DdGen * gen)
  2091. {
  2092. if (gen == NULL) return(1);
  2093. return(gen->status == CUDD_GEN_EMPTY);
  2094. } /* end of Cudd_IsGenEmpty */
  2095. /**Function********************************************************************
  2096. Synopsis [Builds a cube of BDD variables from an array of indices.]
  2097. Description [Builds a cube of BDD variables from an array of indices.
  2098. Returns a pointer to the result if successful; NULL otherwise.]
  2099. SideEffects [None]
  2100. SeeAlso [Cudd_bddComputeCube Cudd_CubeArrayToBdd]
  2101. ******************************************************************************/
  2102. DdNode *
  2103. Cudd_IndicesToCube(
  2104. DdManager * dd,
  2105. int * array,
  2106. int n)
  2107. {
  2108. DdNode *cube, *tmp;
  2109. int i;
  2110. cube = DD_ONE(dd);
  2111. cuddRef(cube);
  2112. for (i = n - 1; i >= 0; i--) {
  2113. tmp = Cudd_bddAnd(dd,Cudd_bddIthVar(dd,array[i]),cube);
  2114. if (tmp == NULL) {
  2115. Cudd_RecursiveDeref(dd,cube);
  2116. return(NULL);
  2117. }
  2118. cuddRef(tmp);
  2119. Cudd_RecursiveDeref(dd,cube);
  2120. cube = tmp;
  2121. }
  2122. cuddDeref(cube);
  2123. return(cube);
  2124. } /* end of Cudd_IndicesToCube */
  2125. /**Function********************************************************************
  2126. Synopsis [Prints the package version number.]
  2127. Description []
  2128. SideEffects [None]
  2129. SeeAlso []
  2130. ******************************************************************************/
  2131. void
  2132. Cudd_PrintVersion(
  2133. FILE * fp)
  2134. {
  2135. (void) fprintf(fp, "%s\n", CUDD_VERSION);
  2136. } /* end of Cudd_PrintVersion */
  2137. /**Function********************************************************************
  2138. Synopsis [Computes the average distance between adjacent nodes.]
  2139. Description [Computes the average distance between adjacent nodes in
  2140. the manager. Adjacent nodes are node pairs such that the second node
  2141. is the then child, else child, or next node in the collision list.]
  2142. SideEffects [None]
  2143. SeeAlso []
  2144. ******************************************************************************/
  2145. double
  2146. Cudd_AverageDistance(
  2147. DdManager * dd)
  2148. {
  2149. double tetotal, nexttotal;
  2150. double tesubtotal, nextsubtotal;
  2151. double temeasured, nextmeasured;
  2152. int i, j;
  2153. int slots, nvars;
  2154. long diff;
  2155. DdNode *scan;
  2156. DdNodePtr *nodelist;
  2157. DdNode *sentinel = &(dd->sentinel);
  2158. nvars = dd->size;
  2159. if (nvars == 0) return(0.0);
  2160. /* Initialize totals. */
  2161. tetotal = 0.0;
  2162. nexttotal = 0.0;
  2163. temeasured = 0.0;
  2164. nextmeasured = 0.0;
  2165. /* Scan the variable subtables. */
  2166. for (i = 0; i < nvars; i++) {
  2167. nodelist = dd->subtables[i].nodelist;
  2168. tesubtotal = 0.0;
  2169. nextsubtotal = 0.0;
  2170. slots = dd->subtables[i].slots;
  2171. for (j = 0; j < slots; j++) {
  2172. scan = nodelist[j];
  2173. while (scan != sentinel) {
  2174. diff = (long) scan - (long) cuddT(scan);
  2175. tesubtotal += (double) ddAbs(diff);
  2176. diff = (long) scan - (long) Cudd_Regular(cuddE(scan));
  2177. tesubtotal += (double) ddAbs(diff);
  2178. temeasured += 2.0;
  2179. if (scan->next != sentinel) {
  2180. diff = (long) scan - (long) scan->next;
  2181. nextsubtotal += (double) ddAbs(diff);
  2182. nextmeasured += 1.0;
  2183. }
  2184. scan = scan->next;
  2185. }
  2186. }
  2187. tetotal += tesubtotal;
  2188. nexttotal += nextsubtotal;
  2189. }
  2190. /* Scan the constant table. */
  2191. nodelist = dd->constants.nodelist;
  2192. nextsubtotal = 0.0;
  2193. slots = dd->constants.slots;
  2194. for (j = 0; j < slots; j++) {
  2195. scan = nodelist[j];
  2196. while (scan != NULL) {
  2197. if (scan->next != NULL) {
  2198. diff = (long) scan - (long) scan->next;
  2199. nextsubtotal += (double) ddAbs(diff);
  2200. nextmeasured += 1.0;
  2201. }
  2202. scan = scan->next;
  2203. }
  2204. }
  2205. nexttotal += nextsubtotal;
  2206. return((tetotal + nexttotal) / (temeasured + nextmeasured));
  2207. } /* end of Cudd_AverageDistance */
  2208. /**Function********************************************************************
  2209. Synopsis [Portable random number generator.]
  2210. Description [Portable number generator based on ran2 from "Numerical
  2211. Recipes in C." It is a long period (> 2 * 10^18) random number generator
  2212. of L'Ecuyer with Bays-Durham shuffle. Returns a long integer uniformly
  2213. distributed between 0 and 2147483561 (inclusive of the endpoint values).
  2214. The random generator can be explicitly initialized by calling
  2215. Cudd_Srandom. If no explicit initialization is performed, then the
  2216. seed 1 is assumed.]
  2217. SideEffects [None]
  2218. SeeAlso [Cudd_Srandom]
  2219. ******************************************************************************/
  2220. long
  2221. Cudd_Random(void)
  2222. {
  2223. int i; /* index in the shuffle table */
  2224. long int w; /* work variable */
  2225. /* cuddRand == 0 if the geneartor has not been initialized yet. */
  2226. if (cuddRand == 0) Cudd_Srandom(1);
  2227. /* Compute cuddRand = (cuddRand * LEQA1) % MODULUS1 avoiding
  2228. ** overflows by Schrage's method.
  2229. */
  2230. w = cuddRand / LEQQ1;
  2231. cuddRand = LEQA1 * (cuddRand - w * LEQQ1) - w * LEQR1;
  2232. cuddRand += (cuddRand < 0) * MODULUS1;
  2233. /* Compute cuddRand2 = (cuddRand2 * LEQA2) % MODULUS2 avoiding
  2234. ** overflows by Schrage's method.
  2235. */
  2236. w = cuddRand2 / LEQQ2;
  2237. cuddRand2 = LEQA2 * (cuddRand2 - w * LEQQ2) - w * LEQR2;
  2238. cuddRand2 += (cuddRand2 < 0) * MODULUS2;
  2239. /* cuddRand is shuffled with the Bays-Durham algorithm.
  2240. ** shuffleSelect and cuddRand2 are combined to generate the output.
  2241. */
  2242. /* Pick one element from the shuffle table; "i" will be in the range
  2243. ** from 0 to STAB_SIZE-1.
  2244. */
  2245. i = (int) (shuffleSelect / STAB_DIV);
  2246. /* Mix the element of the shuffle table with the current iterate of
  2247. ** the second sub-generator, and replace the chosen element of the
  2248. ** shuffle table with the current iterate of the first sub-generator.
  2249. */
  2250. shuffleSelect = shuffleTable[i] - cuddRand2;
  2251. shuffleTable[i] = cuddRand;
  2252. shuffleSelect += (shuffleSelect < 1) * (MODULUS1 - 1);
  2253. /* Since shuffleSelect != 0, and we want to be able to return 0,
  2254. ** here we subtract 1 before returning.
  2255. */
  2256. return(shuffleSelect - 1);
  2257. } /* end of Cudd_Random */
  2258. /**Function********************************************************************
  2259. Synopsis [Initializer for the portable random number generator.]
  2260. Description [Initializer for the portable number generator based on
  2261. ran2 in "Numerical Recipes in C." The input is the seed for the
  2262. generator. If it is negative, its absolute value is taken as seed.
  2263. If it is 0, then 1 is taken as seed. The initialized sets up the two
  2264. recurrences used to generate a long-period stream, and sets up the
  2265. shuffle table.]
  2266. SideEffects [None]
  2267. SeeAlso [Cudd_Random]
  2268. ******************************************************************************/
  2269. void
  2270. Cudd_Srandom(
  2271. long seed)
  2272. {
  2273. int i;
  2274. if (seed < 0) cuddRand = -seed;
  2275. else if (seed == 0) cuddRand = 1;
  2276. else cuddRand = seed;
  2277. cuddRand2 = cuddRand;
  2278. /* Load the shuffle table (after 11 warm-ups). */
  2279. for (i = 0; i < STAB_SIZE + 11; i++) {
  2280. long int w;
  2281. w = cuddRand / LEQQ1;
  2282. cuddRand = LEQA1 * (cuddRand - w * LEQQ1) - w * LEQR1;
  2283. cuddRand += (cuddRand < 0) * MODULUS1;
  2284. shuffleTable[i % STAB_SIZE] = cuddRand;
  2285. }
  2286. shuffleSelect = shuffleTable[1 % STAB_SIZE];
  2287. } /* end of Cudd_Srandom */
  2288. /**Function********************************************************************
  2289. Synopsis [Computes the density of a BDD or ADD.]
  2290. Description [Computes the density of a BDD or ADD. The density is
  2291. the ratio of the number of minterms to the number of nodes. If 0 is
  2292. passed as number of variables, the number of variables existing in
  2293. the manager is used. Returns the density if successful; (double)
  2294. CUDD_OUT_OF_MEM otherwise.]
  2295. SideEffects [None]
  2296. SeeAlso [Cudd_CountMinterm Cudd_DagSize]
  2297. ******************************************************************************/
  2298. double
  2299. Cudd_Density(
  2300. DdManager * dd /* manager */,
  2301. DdNode * f /* function whose density is sought */,
  2302. int nvars /* size of the support of f */)
  2303. {
  2304. double minterms;
  2305. int nodes;
  2306. double density;
  2307. if (nvars == 0) nvars = dd->size;
  2308. minterms = Cudd_CountMinterm(dd,f,nvars);
  2309. if (minterms == (double) CUDD_OUT_OF_MEM) return(minterms);
  2310. nodes = Cudd_DagSize(f);
  2311. density = minterms / (double) nodes;
  2312. return(density);
  2313. } /* end of Cudd_Density */
  2314. /**Function********************************************************************
  2315. Synopsis [Warns that a memory allocation failed.]
  2316. Description [Warns that a memory allocation failed.
  2317. This function can be used as replacement of MMout_of_memory to prevent
  2318. the safe_mem functions of the util package from exiting when malloc
  2319. returns NULL. One possible use is in case of discretionary allocations;
  2320. for instance, the allocation of memory to enlarge the computed table.]
  2321. SideEffects [None]
  2322. SeeAlso []
  2323. ******************************************************************************/
  2324. void
  2325. Cudd_OutOfMem(
  2326. long size /* size of the allocation that failed */)
  2327. {
  2328. (void) fflush(stdout);
  2329. (void) fprintf(stderr, "\nunable to allocate %ld bytes\n", size);
  2330. return;
  2331. } /* end of Cudd_OutOfMem */
  2332. /*---------------------------------------------------------------------------*/
  2333. /* Definition of internal functions */
  2334. /*---------------------------------------------------------------------------*/
  2335. /**Function********************************************************************
  2336. Synopsis [Prints a DD to the standard output. One line per node is
  2337. printed.]
  2338. Description [Prints a DD to the standard output. One line per node is
  2339. printed. Returns 1 if successful; 0 otherwise.]
  2340. SideEffects [None]
  2341. SeeAlso [Cudd_PrintDebug]
  2342. ******************************************************************************/
  2343. int
  2344. cuddP(
  2345. DdManager * dd,
  2346. DdNode * f)
  2347. {
  2348. int retval;
  2349. st_table *table = st_init_table(st_ptrcmp,st_ptrhash);
  2350. if (table == NULL) return(0);
  2351. retval = dp2(dd,f,table);
  2352. st_free_table(table);
  2353. (void) fputc('\n',dd->out);
  2354. return(retval);
  2355. } /* end of cuddP */
  2356. /**Function********************************************************************
  2357. Synopsis [Frees the memory used to store the minterm counts recorded
  2358. in the visited table.]
  2359. Description [Frees the memory used to store the minterm counts
  2360. recorded in the visited table. Returns ST_CONTINUE.]
  2361. SideEffects [None]
  2362. ******************************************************************************/
  2363. enum st_retval
  2364. cuddStCountfree(
  2365. char * key,
  2366. char * value,
  2367. char * arg)
  2368. {
  2369. double *d;
  2370. d = (double *)value;
  2371. FREE(d);
  2372. return(ST_CONTINUE);
  2373. } /* end of cuddStCountfree */
  2374. /**Function********************************************************************
  2375. Synopsis [Recursively collects all the nodes of a DD in a symbol
  2376. table.]
  2377. Description [Traverses the DD f and collects all its nodes in a
  2378. symbol table. f is assumed to be a regular pointer and
  2379. cuddCollectNodes guarantees this assumption in the recursive calls.
  2380. Returns 1 in case of success; 0 otherwise.]
  2381. SideEffects [None]
  2382. SeeAlso []
  2383. ******************************************************************************/
  2384. int
  2385. cuddCollectNodes(
  2386. DdNode * f,
  2387. st_table * visited)
  2388. {
  2389. DdNode *T, *E;
  2390. int retval;
  2391. #ifdef DD_DEBUG
  2392. assert(!Cudd_IsComplement(f));
  2393. #endif
  2394. /* If already visited, nothing to do. */
  2395. if (st_is_member(visited, (char *) f) == 1)
  2396. return(1);
  2397. /* Check for abnormal condition that should never happen. */
  2398. if (f == NULL)
  2399. return(0);
  2400. /* Mark node as visited. */
  2401. if (st_add_direct(visited, (char *) f, NULL) == ST_OUT_OF_MEM)
  2402. return(0);
  2403. /* Check terminal case. */
  2404. if (cuddIsConstant(f))
  2405. return(1);
  2406. /* Recursive calls. */
  2407. T = cuddT(f);
  2408. retval = cuddCollectNodes(T,visited);
  2409. if (retval != 1) return(retval);
  2410. E = Cudd_Regular(cuddE(f));
  2411. retval = cuddCollectNodes(E,visited);
  2412. return(retval);
  2413. } /* end of cuddCollectNodes */
  2414. /**Function********************************************************************
  2415. Synopsis [Recursively collects all the nodes of a DD in an array.]
  2416. Description [Traverses the DD f and collects all its nodes in an array.
  2417. The caller should free the array returned by cuddNodeArray.
  2418. Returns a pointer to the array of nodes in case of success; NULL
  2419. otherwise. The nodes are collected in reverse topological order, so
  2420. that a node is always preceded in the array by all its descendants.]
  2421. SideEffects [The number of nodes is returned as a side effect.]
  2422. SeeAlso [Cudd_FirstNode]
  2423. ******************************************************************************/
  2424. DdNodePtr *
  2425. cuddNodeArray(
  2426. DdNode *f,
  2427. int *n)
  2428. {
  2429. DdNodePtr *table;
  2430. int size, retval;
  2431. size = ddDagInt(Cudd_Regular(f));
  2432. table = ALLOC(DdNodePtr, size);
  2433. if (table == NULL) {
  2434. ddClearFlag(Cudd_Regular(f));
  2435. return(NULL);
  2436. }
  2437. retval = cuddNodeArrayRecur(f, table, 0);
  2438. assert(retval == size);
  2439. *n = size;
  2440. return(table);
  2441. } /* cuddNodeArray */
  2442. /*---------------------------------------------------------------------------*/
  2443. /* Definition of static functions */
  2444. /*---------------------------------------------------------------------------*/
  2445. /**Function********************************************************************
  2446. Synopsis [Performs the recursive step of cuddP.]
  2447. Description [Performs the recursive step of cuddP. Returns 1 in case
  2448. of success; 0 otherwise.]
  2449. SideEffects [None]
  2450. ******************************************************************************/
  2451. static int
  2452. dp2(
  2453. DdManager *dd,
  2454. DdNode * f,
  2455. st_table * t)
  2456. {
  2457. DdNode *g, *n, *N;
  2458. int T,E;
  2459. if (f == NULL) {
  2460. return(0);
  2461. }
  2462. g = Cudd_Regular(f);
  2463. if (cuddIsConstant(g)) {
  2464. #if SIZEOF_VOID_P == 8
  2465. (void) fprintf(dd->out,"ID = %c0x%lx\tvalue = %-9g\n", bang(f),
  2466. (ptruint) g / (ptruint) sizeof(DdNode),cuddV(g));
  2467. #else
  2468. (void) fprintf(dd->out,"ID = %c0x%x\tvalue = %-9g\n", bang(f),
  2469. (ptruint) g / (ptruint) sizeof(DdNode),cuddV(g));
  2470. #endif
  2471. return(1);
  2472. }
  2473. if (st_is_member(t,(char *) g) == 1) {
  2474. return(1);
  2475. }
  2476. if (st_add_direct(t,(char *) g,NULL) == ST_OUT_OF_MEM)
  2477. return(0);
  2478. #ifdef DD_STATS
  2479. #if SIZEOF_VOID_P == 8
  2480. (void) fprintf(dd->out,"ID = %c0x%lx\tindex = %d\tr = %d\t", bang(f),
  2481. (ptruint) g / (ptruint) sizeof(DdNode), g->index, g->ref);
  2482. #else
  2483. (void) fprintf(dd->out,"ID = %c0x%x\tindex = %d\tr = %d\t", bang(f),
  2484. (ptruint) g / (ptruint) sizeof(DdNode),g->index,g->ref);
  2485. #endif
  2486. #else
  2487. #if SIZEOF_VOID_P == 8
  2488. (void) fprintf(dd->out,"ID = %c0x%lx\tindex = %u\t", bang(f),
  2489. (ptruint) g / (ptruint) sizeof(DdNode),g->index);
  2490. #else
  2491. (void) fprintf(dd->out,"ID = %c0x%x\tindex = %hu\t", bang(f),
  2492. (ptruint) g / (ptruint) sizeof(DdNode),g->index);
  2493. #endif
  2494. #endif
  2495. n = cuddT(g);
  2496. if (cuddIsConstant(n)) {
  2497. (void) fprintf(dd->out,"T = %-9g\t",cuddV(n));
  2498. T = 1;
  2499. } else {
  2500. #if SIZEOF_VOID_P == 8
  2501. (void) fprintf(dd->out,"T = 0x%lx\t",(ptruint) n / (ptruint) sizeof(DdNode));
  2502. #else
  2503. (void) fprintf(dd->out,"T = 0x%x\t",(ptruint) n / (ptruint) sizeof(DdNode));
  2504. #endif
  2505. T = 0;
  2506. }
  2507. n = cuddE(g);
  2508. N = Cudd_Regular(n);
  2509. if (cuddIsConstant(N)) {
  2510. (void) fprintf(dd->out,"E = %c%-9g\n",bang(n),cuddV(N));
  2511. E = 1;
  2512. } else {
  2513. #if SIZEOF_VOID_P == 8
  2514. (void) fprintf(dd->out,"E = %c0x%lx\n", bang(n), (ptruint) N/(ptruint) sizeof(DdNode));
  2515. #else
  2516. (void) fprintf(dd->out,"E = %c0x%x\n", bang(n), (ptruint) N/(ptruint) sizeof(DdNode));
  2517. #endif
  2518. E = 0;
  2519. }
  2520. if (E == 0) {
  2521. if (dp2(dd,N,t) == 0)
  2522. return(0);
  2523. }
  2524. if (T == 0) {
  2525. if (dp2(dd,cuddT(g),t) == 0)
  2526. return(0);
  2527. }
  2528. return(1);
  2529. } /* end of dp2 */
  2530. /**Function********************************************************************
  2531. Synopsis [Performs the recursive step of Cudd_PrintMinterm.]
  2532. Description []
  2533. SideEffects [None]
  2534. ******************************************************************************/
  2535. static void
  2536. ddPrintMintermAux(
  2537. DdManager * dd /* manager */,
  2538. DdNode * node /* current node */,
  2539. int * list /* current recursion path */)
  2540. {
  2541. DdNode *N,*Nv,*Nnv;
  2542. int i,v,index;
  2543. N = Cudd_Regular(node);
  2544. if (cuddIsConstant(N)) {
  2545. /* Terminal case: Print one cube based on the current recursion
  2546. ** path, unless we have reached the background value (ADDs) or
  2547. ** the logical zero (BDDs).
  2548. */
  2549. if (node != background && node != zero) {
  2550. for (i = 0; i < dd->size; i++) {
  2551. v = list[i];
  2552. if (v == 0) (void) fprintf(dd->out,"0");
  2553. else if (v == 1) (void) fprintf(dd->out,"1");
  2554. else (void) fprintf(dd->out,"-");
  2555. }
  2556. (void) fprintf(dd->out," % g\n", cuddV(node));
  2557. }
  2558. } else {
  2559. Nv = cuddT(N);
  2560. Nnv = cuddE(N);
  2561. if (Cudd_IsComplement(node)) {
  2562. Nv = Cudd_Not(Nv);
  2563. Nnv = Cudd_Not(Nnv);
  2564. }
  2565. index = N->index;
  2566. list[index] = 0;
  2567. ddPrintMintermAux(dd,Nnv,list);
  2568. list[index] = 1;
  2569. ddPrintMintermAux(dd,Nv,list);
  2570. list[index] = 2;
  2571. }
  2572. return;
  2573. } /* end of ddPrintMintermAux */
  2574. /**Function********************************************************************
  2575. Synopsis [Performs the recursive step of Cudd_DagSize.]
  2576. Description [Performs the recursive step of Cudd_DagSize. Returns the
  2577. number of nodes in the graph rooted at n.]
  2578. SideEffects [None]
  2579. ******************************************************************************/
  2580. static int
  2581. ddDagInt(
  2582. DdNode * n)
  2583. {
  2584. int tval, eval;
  2585. if (Cudd_IsComplement(n->next)) {
  2586. return(0);
  2587. }
  2588. n->next = Cudd_Not(n->next);
  2589. if (cuddIsConstant(n)) {
  2590. return(1);
  2591. }
  2592. tval = ddDagInt(cuddT(n));
  2593. eval = ddDagInt(Cudd_Regular(cuddE(n)));
  2594. return(1 + tval + eval);
  2595. } /* end of ddDagInt */
  2596. /**Function********************************************************************
  2597. Synopsis [Performs the recursive step of cuddNodeArray.]
  2598. Description [Performs the recursive step of cuddNodeArray. Returns
  2599. an the number of nodes in the DD. Clear the least significant bit
  2600. of the next field that was used as visited flag by
  2601. cuddNodeArrayRecur when counting the nodes. node is supposed to be
  2602. regular; the invariant is maintained by this procedure.]
  2603. SideEffects [None]
  2604. SeeAlso []
  2605. ******************************************************************************/
  2606. static int
  2607. cuddNodeArrayRecur(
  2608. DdNode *f,
  2609. DdNodePtr *table,
  2610. int index)
  2611. {
  2612. int tindex, eindex;
  2613. if (!Cudd_IsComplement(f->next)) {
  2614. return(index);
  2615. }
  2616. /* Clear visited flag. */
  2617. f->next = Cudd_Regular(f->next);
  2618. if (cuddIsConstant(f)) {
  2619. table[index] = f;
  2620. return(index + 1);
  2621. }
  2622. tindex = cuddNodeArrayRecur(cuddT(f), table, index);
  2623. eindex = cuddNodeArrayRecur(Cudd_Regular(cuddE(f)), table, tindex);
  2624. table[eindex] = f;
  2625. return(eindex + 1);
  2626. } /* end of cuddNodeArrayRecur */
  2627. /**Function********************************************************************
  2628. Synopsis [Performs the recursive step of Cudd_CofactorEstimate.]
  2629. Description [Performs the recursive step of Cudd_CofactorEstimate.
  2630. Returns an estimate of the number of nodes in the DD of a
  2631. cofactor of node. Uses the least significant bit of the next field as
  2632. visited flag. node is supposed to be regular; the invariant is maintained
  2633. by this procedure.]
  2634. SideEffects [None]
  2635. SeeAlso []
  2636. ******************************************************************************/
  2637. static int
  2638. cuddEstimateCofactor(
  2639. DdManager *dd,
  2640. st_table *table,
  2641. DdNode * node,
  2642. int i,
  2643. int phase,
  2644. DdNode ** ptr)
  2645. {
  2646. int tval, eval, val;
  2647. DdNode *ptrT, *ptrE;
  2648. if (Cudd_IsComplement(node->next)) {
  2649. if (!st_lookup(table,(char *)node,(char **)ptr)) {
  2650. if (st_add_direct(table,(char *)node,(char *)node) ==
  2651. ST_OUT_OF_MEM)
  2652. return(CUDD_OUT_OF_MEM);
  2653. *ptr = node;
  2654. }
  2655. return(0);
  2656. }
  2657. node->next = Cudd_Not(node->next);
  2658. if (cuddIsConstant(node)) {
  2659. *ptr = node;
  2660. if (st_add_direct(table,(char *)node,(char *)node) == ST_OUT_OF_MEM)
  2661. return(CUDD_OUT_OF_MEM);
  2662. return(1);
  2663. }
  2664. if ((int) node->index == i) {
  2665. if (phase == 1) {
  2666. *ptr = cuddT(node);
  2667. val = ddDagInt(cuddT(node));
  2668. } else {
  2669. *ptr = cuddE(node);
  2670. val = ddDagInt(Cudd_Regular(cuddE(node)));
  2671. }
  2672. if (node->ref > 1) {
  2673. if (st_add_direct(table,(char *)node,(char *)*ptr) ==
  2674. ST_OUT_OF_MEM)
  2675. return(CUDD_OUT_OF_MEM);
  2676. }
  2677. return(val);
  2678. }
  2679. if (dd->perm[node->index] > dd->perm[i]) {
  2680. *ptr = node;
  2681. tval = ddDagInt(cuddT(node));
  2682. eval = ddDagInt(Cudd_Regular(cuddE(node)));
  2683. if (node->ref > 1) {
  2684. if (st_add_direct(table,(char *)node,(char *)node) ==
  2685. ST_OUT_OF_MEM)
  2686. return(CUDD_OUT_OF_MEM);
  2687. }
  2688. val = 1 + tval + eval;
  2689. return(val);
  2690. }
  2691. tval = cuddEstimateCofactor(dd,table,cuddT(node),i,phase,&ptrT);
  2692. eval = cuddEstimateCofactor(dd,table,Cudd_Regular(cuddE(node)),i,
  2693. phase,&ptrE);
  2694. ptrE = Cudd_NotCond(ptrE,Cudd_IsComplement(cuddE(node)));
  2695. if (ptrT == ptrE) { /* recombination */
  2696. *ptr = ptrT;
  2697. val = tval;
  2698. if (node->ref > 1) {
  2699. if (st_add_direct(table,(char *)node,(char *)*ptr) ==
  2700. ST_OUT_OF_MEM)
  2701. return(CUDD_OUT_OF_MEM);
  2702. }
  2703. } else if ((ptrT != cuddT(node) || ptrE != cuddE(node)) &&
  2704. (*ptr = cuddUniqueLookup(dd,node->index,ptrT,ptrE)) != NULL) {
  2705. if (Cudd_IsComplement((*ptr)->next)) {
  2706. val = 0;
  2707. } else {
  2708. val = 1 + tval + eval;
  2709. }
  2710. if (node->ref > 1) {
  2711. if (st_add_direct(table,(char *)node,(char *)*ptr) ==
  2712. ST_OUT_OF_MEM)
  2713. return(CUDD_OUT_OF_MEM);
  2714. }
  2715. } else {
  2716. *ptr = node;
  2717. val = 1 + tval + eval;
  2718. }
  2719. return(val);
  2720. } /* end of cuddEstimateCofactor */
  2721. /**Function********************************************************************
  2722. Synopsis [Checks the unique table for the existence of an internal node.]
  2723. Description [Checks the unique table for the existence of an internal
  2724. node. Returns a pointer to the node if it is in the table; NULL otherwise.]
  2725. SideEffects [None]
  2726. SeeAlso [cuddUniqueInter]
  2727. ******************************************************************************/
  2728. static DdNode *
  2729. cuddUniqueLookup(
  2730. DdManager * unique,
  2731. int index,
  2732. DdNode * T,
  2733. DdNode * E)
  2734. {
  2735. int posn;
  2736. unsigned int level;
  2737. DdNodePtr *nodelist;
  2738. DdNode *looking;
  2739. DdSubtable *subtable;
  2740. if (index >= unique->size) {
  2741. return(NULL);
  2742. }
  2743. level = unique->perm[index];
  2744. subtable = &(unique->subtables[level]);
  2745. #ifdef DD_DEBUG
  2746. assert(level < (unsigned) cuddI(unique,T->index));
  2747. assert(level < (unsigned) cuddI(unique,Cudd_Regular(E)->index));
  2748. #endif
  2749. posn = ddHash(T, E, subtable->shift);
  2750. nodelist = subtable->nodelist;
  2751. looking = nodelist[posn];
  2752. while (T < cuddT(looking)) {
  2753. looking = Cudd_Regular(looking->next);
  2754. }
  2755. while (T == cuddT(looking) && E < cuddE(looking)) {
  2756. looking = Cudd_Regular(looking->next);
  2757. }
  2758. if (cuddT(looking) == T && cuddE(looking) == E) {
  2759. return(looking);
  2760. }
  2761. return(NULL);
  2762. } /* end of cuddUniqueLookup */
  2763. /**Function********************************************************************
  2764. Synopsis [Performs the recursive step of Cudd_CofactorEstimateSimple.]
  2765. Description [Performs the recursive step of Cudd_CofactorEstimateSimple.
  2766. Returns an estimate of the number of nodes in the DD of the positive
  2767. cofactor of node. Uses the least significant bit of the next field as
  2768. visited flag. node is supposed to be regular; the invariant is maintained
  2769. by this procedure.]
  2770. SideEffects [None]
  2771. SeeAlso []
  2772. ******************************************************************************/
  2773. static int
  2774. cuddEstimateCofactorSimple(
  2775. DdNode * node,
  2776. int i)
  2777. {
  2778. int tval, eval;
  2779. if (Cudd_IsComplement(node->next)) {
  2780. return(0);
  2781. }
  2782. node->next = Cudd_Not(node->next);
  2783. if (cuddIsConstant(node)) {
  2784. return(1);
  2785. }
  2786. tval = cuddEstimateCofactorSimple(cuddT(node),i);
  2787. if ((int) node->index == i) return(tval);
  2788. eval = cuddEstimateCofactorSimple(Cudd_Regular(cuddE(node)),i);
  2789. return(1 + tval + eval);
  2790. } /* end of cuddEstimateCofactorSimple */
  2791. /**Function********************************************************************
  2792. Synopsis [Performs the recursive step of Cudd_CountMinterm.]
  2793. Description [Performs the recursive step of Cudd_CountMinterm.
  2794. It is based on the following identity. Let |f| be the
  2795. number of minterms of f. Then:
  2796. <xmp>
  2797. |f| = (|f0|+|f1|)/2
  2798. </xmp>
  2799. where f0 and f1 are the two cofactors of f. Does not use the
  2800. identity |f'| = max - |f|, to minimize loss of accuracy due to
  2801. roundoff. Returns the number of minterms of the function rooted at
  2802. node.]
  2803. SideEffects [None]
  2804. ******************************************************************************/
  2805. static double
  2806. ddCountMintermAux(
  2807. DdNode * node,
  2808. double max,
  2809. DdHashTable * table)
  2810. {
  2811. DdNode *N, *Nt, *Ne;
  2812. double min, minT, minE;
  2813. DdNode *res;
  2814. N = Cudd_Regular(node);
  2815. if (cuddIsConstant(N)) {
  2816. if (node == background || node == zero) {
  2817. return(0.0);
  2818. } else {
  2819. return(max);
  2820. }
  2821. }
  2822. if (N->ref != 1 && (res = cuddHashTableLookup1(table,node)) != NULL) {
  2823. min = cuddV(res);
  2824. if (res->ref == 0) {
  2825. table->manager->dead++;
  2826. table->manager->constants.dead++;
  2827. }
  2828. return(min);
  2829. }
  2830. Nt = cuddT(N); Ne = cuddE(N);
  2831. if (Cudd_IsComplement(node)) {
  2832. Nt = Cudd_Not(Nt); Ne = Cudd_Not(Ne);
  2833. }
  2834. minT = ddCountMintermAux(Nt,max,table);
  2835. if (minT == (double)CUDD_OUT_OF_MEM) return((double)CUDD_OUT_OF_MEM);
  2836. minT *= 0.5;
  2837. minE = ddCountMintermAux(Ne,max,table);
  2838. if (minE == (double)CUDD_OUT_OF_MEM) return((double)CUDD_OUT_OF_MEM);
  2839. minE *= 0.5;
  2840. min = minT + minE;
  2841. if (N->ref != 1) {
  2842. ptrint fanout = (ptrint) N->ref;
  2843. cuddSatDec(fanout);
  2844. res = cuddUniqueConst(table->manager,min);
  2845. if (!cuddHashTableInsert1(table,node,res,fanout)) {
  2846. cuddRef(res); Cudd_RecursiveDeref(table->manager, res);
  2847. return((double)CUDD_OUT_OF_MEM);
  2848. }
  2849. }
  2850. return(min);
  2851. } /* end of ddCountMintermAux */
  2852. /**Function********************************************************************
  2853. Synopsis [Performs the recursive step of Cudd_CountPath.]
  2854. Description [Performs the recursive step of Cudd_CountPath.
  2855. It is based on the following identity. Let |f| be the
  2856. number of paths of f. Then:
  2857. <xmp>
  2858. |f| = |f0|+|f1|
  2859. </xmp>
  2860. where f0 and f1 are the two cofactors of f. Uses the
  2861. identity |f'| = |f|, to improve the utilization of the (local) cache.
  2862. Returns the number of paths of the function rooted at node.]
  2863. SideEffects [None]
  2864. ******************************************************************************/
  2865. static double
  2866. ddCountPathAux(
  2867. DdNode * node,
  2868. st_table * table)
  2869. {
  2870. DdNode *Nv, *Nnv;
  2871. double paths, *ppaths, paths1, paths2;
  2872. double *dummy;
  2873. if (cuddIsConstant(node)) {
  2874. return(1.0);
  2875. }
  2876. if (st_lookup(table, node, &dummy)) {
  2877. paths = *dummy;
  2878. return(paths);
  2879. }
  2880. Nv = cuddT(node); Nnv = cuddE(node);
  2881. paths1 = ddCountPathAux(Nv,table);
  2882. if (paths1 == (double)CUDD_OUT_OF_MEM) return((double)CUDD_OUT_OF_MEM);
  2883. paths2 = ddCountPathAux(Cudd_Regular(Nnv),table);
  2884. if (paths2 == (double)CUDD_OUT_OF_MEM) return((double)CUDD_OUT_OF_MEM);
  2885. paths = paths1 + paths2;
  2886. ppaths = ALLOC(double,1);
  2887. if (ppaths == NULL) {
  2888. return((double)CUDD_OUT_OF_MEM);
  2889. }
  2890. *ppaths = paths;
  2891. if (st_add_direct(table,(char *)node, (char *)ppaths) == ST_OUT_OF_MEM) {
  2892. FREE(ppaths);
  2893. return((double)CUDD_OUT_OF_MEM);
  2894. }
  2895. return(paths);
  2896. } /* end of ddCountPathAux */
  2897. /**Function********************************************************************
  2898. Synopsis [Performs the recursive step of Cudd_EpdCountMinterm.]
  2899. Description [Performs the recursive step of Cudd_EpdCountMinterm.
  2900. It is based on the following identity. Let |f| be the
  2901. number of minterms of f. Then:
  2902. <xmp>
  2903. |f| = (|f0|+|f1|)/2
  2904. </xmp>
  2905. where f0 and f1 are the two cofactors of f. Does not use the
  2906. identity |f'| = max - |f|, to minimize loss of accuracy due to
  2907. roundoff. Returns the number of minterms of the function rooted at
  2908. node.]
  2909. SideEffects [None]
  2910. ******************************************************************************/
  2911. static int
  2912. ddEpdCountMintermAux(
  2913. DdNode * node,
  2914. EpDouble * max,
  2915. EpDouble * epd,
  2916. st_table * table)
  2917. {
  2918. DdNode *Nt, *Ne;
  2919. EpDouble *min, minT, minE;
  2920. EpDouble *res;
  2921. int status;
  2922. /* node is assumed to be regular */
  2923. if (cuddIsConstant(node)) {
  2924. if (node == background || node == zero) {
  2925. EpdMakeZero(epd, 0);
  2926. } else {
  2927. EpdCopy(max, epd);
  2928. }
  2929. return(0);
  2930. }
  2931. if (node->ref != 1 && st_lookup(table, node, &res)) {
  2932. EpdCopy(res, epd);
  2933. return(0);
  2934. }
  2935. Nt = cuddT(node); Ne = cuddE(node);
  2936. status = ddEpdCountMintermAux(Nt,max,&minT,table);
  2937. if (status == CUDD_OUT_OF_MEM) return(CUDD_OUT_OF_MEM);
  2938. EpdMultiply(&minT, (double)0.5);
  2939. status = ddEpdCountMintermAux(Cudd_Regular(Ne),max,&minE,table);
  2940. if (status == CUDD_OUT_OF_MEM) return(CUDD_OUT_OF_MEM);
  2941. if (Cudd_IsComplement(Ne)) {
  2942. EpdSubtract3(max, &minE, epd);
  2943. EpdCopy(epd, &minE);
  2944. }
  2945. EpdMultiply(&minE, (double)0.5);
  2946. EpdAdd3(&minT, &minE, epd);
  2947. if (node->ref > 1) {
  2948. min = EpdAlloc();
  2949. if (!min)
  2950. return(CUDD_OUT_OF_MEM);
  2951. EpdCopy(epd, min);
  2952. if (st_insert(table, (char *)node, (char *)min) == ST_OUT_OF_MEM) {
  2953. EpdFree(min);
  2954. return(CUDD_OUT_OF_MEM);
  2955. }
  2956. }
  2957. return(0);
  2958. } /* end of ddEpdCountMintermAux */
  2959. /**Function********************************************************************
  2960. Synopsis [Performs the recursive step of Cudd_CountPathsToNonZero.]
  2961. Description [Performs the recursive step of Cudd_CountPathsToNonZero.
  2962. It is based on the following identity. Let |f| be the
  2963. number of paths of f. Then:
  2964. <xmp>
  2965. |f| = |f0|+|f1|
  2966. </xmp>
  2967. where f0 and f1 are the two cofactors of f. Returns the number of
  2968. paths of the function rooted at node.]
  2969. SideEffects [None]
  2970. ******************************************************************************/
  2971. static double
  2972. ddCountPathsToNonZero(
  2973. DdNode * N,
  2974. st_table * table)
  2975. {
  2976. DdNode *node, *Nt, *Ne;
  2977. double paths, *ppaths, paths1, paths2;
  2978. double *dummy;
  2979. node = Cudd_Regular(N);
  2980. if (cuddIsConstant(node)) {
  2981. return((double) !(Cudd_IsComplement(N) || cuddV(node)==DD_ZERO_VAL));
  2982. }
  2983. if (st_lookup(table, N, &dummy)) {
  2984. paths = *dummy;
  2985. return(paths);
  2986. }
  2987. Nt = cuddT(node); Ne = cuddE(node);
  2988. if (node != N) {
  2989. Nt = Cudd_Not(Nt); Ne = Cudd_Not(Ne);
  2990. }
  2991. paths1 = ddCountPathsToNonZero(Nt,table);
  2992. if (paths1 == (double)CUDD_OUT_OF_MEM) return((double)CUDD_OUT_OF_MEM);
  2993. paths2 = ddCountPathsToNonZero(Ne,table);
  2994. if (paths2 == (double)CUDD_OUT_OF_MEM) return((double)CUDD_OUT_OF_MEM);
  2995. paths = paths1 + paths2;
  2996. ppaths = ALLOC(double,1);
  2997. if (ppaths == NULL) {
  2998. return((double)CUDD_OUT_OF_MEM);
  2999. }
  3000. *ppaths = paths;
  3001. if (st_add_direct(table,(char *)N, (char *)ppaths) == ST_OUT_OF_MEM) {
  3002. FREE(ppaths);
  3003. return((double)CUDD_OUT_OF_MEM);
  3004. }
  3005. return(paths);
  3006. } /* end of ddCountPathsToNonZero */
  3007. /**Function********************************************************************
  3008. Synopsis [Performs the recursive step of Cudd_Support.]
  3009. Description [Performs the recursive step of Cudd_Support. Performs a
  3010. DFS from f. The support is accumulated in supp as a side effect. Uses
  3011. the LSB of the then pointer as visited flag.]
  3012. SideEffects [None]
  3013. SeeAlso [ddClearFlag]
  3014. ******************************************************************************/
  3015. static void
  3016. ddSupportStep(
  3017. DdNode * f,
  3018. int * support)
  3019. {
  3020. if (cuddIsConstant(f) || Cudd_IsComplement(f->next))
  3021. return;
  3022. support[f->index] = 1;
  3023. ddSupportStep(cuddT(f),support);
  3024. ddSupportStep(Cudd_Regular(cuddE(f)),support);
  3025. /* Mark as visited. */
  3026. f->next = Cudd_Complement(f->next);
  3027. } /* end of ddSupportStep */
  3028. /**Function********************************************************************
  3029. Synopsis [Performs a DFS from f, clearing the LSB of the next
  3030. pointers.]
  3031. Description []
  3032. SideEffects [None]
  3033. SeeAlso [ddSupportStep ddFindSupport ddLeavesInt ddDagInt]
  3034. ******************************************************************************/
  3035. static void
  3036. ddClearFlag(
  3037. DdNode * f)
  3038. {
  3039. if (!Cudd_IsComplement(f->next)) {
  3040. return;
  3041. }
  3042. /* Clear visited flag. */
  3043. f->next = Cudd_Regular(f->next);
  3044. if (cuddIsConstant(f)) {
  3045. return;
  3046. }
  3047. ddClearFlag(cuddT(f));
  3048. ddClearFlag(Cudd_Regular(cuddE(f)));
  3049. return;
  3050. } /* end of ddClearFlag */
  3051. /**Function********************************************************************
  3052. Synopsis [Performs the recursive step of Cudd_CountLeaves.]
  3053. Description [Performs the recursive step of Cudd_CountLeaves. Returns
  3054. the number of leaves in the DD rooted at n.]
  3055. SideEffects [None]
  3056. SeeAlso [Cudd_CountLeaves]
  3057. ******************************************************************************/
  3058. static int
  3059. ddLeavesInt(
  3060. DdNode * n)
  3061. {
  3062. int tval, eval;
  3063. if (Cudd_IsComplement(n->next)) {
  3064. return(0);
  3065. }
  3066. n->next = Cudd_Not(n->next);
  3067. if (cuddIsConstant(n)) {
  3068. return(1);
  3069. }
  3070. tval = ddLeavesInt(cuddT(n));
  3071. eval = ddLeavesInt(Cudd_Regular(cuddE(n)));
  3072. return(tval + eval);
  3073. } /* end of ddLeavesInt */
  3074. /**Function********************************************************************
  3075. Synopsis [Performs the recursive step of Cudd_bddPickArbitraryMinterms.]
  3076. Description [Performs the recursive step of Cudd_bddPickArbitraryMinterms.
  3077. Returns 1 if successful; 0 otherwise.]
  3078. SideEffects [none]
  3079. SeeAlso [Cudd_bddPickArbitraryMinterms]
  3080. ******************************************************************************/
  3081. static int
  3082. ddPickArbitraryMinterms(
  3083. DdManager *dd,
  3084. DdNode *node,
  3085. int nvars,
  3086. int nminterms,
  3087. char **string)
  3088. {
  3089. DdNode *N, *T, *E;
  3090. DdNode *one, *bzero;
  3091. int i, t, result;
  3092. double min1, min2;
  3093. if (string == NULL || node == NULL) return(0);
  3094. /* The constant 0 function has no on-set cubes. */
  3095. one = DD_ONE(dd);
  3096. bzero = Cudd_Not(one);
  3097. if (nminterms == 0 || node == bzero) return(1);
  3098. if (node == one) {
  3099. return(1);
  3100. }
  3101. N = Cudd_Regular(node);
  3102. T = cuddT(N); E = cuddE(N);
  3103. if (Cudd_IsComplement(node)) {
  3104. T = Cudd_Not(T); E = Cudd_Not(E);
  3105. }
  3106. min1 = Cudd_CountMinterm(dd, T, nvars) / 2.0;
  3107. if (min1 == (double)CUDD_OUT_OF_MEM) return(0);
  3108. min2 = Cudd_CountMinterm(dd, E, nvars) / 2.0;
  3109. if (min2 == (double)CUDD_OUT_OF_MEM) return(0);
  3110. t = (int)((double)nminterms * min1 / (min1 + min2) + 0.5);
  3111. for (i = 0; i < t; i++)
  3112. string[i][N->index] = '1';
  3113. for (i = t; i < nminterms; i++)
  3114. string[i][N->index] = '0';
  3115. result = ddPickArbitraryMinterms(dd,T,nvars,t,&string[0]);
  3116. if (result == 0)
  3117. return(0);
  3118. result = ddPickArbitraryMinterms(dd,E,nvars,nminterms-t,&string[t]);
  3119. return(result);
  3120. } /* end of ddPickArbitraryMinterms */
  3121. /**Function********************************************************************
  3122. Synopsis [Finds a representative cube of a BDD.]
  3123. Description [Finds a representative cube of a BDD with the weight of
  3124. each variable. From the top variable, if the weight is greater than or
  3125. equal to 0.0, choose THEN branch unless the child is the constant 0.
  3126. Otherwise, choose ELSE branch unless the child is the constant 0.]
  3127. SideEffects [Cudd_SubsetWithMaskVars Cudd_bddPickOneCube]
  3128. ******************************************************************************/
  3129. static int
  3130. ddPickRepresentativeCube(
  3131. DdManager *dd,
  3132. DdNode *node,
  3133. double *weight,
  3134. char *string)
  3135. {
  3136. DdNode *N, *T, *E;
  3137. DdNode *one, *bzero;
  3138. if (string == NULL || node == NULL) return(0);
  3139. /* The constant 0 function has no on-set cubes. */
  3140. one = DD_ONE(dd);
  3141. bzero = Cudd_Not(one);
  3142. if (node == bzero) return(0);
  3143. if (node == DD_ONE(dd)) return(1);
  3144. for (;;) {
  3145. N = Cudd_Regular(node);
  3146. if (N == one)
  3147. break;
  3148. T = cuddT(N);
  3149. E = cuddE(N);
  3150. if (Cudd_IsComplement(node)) {
  3151. T = Cudd_Not(T);
  3152. E = Cudd_Not(E);
  3153. }
  3154. if (weight[N->index] >= 0.0) {
  3155. if (T == bzero) {
  3156. node = E;
  3157. string[N->index] = '0';
  3158. } else {
  3159. node = T;
  3160. string[N->index] = '1';
  3161. }
  3162. } else {
  3163. if (E == bzero) {
  3164. node = T;
  3165. string[N->index] = '1';
  3166. } else {
  3167. node = E;
  3168. string[N->index] = '0';
  3169. }
  3170. }
  3171. }
  3172. return(1);
  3173. } /* end of ddPickRepresentativeCube */
  3174. /**Function********************************************************************
  3175. Synopsis [Frees the memory used to store the minterm counts recorded
  3176. in the visited table.]
  3177. Description [Frees the memory used to store the minterm counts
  3178. recorded in the visited table. Returns ST_CONTINUE.]
  3179. SideEffects [None]
  3180. ******************************************************************************/
  3181. static enum st_retval
  3182. ddEpdFree(
  3183. char * key,
  3184. char * value,
  3185. char * arg)
  3186. {
  3187. EpDouble *epd;
  3188. epd = (EpDouble *) value;
  3189. EpdFree(epd);
  3190. return(ST_CONTINUE);
  3191. } /* end of ddEpdFree */
  3192. /**Function********************************************************************
  3193. Synopsis [Recursively find the support of f.]
  3194. Description [Recursively find the support of f. This function uses the
  3195. LSB of the next field of the nodes of f as visited flag. It also uses the
  3196. LSB of the next field of the variables as flag to remember whether a
  3197. certain index has already been seen. Finally, it uses the manager stack
  3198. to record all seen indices.]
  3199. SideEffects [The stack pointer SP is modified by side-effect. The next
  3200. fields are changed and need to be reset.]
  3201. ******************************************************************************/
  3202. static void
  3203. ddFindSupport(
  3204. DdManager *dd,
  3205. DdNode *f,
  3206. int *SP)
  3207. {
  3208. int index;
  3209. DdNode *var;
  3210. if (cuddIsConstant(f) || Cudd_IsComplement(f->next)) {
  3211. return;
  3212. }
  3213. index = f->index;
  3214. var = dd->vars[index];
  3215. /* It is possible that var is embedded in f. That causes no problem,
  3216. ** though, because if we see it after encountering another node with
  3217. ** the same index, nothing is supposed to happen.
  3218. */
  3219. if (!Cudd_IsComplement(var->next)) {
  3220. var->next = Cudd_Complement(var->next);
  3221. dd->stack[*SP] = (DdNode *)(ptrint) index;
  3222. (*SP)++;
  3223. }
  3224. ddFindSupport(dd, cuddT(f), SP);
  3225. ddFindSupport(dd, Cudd_Regular(cuddE(f)), SP);
  3226. /* Mark as visited. */
  3227. f->next = Cudd_Complement(f->next);
  3228. } /* end of ddFindSupport */
  3229. /**Function********************************************************************
  3230. Synopsis [Clears visited flags for variables.]
  3231. Description [Clears visited flags for variables.]
  3232. SideEffects [None]
  3233. ******************************************************************************/
  3234. static void
  3235. ddClearVars(
  3236. DdManager *dd,
  3237. int SP)
  3238. {
  3239. int i;
  3240. for (i = 0; i < SP; i++) {
  3241. int index = (int) (ptrint) dd->stack[i];
  3242. DdNode *var = dd->vars[index];
  3243. var->next = Cudd_Regular(var->next);
  3244. }
  3245. } /* end of ddClearVars */
  3246. /**Function********************************************************************
  3247. Synopsis [Compares indices for qsort.]
  3248. Description [Compares indices for qsort. Subtracting these integers
  3249. cannot produce overflow, because they are non-negative.]
  3250. SideEffects [None]
  3251. ******************************************************************************/
  3252. static int
  3253. indexCompare(
  3254. const void *a,
  3255. const void *b)
  3256. {
  3257. int ia = *((int *) a);
  3258. int ib = *((int *) b);
  3259. return(ia - ib);
  3260. } /* end of indexCompare */