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.

558 lines
16 KiB

  1. /**CFile***********************************************************************
  2. FileName [cuddClip.c]
  3. PackageName [cudd]
  4. Synopsis [Clipping functions.]
  5. Description [External procedures included in this module:
  6. <ul>
  7. <li> Cudd_bddClippingAnd()
  8. <li> Cudd_bddClippingAndAbstract()
  9. </ul>
  10. Internal procedures included in this module:
  11. <ul>
  12. <li> cuddBddClippingAnd()
  13. <li> cuddBddClippingAndAbstract()
  14. </ul>
  15. Static procedures included in this module:
  16. <ul>
  17. <li> cuddBddClippingAndRecur()
  18. <li> cuddBddClipAndAbsRecur()
  19. </ul>
  20. SeeAlso []
  21. Author [Fabio Somenzi]
  22. Copyright [Copyright (c) 1995-2012, Regents of the University of Colorado
  23. All rights reserved.
  24. Redistribution and use in source and binary forms, with or without
  25. modification, are permitted provided that the following conditions
  26. are met:
  27. Redistributions of source code must retain the above copyright
  28. notice, this list of conditions and the following disclaimer.
  29. Redistributions in binary form must reproduce the above copyright
  30. notice, this list of conditions and the following disclaimer in the
  31. documentation and/or other materials provided with the distribution.
  32. Neither the name of the University of Colorado nor the names of its
  33. contributors may be used to endorse or promote products derived from
  34. this software without specific prior written permission.
  35. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  36. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  37. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  38. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  39. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  40. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  41. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  43. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  44. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  45. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  46. POSSIBILITY OF SUCH DAMAGE.]
  47. ******************************************************************************/
  48. #include "util.h"
  49. #include "cuddInt.h"
  50. /*---------------------------------------------------------------------------*/
  51. /* Constant declarations */
  52. /*---------------------------------------------------------------------------*/
  53. /*---------------------------------------------------------------------------*/
  54. /* Stucture declarations */
  55. /*---------------------------------------------------------------------------*/
  56. /*---------------------------------------------------------------------------*/
  57. /* Type declarations */
  58. /*---------------------------------------------------------------------------*/
  59. /*---------------------------------------------------------------------------*/
  60. /* Variable declarations */
  61. /*---------------------------------------------------------------------------*/
  62. #ifndef lint
  63. static char rcsid[] DD_UNUSED = "$Id: cuddClip.c,v 1.9 2012/02/05 01:07:18 fabio Exp $";
  64. #endif
  65. /*---------------------------------------------------------------------------*/
  66. /* Macro declarations */
  67. /*---------------------------------------------------------------------------*/
  68. /**AutomaticStart*************************************************************/
  69. /*---------------------------------------------------------------------------*/
  70. /* Static function prototypes */
  71. /*---------------------------------------------------------------------------*/
  72. static DdNode * cuddBddClippingAndRecur (DdManager *manager, DdNode *f, DdNode *g, int distance, int direction);
  73. static DdNode * cuddBddClipAndAbsRecur (DdManager *manager, DdNode *f, DdNode *g, DdNode *cube, int distance, int direction);
  74. /**AutomaticEnd***************************************************************/
  75. /*---------------------------------------------------------------------------*/
  76. /* Definition of exported functions */
  77. /*---------------------------------------------------------------------------*/
  78. /**Function********************************************************************
  79. Synopsis [Approximates the conjunction of two BDDs f and g.]
  80. Description [Approximates the conjunction of two BDDs f and g. Returns a
  81. pointer to the resulting BDD if successful; NULL if the intermediate
  82. result blows up.]
  83. SideEffects [None]
  84. SeeAlso [Cudd_bddAnd]
  85. ******************************************************************************/
  86. DdNode *
  87. Cudd_bddClippingAnd(
  88. DdManager * dd /* manager */,
  89. DdNode * f /* first conjunct */,
  90. DdNode * g /* second conjunct */,
  91. int maxDepth /* maximum recursion depth */,
  92. int direction /* under (0) or over (1) approximation */)
  93. {
  94. DdNode *res;
  95. do {
  96. dd->reordered = 0;
  97. res = cuddBddClippingAnd(dd,f,g,maxDepth,direction);
  98. } while (dd->reordered == 1);
  99. return(res);
  100. } /* end of Cudd_bddClippingAnd */
  101. /**Function********************************************************************
  102. Synopsis [Approximates the conjunction of two BDDs f and g and
  103. simultaneously abstracts the variables in cube.]
  104. Description [Approximates the conjunction of two BDDs f and g and
  105. simultaneously abstracts the variables in cube. The variables are
  106. existentially abstracted. Returns a pointer to the resulting BDD if
  107. successful; NULL if the intermediate result blows up.]
  108. SideEffects [None]
  109. SeeAlso [Cudd_bddAndAbstract Cudd_bddClippingAnd]
  110. ******************************************************************************/
  111. DdNode *
  112. Cudd_bddClippingAndAbstract(
  113. DdManager * dd /* manager */,
  114. DdNode * f /* first conjunct */,
  115. DdNode * g /* second conjunct */,
  116. DdNode * cube /* cube of variables to be abstracted */,
  117. int maxDepth /* maximum recursion depth */,
  118. int direction /* under (0) or over (1) approximation */)
  119. {
  120. DdNode *res;
  121. do {
  122. dd->reordered = 0;
  123. res = cuddBddClippingAndAbstract(dd,f,g,cube,maxDepth,direction);
  124. } while (dd->reordered == 1);
  125. return(res);
  126. } /* end of Cudd_bddClippingAndAbstract */
  127. /*---------------------------------------------------------------------------*/
  128. /* Definition of internal functions */
  129. /*---------------------------------------------------------------------------*/
  130. /**Function********************************************************************
  131. Synopsis [Approximates the conjunction of two BDDs f and g.]
  132. Description [Approximates the conjunction of two BDDs f and g. Returns a
  133. pointer to the resulting BDD if successful; NULL if the intermediate
  134. result blows up.]
  135. SideEffects [None]
  136. SeeAlso [Cudd_bddClippingAnd]
  137. ******************************************************************************/
  138. DdNode *
  139. cuddBddClippingAnd(
  140. DdManager * dd /* manager */,
  141. DdNode * f /* first conjunct */,
  142. DdNode * g /* second conjunct */,
  143. int maxDepth /* maximum recursion depth */,
  144. int direction /* under (0) or over (1) approximation */)
  145. {
  146. DdNode *res;
  147. res = cuddBddClippingAndRecur(dd,f,g,maxDepth,direction);
  148. return(res);
  149. } /* end of cuddBddClippingAnd */
  150. /**Function********************************************************************
  151. Synopsis [Approximates the conjunction of two BDDs f and g and
  152. simultaneously abstracts the variables in cube.]
  153. Description [Approximates the conjunction of two BDDs f and g and
  154. simultaneously abstracts the variables in cube. Returns a
  155. pointer to the resulting BDD if successful; NULL if the intermediate
  156. result blows up.]
  157. SideEffects [None]
  158. SeeAlso [Cudd_bddClippingAndAbstract]
  159. ******************************************************************************/
  160. DdNode *
  161. cuddBddClippingAndAbstract(
  162. DdManager * dd /* manager */,
  163. DdNode * f /* first conjunct */,
  164. DdNode * g /* second conjunct */,
  165. DdNode * cube /* cube of variables to be abstracted */,
  166. int maxDepth /* maximum recursion depth */,
  167. int direction /* under (0) or over (1) approximation */)
  168. {
  169. DdNode *res;
  170. res = cuddBddClipAndAbsRecur(dd,f,g,cube,maxDepth,direction);
  171. return(res);
  172. } /* end of cuddBddClippingAndAbstract */
  173. /*---------------------------------------------------------------------------*/
  174. /* Definition of static functions */
  175. /*---------------------------------------------------------------------------*/
  176. /**Function********************************************************************
  177. Synopsis [Implements the recursive step of Cudd_bddClippingAnd.]
  178. Description [Implements the recursive step of Cudd_bddClippingAnd by taking
  179. the conjunction of two BDDs. Returns a pointer to the result is
  180. successful; NULL otherwise.]
  181. SideEffects [None]
  182. SeeAlso [cuddBddClippingAnd]
  183. ******************************************************************************/
  184. static DdNode *
  185. cuddBddClippingAndRecur(
  186. DdManager * manager,
  187. DdNode * f,
  188. DdNode * g,
  189. int distance,
  190. int direction)
  191. {
  192. DdNode *F, *ft, *fe, *G, *gt, *ge;
  193. DdNode *one, *zero, *r, *t, *e;
  194. unsigned int topf, topg, index;
  195. DD_CTFP cacheOp;
  196. statLine(manager);
  197. one = DD_ONE(manager);
  198. zero = Cudd_Not(one);
  199. /* Terminal cases. */
  200. if (f == zero || g == zero || f == Cudd_Not(g)) return(zero);
  201. if (f == g || g == one) return(f);
  202. if (f == one) return(g);
  203. if (distance == 0) {
  204. /* One last attempt at returning the right result. We sort of
  205. ** cheat by calling Cudd_bddLeq. */
  206. if (Cudd_bddLeq(manager,f,g)) return(f);
  207. if (Cudd_bddLeq(manager,g,f)) return(g);
  208. if (direction == 1) {
  209. if (Cudd_bddLeq(manager,f,Cudd_Not(g)) ||
  210. Cudd_bddLeq(manager,g,Cudd_Not(f))) return(zero);
  211. }
  212. return(Cudd_NotCond(one,(direction == 0)));
  213. }
  214. /* At this point f and g are not constant. */
  215. distance--;
  216. /* Check cache. Try to increase cache efficiency by sorting the
  217. ** pointers. */
  218. if (f > g) {
  219. DdNode *tmp = f;
  220. f = g; g = tmp;
  221. }
  222. F = Cudd_Regular(f);
  223. G = Cudd_Regular(g);
  224. cacheOp = (DD_CTFP)
  225. (direction ? Cudd_bddClippingAnd : cuddBddClippingAnd);
  226. if (F->ref != 1 || G->ref != 1) {
  227. r = cuddCacheLookup2(manager, cacheOp, f, g);
  228. if (r != NULL) return(r);
  229. }
  230. /* Here we can skip the use of cuddI, because the operands are known
  231. ** to be non-constant.
  232. */
  233. topf = manager->perm[F->index];
  234. topg = manager->perm[G->index];
  235. /* Compute cofactors. */
  236. if (topf <= topg) {
  237. index = F->index;
  238. ft = cuddT(F);
  239. fe = cuddE(F);
  240. if (Cudd_IsComplement(f)) {
  241. ft = Cudd_Not(ft);
  242. fe = Cudd_Not(fe);
  243. }
  244. } else {
  245. index = G->index;
  246. ft = fe = f;
  247. }
  248. if (topg <= topf) {
  249. gt = cuddT(G);
  250. ge = cuddE(G);
  251. if (Cudd_IsComplement(g)) {
  252. gt = Cudd_Not(gt);
  253. ge = Cudd_Not(ge);
  254. }
  255. } else {
  256. gt = ge = g;
  257. }
  258. t = cuddBddClippingAndRecur(manager, ft, gt, distance, direction);
  259. if (t == NULL) return(NULL);
  260. cuddRef(t);
  261. e = cuddBddClippingAndRecur(manager, fe, ge, distance, direction);
  262. if (e == NULL) {
  263. Cudd_RecursiveDeref(manager, t);
  264. return(NULL);
  265. }
  266. cuddRef(e);
  267. if (t == e) {
  268. r = t;
  269. } else {
  270. if (Cudd_IsComplement(t)) {
  271. r = cuddUniqueInter(manager,(int)index,Cudd_Not(t),Cudd_Not(e));
  272. if (r == NULL) {
  273. Cudd_RecursiveDeref(manager, t);
  274. Cudd_RecursiveDeref(manager, e);
  275. return(NULL);
  276. }
  277. r = Cudd_Not(r);
  278. } else {
  279. r = cuddUniqueInter(manager,(int)index,t,e);
  280. if (r == NULL) {
  281. Cudd_RecursiveDeref(manager, t);
  282. Cudd_RecursiveDeref(manager, e);
  283. return(NULL);
  284. }
  285. }
  286. }
  287. cuddDeref(e);
  288. cuddDeref(t);
  289. if (F->ref != 1 || G->ref != 1)
  290. cuddCacheInsert2(manager, cacheOp, f, g, r);
  291. return(r);
  292. } /* end of cuddBddClippingAndRecur */
  293. /**Function********************************************************************
  294. Synopsis [Approximates the AND of two BDDs and simultaneously abstracts the
  295. variables in cube.]
  296. Description [Approximates the AND of two BDDs and simultaneously
  297. abstracts the variables in cube. The variables are existentially
  298. abstracted. Returns a pointer to the result is successful; NULL
  299. otherwise.]
  300. SideEffects [None]
  301. SeeAlso [Cudd_bddClippingAndAbstract]
  302. ******************************************************************************/
  303. static DdNode *
  304. cuddBddClipAndAbsRecur(
  305. DdManager * manager,
  306. DdNode * f,
  307. DdNode * g,
  308. DdNode * cube,
  309. int distance,
  310. int direction)
  311. {
  312. DdNode *F, *ft, *fe, *G, *gt, *ge;
  313. DdNode *one, *zero, *r, *t, *e, *Cube;
  314. unsigned int topf, topg, topcube, top, index;
  315. ptruint cacheTag;
  316. statLine(manager);
  317. one = DD_ONE(manager);
  318. zero = Cudd_Not(one);
  319. /* Terminal cases. */
  320. if (f == zero || g == zero || f == Cudd_Not(g)) return(zero);
  321. if (f == one && g == one) return(one);
  322. if (cube == one) {
  323. return(cuddBddClippingAndRecur(manager, f, g, distance, direction));
  324. }
  325. if (f == one || f == g) {
  326. return (cuddBddExistAbstractRecur(manager, g, cube));
  327. }
  328. if (g == one) {
  329. return (cuddBddExistAbstractRecur(manager, f, cube));
  330. }
  331. if (distance == 0) return(Cudd_NotCond(one,(direction == 0)));
  332. /* At this point f, g, and cube are not constant. */
  333. distance--;
  334. /* Check cache. */
  335. if (f > g) { /* Try to increase cache efficiency. */
  336. DdNode *tmp = f;
  337. f = g; g = tmp;
  338. }
  339. F = Cudd_Regular(f);
  340. G = Cudd_Regular(g);
  341. cacheTag = direction ? DD_BDD_CLIPPING_AND_ABSTRACT_UP_TAG :
  342. DD_BDD_CLIPPING_AND_ABSTRACT_DOWN_TAG;
  343. if (F->ref != 1 || G->ref != 1) {
  344. r = cuddCacheLookup(manager, cacheTag,
  345. f, g, cube);
  346. if (r != NULL) {
  347. return(r);
  348. }
  349. }
  350. /* Here we can skip the use of cuddI, because the operands are known
  351. ** to be non-constant.
  352. */
  353. topf = manager->perm[F->index];
  354. topg = manager->perm[G->index];
  355. top = ddMin(topf, topg);
  356. topcube = manager->perm[cube->index];
  357. if (topcube < top) {
  358. return(cuddBddClipAndAbsRecur(manager, f, g, cuddT(cube),
  359. distance, direction));
  360. }
  361. /* Now, topcube >= top. */
  362. if (topf == top) {
  363. index = F->index;
  364. ft = cuddT(F);
  365. fe = cuddE(F);
  366. if (Cudd_IsComplement(f)) {
  367. ft = Cudd_Not(ft);
  368. fe = Cudd_Not(fe);
  369. }
  370. } else {
  371. index = G->index;
  372. ft = fe = f;
  373. }
  374. if (topg == top) {
  375. gt = cuddT(G);
  376. ge = cuddE(G);
  377. if (Cudd_IsComplement(g)) {
  378. gt = Cudd_Not(gt);
  379. ge = Cudd_Not(ge);
  380. }
  381. } else {
  382. gt = ge = g;
  383. }
  384. if (topcube == top) {
  385. Cube = cuddT(cube);
  386. } else {
  387. Cube = cube;
  388. }
  389. t = cuddBddClipAndAbsRecur(manager, ft, gt, Cube, distance, direction);
  390. if (t == NULL) return(NULL);
  391. /* Special case: 1 OR anything = 1. Hence, no need to compute
  392. ** the else branch if t is 1.
  393. */
  394. if (t == one && topcube == top) {
  395. if (F->ref != 1 || G->ref != 1)
  396. cuddCacheInsert(manager, cacheTag, f, g, cube, one);
  397. return(one);
  398. }
  399. cuddRef(t);
  400. e = cuddBddClipAndAbsRecur(manager, fe, ge, Cube, distance, direction);
  401. if (e == NULL) {
  402. Cudd_RecursiveDeref(manager, t);
  403. return(NULL);
  404. }
  405. cuddRef(e);
  406. if (topcube == top) { /* abstract */
  407. r = cuddBddClippingAndRecur(manager, Cudd_Not(t), Cudd_Not(e),
  408. distance, (direction == 0));
  409. if (r == NULL) {
  410. Cudd_RecursiveDeref(manager, t);
  411. Cudd_RecursiveDeref(manager, e);
  412. return(NULL);
  413. }
  414. r = Cudd_Not(r);
  415. cuddRef(r);
  416. Cudd_RecursiveDeref(manager, t);
  417. Cudd_RecursiveDeref(manager, e);
  418. cuddDeref(r);
  419. } else if (t == e) {
  420. r = t;
  421. cuddDeref(t);
  422. cuddDeref(e);
  423. } else {
  424. if (Cudd_IsComplement(t)) {
  425. r = cuddUniqueInter(manager,(int)index,Cudd_Not(t),Cudd_Not(e));
  426. if (r == NULL) {
  427. Cudd_RecursiveDeref(manager, t);
  428. Cudd_RecursiveDeref(manager, e);
  429. return(NULL);
  430. }
  431. r = Cudd_Not(r);
  432. } else {
  433. r = cuddUniqueInter(manager,(int)index,t,e);
  434. if (r == NULL) {
  435. Cudd_RecursiveDeref(manager, t);
  436. Cudd_RecursiveDeref(manager, e);
  437. return(NULL);
  438. }
  439. }
  440. cuddDeref(e);
  441. cuddDeref(t);
  442. }
  443. if (F->ref != 1 || G->ref != 1)
  444. cuddCacheInsert(manager, cacheTag, f, g, cube, r);
  445. return (r);
  446. } /* end of cuddBddClipAndAbsRecur */