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.

391 lines
12 KiB

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