| 218 | |
| 219 | template <typename T> |
| 220 | void get_tile(const SimpleTensor<T> &in, SimpleTensor<T> &tile, const Coordinates &coord) |
| 221 | { |
| 222 | ARM_COMPUTE_ERROR_ON(tile.shape().num_dimensions() > 2); |
| 223 | |
| 224 | const int w_tile = tile.shape()[0]; |
| 225 | const int h_tile = tile.shape()[1]; |
| 226 | |
| 227 | // Fill the tile with zeros |
| 228 | std::fill(tile.data() + 0, (tile.data() + (w_tile * h_tile)), static_cast<T>(0)); |
| 229 | |
| 230 | // Check if with the dimensions greater than 2 we could have out-of-bound reads |
| 231 | for (size_t d = 2; d < Coordinates::num_max_dimensions; ++d) |
| 232 | { |
| 233 | if (coord[d] < 0 || coord[d] >= static_cast<int>(in.shape()[d])) |
| 234 | { |
| 235 | ARM_COMPUTE_ERROR("coord[d] < 0 || coord[d] >= in.shape()[d] with d >= 2"); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | // Since we could have out-of-bound reads along the X and Y dimensions, |
| 240 | // we start calculating the input address with x = 0 and y = 0 |
| 241 | Coordinates start_coord = coord; |
| 242 | start_coord[0] = 0; |
| 243 | start_coord[1] = 0; |
| 244 | |
| 245 | // Get input and roi pointers |
| 246 | auto in_ptr = static_cast<const T *>(in(start_coord)); |
| 247 | auto roi_ptr = static_cast<T *>(tile.data()); |
| 248 | |
| 249 | const int x_in_start = std::max(0, coord[0]); |
| 250 | const int y_in_start = std::max(0, coord[1]); |
| 251 | const int x_in_end = std::min(static_cast<int>(in.shape()[0]), coord[0] + w_tile); |
| 252 | const int y_in_end = std::min(static_cast<int>(in.shape()[1]), coord[1] + h_tile); |
| 253 | |
| 254 | // Number of elements to copy per row |
| 255 | const int n = x_in_end - x_in_start; |
| 256 | |
| 257 | // Starting coordinates for the ROI |
| 258 | const int x_tile_start = coord[0] > 0 ? 0 : std::abs(coord[0]); |
| 259 | const int y_tile_start = coord[1] > 0 ? 0 : std::abs(coord[1]); |
| 260 | |
| 261 | // Update input pointer |
| 262 | in_ptr += x_in_start; |
| 263 | in_ptr += (y_in_start * in.shape()[0]); |
| 264 | |
| 265 | // Update ROI pointer |
| 266 | roi_ptr += x_tile_start; |
| 267 | roi_ptr += (y_tile_start * tile.shape()[0]); |
| 268 | |
| 269 | for (int y = y_in_start; y < y_in_end; ++y) |
| 270 | { |
| 271 | // Copy per row |
| 272 | std::copy(in_ptr, in_ptr + n, roi_ptr); |
| 273 | |
| 274 | in_ptr += in.shape()[0]; |
| 275 | roi_ptr += tile.shape()[0]; |
| 276 | } |
| 277 | } |