blob: a17a78f7ee5b961fcddcb27ef78d4d3ea259b180 [file] [log] [blame]
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +01001/*
Anitha Rajeb5696d2023-07-14 11:19:34 +01002* Copyright (c) 2020-2021, 2023 Arm Limited.
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +01003 *
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 */
Anitha Rajb566b6e2023-08-23 11:40:06 +010024#ifndef ACL_SRC_CORE_HELPERS_UTILS_H
25#define ACL_SRC_CORE_HELPERS_UTILS_H
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010026
27#include "arm_compute/core/ITensorInfo.h"
Anitha Rajb566b6e2023-08-23 11:40:06 +010028
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010029namespace arm_compute
30{
31/** Create a strides object based on the provided strides and the tensor dimensions.
32 *
33 * @param[in] info Tensor info object providing the shape of the tensor for unspecified strides.
34 * @param[in] stride_x Stride to be used in X dimension (in bytes).
35 * @param[in] fixed_strides Strides to be used in higher dimensions starting at Y (in bytes).
36 *
37 * @return Strides object based on the specified strides. Missing strides are
38 * calculated based on the tensor shape and the strides of lower dimensions.
39 */
40template <typename T, typename... Ts>
Anitha Rajeb5696d2023-07-14 11:19:34 +010041inline Strides compute_strides(const ITensorInfo &info, T stride_x, Ts &&...fixed_strides)
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010042{
43 const TensorShape &shape = info.tensor_shape();
44
45 // Create strides object
46 Strides strides(stride_x, fixed_strides...);
47
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010048 for (size_t i = 1 + sizeof...(Ts); i < info.num_dimensions(); ++i)
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010049 {
50 strides.set(i, shape[i - 1] * strides[i - 1]);
51 }
52
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010053 return strides;
54}
55
56/** Create a strides object based on the tensor dimensions.
57 *
58 * @param[in] info Tensor info object used to compute the strides.
59 *
60 * @return Strides object based on element size and tensor shape.
61 */
62template <typename... Ts>
63inline Strides compute_strides(const ITensorInfo &info)
64{
65 return compute_strides(info, info.element_size());
66}
67
68/** Given an integer value, this function returns the next power of two
69 *
70 * @param[in] x Input value
71 *
72 * @return the next power of two
73 */
74inline unsigned int get_next_power_two(unsigned int x)
75{
76 // Decrement by 1
77 x--;
78
79 // Shift right by 1
80 x |= x >> 1u;
81 // Shift right by 2
82 x |= x >> 2u;
83 // Shift right by 4
84 x |= x >> 4u;
85 // Shift right by 8
86 x |= x >> 8u;
87 // Shift right by 16
88 x |= x >> 16u;
89
90 // Increment by 1
91 x++;
92
93 return x;
94}
Anitha Rajeb5696d2023-07-14 11:19:34 +010095
96/** Check if the tensor has any holes.
97 *
SiCong Lic5ab4df2023-10-17 17:38:57 +010098 * A hole is defined as any gap in the tensor between two consecutive values. This can be a result of extending
99 * the paddings or manipulating the strides of the tensor
100 *
101 * @param[in] info Tensor info object defining the shape of the input tensor.
102 *
103 * @note This function checks for holes in all dimensions.
104 *
105 */
106bool has_holes(const ITensorInfo &info);
107
108/** Check if the tensor has any holes.
109 *
Anitha Rajeb5696d2023-07-14 11:19:34 +0100110 * @param[in] info Tensor info object defining the shape of the input tensor.
111 * @param[in] dimension Highest dimension to check.
112 *
113 * @note This function checks for holes in all the dimensions upto and including the highest dimension.
114 *
115 */
Anitha Rajb566b6e2023-08-23 11:40:06 +0100116bool has_holes(const ITensorInfo &info, size_t dimension);
Anitha Rajeb5696d2023-07-14 11:19:34 +0100117
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +0100118} // namespace arm_compute
119
Anitha Rajb566b6e2023-08-23 11:40:06 +0100120#endif // ACL_SRC_CORE_HELPERS_UTILS_H