Skip to content

Commit 5b20e71

Browse files
avoid duplicate code in grid_loc_to_compressed_loc_approx_round_down() and grid_loc_to_compressed_loc_approx_round_up()
1 parent 788c9fd commit 5b20e71

File tree

1 file changed

+39
-36
lines changed

1 file changed

+39
-36
lines changed

vpr/src/place/compressed_grid.h

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -55,50 +55,53 @@ struct t_compressed_block_grid {
5555
return {cx, cy, layer_num};
5656
}
5757

58+
/**
59+
* @brief Converts a grid location to its corresponding compressed location, rounding up where necessary.
60+
*
61+
* This function takes a physical tile location in the grid and converts it to the corresponding
62+
* compressed location. The conversion approximates by rounding up to the nearest valid compressed location.
63+
*
64+
* @param grid_loc The physical tile location in the grid.
65+
* @return The corresponding compressed location with the same layer number.
66+
*/
5867
inline t_physical_tile_loc grid_loc_to_compressed_loc_approx_round_up(t_physical_tile_loc grid_loc) const {
59-
int cx = OPEN;
60-
int cy = OPEN;
61-
int layer_num = grid_loc.layer_num;
68+
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);
75+
};
6276

63-
auto itr_x = std::upper_bound(compressed_to_grid_x[layer_num].begin(), compressed_to_grid_x[layer_num].end(), grid_loc.x);
64-
if (itr_x == compressed_to_grid_x[layer_num].begin())
65-
cx = 0;
66-
else if (*(itr_x - 1) == grid_loc.x)
67-
cx = std::distance(compressed_to_grid_x[layer_num].begin(), itr_x - 1);
68-
else if (itr_x == compressed_to_grid_x[layer_num].end())
69-
cx = compressed_to_grid_x[layer_num].size();
70-
else
71-
cx = std::distance(compressed_to_grid_x[layer_num].begin(), itr_x);
72-
73-
auto itr_y = std::upper_bound(compressed_to_grid_y[layer_num].begin(), compressed_to_grid_y[layer_num].end(), grid_loc.y);
74-
if (itr_y == compressed_to_grid_y[layer_num].begin())
75-
cy = 0;
76-
else if (*(itr_y - 1) == grid_loc.y)
77-
cy = std::distance(compressed_to_grid_y[layer_num].begin(), itr_y - 1);
78-
else if (itr_y == compressed_to_grid_y[layer_num].end())
79-
cy = compressed_to_grid_y[layer_num].size();
80-
else
81-
cy = std::distance(compressed_to_grid_y[layer_num].begin(), itr_y);
77+
int layer_num = grid_loc.layer_num;
78+
int cx = find_compressed_index(compressed_to_grid_x[layer_num], grid_loc.x);
79+
int cy = find_compressed_index(compressed_to_grid_y[layer_num], grid_loc.y);
8280

8381
return {cx, cy, layer_num};
8482
}
8583

84+
/**
85+
* @brief Converts a grid location to its corresponding compressed location, rounding down where necessary.
86+
*
87+
* This function takes a physical tile location in the grid and converts it to the corresponding
88+
* compressed location. The conversion approximates by rounding down to the nearest valid compressed location.
89+
*
90+
* @param grid_loc The physical tile location in the grid.
91+
* @return The corresponding compressed location with the same layer number.
92+
*/
8693
inline t_physical_tile_loc grid_loc_to_compressed_loc_approx_round_down(t_physical_tile_loc grid_loc) const {
87-
int cx = OPEN;
88-
int cy = OPEN;
89-
int layer_num = grid_loc.layer_num;
94+
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);
98+
}
99+
return (int)std::distance(compressed.begin(), itr);
100+
};
90101

91-
auto itr_x = std::lower_bound(compressed_to_grid_x[layer_num].begin(), compressed_to_grid_x[layer_num].end(), grid_loc.x);
92-
if (itr_x == compressed_to_grid_x[layer_num].end())
93-
cx = std::distance(compressed_to_grid_x[layer_num].begin(), itr_x - 1);
94-
else
95-
cx = std::distance(compressed_to_grid_x[layer_num].begin(), itr_x);
96-
97-
auto itr_y = std::lower_bound(compressed_to_grid_y[layer_num].begin(), compressed_to_grid_y[layer_num].end(), grid_loc.y);
98-
if (itr_y == compressed_to_grid_y[layer_num].end())
99-
cy = std::distance(compressed_to_grid_y[layer_num].begin(), itr_y - 1);
100-
else
101-
cy = std::distance(compressed_to_grid_y[layer_num].begin(), itr_y);
102+
int layer_num = grid_loc.layer_num;
103+
int cx = find_compressed_index(compressed_to_grid_x[layer_num], grid_loc.x);
104+
int cy = find_compressed_index(compressed_to_grid_y[layer_num], grid_loc.y);
102105

103106
return {cx, cy, layer_num};
104107
}

0 commit comments

Comments
 (0)