summaryrefslogtreecommitdiffstats
path: root/src/bdd/cudd/cuddAddWalsh.c
blob: 980ee2157369e8e0b45a3d6414752e9a576be6b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/**CFile***********************************************************************

  FileName    [cuddAddWalsh.c]

  PackageName [cudd]

  Synopsis    [Functions that generate Walsh matrices and residue
  functions in ADD form.]

  Description [External procedures included in this module:
        <ul>
        <li> Cudd_addWalsh()
        <li> Cudd_addResidue()
        </ul>
    Static procedures included in this module:
        <ul>
        <li> addWalshInt()
        </ul>]

  Author      [Fabio Somenzi]

  Copyright   [This file was created at the University of Colorado at
  Boulder.  The University of Colorado at Boulder makes no warranty
  about the suitability of this software for any purpose.  It is
  presented on an AS IS basis.]

******************************************************************************/

#include "util.h"
#include "cuddInt.h"


/*---------------------------------------------------------------------------*/
/* Constant declarations                                                     */
/*---------------------------------------------------------------------------*/


/*---------------------------------------------------------------------------*/
/* Stucture declarations                                                     */
/*---------------------------------------------------------------------------*/


/*---------------------------------------------------------------------------*/
/* Type declarations                                                         */
/*---------------------------------------------------------------------------*/


/*---------------------------------------------------------------------------*/
/* Variable declarations                                                     */
/*---------------------------------------------------------------------------*/

#ifndef lint
static char rcsid[] DD_UNUSED = "$Id: cuddAddWalsh.c,v 1.1.1.1 2003/02/24 22:23:50 wjiang Exp $";
#endif


/*---------------------------------------------------------------------------*/
/* Macro declarations                                                        */
/*---------------------------------------------------------------------------*/


/**AutomaticStart*************************************************************/

/*---------------------------------------------------------------------------*/
/* Static function prototypes                                                */
/*---------------------------------------------------------------------------*/

static DdNode * addWalshInt ARGS((DdManager *dd, DdNode **x, DdNode **y, int n));

/**AutomaticEnd***************************************************************/


/*---------------------------------------------------------------------------*/
/* Definition of exported functions                                          */
/*---------------------------------------------------------------------------*/


/**Function********************************************************************

  Synopsis    [Generates a Walsh matrix in ADD form.]

  Description [Generates a Walsh matrix in ADD form. Returns a pointer
  to the matrixi if successful; NULL otherwise.]

  SideEffects [None]

******************************************************************************/
DdNode *
Cudd_addWalsh(
  DdManager * dd,
  DdNode ** x,
  DdNode ** y,
  int  n)
{
    DdNode *res;

    do {
    dd->reordered = 0;
    res = addWalshInt(dd, x, y, n);
    } while (dd->reordered == 1);
    return(res);

} /* end of Cudd_addWalsh */


/**Function********************************************************************

  Synopsis    [Builds an ADD for the residue modulo m of an n-bit
  number.]

  Description [Builds an ADD for the residue modulo m of an n-bit
  number. The modulus must be at least 2, and the number of bits at
  least 1. Parameter options specifies whether the MSB should be on top
  or the LSB; and whther the number whose residue is computed is in
  two's complement notation or not. The macro CUDD_RESIDUE_DEFAULT
  specifies LSB on top and unsigned number. The macro CUDD_RESIDUE_MSB
  specifies MSB on top, and the macro CUDD_RESIDUE_TC specifies two's
  complement residue. To request MSB on top and two's complement residue
  simultaneously, one can OR the two macros:
  CUDD_RESIDUE_MSB | CUDD_RESIDUE_TC.
  Cudd_addResidue returns a pointer to the resulting ADD if successful;
  NULL otherwise.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
DdNode *
Cudd_addResidue(
  DdManager * dd /* manager */,
  int  n /* number of bits */,
  int  m /* modulus */,
  int  options /* options */,
  int  top /* index of top variable */)
{
    int msbLsb;    /* MSB on top (1) or LSB on top (0) */
    int tc;    /* two's complement (1) or unsigned (0) */
    int i, j, k, t, residue, thisOne, previous, index;
    DdNode **array[2], *var, *tmp, *res;

    /* Sanity check. */
    if (n < 1 && m < 2) return(NULL);

    msbLsb = options & CUDD_RESIDUE_MSB;
    tc = options & CUDD_RESIDUE_TC;

    /* Allocate and initialize working arrays. */
    array[0] = ALLOC(DdNode *,m);
    if (array[0] == NULL) {
    dd->errorCode = CUDD_MEMORY_OUT;
    return(NULL);
    }
    array[1] = ALLOC(DdNode *,m);
    if (array[1] == NULL) {
    FREE(array[0]);
    dd->errorCode = CUDD_MEMORY_OUT;
    return(NULL);
    }
    for (i = 0; i < m; i++) {
    array[0][i] = array[1][i] = NULL;
    }

    /* Initialize residues. */
    for (i = 0; i < m; i++) {
    tmp = cuddUniqueConst(dd,(CUDD_VALUE_TYPE) i);
    if (tmp == NULL) {
        for (j = 0; j < i; j++) {
        Cudd_RecursiveDeref(dd,array[1][j]);
        }
        FREE(array[0]);
        FREE(array[1]);
        return(NULL);
    }
    cuddRef(tmp);
    array[1][i] = tmp;
    }

    /* Main iteration. */
    residue = 1;    /* residue of 2**0 */
    for (k = 0; k < n; k++) {
    /* Choose current and previous arrays. */
    thisOne = k & 1;
    previous = thisOne ^ 1;
    /* Build an ADD projection function. */
    if (msbLsb) {
        index = top+n-k-1;
    } else {
        index = top+k;
    }
    var = cuddUniqueInter(dd,index,DD_ONE(dd),DD_ZERO(dd));
    if (var == NULL) {
        for (j = 0; j < m; j++) {
        Cudd_RecursiveDeref(dd,array[previous][j]);
        }
        FREE(array[0]);
        FREE(array[1]);
        return(NULL);
    }
    cuddRef(var);
    for (i = 0; i < m; i ++) {
        t = (i + residue) % m;
        tmp = Cudd_addIte(dd,var,array[previous][t],array[previous][i]);
        if (tmp == NULL) {
        for (j = 0; j < i; j++) {
            Cudd_RecursiveDeref(dd,array[thisOne][j]);
        }
        for (j = 0; j < m; j++) {
            Cudd_RecursiveDeref(dd,array[previous][j]);
        }
        FREE(array[0]);
        FREE(array[1]);
        return(NULL);
        }
        cuddRef(tmp);
        array[thisOne][i] = tmp;
    }
    /* One layer completed. Free the other array for the next iteration. */
    for (i = 0; i < m; i++) {
        Cudd_RecursiveDeref(dd,array[previous][i]);
    }
    Cudd_RecursiveDeref(dd,var);
    /* Update residue of 2**k. */
    residue = (2 * residue) % m;
    /* Adjust residue for MSB, if this is a two's complement number. */
    if (tc && (k == n - 1)) {
        residue = (m - residue) % m;
    }
    }

    /* We are only interested in the 0-residue node of the top layer. */
    for (i = 1; i < m; i++) {
    Cudd_RecursiveDeref(dd,array[(n - 1) & 1][i]);
    }
    res = array[(n - 1) & 1][0];

    FREE(array[0]);
    FREE(array[1]);

    cuddDeref(res);
    return(res);

} /* end of Cudd_addResidue */


