blob: 75ffb71b4b06783628b7d95d6d814192a2b4eb54 [file] [log] [blame]
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +01001/*
SiCongLic7b1e842021-02-22 14:28:33 +00002* Copyright (c) 2020-2021 Arm Limited.
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +01003 *
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
26namespace arm_compute
27{
28Window 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
SiCongLic7b1e842021-02-22 14:28:33 +000082Window calculate_max_window(const TensorShape &shape, const Steps &steps, bool skip_border, BorderSize border_size)
83{
84 if(!skip_border)
85 {
86 border_size = BorderSize(0);
87 }
88
89 Window window;
90
91 window.set(0, Window::Dimension(
92 // Skip the border left of the image
93 border_size.left,
94 // Skip the border right of the image
95 // Make sure the window width is a multiple of the step size
96 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]),
97 steps[0]));
98
99 size_t n = 1;
100
101 if(shape.num_dimensions() > 1)
102 {
103 window.set(1, Window::Dimension(
104 // Skip the border above the image
105 border_size.top,
106 // Skip the border below the image
107 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]),
108 steps[1]));
109
110 ++n;
111 }
112
113 if(shape.num_dimensions() > 2)
114 {
115 window.set(2, Window::Dimension(0, std::max<size_t>(1, shape[2]), steps[2]));
116
117 ++n;
118 }
119
120 for(; n < shape.num_dimensions(); ++n)
121 {
122 window.set(n, Window::Dimension(0, std::max<size_t>(1, shape[n])));
123 }
124
125 for(; n < Coordinates::num_max_dimensions; ++n)
126 {
127 window.set(n, Window::Dimension(0, 1));
128 }
129
130 return window;
131}
132
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +0100133Window calculate_max_enlarged_window(const ValidRegion &valid_region, const Steps &steps, BorderSize border_size)
134{
135 const Coordinates &anchor = valid_region.anchor;
136 const TensorShape &shape = valid_region.shape;
137
138 Window window;
139
140 window.set(0, Window::Dimension(
141 // move the anchor to the start from the border
142 anchor[0] - border_size.left,
143 // move the anchor to include the right end border
144 // Make sure the window width is a multiple of the step size
145 anchor[0] - border_size.left + ceil_to_multiple(shape[0] + border_size.left + border_size.right, steps[0]),
146 steps[0]));
147
148 size_t n = 1;
149
150 if(anchor.num_dimensions() > 1)
151 {
152 window.set(1, Window::Dimension(
153 // Include the border above the image
154 anchor[1] - border_size.top,
155 // Include the border below the image
156 anchor[1] - border_size.top + ceil_to_multiple(shape[1] + border_size.top + border_size.bottom, steps[1]),
157 steps[1]));
158
159 ++n;
160 }
161
162 if(anchor.num_dimensions() > 2)
163 {
164 window.set(2, Window::Dimension(0, std::max<size_t>(1, shape[n]), steps[2]));
165
166 ++n;
167 }
168
169 for(; n < anchor.num_dimensions(); ++n)
170 {
171 window.set(n, Window::Dimension(anchor[n], std::max<size_t>(1, shape[n])));
172 }
173
174 for(; n < Coordinates::num_max_dimensions; ++n)
175 {
176 window.set(n, Window::Dimension(0, 1));
177 }
178
179 return window;
180}
181
182Window calculate_max_window_horizontal(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
183{
184 if(skip_border)
185 {
186 border_size.top = 0;
187 border_size.bottom = 0;
188 }
189 else
190 {
191 border_size.left = 0;
192 border_size.right = 0;
193 }
194
195 const Coordinates &anchor = valid_region.anchor;
196 const TensorShape &shape = valid_region.shape;
197
198 Window window;
199
200 window.set(0, Window::Dimension(
201 // Skip the border left of the image
202 anchor[0] + border_size.left,
203 // Skip the border right of the image
204 // Make sure the window width is a multiple of the step size
205 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]),
206 steps[0]));
207
208 size_t n = 1;
209
210 if(anchor.num_dimensions() > 1)
211 {
212 window.set(1, Window::Dimension(
213 // Skip the border above the image
214 anchor[1] - border_size.top,
215 // Skip the border below the image
216 anchor[1] + shape[1] + border_size.bottom,
217 1));
218
219 ++n;
220 }
221
222 for(; n < anchor.num_dimensions(); ++n)
223 {
224 window.set(n, Window::Dimension(anchor[n], std::max<size_t>(1, shape[n])));
225 }
226
227 for(; n < Coordinates::num_max_dimensions; ++n)
228 {
229 window.set(n, Window::Dimension(0, 1));
230 }
231
232 return window;
233}
234} // namespace arm_compute