Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 1 | /* |
Anitha Raj | eb5696d | 2023-07-14 11:19:34 +0100 | [diff] [blame^] | 2 | * Copyright (c) 2020-2021, 2023 Arm Limited. |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #ifndef SRC_CORE_HELPERS_UTILS_H |
| 25 | #define SRC_CORE_HELPERS_UTILS_H |
| 26 | |
| 27 | #include "arm_compute/core/ITensorInfo.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 28 | namespace arm_compute |
| 29 | { |
| 30 | /** Create a strides object based on the provided strides and the tensor dimensions. |
| 31 | * |
| 32 | * @param[in] info Tensor info object providing the shape of the tensor for unspecified strides. |
| 33 | * @param[in] stride_x Stride to be used in X dimension (in bytes). |
| 34 | * @param[in] fixed_strides Strides to be used in higher dimensions starting at Y (in bytes). |
| 35 | * |
| 36 | * @return Strides object based on the specified strides. Missing strides are |
| 37 | * calculated based on the tensor shape and the strides of lower dimensions. |
| 38 | */ |
| 39 | template <typename T, typename... Ts> |
Anitha Raj | eb5696d | 2023-07-14 11:19:34 +0100 | [diff] [blame^] | 40 | inline Strides compute_strides(const ITensorInfo &info, T stride_x, Ts &&...fixed_strides) |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 41 | { |
| 42 | const TensorShape &shape = info.tensor_shape(); |
| 43 | |
| 44 | // Create strides object |
| 45 | Strides strides(stride_x, fixed_strides...); |
| 46 | |
| 47 | for(size_t i = 1 + sizeof...(Ts); i < info.num_dimensions(); ++i) |
| 48 | { |
| 49 | strides.set(i, shape[i - 1] * strides[i - 1]); |
| 50 | } |
| 51 | |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 52 | return strides; |
| 53 | } |
| 54 | |
| 55 | /** Create a strides object based on the tensor dimensions. |
| 56 | * |
| 57 | * @param[in] info Tensor info object used to compute the strides. |
| 58 | * |
| 59 | * @return Strides object based on element size and tensor shape. |
| 60 | */ |
| 61 | template <typename... Ts> |
| 62 | inline Strides compute_strides(const ITensorInfo &info) |
| 63 | { |
| 64 | return compute_strides(info, info.element_size()); |
| 65 | } |
| 66 | |
| 67 | /** Given an integer value, this function returns the next power of two |
| 68 | * |
| 69 | * @param[in] x Input value |
| 70 | * |
| 71 | * @return the next power of two |
| 72 | */ |
| 73 | inline unsigned int get_next_power_two(unsigned int x) |
| 74 | { |
| 75 | // Decrement by 1 |
| 76 | x--; |
| 77 | |
| 78 | // Shift right by 1 |
| 79 | x |= x >> 1u; |
| 80 | // Shift right by 2 |
| 81 | x |= x >> 2u; |
| 82 | // Shift right by 4 |
| 83 | x |= x >> 4u; |
| 84 | // Shift right by 8 |
| 85 | x |= x >> 8u; |
| 86 | // Shift right by 16 |
| 87 | x |= x >> 16u; |
| 88 | |
| 89 | // Increment by 1 |
| 90 | x++; |
| 91 | |
| 92 | return x; |
| 93 | } |
Anitha Raj | eb5696d | 2023-07-14 11:19:34 +0100 | [diff] [blame^] | 94 | |
| 95 | /** Check if the tensor has any holes. |
| 96 | * |
| 97 | * @param[in] info Tensor info object defining the shape of the input tensor. |
| 98 | * @param[in] dimension Highest dimension to check. |
| 99 | * |
| 100 | * @note This function checks for holes in all the dimensions upto and including the highest dimension. |
| 101 | * |
| 102 | */ |
| 103 | inline bool has_holes(const ITensorInfo &info, size_t dimension) |
| 104 | { |
| 105 | const auto &shape = info.tensor_shape(); |
| 106 | const auto &strides = info.strides_in_bytes(); |
| 107 | size_t squashed_bytes = info.element_size(); |
| 108 | |
| 109 | for(size_t dim = 0; dim <= dimension; ++dim) |
| 110 | { |
| 111 | if(strides[dim] != squashed_bytes) |
| 112 | { |
| 113 | return true; |
| 114 | } |
| 115 | squashed_bytes *= shape[dim]; |
| 116 | } |
| 117 | return false; |
| 118 | } |
| 119 | |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 120 | } // namespace arm_compute |
| 121 | |
| 122 | #endif /* SRC_CORE_HELPERS_UTILS_H */ |