extraBddThresh.c 25.2 KB
Newer Older
1 2
/**CFile****************************************************************

3
 FileName    [extraBddThresh.c]
4

5
 SystemName  [ABC: Logic synthesis and verification system.]
6

7
 PackageName [extra]
8

9
 Synopsis    [Dealing with threshold functions.]
10

11
 Author      [Augusto Neutzling, Jody Matos, and Alan Mishchenko (UC Berkeley).]
12

13
 Affiliation [Federal University of Rio Grande do Sul, Brazil]
14

15
 Date        [Ver. 1.0. Started - October 7, 2014.]
16

17
 Revision    [$Id: extraBddThresh.c,v 1.0 2014/10/07 00:00:00 alanmi Exp $]
18 19

 ***********************************************************************/
20 21 22 23 24

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
25 26 27 28
#include "base/main/main.h"
#include "misc/extra/extra.h"
#include "bdd/cudd/cudd.h"
#include "bool/kit/kit.h"
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

#include "misc/vec/vec.h"
#include "misc/util/utilTruth.h"

ABC_NAMESPACE_IMPL_START

////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

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

45 46 47
 Synopsis    [Checks thresholdness of the function.]

 Description []
48

49
 SideEffects []
50

51
 SeeAlso     []
52

53 54
 ***********************************************************************/
void Extra_ThreshPrintChow(int Chow0, int * pChow, int nVars) {
55
    int i;
56 57 58
    for (i = 0; i < nVars; i++)
        printf("%d ", pChow[i]);
    printf("  %d\n", Chow0);
59
}
60
int Extra_ThreshComputeChow(word * t, int nVars, int * pChow) {
61
    int i, k, Chow0 = 0, nMints = (1 << nVars);
62
    memset(pChow, 0, sizeof(int) * nVars);
63
    // compute Chow coefs
64 65 66 67
    for (i = 0; i < nMints; i++)
        if (Abc_TtGetBit(t, i))
            for (Chow0++, k = 0; k < nVars; k++)
                if ((i >> k) & 1)
68 69
                    pChow[k]++;
    // compute modified Chow coefs
70
    for (k = 0; k < nVars; k++)
71
        pChow[k] = 2 * pChow[k] - Chow0;
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    return Chow0 - (1 << (nVars - 1));
}
void Extra_ThreshSortByChow(word * t, int nVars, int * pChow) {
    int i, nWords = Abc_TtWordNum(nVars);
    //sort the variables by Chow in ascending order
    while (1) {
        int fChange = 0;
        for (i = 0; i < nVars - 1; i++) {
            if (pChow[i] >= pChow[i + 1])
                continue;
            ABC_SWAP(int, pChow[i], pChow[i + 1]);
            Abc_TtSwapAdjacent(t, nWords, i);
            fChange = 1;
        }
        if (!fChange)
            return;
    }
89
}
90
void Extra_ThreshSortByChowInverted(word * t, int nVars, int * pChow) {
91
    int i, nWords = Abc_TtWordNum(nVars);
92 93
    //sort the variables by Chow in descending order
    while (1) {
94
        int fChange = 0;
95 96
        for (i = 0; i < nVars - 1; i++) {
            if (pChow[i] <= pChow[i + 1])
97
                continue;
98 99
            ABC_SWAP(int, pChow[i], pChow[i + 1]);
            Abc_TtSwapAdjacent(t, nWords, i);
100 101
            fChange = 1;
        }
102
        if (!fChange)
103 104 105
            return;
    }
}
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
int Extra_ThreshInitializeChow(int nVars, int * pChow) {
    int i = 0, Aux[16], nChows = 0;
    //group the variables which have the same Chow
    for (i = 0; i < nVars; i++) {
        if (i == 0 || (pChow[i] == pChow[i - 1]))
            Aux[i] = nChows;
        else {
            nChows++;
            Aux[i] = nChows;
        }
    }
    for (i = 0; i < nVars; i++)
        pChow[i] = Aux[i];
    nChows++;
    return nChows;

}
static inline int Extra_ThreshWeightedSum(int * pW, int nVars, int m) {
124
    int i, Cost = 0;
125 126
    for (i = 0; i < nVars; i++)
        if ((m >> i) & 1)
127 128 129
            Cost += pW[i];
    return Cost;
}
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
static inline int Extra_ThreshCubeWeightedSum1(int * pWofChow, int * pChow,
        char * pIsop, int nVars, int j) {
    int k, Cost = 0;
    for (k = j; k < j + nVars; k++)
        if (pIsop[k] == '1')
            Cost += pWofChow[pChow[k - j]];
    return Cost;
}
static inline int Extra_ThreshCubeWeightedSum2(int * pWofChow, int * pChow,
        char * pIsop, int nVars, int j) {
    int k, Cost = 0;
    for (k = j; k < j + nVars; k++)
        if (pIsop[k] == '-')
            Cost += pWofChow[pChow[k - j]];
    return Cost;
}

