blob: 151d7de9a429017edaa34700733d8a2e2ea08a79 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2016, 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#include "arm_compute/core/Helpers.h"
25
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/IKernel.h"
28#include "arm_compute/core/ITensorInfo.h"
29#include "arm_compute/core/Utils.h"
30
31#include <algorithm>
32#include <cstdint>
33
34using namespace arm_compute;
35
36Window arm_compute::calculate_max_window(const ITensorInfo &info, const Steps &steps, bool skip_border, BorderSize border_size)
37{
38 if(!skip_border)
39 {
40 border_size = BorderSize(0);
41 }
42
43 const Coordinates &anchor = info.valid_region().anchor;
44 const TensorShape &shape = info.valid_region().shape;
45
46 Window window;
47
48 window.set(0, Window::Dimension(
49 // Skip the border left of the image
50 anchor[0] + border_size.left,
51 // Skip the border right of the image
52 // Make sure the window width is a multiple of the step size
Moritz Pflanzera1848362017-08-25 12:30:03 +010053 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]),
Anthony Barbier6ff3b192017-09-04 18:44:23 +010054 steps[0]));
55
56 size_t n = 1;
57 const TensorShape &tensor_shape = info.tensor_shape();
58
59 if(tensor_shape.num_dimensions() > 1)
60 {
61 window.set(1, Window::Dimension(
62 // Skip the border above the image
63 anchor[1] + border_size.top,
64 // Skip the border below the image
Moritz Pflanzera1848362017-08-25 12:30:03 +010065 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]),
Anthony Barbier6ff3b192017-09-04 18:44:23 +010066 steps[1]));
67
68 ++n;
69 }
70
71 for(; n < Coordinates::num_max_dimensions; ++n)
72 {
73 window.set(n, Window::Dimension(0, std::max<size_t>(1, tensor_shape[n])));
74 }
75
76 return window;
77}
78
79Window arm_compute::calculate_max_enlarged_window(const ITensorInfo &info, const Steps &steps, BorderSize border_size)
80{
81 const Coordinates &anchor = info.valid_region().anchor;
82 const TensorShape &shape = info.valid_region().shape;
83
84 Window window;
85
86 window.set(0, Window::Dimension(
87 // move the anchor to the start from the border
88 anchor[0] - border_size.left,
89 // move the anchor to include the right end border
90 // Make sure the window width is a multiple of the step size
91 anchor[0] - border_size.left + ceil_to_multiple(shape[0] + border_size.left + border_size.right, steps[0]),
92 steps[0]));
93
94 size_t n = 1;
95 const TensorShape &tensor_shape = info.tensor_shape();
96
97 if(tensor_shape.num_dimensions() > 1)
98 {
99 window.set(1, Window::Dimension(
100 // Include the border above the image
101 anchor[1] - border_size.top,
102 // Include the border below the image
103 anchor[1] - border_size.top + ceil_to_multiple(shape[1] + border_size.top + border_size.bottom, steps[1]),
104 steps[1]));
105
106 ++n;
107 }
108
Anthony Barbier7068f992017-10-26 15:23:08 +0100109 if(tensor_shape.num_dimensions() > 2)
110 {
111 window.set(2, Window::Dimension(0, std::max<size_t>(1, tensor_shape[n]), steps[2]));
112
113 ++n;
114 }
115
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100116 for(; n < Coordinates::num_max_dimensions; ++n)
117 {
118 window.set(n, Window::Dimension(0, std::max<size_t>(1, tensor_shape[n])));
119 }
120
121 return window;
122}
123
124Window arm_compute::calculate_max_window_horizontal(const ITensorInfo &info, const Steps &steps, bool skip_border, BorderSize border_size)
125{
126 if(skip_border)
127 {
128 border_size.top = 0;
129 border_size.bottom = 0;
130 }
131 else
132 {
133 border_size.left = 0;
134 border_size.right = 0;
135 }
136
137 const Coordinates &anchor = info.valid_region().anchor;
138 const TensorShape &shape = info.valid_region().shape;
139
140 Window window;
141
142 window.set(0, Window::Dimension(
143 // Skip the border left of the image
144 anchor[0] + border_size.left,
145 // Skip the border right of the image
146 // Make sure the window width is a multiple of the step size
Moritz Pflanzera1848362017-08-25 12:30:03 +0100147 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]),
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100148 steps[0]));
149
150 size_t n = 1;
151 const TensorShape &tensor_shape = info.tensor_shape();
152
153 if(tensor_shape.num_dimensions() > 1)
154 {
155 window.set(1, Window::Dimension(
156 // Skip the border above the image
157 anchor[1] - border_size.top,
158 // Skip the border below the image
159 anchor[1] + shape[1] + border_size.bottom,
160 1));
161
162 ++n;
163 }
164
165 for(; n < Coordinates::num_max_dimensions; ++n)
166 {
167 window.set(n, Window::Dimension(0, std::max<size_t>(1, tensor_shape[n])));
168 }
169
170 return window;
171}