1 /*
2 * Copyright (c) 2016-2020 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 "src/core/NEON/kernels/NEGaussian5x5Kernel.h"
25
26 #include "arm_compute/core/Coordinates.h"
27 #include "arm_compute/core/Helpers.h"
28 #include "arm_compute/core/ITensor.h"
29 #include "arm_compute/core/Types.h"
30 #include "arm_compute/core/Validate.h"
31 #include "arm_compute/core/Window.h"
32 #include "src/core/NEON/INEKernel.h"
33 #include "src/core/helpers/AutoConfiguration.h"
34 #include "src/core/helpers/WindowHelpers.h"
35
36 #include <arm_neon.h>
37 #include <cstddef>
38 #include <cstdint>
39
40 using namespace arm_compute;
41
NEGaussian5x5HorKernel()42 NEGaussian5x5HorKernel::NEGaussian5x5HorKernel()
43 : _border_size(0)
44 {
45 }
46
border_size() const47 BorderSize NEGaussian5x5HorKernel::border_size() const
48 {
49 return _border_size;
50 }
51
configure(const ITensor * input,ITensor * output,bool border_undefined)52 void 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
run(const Window & window,const ThreadInfo & info)78 void NEGaussian5x5HorKernel::run(const Window &window, const ThreadInfo &info)
79 {
80 ARM_COMPUTE_UNUSED(info);
81 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
93 execute_window_loop(window, [&](const Coordinates &)
94 {
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
NEGaussian5x5VertKernel()115 NEGaussian5x5VertKernel::NEGaussian5x5VertKernel()
116 {
117 }
118
border_size() const119 BorderSize NEGaussian5x5VertKernel::border_size() const
120 {
121 return BorderSize{ 2, 0 };
122 }
123
configure(const ITensor * input,ITensor * output,bool border_undefined)124 void NEGaussian5x5VertKernel::configure(const ITensor *input, ITensor *output, bool border_undefined)
125 {
126 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);
128
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
run(const Window & window,const ThreadInfo & info)150 void NEGaussian5x5VertKernel::run(const Window &window, const ThreadInfo &info)
151 {
152 ARM_COMPUTE_UNUSED(info);
153 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
168 execute_window_loop(window, [&](const Coordinates &)
169 {
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 }
212