2 * Copyright (c) 2016, 2017 ARM Limited.
4 * SPDX-License-Identifier: MIT
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:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
24 #include "arm_compute/core/NEON/kernels/NESobel3x3Kernel.h"
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"
36 using namespace arm_compute;
38 NESobel3x3Kernel::NESobel3x3Kernel()
39 : _run_sobel_x(false), _run_sobel_y(false), _input(nullptr), _output_x(nullptr), _output_y(nullptr)
43 BorderSize NESobel3x3Kernel::border_size() const
48 void NESobel3x3Kernel::configure(const ITensor *input, ITensor *output_x, ITensor *output_y, bool border_undefined)
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));
53 _run_sobel_x = output_x != nullptr;
54 _run_sobel_y = output_y != nullptr;
58 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output_x, 1, DataType::S16);
63 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output_y, 1, DataType::S16);
70 // Configure kernel window
71 constexpr unsigned int processed_elements(8);
72 constexpr unsigned int read_elements(16);
73 constexpr unsigned int written_elements(8);
74 constexpr unsigned int read_rows(3);
76 Window win = calculate_max_window(*input->info(), Steps(processed_elements), border_undefined, border_size());
77 AccessWindowHorizontal output_x_access(output_x == nullptr ? nullptr : output_x->info(), 0, written_elements);
78 AccessWindowHorizontal output_y_access(output_y == nullptr ? nullptr : output_y->info(), 0, written_elements);
80 update_window_and_padding(win,
81 AccessWindowRectangle(input->info(), -border_size().left, -border_size().top, read_elements, read_rows),
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());
88 INEKernel::configure(win);
91 void NESobel3x3Kernel::run(const Window &window)
93 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
94 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
96 const unsigned char *const input_top_ptr = _input->ptr_to_element(Coordinates(-1, -1));
97 const unsigned char *const input_mid_ptr = _input->ptr_to_element(Coordinates(-1, 0));
98 const unsigned char *const input_bot_ptr = _input->ptr_to_element(Coordinates(-1, 1));
100 Iterator input(_input, window);
106 output_y = Iterator(_output_y, window);
111 output_x = Iterator(_output_x, window);
114 static const int16x8_t two = vdupq_n_s16(2);
115 static const int16x8_t minustwo = vdupq_n_s16(-2);
117 if(_run_sobel_y && _run_sobel_x)
119 execute_window_loop(window, [&](const Coordinates & id)
121 const uint8x16_t top_data = vld1q_u8(input_top_ptr + input.offset());
122 const uint8x16_t mid_data = vld1q_u8(input_mid_ptr + input.offset());
123 const uint8x16_t bot_data = vld1q_u8(input_bot_ptr + input.offset());
125 const int16x8x2_t top_s16 =
128 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(top_data))),
129 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(top_data)))
132 const int16x8x2_t mid_s16 =
135 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mid_data))),
136 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mid_data)))
139 const int16x8x2_t bot_s16 =
142 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(bot_data))),
143 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(bot_data)))
149 int16x8_t out_y = vnegq_s16(top_s16.val[0]);
151 out_y = vmlaq_s16(out_y, vextq_s16(top_s16.val[0], top_s16.val[1], 1), minustwo);
153 out_y = vsubq_s16(out_y, vextq_s16(top_s16.val[0], top_s16.val[1], 2));
155 out_y = vaddq_s16(out_y, bot_s16.val[0]);
157 out_y = vmlaq_s16(out_y, vextq_s16(bot_s16.val[0], bot_s16.val[1], 1), two);
159 out_y = vaddq_s16(out_y, vextq_s16(bot_s16.val[0], bot_s16.val[1], 2));
161 vst1q_s16(reinterpret_cast<int16_t *>(output_y.ptr()), out_y);
165 int16x8_t out_x = vnegq_s16(top_s16.val[0]);
167 out_x = vaddq_s16(out_x, vextq_s16(top_s16.val[0], top_s16.val[1], 2));
169 out_x = vmlaq_s16(out_x, mid_s16.val[0], minustwo);
171 out_x = vmlaq_s16(out_x, vextq_s16(mid_s16.val[0], mid_s16.val[1], 2), two);
173 out_x = vsubq_s16(out_x, bot_s16.val[0]);
175 out_x = vaddq_s16(out_x, vextq_s16(bot_s16.val[0], bot_s16.val[1], 2));
177 vst1q_s16(reinterpret_cast<int16_t *>(output_x.ptr()), out_x);
179 input, output_x, output_y);
181 else if(_run_sobel_x)
183 execute_window_loop(window, [&](const Coordinates & id)
185 const uint8x16_t top_data = vld1q_u8(input_top_ptr + input.offset());
186 const uint8x16_t mid_data = vld1q_u8(input_mid_ptr + input.offset());
187 const uint8x16_t bot_data = vld1q_u8(input_bot_ptr + input.offset());
189 const int16x8x2_t top_s16 =
192 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(top_data))),
193 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(top_data)))
196 const int16x8x2_t mid_s16 =
199 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mid_data))),
200 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mid_data)))
203 const int16x8x2_t bot_s16 =
206 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(bot_data))),
207 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(bot_data)))
213 int16x8_t out = vnegq_s16(top_s16.val[0]);
215 out = vaddq_s16(out, vextq_s16(top_s16.val[0], top_s16.val[1], 2));
217 out = vmlaq_s16(out, mid_s16.val[0], minustwo);
219 out = vmlaq_s16(out, vextq_s16(mid_s16.val[0], mid_s16.val[1], 2), two);
221 out = vsubq_s16(out, bot_s16.val[0]);
223 out = vaddq_s16(out, vextq_s16(bot_s16.val[0], bot_s16.val[1], 2));
225 vst1q_s16(reinterpret_cast<int16_t *>(output_x.ptr()), out);
229 else if(_run_sobel_y)
231 execute_window_loop(window, [&](const Coordinates & id)
233 const uint8x16_t top_data = vld1q_u8(input_top_ptr + input.offset());
234 const uint8x16_t bot_data = vld1q_u8(input_bot_ptr + input.offset());
236 const int16x8x2_t top_s16 =
239 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(top_data))),
240 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(top_data)))
243 const int16x8x2_t bot_s16 =
246 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(bot_data))),
247 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(bot_data)))
253 int16x8_t out = vnegq_s16(top_s16.val[0]);
255 out = vmlaq_s16(out, vextq_s16(top_s16.val[0], top_s16.val[1], 1), minustwo);
257 out = vsubq_s16(out, vextq_s16(top_s16.val[0], top_s16.val[1], 2));
259 out = vaddq_s16(out, bot_s16.val[0]);
261 out = vmlaq_s16(out, vextq_s16(bot_s16.val[0], bot_s16.val[1], 1), two);
263 out = vaddq_s16(out, vextq_s16(bot_s16.val[0], bot_s16.val[1], 2));
265 vst1q_s16(reinterpret_cast<int16_t *>(output_y.ptr()), out);