cuddAddWalsh.c 12.8 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9 10
/**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:
11 12 13 14 15 16 17 18
            <ul>
            <li> Cudd_addWalsh()
            <li> Cudd_addResidue()
            </ul>
        Static procedures included in this module:
            <ul>
            <li> addWalshInt()
            </ul>]
Alan Mishchenko committed
19 20 21

  Author      [Fabio Somenzi]

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
  Copyright   [Copyright (c) 1995-2004, Regents of the University of Colorado

  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:

  Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.

  Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution.

  Neither the name of the University of Colorado nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  POSSIBILITY OF SUCH DAMAGE.]
Alan Mishchenko committed
53 54 55

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

56
#include "misc/util/util_hack.h"
Alan Mishchenko committed
57 58
#include "cuddInt.h"

59 60 61
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
62

63

Alan Mishchenko committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
/*---------------------------------------------------------------------------*/
/* Constant declarations                                                     */
/*---------------------------------------------------------------------------*/


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


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


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

#ifndef lint
84
static char rcsid[] DD_UNUSED = "$Id: cuddAddWalsh.c,v 1.10 2008/04/17 21:17:11 fabio Exp $";
Alan Mishchenko committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98
#endif


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


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

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

99
static DdNode * addWalshInt (DdManager *dd, DdNode **x, DdNode **y, int n);
Alan Mishchenko committed
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

/**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 {
129 130
        dd->reordered = 0;
        res = addWalshInt(dd, x, y, n);
Alan Mishchenko committed
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
    } 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 */)
{
168 169
    int msbLsb; /* MSB on top (1) or LSB on top (0) */
    int tc;     /* two's complement (1) or unsigned (0) */
Alan Mishchenko committed
170 171 172 173 174 175 176 177 178 179
    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. */
Alan Mishchenko committed
180
    array[0] = ABC_ALLOC(DdNode *,m);
Alan Mishchenko committed
181
    if (array[0] == NULL) {
182 183
        dd->errorCode = CUDD_MEMORY_OUT;
        return(NULL);
Alan Mishchenko committed
184
    }
Alan Mishchenko committed
185
    array[1] = ABC_ALLOC(DdNode *,m);
Alan Mishchenko committed
186
    if (array[1] == NULL) {
187 188 189
        ABC_FREE(array[0]);
        dd->errorCode = CUDD_MEMORY_OUT;
        return(NULL);
Alan Mishchenko committed
190 191
    }
    for (i = 0; i < m; i++) {
192
        array[0][i] = array[1][i] = NULL;
Alan Mishchenko committed
193 194 195 196
    }

    /* Initialize residues. */
    for (i = 0; i < m; i++) {
197 198 199 200 201 202 203 204
        tmp = cuddUniqueConst(dd,(CUDD_VALUE_TYPE) i);
        if (tmp == NULL) {
            for (j = 0; j < i; j++) {
                Cudd_RecursiveDeref(dd,array[1][j]);
            }
            ABC_FREE(array[0]);
            ABC_FREE(array[1]);
            return(NULL);
Alan Mishchenko committed
205
        }
206 207
        cuddRef(tmp);
        array[1][i] = tmp;
Alan Mishchenko committed
208 209 210
    }

    /* Main iteration. */
211
    residue = 1;        /* residue of 2**0 */
Alan Mishchenko committed
212
    for (k = 0; k < n; k++) {
213 214 215 216 217 218 219 220
        /* 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;
Alan Mishchenko committed
221
        }
222 223 224 225 226 227 228 229
        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]);
            }
            ABC_FREE(array[0]);
            ABC_FREE(array[1]);
            return(NULL);
Alan Mishchenko committed
230
        }
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
        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]);
                }
                ABC_FREE(array[0]);
                ABC_FREE(array[1]);
                return(NULL);
            }
            cuddRef(tmp);
            array[thisOne][i] = tmp;
Alan Mishchenko committed
248
        }
249 250 251 252 253 254 255 256 257 258
        /* 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;
Alan Mishchenko committed
259 260 261 262 263
        }
    }

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

Alan Mishchenko committed
268 269
    ABC_FREE(array[0]);
    ABC_FREE(array[1]);
Alan Mishchenko committed
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

    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;
Alan Mishchenko committed
305
    DdNode *t = NULL, *u = NULL, *t1, *u1, *v, *w;
Alan Mishchenko committed
306 307 308 309 310 311 312 313 314 315 316
    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) {
317 318
        Cudd_RecursiveDeref(dd, minusone);
        return(NULL);
Alan Mishchenko committed
319 320 321 322
    }
    cuddRef(v);
    u = Cudd_addIte(dd, x[n-1], v, one);
    if (u == NULL) {
323 324 325
        Cudd_RecursiveDeref(dd, minusone);
        Cudd_RecursiveDeref(dd, v);
        return(NULL);
Alan Mishchenko committed
326 327 328 329
    }
    cuddRef(u);
    Cudd_RecursiveDeref(dd, v);
    if (n>1) {
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
        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);
Alan Mishchenko committed
345 346 347 348 349 350
        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--) {
351 352 353 354 355 356
        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);
Alan Mishchenko committed
357
        }
358 359
        cuddRef(v);
        u = Cudd_addIte(dd, x[i], v, u1);
Alan Mishchenko committed
360
        if (u == NULL) {
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
            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 (w == 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);
        }
Alan Mishchenko committed
388 389 390 391 392 393 394 395
        Cudd_RecursiveDeref(dd, u1);
        Cudd_RecursiveDeref(dd, t1);
    }

    cuddDeref(u);
    return(u);

} /* end of addWalshInt */
396 397


398 399
ABC_NAMESPACE_IMPL_END