blob: d1cff6f62d8224ce50a59c9223ba7ed9d4cb15bb [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/NEON/kernels/NEFillInnerBorderKernel.h"
25
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/ITensor.h"
29#include "arm_compute/core/TensorInfo.h"
30#include "arm_compute/core/Validate.h"
31#include "arm_compute/core/Window.h"
32
33#include <algorithm>
34#include <cstddef>
35#include <cstdint>
36
37using namespace arm_compute;
38
39namespace arm_compute
40{
41class Coordinates;
42} // namespace arm_compute
43
44NEFillInnerBorderKernel::NEFillInnerBorderKernel()
Michalis Spyrou490bf2e2017-09-29 11:24:55 +010045 : _tensor(nullptr), _border_size(0), _constant_border_value(static_cast<float>(0.f))
Anthony Barbier6ff3b192017-09-04 18:44:23 +010046{
47}
48
49void NEFillInnerBorderKernel::configure(ITensor *input, BorderSize border_size, const PixelValue &constant_border_value)
50{
51 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S16, DataType::S32, DataType::F32);
52
53 _tensor = input;
54 _border_size = border_size;
55 _constant_border_value = constant_border_value;
56
57 Window win;
58 win.set(Window::DimX, Window::Dimension(0, 1, 1));
59 win.set(Window::DimY, Window::Dimension(0, 1, 1));
SiCong Li86b53332017-08-23 11:02:43 +010060 win.use_tensor_dimensions(_tensor->info()->tensor_shape(), Window::DimZ);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061 INEKernel::configure(win);
62}
63
Moritz Pflanzerc186b572017-09-07 09:48:04 +010064void NEFillInnerBorderKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010065{
Moritz Pflanzerc186b572017-09-07 09:48:04 +010066 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010067 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
68 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
69
70 // If there is no border: early exit
71 if(_border_size.empty())
72 {
73 return;
74 }
75
76 switch(_tensor->info()->data_type())
77 {
78 case DataType::U8:
79 fill_value_single_channel<uint8_t>(window);
80 break;
81 case DataType::S16:
82 fill_value_single_channel<int16_t>(window);
83 break;
84 case DataType::S32:
85 fill_value_single_channel<int32_t>(window);
86 break;
87 case DataType::F32:
88 static_assert(sizeof(float) == 4, "Float must be 32 bit");
89 fill_value_single_channel<float>(window);
90 break;
91 default:
92 ARM_COMPUTE_ERROR("Not handled");
93 break;
94 }
95}
96
97template <typename T>
98void NEFillInnerBorderKernel::fill_value_single_channel(const Window &window)
99{
100 const size_t stride = _tensor->info()->strides_in_bytes()[1];
101 const size_t width = _tensor->info()->dimension(0);
102 const size_t height = _tensor->info()->dimension(1);
103
104 T constant_border_value;
105 _constant_border_value.get(constant_border_value);
106
107 // Left and right border
108 // All X values are set at once
109 Window vertical(window);
110 vertical.set(Window::DimY, Window::Dimension(0, height, 1));
111
112 Iterator vertical_it(_tensor, vertical);
113
114 execute_window_loop(vertical, [&](const Coordinates & id)
115 {
116 std::fill_n(reinterpret_cast<T *>(vertical_it.ptr()), _border_size.left, constant_border_value);
117 std::fill_n(reinterpret_cast<T *>(vertical_it.ptr()) + width - _border_size.right, _border_size.right, constant_border_value);
118 },
119 vertical_it);
120
121 // Top and bottom border
122 // All values are set at once
123 Iterator horizontal_it(_tensor, window);
124
125 execute_window_loop(window, [&](const Coordinates & id)
126 {
127 for(size_t i = 0; i < _border_size.top; ++i)
128 {
129 std::fill_n(reinterpret_cast<T *>(horizontal_it.ptr() + i * stride), width, constant_border_value);
130 }
131
132 for(size_t i = 0; i < _border_size.bottom; ++i)
133 {
134 std::fill_n(reinterpret_cast<T *>(horizontal_it.ptr() + (height - i - 1) * stride), width, constant_border_value);
135 }
136 },
137 horizontal_it);
138}