blob: 48b959c308a7a73153fee0c0a2a580b296c1d1a8 [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
Alan Kelly0eb16cf2019-04-28 15:53:14 +020036int16x8_t calculate_kernel( const uint8x16_t &top_data, const uint8x16_t &mid_data, const uint8x16_t &bot_data){
37 const int16x8x2_t top_s16 =
38 {
39 {
40 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(top_data))),
41 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(top_data)))
42 }
43 };
44 const int16x8x2_t mid_s16 =
45 {
46 {
47 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mid_data))),
48 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mid_data)))
49 }
50 };
51 const int16x8x2_t bot_s16 =
52 {
53 {
54 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(bot_data))),
55 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(bot_data)))
56 }
57 };
58
59 //top left
60 int16x8_t out = top_s16.val[0];
61 //top mid
62 out = vaddq_s16(out, vextq_s16(top_s16.val[0], top_s16.val[1], 1));
63 //top right
64 out = vaddq_s16(out, vextq_s16(top_s16.val[0], top_s16.val[1], 2));
65 //mid left
66 out = vaddq_s16(out, mid_s16.val[0]);
67 //mid mid
68 out = vaddq_s16(out, vextq_s16(mid_s16.val[0], mid_s16.val[1], 1));
69 //mid right
70 out = vaddq_s16(out, vextq_s16(mid_s16.val[0], mid_s16.val[1], 2));
71 //bot left
72 out = vaddq_s16(out, bot_s16.val[0]);
73 //bot mid
74 out = vaddq_s16(out, vextq_s16(bot_s16.val[0], bot_s16.val[1], 1));
75 //bot right
76 out = vaddq_s16(out, vextq_s16(bot_s16.val[0], bot_s16.val[1], 2));
77 return out;
78}
79
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +000080#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Moritz Pflanzerc186b572017-09-07 09:48:04 +010081void NEBox3x3FP16Kernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010082{
Moritz Pflanzerc186b572017-09-07 09:48:04 +010083 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010084 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
85 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INESimpleKernel::window(), window);
86
87 Iterator input(_input, window);
88 Iterator output(_output, window);
89
90 unsigned char *const input_top_ptr = _input->ptr_to_element(Coordinates(-1, -1));
91 unsigned char *const input_mid_ptr = _input->ptr_to_element(Coordinates(-1, 0));
92 unsigned char *const input_bot_ptr = _input->ptr_to_element(Coordinates(-1, +1));
93
94 const float16x8_t oneovernine = vdupq_n_f16(1.0f / 9.0f);
95
Michalis Spyroua4f378d2019-04-26 14:54:54 +010096 execute_window_loop(window, [&](const Coordinates &)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097 {
98 const uint8x16_t top_data = vld1q_u8(input_top_ptr + input.offset());
99 const uint8x16_t mid_data = vld1q_u8(input_mid_ptr + input.offset());
100 const uint8x16_t bot_data = vld1q_u8(input_bot_ptr + input.offset());
101
Alan Kelly0eb16cf2019-04-28 15:53:14 +0200102 int16x8_t out = calculate_kernel(top_data, mid_data, bot_data);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103
Alan Kellyefcefea2019-04-10 21:37:05 +0200104 float16x8_t outfloat = vcvtq_f16_u16(out);
105 outfloat = vmulq_f16(outfloat, oneovernine);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100106
Alan Kellyefcefea2019-04-10 21:37:05 +0200107 vst1_u8(output.ptr(), vqmovun_s16(vcvtq_s16_f16(outfloat)));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108 },
109 input, output);
110}
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000111#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100112
113BorderSize NEBox3x3Kernel::border_size() const
114{
115 return BorderSize(1);
116}
117
118void NEBox3x3Kernel::configure(const ITensor *input, ITensor *output, bool border_undefined)
119{
120 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
121
122 set_shape_if_empty(*output->info(), input->info()->tensor_shape());
123
124 set_format_if_unknown(*input->info(), Format::U8);
125 set_format_if_unknown(*output->info(), Format::U8);
126
127 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output);
128 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
129 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
130 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
131
132 _input = input;
133 _output = output;
134
135 constexpr unsigned int num_elems_processed_per_iteration = 8;
136 constexpr unsigned int num_elems_read_per_iteration = 16;
137 constexpr unsigned int num_elems_written_per_iteration = 8;
138 constexpr unsigned int num_rows_read_per_iteration = 3;
139 constexpr int rect_offset_xy = -1;
140
141 // Configure kernel window
142 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
143 AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
144
145 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);
146
147 output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
148
149 INEKernel::configure(win);
150}
151
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100152void NEBox3x3Kernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100153{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100154 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100155 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
156 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INESimpleKernel::window(), window);
157
158 Iterator input(_input, window);
159 Iterator output(_output, window);
160
161 unsigned char *const input_top_ptr = _input->ptr_to_element(Coordinates(-1, -1));
162 unsigned char *const input_mid_ptr = _input->ptr_to_element(Coordinates(-1, 0));
163 unsigned char *const input_bot_ptr = _input->ptr_to_element(Coordinates(-1, +1));
164
Alan Kellya4003342019-04-10 21:37:48 +0200165 const int shift = 19;
166 int value = (1 << shift) / 9 + 1; //58255 / (2^19) ~= 1/9
167 const int32x4_t oneovernine = vdupq_n_s32(value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100168
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100169 execute_window_loop(window, [&](const Coordinates &)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100170 {
171 const uint8x16_t top_data = vld1q_u8(input_top_ptr + input.offset());
172 const uint8x16_t mid_data = vld1q_u8(input_mid_ptr + input.offset());
173 const uint8x16_t bot_data = vld1q_u8(input_bot_ptr + input.offset());
174
Alan Kelly0eb16cf2019-04-28 15:53:14 +0200175 int16x8_t out = calculate_kernel(top_data, mid_data, bot_data);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100176
Alan Kellya4003342019-04-10 21:37:48 +0200177 int32x4_t outfloathigh = vmovl_s16(vget_high_s16(out));
178 int32x4_t outfloatlow = vmovl_s16(vget_low_s16(out));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100179
Alan Kellya4003342019-04-10 21:37:48 +0200180 outfloathigh = vmulq_s32(outfloathigh, oneovernine);
181 outfloatlow = vmulq_s32(outfloatlow, oneovernine);
182 outfloathigh = vshrq_n_s32(outfloathigh, shift);
183 outfloatlow = vshrq_n_s32(outfloatlow, shift);
184 out = vcombine_s16(vqmovn_s32((outfloatlow)),
185 vqmovn_s32((outfloathigh)));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100186
187 vst1_u8(output.ptr(), vqmovun_s16(out));
188 },
189 input, output);
190}