summaryrefslogtreecommitdiffstats
path: root/src/misc/espresso/gasp.c
blob: 76e1233ef3ce8434755e36230a3509984b4547c7 (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
/*
 * Revision Control Information
 *
 * $Source$
 * $Author$
 * $Revision$
 * $Date$
 *
 */
/*
    module: gasp.c

    The "last_gasp" heuristic computes the reduction of each cube in
    the cover (without replacement) and then performs an expansion of
    these cubes.  The cubes which expand to cover some other cube are
    added to the original cover and irredundant finds a minimal subset.

    If one of the reduced cubes expands to cover some other reduced
    cube, then the new prime thus generated is a candidate for reducing
    the size of the cover.

    super_gasp is a variation on this strategy which extracts a minimal
    subset from the set of all prime implicants which cover all
    maximally reduced cubes.
*/

#include "espresso.h"

ABC_NAMESPACE_IMPL_START



/*
 *  reduce_gasp -- compute the maximal reduction of each cube of F
 *
 *  If a cube does not reduce, it remains prime; otherwise, it is marked
 *  as nonprime.   If the cube is redundant (should NEVER happen here) we
 *  just crap out ...
 *
 *  A cover with all of the cubes of F is returned.  Those that did
 *  reduce are marked "NONPRIME"; those that reduced are marked "PRIME".
 *  The cubes are in the same order as in F.
 */
static pcover reduce_gasp(F, D)
pcover F, D;
{
    pcube p, last, cunder, *FD;
    pcover G;

    G = new_cover(F->count);
    FD = cube2list(F, D);

    /* Reduce cubes of F without replacement */
    foreach_set(F, last, p) {
    cunder = reduce_cube(FD, p);
    if (setp_empty(cunder)) {
        fatal("empty reduction in reduce_gasp, shouldn't happen");
    } else if (setp_equal(cunder, p)) {
        SET(cunder, PRIME);            /* just to make sure */
        G = sf_addset(G, p);        /* it did not reduce ... */
    } else {
        RESET(cunder, PRIME);        /* it reduced ... */
        G = sf_addset(G, cunder);
    }
    if (debug & GASP) {
        printf("REDUCE_GASP: %s reduced to %s\n", pc1(p), pc2(cunder));
    }
    free_cube(cunder);
    }

    free_cubelist(FD);
    return G;
}

/*
 *  expand_gasp -- expand each nonprime cube of F into a prime implicant
 *
 *  The gasp strategy differs in that only those cubes which expand to
 *  cover some other cube are saved; also, all cubes are expanded
 *  regardless of whether they become covered or not.
 */

pcover expand_gasp(F, D, R, Foriginal)
INOUT pcover F;
IN pcover D;
IN pcover R;
IN pcover Foriginal;
{
    int c1index;
    pcover G;

    /* Try to expand each nonprime and noncovered cube */
    G = new_cover(10);
    for(c1index = 0; c1index < F->count; c1index++) {
    expand1_gasp(F, D, R, Foriginal, c1index, &G);
    }
    G = sf_dupl(G);
    G = expand(G, R, /*nonsparse*/ FALSE);    /* Make them prime ! */
    return G;
}



/*
 *  expand1 -- Expand a single cube against the OFF-set, using the gasp strategy
 */
void expand1_gasp(F, D, R, Foriginal, c1index, G)
pcover F;        /* reduced cubes of ON-set */
pcover D;        /* DC-set */
pcover R;        /* OFF-set */
pcover Foriginal;    /* ON-set before reduction (same order as F) */
int c1index;        /* which index of F (or Freduced) to be checked */
pcover *G;
{
    register int c2index;
    register pcube p, last, c2under;
    pcube RAISE, FREESET, temp, *FD, c2essential;
    pcover F1;

    if (debug & EXPAND1) {
    printf("\nEXPAND1_GASP:    \t%s\n", pc1(GETSET(F, c1index)));
    }

    RAISE = new_cube();
    FREESET = new_cube();
    temp = new_cube();

    /* Initialize the OFF-set */
    R->active_count = R->count;
    foreach_set(R, last, p) {
    SET(p, ACTIVE);
    }
    /* Initialize the reduced ON-set, all nonprime cubes become active */
    F->active_count = F->count;
    foreachi_set(F, c2index, c2under) {
    if (c1index == c2index || TESTP(c2under, PRIME)) {
        F->active_count--;
        RESET(c2under, ACTIVE);
    } else {
        SET(c2under, ACTIVE);
    }
    }

    /* Initialize the raising and unassigned sets */
    (void) set_copy(RAISE, GETSET(F, c1index));
    (void) set_diff(FREESET, cube.fullset, RAISE);

    /* Determine parts which must be lowered */
    essen_parts(R, F, RAISE, FREESET);

    /* Determine parts which can always be raised */
    essen_raising(R, RAISE, FREESET);

    /* See which, if any, of the reduced cubes we can cover */
    foreachi_set(F, c2index, c2under) {
    if (TESTP(c2under, ACTIVE)) {
        /* See if this cube can be covered by an expansion */
        if (setp_implies(c2under, RAISE) ||
          feasibly_covered(R, c2under, RAISE, temp)) {
        
        /* See if c1under can expanded to cover c2 reduced against
         * (F - c1) u c1under; if so, c2 can definitely be removed !
         */

        /* Copy F and replace c1 with c1under */
        F1 = sf_save(Foriginal);
        (void) set_copy(GETSET(F1, c1index), GETSET(F, c1index));

        /* Reduce c2 against ((F - c1) u c1under) */
        FD = cube2list(F1, D);
        c2essential = reduce_cube(FD, GETSET(F1, c2index));
        free_cubelist(FD);
        sf_free(F1);

        /* See if c2essential is covered by an expansion of c1under */
        if (feasibly_covered(R, c2essential, RAISE, temp)) {
            (void) set_or(temp, RAISE, c2essential);
            RESET(temp, PRIME);        /* cube not prime */
            *G = sf_addset(*G, temp);
        }
        set_free(c2essential);
        }
    }
    }

    free_cube(RAISE);
    free_cube(FREESET);
    free_cube(temp);
}

/* irred_gasp -- Add new primes to F and find an irredundant subset */
pcover irred_gasp(F, D, G)
pcover F, D, G;                 /* G is disposed of */
{
    if (G->count != 0)
    F = irredundant(sf_append(F, G), D);
    else
    free_cover(G);
    return F;
}


/* last_gasp */
pcover last_gasp(F, D, R, cost)
pcover F, D, R;
cost_t *cost;
{
    pcover G, G1;

    EXECUTE(G = reduce_gasp(F, D), GREDUCE_TIME, G, *cost);
    EXECUTE(G1 = expand_gasp(G, D, R, F), GEXPAND_TIME, G1, *cost);
    free_cover(G);
    EXECUTE(F = irred_gasp(F, D, G1), GIRRED_TIME, F, *cost);
    return F;
}


/* super_gasp */
pcover super_gasp(F, D, R, cost)
pcover F, D, R;
cost_t *cost;
{
    pcover G, G1;

    EXECUTE(G = reduce_gasp(F, D), GREDUCE_TIME, G, *cost);
    EXECUTE(G1 = all_primes(G, R), GEXPAND_TIME, G1, *cost);
    free_cover(G);
    EXEC(G = sf_dupl(sf_append(F, G1)), "NEWPRIMES", G);
    EXECUTE(F = irredundant(G, D), IRRED_TIME, F, *cost);
    return F;
}
ABC_NAMESPACE_IMPL_END