blob: 835b333a101c2e945c927d4c420e69fad5e7ca63 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +01002 * Copyright (c) 2016-2020 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 */
Michalis Spyrouebcebf12020-10-21 00:04:14 +010024#include "src/core/NEON/kernels/NESobel7x7Kernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
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/Utils.h"
32#include "arm_compute/core/Validate.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010033#include "src/core/helpers/AutoConfiguration.h"
34#include "src/core/helpers/WindowHelpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035
36#include <arm_neon.h>
37#include <cstdint>
38
39using namespace arm_compute;
40
41namespace arm_compute
42{
43class Coordinates;
44} // namespace arm_compute
45
46namespace
47{
48const int32x4_t minusfour = vdupq_n_s32(-4);
49const int32x4_t minusfive = vdupq_n_s32(-5);
50const int32x4_t four = vdupq_n_s32(4);
51const int32x4_t five = vdupq_n_s32(5);
52const int32x4_t six = vdupq_n_s32(6);
53const int32x4_t fifteen = vdupq_n_s32(15);
54const int32x4_t twenty = vdupq_n_s32(20);
55
56inline int32x4x2_t compute_hor_sobel_x(const int32x4x4_t &data)
57{
58 int32x4x2_t out =
59 {
60 {
61 vnegq_s32(data.val[0]),
62 vnegq_s32(data.val[1])
63 }
64 };
65
66 out.val[0] = vmlaq_s32(out.val[0],
67 vextq_s32(data.val[0], data.val[1], 1), minusfour);
68
69 out.val[0] = vmlaq_s32(out.val[0],
70 vextq_s32(data.val[0], data.val[1], 2), minusfive);
71
72 out.val[0] = vmlaq_s32(out.val[0], data.val[1], five);
73
74 out.val[0] = vmlaq_s32(out.val[0],
75 vextq_s32(data.val[1], data.val[2], 1), four);
76
77 out.val[0] = vaddq_s32(out.val[0],
78 vextq_s32(data.val[1], data.val[2], 2));
79
80 out.val[1] = vmlaq_s32(out.val[1],
81 vextq_s32(data.val[1], data.val[2], 1), minusfour);
82
83 out.val[1] = vmlaq_s32(out.val[1],
84 vextq_s32(data.val[1], data.val[2], 2), minusfive);
85
86 out.val[1] = vmlaq_s32(out.val[1], data.val[2], five);
87
88 out.val[1] = vmlaq_s32(out.val[1],
89 vextq_s32(data.val[2], data.val[3], 1), four);
90
91 out.val[1] = vaddq_s32(out.val[1],
92 vextq_s32(data.val[2], data.val[3], 2));
93
94 return out;
95}
96
97inline int32x4x2_t compute_hor_sobel_y(const int32x4x4_t &data)
98{
99 int32x4x2_t out =
100 {
101 {
102 data.val[0],
103 data.val[1]
104 }
105 };
106
107 out.val[0] = vmlaq_s32(out.val[0],
108 vextq_s32(data.val[0], data.val[1], 1), six);
109
110 out.val[0] = vmlaq_s32(out.val[0],
111 vextq_s32(data.val[0], data.val[1], 2), fifteen);
112
113 out.val[0] = vmlaq_s32(out.val[0],
114 vextq_s32(data.val[0], data.val[1], 3), twenty);
115
116 out.val[0] = vmlaq_s32(out.val[0], data.val[1], fifteen);
117
118 out.val[0] = vmlaq_s32(out.val[0],
119 vextq_s32(data.val[1], data.val[2], 1), six);
120
121 out.val[0] = vaddq_s32(out.val[0],
122 vextq_s32(data.val[1], data.val[2], 2));
123
124 out.val[1] = vmlaq_s32(out.val[1],
125 vextq_s32(data.val[1], data.val[2], 1), six);
126
127 out.val[1] = vmlaq_s32(out.val[1],
128 vextq_s32(data.val[1], data.val[2], 2), fifteen);
129
130 out.val[1] = vmlaq_s32(out.val[1],
131 vextq_s32(data.val[1], data.val[2], 3), twenty);
132
133 out.val[1] = vmlaq_s32(out.val[1], data.val[2], fifteen);
134
135 out.val[1] = vmlaq_s32(out.val[1],
136 vextq_s32(data.val[2], data.val[3], 1), six);
137
138 out.val[1] = vaddq_s32(out.val[1],
139 vextq_s32(data.val[2], data.val[3], 2));
140
141 return out;
142}
143} // namespace
144
145NESobel7x7HorKernel::NESobel7x7HorKernel()
146 : _input(nullptr), _output_x(nullptr), _output_y(nullptr), _run_sobel_x(false), _run_sobel_y(false), _border_size(0)
147{
148}
149
150BorderSize NESobel7x7HorKernel::border_size() const
151{
152 return _border_size;
153}
154
155void NESobel7x7HorKernel::configure(const ITensor *input, ITensor *output_x, ITensor *output_y, bool border_undefined)
156{
157 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(input, Format::U8);
158 ARM_COMPUTE_ERROR_ON((output_x == nullptr) && (output_y == nullptr));
159
160 _run_sobel_x = output_x != nullptr;
161 _run_sobel_y = output_y != nullptr;
162
163 if(_run_sobel_x)
164 {
165 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output_x, Format::S32);
166 }
167
168 if(_run_sobel_y)
169 {
170 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output_y, Format::S32);
171 }
172
173 _input = input;
174 _output_x = output_x;
175 _output_y = output_y;
176 _border_size = BorderSize(border_undefined ? 0 : 3, 3);
177
178 // Configure kernel window
179 constexpr unsigned int num_elems_processed_per_iteration = 8;
180 constexpr unsigned int num_elems_read_per_iteration = 16;
181 constexpr unsigned int num_elems_written_per_iteration = 8;
182
183 Window win = calculate_max_window_horizontal(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
184 AccessWindowHorizontal output_x_access(output_x == nullptr ? nullptr : output_x->info(), 0, num_elems_written_per_iteration);
185 AccessWindowHorizontal output_y_access(output_y == nullptr ? nullptr : output_y->info(), 0, num_elems_written_per_iteration);
186
187 update_window_and_padding(win,
188 AccessWindowHorizontal(input->info(), -border_size().left, num_elems_read_per_iteration),
189 output_x_access,
190 output_y_access);
191
192 output_x_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
193 output_y_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
194
195 INEKernel::configure(win);
196}
197
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100198void NESobel7x7HorKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100199{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100200 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100201 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
202 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
203
204 Iterator input(_input, window);
205 Iterator output_x;
206 Iterator output_y;
207
208 if(_run_sobel_x)
209 {
210 output_x = Iterator(_output_x, window);
211 }
212
213 if(_run_sobel_y)
214 {
215 output_y = Iterator(_output_y, window);
216 }
217
218 if(_run_sobel_y && _run_sobel_x)
219 {
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100220 execute_window_loop(window, [&](const Coordinates &)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100221 {
222 const uint8x16_t data = vld1q_u8(input.ptr() - 3);
223
224 const uint16x8_t tmp_low_u16 = vmovl_u8(vget_low_u8(data));
225 const uint16x8_t tmp_high_u16 = vmovl_u8(vget_high_u8(data));
226
227 const int32x4x4_t data_s32 =
228 {
229 {
230 vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(tmp_low_u16))),
231 vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(tmp_low_u16))),
232 vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(tmp_high_u16))),
233 vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(tmp_high_u16)))
234 }
235 };
236
237 const int32x4x2_t out_y = compute_hor_sobel_y(data_s32);
238 vst1q_s32(reinterpret_cast<int32_t *>(output_y.ptr()), out_y.val[0]);
239 vst1q_s32(reinterpret_cast<int32_t *>(output_y.ptr()) + 4, out_y.val[1]);
240
241 const int32x4x2_t out_x = compute_hor_sobel_x(data_s32);
242 vst1q_s32(reinterpret_cast<int32_t *>(output_x.ptr()), out_x.val[0]);
243 vst1q_s32(reinterpret_cast<int32_t *>(output_x.ptr()) + 4, out_x.val[1]);
244 },
245 input, output_x, output_y);
246 }
247 else if(_run_sobel_x)
248 {
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100249 execute_window_loop(window, [&](const Coordinates &)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100250 {
251 const uint8x16_t data = vld1q_u8(input.ptr() - 3);
252
253 const uint16x8_t tmp_low_u16 = vmovl_u8(vget_low_u8(data));
254 const uint16x8_t tmp_high_u16 = vmovl_u8(vget_high_u8(data));
255
256 const int32x4x4_t data_s32 =
257 {
258 {
259 vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(tmp_low_u16))),
260 vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(tmp_low_u16))),
261 vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(tmp_high_u16))),
262 vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(tmp_high_u16)))
263 }
264 };
265
266 const int32x4x2_t out = compute_hor_sobel_x(data_s32);
267 vst1q_s32(reinterpret_cast<int32_t *>(output_x.ptr()), out.val[0]);
268 vst1q_s32(reinterpret_cast<int32_t *>(output_x.ptr()) + 4, out.val[1]);
269 },
270 input, output_x);
271 }
272 else if(_run_sobel_y)
273 {
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100274 execute_window_loop(window, [&](const Coordinates &)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100275 {
276 const uint8x16_t data = vld1q_u8(input.ptr() - 3);
277
278 const uint16x8_t tmp_low_u16 = vmovl_u8(vget_low_u8(data));
279 const uint16x8_t tmp_high_u16 = vmovl_u8(vget_high_u8(data));
280
281 const int32x4x4_t data_s32 =
282 {
283 {
284 vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(tmp_low_u16))),
285 vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(tmp_low_u16))),
286 vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(tmp_high_u16))),
287 vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(tmp_high_u16)))
288 }
289 };
290
Isabella Gottardi43ce8982017-11-08 11:13:23 +0000291 const int32x4x2_t out = compute_hor_sobel_y(data_s32);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100292 vst1q_s32(reinterpret_cast<int32_t *>(output_y.ptr()), out.val[0]);
293 vst1q_s32(reinterpret_cast<int32_t *>(output_y.ptr()) + 4, out.val[1]);
294 },
295 input, output_y);
296 }
297}
298
299NESobel7x7VertKernel::NESobel7x7VertKernel()
300 : _input_x(nullptr), _input_y(nullptr), _output_x(nullptr), _output_y(nullptr), _run_sobel_x(false), _run_sobel_y(false)
301{
302}
303
304BorderSize NESobel7x7VertKernel::border_size() const
305{
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100306 return BorderSize{ 3, 0 };
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100307}
308
309void NESobel7x7VertKernel::configure(const ITensor *input_x, const ITensor *input_y, ITensor *output_x, ITensor *output_y, bool border_undefined)
310{
311 ARM_COMPUTE_ERROR_ON((output_x == nullptr) && (output_y == nullptr));
312
313 _run_sobel_x = (output_x != nullptr);
314 _run_sobel_y = (output_y != nullptr);
315
316 if(_run_sobel_x)
317 {
318 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(input_x, Format::S32);
319 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output_x, Format::S32);
320 }
321
322 if(_run_sobel_y)
323 {
324 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(input_y, Format::S32);
325 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output_y, Format::S32);
326 }
327
328 _input_x = input_x;
329 _input_y = input_y;
330 _output_x = output_x;
331 _output_y = output_y;
332
333 const ITensor *const input = _run_sobel_x ? input_x : input_y;
334
335 // Configure kernel window
336 constexpr unsigned int num_elems_processed_per_iteration = 8;
337 constexpr unsigned int num_elems_read_per_iteration = 8;
338 constexpr unsigned int num_elems_written_per_iteration = 8;
339 constexpr unsigned int num_rows_read_per_iteration = 7;
340
341 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
342 AccessWindowHorizontal output_x_access(output_x == nullptr ? nullptr : output_x->info(), 0, num_elems_written_per_iteration);
343 AccessWindowHorizontal output_y_access(output_y == nullptr ? nullptr : output_y->info(), 0, num_elems_written_per_iteration);
344
345 update_window_and_padding(win,
346 AccessWindowRectangle(input_x == nullptr ? nullptr : input_x->info(), 0, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration),
347 AccessWindowRectangle(input_y == nullptr ? nullptr : input_y->info(), 0, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration),
348 output_x_access,
349 output_y_access);
350
351 output_x_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
352 output_y_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
353
354 INEKernel::configure(win);
355}
356
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100357void NESobel7x7VertKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100358{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100359 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100360 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
361 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
362
363 Iterator input_x;
364 Iterator input_y;
365 Iterator output_x;
366 Iterator output_y;
367
368 int32_t in_x_stride = 0;
369 int32_t in_y_stride = 0;
370
371 if(_run_sobel_x)
372 {
373 input_x = Iterator(_input_x, window);
374 output_x = Iterator(_output_x, window);
375 in_x_stride = _input_x->info()->strides_in_bytes()[1] / pixel_size_from_format(_input_x->info()->format());
376 }
377
378 if(_run_sobel_y)
379 {
380 input_y = Iterator(_input_y, window);
381 output_y = Iterator(_output_y, window);
382 in_y_stride = _input_y->info()->strides_in_bytes()[1] / pixel_size_from_format(_input_y->info()->format());
383 }
384
385 if(_run_sobel_x)
386 {
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100387 execute_window_loop(window, [&](const Coordinates &)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100388 {
389 auto in_ptr = reinterpret_cast<int32_t *>(input_x.ptr()) - 3 * in_x_stride;
390
391 //top3
392 int32x4x2_t data =
393 {
394 {
395 vld1q_s32(in_ptr),
396 vld1q_s32(in_ptr + 4)
397 }
398 };
399
400 int32x4x2_t out = data;
401
402 //top2
403 in_ptr += in_x_stride;
404 data.val[0] = vld1q_s32(in_ptr);
405 out.val[0] = vmlaq_s32(out.val[0], data.val[0], six);
406
407 data.val[1] = vld1q_s32(in_ptr + 4);
408 out.val[1] = vmlaq_s32(out.val[1], data.val[1], six);
409
410 //top
411 in_ptr += in_x_stride;
412 data.val[0] = vld1q_s32(in_ptr);
413 out.val[0] = vmlaq_s32(out.val[0], data.val[0], fifteen);
414
415 data.val[1] = vld1q_s32(in_ptr + 4);
416 out.val[1] = vmlaq_s32(out.val[1], data.val[1], fifteen);
417
418 //mid
419 in_ptr += in_x_stride;
420 data.val[0] = vld1q_s32(in_ptr);
421 out.val[0] = vmlaq_s32(out.val[0], data.val[0], twenty);
422
423 data.val[1] = vld1q_s32(in_ptr + 4);
424 out.val[1] = vmlaq_s32(out.val[1], data.val[1], twenty);
425
426 //low
427 in_ptr += in_x_stride;
428 data.val[0] = vld1q_s32(in_ptr);
429 out.val[0] = vmlaq_s32(out.val[0], data.val[0], fifteen);
430
431 data.val[1] = vld1q_s32(in_ptr + 4);
432 out.val[1] = vmlaq_s32(out.val[1], data.val[1], fifteen);
433
434 //low2
435 in_ptr += in_x_stride;
436 data.val[0] = vld1q_s32(in_ptr);
437 out.val[0] = vmlaq_s32(out.val[0], data.val[0], six);
438
439 data.val[1] = vld1q_s32(in_ptr + 4);
440 out.val[1] = vmlaq_s32(out.val[1], data.val[1], six);
441
442 //low3
443 in_ptr += in_x_stride;
444 data.val[0] = vld1q_s32(in_ptr);
445 out.val[0] = vaddq_s32(out.val[0], data.val[0]);
446
447 data.val[1] = vld1q_s32(in_ptr + 4);
448 out.val[1] = vaddq_s32(out.val[1], data.val[1]);
449
450 vst1q_s32(reinterpret_cast<int32_t *>(output_x.ptr()) + 0, out.val[0]);
451 vst1q_s32(reinterpret_cast<int32_t *>(output_x.ptr()) + 4, out.val[1]);
452 },
453 input_x, output_x);
454 }
455
456 if(_run_sobel_y)
457 {
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100458 execute_window_loop(window, [&](const Coordinates &)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100459 {
460 auto in_ptr = reinterpret_cast<int32_t *>(input_y.ptr()) - 3 * in_y_stride;
461
462 //top3
463 int32x4x2_t data =
464 {
465 {
466 vld1q_s32(in_ptr),
467 vld1q_s32(in_ptr + 4)
468 }
469 };
470
471 int32x4x2_t out =
472 {
473 {
474 vnegq_s32(data.val[0]),
475 vnegq_s32(data.val[1])
476 }
477 };
478
479 //top2
480 in_ptr += in_y_stride;
481 data.val[0] = vld1q_s32(in_ptr);
482 out.val[0] = vmlaq_s32(out.val[0], data.val[0], minusfour);
483
484 data.val[1] = vld1q_s32(in_ptr + 4);
485 out.val[1] = vmlaq_s32(out.val[1], data.val[1], minusfour);
486
487 //top
488 in_ptr += in_y_stride;
489 data.val[0] = vld1q_s32(in_ptr);
490 out.val[0] = vmlaq_s32(out.val[0], data.val[0], minusfive);
491
492 data.val[1] = vld1q_s32(in_ptr + 4);
493 out.val[1] = vmlaq_s32(out.val[1], data.val[1], minusfive);
494
495 //low
496 in_ptr += (2 * in_y_stride);
497 data.val[0] = vld1q_s32(in_ptr);
498 out.val[0] = vmlaq_s32(out.val[0], data.val[0], five);
499
500 data.val[1] = vld1q_s32(in_ptr + 4);
501 out.val[1] = vmlaq_s32(out.val[1], data.val[1], five);
502
503 //low2
504 in_ptr += in_y_stride;
505 data.val[0] = vld1q_s32(in_ptr);
506 out.val[0] = vmlaq_s32(out.val[0], data.val[0], four);
507
508 data.val[1] = vld1q_s32(in_ptr + 4);
509 out.val[1] = vmlaq_s32(out.val[1], data.val[1], four);
510
511 //low3
512 in_ptr += in_y_stride;
513 data.val[0] = vld1q_s32(in_ptr);
514 out.val[0] = vaddq_s32(out.val[0], data.val[0]);
515
516 data.val[1] = vld1q_s32(in_ptr + 4);
517 out.val[1] = vaddq_s32(out.val[1], data.val[1]);
518
519 vst1q_s32(reinterpret_cast<int32_t *>(output_y.ptr()) + 0, out.val[0]);
520 vst1q_s32(reinterpret_cast<int32_t *>(output_y.ptr()) + 4, out.val[1]);
521 },
522 input_y, output_y);
523 }
524}