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.

1243 lines
27 KiB

  1. /**
  2. @file
  3. @ingroup cudd
  4. @brief Apply functions for ADDs and their operators.
  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. /** \endcond */
  56. /*---------------------------------------------------------------------------*/
  57. /* Definition of exported functions */
  58. /*---------------------------------------------------------------------------*/
  59. /**
  60. @brief Applies op to the corresponding discriminants of f and g.
  61. @return a pointer to the result if succssful; NULL otherwise.
  62. @sideeffect None
  63. @see Cudd_addMonadicApply Cudd_addPlus Cudd_addTimes
  64. Cudd_addThreshold Cudd_addSetNZ Cudd_addDivide Cudd_addMinus Cudd_addMinimum
  65. Cudd_addMaximum Cudd_addOneZeroMaximum Cudd_addDiff Cudd_addAgreement
  66. Cudd_addOr Cudd_addNand Cudd_addNor Cudd_addXor Cudd_addXnor
  67. */
  68. DdNode *
  69. Cudd_addApply(
  70. DdManager * dd /**< manager */,
  71. DD_AOP op /**< operator */,
  72. DdNode * f /**< first operand */,
  73. DdNode * g /**< second operand */)
  74. {
  75. DdNode *res;
  76. do {
  77. dd->reordered = 0;
  78. res = cuddAddApplyRecur(dd,op,f,g);
  79. } while (dd->reordered == 1);
  80. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  81. dd->timeoutHandler(dd, dd->tohArg);
  82. }
  83. return(res);
  84. } /* end of Cudd_addApply */
  85. /**
  86. @brief Integer and floating point addition.
  87. @return NULL if not a terminal case; f+g otherwise.
  88. @sideeffect None
  89. @see Cudd_addApply
  90. */
  91. DdNode *
  92. Cudd_addPlus(
  93. DdManager * dd,
  94. DdNode ** f,
  95. DdNode ** g)
  96. {
  97. DdNode *res;
  98. DdNode *F, *G;
  99. CUDD_VALUE_TYPE value;
  100. F = *f; G = *g;
  101. if (F == DD_ZERO(dd)) return(G);
  102. if (G == DD_ZERO(dd)) return(F);
  103. if (cuddIsConstant(F) && cuddIsConstant(G)) {
  104. value = cuddV(F)+cuddV(G);
  105. res = cuddUniqueConst(dd,value);
  106. return(res);
  107. }
  108. if (F > G) { /* swap f and g */
  109. *f = G;
  110. *g = F;
  111. }
  112. return(NULL);
  113. } /* end of Cudd_addPlus */
  114. /**
  115. @brief Integer and floating point multiplication.
  116. @details This function can be used also to take the AND of two 0-1
  117. ADDs.
  118. @return NULL if not a terminal case; f * g otherwise.
  119. @sideeffect None
  120. @see Cudd_addApply
  121. */
  122. DdNode *
  123. Cudd_addTimes(
  124. DdManager * dd,
  125. DdNode ** f,
  126. DdNode ** g)
  127. {
  128. DdNode *res;
  129. DdNode *F, *G;
  130. CUDD_VALUE_TYPE value;
  131. F = *f; G = *g;
  132. if (F == DD_ZERO(dd) || G == DD_ZERO(dd)) return(DD_ZERO(dd));
  133. if (F == DD_ONE(dd)) return(G);
  134. if (G == DD_ONE(dd)) return(F);
  135. if (cuddIsConstant(F) && cuddIsConstant(G)) {
  136. value = cuddV(F)*cuddV(G);
  137. res = cuddUniqueConst(dd,value);
  138. return(res);
  139. }
  140. if (F > G) { /* swap f and g */
  141. *f = G;
  142. *g = F;
  143. }
  144. return(NULL);
  145. } /* end of Cudd_addTimes */
  146. /**
  147. @brief f if f&ge;g; 0 if f&lt;g.
  148. @details Threshold operator for Apply (f if f &ge;g; 0 if f&lt;g).
  149. @return NULL if not a terminal case; f op g otherwise.
  150. @sideeffect None
  151. @see Cudd_addApply
  152. */
  153. DdNode *
  154. Cudd_addThreshold(
  155. DdManager * dd,
  156. DdNode ** f,
  157. DdNode ** g)
  158. {
  159. DdNode *F, *G;
  160. F = *f; G = *g;
  161. if (F == G || F == DD_PLUS_INFINITY(dd)) return(F);
  162. if (cuddIsConstant(F) && cuddIsConstant(G)) {
  163. if (cuddV(F) >= cuddV(G)) {
  164. return(F);
  165. } else {
  166. return(DD_ZERO(dd));
  167. }
  168. }
  169. return(NULL);
  170. } /* end of Cudd_addThreshold */
  171. /**
  172. @brief This operator sets f to the value of g wherever g != 0.
  173. @return NULL if not a terminal case; f op g otherwise.
  174. @sideeffect None
  175. @see Cudd_addApply
  176. */
  177. DdNode *
  178. Cudd_addSetNZ(
  179. DdManager * dd,
  180. DdNode ** f,
  181. DdNode ** g)
  182. {
  183. DdNode *F, *G;
  184. F = *f; G = *g;
  185. if (F == G) return(F);
  186. if (F == DD_ZERO(dd)) return(G);
  187. if (G == DD_ZERO(dd)) return(F);
  188. if (cuddIsConstant(G)) return(G);
  189. return(NULL);
  190. } /* end of Cudd_addSetNZ */
  191. /**
  192. @brief Integer and floating point division.
  193. @return NULL if not a terminal case; f / g otherwise.
  194. @sideeffect None
  195. @see Cudd_addApply
  196. */
  197. DdNode *
  198. Cudd_addDivide(
  199. DdManager * dd,
  200. DdNode ** f,
  201. DdNode ** g)
  202. {
  203. DdNode *res;
  204. DdNode *F, *G;
  205. CUDD_VALUE_TYPE value;
  206. F = *f; G = *g;
  207. /* We would like to use F == G -> F/G == 1, but F and G may
  208. ** contain zeroes. */
  209. if (F == DD_ZERO(dd)) return(DD_ZERO(dd));
  210. if (G == DD_ONE(dd)) return(F);
  211. if (cuddIsConstant(F) && cuddIsConstant(G)) {
  212. value = cuddV(F)/cuddV(G);
  213. res = cuddUniqueConst(dd,value);
  214. return(res);
  215. }
  216. return(NULL);
  217. } /* end of Cudd_addDivide */
  218. /**
  219. @brief Integer and floating point subtraction.
  220. @return NULL if not a terminal case; f - g otherwise.
  221. @sideeffect None
  222. @see Cudd_addApply
  223. */
  224. DdNode *
  225. Cudd_addMinus(
  226. DdManager * dd,
  227. DdNode ** f,
  228. DdNode ** g)
  229. {
  230. DdNode *res;
  231. DdNode *F, *G;
  232. CUDD_VALUE_TYPE value;
  233. F = *f; G = *g;
  234. if (F == G) return(DD_ZERO(dd));
  235. if (F == DD_ZERO(dd)) return(cuddAddNegateRecur(dd,G));
  236. if (G == DD_ZERO(dd)) return(F);
  237. if (cuddIsConstant(F) && cuddIsConstant(G)) {
  238. value = cuddV(F)-cuddV(G);
  239. res = cuddUniqueConst(dd,value);
  240. return(res);
  241. }
  242. return(NULL);
  243. } /* end of Cudd_addMinus */
  244. /**
  245. @brief Integer and floating point min.
  246. @details Integer and floating point min for Cudd_addApply.
  247. @return NULL if not a terminal case; min(f,g) otherwise.
  248. @sideeffect None
  249. @see Cudd_addApply
  250. */
  251. DdNode *
  252. Cudd_addMinimum(
  253. DdManager * dd,
  254. DdNode ** f,
  255. DdNode ** g)
  256. {
  257. DdNode *F, *G;
  258. F = *f; G = *g;
  259. if (F == DD_PLUS_INFINITY(dd)) return(G);
  260. if (G == DD_PLUS_INFINITY(dd)) return(F);
  261. if (F == G) return(F);
  262. #if 0
  263. /* These special cases probably do not pay off. */
  264. if (F == DD_MINUS_INFINITY(dd)) return(F);
  265. if (G == DD_MINUS_INFINITY(dd)) return(G);
  266. #endif
  267. if (cuddIsConstant(F) && cuddIsConstant(G)) {
  268. if (cuddV(F) <= cuddV(G)) {
  269. return(F);
  270. } else {
  271. return(G);
  272. }
  273. }
  274. if (F > G) { /* swap f and g */
  275. *f = G;
  276. *g = F;
  277. }
  278. return(NULL);
  279. } /* end of Cudd_addMinimum */
  280. /**
  281. @brief Integer and floating point max.
  282. @details Integer and floating point max for Cudd_addApply.
  283. @return NULL if not a terminal case; max(f,g) otherwise.
  284. @sideeffect None
  285. @see Cudd_addApply
  286. */
  287. DdNode *
  288. Cudd_addMaximum(
  289. DdManager * dd,
  290. DdNode ** f,
  291. DdNode ** g)
  292. {
  293. DdNode *F, *G;
  294. F = *f; G = *g;
  295. if (F == G) return(F);
  296. if (F == DD_MINUS_INFINITY(dd)) return(G);
  297. if (G == DD_MINUS_INFINITY(dd)) return(F);
  298. #if 0
  299. /* These special cases probably do not pay off. */
  300. if (F == DD_PLUS_INFINITY(dd)) return(F);
  301. if (G == DD_PLUS_INFINITY(dd)) return(G);
  302. #endif
  303. if (cuddIsConstant(F) && cuddIsConstant(G)) {
  304. if (cuddV(F) >= cuddV(G)) {
  305. return(F);
  306. } else {
  307. return(G);
  308. }
  309. }
  310. if (F > G) { /* swap f and g */
  311. *f = G;
  312. *g = F;
  313. }
  314. return(NULL);
  315. } /* end of Cudd_addMaximum */
  316. /**
  317. @brief Returns 1 if f &gt; g and 0 otherwise.
  318. @details Used in conjunction with Cudd_addApply.
  319. @return NULL if not a terminal case.
  320. @sideeffect None
  321. @see Cudd_addApply
  322. */
  323. DdNode *
  324. Cudd_addOneZeroMaximum(
  325. DdManager * dd,
  326. DdNode ** f,
  327. DdNode ** g)
  328. {
  329. if (*f == *g) return(DD_ZERO(dd));
  330. if (*g == DD_PLUS_INFINITY(dd))
  331. return DD_ZERO(dd);
  332. if (cuddIsConstant(*f) && cuddIsConstant(*g)) {
  333. if (cuddV(*f) > cuddV(*g)) {
  334. return(DD_ONE(dd));
  335. } else {
  336. return(DD_ZERO(dd));
  337. }
  338. }
  339. return(NULL);
  340. } /* end of Cudd_addOneZeroMaximum */
  341. /**
  342. @brief Returns plusinfinity if f=g; returns min(f,g) if f!=g.
  343. @return NULL if not a terminal case; f op g otherwise, where f op g
  344. is plusinfinity if f=g; min(f,g) if f!=g.
  345. @sideeffect None
  346. @see Cudd_addApply
  347. */
  348. DdNode *
  349. Cudd_addDiff(
  350. DdManager * dd,
  351. DdNode ** f,
  352. DdNode ** g)
  353. {
  354. DdNode *F, *G;
  355. F = *f; G = *g;
  356. if (F == G) return(DD_PLUS_INFINITY(dd));
  357. if (F == DD_PLUS_INFINITY(dd)) return(G);
  358. if (G == DD_PLUS_INFINITY(dd)) return(F);
  359. if (cuddIsConstant(F) && cuddIsConstant(G)) {
  360. if (cuddV(F) != cuddV(G)) {
  361. if (cuddV(F) < cuddV(G)) {
  362. return(F);
  363. } else {
  364. return(G);
  365. }
  366. } else {
  367. return(DD_PLUS_INFINITY(dd));
  368. }
  369. }
  370. return(NULL);
  371. } /* end of Cudd_addDiff */
  372. /**
  373. @brief f if f==g; background if f!=g.
  374. @return NULL if not a terminal case; f op g otherwise, where f op g
  375. is f if f==g; background if f!=g.
  376. @sideeffect None
  377. @see Cudd_addApply
  378. */
  379. DdNode *
  380. Cudd_addAgreement(
  381. DdManager * dd,
  382. DdNode ** f,
  383. DdNode ** g)
  384. {
  385. DdNode *F, *G;
  386. F = *f; G = *g;
  387. if (F == G) return(F);
  388. if (F == dd->background) return(F);
  389. if (G == dd->background) return(G);
  390. if (cuddIsConstant(F) && cuddIsConstant(G)) return(dd->background);
  391. return(NULL);
  392. } /* end of Cudd_addAgreement */
  393. /**
  394. @brief Disjunction of two 0-1 ADDs.
  395. @return NULL if not a terminal case; f OR g otherwise.
  396. @sideeffect None
  397. @see Cudd_addApply
  398. */
  399. DdNode *
  400. Cudd_addOr(
  401. DdManager * dd,
  402. DdNode ** f,
  403. DdNode ** g)
  404. {
  405. DdNode *F, *G;
  406. F = *f; G = *g;
  407. if (F == DD_ONE(dd) || G == DD_ONE(dd)) return(DD_ONE(dd));
  408. if (cuddIsConstant(F)) return(G);
  409. if (cuddIsConstant(G)) return(F);
  410. if (F == G) return(F);
  411. if (F > G) { /* swap f and g */
  412. *f = G;
  413. *g = F;
  414. }
  415. return(NULL);
  416. } /* end of Cudd_addOr */
  417. /**
  418. @brief NAND of two 0-1 ADDs.
  419. @return NULL if not a terminal case; f NAND g otherwise.
  420. @sideeffect None
  421. @see Cudd_addApply
  422. */
  423. DdNode *
  424. Cudd_addNand(
  425. DdManager * dd,
  426. DdNode ** f,
  427. DdNode ** g)
  428. {
  429. DdNode *F, *G;
  430. F = *f; G = *g;
  431. if (F == DD_ZERO(dd) || G == DD_ZERO(dd)) return(DD_ONE(dd));
  432. if (cuddIsConstant(F) && cuddIsConstant(G)) return(DD_ZERO(dd));
  433. if (F > G) { /* swap f and g */
  434. *f = G;
  435. *g = F;
  436. }
  437. return(NULL);
  438. } /* end of Cudd_addNand */
  439. /**
  440. @brief NOR of two 0-1 ADDs.
  441. @return NULL if not a terminal case; f NOR g otherwise.
  442. @sideeffect None
  443. @see Cudd_addApply
  444. */
  445. DdNode *
  446. Cudd_addNor(
  447. DdManager * dd,
  448. DdNode ** f,
  449. DdNode ** g)
  450. {
  451. DdNode *F, *G;
  452. F = *f; G = *g;
  453. if (F == DD_ONE(dd) || G == DD_ONE(dd)) return(DD_ZERO(dd));
  454. if (cuddIsConstant(F) && cuddIsConstant(G)) return(DD_ONE(dd));
  455. if (F > G) { /* swap f and g */
  456. *f = G;
  457. *g = F;
  458. }
  459. return(NULL);
  460. } /* end of Cudd_addNor */
  461. /**
  462. @brief XOR of two 0-1 ADDs.
  463. @return NULL if not a terminal case; f XOR g otherwise.
  464. @sideeffect None
  465. @see Cudd_addApply
  466. */
  467. DdNode *
  468. Cudd_addXor(
  469. DdManager * dd,
  470. DdNode ** f,
  471. DdNode ** g)
  472. {
  473. DdNode *F, *G;
  474. F = *f; G = *g;
  475. if (F == G) return(DD_ZERO(dd));
  476. if (F == DD_ONE(dd) && G == DD_ZERO(dd)) return(DD_ONE(dd));
  477. if (G == DD_ONE(dd) && F == DD_ZERO(dd)) return(DD_ONE(dd));
  478. if (cuddIsConstant(F) && cuddIsConstant(G)) return(DD_ZERO(dd));
  479. if (F > G) { /* swap f and g */
  480. *f = G;
  481. *g = F;
  482. }
  483. return(NULL);
  484. } /* end of Cudd_addXor */
  485. /**
  486. @brief XNOR of two 0-1 ADDs.
  487. @return NULL if not a terminal case; f XNOR g otherwise.
  488. @sideeffect None
  489. @see Cudd_addApply
  490. */
  491. DdNode *
  492. Cudd_addXnor(
  493. DdManager * dd,
  494. DdNode ** f,
  495. DdNode ** g)
  496. {
  497. DdNode *F, *G;
  498. F = *f; G = *g;
  499. if (F == G) return(DD_ONE(dd));
  500. if (F == DD_ONE(dd) && G == DD_ONE(dd)) return(DD_ONE(dd));
  501. if (G == DD_ZERO(dd) && F == DD_ZERO(dd)) return(DD_ONE(dd));
  502. if (cuddIsConstant(F) && cuddIsConstant(G)) return(DD_ZERO(dd));
  503. if (F > G) { /* swap f and g */
  504. *f = G;
  505. *g = F;
  506. }
  507. return(NULL);
  508. } /* end of Cudd_addXnor */
  509. /**
  510. @brief Applies op to the discriminants of f.
  511. @return a pointer to the result if succssful; NULL otherwise.
  512. @sideeffect None
  513. @see Cudd_addApply Cudd_addLog
  514. */
  515. DdNode *
  516. Cudd_addMonadicApply(
  517. DdManager * dd,
  518. DD_MAOP op,
  519. DdNode * f)
  520. {
  521. DdNode *res;
  522. do {
  523. dd->reordered = 0;
  524. res = cuddAddMonadicApplyRecur(dd,op,f);
  525. } while (dd->reordered == 1);
  526. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  527. dd->timeoutHandler(dd, dd->tohArg);
  528. }
  529. return(res);
  530. } /* end of Cudd_addMonadicApply */
  531. /**
  532. @brief Natural logarithm of an %ADD.
  533. @details The discriminants of f must be positive double's.
  534. @return NULL if not a terminal case; log(f) otherwise.
  535. @sideeffect None
  536. @see Cudd_addMonadicApply
  537. */
  538. DdNode *
  539. Cudd_addLog(
  540. DdManager * dd,
  541. DdNode * f)
  542. {
  543. if (cuddIsConstant(f)) {
  544. CUDD_VALUE_TYPE value = log(cuddV(f));
  545. DdNode *res = cuddUniqueConst(dd,value);
  546. return(res);
  547. }
  548. return(NULL);
  549. } /* end of Cudd_addLog */
  550. /**Function********************************************************************
  551. Synopsis [Floor of an ADD.]
  552. Description [Floor of an ADD. Returns NULL
  553. if not a terminal case; floor(f) otherwise.]
  554. SideEffects [None]
  555. SeeAlso [Cudd_addMonadicApply]
  556. ******************************************************************************/
  557. DdNode *
  558. Cudd_addFloor(
  559. DdManager * dd,
  560. DdNode * f)
  561. {
  562. if (cuddIsConstant(f)) {
  563. CUDD_VALUE_TYPE value = floor(cuddV(f));
  564. DdNode *res = cuddUniqueConst(dd,value);
  565. return(res);
  566. }
  567. return(NULL);
  568. } /* end of Cudd_addFloor */
  569. /**Function********************************************************************
  570. Synopsis [Ceiling of an ADD.]
  571. Description [Ceiling of an ADD. Returns NULL
  572. if not a terminal case; ceil(f) otherwise.]
  573. SideEffects [None]
  574. SeeAlso [Cudd_addMonadicApply]
  575. ******************************************************************************/
  576. DdNode *
  577. Cudd_addCeil(
  578. DdManager * dd,
  579. DdNode * f)
  580. {
  581. if (cuddIsConstant(f)) {
  582. CUDD_VALUE_TYPE value = ceil(cuddV(f));
  583. DdNode *res = cuddUniqueConst(dd,value);
  584. return(res);
  585. }
  586. return(NULL);
  587. } /* end of Cudd_addCeiling */
  588. /**Function********************************************************************
  589. Synopsis [1 if f==g; 0 otherwise.]
  590. Description [Returns NULL if not a terminal case; f op g otherwise,
  591. where f op g is 1 if f==g; 0 otherwise.]
  592. SideEffects [None]
  593. SeeAlso [Cudd_addApply]
  594. ******************************************************************************/
  595. DdNode *
  596. Cudd_addEquals(
  597. DdManager * dd,
  598. DdNode ** f,
  599. DdNode ** g)
  600. {
  601. DdNode *F, *G;
  602. F = *f; G = *g;
  603. if (F == G) return(DD_ONE(dd));
  604. if (cuddIsConstant(F) && cuddIsConstant(G)) return(DD_ZERO(dd));
  605. if (F > G) { /* swap f and g */
  606. *f = G;
  607. *g = F;
  608. }
  609. return(NULL);
  610. } /* end of Cudd_addEquals */
  611. /**Function********************************************************************
  612. Synopsis [1 if f!=g; 0 otherwise.]
  613. Description [Returns NULL if not a terminal case; f op g otherwise,
  614. where f op g is 1 if f!=g; 0 otherwise.]
  615. SideEffects [None]
  616. SeeAlso [Cudd_addApply]
  617. ******************************************************************************/
  618. DdNode *
  619. Cudd_addNotEquals(
  620. DdManager * dd,
  621. DdNode ** f,
  622. DdNode ** g)
  623. {
  624. DdNode *F, *G;
  625. F = *f; G = *g;
  626. if (F == G) return(DD_ZERO(dd));
  627. if (cuddIsConstant(F) && cuddIsConstant(G)) return(DD_ONE(dd));
  628. if (F > G) { /* swap f and g */
  629. *f = G;
  630. *g = F;
  631. }
  632. return(NULL);
  633. } /* end of Cudd_addNotEquals */
  634. /**Function********************************************************************
  635. Synopsis [1 if f>g; 0 otherwise.]
  636. Description [Returns NULL if not a terminal case; f op g otherwise,
  637. where f op g is 1 if f>g; 0 otherwise.]
  638. SideEffects [None]
  639. SeeAlso [Cudd_addApply]
  640. ******************************************************************************/
  641. DdNode *
  642. Cudd_addGreaterThan(
  643. DdManager * dd,
  644. DdNode ** f,
  645. DdNode ** g)
  646. {
  647. DdNode *F, *G;
  648. F = *f; G = *g;
  649. if (F == G) return(DD_ZERO(dd));
  650. if (cuddIsConstant(F) && cuddIsConstant(G)) {
  651. if (cuddV(F)>cuddV(G)) return (DD_ONE(dd)); else return (DD_ZERO(dd));
  652. }
  653. return(NULL);
  654. } /* end of Cudd_addGreaterThan */
  655. /**Function********************************************************************
  656. Synopsis [1 if f>=g; 0 otherwise.]
  657. Description [Returns NULL if not a terminal case; f op g otherwise,
  658. where f op g is 1 if f>=g; 0 otherwise.]
  659. SideEffects [None]
  660. SeeAlso [Cudd_addApply]
  661. ******************************************************************************/
  662. DdNode *
  663. Cudd_addGreaterThanEquals(
  664. DdManager * dd,
  665. DdNode ** f,
  666. DdNode ** g)
  667. {
  668. DdNode *F, *G;
  669. F = *f; G = *g;
  670. if (F == G) return(DD_ONE(dd));
  671. if (cuddIsConstant(F) && cuddIsConstant(G)) {
  672. if (cuddV(F)>=cuddV(G)) return (DD_ONE(dd)); else return (DD_ZERO(dd));
  673. }
  674. return(NULL);
  675. } /* end of Cudd_addGreaterThanEquals */
  676. /**Function********************************************************************
  677. Synopsis [1 if f<g; 0 otherwise.]
  678. Description [Returns NULL if not a terminal case; f op g otherwise,
  679. where f op g is 1 if f<g; 0 otherwise.]
  680. SideEffects [None]
  681. SeeAlso [Cudd_addApply]
  682. ******************************************************************************/
  683. DdNode *
  684. Cudd_addLessThan(
  685. DdManager * dd,
  686. DdNode ** f,
  687. DdNode ** g)
  688. {
  689. DdNode *F, *G;
  690. F = *f; G = *g;
  691. if (F == G) return(DD_ZERO(dd));
  692. if (cuddIsConstant(F) && cuddIsConstant(G)) {
  693. if (cuddV(F)<cuddV(G)) return (DD_ONE(dd)); else return (DD_ZERO(dd));
  694. }
  695. return(NULL);
  696. } /* end of Cudd_addLessThan */
  697. /**Function********************************************************************
  698. Synopsis [1 if f<=g; 0 otherwise.]
  699. Description [Returns NULL if not a terminal case; f op g otherwise,
  700. where f op g is 1 if f<=g; 0 otherwise.]
  701. SideEffects [None]
  702. SeeAlso [Cudd_addApply]
  703. ******************************************************************************/
  704. DdNode *
  705. Cudd_addLessThanEquals(
  706. DdManager * dd,
  707. DdNode ** f,
  708. DdNode ** g)
  709. {
  710. DdNode *F, *G;
  711. F = *f; G = *g;
  712. if (F == G) return(DD_ONE(dd));
  713. if (cuddIsConstant(F) && cuddIsConstant(G)) {
  714. if (cuddV(F)<=cuddV(G)) return (DD_ONE(dd)); else return (DD_ZERO(dd));
  715. }
  716. return(NULL);
  717. } /* end of Cudd_addLessThanEquals */
  718. /**Function********************************************************************
  719. Synopsis [f to the power of g.]
  720. Description [Returns NULL if not a terminal case; f op g otherwise,
  721. where f op g is f to the power of g.]
  722. SideEffects [None]
  723. SeeAlso [Cudd_addApply]
  724. ******************************************************************************/
  725. DdNode *
  726. Cudd_addPow(
  727. DdManager * dd,
  728. DdNode ** f,
  729. DdNode ** g)
  730. {
  731. DdNode *res;
  732. DdNode *F, *G;
  733. CUDD_VALUE_TYPE value;
  734. F = *f; G = *g;
  735. if (G == DD_ZERO(dd)) return(DD_ONE(dd));
  736. if (cuddIsConstant(F) && cuddIsConstant(G)) {
  737. value = pow(cuddV(F), cuddV(G));
  738. res = cuddUniqueConst(dd,value);
  739. return(res);
  740. }
  741. return(NULL);
  742. } /* end of Cudd_addPow */
  743. /**Function********************************************************************
  744. Synopsis [f modulo g.]
  745. Description [Returns NULL if not a terminal case; f op g otherwise,
  746. where f op g is f modulo g.]
  747. SideEffects [None]
  748. SeeAlso [Cudd_addApply]
  749. ******************************************************************************/
  750. DdNode *
  751. Cudd_addMod(
  752. DdManager * dd,
  753. DdNode ** f,
  754. DdNode ** g)
  755. {
  756. DdNode *res;
  757. DdNode *F, *G;
  758. int rem;
  759. CUDD_VALUE_TYPE value;
  760. F = *f; G = *g;
  761. if (cuddIsConstant(F) && cuddIsConstant(G)) {
  762. // If g is <=0, then result is NaN
  763. if (cuddV(G) <= 0) value = (NAN);
  764. // Take care of negative case (% is remainder, not modulo)
  765. else {
  766. rem = ((int)cuddV(F) % (int)cuddV(G));
  767. if (rem < 0) rem += (int)cuddV(G);
  768. value = rem;
  769. }
  770. // Create/return result
  771. res = cuddUniqueConst(dd,value);
  772. return(res);
  773. }
  774. return(NULL);
  775. } /* end of Cudd_addMod */
  776. /**Function********************************************************************
  777. Synopsis [log f base g.]
  778. Description [Returns NULL if not a terminal case; f op g otherwise,
  779. where f op g is log f base g.]
  780. SideEffects [None]
  781. SeeAlso [Cudd_addApply]
  782. ******************************************************************************/
  783. DdNode *
  784. Cudd_addLogXY(
  785. DdManager * dd,
  786. DdNode ** f,
  787. DdNode ** g)
  788. {
  789. DdNode *res;
  790. DdNode *F, *G;
  791. CUDD_VALUE_TYPE value;
  792. F = *f; G = *g;
  793. if (cuddIsConstant(F) && cuddIsConstant(G)) {
  794. // If base is <=0 or ==1 (or +Inf/NaN), then result is NaN
  795. if (cuddV(G) <= 0 || cuddV(G) == 1.0 || G==DD_PLUS_INFINITY(dd) || cuddV(G) != cuddV(G)) value = (NAN);
  796. // If arg is <0 or NaN, then result is NaN
  797. else if (cuddV(F) < 0 || cuddV(F) != cuddV(F)) value = (NAN);
  798. // If arg is +Inf, then result is +Inf
  799. else if (F==DD_PLUS_INFINITY(dd)) return DD_PLUS_INFINITY(dd);
  800. // If arg is (positive/negative) 0, then result is -Inf
  801. else if (cuddV(F) == 0.0 || cuddV(F) == -0.0) return DD_MINUS_INFINITY(dd);
  802. // Default case: normal log
  803. else value = log(cuddV(F)) / log(cuddV(G));
  804. // Create/return result
  805. res = cuddUniqueConst(dd,value);
  806. return(res);
  807. }
  808. return(NULL);
  809. } /* end of Cudd_addLogXY */
  810. /*---------------------------------------------------------------------------*/
  811. /* Definition of internal functions */
  812. /*---------------------------------------------------------------------------*/
  813. /**
  814. @brief Performs the recursive step of Cudd_addApply.
  815. @return a pointer to the result if successful; NULL otherwise.
  816. @sideeffect None
  817. @see cuddAddMonadicApplyRecur
  818. */
  819. DdNode *
  820. cuddAddApplyRecur(
  821. DdManager * dd,
  822. DD_AOP op,
  823. DdNode * f,
  824. DdNode * g)
  825. {
  826. DdNode *res,
  827. *fv, *fvn, *gv, *gvn,
  828. *T, *E;
  829. int ford, gord;
  830. unsigned int index;
  831. DD_CTFP cacheOp;
  832. /* Check terminal cases. Op may swap f and g to increase the
  833. * cache hit rate.
  834. */
  835. statLine(dd);
  836. res = (*op)(dd,&f,&g);
  837. if (res != NULL) return(res);
  838. /* Check cache. */
  839. cacheOp = (DD_CTFP) op;
  840. res = cuddCacheLookup2(dd,cacheOp,f,g);
  841. if (res != NULL) return(res);
  842. checkWhetherToGiveUp(dd);
  843. /* Recursive step. */
  844. ford = cuddI(dd,f->index);
  845. gord = cuddI(dd,g->index);
  846. if (ford <= gord) {
  847. index = f->index;
  848. fv = cuddT(f);
  849. fvn = cuddE(f);
  850. } else {
  851. index = g->index;
  852. fv = fvn = f;
  853. }
  854. if (gord <= ford) {
  855. gv = cuddT(g);
  856. gvn = cuddE(g);
  857. } else {
  858. gv = gvn = g;
  859. }
  860. T = cuddAddApplyRecur(dd,op,fv,gv);
  861. if (T == NULL) return(NULL);
  862. cuddRef(T);
  863. E = cuddAddApplyRecur(dd,op,fvn,gvn);
  864. if (E == NULL) {
  865. Cudd_RecursiveDeref(dd,T);
  866. return(NULL);
  867. }
  868. cuddRef(E);
  869. res = (T == E) ? T : cuddUniqueInter(dd,(int)index,T,E);
  870. if (res == NULL) {
  871. Cudd_RecursiveDeref(dd, T);
  872. Cudd_RecursiveDeref(dd, E);
  873. return(NULL);
  874. }
  875. cuddDeref(T);
  876. cuddDeref(E);
  877. /* Store result. */
  878. cuddCacheInsert2(dd,cacheOp,f,g,res);
  879. return(res);
  880. } /* end of cuddAddApplyRecur */
  881. /**
  882. @brief Performs the recursive step of Cudd_addMonadicApply.
  883. @return a pointer to the result if successful; NULL otherwise.
  884. @sideeffect None
  885. @see cuddAddApplyRecur
  886. */
  887. DdNode *
  888. cuddAddMonadicApplyRecur(
  889. DdManager * dd,
  890. DD_MAOP op,
  891. DdNode * f)
  892. {
  893. DdNode *res, *ft, *fe, *T, *E;
  894. unsigned int index;
  895. /* Check terminal cases. */
  896. statLine(dd);
  897. res = (*op)(dd,f);
  898. if (res != NULL) return(res);
  899. /* Check cache. */
  900. res = cuddCacheLookup1(dd,op,f);
  901. if (res != NULL) return(res);
  902. checkWhetherToGiveUp(dd);
  903. /* Recursive step. */
  904. index = f->index;
  905. ft = cuddT(f);
  906. fe = cuddE(f);
  907. T = cuddAddMonadicApplyRecur(dd,op,ft);
  908. if (T == NULL) return(NULL);
  909. cuddRef(T);
  910. E = cuddAddMonadicApplyRecur(dd,op,fe);
  911. if (E == NULL) {
  912. Cudd_RecursiveDeref(dd,T);
  913. return(NULL);
  914. }
  915. cuddRef(E);
  916. res = (T == E) ? T : cuddUniqueInter(dd,(int)index,T,E);
  917. if (res == NULL) {
  918. Cudd_RecursiveDeref(dd, T);
  919. Cudd_RecursiveDeref(dd, E);
  920. return(NULL);
  921. }
  922. cuddDeref(T);
  923. cuddDeref(E);
  924. /* Store result. */
  925. cuddCacheInsert1(dd,op,f,res);
  926. return(res);
  927. } /* end of cuddAddMonadicApplyRecur */
  928. /*---------------------------------------------------------------------------*/
  929. /* Definition of static functions */
  930. /*---------------------------------------------------------------------------*/