summaryrefslogtreecommitdiffstats
path: root/src/bdd/cudd/cuddEssent.c
blob: fb6f3275601378178ae55ce3e4033778643b9090 (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
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
571
572
573
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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
/**CFile***********************************************************************

  FileName    [cuddEssent.c]

  PackageName [cudd]

  Synopsis    [Functions for the detection of essential variables.]

  Description [External procedures included in this file:
                <ul>
                <li> Cudd_FindEssential()
                <li> Cudd_bddIsVarEssential()
                <li> Cudd_FindTwoLiteralClauses()
                <li> Cudd_ReadIthClause()
                <li> Cudd_PrintTwoLiteralClauses()
                <li> Cudd_tlcInfoFree()
                </ul>
        Static procedures included in this module:
                <ul>
                <li> ddFindEssentialRecur()
                <li> ddFindTwoLiteralClausesRecur()
                <li> computeClauses()
                <li> computeClausesWithUniverse()
                <li> emptyClauseSet()
                <li> sentinelp()
                <li> equalp()
                <li> beforep()
                <li> oneliteralp()
                <li> impliedp()
                <li> bitVectorAlloc()
                <li> bitVectorClear()
                <li> bitVectorFree()
                <li> bitVectorRead()
                <li> bitVectorSet()
                <li> tlcInfoAlloc()
                </ul>]

  Author      [Fabio Somenzi]

  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.]

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

#include "misc/util/util_hack.h"
#include "cuddInt.h"

ABC_NAMESPACE_IMPL_START



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

/* These definitions are for the bit vectors. */
#if SIZEOF_LONG == 8
#define BPL 64
#define LOGBPL 6
#else
#define BPL 32
#define LOGBPL 5
#endif

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

/* This structure holds the set of clauses for a node.  Each clause consists
** of two literals.  For one-literal clauses, the second lietral is FALSE.
** Each literal is composed of a variable and a phase.  A variable is a node
** index, and requires sizeof(DdHalfWord) bytes.  The constant literals use
** CUDD_MAXINDEX as variable indicator.  Each phase is a bit: 0 for positive
** phase, and 1 for negative phase.
** Variables and phases are stored separately for the sake of compactness.
** The variables are stored in an array of DdHalfWord's terminated by a
** sentinel (a pair of zeroes).  The phases are stored in a bit vector.
** The cnt field holds, at the end, the number of clauses.
** The clauses of the set are kept sorted.  For each clause, the first literal
** is the one of least index.  So, the clause with literals +2 and -4 is stored
** as (+2,-4).  A one-literal clause with literal +3 is stored as
** (+3,-CUDD_MAXINDEX).  Clauses are sorted in decreasing order as follows:
**      (+5,-7)
**      (+5,+6)
**      (-5,+7)
**      (-4,FALSE)
**      (-4,+8)
**      ...
** That is, one first looks at the variable of the first literal, then at the
** phase of the first litral, then at the variable of the second literal,
** and finally at the phase of the second literal.
*/
struct DdTlcInfo {
    DdHalfWord *vars;
    long *phases;
    DdHalfWord cnt;
};

/* This structure is for temporary representation of sets of clauses.  It is
** meant to be used in link lists, when the number of clauses is not yet
** known. The encoding of a clause is the same as in DdTlcInfo, though
** the phase information is not stored in a bit array. */
struct TlClause {
    DdHalfWord v1, v2;
    short p1, p2;
    struct TlClause *next;
};

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

typedef long BitVector;
typedef struct TlClause TlClause;

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

#ifndef lint
static char rcsid[] DD_UNUSED = "$Id: cuddEssent.c,v 1.24 2009/02/21 18:24:10 fabio Exp $";
#endif

static BitVector *Tolv;
static BitVector *Tolp;
static BitVector *Eolv;
static BitVector *Eolp;

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

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

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

static DdNode * ddFindEssentialRecur (DdManager *dd, DdNode *f);
static DdTlcInfo * ddFindTwoLiteralClausesRecur (DdManager * dd, DdNode * f, st__table *table);
static DdTlcInfo * computeClauses (DdTlcInfo *Tres, DdTlcInfo *Eres, DdHalfWord label, int size);
static DdTlcInfo * computeClausesWithUniverse (DdTlcInfo *Cres, DdHalfWord label, short phase);
static DdTlcInfo * emptyClauseSet (void);
static int sentinelp (DdHalfWord var1, DdHalfWord var2);
static int equalp (DdHalfWord var1a, short phase1a, DdHalfWord var1b, short phase1b, DdHalfWord var2a, short phase2a, DdHalfWord var2b, short phase2b);
static int beforep (DdHalfWord var1a, short phase1a, DdHalfWord var1b, short phase1b, DdHalfWord var2a, short phase2a, DdHalfWord var2b, short phase2b);
static int oneliteralp (DdHalfWord var);
static int impliedp (DdHalfWord var1, short phase1, DdHalfWord var2, short phase2, BitVector *olv, BitVector *olp);
static BitVector * bitVectorAlloc (int size);
DD_INLINE static void bitVectorClear (BitVector *vector, int size);
static void bitVectorFree (BitVector *vector);
DD_INLINE static short bitVectorRead (BitVector *vector, int i);
DD_INLINE static void bitVectorSet (BitVector * vector, int i, short val);
static DdTlcInfo * tlcInfoAlloc (void);

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


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


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

  Synopsis    [Finds the essential variables of a DD.]

  Description [Returns the cube of the essential variables. A positive
  literal means that the variable must be set to 1 for the function to be
  1. A negative literal means that the variable must be set to 0 for the
  function to be 1. Returns a pointer to the cube BDD if successful;
  NULL otherwise.]

  SideEffects [None]

  SeeAlso     [Cudd_bddIsVarEssential]

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

    do {
        dd->reordered = 0;
        res = ddFindEssentialRecur(dd,f);
    } while (dd->reordered == 1);
    return(res);

} /* end of Cudd_FindEssential */


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

  Synopsis    [Determines whether a given variable is essential with a
  given phase in a BDD.]

  Description [Determines whether a given variable is essential with a
  given phase in a BDD. Uses Cudd_bddIteConstant. Returns 1 if phase == 1
  and f-->x_id, or if phase == 0 and f-->x_id'.]

  SideEffects [None]

  SeeAlso     [Cudd_FindEssential]

******************************************************************************/
int
Cudd_bddIsVarEssential(
  DdManager * manager,
  DdNode * f,
  int  id,
  int  phase)
{
    DdNode      *var;
    int         res;

    var = Cudd_bddIthVar(manager, id);

    var = Cudd_NotCond(var,phase == 0);

    res = Cudd_bddLeq(manager, f, var);

    return(res);

} /* end of Cudd_bddIsVarEssential */


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

  Synopsis    [Finds the two literal clauses of a DD.]

  Description [Returns the one- and two-literal clauses of a DD.
  Returns a pointer to the structure holding the clauses if
  successful; NULL otherwise.  For a constant DD, the empty set of clauses
  is returned.  This is obviously correct for a non-zero constant.  For the
  constant zero, it is based on the assumption that only those clauses
  containing variables in the support of the function are considered.  Since
  the support of a constant function is empty, no clauses are returned.]

  SideEffects [None]

  SeeAlso     [Cudd_FindEssential]

******************************************************************************/
DdTlcInfo *
Cudd_FindTwoLiteralClauses(
  DdManager * dd,
  DdNode * f)
{
    DdTlcInfo *res;
    st__table *table;
    st__generator *gen;
    DdTlcInfo *tlc;
    DdNode *node;
    int size = dd->size;

    if (Cudd_IsConstant(f)) {
        res = emptyClauseSet();
        return(res);
    }
    table = st__init_table( st__ptrcmp, st__ptrhash);
    if (table == NULL) return(NULL);
    Tolv = bitVectorAlloc(size);
    if (Tolv == NULL) {
        st__free_table(table);
        return(NULL);
    }
    Tolp = bitVectorAlloc(size);
    if (Tolp == NULL) {
        st__free_table(table);
        bitVectorFree(Tolv);
        return(NULL);
    }
    Eolv = bitVectorAlloc(size);
    if (Eolv == NULL) {
        st__free_table(table);
        bitVectorFree(Tolv);
        bitVectorFree(Tolp);
        return(NULL);
    }
    Eolp = bitVectorAlloc(size);
    if (Eolp == NULL) {
        st__free_table(table);
        bitVectorFree(Tolv);
        bitVectorFree(Tolp);
        bitVectorFree(Eolv);
        return(NULL);
    }

    res = ddFindTwoLiteralClausesRecur(dd,f,table);
    /* Dispose of table contents and free table. */
    st__foreach_item(table, gen, (const char **)&node, (char **)&tlc) {
        if (node != f) {
            Cudd_tlcInfoFree(tlc);
        }
    }
    st__free_table(table);
    bitVectorFree(Tolv);
    bitVectorFree(Tolp);
    bitVectorFree(Eolv);
    bitVectorFree(Eolp);

    if (res != NULL) {
        int i;
        for (i = 0; !sentinelp(res->vars[i], res->vars[i+1]); i += 2);
        res->cnt = i >> 1;
    }

    return(res);

} /* end of Cudd_FindTwoLiteralClauses */


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

  Synopsis    [Accesses the i-th clause of a DD.]

  Description [Accesses the i-th clause of a DD given the clause set which
  must be already computed.  Returns 1 if successful; 0 if i is out of range,
  or in case of error.]

  SideEffects [the four components of a clause are returned as side effects.]

  SeeAlso     [Cudd_FindTwoLiteralClauses]

******************************************************************************/
int
Cudd_ReadIthClause(
  DdTlcInfo * tlc,
  int i,
  DdHalfWord *var1,
  DdHalfWord *var2,
  int *phase1,
  int *phase2)
{
    if (tlc == NULL) return(0);
    if (tlc->vars == NULL || tlc->phases == NULL) return(0);
    if (i < 0 || (unsigned) i >= tlc->cnt) return(0);
    *var1 = tlc->vars[2*i];
    *var2 = tlc->vars[2*i+1];
    *phase1 = (int) bitVectorRead(tlc->phases, 2*i);
    *phase2 = (int) bitVectorRead(tlc->phases, 2*i+1);
    return(1);

} /* end of Cudd_ReadIthClause */


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

  Synopsis    [Prints the two literal clauses of a DD.]

  Description [Prints the one- and two-literal clauses. Returns 1 if
  successful; 0 otherwise.  The argument "names" can be NULL, in which case
  the variable indices are printed.]

  SideEffects [None]

  SeeAlso     [Cudd_FindTwoLiteralClauses]

******************************************************************************/
int
Cudd_PrintTwoLiteralClauses(
  DdManager * dd,
  DdNode * f,
  char **names,
  FILE *fp)
{
    DdHalfWord *vars;
    BitVector *phases;
    int i;
    DdTlcInfo *res = Cudd_FindTwoLiteralClauses(dd, f);
    FILE *ifp = fp == NULL ? dd->out : fp;

    if (res == NULL) return(0);
    vars = res->vars;
    phases = res->phases;
    for (i = 0; !sentinelp(vars[i], vars[i+1]); i += 2) {
        if (names != NULL) {
            if (vars[i+1] == CUDD_MAXINDEX) {
                (void) fprintf(ifp, "%s%s\n",
                               bitVectorRead(phases, i) ? "~" : " ",
                               names[vars[i]]);
            } else {
                (void) fprintf(ifp, "%s%s | %s%s\n",
                               bitVectorRead(phases, i) ? "~" : " ",
                               names[vars[i]],
                               bitVectorRead(phases, i+1) ? "~" : " ",
                               names[vars[i+1]]);
            }
        } else {
            if (vars[i+1] == CUDD_MAXINDEX) {
                (void) fprintf(ifp, "%s%d\n",
                               bitVectorRead(phases, i) ? "~" : " ",
                               (int) vars[i]);
            } else {
                (void) fprintf(ifp, "%s%d | %s%d\n",
                               bitVectorRead(phases, i) ? "~" : " ",
                               (int) vars[i],
                               bitVectorRead(phases, i+1) ? "~" : " ",
                               (int) vars[i+1]);
            }
        }
    }
    Cudd_tlcInfoFree(res);

    return(1);

} /* end of Cudd_PrintTwoLiteralClauses */


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

  Synopsis    [Frees a DdTlcInfo Structure.]

  Description [Frees a DdTlcInfo Structure as well as the memory pointed
  by it.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
void
Cudd_tlcInfoFree(
  DdTlcInfo * t)
{
    if (t->vars != NULL) ABC_FREE(t->vars);
    if (t->phases != NULL) ABC_FREE(t->phases);
    ABC_FREE(t);

} /* end of Cudd_tlcInfoFree */


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


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


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

  Synopsis    [Implements the recursive step of Cudd_FindEssential.]

  Description [Implements the recursive step of Cudd_FindEssential.
  Returns a pointer to the cube BDD if successful; NULL otherwise.]

  SideEffects [None]

******************************************************************************/
static DdNode *
ddFindEssentialRecur(
  DdManager * dd,
  DdNode * f)
{
    DdNode      *T, *E, *F;
    DdNode      *essT, *essE, *res;
    int         index;
    DdNode      *one, *lzero, *azero;

    one = DD_ONE(dd);
    F = Cudd_Regular(f);
    /* If f is constant the set of essential variables is empty. */
    if (cuddIsConstant(F)) return(one);

    res = cuddCacheLookup1(dd,Cudd_FindEssential,f);
    if (res != NULL) {
        return(res);
    }

    lzero = Cudd_Not(one);
    azero = DD_ZERO(dd);
    /* Find cofactors: here f is non-constant. */
    T = cuddT(F);
    E = cuddE(F);
    if (Cudd_IsComplement(f)) {
        T = Cudd_Not(T); E = Cudd_Not(E);
    }

    index = F->index;
    if (Cudd_IsConstant(T) && T != lzero && T != azero) {
        /* if E is zero, index is essential, otherwise there are no
        ** essentials, because index is not essential and no other variable
        ** can be, since setting index = 1 makes the function constant and
        ** different from 0.
        */
        if (E == lzero || E == azero) {
            res = dd->vars[index];
        } else {
            res = one;
        }
    } else if (T == lzero || T == azero) {
        if (Cudd_IsConstant(E)) { /* E cannot be zero here */
            res = Cudd_Not(dd->vars[index]);
        } else { /* E == non-constant */
            /* find essentials in the else branch */
            essE = ddFindEssentialRecur(dd,E);
            if (essE == NULL) {
                return(NULL);
            }
            cuddRef(essE);

            /* add index to the set with negative phase */
            res = cuddUniqueInter(dd,index,one,Cudd_Not(essE));
            if (res == NULL) {
                Cudd_RecursiveDeref(dd,essE);
                return(NULL);
            }
            res = Cudd_Not(res);
            cuddDeref(essE);
        }
    } else { /* T == non-const */
        if (E == lzero || E == azero) {
            /* find essentials in the then branch */
            essT = ddFindEssentialRecur(dd,T);
            if (essT == NULL) {
                return(NULL);
            }
            cuddRef(essT);

            /* add index to the set with positive phase */
            /* use And because essT may be complemented */
            res = cuddBddAndRecur(dd,dd->vars[index],essT);
            if (res == NULL) {
                Cudd_RecursiveDeref(dd,essT);
                return(NULL);
            }
            cuddDeref(essT);
        } else if (!Cudd_IsConstant(E)) {
            /* if E is a non-zero constant there are no essentials
            ** because T is non-constant.
            */
            essT = ddFindEssentialRecur(dd,T);
            if (essT == NULL) {
                return(NULL);
            }
            if (essT == one) {
                res = one;
            } else {
                cuddRef(essT);
                essE = ddFindEssentialRecur(dd,E);
                if (essE == NULL) {
                    Cudd_RecursiveDeref(dd,essT);
                    return(NULL);
                }
                cuddRef(essE);

                /* res = intersection(essT, essE) */
                res = cuddBddLiteralSetIntersectionRecur(dd,essT,essE);
                if (res == NULL) {
                    Cudd_RecursiveDeref(dd,essT);
                    Cudd_RecursiveDeref(dd,essE);
                    return(NULL);
                }
                cuddRef(res);
                Cudd_RecursiveDeref(dd,essT);
                Cudd_RecursiveDeref(dd,essE);
                cuddDeref(res);
            }
        } else {        /* E is a non-zero constant */
            res = one;
        }
    }

    cuddCacheInsert1(dd,Cudd_FindEssential, f, res);
    return(res);

} /* end of ddFindEssentialRecur */


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

  Synopsis    [Implements the recursive step of Cudd_FindTwoLiteralClauses.]

  Description [Implements the recursive step of
  Cudd_FindTwoLiteralClauses.  The DD node is assumed to be not
  constant.  Returns a pointer to a set of clauses if successful; NULL
  otherwise.]

  SideEffects [None]

  SeeAlso     [Cudd_FindTwoLiteralClauses]

******************************************************************************/
static DdTlcInfo *
ddFindTwoLiteralClausesRecur(
  DdManager * dd,
  DdNode * f,
  st__table *table)
{
    DdNode *T, *E, *F;
    DdNode *one, *lzero, *azero;
    DdTlcInfo *res, *Tres, *Eres;
    DdHalfWord index;

    F = Cudd_Regular(f);

    assert(!cuddIsConstant(F));

    /* Check computed table.  Separate entries are necessary for
    ** a node and its complement.  We should update the counter here. */
    if ( st__lookup(table, (const char *)f, (char **)&res)) {
        return(res);
    }

    /* Easy access to the constants for BDDs and ADDs. */
    one = DD_ONE(dd);
    lzero = Cudd_Not(one);
    azero = DD_ZERO(dd);

    /* Find cofactors and variable labeling the top node. */
    T = cuddT(F); E = cuddE(F);
    if (Cudd_IsComplement(f)) {
        T = Cudd_Not(T); E = Cudd_Not(E);
    }
    index = F->index;

    if (Cudd_IsConstant(T) && T != lzero && T != azero) {
        /* T is a non-zero constant.  If E is zero, then this node's index
        ** is a one-literal clause.  Otherwise, if E is a non-zero
        ** constant, there are no clauses for this node.  Finally,
        ** if E is not constant, we recursively compute its clauses, and then
        ** merge using the empty set for T. */
        if (E == lzero || E == azero) {
            /* Create the clause (index + 0). */
            res = tlcInfoAlloc();
            if (res == NULL) return(NULL);
            res->vars = ABC_ALLOC(DdHalfWord,4);
            if (res->vars == NULL) {
                ABC_FREE(res);
                return(NULL);
            }
            res->phases = bitVectorAlloc(2);
            if (res->phases == NULL) {
                ABC_FREE(res->vars);
                ABC_FREE(res);
                return(NULL);
            }
            res->vars[0] = index;
            res->vars[1] = CUDD_MAXINDEX;
            res->vars[2] = 0;
            res->vars[3] = 0;
            bitVectorSet(res->phases, 0, 0); /* positive phase */
            bitVectorSet(res->phases, 1, 1); /* negative phase */
        } else if (Cudd_IsConstant(E)) {
            /* If E is a non-zero constant, no clauses. */
            res = emptyClauseSet();
        } else {
            /* E is non-constant */
            Tres = emptyClauseSet();
            if (Tres == NULL) return(NULL);
            Eres = ddFindTwoLiteralClausesRecur(dd, E, table);
            if (Eres == NULL) {
                Cudd_tlcInfoFree(Tres);
                return(NULL);
            }
            res = computeClauses(Tres, Eres, index, dd->size);
            Cudd_tlcInfoFree(Tres);
        }
    } else if (T == lzero || T == azero) {
        /* T is zero.  If E is a non-zero constant, then the
        ** complement of this node's index is a one-literal clause.
        ** Otherwise, if E is not constant, we recursively compute its
        ** clauses, and then merge using the universal set for T. */
        if (Cudd_IsConstant(E)) { /* E cannot be zero here */
            /* Create the clause (!index + 0). */
            res = tlcInfoAlloc();
            if (res == NULL) return(NULL);
            res->vars = ABC_ALLOC(DdHalfWord,4);
            if (res->vars == NULL) {
                ABC_FREE(res);
                return(NULL);
            }
            res->phases = bitVectorAlloc(2);
            if (res->phases == NULL) {
                ABC_FREE(res->vars);
                ABC_FREE(res);
                return(NULL);
            }
            res->vars[0] = index;
            res->vars[1] = CUDD_MAXINDEX;
            res->vars[2] = 0;
            res->vars[3] = 0;
            bitVectorSet(res->phases, 0, 1); /* negative phase */
            bitVectorSet(res->phases, 1, 1); /* negative phase */
        } else { /* E == non-constant */
            Eres = ddFindTwoLiteralClausesRecur(dd, E, table);
            if (Eres == NULL) return(NULL);
            res = computeClausesWithUniverse(Eres, index, 1);
        }
    } else { /* T == non-const */
        Tres = ddFindTwoLiteralClausesRecur(dd, T, table);
        if (Tres == NULL) return(NULL);
        if (Cudd_IsConstant(E)) {
            if (E == lzero || E == azero) {
                res = computeClausesWithUniverse(Tres, index, 0);
            } else {
                Eres = emptyClauseSet();
                if (Eres == NULL) return(NULL);
                res = computeClauses(Tres, Eres, index, dd->size);
                Cudd_tlcInfoFree(Eres);
            }
        } else {
            Eres = ddFindTwoLiteralClausesRecur(dd, E, table);
            if (Eres == NULL) return(NULL);
            res = computeClauses(Tres, Eres, index, dd->size);
        }
    }

    /* Cache results. */
    if ( st__add_direct(table, (char *)f, (char *)res) == st__OUT_OF_MEM) {
        ABC_FREE(res);
        return(NULL);
    }
    return(res);

} /* end of ddFindTwoLiteralClausesRecur */


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

  Synopsis    [Computes the two-literal clauses for a node.]

  Description [Computes the two-literal clauses for a node given the
  clauses for its children and the label of the node.  Returns a
  pointer to a TclInfo structure if successful; NULL otherwise.]

  SideEffects [None]

  SeeAlso     [computeClausesWithUniverse]

******************************************************************************/
static DdTlcInfo *
computeClauses(
  DdTlcInfo *Tres /* list of clauses for T child */,
  DdTlcInfo *Eres /* list of clauses for E child */,
  DdHalfWord label /* variable labeling the current node */,
  int size /* number of variables in the manager */)
{
    DdHalfWord *Tcv = Tres->vars; /* variables of clauses for the T child */
    BitVector *Tcp = Tres->phases; /* phases of clauses for the T child */
    DdHalfWord *Ecv = Eres->vars; /* variables of clauses for the E child */
    BitVector *Ecp = Eres->phases; /* phases of clauses for the E child */
    DdHalfWord *Vcv = NULL; /* pointer to variables of the clauses for v */
    BitVector *Vcp = NULL; /* pointer to phases of the clauses for v */
    DdTlcInfo *res = NULL; /* the set of clauses to be returned */
    int pt = 0; /* index in the list of clauses of T */
    int pe = 0; /* index in the list of clauses of E */
    int cv = 0; /* counter of the clauses for this node */
    TlClause *iclauses = NULL; /* list of inherited clauses */
    TlClause *tclauses = NULL; /* list of 1-literal clauses of T */
    TlClause *eclauses = NULL; /* list of 1-literal clauses of E */
    TlClause *nclauses = NULL; /* list of new (non-inherited) clauses */
    TlClause *lnclause = NULL; /* pointer to last new clause */
    TlClause *newclause; /* temporary pointer to new clauses */

    /* Initialize sets of one-literal clauses.  The one-literal clauses
    ** are stored redundantly.  These sets allow constant-time lookup, which
    ** we need when we check for implication of a two-literal clause by a
    ** one-literal clause.  The linked lists allow fast sequential
    ** processing. */
    bitVectorClear(Tolv, size);
    bitVectorClear(Tolp, size);
    bitVectorClear(Eolv, size);
    bitVectorClear(Eolp, size);

    /* Initialize result structure. */
    res = tlcInfoAlloc();
    if (res == NULL) goto cleanup;

    /* Scan the two input list.  Extract inherited two-literal clauses
    ** and set aside one-literal clauses from each list.  The incoming lists
    ** are sorted in the order defined by beforep.  The three linked list
    ** produced by this loop are sorted in the reverse order because we
    ** always append to the front of the lists.
    ** The inherited clauses are those clauses (both one- and two-literal)
    ** that are common to both children; and the two-literal clauses of
    ** one child that are implied by a one-literal clause of the other
    ** child. */
    while (!sentinelp(Tcv[pt], Tcv[pt+1]) || !sentinelp(Ecv[pe], Ecv[pe+1])) {
        if (equalp(Tcv[pt], bitVectorRead(Tcp, pt),
                   Tcv[pt+1], bitVectorRead(Tcp, pt+1),
                   Ecv[pe], bitVectorRead(Ecp, pe),
                   Ecv[pe+1], bitVectorRead(Ecp, pe+1))) {
            /* Add clause to inherited list. */
            newclause = ABC_ALLOC(TlClause,1);
            if (newclause == NULL) goto cleanup;
            newclause->v1 = Tcv[pt];
            newclause->v2 = Tcv[pt+1];
            newclause->p1 = bitVectorRead(Tcp, pt);
            newclause->p2 = bitVectorRead(Tcp, pt+1);
            newclause->next = iclauses;
            iclauses = newclause;
            pt += 2; pe += 2; cv++;
        } else if (beforep(Tcv[pt], bitVectorRead(Tcp, pt),
                   Tcv[pt+1], bitVectorRead(Tcp, pt+1),
                   Ecv[pe], bitVectorRead(Ecp, pe),
                   Ecv[pe+1], bitVectorRead(Ecp, pe+1))) {
            if (oneliteralp(Tcv[pt+1])) {
                /* Add this one-literal clause to the T set. */
                newclause = ABC_ALLOC(TlClause,1);
                if (newclause == NULL) goto cleanup;
                newclause->v1 = Tcv[pt];
                newclause->v2 = CUDD_MAXINDEX;
                newclause->p1 = bitVectorRead(Tcp, pt);
                newclause->p2 = 1;
                newclause->next = tclauses;
                tclauses = newclause;
                bitVectorSet(Tolv, Tcv[pt], 1);
                bitVectorSet(Tolp, Tcv[pt], bitVectorRead(Tcp, pt));
            } else {
                if (impliedp(Tcv[pt], bitVectorRead(Tcp, pt),
                             Tcv[pt+1], bitVectorRead(Tcp, pt+1),
                             Eolv, Eolp)) {
                    /* Add clause to inherited list. */
                    newclause = ABC_ALLOC(TlClause,1);
                    if (newclause == NULL) goto cleanup;
                    newclause->v1 = Tcv[pt];
                    newclause->v2 = Tcv[pt+1];
                    newclause->p1 = bitVectorRead(Tcp, pt);
                    newclause->p2 = bitVectorRead(Tcp, pt+1);
                    newclause->next = iclauses;
                    iclauses = newclause;
                    cv++;
                }
            }
            pt += 2;
        } else { /* !beforep() */
            if (oneliteralp(Ecv[pe+1])) {
                /* Add this one-literal clause to the E set. */
                newclause = ABC_ALLOC(TlClause,1);
                if (newclause == NULL) goto cleanup;
                newclause->v1 = Ecv[pe];
                newclause->v2 = CUDD_MAXINDEX;
                newclause->p1 = bitVectorRead(Ecp, pe);
                newclause->p2 = 1;
                newclause->next = eclauses;
                eclauses = newclause;
                bitVectorSet(Eolv, Ecv[pe], 1);
                bitVectorSet(Eolp, Ecv[pe], bitVectorRead(Ecp, pe));
            } else {
                if (impliedp(Ecv[pe], bitVectorRead(Ecp, pe),
                             Ecv[pe+1], bitVectorRead(Ecp, pe+1),
                             Tolv, Tolp)) {
                    /* Add clause to inherited list. */
                    newclause = ABC_ALLOC(TlClause,1);
                    if (newclause == NULL) goto cleanup;
                    newclause->v1 = Ecv[pe];
                    newclause->v2 = Ecv[pe+1];
                    newclause->p1 = bitVectorRead(Ecp, pe);
                    newclause->p2 = bitVectorRead(Ecp, pe+1);
                    newclause->next = iclauses;
                    iclauses = newclause;
                    cv++;
                }
            }
            pe += 2;
        }
    }

    /* Add one-literal clauses for the label variable to the front of
    ** the two lists. */
    newclause = ABC_ALLOC(TlClause,1);
    if (newclause == NULL) goto cleanup;
    newclause->v1 = label;
    newclause->v2 = CUDD_MAXINDEX;
    newclause->p1 = 0;
    newclause->p2 = 1;
    newclause->next = tclauses;
    tclauses = newclause;
    newclause = ABC_ALLOC(TlClause,1);
    if (newclause == NULL) goto cleanup;
    newclause->v1 = label;
    newclause->v2 = CUDD_MAXINDEX;
    newclause->p1 = 1;
    newclause->p2 = 1;
    newclause->next = eclauses;
    eclauses = newclause;

    /* Produce the non-inherited clauses.  We preserve the "reverse"
    ** order of the two input lists by appending to the end of the
    ** list.  In this way, iclauses and nclauses are consistent. */
    while (tclauses != NULL && eclauses != NULL) {
        if (beforep(eclauses->v1, eclauses->p1, eclauses->v2, eclauses->p2,
                    tclauses->v1, tclauses->p1, tclauses->v2, tclauses->p2)) {
            TlClause *nextclause = tclauses->next;
            TlClause *otherclauses = eclauses;
            while (otherclauses != NULL) {
                if (tclauses->v1 != otherclauses->v1) {
                    newclause = ABC_ALLOC(TlClause,1);
                    if (newclause == NULL) goto cleanup;
                    newclause->v1 = tclauses->v1;
                    newclause->v2 = otherclauses->v1;
                    newclause->p1 = tclauses->p1;
                    newclause->p2 = otherclauses->p1;
                    newclause->next = NULL;
                    if (nclauses == NULL) {
                        nclauses = newclause;
                        lnclause = newclause;
                    } else {
                        lnclause->next = newclause;
                        lnclause = newclause;
                    }
                    cv++;
                }
                otherclauses = otherclauses->next;
            }
            ABC_FREE(tclauses);
            tclauses = nextclause;
        } else {
            TlClause *nextclause = eclauses->next;
            TlClause *otherclauses = tclauses;
            while (otherclauses != NULL) {
                if (eclauses->v1 != otherclauses->v1) {
                    newclause = ABC_ALLOC(TlClause,1);
                    if (newclause == NULL) goto cleanup;
                    newclause->v1 = eclauses->v1;
                    newclause->v2 = otherclauses->v1;
                    newclause->p1 = eclauses->p1;
                    newclause->p2 = otherclauses->p1;
                    newclause->next = NULL;
                    if (nclauses == NULL) {
                        nclauses = newclause;
                        lnclause = newclause;
                    } else {
                        lnclause->next = newclause;
                        lnclause = newclause;
                    }
                    cv++;
                }
                otherclauses = otherclauses->next;
            }
            ABC_FREE(eclauses);
            eclauses = nextclause;
        }
    }
    while (tclauses != NULL) {
        TlClause *nextclause = tclauses->next;
        ABC_FREE(tclauses);
        tclauses = nextclause;
    }
    while (eclauses != NULL) {
        TlClause *nextclause = eclauses->next;
        ABC_FREE(eclauses);
        eclauses = nextclause;
    }

    /* Merge inherited and non-inherited clauses.  Now that we know the
    ** total number, we allocate the arrays, and we fill them bottom-up
    ** to restore the proper ordering. */
    Vcv = ABC_ALLOC(DdHalfWord, 2*(cv+1));
    if (Vcv == NULL) goto cleanup;
    if (cv > 0) {
        Vcp = bitVectorAlloc(2*cv);
        if (Vcp == NULL) goto cleanup;
    } else {
        Vcp = NULL;
    }
    res->vars = Vcv;
    res->phases = Vcp;
    /* Add sentinel. */
    Vcv[2*cv] = 0;
    Vcv[2*cv+1] = 0;
    while (iclauses != NULL || nclauses != NULL) {
        TlClause *nextclause;
        cv--;
        if (nclauses == NULL || (iclauses != NULL &&
            beforep(nclauses->v1, nclauses->p1, nclauses->v2, nclauses->p2,
                    iclauses->v1, iclauses->p1, iclauses->v2, iclauses->p2))) {
            Vcv[2*cv] = iclauses->v1;
            Vcv[2*cv+1] = iclauses->v2;
            bitVectorSet(Vcp, 2*cv, iclauses->p1);
            bitVectorSet(Vcp, 2*cv+1, iclauses->p2);
            nextclause = iclauses->next;
            ABC_FREE(iclauses);
            iclauses = nextclause;
        } else {
            Vcv[2*cv] = nclauses->v1;
            Vcv[2*cv+1] = nclauses->v2;
            bitVectorSet(Vcp, 2*cv, nclauses->p1);
            bitVectorSet(Vcp, 2*cv+1, nclauses->p2);
            nextclause = nclauses->next;
            ABC_FREE(nclauses);
            nclauses = nextclause;
        }
    }
    assert(cv == 0);

    return(res);

 cleanup:
    if (res != NULL) Cudd_tlcInfoFree(res);
    while (iclauses != NULL) {
        TlClause *nextclause = iclauses->next;
        ABC_FREE(iclauses);
        iclauses = nextclause;
    }
    while (nclauses != NULL) {
        TlClause *nextclause = nclauses->next;
        ABC_FREE(nclauses);
        nclauses = nextclause;
    }
    while (tclauses != NULL) {
        TlClause *nextclause = tclauses->next;
        ABC_FREE(tclauses);
        tclauses = nextclause;
    }
    while (eclauses != NULL) {
        TlClause *nextclause = eclauses->next;
        ABC_FREE(eclauses);
        eclauses = nextclause;
    }

    return(NULL);

} /* end of computeClauses */


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

  Synopsis    [Computes the two-literal clauses for a node.]

  Description [Computes the two-literal clauses for a node with a zero
  child, given the clauses for its other child and the label of the
  node.  Returns a pointer to a TclInfo structure if successful; NULL
  otherwise.]

  SideEffects [None]

  SeeAlso     [computeClauses]

******************************************************************************/
static DdTlcInfo *
computeClausesWithUniverse(
  DdTlcInfo *Cres /* list of clauses for child */,
  DdHalfWord label /* variable labeling the current node */,
  short phase /* 0 if E child is zero; 1 if T child is zero */)
{
    DdHalfWord *Ccv = Cres->vars; /* variables of clauses for child */
    BitVector *Ccp = Cres->phases; /* phases of clauses for child */
    DdHalfWord *Vcv = NULL; /* pointer to the variables of the clauses for v */
    BitVector *Vcp = NULL; /* pointer to the phases of the clauses for v */
    DdTlcInfo *res = NULL; /* the set of clauses to be returned */
    int i;

    /* Initialize result. */
    res = tlcInfoAlloc();
    if (res == NULL) goto cleanup;
    /* Count entries for new list and allocate accordingly. */
    for (i = 0; !sentinelp(Ccv[i], Ccv[i+1]); i += 2);
    /* At this point, i is twice the number of clauses in the child's
    ** list.  We need four more entries for this node: 2 for the one-literal
    ** clause for the label, and 2 for the sentinel. */
    Vcv = ABC_ALLOC(DdHalfWord,i+4);
    if (Vcv == NULL) goto cleanup;
    Vcp = bitVectorAlloc(i+4);
    if (Vcp == NULL) goto cleanup;
    res->vars = Vcv;
    res->phases = Vcp;
    /* Copy old list into new. */
    for (i = 0; !sentinelp(Ccv[i], Ccv[i+1]); i += 2) {
        Vcv[i] = Ccv[i];
        Vcv[i+1] = Ccv[i+1];
        bitVectorSet(Vcp, i, bitVectorRead(Ccp, i));
        bitVectorSet(Vcp, i+1, bitVectorRead(Ccp, i+1));
    }
    /* Add clause corresponding to label. */
    Vcv[i] = label;
    bitVectorSet(Vcp, i, phase);
    i++;
    Vcv[i] = CUDD_MAXINDEX;
    bitVectorSet(Vcp, i, 1);
    i++;
    /* Add sentinel. */
    Vcv[i] = 0;
    Vcv[i+1] = 0;
    bitVectorSet(Vcp, i, 0);
    bitVectorSet(Vcp, i+1, 0);

    return(res);

 cleanup:
    /* Vcp is guaranteed to be NULL here.  Hence, we do not try to free it. */
    if (Vcv != NULL) ABC_FREE(Vcv);
    if (res != NULL) Cudd_tlcInfoFree(res);

    return(NULL);

} /* end of computeClausesWithUniverse */


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

  Synopsis    [Returns an enpty set of clauses.]

  Description [Returns a pointer to an empty set of clauses if
  successful; NULL otherwise.  No bit vector for the phases is
  allocated.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static DdTlcInfo *
emptyClauseSet(void)
{
    DdTlcInfo *eset;

    eset = ABC_ALLOC(DdTlcInfo,1);
    if (eset == NULL) return(NULL);
    eset->vars = ABC_ALLOC(DdHalfWord,2);
    if (eset->vars == NULL) {
        ABC_FREE(eset);
        return(NULL);
    }
    /* Sentinel */
    eset->vars[0] = 0;
    eset->vars[1] = 0;
    eset->phases = NULL; /* does not matter */
    eset->cnt = 0;
    return(eset);

} /* end of emptyClauseSet */


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

  Synopsis    [Returns true iff the argument is the sentinel clause.]

  Description [Returns true iff the argument is the sentinel clause.
  A sentinel clause has both variables equal to 0.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static int
sentinelp(
  DdHalfWord var1,
  DdHalfWord var2)
{
    return(var1 == 0 && var2 == 0);

} /* end of sentinelp */


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

  Synopsis    [Returns true iff the two arguments are identical clauses.]

  Description [Returns true iff the two arguments are identical
  clauses.  Since literals are sorted, we only need to compare
  literals in the same position.]

  SideEffects [None]

  SeeAlso     [beforep]

******************************************************************************/
static int
equalp(
  DdHalfWord var1a,
  short phase1a,
  DdHalfWord var1b,
  short phase1b,
  DdHalfWord var2a,
  short phase2a,
  DdHalfWord var2b,
  short phase2b)
{
    return(var1a == var2a && phase1a == phase2a &&
           var1b == var2b && phase1b == phase2b);

} /* end of equalp */


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

  Synopsis    [Returns true iff the first argument precedes the second in
  the clause order.]

  Description [Returns true iff the first argument precedes the second
  in the clause order.  A clause precedes another if its first lieral
  precedes the first literal of the other, or if the first literals
  are the same, and its second literal precedes the second literal of
  the other clause.  A literal precedes another if it has a higher
  index, of if it has the same index, but it has lower phase.  Phase 0
  is the positive phase, and it is lower than Phase 1 (negative
  phase).]

  SideEffects [None]

  SeeAlso     [equalp]

******************************************************************************/
static int
beforep(
  DdHalfWord var1a,
  short phase1a,
  DdHalfWord var1b,
  short phase1b,
  DdHalfWord var2a,
  short phase2a,
  DdHalfWord var2b,
  short phase2b)
{
    return(var1a > var2a || (var1a == var2a &&
           (phase1a < phase2a || (phase1a == phase2a &&
            (var1b > var2b || (var1b == var2b && phase1b < phase2b))))));

} /* end of beforep */


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

  Synopsis    [Returns true iff the argument is a one-literal clause.]

  Description [Returns true iff the argument is a one-literal clause.
  A one-litaral clause has the constant FALSE as second literal.
  Since the constant TRUE is never used, it is sufficient to test for
  a constant.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static int
oneliteralp(
  DdHalfWord var)
{
    return(var == CUDD_MAXINDEX);

} /* end of oneliteralp */


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

  Synopsis [Returns true iff either literal of a clause is in a set of
  literals.]

  Description [Returns true iff either literal of a clause is in a set
  of literals.  The first four arguments specify the clause.  The
  remaining two arguments specify the literal set.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static int
impliedp(
  DdHalfWord var1,
  short phase1,
  DdHalfWord var2,
  short phase2,
  BitVector *olv,
  BitVector *olp)
{
    return((bitVectorRead(olv, var1) &&
            bitVectorRead(olp, var1) == phase1) ||
           (bitVectorRead(olv, var2) &&
            bitVectorRead(olp, var2) == phase2));

} /* end of impliedp */


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

  Synopsis    [Allocates a bit vector.]

  Description [Allocates a bit vector.  The parameter size gives the
  number of bits.  This procedure allocates enough long's to hold the
  specified number of bits.  Returns a pointer to the allocated vector
  if successful; NULL otherwise.]

  SideEffects [None]

  SeeAlso     [bitVectorClear bitVectorFree]

******************************************************************************/
static BitVector *
bitVectorAlloc(
  int size)
{
    int allocSize;
    BitVector *vector;

    /* Find out how many long's we need.
    ** There are sizeof(long) * 8 bits in a long.
    ** The ceiling of the ratio of two integers m and n is given
    ** by ((n-1)/m)+1.  Putting all this together, we get... */
    allocSize = ((size - 1) / (sizeof(BitVector) * 8)) + 1;
    vector = ABC_ALLOC(BitVector, allocSize);
    if (vector == NULL) return(NULL);
    /* Clear the whole array. */
    (void) memset(vector, 0, allocSize * sizeof(BitVector));
    return(vector);

} /* end of bitVectorAlloc */


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

  Synopsis    [Clears a bit vector.]

  Description [Clears a bit vector.  The parameter size gives the
  number of bits.]

  SideEffects [None]

  SeeAlso     [bitVectorAlloc]

******************************************************************************/
DD_INLINE
static void
bitVectorClear(
  BitVector *vector,
  int size)
{
    int allocSize;

    /* Find out how many long's we need.
    ** There are sizeof(long) * 8 bits in a long.
    ** The ceiling of the ratio of two integers m and n is given
    ** by ((n-1)/m)+1.  Putting all this together, we get... */
    allocSize = ((size - 1) / (sizeof(BitVector) * 8)) + 1;
    /* Clear the whole array. */
    (void) memset(vector, 0, allocSize * sizeof(BitVector));
    return;

} /* end of bitVectorClear */


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

  Synopsis    [Frees a bit vector.]

  Description [Frees a bit vector.]

  SideEffects [None]

  SeeAlso     [bitVectorAlloc]

******************************************************************************/
static void
bitVectorFree(
  BitVector *vector)
{
    ABC_FREE(vector);

} /* end of bitVectorFree */


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

  Synopsis    [Returns the i-th entry of a bit vector.]

  Description [Returns the i-th entry of a bit vector.]

  SideEffects [None]

  SeeAlso     [bitVectorSet]

******************************************************************************/
DD_INLINE
static short
bitVectorRead(
  BitVector *vector,
  int i)
{
    int word, bit;
    short result;

    if (vector == NULL) return((short) 0);

    word = i >> LOGBPL;
    bit = i & (BPL - 1);
    result = (short) ((vector[word] >> bit) & 1L);
    return(result);

} /* end of bitVectorRead */


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

  Synopsis    [Sets the i-th entry of a bit vector to a value.]

  Description [Sets the i-th entry of a bit vector to a value.]

  SideEffects [None]

  SeeAlso     [bitVectorRead]

******************************************************************************/
DD_INLINE
static void
bitVectorSet(
  BitVector * vector,
  int i,
  short val)
{
    int word, bit;

    word = i >> LOGBPL;
    bit = i & (BPL - 1);
    vector[word] &= ~(1L << bit);
    vector[word] |= (((long) val) << bit);

} /* end of bitVectorSet */


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

  Synopsis    [Allocates a DdTlcInfo Structure.]

  Description [Returns a pointer to a DdTlcInfo Structure if successful;
  NULL otherwise.]

  SideEffects [None]

  SeeAlso     [Cudd_tlcInfoFree]

******************************************************************************/
static DdTlcInfo *
tlcInfoAlloc(void)
{
    DdTlcInfo *res = ABC_ALLOC(DdTlcInfo,1);
    if (res == NULL) return(NULL);
    res->vars = NULL;
    res->phases = NULL;
    res->cnt = 0;
    return(res);

} /* end of tlcInfoAlloc */


ABC_NAMESPACE_IMPL_END