Compute Library
 21.02
Utils.h
Go to the documentation of this file.
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 
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>
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>
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  */
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 */
void set(size_t dimension, T value, bool increase_dim_unit=true)
Accessor to set the value of one of the dimensions.
Definition: Dimensions.h:76
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
Shape of a tensor.
Definition: TensorShape.h:39
unsigned int get_next_power_two(unsigned int x)
Given an integer value, this function returns the next power of two.
Definition: Utils.h:74
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
Copyright (c) 2017-2021 Arm Limited.
Strides compute_strides(const ITensorInfo &info, T stride_x, Ts &&... fixed_strides)
Create a strides object based on the provided strides and the tensor dimensions.
Definition: Utils.h:41
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
virtual size_t element_size() const =0
Element size in bytes calculated as data_size() * num_channels()
Strides of an item in bytes.
Definition: Strides.h:37
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)