xref: /aosp_15_r20/external/ComputeLibrary/src/core/helpers/Utils.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2 * Copyright (c) 2020-2021 Arm Limited.
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"
28 
29 namespace 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  */
40 template <typename T, typename... Ts>
compute_strides(const ITensorInfo & info,T stride_x,Ts &&...fixed_strides)41 inline Strides compute_strides(const ITensorInfo &info, T stride_x, Ts &&... fixed_strides)
42 {
43     const TensorShape &shape = info.tensor_shape();
44 
45     // Create strides object
46     Strides strides(stride_x, fixed_strides...);
47 
48     for(size_t i = 1 + sizeof...(Ts); i < info.num_dimensions(); ++i)
49     {
50         strides.set(i, shape[i - 1] * strides[i - 1]);
51     }
52 
53     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  */
62 template <typename... Ts>
compute_strides(const ITensorInfo & info)63 inline 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  */
get_next_power_two(unsigned int x)74 inline 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 }
95 } // namespace arm_compute
96 
97 #endif /* SRC_CORE_HELPERS_UTILS_H */
98