1
1
#ifndef VPR_COMPRESSED_GRID_H
2
2
#define VPR_COMPRESSED_GRID_H
3
3
4
+ #include < algorithm>
4
5
#include " physical_types.h"
5
6
6
7
#include " vtr_geometry.h"
@@ -60,18 +61,22 @@ struct t_compressed_block_grid {
60
61
*
61
62
* This function takes a physical tile location in the grid and converts it to the corresponding
62
63
* 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.
63
65
*
64
66
* @param grid_loc The physical tile location in the grid.
65
67
* @return The corresponding compressed location with the same layer number.
66
68
*/
67
69
inline t_physical_tile_loc grid_loc_to_compressed_loc_approx_round_up (t_physical_tile_loc grid_loc) const {
68
70
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
+ }
75
80
};
76
81
77
82
int layer_num = grid_loc.layer_num ;
@@ -86,17 +91,22 @@ struct t_compressed_block_grid {
86
91
*
87
92
* This function takes a physical tile location in the grid and converts it to the corresponding
88
93
* 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.
89
95
*
90
96
* @param grid_loc The physical tile location in the grid.
91
97
* @return The corresponding compressed location with the same layer number.
92
98
*/
93
99
inline t_physical_tile_loc grid_loc_to_compressed_loc_approx_round_down (t_physical_tile_loc grid_loc) const {
94
100
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 );
98
109
}
99
- return (int )std::distance (compressed.begin (), itr);
100
110
};
101
111
102
112
int layer_num = grid_loc.layer_num ;
@@ -120,23 +130,21 @@ struct t_compressed_block_grid {
120
130
121
131
// Find the first element not less than loc
122
132
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
+
130
134
if (itr == compressed_grid_dim.end ()) {
135
+ // If all the compressed locations are less than the grid location, return the last compressed location
131
136
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));
132
147
}
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);
140
148
};
141
149
142
150
const int layer_num = grid_loc.layer_num ;
0 commit comments