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.

1205 lines
34 KiB

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