1 /*
2 * Copyright (c) 2017-2019 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 ARM_COMPUTE_TEST_PADDING_CALCULATOR_H
25 #define ARM_COMPUTE_TEST_PADDING_CALCULATOR_H
26
27 #include "arm_compute/core/Types.h"
28
29 #include <cmath>
30
31 namespace arm_compute
32 {
33 namespace test
34 {
35 /** Calculate required padding. */
36 class PaddingCalculator final
37 {
38 public:
39 /** Options for computing the padding */
40 enum class Option
41 {
42 EXCLUDE_BORDER,
43 INCLUDE_BORDER
44 };
45
46 /** Construct calculator with size of tensor's dimension and step size.
47 *
48 * @param[in] size Number of elements available.
49 * @param[in] processed_elements Number of elements processed per iteration.
50 */
PaddingCalculator(int size,int processed_elements)51 PaddingCalculator(int size, int processed_elements)
52 : _size{ size }, _num_processed_elements{ processed_elements }, _num_accessed_elements{ processed_elements }
53 {
54 }
55
56 /** Set border mode.
57 *
58 * @param[in] mode Border mode.
59 */
60 void set_border_mode(BorderMode mode);
61
62 /** Set border size.
63 *
64 * @param[in] size Border size in elements.
65 */
66 void set_border_size(int size);
67
68 /** Set offset of the access relative to the current position.
69 *
70 * @param[in] offset Offset of the access.
71 */
72 void set_access_offset(int offset);
73
74 /** Set number of processed elements.
75 *
76 * @param[in] elements Number of processed elements per iteration.
77 */
78 void set_processed_elements(int elements);
79
80 /** Set number of accessed elements.
81 *
82 * @param[in] elements Number of accessed elements per iteration.
83 */
84 void set_accessed_elements(int elements);
85
86 /** Compute the required padding.
87 *
88 * If access offset is negative and border mode is not undefined, the top,
89 * bottom and left padding is set to boder size. Otherwise it is zero.
90 * The right padding is always computed based on the specified parameters.
91 *
92 * @return Required padding in number of elements.
93 */
94 PaddingSize required_padding() const;
95
96 /** Compute the required padding.
97 *
98 * If @p option is INCLUDE_BORDER and border mode is not undefined, the top,
99 * bottom and left padding is set to boder size. Otherwise it is zero.
100 * The right padding is always computed based on the specified parameters.
101 *
102 * @param[in] option Padding option
103 *
104 * @return Required padding in number of elements.
105 */
106 PaddingSize required_padding(Option option) const;
107
108 private:
109 int _size;
110 int _num_processed_elements;
111 int _num_accessed_elements;
112 BorderMode _mode{ BorderMode::UNDEFINED };
113 int _border_size{ 0 };
114 int _offset{ 0 };
115 };
116
set_border_mode(BorderMode mode)117 inline void PaddingCalculator::set_border_mode(BorderMode mode)
118 {
119 _mode = mode;
120 }
121
set_border_size(int size)122 inline void PaddingCalculator::set_border_size(int size)
123 {
124 _border_size = size;
125 }
126
set_access_offset(int offset)127 inline void PaddingCalculator::set_access_offset(int offset)
128 {
129 _offset = offset;
130 }
131
set_processed_elements(int elements)132 inline void PaddingCalculator::set_processed_elements(int elements)
133 {
134 _num_processed_elements = elements;
135 }
set_accessed_elements(int elements)136 inline void PaddingCalculator::set_accessed_elements(int elements)
137 {
138 _num_accessed_elements = elements;
139 }
140
required_padding()141 inline PaddingSize PaddingCalculator::required_padding() const
142 {
143 return required_padding(_offset < 0 ? Option::INCLUDE_BORDER : Option::EXCLUDE_BORDER);
144 }
145
required_padding(Option option)146 inline PaddingSize PaddingCalculator::required_padding(Option option) const
147 {
148 PaddingSize padding{ (_mode == BorderMode::UNDEFINED || option == Option::EXCLUDE_BORDER) ? 0U : static_cast<unsigned int>(std::max(0, _border_size)) };
149
150 int padding_right = 0;
151
152 if(_mode == BorderMode::UNDEFINED)
153 {
154 padding_right = (((_size - 2 * _border_size + _num_processed_elements - 1) / _num_processed_elements) - 1) * _num_processed_elements + _num_accessed_elements - _size + _border_size + _offset;
155 }
156 else
157 {
158 padding_right = (((_size + _num_processed_elements - 1) / _num_processed_elements) - 1) * _num_processed_elements + _num_accessed_elements - _size + _offset;
159 }
160
161 padding.right = std::max(0, padding_right);
162
163 return padding;
164 }
165 } // namespace test
166 } // namespace arm_compute
167 #endif /* ARM_COMPUTE_TEST_PADDING_CALCULATOR_H */
168