blob: 3c3b2b93f97815f7eca9353f940cd4320740f855 [file] [log] [blame]
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +01001/*
2* Copyright (c) 2020 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
29namespace 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>
41inline 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 */
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}
95} // namespace arm_compute
96
97#endif /* SRC_CORE_HELPERS_UTILS_H */