blob: 4e1880d968bed04fb5ff37b29bd858d7f9c2a1bf [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/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/NEON/INEKernel.h"
30#include "arm_compute/core/Types.h"
31#include "arm_compute/core/Validate.h"
32#include "arm_compute/core/Window.h"
33
34#include <arm_neon.h>
35#include <cstddef>
36#include <cstdint>
37
38using namespace arm_compute;
39
40NEGaussian5x5HorKernel::NEGaussian5x5HorKernel()
41 : _border_size(0)
42{
43}
44
45BorderSize NEGaussian5x5HorKernel::border_size() const
46{
47 return _border_size;
48}
49
50void NEGaussian5x5HorKernel::configure(const ITensor *input, ITensor *output, bool border_undefined)
51{
52 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
53 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S16);
54
55 _input = input;
56 _output = output;
57 _border_size = BorderSize(border_undefined ? 0 : 2, 2);
58
59 // Configure kernel window
60 constexpr unsigned int num_elems_processed_per_iteration = 8;
61 constexpr unsigned int num_elems_read_per_iteration = 16;
62 constexpr unsigned int num_elems_written_per_iteration = 8;
63
64 Window win = calculate_max_window_horizontal(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
65 AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
66
67 update_window_and_padding(win,
68 AccessWindowHorizontal(input->info(), -border_size().left, num_elems_read_per_iteration),
69 output_access);
70
71 output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
72
73 INEKernel::configure(win);
74}
75
76void NEGaussian5x5HorKernel::run(const Window &window)
77{
78 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
79 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
80
81 Window win_in(window);
82 win_in.shift(Window::DimX, -2);
83
84 Iterator input(_input, win_in);
85 Iterator output(_output, window);
86
87 static const int16x8_t six = vdupq_n_s16(6);
88 static const int16x8_t four = vdupq_n_s16(4);
89
90 execute_window_loop(window, [&](const Coordinates & id)
91 {
92 uint8x16_t data = vld1q_u8(input.ptr());
93
94 const int16x8x2_t data_s16 =
95 {
96 {
97 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(data))),
98 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(data)))
99 }
100 };
101
102 int16x8_t out = vaddq_s16(data_s16.val[0], vextq_s16(data_s16.val[0], data_s16.val[1], 4));
103 out = vmlaq_s16(out, vextq_s16(data_s16.val[0], data_s16.val[1], 1), four);
104 out = vmlaq_s16(out, vextq_s16(data_s16.val[0], data_s16.val[1], 2), six);
105 out = vmlaq_s16(out, vextq_s16(data_s16.val[0], data_s16.val[1], 3), four);
106
107 vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()), out);
108 },
109 input, output);
110}
111
112BorderSize NEGaussian5x5VertKernel::border_size() const
113{
114 return BorderSize(2, 0);
115}
116
117void NEGaussian5x5VertKernel::configure(const ITensor *input, ITensor *output, bool border_undefined)
118{
SiCong Li3eb263e2017-06-19 15:31:43 +0100119 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::S16);
120 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100121
122 _input = input;
123 _output = output;
124
125 // Configure kernel window
126 constexpr unsigned int num_elems_processed_per_iteration = 16;
127 constexpr unsigned int num_elems_read_per_iteration = 32;
128 constexpr unsigned int num_elems_written_per_iteration = 16;
129 constexpr unsigned int num_rows_read_per_iteration = 5;
130
131 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
132 AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
133
134 update_window_and_padding(win,
135 AccessWindowRectangle(input->info(), 0, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration),
136 output_access);
137
138 output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
139
140 INEKernel::configure(win);
141}
142
143void NEGaussian5x5VertKernel::run(const Window &window)
144{
145 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
146 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INESimpleKernel::window(), window);
147
148 Iterator input(_input, window);
149 Iterator output(_output, window);
150
151 const uint8_t *input_top2_ptr = _input->ptr_to_element(Coordinates(0, -2));
152 const uint8_t *input_top_ptr = _input->ptr_to_element(Coordinates(0, -1));
153 const uint8_t *input_mid_ptr = _input->ptr_to_element(Coordinates(0, 0));
154 const uint8_t *input_low_ptr = _input->ptr_to_element(Coordinates(0, 1));
155 const uint8_t *input_low2_ptr = _input->ptr_to_element(Coordinates(0, 2));
156
157 const uint16x8_t six = vdupq_n_u16(6);
158 const uint16x8_t four = vdupq_n_u16(4);
159
160 execute_window_loop(window, [&](const Coordinates & id)
161 {
162 const size_t input_offset_high_s16 = input.offset();
163 const size_t input_offset_low_s16 = input.offset() + 16;
164
165 //HIGH DATA
166 //top2
167 uint16x8_t data_high = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_top2_ptr + input_offset_high_s16)));
168 uint16x8_t out_high = data_high;
169 //top
170 data_high = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_top_ptr + input_offset_high_s16)));
171 out_high = vmlaq_u16(out_high, data_high, four);
172 //mid
173 data_high = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_mid_ptr + input_offset_high_s16)));
174 out_high = vmlaq_u16(out_high, data_high, six);
175 //low
176 data_high = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_low_ptr + input_offset_high_s16)));
177 out_high = vmlaq_u16(out_high, data_high, four);
178 //low2
179 data_high = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_low2_ptr + input_offset_high_s16)));
180 out_high = vaddq_u16(out_high, data_high);
181
182 //LOW DATA
183 //top2
184 uint16x8_t data_low = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_top2_ptr + input_offset_low_s16)));
185 uint16x8_t out_low = data_low;
186 //top
187 data_low = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_top_ptr + input_offset_low_s16)));
188 out_low = vmlaq_u16(out_low, data_low, four);
189 //mid
190 data_low = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_mid_ptr + input_offset_low_s16)));
191 out_low = vmlaq_u16(out_low, data_low, six);
192 //low
193 data_low = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_low_ptr + input_offset_low_s16)));
194 out_low = vmlaq_u16(out_low, data_low, four);
195 //low2
196 data_low = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_low2_ptr + input_offset_low_s16)));
197 out_low = vaddq_u16(out_low, data_low);
198
199 vst1q_u8(output.ptr(), vcombine_u8(vqshrn_n_u16(out_high, 8),
200 vqshrn_n_u16(out_low, 8)));
201 },
202 input, output);
203}