Moritz Pflanzer | 5b51229 | 2017-06-21 15:54:07 +0100 | [diff] [blame^] | 1 | /* |
| 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 | |
| 29 | namespace arm_compute |
| 30 | { |
| 31 | namespace test |
| 32 | { |
| 33 | /** Calculate required padding. */ |
| 34 | class PaddingCalculator final |
| 35 | { |
| 36 | public: |
| 37 | /** Construct calculator with size of tensor's dimension and step size. |
| 38 | * |
| 39 | * @param[in] size Number of elements available. |
| 40 | * @param[in] processed_elements Number of elements processed per iteration. |
| 41 | */ |
| 42 | PaddingCalculator(int size, int processed_elements) |
| 43 | : _size{ size }, _num_processed_elements{ processed_elements }, _num_accessed_elements{ processed_elements } |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | /** Set border mode. |
| 48 | * |
| 49 | * @param[in] mode Border mode. |
| 50 | */ |
| 51 | void set_border_mode(BorderMode mode); |
| 52 | |
| 53 | /** Set border size. |
| 54 | * |
| 55 | * @param[in] size Border size in elements. |
| 56 | */ |
| 57 | void set_border_size(int size); |
| 58 | |
| 59 | /** Set offset of the access relative to the current position. |
| 60 | * |
| 61 | * @param[in] offset Offset of the access. |
| 62 | */ |
| 63 | void set_access_offset(int offset); |
| 64 | |
| 65 | /** Set number of accessed elements. |
| 66 | * |
| 67 | * @param[in] elements Number of accessed elements per iteration. |
| 68 | */ |
| 69 | void set_accessed_elements(int elements); |
| 70 | |
| 71 | /** Compute the required padding. |
| 72 | * |
| 73 | * @return Required padding in number of elements. |
| 74 | */ |
| 75 | int required_padding() const; |
| 76 | |
| 77 | private: |
| 78 | int _size; |
| 79 | int _num_processed_elements; |
| 80 | int _num_accessed_elements; |
| 81 | BorderMode _mode{ BorderMode::UNDEFINED }; |
| 82 | int _border_size{ 0 }; |
| 83 | int _offset{ 0 }; |
| 84 | }; |
| 85 | |
| 86 | inline void PaddingCalculator::set_border_mode(BorderMode mode) |
| 87 | { |
| 88 | _mode = mode; |
| 89 | } |
| 90 | |
| 91 | inline void PaddingCalculator::set_border_size(int size) |
| 92 | { |
| 93 | _border_size = size; |
| 94 | } |
| 95 | |
| 96 | inline void PaddingCalculator::set_access_offset(int offset) |
| 97 | { |
| 98 | _offset = offset; |
| 99 | } |
| 100 | |
| 101 | inline void PaddingCalculator::set_accessed_elements(int elements) |
| 102 | { |
| 103 | _num_accessed_elements = elements; |
| 104 | } |
| 105 | |
| 106 | inline int PaddingCalculator::required_padding() const |
| 107 | { |
| 108 | if(_mode == BorderMode::UNDEFINED) |
| 109 | { |
| 110 | return (((_size - _border_size + _num_processed_elements - 1) / _num_processed_elements) - 1) * _num_processed_elements + _num_accessed_elements - _size + _border_size + _offset; |
| 111 | } |
| 112 | else |
| 113 | { |
| 114 | return (((_size + _num_processed_elements - 1) / _num_processed_elements) - 1) * _num_processed_elements + _num_accessed_elements - _size + _offset; |
| 115 | } |
| 116 | } |
| 117 | } // namespace test |
| 118 | } // namespace arm_compute |
| 119 | #endif |