blob: ab08a1cfebff5237331282ce7205024afbb164d8 [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
91void NESobel3x3Kernel::run(const Window &window)
92{
93 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
94 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
95
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));
99
100 Iterator input(_input, window);
101 Iterator output_y;
102 Iterator output_x;
103
104 if(_run_sobel_y)
105 {
106 output_y = Iterator(_output_y, window);
107 }
108
109 if(_run_sobel_x)
110 {
111 output_x = Iterator(_output_x, window);
112 }
113
114 static const int16x8_t two = vdupq_n_s16(2);
115 static const int16x8_t minustwo = vdupq_n_s16(-2);
116
117 if(_run_sobel_y && _run_sobel_x)
118 {
119 execute_window_loop(window, [&](const Coordinates & id)
120 {
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());
124
125 const int16x8x2_t top_s16 =
126 {
127 {
128 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(top_data))),
129 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(top_data)))
130 }
131 };
132 const int16x8x2_t mid_s16 =
133 {
134 {
135 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mid_data))),
136 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mid_data)))
137 }
138 };
139 const int16x8x2_t bot_s16 =
140 {
141 {
142 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(bot_data))),
143 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(bot_data)))
144 }
145 };
146
147 //SOBEL Y
148 //top left
149 int16x8_t out_y = vnegq_s16(top_s16.val[0]);
150 //top mid
151 out_y = vmlaq_s16(out_y, vextq_s16(top_s16.val[0], top_s16.val[1], 1), minustwo);
152 //top right
153 out_y = vsubq_s16(out_y, vextq_s16(top_s16.val[0], top_s16.val[1], 2));
154 //bot left
155 out_y = vaddq_s16(out_y, bot_s16.val[0]);
156 //bot mid
157 out_y = vmlaq_s16(out_y, vextq_s16(bot_s16.val[0], bot_s16.val[1], 1), two);
158 //bot right
159 out_y = vaddq_s16(out_y, vextq_s16(bot_s16.val[0], bot_s16.val[1], 2));
160
161 vst1q_s16(reinterpret_cast<int16_t *>(output_y.ptr()), out_y);
162
163 //SOBEL X
164 //top left
165 int16x8_t out_x = vnegq_s16(top_s16.val[0]);
166 //top right
167 out_x = vaddq_s16(out_x, vextq_s16(top_s16.val[0], top_s16.val[1], 2));
168 //mid left
169 out_x = vmlaq_s16(out_x, mid_s16.val[0], minustwo);
170 //mid right
171 out_x = vmlaq_s16(out_x, vextq_s16(mid_s16.val[0], mid_s16.val[1], 2), two);
172 //bot left
173 out_x = vsubq_s16(out_x, bot_s16.val[0]);
174 //bot right
175 out_x = vaddq_s16(out_x, vextq_s16(bot_s16.val[0], bot_s16.val[1], 2));
176
177 vst1q_s16(reinterpret_cast<int16_t *>(output_x.ptr()), out_x);
178 },
179 input, output_x, output_y);
180 }
181 else if(_run_sobel_x)
182 {
183 execute_window_loop(window, [&](const Coordinates & id)
184 {
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());
188
189 const int16x8x2_t top_s16 =
190 {
191 {
192 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(top_data))),
193 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(top_data)))
194 }
195 };
196 const int16x8x2_t mid_s16 =
197 {
198 {
199 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mid_data))),
200 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mid_data)))
201 }
202 };
203 const int16x8x2_t bot_s16 =
204 {
205 {
206 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(bot_data))),
207 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(bot_data)))
208 }
209 };
210
211 //SOBEL X
212 //top left
213 int16x8_t out = vnegq_s16(top_s16.val[0]);
214 //top right
215 out = vaddq_s16(out, vextq_s16(top_s16.val[0], top_s16.val[1], 2));
216 //mid left
217 out = vmlaq_s16(out, mid_s16.val[0], minustwo);
218 //mid right
219 out = vmlaq_s16(out, vextq_s16(mid_s16.val[0], mid_s16.val[1], 2), two);
220 //bot left
221 out = vsubq_s16(out, bot_s16.val[0]);
222 //bot right
223 out = vaddq_s16(out, vextq_s16(bot_s16.val[0], bot_s16.val[1], 2));
224
225 vst1q_s16(reinterpret_cast<int16_t *>(output_x.ptr()), out);
226 },
227 input, output_x);
228 }
229 else if(_run_sobel_y)
230 {
231 execute_window_loop(window, [&](const Coordinates & id)
232 {
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());
235
236 const int16x8x2_t top_s16 =
237 {
238 {
239 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(top_data))),
240 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(top_data)))
241 }
242 };
243 const int16x8x2_t bot_s16 =
244 {
245 {
246 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(bot_data))),
247 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(bot_data)))
248 }
249 };
250
251 //SOBEL Y
252 //top left
253 int16x8_t out = vnegq_s16(top_s16.val[0]);
254 //top mid
255 out = vmlaq_s16(out, vextq_s16(top_s16.val[0], top_s16.val[1], 1), minustwo);
256 //top right
257 out = vsubq_s16(out, vextq_s16(top_s16.val[0], top_s16.val[1], 2));
258 //bot left
259 out = vaddq_s16(out, bot_s16.val[0]);
260 //bot mid
261 out = vmlaq_s16(out, vextq_s16(bot_s16.val[0], bot_s16.val[1], 1), two);
262 //bot right
263 out = vaddq_s16(out, vextq_s16(bot_s16.val[0], bot_s16.val[1], 2));
264
265 vst1q_s16(reinterpret_cast<int16_t *>(output_y.ptr()), out);
266 },
267 input, output_y);
268 }
269}