blob: 3add699f629e433473e851574c2a67288452201a [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michalis Spyroua4f378d2019-04-26 14:54:54 +01002 * Copyright (c) 2016-2019 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 */
24#include "arm_compute/core/NEON/kernels/NEScharr3x3Kernel.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
38namespace
39{
40const int16x8_t three = vdupq_n_s16(3);
41const int16x8_t minus_three = vdupq_n_s16(-3);
42const int16x8_t ten = vdupq_n_s16(10);
43const int16x8_t minus_ten = vdupq_n_s16(-10);
44
45inline int16x8_t scharr_y(const int16x8x2_t &top, const int16x8x2_t &bottom)
46{
47 // Top left
48 int16x8_t out = vmulq_s16(top.val[0], minus_three);
49 // Top center
50 out = vmlaq_s16(out, vextq_s16(top.val[0], top.val[1], 1), minus_ten);
51 // Top right
52 out = vmlaq_s16(out, vextq_s16(top.val[0], top.val[1], 2), minus_three);
53
54 // Bottom left
55 out = vmlaq_s16(out, bottom.val[0], three);
56 // Bottom center
57 out = vmlaq_s16(out, vextq_s16(bottom.val[0], bottom.val[1], 1), ten);
58 // Bottom right
59 out = vmlaq_s16(out, vextq_s16(bottom.val[0], bottom.val[1], 2), three);
60
61 return out;
62}
63
64inline int16x8_t scharr_x(const int16x8x2_t &top, const int16x8x2_t &middle, const int16x8x2_t &bottom)
65{
66 // Top left
67 int16x8_t out = vmulq_s16(top.val[0], minus_three);
68 // Top right
69 out = vmlaq_s16(out, vextq_s16(top.val[0], top.val[1], 2), three);
70
71 // Middle left
72 out = vmlaq_s16(out, middle.val[0], minus_ten);
73 // Middle right
74 out = vmlaq_s16(out, vextq_s16(middle.val[0], middle.val[1], 2), ten);
75
76 // Bottom left
77 out = vmlaq_s16(out, bottom.val[0], minus_three);
78 // Bottom right
79 out = vmlaq_s16(out, vextq_s16(bottom.val[0], bottom.val[1], 2), three);
80
81 return out;
82}
83} // namespace
84
85NEScharr3x3Kernel::NEScharr3x3Kernel()
86 : _run_scharr_x(false), _run_scharr_y(false), _input(nullptr), _output_x(nullptr), _output_y(nullptr)
87{
88}
89
90void NEScharr3x3Kernel::configure(const ITensor *input, ITensor *output_x, ITensor *output_y, bool border_undefined)
91{
92 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
93 ARM_COMPUTE_ERROR_ON((output_x == nullptr) && (output_y == nullptr));
94
95 _run_scharr_x = output_x != nullptr;
96 _run_scharr_y = output_y != nullptr;
97
98 if(_run_scharr_x)
99 {
100 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output_x, 1, DataType::S16);
101 }
102
103 if(_run_scharr_y)
104 {
105 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output_y, 1, DataType::S16);
106 }
107
108 _input = input;
109 _output_x = output_x;
110 _output_y = output_y;
111
112 // Configure kernel window
113 constexpr unsigned int num_elems_processed_per_iteration = 8;
114 constexpr unsigned int num_elems_read_per_iteration = 16;
115 constexpr unsigned int num_elems_written_per_iteration = 8;
116 constexpr unsigned int num_rows_read_per_iteration = 3;
117
118 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
119 AccessWindowHorizontal output_x_access(output_x == nullptr ? nullptr : output_x->info(), 0, num_elems_written_per_iteration);
120 AccessWindowHorizontal output_y_access(output_y == nullptr ? nullptr : output_y->info(), 0, num_elems_written_per_iteration);
121
122 update_window_and_padding(win,
123 AccessWindowRectangle(input->info(), -border_size().left, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration),
124 output_x_access,
125 output_y_access);
126
127 output_x_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
128 output_y_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
129
130 INEKernel::configure(win);
131}
132
133BorderSize NEScharr3x3Kernel::border_size() const
134{
135 return BorderSize(1);
136}
137
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100138void NEScharr3x3Kernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100139{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100140 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100141 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
142 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
143
144 const unsigned char *const input_top_ptr = _input->ptr_to_element(Coordinates(-1, -1));
145 const unsigned char *const input_mid_ptr = _input->ptr_to_element(Coordinates(-1, 0));
146 const unsigned char *const input_bot_ptr = _input->ptr_to_element(Coordinates(-1, +1));
147
148 Iterator input(_input, window);
149 Iterator output_y;
150 Iterator output_x;
151
152 if(_run_scharr_y)
153 {
154 output_y = Iterator(_output_y, window);
155 }
156
157 if(_run_scharr_x)
158 {
159 output_x = Iterator(_output_x, window);
160 }
161
162 if(_run_scharr_x && _run_scharr_y)
163 {
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100164 execute_window_loop(window, [&](const Coordinates &)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100165 {
166
167 const uint8x16_t top_data = vld1q_u8(input_top_ptr + input.offset());
168 const uint8x16_t mid_data = vld1q_u8(input_mid_ptr + input.offset());
169 const uint8x16_t bot_data = vld1q_u8(input_bot_ptr + input.offset());
170
171 const int16x8x2_t top_s16 =
172 {
173 {
174 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(top_data))),
175 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(top_data)))
176 }
177 };
178 const int16x8x2_t mid_s16 =
179 {
180 {
181 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mid_data))),
182 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mid_data)))
183 }
184 };
185 const int16x8x2_t bot_s16 =
186 {
187 {
188 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(bot_data))),
189 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(bot_data)))
190 }
191 };
192
193 vst1q_s16(reinterpret_cast<int16_t *>(output_x.ptr()), scharr_x(top_s16, mid_s16, bot_s16));
194 vst1q_s16(reinterpret_cast<int16_t *>(output_y.ptr()), scharr_y(top_s16, bot_s16));
195 },
196 input, output_x, output_y);
197 }
198 else if(_run_scharr_x)
199 {
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100200 execute_window_loop(window, [&](const Coordinates &)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100201 {
202
203 const uint8x16_t top_data = vld1q_u8(input_top_ptr + input.offset());
204 const uint8x16_t mid_data = vld1q_u8(input_mid_ptr + input.offset());
205 const uint8x16_t bot_data = vld1q_u8(input_bot_ptr + input.offset());
206
207 const int16x8x2_t top_s16 =
208 {
209 {
210 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(top_data))),
211 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(top_data)))
212 }
213 };
214 const int16x8x2_t mid_s16 =
215 {
216 {
217 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mid_data))),
218 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mid_data)))
219 }
220 };
221 const int16x8x2_t bot_s16 =
222 {
223 {
224 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(bot_data))),
225 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(bot_data)))
226 }
227 };
228
229 vst1q_s16(reinterpret_cast<int16_t *>(output_x.ptr()), scharr_x(top_s16, mid_s16, bot_s16));
230 },
231 input, output_x);
232 }
233 else if(_run_scharr_y)
234 {
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100235 execute_window_loop(window, [&](const Coordinates &)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100236 {
237
238 const uint8x16_t top_data = vld1q_u8(input_top_ptr + input.offset());
239 const uint8x16_t bot_data = vld1q_u8(input_bot_ptr + input.offset());
240
241 const int16x8x2_t top_s16 =
242 {
243 {
244 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(top_data))),
245 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(top_data)))
246 }
247 };
248 const int16x8x2_t bot_s16 =
249 {
250 {
251 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(bot_data))),
252 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(bot_data)))
253 }
254 };
255
256 vst1q_s16(reinterpret_cast<int16_t *>(output_y.ptr()), scharr_y(top_s16, bot_s16));
257 },
258 input, output_y);
259 }
260}