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.

1996 lines
54 KiB

  1. /**
  2. @file
  3. @ingroup cudd
  4. @brief Priority 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 int cuddMinHammingDistRecur (DdNode * f, int *minterm, DdHashTable * table, int upperBound);
  56. static DdNode * separateCube (DdManager *dd, DdNode *f, CUDD_VALUE_TYPE *distance);
  57. static DdNode * createResult (DdManager *dd, unsigned int index, unsigned int phase, DdNode *cube, CUDD_VALUE_TYPE distance);
  58. /** \endcond */
  59. /*---------------------------------------------------------------------------*/
  60. /* Definition of exported functions */
  61. /*---------------------------------------------------------------------------*/
  62. /**
  63. @brief Selects pairs from R using a priority function.
  64. @details Selects pairs from a relation R(x,y) (given as a %BDD)
  65. in such a way that a given x appears in one pair only. Uses a
  66. priority function to determine which y should be paired to a given x.
  67. Three of the arguments--x, y, and z--are vectors of %BDD variables.
  68. The first two are the variables on which R depends. The third vector
  69. is a vector of auxiliary variables, used during the computation. This
  70. vector is optional. If a NULL value is passed instead,
  71. Cudd_PrioritySelect will create the working variables on the fly.
  72. The sizes of x and y (and z if it is not NULL) should equal n.
  73. The priority function Pi can be passed as a %BDD, or can be built by
  74. Cudd_PrioritySelect. If NULL is passed instead of a DdNode *,
  75. parameter Pifunc is used by Cudd_PrioritySelect to build a %BDD for the
  76. priority function. (Pifunc is a pointer to a C function.) If Pi is not
  77. NULL, then Pifunc is ignored. Pifunc should have the same interface as
  78. the standard priority functions (e.g., Cudd_Dxygtdxz).
  79. Cudd_PrioritySelect and Cudd_CProjection can sometimes be used
  80. interchangeably. Specifically, calling Cudd_PrioritySelect with
  81. Cudd_Xgty as Pifunc produces the same result as calling
  82. Cudd_CProjection with the all-zero minterm as reference minterm.
  83. However, depending on the application, one or the other may be
  84. preferable:
  85. <ul>
  86. <li> When extracting representatives from an equivalence relation,
  87. Cudd_CProjection has the advantage of nor requiring the auxiliary
  88. variables.
  89. <li> When computing matchings in general bipartite graphs,
  90. Cudd_PrioritySelect normally obtains better results because it can use
  91. more powerful matching schemes (e.g., Cudd_Dxygtdxz).
  92. </ul>
  93. @return a pointer to the selected function if successful; NULL
  94. otherwise.
  95. @sideeffect If called with z == NULL, will create new variables in
  96. the manager.
  97. @see Cudd_Dxygtdxz Cudd_Dxygtdyz Cudd_Xgty
  98. Cudd_bddAdjPermuteX Cudd_CProjection
  99. */
  100. DdNode *
  101. Cudd_PrioritySelect(
  102. DdManager * dd /**< manager */,
  103. DdNode * R /**< %BDD of the relation */,
  104. DdNode ** x /**< array of x variables */,
  105. DdNode ** y /**< array of y variables */,
  106. DdNode ** z /**< array of z variables (optional: may be NULL) */,
  107. DdNode * Pi /**< %BDD of the priority function (optional: may be NULL) */,
  108. int n /**< size of x, y, and z */,
  109. DD_PRFP Pifunc /**< function used to build Pi if it is NULL */)
  110. {
  111. DdNode *res = NULL;
  112. DdNode *zcube = NULL;
  113. DdNode *Rxz, *Q;
  114. int createdZ = 0;
  115. int createdPi = 0;
  116. int i;
  117. /* Create z variables if needed. */
  118. if (z == NULL) {
  119. if (Pi != NULL) return(NULL);
  120. z = ALLOC(DdNode *,n);
  121. if (z == NULL) {
  122. dd->errorCode = CUDD_MEMORY_OUT;
  123. return(NULL);
  124. }
  125. createdZ = 1;
  126. for (i = 0; i < n; i++) {
  127. if (dd->size >= (int) CUDD_MAXINDEX - 1) goto endgame;
  128. z[i] = cuddUniqueInter(dd,dd->size,dd->one,Cudd_Not(dd->one));
  129. if (z[i] == NULL) goto endgame;
  130. }
  131. }
  132. /* Create priority function BDD if needed. */
  133. if (Pi == NULL) {
  134. Pi = Pifunc(dd,n,x,y,z);
  135. if (Pi == NULL) goto endgame;
  136. createdPi = 1;
  137. cuddRef(Pi);
  138. }
  139. /* Initialize abstraction cube. */
  140. zcube = DD_ONE(dd);
  141. cuddRef(zcube);
  142. for (i = n - 1; i >= 0; i--) {
  143. DdNode *tmpp;
  144. tmpp = Cudd_bddAnd(dd,z[i],zcube);
  145. if (tmpp == NULL) goto endgame;
  146. cuddRef(tmpp);
  147. Cudd_RecursiveDeref(dd,zcube);
  148. zcube = tmpp;
  149. }
  150. /* Compute subset of (x,y) pairs. */
  151. Rxz = Cudd_bddSwapVariables(dd,R,y,z,n);
  152. if (Rxz == NULL) goto endgame;
  153. cuddRef(Rxz);
  154. Q = Cudd_bddAndAbstract(dd,Rxz,Pi,zcube);
  155. if (Q == NULL) {
  156. Cudd_RecursiveDeref(dd,Rxz);
  157. goto endgame;
  158. }
  159. cuddRef(Q);
  160. Cudd_RecursiveDeref(dd,Rxz);
  161. res = Cudd_bddAnd(dd,R,Cudd_Not(Q));
  162. if (res == NULL) {
  163. Cudd_RecursiveDeref(dd,Q);
  164. goto endgame;
  165. }
  166. cuddRef(res);
  167. Cudd_RecursiveDeref(dd,Q);
  168. endgame:
  169. if (zcube != NULL) Cudd_RecursiveDeref(dd,zcube);
  170. if (createdZ) {
  171. FREE(z);
  172. }
  173. if (createdPi) {
  174. Cudd_RecursiveDeref(dd,Pi);
  175. }
  176. if (res != NULL) cuddDeref(res);
  177. return(res);
  178. } /* Cudd_PrioritySelect */
  179. /**
  180. @brief Generates a %BDD for the function x &gt; y.
  181. @details This function generates a %BDD for the function x &gt; y.
  182. Both x and y are N-bit numbers, x\[0\] x\[1\] ... x\[N-1\] and
  183. y\[0\] y\[1\] ... y\[N-1\], with 0 the most significant bit.
  184. The %BDD is built bottom-up.
  185. It has 3*N-1 internal nodes, if the variables are ordered as follows:
  186. x\[0\] y\[0\] x\[1\] y\[1\] ... x\[N-1\] y\[N-1\].
  187. Argument z is not used by Cudd_Xgty: it is included to make it
  188. call-compatible to Cudd_Dxygtdxz and Cudd_Dxygtdyz.
  189. @sideeffect None
  190. @see Cudd_PrioritySelect Cudd_Dxygtdxz Cudd_Dxygtdyz
  191. */
  192. DdNode *
  193. Cudd_Xgty(
  194. DdManager * dd /**< %DD manager */,
  195. int N /**< number of x and y variables */,
  196. DdNode ** z /**< array of z variables: unused */,
  197. DdNode ** x /**< array of x variables */,
  198. DdNode ** y /**< array of y variables */)
  199. {
  200. DdNode *u, *v, *w;
  201. int i;
  202. (void) z; /* avoid warning */
  203. /* Build bottom part of BDD outside loop. */
  204. u = Cudd_bddAnd(dd, x[N-1], Cudd_Not(y[N-1]));
  205. if (u == NULL) return(NULL);
  206. cuddRef(u);
  207. /* Loop to build the rest of the BDD. */
  208. for (i = N-2; i >= 0; i--) {
  209. v = Cudd_bddAnd(dd, y[i], Cudd_Not(u));
  210. if (v == NULL) {
  211. Cudd_RecursiveDeref(dd, u);
  212. return(NULL);
  213. }
  214. cuddRef(v);
  215. w = Cudd_bddAnd(dd, Cudd_Not(y[i]), u);
  216. if (w == NULL) {
  217. Cudd_RecursiveDeref(dd, u);
  218. Cudd_RecursiveDeref(dd, v);
  219. return(NULL);
  220. }
  221. cuddRef(w);
  222. Cudd_RecursiveDeref(dd, u);
  223. u = Cudd_bddIte(dd, x[i], Cudd_Not(v), w);
  224. if (u == NULL) {
  225. Cudd_RecursiveDeref(dd, v);
  226. Cudd_RecursiveDeref(dd, w);
  227. return(NULL);
  228. }
  229. cuddRef(u);
  230. Cudd_RecursiveDeref(dd, v);
  231. Cudd_RecursiveDeref(dd, w);
  232. }
  233. cuddDeref(u);
  234. return(u);
  235. } /* end of Cudd_Xgty */
  236. /**
  237. @brief Generates a %BDD for the function x==y.
  238. @details This function generates a %BDD for the function x==y.
  239. Both x and y are N-bit numbers, x\[0\] x\[1\] ... x\[N-1\] and
  240. y\[0\] y\[1\] ... y\[N-1\]. The %BDD is built bottom-up.
  241. It has 3*N-1 internal nodes, if the variables are ordered as follows:
  242. x\[0\] y\[0\] x\[1\] y\[1\] ... x\[N-1\] y\[N-1\].
  243. @sideeffect None
  244. @see Cudd_addXeqy
  245. */
  246. DdNode *
  247. Cudd_Xeqy(
  248. DdManager * dd /**< %DD manager */,
  249. int N /**< number of x and y variables */,
  250. DdNode ** x /**< array of x variables */,
  251. DdNode ** y /**< array of y variables */)
  252. {
  253. DdNode *u, *v, *w;
  254. int i;
  255. /* Build bottom part of BDD outside loop. */
  256. u = Cudd_bddIte(dd, x[N-1], y[N-1], Cudd_Not(y[N-1]));
  257. if (u == NULL) return(NULL);
  258. cuddRef(u);
  259. /* Loop to build the rest of the BDD. */
  260. for (i = N-2; i >= 0; i--) {
  261. v = Cudd_bddAnd(dd, y[i], u);
  262. if (v == NULL) {
  263. Cudd_RecursiveDeref(dd, u);
  264. return(NULL);
  265. }
  266. cuddRef(v);
  267. w = Cudd_bddAnd(dd, Cudd_Not(y[i]), u);
  268. if (w == NULL) {
  269. Cudd_RecursiveDeref(dd, u);
  270. Cudd_RecursiveDeref(dd, v);
  271. return(NULL);
  272. }
  273. cuddRef(w);
  274. Cudd_RecursiveDeref(dd, u);
  275. u = Cudd_bddIte(dd, x[i], v, w);
  276. if (u == NULL) {
  277. Cudd_RecursiveDeref(dd, v);
  278. Cudd_RecursiveDeref(dd, w);
  279. return(NULL);
  280. }
  281. cuddRef(u);
  282. Cudd_RecursiveDeref(dd, v);
  283. Cudd_RecursiveDeref(dd, w);
  284. }
  285. cuddDeref(u);
  286. return(u);
  287. } /* end of Cudd_Xeqy */
  288. /**
  289. @brief Generates an %ADD for the function x==y.
  290. @details This function generates an %ADD for the function x==y.
  291. Both x and y are N-bit numbers, x\[0\] x\[1\] ... x\[N-1\] and
  292. y\[0\] y\[1\] ... y\[N-1\]. The %ADD is built bottom-up.
  293. It has 3*N-1 internal nodes, if the variables are ordered as follows:
  294. x\[0\] y\[0\] x\[1\] y\[1\] ... x\[N-1\] y\[N-1\].
  295. @sideeffect None
  296. @see Cudd_Xeqy
  297. */
  298. DdNode *
  299. Cudd_addXeqy(
  300. DdManager * dd /**< %DD manager */,
  301. int N /**< number of x and y variables */,
  302. DdNode ** x /**< array of x variables */,
  303. DdNode ** y /**< array of y variables */)
  304. {
  305. DdNode *one, *zero;
  306. DdNode *u, *v, *w;
  307. int i;
  308. one = DD_ONE(dd);
  309. zero = DD_ZERO(dd);
  310. /* Build bottom part of ADD outside loop. */
  311. v = Cudd_addIte(dd, y[N-1], one, zero);
  312. if (v == NULL) return(NULL);
  313. cuddRef(v);
  314. w = Cudd_addIte(dd, y[N-1], zero, one);
  315. if (w == NULL) {
  316. Cudd_RecursiveDeref(dd, v);
  317. return(NULL);
  318. }
  319. cuddRef(w);
  320. u = Cudd_addIte(dd, x[N-1], v, w);
  321. if (u == NULL) {
  322. Cudd_RecursiveDeref(dd, v);
  323. Cudd_RecursiveDeref(dd, w);
  324. return(NULL);
  325. }
  326. cuddRef(u);
  327. Cudd_RecursiveDeref(dd, v);
  328. Cudd_RecursiveDeref(dd, w);
  329. /* Loop to build the rest of the ADD. */
  330. for (i = N-2; i >= 0; i--) {
  331. v = Cudd_addIte(dd, y[i], u, zero);
  332. if (v == NULL) {
  333. Cudd_RecursiveDeref(dd, u);
  334. return(NULL);
  335. }
  336. cuddRef(v);
  337. w = Cudd_addIte(dd, y[i], zero, u);
  338. if (w == NULL) {
  339. Cudd_RecursiveDeref(dd, u);
  340. Cudd_RecursiveDeref(dd, v);
  341. return(NULL);
  342. }
  343. cuddRef(w);
  344. Cudd_RecursiveDeref(dd, u);
  345. u = Cudd_addIte(dd, x[i], v, w);
  346. if (w == NULL) {
  347. Cudd_RecursiveDeref(dd, v);
  348. Cudd_RecursiveDeref(dd, w);
  349. return(NULL);
  350. }
  351. cuddRef(u);
  352. Cudd_RecursiveDeref(dd, v);
  353. Cudd_RecursiveDeref(dd, w);
  354. }
  355. cuddDeref(u);
  356. return(u);
  357. } /* end of Cudd_addXeqy */
  358. /**
  359. @brief Generates a %BDD for the function d(x,y) &gt; d(x,z).
  360. @details This function generates a %BDD for the function d(x,y)
  361. &gt; d(x,z);
  362. x, y, and z are N-bit numbers, x\[0\] x\[1\] ... x\[N-1\],
  363. y\[0\] y\[1\] ... y\[N-1\], and z\[0\] z\[1\] ... z\[N-1\],
  364. with 0 the most significant bit.
  365. The distance d(x,y) is defined as:
  366. \f$\sum_{i=0}^{N-1}(|x_i - y_i| \cdot 2^{N-i-1})\f$.
  367. The %BDD is built bottom-up.
  368. It has 7*N-3 internal nodes, if the variables are ordered as follows:
  369. x\[0\] y\[0\] z\[0\] x\[1\] y\[1\] z\[1\] ... x\[N-1\] y\[N-1\] z\[N-1\].
  370. @sideeffect None
  371. @see Cudd_PrioritySelect Cudd_Dxygtdyz Cudd_Xgty Cudd_bddAdjPermuteX
  372. */
  373. DdNode *
  374. Cudd_Dxygtdxz(
  375. DdManager * dd /**< %DD manager */,
  376. int N /**< number of x, y, and z variables */,
  377. DdNode ** x /**< array of x variables */,
  378. DdNode ** y /**< array of y variables */,
  379. DdNode ** z /**< array of z variables */)
  380. {
  381. DdNode *one, *zero;
  382. DdNode *z1, *z2, *z3, *z4, *y1_, *y2, *x1;
  383. int i;
  384. one = DD_ONE(dd);
  385. zero = Cudd_Not(one);
  386. /* Build bottom part of BDD outside loop. */
  387. y1_ = Cudd_bddIte(dd, y[N-1], one, Cudd_Not(z[N-1]));
  388. if (y1_ == NULL) return(NULL);
  389. cuddRef(y1_);
  390. y2 = Cudd_bddIte(dd, y[N-1], z[N-1], one);
  391. if (y2 == NULL) {
  392. Cudd_RecursiveDeref(dd, y1_);
  393. return(NULL);
  394. }
  395. cuddRef(y2);
  396. x1 = Cudd_bddIte(dd, x[N-1], y1_, y2);
  397. if (x1 == NULL) {
  398. Cudd_RecursiveDeref(dd, y1_);
  399. Cudd_RecursiveDeref(dd, y2);
  400. return(NULL);
  401. }
  402. cuddRef(x1);
  403. Cudd_RecursiveDeref(dd, y1_);
  404. Cudd_RecursiveDeref(dd, y2);
  405. /* Loop to build the rest of the BDD. */
  406. for (i = N-2; i >= 0; i--) {
  407. z1 = Cudd_bddIte(dd, z[i], one, Cudd_Not(x1));
  408. if (z1 == NULL) {
  409. Cudd_RecursiveDeref(dd, x1);
  410. return(NULL);
  411. }
  412. cuddRef(z1);
  413. z2 = Cudd_bddIte(dd, z[i], x1, one);
  414. if (z2 == NULL) {
  415. Cudd_RecursiveDeref(dd, x1);
  416. Cudd_RecursiveDeref(dd, z1);
  417. return(NULL);
  418. }
  419. cuddRef(z2);
  420. z3 = Cudd_bddIte(dd, z[i], one, x1);
  421. if (z3 == NULL) {
  422. Cudd_RecursiveDeref(dd, x1);
  423. Cudd_RecursiveDeref(dd, z1);
  424. Cudd_RecursiveDeref(dd, z2);
  425. return(NULL);
  426. }
  427. cuddRef(z3);
  428. z4 = Cudd_bddIte(dd, z[i], x1, zero);
  429. if (z4 == NULL) {
  430. Cudd_RecursiveDeref(dd, x1);
  431. Cudd_RecursiveDeref(dd, z1);
  432. Cudd_RecursiveDeref(dd, z2);
  433. Cudd_RecursiveDeref(dd, z3);
  434. return(NULL);
  435. }
  436. cuddRef(z4);
  437. Cudd_RecursiveDeref(dd, x1);
  438. y1_ = Cudd_bddIte(dd, y[i], z2, Cudd_Not(z1));
  439. if (y1_ == NULL) {
  440. Cudd_RecursiveDeref(dd, z1);
  441. Cudd_RecursiveDeref(dd, z2);
  442. Cudd_RecursiveDeref(dd, z3);
  443. Cudd_RecursiveDeref(dd, z4);
  444. return(NULL);
  445. }
  446. cuddRef(y1_);
  447. y2 = Cudd_bddIte(dd, y[i], z4, z3);
  448. if (y2 == NULL) {
  449. Cudd_RecursiveDeref(dd, z1);
  450. Cudd_RecursiveDeref(dd, z2);
  451. Cudd_RecursiveDeref(dd, z3);
  452. Cudd_RecursiveDeref(dd, z4);
  453. Cudd_RecursiveDeref(dd, y1_);
  454. return(NULL);
  455. }
  456. cuddRef(y2);
  457. Cudd_RecursiveDeref(dd, z1);
  458. Cudd_RecursiveDeref(dd, z2);
  459. Cudd_RecursiveDeref(dd, z3);
  460. Cudd_RecursiveDeref(dd, z4);
  461. x1 = Cudd_bddIte(dd, x[i], y1_, y2);
  462. if (x1 == NULL) {
  463. Cudd_RecursiveDeref(dd, y1_);
  464. Cudd_RecursiveDeref(dd, y2);
  465. return(NULL);
  466. }
  467. cuddRef(x1);
  468. Cudd_RecursiveDeref(dd, y1_);
  469. Cudd_RecursiveDeref(dd, y2);
  470. }
  471. cuddDeref(x1);
  472. return(Cudd_Not(x1));
  473. } /* end of Cudd_Dxygtdxz */
  474. /**
  475. @brief Generates a %BDD for the function d(x,y) &gt; d(y,z).
  476. @details This function generates a %BDD for the function d(x,y)
  477. &gt; d(y,z);
  478. x, y, and z are N-bit numbers, x\[0\] x\[1\] ... x\[N-1\],
  479. y\[0\] y\[1\] ... y\[N-1\], and z\[0\] z\[1\] ... z\[N-1\],
  480. with 0 the most significant bit.
  481. The distance d(x,y) is defined as:
  482. \f$\sum_{i=0}^{N-1}(|x_i - y_i| \cdot 2^{N-i-1})\f$.
  483. The %BDD is built bottom-up.
  484. It has 7*N-3 internal nodes, if the variables are ordered as follows:
  485. x\[0\] y\[0\] z\[0\] x\[1\] y\[1\] z\[1\] ... x\[N-1\] y\[N-1\] z\[N-1\].
  486. @sideeffect None
  487. @see Cudd_PrioritySelect Cudd_Dxygtdxz Cudd_Xgty Cudd_bddAdjPermuteX
  488. */
  489. DdNode *
  490. Cudd_Dxygtdyz(
  491. DdManager * dd /**< %DD manager */,
  492. int N /**< number of x, y, and z variables */,
  493. DdNode ** x /**< array of x variables */,
  494. DdNode ** y /**< array of y variables */,
  495. DdNode ** z /**< array of z variables */)
  496. {
  497. DdNode *one, *zero;
  498. DdNode *z1, *z2, *z3, *z4, *y1_, *y2, *x1;
  499. int i;
  500. one = DD_ONE(dd);
  501. zero = Cudd_Not(one);
  502. /* Build bottom part of BDD outside loop. */
  503. y1_ = Cudd_bddIte(dd, y[N-1], one, z[N-1]);
  504. if (y1_ == NULL) return(NULL);
  505. cuddRef(y1_);
  506. y2 = Cudd_bddIte(dd, y[N-1], z[N-1], zero);
  507. if (y2 == NULL) {
  508. Cudd_RecursiveDeref(dd, y1_);
  509. return(NULL);
  510. }
  511. cuddRef(y2);
  512. x1 = Cudd_bddIte(dd, x[N-1], y1_, Cudd_Not(y2));
  513. if (x1 == NULL) {
  514. Cudd_RecursiveDeref(dd, y1_);
  515. Cudd_RecursiveDeref(dd, y2);
  516. return(NULL);
  517. }
  518. cuddRef(x1);
  519. Cudd_RecursiveDeref(dd, y1_);
  520. Cudd_RecursiveDeref(dd, y2);
  521. /* Loop to build the rest of the BDD. */
  522. for (i = N-2; i >= 0; i--) {
  523. z1 = Cudd_bddIte(dd, z[i], x1, zero);
  524. if (z1 == NULL) {
  525. Cudd_RecursiveDeref(dd, x1);
  526. return(NULL);
  527. }
  528. cuddRef(z1);
  529. z2 = Cudd_bddIte(dd, z[i], x1, one);
  530. if (z2 == NULL) {
  531. Cudd_RecursiveDeref(dd, x1);
  532. Cudd_RecursiveDeref(dd, z1);
  533. return(NULL);
  534. }
  535. cuddRef(z2);
  536. z3 = Cudd_bddIte(dd, z[i], one, x1);
  537. if (z3 == NULL) {
  538. Cudd_RecursiveDeref(dd, x1);
  539. Cudd_RecursiveDeref(dd, z1);
  540. Cudd_RecursiveDeref(dd, z2);
  541. return(NULL);
  542. }
  543. cuddRef(z3);
  544. z4 = Cudd_bddIte(dd, z[i], one, Cudd_Not(x1));
  545. if (z4 == NULL) {
  546. Cudd_RecursiveDeref(dd, x1);
  547. Cudd_RecursiveDeref(dd, z1);
  548. Cudd_RecursiveDeref(dd, z2);
  549. Cudd_RecursiveDeref(dd, z3);
  550. return(NULL);
  551. }
  552. cuddRef(z4);
  553. Cudd_RecursiveDeref(dd, x1);
  554. y1_ = Cudd_bddIte(dd, y[i], z2, z1);
  555. if (y1_ == NULL) {
  556. Cudd_RecursiveDeref(dd, z1);
  557. Cudd_RecursiveDeref(dd, z2);
  558. Cudd_RecursiveDeref(dd, z3);
  559. Cudd_RecursiveDeref(dd, z4);
  560. return(NULL);
  561. }
  562. cuddRef(y1_);
  563. y2 = Cudd_bddIte(dd, y[i], z4, Cudd_Not(z3));
  564. if (y2 == NULL) {
  565. Cudd_RecursiveDeref(dd, z1);
  566. Cudd_RecursiveDeref(dd, z2);
  567. Cudd_RecursiveDeref(dd, z3);
  568. Cudd_RecursiveDeref(dd, z4);
  569. Cudd_RecursiveDeref(dd, y1_);
  570. return(NULL);
  571. }
  572. cuddRef(y2);
  573. Cudd_RecursiveDeref(dd, z1);
  574. Cudd_RecursiveDeref(dd, z2);
  575. Cudd_RecursiveDeref(dd, z3);
  576. Cudd_RecursiveDeref(dd, z4);
  577. x1 = Cudd_bddIte(dd, x[i], y1_, Cudd_Not(y2));
  578. if (x1 == NULL) {
  579. Cudd_RecursiveDeref(dd, y1_);
  580. Cudd_RecursiveDeref(dd, y2);
  581. return(NULL);
  582. }
  583. cuddRef(x1);
  584. Cudd_RecursiveDeref(dd, y1_);
  585. Cudd_RecursiveDeref(dd, y2);
  586. }
  587. cuddDeref(x1);
  588. return(Cudd_Not(x1));
  589. } /* end of Cudd_Dxygtdyz */
  590. /**
  591. @brief Generates a %BDD for the function x - y &ge; c.
  592. @details This function generates a %BDD for the function x -y &ge; c.
  593. Both x and y are N-bit numbers, x\[0\] x\[1\] ... x\[N-1\] and
  594. y\[0\] y\[1\] ... y\[N-1\], with 0 the most significant bit.
  595. The %BDD is built bottom-up.
  596. It has a linear number of nodes if the variables are ordered as follows:
  597. x\[0\] y\[0\] x\[1\] y\[1\] ... x\[N-1\] y\[N-1\].
  598. @sideeffect None
  599. @see Cudd_Xgty
  600. */
  601. DdNode *
  602. Cudd_Inequality(
  603. DdManager * dd /**< %DD manager */,
  604. int N /**< number of x and y variables */,
  605. int c /**< right-hand side constant */,
  606. DdNode ** x /**< array of x variables */,
  607. DdNode ** y /**< array of y variables */)
  608. {
  609. /* The nodes at level i represent values of the difference that are
  610. ** multiples of 2^i. We use variables with names starting with k
  611. ** to denote the multipliers of 2^i in such multiples. */
  612. int kTrue = c;
  613. int kFalse = c - 1;
  614. /* Mask used to compute the ceiling function. Since we divide by 2^i,
  615. ** we want to know whether the dividend is a multiple of 2^i. If it is,
  616. ** then ceiling and floor coincide; otherwise, they differ by one. */
  617. int mask = 1;
  618. int i;
  619. DdNode *f = NULL; /* the eventual result */
  620. DdNode *one = DD_ONE(dd);
  621. DdNode *zero = Cudd_Not(one);
  622. /* Two x-labeled nodes are created at most at each iteration. They are
  623. ** stored, along with their k values, in these variables. At each level,
  624. ** the old nodes are freed and the new nodes are copied into the old map.
  625. */
  626. DdNode *map[2] = {NULL, NULL};
  627. int invalidIndex = 1 << (N-1);
  628. int index[2] = {invalidIndex, invalidIndex};
  629. /* This should never happen. */
  630. if (N < 0) return(NULL);
  631. /* If there are no bits, both operands are 0. The result depends on c. */
  632. if (N == 0) {
  633. if (c >= 0) return(one);
  634. else return(zero);
  635. }
  636. /* The maximum or the minimum difference comparing to c can generate the terminal case */
  637. if ((1 << N) - 1 < c) return(zero);
  638. else if ((-(1 << N) + 1) >= c) return(one);
  639. /* Build the result bottom up. */
  640. for (i = 1; i <= N; i++) {
  641. int kTrueLower, kFalseLower;
  642. int leftChild, middleChild, rightChild;
  643. DdNode *g0, *g1, *fplus, *fequal, *fminus;
  644. int j;
  645. DdNode *newMap[2] = {NULL, NULL};
  646. int newIndex[2];
  647. kTrueLower = kTrue;
  648. kFalseLower = kFalse;
  649. /* kTrue = ceiling((c-1)/2^i) + 1 */
  650. kTrue = ((c-1) >> i) + ((c & mask) != 1) + 1;
  651. mask = (mask << 1) | 1;
  652. /* kFalse = floor(c/2^i) - 1 */
  653. kFalse = (c >> i) - 1;
  654. newIndex[0] = invalidIndex;
  655. newIndex[1] = invalidIndex;
  656. for (j = kFalse + 1; j < kTrue; j++) {
  657. /* Skip if node is not reachable from top of BDD. */
  658. if ((j >= (1 << (N - i))) || (j <= -(1 << (N -i)))) continue;
  659. /* Find f- */
  660. leftChild = (j << 1) - 1;
  661. if (leftChild >= kTrueLower) {
  662. fminus = one;
  663. } else if (leftChild <= kFalseLower) {
  664. fminus = zero;
  665. } else {
  666. assert(leftChild == index[0] || leftChild == index[1]);
  667. if (leftChild == index[0]) {
  668. fminus = map[0];
  669. } else {
  670. fminus = map[1];
  671. }
  672. }
  673. /* Find f= */
  674. middleChild = j << 1;
  675. if (middleChild >= kTrueLower) {
  676. fequal = one;
  677. } else if (middleChild <= kFalseLower) {
  678. fequal = zero;
  679. } else {
  680. assert(middleChild == index[0] || middleChild == index[1]);
  681. if (middleChild == index[0]) {
  682. fequal = map[0];
  683. } else {
  684. fequal = map[1];
  685. }
  686. }
  687. /* Find f+ */
  688. rightChild = (j << 1) + 1;
  689. if (rightChild >= kTrueLower) {
  690. fplus = one;
  691. } else if (rightChild <= kFalseLower) {
  692. fplus = zero;
  693. } else {
  694. assert(rightChild == index[0] || rightChild == index[1]);
  695. if (rightChild == index[0]) {
  696. fplus = map[0];
  697. } else {
  698. fplus = map[1];
  699. }
  700. }
  701. /* Build new nodes. */
  702. g1 = Cudd_bddIte(dd, y[N - i], fequal, fplus);
  703. if (g1 == NULL) {
  704. if (index[0] != invalidIndex) Cudd_IterDerefBdd(dd, map[0]);
  705. if (index[1] != invalidIndex) Cudd_IterDerefBdd(dd, map[1]);
  706. if (newIndex[0] != invalidIndex) Cudd_IterDerefBdd(dd, newMap[0]);
  707. if (newIndex[1] != invalidIndex) Cudd_IterDerefBdd(dd, newMap[1]);
  708. return(NULL);
  709. }
  710. cuddRef(g1);
  711. g0 = Cudd_bddIte(dd, y[N - i], fminus, fequal);
  712. if (g0 == NULL) {
  713. Cudd_IterDerefBdd(dd, g1);
  714. if (index[0] != invalidIndex) Cudd_IterDerefBdd(dd, map[0]);
  715. if (index[1] != invalidIndex) Cudd_IterDerefBdd(dd, map[1]);
  716. if (newIndex[0] != invalidIndex) Cudd_IterDerefBdd(dd, newMap[0]);
  717. if (newIndex[1] != invalidIndex) Cudd_IterDerefBdd(dd, newMap[1]);
  718. return(NULL);
  719. }
  720. cuddRef(g0);
  721. f = Cudd_bddIte(dd, x[N - i], g1, g0);
  722. if (f == NULL) {
  723. Cudd_IterDerefBdd(dd, g1);
  724. Cudd_IterDerefBdd(dd, g0);
  725. if (index[0] != invalidIndex) Cudd_IterDerefBdd(dd, map[0]);
  726. if (index[1] != invalidIndex) Cudd_IterDerefBdd(dd, map[1]);
  727. if (newIndex[0] != invalidIndex) Cudd_IterDerefBdd(dd, newMap[0]);
  728. if (newIndex[1] != invalidIndex) Cudd_IterDerefBdd(dd, newMap[1]);
  729. return(NULL);
  730. }
  731. cuddRef(f);
  732. Cudd_IterDerefBdd(dd, g1);
  733. Cudd_IterDerefBdd(dd, g0);
  734. /* Save newly computed node in map. */
  735. assert(newIndex[0] == invalidIndex || newIndex[1] == invalidIndex);
  736. if (newIndex[0] == invalidIndex) {
  737. newIndex[0] = j;
  738. newMap[0] = f;
  739. } else {
  740. newIndex[1] = j;
  741. newMap[1] = f;
  742. }
  743. }
  744. /* Copy new map to map. */
  745. if (index[0] != invalidIndex) Cudd_IterDerefBdd(dd, map[0]);
  746. if (index[1] != invalidIndex) Cudd_IterDerefBdd(dd, map[1]);
  747. map[0] = newMap[0];
  748. map[1] = newMap[1];
  749. index[0] = newIndex[0];
  750. index[1] = newIndex[1];
  751. }
  752. cuddDeref(f);
  753. return(f);
  754. } /* end of Cudd_Inequality */
  755. /**
  756. @brief Generates a %BDD for the function x - y != c.
  757. @details This function generates a %BDD for the function x -y != c.
  758. Both x and y are N-bit numbers, x\[0\] x\[1\] ... x\[N-1\] and
  759. y\[0\] y\[1\] ... y\[N-1\], with 0 the most significant bit.
  760. The %BDD is built bottom-up.
  761. It has a linear number of nodes if the variables are ordered as follows:
  762. x\[0\] y\[0\] x\[1\] y\[1\] ... x\[N-1\] y\[N-1\].
  763. @sideeffect None
  764. @see Cudd_Xgty
  765. */
  766. DdNode *
  767. Cudd_Disequality(
  768. DdManager * dd /**< %DD manager */,
  769. int N /**< number of x and y variables */,
  770. int c /**< right-hand side constant */,
  771. DdNode ** x /**< array of x variables */,
  772. DdNode ** y /**< array of y variables */)
  773. {
  774. /* The nodes at level i represent values of the difference that are
  775. ** multiples of 2^i. We use variables with names starting with k
  776. ** to denote the multipliers of 2^i in such multiples. */
  777. int kTrueLb = c + 1;
  778. int kTrueUb = c - 1;
  779. int kFalse = c;
  780. /* Mask used to compute the ceiling function. Since we divide by 2^i,
  781. ** we want to know whether the dividend is a multiple of 2^i. If it is,
  782. ** then ceiling and floor coincide; otherwise, they differ by one. */
  783. int mask = 1;
  784. int i;
  785. DdNode *f = NULL; /* the eventual result */
  786. DdNode *one = DD_ONE(dd);
  787. DdNode *zero = Cudd_Not(one);
  788. /* Two x-labeled nodes are created at most at each iteration. They are
  789. ** stored, along with their k values, in these variables. At each level,
  790. ** the old nodes are freed and the new nodes are copied into the old map.
  791. */
  792. DdNode *map[2] = {NULL, NULL};
  793. int invalidIndex = 1 << (N-1);
  794. int index[2] = {invalidIndex, invalidIndex};
  795. /* This should never happen. */
  796. if (N < 0) return(NULL);
  797. /* If there are no bits, both operands are 0. The result depends on c. */
  798. if (N == 0) {
  799. if (c != 0) return(one);
  800. else return(zero);
  801. }
  802. /* The maximum or the minimum difference comparing to c can generate the terminal case */
  803. if ((1 << N) - 1 < c || (-(1 << N) + 1) > c) return(one);
  804. /* Build the result bottom up. */
  805. for (i = 1; i <= N; i++) {
  806. int kTrueLbLower, kTrueUbLower;
  807. int leftChild, middleChild, rightChild;
  808. DdNode *g0, *g1, *fplus, *fequal, *fminus;
  809. int j;
  810. DdNode *newMap[2] = {NULL, NULL};
  811. int newIndex[2];
  812. kTrueLbLower = kTrueLb;
  813. kTrueUbLower = kTrueUb;
  814. /* kTrueLb = floor((c-1)/2^i) + 2 */
  815. kTrueLb = ((c-1) >> i) + 2;
  816. /* kTrueUb = ceiling((c+1)/2^i) - 2 */
  817. kTrueUb = ((c+1) >> i) + (((c+2) & mask) != 1) - 2;
  818. mask = (mask << 1) | 1;
  819. newIndex[0] = invalidIndex;
  820. newIndex[1] = invalidIndex;
  821. for (j = kTrueUb + 1; j < kTrueLb; j++) {
  822. /* Skip if node is not reachable from top of BDD. */
  823. if ((j >= (1 << (N - i))) || (j <= -(1 << (N -i)))) continue;
  824. /* Find f- */
  825. leftChild = (j << 1) - 1;
  826. if (leftChild >= kTrueLbLower || leftChild <= kTrueUbLower) {
  827. fminus = one;
  828. } else if (i == 1 && leftChild == kFalse) {
  829. fminus = zero;
  830. } else {
  831. assert(leftChild == index[0] || leftChild == index[1]);
  832. if (leftChild == index[0]) {
  833. fminus = map[0];
  834. } else {
  835. fminus = map[1];
  836. }
  837. }
  838. /* Find f= */
  839. middleChild = j << 1;
  840. if (middleChild >= kTrueLbLower || middleChild <= kTrueUbLower) {
  841. fequal = one;
  842. } else if (i == 1 && middleChild == kFalse) {
  843. fequal = zero;
  844. } else {
  845. assert(middleChild == index[0] || middleChild == index[1]);
  846. if (middleChild == index[0]) {
  847. fequal = map[0];
  848. } else {
  849. fequal = map[1];
  850. }
  851. }
  852. /* Find f+ */
  853. rightChild = (j << 1) + 1;
  854. if (rightChild >= kTrueLbLower || rightChild <= kTrueUbLower) {
  855. fplus = one;
  856. } else if (i == 1 && rightChild == kFalse) {
  857. fplus = zero;
  858. } else {
  859. assert(rightChild == index[0] || rightChild == index[1]);
  860. if (rightChild == index[0]) {
  861. fplus = map[0];
  862. } else {
  863. fplus = map[1];
  864. }
  865. }
  866. /* Build new nodes. */
  867. g1 = Cudd_bddIte(dd, y[N - i], fequal, fplus);
  868. if (g1 == NULL) {
  869. if (index[0] != invalidIndex) Cudd_IterDerefBdd(dd, map[0]);
  870. if (index[1] != invalidIndex) Cudd_IterDerefBdd(dd, map[1]);
  871. if (newIndex[0] != invalidIndex) Cudd_IterDerefBdd(dd, newMap[0]);
  872. if (newIndex[1] != invalidIndex) Cudd_IterDerefBdd(dd, newMap[1]);
  873. return(NULL);
  874. }
  875. cuddRef(g1);
  876. g0 = Cudd_bddIte(dd, y[N - i], fminus, fequal);
  877. if (g0 == NULL) {
  878. Cudd_IterDerefBdd(dd, g1);
  879. if (index[0] != invalidIndex) Cudd_IterDerefBdd(dd, map[0]);
  880. if (index[1] != invalidIndex) Cudd_IterDerefBdd(dd, map[1]);
  881. if (newIndex[0] != invalidIndex) Cudd_IterDerefBdd(dd, newMap[0]);
  882. if (newIndex[1] != invalidIndex) Cudd_IterDerefBdd(dd, newMap[1]);
  883. return(NULL);
  884. }
  885. cuddRef(g0);
  886. f = Cudd_bddIte(dd, x[N - i], g1, g0);
  887. if (f == NULL) {
  888. Cudd_IterDerefBdd(dd, g1);
  889. Cudd_IterDerefBdd(dd, g0);
  890. if (index[0] != invalidIndex) Cudd_IterDerefBdd(dd, map[0]);
  891. if (index[1] != invalidIndex) Cudd_IterDerefBdd(dd, map[1]);
  892. if (newIndex[0] != invalidIndex) Cudd_IterDerefBdd(dd, newMap[0]);
  893. if (newIndex[1] != invalidIndex) Cudd_IterDerefBdd(dd, newMap[1]);
  894. return(NULL);
  895. }
  896. cuddRef(f);
  897. Cudd_IterDerefBdd(dd, g1);
  898. Cudd_IterDerefBdd(dd, g0);
  899. /* Save newly computed node in map. */
  900. assert(newIndex[0] == invalidIndex || newIndex[1] == invalidIndex);
  901. if (newIndex[0] == invalidIndex) {
  902. newIndex[0] = j;
  903. newMap[0] = f;
  904. } else {
  905. newIndex[1] = j;
  906. newMap[1] = f;
  907. }
  908. }
  909. /* Copy new map to map. */
  910. if (index[0] != invalidIndex) Cudd_IterDerefBdd(dd, map[0]);
  911. if (index[1] != invalidIndex) Cudd_IterDerefBdd(dd, map[1]);
  912. map[0] = newMap[0];
  913. map[1] = newMap[1];
  914. index[0] = newIndex[0];
  915. index[1] = newIndex[1];
  916. }
  917. cuddDeref(f);
  918. return(f);
  919. } /* end of Cudd_Disequality */
  920. /**
  921. @brief Generates a %BDD for the function lowerB &le; x &le; upperB.
  922. @details This function generates a %BDD for the function
  923. lowerB &le; x &le; upperB, where x is an N-bit number,
  924. x\[0\] x\[1\] ... x\[N-1\], with 0 the most significant bit (important!).
  925. The number of variables N should be sufficient to represent the bounds;
  926. otherwise, the bounds are truncated to their N least significant bits.
  927. Two BDDs are built bottom-up for lowerB &le; x and x &le; upperB, and they
  928. are finally conjoined.
  929. @sideeffect None
  930. @see Cudd_Xgty
  931. */
  932. DdNode *
  933. Cudd_bddInterval(
  934. DdManager * dd /**< %DD manager */,
  935. int N /**< number of x variables */,
  936. DdNode ** x /**< array of x variables */,
  937. unsigned int lowerB /**< lower bound */,
  938. unsigned int upperB /**< upper bound */)
  939. {
  940. DdNode *one, *zero;
  941. DdNode *r, *rl, *ru;
  942. int i;
  943. one = DD_ONE(dd);
  944. zero = Cudd_Not(one);
  945. rl = one;
  946. cuddRef(rl);
  947. ru = one;
  948. cuddRef(ru);
  949. /* Loop to build the rest of the BDDs. */
  950. for (i = N-1; i >= 0; i--) {
  951. DdNode *vl, *vu;
  952. vl = Cudd_bddIte(dd, x[i],
  953. lowerB&1 ? rl : one,
  954. lowerB&1 ? zero : rl);
  955. if (vl == NULL) {
  956. Cudd_IterDerefBdd(dd, rl);
  957. Cudd_IterDerefBdd(dd, ru);
  958. return(NULL);
  959. }
  960. cuddRef(vl);
  961. Cudd_IterDerefBdd(dd, rl);
  962. rl = vl;
  963. lowerB >>= 1;
  964. vu = Cudd_bddIte(dd, x[i],
  965. upperB&1 ? ru : zero,
  966. upperB&1 ? one : ru);
  967. if (vu == NULL) {
  968. Cudd_IterDerefBdd(dd, rl);
  969. Cudd_IterDerefBdd(dd, ru);
  970. return(NULL);
  971. }
  972. cuddRef(vu);
  973. Cudd_IterDerefBdd(dd, ru);
  974. ru = vu;
  975. upperB >>= 1;
  976. }
  977. /* Conjoin the two bounds. */
  978. r = Cudd_bddAnd(dd, rl, ru);
  979. if (r == NULL) {
  980. Cudd_IterDerefBdd(dd, rl);
  981. Cudd_IterDerefBdd(dd, ru);
  982. return(NULL);
  983. }
  984. cuddRef(r);
  985. Cudd_IterDerefBdd(dd, rl);
  986. Cudd_IterDerefBdd(dd, ru);
  987. cuddDeref(r);
  988. return(r);
  989. } /* end of Cudd_bddInterval */
  990. /**
  991. @brief Computes the compatible projection of R w.r.t. cube Y.
  992. @details Computes the compatible projection of relation R with
  993. respect to cube Y. For a comparison between Cudd_CProjection and
  994. Cudd_PrioritySelect, see the documentation of the latter.
  995. @return a pointer to the c-projection if successful; NULL otherwise.
  996. @sideeffect None
  997. @see Cudd_PrioritySelect
  998. */
  999. DdNode *
  1000. Cudd_CProjection(
  1001. DdManager * dd,
  1002. DdNode * R,
  1003. DdNode * Y)
  1004. {
  1005. DdNode *res;
  1006. DdNode *support;
  1007. if (Cudd_CheckCube(dd,Y) == 0) {
  1008. (void) fprintf(dd->err,
  1009. "Error: The third argument of Cudd_CProjection should be a cube\n");
  1010. dd->errorCode = CUDD_INVALID_ARG;
  1011. return(NULL);
  1012. }
  1013. /* Compute the support of Y, which is used by the abstraction step
  1014. ** in cuddCProjectionRecur.
  1015. */
  1016. support = Cudd_Support(dd,Y);
  1017. if (support == NULL) return(NULL);
  1018. cuddRef(support);
  1019. do {
  1020. dd->reordered = 0;
  1021. res = cuddCProjectionRecur(dd,R,Y,support);
  1022. } while (dd->reordered == 1);
  1023. if (res == NULL) {
  1024. Cudd_RecursiveDeref(dd,support);
  1025. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  1026. dd->timeoutHandler(dd, dd->tohArg);
  1027. }
  1028. return(NULL);
  1029. }
  1030. cuddRef(res);
  1031. Cudd_RecursiveDeref(dd,support);
  1032. cuddDeref(res);
  1033. return(res);
  1034. } /* end of Cudd_CProjection */
  1035. /**
  1036. @brief Computes the Hamming distance %ADD.
  1037. @details The two vectors xVars and yVars identify the variables that
  1038. form the two arguments.
  1039. @return an %ADD that gives the Hamming distance between its two
  1040. arguments if successful; NULL otherwise.
  1041. @sideeffect None
  1042. */
  1043. DdNode *
  1044. Cudd_addHamming(
  1045. DdManager * dd,
  1046. DdNode ** xVars,
  1047. DdNode ** yVars,
  1048. int nVars)
  1049. {
  1050. DdNode *result,*tempBdd;
  1051. DdNode *tempAdd,*temp;
  1052. int i;
  1053. result = DD_ZERO(dd);
  1054. cuddRef(result);
  1055. for (i = 0; i < nVars; i++) {
  1056. tempBdd = Cudd_bddIte(dd,xVars[i],Cudd_Not(yVars[i]),yVars[i]);
  1057. if (tempBdd == NULL) {
  1058. Cudd_RecursiveDeref(dd,result);
  1059. return(NULL);
  1060. }
  1061. cuddRef(tempBdd);
  1062. tempAdd = Cudd_BddToAdd(dd,tempBdd);
  1063. if (tempAdd == NULL) {
  1064. Cudd_RecursiveDeref(dd,tempBdd);
  1065. Cudd_RecursiveDeref(dd,result);
  1066. return(NULL);
  1067. }
  1068. cuddRef(tempAdd);
  1069. Cudd_RecursiveDeref(dd,tempBdd);
  1070. temp = Cudd_addApply(dd,Cudd_addPlus,tempAdd,result);
  1071. if (temp == NULL) {
  1072. Cudd_RecursiveDeref(dd,tempAdd);
  1073. Cudd_RecursiveDeref(dd,result);
  1074. return(NULL);
  1075. }
  1076. cuddRef(temp);
  1077. Cudd_RecursiveDeref(dd,tempAdd);
  1078. Cudd_RecursiveDeref(dd,result);
  1079. result = temp;
  1080. }
  1081. cuddDeref(result);
  1082. return(result);
  1083. } /* end of Cudd_addHamming */
  1084. /**
  1085. @brief Returns the minimum Hamming distance between f and minterm.
  1086. @details Returns the minimum Hamming distance between the
  1087. minterms of a function f and a reference minterm. The function is
  1088. given as a %BDD; the minterm is given as an array of integers, one
  1089. for each variable in the manager.
  1090. @return the minimum distance if it is less than the upper bound; the
  1091. upper bound if the minimum distance is at least as large;
  1092. CUDD_OUT_OF_MEM in case of failure.
  1093. @sideeffect None
  1094. @see Cudd_addHamming Cudd_bddClosestCube
  1095. */
  1096. int
  1097. Cudd_MinHammingDist(
  1098. DdManager *dd /**< %DD manager */,
  1099. DdNode *f /**< function to examine */,
  1100. int *minterm /**< reference minterm */,
  1101. int upperBound /**< distance above which an approximate answer is OK */)
  1102. {
  1103. DdHashTable *table;
  1104. CUDD_VALUE_TYPE epsilon;
  1105. int res;
  1106. table = cuddHashTableInit(dd,1,2);
  1107. if (table == NULL) {
  1108. return(CUDD_OUT_OF_MEM);
  1109. }
  1110. epsilon = Cudd_ReadEpsilon(dd);
  1111. Cudd_SetEpsilon(dd,(CUDD_VALUE_TYPE)0.0);
  1112. res = cuddMinHammingDistRecur(f,minterm,table,upperBound);
  1113. cuddHashTableQuit(table);
  1114. Cudd_SetEpsilon(dd,epsilon);
  1115. return(res);
  1116. } /* end of Cudd_MinHammingDist */
  1117. /**
  1118. @brief Finds a cube of f at minimum Hamming distance from the minterms of g.
  1119. @details All the minterms of the cube are at the minimum distance.
  1120. If the distance is 0, the cube belongs to the intersection of f and
  1121. g.
  1122. @return the cube if successful; NULL otherwise.
  1123. @sideeffect The distance is returned as a side effect.
  1124. @see Cudd_MinHammingDist
  1125. */
  1126. DdNode *
  1127. Cudd_bddClosestCube(
  1128. DdManager *dd,
  1129. DdNode * f,
  1130. DdNode *g,
  1131. int *distance)
  1132. {
  1133. DdNode *res, *acube;
  1134. CUDD_VALUE_TYPE rdist;
  1135. /* Compute the cube and distance as a single ADD. */
  1136. do {
  1137. dd->reordered = 0;
  1138. res = cuddBddClosestCube(dd,f,g,CUDD_CONST_INDEX + 1.0);
  1139. } while (dd->reordered == 1);
  1140. if (res == NULL) {
  1141. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  1142. dd->timeoutHandler(dd, dd->tohArg);
  1143. }
  1144. return(NULL);
  1145. }
  1146. cuddRef(res);
  1147. /* Unpack distance and cube. */
  1148. do {
  1149. dd->reordered = 0;
  1150. acube = separateCube(dd, res, &rdist);
  1151. } while (dd->reordered == 1);
  1152. if (acube == NULL) {
  1153. Cudd_RecursiveDeref(dd, res);
  1154. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  1155. dd->timeoutHandler(dd, dd->tohArg);
  1156. }
  1157. return(NULL);
  1158. }
  1159. cuddRef(acube);
  1160. Cudd_RecursiveDeref(dd, res);
  1161. /* Convert cube from ADD to BDD. */
  1162. do {
  1163. dd->reordered = 0;
  1164. res = cuddAddBddDoPattern(dd, acube);
  1165. } while (dd->reordered == 1);
  1166. if (res == NULL) {
  1167. Cudd_RecursiveDeref(dd, acube);
  1168. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  1169. dd->timeoutHandler(dd, dd->tohArg);
  1170. }
  1171. return(NULL);
  1172. }
  1173. cuddRef(res);
  1174. Cudd_RecursiveDeref(dd, acube);
  1175. *distance = (int) rdist;
  1176. cuddDeref(res);
  1177. return(res);
  1178. } /* end of Cudd_bddClosestCube */
  1179. /*---------------------------------------------------------------------------*/
  1180. /* Definition of internal functions */
  1181. /*---------------------------------------------------------------------------*/
  1182. /**
  1183. @brief Performs the recursive step of Cudd_CProjection.
  1184. @return the projection if successful; NULL otherwise.
  1185. @sideeffect None
  1186. @see Cudd_CProjection
  1187. */
  1188. DdNode *
  1189. cuddCProjectionRecur(
  1190. DdManager * dd,
  1191. DdNode * R,
  1192. DdNode * Y,
  1193. DdNode * Ysupp)
  1194. {
  1195. DdNode *res, *res1, *res2, *resA;
  1196. DdNode *r, *y, *RT, *RE, *YT, *YE, *Yrest, *Ra, *Ran, *Gamma, *Alpha;
  1197. int topR, topY, top;
  1198. unsigned int index;
  1199. DdNode *one = DD_ONE(dd);
  1200. statLine(dd);
  1201. if (Y == one) return(R);
  1202. #ifdef DD_DEBUG
  1203. assert(!Cudd_IsConstantInt(Y));
  1204. #endif
  1205. if (R == Cudd_Not(one)) return(R);
  1206. res = cuddCacheLookup2(dd, Cudd_CProjection, R, Y);
  1207. if (res != NULL) return(res);
  1208. checkWhetherToGiveUp(dd);
  1209. r = Cudd_Regular(R);
  1210. topR = cuddI(dd,r->index);
  1211. y = Cudd_Regular(Y);
  1212. topY = cuddI(dd,y->index);
  1213. top = ddMin(topR, topY);
  1214. /* Compute the cofactors of R */
  1215. index = r->index;
  1216. if (topR == top) {
  1217. RT = cuddT(r);
  1218. RE = cuddE(r);
  1219. if (r != R) {
  1220. RT = Cudd_Not(RT); RE = Cudd_Not(RE);
  1221. }
  1222. } else {
  1223. RT = RE = R;
  1224. }
  1225. if (topY > top) {
  1226. /* Y does not depend on the current top variable.
  1227. ** We just need to compute the results on the two cofactors of R
  1228. ** and make them the children of a node labeled r->index.
  1229. */
  1230. res1 = cuddCProjectionRecur(dd,RT,Y,Ysupp);
  1231. if (res1 == NULL) return(NULL);
  1232. cuddRef(res1);
  1233. res2 = cuddCProjectionRecur(dd,RE,Y,Ysupp);
  1234. if (res2 == NULL) {
  1235. Cudd_RecursiveDeref(dd,res1);
  1236. return(NULL);
  1237. }
  1238. cuddRef(res2);
  1239. res = cuddBddIteRecur(dd, dd->vars[index], res1, res2);
  1240. if (res == NULL) {
  1241. Cudd_RecursiveDeref(dd,res1);
  1242. Cudd_RecursiveDeref(dd,res2);
  1243. return(NULL);
  1244. }
  1245. /* If we have reached this point, res1 and res2 are now
  1246. ** incorporated in res. cuddDeref is therefore sufficient.
  1247. */
  1248. cuddDeref(res1);
  1249. cuddDeref(res2);
  1250. } else {
  1251. /* Compute the cofactors of Y */
  1252. index = y->index;
  1253. YT = cuddT(y);
  1254. YE = cuddE(y);
  1255. if (y != Y) {
  1256. YT = Cudd_Not(YT); YE = Cudd_Not(YE);
  1257. }
  1258. if (YT == Cudd_Not(one)) {
  1259. Alpha = Cudd_Not(dd->vars[index]);
  1260. Yrest = YE;
  1261. Ra = RE;
  1262. Ran = RT;
  1263. } else {
  1264. Alpha = dd->vars[index];
  1265. Yrest = YT;
  1266. Ra = RT;
  1267. Ran = RE;
  1268. }
  1269. Gamma = cuddBddExistAbstractRecur(dd,Ra,cuddT(Ysupp));
  1270. if (Gamma == NULL) return(NULL);
  1271. if (Gamma == one) {
  1272. res1 = cuddCProjectionRecur(dd,Ra,Yrest,cuddT(Ysupp));
  1273. if (res1 == NULL) return(NULL);
  1274. cuddRef(res1);
  1275. res = cuddBddAndRecur(dd, Alpha, res1);
  1276. if (res == NULL) {
  1277. Cudd_RecursiveDeref(dd,res1);
  1278. return(NULL);
  1279. }
  1280. cuddDeref(res1);
  1281. } else if (Gamma == Cudd_Not(one)) {
  1282. res1 = cuddCProjectionRecur(dd,Ran,Yrest,cuddT(Ysupp));
  1283. if (res1 == NULL) return(NULL);
  1284. cuddRef(res1);
  1285. res = cuddBddAndRecur(dd, Cudd_Not(Alpha), res1);
  1286. if (res == NULL) {
  1287. Cudd_RecursiveDeref(dd,res1);
  1288. return(NULL);
  1289. }
  1290. cuddDeref(res1);
  1291. } else {
  1292. cuddRef(Gamma);
  1293. resA = cuddCProjectionRecur(dd,Ran,Yrest,cuddT(Ysupp));
  1294. if (resA == NULL) {
  1295. Cudd_RecursiveDeref(dd,Gamma);
  1296. return(NULL);
  1297. }
  1298. cuddRef(resA);
  1299. res2 = cuddBddAndRecur(dd, Cudd_Not(Gamma), resA);
  1300. if (res2 == NULL) {
  1301. Cudd_RecursiveDeref(dd,Gamma);
  1302. Cudd_RecursiveDeref(dd,resA);
  1303. return(NULL);
  1304. }
  1305. cuddRef(res2);
  1306. Cudd_RecursiveDeref(dd,Gamma);
  1307. Cudd_RecursiveDeref(dd,resA);
  1308. res1 = cuddCProjectionRecur(dd,Ra,Yrest,cuddT(Ysupp));
  1309. if (res1 == NULL) {
  1310. Cudd_RecursiveDeref(dd,res2);
  1311. return(NULL);
  1312. }
  1313. cuddRef(res1);
  1314. res = cuddBddIteRecur(dd, Alpha, res1, res2);
  1315. if (res == NULL) {
  1316. Cudd_RecursiveDeref(dd,res1);
  1317. Cudd_RecursiveDeref(dd,res2);
  1318. return(NULL);
  1319. }
  1320. cuddDeref(res1);
  1321. cuddDeref(res2);
  1322. }
  1323. }
  1324. cuddCacheInsert2(dd,Cudd_CProjection,R,Y,res);
  1325. return(res);
  1326. } /* end of cuddCProjectionRecur */
  1327. /**
  1328. @brief Performs the recursive step of Cudd_bddClosestCube.
  1329. @details@parblock
  1330. The procedure uses a four-way recursion to examine all four combinations
  1331. of cofactors of <code>f</code> and <code>g</code> according to the
  1332. following formula.
  1333. H(f,g) = min(H(ft,gt), H(fe,ge), H(ft,ge)+1, H(fe,gt)+1)
  1334. Bounding is based on the following observations.
  1335. <ul>
  1336. <li> If we already found two points at distance 0, there is no point in
  1337. continuing. Furthermore,
  1338. <li> If F == not(G) then the best we can hope for is a minimum distance
  1339. of 1. If we have already found two points at distance 1, there is
  1340. no point in continuing. (Indeed, H(F,G) == 1 in this case. We
  1341. have to continue, though, to find the cube.)
  1342. </ul>
  1343. The variable <code>bound</code> is set at the largest value of the distance
  1344. that we are still interested in. Therefore, we desist when
  1345. (bound == -1) and (F != not(G)) or (bound == 0) and (F == not(G)).
  1346. If we were maximally aggressive in using the bound, we would always
  1347. set the bound to the minimum distance seen thus far minus one. That
  1348. is, we would maintain the invariant
  1349. bound < minD,
  1350. except at the very beginning, when we have no value for
  1351. <code>minD</code>.
  1352. However, we do not use <code>bound < minD</code> when examining the
  1353. two negative cofactors, because we try to find a large cube at
  1354. minimum distance. To do so, we try to find a cube in the negative
  1355. cofactors at the same or smaller distance from the cube found in the
  1356. positive cofactors.
  1357. When we compute <code>H(ft,ge)</code> and <code>H(fe,gt)</code> we
  1358. know that we are going to add 1 to the result of the recursive call
  1359. to account for the difference in the splitting variable. Therefore,
  1360. we decrease the bound correspondingly.
  1361. Another important observation concerns the need of examining all
  1362. four pairs of cofators only when both <code>f</code> and
  1363. <code>g</code> depend on the top variable.
  1364. Suppose <code>gt == ge == g</code>. (That is, <code>g</code> does
  1365. not depend on the top variable.) Then
  1366. H(f,g) = min(H(ft,g), H(fe,g), H(ft,g)+1, H(fe,g)+1)
  1367. = min(H(ft,g), H(fe,g)) .
  1368. Therefore, under these circumstances, we skip the two "cross" cases.
  1369. An interesting feature of this function is the scheme used for
  1370. caching the results in the global computed table. Since we have a
  1371. cube and a distance, we combine them to form an %ADD. The
  1372. combination replaces the zero child of the top node of the cube with
  1373. the negative of the distance. (The use of the negative is to avoid
  1374. ambiguity with 1.) The degenerate cases (zero and one) are treated
  1375. specially because the distance is known (0 for one, and infinity for
  1376. zero).
  1377. @endparblock
  1378. @return the cube if succesful; NULL otherwise.
  1379. @sideeffect None
  1380. @see Cudd_bddClosestCube
  1381. */
  1382. DdNode *
  1383. cuddBddClosestCube(
  1384. DdManager *dd,
  1385. DdNode *f,
  1386. DdNode *g,
  1387. CUDD_VALUE_TYPE bound)
  1388. {
  1389. DdNode *res, *F, *G, *ft, *fe, *gt, *ge, *tt, *ee;
  1390. DdNode *ctt, *cee, *cte, *cet;
  1391. CUDD_VALUE_TYPE minD, dtt, dee, dte, det;
  1392. DdNode *one = DD_ONE(dd);
  1393. DdNode *lzero = Cudd_Not(one);
  1394. DdNode *azero = DD_ZERO(dd);
  1395. int topf, topg;
  1396. unsigned int index;
  1397. statLine(dd);
  1398. if (bound < (f == Cudd_Not(g))) return(azero);
  1399. /* Terminal cases. */
  1400. if (g == lzero || f == lzero) return(azero);
  1401. if (f == one && g == one) return(one);
  1402. /* Check cache. */
  1403. F = Cudd_Regular(f);
  1404. G = Cudd_Regular(g);
  1405. if (F->ref != 1 || G->ref != 1) {
  1406. res = cuddCacheLookup2(dd,(DD_CTFP) Cudd_bddClosestCube, f, g);
  1407. if (res != NULL) return(res);
  1408. }
  1409. checkWhetherToGiveUp(dd);
  1410. topf = cuddI(dd,F->index);
  1411. topg = cuddI(dd,G->index);
  1412. /* Compute cofactors. */
  1413. if (topf <= topg) {
  1414. index = F->index;
  1415. ft = cuddT(F);
  1416. fe = cuddE(F);
  1417. if (Cudd_IsComplement(f)) {
  1418. ft = Cudd_Not(ft);
  1419. fe = Cudd_Not(fe);
  1420. }
  1421. } else {
  1422. index = G->index;
  1423. ft = fe = f;
  1424. }
  1425. if (topg <= topf) {
  1426. gt = cuddT(G);
  1427. ge = cuddE(G);
  1428. if (Cudd_IsComplement(g)) {
  1429. gt = Cudd_Not(gt);
  1430. ge = Cudd_Not(ge);
  1431. }
  1432. } else {
  1433. gt = ge = g;
  1434. }
  1435. tt = cuddBddClosestCube(dd,ft,gt,bound);
  1436. if (tt == NULL) return(NULL);
  1437. cuddRef(tt);
  1438. ctt = separateCube(dd,tt,&dtt);
  1439. if (ctt == NULL) {
  1440. Cudd_RecursiveDeref(dd, tt);
  1441. return(NULL);
  1442. }
  1443. cuddRef(ctt);
  1444. Cudd_RecursiveDeref(dd, tt);
  1445. minD = dtt;
  1446. bound = ddMin(bound,minD);
  1447. ee = cuddBddClosestCube(dd,fe,ge,bound);
  1448. if (ee == NULL) {
  1449. Cudd_RecursiveDeref(dd, ctt);
  1450. return(NULL);
  1451. }
  1452. cuddRef(ee);
  1453. cee = separateCube(dd,ee,&dee);
  1454. if (cee == NULL) {
  1455. Cudd_RecursiveDeref(dd, ctt);
  1456. Cudd_RecursiveDeref(dd, ee);
  1457. return(NULL);
  1458. }
  1459. cuddRef(cee);
  1460. Cudd_RecursiveDeref(dd, ee);
  1461. minD = ddMin(dtt, dee);
  1462. if (minD <= CUDD_CONST_INDEX) bound = ddMin(bound,minD-1);
  1463. if (minD > 0 && topf == topg) {
  1464. DdNode *te = cuddBddClosestCube(dd,ft,ge,bound-1);
  1465. if (te == NULL) {
  1466. Cudd_RecursiveDeref(dd, ctt);
  1467. Cudd_RecursiveDeref(dd, cee);
  1468. return(NULL);
  1469. }
  1470. cuddRef(te);
  1471. cte = separateCube(dd,te,&dte);
  1472. if (cte == NULL) {
  1473. Cudd_RecursiveDeref(dd, ctt);
  1474. Cudd_RecursiveDeref(dd, cee);
  1475. Cudd_RecursiveDeref(dd, te);
  1476. return(NULL);
  1477. }
  1478. cuddRef(cte);
  1479. Cudd_RecursiveDeref(dd, te);
  1480. dte += 1.0;
  1481. minD = ddMin(minD, dte);
  1482. } else {
  1483. cte = azero;
  1484. cuddRef(cte);
  1485. dte = CUDD_CONST_INDEX + 1.0;
  1486. }
  1487. if (minD <= CUDD_CONST_INDEX) bound = ddMin(bound,minD-1);
  1488. if (minD > 0 && topf == topg) {
  1489. DdNode *et = cuddBddClosestCube(dd,fe,gt,bound-1);
  1490. if (et == NULL) {
  1491. Cudd_RecursiveDeref(dd, ctt);
  1492. Cudd_RecursiveDeref(dd, cee);
  1493. Cudd_RecursiveDeref(dd, cte);
  1494. return(NULL);
  1495. }
  1496. cuddRef(et);
  1497. cet = separateCube(dd,et,&det);
  1498. if (cet == NULL) {
  1499. Cudd_RecursiveDeref(dd, ctt);
  1500. Cudd_RecursiveDeref(dd, cee);
  1501. Cudd_RecursiveDeref(dd, cte);
  1502. Cudd_RecursiveDeref(dd, et);
  1503. return(NULL);
  1504. }
  1505. cuddRef(cet);
  1506. Cudd_RecursiveDeref(dd, et);
  1507. det += 1.0;
  1508. minD = ddMin(minD, det);
  1509. } else {
  1510. cet = azero;
  1511. cuddRef(cet);
  1512. det = CUDD_CONST_INDEX + 1.0;
  1513. }
  1514. if (minD == dtt) {
  1515. if (dtt == dee && ctt == cee) {
  1516. res = createResult(dd,CUDD_CONST_INDEX,1,ctt,dtt);
  1517. } else {
  1518. res = createResult(dd,index,1,ctt,dtt);
  1519. }
  1520. } else if (minD == dee) {
  1521. res = createResult(dd,index,0,cee,dee);
  1522. } else if (minD == dte) {
  1523. #ifdef DD_DEBUG
  1524. assert(topf == topg);
  1525. #endif
  1526. res = createResult(dd,index,1,cte,dte);
  1527. } else {
  1528. #ifdef DD_DEBUG
  1529. assert(topf == topg);
  1530. #endif
  1531. res = createResult(dd,index,0,cet,det);
  1532. }
  1533. if (res == NULL) {
  1534. Cudd_RecursiveDeref(dd, ctt);
  1535. Cudd_RecursiveDeref(dd, cee);
  1536. Cudd_RecursiveDeref(dd, cte);
  1537. Cudd_RecursiveDeref(dd, cet);
  1538. return(NULL);
  1539. }
  1540. cuddRef(res);
  1541. Cudd_RecursiveDeref(dd, ctt);
  1542. Cudd_RecursiveDeref(dd, cee);
  1543. Cudd_RecursiveDeref(dd, cte);
  1544. Cudd_RecursiveDeref(dd, cet);
  1545. /* Only cache results that are different from azero to avoid
  1546. ** storing results that depend on the value of the bound. */
  1547. if ((F->ref != 1 || G->ref != 1) && res != azero)
  1548. cuddCacheInsert2(dd,(DD_CTFP) Cudd_bddClosestCube, f, g, res);
  1549. cuddDeref(res);
  1550. return(res);
  1551. } /* end of cuddBddClosestCube */
  1552. /*---------------------------------------------------------------------------*/
  1553. /* Definition of static functions */
  1554. /*---------------------------------------------------------------------------*/
  1555. /**
  1556. @brief Performs the recursive step of Cudd_MinHammingDist.
  1557. @details It is based on the following identity. Let H(f) be the
  1558. minimum Hamming distance of the minterms of f from the reference
  1559. minterm. Then:
  1560. H(f) = min(H(f0)+h0,H(f1)+h1)
  1561. where f0 and f1 are the two cofactors of f with respect to its top
  1562. variable; h0 is 1 if the minterm assigns 1 to the top variable of f;
  1563. h1 is 1 if the minterm assigns 0 to the top variable of f.
  1564. The upper bound on the distance is used to bound the depth of the
  1565. recursion.
  1566. @return the minimum distance unless it exceeds the upper bound or
  1567. computation fails.
  1568. @sideeffect None
  1569. @see Cudd_MinHammingDist
  1570. */
  1571. static int
  1572. cuddMinHammingDistRecur(
  1573. DdNode * f,
  1574. int *minterm,
  1575. DdHashTable * table,
  1576. int upperBound)
  1577. {
  1578. DdNode *F, *Ft, *Fe;
  1579. double h, hT, hE;
  1580. DdNode *zero, *res;
  1581. DdManager *dd = table->manager;
  1582. statLine(dd);
  1583. if (upperBound == 0) return(0);
  1584. F = Cudd_Regular(f);
  1585. if (cuddIsConstant(F)) {
  1586. zero = Cudd_Not(DD_ONE(dd));
  1587. if (f == dd->background || f == zero) {
  1588. return(upperBound);
  1589. } else {
  1590. return(0);
  1591. }
  1592. }
  1593. if ((res = cuddHashTableLookup1(table,f)) != NULL) {
  1594. h = cuddV(res);
  1595. if (res->ref == 0) {
  1596. dd->dead++;
  1597. dd->constants.dead++;
  1598. }
  1599. return((int) h);
  1600. }
  1601. Ft = cuddT(F); Fe = cuddE(F);
  1602. if (Cudd_IsComplement(f)) {
  1603. Ft = Cudd_Not(Ft); Fe = Cudd_Not(Fe);
  1604. }
  1605. if (minterm[F->index] == 0) {
  1606. DdNode *temp = Ft;
  1607. Ft = Fe; Fe = temp;
  1608. }
  1609. hT = cuddMinHammingDistRecur(Ft,minterm,table,upperBound);
  1610. if (hT == CUDD_OUT_OF_MEM) return(CUDD_OUT_OF_MEM);
  1611. if (hT == 0) {
  1612. hE = upperBound;
  1613. } else {
  1614. hE = cuddMinHammingDistRecur(Fe,minterm,table,upperBound - 1);
  1615. if (hE == CUDD_OUT_OF_MEM) return(CUDD_OUT_OF_MEM);
  1616. }
  1617. h = ddMin(hT, hE + 1);
  1618. if (F->ref != 1) {
  1619. ptrint fanout = (ptrint) F->ref;
  1620. cuddSatDec(fanout);
  1621. res = cuddUniqueConst(dd, (CUDD_VALUE_TYPE) h);
  1622. if (!cuddHashTableInsert1(table,f,res,fanout)) {
  1623. cuddRef(res); Cudd_RecursiveDeref(dd, res);
  1624. return(CUDD_OUT_OF_MEM);
  1625. }
  1626. }
  1627. return((int) h);
  1628. } /* end of cuddMinHammingDistRecur */
  1629. /**
  1630. @brief Separates cube from distance.
  1631. @return the cube if successful; NULL otherwise.
  1632. @sideeffect The distance is returned as a side effect.
  1633. @see cuddBddClosestCube createResult
  1634. */
  1635. static DdNode *
  1636. separateCube(
  1637. DdManager *dd,
  1638. DdNode *f,
  1639. CUDD_VALUE_TYPE *distance)
  1640. {
  1641. DdNode *cube, *t;
  1642. /* One and zero are special cases because the distance is implied. */
  1643. if (Cudd_IsConstantInt(f)) {
  1644. *distance = (f == DD_ONE(dd)) ? 0.0 :
  1645. (1.0 + (CUDD_VALUE_TYPE) CUDD_CONST_INDEX);
  1646. return(f);
  1647. }
  1648. /* Find out which branch points to the distance and replace the top
  1649. ** node with one pointing to zero instead. */
  1650. t = cuddT(f);
  1651. if (Cudd_IsConstantInt(t) && cuddV(t) <= 0) {
  1652. #ifdef DD_DEBUG
  1653. assert(!Cudd_IsConstantInt(cuddE(f)) || cuddE(f) == DD_ONE(dd));
  1654. #endif
  1655. *distance = -cuddV(t);
  1656. cube = cuddUniqueInter(dd, f->index, DD_ZERO(dd), cuddE(f));
  1657. } else {
  1658. #ifdef DD_DEBUG
  1659. assert(!Cudd_IsConstantInt(t) || t == DD_ONE(dd));
  1660. #endif
  1661. *distance = -cuddV(cuddE(f));
  1662. cube = cuddUniqueInter(dd, f->index, t, DD_ZERO(dd));
  1663. }
  1664. return(cube);
  1665. } /* end of separateCube */
  1666. /**
  1667. @brief Builds a result for cache storage.
  1668. @return a pointer to the resulting %ADD if successful; NULL
  1669. otherwise.
  1670. @sideeffect None
  1671. @see cuddBddClosestCube separateCube
  1672. */
  1673. static DdNode *
  1674. createResult(
  1675. DdManager *dd,
  1676. unsigned int index,
  1677. unsigned int phase,
  1678. DdNode *cube,
  1679. CUDD_VALUE_TYPE distance)
  1680. {
  1681. DdNode *res, *constant;
  1682. /* Special case. The cube is either one or zero, and we do not
  1683. ** add any variables. Hence, the result is also one or zero,
  1684. ** and the distance remains implied by the value of the constant. */
  1685. if (index == CUDD_CONST_INDEX && Cudd_IsConstantInt(cube)) return(cube);
  1686. constant = cuddUniqueConst(dd,-distance);
  1687. if (constant == NULL) return(NULL);
  1688. cuddRef(constant);
  1689. if (index == CUDD_CONST_INDEX) {
  1690. /* Replace the top node. */
  1691. if (cuddT(cube) == DD_ZERO(dd)) {
  1692. res = cuddUniqueInter(dd,cube->index,constant,cuddE(cube));
  1693. } else {
  1694. res = cuddUniqueInter(dd,cube->index,cuddT(cube),constant);
  1695. }
  1696. } else {
  1697. /* Add a new top node. */
  1698. #ifdef DD_DEBUG
  1699. assert(cuddI(dd,index) < cuddI(dd,cube->index));
  1700. #endif
  1701. if (phase) {
  1702. res = cuddUniqueInter(dd,index,cube,constant);
  1703. } else {
  1704. res = cuddUniqueInter(dd,index,constant,cube);
  1705. }
  1706. }
  1707. if (res == NULL) {
  1708. Cudd_RecursiveDeref(dd, constant);
  1709. return(NULL);
  1710. }
  1711. cuddDeref(constant); /* safe because constant is part of res */
  1712. return(res);
  1713. } /* end of createResult */