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.

579 lines
17 KiB

  1. /**CFile***********************************************************************
  2. FileName [cuddAddAbs.c]
  3. PackageName [cudd]
  4. Synopsis [Quantification functions for ADDs.]
  5. Description [External procedures included in this module:
  6. <ul>
  7. <li> Cudd_addExistAbstract()
  8. <li> Cudd_addUnivAbstract()
  9. <li> Cudd_addOrAbstract()
  10. </ul>
  11. Internal procedures included in this module:
  12. <ul>
  13. <li> cuddAddExistAbstractRecur()
  14. <li> cuddAddUnivAbstractRecur()
  15. <li> cuddAddOrAbstractRecur()
  16. </ul>
  17. Static procedures included in this module:
  18. <ul>
  19. <li> addCheckPositiveCube()
  20. </ul>]
  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: cuddAddAbs.c,v 1.16 2012/02/05 01:07:18 fabio Exp $";
  64. #endif
  65. static DdNode *two;
  66. /*---------------------------------------------------------------------------*/
  67. /* Macro declarations */
  68. /*---------------------------------------------------------------------------*/
  69. /**AutomaticStart*************************************************************/
  70. /*---------------------------------------------------------------------------*/
  71. /* Static function prototypes */
  72. /*---------------------------------------------------------------------------*/
  73. static int addCheckPositiveCube (DdManager *manager, DdNode *cube);
  74. /**AutomaticEnd***************************************************************/
  75. /*---------------------------------------------------------------------------*/
  76. /* Definition of exported functions */
  77. /*---------------------------------------------------------------------------*/
  78. /**Function********************************************************************
  79. Synopsis [Existentially Abstracts all the variables in cube from f.]
  80. Description [Abstracts all the variables in cube from f by summing
  81. over all possible values taken by the variables. Returns the
  82. abstracted ADD.]
  83. SideEffects [None]
  84. SeeAlso [Cudd_addUnivAbstract Cudd_bddExistAbstract
  85. Cudd_addOrAbstract]
  86. ******************************************************************************/
  87. DdNode *
  88. Cudd_addExistAbstract(
  89. DdManager * manager,
  90. DdNode * f,
  91. DdNode * cube)
  92. {
  93. DdNode *res;
  94. two = cuddUniqueConst(manager,(CUDD_VALUE_TYPE) 2);
  95. if (two == NULL) return(NULL);
  96. cuddRef(two);
  97. if (addCheckPositiveCube(manager, cube) == 0) {
  98. (void) fprintf(manager->err,"Error: Can only abstract cubes");
  99. return(NULL);
  100. }
  101. do {
  102. manager->reordered = 0;
  103. res = cuddAddExistAbstractRecur(manager, f, cube);
  104. } while (manager->reordered == 1);
  105. if (res == NULL) {
  106. Cudd_RecursiveDeref(manager,two);
  107. return(NULL);
  108. }
  109. cuddRef(res);
  110. Cudd_RecursiveDeref(manager,two);
  111. cuddDeref(res);
  112. return(res);
  113. } /* end of Cudd_addExistAbstract */
  114. /**Function********************************************************************
  115. Synopsis [Universally Abstracts all the variables in cube from f.]
  116. Description [Abstracts all the variables in cube from f by taking
  117. the product over all possible values taken by the variable. Returns
  118. the abstracted ADD if successful; NULL otherwise.]
  119. SideEffects [None]
  120. SeeAlso [Cudd_addExistAbstract Cudd_bddUnivAbstract
  121. Cudd_addOrAbstract]
  122. ******************************************************************************/
  123. DdNode *
  124. Cudd_addUnivAbstract(
  125. DdManager * manager,
  126. DdNode * f,
  127. DdNode * cube)
  128. {
  129. DdNode *res;
  130. if (addCheckPositiveCube(manager, cube) == 0) {
  131. (void) fprintf(manager->err,"Error: Can only abstract cubes");
  132. return(NULL);
  133. }
  134. do {
  135. manager->reordered = 0;
  136. res = cuddAddUnivAbstractRecur(manager, f, cube);
  137. } while (manager->reordered == 1);
  138. return(res);
  139. } /* end of Cudd_addUnivAbstract */
  140. /**Function********************************************************************
  141. Synopsis [Disjunctively abstracts all the variables in cube from the
  142. 0-1 ADD f.]
  143. Description [Abstracts all the variables in cube from the 0-1 ADD f
  144. by taking the disjunction over all possible values taken by the
  145. variables. Returns the abstracted ADD if successful; NULL
  146. otherwise.]
  147. SideEffects [None]
  148. SeeAlso [Cudd_addUnivAbstract Cudd_addExistAbstract]
  149. ******************************************************************************/
  150. DdNode *
  151. Cudd_addOrAbstract(
  152. DdManager * manager,
  153. DdNode * f,
  154. DdNode * cube)
  155. {
  156. DdNode *res;
  157. if (addCheckPositiveCube(manager, cube) == 0) {
  158. (void) fprintf(manager->err,"Error: Can only abstract cubes");
  159. return(NULL);
  160. }
  161. do {
  162. manager->reordered = 0;
  163. res = cuddAddOrAbstractRecur(manager, f, cube);
  164. } while (manager->reordered == 1);
  165. return(res);
  166. } /* end of Cudd_addOrAbstract */
  167. /*---------------------------------------------------------------------------*/
  168. /* Definition of internal functions */
  169. /*---------------------------------------------------------------------------*/
  170. /**Function********************************************************************
  171. Synopsis [Performs the recursive step of Cudd_addExistAbstract.]
  172. Description [Performs the recursive step of Cudd_addExistAbstract.
  173. Returns the ADD obtained by abstracting the variables of cube from f,
  174. if successful; NULL otherwise.]
  175. SideEffects [None]
  176. SeeAlso []
  177. ******************************************************************************/
  178. DdNode *
  179. cuddAddExistAbstractRecur(
  180. DdManager * manager,
  181. DdNode * f,
  182. DdNode * cube)
  183. {
  184. DdNode *T, *E, *res, *res1, *res2, *zero;
  185. statLine(manager);
  186. zero = DD_ZERO(manager);
  187. /* Cube is guaranteed to be a cube at this point. */
  188. if (f == zero || cuddIsConstant(cube)) {
  189. return(f);
  190. }
  191. /* Abstract a variable that does not appear in f => multiply by 2. */
  192. if (cuddI(manager,f->index) > cuddI(manager,cube->index)) {
  193. res1 = cuddAddExistAbstractRecur(manager, f, cuddT(cube));
  194. if (res1 == NULL) return(NULL);
  195. cuddRef(res1);
  196. /* Use the "internal" procedure to be alerted in case of
  197. ** dynamic reordering. If dynamic reordering occurs, we
  198. ** have to abort the entire abstraction.
  199. */
  200. res = cuddAddApplyRecur(manager,Cudd_addTimes,res1,two);
  201. if (res == NULL) {
  202. Cudd_RecursiveDeref(manager,res1);
  203. return(NULL);
  204. }
  205. cuddRef(res);
  206. Cudd_RecursiveDeref(manager,res1);
  207. cuddDeref(res);
  208. return(res);
  209. }
  210. if ((res = cuddCacheLookup2(manager, Cudd_addExistAbstract, f, cube)) != NULL) {
  211. return(res);
  212. }
  213. T = cuddT(f);
  214. E = cuddE(f);
  215. /* If the two indices are the same, so are their levels. */
  216. if (f->index == cube->index) {
  217. res1 = cuddAddExistAbstractRecur(manager, T, cuddT(cube));
  218. if (res1 == NULL) return(NULL);
  219. cuddRef(res1);
  220. res2 = cuddAddExistAbstractRecur(manager, E, cuddT(cube));
  221. if (res2 == NULL) {
  222. Cudd_RecursiveDeref(manager,res1);
  223. return(NULL);
  224. }
  225. cuddRef(res2);
  226. res = cuddAddApplyRecur(manager, Cudd_addPlus, res1, res2);
  227. if (res == NULL) {
  228. Cudd_RecursiveDeref(manager,res1);
  229. Cudd_RecursiveDeref(manager,res2);
  230. return(NULL);
  231. }
  232. cuddRef(res);
  233. Cudd_RecursiveDeref(manager,res1);
  234. Cudd_RecursiveDeref(manager,res2);
  235. cuddCacheInsert2(manager, Cudd_addExistAbstract, f, cube, res);
  236. cuddDeref(res);
  237. return(res);
  238. } else { /* if (cuddI(manager,f->index) < cuddI(manager,cube->index)) */
  239. res1 = cuddAddExistAbstractRecur(manager, T, cube);
  240. if (res1 == NULL) return(NULL);
  241. cuddRef(res1);
  242. res2 = cuddAddExistAbstractRecur(manager, E, cube);
  243. if (res2 == NULL) {
  244. Cudd_RecursiveDeref(manager,res1);
  245. return(NULL);
  246. }
  247. cuddRef(res2);
  248. res = (res1 == res2) ? res1 :
  249. cuddUniqueInter(manager, (int) f->index, res1, res2);
  250. if (res == NULL) {
  251. Cudd_RecursiveDeref(manager,res1);
  252. Cudd_RecursiveDeref(manager,res2);
  253. return(NULL);
  254. }
  255. cuddDeref(res1);
  256. cuddDeref(res2);
  257. cuddCacheInsert2(manager, Cudd_addExistAbstract, f, cube, res);
  258. return(res);
  259. }
  260. } /* end of cuddAddExistAbstractRecur */
  261. /**Function********************************************************************
  262. Synopsis [Performs the recursive step of Cudd_addUnivAbstract.]
  263. Description [Performs the recursive step of Cudd_addUnivAbstract.
  264. Returns the ADD obtained by abstracting the variables of cube from f,
  265. if successful; NULL otherwise.]
  266. SideEffects [None]
  267. SeeAlso []
  268. ******************************************************************************/
  269. DdNode *
  270. cuddAddUnivAbstractRecur(
  271. DdManager * manager,
  272. DdNode * f,
  273. DdNode * cube)
  274. {
  275. DdNode *T, *E, *res, *res1, *res2, *one, *zero;
  276. statLine(manager);
  277. one = DD_ONE(manager);
  278. zero = DD_ZERO(manager);
  279. /* Cube is guaranteed to be a cube at this point.
  280. ** zero and one are the only constatnts c such that c*c=c.
  281. */
  282. if (f == zero || f == one || cube == one) {
  283. return(f);
  284. }
  285. /* Abstract a variable that does not appear in f. */
  286. if (cuddI(manager,f->index) > cuddI(manager,cube->index)) {
  287. res1 = cuddAddUnivAbstractRecur(manager, f, cuddT(cube));
  288. if (res1 == NULL) return(NULL);
  289. cuddRef(res1);
  290. /* Use the "internal" procedure to be alerted in case of
  291. ** dynamic reordering. If dynamic reordering occurs, we
  292. ** have to abort the entire abstraction.
  293. */
  294. res = cuddAddApplyRecur(manager, Cudd_addTimes, res1, res1);
  295. if (res == NULL) {
  296. Cudd_RecursiveDeref(manager,res1);
  297. return(NULL);
  298. }
  299. cuddRef(res);
  300. Cudd_RecursiveDeref(manager,res1);
  301. cuddDeref(res);
  302. return(res);
  303. }
  304. if ((res = cuddCacheLookup2(manager, Cudd_addUnivAbstract, f, cube)) != NULL) {
  305. return(res);
  306. }
  307. T = cuddT(f);
  308. E = cuddE(f);
  309. /* If the two indices are the same, so are their levels. */
  310. if (f->index == cube->index) {
  311. res1 = cuddAddUnivAbstractRecur(manager, T, cuddT(cube));
  312. if (res1 == NULL) return(NULL);
  313. cuddRef(res1);
  314. res2 = cuddAddUnivAbstractRecur(manager, E, cuddT(cube));
  315. if (res2 == NULL) {
  316. Cudd_RecursiveDeref(manager,res1);
  317. return(NULL);
  318. }
  319. cuddRef(res2);
  320. res = cuddAddApplyRecur(manager, Cudd_addTimes, res1, res2);
  321. if (res == NULL) {
  322. Cudd_RecursiveDeref(manager,res1);
  323. Cudd_RecursiveDeref(manager,res2);
  324. return(NULL);
  325. }
  326. cuddRef(res);
  327. Cudd_RecursiveDeref(manager,res1);
  328. Cudd_RecursiveDeref(manager,res2);
  329. cuddCacheInsert2(manager, Cudd_addUnivAbstract, f, cube, res);
  330. cuddDeref(res);
  331. return(res);
  332. } else { /* if (cuddI(manager,f->index) < cuddI(manager,cube->index)) */
  333. res1 = cuddAddUnivAbstractRecur(manager, T, cube);
  334. if (res1 == NULL) return(NULL);
  335. cuddRef(res1);
  336. res2 = cuddAddUnivAbstractRecur(manager, E, cube);
  337. if (res2 == NULL) {
  338. Cudd_RecursiveDeref(manager,res1);
  339. return(NULL);
  340. }
  341. cuddRef(res2);
  342. res = (res1 == res2) ? res1 :
  343. cuddUniqueInter(manager, (int) f->index, res1, res2);
  344. if (res == NULL) {
  345. Cudd_RecursiveDeref(manager,res1);
  346. Cudd_RecursiveDeref(manager,res2);
  347. return(NULL);
  348. }
  349. cuddDeref(res1);
  350. cuddDeref(res2);
  351. cuddCacheInsert2(manager, Cudd_addUnivAbstract, f, cube, res);
  352. return(res);
  353. }
  354. } /* end of cuddAddUnivAbstractRecur */
  355. /**Function********************************************************************
  356. Synopsis [Performs the recursive step of Cudd_addOrAbstract.]
  357. Description [Performs the recursive step of Cudd_addOrAbstract.
  358. Returns the ADD obtained by abstracting the variables of cube from f,
  359. if successful; NULL otherwise.]
  360. SideEffects [None]
  361. SeeAlso []
  362. ******************************************************************************/
  363. DdNode *
  364. cuddAddOrAbstractRecur(
  365. DdManager * manager,
  366. DdNode * f,
  367. DdNode * cube)
  368. {
  369. DdNode *T, *E, *res, *res1, *res2, *one;
  370. statLine(manager);
  371. one = DD_ONE(manager);
  372. /* Cube is guaranteed to be a cube at this point. */
  373. if (cuddIsConstant(f) || cube == one) {
  374. return(f);
  375. }
  376. /* Abstract a variable that does not appear in f. */
  377. if (cuddI(manager,f->index) > cuddI(manager,cube->index)) {
  378. res = cuddAddOrAbstractRecur(manager, f, cuddT(cube));
  379. return(res);
  380. }
  381. if ((res = cuddCacheLookup2(manager, Cudd_addOrAbstract, f, cube)) != NULL) {
  382. return(res);
  383. }
  384. T = cuddT(f);
  385. E = cuddE(f);
  386. /* If the two indices are the same, so are their levels. */
  387. if (f->index == cube->index) {
  388. res1 = cuddAddOrAbstractRecur(manager, T, cuddT(cube));
  389. if (res1 == NULL) return(NULL);
  390. cuddRef(res1);
  391. if (res1 != one) {
  392. res2 = cuddAddOrAbstractRecur(manager, E, cuddT(cube));
  393. if (res2 == NULL) {
  394. Cudd_RecursiveDeref(manager,res1);
  395. return(NULL);
  396. }
  397. cuddRef(res2);
  398. res = cuddAddApplyRecur(manager, Cudd_addOr, res1, res2);
  399. if (res == NULL) {
  400. Cudd_RecursiveDeref(manager,res1);
  401. Cudd_RecursiveDeref(manager,res2);
  402. return(NULL);
  403. }
  404. cuddRef(res);
  405. Cudd_RecursiveDeref(manager,res1);
  406. Cudd_RecursiveDeref(manager,res2);
  407. } else {
  408. res = res1;
  409. }
  410. cuddCacheInsert2(manager, Cudd_addOrAbstract, f, cube, res);
  411. cuddDeref(res);
  412. return(res);
  413. } else { /* if (cuddI(manager,f->index) < cuddI(manager,cube->index)) */
  414. res1 = cuddAddOrAbstractRecur(manager, T, cube);
  415. if (res1 == NULL) return(NULL);
  416. cuddRef(res1);
  417. res2 = cuddAddOrAbstractRecur(manager, E, cube);
  418. if (res2 == NULL) {
  419. Cudd_RecursiveDeref(manager,res1);
  420. return(NULL);
  421. }
  422. cuddRef(res2);
  423. res = (res1 == res2) ? res1 :
  424. cuddUniqueInter(manager, (int) f->index, res1, res2);
  425. if (res == NULL) {
  426. Cudd_RecursiveDeref(manager,res1);
  427. Cudd_RecursiveDeref(manager,res2);
  428. return(NULL);
  429. }
  430. cuddDeref(res1);
  431. cuddDeref(res2);
  432. cuddCacheInsert2(manager, Cudd_addOrAbstract, f, cube, res);
  433. return(res);
  434. }
  435. } /* end of cuddAddOrAbstractRecur */
  436. /*---------------------------------------------------------------------------*/
  437. /* Definition of static functions */
  438. /*---------------------------------------------------------------------------*/
  439. /**Function********************************************************************
  440. Synopsis [Checks whether cube is an ADD representing the product
  441. of positive literals.]
  442. Description [Checks whether cube is an ADD representing the product of
  443. positive literals. Returns 1 in case of success; 0 otherwise.]
  444. SideEffects [None]
  445. SeeAlso []
  446. ******************************************************************************/
  447. static int
  448. addCheckPositiveCube(
  449. DdManager * manager,
  450. DdNode * cube)
  451. {
  452. if (Cudd_IsComplement(cube)) return(0);
  453. if (cube == DD_ONE(manager)) return(1);
  454. if (cuddIsConstant(cube)) return(0);
  455. if (cuddE(cube) == DD_ZERO(manager)) {
  456. return(addCheckPositiveCube(manager, cuddT(cube)));
  457. }
  458. return(0);
  459. } /* end of addCheckPositiveCube */