Convert a linear index into n-dimensional coordinates. * * @param[in] shape Shape of the n-dimensional tensor. * @param[in] index Linear index specifying the i-th element. * * @return n-dimensional coordinates. */
| 361 | * @return n-dimensional coordinates. |
| 362 | */ |
| 363 | inline Coordinates index2coord(const TensorShape &shape, int index) |
| 364 | { |
| 365 | int num_elements = shape.total_size(); |
| 366 | |
| 367 | ARM_COMPUTE_ERROR_ON_MSG(index < 0 || index >= num_elements, "Index has to be in [0, num_elements]"); |
| 368 | ARM_COMPUTE_ERROR_ON_MSG(num_elements == 0, "Cannot create coordinate from empty shape"); |
| 369 | |
| 370 | Coordinates coord{0}; |
| 371 | |
| 372 | for (int d = shape.num_dimensions() - 1; d >= 0; --d) |
| 373 | { |
| 374 | num_elements /= shape[d]; |
| 375 | coord.set(d, index / num_elements); |
| 376 | index %= num_elements; |
| 377 | } |
| 378 | |
| 379 | return coord; |
| 380 | } |
| 381 | |
| 382 | /** Linearise the given coordinate. |
| 383 | * |
no test coverage detected