Skip to content

Commit

Permalink
WIP fast finite set implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
SJulianS committed Nov 13, 2023
1 parent 7194edd commit d2c4aad
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 24 deletions.
20 changes: 10 additions & 10 deletions include/hal_core/netlist/decorators/netlist_traversal_decorator.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ namespace hal
* @param[in] entry_endpoint_filter - Filter condition that determines whether to stop traversal on a successor/predecessor endpoint.
* @returns The next gates fulfilling the target gate filter condition.
*/
Result<std::unordered_set<Gate*>> get_next_gates_fancy(const Net* net,
bool successors,
const std::function<bool(const Gate*)>& target_gate_filter,
const std::function<bool(const Endpoint*, const u32 current_depth)>& exit_endpoint_filter = nullptr,
const std::function<bool(const Endpoint*, const u32 current_depth)>& entry_endpoint_filter = nullptr) const;
Result<std::set<Gate*>> get_next_gates_fancy(const Net* net,
bool successors,
const std::function<bool(const Gate*)>& target_gate_filter,
const std::function<bool(const Endpoint*, const u32 current_depth)>& exit_endpoint_filter = nullptr,
const std::function<bool(const Endpoint*, const u32 current_depth)>& entry_endpoint_filter = nullptr) const;

/**
* Starting from the given gate, traverse the netlist and return only the successor/predecessor gates for which the `target_gate_filter` evaluates to `true`.
Expand All @@ -105,11 +105,11 @@ namespace hal
* @param[in] entry_endpoint_filter - Filter condition that determines whether to stop traversal on a successor/predecessor endpoint.
* @returns The next gates fulfilling the target gate filter condition.
*/
Result<std::unordered_set<Gate*>> get_next_gates_fancy(const Gate* gate,
bool successors,
const std::function<bool(const Gate*)>& target_gate_filter,
const std::function<bool(const Endpoint*, const u32 current_depth)>& exit_endpoint_filter = nullptr,
const std::function<bool(const Endpoint*, const u32 current_depth)>& entry_endpoint_filter = nullptr) const;
Result<std::set<Gate*>> get_next_gates_fancy(const Gate* gate,
bool successors,
const std::function<bool(const Gate*)>& target_gate_filter,
const std::function<bool(const Endpoint*, const u32 current_depth)>& exit_endpoint_filter = nullptr,
const std::function<bool(const Endpoint*, const u32 current_depth)>& entry_endpoint_filter = nullptr) const;

private:
const Netlist& m_netlist;
Expand Down
61 changes: 61 additions & 0 deletions include/hal_core/utilities/finite_set.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// MIT License
//
// Copyright (c) 2019 Ruhr University Bochum, Chair for Embedded Security. All Rights reserved.
// Copyright (c) 2019 Marc Fyrbiak, Sebastian Wallat, Max Hoffmann ("ORIGINAL AUTHORS"). All rights reserved.
// Copyright (c) 2021 Max Planck Institute for Security and Privacy. All Rights reserved.
// Copyright (c) 2021 Jörn Langheinrich, Julian Speith, Nils Albartus, René Walendy, Simon Klix ("ORIGINAL AUTHORS"). All Rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#pragma once

#include "hal_core/defines.h"

#include <unordered_map>
#include <vector>

namespace hal
{
template<typename T>
struct FiniteSet
{
FiniteSet(const u32 size);

bool operator==(const FiniteSet& rhs) const;
bool operator<(const FiniteSet& rhs) const;
FiniteSet<T> operator&(const FiniteSet& rhs) const; // intersect
FiniteSet<T> operator|(const FiniteSet& rhs) const; // union
FiniteSet<T> operator-(const FiniteSet& rhs) const; // difference
FiniteSet<T> operator^(const FiniteSet& rhs) const; // symmetric difference

bool is_disjoint(const FiniteSet& rhs) const;
bool is_subset(const FiniteSet& rhs) const;
bool is_superset(const FiniteSet& rhs) const;

bool insert(const u32 index);
bool erase(const u32 index);
bool contains(const u32 index);

u32 m_size;
std::vector<u64> m_content;

private:
void initialize();
};
} // namespace hal
24 changes: 12 additions & 12 deletions src/netlist/decorators/netlist_traversal_decorator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ namespace hal
return OK(res);
}

