blob: 5a80630a76b8e0b7237b22ec66912cd8a0e9e27f [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/NESobel3x3Kernel.h"
25
26#include "arm_compute/core/Coordinates.h"
27#include "arm_compute/core/Error.h"
28#include "arm_compute/core/Helpers.h"
29#include "arm_compute/core/ITensor.h"
30#include "arm_compute/core/Types.h"
31#include "arm_compute/core/Validate.h"
32
33#include <arm_neon.h>
34#include <cstdint>
35
36using namespace arm_compute;
37
38NESobel3x3Kernel::NESobel3x3Kernel()
39 : _run_sobel_x(false), _run_sobel_y(false), _input(nullptr), _output_x(nullptr), _output_y(nullptr)
40{
41}
42
43BorderSize NESobel3x3Kernel::border_size() const
44{
45 return BorderSize(1);
46}
47
48void NESobel3x3Kernel::configure(const ITensor *input, ITensor *output_x, ITensor *output_y, bool border_undefined)
49{
50 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
51 ARM_COMPUTE_ERROR_ON((output_x == nullptr) && (output_y == nullptr));
52
53 _run_sobel_x = output_x != nullptr;
54 _run_sobel_y = output_y != nullptr;
55
56 if(_run_sobel_x)
57 {
58 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output_x, 1, DataType::S16);
59 }
60
61 if(_run_sobel_y)
62 {
63 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output_y, 1, DataType::S16);
64 }
65
66 _input = input;
67 _output_x = output_x;
68 _output_y = output_y;
69
70 // Configure kernel window
71 constexpr unsigned int num_elems_processed_per_iteration = 8;
72 constexpr unsigned int num_elems_read_per_iteration = 16;
73 constexpr unsigned int num_elems_written_per_iteration = 8;
74 constexpr unsigned int num_rows_read_per_iteration = 3;
75
76 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
77 AccessWindowHorizontal output_x_access(output_x == nullptr ? nullptr : output_x->info(), 0, num_elems_written_per_iteration);
78 AccessWindowHorizontal output_y_access(output_y == nullptr ? nullptr : output_y->info(), 0, num_elems_written_per_iteration);
79
80 update_window_and_padding(win,
81 AccessWindowRectangle(input->info(), -border_size().left, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration),
82 output_x_access,
83 output_y_access);
84
85 output_x_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
86 output_y_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
87
88 INEKernel::configure(win);
89}
90
Moritz Pflanzerc186b572017-09-07 09:48:04 +010091void NESobel3x3Kernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010092{
Moritz Pflanzerc186b572017-09-07 09:48:04 +010093 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
95 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
96
97 const unsigned char *const input_top_ptr = _input->ptr_to_element(Coordinates(-1, -1));
98 const unsigned char *const input_mid_ptr = _input->ptr_to_element(Coordinates(-1, 0));
99 const unsigned char *const input_bot_ptr = _input->ptr_to_element(Coordinates(-1, 1));
100
101 Iterator input(_input, window);
102 Iterator output_y;
103 Iterator output_x;
104
105 if(_run_sobel_y)
106 {
107 output_y = Iterator(_output_y, window);
108 }
109
110 if(_run_sobel_x)
111 {
112 output_x = Iterator(_output_x, window);
113 }
114
115 static const int16x8_t two = vdupq_n_s16(2);
116 static const int16x8_t minustwo = vdupq_n_s16(-2);
117
118 if(_run_sobel_y && _run_sobel_x)
119 {
120 execute_window_loop(window, [&](const Coordinates & id)
121 {
122 const uint8x16_t top_data = vld1q_u8(input_top_ptr + input.offset());
123 const uint8x16_t mid_data = vld1q_u8(input_mid_ptr + input.offset());
124 const uint8x16_t bot_data = vld1q_u8(input_bot_ptr + input.offset());
125
126 const int16x8x2_t top_s16 =
127 {
128 {
129 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(top_data))),
130 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(top_data)))
131 }
132 };
133 const int16x8x2_t mid_s16 =
134 {
135 {
136 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mid_data))),
137 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mid_data)))
138 }
139 };
140 const int16x8x2_t bot_s16 =
141 {
142 {
143 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(bot_data))),
144 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(bot_data)))
145 }
146 };
147
148 //SOBEL Y
149 //top left
150 int16x8_t out_y = vnegq_s16(top_s16.val[0]);
151 //top mid
152 out_y = vmlaq_s16(out_y, vextq_s16(top_s16.val[0], top_s16.val[1], 1), minustwo);
153 //top right
154 out_y = vsubq_s16(out_y, vextq_s16(top_s16.val[0], top_s16.val[1], 2));
155 //bot left
156 out_y = vaddq_s16(out_y, bot_s16.val[0]);
157 //bot mid
158 out_y = vmlaq_s16(out_y, vextq_s16(bot_s16.val[0], bot_s16.val[1], 1), two);
159 //bot right
160 out_y = vaddq_s16(out_y, vextq_s16(bot_s16.val[0], bot_s16.val[1], 2));
161
162 vst1q_s16(reinterpret_cast<int16_t *>(output_y.ptr()), out_y);
163
164 //SOBEL X
165 //top left
166 int16x8_t out_x = vnegq_s16(top_s16.val[0]);
167 //top right
168 out_x = vaddq_s16(out_x, vextq_s16(top_s16.val[0], top_s16.val[1], 2));
169 //mid left
170 out_x = vmlaq_s16(out_x, mid_s16.val[0], minustwo);
171 //mid right
172 out_x = vmlaq_s16(out_x, vextq_s16(mid_s16.val[0], mid_s16.val[1], 2), two);
173 //bot left
174 out_x = vsubq_s16(out_x, bot_s16.val[0]);
175 //bot right
176 out_x = vaddq_s16(out_x, vextq_s16(bot_s16.val[0], bot_s16.val[1], 2));
177
178 vst1q_s16(reinterpret_cast<int16_t *>(output_x.ptr()), out_x);
179 },
180 input, output_x, output_y);
181 }
182 else if(_run_sobel_x)
183 {
184 execute_window_loop(window, [&](const Coordinates & id)
185 {
186 const uint8x16_t top_data = vld1q_u8(input_top_ptr + input.offset());
187 const uint8x16_t mid_data = vld1q_u8(input_mid_ptr + input.offset());
188 const uint8x16_t bot_data = vld1q_u8(input_bot_ptr + input.offset());
189
190 const int16x8x2_t top_s16 =
191 {
192 {
193 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(top_data))),
194 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(top_data)))
195 }
196 };
197 const int16x8x2_t mid_s16 =
198 {
199 {
200 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mid_data))),
201 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mid_data)))
202 }
203 };
204 const int16x8x2_t bot_s16 =
205 {
206 {
207 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(bot_data))),
208 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(bot_data)))
209 }
210 };
211
212 //SOBEL X
213 //top left
214 int16x8_t out = vnegq_s16(top_s16.val[0]);
215 //top right
216 out = vaddq_s16(out, vextq_s16(top_s16.val[0], top_s16.val[1], 2));
217 //mid left
218 out = vmlaq_s16(out, mid_s16.val[0], minustwo);
219 //mid right
220 out = vmlaq_s16(out, vextq_s16(mid_s16.val[0], mid_s16.val[1], 2), two);
221 //bot left
222 out = vsubq_s16(out, bot_s16.val[0]);
223 //bot right
224 out = vaddq_s16(out, vextq_s16(bot_s16.val[0], bot_s16.val[1], 2));
225
226 vst1q_s16(reinterpret_cast<int16_t *>(output_x.ptr()), out);
227 },
228 input, output_x);
229 }
230 else if(_run_sobel_y)
231 {
232 execute_window_loop(window, [&](const Coordinates & id)
233 {
234 const uint8x16_t top_data = vld1q_u8(input_top_ptr + input.offset());
235 const uint8x16_t bot_data = vld1q_u8(input_bot_ptr + input.offset());
236
237 const int16x8x2_t top_s16 =
238 {
239 {
240 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(top_data))),
241 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(top_data)))
242 }
243 };
244 const int16x8x2_t bot_s16 =
245 {
246 {
247 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(bot_data))),
248 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(bot_data)))
249 }
250 };
251
252 //SOBEL Y
253 //top left
254 int16x8_t out = vnegq_s16(top_s16.val[0]);
255 //top mid
256 out = vmlaq_s16(out, vextq_s16(top_s16.val[0], top_s16.val[1], 1), minustwo);
257 //top right
258 out = vsubq_s16(out, vextq_s16(top_s16.val[0], top_s16.val[1], 2));
259 //bot left
260 out = vaddq_s16(out, bot_s16.val[0]);
261 //bot mid
262 out = vmlaq_s16(out, vextq_s16(bot_s16.val[0], bot_s16.val[1], 1), two);
263 //bot right
264 out = vaddq_s16(out, vextq_s16(bot_s16.val[0], bot_s16.val[1], 2));
265
266 vst1q_s16(reinterpret_cast<int16_t *>(output_y.ptr()), out);
267 },
268 input, output_y);
269 }
270}