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.

639 lines
17 KiB

  1. /**CFile***********************************************************************
  2. FileName [cuddAddIte.c]
  3. PackageName [cudd]
  4. Synopsis [ADD ITE function and satellites.]
  5. Description [External procedures included in this module:
  6. <ul>
  7. <li> Cudd_addIte()
  8. <li> Cudd_addIteConstant()
  9. <li> Cudd_addEvalConst()
  10. <li> Cudd_addCmpl()
  11. <li> Cudd_addLeq()
  12. </ul>
  13. Internal procedures included in this module:
  14. <ul>
  15. <li> cuddAddIteRecur()
  16. <li> cuddAddCmplRecur()
  17. </ul>
  18. Static procedures included in this module:
  19. <ul>
  20. <li> addVarToConst()
  21. </ul>]
  22. Author [Fabio Somenzi]
  23. Copyright [Copyright (c) 1995-2012, Regents of the University of Colorado
  24. All rights reserved.
  25. Redistribution and use in source and binary forms, with or without
  26. modification, are permitted provided that the following conditions
  27. are met:
  28. Redistributions of source code must retain the above copyright
  29. notice, this list of conditions and the following disclaimer.
  30. Redistributions in binary form must reproduce the above copyright
  31. notice, this list of conditions and the following disclaimer in the
  32. documentation and/or other materials provided with the distribution.
  33. Neither the name of the University of Colorado nor the names of its
  34. contributors may be used to endorse or promote products derived from
  35. this software without specific prior written permission.
  36. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  37. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  38. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  39. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  40. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  41. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  42. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  44. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  45. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  46. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  47. POSSIBILITY OF SUCH DAMAGE.]
  48. ******************************************************************************/
  49. #include "util.h"
  50. #include "cuddInt.h"
  51. /*---------------------------------------------------------------------------*/
  52. /* Constant declarations */
  53. /*---------------------------------------------------------------------------*/
  54. /*---------------------------------------------------------------------------*/
  55. /* Stucture declarations */
  56. /*---------------------------------------------------------------------------*/
  57. /*---------------------------------------------------------------------------*/
  58. /* Type declarations */
  59. /*---------------------------------------------------------------------------*/
  60. /*---------------------------------------------------------------------------*/
  61. /* Variable declarations */
  62. /*---------------------------------------------------------------------------*/
  63. #ifndef lint
  64. static char rcsid[] DD_UNUSED = "$Id: cuddAddIte.c,v 1.16 2012/02/05 01:07:18 fabio Exp $";
  65. #endif
  66. /*---------------------------------------------------------------------------*/
  67. /* Macro declarations */
  68. /*---------------------------------------------------------------------------*/
  69. /**AutomaticStart*************************************************************/
  70. /*---------------------------------------------------------------------------*/
  71. /* Static function prototypes */
  72. /*---------------------------------------------------------------------------*/
  73. static void addVarToConst (DdNode *f, DdNode **gp, DdNode **hp, DdNode *one, DdNode *zero);
  74. /**AutomaticEnd***************************************************************/
  75. /*---------------------------------------------------------------------------*/
  76. /* Definition of exported functions */
  77. /*---------------------------------------------------------------------------*/
  78. /**Function********************************************************************
  79. Synopsis [Implements ITE(f,g,h).]
  80. Description [Implements ITE(f,g,h). This procedure assumes that f is
  81. a 0-1 ADD. Returns a pointer to the resulting ADD if successful; NULL
  82. otherwise.]
  83. SideEffects [None]
  84. SeeAlso [Cudd_bddIte Cudd_addIteConstant Cudd_addApply]
  85. ******************************************************************************/
  86. DdNode *
  87. Cudd_addIte(
  88. DdManager * dd,
  89. DdNode * f,
  90. DdNode * g,
  91. DdNode * h)
  92. {
  93. DdNode *res;
  94. do {
  95. dd->reordered = 0;
  96. res = cuddAddIteRecur(dd,f,g,h);
  97. } while (dd->reordered == 1);
  98. return(res);
  99. } /* end of Cudd_addIte */
  100. /**Function********************************************************************
  101. Synopsis [Implements ITEconstant for ADDs.]
  102. Description [Implements ITEconstant for ADDs. f must be a 0-1 ADD.
  103. Returns a pointer to the resulting ADD (which may or may not be
  104. constant) or DD_NON_CONSTANT. No new nodes are created. This function
  105. can be used, for instance, to check that g has a constant value
  106. (specified by h) whenever f is 1. If the constant value is unknown,
  107. then one should use Cudd_addEvalConst.]
  108. SideEffects [None]
  109. SeeAlso [Cudd_addIte Cudd_addEvalConst Cudd_bddIteConstant]
  110. ******************************************************************************/
  111. DdNode *
  112. Cudd_addIteConstant(
  113. DdManager * dd,
  114. DdNode * f,
  115. DdNode * g,
  116. DdNode * h)
  117. {
  118. DdNode *one,*zero;
  119. DdNode *Fv,*Fnv,*Gv,*Gnv,*Hv,*Hnv,*r,*t,*e;
  120. unsigned int topf,topg,toph,v;
  121. statLine(dd);
  122. /* Trivial cases. */
  123. if (f == (one = DD_ONE(dd))) { /* ITE(1,G,H) = G */
  124. return(g);
  125. }
  126. if (f == (zero = DD_ZERO(dd))) { /* ITE(0,G,H) = H */
  127. return(h);
  128. }
  129. /* From now on, f is known not to be a constant. */
  130. addVarToConst(f,&g,&h,one,zero);
  131. /* Check remaining one variable cases. */
  132. if (g == h) { /* ITE(F,G,G) = G */
  133. return(g);
  134. }
  135. if (cuddIsConstant(g) && cuddIsConstant(h)) {
  136. return(DD_NON_CONSTANT);
  137. }
  138. topf = cuddI(dd,f->index);
  139. topg = cuddI(dd,g->index);
  140. toph = cuddI(dd,h->index);
  141. v = ddMin(topg,toph);
  142. /* ITE(F,G,H) = (x,G,H) (non constant) if F = (x,1,0), x < top(G,H). */
  143. if (topf < v && cuddIsConstant(cuddT(f)) && cuddIsConstant(cuddE(f))) {
  144. return(DD_NON_CONSTANT);
  145. }
  146. /* Check cache. */
  147. r = cuddConstantLookup(dd,DD_ADD_ITE_CONSTANT_TAG,f,g,h);
  148. if (r != NULL) {
  149. return(r);
  150. }
  151. /* Compute cofactors. */
  152. if (topf <= v) {
  153. v = ddMin(topf,v); /* v = top_var(F,G,H) */
  154. Fv = cuddT(f); Fnv = cuddE(f);
  155. } else {
  156. Fv = Fnv = f;
  157. }
  158. if (topg == v) {
  159. Gv = cuddT(g); Gnv = cuddE(g);
  160. } else {
  161. Gv = Gnv = g;
  162. }
  163. if (toph == v) {
  164. Hv = cuddT(h); Hnv = cuddE(h);
  165. } else {
  166. Hv = Hnv = h;
  167. }
  168. /* Recursive step. */
  169. t = Cudd_addIteConstant(dd,Fv,Gv,Hv);
  170. if (t == DD_NON_CONSTANT || !cuddIsConstant(t)) {
  171. cuddCacheInsert(dd, DD_ADD_ITE_CONSTANT_TAG, f, g, h, DD_NON_CONSTANT);
  172. return(DD_NON_CONSTANT);
  173. }
  174. e = Cudd_addIteConstant(dd,Fnv,Gnv,Hnv);
  175. if (e == DD_NON_CONSTANT || !cuddIsConstant(e) || t != e) {
  176. cuddCacheInsert(dd, DD_ADD_ITE_CONSTANT_TAG, f, g, h, DD_NON_CONSTANT);
  177. return(DD_NON_CONSTANT);
  178. }
  179. cuddCacheInsert(dd, DD_ADD_ITE_CONSTANT_TAG, f, g, h, t);
  180. return(t);
  181. } /* end of Cudd_addIteConstant */
  182. /**Function********************************************************************
  183. Synopsis [Checks whether ADD g is constant whenever ADD f is 1.]
  184. Description [Checks whether ADD g is constant whenever ADD f is 1. f
  185. must be a 0-1 ADD. Returns a pointer to the resulting ADD (which may
  186. or may not be constant) or DD_NON_CONSTANT. If f is identically 0,
  187. the check is assumed to be successful, and the background value is
  188. returned. No new nodes are created.]
  189. SideEffects [None]
  190. SeeAlso [Cudd_addIteConstant Cudd_addLeq]
  191. ******************************************************************************/
  192. DdNode *
  193. Cudd_addEvalConst(
  194. DdManager * dd,
  195. DdNode * f,
  196. DdNode * g)
  197. {
  198. DdNode *zero;
  199. DdNode *Fv,*Fnv,*Gv,*Gnv,*r,*t,*e;
  200. unsigned int topf,topg;
  201. #ifdef DD_DEBUG
  202. assert(!Cudd_IsComplement(f));
  203. #endif
  204. statLine(dd);
  205. /* Terminal cases. */
  206. if (f == DD_ONE(dd) || cuddIsConstant(g)) {
  207. return(g);
  208. }
  209. if (f == (zero = DD_ZERO(dd))) {
  210. return(dd->background);
  211. }
  212. #ifdef DD_DEBUG
  213. assert(!cuddIsConstant(f));
  214. #endif
  215. /* From now on, f and g are known not to be constants. */
  216. topf = cuddI(dd,f->index);
  217. topg = cuddI(dd,g->index);
  218. /* Check cache. */
  219. r = cuddConstantLookup(dd,DD_ADD_EVAL_CONST_TAG,f,g,g);
  220. if (r != NULL) {
  221. return(r);
  222. }
  223. /* Compute cofactors. */
  224. if (topf <= topg) {
  225. Fv = cuddT(f); Fnv = cuddE(f);
  226. } else {
  227. Fv = Fnv = f;
  228. }
  229. if (topg <= topf) {
  230. Gv = cuddT(g); Gnv = cuddE(g);
  231. } else {
  232. Gv = Gnv = g;
  233. }
  234. /* Recursive step. */
  235. if (Fv != zero) {
  236. t = Cudd_addEvalConst(dd,Fv,Gv);
  237. if (t == DD_NON_CONSTANT || !cuddIsConstant(t)) {
  238. cuddCacheInsert2(dd, Cudd_addEvalConst, f, g, DD_NON_CONSTANT);
  239. return(DD_NON_CONSTANT);
  240. }
  241. if (Fnv != zero) {
  242. e = Cudd_addEvalConst(dd,Fnv,Gnv);
  243. if (e == DD_NON_CONSTANT || !cuddIsConstant(e) || t != e) {
  244. cuddCacheInsert2(dd, Cudd_addEvalConst, f, g, DD_NON_CONSTANT);
  245. return(DD_NON_CONSTANT);
  246. }
  247. }
  248. cuddCacheInsert2(dd,Cudd_addEvalConst,f,g,t);
  249. return(t);
  250. } else { /* Fnv must be != zero */
  251. e = Cudd_addEvalConst(dd,Fnv,Gnv);
  252. cuddCacheInsert2(dd, Cudd_addEvalConst, f, g, e);
  253. return(e);
  254. }
  255. } /* end of Cudd_addEvalConst */
  256. /**Function********************************************************************
  257. Synopsis [Computes the complement of an ADD a la C language.]
  258. Description [Computes the complement of an ADD a la C language: The
  259. complement of 0 is 1 and the complement of everything else is 0.
  260. Returns a pointer to the resulting ADD if successful; NULL otherwise.]
  261. SideEffects [None]
  262. SeeAlso [Cudd_addNegate]
  263. ******************************************************************************/
  264. DdNode *
  265. Cudd_addCmpl(
  266. DdManager * dd,
  267. DdNode * f)
  268. {
  269. DdNode *res;
  270. do {
  271. dd->reordered = 0;
  272. res = cuddAddCmplRecur(dd,f);
  273. } while (dd->reordered == 1);
  274. return(res);
  275. } /* end of Cudd_addCmpl */
  276. /**Function********************************************************************
  277. Synopsis [Determines whether f is less than or equal to g.]
  278. Description [Returns 1 if f is less than or equal to g; 0 otherwise.
  279. No new nodes are created. This procedure works for arbitrary ADDs.
  280. For 0-1 ADDs Cudd_addEvalConst is more efficient.]
  281. SideEffects [None]
  282. SeeAlso [Cudd_addIteConstant Cudd_addEvalConst Cudd_bddLeq]
  283. ******************************************************************************/
  284. int
  285. Cudd_addLeq(
  286. DdManager * dd,
  287. DdNode * f,
  288. DdNode * g)
  289. {
  290. DdNode *tmp, *fv, *fvn, *gv, *gvn;
  291. unsigned int topf, topg, res;
  292. /* Terminal cases. */
  293. if (f == g) return(1);
  294. statLine(dd);
  295. if (cuddIsConstant(f)) {
  296. if (cuddIsConstant(g)) return(cuddV(f) <= cuddV(g));
  297. if (f == DD_MINUS_INFINITY(dd)) return(1);
  298. if (f == DD_PLUS_INFINITY(dd)) return(0); /* since f != g */
  299. }
  300. if (g == DD_PLUS_INFINITY(dd)) return(1);
  301. if (g == DD_MINUS_INFINITY(dd)) return(0); /* since f != g */
  302. /* Check cache. */
  303. tmp = cuddCacheLookup2(dd,(DD_CTFP)Cudd_addLeq,f,g);
  304. if (tmp != NULL) {
  305. return(tmp == DD_ONE(dd));
  306. }
  307. /* Compute cofactors. One of f and g is not constant. */
  308. topf = cuddI(dd,f->index);
  309. topg = cuddI(dd,g->index);
  310. if (topf <= topg) {
  311. fv = cuddT(f); fvn = cuddE(f);
  312. } else {
  313. fv = fvn = f;
  314. }
  315. if (topg <= topf) {
  316. gv = cuddT(g); gvn = cuddE(g);
  317. } else {
  318. gv = gvn = g;
  319. }
  320. res = Cudd_addLeq(dd,fvn,gvn) && Cudd_addLeq(dd,fv,gv);
  321. /* Store result in cache and return. */
  322. cuddCacheInsert2(dd,(DD_CTFP) Cudd_addLeq,f,g,
  323. Cudd_NotCond(DD_ONE(dd),res==0));
  324. return(res);
  325. } /* end of Cudd_addLeq */
  326. /*---------------------------------------------------------------------------*/
  327. /* Definition of internal functions */
  328. /*---------------------------------------------------------------------------*/
  329. /**Function********************************************************************
  330. Synopsis [Implements the recursive step of Cudd_addIte(f,g,h).]
  331. Description [Implements the recursive step of Cudd_addIte(f,g,h).
  332. Returns a pointer to the resulting ADD if successful; NULL
  333. otherwise.]
  334. SideEffects [None]
  335. SeeAlso [Cudd_addIte]
  336. ******************************************************************************/
  337. DdNode *
  338. cuddAddIteRecur(
  339. DdManager * dd,
  340. DdNode * f,
  341. DdNode * g,
  342. DdNode * h)
  343. {
  344. DdNode *one,*zero;
  345. DdNode *r,*Fv,*Fnv,*Gv,*Gnv,*Hv,*Hnv,*t,*e;
  346. unsigned int topf,topg,toph,v;
  347. int index;
  348. statLine(dd);
  349. /* Trivial cases. */
  350. /* One variable cases. */
  351. if (f == (one = DD_ONE(dd))) { /* ITE(1,G,H) = G */
  352. return(g);
  353. }
  354. if (f == (zero = DD_ZERO(dd))) { /* ITE(0,G,H) = H */
  355. return(h);
  356. }
  357. /* From now on, f is known to not be a constant. */
  358. addVarToConst(f,&g,&h,one,zero);
  359. /* Check remaining one variable cases. */
  360. if (g == h) { /* ITE(F,G,G) = G */
  361. return(g);
  362. }
  363. if (g == one) { /* ITE(F,1,0) = F */
  364. if (h == zero) return(f);
  365. }
  366. topf = cuddI(dd,f->index);
  367. topg = cuddI(dd,g->index);
  368. toph = cuddI(dd,h->index);
  369. v = ddMin(topg,toph);
  370. /* A shortcut: ITE(F,G,H) = (x,G,H) if F=(x,1,0), x < top(G,H). */
  371. if (topf < v && cuddT(f) == one && cuddE(f) == zero) {
  372. r = cuddUniqueInter(dd,(int)f->index,g,h);
  373. return(r);
  374. }
  375. if (topf < v && cuddT(f) == zero && cuddE(f) == one) {
  376. r = cuddUniqueInter(dd,(int)f->index,h,g);
  377. return(r);
  378. }
  379. /* Check cache. */
  380. r = cuddCacheLookup(dd,DD_ADD_ITE_TAG,f,g,h);
  381. if (r != NULL) {
  382. return(r);
  383. }
  384. /* Compute cofactors. */
  385. if (topf <= v) {
  386. v = ddMin(topf,v); /* v = top_var(F,G,H) */
  387. index = f->index;
  388. Fv = cuddT(f); Fnv = cuddE(f);
  389. } else {
  390. Fv = Fnv = f;
  391. }
  392. if (topg == v) {
  393. index = g->index;
  394. Gv = cuddT(g); Gnv = cuddE(g);
  395. } else {
  396. Gv = Gnv = g;
  397. }
  398. if (toph == v) {
  399. index = h->index;
  400. Hv = cuddT(h); Hnv = cuddE(h);
  401. } else {
  402. Hv = Hnv = h;
  403. }
  404. /* Recursive step. */
  405. t = cuddAddIteRecur(dd,Fv,Gv,Hv);
  406. if (t == NULL) return(NULL);
  407. cuddRef(t);
  408. e = cuddAddIteRecur(dd,Fnv,Gnv,Hnv);
  409. if (e == NULL) {
  410. Cudd_RecursiveDeref(dd,t);
  411. return(NULL);
  412. }
  413. cuddRef(e);
  414. r = (t == e) ? t : cuddUniqueInter(dd,index,t,e);
  415. if (r == NULL) {
  416. Cudd_RecursiveDeref(dd,t);
  417. Cudd_RecursiveDeref(dd,e);
  418. return(NULL);
  419. }
  420. cuddDeref(t);
  421. cuddDeref(e);
  422. cuddCacheInsert(dd,DD_ADD_ITE_TAG,f,g,h,r);
  423. return(r);
  424. } /* end of cuddAddIteRecur */
  425. /**Function********************************************************************
  426. Synopsis [Performs the recursive step of Cudd_addCmpl.]
  427. Description [Performs the recursive step of Cudd_addCmpl. Returns a
  428. pointer to the resulting ADD if successful; NULL otherwise.]
  429. SideEffects [None]
  430. SeeAlso [Cudd_addCmpl]
  431. ******************************************************************************/
  432. DdNode *
  433. cuddAddCmplRecur(
  434. DdManager * dd,
  435. DdNode * f)
  436. {
  437. DdNode *one,*zero;
  438. DdNode *r,*Fv,*Fnv,*t,*e;
  439. statLine(dd);
  440. one = DD_ONE(dd);
  441. zero = DD_ZERO(dd);
  442. if (cuddIsConstant(f)) {
  443. if (f == zero) {
  444. return(one);
  445. } else {
  446. return(zero);
  447. }
  448. }
  449. r = cuddCacheLookup1(dd,Cudd_addCmpl,f);
  450. if (r != NULL) {
  451. return(r);
  452. }
  453. Fv = cuddT(f);
  454. Fnv = cuddE(f);
  455. t = cuddAddCmplRecur(dd,Fv);
  456. if (t == NULL) return(NULL);
  457. cuddRef(t);
  458. e = cuddAddCmplRecur(dd,Fnv);
  459. if (e == NULL) {
  460. Cudd_RecursiveDeref(dd,t);
  461. return(NULL);
  462. }
  463. cuddRef(e);
  464. r = (t == e) ? t : cuddUniqueInter(dd,(int)f->index,t,e);
  465. if (r == NULL) {
  466. Cudd_RecursiveDeref(dd, t);
  467. Cudd_RecursiveDeref(dd, e);
  468. return(NULL);
  469. }
  470. cuddDeref(t);
  471. cuddDeref(e);
  472. cuddCacheInsert1(dd,Cudd_addCmpl,f,r);
  473. return(r);
  474. } /* end of cuddAddCmplRecur */
  475. /*---------------------------------------------------------------------------*/
  476. /* Definition of static functions */
  477. /*---------------------------------------------------------------------------*/
  478. /**Function********************************************************************
  479. Synopsis [Replaces variables with constants if possible (part of
  480. canonical form).]
  481. Description []
  482. SideEffects [None]
  483. ******************************************************************************/
  484. static void
  485. addVarToConst(
  486. DdNode * f,
  487. DdNode ** gp,
  488. DdNode ** hp,
  489. DdNode * one,
  490. DdNode * zero)
  491. {
  492. DdNode *g = *gp;
  493. DdNode *h = *hp;
  494. if (f == g) { /* ITE(F,F,H) = ITE(F,1,H) = F + H */
  495. *gp = one;
  496. }
  497. if (f == h) { /* ITE(F,G,F) = ITE(F,G,0) = F * G */
  498. *hp = zero;
  499. }
  500. } /* end of addVarToConst */