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
7
+ #include " vtr_assert.h"
6
8
#include " vtr_geometry.h"
7
9
#include " vtr_flat_map.h"
8
10
@@ -60,18 +62,22 @@ struct t_compressed_block_grid {
60
62
*
61
63
* This function takes a physical tile location in the grid and converts it to the corresponding
62
64
* compressed location. The conversion approximates by rounding up to the nearest valid compressed location.
65
+ * If all the compressed locations are less than the grid location, the function will return the last compressed location.
63
66
*
64
67
* @param grid_loc The physical tile location in the grid.
65
68
* @return The corresponding compressed location with the same layer number.
66
69
*/
67
70
inline t_physical_tile_loc grid_loc_to_compressed_loc_approx_round_up (t_physical_tile_loc grid_loc) const {
68
71
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);
72
+ // Get the first element that is not less than the value
73
+ auto itr = std::lower_bound (compressed.begin (), compressed.end (), value);
74
+ if (itr == compressed.end ()) {
75
+ // If all the compressed locations are less than the grid location, return the last compressed location
76
+ return compressed.size () - 1 ;
77
+ } else {
78
+ // Return the index of the first element that is not less than the value
79
+ return std::distance (compressed.begin (), itr);
80
+ }
75
81
};
76
82
77
83
int layer_num = grid_loc.layer_num ;
@@ -86,17 +92,22 @@ struct t_compressed_block_grid {
86
92
*
87
93
* This function takes a physical tile location in the grid and converts it to the corresponding
88
94
* compressed location. The conversion approximates by rounding down to the nearest valid compressed location.
95
+ * If all the compressed locations are bigger than the grid location, the function will return the first compressed location.
89
96
*
90
97
* @param grid_loc The physical tile location in the grid.
91
98
* @return The corresponding compressed location with the same layer number.
92
99
*/
93
100
inline t_physical_tile_loc grid_loc_to_compressed_loc_approx_round_down (t_physical_tile_loc grid_loc) const {
94
101
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 );
102
+ // Get the first element that is strictly bigger than the value
103
+ auto itr = std::upper_bound (compressed.begin (), compressed.end (), value);
104
+ if (itr == compressed.begin ()) {
105
+ // If all the compressed locations are bigger than the grid location, return the first compressed location
106
+ return 0 ;
107
+ } else {
108
+ // Return the index of the first element that is less than or equal to the value
109
+ return std::distance (compressed.begin (), itr - 1 );
98
110
}
99
- return (int )std::distance (compressed.begin (), itr);
100
111
};
101
112
102
113
int layer_num = grid_loc.layer_num ;
@@ -111,31 +122,30 @@ struct t_compressed_block_grid {
111
122
*
112
123
* Useful when the point is of a different block type from coords.
113
124
*
114
- * @param point represents a coordinate in one dimension of the point
115
- * @param coords represents vector of coordinate values of a single type only
116
- *
117
- * Hence, the exact point coordinate will not be found in coords if they are of different block types. In this case the function will return
118
- * the nearest compressed location to point by rounding it down
125
+ * @param grid_loc non-compressed physical tile location in the grid
126
+ * @return Nearest x and y compressed locations in the grid (in the same layer)
119
127
*/
120
128
inline t_physical_tile_loc grid_loc_to_compressed_loc_approx (t_physical_tile_loc grid_loc) const {
121
129
auto find_closest_compressed_point = [](int loc, const std::vector<int >& compressed_grid_dim) -> int {
130
+ VTR_ASSERT_DEBUG (compressed_grid_dim.size () > 0 );
131
+
132
+ // Find the first element not less than loc
122
133
auto itr = std::lower_bound (compressed_grid_dim.begin (), compressed_grid_dim.end (), loc);
123
- int cx;
124
- if (itr < compressed_grid_dim.end () - 1 ) {
125
- int dist_prev = abs (loc - *itr);
126
- int dist_next = abs (loc - *(itr+1 ));
127
- if (dist_prev < dist_next) {
128
- cx = std::distance (compressed_grid_dim.begin (), itr);
129
- } else {
130
- cx = std::distance (compressed_grid_dim.begin (), itr + 1 );
131
- }
134
+
135
+ if (itr == compressed_grid_dim.begin ()) {
136
+ // If all the compressed locations are bigger that or equal to loc, return the first compressed location
137
+ return 0 ;
132
138
} else if (itr == compressed_grid_dim.end ()) {
133
- cx = std::distance (compressed_grid_dim.begin (), itr - 1 );
139
+ // If all the compressed locations are less than loc, return the last compressed location
140
+ return compressed_grid_dim.size () - 1 ;
134
141
} else {
135
- cx = std::distance (compressed_grid_dim.begin (), itr);
142
+ // Find the nearest compressed location.
143
+ int dist_prev = loc - *(itr - 1 );
144
+ int dist_next = *itr - loc;
145
+ VTR_ASSERT_DEBUG (dist_prev >= 0 && dist_next >= 0 );
146
+ return (dist_prev <= dist_next) ? (std::distance (compressed_grid_dim.begin (), itr - 1 )) :
147
+ (std::distance (compressed_grid_dim.begin (), itr));
136
148
}
137
-
138
- return cx;
139
149
};
140
150
141
151
const int layer_num = grid_loc.layer_num ;
0 commit comments