aboutsummaryrefslogtreecommitdiff
path: root/tests/PaddingCalculator.h
blob: 029312b73b9266de3ac07949d2c57c3f26a91f0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * Copyright (c) 2017 ARM Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#ifndef __ARM_COMPUTE_TEST_PADDING_CALCULATOR_H__
#define __ARM_COMPUTE_TEST_PADDING_CALCULATOR_H__

#include "arm_compute/core/Types.h"

namespace arm_compute
{
namespace test
{
/** Calculate required padding. */
class PaddingCalculator final
{
public:
    /** Construct calculator with size of tensor's dimension and step size.
     *
     * @param[in] size               Number of elements available.
     * @param[in] processed_elements Number of elements processed per iteration.
     */
    PaddingCalculator(int size, int processed_elements)
        : _size{ size }, _num_processed_elements{ processed_elements }, _num_accessed_elements{ processed_elements }
    {
    }

    /** Set border mode.
     *
     * @param[in] mode Border mode.
     */
    void set_border_mode(BorderMode mode);

    /** Set border size.
     *
     * @param[in] size Border size in elements.
     */
    void set_border_size(int size);

    /** Set offset of the access relative to the current position.
     *
     * @param[in] offset Offset of the access.
     */
    void set_access_offset(int offset);

    /** Set number of accessed elements.
     *
     * @param[in] elements Number of accessed elements per iteration.
     */
    void set_accessed_elements(int elements);

    /** Compute the required padding.
     *
     * @return Required padding in number of elements.
     */
    int required_padding() const;

private:
    int        _size;
    int        _num_processed_elements;
    int        _num_accessed_elements;
    BorderMode _mode{ BorderMode::UNDEFINED };
    int        _border_size{ 0 };
    int        _offset{ 0 };
};

inline void PaddingCalculator::set_border_mode(BorderMode mode)
{
    _mode = mode;
}

inline void PaddingCalculator::set_border_size(int size)
{
    _border_size = size;
}

inline void PaddingCalculator::set_access_offset(int offset)
{
    _offset = offset;
}

inline void PaddingCalculator::set_accessed_elements(int elements)
{
    _num_accessed_elements = elements;
}

inline int PaddingCalculator::required_padding() const
{
    if(_mode == BorderMode::UNDEFINED)
    {
        return (((_size - _border_size + _num_processed_elements - 1) / _num_processed_elements) - 1) * _num_processed_elements + _num_accessed_elements - _size + _border_size + _offset;
    }
    else
    {
        return (((_size + _num_processed_elements - 1) / _num_processed_elements) - 1) * _num_processed_elements + _num_accessed_elements - _size + _offset;
    }
}
} // namespace test
} // namespace arm_compute
#endif