static inline int Extra_ThreshCubeWeightedSum3(int * pWofChow, int nChows,
        unsigned long ** pGreaters, int j) {
    int i, Cost = 0;
    for (i = 0; i < nChows; i++)
        Cost += pWofChow[i] * pGreaters[j][i];
    return Cost;
}
static inline int Extra_ThreshCubeWeightedSum4(int * pWofChow, int nChows,
        unsigned long ** pSmallers, int j) {
    int i, Cost = 0;
    for (i = 0; i < nChows; i++)
        Cost += pWofChow[i] * pSmallers[j][i];
    return Cost;
}
int Extra_ThreshSelectWeights3(word * t, int nVars, int * pW) {
162
    int m, Lmin, Lmax, nMints = (1 << nVars);
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
    assert(nVars == 3);
    for (pW[2] = 1; pW[2] <= nVars; pW[2]++)
        for (pW[1] = pW[2]; pW[1] <= nVars; pW[1]++)
            for (pW[0] = pW[1]; pW[0] <= nVars; pW[0]++) {
                Lmin = 10000;
                Lmax = 0;
                for (m = 0; m < nMints; m++) {
                    if (Abc_TtGetBit(t, m))
                        Lmin = Abc_MinInt(Lmin,
                                Extra_ThreshWeightedSum(pW, nVars, m));
                    else
                        Lmax = Abc_MaxInt(Lmax,
                                Extra_ThreshWeightedSum(pW, nVars, m));
                    if (Lmax >= Lmin)
                        break;
178
//            printf( "%c%d ", Abc_TtGetBit(t, m) ? '+' : '-', Extra_ThreshWeightedSum(pW, nVars, m) );
179
                }
180
//        printf( "  -%d +%d\n", Lmax, Lmin );
181 182 183 184 185
                if (m < nMints)
                    continue;
                assert(Lmax < Lmin);
                return Lmin;
            }
186 187
    return 0;
}
188
int Extra_ThreshSelectWeights4(word * t, int nVars, int * pW) {
189
    int m, Lmin, Lmax, nMints = (1 << nVars);
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    assert(nVars == 4);
    for (pW[3] = 1; pW[3] <= nVars; pW[3]++)
        for (pW[2] = pW[3]; pW[2] <= nVars; pW[2]++)
            for (pW[1] = pW[2]; pW[1] <= nVars; pW[1]++)
                for (pW[0] = pW[1]; pW[0] <= nVars; pW[0]++) {
                    Lmin = 10000;
                    Lmax = 0;
                    for (m = 0; m < nMints; m++) {
                        if (Abc_TtGetBit(t, m))
                            Lmin = Abc_MinInt(Lmin,
                                    Extra_ThreshWeightedSum(pW, nVars, m));
                        else
                            Lmax = Abc_MaxInt(Lmax,
                                    Extra_ThreshWeightedSum(pW, nVars, m));
                        if (Lmax >= Lmin)
                            break;
                    }
                    if (m < nMints)
                        continue;
                    assert(Lmax < Lmin);
                    return Lmin;
                }
212 213
    return 0;
}
214
int Extra_ThreshSelectWeights5(word * t, int nVars, int * pW) {
215
    int m, Lmin, Lmax, nMints = (1 << nVars), Limit = nVars + 0;
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
    assert(nVars == 5);
    for (pW[4] = 1; pW[4] <= Limit; pW[4]++)
        for (pW[3] = pW[4]; pW[3] <= Limit; pW[3]++)
            for (pW[2] = pW[3]; pW[2] <= Limit; pW[2]++)
                for (pW[1] = pW[2]; pW[1] <= Limit; pW[1]++)
                    for (pW[0] = pW[1]; pW[0] <= Limit; pW[0]++) {
                        Lmin = 10000;
                        Lmax = 0;
                        for (m = 0; m < nMints; m++) {
                            if (Abc_TtGetBit(t, m))
                                Lmin = Abc_MinInt(Lmin,
                                        Extra_ThreshWeightedSum(pW, nVars, m));
                            else
                                Lmax = Abc_MaxInt(Lmax,
                                        Extra_ThreshWeightedSum(pW, nVars, m));
                            if (Lmax >= Lmin)
                                break;
                        }
                        if (m < nMints)
                            continue;
                        assert(Lmax < Lmin);
                        return Lmin;
                    }
239 240
    return 0;
}
241
int Extra_ThreshSelectWeights6(word * t, int nVars, int * pW) {
242
    int m, Lmin, Lmax, nMints = (1 << nVars), Limit = nVars + 3;
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
    assert(nVars == 6);
    for (pW[5] = 1; pW[5] <= Limit; pW[5]++)
        for (pW[4] = pW[5]; pW[4] <= Limit; pW[4]++)
            for (pW[3] = pW[4]; pW[3] <= Limit; pW[3]++)
                for (pW[2] = pW[3]; pW[2] <= Limit; pW[2]++)
                    for (pW[1] = pW[2]; pW[1] <= Limit; pW[1]++)
                        for (pW[0] = pW[1]; pW[0] <= Limit; pW[0]++) {
                            Lmin = 10000;
                            Lmax = 0;
                            for (m = 0; m < nMints; m++) {
                                if (Abc_TtGetBit(t, m))
                                    Lmin = Abc_MinInt(Lmin,
                                            Extra_ThreshWeightedSum(pW, nVars,
                                                    m));
                                else
                                    Lmax = Abc_MaxInt(Lmax,
                                            Extra_ThreshWeightedSum(pW, nVars,
                                                    m));
                                if (Lmax >= Lmin)
                                    break;
                            }
                            if (m < nMints)
                                continue;
                            assert(Lmax < Lmin);
                            return Lmin;
                        }
269 270
    return 0;
}
271
int Extra_ThreshSelectWeights7(word * t, int nVars, int * pW) {
272
    int m, Lmin, Lmax, nMints = (1 << nVars), Limit = nVars + 6;
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
    assert(nVars == 7);
    for (pW[6] = 1; pW[6] <= Limit; pW[6]++)
        for (pW[5] = pW[6]; pW[5] <= Limit; pW[5]++)
            for (pW[4] = pW[5]; pW[4] <= Limit; pW[4]++)
                for (pW[3] = pW[4]; pW[3] <= Limit; pW[3]++)
                    for (pW[2] = pW[3]; pW[2] <= Limit; pW[2]++)
                        for (pW[1] = pW[2]; pW[1] <= Limit; pW[1]++)
                            for (pW[0] = pW[1]; pW[0] <= Limit; pW[0]++) {
                                Lmin = 10000;
                                Lmax = 0;
                                for (m = 0; m < nMints; m++) {
                                    if (Abc_TtGetBit(t, m))
                                        Lmin = Abc_MinInt(Lmin,
                                                Extra_ThreshWeightedSum(pW,
                                                        nVars, m));
                                    else
                                        Lmax = Abc_MaxInt(Lmax,
                                                Extra_ThreshWeightedSum(pW,
                                                        nVars, m));
                                    if (Lmax >= Lmin)
                                        break;
                                }
                                if (m < nMints)
                                    continue;
                                assert(Lmax < Lmin);
                                return Lmin;
                            }
300 301
    return 0;
}
302
int Extra_ThreshSelectWeights8(word * t, int nVars, int * pW) {
303
    int m, Lmin, Lmax, nMints = (1 << nVars), Limit = nVars + 1; // <<-- incomplete detection to save runtime!
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
    assert(nVars == 8);
    for (pW[7] = 1; pW[7] <= Limit; pW[7]++)
        for (pW[6] = pW[7]; pW[6] <= Limit; pW[6]++)
            for (pW[5] = pW[6]; pW[5] <= Limit; pW[5]++)
                for (pW[4] = pW[5]; pW[4] <= Limit; pW[4]++)
                    for (pW[3] = pW[4]; pW[3] <= Limit; pW[3]++)
                        for (pW[2] = pW[3]; pW[2] <= Limit; pW[2]++)
                            for (pW[1] = pW[2]; pW[1] <= Limit; pW[1]++)
                                for (pW[0] = pW[1]; pW[0] <= Limit; pW[0]++) {
                                    Lmin = 10000;
                                    Lmax = 0;
                                    for (m = 0; m < nMints; m++) {
                                        if (Abc_TtGetBit(t, m))
                                            Lmin = Abc_MinInt(Lmin,
                                                    Extra_ThreshWeightedSum(pW,
                                                            nVars, m));
                                        else
                                            Lmax = Abc_MaxInt(Lmax,
                                                    Extra_ThreshWeightedSum(pW,
                                                            nVars, m));
                                        if (Lmax >= Lmin)
                                            break;
                                    }
                                    if (m < nMints)
                                        continue;
                                    assert(Lmax < Lmin);
                                    return Lmin;
                                }
332 333
    return 0;
}
334 335
int Extra_ThreshSelectWeights(word * t, int nVars, int * pW) {
    if (nVars <= 2)
336
        return (t[0] & 0xF) != 6 && (t[0] & 0xF) != 9;
337 338 339 340 341 342 343 344 345 346 347 348
    if (nVars == 3)
        return Extra_ThreshSelectWeights3(t, nVars, pW);
    if (nVars == 4)
        return Extra_ThreshSelectWeights4(t, nVars, pW);
    if (nVars == 5)
        return Extra_ThreshSelectWeights5(t, nVars, pW);
    if (nVars == 6)
        return Extra_ThreshSelectWeights6(t, nVars, pW);
    if (nVars == 7)
        return Extra_ThreshSelectWeights7(t, nVars, pW);
    if (nVars == 8)
        return Extra_ThreshSelectWeights8(t, nVars, pW);
349 350
    return 0;
}
351 352 353 354 355 356 357 358 359 360 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 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
void Extra_ThreshIncrementWeights(int nChows, int * pWofChow, int i) {
    int k;
    for (k = i; k < nChows; k++) {
        pWofChow[k]++;
    }
}
void Extra_ThreshDecrementWeights(int nChows, int * pWofChow, int i) {
    int k;
    for (k = i; k < nChows; k++) {
        pWofChow[k]--;
    }
}
void Extra_ThreshPrintInequalities(unsigned long ** pGreaters,
        unsigned long ** pSmallers, int nChows, int nInequalities) {
    int i = 0, k = 0;
    for (k = 0; k < nInequalities; k++) {
        printf("\n Inequality [%d] = ", k);
        for (i = 0; i < nChows; i++) {
            printf("%ld ", pGreaters[k][i]);
        }
        printf(" > ");

        for (i = 0; i < nChows; i++) {
            printf("%ld ", pSmallers[k][i]);
        }
    }
}
void Extra_ThreshCreateInequalities(char * pIsop, char * pIsopFneg, int nVars,
        int * pWofChow, int * pChow, int nChows, int nInequalities,
        unsigned long ** pGreaters, unsigned long ** pSmallers) {
    int i = 0, j = 0, k = 0, m = 0;

    int nCubesIsop = strlen(pIsop) / (nVars + 3);
    int nCubesIsopFneg = strlen(pIsopFneg) / (nVars + 3);

    for (k = 0; k < nCubesIsop * nCubesIsopFneg; k++)
        for (i = 0; i < nChows; i++) {
            pGreaters[k][i] = 0;
            pSmallers[k][i] = 0;
        }
    m = 0;
    for (i = 0; i < (int)strlen(pIsop); i += (nVars + 3))
        for (j = 0; j < nCubesIsopFneg; j++) {
            for (k = 0; k < nVars; k++)
                if (pIsop[i + k] == '1')
                    pGreaters[m][pChow[k]] = pGreaters[m][(pChow[k])] + 1;

            m++;
        }
    m = 0;
    for (i = 0; i < nCubesIsop; i++)
        for (j = 0; j < (int)strlen(pIsopFneg); j += (nVars + 3)) {
            for (k = 0; k < nVars; k++)
                if (pIsopFneg[j + k] == '-')
                    pSmallers[m][pChow[k]] = pSmallers[m][pChow[k]] + 1;
            m++;
        }
//        Extra_ThreshPrintInequalities( pGreaters, pSmallers, nChows, nInequalities);
//        printf( "\nInequalities was Created \n");
}

