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
|
/**CFile***********************************************************************
FileName [cuddZddGroup.c]
PackageName [cudd]
Synopsis [Functions for ZDD group sifting.]
Description [External procedures included in this file:
<ul>
<li> Cudd_MakeZddTreeNode()
</ul>
Internal procedures included in this file:
<ul>
<li> cuddZddTreeSifting()
</ul>
Static procedures included in this module:
<ul>
<li> zddTreeSiftingAux()
<li> zddCountInternalMtrNodes()
<li> zddReorderChildren()
<li> zddFindNodeHiLo()
<li> zddUniqueCompareGroup()
<li> zddGroupSifting()
<li> zddGroupSiftingAux()
<li> zddGroupSiftingUp()
<li> zddGroupSiftingDown()
<li> zddGroupMove()
<li> zddGroupMoveBackward()
<li> zddGroupSiftingBackward()
<li> zddMergeGroups()
</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 */
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/* Stucture declarations */
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/* Type declarations */
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/* Variable declarations */
/*---------------------------------------------------------------------------*/
#ifndef lint
static char rcsid[] DD_UNUSED = "$Id: cuddZddGroup.c,v 1.20 2009/02/19 16:25:36 fabio Exp $";
#endif
static int *entry;
extern int zddTotalNumberSwapping;
#ifdef DD_STATS
static int extsymmcalls;
static int extsymm;
static int secdiffcalls;
static int secdiff;
static int secdiffmisfire;
#endif
#ifdef DD_DEBUG
static int pr = 0; /* flag to enable printing while debugging */
/* by depositing a 1 into it */
#endif
/*---------------------------------------------------------------------------*/
/* Macro declarations */
/*---------------------------------------------------------------------------*/
/**AutomaticStart*************************************************************/
/*---------------------------------------------------------------------------*/
/* Static function prototypes */
/*---------------------------------------------------------------------------*/
static int zddTreeSiftingAux (DdManager *table, MtrNode *treenode, Cudd_ReorderingType method);
#ifdef DD_STATS
static int zddCountInternalMtrNodes (DdManager *table, MtrNode *treenode);
#endif
static int zddReorderChildren (DdManager *table, MtrNode *treenode, Cudd_ReorderingType method);
static void zddFindNodeHiLo (DdManager *table, MtrNode *treenode, int *lower, int *upper);
static int zddUniqueCompareGroup (int *ptrX, int *ptrY);
static int zddGroupSifting (DdManager *table, int lower, int upper);
static int zddGroupSiftingAux (DdManager *table, int x, int xLow, int xHigh);
static int zddGroupSiftingUp (DdManager *table, int y, int xLow, Move **moves);
static int zddGroupSiftingDown (DdManager *table, int x, int xHigh, Move **moves);
static int zddGroupMove (DdManager *table, int x, int y, Move **moves);
static int zddGroupMoveBackward (DdManager *table, int x, int y);
static int zddGroupSiftingBackward (DdManager *table, Move *moves, int size);
static void zddMergeGroups (DdManager *table, MtrNode *treenode, int low, int high);
/**AutomaticEnd***************************************************************/
/*---------------------------------------------------------------------------*/
/* Definition of exported functions */
/*---------------------------------------------------------------------------*/
/**Function********************************************************************
Synopsis [Creates a new ZDD variable group.]
Description [Creates a new ZDD variable group. The group starts at
variable and contains size variables. The parameter low is the index
of the first variable. If the variable already exists, its current
position in the order is known to the manager. If the variable does
not exist yet, the position is assumed to be the same as the index.
The group tree is created if it does not exist yet.
Returns a pointer to the group if successful; NULL otherwise.]
SideEffects [The ZDD variable tree is changed.]
SeeAlso [Cudd_MakeTreeNode]
******************************************************************************/
MtrNode *
Cudd_MakeZddTreeNode(
DdManager * dd /* manager */,
unsigned int low /* index of the first group variable */,
unsigned int size /* number of variables in the group */,
unsigned int type /* MTR_DEFAULT or MTR_FIXED */)
{
MtrNode *group;
MtrNode *tree;
unsigned int level;
/* If the variable does not exist yet, the position is assumed to be
** the same as the index. Therefore, applications that rely on
** Cudd_bddNewVarAtLevel or Cudd_addNewVarAtLevel to create new
** variables have to create the variables before they group them.
*/
level = (low < (unsigned int) dd->sizeZ) ? dd->permZ[low] : low;
if (level + size - 1> (int) MTR_MAXHIGH)
return(NULL);
/* If the tree does not exist yet, create it. */
tree = dd->treeZ;
if (tree == NULL) {
dd->treeZ = tree = Mtr_InitGroupTree(0, dd->sizeZ);
if (tree == NULL)
return(NULL);
tree->index = dd->invpermZ[0];
}
/* Extend the upper bound of the tree if necessary. This allows the
** application to create groups even before the variables are created.
*/
tree->size = ddMax(tree->size, level + size);
/* Create the group. */
group = Mtr_MakeGroup(tree, level, size, type);
if (group == NULL)
return(NULL);
/* Initialize the index field to the index of the variable currently
** in position low. This field will be updated by the reordering
** procedure to provide a handle to the group once it has been moved.
*/
group->index = (MtrHalfWord) low;
return(group);
} /* end of Cudd_MakeZddTreeNode */
/*---------------------------------------------------------------------------*/
/* Definition of internal functions */
/*---------------------------------------------------------------------------*/
/**Function********************************************************************
Synopsis [Tree sifting algorithm for ZDDs.]
Description [Tree sifting algorithm for ZDDs. Assumes that a tree
representing a group hierarchy is passed as a parameter. It then
reorders each group in postorder fashion by calling
zddTreeSiftingAux. Assumes that no dead nodes are present. Returns
1 if successful; 0 otherwise.]
SideEffects [None]
******************************************************************************/
int
cuddZddTreeSifting(
DdManager * table /* DD table */,
Cudd_ReorderingType method /* reordering method for the groups of leaves */)
{
int i;
int nvars;
int result;
int tempTree;
/* If no tree is provided we create a temporary one in which all
** variables are in a single group. After reordering this tree is
** destroyed.
*/
tempTree = table->treeZ == NULL;
if (tempTree) {
table->treeZ = Mtr_InitGroupTree(0,table->sizeZ);
table->treeZ->index = table->invpermZ[0];
}
nvars = table->sizeZ;
#ifdef DD_DEBUG
if (pr > 0 && !tempTree)
(void) fprintf(table->out,"cuddZddTreeSifting:");
Mtr_PrintGroups(table->treeZ,pr <= 0);
#endif
#if 0
/* Debugging code. */
if (table->tree && table->treeZ) {
(void) fprintf(table->out,"\n");
Mtr_PrintGroups(table->tree, 0);
cuddPrintVarGroups(table,table->tree,0,0);
for (i = 0; i < table->size; i++) {
(void) fprintf(table->out,"%s%d",
(i == 0) ? "" : ",", table->invperm[i]);
}
(void) fprintf(table->out,"\n");
for (i = 0; i < table->size; i++) {
(void) fprintf(table->out,"%s%d",
(i == 0) ? "" : ",", table->perm[i]);
}
(void) fprintf(table->out,"\n\n");
Mtr_PrintGroups(table->treeZ,0);
cuddPrintVarGroups(table,table->treeZ,1,0);
for (i = 0; i < table->sizeZ; i++) {
(void) fprintf(table->out,"%s%d",
(i == 0) ? "" : ",", table->invpermZ[i]);
}
(void) fprintf(table->out,"\n");
for (i = 0; i < table->sizeZ; i++) {
(void) fprintf(table->out,"%s%d",
(i == 0) ? "" : ",", table->permZ[i]);
}
(void) fprintf(table->out,"\n");
}
/* End of debugging code. */
#endif
#ifdef DD_STATS
extsymmcalls = 0;
extsymm = 0;
secdiffcalls = 0;
secdiff = 0;
secdiffmisfire = 0;
(void) fprintf(table->out,"\n");
if (!tempTree)
(void) fprintf(table->out,"#:IM_NODES %8d: group tree nodes\n",
zddCountInternalMtrNodes(table,table->treeZ));
#endif
/* Initialize the group of each subtable to itself. Initially
** there are no groups. Groups are created according to the tree
** structure in postorder fashion.
*/
for (i = 0; i < nvars; i++)
table->subtableZ[i].next = i;
/* Reorder. */
result = zddTreeSiftingAux(table, table->treeZ, method);
#ifdef DD_STATS /* print stats */
if (!tempTree && method == CUDD_REORDER_GROUP_SIFT &&
(table->groupcheck == CUDD_GROUP_CHECK7 ||
table->groupcheck == CUDD_GROUP_CHECK5)) {
(void) fprintf(table->out,"\nextsymmcalls = %d\n",extsymmcalls);
(void) fprintf(table->out,"extsymm = %d",extsymm);
}
if (!tempTree && method == CUDD_REORDER_GROUP_SIFT &&
table->groupcheck == CUDD_GROUP_CHECK7) {
(void) fprintf(table->out,"\nsecdiffcalls = %d\n",secdiffcalls);
(void) fprintf(table->out,"secdiff = %d\n",secdiff);
(void) fprintf(table->out,"secdiffmisfire = %d",secdiffmisfire);
}
#endif
if (tempTree)
Cudd_FreeZddTree(table);
return(result);
} /* end of cuddZddTreeSifting */
/*---------------------------------------------------------------------------*/
/* Definition of static functions */
/*---------------------------------------------------------------------------*/
/**Function********************************************************************
Synopsis [Visits the group tree and reorders each group.]
Description [Recursively visits the group tree and reorders each
group in postorder fashion. Returns 1 if successful; 0 otherwise.]
SideEffects [None]
******************************************************************************/
static int
zddTreeSiftingAux(
DdManager * table,
MtrNode * treenode,
Cudd_ReorderingType method)
{
MtrNode *auxnode;
int res;
#ifdef DD_DEBUG
Mtr_PrintGroups(treenode,1);
#endif
auxnode = treenode;
while (auxnode != NULL) {
if (auxnode->child != NULL) {
if (!zddTreeSiftingAux(table, auxnode->child, method))
return(0);
res = zddReorderChildren(table, auxnode, CUDD_REORDER_GROUP_SIFT);
if (res == 0)
return(0);
} else if (auxnode->size > 1) {
if (!zddReorderChildren(table, auxnode, method))
return(0);
}
auxnode = auxnode->younger;
}
return(1);
} /* end of zddTreeSiftingAux */
#ifdef DD_STATS
/**Function********************************************************************
Synopsis [Counts the number of internal nodes of the group tree.]
Description [Counts the number of internal nodes of the group tree.
Returns the count.]
SideEffects [None]
******************************************************************************/
static int
zddCountInternalMtrNodes(
DdManager * table,
MtrNode * treenode)
{
MtrNode *auxnode;
int count,nodeCount;
nodeCount = 0;
auxnode = treenode;
while (auxnode != NULL) {
if (!(MTR_TEST(auxnode,MTR_TERMINAL))) {
nodeCount++;
count = zddCountInternalMtrNodes(table,auxnode->child);
nodeCount += count;
}
auxnode = auxnode->younger;
}
return(nodeCount);
} /* end of zddCountInternalMtrNodes */
#endif
/**Function********************************************************************
Synopsis [Reorders the children of a group tree node according to
the options.]
Description [Reorders the children of a group tree node according to
the options. After reordering puts all the variables in the group
and/or its descendents in a single group. This allows hierarchical
reordering. If the variables in the group do not exist yet, simply
does nothing. Returns 1 if successful; 0 otherwise.]
SideEffects [None]
******************************************************************************/
static int
zddReorderChildren(
DdManager * table,
MtrNode * treenode,
Cudd_ReorderingType method)
{
int lower;
int upper = -1;
int result;
unsigned int initialSize;
zddFindNodeHiLo(table,treenode,&lower,&upper);
/* If upper == -1 these variables do not exist yet. */
if (upper == -1)
return(1);
if (treenode->flags == MTR_FIXED) {
result = 1;
} else {
#ifdef DD_STATS
(void) fprintf(table->out," ");
#endif
switch (method) {
case CUDD_REORDER_RANDOM:
case CUDD_REORDER_RANDOM_PIVOT:
result = cuddZddSwapping(table,lower,upper,method);
break;
case CUDD_REORDER_SIFT:
result = cuddZddSifting(table,lower,upper);
break;
case CUDD_REORDER_SIFT_CONVERGE:
do {
initialSize = table->keysZ;
result = cuddZddSifting(table,lower,upper);
if (initialSize <= table->keysZ)
break;
#ifdef DD_STATS
else
(void) fprintf(table->out,"\n");
#endif
} while (result != 0);
break;
case CUDD_REORDER_SYMM_SIFT:
result = cuddZddSymmSifting(table,lower,upper);
break;
case CUDD_REORDER_SYMM_SIFT_CONV:
result = cuddZddSymmSiftingConv(table,lower,upper);
break;
case CUDD_REORDER_GROUP_SIFT:
result = zddGroupSifting(table,lower,upper);
break;
case CUDD_REORDER_LINEAR:
result = cuddZddLinearSifting(table,lower,upper);
break;
case CUDD_REORDER_LINEAR_CONVERGE:
do {
initialSize = table->keysZ;
result = cuddZddLinearSifting(table,lower,upper);
if (initialSize <= table->keysZ)
break;
#ifdef DD_STATS
else
(void) fprintf(table->out,"\n");
#endif
} while (result != 0);
break;
default:
return(0);
}
}
/* Create a single group for all the variables that were sifted,
** so that they will be treated as a single block by successive
** invocations of zddGroupSifting.
*/
zddMergeGroups(table,treenode,lower,upper);
#ifdef DD_DEBUG
if (pr > 0) (void) fprintf(table->out,"zddReorderChildren:");
#endif
return(result);
} /* end of zddReorderChildren */
/**Function********************************************************************
Synopsis [Finds the lower and upper bounds of the group represented
by treenode.]
Description [Finds the lower and upper bounds of the group represented
by treenode. The high and low fields of treenode are indices. From
those we need to derive the current positions, and find maximum and
minimum.]
SideEffects [The bounds are returned as side effects.]
SeeAlso []
******************************************************************************/
static void
zddFindNodeHiLo(
DdManager * table,
MtrNode * treenode,
int * lower,
int * upper)
{
int low;
int high;
/* Check whether no variables in this group already exist.
** If so, return immediately. The calling procedure will know from
** the values of upper that no reordering is needed.
*/
if ((int) treenode->low >= table->sizeZ) {
*lower = table->sizeZ;
*upper = -1;
return;
}
*lower = low = (unsigned int) table->permZ[treenode->index];
high = (int) (low + treenode->size - 1);
if (high >= table->sizeZ) {
/* This is the case of a partially existing group. The aim is to
** reorder as many variables as safely possible. If the tree
** node is terminal, we just reorder the subset of the group
** that is currently in existence. If the group has
** subgroups, then we only reorder those subgroups that are
** fully instantiated. This way we avoid breaking up a group.
*/
MtrNode *auxnode = treenode->child;
if (auxnode == NULL) {
*upper = (unsigned int) table->sizeZ - 1;
} else {
/* Search the subgroup that strands the table->sizeZ line.
** If the first group starts at 0 and goes past table->sizeZ
** upper will get -1, thus correctly signaling that no reordering
** should take place.
*/
while (auxnode != NULL) {
int thisLower = table->permZ[auxnode->low];
int thisUpper = thisLower + auxnode->size - 1;
if (thisUpper >= table->sizeZ && thisLower < table->sizeZ)
*upper = (unsigned int) thisLower - 1;
auxnode = auxnode->younger;
}
}
} else {
/* Normal case: All the variables of the group exist. */
*upper = (unsigned int) high;
}
#ifdef DD_DEBUG
/* Make sure that all variables in group are contiguous. */
assert(treenode->size >= *upper - *lower + 1);
#endif
return;
} /* end of zddFindNodeHiLo */
/**Function********************************************************************
Synopsis [Comparison function used by qsort.]
Description [Comparison function used by qsort to order the variables
according to the number of keys in the subtables. Returns the
difference in number of keys between the two variables being
compared.]
SideEffects [None]
******************************************************************************/
static int
zddUniqueCompareGroup(
int * ptrX,
int * ptrY)
{
#if 0
if (entry[*ptrY] == entry[*ptrX]) {
return((*ptrX) - (*ptrY));
}
#endif
return(entry[*ptrY] - entry[*ptrX]);
} /* end of zddUniqueCompareGroup */
/**Function********************************************************************
Synopsis [Sifts from treenode->low to treenode->high.]
Description [Sifts from treenode->low to treenode->high. If
croupcheck == CUDD_GROUP_CHECK7, it checks for group creation at the
end of the initial sifting. If a group is created, it is then sifted
again. After sifting one variable, the group that contains it is
dissolved. Returns 1 in case of success; 0 otherwise.]
SideEffects [None]
******************************************************************************/
static int
zddGroupSifting(
DdManager * table,
int lower,
int upper)
{
int *var;
int i,j,x,xInit;
int nvars;
int classes;
int result;
int *sifted;
#ifdef DD_STATS
unsigned previousSize;
#endif
int xindex;
nvars = table->sizeZ;
/* Order variables to sift. */
entry = NULL;
sifted = NULL;
var = ABC_ALLOC(int,nvars);
if (var == NULL) {
table->errorCode = CUDD_MEMORY_OUT;
goto zddGroupSiftingOutOfMem;
}
entry = ABC_ALLOC(int,nvars);
if (entry == NULL) {
table->errorCode = CUDD_MEMORY_OUT;
goto zddGroupSiftingOutOfMem;
}
sifted = ABC_ALLOC(int,nvars);
if (sifted == NULL) {
table->errorCode = CUDD_MEMORY_OUT;
goto zddGroupSiftingOutOfMem;
}
/* Here we consider only one representative for each group. */
for (i = 0, classes = 0; i < nvars; i++) {
sifted[i] = 0;
x = table->permZ[i];
if ((unsigned) x >= table->subtableZ[x].next) {
entry[i] = table->subtableZ[x].keys;
var[classes] = i;
classes++;
}
}
qsort((void *)var,(size_t)classes,sizeof(int),(DD_QSFP)zddUniqueCompareGroup);
/* Now sift. */
for (i = 0; i < ddMin(table->siftMaxVar,classes); i++) {
if (zddTotalNumberSwapping >= table->siftMaxSwap)
break;
xindex = var[i];
if (sifted[xindex] == 1) /* variable already sifted as part of group */
continue;
x = table->permZ[xindex]; /* find current level of this variable */
if (x < lower || x > upper)
continue;
#ifdef DD_STATS
previousSize = table->keysZ;
#endif
#ifdef DD_DEBUG
/* x is bottom of group */
assert((unsigned) x >= table->subtableZ[x].next);
#endif
result = zddGroupSiftingAux(table,x,lower,upper);
if (!result) goto zddGroupSiftingOutOfMem;
#ifdef DD_STATS
if (table->keysZ < previousSize) {
(void) fprintf(table->out,"-");
} else if (table->keysZ > previousSize) {
(void) fprintf(table->out,"+");
} else {
(void) fprintf(table->out,"=");
}
fflush(table->out);
#endif
/* Mark variables in the group just sifted. */
x = table->permZ[xindex];
if ((unsigned) x != table->subtableZ[x].next) {
xInit = x;
do {
j = table->invpermZ[x];
sifted[j] = 1;
x = table->subtableZ[x].next;
} while (x != xInit);
}
#ifdef DD_DEBUG
if (pr > 0) (void) fprintf(table->out,"zddGroupSifting:");
#endif
} /* for */
ABC_FREE(sifted);
ABC_FREE(var);
ABC_FREE(entry);
return(1);
zddGroupSiftingOutOfMem:
if (entry != NULL) ABC_FREE(entry);
if (var != NULL) ABC_FREE(var);
if (sifted != NULL) ABC_FREE(sifted);
return(0);
} /* end of zddGroupSifting */
/**Function********************************************************************
Synopsis [Sifts one variable up and down until it has taken all
positions. Checks for aggregation.]
Description [Sifts one variable up and down until it has taken all
positions. Checks for aggregation. There may be at most two sweeps,
even if the group grows. Assumes that x is either an isolated
variable, or it is the bottom of a group. All groups may not have
been found. The variable being moved is returned to the best position
seen during sifting. Returns 1 in case of success; 0 otherwise.]
SideEffects [None]
******************************************************************************/
static int
zddGroupSiftingAux(
DdManager * table,
int x,
int xLow,
int xHigh)
{
Move *move;
Move *moves; /* list of moves */
int initialSize;
int result;
#ifdef DD_DEBUG
if (pr > 0) (void) fprintf(table->out,"zddGroupSiftingAux from %d to %d\n",xLow,xHigh);
assert((unsigned) x >= table->subtableZ[x].next); /* x is bottom of group */
#endif
initialSize = table->keysZ;
moves = NULL;
if (x == xLow) { /* Sift down */
#ifdef DD_DEBUG
/* x must be a singleton */
assert((unsigned) x == table->subtableZ[x].next);
#endif
if (x == xHigh) return(1); /* just one variable */
if (!zddGroupSiftingDown(table,x,xHigh,&moves))
goto zddGroupSiftingAuxOutOfMem;
/* at this point x == xHigh, unless early term */
/* move backward and stop at best position */
result = zddGroupSiftingBackward(table,moves,initialSize);
#ifdef DD_DEBUG
assert(table->keysZ <= (unsigned) initialSize);
#endif
if (!result) goto zddGroupSiftingAuxOutOfMem;
} else if (cuddZddNextHigh(table,x) > xHigh) { /* Sift up */
#ifdef DD_DEBUG
/* x is bottom of group */
assert((unsigned) x >= table->subtableZ[x].next);
#endif
/* Find top of x's group */
x = table->subtableZ[x].next;
if (!zddGroupSiftingUp(table,x,xLow,&moves))
goto zddGroupSiftingAuxOutOfMem;
/* at this point x == xLow, unless early term */
/* move backward and stop at best position */
result = zddGroupSiftingBackward(table,moves,initialSize);
#ifdef DD_DEBUG
assert(table->keysZ <= (unsigned) initialSize);
#endif
if (!result) goto zddGroupSiftingAuxOutOfMem;
} else if (x - xLow > xHigh - x) { /* must go down first: shorter */
if (!zddGroupSiftingDown(table,x,xHigh,&moves))
goto zddGroupSiftingAuxOutOfMem;
/* at this point x == xHigh, unless early term */
/* Find top of group */
if (moves) {
x = moves->y;
}
while ((unsigned) x < table->subtableZ[x].next)
x = table->subtableZ[x].next;
x = table->subtableZ[x].next;
#ifdef DD_DEBUG
/* x should be the top of a group */
assert((unsigned) x <= table->subtableZ[x].next);
#endif
if (!zddGroupSiftingUp(table,x,xLow,&moves))
goto zddGroupSiftingAuxOutOfMem;
/* move backward and stop at best position */
result = zddGroupSiftingBackward(table,moves,initialSize);
#ifdef DD_DEBUG
assert(table->keysZ <= (unsigned) initialSize);
#endif
if (!result) goto zddGroupSiftingAuxOutOfMem;
} else { /* moving up first: shorter */
/* Find top of x's group */
x = table->subtableZ[x].next;
if (!zddGroupSiftingUp(table,x,xLow,&moves))
goto zddGroupSiftingAuxOutOfMem;
/* at this point x == xHigh, unless early term */
if (moves) {
x = moves->x;
}
while ((unsigned) x < table->subtableZ[x].next)
x = table->subtableZ[x].next;
#ifdef DD_DEBUG
/* x is bottom of a group */
assert((unsigned) x >= table->subtableZ[x].next);
#endif
if (!zddGroupSiftingDown(table,x,xHigh,&moves))
goto zddGroupSiftingAuxOutOfMem;
/* move backward and stop at best position */
result = zddGroupSiftingBackward(table,moves,initialSize);
#ifdef DD_DEBUG
assert(table->keysZ <= (unsigned) initialSize);
#endif
if (!result) goto zddGroupSiftingAuxOutOfMem;
}
while (moves != NULL) {
move = moves->next;
cuddDeallocMove(table, moves);
moves = move;
}
return(1);
zddGroupSiftingAuxOutOfMem:
while (moves != NULL) {
move = moves->next;
cuddDeallocMove(table, moves);
moves = move;
}
return(0);
} /* end of zddGroupSiftingAux */
/**Function********************************************************************
Synopsis [Sifts up a variable until either it reaches position xLow
or the size of the DD heap increases too much.]
Description [Sifts up a variable until either it reaches position
xLow or the size of the DD heap increases too much. Assumes that y is
the top of a group (or a singleton). Checks y for aggregation to the
adjacent variables. Records all the moves that are appended to the
list of moves received as input and returned as a side effect.
Returns 1 in case of success; 0 otherwise.]
SideEffects [None]
******************************************************************************/
static int
zddGroupSiftingUp(
DdManager * table,
int y,
int xLow,
Move ** moves)
{
Move *move;
int x;
int size;
int gxtop;
int limitSize;
limitSize = table->keysZ;
x = cuddZddNextLow(table,y);
while (x >= xLow) {
gxtop = table->subtableZ[x].next;
if (table->subtableZ[x].next == (unsigned) x &&
table->subtableZ[y].next == (unsigned) y) {
/* x and y are self groups */
size = cuddZddSwapInPlace(table,x,y);
#ifdef DD_DEBUG
assert(table->subtableZ[x].next == (unsigned) x);
assert(table->subtableZ[y].next == (unsigned) y);
#endif
if (size == 0) goto zddGroupSiftingUpOutOfMem;
move = (Move *)cuddDynamicAllocNode(table);
if (move == NULL) goto zddGroupSiftingUpOutOfMem;
move->x = x;
move->y = y;
move->flags = MTR_DEFAULT;
move->size = size;
move->next = *moves;
*moves = move;
#ifdef DD_DEBUG
if (pr > 0) (void) fprintf(table->out,"zddGroupSiftingUp (2 single groups):\n");
#endif
if ((double) size > (double) limitSize * table->maxGrowth)
return(1);
if (size < limitSize) limitSize = size;
} else { /* group move */
size = zddGroupMove(table,x,y,moves);
if (size == 0) goto zddGroupSiftingUpOutOfMem;
if ((double) size > (double) limitSize * table->maxGrowth)
return(1);
if (size < limitSize) limitSize = size;
}
y = gxtop;
x = cuddZddNextLow(table,y);
}
return(1);
zddGroupSiftingUpOutOfMem:
while (*moves != NULL) {
move = (*moves)->next;
cuddDeallocMove(table, *moves);
*moves = move;
}
return(0);
} /* end of zddGroupSiftingUp */
/**Function********************************************************************
Synopsis [Sifts down a variable until it reaches position xHigh.]
Description [Sifts down a variable until it reaches position xHigh.
Assumes that x is the bottom of a group (or a singleton). Records
all the moves. Returns 1 in case of success; 0 otherwise.]
SideEffects [None]
******************************************************************************/
static int
zddGroupSiftingDown(
DdManager * table,
int x,
int xHigh,
Move ** moves)
{
Move *move;
int y;
int size;
int limitSize;
int gybot;
/* Initialize R */
limitSize = size = table->keysZ;
y = cuddZddNextHigh(table,x);
while (y <= xHigh) {
/* Find bottom of y group. */
gybot = table->subtableZ[y].next;
while (table->subtableZ[gybot].next != (unsigned) y)
gybot = table->subtableZ[gybot].next;
if (table->subtableZ[x].next == (unsigned) x &&
table->subtableZ[y].next == (unsigned) y) {
/* x and y are self groups */
size = cuddZddSwapInPlace(table,x,y);
#ifdef DD_DEBUG
assert(table->subtableZ[x].next == (unsigned) x);
assert(table->subtableZ[y].next == (unsigned) y);
#endif
if (size == 0) goto zddGroupSiftingDownOutOfMem;
/* Record move. */
move = (Move *) cuddDynamicAllocNode(table);
if (move == NULL) goto zddGroupSiftingDownOutOfMem;
move->x = x;
move->y = y;
move->flags = MTR_DEFAULT;
move->size = size;
move->next = *moves;
*moves = move;
#ifdef DD_DEBUG
if (pr > 0) (void) fprintf(table->out,"zddGroupSiftingDown (2 single groups):\n");
#endif
if ((double) size > (double) limitSize * table->maxGrowth)
return(1);
if (size < limitSize) limitSize = size;
x = y;
y = cuddZddNextHigh(table,x);
} else { /* Group move */
size = zddGroupMove(table,x,y,moves);
if (size == 0) goto zddGroupSiftingDownOutOfMem;
if ((double) size > (double) limitSize * table->maxGrowth)
return(1);
if (size < limitSize) limitSize = size;
}
x = gybot;
y = cuddZddNextHigh(table,x);
}
return(1);
zddGroupSiftingDownOutOfMem:
while (*moves != NULL) {
move = (*moves)->next;
cuddDeallocMove(table, *moves);
*moves = move;
}
return(0);
} /* end of zddGroupSiftingDown */
/**Function********************************************************************
Synopsis [Swaps two groups and records the move.]
Description [Swaps two groups and records the move. Returns the
number of keys in the DD table in case of success; 0 otherwise.]
SideEffects [None]
******************************************************************************/
static int
zddGroupMove(
DdManager * table,
int x,
int y,
Move ** moves)
{
Move *move;
int size;
int i,j,xtop,xbot,xsize,ytop,ybot,ysize,newxtop;
int swapx=-1,swapy=-1;
#if defined(DD_DEBUG) && defined(DD_VERBOSE)
int initialSize,bestSize;
#endif
#ifdef DD_DEBUG
/* We assume that x < y */
assert(x < y);
#endif
/* Find top, bottom, and size for the two groups. */
xbot = x;
xtop = table->subtableZ[x].next;
xsize = xbot - xtop + 1;
ybot = y;
while ((unsigned) ybot < table->subtableZ[ybot].next)
ybot = table->subtableZ[ybot].next;
ytop = y;
ysize = ybot - ytop + 1;
#if defined(DD_DEBUG) && defined(DD_VERBOSE)
initialSize = bestSize = table->keysZ;
#endif
/* Sift the variables of the second group up through the first group */
for (i = 1; i <= ysize; i++) {
for (j = 1; j <= xsize; j++) {
size = cuddZddSwapInPlace(table,x,y);
if (size == 0) goto zddGroupMoveOutOfMem;
#if defined(DD_DEBUG) && defined(DD_VERBOSE)
if (size < bestSize)
bestSize = size;
#endif
swapx = x; swapy = y;
y = x;
x = cuddZddNextLow(table,y);
}
y = ytop + i;
x = cuddZddNextLow(table,y);
}
#if defined(DD_DEBUG) && defined(DD_VERBOSE)
if ((bestSize < initialSize) && (bestSize < size))
(void) fprintf(table->out,"Missed local minimum: initialSize:%d bestSize:%d finalSize:%d\n",initialSize,bestSize,size);
#endif
/* fix groups */
y = xtop; /* ytop is now where xtop used to be */
for (i = 0; i < ysize - 1; i++) {
table->subtableZ[y].next = cuddZddNextHigh(table,y);
y = cuddZddNextHigh(table,y);
}
table->subtableZ[y].next = xtop; /* y is bottom of its group, join */
/* it to top of its group */
x = cuddZddNextHigh(table,y);
newxtop = x;
for (i = 0; i < xsize - 1; i++) {
table->subtableZ[x].next = cuddZddNextHigh(table,x);
x = cuddZddNextHigh(table,x);
}
table->subtableZ[x].next = newxtop; /* x is bottom of its group, join */
/* it to top of its group */
#ifdef DD_DEBUG
if (pr > 0) (void) fprintf(table->out,"zddGroupMove:\n");
#endif
/* Store group move */
move = (Move *) cuddDynamicAllocNode(table);
if (move == NULL) goto zddGroupMoveOutOfMem;
move->x = swapx;
move->y = swapy;
move->flags = MTR_DEFAULT;
move->size = table->keysZ;
move->next = *moves;
*moves = move;
return(table->keysZ);
zddGroupMoveOutOfMem:
while (*moves != NULL) {
move = (*moves)->next;
cuddDeallocMove(table, *moves);
*moves = move;
}
return(0);
} /* end of zddGroupMove */
/**Function********************************************************************
Synopsis [Undoes the swap two groups.]
Description [Undoes the swap two groups. Returns 1 in case of
success; 0 otherwise.]
SideEffects [None]
******************************************************************************/
static int
zddGroupMoveBackward(
DdManager * table,
int x,
int y)
{
int size;
int i,j,xtop,xbot,xsize,ytop,ybot,ysize,newxtop;
#ifdef DD_DEBUG
/* We assume that x < y */
assert(x < y);
#endif
/* Find top, bottom, and size for the two groups. */
xbot = x;
xtop = table->subtableZ[x].next;
xsize = xbot - xtop + 1;
ybot = y;
while ((unsigned) ybot < table->subtableZ[ybot].next)
ybot = table->subtableZ[ybot].next;
ytop = y;
ysize = ybot - ytop + 1;
/* Sift the variables of the second group up through the first group */
for (i = 1; i <= ysize; i++) {
for (j = 1; j <= xsize; j++) {
size = cuddZddSwapInPlace(table,x,y);
if (size == 0)
return(0);
y = x;
x = cuddZddNextLow(table,y);
}
y = ytop + i;
x = cuddZddNextLow(table,y);
}
/* fix groups */
y = xtop;
for (i = 0; i < ysize - 1; i++) {
table->subtableZ[y].next = cuddZddNextHigh(table,y);
y = cuddZddNextHigh(table,y);
}
table->subtableZ[y].next = xtop; /* y is bottom of its group, join */
/* to its top */
x = cuddZddNextHigh(table,y);
newxtop = x;
for (i = 0; i < xsize - 1; i++) {
table->subtableZ[x].next = cuddZddNextHigh(table,x);
x = cuddZddNextHigh(table,x);
}
table->subtableZ[x].next = newxtop; /* x is bottom of its group, join */
/* to its top */
#ifdef DD_DEBUG
if (pr > 0) (void) fprintf(table->out,"zddGroupMoveBackward:\n");
#endif
return(1);
} /* end of zddGroupMoveBackward */
/**Function********************************************************************
Synopsis [Determines the best position for a variables and returns
it there.]
Description [Determines the best position for a variables and returns
it there. Returns 1 in case of success; 0 otherwise.]
SideEffects [None]
******************************************************************************/
static int
zddGroupSiftingBackward(
DdManager * table,
Move * moves,
int size)
{
Move *move;
int res;
for (move = moves; move != NULL; move = move->next) {
if (move->size < size) {
size = move->size;
}
}
for (move = moves; move != NULL; move = move->next) {
if (move->size == size) return(1);
if ((table->subtableZ[move->x].next == move->x) &&
(table->subtableZ[move->y].next == move->y)) {
res = cuddZddSwapInPlace(table,(int)move->x,(int)move->y);
if (!res) return(0);
#ifdef DD_DEBUG
if (pr > 0) (void) fprintf(table->out,"zddGroupSiftingBackward:\n");
assert(table->subtableZ[move->x].next == move->x);
assert(table->subtableZ[move->y].next == move->y);
#endif
} else { /* Group move necessary */
res = zddGroupMoveBackward(table,(int)move->x,(int)move->y);
if (!res) return(0);
}
}
return(1);
} /* end of zddGroupSiftingBackward */
/**Function********************************************************************
Synopsis [Merges groups in the DD table.]
Description [Creates a single group from low to high and adjusts the
idex field of the tree node.]
SideEffects [None]
******************************************************************************/
static void
zddMergeGroups(
DdManager * table,
MtrNode * treenode,
int low,
int high)
{
int i;
MtrNode *auxnode;
int saveindex;
int newindex;
/* Merge all variables from low to high in one group, unless
** this is the topmost group. In such a case we do not merge lest
** we lose the symmetry information. */
if (treenode != table->treeZ) {
for (i = low; i < high; i++)
table->subtableZ[i].next = i+1;
table->subtableZ[high].next = low;
}
/* Adjust the index fields of the tree nodes. If a node is the
** first child of its parent, then the parent may also need adjustment. */
saveindex = treenode->index;
newindex = table->invpermZ[low];
auxnode = treenode;
do {
auxnode->index = newindex;
if (auxnode->parent == NULL ||
(int) auxnode->parent->index != saveindex)
break;
auxnode = auxnode->parent;
} while (1);
return;
} /* end of zddMergeGroups */
ABC_NAMESPACE_IMPL_END
|