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.

538 lines
14 KiB

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