blob: 957abf5aafb1b667af2febec9e21110ad8c9d2d8 [file] [log] [blame]
Moritz Pflanzer5b512292017-06-21 15:54:07 +01001/*
2 * Copyright (c) 2017 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
Moritz Pflanzer2509fba2017-06-23 14:15:03 +010029#include <cmath>
30
Moritz Pflanzer5b512292017-06-21 15:54:07 +010031namespace arm_compute
32{
33namespace test
34{
35/** Calculate required padding. */
36class PaddingCalculator final
37{
38public:
Moritz Pflanzer2509fba2017-06-23 14:15:03 +010039 /** Options for computing the padding */
40 enum class Option
41 {
42 EXCLUDE_BORDER,
43 INCLUDE_BORDER
44 };
45
Moritz Pflanzer5b512292017-06-21 15:54:07 +010046 /** 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 */
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
Moritz Pflanzer2509fba2017-06-23 14:15:03 +010074 /** 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
Moritz Pflanzer5b512292017-06-21 15:54:07 +010080 /** 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 *
Moritz Pflanzer2509fba2017-06-23 14:15:03 +010088 * 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 *
Moritz Pflanzer5b512292017-06-21 15:54:07 +010092 * @return Required padding in number of elements.
93 */
Moritz Pflanzer2509fba2017-06-23 14:15:03 +010094 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;
Moritz Pflanzer5b512292017-06-21 15:54:07 +0100107
108private:
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
117inline void PaddingCalculator::set_border_mode(BorderMode mode)
118{
119 _mode = mode;
120}
121
122inline void PaddingCalculator::set_border_size(int size)
123{
124 _border_size = size;
125}
126
127inline void PaddingCalculator::set_access_offset(int offset)
128{
129 _offset = offset;
130}
131
Moritz Pflanzer2509fba2017-06-23 14:15:03 +0100132inline void PaddingCalculator::set_processed_elements(int elements)
133{
134 _num_processed_elements = elements;
135}
Moritz Pflanzer5b512292017-06-21 15:54:07 +0100136inline void PaddingCalculator::set_accessed_elements(int elements)
137{
138 _num_accessed_elements = elements;
139}
140
Moritz Pflanzer2509fba2017-06-23 14:15:03 +0100141inline PaddingSize PaddingCalculator::required_padding() const
Moritz Pflanzer5b512292017-06-21 15:54:07 +0100142{
Moritz Pflanzer2509fba2017-06-23 14:15:03 +0100143 return required_padding(_offset < 0 ? Option::INCLUDE_BORDER : Option::EXCLUDE_BORDER);
144}
145
146inline 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
Moritz Pflanzer5b512292017-06-21 15:54:07 +0100152 if(_mode == BorderMode::UNDEFINED)
153 {
Moritz Pflanzer2509fba2017-06-23 14:15:03 +0100154 padding_right = (((_size - 2 * _border_size + _num_processed_elements - 1) / _num_processed_elements) - 1) * _num_processed_elements + _num_accessed_elements - _size + _border_size + _offset;
Moritz Pflanzer5b512292017-06-21 15:54:07 +0100155 }
156 else
157 {
Moritz Pflanzer2509fba2017-06-23 14:15:03 +0100158 padding_right = (((_size + _num_processed_elements - 1) / _num_processed_elements) - 1) * _num_processed_elements + _num_accessed_elements - _size + _offset;
Moritz Pflanzer5b512292017-06-21 15:54:07 +0100159 }
Moritz Pflanzer2509fba2017-06-23 14:15:03 +0100160
161 padding.right = std::max(0, padding_right);
162
163 return padding;
Moritz Pflanzer5b512292017-06-21 15:54:07 +0100164}
165} // namespace test
166} // namespace arm_compute
Anthony Barbierac69aa12017-07-03 17:39:37 +0100167#endif /* __ARM_COMPUTE_TEST_PADDING_CALCULATOR_H__ */