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
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
|
/**CFile****************************************************************
FileName [dauTree.c]
SystemName [ABC: Logic synthesis and verification system.]
PackageName [DAG-aware unmapping.]
Synopsis [Canonical DSD package.]
Author [Alan Mishchenko]
Affiliation [UC Berkeley]
Date [Ver. 1.0. Started - June 20, 2005.]
Revision [$Id: dauTree.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
***********************************************************************/
#include "dauInt.h"
#include "misc/mem/mem.h"
#include "misc/util/utilTruth.h"
ABC_NAMESPACE_IMPL_START
////////////////////////////////////////////////////////////////////////
/// DECLARATIONS ///
////////////////////////////////////////////////////////////////////////
typedef struct Dss_Fun_t_ Dss_Fun_t;
struct Dss_Fun_t_
{
unsigned iDsd : 26; // DSD literal
unsigned nFans : 6; // fanin count
unsigned char pFans[0]; // fanins
};
typedef struct Dss_Ent_t_ Dss_Ent_t;
struct Dss_Ent_t_
{
Dss_Fun_t * pFunc;
Dss_Ent_t * pNext;
unsigned iDsd0 : 27; // dsd entry
unsigned nWords : 5; // total word count (struct + shared)
unsigned iDsd1 : 27; // dsd entry
unsigned nShared: 5; // shared count
unsigned char pShared[0]; // shared literals
};
typedef struct Dss_Obj_t_ Dss_Obj_t;
struct Dss_Obj_t_
{
unsigned Id; // node ID
unsigned Type : 3; // node type
unsigned nSupp : 8; // variable
unsigned iVar : 8; // variable
unsigned nWords : 6; // variable
unsigned fMark0 : 1; // user mark
unsigned fMark1 : 1; // user mark
unsigned nFans : 5; // fanin count
unsigned pFans[0]; // fanins
};
typedef struct Dss_Ntk_t_ Dss_Ntk_t;
struct Dss_Ntk_t_
{
int nVars; // the number of variables
int nMem; // memory used
int nMemAlloc; // memory allocated
word * pMem; // memory array
Dss_Obj_t * pRoot; // root node
Vec_Ptr_t * vObjs; // internal nodes
};
struct Dss_Man_t_
{
int nVars; // variable number
int nNonDecLimit; // limit on non-dec size
int nBins; // table size
unsigned * pBins; // hash table
Mem_Flex_t * pMem; // memory for nodes
Vec_Ptr_t * vObjs; // objects
Vec_Int_t * vNexts; // next pointers
Vec_Int_t * vLeaves; // temp
Vec_Int_t * vCopies; // temp
word ** pTtElems; // elementary TTs
Dss_Ent_t ** pCache; // decomposition cache
int nCache; // size of decomposition cache
Mem_Flex_t * pMemEnts; // memory for cache entries
int nCacheHits[2];
int nCacheMisses[2];
int nCacheEntries[2];
abctime timeBeg;
abctime timeDec;
abctime timeLook;
abctime timeEnd;
};
static inline Dss_Obj_t * Dss_Regular( Dss_Obj_t * p ) { return (Dss_Obj_t *)((ABC_PTRUINT_T)(p) & ~01); }
static inline Dss_Obj_t * Dss_Not( Dss_Obj_t * p ) { return (Dss_Obj_t *)((ABC_PTRUINT_T)(p) ^ 01); }
static inline Dss_Obj_t * Dss_NotCond( Dss_Obj_t * p, int c ) { return (Dss_Obj_t *)((ABC_PTRUINT_T)(p) ^ (c)); }
static inline int Dss_IsComplement( Dss_Obj_t * p ) { return (int)((ABC_PTRUINT_T)(p) & 01); }
static inline int Dss_EntWordNum( Dss_Ent_t * p ) { return sizeof(Dss_Ent_t) / 8 + p->nShared / 4 + ((p->nShared & 3) > 0); }
static inline int Dss_FunWordNum( Dss_Fun_t * p ) { assert(p->nFans >= 2); return (p->nFans + 4) / 8 + (((p->nFans + 4) & 7) > 0); }
static inline int Dss_ObjWordNum( int nFans ) { return sizeof(Dss_Obj_t) / 8 + nFans / 2 + ((nFans & 1) > 0); }
static inline word * Dss_ObjTruth( Dss_Obj_t * pObj ) { return (word *)pObj + pObj->nWords; }
static inline void Dss_ObjClean( Dss_Obj_t * pObj ) { memset( pObj, 0, sizeof(Dss_Obj_t) ); }
static inline int Dss_ObjId( Dss_Obj_t * pObj ) { return pObj->Id; }
static inline int Dss_ObjType( Dss_Obj_t * pObj ) { return pObj->Type; }
static inline int Dss_ObjSuppSize( Dss_Obj_t * pObj ) { return pObj->nSupp; }
static inline int Dss_ObjFaninNum( Dss_Obj_t * pObj ) { return pObj->nFans; }
static inline int Dss_ObjFaninC( Dss_Obj_t * pObj, int i ) { assert(i < (int)pObj->nFans); return Abc_LitIsCompl(pObj->pFans[i]); }
static inline Dss_Obj_t * Dss_VecObj( Vec_Ptr_t * p, int Id ) { return (Dss_Obj_t *)Vec_PtrEntry(p, Id); }
static inline Dss_Obj_t * Dss_VecConst0( Vec_Ptr_t * p ) { return Dss_VecObj( p, 0 ); }
static inline Dss_Obj_t * Dss_VecVar( Vec_Ptr_t * p, int v ) { return Dss_VecObj( p, v+1 ); }
static inline int Dss_VecLitSuppSize( Vec_Ptr_t * p, int iLit ) { return Dss_VecObj( p, Abc_Lit2Var(iLit) )->nSupp; }
static inline int Dss_Obj2Lit( Dss_Obj_t * pObj ) { return Abc_Var2Lit(Dss_Regular(pObj)->Id, Dss_IsComplement(pObj)); }
static inline Dss_Obj_t * Dss_Lit2Obj( Vec_Ptr_t * p, int iLit ) { return Dss_NotCond(Dss_VecObj(p, Abc_Lit2Var(iLit)), Abc_LitIsCompl(iLit)); }
static inline Dss_Obj_t * Dss_ObjFanin( Vec_Ptr_t * p, Dss_Obj_t * pObj, int i ) { assert(i < (int)pObj->nFans); return Dss_VecObj(p, Abc_Lit2Var(pObj->pFans[i])); }
static inline Dss_Obj_t * Dss_ObjChild( Vec_Ptr_t * p, Dss_Obj_t * pObj, int i ) { assert(i < (int)pObj->nFans); return Dss_Lit2Obj(p, pObj->pFans[i]); }
#define Dss_VecForEachObj( vVec, pObj, i ) \
Vec_PtrForEachEntry( Dss_Obj_t *, vVec, pObj, i )
#define Dss_VecForEachObjVec( vLits, vVec, pObj, i ) \
for ( i = 0; (i < Vec_IntSize(vLits)) && ((pObj) = Dss_Lit2Obj(vVec, Vec_IntEntry(vLits,i))); i++ )
#define Dss_VecForEachNode( vVec, pObj, i ) \
Vec_PtrForEachEntry( Dss_Obj_t *, vVec, pObj, i ) \
if ( pObj->Type == DAU_DSD_CONST0 || pObj->Type == DAU_DSD_VAR ) {} else
#define Dss_ObjForEachFanin( vVec, pObj, pFanin, i ) \
for ( i = 0; (i < Dss_ObjFaninNum(pObj)) && ((pFanin) = Dss_ObjFanin(vVec, pObj, i)); i++ )
#define Dss_ObjForEachChild( vVec, pObj, pFanin, i ) \
for ( i = 0; (i < Dss_ObjFaninNum(pObj)) && ((pFanin) = Dss_ObjChild(vVec, pObj, i)); i++ )
static inline int Dss_WordCountOnes( unsigned uWord )
{
uWord = (uWord & 0x55555555) + ((uWord>>1) & 0x55555555);
uWord = (uWord & 0x33333333) + ((uWord>>2) & 0x33333333);
uWord = (uWord & 0x0F0F0F0F) + ((uWord>>4) & 0x0F0F0F0F);
uWord = (uWord & 0x00FF00FF) + ((uWord>>8) & 0x00FF00FF);
return (uWord & 0x0000FFFF) + (uWord>>16);
}
static inline int Dss_Lit2Lit( int * pMapLit, int Lit ) { return Abc_Var2Lit( Abc_Lit2Var(pMapLit[Abc_Lit2Var(Lit)]), Abc_LitIsCompl(Lit) ^ Abc_LitIsCompl(pMapLit[Abc_Lit2Var(Lit)]) ); }
////////////////////////////////////////////////////////////////////////
/// FUNCTION DEFINITIONS ///
////////////////////////////////////////////////////////////////////////
#if 0
/**Function*************************************************************
Synopsis [Check decomposability for 666.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
// recursively collects 6-feasible supports
int Dss_ObjCheck666_rec( Dss_Ntk_t * p, Dss_Obj_t * pObj, Vec_Int_t * vSupps )
{
Dss_Obj_t * pFanin;
int i, uSupp = 0;
assert( !Dss_IsComplement(pObj) );
if ( pObj->Type == DAU_DSD_VAR )
{
assert( pObj->iVar >= 0 && pObj->iVar < 30 );
return (1 << pObj->iVar);
}
if ( pObj->Type == DAU_DSD_AND || pObj->Type == DAU_DSD_XOR )
{
int c0, c1, c2, uSuppTemp;
int uSuppVars[16];
int nSuppVars = 0;
int nFanins = Dss_ObjFaninNum(pObj);
int uSupps[16], nSuppSizes[16];
Dss_ObjForEachFanin( p->vObjs, pObj, pFanin, i )
{
uSupps[i] = Dss_ObjCheck666_rec( p, pFanin, vSupps );
nSuppSizes[i] = Dss_WordCountOnes( uSupps[i] );
uSupp |= uSupps[i];
if ( nSuppSizes[i] == 1 )
uSuppVars[nSuppVars++] = uSupps[i];
}
// iterate through the permutations
for ( c0 = 0; c0 < nFanins; c0++ )
if ( nSuppSizes[c0] > 1 && nSuppSizes[c0] < 6 )
{
uSuppTemp = uSupps[c0];
for ( i = 0; i < nSuppVars; i++ )
if ( nSuppSizes[c0] + i < 6 )
uSuppTemp |= uSuppVars[i];
else
break;
if ( Dss_WordCountOnes(uSuppTemp) <= 6 )
Vec_IntPush( vSupps, uSuppTemp );
for ( c1 = c0 + 1; c1 < nFanins; c1++ )
if ( nSuppSizes[c1] > 1 && nSuppSizes[c1] < 6 )
{
if ( nSuppSizes[c0] + nSuppSizes[c1] <= 6 )
Vec_IntPush( vSupps, uSupps[c0] | uSupps[c1] );
uSuppTemp = uSupps[c0] | uSupps[c1];
for ( i = 0; i < nSuppVars; i++ )
if ( nSuppSizes[c0] + nSuppSizes[c1] + i < 6 )
uSuppTemp |= uSuppVars[i];
else
break;
if ( Dss_WordCountOnes(uSuppTemp) <= 6 )
Vec_IntPush( vSupps, uSuppTemp );
for ( c2 = c1 + 1; c2 < nFanins; c2++ )
if ( nSuppSizes[c2] > 1 && nSuppSizes[c2] < 6 )
{
if ( nSuppSizes[c0] + nSuppSizes[c1] + nSuppSizes[c2] <= 6 )
Vec_IntPush( vSupps, uSupps[c0] | uSupps[c1] | uSupps[c2] );
assert( nSuppSizes[c0] + nSuppSizes[c1] + nSuppSizes[c2] >= 6 );
}
}
}
if ( nSuppVars > 1 && nSuppVars <= 6 )
{
uSuppTemp = 0;
for ( i = 0; i < nSuppVars; i++ )
uSuppTemp |= uSuppVars[i];
Vec_IntPush( vSupps, uSuppTemp );
}
else if ( nSuppVars > 6 && nSuppVars <= 12 )
{
uSuppTemp = 0;
for ( i = 0; i < 6; i++ )
uSuppTemp |= uSuppVars[i];
Vec_IntPush( vSupps, uSuppTemp );
uSuppTemp = 0;
for ( i = 6; i < nSuppVars; i++ )
uSuppTemp |= uSuppVars[i];
Vec_IntPush( vSupps, uSuppTemp );
}
}
else if ( pObj->Type == DAU_DSD_MUX || pObj->Type == DAU_DSD_PRIME )
{
Dss_ObjForEachFanin( p->vObjs, pObj, pFanin, i )
uSupp |= Dss_ObjCheck666_rec( p, pFanin, vSupps );
}
if ( Dss_WordCountOnes( uSupp ) <= 6 )
Vec_IntPush( vSupps, uSupp );
return uSupp;
}
int Dss_ObjCheck666( Dss_Ntk_t * p )
{
Vec_Int_t * vSupps;
int i, k, SuppI, SuppK;
int nSupp = Dss_ObjSuppSize(Dss_Regular(p->pRoot));
if ( nSupp <= 6 )
return 1;
// compute supports
vSupps = Vec_IntAlloc( 100 );
Dss_ObjCheck666_rec( p, Dss_Regular(p->pRoot), vSupps );
Vec_IntUniqify( vSupps );
Vec_IntForEachEntry( vSupps, SuppI, i )
{
k = Dss_WordCountOnes(SuppI);
assert( k > 0 && k <= 6 );
/*
for ( k = 0; k < 16; k++ )
if ( (SuppI >> k) & 1 )
printf( "%c", 'a' + k );
else
printf( "-" );
printf( "\n" );
*/
}
// consider support pairs
Vec_IntForEachEntry( vSupps, SuppI, i )
Vec_IntForEachEntryStart( vSupps, SuppK, k, i+1 )
{
if ( SuppI & SuppK )
continue;
if ( Dss_WordCountOnes(SuppI | SuppK) + 4 >= nSupp )
{
Vec_IntFree( vSupps );
return 1;
}
}
Vec_IntFree( vSupps );
return 0;
}
void Dau_DsdTest_()
{
/*
extern Dss_Ntk_t * Dss_NtkCreate( char * pDsd, int nVars, word * pTruth );
extern void Dss_NtkFree( Dss_Ntk_t * p );
// char * pDsd = "(!(amn!(bh))[cdeij]!(fklg)o)";
char * pDsd = "<[(ab)(cd)(ef)][(gh)(ij)(kl)](mn)>";
Dss_Ntk_t * pNtk = Dss_NtkCreate( pDsd, 16, NULL );
int Status = Dss_ObjCheck666( pNtk );
Dss_NtkFree( pNtk );
*/
}
abctime if_dec_time;
void Dau_DsdCheckStructOne( word * pTruth, int nVars, int nLeaves )
{
extern Dss_Ntk_t * Dss_NtkCreate( char * pDsd, int nVars, word * pTruth );
extern void Dss_NtkFree( Dss_Ntk_t * p );
static abctime timeTt = 0;
static abctime timeDsd = 0;
abctime clkTt, clkDsd;
char pDsd[1000];
word Truth[1024];
Dss_Ntk_t * pNtk;
int Status, nNonDec;
if ( pTruth == NULL )
{
Abc_PrintTime( 1, "TT runtime", timeTt );
Abc_PrintTime( 1, "DSD runtime", timeDsd );
Abc_PrintTime( 1, "Total ", if_dec_time );
if_dec_time = 0;
timeTt = 0;
timeDsd = 0;
return;
}
Abc_TtCopy( Truth, pTruth, Abc_TtWordNum(nVars), 0 );
nNonDec = Dau_DsdDecompose( Truth, nVars, 0, 0, pDsd );
if ( nNonDec > 0 )
return;
pNtk = Dss_NtkCreate( pDsd, 16, NULL );
// measure DSD runtime
clkDsd = Abc_Clock();
Status = Dss_ObjCheck666( pNtk );
timeDsd += Abc_Clock() - clkDsd;
Dss_NtkFree( pNtk );
// measure TT runtime
clkTt = Abc_Clock();
{
#define CLU_VAR_MAX 16
// decomposition
typedef struct If_Grp_t_ If_Grp_t;
struct If_Grp_t_
{
char nVars;
char nMyu;
char pVars[CLU_VAR_MAX];
};
int nLutLeaf = 6;
int nLutLeaf2 = 6;
int nLutRoot = 6;
If_Grp_t G;
If_Grp_t G2, R;
word Func0, Func1, Func2;
{
extern If_Grp_t If_CluCheck3( void * p, word * pTruth0, int nVars, int nLutLeaf, int nLutLeaf2, int nLutRoot,
If_Grp_t * pR, If_Grp_t * pG2, word * pFunc0, word * pFunc1, word * pFunc2 );
G = If_CluCheck3( NULL, pTruth, nLeaves, nLutLeaf, nLutLeaf2, nLutRoot, &R, &G2, &Func0, &Func1, &Func2 );
}
}
timeTt += Abc_Clock() - clkTt;
}
#endif
/**Function*************************************************************
Synopsis [Elementary truth tables.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
static inline word ** Dss_ManTtElems()
{
static word TtElems[DAU_MAX_VAR+1][DAU_MAX_WORD], * pTtElems[DAU_MAX_VAR+1] = {NULL};
if ( pTtElems[0] == NULL )
{
int v;
for ( v = 0; v <= DAU_MAX_VAR; v++ )
pTtElems[v] = TtElems[v];
Abc_TtElemInit( pTtElems, DAU_MAX_VAR );
}
return pTtElems;
}
/**Function*************************************************************
Synopsis [Creating DSD network.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
Dss_Obj_t * Dss_ObjAllocNtk( Dss_Ntk_t * p, int Type, int nFans, int nTruthVars )
{
Dss_Obj_t * pObj;
pObj = (Dss_Obj_t *)(p->pMem + p->nMem);
Dss_ObjClean( pObj );
pObj->nFans = nFans;
pObj->nWords = Dss_ObjWordNum( nFans );
pObj->Type = Type;
pObj->Id = Vec_PtrSize( p->vObjs );
pObj->iVar = 31;
Vec_PtrPush( p->vObjs, pObj );
p->nMem += pObj->nWords + (nTruthVars ? Abc_TtWordNum(nTruthVars) : 0);
assert( p->nMem < p->nMemAlloc );
return pObj;
}
Dss_Obj_t * Dss_ObjCreateNtk( Dss_Ntk_t * p, int Type, Vec_Int_t * vFaninLits )
{
Dss_Obj_t * pObj;
int i, Entry;
pObj = Dss_ObjAllocNtk( p, Type, Vec_IntSize(vFaninLits), Type == DAU_DSD_PRIME ? Vec_IntSize(vFaninLits) : 0 );
Vec_IntForEachEntry( vFaninLits, Entry, i )
{
pObj->pFans[i] = Entry;
pObj->nSupp += Dss_VecLitSuppSize(p->vObjs, Entry);
}
assert( i == (int)pObj->nFans );
return pObj;
}
Dss_Ntk_t * Dss_NtkAlloc( int nVars )
{
Dss_Ntk_t * p;
Dss_Obj_t * pObj;
int i;
p = ABC_CALLOC( Dss_Ntk_t, 1 );
p->nVars = nVars;
p->nMemAlloc = DAU_MAX_STR;
p->pMem = ABC_ALLOC( word, p->nMemAlloc );
p->vObjs = Vec_PtrAlloc( 100 );
Dss_ObjAllocNtk( p, DAU_DSD_CONST0, 0, 0 );
for ( i = 0; i < nVars; i++ )
{
pObj = Dss_ObjAllocNtk( p, DAU_DSD_VAR, 0, 0 );
pObj->iVar = i;
pObj->nSupp = 1;
}
return p;
}
void Dss_NtkFree( Dss_Ntk_t * p )
{
Vec_PtrFree( p->vObjs );
ABC_FREE( p->pMem );
ABC_FREE( p );
}
void Dss_NtkPrint_rec( Dss_Ntk_t * p, Dss_Obj_t * pObj )
{
char OpenType[7] = {0, 0, 0, '(', '[', '<', '{'};
char CloseType[7] = {0, 0, 0, ')', ']', '>', '}'};
Dss_Obj_t * pFanin;
int i;
assert( !Dss_IsComplement(pObj) );
if ( pObj->Type == DAU_DSD_VAR )
{ printf( "%c", 'a' + pObj->iVar ); return; }
if ( pObj->Type == DAU_DSD_PRIME )
Abc_TtPrintHexRev( stdout, Dss_ObjTruth(pObj), pObj->nFans );
printf( "%c", OpenType[pObj->Type] );
Dss_ObjForEachFanin( p->vObjs, pObj, pFanin, i )
{
printf( "%s", Dss_ObjFaninC(pObj, i) ? "!":"" );
Dss_NtkPrint_rec( p, pFanin );
}
printf( "%c", CloseType[pObj->Type] );
}
void Dss_NtkPrint( Dss_Ntk_t * p )
{
if ( Dss_Regular(p->pRoot)->Type == DAU_DSD_CONST0 )
printf( "%d", Dss_IsComplement(p->pRoot) );
else
{
printf( "%s", Dss_IsComplement(p->pRoot) ? "!":"" );
if ( Dss_Regular(p->pRoot)->Type == DAU_DSD_VAR )
printf( "%c", 'a' + Dss_Regular(p->pRoot)->iVar );
else
Dss_NtkPrint_rec( p, Dss_Regular(p->pRoot) );
}
printf( "\n" );
}
/**Function*************************************************************
Synopsis [Creating DSD network from SOP.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
static inline void Dau_DsdMergeMatches( char * pDsd, int * pMatches )
{
int pNested[DAU_MAX_VAR];
int i, nNested = 0;
for ( i = 0; pDsd[i]; i++ )
{
pMatches[i] = 0;
if ( pDsd[i] == '(' || pDsd[i] == '[' || pDsd[i] == '<' || pDsd[i] == '{' )
pNested[nNested++] = i;
else if ( pDsd[i] == ')' || pDsd[i] == ']' || pDsd[i] == '>' || pDsd[i] == '}' )
pMatches[pNested[--nNested]] = i;
assert( nNested < DAU_MAX_VAR );
}
assert( nNested == 0 );
}
int Dss_NtkCreate_rec( char * pStr, char ** p, int * pMatches, Dss_Ntk_t * pNtk, word * pTruth )
{
int fCompl = 0;
if ( **p == '!' )
{
fCompl = 1;
(*p)++;
}
while ( (**p >= 'A' && **p <= 'F') || (**p >= '0' && **p <= '9') )
(*p)++;
/*
if ( **p == '<' )
{
char * q = pStr + pMatches[ *p - pStr ];
if ( *(q+1) == '{' )
*p = q+1;
}
*/
if ( **p >= 'a' && **p <= 'z' ) // var
return Abc_Var2Lit( Dss_ObjId(Dss_VecVar(pNtk->vObjs, **p - 'a')), fCompl );
if ( **p == '(' || **p == '[' || **p == '<' || **p == '{' ) // and/or/xor
{
Dss_Obj_t * pObj;
Vec_Int_t * vFaninLits = Vec_IntAlloc( 10 );
char * q = pStr + pMatches[ *p - pStr ];
int Type;
if ( **p == '(' )
Type = DAU_DSD_AND;
else if ( **p == '[' )
Type = DAU_DSD_XOR;
else if ( **p == '<' )
Type = DAU_DSD_MUX;
else if ( **p == '{' )
Type = DAU_DSD_PRIME;
else assert( 0 );
assert( *q == **p + 1 + (**p != '(') );
for ( (*p)++; *p < q; (*p)++ )
Vec_IntPush( vFaninLits, Dss_NtkCreate_rec(pStr, p, pMatches, pNtk, pTruth) );
assert( *p == q );
if ( Type == DAU_DSD_PRIME )
{
Vec_Int_t * vFaninLitsNew;
word pTemp[DAU_MAX_WORD];
char pCanonPerm[DAU_MAX_VAR];
int i, uCanonPhase, nFanins = Vec_IntSize(vFaninLits);
Abc_TtCopy( pTemp, pTruth, Abc_TtWordNum(nFanins), 0 );
uCanonPhase = Abc_TtCanonicize( pTemp, nFanins, pCanonPerm );
fCompl = (uCanonPhase >> nFanins) & 1;
vFaninLitsNew = Vec_IntAlloc( nFanins );
for ( i = 0; i < nFanins; i++ )
Vec_IntPush( vFaninLitsNew, Abc_LitNotCond(Vec_IntEntry(vFaninLits, pCanonPerm[i]), (uCanonPhase>>i)&1) );
pObj = Dss_ObjCreateNtk( pNtk, DAU_DSD_PRIME, vFaninLitsNew );
Abc_TtCopy( Dss_ObjTruth(pObj), pTemp, Abc_TtWordNum(nFanins), 0 );
Vec_IntFree( vFaninLitsNew );
}
else
pObj = Dss_ObjCreateNtk( pNtk, Type, vFaninLits );
Vec_IntFree( vFaninLits );
return Abc_LitNotCond( Dss_Obj2Lit(pObj), fCompl );
}
assert( 0 );
return -1;
}
Dss_Ntk_t * Dss_NtkCreate( char * pDsd, int nVars, word * pTruth )
{
int fCompl = 0;
Dss_Ntk_t * pNtk = Dss_NtkAlloc( nVars );
if ( *pDsd == '!' )
pDsd++, fCompl = 1;
if ( Dau_DsdIsConst(pDsd) )
pNtk->pRoot = Dss_VecConst0(pNtk->vObjs);
else if ( Dau_DsdIsVar(pDsd) )
pNtk->pRoot = Dss_VecVar(pNtk->vObjs, Dau_DsdReadVar(pDsd));
else
{
int iLit, pMatches[DAU_MAX_STR];
Dau_DsdMergeMatches( pDsd, pMatches );
iLit = Dss_NtkCreate_rec( pDsd, &pDsd, pMatches, pNtk, pTruth );
pNtk->pRoot = Dss_Lit2Obj( pNtk->vObjs, iLit );
}
if ( fCompl )
pNtk->pRoot = Dss_Not(pNtk->pRoot);
return pNtk;
}
/**Function*************************************************************
Synopsis [Comparing two DSD nodes.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
int Dss_ObjCompare( Vec_Ptr_t * p, Dss_Obj_t * p0i, Dss_Obj_t * p1i )
{
Dss_Obj_t * p0 = Dss_Regular(p0i);
Dss_Obj_t * p1 = Dss_Regular(p1i);
Dss_Obj_t * pChild0, * pChild1;
int i, Res;
if ( Dss_ObjType(p0) < Dss_ObjType(p1) )
return -1;
if ( Dss_ObjType(p0) > Dss_ObjType(p1) )
return 1;
if ( Dss_ObjType(p0) < DAU_DSD_AND )
return 0;
if ( Dss_ObjFaninNum(p0) < Dss_ObjFaninNum(p1) )
return -1;
if ( Dss_ObjFaninNum(p0) > Dss_ObjFaninNum(p1) )
return 1;
for ( i = 0; i < Dss_ObjFaninNum(p0); i++ )
{
pChild0 = Dss_ObjChild( p, p0, i );
pChild1 = Dss_ObjChild( p, p1, i );
Res = Dss_ObjCompare( p, pChild0, pChild1 );
if ( Res != 0 )
return Res;
}
if ( Dss_IsComplement(p0i) < Dss_IsComplement(p1i) )
return -1;
if ( Dss_IsComplement(p0i) > Dss_IsComplement(p1i) )
return 1;
return 0;
}
void Dss_ObjSort( Vec_Ptr_t * p, Dss_Obj_t ** pNodes, int nNodes, int * pPerm )
{
int i, j, best_i;
for ( i = 0; i < nNodes-1; i++ )
{
best_i = i;
for ( j = i+1; j < nNodes; j++ )
if ( Dss_ObjCompare(p, pNodes[best_i], pNodes[j]) == 1 )
best_i = j;
if ( i == best_i )
continue;
ABC_SWAP( Dss_Obj_t *, pNodes[i], pNodes[best_i] );
if ( pPerm )
ABC_SWAP( int, pPerm[i], pPerm[best_i] );
}
}
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
void Dss_NtkCheck( Dss_Ntk_t * p )
{
Dss_Obj_t * pObj, * pFanin;
int i, k;
Dss_VecForEachNode( p->vObjs, pObj, i )
{
Dss_ObjForEachFanin( p->vObjs, pObj, pFanin, k )
{
if ( pObj->Type == DAU_DSD_AND && pFanin->Type == DAU_DSD_AND )
assert( Dss_ObjFaninC(pObj, k) );
else if ( pObj->Type == DAU_DSD_XOR )
assert( pFanin->Type != DAU_DSD_XOR );
else if ( pObj->Type == DAU_DSD_MUX )
assert( !Dss_ObjFaninC(pObj, 0) );
}
}
}
int Dss_NtkCollectPerm_rec( Dss_Ntk_t * p, Dss_Obj_t * pObj, int * pPermDsd, int * pnPerms )
{
Dss_Obj_t * pChild;
int k, fCompl = Dss_IsComplement(pObj);
pObj = Dss_Regular( pObj );
if ( pObj->Type == DAU_DSD_VAR )
{
pPermDsd[*pnPerms] = Abc_Var2Lit(pObj->iVar, fCompl);
pObj->iVar = (*pnPerms)++;
return fCompl;
}
Dss_ObjForEachChild( p->vObjs, pObj, pChild, k )
if ( Dss_NtkCollectPerm_rec( p, pChild, pPermDsd, pnPerms ) )
pObj->pFans[k] = (unsigned char)Abc_LitRegular((int)pObj->pFans[k]);
return 0;
}
void Dss_NtkTransform( Dss_Ntk_t * p, int * pPermDsd )
{
Dss_Obj_t * pChildren[DAU_MAX_VAR];
Dss_Obj_t * pObj, * pChild;
int i, k, nPerms;
if ( Dss_Regular(p->pRoot)->Type == DAU_DSD_CONST0 )
return;
Dss_VecForEachNode( p->vObjs, pObj, i )
{
if ( pObj->Type == DAU_DSD_MUX || pObj->Type == DAU_DSD_PRIME )
continue;
Dss_ObjForEachChild( p->vObjs, pObj, pChild, k )
pChildren[k] = pChild;
Dss_ObjSort( p->vObjs, pChildren, Dss_ObjFaninNum(pObj), NULL );
for ( k = 0; k < Dss_ObjFaninNum(pObj); k++ )
pObj->pFans[k] = Dss_Obj2Lit( pChildren[k] );
}
nPerms = 0;
if ( Dss_NtkCollectPerm_rec( p, p->pRoot, pPermDsd, &nPerms ) )
p->pRoot = Dss_Regular(p->pRoot);
assert( nPerms == (int)Dss_Regular(p->pRoot)->nSupp );
}
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
Dss_Obj_t * Dss_ObjAlloc( Dss_Man_t * p, int Type, int nFans, int nTruthVars )
{
int nWords = Dss_ObjWordNum(nFans) + (nTruthVars ? Abc_TtWordNum(nTruthVars) : 0);
Dss_Obj_t * pObj = (Dss_Obj_t *)Mem_FlexEntryFetch( p->pMem, sizeof(word) * nWords );
Dss_ObjClean( pObj );
pObj->Type = Type;
pObj->nFans = nFans;
pObj->nWords = Dss_ObjWordNum(nFans);
pObj->Id = Vec_PtrSize( p->vObjs );
pObj->iVar = 31;
Vec_PtrPush( p->vObjs, pObj );
Vec_IntPush( p->vNexts, 0 );
return pObj;
}
Dss_Obj_t * Dss_ObjCreate( Dss_Man_t * p, int Type, Vec_Int_t * vFaninLits, word * pTruth )
{
Dss_Obj_t * pObj, * pFanin, * pPrev = NULL;
int i, Entry;
// check structural canonicity
assert( Type != DAU_DSD_MUX || Vec_IntSize(vFaninLits) == 3 );
assert( Type != DAU_DSD_MUX || !Abc_LitIsCompl(Vec_IntEntry(vFaninLits, 0)) );
assert( Type != DAU_DSD_MUX || !Abc_LitIsCompl(Vec_IntEntry(vFaninLits, 1)) || !Abc_LitIsCompl(Vec_IntEntry(vFaninLits, 2)) );
// check that leaves are in good order
if ( Type == DAU_DSD_AND || Type == DAU_DSD_XOR )
Dss_VecForEachObjVec( vFaninLits, p->vObjs, pFanin, i )
{
assert( Type != DAU_DSD_AND || Abc_LitIsCompl(Vec_IntEntry(vFaninLits, i)) || Dss_ObjType(pFanin) != DAU_DSD_AND );
assert( Type != DAU_DSD_XOR || Dss_ObjType(pFanin) != DAU_DSD_XOR );
assert( pPrev == NULL || Dss_ObjCompare(p->vObjs, pPrev, pFanin) <= 0 );
pPrev = pFanin;
}
// create new node
pObj = Dss_ObjAlloc( p, Type, Vec_IntSize(vFaninLits), Type == DAU_DSD_PRIME ? Vec_IntSize(vFaninLits) : 0 );
if ( Type == DAU_DSD_PRIME )
Abc_TtCopy( Dss_ObjTruth(pObj), pTruth, Abc_TtWordNum(Vec_IntSize(vFaninLits)), 0 );
assert( pObj->nSupp == 0 );
Vec_IntForEachEntry( vFaninLits, Entry, i )
{
pObj->pFans[i] = Entry;
pObj->nSupp += Dss_VecLitSuppSize(p->vObjs, Entry);
}
/*
{
extern void Dss_ManPrintOne( Dss_Man_t * p, int iDsdLit, int * pPermLits );
Dss_ManPrintOne( p, Dss_Obj2Lit(pObj), NULL );
}
*/
return pObj;
}
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
void Dss_ManHashProfile( Dss_Man_t * p )
{
Dss_Obj_t * pObj;
unsigned * pSpot;
int i, Counter;
for ( i = 0; i < p->nBins; i++ )
{
Counter = 0;
for ( pSpot = p->pBins + i; *pSpot; pSpot = (unsigned *)Vec_IntEntryP(p->vNexts, pObj->Id), Counter++ )
pObj = Dss_VecObj( p->vObjs, *pSpot );
if ( Counter )
printf( "%d ", Counter );
}
printf( "\n" );
}
static inline unsigned Dss_ObjHashKey( Dss_Man_t * p, int Type, Vec_Int_t * vFaninLits, word * pTruth )
{
static int s_Primes[8] = { 1699, 4177, 5147, 5647, 6343, 7103, 7873, 8147 };
int i, Entry;
unsigned uHash = Type * 7873 + Vec_IntSize(vFaninLits) * 8147;
Vec_IntForEachEntry( vFaninLits, Entry, i )
uHash += Entry * s_Primes[i & 0x7];
assert( (Type == DAU_DSD_PRIME) == (pTruth != NULL) );
if ( pTruth )
{
unsigned char * pTruthC = (unsigned char *)pTruth;
int nBytes = Abc_TtByteNum(Vec_IntSize(vFaninLits));
for ( i = 0; i < nBytes; i++ )
uHash += pTruthC[i] * s_Primes[i & 0x7];
}
return uHash % p->nBins;
}
unsigned * Dss_ObjHashLookup( Dss_Man_t * p, int Type, Vec_Int_t * vFaninLits, word * pTruth )
{
Dss_Obj_t * pObj;
unsigned * pSpot = p->pBins + Dss_ObjHashKey(p, Type, vFaninLits, pTruth);
for ( ; *pSpot; pSpot = (unsigned *)Vec_IntEntryP(p->vNexts, pObj->Id) )
{
pObj = Dss_VecObj( p->vObjs, *pSpot );
if ( (int)pObj->Type == Type &&
(int)pObj->nFans == Vec_IntSize(vFaninLits) &&
!memcmp(pObj->pFans, Vec_IntArray(vFaninLits), sizeof(int)*pObj->nFans) &&
(pTruth == NULL || !memcmp(Dss_ObjTruth(pObj), pTruth, Abc_TtByteNum(pObj->nFans))) ) // equal
return pSpot;
}
return pSpot;
}
Dss_Obj_t * Dss_ObjFindOrAdd( Dss_Man_t * p, int Type, Vec_Int_t * vFaninLits, word * pTruth )
{
Dss_Obj_t * pObj;
unsigned * pSpot = Dss_ObjHashLookup( p, Type, vFaninLits, pTruth );
if ( *pSpot )
return Dss_VecObj( p->vObjs, *pSpot );
*pSpot = Vec_PtrSize( p->vObjs );
pObj = Dss_ObjCreate( p, Type, vFaninLits, pTruth );
return pObj;
}
/**Function*************************************************************
Synopsis [Cache for decomposition calls.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
void Dss_ManCacheAlloc( Dss_Man_t * p )
{
assert( p->nCache == 0 );
p->nCache = Abc_PrimeCudd( 100000 );
p->pCache = ABC_CALLOC( Dss_Ent_t *, p->nCache );
}
void Dss_ManCacheFree( Dss_Man_t * p )
{
if ( p->pCache == NULL )
return;
assert( p->nCache != 0 );
p->nCache = 0;
ABC_FREE( p->pCache );
}
static inline unsigned Dss_ManCacheHashKey( Dss_Man_t * p, Dss_Ent_t * pEnt )
{
static int s_Primes[8] = { 1699, 4177, 5147, 5647, 6343, 7103, 7873, 8147 };
int i;
unsigned uHash = pEnt->nShared * 7103 + pEnt->iDsd0 * 7873 + pEnt->iDsd1 * 8147;
for ( i = 0; i < 2*(int)pEnt->nShared; i++ )
uHash += pEnt->pShared[i] * s_Primes[i & 0x7];
return uHash % p->nCache;
}
void Dss_ManCacheProfile( Dss_Man_t * p )
{
Dss_Ent_t ** pSpot;
int i, Counter;
for ( i = 0; i < p->nCache; i++ )
{
Counter = 0;
for ( pSpot = p->pCache + i; *pSpot; pSpot = &(*pSpot)->pNext, Counter++ )
;
if ( Counter )
printf( "%d ", Counter );
}
printf( "\n" );
}
Dss_Ent_t ** Dss_ManCacheLookup( Dss_Man_t * p, Dss_Ent_t * pEnt )
{
Dss_Ent_t ** pSpot = p->pCache + Dss_ManCacheHashKey( p, pEnt );
for ( ; *pSpot; pSpot = &(*pSpot)->pNext )
{
if ( (*pSpot)->iDsd0 == pEnt->iDsd0 &&
(*pSpot)->iDsd1 == pEnt->iDsd1 &&
(*pSpot)->nShared == pEnt->nShared &&
!memcmp((*pSpot)->pShared, pEnt->pShared, sizeof(char)*2*pEnt->nShared) ) // equal
{
p->nCacheHits[pEnt->nShared!=0]++;
return pSpot;
}
}
p->nCacheMisses[pEnt->nShared!=0]++;
return pSpot;
}
Dss_Ent_t * Dss_ManCacheCreate( Dss_Man_t * p, Dss_Ent_t * pEnt0, Dss_Fun_t * pFun0 )
{
Dss_Ent_t * pEnt = (Dss_Ent_t *)Mem_FlexEntryFetch( p->pMemEnts, sizeof(word) * pEnt0->nWords );
Dss_Fun_t * pFun = (Dss_Fun_t *)Mem_FlexEntryFetch( p->pMemEnts, sizeof(word) * Dss_FunWordNum(pFun0) );
memcpy( pEnt, pEnt0, sizeof(word) * pEnt0->nWords );
memcpy( pFun, pFun0, sizeof(word) * Dss_FunWordNum(pFun0) );
pEnt->pFunc = pFun;
pEnt->pNext = NULL;
p->nCacheEntries[pEnt->nShared!=0]++;
return pEnt;
}
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
Dss_Man_t * Dss_ManAlloc( int nVars, int nNonDecLimit )
{
Dss_Man_t * p;
p = ABC_CALLOC( Dss_Man_t, 1 );
p->nVars = nVars;
p->nNonDecLimit = nNonDecLimit;
p->nBins = Abc_PrimeCudd( 1000000 );
p->pBins = ABC_CALLOC( unsigned, p->nBins );
p->pMem = Mem_FlexStart();
p->vObjs = Vec_PtrAlloc( 10000 );
p->vNexts = Vec_IntAlloc( 10000 );
Dss_ObjAlloc( p, DAU_DSD_CONST0, 0, 0 );
Dss_ObjAlloc( p, DAU_DSD_VAR, 0, 0 )->nSupp = 1;
p->vLeaves = Vec_IntAlloc( 32 );
p->vCopies = Vec_IntAlloc( 32 );
p->pTtElems = Dss_ManTtElems();
p->pMemEnts = Mem_FlexStart();
// Dss_ManCacheAlloc( p );
return p;
}
void Dss_ManFree( Dss_Man_t * p )
{
Abc_PrintTime( 1, "Time begin ", p->timeBeg );
Abc_PrintTime( 1, "Time decomp", p->timeDec );
Abc_PrintTime( 1, "Time lookup", p->timeLook );
Abc_PrintTime( 1, "Time end ", p->timeEnd );
// Dss_ManCacheProfile( p );
Dss_ManCacheFree( p );
Mem_FlexStop( p->pMemEnts, 0 );
Vec_IntFreeP( &p->vCopies );
Vec_IntFreeP( &p->vLeaves );
Vec_IntFreeP( &p->vNexts );
Vec_PtrFreeP( &p->vObjs );
Mem_FlexStop( p->pMem, 0 );
ABC_FREE( p->pBins );
ABC_FREE( p );
}
void Dss_ManPrint_rec( FILE * pFile, Dss_Man_t * p, Dss_Obj_t * pObj, int * pPermLits, int * pnSupp )
{
char OpenType[7] = {0, 0, 0, '(', '[', '<', '{'};
char CloseType[7] = {0, 0, 0, ')', ']', '>', '}'};
Dss_Obj_t * pFanin;
int i;
assert( !Dss_IsComplement(pObj) );
if ( pObj->Type == DAU_DSD_CONST0 )
{ fprintf( pFile, "0" ); return; }
if ( pObj->Type == DAU_DSD_VAR )
{
int iPermLit = pPermLits ? pPermLits[(*pnSupp)++] : Abc_Var2Lit((*pnSupp)++, 0);
fprintf( pFile, "%s%c", Abc_LitIsCompl(iPermLit)? "!":"", 'a' + Abc_Lit2Var(iPermLit) );
return;
}
if ( pObj->Type == DAU_DSD_PRIME )
Abc_TtPrintHexRev( pFile, Dss_ObjTruth(pObj), pObj->nFans );
fprintf( pFile, "%c", OpenType[pObj->Type] );
Dss_ObjForEachFanin( p->vObjs, pObj, pFanin, i )
{
fprintf( pFile, "%s", Dss_ObjFaninC(pObj, i) ? "!":"" );
Dss_ManPrint_rec( pFile, p, pFanin, pPermLits, pnSupp );
}
fprintf( pFile, "%c", CloseType[pObj->Type] );
}
void Dss_ManPrintOne( FILE * pFile, Dss_Man_t * p, int iDsdLit, int * pPermLits )
{
int nSupp = 0;
fprintf( pFile, "%6d : ", Abc_Lit2Var(iDsdLit) );
fprintf( pFile, "%2d ", Dss_VecLitSuppSize(p->vObjs, iDsdLit) );
fprintf( pFile, "%s", Abc_LitIsCompl(iDsdLit) ? "!" : "" );
Dss_ManPrint_rec( pFile, p, Dss_VecObj(p->vObjs, Abc_Lit2Var(iDsdLit)), pPermLits, &nSupp );
fprintf( pFile, "\n" );
assert( nSupp == (int)Dss_VecObj(p->vObjs, Abc_Lit2Var(iDsdLit))->nSupp );
}
int Dss_ManCheckNonDec_rec( Dss_Man_t * p, Dss_Obj_t * pObj )
{
Dss_Obj_t * pFanin;
int i;
assert( !Dss_IsComplement(pObj) );
if ( pObj->Type == DAU_DSD_CONST0 )
return 0;
if ( pObj->Type == DAU_DSD_VAR )
return 0;
if ( pObj->Type == DAU_DSD_PRIME )
return 1;
Dss_ObjForEachFanin( p->vObjs, pObj, pFanin, i )
if ( Dss_ManCheckNonDec_rec( p, pFanin ) )
return 1;
return 0;
}
void Dss_ManDump( Dss_Man_t * p )
{
char * pFileName = "dss_tts.txt";
FILE * pFile;
word Temp[DAU_MAX_WORD];
Dss_Obj_t * pObj;
int i;
pFile = fopen( pFileName, "wb" );
if ( pFile == NULL )
{
printf( "Cannot open file \"%s\".\n", pFileName );
return;
}
Dss_VecForEachObj( p->vObjs, pObj, i )
{
if ( pObj->Type != DAU_DSD_PRIME )
continue;
Abc_TtCopy( Temp, Dss_ObjTruth(pObj), Abc_TtWordNum(pObj->nFans), 0 );
Abc_TtStretch6( Temp, pObj->nFans, p->nVars );
fprintf( pFile, "0x" );
Abc_TtPrintHexRev( pFile, Temp, p->nVars );
fprintf( pFile, "\n" );
// printf( "%6d : ", i );
// Abc_TtPrintHexRev( stdout, Temp, p->nVars );
// printf( " " );
// Dau_DsdPrintFromTruth( stdout, Temp, p->nVars );
}
fclose( pFile );
}
void Dss_ManPrint( char * pFileName, Dss_Man_t * p )
{
Dss_Obj_t * pObj;
int CountNonDsd = 0, CountNonDsdStr = 0;
int i, clk = Abc_Clock();
FILE * pFile;
pFile = pFileName ? fopen( pFileName, "wb" ) : stdout;
if ( pFileName && pFile == NULL )
{
printf( "cannot open output file\n" );
return;
}
Dss_VecForEachObj( p->vObjs, pObj, i )
{
CountNonDsd += (pObj->Type == DAU_DSD_PRIME);
CountNonDsdStr += Dss_ManCheckNonDec_rec( p, pObj );
}
fprintf( pFile, "Total number of objects = %8d\n", Vec_PtrSize(p->vObjs) );
fprintf( pFile, "Non-DSD objects (max =%2d) = %8d\n", p->nNonDecLimit, CountNonDsd );
fprintf( pFile, "Non-DSD structures = %8d\n", CountNonDsdStr );
fprintf( pFile, "Memory used for objects = %6.2f MB.\n", 1.0*Mem_FlexReadMemUsage(p->pMem)/(1<<20) );
fprintf( pFile, "Memory used for array = %6.2f MB.\n", 1.0*sizeof(void *)*Vec_PtrCap(p->vObjs)/(1<<20) );
fprintf( pFile, "Memory used for hash table = %6.2f MB.\n", 1.0*sizeof(int)*p->nBins/(1<<20) );
fprintf( pFile, "Memory used for cache = %6.2f MB.\n", 1.0*Mem_FlexReadMemUsage(p->pMemEnts)/(1<<20) );
fprintf( pFile, "Cache hits = %8d %8d\n", p->nCacheHits[0], p->nCacheHits[1] );
fprintf( pFile, "Cache misses = %8d %8d\n", p->nCacheMisses[0], p->nCacheMisses[1] );
fprintf( pFile, "Cache entries = %8d %8d\n", p->nCacheEntries[0], p->nCacheEntries[1] );
Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
// Dss_ManHashProfile( p );
// Dss_ManDump( p );
// return;
Dss_VecForEachObj( p->vObjs, pObj, i )
{
if ( i == 50 )
break;
Dss_ManPrintOne( pFile, p, Dss_Obj2Lit(pObj), NULL );
}
fprintf( pFile, "\n" );
if ( pFileName )
fclose( pFile );
}
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
void Dss_ManComputeTruth_rec( Dss_Man_t * p, Dss_Obj_t * pObj, int nVars, word * pRes, int * pPermLits, int * pnSupp )
{
Dss_Obj_t * pChild;
int nWords = Abc_TtWordNum(nVars);
int i, fCompl = Dss_IsComplement(pObj);
pObj = Dss_Regular(pObj);
if ( pObj->Type == DAU_DSD_VAR )
{
int iPermLit = pPermLits[(*pnSupp)++];
assert( (*pnSupp) <= nVars );
Abc_TtCopy( pRes, p->pTtElems[Abc_Lit2Var(iPermLit)], nWords, fCompl ^ Abc_LitIsCompl(iPermLit) );
return;
}
if ( pObj->Type == DAU_DSD_AND || pObj->Type == DAU_DSD_XOR )
{
word pTtTemp[DAU_MAX_WORD];
if ( pObj->Type == DAU_DSD_AND )
Abc_TtConst1( pRes, nWords );
else
Abc_TtConst0( pRes, nWords );
Dss_ObjForEachChild( p->vObjs, pObj, pChild, i )
{
Dss_ManComputeTruth_rec( p, pChild, nVars, pTtTemp, pPermLits, pnSupp );
if ( pObj->Type == DAU_DSD_AND )
Abc_TtAnd( pRes, pRes, pTtTemp, nWords, 0 );
else
Abc_TtXor( pRes, pRes, pTtTemp, nWords, 0 );
}
if ( fCompl ) Abc_TtNot( pRes, nWords );
return;
}
if ( pObj->Type == DAU_DSD_MUX ) // mux
{
word pTtTemp[3][DAU_MAX_WORD];
Dss_ObjForEachChild( p->vObjs, pObj, pChild, i )
Dss_ManComputeTruth_rec( p, pChild, nVars, pTtTemp[i], pPermLits, pnSupp );
assert( i == 3 );
Abc_TtMux( pRes, pTtTemp[0], pTtTemp[1], pTtTemp[2], nWords );
if ( fCompl ) Abc_TtNot( pRes, nWords );
return;
}
if ( pObj->Type == DAU_DSD_PRIME ) // function
{
word pFanins[DAU_MAX_VAR][DAU_MAX_WORD];
Dss_ObjForEachChild( p->vObjs, pObj, pChild, i )
Dss_ManComputeTruth_rec( p, pChild, nVars, pFanins[i], pPermLits, pnSupp );
Dau_DsdTruthCompose_rec( Dss_ObjTruth(pObj), pFanins, pRes, pObj->nFans, nWords );
if ( fCompl ) Abc_TtNot( pRes, nWords );
return;
}
assert( 0 );
}
word * Dss_ManComputeTruth( Dss_Man_t * p, int iDsd, int nVars, int * pPermLits )
{
Dss_Obj_t * pObj = Dss_Lit2Obj(p->vObjs, iDsd);
word * pRes = p->pTtElems[DAU_MAX_VAR];
int nWords = Abc_TtWordNum( nVars );
int nSupp = 0;
assert( nVars <= DAU_MAX_VAR );
if ( iDsd == 0 )
Abc_TtConst0( pRes, nWords );
else if ( iDsd == 1 )
Abc_TtConst1( pRes, nWords );
else if ( Dss_Regular(pObj)->Type == DAU_DSD_VAR )
{
int iPermLit = pPermLits[nSupp++];
Abc_TtCopy( pRes, p->pTtElems[Abc_Lit2Var(iPermLit)], nWords, Abc_LitIsCompl(iDsd) ^ Abc_LitIsCompl(iPermLit) );
}
else
Dss_ManComputeTruth_rec( p, pObj, nVars, pRes, pPermLits, &nSupp );
assert( nSupp == (int)Dss_Regular(pObj)->nSupp );
return pRes;
}
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
// returns literal of non-shifted tree in p, corresponding to pObj in pNtk, which may be compl
int Dss_NtkRebuild_rec( Dss_Man_t * p, Dss_Ntk_t * pNtk, Dss_Obj_t * pObj )
{
Dss_Obj_t * pChildren[DAU_MAX_VAR];
Dss_Obj_t * pChild, * pObjNew;
int i, k, fCompl = Dss_IsComplement(pObj);
pObj = Dss_Regular(pObj);
if ( pObj->Type == DAU_DSD_VAR )
return Abc_Var2Lit( 1, fCompl );
Dss_ObjForEachChild( pNtk->vObjs, pObj, pChild, k )
{
pChildren[k] = Dss_Lit2Obj( p->vObjs, Dss_NtkRebuild_rec( p, pNtk, pChild ) );
if ( pObj->Type == DAU_DSD_XOR && Dss_IsComplement(pChildren[k]) )
pChildren[k] = Dss_Not(pChildren[k]), fCompl ^= 1;
}
// normalize MUX
if ( pObj->Type == DAU_DSD_MUX )
{
if ( Dss_IsComplement(pChildren[0]) )
{
pChildren[0] = Dss_Not(pChildren[0]);
ABC_SWAP( Dss_Obj_t *, pChildren[1], pChildren[2] );
}
if ( Dss_IsComplement(pChildren[1]) )
{
pChildren[1] = Dss_Not(pChildren[1]);
pChildren[2] = Dss_Not(pChildren[2]);
fCompl ^= 1;
}
}
// shift subgraphs
Vec_IntClear( p->vLeaves );
for ( i = 0; i < k; i++ )
Vec_IntPush( p->vLeaves, Dss_Obj2Lit(pChildren[i]) );
// create new graph
pObjNew = Dss_ObjFindOrAdd( p, pObj->Type, p->vLeaves, pObj->Type == DAU_DSD_PRIME ? Dss_ObjTruth(pObj) : NULL );
return Abc_Var2Lit( pObjNew->Id, fCompl );
}
int Dss_NtkRebuild( Dss_Man_t * p, Dss_Ntk_t * pNtk )
{
assert( p->nVars == pNtk->nVars );
if ( Dss_Regular(pNtk->pRoot)->Type == DAU_DSD_CONST0 )
return Dss_IsComplement(pNtk->pRoot);
if ( Dss_Regular(pNtk->pRoot)->Type == DAU_DSD_VAR )
return Abc_Var2Lit( Dss_Regular(pNtk->pRoot)->iVar + 1, Dss_IsComplement(pNtk->pRoot) );
return Dss_NtkRebuild_rec( p, pNtk, pNtk->pRoot );
}
/**Function*************************************************************
Synopsis [Performs DSD operation on the two literals.]
Description [Returns the perm of the resulting literals. The perm size
is equal to the number of support variables. The perm variables are 0-based
numbers of pLits[0] followed by nLits[0]-based numbers of pLits[1].]
SideEffects []
SeeAlso []
***********************************************************************/
int Dss_ManOperation( Dss_Man_t * p, int Type, int * pLits, int nLits, unsigned char * pPerm, word * pTruth )
{
Dss_Obj_t * pChildren[DAU_MAX_VAR];
Dss_Obj_t * pObj, * pChild;
int i, k, nChildren = 0, fCompl = 0, fComplFan;
assert( Type == DAU_DSD_AND || pPerm == NULL );
if ( Type == DAU_DSD_AND && pPerm != NULL )
{
int pBegEnd[DAU_MAX_VAR];
int j, nSSize = 0;
for ( k = 0; k < nLits; k++ )
{
pObj = Dss_Lit2Obj(p->vObjs, pLits[k]);
if ( Dss_IsComplement(pObj) || pObj->Type != DAU_DSD_AND )
{
fComplFan = (Dss_Regular(pObj)->Type == DAU_DSD_VAR && Dss_IsComplement(pObj));
if ( fComplFan )
pObj = Dss_Regular(pObj);
pBegEnd[nChildren] = (nSSize << 16) | (fComplFan << 8) | (nSSize + Dss_Regular(pObj)->nSupp);
nSSize += Dss_Regular(pObj)->nSupp;
pChildren[nChildren++] = pObj;
}
else
Dss_ObjForEachChild( p->vObjs, pObj, pChild, i )
{
fComplFan = (Dss_Regular(pChild)->Type == DAU_DSD_VAR && Dss_IsComplement(pChild));
if ( fComplFan )
pChild = Dss_Regular(pChild);
pBegEnd[nChildren] = (nSSize << 16) | (fComplFan << 8) | (nSSize + Dss_Regular(pChild)->nSupp);
nSSize += Dss_Regular(pChild)->nSupp;
pChildren[nChildren++] = pChild;
}
}
Dss_ObjSort( p->vObjs, pChildren, nChildren, pBegEnd );
// create permutation
for ( j = i = 0; i < nChildren; i++ )
for ( k = (pBegEnd[i] >> 16); k < (pBegEnd[i] & 0xFF); k++ )
pPerm[j++] = (unsigned char)Abc_Var2Lit( k, (pBegEnd[i] >> 8) & 1 );
assert( j == nSSize );
}
else if ( Type == DAU_DSD_AND )
{
for ( k = 0; k < nLits; k++ )
{
pObj = Dss_Lit2Obj(p->vObjs, pLits[k]);
if ( Dss_IsComplement(pObj) || pObj->Type != DAU_DSD_AND )
pChildren[nChildren++] = pObj;
else
Dss_ObjForEachChild( p->vObjs, pObj, pChild, i )
pChildren[nChildren++] = pChild;
}
Dss_ObjSort( p->vObjs, pChildren, nChildren, NULL );
}
else if ( Type == DAU_DSD_XOR )
{
for ( k = 0; k < nLits; k++ )
{
fCompl ^= Abc_LitIsCompl(pLits[k]);
pObj = Dss_Lit2Obj(p->vObjs, Abc_LitRegular(pLits[k]));
if ( pObj->Type != DAU_DSD_XOR )
pChildren[nChildren++] = pObj;
else
Dss_ObjForEachChild( p->vObjs, pObj, pChild, i )
{
assert( !Dss_IsComplement(pChild) );
pChildren[nChildren++] = pChild;
}
}
Dss_ObjSort( p->vObjs, pChildren, nChildren, NULL );
}
else if ( Type == DAU_DSD_MUX )
{
if ( Abc_LitIsCompl(pLits[0]) )
{
pLits[0] = Abc_LitNot(pLits[0]);
ABC_SWAP( int, pLits[1], pLits[2] );
}
if ( Abc_LitIsCompl(pLits[1]) )
{
pLits[1] = Abc_LitNot(pLits[1]);
pLits[2] = Abc_LitNot(pLits[2]);
fCompl ^= 1;
}
for ( k = 0; k < nLits; k++ )
pChildren[nChildren++] = Dss_Lit2Obj(p->vObjs, pLits[k]);
}
else if ( Type == DAU_DSD_PRIME )
{
for ( k = 0; k < nLits; k++ )
pChildren[nChildren++] = Dss_Lit2Obj(p->vObjs, pLits[k]);
}
else assert( 0 );
// shift subgraphs
Vec_IntClear( p->vLeaves );
for ( i = 0; i < nChildren; i++ )
Vec_IntPush( p->vLeaves, Dss_Obj2Lit(pChildren[i]) );
// create new graph
pObj = Dss_ObjFindOrAdd( p, Type, p->vLeaves, pTruth );
return Abc_Var2Lit( pObj->Id, fCompl );
}
Dss_Fun_t * Dss_ManOperationFun( Dss_Man_t * p, int * iDsd, int nFansTot )
{
static char Buffer[100];
Dss_Fun_t * pFun = (Dss_Fun_t *)Buffer;
pFun->iDsd = Dss_ManOperation( p, DAU_DSD_AND, iDsd, 2, pFun->pFans, NULL );
//printf( "%d %d -> %d ", iDsd[0], iDsd[1], pFun->iDsd );
pFun->nFans = nFansTot;
assert( (int)pFun->nFans == Dss_VecLitSuppSize(p->vObjs, pFun->iDsd) );
return pFun;
}
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
void Dss_EntPrint( Dss_Ent_t * p, Dss_Fun_t * pFun )
{
int i;
printf( "%d %d ", p->iDsd0, p->iDsd1 );
for ( i = 0; i < (int)p->nShared; i++ )
printf( "%d=%d ", p->pShared[2*i], p->pShared[2*i+1] );
printf( "-> %d ", pFun->iDsd );
}
/**Function*************************************************************
Synopsis [Performs AND on two DSD functions with support overlap.]
Description [Returns the perm of the resulting literals. The perm size
is equal to the number of support variables. The perm variables are 0-based
numbers of pLits[0] followed by nLits[0]-based numbers of pLits[1].]
SideEffects []
SeeAlso []
***********************************************************************/
Dss_Fun_t * Dss_ManBooleanAnd( Dss_Man_t * p, Dss_Ent_t * pEnt, int Counter )
{
static char Buffer[100];
Dss_Fun_t * pFun = (Dss_Fun_t *)Buffer;
Dss_Ntk_t * pNtk;
word * pTruthOne, pTruth[DAU_MAX_WORD];
char pDsd[DAU_MAX_STR];
int pMapDsd2Truth[DAU_MAX_VAR];
int pPermLits[DAU_MAX_VAR];
int pPermDsd[DAU_MAX_VAR];
int i, nNonDec, nSuppSize = 0;
int nFans[2];
nFans[0] = Dss_VecLitSuppSize( p->vObjs, pEnt->iDsd0 );
nFans[1] = Dss_VecLitSuppSize( p->vObjs, pEnt->iDsd1 );
// create first truth table
for ( i = 0; i < nFans[0]; i++ )
{
pMapDsd2Truth[nSuppSize] = i;
pPermLits[i] = Abc_Var2Lit( nSuppSize++, 0 );
}
pTruthOne = Dss_ManComputeTruth( p, pEnt->iDsd0, p->nVars, pPermLits );
Abc_TtCopy( pTruth, pTruthOne, Abc_TtWordNum(p->nVars), 0 );
if ( Counter )
{
//Kit_DsdPrintFromTruth( pTruthOne, p->nVars ); printf( "\n" );
}
// create second truth table
for ( i = 0; i < nFans[1]; i++ )
pPermLits[i] = -1;
for ( i = 0; i < (int)pEnt->nShared; i++ )
pPermLits[pEnt->pShared[2*i+0]] = pEnt->pShared[2*i+1];
for ( i = 0; i < nFans[1]; i++ )
if ( pPermLits[i] == -1 )
{
pMapDsd2Truth[nSuppSize] = nFans[0] + i;
pPermLits[i] = Abc_Var2Lit( nSuppSize++, 0 );
}
pTruthOne = Dss_ManComputeTruth( p, pEnt->iDsd1, p->nVars, pPermLits );
if ( Counter )
{
//Kit_DsdPrintFromTruth( pTruthOne, p->nVars ); printf( "\n" );
}
Abc_TtAnd( pTruth, pTruth, pTruthOne, Abc_TtWordNum(p->nVars), 0 );
// perform decomposition
nNonDec = Dau_DsdDecompose( pTruth, nSuppSize, 0, 0, pDsd );
if ( p->nNonDecLimit && nNonDec > p->nNonDecLimit )
return NULL;
// derive network and convert it into the manager
pNtk = Dss_NtkCreate( pDsd, p->nVars, nNonDec ? pTruth : NULL );
//Dss_NtkPrint( pNtk );
Dss_NtkCheck( pNtk );
Dss_NtkTransform( pNtk, pPermDsd );
//Dss_NtkPrint( pNtk );
pFun->iDsd = Dss_NtkRebuild( p, pNtk );
Dss_NtkFree( pNtk );
// pPermDsd maps vars of iDsdRes into literals of pTruth
// translate this map into the one that maps vars of iDsdRes into literals of cut
pFun->nFans = Dss_VecLitSuppSize( p->vObjs, pFun->iDsd );
for ( i = 0; i < (int)pFun->nFans; i++ )
pFun->pFans[i] = (unsigned char)Abc_Lit2LitV( pMapDsd2Truth, pPermDsd[i] );
// Dss_EntPrint( pEnt, pFun );
return pFun;
}
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
// returns mapping of variables of dsd1 into literals of dsd0
Dss_Ent_t * Dss_ManSharedMap( Dss_Man_t * p, int * iDsd, int * nFans, int ** pFans, unsigned uSharedMask )
{
static char Buffer[100];
Dss_Ent_t * pEnt = (Dss_Ent_t *)Buffer;
pEnt->iDsd0 = iDsd[0];
pEnt->iDsd1 = iDsd[1];
pEnt->nShared = 0;
if ( uSharedMask )
{
int i, g, pMapGtoL[DAU_MAX_VAR] = {-1};
for ( i = 0; i < nFans[0]; i++ )
pMapGtoL[ Abc_Lit2Var(pFans[0][i]) ] = Abc_Var2Lit( i, Abc_LitIsCompl(pFans[0][i]) );
for ( i = 0; i < nFans[1]; i++ )
{
g = Abc_Lit2Var( pFans[1][i] );
if ( (uSharedMask >> g) & 1 )
{
assert( pMapGtoL[g] >= 0 );
pEnt->pShared[2*pEnt->nShared+0] = (unsigned char)i;
pEnt->pShared[2*pEnt->nShared+1] = (unsigned char)Abc_LitNotCond( pMapGtoL[g], Abc_LitIsCompl(pFans[1][i]) );
pEnt->nShared++;
}
}
}
pEnt->nWords = Dss_EntWordNum( pEnt );
return pEnt;
}
// merge two DSD functions
int Dss_ManMerge( Dss_Man_t * p, int * iDsd, int * nFans, int ** pFans, unsigned uSharedMask, int nKLutSize, unsigned char * pPermRes, word * pTruth )
{
int fVerbose = 0;
int fCheck = 0;
static int Counter = 0;
// word pTtTemp[DAU_MAX_WORD];
word * pTruthOne;
int pPermResInt[DAU_MAX_VAR];
Dss_Ent_t * pEnt, ** ppSpot;
Dss_Fun_t * pFun;
int i;
abctime clk;
Counter++;
if ( DAU_MAX_VAR < nKLutSize )
{
printf( "Paramater DAU_MAX_VAR (%d) smaller than LUT size (%d).\n", DAU_MAX_VAR, nKLutSize );
return -1;
}
assert( iDsd[0] <= iDsd[1] );
if ( fVerbose )
{
Dss_ManPrintOne( stdout, p, iDsd[0], pFans[0] );
Dss_ManPrintOne( stdout, p, iDsd[1], pFans[1] );
}
// constant argument
if ( iDsd[0] == 0 ) return 0;
if ( iDsd[0] == 1 ) return iDsd[1];
if ( iDsd[1] == 0 ) return 0;
if ( iDsd[1] == 1 ) return iDsd[0];
// no overlap
clk = Abc_Clock();
assert( nFans[0] == Dss_VecLitSuppSize(p->vObjs, iDsd[0]) );
assert( nFans[1] == Dss_VecLitSuppSize(p->vObjs, iDsd[1]) );
assert( nFans[0] + nFans[1] <= nKLutSize + Dss_WordCountOnes(uSharedMask) );
// create map of shared variables
pEnt = Dss_ManSharedMap( p, iDsd, nFans, pFans, uSharedMask );
p->timeBeg += Abc_Clock() - clk;
// check cache
if ( p->pCache == NULL )
{
clk = Abc_Clock();
if ( uSharedMask == 0 )
pFun = Dss_ManOperationFun( p, iDsd, nFans[0] + nFans[1] );
else
pFun = Dss_ManBooleanAnd( p, pEnt, 0 );
if ( pFun == NULL )
return -1;
assert( (int)pFun->nFans == Dss_VecLitSuppSize(p->vObjs, pFun->iDsd) );
assert( (int)pFun->nFans <= nKLutSize );
p->timeDec += Abc_Clock() - clk;
}
else
{
clk = Abc_Clock();
ppSpot = Dss_ManCacheLookup( p, pEnt );
p->timeLook += Abc_Clock() - clk;
clk = Abc_Clock();
if ( *ppSpot == NULL )
{
if ( uSharedMask == 0 )
pFun = Dss_ManOperationFun( p, iDsd, nFans[0] + nFans[1] );
else
pFun = Dss_ManBooleanAnd( p, pEnt, 0 );
if ( pFun == NULL )
return -1;
assert( (int)pFun->nFans == Dss_VecLitSuppSize(p->vObjs, pFun->iDsd) );
assert( (int)pFun->nFans <= nKLutSize );
// create cache entry
*ppSpot = Dss_ManCacheCreate( p, pEnt, pFun );
}
pFun = (*ppSpot)->pFunc;
p->timeDec += Abc_Clock() - clk;
}
clk = Abc_Clock();
for ( i = 0; i < (int)pFun->nFans; i++ )
if ( pFun->pFans[i] < 2 * nFans[0] ) // first dec
pPermRes[i] = (unsigned char)Dss_Lit2Lit( pFans[0], pFun->pFans[i] );
else
pPermRes[i] = (unsigned char)Dss_Lit2Lit( pFans[1], pFun->pFans[i] - 2 * nFans[0] );
// perform support minimization
if ( uSharedMask && pFun->nFans > 1 )
{
int pVarPres[DAU_MAX_VAR];
int nSupp = 0;
for ( i = 0; i < p->nVars; i++ )
pVarPres[i] = -1;
for ( i = 0; i < (int)pFun->nFans; i++ )
pVarPres[ Abc_Lit2Var(pPermRes[i]) ] = i;
for ( i = 0; i < p->nVars; i++ )
if ( pVarPres[i] >= 0 )
pPermRes[pVarPres[i]] = Abc_Var2Lit( nSupp++, Abc_LitIsCompl(pPermRes[pVarPres[i]]) );
assert( nSupp == (int)pFun->nFans );
}
for ( i = 0; i < (int)pFun->nFans; i++ )
pPermResInt[i] = pPermRes[i];
p->timeEnd += Abc_Clock() - clk;
if ( fVerbose )
{
Dss_ManPrintOne( stdout, p, pFun->iDsd, pPermResInt );
printf( "\n" );
}
if ( Counter == 43418 )
{
// int s = 0;
// Dss_ManPrint( NULL, p );
}
if ( fCheck )
{
pTruthOne = Dss_ManComputeTruth( p, pFun->iDsd, p->nVars, pPermResInt );
if ( !Abc_TtEqual( pTruthOne, pTruth, Abc_TtWordNum(p->nVars) ) )
{
int s;
// Kit_DsdPrintFromTruth( pTruthOne, p->nVars ); printf( "\n" );
// Kit_DsdPrintFromTruth( pTruth, p->nVars ); printf( "\n" );
printf( "Verification failed.\n" );
s = 0;
}
}
return pFun->iDsd;
}
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
Dss_Ent_t * Dss_ManSharedMapDerive( Dss_Man_t * p, int iDsd0, int iDsd1, Vec_Str_t * vShared )
{
static char Buffer[100];
Dss_Ent_t * pEnt = (Dss_Ent_t *)Buffer;
pEnt->iDsd0 = iDsd0;
pEnt->iDsd1 = iDsd1;
pEnt->nShared = Vec_StrSize(vShared)/2;
memcpy( pEnt->pShared, (unsigned char *)Vec_StrArray(vShared), sizeof(char) * Vec_StrSize(vShared) );
pEnt->nWords = Dss_EntWordNum( pEnt );
return pEnt;
}
int Mpm_FuncCompute( Dss_Man_t * p, int iDsd0, int iDsd1, Vec_Str_t * vShared, int * pPerm, int * pnLeaves )
{
int fVerbose = 0;
// int fCheck = 0;
Dss_Ent_t * pEnt, ** ppSpot;
Dss_Fun_t * pFun;
int iDsd[2] = { iDsd0, iDsd1 };
int i;
abctime clk;
assert( iDsd0 <= iDsd1 );
if ( DAU_MAX_VAR < *pnLeaves )
{
printf( "Paramater DAU_MAX_VAR (%d) smaller than LUT size (%d).\n", DAU_MAX_VAR, *pnLeaves );
return -1;
}
if ( fVerbose )
{
Dss_ManPrintOne( stdout, p, iDsd0, NULL );
Dss_ManPrintOne( stdout, p, iDsd1, NULL );
}
clk = Abc_Clock();
pEnt = Dss_ManSharedMapDerive( p, iDsd0, iDsd1, vShared );
ppSpot = Dss_ManCacheLookup( p, pEnt );
p->timeLook += Abc_Clock() - clk;
clk = Abc_Clock();
if ( *ppSpot == NULL )
{
if ( Vec_StrSize(vShared) == 0 )
pFun = Dss_ManOperationFun( p, iDsd, *pnLeaves );
else
pFun = Dss_ManBooleanAnd( p, pEnt, 0 );
if ( pFun == NULL )
return -1;
assert( (int)pFun->nFans == Dss_VecLitSuppSize(p->vObjs, pFun->iDsd) );
assert( (int)pFun->nFans <= *pnLeaves );
// create cache entry
*ppSpot = Dss_ManCacheCreate( p, pEnt, pFun );
}
pFun = (*ppSpot)->pFunc;
p->timeDec += Abc_Clock() - clk;
*pnLeaves = (int)pFun->nFans;
for ( i = 0; i < (int)pFun->nFans; i++ )
pPerm[i] = (int)pFun->pFans[i];
if ( fVerbose )
{
Dss_ManPrintOne( stdout, p, pFun->iDsd, NULL );
printf( "\n" );
}
/*
if ( fCheck )
{
pTruthOne = Dss_ManComputeTruth( p, pFun->iDsd, p->nVars, pPermResInt );
if ( !Abc_TtEqual( pTruthOne, pTruth, Abc_TtWordNum(p->nVars) ) )
{
int s;
// Kit_DsdPrintFromTruth( pTruthOne, p->nVars ); printf( "\n" );
// Kit_DsdPrintFromTruth( pTruth, p->nVars ); printf( "\n" );
printf( "Verification failed.\n" );
s = 0;
}
}
*/
return pFun->iDsd;
}
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
int Dss_ObjCheckTransparent( Dss_Man_t * p, Dss_Obj_t * pObj )
{
Dss_Obj_t * pFanin;
int i;
if ( pObj->Type == DAU_DSD_VAR )
return 1;
if ( pObj->Type == DAU_DSD_AND )
return 0;
if ( pObj->Type == DAU_DSD_XOR )
{
Dss_ObjForEachFanin( p->vObjs, pObj, pFanin, i )
if ( Dss_ObjCheckTransparent( p, pFanin ) )
return 1;
return 0;
}
if ( pObj->Type == DAU_DSD_MUX )
{
pFanin = Dss_ObjFanin( p->vObjs, pObj, 1 );
if ( !Dss_ObjCheckTransparent(p, pFanin) )
return 0;
pFanin = Dss_ObjFanin( p->vObjs, pObj, 2 );
if ( !Dss_ObjCheckTransparent(p, pFanin) )
return 0;
return 1;
}
assert( pObj->Type == DAU_DSD_PRIME );
return 0;
}
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
void Dau_DsdTest__()
{
int nVars = 8;
// char * pDsd = "[(ab)(cd)]";
char * pDsd = "(!(a!(bh))[cde]!(fg))";
Dss_Ntk_t * pNtk = Dss_NtkCreate( pDsd, nVars, NULL );
// Dss_NtkPrint( pNtk );
// Dss_NtkCheck( pNtk );
// Dss_NtkTransform( pNtk );
// Dss_NtkPrint( pNtk );
Dss_NtkFree( pNtk );
nVars = 0;
}
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
void Dau_DsdTest()
{
int nVars = 8;
Vec_Vec_t * vFuncs;
Vec_Int_t * vOne, * vTwo, * vRes;//, * vThree;
Dss_Man_t * p;
int pEntries[3];
int iLit, e0, e1;//, e2;
int i, k, s;//, j;
return;
vFuncs = Vec_VecStart( nVars+1 );
assert( nVars < DAU_MAX_VAR );
p = Dss_ManAlloc( nVars, 0 );
// init
Vec_VecPushInt( vFuncs, 1, Dss_Obj2Lit(Dss_VecVar(p->vObjs,0)) );
// enumerate
for ( s = 2; s <= nVars; s++ )
{
vRes = Vec_VecEntryInt( vFuncs, s );
for ( i = 1; i < s; i++ )
for ( k = i; k < s; k++ )
if ( i + k == s )
{
vOne = Vec_VecEntryInt( vFuncs, i );
vTwo = Vec_VecEntryInt( vFuncs, k );
Vec_IntForEachEntry( vOne, pEntries[0], e0 )
Vec_IntForEachEntry( vTwo, pEntries[1], e1 )
{
int fAddInv0 = !Dss_ObjCheckTransparent( p, Dss_VecObj(p->vObjs, Abc_Lit2Var(pEntries[0])) );
int fAddInv1 = !Dss_ObjCheckTransparent( p, Dss_VecObj(p->vObjs, Abc_Lit2Var(pEntries[1])) );
iLit = Dss_ManOperation( p, DAU_DSD_AND, pEntries, 2, NULL, NULL );
assert( !Abc_LitIsCompl(iLit) );
Vec_IntPush( vRes, iLit );
if ( fAddInv0 )
{
pEntries[0] = Abc_LitNot( pEntries[0] );
iLit = Dss_ManOperation( p, DAU_DSD_AND, pEntries, 2, NULL, NULL );
pEntries[0] = Abc_LitNot( pEntries[0] );
assert( !Abc_LitIsCompl(iLit) );
Vec_IntPush( vRes, iLit );
}
if ( fAddInv1 )
{
pEntries[1] = Abc_LitNot( pEntries[1] );
iLit = Dss_ManOperation( p, DAU_DSD_AND, pEntries, 2, NULL, NULL );
pEntries[1] = Abc_LitNot( pEntries[1] );
assert( !Abc_LitIsCompl(iLit) );
Vec_IntPush( vRes, iLit );
}
if ( fAddInv0 && fAddInv1 )
{
pEntries[0] = Abc_LitNot( pEntries[0] );
pEntries[1] = Abc_LitNot( pEntries[1] );
iLit = Dss_ManOperation( p, DAU_DSD_AND, pEntries, 2, NULL, NULL );
pEntries[0] = Abc_LitNot( pEntries[0] );
pEntries[1] = Abc_LitNot( pEntries[1] );
assert( !Abc_LitIsCompl(iLit) );
Vec_IntPush( vRes, iLit );
}
iLit = Dss_ManOperation( p, DAU_DSD_XOR, pEntries, 2, NULL, NULL );
assert( !Abc_LitIsCompl(iLit) );
Vec_IntPush( vRes, Abc_LitRegular(iLit) );
}
}
/*
for ( i = 1; i < s; i++ )
for ( k = 1; k < s; k++ )
for ( j = 1; j < s; j++ )
if ( i + k + j == s )
{
vOne = Vec_VecEntryInt( vFuncs, i );
vTwo = Vec_VecEntryInt( vFuncs, k );
vThree = Vec_VecEntryInt( vFuncs, j );
Vec_IntForEachEntry( vOne, pEntries[0], e0 )
Vec_IntForEachEntry( vTwo, pEntries[1], e1 )
Vec_IntForEachEntry( vThree, pEntries[2], e2 )
{
int fAddInv0 = !Dss_ObjCheckTransparent( p, Dss_VecObj(p->vObjs, Abc_Lit2Var(pEntries[0])) );
int fAddInv1 = !Dss_ObjCheckTransparent( p, Dss_VecObj(p->vObjs, Abc_Lit2Var(pEntries[1])) );
int fAddInv2 = !Dss_ObjCheckTransparent( p, Dss_VecObj(p->vObjs, Abc_Lit2Var(pEntries[2])) );
if ( !fAddInv0 && k > j )
continue;
iLit = Dss_ManOperation( p, DAU_DSD_MUX, pEntries, 3, NULL, NULL );
assert( !Abc_LitIsCompl(iLit) );
Vec_IntPush( vRes, iLit );
if ( fAddInv1 )
{
pEntries[1] = Abc_LitNot( pEntries[1] );
iLit = Dss_ManOperation( p, DAU_DSD_MUX, pEntries, 3, NULL, NULL );
pEntries[1] = Abc_LitNot( pEntries[1] );
assert( !Abc_LitIsCompl(iLit) );
Vec_IntPush( vRes, iLit );
}
if ( fAddInv2 )
{
pEntries[2] = Abc_LitNot( pEntries[2] );
iLit = Dss_ManOperation( p, DAU_DSD_MUX, pEntries, 3, NULL, NULL );
pEntries[2] = Abc_LitNot( pEntries[2] );
assert( !Abc_LitIsCompl(iLit) );
Vec_IntPush( vRes, iLit );
}
}
}
*/
Vec_IntUniqify( vRes );
}
Dss_ManPrint( "_npn/npn/dsdcanon.txt", p );
Dss_ManFree( p );
Vec_VecFree( vFuncs );
}
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
void Dau_DsdTest444()
{
Dss_Man_t * p = Dss_ManAlloc( 6, 0 );
int iLit1[3] = { 2, 4 };
int iLit2[3] = { 2, 4, 6 };
int iRes[5];
int nFans[2] = { 4, 3 };
int pPermLits1[4] = { 0, 2, 5, 6 };
int pPermLits2[5] = { 2, 9, 10 };
int * pPermLits[2] = { pPermLits1, pPermLits2 };
unsigned char pPermRes[6];
int pPermResInt[6];
unsigned uMaskShared = 2;
int i;
iRes[0] = 1 ^ Dss_ManOperation( p, DAU_DSD_AND, iLit1, 2, NULL, NULL );
iRes[1] = iRes[0];
iRes[2] = 1 ^ Dss_ManOperation( p, DAU_DSD_AND, iRes, 2, NULL, NULL );
iRes[3] = Dss_ManOperation( p, DAU_DSD_AND, iLit2, 3, NULL, NULL );
Dss_ManPrintOne( stdout, p, iRes[0], NULL );
Dss_ManPrintOne( stdout, p, iRes[2], NULL );
Dss_ManPrintOne( stdout, p, iRes[3], NULL );
Dss_ManPrintOne( stdout, p, iRes[2], pPermLits1 );
Dss_ManPrintOne( stdout, p, iRes[3], pPermLits2 );
iRes[4] = Dss_ManMerge( p, iRes+2, nFans, pPermLits, uMaskShared, 6, pPermRes, NULL );
for ( i = 0; i < 6; i++ )
pPermResInt[i] = pPermRes[i];
Dss_ManPrintOne( stdout, p, iRes[4], NULL );
Dss_ManPrintOne( stdout, p, iRes[4], pPermResInt );
Dss_ManFree( p );
}
////////////////////////////////////////////////////////////////////////
/// END OF FILE ///
////////////////////////////////////////////////////////////////////////
ABC_NAMESPACE_IMPL_END
|