/*---------------------------------------------------------------------------*/
/* Definition of internal functions                                          */
/*---------------------------------------------------------------------------*/


/*---------------------------------------------------------------------------*/
/* Definition of static functions                                            */
/*---------------------------------------------------------------------------*/


/**Function********************************************************************

  Synopsis    [Implements the recursive step of Cudd_addWalsh.]

  Description [Generates a Walsh matrix in ADD form. Returns a pointer
  to the matrixi if successful; NULL otherwise.]

  SideEffects [None]

******************************************************************************/
static DdNode *
addWalshInt(
  DdManager * dd,
  DdNode ** x,
  DdNode ** y,
  int  n)
{
    DdNode *one, *minusone;
    DdNode *t, *u, *t1, *u1, *v, *w;
    int     i;

    one = DD_ONE(dd);
    if (n == 0) return(one);

    /* Build bottom part of ADD outside loop */
    minusone = cuddUniqueConst(dd,(CUDD_VALUE_TYPE) -1);
    if (minusone == NULL) return(NULL);
    cuddRef(minusone);
    v = Cudd_addIte(dd, y[n-1], minusone, one);
    if (v == NULL) {
    Cudd_RecursiveDeref(dd, minusone);
    return(NULL);
    }
    cuddRef(v);
    u = Cudd_addIte(dd, x[n-1], v, one);
    if (u == NULL) {
    Cudd_RecursiveDeref(dd, minusone);
    Cudd_RecursiveDeref(dd, v);
    return(NULL);
    }
    cuddRef(u);
    Cudd_RecursiveDeref(dd, v);
    if (n>1) {
    w = Cudd_addIte(dd, y[n-1], one, minusone);
    if (w == NULL) {
        Cudd_RecursiveDeref(dd, minusone);
        Cudd_RecursiveDeref(dd, u);
        return(NULL);
    }
    cuddRef(w);
    t = Cudd_addIte(dd, x[n-1], w, minusone);
    if (t == NULL) {
        Cudd_RecursiveDeref(dd, minusone);
        Cudd_RecursiveDeref(dd, u);
        Cudd_RecursiveDeref(dd, w);
        return(NULL);
    }
    cuddRef(t);
    Cudd_RecursiveDeref(dd, w);
    }
    cuddDeref(minusone); /* minusone is in the result; it won't die */

    /* Loop to build the rest of the ADD */
    for (i=n-2; i>=0; i--) {
    t1 = t; u1 = u;
    v = Cudd_addIte(dd, y[i], t1, u1);
    if (v == NULL) {
        Cudd_RecursiveDeref(dd, u1);
        Cudd_RecursiveDeref(dd, t1);
        return(NULL);
    }
    cuddRef(v);
    u = Cudd_addIte(dd, x[i], v, u1);
    if (u == NULL) {
        Cudd_RecursiveDeref(dd, u1);
        Cudd_RecursiveDeref(dd, t1);
        Cudd_RecursiveDeref(dd, v);
        return(NULL);
    }
    cuddRef(u);
    Cudd_RecursiveDeref(dd, v);
    if (i>0) {
        w = Cudd_addIte(dd, y[i], u1, t1);
        if (u == NULL) {
        Cudd_RecursiveDeref(dd, u1);
        Cudd_RecursiveDeref(dd, t1);
        Cudd_RecursiveDeref(dd, u);
        return(NULL);
        }
        cuddRef(w);
        t = Cudd_addIte(dd, x[i], w, t1);
        if (u == NULL) {
        Cudd_RecursiveDeref(dd, u1);
        Cudd_RecursiveDeref(dd, t1);
        Cudd_RecursiveDeref(dd, u);
        Cudd_RecursiveDeref(dd, w);
        return(NULL);
        }
        cuddRef(t);
        Cudd_RecursiveDeref(dd, w);
    }
    Cudd_RecursiveDeref(dd, u1);
    Cudd_RecursiveDeref(dd, t1);
    }

    cuddDeref(u);
    return(u);

} /* end of addWalshInt */