Compute Library
 21.02
NEGaussian5x5Kernel.cpp
Go to the documentation of this file.
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  */
25 
29 #include "arm_compute/core/Types.h"
35 
36 #include <arm_neon.h>
37 #include <cstddef>
38 #include <cstdint>
39 
40 using namespace arm_compute;
41 
43  : _border_size(0)
44 {
45 }
46 
48 {
49  return _border_size;
50 }
51 
52 void NEGaussian5x5HorKernel::configure(const ITensor *input, ITensor *output, bool border_undefined)
53 {
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 
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 
79 {
80  ARM_COMPUTE_UNUSED(info);
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 
116 {
117 }
118 
120 {
121  return BorderSize{ 2, 0 };
122 }
123 
124 void NEGaussian5x5VertKernel::configure(const ITensor *input, ITensor *output, bool border_undefined)
125 {
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 
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 
151 {
152  ARM_COMPUTE_UNUSED(info);
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 }
unsigned int top
top of the border
Definition: Types.h:375
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
BorderSize border_size() const override
The size of the border for that kernel.
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
void shift(size_t dimension, int shift_value)
Shift the values of a given dimension by the given shift_value.
Definition: Window.inl:133
Container for 2D border size.
Definition: Types.h:273
1 channel, 1 U8 per channel
Interface for Neon tensor.
Definition: ITensor.h:36
Window calculate_max_window_horizontal(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
Copyright (c) 2017-2021 Arm Limited.
virtual ValidRegion valid_region() const =0
Valid region of the tensor.
Implementation of a rectangular access pattern.
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
bool update_window_and_padding(Window &win, Ts &&... patterns)
Update window and padding size for each of the access patterns.
Definition: WindowHelpers.h:46
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
Coordinates of an item.
Definition: Coordinates.h:37
Implementation of a row access pattern.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
Definition: Helpers.inl:139
unsigned int left
left of the border
Definition: Types.h:378
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
1 channel, 1 S16 per channel
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:790
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Information about executing thread and CPU.
Definition: CPPTypes.h:235
unsigned int num_elems_processed_per_iteration
void configure(const ITensor *input, ITensor *output, bool border_undefined)
Initialise the kernel&#39;s source, destination and border mode.
void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators)
Iterate through the passed window, automatically adjusting the iterators and calling the lambda_funct...
Definition: Helpers.inl:77
constexpr size_t offset() const
Return the offset in bytes from the first element to the current position of the iterator.
Definition: Helpers.inl:134
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
void configure(const ITensor *input, ITensor *output, bool border_undefined)
Initialise the kernel&#39;s source, destination and border mode.
NEGaussian5x5HorKernel()
Default constructor.
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
BorderSize border_size() const override
The size of the border for that kernel.
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.