blob: 126b62b21e28b2ce2a05ea0bf97e973b04785ec7 [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/NEBox3x3Kernel.h"
25
26#include "arm_compute/core/Coordinates.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/IAccessWindow.h"
29#include "arm_compute/core/ITensor.h"
30#include "arm_compute/core/NEON/INEKernel.h"
31#include "arm_compute/core/Validate.h"
32#include <arm_neon.h>
33
34using namespace arm_compute;
35
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +000036#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Moritz Pflanzerc186b572017-09-07 09:48:04 +010037void NEBox3x3FP16Kernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038{
Moritz Pflanzerc186b572017-09-07 09:48:04 +010039 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
41 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INESimpleKernel::window(), window);
42
43 Iterator input(_input, window);
44 Iterator output(_output, window);
45
46 unsigned char *const input_top_ptr = _input->ptr_to_element(Coordinates(-1, -1));
47 unsigned char *const input_mid_ptr = _input->ptr_to_element(Coordinates(-1, 0));
48 unsigned char *const input_bot_ptr = _input->ptr_to_element(Coordinates(-1, +1));
49
50 const float16x8_t oneovernine = vdupq_n_f16(1.0f / 9.0f);
51
Michalis Spyroua4f378d2019-04-26 14:54:54 +010052 execute_window_loop(window, [&](const Coordinates &)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053 {
54 const uint8x16_t top_data = vld1q_u8(input_top_ptr + input.offset());
55 const uint8x16_t mid_data = vld1q_u8(input_mid_ptr + input.offset());
56 const uint8x16_t bot_data = vld1q_u8(input_bot_ptr + input.offset());
57
Alan Kellyefcefea2019-04-10 21:37:05 +020058 const uint16x8x2_t top_f16 =
Anthony Barbier6ff3b192017-09-04 18:44:23 +010059 {
60 {
Alan Kellyefcefea2019-04-10 21:37:05 +020061 vmovl_u8(vget_low_u8(top_data)),
62 vmovl_u8(vget_high_u8(top_data))
Anthony Barbier6ff3b192017-09-04 18:44:23 +010063 }
64 };
65
Alan Kellyefcefea2019-04-10 21:37:05 +020066 const uint16x8x2_t mid_f16 =
Anthony Barbier6ff3b192017-09-04 18:44:23 +010067 {
68 {
Alan Kellyefcefea2019-04-10 21:37:05 +020069 vmovl_u8(vget_low_u8(mid_data)),
70 vmovl_u8(vget_high_u8(mid_data))
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071 }
72 };
73
Alan Kellyefcefea2019-04-10 21:37:05 +020074 const uint16x8x2_t bot_f16 =
Anthony Barbier6ff3b192017-09-04 18:44:23 +010075 {
76 {
Alan Kellyefcefea2019-04-10 21:37:05 +020077 vmovl_u8(vget_low_u8(bot_data)),
78 vmovl_u8(vget_high_u8(bot_data))
Anthony Barbier6ff3b192017-09-04 18:44:23 +010079 }
80 };
81
82 //top left
Alan Kellyefcefea2019-04-10 21:37:05 +020083 uint16x8_t out = top_f16.val[0];
Anthony Barbier6ff3b192017-09-04 18:44:23 +010084 //top mid
Alan Kellyefcefea2019-04-10 21:37:05 +020085 out = vaddq_u16(out, vextq_u16(top_f16.val[0], top_f16.val[1], 1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010086 //top right
Alan Kellyefcefea2019-04-10 21:37:05 +020087 out = vaddq_u16(out, vextq_u16(top_f16.val[0], top_f16.val[1], 2));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010088 //mid left
Alan Kellyefcefea2019-04-10 21:37:05 +020089 out = vaddq_u16(out, mid_f16.val[0]);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090 //mid mid
Alan Kellyefcefea2019-04-10 21:37:05 +020091 out = vaddq_u16(out, vextq_u16(mid_f16.val[0], mid_f16.val[1], 1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010092 //mid right
Alan Kellyefcefea2019-04-10 21:37:05 +020093 out = vaddq_u16(out, vextq_u16(mid_f16.val[0], mid_f16.val[1], 2));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094 //bot left
Alan Kellyefcefea2019-04-10 21:37:05 +020095 out = vaddq_u16(out, bot_f16.val[0]);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010096 //bot mid
Alan Kellyefcefea2019-04-10 21:37:05 +020097 out = vaddq_u16(out, vextq_u16(bot_f16.val[0], bot_f16.val[1], 1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010098 //bot right
Alan Kellyefcefea2019-04-10 21:37:05 +020099 out = vaddq_u16(out, vextq_u16(bot_f16.val[0], bot_f16.val[1], 2));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100
Alan Kellyefcefea2019-04-10 21:37:05 +0200101 float16x8_t outfloat = vcvtq_f16_u16(out);
102 outfloat = vmulq_f16(outfloat, oneovernine);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103
Alan Kellyefcefea2019-04-10 21:37:05 +0200104 vst1_u8(output.ptr(), vqmovun_s16(vcvtq_s16_f16(outfloat)));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105 },
106 input, output);
107}
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000108#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109
110BorderSize NEBox3x3Kernel::border_size() const
111{
112 return BorderSize(1);
113}
114
115void NEBox3x3Kernel::configure(const ITensor *input, ITensor *output, bool border_undefined)
116{
117 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
118
119 set_shape_if_empty(*output->info(), input->info()->tensor_shape());
120
121 set_format_if_unknown(*input->info(), Format::U8);
122 set_format_if_unknown(*output->info(), Format::U8);
123
124 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output);
125 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
126 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
127 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
128
129 _input = input;
130 _output = output;
131
132 constexpr unsigned int num_elems_processed_per_iteration = 8;
133 constexpr unsigned int num_elems_read_per_iteration = 16;
134 constexpr unsigned int num_elems_written_per_iteration = 8;
135 constexpr unsigned int num_rows_read_per_iteration = 3;
136 constexpr int rect_offset_xy = -1;
137
138 // Configure kernel window
139 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
140 AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
141
142 update_window_and_padding(win, AccessWindowRectangle(input->info(), rect_offset_xy, rect_offset_xy, num_elems_read_per_iteration, num_rows_read_per_iteration), output_access);
143
144 output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
145
146 INEKernel::configure(win);
147}
148
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100149void NEBox3x3Kernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100151 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
153 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INESimpleKernel::window(), window);
154
155 Iterator input(_input, window);
156 Iterator output(_output, window);
157
158 unsigned char *const input_top_ptr = _input->ptr_to_element(Coordinates(-1, -1));
159 unsigned char *const input_mid_ptr = _input->ptr_to_element(Coordinates(-1, 0));
160 unsigned char *const input_bot_ptr = _input->ptr_to_element(Coordinates(-1, +1));
161
Alan Kellya4003342019-04-10 21:37:48 +0200162 const int shift = 19;
163 int value = (1 << shift) / 9 + 1; //58255 / (2^19) ~= 1/9
164 const int32x4_t oneovernine = vdupq_n_s32(value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100165
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100166 execute_window_loop(window, [&](const Coordinates &)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100167 {
168 const uint8x16_t top_data = vld1q_u8(input_top_ptr + input.offset());
169 const uint8x16_t mid_data = vld1q_u8(input_mid_ptr + input.offset());
170 const uint8x16_t bot_data = vld1q_u8(input_bot_ptr + input.offset());
171
172 const int16x8x2_t top_s16 =
173 {
174 {
175 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(top_data))),
176 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(top_data)))
177 }
178 };
179 const int16x8x2_t mid_s16 =
180 {
181 {
182 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mid_data))),
183 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mid_data)))
184 }
185 };
186 const int16x8x2_t bot_s16 =
187 {
188 {
189 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(bot_data))),
190 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(bot_data)))
191 }
192 };
193
194 //top left
195 int16x8_t out = top_s16.val[0];
196 //top mid
197 out = vaddq_s16(out, vextq_s16(top_s16.val[0], top_s16.val[1], 1));
198 //top right
199 out = vaddq_s16(out, vextq_s16(top_s16.val[0], top_s16.val[1], 2));
200 //mid left
201 out = vaddq_s16(out, mid_s16.val[0]);
202 //mid mid
203 out = vaddq_s16(out, vextq_s16(mid_s16.val[0], mid_s16.val[1], 1));
204 //mid right
205 out = vaddq_s16(out, vextq_s16(mid_s16.val[0], mid_s16.val[1], 2));
206 //bot left
207 out = vaddq_s16(out, bot_s16.val[0]);
208 //bot mid
209 out = vaddq_s16(out, vextq_s16(bot_s16.val[0], bot_s16.val[1], 1));
210 //bot right
211 out = vaddq_s16(out, vextq_s16(bot_s16.val[0], bot_s16.val[1], 2));
212
Alan Kellya4003342019-04-10 21:37:48 +0200213 int32x4_t outfloathigh = vmovl_s16(vget_high_s16(out));
214 int32x4_t outfloatlow = vmovl_s16(vget_low_s16(out));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100215
Alan Kellya4003342019-04-10 21:37:48 +0200216 outfloathigh = vmulq_s32(outfloathigh, oneovernine);
217 outfloatlow = vmulq_s32(outfloatlow, oneovernine);
218 outfloathigh = vshrq_n_s32(outfloathigh, shift);
219 outfloatlow = vshrq_n_s32(outfloatlow, shift);
220 out = vcombine_s16(vqmovn_s32((outfloatlow)),
221 vqmovn_s32((outfloathigh)));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100222
223 vst1_u8(output.ptr(), vqmovun_s16(out));
224 },
225 input, output);
226}