cuddZddPort.c 11.5 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9
/**CFile***********************************************************************

  FileName    [cuddZddPort.c]

  PackageName [cudd]

  Synopsis    [Functions that translate BDDs to ZDDs.]

  Description [External procedures included in this module:
10 11 12 13 14 15 16 17 18 19 20 21 22
                    <ul>
                    <li> Cudd_zddPortFromBdd()
                    <li> Cudd_zddPortToBdd()
                    </ul>
               Internal procedures included in this module:
                    <ul>
                    </ul>
               Static procedures included in this module:
                    <ul>
                    <li> zddPortFromBddStep()
                    <li> zddPortToBddStep()
                    </ul>
              ]
Alan Mishchenko committed
23 24 25 26 27

  SeeAlso     []

  Author      [Hyong-kyoon Shin, In-Ho Moon]

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
  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
59 60 61

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

62
#include "misc/util/util_hack.h"
63
#include "cuddInt.h"
Alan Mishchenko committed
64

65 66 67
ABC_NAMESPACE_IMPL_START


68

Alan Mishchenko committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
/*---------------------------------------------------------------------------*/
/* Constant declarations                                                     */
/*---------------------------------------------------------------------------*/


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


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


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

#ifndef lint
89
static char rcsid[] DD_UNUSED = "$Id: cuddZddPort.c,v 1.13 2004/08/13 18:04:53 fabio Exp $";
Alan Mishchenko committed
90 91 92 93 94 95 96 97 98 99 100 101 102
#endif

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


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

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

103 104
static DdNode * zddPortFromBddStep (DdManager *dd, DdNode *B, int expected);
static DdNode * zddPortToBddStep (DdManager *dd, DdNode *f, int depth);
Alan Mishchenko committed
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

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


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


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

  Synopsis [Converts a BDD into a ZDD.]

  Description [Converts a BDD into a ZDD. This function assumes that
  there is a one-to-one correspondence between the BDD variables and the
  ZDD variables, and that the variable order is the same for both types
  of variables. These conditions are established if the ZDD variables
  are created by one call to Cudd_zddVarsFromBddVars with multiplicity =
  1. Returns a pointer to the resulting ZDD if successful; NULL otherwise.]

  SideEffects [None]

  SeeAlso     [Cudd_zddVarsFromBddVars]

******************************************************************************/
DdNode *
Cudd_zddPortFromBdd(
  DdManager * dd,
  DdNode * B)
{
    DdNode *res;

    do {
138 139
        dd->reordered = 0;
        res = zddPortFromBddStep(dd,B,0);
Alan Mishchenko committed
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
    } while (dd->reordered == 1);

    return(res);

} /* end of Cudd_zddPortFromBdd */


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

  Synopsis [Converts a ZDD into a BDD.]

  Description [Converts a ZDD into a BDD. Returns a pointer to the resulting
  ZDD if successful; NULL otherwise.]

  SideEffects [None]

  SeeAlso     [Cudd_zddPortFromBdd]

