blob: cfed32477395b3a44c2ad4b6a0efaef04b20c504 [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/NEDerivativeKernel.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/Types.h"
31#include "arm_compute/core/Validate.h"
32
33#include <arm_neon.h>
34#include <cstddef>
35#include <cstdint>
36
37using namespace arm_compute;
38
39namespace arm_compute
40{
41class Coordinates;
42} // namespace arm_compute
43
44NEDerivativeKernel::NEDerivativeKernel()
45 : _func(nullptr), _input(nullptr), _output_x(nullptr), _output_y(nullptr)
46{
47}
48
49BorderSize NEDerivativeKernel::border_size() const
50{
51 return BorderSize(1);
52}
53
54void NEDerivativeKernel::configure(const ITensor *input, ITensor *output_x, ITensor *output_y, bool border_undefined)
55{
56 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
57 ARM_COMPUTE_ERROR_ON((output_x == nullptr) && (output_y == nullptr));
58
59 const bool run_der_x = output_x != nullptr;
60 const bool run_der_y = output_y != nullptr;
61
62 if(run_der_x)
63 {
64 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output_x, 1, DataType::S16);
65 }
66
67 if(run_der_y)
68 {
69 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output_y, 1, DataType::S16);
70 }
71
72 _input = input;
73 _output_x = output_x;
74 _output_y = output_y;
75
76 constexpr unsigned int num_elems_processed_per_iteration = 16;
77 constexpr unsigned int num_rows_read_per_iteration = 3;
78
79 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
80
81 AccessWindowHorizontal out_x_access(output_x == nullptr ? nullptr : output_x->info(), 0, num_elems_processed_per_iteration);
82 AccessWindowHorizontal out_y_access(output_y == nullptr ? nullptr : output_y->info(), 0, num_elems_processed_per_iteration);
John Richardson384a43e2017-12-14 15:41:13 +000083
Michele Di Giorgio29258e82018-08-13 16:49:30 +010084 // TODO(COMPMID-1503) Fix x-access input bug in NEON kernel instead of '+2'
John Richardson384a43e2017-12-14 15:41:13 +000085 AccessWindowHorizontal in_x_access(input->info(), -border_size().left, num_elems_processed_per_iteration + 2);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010086 AccessWindowRectangle in_y_access(input->info(), 0, -border_size().left, num_elems_processed_per_iteration, num_rows_read_per_iteration);
John Richardson384a43e2017-12-14 15:41:13 +000087
Michele Di Giorgio29258e82018-08-13 16:49:30 +010088 // TODO(COMPMID-1503) Fix x-access input bug in NEON kernel instead of '+2'
John Richardson384a43e2017-12-14 15:41:13 +000089 AccessWindowRectangle in_xy_access(input->info(), -border_size().left, -border_size().top, num_elems_processed_per_iteration + 2, num_rows_read_per_iteration);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090
91 if(run_der_x && run_der_y)
92 {
93 _func = &NEDerivativeKernel::derivative_xy;
94 update_window_and_padding(win, in_xy_access, out_x_access, out_y_access);
95 out_y_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
96 out_x_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
97 }
98 else
99 {
100 if(run_der_x)
101 {
102 _func = &NEDerivativeKernel::derivative_x;
103 update_window_and_padding(win, in_x_access, out_x_access);
104 out_x_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
105 }
106 else if(run_der_y)
107 {
108 _func = &NEDerivativeKernel::derivative_y;
109 update_window_and_padding(win, in_y_access, out_y_access);
110 out_y_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
111 }
112 else
113 {
114 ARM_COMPUTE_ERROR("At least one output must be NOT NULL");
115 }
116 }
117
118 INEKernel::configure(win);
119}
120
121void NEDerivativeKernel::derivative_x(const Window &window)
122{
123 Iterator in(_input, window);
124 Iterator out_x(_output_x, window);
125
126 /* Apply 1-D centered point discrete derivative mask ([-1 0 1]) along the X direction */
127 execute_window_loop(window, [&](const Coordinates & id)
128 {
129 /* Load left and right data */
130 const uint8x16_t l_data = vld1q_u8(in.ptr() - 1);
131 const uint8x16_t r_data = vld1q_u8(in.ptr() + 1);
132
133 /* Cast to int16 and perform the subtraction between the right and left data */
134 const int16x8_t out0 = vsubq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(r_data))),
135 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(l_data))));
136
137 /* Cast to int16 and perform the subtraction between the right and left data */
138 const int16x8_t out1 = vsubq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(r_data))),
139 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(l_data))));
140
141 /* Store result of derivative along the X direction */
142 vst1q_s16(reinterpret_cast<int16_t *>(out_x.ptr()), out0);
143 vst1q_s16(reinterpret_cast<int16_t *>(out_x.ptr()) + 8, out1);
144 },
145 in, out_x);
146}
147
148void NEDerivativeKernel::derivative_y(const Window &window)
149{
150 Iterator in(_input, window);
151 Iterator out_y(_output_y, window);
152
153 const size_t stride = _input->info()->strides_in_bytes()[1];
154
155 /* Apply 1-D centered point discrete derivative mask ([-1 0 1]^T) along the Y direction */
156 execute_window_loop(window, [&](const Coordinates & id)
157 {
158 /* Load top and bottom data */
159 const uint8x16_t t_data = vld1q_u8(in.ptr() - stride);
160 const uint8x16_t b_data = vld1q_u8(in.ptr() + stride);
161
162 /* Cast to int16 and perform the subtraction between the bottom and top data */
163 const int16x8_t out0 = vsubq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(b_data))),
164 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(t_data))));
165
166 /* Cast to int16 and perform the subtraction between the bottom and top data */
167 const int16x8_t out1 = vsubq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(b_data))),
168 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(t_data))));
169
170 /* Store result of derivative along the Y direction */
171 vst1q_s16(reinterpret_cast<int16_t *>(out_y.ptr()), out0);
172 vst1q_s16(reinterpret_cast<int16_t *>(out_y.ptr()) + 8, out1);
173 },
174 in, out_y);
175}
176
177void NEDerivativeKernel::derivative_xy(const Window &window)
178{
179 Iterator in(_input, window);
180 Iterator out_x(_output_x, window);
181 Iterator out_y(_output_y, window);
182
183 const size_t stride = _input->info()->strides_in_bytes()[1];
184
185 /* Apply 1-D centered point discrete derivative masks ([-1 0 1] and [-1 0 1]^T) along the X and Y directions */
186 execute_window_loop(window, [&](const Coordinates & id)
187 {
188 /* Load top, bottom, left and right data */
189 const uint8x16_t t_data = vld1q_u8(in.ptr() - stride);
190 const uint8x16_t b_data = vld1q_u8(in.ptr() + stride);
191 const uint8x16_t l_data = vld1q_u8(in.ptr() - 1);
192 const uint8x16_t r_data = vld1q_u8(in.ptr() + 1);
193
194 /* Cast to int16 and perform the subtraction between the bottom and top data */
195 const int16x8_t out0 = vsubq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(b_data))),
196 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(t_data))));
197
198 /* Cast to int16 and perform the subtraction between the bottom and top data */
199 const int16x8_t out1 = vsubq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(b_data))),
200 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(t_data))));
201
202 /* Cast to int16 and perform the subtraction between the right and left data */
203 const int16x8_t out2 = vsubq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(r_data))),
204 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(l_data))));
205
206 /* Cast to int16 and perform the subtraction between the right and left data */
207 const int16x8_t out3 = vsubq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(r_data))),
208 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(l_data))));
209
210 /* Store result of derivative along the Y direction */
211 vst1q_s16(reinterpret_cast<int16_t *>(out_y.ptr()), out0);
212 vst1q_s16(reinterpret_cast<int16_t *>(out_y.ptr()) + 8, out1);
213
214 /* Store result of derivative along the X direction */
215 vst1q_s16(reinterpret_cast<int16_t *>(out_x.ptr()), out2);
216 vst1q_s16(reinterpret_cast<int16_t *>(out_x.ptr()) + 8, out3);
217 },
218 in, out_x, out_y);
219}
220
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100221void NEDerivativeKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100222{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100223 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100224 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
225 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
226 ARM_COMPUTE_ERROR_ON(_func == nullptr);
227
228 (this->*_func)(window);
229}