void Extra_ThreshSimplifyInequalities(int nInequalities, int nChows,
        unsigned long ** pGreaters, unsigned long ** pSmallers) {
    int i = 0, k = 0;

    for (k = 0; k < nInequalities; k++) {
        for (i = 0; i < nChows; i++) {
            if ((pGreaters[k][i]) == (pSmallers[k][i])) {
                pGreaters[k][i] = 0;
                pSmallers[k][i] = 0;
            } else if ((pGreaters[k][i]) > (pSmallers[k][i])) {
                pGreaters[k][i] = pGreaters[k][i] - pSmallers[k][i];
                pSmallers[k][i] = 0;
            } else {
                pSmallers[k][i] = pSmallers[k][i] - pGreaters[k][i];
                ;
                pGreaters[k][i] = 0;
            }
        }
    }
//        Extra_ThreshPrintInequalities( pGreaters, pSmallers, nChows, nInequalities);
//        printf( "\nInequalities was Siplified \n");
}
int Extra_ThreshAssignWeights(word * t, char * pIsop, char * pIsopFneg,
        int nVars, int * pW, int * pChow, int nChows, int Wmin) {

    int i = 0, j = 0, Lmin = 1000, Lmax = 0, Limit = nVars * 2, delta = 0,
            deltaOld = -1000, fIncremented = 0;
    //******************************
    int * pWofChow = ABC_ALLOC( int, nChows );
    int nCubesIsop = strlen(pIsop) / (nVars + 3);
    int nCubesIsopFneg = strlen(pIsopFneg) / (nVars + 3);
    int nInequalities = nCubesIsop * nCubesIsopFneg;
    unsigned long **pGreaters;
    unsigned long **pSmallers;

    pGreaters = malloc(nCubesIsop * nCubesIsopFneg * sizeof *pGreaters);
    for (i = 0; i < nCubesIsop * nCubesIsopFneg; i++) {
        pGreaters[i] = malloc(nChows * sizeof *pGreaters[i]);
    }
    pSmallers = malloc(nCubesIsop * nCubesIsopFneg * sizeof *pSmallers);
    for (i = 0; i < nCubesIsop * nCubesIsopFneg; i++) {
        pSmallers[i] = malloc(nChows * sizeof *pSmallers[i]);
    }

    //******************************
    Extra_ThreshCreateInequalities(pIsop, pIsopFneg, nVars, pWofChow, pChow,
            nChows, nInequalities, pGreaters, pSmallers);
    Extra_ThreshSimplifyInequalities(nInequalities, nChows, pGreaters,
            pSmallers);

    //initializes the weights
    pWofChow[0] = Wmin;
    for (i = 1; i < nChows; i++) {
        pWofChow[i] = pWofChow[i - 1] + 1;
    }
    i = 0;

    //assign the weights respecting the inequalities
    while (i < nChows && pWofChow[nChows - 1] <= Limit) {
        Lmin = 1000;
        Lmax = 0;

        while (j < nInequalities) {
            if (pGreaters[j][i] != 0) {
                Lmin = Extra_ThreshCubeWeightedSum3(pWofChow, nChows, pGreaters,
                        j);
                Lmax = Extra_ThreshCubeWeightedSum4(pWofChow, nChows, pSmallers,
                        j);
                delta = Lmin - Lmax;

                if (delta > 0) {
                    if (fIncremented == 1) {
                        j = 0;
                        fIncremented = 0;
                        deltaOld = -1000;
                    } else
                        j++;
                    continue;
                }
                if (delta > deltaOld) {
                    Extra_ThreshIncrementWeights(nChows, pWofChow, i);
                    deltaOld = delta;
                    fIncremented = 1;
                } else if (fIncremented == 1) {
                    Extra_ThreshDecrementWeights(nChows, pWofChow, i);
                    j++;
                    deltaOld = -1000;
                    fIncremented = 0;
                    continue;
                } else
                    j++;
            } else
                j++;

        }
        i++;
        j = 0;
    }

    //******************************
    for (i = 0; i < nCubesIsop * nCubesIsopFneg; i++) {
        free(pGreaters[i]);
    }
    free(pGreaters);
    for (i = 0; i < nCubesIsop * nCubesIsopFneg; i++) {
        free(pSmallers[i]);
    }
    free(pSmallers);
    //******************************

    i = 0;
    Lmin = 1000;
    Lmax = 0;

    //check the assigned weights in the original system
    for (j = 0; j < (int)strlen(pIsop); j += (nVars + 3))
        Lmin = Abc_MinInt(Lmin,
                Extra_ThreshCubeWeightedSum1(pWofChow, pChow, pIsop, nVars, j));
    for (j = 0; j < (int)strlen(pIsopFneg); j += (nVars + 3))
        Lmax = Abc_MaxInt(Lmax,
                Extra_ThreshCubeWeightedSum2(pWofChow, pChow, pIsopFneg, nVars,
                        j));

    for (i = 0; i < nVars; i++) {
        pW[i] = pWofChow[pChow[i]];
    }

    ABC_FREE( pWofChow );
    if (Lmin > Lmax)
        return Lmin;
    else
        return 0;
}
void Extra_ThreshPrintWeights(int T, int * pW, int nVars) {
    int i;

    if (T == 0)
        fprintf( stdout, "\nHeuristic method: is not TLF\n\n");
    else {
        fprintf( stdout, "\nHeuristic method: Weights and threshold value:\n");
        for (i = 0; i < nVars; i++)
            printf("%d ", pW[i]);
        printf("  %d\n", T);
    }
}
/**Function*************************************************************


 Synopsis    [Checks thresholdness of the function.]

 Description []

 SideEffects []


 SeeAlso     []

 ***********************************************************************/
