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.

4228 lines
101 KiB

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