Skip to content

Commit

Permalink
Merge pull request KLayout#1646 from KLayout/xor-performance
Browse files Browse the repository at this point in the history
Xor performance
  • Loading branch information
klayoutmatthias authored Mar 29, 2024
2 parents b5ee7d3 + e0e6017 commit 14a16f8
Show file tree
Hide file tree
Showing 40 changed files with 1,215 additions and 111 deletions.
10 changes: 8 additions & 2 deletions src/buddies/src/bd/strmxor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -680,13 +680,17 @@ bool run_tiled_xor (const XORData &xor_data)
if (ll->second.first < 0) {
proc.input (in_a, db::RecursiveShapeIterator ());
} else {
proc.input (in_a, db::RecursiveShapeIterator (*xor_data.layout_a, xor_data.layout_a->cell (xor_data.cell_a), ll->second.first));
db::RecursiveShapeIterator si (*xor_data.layout_a, xor_data.layout_a->cell (xor_data.cell_a), ll->second.first);
si.set_for_merged_input (true);
proc.input (in_a, si);
}

if (ll->second.second < 0) {
proc.input (in_b, db::RecursiveShapeIterator ());
} else {
proc.input (in_b, db::RecursiveShapeIterator (*xor_data.layout_b, xor_data.layout_b->cell (xor_data.cell_b), ll->second.second));
db::RecursiveShapeIterator si (*xor_data.layout_b, xor_data.layout_b->cell (xor_data.cell_b), ll->second.second);
si.set_for_merged_input (true);
proc.input (in_b, si);
}

std::string expr = "var x=" + in_a + "^" + in_b + "; ";
Expand Down Expand Up @@ -805,10 +809,12 @@ bool run_deep_xor (const XORData &xor_data)

if (ll->second.first >= 0) {
ri_a = db::RecursiveShapeIterator (*xor_data.layout_a, xor_data.layout_a->cell (xor_data.cell_a), ll->second.first);
ri_a.set_for_merged_input (true);
}

if (ll->second.second >= 0) {
ri_b = db::RecursiveShapeIterator (*xor_data.layout_b, xor_data.layout_b->cell (xor_data.cell_b), ll->second.second);
ri_b.set_for_merged_input (true);
}

