Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2020 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 | #include "src/core/helpers/WindowHelpers.h" |
| 25 | |
| 26 | namespace arm_compute |
| 27 | { |
| 28 | Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size) |
| 29 | { |
| 30 | if(!skip_border) |
| 31 | { |
| 32 | border_size = BorderSize(0); |
| 33 | } |
| 34 | |
| 35 | const Coordinates &anchor = valid_region.anchor; |
| 36 | const TensorShape &shape = valid_region.shape; |
| 37 | |
| 38 | Window window; |
| 39 | |
| 40 | window.set(0, Window::Dimension( |
| 41 | // Skip the border left of the image |
| 42 | anchor[0] + border_size.left, |
| 43 | // Skip the border right of the image |
| 44 | // Make sure the window width is a multiple of the step size |
| 45 | anchor[0] + border_size.left + ceil_to_multiple(std::max(0, static_cast<int>(shape[0]) - static_cast<int>(border_size.left) - static_cast<int>(border_size.right)), steps[0]), |
| 46 | steps[0])); |
| 47 | |
| 48 | size_t n = 1; |
| 49 | |
| 50 | if(anchor.num_dimensions() > 1) |
| 51 | { |
| 52 | window.set(1, Window::Dimension( |
| 53 | // Skip the border above the image |
| 54 | anchor[1] + border_size.top, |
| 55 | // Skip the border below the image |
| 56 | anchor[1] + border_size.top + ceil_to_multiple(std::max(0, static_cast<int>(shape[1]) - static_cast<int>(border_size.top) - static_cast<int>(border_size.bottom)), steps[1]), |
| 57 | steps[1])); |
| 58 | |
| 59 | ++n; |
| 60 | } |
| 61 | |
| 62 | if(anchor.num_dimensions() > 2) |
| 63 | { |
| 64 | window.set(2, Window::Dimension(anchor[2], std::max<size_t>(1, shape[2]), steps[2])); |
| 65 | |
| 66 | ++n; |
| 67 | } |
| 68 | |
| 69 | for(; n < anchor.num_dimensions(); ++n) |
| 70 | { |
| 71 | window.set(n, Window::Dimension(anchor[n], std::max<size_t>(1, shape[n]))); |
| 72 | } |
| 73 | |
| 74 | for(; n < Coordinates::num_max_dimensions; ++n) |
| 75 | { |
| 76 | window.set(n, Window::Dimension(0, 1)); |
| 77 | } |
| 78 | |
| 79 | return window; |
| 80 | } |
| 81 | |
| 82 | Window calculate_max_enlarged_window(const ValidRegion &valid_region, const Steps &steps, BorderSize border_size) |
| 83 | { |
| 84 | const Coordinates &anchor = valid_region.anchor; |
| 85 | const TensorShape &shape = valid_region.shape; |
| 86 | |
| 87 | Window window; |
| 88 | |
| 89 | window.set(0, Window::Dimension( |
| 90 | // move the anchor to the start from the border |
| 91 | anchor[0] - border_size.left, |
| 92 | // move the anchor to include the right end border |
| 93 | // Make sure the window width is a multiple of the step size |
| 94 | anchor[0] - border_size.left + ceil_to_multiple(shape[0] + border_size.left + border_size.right, steps[0]), |
| 95 | steps[0])); |
| 96 | |
| 97 | size_t n = 1; |
| 98 | |
| 99 | if(anchor.num_dimensions() > 1) |
| 100 | { |
| 101 | window.set(1, Window::Dimension( |
| 102 | // Include the border above the image |
| 103 | anchor[1] - border_size.top, |
| 104 | // Include the border below the image |
| 105 | anchor[1] - border_size.top + ceil_to_multiple(shape[1] + border_size.top + border_size.bottom, steps[1]), |
| 106 | steps[1])); |
| 107 | |
| 108 | ++n; |
| 109 | } |
| 110 | |
| 111 | if(anchor.num_dimensions() > 2) |
| 112 | { |
| 113 | window.set(2, Window::Dimension(0, std::max<size_t>(1, shape[n]), steps[2])); |
| 114 | |
| 115 | ++n; |
| 116 | } |
| 117 | |
| 118 | for(; n < anchor.num_dimensions(); ++n) |
| 119 | { |
| 120 | window.set(n, Window::Dimension(anchor[n], std::max<size_t>(1, shape[n]))); |
| 121 | } |
| 122 | |
| 123 | for(; n < Coordinates::num_max_dimensions; ++n) |
| 124 | { |
| 125 | window.set(n, Window::Dimension(0, 1)); |
| 126 | } |
| 127 | |
| 128 | return window; |
| 129 | } |
| 130 | |
| 131 | Window calculate_max_window_horizontal(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size) |
| 132 | { |
| 133 | if(skip_border) |
| 134 | { |
| 135 | border_size.top = 0; |
| 136 | border_size.bottom = 0; |
| 137 | } |
| 138 | else |
| 139 | { |
| 140 | border_size.left = 0; |
| 141 | border_size.right = 0; |
| 142 | } |
| 143 | |
| 144 | const Coordinates &anchor = valid_region.anchor; |
| 145 | const TensorShape &shape = valid_region.shape; |
| 146 | |
| 147 | Window window; |
| 148 | |
| 149 | window.set(0, Window::Dimension( |
| 150 | // Skip the border left of the image |
| 151 | anchor[0] + border_size.left, |
| 152 | // Skip the border right of the image |
| 153 | // Make sure the window width is a multiple of the step size |
| 154 | anchor[0] + border_size.left + ceil_to_multiple(std::max(0, static_cast<int>(shape[0]) - static_cast<int>(border_size.left) - static_cast<int>(border_size.right)), steps[0]), |
| 155 | steps[0])); |
| 156 | |
| 157 | size_t n = 1; |
| 158 | |
| 159 | if(anchor.num_dimensions() > 1) |
| 160 | { |
| 161 | window.set(1, Window::Dimension( |
| 162 | // Skip the border above the image |
| 163 | anchor[1] - border_size.top, |
| 164 | // Skip the border below the image |
| 165 | anchor[1] + shape[1] + border_size.bottom, |
| 166 | 1)); |
| 167 | |
| 168 | ++n; |
| 169 | } |
| 170 | |
| 171 | for(; n < anchor.num_dimensions(); ++n) |
| 172 | { |
| 173 | window.set(n, Window::Dimension(anchor[n], std::max<size_t>(1, shape[n]))); |
| 174 | } |
| 175 | |
| 176 | for(; n < Coordinates::num_max_dimensions; ++n) |
| 177 | { |
| 178 | window.set(n, Window::Dimension(0, 1)); |
| 179 | } |
| 180 | |
| 181 | return window; |
| 182 | } |
| 183 | } // namespace arm_compute |