Result<std::unordered_set<Gate*>> NetlistTraversalDecorator::get_next_gates_fancy(const Net* net,
bool successors,
const std::function<bool(const Gate*)>& target_gate_filter,
const std::function<bool(const Endpoint*, u32 current_depth)>& exit_endpoint_filter,
const std::function<bool(const Endpoint*, u32 current_depth)>& entry_endpoint_filter) const
Result<std::set<Gate*>> NetlistTraversalDecorator::get_next_gates_fancy(const Net* net,
bool successors,
const std::function<bool(const Gate*)>& target_gate_filter,
const std::function<bool(const Endpoint*, u32 current_depth)>& exit_endpoint_filter,
const std::function<bool(const Endpoint*, u32 current_depth)>& entry_endpoint_filter) const
{
if (net == nullptr)
{
Expand All @@ -157,7 +157,7 @@ namespace hal
std::unordered_set<const Net*> visited;
std::vector<const Net*> stack = {net};
std::vector<const Net*> previous;
std::unordered_set<Gate*> res;
std::set<Gate*> res;
while (!stack.empty())
{
const Net* current = stack.back();
Expand Down Expand Up @@ -217,11 +217,11 @@ namespace hal
return OK(res);
}

Result<std::unordered_set<Gate*>> NetlistTraversalDecorator::get_next_gates_fancy(const Gate* gate,
bool successors,
const std::function<bool(const Gate*)>& target_gate_filter,
const std::function<bool(const Endpoint*, u32 current_depth)>& exit_endpoint_filter,
const std::function<bool(const Endpoint*, u32 current_depth)>& entry_endpoint_filter) const
Result<std::set<Gate*>> NetlistTraversalDecorator::get_next_gates_fancy(const Gate* gate,
bool successors,
const std::function<bool(const Gate*)>& target_gate_filter,
const std::function<bool(const Endpoint*, u32 current_depth)>& exit_endpoint_filter,
const std::function<bool(const Endpoint*, u32 current_depth)>& entry_endpoint_filter) const
{
if (gate == nullptr)
{
Expand All @@ -233,7 +233,7 @@ namespace hal
return ERR("net does not belong to netlist");
}

std::unordered_set<Gate*> res;
std::set<Gate*> res;
for (const auto* exit_ep : successors ? gate->get_fan_out_endpoints() : gate->get_fan_in_endpoints())
{
if (exit_endpoint_filter != nullptr && !exit_endpoint_filter(exit_ep, 0))
Expand Down
4 changes: 2 additions & 2 deletions src/python_bindings/bindings/netlist_traversal_decorator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ namespace hal
bool successors,
const std::function<bool(const Gate*)>& target_gate_filter,
const std::function<bool(const Endpoint*, const u32 current_depth)>& exit_endpoint_filter = nullptr,
const std::function<bool(const Endpoint*, const u32 current_depth)>& entry_endpoint_filter = nullptr) -> std::optional<std::unordered_set<Gate*>> {
const std::function<bool(const Endpoint*, const u32 current_depth)>& entry_endpoint_filter = nullptr) -> std::optional<std::set<Gate*>> {
auto res = self.get_next_gates_fancy(net, successors, target_gate_filter, exit_endpoint_filter, entry_endpoint_filter);
if (res.is_ok())
{
Expand Down Expand Up @@ -131,7 +131,7 @@ namespace hal
bool successors,
const std::function<bool(const Gate*)>& target_gate_filter,
const std::function<bool(const Endpoint*, const u32 current_depth)>& exit_endpoint_filter = nullptr,
const std::function<bool(const Endpoint*, const u32 current_depth)>& entry_endpoint_filter = nullptr) -> std::optional<std::unordered_set<Gate*>> {
const std::function<bool(const Endpoint*, const u32 current_depth)>& entry_endpoint_filter = nullptr) -> std::optional<std::set<Gate*>> {
auto res = self.get_next_gates_fancy(gate, successors, target_gate_filter, exit_endpoint_filter, entry_endpoint_filter);
if (res.is_ok())
{
Expand Down
108 changes: 108 additions & 0 deletions src/utilities/finite_set.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include "hal_core/utilities/finite_set.h"

namespace hal
{
template<typename T>
FiniteSet<T>::FiniteSet(const u32 size) : m_size(size)
{
m_content = std::vector<u64>((m_size / 64) + 1, 0);
}

template<typename T>
bool FiniteSet<T>::operator==(const FiniteSet& rhs) const
{
return m_content == rhs.m_content;
}

template<typename T>
bool FiniteSet<T>::operator<(const FiniteSet& rhs) const
{
return m_content < rhs.m_content;
}

template<typename T>
FiniteSet<T> FiniteSet<T>::operator&(const FiniteSet& rhs) const
{
FiniteSet<T> res(m_size);
for (u32 i = 0; i < m_size; i++)
{
res.m_content[i] = m_content[i] & rhs.m_content[i];
}
return std::move(res);
}

template<typename T>
FiniteSet<T> FiniteSet<T>::operator|(const FiniteSet& rhs) const
{
FiniteSet<T> res(m_size);
for (u32 i = 0; i < m_size; i++)
{
res.m_content[i] = m_content[i] | rhs.m_content[i];
}
return std::move(res);
}

template<typename T>
FiniteSet<T> FiniteSet<T>::operator-(const FiniteSet& rhs) const
{
FiniteSet<T> res(m_size);
for (u32 i = 0; i < m_size; i++)
{
res.m_content[i] = m_content[i] & ~rhs.m_content[i];
}
return std::move(res);
}

template<typename T>
FiniteSet<T> FiniteSet<T>::operator^(const FiniteSet& rhs) const
{
FiniteSet<T> res(m_size);
for (u32 i = 0; i < m_size; i++)
{
res.m_content[i] = m_content[i] ^ rhs.m_content[i];
}
return std::move(res);
}

template<typename T>
bool FiniteSet<T>::is_disjoint(const FiniteSet& rhs) const
{
// TODO implement
return false;
}

template<typename T>
bool FiniteSet<T>::is_subset(const FiniteSet& rhs) const
{
// TODO implement
return false;
}

template<typename T>
bool FiniteSet<T>::is_superset(const FiniteSet& rhs) const
{
// TODO implement
return false;
}

template<typename T>
bool FiniteSet<T>::insert(const u32 index)
{
// TODO implement
return false;
}

template<typename T>
bool FiniteSet<T>::erase(const u32 index)
{
// TODO implement
return false;
}

template<typename T>
bool FiniteSet<T>::contains(const u32 index)
{
// TODO implement
return false;
}
} // namespace hal

0 comments on commit d2c4aad

Please sign in to comment.