db::Region in_a (ri_a, dss, db::ICplxTrans (xor_data.layout_a->dbu () / dbu));
Expand Down
2 changes: 2 additions & 0 deletions src/db/db/dbAsIfFlatRegion.cc
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ AsIfFlatRegion::area (const db::Box &box) const
for (RegionIterator p (begin_merged ()); ! p.at_end (); ++p) {
if (box.empty () || p->box ().inside (box)) {
a += p->area ();
} else if (p->is_box ()) {
a += (p->box () & box).area ();
} else {
std::vector<db::Polygon> clipped;
clip_poly (*p, box, clipped);
Expand Down
82 changes: 82 additions & 0 deletions src/db/db/dbBox.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,27 @@ struct DB_PUBLIC_TEMPLATE box
*/
box<C, R> &operator+= (const point<C> &p);

/**
* @brief Subtraction of boxes.
*
* The -= operator subtracts the argument box from *this.
* Subtraction leaves the bounding box of the region resulting
* from the geometrical NOT of *this and the argument box.
* Subtracting a box from itself gives an empty box.
* Subtracting a box that does not cover a full side of
* *this will not modify the box.
*
* @param b The box to subtract from *this.
*
* @return The result box.
*/
box<C, R> &operator-= (const box<C, R> &b);

/**
* @brief A method version for operator- (mainly for automation purposes)
*/
box<C, R> subtracted (const box<C, R> &b) const;

/**
* @brief Intersection of boxes.
*
Expand Down Expand Up @@ -784,6 +805,50 @@ box<C, R>::operator+= (const point<C> &p)
return *this;
}

template <class C, class R>
inline box<C, R>
box<C, R>::subtracted (const box<C, R> &b) const
{
box<C, R> r (*this);
r -= b;
return r;
}

template <class C, class R>
inline box<C, R> &
box<C, R>::operator-= (const box<C, R> &bx)
{
if (bx.empty () || empty ()) {
return *this;
}

coord_type l = m_p1.x (), r = m_p2.x ();
coord_type b = m_p1.y (), t = m_p2.y ();

if (bx.bottom () <= bottom () && bx.top () >= top ()) {
if (bx.left () <= left ()) {
l = std::max (bx.right (), left ());
}
if (bx.right () >= right ()) {
r = std::min (bx.left (), right ());
}
}

if (bx.left () <= left () && bx.right () >= right ()) {
if (bx.bottom () <= bottom ()) {
b = std::max (bx.top (), bottom ());
}
if (bx.top () >= top ()) {
t = std::min (bx.bottom (), top ());
}
}

m_p1 = point_type (l, b);
m_p2 = point_type (r, t);

return *this;
}

template <class C, class R>
inline box<C, R> &
box<C, R>::operator&= (const box<C, R> &b)
Expand Down Expand Up @@ -1363,6 +1428,23 @@ operator+ (const box<C> &b1, const box<C> &b2)
return bb;
}

/**
* @brief Box subtraction mapped on the - operator
*
* @param b1 The first box
* @param b2 The second box to subtract from the first
*
* @return The bounding box of the region formed but subtracting b2 from b1
*/
template <class C>
inline box<C>
operator- (const box<C> &b1, const box<C> &b2)
{
box<C> bb (b1);
bb -= b2;
return bb;
}

/**
* @brief "Folding" of two boxes
*
Expand Down
20 changes: 20 additions & 0 deletions src/db/db/dbEdgePairs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
#include "dbOriginalLayerEdgePairs.h"
#include "dbEdges.h"
#include "dbRegion.h"
#include "dbLayout.h"
#include "dbWriter.h"
#include "tlStream.h"

#include "tlVariant.h"

Expand Down Expand Up @@ -93,6 +96,23 @@ EdgePairs::EdgePairs (const RecursiveShapeIterator &si, DeepShapeStore &dss, con
mp_delegate = new DeepEdgePairs (si, dss, trans);
}

void
EdgePairs::write (const std::string &fn) const
{
// method provided for debugging purposes

db::Layout layout;
const db::Cell &top = layout.cell (layout.add_cell ("EDGE_PAIRS"));
unsigned int li = layout.insert_layer (db::LayerProperties (0, 0));
insert_into (&layout, top.cell_index (), li);

tl::OutputStream os (fn);
db::SaveLayoutOptions opt;
opt.set_format_from_filename (fn);
db::Writer writer (opt);
writer.write (layout, os);
}

template <class Sh>
void EdgePairs::insert (const Sh &shape)
{
Expand Down
8 changes: 8 additions & 0 deletions src/db/db/dbEdgePairs.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@ class DB_PUBLIC EdgePairs
*/
explicit EdgePairs (const RecursiveShapeIterator &si, DeepShapeStore &dss, const db::ICplxTrans &trans);

/**
* @brief Writes the edge pair collection to a file
*
* This method is provided for debugging purposes. A flat image of the
* region is written to a layout file with a single top cell on layer 0/0.
*/
void write (const std::string &fn) const;

/**
* @brief Implementation of the ShapeCollection interface
*/
Expand Down
13 changes: 13 additions & 0 deletions src/db/db/dbEdgeProcessor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,13 @@ struct CutPoints

}

// do not insert points twice
for (auto c = cut_points.begin (); c != cut_points.end (); ++c) {
if (*c == p) {
return;
}
}

cut_points.push_back (p);

}
Expand Down Expand Up @@ -1057,6 +1064,12 @@ EdgeProcessor::reserve (size_t n)
mp_work_edges->reserve (n);
}

size_t
EdgeProcessor::count () const
{
return mp_work_edges->size ();
}

