Skip to content

Commit 5cf1b67

Browse files
committed
vpr/src/place/compressed_grid.h
1 parent 0a80a54 commit 5cf1b67

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

vpr/src/place/compressed_grid.h

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef VPR_COMPRESSED_GRID_H
22
#define VPR_COMPRESSED_GRID_H
33

4+
#include <algorithm>
45
#include "physical_types.h"
56

67
#include "vtr_geometry.h"
@@ -60,18 +61,22 @@ struct t_compressed_block_grid {
6061
*
6162
* This function takes a physical tile location in the grid and converts it to the corresponding
6263
* compressed location. The conversion approximates by rounding up to the nearest valid compressed location.
64+
* If all the compressed locations are less than the grid location, the function will return the last compressed location.
6365
*
6466
* @param grid_loc The physical tile location in the grid.
6567
* @return The corresponding compressed location with the same layer number.
6668
*/
6769
inline t_physical_tile_loc grid_loc_to_compressed_loc_approx_round_up(t_physical_tile_loc grid_loc) const {
6870
auto find_compressed_index = [](const std::vector<int>& compressed, int value) -> int {
69-
auto itr = std::upper_bound(compressed.begin(), compressed.end(), value);
70-
if (itr == compressed.begin())
71-
return 0;
72-
if (itr == compressed.end() || *(itr - 1) == value)
73-
return (int)std::distance(compressed.begin(), itr - 1);
74-
return (int)std::distance(compressed.begin(), itr);
71+
// Get the first element that is not less than the value
72+
auto itr = std::lower_bound(compressed.begin(), compressed.end(), value);
73+
if (itr == compressed.end()) {
74+
// If all the compressed locations are less than the grid location, return the last compressed location
75+
return compressed.size() - 1;
76+
} else {
77+
// Return the index of the first element that is not less than the value
78+
return std::distance(compressed.begin(), itr);
79+
}
7580
};
7681

7782
int layer_num = grid_loc.layer_num;
@@ -86,17 +91,22 @@ struct t_compressed_block_grid {
8691
*
8792
* This function takes a physical tile location in the grid and converts it to the corresponding
8893
* compressed location. The conversion approximates by rounding down to the nearest valid compressed location.
94+
* If all the compressed locations are bigger than the grid location, the function will return the first compressed location.
8995
*
9096
* @param grid_loc The physical tile location in the grid.
9197
* @return The corresponding compressed location with the same layer number.
9298
*/
9399
inline t_physical_tile_loc grid_loc_to_compressed_loc_approx_round_down(t_physical_tile_loc grid_loc) const {
94100
auto find_compressed_index = [](const std::vector<int>& compressed, int value) -> int {
95-
auto itr = std::lower_bound(compressed.begin(), compressed.end(), value);
96-
if (itr == compressed.end()) {
97-
return (int)std::distance(compressed.begin(), itr - 1);
101+
// Get the first element that is strictly bigger than the value
102+
auto itr = std::upper_bound(compressed.begin(), compressed.end(), value);
103+
if (itr == compressed.begin()) {
104+
// If all the compressed locations are less than the grid location, return the first compressed location
105+
return 0;
106+
} else {
107+
// Return the index of the first element that is less than or equal to the value
108+
return std::distance(compressed.begin(), itr - 1);
98109
}
99-
return (int)std::distance(compressed.begin(), itr);
100110
};
101111

102112
int layer_num = grid_loc.layer_num;
@@ -120,23 +130,21 @@ struct t_compressed_block_grid {
120130

121131
// Find the first element not less than loc
122132
auto itr = std::lower_bound(compressed_grid_dim.begin(), compressed_grid_dim.end(), loc);
123-
124-
// If we found exact match or at beginning, return current position
125-
if (itr == compressed_grid_dim.begin() || (itr != compressed_grid_dim.end() && *itr == loc)) {
126-
return std::distance(compressed_grid_dim.begin(), itr);
127-
}
128-
129-
// If we're past the end, return last element
133+
130134
if (itr == compressed_grid_dim.end()) {
135+
// If all the compressed locations are less than the grid location, return the last compressed location
131136
return compressed_grid_dim.size() - 1;
137+
} else if (*itr == loc) {
138+
// If we found exact match, return the index of the element
139+
return std::distance(compressed_grid_dim.begin(), itr);
140+
} else {
141+
// If we didn't find exact match, return the index of the closest compressed location
142+
int dist_prev = loc - *(itr - 1);
143+
int dist_next = *itr - loc;
144+
VTR_ASSERT_DEBUG(dist_prev > 0 && dist_next > 0);
145+
return (dist_prev <= dist_next) ? (std::distance(compressed_grid_dim.begin(), itr - 1)) :
146+
(std::distance(compressed_grid_dim.begin(), itr));
132147
}
133-
134-
// Compare distances to previous and current elements
135-
int dist_prev = loc - *(itr - 1);
136-
int dist_next = *itr - loc;
137-
138-
return std::distance(compressed_grid_dim.begin(),
139-
(dist_prev <= dist_next) ? (itr - 1) : itr);
140148
};
141149

142150
const int layer_num = grid_loc.layer_num;

0 commit comments

Comments
 (0)