From 14f585b883e1a5b0aec60e00102074f47290be15 Mon Sep 17 00:00:00 2001 From: SJulianS Date: Tue, 14 Nov 2023 15:55:20 +0100 Subject: [PATCH 1/3] added optional filter to get_num_of_sources/destinations --- include/hal_core/netlist/net.h | 8 +++++-- src/netlist/net.cpp | 34 ++++++++++++++++++++++++---- src/python_bindings/bindings/net.cpp | 14 ++++++++---- tests/netlist/net.cpp | 7 +++++- 4 files changed, 52 insertions(+), 11 deletions(-) diff --git a/include/hal_core/netlist/net.h b/include/hal_core/netlist/net.h index 4cb0d14ca27..3b5985068fc 100644 --- a/include/hal_core/netlist/net.h +++ b/include/hal_core/netlist/net.h @@ -210,10 +210,12 @@ namespace hal /** * Get the number of sources of the net. + * The optional filter is evaluated on every candidate such that the result only contains those matching the specified condition. * + * @param[in] filter - An optional filter. * @returns The number of sources. */ - u32 get_num_of_sources() const; + u32 get_num_of_sources(const std::function& filter = nullptr) const; /** * Get a vector of sources of the net. @@ -318,10 +320,12 @@ namespace hal /** * Get the number of destinations of the net. + * The optional filter is evaluated on every candidate such that the result only contains those matching the specified condition. * + * @param[in] filter - An optional filter. * @returns The number of destinations. */ - u32 get_num_of_destinations() const; + u32 get_num_of_destinations(const std::function& filter = nullptr) const; /** * Get a vector of destinations of the net. diff --git a/src/netlist/net.cpp b/src/netlist/net.cpp index 2a28be2fcb5..c835516e027 100644 --- a/src/netlist/net.cpp +++ b/src/netlist/net.cpp @@ -243,9 +243,22 @@ namespace hal return std::find(m_sources_raw.begin(), m_sources_raw.end(), ep) != m_sources_raw.end(); } - u32 Net::get_num_of_sources() const + u32 Net::get_num_of_sources(const std::function& filter) const { - return (u32)m_sources_raw.size(); + if (!filter) + { + return (u32)m_sources_raw.size(); + } + + u32 num = 0; + for (auto dst : m_sources_raw) + { + if (filter(dst)) + { + num++; + } + } + return num; } std::vector Net::get_sources(const std::function& filter) const @@ -392,9 +405,22 @@ namespace hal return std::find(m_destinations_raw.begin(), m_destinations_raw.end(), ep) != m_destinations_raw.end(); } - u32 Net::get_num_of_destinations() const + u32 Net::get_num_of_destinations(const std::function& filter) const { - return (u32)m_destinations_raw.size(); + if (!filter) + { + return (u32)m_destinations_raw.size(); + } + + u32 num = 0; + for (auto dst : m_destinations_raw) + { + if (filter(dst)) + { + num++; + } + } + return num; } std::vector Net::get_destinations(const std::function& filter) const diff --git a/src/python_bindings/bindings/net.cpp b/src/python_bindings/bindings/net.cpp index fac346d9008..d2675a1cf16 100644 --- a/src/python_bindings/bindings/net.cpp +++ b/src/python_bindings/bindings/net.cpp @@ -166,15 +166,18 @@ namespace hal :rtype: bool )"); - py_net.def_property_readonly("num_of_sources", &Net::get_num_of_sources, R"( + py_net.def_property_readonly( + "num_of_sources", [](Net* n) { return n->get_num_of_sources(); }, R"( The number of sources of the net. :type: int )"); - py_net.def("get_num_of_sources", &Net::get_num_of_sources, R"( + py_net.def("get_num_of_sources", &Net::get_num_of_sources, py::arg("filter") = nullptr, R"( Get the number of sources of the net. + The optional filter is evaluated on every candidate such that the result only contains those matching the specified condition. + :param lambda filter: An optional filter. :returns: The number of sources. :rtype: int )"); @@ -279,15 +282,18 @@ namespace hal :rtype: bool )"); - py_net.def_property_readonly("num_of_destinations", &Net::get_num_of_destinations, R"( + py_net.def_property_readonly( + "num_of_destinations", [](Net* n) { return n->get_num_of_destinations(); }, R"( The number of destinations of the net. :type: int )"); - py_net.def("get_num_of_destinations", &Net::get_num_of_destinations, R"( + py_net.def("get_num_of_destinations", &Net::get_num_of_destinations, py::arg("filter") = nullptr, R"( Get the number of destinations of the net. + The optional filter is evaluated on every candidate such that the result only contains those matching the specified condition. + :param filter: An optional filter. :returns: The number of destinations. :rtype: int )"); diff --git a/tests/netlist/net.cpp b/tests/netlist/net.cpp index 678e590989a..8fe0af12aba 100644 --- a/tests/netlist/net.cpp +++ b/tests/netlist/net.cpp @@ -451,7 +451,8 @@ namespace hal { Net* test_net = nl->create_net("test_net"); ASSERT_NE(test_net, nullptr); EXPECT_TRUE(test_net->get_destinations().empty()); - } + EXPECT_EQ(test_net->get_num_of_destinations(), 0); + } { // add destination to net auto nl = test_utils::create_empty_netlist(); @@ -461,9 +462,11 @@ namespace hal { Gate* test_gate = nl->create_gate(nl->get_gate_library()->get_gate_type_by_name("BUF"), "test_gate"); ASSERT_NE(test_gate, nullptr); EXPECT_TRUE(test_net->get_destinations().empty()); + EXPECT_EQ(test_net->get_num_of_destinations(), 0); EXPECT_NE(test_net->add_destination(test_gate, "I"), nullptr); ASSERT_EQ(test_net->get_destinations().size(), 1); EXPECT_EQ(test_net->get_destinations().at(0), test_gate->get_fan_in_endpoint("I")); + EXPECT_EQ(test_net->get_num_of_destinations(), 1); } { // get multiple destinations (no filter applied) @@ -478,6 +481,7 @@ namespace hal { EXPECT_NE(test_net->add_destination(test_gate, "DATA_IN(2)"), nullptr); EXPECT_NE(test_net->add_destination(test_gate, "DATA_IN(3)"), nullptr); EXPECT_EQ(test_net->get_destinations(), std::vector(test_gate->get_fan_in_endpoints())); + EXPECT_EQ(test_net->get_num_of_destinations(), 4); } { // get multiple destinations (filter applied) @@ -495,6 +499,7 @@ namespace hal { EXPECT_NE(test_net->add_destination(test_gate_2, "DATA_IN(1)"), nullptr); EXPECT_EQ(test_net->get_destinations([](const Endpoint* ep){return ep->get_gate()->get_name() == "test_gate_1";}), std::vector(test_gate_1->get_fan_in_endpoints())); + EXPECT_EQ(test_net->get_num_of_destinations([](const Endpoint* ep){return ep->get_gate()->get_name() == "test_gate_1";} ), 2); } { // remove a destination by specifying gate and pin From e6dbf6f2b4e808579dc24ea3ca4fa910fb79e85c Mon Sep 17 00:00:00 2001 From: SJulianS Date: Tue, 14 Nov 2023 15:55:45 +0100 Subject: [PATCH 2/3] added insertion by iterator to FiniteSet --- include/hal_core/utilities/finite_set.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/include/hal_core/utilities/finite_set.h b/include/hal_core/utilities/finite_set.h index 9f5f231e9f4..dc542e4cc3d 100644 --- a/include/hal_core/utilities/finite_set.h +++ b/include/hal_core/utilities/finite_set.h @@ -153,6 +153,20 @@ namespace hal return true; } + template + bool insert(InputIt begin, InputIt end) + { + for (InputIt it = begin; it != end; ++it) + { + if (!insert(*it)) + { + return false; + } + } + + return true; + } + bool erase(const u32 index) { if (index >= m_size) From a0871537fd05d2a1bca1434cee1b921f0eb5fbb4 Mon Sep 17 00:00:00 2001 From: SJulianS Date: Tue, 14 Nov 2023 15:56:05 +0100 Subject: [PATCH 3/3] added and, invert, xor gate lib --- .../definitions/helper_libs/aix_hal_i2.hgl | 191 ++++++++++++++++++ 1 file changed, 191 insertions(+) create mode 100644 plugins/gate_libraries/definitions/helper_libs/aix_hal_i2.hgl diff --git a/plugins/gate_libraries/definitions/helper_libs/aix_hal_i2.hgl b/plugins/gate_libraries/definitions/helper_libs/aix_hal_i2.hgl new file mode 100644 index 00000000000..4d666630565 --- /dev/null +++ b/plugins/gate_libraries/definitions/helper_libs/aix_hal_i2.hgl @@ -0,0 +1,191 @@ +{ + "version": 3, + "library": "basic_hal_i2", + "gate_locations": { + "data_category": "generic", + "data_x_identifier": "X_COORDINATE", + "data_y_identifier": "Y_COORDINATE" + }, + "cells": [ + { + "name": "HAL_BUF", + "types": [ + "combinational", + "c_buffer" + ], + "pin_groups": [ + { + "name": "A", + "direction": "input", + "type": "none", + "ascending": false, + "start_index": 0, + "pins": [ + { + "name": "A", + "direction": "input", + "type": "none" + } + ] + }, + { + "name": "O", + "direction": "output", + "type": "none", + "ascending": false, + "start_index": 0, + "pins": [ + { + "name": "O", + "direction": "output", + "type": "none", + "function": "A" + } + ] + } + ] + }, + { + "name": "HAL_INV", + "types": [ + "combinational", + "c_inverter" + ], + "pin_groups": [ + { + "name": "A", + "direction": "input", + "type": "none", + "ascending": false, + "start_index": 0, + "pins": [ + { + "name": "A", + "direction": "input", + "type": "none" + } + ] + }, + { + "name": "O", + "direction": "output", + "type": "none", + "ascending": false, + "start_index": 0, + "pins": [ + { + "name": "O", + "direction": "output", + "type": "none", + "function": "(! A)" + } + ] + } + ] + }, + { + "name": "HAL_AND2", + "types": [ + "combinational", + "c_and" + ], + "pin_groups": [ + { + "name": "A", + "direction": "input", + "type": "none", + "ascending": false, + "start_index": 0, + "pins": [ + { + "name": "A", + "direction": "input", + "type": "none" + } + ] + }, + { + "name": "B", + "direction": "input", + "type": "none", + "ascending": false, + "start_index": 0, + "pins": [ + { + "name": "B", + "direction": "input", + "type": "none" + } + ] + }, + { + "name": "O", + "direction": "output", + "type": "none", + "ascending": false, + "start_index": 0, + "pins": [ + { + "name": "O", + "direction": "output", + "type": "none", + "function": "(A & B)" + } + ] + } + ] + }, + { + "name": "HAL_XOR2", + "types": [ + "combinational", + "c_xor" + ], + "pin_groups": [ + { + "name": "A", + "direction": "input", + "type": "none", + "ascending": false, + "start_index": 0, + "pins": [ + { + "name": "A", + "direction": "input", + "type": "none" + } + ] + }, + { + "name": "B", + "direction": "input", + "type": "none", + "ascending": false, + "start_index": 0, + "pins": [ + { + "name": "B", + "direction": "input", + "type": "none" + } + ] + }, + { + "name": "O", + "direction": "output", + "type": "none", + "ascending": false, + "start_index": 0, + "pins": [ + { + "name": "O", + "direction": "output", + "type": "none", + "function": "(A ^ B)" + } + ] + } + ] + } + ] +} \ No newline at end of file