2027 lines
58 KiB

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