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.

455 lines
12 KiB

  1. /**CFile**********************************************************************
  2. FileName [dddmpDdNodeBdd.c]
  3. PackageName [dddmp]
  4. Synopsis [Functions to handle BDD node infos and numbering]
  5. Description [Functions to handle BDD node infos and numbering.
  6. ]
  7. Author [Gianpiero Cabodi and Stefano Quer]
  8. Copyright [
  9. Copyright (c) 2004 by Politecnico di Torino.
  10. All Rights Reserved. This software is for educational purposes only.
  11. Permission is given to academic institutions to use, copy, and modify
  12. this software and its documentation provided that this introductory
  13. message is not removed, that this software and its documentation is
  14. used for the institutions' internal research and educational purposes,
  15. and that no monies are exchanged. No guarantee is expressed or implied
  16. by the distribution of this code.
  17. Send bug-reports and/or questions to:
  18. {gianpiero.cabodi,stefano.quer}@polito.it.
  19. ]
  20. ******************************************************************************/
  21. #include "dddmpInt.h"
  22. /*---------------------------------------------------------------------------*/
  23. /* Stucture declarations */
  24. /*---------------------------------------------------------------------------*/
  25. /*---------------------------------------------------------------------------*/
  26. /* Type declarations */
  27. /*---------------------------------------------------------------------------*/
  28. /*---------------------------------------------------------------------------*/
  29. /* Variable declarations */
  30. /*---------------------------------------------------------------------------*/
  31. /*---------------------------------------------------------------------------*/
  32. /* Macro declarations */
  33. /*---------------------------------------------------------------------------*/
  34. /**AutomaticStart*************************************************************/
  35. /*---------------------------------------------------------------------------*/
  36. /* Static function prototypes */
  37. /*---------------------------------------------------------------------------*/
  38. static int NumberNodeRecur(DdNode *f, int id);
  39. static void RemoveFromUniqueRecur(DdManager *ddMgr, DdNode *f);
  40. static void RestoreInUniqueRecur(DdManager *ddMgr, DdNode *f);
  41. /**AutomaticEnd***************************************************************/
  42. /*---------------------------------------------------------------------------*/
  43. /* Definition of exported functions */
  44. /*---------------------------------------------------------------------------*/
  45. /*---------------------------------------------------------------------------*/
  46. /* Definition of internal functions */
  47. /*---------------------------------------------------------------------------*/
  48. /**Function********************************************************************
  49. Synopsis [Removes nodes from unique table and number them]
  50. Description [Node numbering is required to convert pointers to integers.
  51. Since nodes are removed from unique table, no new nodes should
  52. be generated before re-inserting nodes in the unique table
  53. (DddmpUnnumberDdNodes()).
  54. ]
  55. SideEffects [Nodes are temporarily removed from unique table]
  56. SeeAlso [RemoveFromUniqueRecur(), NumberNodeRecur(),
  57. DddmpUnnumberDdNodes()]
  58. ******************************************************************************/
  59. int
  60. DddmpNumberDdNodes (
  61. DdManager *ddMgr /* IN: DD Manager */,
  62. DdNode **f /* IN: array of BDDs */,
  63. int n /* IN: number of BDD roots in the array of BDDs */
  64. )
  65. {
  66. int id=0, i;
  67. for (i=0; i<n; i++) {
  68. RemoveFromUniqueRecur (ddMgr, f[i]);
  69. }
  70. for (i=0; i<n; i++) {
  71. id = NumberNodeRecur (f[i], id);
  72. }
  73. return (id);
  74. }
  75. /**Function********************************************************************
  76. Synopsis [Restores nodes in unique table, loosing numbering]
  77. Description [Node indexes are no more needed. Nodes are re-linked in the
  78. unique table.
  79. ]
  80. SideEffects [None]
  81. SeeAlso [DddmpNumberDdNode()]
  82. ******************************************************************************/
  83. void
  84. DddmpUnnumberDdNodes(
  85. DdManager *ddMgr /* IN: DD Manager */,
  86. DdNode **f /* IN: array of BDDs */,
  87. int n /* IN: number of BDD roots in the array of BDDs */
  88. )
  89. {
  90. int i;
  91. for (i=0; i<n; i++) {
  92. RestoreInUniqueRecur (ddMgr, f[i]);
  93. }
  94. return;
  95. }
  96. /**Function********************************************************************
  97. Synopsis [Write index to node]
  98. Description [The index of the node is written in the "next" field of
  99. a DdNode struct. LSB is not used (set to 0). It is used as
  100. "visited" flag in DD traversals.
  101. ]
  102. SideEffects [None]
  103. SeeAlso [DddmpReadNodeIndex(), DddmpSetVisited (), DddmpVisited ()]
  104. ******************************************************************************/
  105. void
  106. DddmpWriteNodeIndex (
  107. DdNode *f /* IN: BDD node */,
  108. int id /* IN: index to be written */
  109. )
  110. {
  111. #if 0
  112. if (1 || !Cudd_IsConstant (f)) {
  113. #else
  114. if (!Cudd_IsConstant (f)) {
  115. #endif
  116. f->next = (struct DdNode *)((ptruint)((id)<<1));
  117. }
  118. return;
  119. }
  120. /**Function********************************************************************
  121. Synopsis [Reads the index of a node]
  122. Description [Reads the index of a node. LSB is skipped (used as visited
  123. flag).
  124. ]
  125. SideEffects [None]
  126. SeeAlso [DddmpWriteNodeIndex(), DddmpSetVisited (), DddmpVisited ()]
  127. ******************************************************************************/
  128. int
  129. DddmpReadNodeIndex (
  130. DdNode *f /* IN: BDD node */
  131. )
  132. {
  133. #if 0
  134. if (1 || !Cudd_IsConstant (f)) {
  135. #else
  136. if (!Cudd_IsConstant (f)) {
  137. #endif
  138. return ((int)(((ptruint)(f->next))>>1));
  139. } else {
  140. return (1);
  141. }
  142. }
  143. /**Function********************************************************************
  144. Synopsis [Returns true if node is visited]
  145. Description [Returns true if node is visited]
  146. SideEffects [None]
  147. SeeAlso [DddmpSetVisited (), DddmpClearVisited ()]
  148. ******************************************************************************/
  149. int
  150. DddmpVisited (
  151. DdNode *f /* IN: BDD node to be tested */
  152. )
  153. {
  154. f = Cudd_Regular(f);
  155. return ((int)((ptruint)(f->next)) & (01));
  156. }
  157. /**Function********************************************************************
  158. Synopsis [Marks a node as visited]
  159. Description [Marks a node as visited]
  160. SideEffects [None]
  161. SeeAlso [DddmpVisited (), DddmpClearVisited ()]
  162. ******************************************************************************/
  163. void
  164. DddmpSetVisited (
  165. DdNode *f /* IN: BDD node to be marked (as visited) */
  166. )
  167. {
  168. f = Cudd_Regular(f);
  169. f->next = (DdNode *)(ptruint)((int)((ptruint)(f->next))|01);
  170. return;
  171. }
  172. /**Function********************************************************************
  173. Synopsis [Marks a node as not visited]
  174. Description [Marks a node as not visited]
  175. SideEffects [None]
  176. SeeAlso [DddmpVisited (), DddmpSetVisited ()]
  177. ******************************************************************************/
  178. void
  179. DddmpClearVisited (
  180. DdNode *f /* IN: BDD node to be marked (as not visited) */
  181. )
  182. {
  183. f = Cudd_Regular (f);
  184. f->next = (DdNode *)(ptruint)((int)((ptruint)(f->next)) & (~01));
  185. return;
  186. }
  187. /*---------------------------------------------------------------------------*/
  188. /* Definition of static functions */
  189. /*---------------------------------------------------------------------------*/
  190. /**Function********************************************************************
  191. Synopsis [Number nodes recursively in post-order]
  192. Description [Number nodes recursively in post-order.
  193. The "visited" flag is used with inverse polarity, because all nodes
  194. were set "visited" when removing them from unique.
  195. ]
  196. SideEffects ["visited" flags are reset.]
  197. SeeAlso []
  198. ******************************************************************************/
  199. static int
  200. NumberNodeRecur(
  201. DdNode *f /* IN: root of the BDD to be numbered */,
  202. int id /* IN/OUT: index to be assigned to the node */
  203. )
  204. {
  205. f = Cudd_Regular(f);
  206. if (!DddmpVisited (f)) {
  207. return (id);
  208. }
  209. if (!cuddIsConstant (f)) {
  210. id = NumberNodeRecur (cuddT (f), id);
  211. id = NumberNodeRecur (cuddE (f), id);
  212. }
  213. DddmpWriteNodeIndex (f, ++id);
  214. DddmpClearVisited (f);
  215. return (id);
  216. }
  217. /**Function********************************************************************
  218. Synopsis [Removes a node from unique table]
  219. Description [Removes a node from the unique table by locating the proper
  220. subtable and unlinking the node from it. It recurs on the
  221. children of the node.
  222. ]
  223. SideEffects [Nodes are left with the "visited" flag true.]
  224. SeeAlso [RestoreInUniqueRecur()]
  225. ******************************************************************************/
  226. static void
  227. RemoveFromUniqueRecur (
  228. DdManager *ddMgr /* IN: DD Manager */,
  229. DdNode *f /* IN: root of the BDD to be extracted */
  230. )
  231. {
  232. DdNode *node, *last, *next;
  233. DdNode *sentinel = &(ddMgr->sentinel);
  234. DdNodePtr *nodelist;
  235. DdSubtable *subtable;
  236. int pos, level;
  237. f = Cudd_Regular (f);
  238. if (DddmpVisited (f)) {
  239. return;
  240. }
  241. if (!cuddIsConstant (f)) {
  242. RemoveFromUniqueRecur (ddMgr, cuddT (f));
  243. RemoveFromUniqueRecur (ddMgr, cuddE (f));
  244. level = ddMgr->perm[f->index];
  245. subtable = &(ddMgr->subtables[level]);
  246. nodelist = subtable->nodelist;
  247. pos = ddHash (cuddT (f), cuddE (f), subtable->shift);
  248. node = nodelist[pos];
  249. last = NULL;
  250. while (node != sentinel) {
  251. next = node->next;
  252. if (node == f) {
  253. if (last != NULL)
  254. last->next = next;
  255. else
  256. nodelist[pos] = next;
  257. break;
  258. } else {
  259. last = node;
  260. node = next;
  261. }
  262. }
  263. f->next = NULL;
  264. }
  265. DddmpSetVisited (f);
  266. return;
  267. }
  268. /**Function********************************************************************
  269. Synopsis [Restores a node in unique table]
  270. Description [Restores a node in unique table (recursively)]
  271. SideEffects [Nodes are not restored in the same order as before removal]
  272. SeeAlso [RemoveFromUnique()]
  273. ******************************************************************************/
  274. static void
  275. RestoreInUniqueRecur (
  276. DdManager *ddMgr /* IN: DD Manager */,
  277. DdNode *f /* IN: root of the BDD to be restored */
  278. )
  279. {
  280. DdNodePtr *nodelist;
  281. DdNode *T, *E, *looking;
  282. DdNodePtr *previousP;
  283. DdSubtable *subtable;
  284. int pos, level;
  285. #ifdef DDDMP_DEBUG
  286. DdNode *node;
  287. DdNode *sentinel = &(ddMgr->sentinel);
  288. #endif
  289. f = Cudd_Regular(f);
  290. if (!Cudd_IsComplement (f->next)) {
  291. return;
  292. }
  293. if (cuddIsConstant (f)) {
  294. DddmpClearVisited (f);
  295. /*f->next = NULL;*/
  296. return;
  297. }
  298. RestoreInUniqueRecur (ddMgr, cuddT (f));
  299. RestoreInUniqueRecur (ddMgr, cuddE (f));
  300. level = ddMgr->perm[f->index];
  301. subtable = &(ddMgr->subtables[level]);
  302. nodelist = subtable->nodelist;
  303. pos = ddHash (cuddT (f), cuddE (f), subtable->shift);
  304. #ifdef DDDMP_DEBUG
  305. /* verify uniqueness to avoid duplicate nodes in unique table */
  306. for (node=nodelist[pos]; node != sentinel; node=node->next)
  307. assert(node!=f);
  308. #endif
  309. T = cuddT (f);
  310. E = cuddE (f);
  311. previousP = &(nodelist[pos]);
  312. looking = *previousP;
  313. while (T < cuddT (looking)) {
  314. previousP = &(looking->next);
  315. looking = *previousP;
  316. }
  317. while (T == cuddT (looking) && E < cuddE (looking)) {
  318. previousP = &(looking->next);
  319. looking = *previousP;
  320. }
  321. f->next = *previousP;
  322. *previousP = f;
  323. return;
  324. }