void
EdgeProcessor::insert (const db::Edge &e, EdgeProcessor::property_type p)
{
Expand Down
5 changes: 5 additions & 0 deletions src/db/db/dbEdgeProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,11 @@ class DB_PUBLIC EdgeProcessor
*/
void reserve (size_t n);

/**
* @brief Reports the number of edges stored in the processor
*/
size_t count () const;

/**
* @brief Insert an edge
*/
Expand Down
20 changes: 20 additions & 0 deletions src/db/db/dbEdges.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
#include "dbFlatEdges.h"
#include "dbEdgesUtils.h"
#include "dbRegion.h"
#include "dbLayout.h"
#include "dbWriter.h"
#include "tlStream.h"

namespace db
{
Expand Down Expand Up @@ -141,6 +144,23 @@ Edges::set_delegate (EdgesDelegate *delegate, bool keep_attributes)
}
}

void
Edges::write (const std::string &fn) const
{
// method provided for debugging purposes

db::Layout layout;
const db::Cell &top = layout.cell (layout.add_cell ("EDGES"));
unsigned int li = layout.insert_layer (db::LayerProperties (0, 0));
insert_into (&layout, top.cell_index (), li);

tl::OutputStream os (fn);
db::SaveLayoutOptions opt;
opt.set_format_from_filename (fn);
db::Writer writer (opt);
writer.write (layout, os);
}

void
Edges::clear ()
{
Expand Down
8 changes: 8 additions & 0 deletions src/db/db/dbEdges.h
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,14 @@ class DB_PUBLIC Edges
return *this;
}

/**
* @brief Writes the edge collection to a file
*
* This method is provided for debugging purposes. A flat image of the
* region is written to a layout file with a single top cell on layer 0/0.
*/
void write (const std::string &fn) const;

/**
* @brief Intersections with other edges
* Intersections are similar to "AND", but will also report
Expand Down
10 changes: 8 additions & 2 deletions src/db/db/dbFlatRegion.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ FlatRegion::FlatRegion (const FlatRegion &other)
: MutableRegion (other), mp_polygons (other.mp_polygons), mp_merged_polygons (other.mp_merged_polygons), mp_properties_repository (other.mp_properties_repository)
{
init ();

m_is_merged = other.m_is_merged;
m_merged_polygons_valid = other.m_merged_polygons_valid;
}
Expand All @@ -52,15 +51,22 @@ FlatRegion::FlatRegion (const db::Shapes &polygons, bool is_merged)
: MutableRegion (), mp_polygons (new db::Shapes (polygons)), mp_merged_polygons (new db::Shapes (false)), mp_properties_repository (new db::PropertiesRepository ())
{
init ();
m_is_merged = is_merged;
}

FlatRegion::FlatRegion (const db::Shapes &polygons, const db::ICplxTrans &trans, bool merged_semantics, bool is_merged)
: MutableRegion (), mp_polygons (new db::Shapes (polygons)), mp_merged_polygons (new db::Shapes (false)), mp_properties_repository (new db::PropertiesRepository ())
{
init ();
m_is_merged = is_merged;
transform_generic (trans);
set_merged_semantics (merged_semantics);
}

FlatRegion::FlatRegion (bool is_merged)
: MutableRegion (), mp_polygons (new db::Shapes (false)), mp_merged_polygons (new db::Shapes (false)), mp_properties_repository (new db::PropertiesRepository ())
{
init ();

m_is_merged = is_merged;
}

Expand Down
3 changes: 2 additions & 1 deletion src/db/db/dbFlatRegion.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class DB_PUBLIC FlatRegion
typedef polygon_layer_wp_type::iterator polygon_iterator_wp_type;

FlatRegion ();
FlatRegion (const db::Shapes &polygons, bool is_merged);
FlatRegion (const db::Shapes &polygons, bool is_merged = false);
FlatRegion (const db::Shapes &polygons, const db::ICplxTrans &trans, bool merged_semantics, bool is_merged = false);
FlatRegion (bool is_merged);

FlatRegion (const FlatRegion &other);
Expand Down
Loading

0 comments on commit 14a16f8

Please sign in to comment.