int Extra_ThreshCheck(word * t, int nVars, int * pW) {
Alan Mishchenko committed
571
    int Chow0, Chow[16];
572
    if (!Abc_TtIsUnate(t, nVars))
573
        return 0;
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
    Abc_TtMakePosUnate(t, nVars);
    Chow0 = Extra_ThreshComputeChow(t, nVars, Chow);
    Extra_ThreshSortByChow(t, nVars, Chow);
    return Extra_ThreshSelectWeights(t, nVars, pW);
}
/**Function*************************************************************


 Synopsis    [Checks thresholdness of the function by using a heuristic method.]

 Description []

 SideEffects []


 SeeAlso     []

 ***********************************************************************/
int Extra_ThreshHeuristic(word * t, int nVars, int * pW) {

    extern char * Abc_ConvertBddToSop( Mem_Flex_t * pMan, DdManager * dd, DdNode * bFuncOn, DdNode * bFuncOnDc, int nFanins, int fAllPrimes, Vec_Str_t * vCube, int fMode );
    int Chow0, Chow[16], nChows, i, T = 0;
    DdManager * dd;
    Vec_Str_t * vCube;
    DdNode * ddNode, * ddNodeFneg;
    char * pIsop, * pIsopFneg;
    if (nVars <= 1)
        return 1;
    if (!Abc_TtIsUnate(t, nVars))
        return 0;
    Abc_TtMakePosUnate(t, nVars);
    Chow0 = Extra_ThreshComputeChow(t, nVars, Chow);
    Extra_ThreshSortByChowInverted(t, nVars, Chow);
    nChows = Extra_ThreshInitializeChow(nVars, Chow);

    dd = (DdManager *) Abc_FrameReadManDd();
    vCube = Vec_StrAlloc(nVars);
    for (i = 0; i < nVars; i++)
        Cudd_bddIthVar(dd, i);
    ddNode = Kit_TruthToBdd(dd, (unsigned *) t, nVars, 0);
    Cudd_Ref(ddNode);
    pIsop = Abc_ConvertBddToSop( NULL, dd, ddNode, ddNode, nVars, 1,
            vCube, 1);

    Abc_TtNot(t, Abc_TruthWordNum(nVars));
    ddNodeFneg = Kit_TruthToBdd(dd, (unsigned *) t, nVars, 0);
    Cudd_Ref(ddNodeFneg);

    pIsopFneg = Abc_ConvertBddToSop( NULL, dd, ddNodeFneg, ddNodeFneg,
            nVars, 1, vCube, 1);

    Cudd_RecursiveDeref(dd, ddNode);
    Cudd_RecursiveDeref(dd, ddNodeFneg);

    T = Extra_ThreshAssignWeights(t, pIsop, pIsopFneg, nVars, pW, Chow, nChows,
            1);

    for (i = 2; (i < 4) && (T == 0) && (nVars >= 6); i++)
        T = Extra_ThreshAssignWeights(t, pIsop, pIsopFneg, nVars, pW, Chow,
                nChows, i);

    free(pIsop);
    free(pIsopFneg);
    Vec_StrFree(vCube);

    return T;
640 641 642 643
}

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

