The source code and dockerfile for the GSW2024 AI Lab.
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

1127 lines
30 KiB

2 months ago
  1. /**
  2. @file
  3. @ingroup cudd
  4. @brief Utility functions for ZDDs.
  5. @author Hyong-Kyoon Shin, In-Ho Moon, 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 "util.h"
  35. #include "cuddInt.h"
  36. /*---------------------------------------------------------------------------*/
  37. /* Constant declarations */
  38. /*---------------------------------------------------------------------------*/
  39. /*---------------------------------------------------------------------------*/
  40. /* Stucture declarations */
  41. /*---------------------------------------------------------------------------*/
  42. /*---------------------------------------------------------------------------*/
  43. /* Type declarations */
  44. /*---------------------------------------------------------------------------*/
  45. /*---------------------------------------------------------------------------*/
  46. /* Variable declarations */
  47. /*---------------------------------------------------------------------------*/
  48. /*---------------------------------------------------------------------------*/
  49. /* Macro declarations */
  50. /*---------------------------------------------------------------------------*/
  51. /** \cond */
  52. /*---------------------------------------------------------------------------*/
  53. /* Static function prototypes */
  54. /*---------------------------------------------------------------------------*/
  55. static int zp2 (DdManager *zdd, DdNode *f, st_table *t);
  56. static void zdd_print_minterm_aux (DdManager *zdd, DdNode *node, int level, int *list);
  57. static void zddPrintCoverAux (DdManager *zdd, DdNode *node, int level, int *list);
  58. static void zddSupportStep(DdNode * f, int * support);
  59. static void zddClearFlag(DdNode * f);
  60. /** \endcond */
  61. /*---------------------------------------------------------------------------*/
  62. /* Definition of exported functions */
  63. /*---------------------------------------------------------------------------*/
  64. /**
  65. @brief Prints a disjoint sum of product form for a %ZDD.
  66. @return 1 if successful; 0 otherwise.
  67. @sideeffect None
  68. @see Cudd_zddPrintDebug Cudd_zddPrintCover
  69. */
  70. int
  71. Cudd_zddPrintMinterm(
  72. DdManager * zdd,
  73. DdNode * node)
  74. {
  75. int i, size;
  76. int *list;
  77. size = (int)zdd->sizeZ;
  78. list = ALLOC(int, size);
  79. if (list == NULL) {
  80. zdd->errorCode = CUDD_MEMORY_OUT;
  81. return(0);
  82. }
  83. for (i = 0; i < size; i++) list[i] = 3; /* bogus value should disappear */
  84. zdd_print_minterm_aux(zdd, node, 0, list);
  85. FREE(list);
  86. return(1);
  87. } /* end of Cudd_zddPrintMinterm */
  88. /**
  89. @brief Prints a sum of products from a %ZDD representing a cover.
  90. @return 1 if successful; 0 otherwise.
  91. @sideeffect None
  92. @see Cudd_zddPrintMinterm
  93. */
  94. int
  95. Cudd_zddPrintCover(
  96. DdManager * zdd,
  97. DdNode * node)
  98. {
  99. int i, size;
  100. int *list;
  101. size = (int)zdd->sizeZ;
  102. if (size % 2 != 0) return(0); /* number of variables should be even */
  103. list = ALLOC(int, size);
  104. if (list == NULL) {
  105. zdd->errorCode = CUDD_MEMORY_OUT;
  106. return(0);
  107. }
  108. for (i = 0; i < size; i++) list[i] = 3; /* bogus value should disappear */
  109. zddPrintCoverAux(zdd, node, 0, list);
  110. FREE(list);
  111. return(1);
  112. } /* end of Cudd_zddPrintCover */
  113. /**
  114. @brief Prints to the standard output a %ZDD and its statistics.
  115. @details The statistics include the number of nodes and the number of
  116. minterms. (The number of minterms is also the number of combinations
  117. in the set.) The statistics are printed if pr &gt; 0. Specifically:
  118. <ul>
  119. <li> pr = 0 : prints nothing
  120. <li> pr = 1 : prints counts of nodes and minterms
  121. <li> pr = 2 : prints counts + disjoint sum of products
  122. <li> pr = 3 : prints counts + list of nodes
  123. <li> pr &gt; 3 : prints counts + disjoint sum of products + list of nodes
  124. </ul>
  125. @return 1 if successful; 0 otherwise.
  126. @sideeffect None
  127. */
  128. int
  129. Cudd_zddPrintDebug(
  130. DdManager * zdd,
  131. DdNode * f,
  132. int n,
  133. int pr)
  134. {
  135. DdNode *empty = DD_ZERO(zdd);
  136. int nodes;
  137. double minterms;
  138. int retval = 1;
  139. if (f == empty && pr > 0) {
  140. (void) fprintf(zdd->out,": is the empty ZDD\n");
  141. (void) fflush(zdd->out);
  142. return(1);
  143. }
  144. if (pr > 0) {
  145. nodes = Cudd_zddDagSize(f);
  146. if (nodes == CUDD_OUT_OF_MEM) retval = 0;
  147. minterms = Cudd_zddCountMinterm(zdd, f, n);
  148. if (minterms == (double)CUDD_OUT_OF_MEM) retval = 0;
  149. (void) fprintf(zdd->out,": %d nodes %g minterms\n",
  150. nodes, minterms);
  151. if (pr > 2)
  152. if (!cuddZddP(zdd, f)) retval = 0;
  153. if (pr == 2 || pr > 3) {
  154. if (!Cudd_zddPrintMinterm(zdd, f)) retval = 0;
  155. (void) fprintf(zdd->out,"\n");
  156. }
  157. (void) fflush(zdd->out);
  158. }
  159. return(retval);
  160. } /* end of Cudd_zddPrintDebug */
  161. /**
  162. @brief Finds the first path of a %ZDD.
  163. @details Defines an iterator on the paths of a %ZDD and finds its first
  164. path.<p>
  165. A path is represented as an array of literals, which are integers in
  166. {0, 1, 2}; 0 represents an else arc out of a node, 1 represents a then arc
  167. out of a node, and 2 stands for the absence of a node.
  168. The size of the array equals the number of variables in the manager at
  169. the time Cudd_zddFirstCube is called.<p>
  170. The paths that end in the empty terminal are not enumerated.
  171. @return a generator that contains the information necessary to
  172. continue the enumeration if successful; NULL otherwise.
  173. @sideeffect The first path is returned as a side effect.
  174. @see Cudd_zddForeachPath Cudd_zddNextPath Cudd_GenFree
  175. Cudd_IsGenEmpty
  176. */
  177. DdGen *
  178. Cudd_zddFirstPath(
  179. DdManager * zdd,
  180. DdNode * f,
  181. int ** path)
  182. {
  183. DdGen *gen;
  184. DdNode *top, *next, *prev;
  185. int i;
  186. int nvars;
  187. /* Sanity Check. */
  188. if (zdd == NULL || f == NULL) return(NULL);
  189. /* Allocate generator an initialize it. */
  190. gen = ALLOC(DdGen,1);
  191. if (gen == NULL) {
  192. zdd->errorCode = CUDD_MEMORY_OUT;
  193. return(NULL);
  194. }
  195. gen->manager = zdd;
  196. gen->type = CUDD_GEN_ZDD_PATHS;
  197. gen->status = CUDD_GEN_EMPTY;
  198. gen->gen.cubes.cube = NULL;
  199. gen->gen.cubes.value = DD_ZERO_VAL;
  200. gen->stack.sp = 0;
  201. gen->stack.stack = NULL;
  202. gen->node = NULL;
  203. nvars = zdd->sizeZ;
  204. gen->gen.cubes.cube = ALLOC(int,nvars);
  205. if (gen->gen.cubes.cube == NULL) {
  206. zdd->errorCode = CUDD_MEMORY_OUT;
  207. FREE(gen);
  208. return(NULL);
  209. }
  210. for (i = 0; i < nvars; i++) gen->gen.cubes.cube[i] = 2;
  211. /* The maximum stack depth is one plus the number of variables.
  212. ** because a path may have nodes at all levels, including the
  213. ** constant level.
  214. */
  215. gen->stack.stack = ALLOC(DdNodePtr, nvars+1);
  216. if (gen->stack.stack == NULL) {
  217. zdd->errorCode = CUDD_MEMORY_OUT;
  218. FREE(gen->gen.cubes.cube);
  219. FREE(gen);
  220. return(NULL);
  221. }
  222. for (i = 0; i <= nvars; i++) gen->stack.stack[i] = NULL;
  223. /* Find the first path of the ZDD. */
  224. gen->stack.stack[gen->stack.sp] = f; gen->stack.sp++;
  225. while (1) {
  226. top = gen->stack.stack[gen->stack.sp-1];
  227. if (!cuddIsConstant(Cudd_Regular(top))) {
  228. /* Take the else branch first. */
  229. gen->gen.cubes.cube[Cudd_Regular(top)->index] = 0;
  230. next = cuddE(Cudd_Regular(top));
  231. gen->stack.stack[gen->stack.sp] = Cudd_Not(next); gen->stack.sp++;
  232. } else if (Cudd_Regular(top) == DD_ZERO(zdd)) {
  233. /* Backtrack. */
  234. while (1) {
  235. if (gen->stack.sp == 1) {
  236. /* The current node has no predecessor. */
  237. gen->status = CUDD_GEN_EMPTY;
  238. gen->stack.sp--;
  239. goto done;
  240. }
  241. prev = Cudd_Regular(gen->stack.stack[gen->stack.sp-2]);
  242. next = cuddT(prev);
  243. if (next != top) { /* follow the then branch next */
  244. gen->gen.cubes.cube[prev->index] = 1;
  245. gen->stack.stack[gen->stack.sp-1] = next;
  246. break;
  247. }
  248. /* Pop the stack and try again. */
  249. gen->gen.cubes.cube[prev->index] = 2;
  250. gen->stack.sp--;
  251. top = gen->stack.stack[gen->stack.sp-1];
  252. }
  253. } else {
  254. gen->status = CUDD_GEN_NONEMPTY;
  255. gen->gen.cubes.value = cuddV(Cudd_Regular(top));
  256. goto done;
  257. }
  258. }
  259. done:
  260. *path = gen->gen.cubes.cube;
  261. return(gen);
  262. } /* end of Cudd_zddFirstPath */
  263. /**
  264. @brief Generates the next path of a %ZDD.
  265. @details Generates the next path of a %ZDD onset, using generator
  266. gen.
  267. @return 0 if the enumeration is completed; 1 otherwise.
  268. @sideeffect The path is returned as a side effect. The generator is
  269. modified.
  270. @see Cudd_zddForeachPath Cudd_zddFirstPath Cudd_GenFree
  271. Cudd_IsGenEmpty
  272. */
  273. int
  274. Cudd_zddNextPath(
  275. DdGen * gen,
  276. int ** path)
  277. {
  278. DdNode *top, *next, *prev;
  279. DdManager *zdd = gen->manager;
  280. /* Backtrack from previously reached terminal node. */
  281. while (1) {
  282. if (gen->stack.sp == 1) {
  283. /* The current node has no predecessor. */
  284. gen->status = CUDD_GEN_EMPTY;
  285. gen->stack.sp--;
  286. goto done;
  287. }
  288. top = gen->stack.stack[gen->stack.sp-1];
  289. prev = Cudd_Regular(gen->stack.stack[gen->stack.sp-2]);
  290. next = cuddT(prev);
  291. if (next != top) { /* follow the then branch next */
  292. gen->gen.cubes.cube[prev->index] = 1;
  293. gen->stack.stack[gen->stack.sp-1] = next;
  294. break;
  295. }
  296. /* Pop the stack and try again. */
  297. gen->gen.cubes.cube[prev->index] = 2;
  298. gen->stack.sp--;
  299. }
  300. while (1) {
  301. top = gen->stack.stack[gen->stack.sp-1];
  302. if (!cuddIsConstant(Cudd_Regular(top))) {
  303. /* Take the else branch first. */
  304. gen->gen.cubes.cube[Cudd_Regular(top)->index] = 0;
  305. next = cuddE(Cudd_Regular(top));
  306. gen->stack.stack[gen->stack.sp] = Cudd_Not(next); gen->stack.sp++;
  307. } else if (Cudd_Regular(top) == DD_ZERO(zdd)) {
  308. /* Backtrack. */
  309. while (1) {
  310. if (gen->stack.sp == 1) {
  311. /* The current node has no predecessor. */
  312. gen->status = CUDD_GEN_EMPTY;
  313. gen->stack.sp--;
  314. goto done;
  315. }
  316. prev = Cudd_Regular(gen->stack.stack[gen->stack.sp-2]);
  317. next = cuddT(prev);
  318. if (next != top) { /* follow the then branch next */
  319. gen->gen.cubes.cube[prev->index] = 1;
  320. gen->stack.stack[gen->stack.sp-1] = next;
  321. break;
  322. }
  323. /* Pop the stack and try again. */
  324. gen->gen.cubes.cube[prev->index] = 2;
  325. gen->stack.sp--;
  326. top = gen->stack.stack[gen->stack.sp-1];
  327. }
  328. } else {
  329. gen->status = CUDD_GEN_NONEMPTY;
  330. gen->gen.cubes.value = cuddV(Cudd_Regular(top));
  331. goto done;
  332. }
  333. }
  334. done:
  335. if (gen->status == CUDD_GEN_EMPTY) return(0);
  336. *path = gen->gen.cubes.cube;
  337. return(1);
  338. } /* end of Cudd_zddNextPath */
  339. /**
  340. @brief Converts a path of a %ZDD representing a cover to a string.
  341. @details The string represents an implicant of the cover. The path
  342. is typically produced by Cudd_zddForeachPath. If the str input is
  343. NULL, it allocates a new string. The string passed to this function
  344. must have enough room for all variables and for the terminator.
  345. @return a pointer to the string if successful; NULL otherwise.
  346. @sideeffect None
  347. @see Cudd_zddForeachPath
  348. */
  349. char *
  350. Cudd_zddCoverPathToString(
  351. DdManager *zdd /**< %DD manager */,
  352. int *path /**< path of %ZDD representing a cover */,
  353. char *str /**< pointer to string to use if != NULL */
  354. )
  355. {
  356. int nvars = zdd->sizeZ;
  357. int i;
  358. char *res;
  359. if (nvars & 1) return(NULL);
  360. nvars >>= 1;
  361. if (str == NULL) {
  362. res = ALLOC(char, nvars+1);
  363. if (res == NULL) return(NULL);
  364. } else {
  365. res = str;
  366. }
  367. for (i = 0; i < nvars; i++) {
  368. int v = (path[2*i] << 2) | path[2*i+1];
  369. switch (v) {
  370. case 0:
  371. case 2:
  372. case 8:
  373. case 10:
  374. res[i] = '-';
  375. break;
  376. case 1:
  377. case 9:
  378. res[i] = '0';
  379. break;
  380. case 4:
  381. case 6:
  382. res[i] = '1';
  383. break;
  384. default:
  385. res[i] = '?';
  386. }
  387. }
  388. res[nvars] = 0;
  389. return(res);
  390. } /* end of Cudd_zddCoverPathToString */
  391. /**
  392. @brief Finds the variables on which a %ZDD depends.
  393. @return a %BDD consisting of the product of the variables if
  394. successful; NULL otherwise.
  395. @sideeffect None
  396. @see Cudd_Support
  397. */
  398. DdNode *
  399. Cudd_zddSupport(
  400. DdManager * dd /**< manager */,
  401. DdNode * f /**< %ZDD whose support is sought */)
  402. {
  403. int *support;
  404. DdNode *res, *tmp, *var;
  405. int i,j;
  406. int size;
  407. /* Allocate and initialize support array for ddSupportStep. */
  408. size = ddMax(dd->size, dd->sizeZ);
  409. support = ALLOC(int,size);
  410. if (support == NULL) {
  411. dd->errorCode = CUDD_MEMORY_OUT;
  412. return(NULL);
  413. }
  414. for (i = 0; i < size; i++) {
  415. support[i] = 0;
  416. }
  417. /* Compute support and clean up markers. */
  418. zddSupportStep(Cudd_Regular(f),support);
  419. zddClearFlag(Cudd_Regular(f));
  420. /* Transform support from array to cube. */
  421. do {
  422. dd->reordered = 0;
  423. res = DD_ONE(dd);
  424. cuddRef(res);
  425. for (j = size - 1; j >= 0; j--) { /* for each level bottom-up */
  426. i = (j >= dd->size) ? j : dd->invperm[j];
  427. if (support[i] == 1) {
  428. /* The following call to cuddUniqueInter is guaranteed
  429. ** not to trigger reordering because the node we look up
  430. ** already exists. */
  431. var = cuddUniqueInter(dd,i,dd->one,Cudd_Not(dd->one));
  432. cuddRef(var);
  433. tmp = cuddBddAndRecur(dd,res,var);
  434. if (tmp == NULL) {
  435. Cudd_RecursiveDeref(dd,res);
  436. Cudd_RecursiveDeref(dd,var);
  437. res = NULL;
  438. break;
  439. }
  440. cuddRef(tmp);
  441. Cudd_RecursiveDeref(dd,res);
  442. Cudd_RecursiveDeref(dd,var);
  443. res = tmp;
  444. }
  445. }
  446. } while (dd->reordered == 1);
  447. FREE(support);
  448. if (res != NULL) cuddDeref(res);
  449. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  450. dd->timeoutHandler(dd, dd->tohArg);
  451. }
  452. return(res);
  453. } /* end of Cudd_zddSupport */
  454. /**
  455. @brief Writes a dot file representing the argument ZDDs.
  456. @details Writes a file representing the argument ZDDs in a format
  457. suitable for the graph drawing program dot.
  458. Cudd_zddDumpDot does not close the file: This is the caller
  459. responsibility. Cudd_zddDumpDot uses a minimal unique subset of the
  460. hexadecimal address of a node as name for it.
  461. If the argument inames is non-null, it is assumed to hold the pointers
  462. to the names of the inputs. Similarly for onames.
  463. Cudd_zddDumpDot uses the following convention to draw arcs:
  464. <ul>
  465. <li> solid line: THEN arcs;
  466. <li> dashed line: ELSE arcs.
  467. </ul>
  468. The dot options are chosen so that the drawing fits on a letter-size
  469. sheet.
  470. @return 1 in case of success; 0 otherwise (e.g., out-of-memory, file
  471. system full).
  472. @sideeffect None
  473. @see Cudd_DumpDot Cudd_zddPrintDebug
  474. */
  475. int
  476. Cudd_zddDumpDot(
  477. DdManager * dd /**< manager */,
  478. int n /**< number of output nodes to be dumped */,
  479. DdNode ** f /**< array of output nodes to be dumped */,
  480. char const * const * inames /**< array of input names (or NULL) */,
  481. char const * const * onames /**< array of output names (or NULL) */,
  482. FILE * fp /**< pointer to the dump file */)
  483. {
  484. DdNode *support = NULL;
  485. DdNode *scan;
  486. int *sorted = NULL;
  487. int nvars = dd->sizeZ;
  488. st_table *visited = NULL;
  489. st_generator *gen;
  490. int retval;
  491. int i, j;
  492. int slots;
  493. DdNodePtr *nodelist;
  494. ptruint refAddr, diff, mask = 0;
  495. /* Build a bit array with the support of f. */
  496. sorted = ALLOC(int,nvars);
  497. if (sorted == NULL) {
  498. dd->errorCode = CUDD_MEMORY_OUT;
  499. goto failure;
  500. }
  501. for (i = 0; i < nvars; i++) sorted[i] = 0;
  502. /* Take the union of the supports of each output function. */
  503. for (i = 0; i < n; i++) {
  504. support = Cudd_zddSupport(dd,f[i]);
  505. if (support == NULL) goto failure;
  506. cuddRef(support);
  507. scan = support;
  508. while (!cuddIsConstant(scan)) {
  509. sorted[scan->index] = 1;
  510. scan = cuddT(scan);
  511. }
  512. Cudd_RecursiveDeref(dd,support);
  513. }
  514. support = NULL; /* so that we do not try to free it in case of failure */
  515. /* Initialize symbol table for visited nodes. */
  516. visited = st_init_table(st_ptrcmp, st_ptrhash);
  517. if (visited == NULL) goto failure;
  518. /* Collect all the nodes of this DD in the symbol table. */
  519. for (i = 0; i < n; i++) {
  520. retval = cuddCollectNodes(f[i],visited);
  521. if (retval == 0) goto failure;
  522. }
  523. /* Find how many most significant hex digits are identical
  524. ** in the addresses of all the nodes. Build a mask based
  525. ** on this knowledge, so that digits that carry no information
  526. ** will not be printed. This is done in two steps.
  527. ** 1. We scan the symbol table to find the bits that differ
  528. ** in at least 2 addresses.
  529. ** 2. We choose one of the possible masks. There are 8 possible
  530. ** masks for 32-bit integer, and 16 possible masks for 64-bit
  531. ** integers.
  532. */
  533. /* Find the bits that are different. */
  534. refAddr = (ptruint) f[0];
  535. diff = 0;
  536. gen = st_init_gen(visited);
  537. while (st_gen(gen, (void **) &scan, NULL)) {
  538. diff |= refAddr ^ (ptruint) scan;
  539. }
  540. st_free_gen(gen);
  541. /* Choose the mask. */
  542. for (i = 0; (unsigned) i < 8 * sizeof(ptruint); i += 4) {
  543. mask = ((ptruint) 1 << i) - 1;
  544. if (diff <= mask) break;
  545. }
  546. /* Write the header and the global attributes. */
  547. retval = fprintf(fp,"digraph \"ZDD\" {\n");
  548. if (retval == EOF) return(0);
  549. retval = fprintf(fp,
  550. "size = \"7.5,10\"\ncenter = true;\nedge [dir = none];\n");
  551. if (retval == EOF) return(0);
  552. /* Write the input name subgraph by scanning the support array. */
  553. retval = fprintf(fp,"{ node [shape = plaintext];\n");
  554. if (retval == EOF) goto failure;
  555. retval = fprintf(fp," edge [style = invis];\n");
  556. if (retval == EOF) goto failure;
  557. /* We use a name ("CONST NODES") with an embedded blank, because
  558. ** it is unlikely to appear as an input name.
  559. */
  560. retval = fprintf(fp," \"CONST NODES\" [style = invis];\n");
  561. if (retval == EOF) goto failure;
  562. for (i = 0; i < nvars; i++) {
  563. if (sorted[dd->invpermZ[i]]) {
  564. if (inames == NULL) {
  565. retval = fprintf(fp,"\" %d \" -> ", dd->invpermZ[i]);
  566. } else {
  567. retval = fprintf(fp,"\" %s \" -> ", inames[dd->invpermZ[i]]);
  568. }
  569. if (retval == EOF) goto failure;
  570. }
  571. }
  572. retval = fprintf(fp,"\"CONST NODES\"; \n}\n");
  573. if (retval == EOF) goto failure;
  574. /* Write the output node subgraph. */
  575. retval = fprintf(fp,"{ rank = same; node [shape = box]; edge [style = invis];\n");
  576. if (retval == EOF) goto failure;
  577. for (i = 0; i < n; i++) {
  578. if (onames == NULL) {
  579. retval = fprintf(fp,"\"F%d\"", i);
  580. } else {
  581. retval = fprintf(fp,"\" %s \"", onames[i]);
  582. }
  583. if (retval == EOF) goto failure;
  584. if (i == n - 1) {
  585. retval = fprintf(fp,"; }\n");
  586. } else {
  587. retval = fprintf(fp," -> ");
  588. }
  589. if (retval == EOF) goto failure;
  590. }
  591. /* Write rank info: All nodes with the same index have the same rank. */
  592. for (i = 0; i < nvars; i++) {
  593. if (sorted[dd->invpermZ[i]]) {
  594. retval = fprintf(fp,"{ rank = same; ");
  595. if (retval == EOF) goto failure;
  596. if (inames == NULL) {
  597. retval = fprintf(fp,"\" %d \";\n", dd->invpermZ[i]);
  598. } else {
  599. retval = fprintf(fp,"\" %s \";\n", inames[dd->invpermZ[i]]);
  600. }
  601. if (retval == EOF) goto failure;
  602. nodelist = dd->subtableZ[i].nodelist;
  603. slots = dd->subtableZ[i].slots;
  604. for (j = 0; j < slots; j++) {
  605. scan = nodelist[j];
  606. while (scan != NULL) {
  607. if (st_is_member(visited,scan)) {
  608. retval = fprintf(fp,"\"%#" PRIxPTR "\";\n",
  609. ((mask & (ptruint) scan) /
  610. sizeof(DdNode)));
  611. if (retval == EOF) goto failure;
  612. }
  613. scan = scan->next;
  614. }
  615. }
  616. retval = fprintf(fp,"}\n");
  617. if (retval == EOF) goto failure;
  618. }
  619. }
  620. /* All constants have the same rank. */
  621. retval = fprintf(fp,
  622. "{ rank = same; \"CONST NODES\";\n{ node [shape = box]; ");
  623. if (retval == EOF) goto failure;
  624. nodelist = dd->constants.nodelist;
  625. slots = dd->constants.slots;
  626. for (j = 0; j < slots; j++) {
  627. scan = nodelist[j];
  628. while (scan != NULL) {
  629. if (st_is_member(visited,scan)) {
  630. retval = fprintf(fp,"\"%#" PRIxPTR "\";\n",
  631. ((mask & (ptruint) scan) / sizeof(DdNode)));
  632. if (retval == EOF) goto failure;
  633. }
  634. scan = scan->next;
  635. }
  636. }
  637. retval = fprintf(fp,"}\n}\n");
  638. if (retval == EOF) goto failure;
  639. /* Write edge info. */
  640. /* Edges from the output nodes. */
  641. for (i = 0; i < n; i++) {
  642. if (onames == NULL) {
  643. retval = fprintf(fp,"\"F%d\"", i);
  644. } else {
  645. retval = fprintf(fp,"\" %s \"", onames[i]);
  646. }
  647. if (retval == EOF) goto failure;
  648. retval = fprintf(fp," -> \"%#" PRIxPTR "\" [style = solid];\n",
  649. ((mask & (ptruint) f[i]) / sizeof(DdNode)));
  650. if (retval == EOF) goto failure;
  651. }
  652. /* Edges from internal nodes. */
  653. for (i = 0; i < nvars; i++) {
  654. if (sorted[dd->invpermZ[i]]) {
  655. nodelist = dd->subtableZ[i].nodelist;
  656. slots = dd->subtableZ[i].slots;
  657. for (j = 0; j < slots; j++) {
  658. scan = nodelist[j];
  659. while (scan != NULL) {
  660. if (st_is_member(visited,scan)) {
  661. retval = fprintf(fp,
  662. "\"%#" PRIxPTR "\" -> \"%#" PRIxPTR "\";\n",
  663. ((mask & (ptruint) scan) / sizeof(DdNode)),
  664. ((mask & (ptruint) cuddT(scan)) / sizeof(DdNode)));
  665. if (retval == EOF) goto failure;
  666. retval = fprintf(fp,
  667. "\"%#" PRIxPTR "\" -> \"%#" PRIxPTR
  668. "\" [style = dashed];\n",
  669. ((mask & (ptruint) scan) /
  670. sizeof(DdNode)),
  671. ((mask & (ptruint) cuddE(scan)) /
  672. sizeof(DdNode)));
  673. if (retval == EOF) goto failure;
  674. }
  675. scan = scan->next;
  676. }
  677. }
  678. }
  679. }
  680. /* Write constant labels. */
  681. nodelist = dd->constants.nodelist;
  682. slots = dd->constants.slots;
  683. for (j = 0; j < slots; j++) {
  684. scan = nodelist[j];
  685. while (scan != NULL) {
  686. if (st_is_member(visited,scan)) {
  687. retval = fprintf(fp,"\"%#" PRIxPTR "\" [label = \"%g\"];\n",
  688. ((mask & (ptruint) scan) / sizeof(DdNode)),
  689. cuddV(scan));
  690. if (retval == EOF) goto failure;
  691. }
  692. scan = scan->next;
  693. }
  694. }
  695. /* Write trailer and return. */
  696. retval = fprintf(fp,"}\n");
  697. if (retval == EOF) goto failure;
  698. st_free_table(visited);
  699. FREE(sorted);
  700. return(1);
  701. failure:
  702. if (sorted != NULL) FREE(sorted);
  703. if (visited != NULL) st_free_table(visited);
  704. return(0);
  705. } /* end of Cudd_zddDumpBlif */
  706. /*---------------------------------------------------------------------------*/
  707. /* Definition of internal functions */
  708. /*---------------------------------------------------------------------------*/
  709. /**
  710. @brief Prints a %ZDD to the standard output. One line per node is
  711. printed.
  712. @return 1 if successful; 0 otherwise.
  713. @sideeffect None
  714. @see Cudd_zddPrintDebug
  715. */
  716. int
  717. cuddZddP(
  718. DdManager * zdd,
  719. DdNode * f)
  720. {
  721. int retval;
  722. st_table *table = st_init_table(st_ptrcmp, st_ptrhash);
  723. if (table == NULL) return(0);
  724. retval = zp2(zdd, f, table);
  725. st_free_table(table);
  726. (void) fputc('\n', zdd->out);
  727. return(retval);
  728. } /* end of cuddZddP */
  729. /*---------------------------------------------------------------------------*/
  730. /* Definition of static functions */
  731. /*---------------------------------------------------------------------------*/
  732. /**
  733. @brief Performs the recursive step of cuddZddP.
  734. @return 1 in case of success; 0 otherwise.
  735. @sideeffect None
  736. */
  737. static int
  738. zp2(
  739. DdManager * zdd,
  740. DdNode * f,
  741. st_table * t)
  742. {
  743. DdNode *n;
  744. int T, E;
  745. DdNode *base = DD_ONE(zdd);
  746. if (f == NULL)
  747. return(0);
  748. if (Cudd_IsConstantInt(f)) {
  749. (void)fprintf(zdd->out, "ID = %d\n", (f == base));
  750. return(1);
  751. }
  752. if (st_is_member(t, f) == 1)
  753. return(1);
  754. if (st_insert(t, f, NULL) == ST_OUT_OF_MEM)
  755. return(0);
  756. (void) fprintf(zdd->out, "ID = 0x%" PRIxPTR "\tindex = %u\tr = %u\t",
  757. (ptruint)f / (ptruint) sizeof(DdNode), f->index, f->ref);
  758. n = cuddT(f);
  759. if (Cudd_IsConstantInt(n)) {
  760. (void) fprintf(zdd->out, "T = %d\t\t", (n == base));
  761. T = 1;
  762. } else {
  763. (void) fprintf(zdd->out, "T = 0x%" PRIxPTR "\t", (ptruint) n /
  764. (ptruint) sizeof(DdNode));
  765. T = 0;
  766. }
  767. n = cuddE(f);
  768. if (Cudd_IsConstantInt(n)) {
  769. (void) fprintf(zdd->out, "E = %d\n", (n == base));
  770. E = 1;
  771. } else {
  772. (void) fprintf(zdd->out, "E = 0x%" PRIxPTR "\n", (ptruint) n /
  773. (ptruint) sizeof(DdNode));
  774. E = 0;
  775. }
  776. if (E == 0)
  777. if (zp2(zdd, cuddE(f), t) == 0) return(0);
  778. if (T == 0)
  779. if (zp2(zdd, cuddT(f), t) == 0) return(0);
  780. return(1);
  781. } /* end of zp2 */
  782. /**
  783. @brief Performs the recursive step of Cudd_zddPrintMinterm.
  784. @sideeffect None
  785. */
  786. static void
  787. zdd_print_minterm_aux(
  788. DdManager * zdd /* manager */,
  789. DdNode * node /* current node */,
  790. int level /* depth in the recursion */,
  791. int * list /* current recursion path */)
  792. {
  793. DdNode *Nv, *Nnv;
  794. int i, v;
  795. DdNode *base = DD_ONE(zdd);
  796. if (Cudd_IsConstantInt(node)) {
  797. if (node == base) {
  798. /* Check for missing variable. */
  799. if (level != zdd->sizeZ) {
  800. list[zdd->invpermZ[level]] = 0;
  801. zdd_print_minterm_aux(zdd, node, level + 1, list);
  802. return;
  803. }
  804. /* Terminal case: Print one cube based on the current recursion
  805. ** path.
  806. */
  807. for (i = 0; i < zdd->sizeZ; i++) {
  808. v = list[i];
  809. if (v == 0)
  810. (void) fprintf(zdd->out,"0");
  811. else if (v == 1)
  812. (void) fprintf(zdd->out,"1");
  813. else if (v == 3)
  814. (void) fprintf(zdd->out,"@"); /* should never happen */
  815. else
  816. (void) fprintf(zdd->out,"-");
  817. }
  818. (void) fprintf(zdd->out," 1\n");
  819. }
  820. } else {
  821. /* Check for missing variable. */
  822. if (level != cuddIZ(zdd,node->index)) {
  823. list[zdd->invpermZ[level]] = 0;
  824. zdd_print_minterm_aux(zdd, node, level + 1, list);
  825. return;
  826. }
  827. Nnv = cuddE(node);
  828. Nv = cuddT(node);
  829. if (Nv == Nnv) {
  830. list[node->index] = 2;
  831. zdd_print_minterm_aux(zdd, Nnv, level + 1, list);
  832. return;
  833. }
  834. list[node->index] = 1;
  835. zdd_print_minterm_aux(zdd, Nv, level + 1, list);
  836. list[node->index] = 0;
  837. zdd_print_minterm_aux(zdd, Nnv, level + 1, list);
  838. }
  839. return;
  840. } /* end of zdd_print_minterm_aux */
  841. /**
  842. @brief Performs the recursive step of Cudd_zddPrintCover.
  843. @sideeffect None
  844. */
  845. static void
  846. zddPrintCoverAux(
  847. DdManager * zdd /* manager */,
  848. DdNode * node /* current node */,
  849. int level /* depth in the recursion */,
  850. int * list /* current recursion path */)
  851. {
  852. DdNode *Nv, *Nnv;
  853. int i, v;
  854. DdNode *base = DD_ONE(zdd);
  855. if (Cudd_IsConstantInt(node)) {
  856. if (node == base) {
  857. /* Check for missing variable. */
  858. if (level != zdd->sizeZ) {
  859. list[zdd->invpermZ[level]] = 0;
  860. zddPrintCoverAux(zdd, node, level + 1, list);
  861. return;
  862. }
  863. /* Terminal case: Print one cube based on the current recursion
  864. ** path.
  865. */
  866. for (i = 0; i < zdd->sizeZ; i += 2) {
  867. v = list[i] * 4 + list[i+1];
  868. if (v == 0)
  869. (void) putc('-',zdd->out);
  870. else if (v == 4)
  871. (void) putc('1',zdd->out);
  872. else if (v == 1)
  873. (void) putc('0',zdd->out);
  874. else
  875. (void) putc('@',zdd->out); /* should never happen */
  876. }
  877. (void) fprintf(zdd->out," 1\n");
  878. }
  879. } else {
  880. /* Check for missing variable. */
  881. if (level != cuddIZ(zdd,node->index)) {
  882. list[zdd->invpermZ[level]] = 0;
  883. zddPrintCoverAux(zdd, node, level + 1, list);
  884. return;
  885. }
  886. Nnv = cuddE(node);
  887. Nv = cuddT(node);
  888. if (Nv == Nnv) {
  889. list[node->index] = 2;
  890. zddPrintCoverAux(zdd, Nnv, level + 1, list);
  891. return;
  892. }
  893. list[node->index] = 1;
  894. zddPrintCoverAux(zdd, Nv, level + 1, list);
  895. list[node->index] = 0;
  896. zddPrintCoverAux(zdd, Nnv, level + 1, list);
  897. }
  898. return;
  899. } /* end of zddPrintCoverAux */
  900. /**
  901. @brief Performs the recursive step of Cudd_zddSupport.
  902. @details Performs a DFS from f. The support is accumulated in supp
  903. as a side effect. Uses the LSB of the then pointer as visited flag.
  904. @sideeffect None
  905. @see zddClearFlag
  906. */
  907. static void
  908. zddSupportStep(
  909. DdNode * f,
  910. int * support)
  911. {
  912. if (cuddIsConstant(f) || Cudd_IsComplement(f->next)) {
  913. return;
  914. }
  915. support[f->index] = 1;
  916. zddSupportStep(cuddT(f),support);
  917. zddSupportStep(Cudd_Regular(cuddE(f)),support);
  918. /* Mark as visited. */
  919. f->next = Cudd_Not(f->next);
  920. return;
  921. } /* end of zddSupportStep */
  922. /**
  923. @brief Performs a DFS from f, clearing the LSB of the next
  924. pointers.
  925. @sideeffect None
  926. @see zddSupportStep
  927. */
  928. static void
  929. zddClearFlag(
  930. DdNode * f)
  931. {
  932. if (!Cudd_IsComplement(f->next)) {
  933. return;
  934. }
  935. /* Clear visited flag. */
  936. f->next = Cudd_Regular(f->next);
  937. if (cuddIsConstant(f)) {
  938. return;
  939. }
  940. zddClearFlag(cuddT(f));
  941. zddClearFlag(Cudd_Regular(cuddE(f)));
  942. return;
  943. } /* end of zddClearFlag */