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.

375 lines
10 KiB

3 months ago
  1. /**
  2. @file
  3. @ingroup cudd
  4. @brief Functions that generate Walsh matrices and residue
  5. functions in %ADD form.
  6. @author Fabio Somenzi
  7. @copyright@parblock
  8. Copyright (c) 1995-2015, Regents of the University of Colorado
  9. All rights reserved.
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions
  12. are met:
  13. Redistributions of source code must retain the above copyright
  14. notice, this list of conditions and the following disclaimer.
  15. Redistributions in binary form must reproduce the above copyright
  16. notice, this list of conditions and the following disclaimer in the
  17. documentation and/or other materials provided with the distribution.
  18. Neither the name of the University of Colorado nor the names of its
  19. contributors may be used to endorse or promote products derived from
  20. this software without specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. POSSIBILITY OF SUCH DAMAGE.
  33. @endparblock
  34. */
  35. #include "util.h"
  36. #include "cuddInt.h"
  37. /*---------------------------------------------------------------------------*/
  38. /* Constant declarations */
  39. /*---------------------------------------------------------------------------*/
  40. /*---------------------------------------------------------------------------*/
  41. /* Stucture declarations */
  42. /*---------------------------------------------------------------------------*/
  43. /*---------------------------------------------------------------------------*/
  44. /* Type declarations */
  45. /*---------------------------------------------------------------------------*/
  46. /*---------------------------------------------------------------------------*/
  47. /* Variable declarations */
  48. /*---------------------------------------------------------------------------*/
  49. /*---------------------------------------------------------------------------*/
  50. /* Macro declarations */
  51. /*---------------------------------------------------------------------------*/
  52. /** \cond */
  53. /*---------------------------------------------------------------------------*/
  54. /* Static function prototypes */
  55. /*---------------------------------------------------------------------------*/
  56. static DdNode * addWalshInt (DdManager *dd, DdNode **x, DdNode **y, int n);
  57. /** \endcond */
  58. /*---------------------------------------------------------------------------*/
  59. /* Definition of exported functions */
  60. /*---------------------------------------------------------------------------*/
  61. /**
  62. @brief Generates a Walsh matrix in %ADD form.
  63. @return a pointer to the matrixi if successful; NULL otherwise.
  64. @sideeffect None
  65. */
  66. DdNode *
  67. Cudd_addWalsh(
  68. DdManager * dd,
  69. DdNode ** x,
  70. DdNode ** y,
  71. int n)
  72. {
  73. DdNode *res;
  74. do {
  75. dd->reordered = 0;
  76. res = addWalshInt(dd, x, y, n);
  77. } while (dd->reordered == 1);
  78. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  79. dd->timeoutHandler(dd, dd->tohArg);
  80. }
  81. return(res);
  82. } /* end of Cudd_addWalsh */
  83. /**
  84. @brief Builds an %ADD for the residue modulo m of an n-bit
  85. number.
  86. @details The modulus must be at least 2, and the number of bits at
  87. least 1. Parameter options specifies whether the MSB should be on top
  88. or the LSB; and whther the number whose residue is computed is in
  89. two's complement notation or not. The macro CUDD_RESIDUE_DEFAULT
  90. specifies LSB on top and unsigned number. The macro CUDD_RESIDUE_MSB
  91. specifies MSB on top, and the macro CUDD_RESIDUE_TC specifies two's
  92. complement residue. To request MSB on top and two's complement residue
  93. simultaneously, one can OR the two macros:
  94. CUDD_RESIDUE_MSB | CUDD_RESIDUE_TC.
  95. @return a pointer to the resulting %ADD if successful; NULL
  96. otherwise.
  97. @sideeffect None
  98. */
  99. DdNode *
  100. Cudd_addResidue(
  101. DdManager * dd /**< manager */,
  102. int n /**< number of bits */,
  103. int m /**< modulus */,
  104. int options /**< options */,
  105. int top /**< index of top variable */)
  106. {
  107. int msbLsb; /* MSB on top (1) or LSB on top (0) */
  108. int tc; /* two's complement (1) or unsigned (0) */
  109. int i, j, k, t, residue, thisOne, previous, index;
  110. DdNode **array[2], *var, *tmp, *res;
  111. /* Sanity check. */
  112. if (n < 1 && m < 2) return(NULL);
  113. msbLsb = options & CUDD_RESIDUE_MSB;
  114. tc = options & CUDD_RESIDUE_TC;
  115. /* Allocate and initialize working arrays. */
  116. array[0] = ALLOC(DdNode *,m);
  117. if (array[0] == NULL) {
  118. dd->errorCode = CUDD_MEMORY_OUT;
  119. return(NULL);
  120. }
  121. array[1] = ALLOC(DdNode *,m);
  122. if (array[1] == NULL) {
  123. FREE(array[0]);
  124. dd->errorCode = CUDD_MEMORY_OUT;
  125. return(NULL);
  126. }
  127. for (i = 0; i < m; i++) {
  128. array[0][i] = array[1][i] = NULL;
  129. }
  130. /* Initialize residues. */
  131. for (i = 0; i < m; i++) {
  132. tmp = cuddUniqueConst(dd,(CUDD_VALUE_TYPE) i);
  133. if (tmp == NULL) {
  134. for (j = 0; j < i; j++) {
  135. Cudd_RecursiveDeref(dd,array[1][j]);
  136. }
  137. FREE(array[0]);
  138. FREE(array[1]);
  139. return(NULL);
  140. }
  141. cuddRef(tmp);
  142. array[1][i] = tmp;
  143. }
  144. /* Main iteration. */
  145. residue = 1; /* residue of 2**0 */
  146. for (k = 0; k < n; k++) {
  147. /* Choose current and previous arrays. */
  148. thisOne = k & 1;
  149. previous = thisOne ^ 1;
  150. /* Build an ADD projection function. */
  151. if (msbLsb) {
  152. index = top+n-k-1;
  153. } else {
  154. index = top+k;
  155. }
  156. var = cuddUniqueInter(dd,index,DD_ONE(dd),DD_ZERO(dd));
  157. if (var == NULL) {
  158. for (j = 0; j < m; j++) {
  159. Cudd_RecursiveDeref(dd,array[previous][j]);
  160. }
  161. FREE(array[0]);
  162. FREE(array[1]);
  163. return(NULL);
  164. }
  165. cuddRef(var);
  166. for (i = 0; i < m; i ++) {
  167. t = (i + residue) % m;
  168. tmp = Cudd_addIte(dd,var,array[previous][t],array[previous][i]);
  169. if (tmp == NULL) {
  170. for (j = 0; j < i; j++) {
  171. Cudd_RecursiveDeref(dd,array[thisOne][j]);
  172. }
  173. for (j = 0; j < m; j++) {
  174. Cudd_RecursiveDeref(dd,array[previous][j]);
  175. }
  176. FREE(array[0]);
  177. FREE(array[1]);
  178. return(NULL);
  179. }
  180. cuddRef(tmp);
  181. array[thisOne][i] = tmp;
  182. }
  183. /* One layer completed. Free the other array for the next iteration. */
  184. for (i = 0; i < m; i++) {
  185. Cudd_RecursiveDeref(dd,array[previous][i]);
  186. }
  187. Cudd_RecursiveDeref(dd,var);
  188. /* Update residue of 2**k. */
  189. residue = (2 * residue) % m;
  190. /* Adjust residue for MSB, if this is a two's complement number. */
  191. if (tc && (k == n - 1)) {
  192. residue = (m - residue) % m;
  193. }
  194. }
  195. /* We are only interested in the 0-residue node of the top layer. */
  196. for (i = 1; i < m; i++) {
  197. Cudd_RecursiveDeref(dd,array[(n - 1) & 1][i]);
  198. }
  199. res = array[(n - 1) & 1][0];
  200. FREE(array[0]);
  201. FREE(array[1]);
  202. cuddDeref(res);
  203. return(res);
  204. } /* end of Cudd_addResidue */
  205. /*---------------------------------------------------------------------------*/
  206. /* Definition of internal functions */
  207. /*---------------------------------------------------------------------------*/
  208. /*---------------------------------------------------------------------------*/
  209. /* Definition of static functions */
  210. /*---------------------------------------------------------------------------*/
  211. /**
  212. @brief Implements the recursive step of Cudd_addWalsh.
  213. @return a pointer to the matrixi if successful; NULL otherwise.
  214. @sideeffect None
  215. @see Cudd_addWalsh
  216. */
  217. static DdNode *
  218. addWalshInt(
  219. DdManager * dd,
  220. DdNode ** x,
  221. DdNode ** y,
  222. int n)
  223. {
  224. DdNode *one, *minusone;
  225. DdNode *t = NULL, *u, *t1, *u1, *v, *w;
  226. int i;
  227. one = DD_ONE(dd);
  228. if (n == 0) return(one);
  229. /* Build bottom part of ADD outside loop */
  230. minusone = cuddUniqueConst(dd,(CUDD_VALUE_TYPE) -1);
  231. if (minusone == NULL) return(NULL);
  232. cuddRef(minusone);
  233. v = Cudd_addIte(dd, y[n-1], minusone, one);
  234. if (v == NULL) {
  235. Cudd_RecursiveDeref(dd, minusone);
  236. return(NULL);
  237. }
  238. cuddRef(v);
  239. u = Cudd_addIte(dd, x[n-1], v, one);
  240. if (u == NULL) {
  241. Cudd_RecursiveDeref(dd, minusone);
  242. Cudd_RecursiveDeref(dd, v);
  243. return(NULL);
  244. }
  245. cuddRef(u);
  246. Cudd_RecursiveDeref(dd, v);
  247. if (n>1) {
  248. w = Cudd_addIte(dd, y[n-1], one, minusone);
  249. if (w == NULL) {
  250. Cudd_RecursiveDeref(dd, minusone);
  251. Cudd_RecursiveDeref(dd, u);
  252. return(NULL);
  253. }
  254. cuddRef(w);
  255. t = Cudd_addIte(dd, x[n-1], w, minusone);
  256. if (t == NULL) {
  257. Cudd_RecursiveDeref(dd, minusone);
  258. Cudd_RecursiveDeref(dd, u);
  259. Cudd_RecursiveDeref(dd, w);
  260. return(NULL);
  261. }
  262. cuddRef(t);
  263. Cudd_RecursiveDeref(dd, w);
  264. }
  265. cuddDeref(minusone); /* minusone is in the result; it won't die */
  266. /* Loop to build the rest of the ADD */
  267. for (i=n-2; i>=0; i--) {
  268. t1 = t; u1 = u;
  269. v = Cudd_addIte(dd, y[i], t1, u1);
  270. if (v == NULL) {
  271. Cudd_RecursiveDeref(dd, u1);
  272. Cudd_RecursiveDeref(dd, t1);
  273. return(NULL);
  274. }
  275. cuddRef(v);
  276. u = Cudd_addIte(dd, x[i], v, u1);
  277. if (u == NULL) {
  278. Cudd_RecursiveDeref(dd, u1);
  279. Cudd_RecursiveDeref(dd, t1);
  280. Cudd_RecursiveDeref(dd, v);
  281. return(NULL);
  282. }
  283. cuddRef(u);
  284. Cudd_RecursiveDeref(dd, v);
  285. if (i>0) {
  286. w = Cudd_addIte(dd, y[i], u1, t1);
  287. if (w == NULL) {
  288. Cudd_RecursiveDeref(dd, u1);
  289. Cudd_RecursiveDeref(dd, t1);
  290. Cudd_RecursiveDeref(dd, u);
  291. return(NULL);
  292. }
  293. cuddRef(w);
  294. t = Cudd_addIte(dd, x[i], w, t1);
  295. if (u == NULL) {
  296. Cudd_RecursiveDeref(dd, u1);
  297. Cudd_RecursiveDeref(dd, t1);
  298. Cudd_RecursiveDeref(dd, u);
  299. Cudd_RecursiveDeref(dd, w);
  300. return(NULL);
  301. }
  302. cuddRef(t);
  303. Cudd_RecursiveDeref(dd, w);
  304. }
  305. Cudd_RecursiveDeref(dd, u1);
  306. Cudd_RecursiveDeref(dd, t1);
  307. }
  308. cuddDeref(u);
  309. return(u);
  310. } /* end of addWalshInt */