Compute Library
 21.02
NEMedian3x3Kernel.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 
34 
35 #include <arm_neon.h>
36 #include <utility>
37 
38 using namespace arm_compute;
39 
40 namespace
41 {
42 inline void sort(uint8x8_t &a, uint8x8_t &b)
43 {
44  const uint8x8_t min = vmin_u8(a, b);
45  const uint8x8_t max = vmax_u8(a, b);
46  a = min;
47  b = max;
48 }
49 } // namespace
50 
52 {
53  return BorderSize(1);
54 }
55 
56 void NEMedian3x3Kernel::configure(const ITensor *input, ITensor *output, bool border_undefined)
57 {
58  _input = input;
59  _output = output;
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  constexpr unsigned int num_rows_read_per_iteration = 3;
66 
67  Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
68  AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
69 
71  AccessWindowRectangle(input->info(), -border_size().left, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration),
72  output_access);
73 
74  output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
75 
76  INEKernel::configure(win);
77 }
78 
80 {
81  ARM_COMPUTE_UNUSED(info);
84 
85  const unsigned char *input_bot_ptr = _input->ptr_to_element(Coordinates(-1, -1));
86  const unsigned char *input_mid_ptr = _input->ptr_to_element(Coordinates(-1, 0));
87  const unsigned char *input_top_ptr = _input->ptr_to_element(Coordinates(-1, +1));
88 
89  Iterator input(_input, window);
90  Iterator output(_output, window);
91 
92  execute_window_loop(window, [&](const Coordinates &)
93  {
94  const uint8x16_t top_data = vld1q_u8(input_top_ptr + input.offset());
95  const uint8x16_t mid_data = vld1q_u8(input_mid_ptr + input.offset());
96  const uint8x16_t bot_data = vld1q_u8(input_bot_ptr + input.offset());
97 
98  uint8x8_t p0 = vget_low_u8(top_data);
99  uint8x8_t p1 = vext_u8(vget_low_u8(top_data), vget_high_u8(top_data), 1);
100  uint8x8_t p2 = vext_u8(vget_low_u8(top_data), vget_high_u8(top_data), 2);
101  uint8x8_t p3 = vget_low_u8(mid_data);
102  uint8x8_t p4 = vext_u8(vget_low_u8(mid_data), vget_high_u8(mid_data), 1);
103  uint8x8_t p5 = vext_u8(vget_low_u8(mid_data), vget_high_u8(mid_data), 2);
104  uint8x8_t p6 = vget_low_u8(bot_data);
105  uint8x8_t p7 = vext_u8(vget_low_u8(bot_data), vget_high_u8(bot_data), 1);
106  uint8x8_t p8 = vext_u8(vget_low_u8(bot_data), vget_high_u8(bot_data), 2);
107 
108  sort(p1, p2);
109  sort(p4, p5);
110  sort(p7, p8);
111 
112  sort(p0, p1);
113  sort(p3, p4);
114  sort(p6, p7);
115 
116  sort(p1, p2);
117  sort(p4, p5);
118  sort(p7, p8);
119 
120  sort(p0, p3);
121  sort(p5, p8);
122  sort(p4, p7);
123 
124  sort(p3, p6);
125  sort(p1, p4);
126  sort(p2, p5);
127 
128  sort(p4, p7);
129  sort(p4, p2);
130  sort(p6, p4);
131 
132  sort(p4, p2);
133 
134  vst1_u8(output.ptr(), p4);
135  },
136  input, output);
137 }
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)
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
SimpleTensor< float > b
Definition: DFT.cpp:157
Container for 2D border size.
Definition: Types.h:273
BorderSize border_size() const override
The size of the border for that kernel.
Interface for Neon tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2021 Arm Limited.
virtual ValidRegion valid_region() const =0
Valid region of the tensor.
Implementation of a rectangular access pattern.
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
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
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 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
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
void configure(const ITensor *input, ITensor *output, bool border_undefined)
Set the source, destination and border mode of the kernel.