blob: 0c9700526bcfe65b68537d690790aeea50347aa4 [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/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
52 execute_window_loop(window, [&](const Coordinates & id)
53 {
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
58 const float16x8x2_t top_f16 =
59 {
60 {
61 vcvtq_f16_u16(vmovl_u8(vget_low_u8(top_data))),
62 vcvtq_f16_u16(vmovl_u8(vget_high_u8(top_data)))
63 }
64 };
65
66 const float16x8x2_t mid_f16 =
67 {
68 {
69 vcvtq_f16_u16(vmovl_u8(vget_low_u8(mid_data))),
70 vcvtq_f16_u16(vmovl_u8(vget_high_u8(mid_data)))
71 }
72 };
73
74 const float16x8x2_t bot_f16 =
75 {
76 {
77 vcvtq_f16_u16(vmovl_u8(vget_low_u8(bot_data))),
78 vcvtq_f16_u16(vmovl_u8(vget_high_u8(bot_data)))
79 }
80 };
81
82 //top left
83 float16x8_t out = top_f16.val[0];
84 //top mid
85 out = vaddq_f16(out, vextq_f16(top_f16.val[0], top_f16.val[1], 1));
86 //top right
87 out = vaddq_f16(out, vextq_f16(top_f16.val[0], top_f16.val[1], 2));
88 //mid left
89 out = vaddq_f16(out, mid_f16.val[0]);
90 //mid mid
91 out = vaddq_f16(out, vextq_f16(mid_f16.val[0], mid_f16.val[1], 1));
92 //mid right
93 out = vaddq_f16(out, vextq_f16(mid_f16.val[0], mid_f16.val[1], 2));
94 //bot left
95 out = vaddq_f16(out, bot_f16.val[0]);
96 //bot mid
97 out = vaddq_f16(out, vextq_f16(bot_f16.val[0], bot_f16.val[1], 1));
98 //bot right
99 out = vaddq_f16(out, vextq_f16(bot_f16.val[0], bot_f16.val[1], 2));
100
101 out = vmulq_f16(out, oneovernine);
102
103 vst1_u8(output.ptr(), vqmovun_s16(vcvtq_s16_f16(out)));
104 },
105 input, output);
106}
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000107#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108
109BorderSize NEBox3x3Kernel::border_size() const
110{
111 return BorderSize(1);
112}
113
114void NEBox3x3Kernel::configure(const ITensor *input, ITensor *output, bool border_undefined)
115{
116 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
117
118 set_shape_if_empty(*output->info(), input->info()->tensor_shape());
119
120 set_format_if_unknown(*input->info(), Format::U8);
121 set_format_if_unknown(*output->info(), Format::U8);
122
123 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output);
124 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
125 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
126 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
127
128 _input = input;
129 _output = output;
130
131 constexpr unsigned int num_elems_processed_per_iteration = 8;
132 constexpr unsigned int num_elems_read_per_iteration = 16;
133 constexpr unsigned int num_elems_written_per_iteration = 8;
134 constexpr unsigned int num_rows_read_per_iteration = 3;
135 constexpr int rect_offset_xy = -1;
136
137 // Configure kernel window
138 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
139 AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
140
141 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);
142
143 output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
144
145 INEKernel::configure(win);
146}
147
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100148void NEBox3x3Kernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100149{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100150 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100151 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
152 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INESimpleKernel::window(), window);
153
154 Iterator input(_input, window);
155 Iterator output(_output, window);
156
157 unsigned char *const input_top_ptr = _input->ptr_to_element(Coordinates(-1, -1));
158 unsigned char *const input_mid_ptr = _input->ptr_to_element(Coordinates(-1, 0));
159 unsigned char *const input_bot_ptr = _input->ptr_to_element(Coordinates(-1, +1));
160
161 const float32x4_t oneovernine = vdupq_n_f32(1.0f / 9.0f);
162
163 execute_window_loop(window, [&](const Coordinates & id)
164 {
165 const uint8x16_t top_data = vld1q_u8(input_top_ptr + input.offset());
166 const uint8x16_t mid_data = vld1q_u8(input_mid_ptr + input.offset());
167 const uint8x16_t bot_data = vld1q_u8(input_bot_ptr + input.offset());
168
169 const int16x8x2_t top_s16 =
170 {
171 {
172 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(top_data))),
173 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(top_data)))
174 }
175 };
176 const int16x8x2_t mid_s16 =
177 {
178 {
179 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mid_data))),
180 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mid_data)))
181 }
182 };
183 const int16x8x2_t bot_s16 =
184 {
185 {
186 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(bot_data))),
187 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(bot_data)))
188 }
189 };
190
191 //top left
192 int16x8_t out = top_s16.val[0];
193 //top mid
194 out = vaddq_s16(out, vextq_s16(top_s16.val[0], top_s16.val[1], 1));
195 //top right
196 out = vaddq_s16(out, vextq_s16(top_s16.val[0], top_s16.val[1], 2));
197 //mid left
198 out = vaddq_s16(out, mid_s16.val[0]);
199 //mid mid
200 out = vaddq_s16(out, vextq_s16(mid_s16.val[0], mid_s16.val[1], 1));
201 //mid right
202 out = vaddq_s16(out, vextq_s16(mid_s16.val[0], mid_s16.val[1], 2));
203 //bot left
204 out = vaddq_s16(out, bot_s16.val[0]);
205 //bot mid
206 out = vaddq_s16(out, vextq_s16(bot_s16.val[0], bot_s16.val[1], 1));
207 //bot right
208 out = vaddq_s16(out, vextq_s16(bot_s16.val[0], bot_s16.val[1], 2));
209
210 float32x4_t outfloathigh = vcvtq_f32_s32(vmovl_s16(vget_high_s16(out)));
211 float32x4_t outfloatlow = vcvtq_f32_s32(vmovl_s16(vget_low_s16(out)));
212
213 outfloathigh = vmulq_f32(outfloathigh, oneovernine);
214 outfloatlow = vmulq_f32(outfloatlow, oneovernine);
215
216 out = vcombine_s16(vqmovn_s32(vcvtq_s32_f32(outfloatlow)),
217 vqmovn_s32(vcvtq_s32_f32(outfloathigh)));
218
219 vst1_u8(output.ptr(), vqmovun_s16(out));
220 },
221 input, output);
222}