-
Notifications
You must be signed in to change notification settings - Fork 0
/
bryant.c
3013 lines (2484 loc) · 74.7 KB
/
bryant.c
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
/*****************************************************************
File : bryant.c
RCS : $Id: bryant.c,v 1.28 2004/04/02 14:04:36 schachte Exp $
Author : Peter Schachte, based on code by Tania Armstrong
Purpose : Manipulation of boolean functions
Copyright: � 1995 Peter Schachte. All rights reserved.
*****************************************************************/
/*****************************************************************
Controlling #defined Symbols
This code is conditionalized on a number of #defined symbols. They are:
STATISTICS Controls collecting of computed and unique
table hash performance. If defined,
concludeRep() prints out a bunch of
statistics.
CLEAR_CACHES If defined, all computed and unique tables are
cleared on a call to initRep(). This makes
running a number of tests in sequence more
fair.
USE_ITE_CONSTANT Include the ite_constant function.
SHARING Include algorithms useful in sharing analysis
performed using Boolean functions.
ELIM_DUPS If defined, iff_conj_array will not assume
that the input array has no duplicates. It
will still assume that it's sorted.
EQUAL_TEST If defined, lub(), glb(), and projected_glb
compare their arguments for equality, and if
equal return it as value. ite and ite_var
always do this with their last 2 args. The
equality test is pretty cheap, and the savings
will sometimes be huge, so this should
probably always be defined.
NO_CHEAP_SHIFT If defined, shifting an integer variable number
of places is relatively expensive on this platform,
and so should be avoided. In this case we use a
table where possible to avoid shifting.
*****************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <limits.h>
#include "bryant.h"
/* #define VERBOSE_DEBUG */
#define UNUSED_MAPPING -1 /* this MUST BE -1 */
#if !defined(max)
#define max(a,b) ((a)<(b) ? (b) : (a))
#endif
#if !defined(min)
#define min(a,b) ((a)<(b) ? (a) : (b))
#endif
#define PERCENTAGE(x,y) ((100.0 * (float)(x)) / (float)(y))
typedef struct pool {
node data[POOL_SIZE];
struct pool *prev;
} pool;
#if defined(NO_CHEAP_SHIFT) && BITS_PER_WORD == 32
bitmask following_bits[BITS_PER_WORD] =
{ 0xffffffff, 0xfffffffe, 0xfffffffc, 0xfffffff8,
0xfffffff0, 0xffffffe0, 0xffffffc0, 0xffffff80,
0xffffff00, 0xfffffe00, 0xfffffc00, 0xfffff800,
0xfffff000, 0xffffe000, 0xffffc000, 0xffff8000,
0xffff0000, 0xfffe0000, 0xfffc0000, 0xfff80000,
0xfff00000, 0xffe00000, 0xffc00000, 0xff800000,
0xff000000, 0xfe000000, 0xfc000000, 0xf8000000,
0xf0000000, 0xe0000000, 0xc0000000, 0x80000000
};
bitmask preceding_bits[BITS_PER_WORD] =
{ 0x00000001, 0x00000003, 0x00000007, 0x0000000f,
0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
0x0001ffff, 0x0003ffff, 0x0007ffff, 0x000fffff,
0x001fffff, 0x003fffff, 0x007fffff, 0x00ffffff,
0x01ffffff, 0x03ffffff, 0x07ffffff, 0x0fffffff,
0x1fffffff, 0x3fffffff, 0x7fffffff, 0xffffffff
};
#endif
unsigned char first_one_bit[256] =
{255, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 16 */
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 32 */
5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 48 */
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 64 */
6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 80 */
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 96 */
5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 112 */
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 128 */
7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 144 */
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 160 */
5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 176 */
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 192 */
6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 208 */
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 224 */
5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 240 */
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 256 */
};
unsigned char last_one_bit[256] =
{255, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, /* 16 */
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, /* 32 */
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, /* 48 */
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, /* 64 */
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, /* 80 */
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, /* 96 */
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, /* 112 */
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, /* 128 */
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, /* 144 */
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, /* 160 */
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, /* 176 */
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, /* 192 */
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, /* 208 */
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, /* 224 */
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, /* 240 */
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, /* 256 */
};
/* automatically initialized to all 0 bits: */
bitset emptyset;
/* To give as a renaming array when we don't want any renaming */
int identity_renaming[MAXVAR];
/****************************************************************
Macros to avoid #ifdefs
****************************************************************/
#if defined(CLEAR_CACHES)
#define CLEAR_CACHE(op) \
memset(op##_computed_cache, 0, sizeof(op##_computed_cache))
#else /* !CLEAR_CACHES */
#define CLEAR_CACHE(op)
#endif
#if defined(STATISTICS)
/* The largest bucket size to be separately counted. Larger buckets
* will be listed as "> MAX_COUNT."
*/
#define MAX_COUNT 1000
int unique_table_hits, unique_table_misses;
#define DECLARE_FN_COUNT(op) int op##_count;
#define COUNT_FN(fn) (++fn##_count)
#define INIT_FN_COUNT(fn) fn##_count = 0
#define PRINT_FN_COUNT(fn) \
if (fn##_count!=0) printf("%6d calls to " #fn "\n", fn##_count);
#define COUNT_UNIQUE_HIT (++unique_table_hits)
#define COUNT_UNIQUE_MISS (++unique_table_misses)
#define COUNT_HIT(op) (++op##_computed_hits)
#define COUNT_MISS(op) (++op##_computed_misses, ++cache->count)
#define INIT_CACHE(op) \
do { \
op##_computed_misses = 0; \
op##_computed_hits = 0; \
CLEAR_CACHE(op); \
} while (0)
#define CACHE_COUNT_MEMBER int count;
#define PRINT_CACHE_PERFORMANCE(op) \
do { \
if (op##_computed_misses > 0 ) { \
int i, size_count[MAX_COUNT+2]; \
printf(#op " computed table: %d hits, %d misses, %.2f%% hit rate\n",\
op##_computed_hits, op##_computed_misses, \
PERCENTAGE(op##_computed_hits, \
op##_computed_hits + op##_computed_misses)); \
memset(size_count, 0, sizeof(size_count)); \
for (i=0; i<COMPUTED_TABLE_SIZE; ++i) { \
int count = op##_computed_cache[i].count; \
++size_count[(count<=MAX_COUNT ? count : MAX_COUNT+1)]; \
} \
print_distribution(size_count, MAX_COUNT); \
} \
} while (0)
#else /* ! STATISTICS */
#define DECLARE_FN_COUNT(op)
#ifdef VERBOSE_DEBUG
#define COUNT_FN(fn) (QP_printf("Calling " #fn "()\n"), QP_flush(QP_curout))
#else
#define COUNT_FN(fn)
#endif
#define INIT_FN_COUNT(fn)
#define PRINT_FN_COUNT(fn)
#define COUNT_UNIQUE_HIT
#define COUNT_UNIQUE_MISS
#define INIT_CACHE(op) CLEAR_CACHE(op)
#define PRINT_CACHE_PERFORMANCE(op)
#define COUNT_HIT(fn)
#define COUNT_MISS(fn)
#define CACHE_COUNT_MEMBER
#endif /* STATISTICS */
#if defined(EQUAL_TEST)
#define DIRECT_EQUAL_TEST(f,g,result) if ((f) == (g)) return (result)
#define ELSE_TRY_EQUAL_TEST(f,g,result) \
else DIRECT_EQUAL_TEST(f,g,result);
#else /* ! EQUAL_TEST */
#define DIRECT_EQUAL_TEST(f,g,result)
#define ELSE_TRY_EQUAL_TEST(f,g,result)
#endif
#if defined(CLEAR_CACHES)
#define INIT_UNIQUE_TABLE \
do { \
memset(unique_table, 0, sizeof(unique_table)); \
while (curr_pool!=NULL) { \
pool *p = curr_pool->prev; \
free(curr_pool); \
curr_pool = p; \
} \
curr_pool_ptr = curr_pool_end_ptr = NULL; \
pool_count = 0; \
} while (0)
#else /* !CLEAR_CACHES */
#define INIT_UNIQUE_TABLE
#endif /* CLEAR_CACHES */
/********************************************************************
Caching of computed values
For improved efficiency, we maintain a
cache of previously computed values to certain functions, and use
this to avoid costly computations when possible. This is
particularly important for ROBDDs, because the high degree of fan-in
causes frequent repeated computations.
Because caching is in many ways extraneous to the functions being
cached, we put the caching code here as macros, so that the
presentation of the algorithms is minimally impacted by caching.
********************************************************************/
#if defined(STATISTICS)
#define DECLARE_CACHE(op,type) \
static int op##_computed_hits; \
static int op##_computed_misses; \
static type op##_computed_cache[COMPUTED_TABLE_SIZE]
#else /* !STATISTICS */
#define DECLARE_CACHE(op,type) \
static type op##_computed_cache[COMPUTED_TABLE_SIZE]
#endif /* STATISTICS */
/**************************** the cache for ite **************************/
#define TERNARY_NODE_HASH(f,g,h) \
(((INTCAST(f)>>4)+INTCAST(g)+(INTCAST(h)<<1)) % COMPUTED_TABLE_SIZE)
typedef struct {
node *f;
node *g;
node *h;
node *result;
CACHE_COUNT_MEMBER
} ite_cache_entry;
DECLARE_CACHE(ite, ite_cache_entry);
#if defined(USE_ITE_CONSTANT)
DECLARE_CACHE(ite_constant, ite_cache_entry);
#endif /* USE_ITE_CONSTANT */
#define DECLARE_ITE_CACHE_ENTRY ite_cache_entry *cache;
#define TRY_ITE_CACHE(n1,n2,n3,op) \
do { \
cache = &op##_computed_cache[TERNARY_NODE_HASH(n1,n2,n3)]; \
if (cache->f==n1 && cache->g==n2 && cache->h==n3) { \
COUNT_HIT(op); \
return cache->result; \
} \
} while (0)
#define UPDATE_ITE_CACHE(n1,n2,n3,node,op) \
do { \
cache->f = n1; \
cache->g = n2; \
cache->h = n3; \
cache->result = node; \
COUNT_MISS(op); \
} while (0)
/******************** the cache for unary operations ************/
#define UNARY_NODE_HASH(a) \
(INTCAST(a) % COMPUTED_TABLE_SIZE)
typedef struct {
node *f;
node *result;
CACHE_COUNT_MEMBER
} unary_cache_entry;
#if defined(SHARING)
DECLARE_CACHE(upclose, unary_cache_entry);
DECLARE_CACHE(complete_one, unary_cache_entry);
#endif /* SHARING */
#define DECLARE_UNARY_CACHE_ENTRY unary_cache_entry *cache;
#define UPDATE_UNARY_CACHE(n,node,op) \
do { \
cache->f = n; \
cache->result = node; \
COUNT_MISS(op); \
} while (0)
#define TRY_UNARY_CACHE(n,op) \
do { \
cache = &((op##_computed_cache)[UNARY_NODE_HASH(n)]); \
if (cache->f==(n)) { \
COUNT_HIT(op); \
return cache->result; \
} \
} while (0)
/******************** the cache for var_entailed ************/
#define VAR_ENTAILED_HASH(a,n) \
((INTCAST(a)+n) % COMPUTED_TABLE_SIZE)
typedef struct {
node *f;
int n;
int result;
CACHE_COUNT_MEMBER
} var_entailed_cache_entry;
DECLARE_CACHE(var_entailed, var_entailed_cache_entry);
#define DECLARE_VAR_ENTAILED_CACHE_ENTRY var_entailed_cache_entry *cache;
#define UPDATE_VAR_ENTAILED_CACHE(node,var,val) \
do { \
cache->f = node; \
cache->n = var; \
cache->result = val; \
COUNT_MISS(var_entailed); \
} while (0)
#define TRY_VAR_ENTAILED_CACHE(node,var) \
do { \
cache = &((var_entailed_computed_cache) \
[VAR_ENTAILED_HASH(node,var)]); \
if (cache->f==(node) && cache->n==(var)) { \
COUNT_HIT(op); \
return cache->result; \
} \
} while (0)
/**************** the cache for unary set-valued operations ********/
typedef struct {
node *f;
bitset result;
CACHE_COUNT_MEMBER
} unary_bitset_cache_entry;
DECLARE_CACHE(vars_entailed, unary_bitset_cache_entry);
#define DECLARE_UNARY_BITSET_CACHE_ENTRY \
unary_bitset_cache_entry *cache;
#define UPDATE_UNARY_BITSET_CACHE(n,set,op) \
do { \
cache->f = n; \
cache->result = set; \
COUNT_MISS(op); \
} while (0)
#define TRY_UNARY_BITSET_CACHE(n,op) \
do { \
cache = &((op##_computed_cache)[UNARY_NODE_HASH(n)]); \
if (cache->f==(n)) { \
COUNT_HIT(op); \
return &cache->result; \
} \
} while (0)
/******************** the cache for symmetric binary operations ************/
/* NB: Since glb and lub are commutative, cache entries will work both
* ways round, so we want a symmetrical cache, ie, (a,b) should hash
* to the same value as (b,a). We achieve this by first exchanging a and b
* if a > b (comparing their addresses) in TRY_BIN_CACHE. We assume that
* they won't be changed (or exchanged) before UPDATE_BIN_CACHE is called.
*/
#define BINARY_NODE_HASH(a,b) \
((INTCAST(a)+(INTCAST(b)<<1)) % COMPUTED_TABLE_SIZE)
typedef struct {
node *f;
node *g;
node *result;
CACHE_COUNT_MEMBER
} bin_cache_entry;
DECLARE_CACHE(glb, bin_cache_entry);
DECLARE_CACHE(lub, bin_cache_entry);
#if defined(SHARING)
DECLARE_CACHE(bin, bin_cache_entry);
DECLARE_CACHE(complete, bin_cache_entry);
#endif
#define DECLARE_BIN_CACHE_ENTRY bin_cache_entry *cache;
#define UPDATE_BIN_CACHE(n1,n2,node,op) \
do { \
cache->f = n1; \
cache->g = n2; \
cache->result = node; \
COUNT_MISS(op); \
} while (0)
#define TRY_BIN_CACHE(n1,n2,op) \
do { \
if (n2 < n1) { node *temp = (n2); (n2) = (n1); (n1) = temp; } \
cache = &((op##_computed_cache)[BINARY_NODE_HASH(n1,n2)]); \
if (cache->f==(n1) && cache->g==(n2)) { \
COUNT_HIT(op); \
return cache->result; \
} \
} while (0)
/******************** the cache for asymmetric binary operations ************/
#if defined(SHARING)
DECLARE_CACHE(complete_one_or, bin_cache_entry);
#define DECLARE_ASYM_BIN_CACHE_ENTRY bin_cache_entry *cache;
#define UPDATE_ASYM_BIN_CACHE(n1,n2,node,op) \
UPDATE_BIN_CACHE(n1,n2,node,op)
#define TRY_ASYM_BIN_CACHE(n1,n2,op) \
do { \
cache = &((op##_computed_cache)[BINARY_NODE_HASH(n1,n2)]); \
if (cache->f==(n1) && cache->g==(n2)) { \
COUNT_HIT(op); \
return cache->result; \
} \
} while (0)
#endif /* SHARING */
/************************ the cache for rename_pglb ********************/
typedef struct {
node *f;
node *g;
node *result;
unsigned int unique;
CACHE_COUNT_MEMBER
} rename_pglb_cache_entry;
static unsigned int oae_accumulator = 0;
DECLARE_CACHE(rename_pglb, rename_pglb_cache_entry);
#define DECLARE_RENAME_PGLB_CACHE_ENTRY rename_pglb_cache_entry *cache;
#define UPDATE_RENAME_PGLB_CACHE(n1,n2,node,acc) \
do { \
cache->f = n1; \
cache->g = n2; \
cache->result = node; \
cache->unique = acc; \
COUNT_MISS(rename_pglb); \
} while (0)
#define TRY_RENAME_PGLB_CACHE(n1,n2,acc) \
do { \
cache = &((rename_pglb_computed_cache)[BINARY_NODE_HASH(n1,n2)]);\
if (cache -> unique == acc && cache->f==(n1) && cache->g==(n2)) { \
COUNT_HIT(op); \
return cache->result; \
} \
} while (0)
/*
#define CLEAR_RENAME_PGLB_CACHE \
do { \
memset(rename_pglb_computed_cache, 0x0, sizeof(bin_cache_entry) * COMPUTED_TABLE_SIZE) \
} while(0)
#define CLEAR_RENAME_PGLB_CACHE \
do { \
int i; \
for (i = 0; i < COMPUTED_TABLE_SIZE; i++) { \
rename_pglb_computed_cache[i].f = (node *) 2; \
rename_pglb_computed_cache[i].g = (node *) 2; \
rename_pglb_computed_cache[i].result = (node *) 2; \
} \
} while(0)
*/
/**************************** the cache for rglb ***********************/
typedef struct {
node *f;
node *g;
node *result;
int thresh;
CACHE_COUNT_MEMBER
} rglb_cache_entry;
DECLARE_CACHE(rglb, rglb_cache_entry);
#define DECLARE_RGLB_CACHE_ENTRY rglb_cache_entry *cache;
#define TRY_RGLB_CACHE(n1,n2,th) \
do { \
if (n2 < n1) { node *temp = (n2); (n2) = (n1); (n1) = temp; } \
cache = &rglb_computed_cache[BINARY_NODE_HASH(n1,n2)]; \
if (cache->f==(n1) && cache->g==(n2) && \
cache->thresh >= th) { \
COUNT_HIT(rglb); \
if (cache->thresh == th) return cache->result; \
return projectThresh(th, cache->result); \
} \
} while (0)
#define UPDATE_RGLB_CACHE(n1,n2,th,node) \
do { \
cache->f = n1; \
cache->g = n2; \
cache->thresh = th; \
cache->result = node; \
COUNT_MISS(rglb); \
} while (0)
/************************ the cache for complete_or *******************/
#if defined(SHARING)
typedef struct {
node *f;
node *g;
node *prev;
node *result;
CACHE_COUNT_MEMBER
} complete_or_cache_entry;
DECLARE_CACHE(complete_or, complete_or_cache_entry);
#define DECLARE_COMPLETE_OR_CACHE_ENTRY complete_or_cache_entry *cache;
#define TRY_COMPLETE_OR_CACHE(n1,n2,pr) \
do { \
if (n2 < n1) { node *temp = (n2); (n2) = (n1); (n1) = temp; } \
cache = &complete_or_computed_cache[TERNARY_NODE_HASH(n1,n2,pr)]; \
if ((cache->f==n1 && cache->g==n2 && cache->prev==pr)) { \
COUNT_HIT(complete_or); \
return cache->result; \
} \
} while (0)
#define UPDATE_COMPLETE_OR_CACHE(n1,n2,pr,node) \
do { \
cache->f = n1; \
cache->g = n2; \
cache->prev = pr; \
cache->result = node; \
COUNT_MISS(complete_or); \
} while (0)
#endif /* SHARING */
/**************************** the cache for ite_var ***********************/
#define ITE_VAR_COMPUTED_HASH(f,g,h) \
((f+INTCAST(g)+(INTCAST(h)<<1)) % COMPUTED_TABLE_SIZE)
typedef struct {
int f;
node *g;
node *h;
node *result;
CACHE_COUNT_MEMBER
} ite_var_cache_entry;
DECLARE_CACHE(ite_var, ite_var_cache_entry);
#define DECLARE_ITE_VAR_CACHE_ENTRY ite_var_cache_entry *cache;
#define TRY_ITE_VAR_CACHE(n1,n2,h) \
do { \
cache = \
&ite_var_computed_cache[ITE_VAR_COMPUTED_HASH(n1,n2,h)];\
if (cache->f==n1 && cache->g==n2 && cache->h==h) { \
COUNT_HIT(ite_var); \
return cache->result; \
} \
} while (0)
#define UPDATE_ITE_VAR_CACHE(n1,n2,h,node)\
do { \
cache->f = n1; \
cache->g = n2; \
cache->h = h; \
cache->result = node; \
COUNT_MISS(ite_var); \
} while (0)
/****************************************************************
The Unique Table
****************************************************************/
static node *unique_table[UNIQUE_TABLE_SIZE];
#define UNIQUE_HASH(var,tr,fa) \
(((var)+INTCAST(tr)+(INTCAST(fa)<<1)) % UNIQUE_TABLE_SIZE)
/****************************************************************
Prototypes
****************************************************************/
node *complete(node *f, node *g);
node *bin_univ(node *f);
static int intcompare(const void *i, const void *j);
/****************************************************************
Inline Bit Set Stuff
****************************************************************/
/* next_element() finds the next element in set beginning with var,
* and updates var, word, and mask to refer to it.
* prev_element() finds the next earlier element in set.
* next_nonelement() finds the next potential element of set that is in
* fact not an element of set.
* prev_nonelement() finds the next earlier non-element of set.
*
* NB: if the initally supplied element is a member of the set, next_element
* and prev_element will happily return that one. Similarly,
* next_nonelement and prev_nonelement will happily return the initial
* input if it fits the bill. That is, these find the next or next
* earlier element INLCUDING THE INITIAL ONE that meets the criterion.
*/
#if defined(NO_CHEAP_SHIFT)
__inline int next_element(bitset *set, int *var, int *word, bitmask *mask)
{
int vr = *var;
int wd = *word;
bitmask f = FOLLOWING_BITS(vr&(BITS_PER_WORD-1));
bitmask *ptr = &(set->bits[wd]);
bitmask bits = *ptr;
bitmask msk = *mask;
assert(vr >= 0 && vr < MAXVAR);
if ((bits&f) == 0) {
do {
if (++wd > ((MAXVAR-1)/BITS_PER_WORD)) return FALSE;
} while ((bits=*++ptr) == 0);
vr = wd<<LOG_BITS_PER_WORD;
msk = 1;
}
/* I know there's a later bit set in bits, so this is safe */
while ((bits&msk) == 0) {
++vr;
msk <<= 1;
assert(vr < (wd+1)<<LOG_BITS_PER_WORD);
}
*var = vr;
*word = wd;
*mask = msk;
return TRUE;
}
#else /* !NO_CHEAP_SHIFT */
__inline int next_element(bitset *set, int *var, int *word, bitmask *mask)
{
int vr = *var;
int wd = *word;
bitmask *ptr = &(set->bits[wd]);
bitmask bits = *ptr&FOLLOWING_BITS(vr&(BITS_PER_WORD-1));
assert(vr >= 0 && vr < MAXVAR);
while (bits == 0) {
if (++wd > (MAXVAR-1)/BITS_PER_WORD) return FALSE;
bits = *++ptr;
}
vr = wd<<LOG_BITS_PER_WORD;
/* I know there's a later bit set in bits, so this is safe */
while ((bits & CHAR_MASK) == 0) {
bits >>= CHAR_BIT;
vr += CHAR_BIT;
assert(vr < (wd+1)<<LOG_BITS_PER_WORD);
}
vr += first_one_bit[bits & CHAR_MASK];
*var = vr;
*word = wd;
*mask = 1<<(vr&(BITS_PER_WORD-1));
return TRUE;
}
#endif /* NO_CHEAP_SHIFT */
#if defined(NO_CHEAP_SHIFT)
__inline int prev_element(bitset *set, int *var, int *word, bitmask *mask)
{
int vr = *var;
int wd = *word;
assert(vr >= 0 && vr < MAXVAR);
bitmask f = PRECEDING_BITS(vr&(BITS_PER_WORD-1));
bitmask *ptr = &(set->bits[wd]);
bitmask bits = *ptr;
bitmask msk = *mask;
if ((bits&f) == 0) {
do {
if (--wd < 0) return FALSE;
} while ((bits=*--ptr) == 0);
vr = (wd<<LOG_BITS_PER_WORD) + BITS_PER_WORD-1;
msk = 1<<(BITS_PER_WORD-1);
}
/* I know there's an earlier bit set in bits, so this is safe */
while ((bits&msk) == 0) {
--vr;
msk >>= 1;
assert(vr >= 0);
}
*var = vr;
*word = wd;
*mask = msk;
return TRUE;
}
#else /* !NO_CHEAP_SHIFT */
__inline int prev_element(bitset *set, int *var, int *word, bitmask *mask)
{
int vr = *var;
int wd = *word;
bitmask *ptr = &(set->bits[wd]);
bitmask bits = *ptr&PRECEDING_BITS(vr&(BITS_PER_WORD-1));
bitmask temp;
assert(vr >= 0 && vr < MAXVAR);
while (bits == 0) {
if (--wd < 0) return FALSE;
bits = *--ptr;
}
vr = BITS_PER_WORD - CHAR_BIT;
/* I know there's an earlier bit set in bits, so this is safe */
while ((temp=((bits>>vr)&CHAR_MASK)) == 0) {
vr -= CHAR_BIT;
assert(vr >= 0);
}
vr += (int)last_one_bit[(int)temp];
vr += (int)wd<<LOG_BITS_PER_WORD;
*var = vr;
*word = wd;
*mask = 1<<(vr&(BITS_PER_WORD-1));
return TRUE;
}
#endif /* NO_CHEAP_SHIFT */
__inline int next_nonelement(bitset *set, int *var, int *word, bitmask *mask)
{
int vr = *var;
int wd = *word;
bitmask f = FOLLOWING_BITS(vr&(BITS_PER_WORD-1));
bitmask *ptr = &(set->bits[wd]);
bitmask bits = *ptr;
bitmask msk = *mask;
assert(vr >= 0 && vr < MAXVAR);
if ((bits&f) == f) {
do {
if (++wd >= MAXVAR/BITS_PER_WORD) return FALSE;
} while ((bits=*++ptr) == ~0);
vr = wd<<LOG_BITS_PER_WORD;
msk = 1;
}
/* I know there's a later bit clear in bits, so this is safe */
while ((bits&msk) != 0) {
++vr;
msk <<= 1;
}
*var = vr;
*word = wd;
*mask = msk;
return TRUE;
}
__inline int prev_nonelement(bitset *set, int *var, int *word, bitmask *mask)
{
int vr = *var;
int wd = *word;
bitmask f = PRECEDING_BITS(vr&(BITS_PER_WORD-1));
bitmask *ptr = &(set->bits[wd]);
bitmask bits = *ptr;
bitmask msk = *mask;
assert(vr >= 0 && vr < MAXVAR);
if ((bits&f) == f) {
do {
if (--wd < 0) return FALSE;
} while ((bits=*--ptr) == ~0);
vr = (wd<<LOG_BITS_PER_WORD) + BITS_PER_WORD-1;
msk = 1<<(BITS_PER_WORD-1);
}
/* I know there's an earlier bit clear in bits, so this is safe */
while ((bits&msk) != 0) {
--vr;
msk >>= 1;
}
*var = vr;
*word = wd;
*mask = msk;
return TRUE;
}
/* returns 1 if set1 is identical to set2 */
__inline int bitset_equal(bitset *set1, bitset *set2)
{
bitmask *ptr1 = &set1->bits[0];
bitmask *ptr2 = &set2->bits[0];
bitmask *ptr1end = &set1->bits[((MAXVAR-1)/BITS_PER_WORD)+1];
for (;;) {
if (*ptr1 != *ptr2) return 0;
if (++ptr1 >= ptr1end) return 1;
++ptr2;
}
}
/* returns 1 if 2 sets are disjoint, else 0 */
__inline int bitset_disjoint(bitset *set1, bitset *set2)
{
bitmask *ptr1 = &set1->bits[0];
bitmask *ptr2 = &set2->bits[0];
bitmask *ptr1end = &set1->bits[((MAXVAR-1)/BITS_PER_WORD)+1];
for (;;) {
if ((*ptr1 & *ptr2) != 0) return 0;
if (++ptr1 >= ptr1end) return 1;
++ptr2;
}
}
/* returns 1 if set1 is a subset of set2 */
__inline int bitset_subset(bitset *set1, bitset *set2)
{
bitmask *ptr1 = &set1->bits[0];
bitmask *ptr2 = &set2->bits[0];
bitmask *ptr1end = &set1->bits[((MAXVAR-1)/BITS_PER_WORD)+1];
for (;;) {
if ((*ptr1 | *ptr2) != *ptr2) return 0;
if (++ptr1 >= ptr1end) return 1;
++ptr2;
}
}
/* returns 1 if set1 is a subset of set2 */
__inline int bitset_empty(bitset *set)
{
bitmask *ptr = &set->bits[0];
bitmask *ptrend = &set->bits[((MAXVAR-1)/BITS_PER_WORD)+1];
for (;;) {
if ((*ptr) != 0) return 0;
if (++ptr >= ptrend) return 1;
}
}
/****************************************************************
Making Nodes
****************************************************************/
static pool *curr_pool = NULL;
static node *curr_pool_ptr = NULL;
static node *curr_pool_end_ptr = NULL;
static int pool_count = 0;
static node *alloc_node(int value, node* tr, node* fa)
{
pool *newpool;
node *n;
if (curr_pool_ptr >= curr_pool_end_ptr) {
/* allocate a new pool */
newpool = malloc(sizeof(pool));
newpool->prev = curr_pool;
curr_pool = newpool;
curr_pool_ptr = &(newpool->data[0]);
curr_pool_end_ptr = &(newpool->data[POOL_SIZE]);
++pool_count;
}
n = curr_pool_ptr++;
n->value = value;
n->tr = tr;
n->fa = fa;
return n;
}
/* return the number of graph nodes that have been created. */
int nodes_in_use(void)
{
return pool_count*POOL_SIZE - (curr_pool_end_ptr - curr_pool_ptr);
}
DECLARE_FN_COUNT(make_node)
node *make_node(int var, node *tr, node *fa)
{
node **bucket;
node *ptr;
#ifdef VERBOSE_DEBUG
QP_printf("make_node(%d, ", var);
printOut(tr);
QP_printf(", ");
printOut(fa);
QP_printf(")\n");
QP_flush(QP_curout);
#endif
assert(var>=0);
assert(var<MAXVAR);
assert(IS_TERMINAL(tr) || tr->value > var);
assert(IS_TERMINAL(fa) || fa->value > var);
COUNT_FN(make_node);
if (tr == fa) return tr;