blob: bf7e0972d5ded32b2a49d1c75e552686ab394e6a [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);
83 AccessWindowHorizontal in_x_access(input->info(), -border_size().left, num_elems_processed_per_iteration);
84 AccessWindowRectangle in_y_access(input->info(), 0, -border_size().left, num_elems_processed_per_iteration, num_rows_read_per_iteration);
85 AccessWindowRectangle in_xy_access(input->info(), -border_size().left, -border_size().top, num_elems_processed_per_iteration, num_rows_read_per_iteration);
86
87 if(run_der_x && run_der_y)
88 {
89 _func = &NEDerivativeKernel::derivative_xy;
90 update_window_and_padding(win, in_xy_access, out_x_access, out_y_access);
91 out_y_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
92 out_x_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
93 }
94 else
95 {
96 if(run_der_x)
97 {
98 _func = &NEDerivativeKernel::derivative_x;
99 update_window_and_padding(win, in_x_access, out_x_access);
100 out_x_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
101 }
102 else if(run_der_y)
103 {
104 _func = &NEDerivativeKernel::derivative_y;
105 update_window_and_padding(win, in_y_access, out_y_access);
106 out_y_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
107 }
108 else
109 {
110 ARM_COMPUTE_ERROR("At least one output must be NOT NULL");
111 }
112 }
113
114 INEKernel::configure(win);
115}
116
117void NEDerivativeKernel::derivative_x(const Window &window)
118{
119 Iterator in(_input, window);
120 Iterator out_x(_output_x, window);
121
122 /* Apply 1-D centered point discrete derivative mask ([-1 0 1]) along the X direction */
123 execute_window_loop(window, [&](const Coordinates & id)
124 {
125 /* Load left and right data */
126 const uint8x16_t l_data = vld1q_u8(in.ptr() - 1);
127 const uint8x16_t r_data = vld1q_u8(in.ptr() + 1);
128
129 /* Cast to int16 and perform the subtraction between the right and left data */
130 const int16x8_t out0 = vsubq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(r_data))),
131 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(l_data))));
132
133 /* Cast to int16 and perform the subtraction between the right and left data */
134 const int16x8_t out1 = vsubq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(r_data))),
135 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(l_data))));
136
137 /* Store result of derivative along the X direction */
138 vst1q_s16(reinterpret_cast<int16_t *>(out_x.ptr()), out0);
139 vst1q_s16(reinterpret_cast<int16_t *>(out_x.ptr()) + 8, out1);
140 },
141 in, out_x);
142}
143
144void NEDerivativeKernel::derivative_y(const Window &window)
145{
146 Iterator in(_input, window);
147 Iterator out_y(_output_y, window);
148
149 const size_t stride = _input->info()->strides_in_bytes()[1];
150
151 /* Apply 1-D centered point discrete derivative mask ([-1 0 1]^T) along the Y direction */
152 execute_window_loop(window, [&](const Coordinates & id)
153 {
154 /* Load top and bottom data */
155 const uint8x16_t t_data = vld1q_u8(in.ptr() - stride);
156 const uint8x16_t b_data = vld1q_u8(in.ptr() + stride);
157
158 /* Cast to int16 and perform the subtraction between the bottom and top data */
159 const int16x8_t out0 = vsubq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(b_data))),
160 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(t_data))));
161
162 /* Cast to int16 and perform the subtraction between the bottom and top data */
163 const int16x8_t out1 = vsubq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(b_data))),
164 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(t_data))));
165
166 /* Store result of derivative along the Y direction */
167 vst1q_s16(reinterpret_cast<int16_t *>(out_y.ptr()), out0);
168 vst1q_s16(reinterpret_cast<int16_t *>(out_y.ptr()) + 8, out1);
169 },
170 in, out_y);
171}
172
173void NEDerivativeKernel::derivative_xy(const Window &window)
174{
175 Iterator in(_input, window);
176 Iterator out_x(_output_x, window);
177 Iterator out_y(_output_y, window);
178
179 const size_t stride = _input->info()->strides_in_bytes()[1];
180
181 /* Apply 1-D centered point discrete derivative masks ([-1 0 1] and [-1 0 1]^T) along the X and Y directions */
182 execute_window_loop(window, [&](const Coordinates & id)
183 {
184 /* Load top, bottom, left and right data */
185 const uint8x16_t t_data = vld1q_u8(in.ptr() - stride);
186 const uint8x16_t b_data = vld1q_u8(in.ptr() + stride);
187 const uint8x16_t l_data = vld1q_u8(in.ptr() - 1);
188 const uint8x16_t r_data = vld1q_u8(in.ptr() + 1);
189
190 /* Cast to int16 and perform the subtraction between the bottom and top data */
191 const int16x8_t out0 = vsubq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(b_data))),
192 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(t_data))));
193
194 /* Cast to int16 and perform the subtraction between the bottom and top data */
195 const int16x8_t out1 = vsubq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(b_data))),
196 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(t_data))));
197
198 /* Cast to int16 and perform the subtraction between the right and left data */
199 const int16x8_t out2 = vsubq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(r_data))),
200 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(l_data))));
201
202 /* Cast to int16 and perform the subtraction between the right and left data */
203 const int16x8_t out3 = vsubq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(r_data))),
204 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(l_data))));
205
206 /* Store result of derivative along the Y direction */
207 vst1q_s16(reinterpret_cast<int16_t *>(out_y.ptr()), out0);
208 vst1q_s16(reinterpret_cast<int16_t *>(out_y.ptr()) + 8, out1);
209
210 /* Store result of derivative along the X direction */
211 vst1q_s16(reinterpret_cast<int16_t *>(out_x.ptr()), out2);
212 vst1q_s16(reinterpret_cast<int16_t *>(out_x.ptr()) + 8, out3);
213 },
214 in, out_x, out_y);
215}
216
217void NEDerivativeKernel::run(const Window &window)
218{
219 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
220 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
221 ARM_COMPUTE_ERROR_ON(_func == nullptr);
222
223 (this->*_func)(window);
224}