644 645 646
 Synopsis    [Checks unateness of a function.]

 Description []
647

648
 SideEffects []
649

650
 SeeAlso     []
651

652 653 654
 ***********************************************************************/
void Extra_ThreshCheckTest() {
    int nVars = 6;
655 656 657
    int T, Chow0, Chow[16], Weights[16];
//    word t =  s_Truths6[0] & s_Truths6[1] & s_Truths6[2] & s_Truths6[3] & s_Truths6[4];
//    word t =  (s_Truths6[0] & s_Truths6[1]) | (s_Truths6[0] & s_Truths6[2] & s_Truths6[3]) | (s_Truths6[0] & s_Truths6[2] & s_Truths6[4]);
658 659 660 661
//    word t = (s_Truths6[2] & s_Truths6[1])
//            | (s_Truths6[2] & s_Truths6[0] & s_Truths6[3])
//            | (s_Truths6[2] & s_Truths6[0] & ~s_Truths6[4]);
    word t = (s_Truths6[0] & s_Truths6[1] & s_Truths6[2])| (s_Truths6[0] & s_Truths6[1] & s_Truths6[3]) | (s_Truths6[0] & s_Truths6[1] & s_Truths6[4] & s_Truths6[5]) | (s_Truths6[0] & s_Truths6[2] & s_Truths6[3] & s_Truths6[4] & s_Truths6[5]);
662 663 664 665
//    word t =  (s_Truths6[0] & s_Truths6[1]) | (s_Truths6[0] & s_Truths6[2] & s_Truths6[3]) | (s_Truths6[0] & s_Truths6[2] & s_Truths6[4]) | (s_Truths6[1] & s_Truths6[2] & s_Truths6[3]);
//    word t =  (s_Truths6[0] & s_Truths6[1]) | (s_Truths6[0] & s_Truths6[2]) | (s_Truths6[0] & s_Truths6[3] & s_Truths6[4] & s_Truths6[5]) | 
//        (s_Truths6[1] & s_Truths6[2] & s_Truths6[3]) | (s_Truths6[1] & s_Truths6[2] & s_Truths6[4]) | (s_Truths6[1] & s_Truths6[2] & s_Truths6[5]);
    int i;
666 667 668 669
    assert(nVars <= 8);
    for (i = 0; i < nVars; i++)
        printf("%d %d %d\n", i, Abc_TtPosVar(&t, nVars, i),
                Abc_TtNegVar(&t, nVars, i));
670
//    word t = s_Truths6[0] & s_Truths6[1] & s_Truths6[2];
671 672 673
    Chow0 = Extra_ThreshComputeChow(&t, nVars, Chow);
    if ((T = Extra_ThreshCheck(&t, nVars, Weights)))
        Extra_ThreshPrintChow(T, Weights, nVars);
674
    else
675
        printf("No threshold\n");
676
}
677 678 679
void Extra_ThreshHeuristicTest() {
    int nVars = 6;
    int T, Weights[16];
680

681 682 683 684 685 686 687
    //    word t = 19983902376700760000;
    word t = (s_Truths6[0] & s_Truths6[1] & s_Truths6[2])| (s_Truths6[0] & s_Truths6[1] & s_Truths6[3]) | (s_Truths6[0] & s_Truths6[1] & s_Truths6[4] & s_Truths6[5]) | (s_Truths6[0] & s_Truths6[2] & s_Truths6[3] & s_Truths6[4] & s_Truths6[5]);
    word * pT = &t;
    T = Extra_ThreshHeuristic(pT, nVars, Weights);
    Extra_ThreshPrintWeights(T, Weights, nVars);

}
688 689 690 691 692 693
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////

ABC_NAMESPACE_IMPL_END