blob: ff903e9802ade2f23c40ef0a62c000d784ee509d [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
53 anchor[0] + border_size.left + ceil_to_multiple(shape[0] - border_size.left - border_size.right, steps[0]),
54 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
65 anchor[1] + border_size.top + ceil_to_multiple(shape[1] - border_size.top - border_size.bottom, steps[1]),
66 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
109 for(; n < Coordinates::num_max_dimensions; ++n)
110 {
111 window.set(n, Window::Dimension(0, std::max<size_t>(1, tensor_shape[n])));
112 }
113
114 return window;
115}
116
117Window arm_compute::calculate_max_window_horizontal(const ITensorInfo &info, const Steps &steps, bool skip_border, BorderSize border_size)
118{
119 if(skip_border)
120 {
121 border_size.top = 0;
122 border_size.bottom = 0;
123 }
124 else
125 {
126 border_size.left = 0;
127 border_size.right = 0;
128 }
129
130 const Coordinates &anchor = info.valid_region().anchor;
131 const TensorShape &shape = info.valid_region().shape;
132
133 Window window;
134
135 window.set(0, Window::Dimension(
136 // Skip the border left of the image
137 anchor[0] + border_size.left,
138 // Skip the border right of the image
139 // Make sure the window width is a multiple of the step size
140 anchor[0] + border_size.left + ceil_to_multiple(shape[0] - border_size.left - border_size.right, steps[0]),
141 steps[0]));
142
143 size_t n = 1;
144 const TensorShape &tensor_shape = info.tensor_shape();
145
146 if(tensor_shape.num_dimensions() > 1)
147 {
148 window.set(1, Window::Dimension(
149 // Skip the border above the image
150 anchor[1] - border_size.top,
151 // Skip the border below the image
152 anchor[1] + shape[1] + border_size.bottom,
153 1));
154
155 ++n;
156 }
157
158 for(; n < Coordinates::num_max_dimensions; ++n)
159 {
160 window.set(n, Window::Dimension(0, std::max<size_t>(1, tensor_shape[n])));
161 }
162
163 return window;
164}