******************************************************************************/
DdNode *
Cudd_zddPortToBdd(
  DdManager * dd,
  DdNode * f)
{
    DdNode *res;

    do {
167 168
        dd->reordered = 0;
        res = zddPortToBddStep(dd,f,0);
Alan Mishchenko committed
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
    } while (dd->reordered == 1);

    return(res);

} /* end of Cudd_zddPortToBdd */


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

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


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

  Synopsis [Performs the recursive step of Cudd_zddPortFromBdd.]

  Description []

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static DdNode *
zddPortFromBddStep(
  DdManager * dd,
  DdNode * B,
  int  expected)
{
202 203 204
    DdNode      *res, *prevZdd, *t, *e;
    DdNode      *Breg, *Bt, *Be;
    int         id, level;
Alan Mishchenko committed
205 206 207 208

    statLine(dd);
    /* Terminal cases. */
    if (B == Cudd_Not(DD_ONE(dd)))
209
        return(DD_ZERO(dd));
Alan Mishchenko committed
210
    if (B == DD_ONE(dd)) {
211 212 213 214 215
        if (expected >= dd->sizeZ) {
            return(DD_ONE(dd));
        } else {
            return(dd->univ[expected]);
        }
Alan Mishchenko committed
216 217 218 219 220 221 222
    }

    Breg = Cudd_Regular(B);

    /* Computed table look-up. */
    res = cuddCacheLookup1Zdd(dd,Cudd_zddPortFromBdd,B);
    if (res != NULL) {
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
        level = cuddI(dd,Breg->index);
        /* Adding DC vars. */
        if (expected < level) {
            /* Add suppressed variables. */
            cuddRef(res);
            for (level--; level >= expected; level--) {
                prevZdd = res;
                id = dd->invperm[level];
                res = cuddZddGetNode(dd, id, prevZdd, prevZdd);
                if (res == NULL) {
                    Cudd_RecursiveDerefZdd(dd, prevZdd);
                    return(NULL);
                }
                cuddRef(res);
                Cudd_RecursiveDerefZdd(dd, prevZdd);
            }
            cuddDeref(res);
Alan Mishchenko committed
240
        }
241 242
        return(res);
    }   /* end of cache look-up */
Alan Mishchenko committed
243 244

    if (Cudd_IsComplement(B)) {
245 246
        Bt = Cudd_Not(cuddT(Breg));
        Be = Cudd_Not(cuddE(Breg));
Alan Mishchenko committed
247
    } else {
248 249
        Bt = cuddT(Breg);
        Be = cuddE(Breg);
Alan Mishchenko committed
250 251 252 253 254 255 256 257 258
    }

    id = Breg->index;
    level = cuddI(dd,id);
    t = zddPortFromBddStep(dd, Bt, level+1);
    if (t == NULL) return(NULL);
    cuddRef(t);
    e = zddPortFromBddStep(dd, Be, level+1);
    if (e == NULL) {
259 260
        Cudd_RecursiveDerefZdd(dd, t);
        return(NULL);
Alan Mishchenko committed
261 262 263 264
    }
    cuddRef(e);
    res = cuddZddGetNode(dd, id, t, e);
    if (res == NULL) {
265 266 267
        Cudd_RecursiveDerefZdd(dd, t);
        Cudd_RecursiveDerefZdd(dd, e);
        return(NULL);
Alan Mishchenko committed
268 269 270 271 272 273 274 275
    }
    cuddRef(res);
    Cudd_RecursiveDerefZdd(dd, t);
    Cudd_RecursiveDerefZdd(dd, e);

    cuddCacheInsert1(dd,Cudd_zddPortFromBdd,B,res);

    for (level--; level >= expected; level--) {
276 277 278 279 280 281 282 283
        prevZdd = res;
        id = dd->invperm[level];
        res = cuddZddGetNode(dd, id, prevZdd, prevZdd);
        if (res == NULL) {
            Cudd_RecursiveDerefZdd(dd, prevZdd);
            return(NULL);
        }
        cuddRef(res);
Alan Mishchenko committed
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
        Cudd_RecursiveDerefZdd(dd, prevZdd);
    }

    cuddDeref(res);
    return(res);

} /* end of zddPortFromBddStep */


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

  Synopsis [Performs the recursive step of Cudd_zddPortToBdd.]

  Description []

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static DdNode *
zddPortToBddStep(
  DdManager * dd /* manager */,
  DdNode * f /* ZDD to be converted */,
  int  depth /* recursion depth */)
{
    DdNode *one, *zero, *T, *E, *res, *var;
    unsigned int index;
    unsigned int level;

    statLine(dd);
    one = DD_ONE(dd);
    zero = DD_ZERO(dd);
    if (f == zero) return(Cudd_Not(one));

    if (depth == dd->sizeZ) return(one);

    index = dd->invpermZ[depth];
    level = cuddIZ(dd,f->index);
    var = cuddUniqueInter(dd,index,one,Cudd_Not(one));
    if (var == NULL) return(NULL);
    cuddRef(var);

    if (level > (unsigned) depth) {
328 329 330 331 332 333 334 335 336 337 338 339 340
        E = zddPortToBddStep(dd,f,depth+1);
        if (E == NULL) {
            Cudd_RecursiveDeref(dd,var);
            return(NULL);
        }
        cuddRef(E);
        res = cuddBddIteRecur(dd,var,Cudd_Not(one),E);
        if (res == NULL) {
            Cudd_RecursiveDeref(dd,var);
            Cudd_RecursiveDeref(dd,E);
            return(NULL);
        }
        cuddRef(res);
Alan Mishchenko committed
341 342
        Cudd_RecursiveDeref(dd,var);
        Cudd_RecursiveDeref(dd,E);
343 344
        cuddDeref(res);
        return(res);
Alan Mishchenko committed
345 346 347 348
    }

    res = cuddCacheLookup1(dd,Cudd_zddPortToBdd,f);
    if (res != NULL) {
349 350
        Cudd_RecursiveDeref(dd,var);
        return(res);
Alan Mishchenko committed
351 352 353 354
    }

    T = zddPortToBddStep(dd,cuddT(f),depth+1);
    if (T == NULL) {
355 356
        Cudd_RecursiveDeref(dd,var);
        return(NULL);
Alan Mishchenko committed
357 358 359 360
    }
    cuddRef(T);
    E = zddPortToBddStep(dd,cuddE(f),depth+1);
    if (E == NULL) {
361 362 363
        Cudd_RecursiveDeref(dd,var);
        Cudd_RecursiveDeref(dd,T);
        return(NULL);
Alan Mishchenko committed
364 365 366 367 368
    }
    cuddRef(E);

    res = cuddBddIteRecur(dd,var,T,E);
    if (res == NULL) {
369 370 371 372
        Cudd_RecursiveDeref(dd,var);
        Cudd_RecursiveDeref(dd,T);
        Cudd_RecursiveDeref(dd,E);
        return(NULL);
Alan Mishchenko committed
373 374 375 376 377 378 379 380 381 382 383 384 385
    }
    cuddRef(res);
    Cudd_RecursiveDeref(dd,var);
    Cudd_RecursiveDeref(dd,T);
    Cudd_RecursiveDeref(dd,E);
    cuddDeref(res);

    cuddCacheInsert1(dd,Cudd_zddPortToBdd,f,res);

    return(res);

} /* end of zddPortToBddStep */

386

387 388
ABC_NAMESPACE_IMPL_END

389