From 40b7409c31265d4254ccbe5ea5d9eec95b365555 Mon Sep 17 00:00:00 2001 From: Tolga Uye Date: Fri, 24 Dec 2021 09:16:31 +0100 Subject: [PATCH] All test passed --- .../algorithms/aig_algebraic_rewriting.hpp | 189 +++++++++++++++++- 1 file changed, 182 insertions(+), 7 deletions(-) diff --git a/include/mockturtle/algorithms/aig_algebraic_rewriting.hpp b/include/mockturtle/algorithms/aig_algebraic_rewriting.hpp index 217cd1df1..bcd80362f 100644 --- a/include/mockturtle/algorithms/aig_algebraic_rewriting.hpp +++ b/include/mockturtle/algorithms/aig_algebraic_rewriting.hpp @@ -1,7 +1,6 @@ /*! \file aig_algebraic_rewriting.hpp \brief AIG algebraric rewriting - EPFL CS-472 2021 Final Project Option 1 */ @@ -10,6 +9,7 @@ #include "../networks/aig.hpp" #include "../views/depth_view.hpp" #include "../views/topo_view.hpp" +#include "../utils/debugging_utils.hpp" namespace mockturtle { @@ -50,28 +50,203 @@ class aig_algebraic_rewriting_impl /* Try various algebraic rules on node n. Return true if the network is updated. */ bool try_algebraic_rules( node n ) { + + if ( try_associativity( n ) ) return true; if ( try_distributivity( n ) ) return true; + if(try_three_layer_distributivity(n)) + return true; /* TODO: add more rules here... */ return false; } - /* Try the associativity rule on node n. Return true if the network is updated. */ - bool try_associativity( node n ) + + bool try_associativity(node n) { - /* TODO */ + + if(ntk.is_on_critical_path(n) && ntk.level(n) > 1) + { + //Primary Checks is Associativity even useful / possible? + std::vector critical , noncritical; + uint32_t nonCriticalLevel; + + ntk.foreach_fanin(n, [&](signal const& child){ + + node childnode = ntk.get_node(child); + + if(ntk.is_on_critical_path(childnode) && !ntk.is_complemented(child)) + { + ntk.foreach_fanin(childnode, [&](signal const& grandchild){ + node grandchildnode = ntk.get_node(grandchild); + if(ntk.is_on_critical_path(grandchildnode)) + { + critical.push_back(grandchild); + } + else + { + noncritical.push_back(grandchild); + } + }); + } + else + { + noncritical.push_back(child); + nonCriticalLevel = ntk.level(childnode); + } + + }); + + if(critical.size() == 1 && noncritical.size() == 2) + { + node criticalNode = ntk.get_node(critical[0]); + + if(nonCriticalLevel < ntk.level(criticalNode)) + { + + signal subNode = ntk.create_and(critical[0], ntk.create_and(noncritical[0], noncritical[1])); + ntk.substitute_node(n, subNode); + + return true; + } + + } + + } + return false; } + /* Try the distributivity rule on node n. Return true if the network is updated. */ - bool try_distributivity( node n ) + bool try_distributivity(node n) { - /* TODO */ + if(ntk.is_on_critical_path(n) && ntk.level(n) > 1) + { + std::vector nonCriticalGrandChildren, criticalGrandChildren; + + ntk.foreach_fanin(n, [&](signal const& child){ + + node childnode = ntk.get_node(child); + + if(ntk.is_on_critical_path(childnode)&&ntk.is_complemented(child)) + { + ntk.foreach_fanin(childnode, [&](signal const& grandchild){ + node grandchildnode = ntk.get_node(grandchild); + + if(ntk.is_on_critical_path(grandchildnode)) + { + criticalGrandChildren.push_back(grandchild); + }else{ + nonCriticalGrandChildren.push_back(grandchild); + } + + }); + } + + }); + + if(criticalGrandChildren.size() == 2 && nonCriticalGrandChildren.size() == 2) + { + if(criticalGrandChildren[0] == criticalGrandChildren[1]) + { + signal subNode = ntk.create_nand(criticalGrandChildren[0],ntk.create_or(nonCriticalGrandChildren[0],nonCriticalGrandChildren[1])); + if(ntk.is_or(n)) + subNode = ntk.create_not(subNode); + + ntk.substitute_node(n,subNode); + return true; + } + + } + + } + return false; - } +} + +bool try_three_layer_distributivity(node n) +{ + if (ntk.is_on_critical_path( n )) + { + std::vector nonCriticalChildren, criticalChildren; + std::vector nonCriticalGrandChildren, criticalGrandChildren; + std::vector nonCriticalGranderChildren, criticalGranderChildren; + + + ntk.foreach_fanin(n, [&](signal const& child){ + + node childnode = ntk.get_node(child); + + if(ntk.is_on_critical_path(childnode)&&ntk.is_complemented(child)) + { + criticalChildren.push_back(child); + + ntk.foreach_fanin(childnode, [&](signal const& grandChild){ + + node grandChildNode = ntk.get_node(grandChild); + + if(ntk.is_on_critical_path(grandChildNode)&&ntk.is_complemented(grandChild)) + { + criticalGrandChildren.push_back(grandChild); + + ntk.foreach_fanin(grandChildNode, [&](signal const& granderChild){ + + node granderChildNode = ntk.get_node(granderChild); + + if(ntk.is_on_critical_path(granderChildNode)) + { + criticalGranderChildren.push_back(granderChild); + } + else + { + nonCriticalGranderChildren.push_back(granderChild); + } + + });//granderchild foreach + + } + else + { + nonCriticalGrandChildren.push_back(grandChild); + } + + });//grandchild foreach + + }else{ + nonCriticalChildren.push_back(child); + } + + });//child foreach + + + if(nonCriticalChildren.size() == 1 && criticalChildren.size() == 1 && + nonCriticalGrandChildren.size() == 1 && criticalGrandChildren.size() == 1 && + nonCriticalGranderChildren.size() == 1 && criticalGranderChildren.size() == 1) + { + + node top_crit_node = ntk.get_node(criticalChildren[0]); + node top_noncrit_node = ntk.get_node(nonCriticalChildren[0]); + if(ntk.level(top_crit_node) - ntk.level(top_noncrit_node) >= 2) + { + signal node_bir , node_iki , subNode; + + node_bir = ntk.create_not(ntk.create_and(criticalGranderChildren[0],ntk.create_and(nonCriticalChildren[0],nonCriticalGranderChildren[0]))); + node_iki = ntk.create_not(ntk.create_and(nonCriticalChildren[0], ntk.create_not(nonCriticalGrandChildren[0]))); + subNode = ntk.create_nand(node_bir,node_iki); + + ntk.substitute_node(n, subNode); + return true; + } + } + + } + + + return false; +} private: Ntk& ntk;