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.

1997 lines
54 KiB

3 months ago
  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 = NULL;
  1134. CUDD_VALUE_TYPE rdist = DD_PLUS_INF_VAL;
  1135. CUDD_VALUE_TYPE epsilon = Cudd_ReadEpsilon(dd);
  1136. do {
  1137. /* Compute the cube and distance as a single ADD. */
  1138. Cudd_SetEpsilon(dd,(CUDD_VALUE_TYPE)0.0);
  1139. dd->reordered = 0;
  1140. res = cuddBddClosestCube(dd,f,g,CUDD_CONST_INDEX + 1.0);
  1141. Cudd_SetEpsilon(dd,epsilon);
  1142. if (dd->reordered == 0) {
  1143. if (res == NULL) {
  1144. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  1145. dd->timeoutHandler(dd, dd->tohArg);
  1146. }
  1147. return(NULL);
  1148. }
  1149. cuddRef(res);
  1150. /* Unpack distance and cube. */
  1151. acube = separateCube(dd, res, &rdist);
  1152. Cudd_RecursiveDeref(dd, res);
  1153. }
  1154. } while (dd->reordered == 1);
  1155. if (acube == NULL) {
  1156. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  1157. dd->timeoutHandler(dd, dd->tohArg);
  1158. }
  1159. return(NULL);
  1160. }
  1161. cuddRef(acube);
  1162. /* Convert cube from ADD to BDD. */
  1163. do {
  1164. dd->reordered = 0;
  1165. res = cuddAddBddDoPattern(dd, acube);
  1166. } while (dd->reordered == 1);
  1167. if (res == NULL) {
  1168. Cudd_RecursiveDeref(dd, acube);
  1169. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  1170. dd->timeoutHandler(dd, dd->tohArg);
  1171. }
  1172. return(NULL);
  1173. }
  1174. cuddRef(res);
  1175. Cudd_RecursiveDeref(dd, acube);
  1176. *distance = (int) rdist;
  1177. cuddDeref(res);
  1178. return(res);
  1179. } /* end of Cudd_bddClosestCube */
  1180. /*---------------------------------------------------------------------------*/
  1181. /* Definition of internal functions */
  1182. /*---------------------------------------------------------------------------*/
  1183. /**
  1184. @brief Performs the recursive step of Cudd_CProjection.
  1185. @return the projection if successful; NULL otherwise.
  1186. @sideeffect None
  1187. @see Cudd_CProjection
  1188. */
  1189. DdNode *
  1190. cuddCProjectionRecur(
  1191. DdManager * dd,
  1192. DdNode * R,
  1193. DdNode * Y,
  1194. DdNode * Ysupp)
  1195. {
  1196. DdNode *res, *res1, *res2, *resA;
  1197. DdNode *r, *y, *RT, *RE, *YT, *YE, *Yrest, *Ra, *Ran, *Gamma, *Alpha;
  1198. int topR, topY, top;
  1199. unsigned int index;
  1200. DdNode *one = DD_ONE(dd);
  1201. statLine(dd);
  1202. if (Y == one) return(R);
  1203. #ifdef DD_DEBUG
  1204. assert(!Cudd_IsConstantInt(Y));
  1205. #endif
  1206. if (R == Cudd_Not(one)) return(R);
  1207. res = cuddCacheLookup2(dd, Cudd_CProjection, R, Y);
  1208. if (res != NULL) return(res);
  1209. checkWhetherToGiveUp(dd);
  1210. r = Cudd_Regular(R);
  1211. topR = cuddI(dd,r->index);
  1212. y = Cudd_Regular(Y);
  1213. topY = cuddI(dd,y->index);
  1214. top = ddMin(topR, topY);
  1215. /* Compute the cofactors of R */
  1216. index = r->index;
  1217. if (topR == top) {
  1218. RT = cuddT(r);
  1219. RE = cuddE(r);
  1220. if (r != R) {
  1221. RT = Cudd_Not(RT); RE = Cudd_Not(RE);
  1222. }
  1223. } else {
  1224. RT = RE = R;
  1225. }
  1226. if (topY > top) {
  1227. /* Y does not depend on the current top variable.
  1228. ** We just need to compute the results on the two cofactors of R
  1229. ** and make them the children of a node labeled r->index.
  1230. */
  1231. res1 = cuddCProjectionRecur(dd,RT,Y,Ysupp);
  1232. if (res1 == NULL) return(NULL);
  1233. cuddRef(res1);
  1234. res2 = cuddCProjectionRecur(dd,RE,Y,Ysupp);
  1235. if (res2 == NULL) {
  1236. Cudd_RecursiveDeref(dd,res1);
  1237. return(NULL);
  1238. }
  1239. cuddRef(res2);
  1240. res = cuddBddIteRecur(dd, dd->vars[index], res1, res2);
  1241. if (res == NULL) {
  1242. Cudd_RecursiveDeref(dd,res1);
  1243. Cudd_RecursiveDeref(dd,res2);
  1244. return(NULL);
  1245. }
  1246. /* If we have reached this point, res1 and res2 are now
  1247. ** incorporated in res. cuddDeref is therefore sufficient.
  1248. */
  1249. cuddDeref(res1);
  1250. cuddDeref(res2);
  1251. } else {
  1252. /* Compute the cofactors of Y */
  1253. index = y->index;
  1254. YT = cuddT(y);
  1255. YE = cuddE(y);
  1256. if (y != Y) {
  1257. YT = Cudd_Not(YT); YE = Cudd_Not(YE);
  1258. }
  1259. if (YT == Cudd_Not(one)) {
  1260. Alpha = Cudd_Not(dd->vars[index]);
  1261. Yrest = YE;
  1262. Ra = RE;
  1263. Ran = RT;
  1264. } else {
  1265. Alpha = dd->vars[index];
  1266. Yrest = YT;
  1267. Ra = RT;
  1268. Ran = RE;
  1269. }
  1270. Gamma = cuddBddExistAbstractRecur(dd,Ra,cuddT(Ysupp));
  1271. if (Gamma == NULL) return(NULL);
  1272. if (Gamma == one) {
  1273. res1 = cuddCProjectionRecur(dd,Ra,Yrest,cuddT(Ysupp));
  1274. if (res1 == NULL) return(NULL);
  1275. cuddRef(res1);
  1276. res = cuddBddAndRecur(dd, Alpha, res1);
  1277. if (res == NULL) {
  1278. Cudd_RecursiveDeref(dd,res1);
  1279. return(NULL);
  1280. }
  1281. cuddDeref(res1);
  1282. } else if (Gamma == Cudd_Not(one)) {
  1283. res1 = cuddCProjectionRecur(dd,Ran,Yrest,cuddT(Ysupp));
  1284. if (res1 == NULL) return(NULL);
  1285. cuddRef(res1);
  1286. res = cuddBddAndRecur(dd, Cudd_Not(Alpha), res1);
  1287. if (res == NULL) {
  1288. Cudd_RecursiveDeref(dd,res1);
  1289. return(NULL);
  1290. }
  1291. cuddDeref(res1);
  1292. } else {
  1293. cuddRef(Gamma);
  1294. resA = cuddCProjectionRecur(dd,Ran,Yrest,cuddT(Ysupp));
  1295. if (resA == NULL) {
  1296. Cudd_RecursiveDeref(dd,Gamma);
  1297. return(NULL);
  1298. }
  1299. cuddRef(resA);
  1300. res2 = cuddBddAndRecur(dd, Cudd_Not(Gamma), resA);
  1301. if (res2 == NULL) {
  1302. Cudd_RecursiveDeref(dd,Gamma);
  1303. Cudd_RecursiveDeref(dd,resA);
  1304. return(NULL);
  1305. }
  1306. cuddRef(res2);
  1307. Cudd_RecursiveDeref(dd,Gamma);
  1308. Cudd_RecursiveDeref(dd,resA);
  1309. res1 = cuddCProjectionRecur(dd,Ra,Yrest,cuddT(Ysupp));
  1310. if (res1 == NULL) {
  1311. Cudd_RecursiveDeref(dd,res2);
  1312. return(NULL);
  1313. }
  1314. cuddRef(res1);
  1315. res = cuddBddIteRecur(dd, Alpha, res1, res2);
  1316. if (res == NULL) {
  1317. Cudd_RecursiveDeref(dd,res1);
  1318. Cudd_RecursiveDeref(dd,res2);
  1319. return(NULL);
  1320. }
  1321. cuddDeref(res1);
  1322. cuddDeref(res2);
  1323. }
  1324. }
  1325. cuddCacheInsert2(dd,Cudd_CProjection,R,Y,res);
  1326. return(res);
  1327. } /* end of cuddCProjectionRecur */
  1328. /**
  1329. @brief Performs the recursive step of Cudd_bddClosestCube.
  1330. @details@parblock
  1331. The procedure uses a four-way recursion to examine all four combinations
  1332. of cofactors of <code>f</code> and <code>g</code> according to the
  1333. following formula.
  1334. H(f,g) = min(H(ft,gt), H(fe,ge), H(ft,ge)+1, H(fe,gt)+1)
  1335. Bounding is based on the following observations.
  1336. <ul>
  1337. <li> If we already found two points at distance 0, there is no point in
  1338. continuing. Furthermore,
  1339. <li> If F == not(G) then the best we can hope for is a minimum distance
  1340. of 1. If we have already found two points at distance 1, there is
  1341. no point in continuing. (Indeed, H(F,G) == 1 in this case. We
  1342. have to continue, though, to find the cube.)
  1343. </ul>
  1344. The variable <code>bound</code> is set at the largest value of the distance
  1345. that we are still interested in. Therefore, we desist when
  1346. (bound == -1) and (F != not(G)) or (bound == 0) and (F == not(G)).
  1347. If we were maximally aggressive in using the bound, we would always
  1348. set the bound to the minimum distance seen thus far minus one. That
  1349. is, we would maintain the invariant
  1350. bound < minD,
  1351. except at the very beginning, when we have no value for
  1352. <code>minD</code>.
  1353. However, we do not use <code>bound < minD</code> when examining the
  1354. two negative cofactors, because we try to find a large cube at
  1355. minimum distance. To do so, we try to find a cube in the negative
  1356. cofactors at the same or smaller distance from the cube found in the
  1357. positive cofactors.
  1358. When we compute <code>H(ft,ge)</code> and <code>H(fe,gt)</code> we
  1359. know that we are going to add 1 to the result of the recursive call
  1360. to account for the difference in the splitting variable. Therefore,
  1361. we decrease the bound correspondingly.
  1362. Another important observation concerns the need of examining all
  1363. four pairs of cofators only when both <code>f</code> and
  1364. <code>g</code> depend on the top variable.
  1365. Suppose <code>gt == ge == g</code>. (That is, <code>g</code> does
  1366. not depend on the top variable.) Then
  1367. H(f,g) = min(H(ft,g), H(fe,g), H(ft,g)+1, H(fe,g)+1)
  1368. = min(H(ft,g), H(fe,g)) .
  1369. Therefore, under these circumstances, we skip the two "cross" cases.
  1370. An interesting feature of this function is the scheme used for
  1371. caching the results in the global computed table. Since we have a
  1372. cube and a distance, we combine them to form an %ADD. The
  1373. combination replaces the zero child of the top node of the cube with
  1374. the negative of the distance. (The use of the negative is to avoid
  1375. ambiguity with 1.) The degenerate cases (zero and one) are treated
  1376. specially because the distance is known (0 for one, and infinity for
  1377. zero).
  1378. @endparblock
  1379. @return the cube if succesful; NULL otherwise.
  1380. @sideeffect None
  1381. @see Cudd_bddClosestCube
  1382. */
  1383. DdNode *
  1384. cuddBddClosestCube(
  1385. DdManager *dd,
  1386. DdNode *f,
  1387. DdNode *g,
  1388. CUDD_VALUE_TYPE bound)
  1389. {
  1390. DdNode *res, *F, *G, *ft, *fe, *gt, *ge, *tt, *ee;
  1391. DdNode *ctt, *cee, *cte, *cet;
  1392. CUDD_VALUE_TYPE minD, dtt, dee, dte, det;
  1393. DdNode *one = DD_ONE(dd);
  1394. DdNode *lzero = Cudd_Not(one);
  1395. DdNode *azero = DD_ZERO(dd);
  1396. int topf, topg;
  1397. unsigned int index;
  1398. statLine(dd);
  1399. if (bound < (f == Cudd_Not(g))) return(azero);
  1400. /* Terminal cases. */
  1401. if (g == lzero || f == lzero) return(azero);
  1402. if (f == one && g == one) return(one);
  1403. /* Check cache. */
  1404. F = Cudd_Regular(f);
  1405. G = Cudd_Regular(g);
  1406. if (F->ref != 1 || G->ref != 1) {
  1407. res = cuddCacheLookup2(dd,(DD_CTFP) Cudd_bddClosestCube, f, g);
  1408. if (res != NULL) return(res);
  1409. }
  1410. checkWhetherToGiveUp(dd);
  1411. topf = cuddI(dd,F->index);
  1412. topg = cuddI(dd,G->index);
  1413. /* Compute cofactors. */
  1414. if (topf <= topg) {
  1415. index = F->index;
  1416. ft = cuddT(F);
  1417. fe = cuddE(F);
  1418. if (Cudd_IsComplement(f)) {
  1419. ft = Cudd_Not(ft);
  1420. fe = Cudd_Not(fe);
  1421. }
  1422. } else {
  1423. index = G->index;
  1424. ft = fe = f;
  1425. }
  1426. if (topg <= topf) {
  1427. gt = cuddT(G);
  1428. ge = cuddE(G);
  1429. if (Cudd_IsComplement(g)) {
  1430. gt = Cudd_Not(gt);
  1431. ge = Cudd_Not(ge);
  1432. }
  1433. } else {
  1434. gt = ge = g;
  1435. }
  1436. tt = cuddBddClosestCube(dd,ft,gt,bound);
  1437. if (tt == NULL) return(NULL);
  1438. cuddRef(tt);
  1439. ctt = separateCube(dd,tt,&dtt);
  1440. if (ctt == NULL) {
  1441. Cudd_RecursiveDeref(dd, tt);
  1442. return(NULL);
  1443. }
  1444. cuddRef(ctt);
  1445. Cudd_RecursiveDeref(dd, tt);
  1446. minD = dtt;
  1447. bound = ddMin(bound,minD);
  1448. ee = cuddBddClosestCube(dd,fe,ge,bound);
  1449. if (ee == NULL) {
  1450. Cudd_RecursiveDeref(dd, ctt);
  1451. return(NULL);
  1452. }
  1453. cuddRef(ee);
  1454. cee = separateCube(dd,ee,&dee);
  1455. if (cee == NULL) {
  1456. Cudd_RecursiveDeref(dd, ctt);
  1457. Cudd_RecursiveDeref(dd, ee);
  1458. return(NULL);
  1459. }
  1460. cuddRef(cee);
  1461. Cudd_RecursiveDeref(dd, ee);
  1462. minD = ddMin(dtt, dee);
  1463. if (minD <= CUDD_CONST_INDEX) bound = ddMin(bound,minD-1);
  1464. if (minD > 0 && topf == topg) {
  1465. DdNode *te = cuddBddClosestCube(dd,ft,ge,bound-1);
  1466. if (te == NULL) {
  1467. Cudd_RecursiveDeref(dd, ctt);
  1468. Cudd_RecursiveDeref(dd, cee);
  1469. return(NULL);
  1470. }
  1471. cuddRef(te);
  1472. cte = separateCube(dd,te,&dte);
  1473. if (cte == NULL) {
  1474. Cudd_RecursiveDeref(dd, ctt);
  1475. Cudd_RecursiveDeref(dd, cee);
  1476. Cudd_RecursiveDeref(dd, te);
  1477. return(NULL);
  1478. }
  1479. cuddRef(cte);
  1480. Cudd_RecursiveDeref(dd, te);
  1481. dte += 1.0;
  1482. minD = ddMin(minD, dte);
  1483. } else {
  1484. cte = azero;
  1485. cuddRef(cte);
  1486. dte = CUDD_CONST_INDEX + 1.0;
  1487. }
  1488. if (minD <= CUDD_CONST_INDEX) bound = ddMin(bound,minD-1);
  1489. if (minD > 0 && topf == topg) {
  1490. DdNode *et = cuddBddClosestCube(dd,fe,gt,bound-1);
  1491. if (et == NULL) {
  1492. Cudd_RecursiveDeref(dd, ctt);
  1493. Cudd_RecursiveDeref(dd, cee);
  1494. Cudd_RecursiveDeref(dd, cte);
  1495. return(NULL);
  1496. }
  1497. cuddRef(et);
  1498. cet = separateCube(dd,et,&det);
  1499. if (cet == NULL) {
  1500. Cudd_RecursiveDeref(dd, ctt);
  1501. Cudd_RecursiveDeref(dd, cee);
  1502. Cudd_RecursiveDeref(dd, cte);
  1503. Cudd_RecursiveDeref(dd, et);
  1504. return(NULL);
  1505. }
  1506. cuddRef(cet);
  1507. Cudd_RecursiveDeref(dd, et);
  1508. det += 1.0;
  1509. minD = ddMin(minD, det);
  1510. } else {
  1511. cet = azero;
  1512. cuddRef(cet);
  1513. det = CUDD_CONST_INDEX + 1.0;
  1514. }
  1515. if (minD == dtt) {
  1516. if (dtt == dee && ctt == cee) {
  1517. res = createResult(dd,CUDD_CONST_INDEX,1,ctt,dtt);
  1518. } else {
  1519. res = createResult(dd,index,1,ctt,dtt);
  1520. }
  1521. } else if (minD == dee) {
  1522. res = createResult(dd,index,0,cee,dee);
  1523. } else if (minD == dte) {
  1524. #ifdef DD_DEBUG
  1525. assert(topf == topg);
  1526. #endif
  1527. res = createResult(dd,index,1,cte,dte);
  1528. } else {
  1529. #ifdef DD_DEBUG
  1530. assert(topf == topg);
  1531. #endif
  1532. res = createResult(dd,index,0,cet,det);
  1533. }
  1534. if (res == NULL) {
  1535. Cudd_RecursiveDeref(dd, ctt);
  1536. Cudd_RecursiveDeref(dd, cee);
  1537. Cudd_RecursiveDeref(dd, cte);
  1538. Cudd_RecursiveDeref(dd, cet);
  1539. return(NULL);
  1540. }
  1541. cuddRef(res);
  1542. Cudd_RecursiveDeref(dd, ctt);
  1543. Cudd_RecursiveDeref(dd, cee);
  1544. Cudd_RecursiveDeref(dd, cte);
  1545. Cudd_RecursiveDeref(dd, cet);
  1546. /* Only cache results that are different from azero to avoid
  1547. ** storing results that depend on the value of the bound. */
  1548. if ((F->ref != 1 || G->ref != 1) && res != azero)
  1549. cuddCacheInsert2(dd,(DD_CTFP) Cudd_bddClosestCube, f, g, res);
  1550. cuddDeref(res);
  1551. return(res);
  1552. } /* end of cuddBddClosestCube */
  1553. /*---------------------------------------------------------------------------*/
  1554. /* Definition of static functions */
  1555. /*---------------------------------------------------------------------------*/
  1556. /**
  1557. @brief Performs the recursive step of Cudd_MinHammingDist.
  1558. @details It is based on the following identity. Let H(f) be the
  1559. minimum Hamming distance of the minterms of f from the reference
  1560. minterm. Then:
  1561. H(f) = min(H(f0)+h0,H(f1)+h1)
  1562. where f0 and f1 are the two cofactors of f with respect to its top
  1563. variable; h0 is 1 if the minterm assigns 1 to the top variable of f;
  1564. h1 is 1 if the minterm assigns 0 to the top variable of f.
  1565. The upper bound on the distance is used to bound the depth of the
  1566. recursion.
  1567. @return the minimum distance unless it exceeds the upper bound or
  1568. computation fails.
  1569. @sideeffect None
  1570. @see Cudd_MinHammingDist
  1571. */
  1572. static int
  1573. cuddMinHammingDistRecur(
  1574. DdNode * f,
  1575. int *minterm,
  1576. DdHashTable * table,
  1577. int upperBound)
  1578. {
  1579. DdNode *F, *Ft, *Fe;
  1580. double h, hT, hE;
  1581. DdNode *zero, *res;
  1582. DdManager *dd = table->manager;
  1583. statLine(dd);
  1584. if (upperBound == 0) return(0);
  1585. F = Cudd_Regular(f);
  1586. if (cuddIsConstant(F)) {
  1587. zero = Cudd_Not(DD_ONE(dd));
  1588. if (f == dd->background || f == zero) {
  1589. return(upperBound);
  1590. } else {
  1591. return(0);
  1592. }
  1593. }
  1594. if ((res = cuddHashTableLookup1(table,f)) != NULL) {
  1595. h = cuddV(res);
  1596. if (res->ref == 0) {
  1597. dd->dead++;
  1598. dd->constants.dead++;
  1599. }
  1600. return((int) h);
  1601. }
  1602. Ft = cuddT(F); Fe = cuddE(F);
  1603. if (Cudd_IsComplement(f)) {
  1604. Ft = Cudd_Not(Ft); Fe = Cudd_Not(Fe);
  1605. }
  1606. if (minterm[F->index] == 0) {
  1607. DdNode *temp = Ft;
  1608. Ft = Fe; Fe = temp;
  1609. }
  1610. hT = cuddMinHammingDistRecur(Ft,minterm,table,upperBound);
  1611. if (hT == CUDD_OUT_OF_MEM) return(CUDD_OUT_OF_MEM);
  1612. if (hT == 0) {
  1613. hE = upperBound;
  1614. } else {
  1615. hE = cuddMinHammingDistRecur(Fe,minterm,table,upperBound - 1);
  1616. if (hE == CUDD_OUT_OF_MEM) return(CUDD_OUT_OF_MEM);
  1617. }
  1618. h = ddMin(hT, hE + 1);
  1619. if (F->ref != 1) {
  1620. ptrint fanout = (ptrint) F->ref;
  1621. cuddSatDec(fanout);
  1622. res = cuddUniqueConst(dd, (CUDD_VALUE_TYPE) h);
  1623. if (!cuddHashTableInsert1(table,f,res,fanout)) {
  1624. cuddRef(res); Cudd_RecursiveDeref(dd, res);
  1625. return(CUDD_OUT_OF_MEM);
  1626. }
  1627. }
  1628. return((int) h);
  1629. } /* end of cuddMinHammingDistRecur */
  1630. /**
  1631. @brief Separates cube from distance.
  1632. @return the cube if successful; NULL otherwise.
  1633. @sideeffect The distance is returned as a side effect.
  1634. @see cuddBddClosestCube createResult
  1635. */
  1636. static DdNode *
  1637. separateCube(
  1638. DdManager *dd,
  1639. DdNode *f,
  1640. CUDD_VALUE_TYPE *distance)
  1641. {
  1642. DdNode *cube, *t;
  1643. /* One and zero are special cases because the distance is implied. */
  1644. if (Cudd_IsConstantInt(f)) {
  1645. *distance = (f == DD_ONE(dd)) ? 0.0 :
  1646. (1.0 + (CUDD_VALUE_TYPE) CUDD_CONST_INDEX);
  1647. return(f);
  1648. }
  1649. /* Find out which branch points to the distance and replace the top
  1650. ** node with one pointing to zero instead. */
  1651. t = cuddT(f);
  1652. if (Cudd_IsConstantInt(t) && cuddV(t) <= 0) {
  1653. #ifdef DD_DEBUG
  1654. assert(!Cudd_IsConstantInt(cuddE(f)) || cuddE(f) == DD_ONE(dd));
  1655. #endif
  1656. *distance = -cuddV(t);
  1657. cube = cuddUniqueInter(dd, f->index, DD_ZERO(dd), cuddE(f));
  1658. } else {
  1659. #ifdef DD_DEBUG
  1660. assert(!Cudd_IsConstantInt(t) || t == DD_ONE(dd));
  1661. #endif
  1662. *distance = -cuddV(cuddE(f));
  1663. cube = cuddUniqueInter(dd, f->index, t, DD_ZERO(dd));
  1664. }
  1665. return(cube);
  1666. } /* end of separateCube */
  1667. /**
  1668. @brief Builds a result for cache storage.
  1669. @return a pointer to the resulting %ADD if successful; NULL
  1670. otherwise.
  1671. @sideeffect None
  1672. @see cuddBddClosestCube separateCube
  1673. */
  1674. static DdNode *
  1675. createResult(
  1676. DdManager *dd,
  1677. unsigned int index,
  1678. unsigned int phase,
  1679. DdNode *cube,
  1680. CUDD_VALUE_TYPE distance)
  1681. {
  1682. DdNode *res, *constant;
  1683. /* Special case. The cube is either one or zero, and we do not
  1684. ** add any variables. Hence, the result is also one or zero,
  1685. ** and the distance remains implied by the value of the constant. */
  1686. if (index == CUDD_CONST_INDEX && Cudd_IsConstantInt(cube)) return(cube);
  1687. constant = cuddUniqueConst(dd,-distance);
  1688. if (constant == NULL) return(NULL);
  1689. cuddRef(constant);
  1690. if (index == CUDD_CONST_INDEX) {
  1691. /* Replace the top node. */
  1692. if (cuddT(cube) == DD_ZERO(dd)) {
  1693. res = cuddUniqueInter(dd,cube->index,constant,cuddE(cube));
  1694. } else {
  1695. res = cuddUniqueInter(dd,cube->index,cuddT(cube),constant);
  1696. }
  1697. } else {
  1698. /* Add a new top node. */
  1699. #ifdef DD_DEBUG
  1700. assert(cuddI(dd,index) < cuddI(dd,cube->index));
  1701. #endif
  1702. if (phase) {
  1703. res = cuddUniqueInter(dd,index,cube,constant);
  1704. } else {
  1705. res = cuddUniqueInter(dd,index,constant,cube);
  1706. }
  1707. }
  1708. if (res == NULL) {
  1709. Cudd_RecursiveDeref(dd, constant);
  1710. return(NULL);
  1711. }
  1712. cuddDeref(constant); /* safe because constant is part of res */
  1713. return(res);
  1714. } /* end of createResult */