2 * Copyright (c) 2017 ARM Limited.
4 * SPDX-License-Identifier: MIT
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:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
24 #ifndef __ARM_COMPUTE_TEST_PADDING_CALCULATOR_H__
25 #define __ARM_COMPUTE_TEST_PADDING_CALCULATOR_H__
27 #include "arm_compute/core/Types.h"
35 /** Calculate required padding. */
36 class PaddingCalculator final
39 /** Options for computing the padding */
46 /** Construct calculator with size of tensor's dimension and step size.
48 * @param[in] size Number of elements available.
49 * @param[in] processed_elements Number of elements processed per iteration.
51 PaddingCalculator(int size, int processed_elements)
52 : _size{ size }, _num_processed_elements{ processed_elements }, _num_accessed_elements{ processed_elements }
58 * @param[in] mode Border mode.
60 void set_border_mode(BorderMode mode);
64 * @param[in] size Border size in elements.
66 void set_border_size(int size);
68 /** Set offset of the access relative to the current position.
70 * @param[in] offset Offset of the access.
72 void set_access_offset(int offset);
74 /** Set number of processed elements.
76 * @param[in] elements Number of processed elements per iteration.
78 void set_processed_elements(int elements);
80 /** Set number of accessed elements.
82 * @param[in] elements Number of accessed elements per iteration.
84 void set_accessed_elements(int elements);
86 /** Compute the required padding.
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.
92 * @return Required padding in number of elements.
94 PaddingSize required_padding() const;
96 /** Compute the required padding.
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.
102 * @param[in] option Padding option
104 * @return Required padding in number of elements.
106 PaddingSize required_padding(Option option) const;
110 int _num_processed_elements;
111 int _num_accessed_elements;
112 BorderMode _mode{ BorderMode::UNDEFINED };
113 int _border_size{ 0 };
117 inline void PaddingCalculator::set_border_mode(BorderMode mode)
122 inline void PaddingCalculator::set_border_size(int size)
127 inline void PaddingCalculator::set_access_offset(int offset)
132 inline void PaddingCalculator::set_processed_elements(int elements)
134 _num_processed_elements = elements;
136 inline void PaddingCalculator::set_accessed_elements(int elements)
138 _num_accessed_elements = elements;
141 inline PaddingSize PaddingCalculator::required_padding() const
143 return required_padding(_offset < 0 ? Option::INCLUDE_BORDER : Option::EXCLUDE_BORDER);
146 inline PaddingSize PaddingCalculator::required_padding(Option option) const
148 PaddingSize padding{ (_mode == BorderMode::UNDEFINED || option == Option::EXCLUDE_BORDER) ? 0U : static_cast<unsigned int>(std::max(0, _border_size)) };
150 int padding_right = 0;
152 if(_mode == BorderMode::UNDEFINED)
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;
158 padding_right = (((_size + _num_processed_elements - 1) / _num_processed_elements) - 1) * _num_processed_elements + _num_accessed_elements - _size + _offset;
161 padding.right = std::max(0, padding_right);
166 } // namespace arm_compute
167 #endif /* __ARM_COMPUTE_TEST_PADDING_CALCULATOR_H__ */