blob: ab2feb0dc2eccaa1a3c6e9ff0e952c591a9475cd [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +01002 * Copyright (c) 2016-2020 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 */
Michalis Spyrouebcebf12020-10-21 00:04:14 +010024#include "src/core/NEON/kernels/NEGaussian5x5Kernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
26#include "arm_compute/core/Coordinates.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/ITensor.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/core/Types.h"
30#include "arm_compute/core/Validate.h"
31#include "arm_compute/core/Window.h"
Michalis Spyrouebcebf12020-10-21 00:04:14 +010032#include "src/core/NEON/INEKernel.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010033#include "src/core/helpers/AutoConfiguration.h"
34#include "src/core/helpers/WindowHelpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035
36#include <arm_neon.h>
37#include <cstddef>
38#include <cstdint>
39
40using namespace arm_compute;
41
42NEGaussian5x5HorKernel::NEGaussian5x5HorKernel()
43 : _border_size(0)
44{
45}
46
47BorderSize NEGaussian5x5HorKernel::border_size() const
48{
49 return _border_size;
50}
51
52void NEGaussian5x5HorKernel::configure(const ITensor *input, ITensor *output, bool border_undefined)
53{
54 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
55 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S16);
56
57 _input = input;
58 _output = output;
59 _border_size = BorderSize(border_undefined ? 0 : 2, 2);
60
61 // Configure kernel window
62 constexpr unsigned int num_elems_processed_per_iteration = 8;
63 constexpr unsigned int num_elems_read_per_iteration = 16;
64 constexpr unsigned int num_elems_written_per_iteration = 8;
65
66 Window win = calculate_max_window_horizontal(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
67 AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
68
69 update_window_and_padding(win,
70 AccessWindowHorizontal(input->info(), -border_size().left, num_elems_read_per_iteration),
71 output_access);
72
73 output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
74
75 INEKernel::configure(win);
76}
77
Moritz Pflanzerc186b572017-09-07 09:48:04 +010078void NEGaussian5x5HorKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010079{
Moritz Pflanzerc186b572017-09-07 09:48:04 +010080 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010081 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
82 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
83
84 Window win_in(window);
85 win_in.shift(Window::DimX, -2);
86
87 Iterator input(_input, win_in);
88 Iterator output(_output, window);
89
90 static const int16x8_t six = vdupq_n_s16(6);
91 static const int16x8_t four = vdupq_n_s16(4);
92
Michalis Spyroua4f378d2019-04-26 14:54:54 +010093 execute_window_loop(window, [&](const Coordinates &)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094 {
95 uint8x16_t data = vld1q_u8(input.ptr());
96
97 const int16x8x2_t data_s16 =
98 {
99 {
100 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(data))),
101 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(data)))
102 }
103 };
104
105 int16x8_t out = vaddq_s16(data_s16.val[0], vextq_s16(data_s16.val[0], data_s16.val[1], 4));
106 out = vmlaq_s16(out, vextq_s16(data_s16.val[0], data_s16.val[1], 1), four);
107 out = vmlaq_s16(out, vextq_s16(data_s16.val[0], data_s16.val[1], 2), six);
108 out = vmlaq_s16(out, vextq_s16(data_s16.val[0], data_s16.val[1], 3), four);
109
110 vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()), out);
111 },
112 input, output);
113}
114
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100115NEGaussian5x5VertKernel::NEGaussian5x5VertKernel()
116{
117}
118
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100119BorderSize NEGaussian5x5VertKernel::border_size() const
120{
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100121 return BorderSize{ 2, 0 };
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100122}
123
124void NEGaussian5x5VertKernel::configure(const ITensor *input, ITensor *output, bool border_undefined)
125{
SiCong Li3eb263e2017-06-19 15:31:43 +0100126 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::S16);
127 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128
129 _input = input;
130 _output = output;
131
132 // Configure kernel window
133 constexpr unsigned int num_elems_processed_per_iteration = 16;
134 constexpr unsigned int num_elems_read_per_iteration = 32;
135 constexpr unsigned int num_elems_written_per_iteration = 16;
136 constexpr unsigned int num_rows_read_per_iteration = 5;
137
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,
142 AccessWindowRectangle(input->info(), 0, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration),
143 output_access);
144
145 output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
146
147 INEKernel::configure(win);
148}
149
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100150void NEGaussian5x5VertKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100151{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100152 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100153 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
154 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INESimpleKernel::window(), window);
155
156 Iterator input(_input, window);
157 Iterator output(_output, window);
158
159 const uint8_t *input_top2_ptr = _input->ptr_to_element(Coordinates(0, -2));
160 const uint8_t *input_top_ptr = _input->ptr_to_element(Coordinates(0, -1));
161 const uint8_t *input_mid_ptr = _input->ptr_to_element(Coordinates(0, 0));
162 const uint8_t *input_low_ptr = _input->ptr_to_element(Coordinates(0, 1));
163 const uint8_t *input_low2_ptr = _input->ptr_to_element(Coordinates(0, 2));
164
165 const uint16x8_t six = vdupq_n_u16(6);
166 const uint16x8_t four = vdupq_n_u16(4);
167
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100168 execute_window_loop(window, [&](const Coordinates &)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100169 {
170 const size_t input_offset_high_s16 = input.offset();
171 const size_t input_offset_low_s16 = input.offset() + 16;
172
173 //HIGH DATA
174 //top2
175 uint16x8_t data_high = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_top2_ptr + input_offset_high_s16)));
176 uint16x8_t out_high = data_high;
177 //top
178 data_high = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_top_ptr + input_offset_high_s16)));
179 out_high = vmlaq_u16(out_high, data_high, four);
180 //mid
181 data_high = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_mid_ptr + input_offset_high_s16)));
182 out_high = vmlaq_u16(out_high, data_high, six);
183 //low
184 data_high = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_low_ptr + input_offset_high_s16)));
185 out_high = vmlaq_u16(out_high, data_high, four);
186 //low2
187 data_high = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_low2_ptr + input_offset_high_s16)));
188 out_high = vaddq_u16(out_high, data_high);
189
190 //LOW DATA
191 //top2
192 uint16x8_t data_low = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_top2_ptr + input_offset_low_s16)));
193 uint16x8_t out_low = data_low;
194 //top
195 data_low = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_top_ptr + input_offset_low_s16)));
196 out_low = vmlaq_u16(out_low, data_low, four);
197 //mid
198 data_low = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_mid_ptr + input_offset_low_s16)));
199 out_low = vmlaq_u16(out_low, data_low, six);
200 //low
201 data_low = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_low_ptr + input_offset_low_s16)));
202 out_low = vmlaq_u16(out_low, data_low, four);
203 //low2
204 data_low = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_low2_ptr + input_offset_low_s16)));
205 out_low = vaddq_u16(out_low, data_low);
206
207 vst1q_u8(output.ptr(), vcombine_u8(vqshrn_n_u16(out_high, 8),
208 vqshrn_n_u16(out_low, 8)));
209 },
210 input, output);
211}