blob: 5bcdc7bc7a8a99e5dc674a9864a4841bff3a8112 [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/NEMedian3x3Kernel.h"
25
26#include "arm_compute/core/Coordinates.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/IAccessWindow.h"
29#include "arm_compute/core/ITensor.h"
30#include "arm_compute/core/NEON/INEKernel.h"
31#include "arm_compute/core/Validate.h"
32
33#include <arm_neon.h>
34#include <utility>
35
36using namespace arm_compute;
37
38namespace
39{
40inline void sort(uint8x8_t &a, uint8x8_t &b)
41{
42 const uint8x8_t min = vmin_u8(a, b);
43 const uint8x8_t max = vmax_u8(a, b);
44 a = min;
45 b = max;
46}
47} // namespace
48
49BorderSize NEMedian3x3Kernel::border_size() const
50{
51 return BorderSize(1);
52}
53
54void NEMedian3x3Kernel::configure(const ITensor *input, ITensor *output, bool border_undefined)
55{
56 _input = input;
57 _output = output;
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 constexpr unsigned int num_rows_read_per_iteration = 3;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010064
65 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
66 AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
67
68 update_window_and_padding(win,
Isabella Gottardi6eb9fca2017-10-11 12:16:57 +010069 AccessWindowRectangle(input->info(), -border_size().left, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration),
Anthony Barbier6ff3b192017-09-04 18:44:23 +010070 output_access);
71
72 output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
73
74 INEKernel::configure(win);
75}
76
Moritz Pflanzerc186b572017-09-07 09:48:04 +010077void NEMedian3x3Kernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078{
Moritz Pflanzerc186b572017-09-07 09:48:04 +010079 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010080 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
81 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INESimpleKernel::window(), window);
82
83 const unsigned char *input_bot_ptr = _input->ptr_to_element(Coordinates(-1, -1));
84 const unsigned char *input_mid_ptr = _input->ptr_to_element(Coordinates(-1, 0));
85 const unsigned char *input_top_ptr = _input->ptr_to_element(Coordinates(-1, +1));
86
87 Iterator input(_input, window);
88 Iterator output(_output, window);
89
90 execute_window_loop(window, [&](const Coordinates & id)
91 {
92 const uint8x16_t top_data = vld1q_u8(input_top_ptr + input.offset());
93 const uint8x16_t mid_data = vld1q_u8(input_mid_ptr + input.offset());
94 const uint8x16_t bot_data = vld1q_u8(input_bot_ptr + input.offset());
95
96 uint8x8_t p0 = vget_low_u8(top_data);
97 uint8x8_t p1 = vext_u8(vget_low_u8(top_data), vget_high_u8(top_data), 1);
98 uint8x8_t p2 = vext_u8(vget_low_u8(top_data), vget_high_u8(top_data), 2);
99 uint8x8_t p3 = vget_low_u8(mid_data);
100 uint8x8_t p4 = vext_u8(vget_low_u8(mid_data), vget_high_u8(mid_data), 1);
101 uint8x8_t p5 = vext_u8(vget_low_u8(mid_data), vget_high_u8(mid_data), 2);
102 uint8x8_t p6 = vget_low_u8(bot_data);
103 uint8x8_t p7 = vext_u8(vget_low_u8(bot_data), vget_high_u8(bot_data), 1);
104 uint8x8_t p8 = vext_u8(vget_low_u8(bot_data), vget_high_u8(bot_data), 2);
105
106 sort(p1, p2);
107 sort(p4, p5);
108 sort(p7, p8);
109
110 sort(p0, p1);
111 sort(p3, p4);
112 sort(p6, p7);
113
114 sort(p1, p2);
115 sort(p4, p5);
116 sort(p7, p8);
117
118 sort(p0, p3);
119 sort(p5, p8);
120 sort(p4, p7);
121
122 sort(p3, p6);
123 sort(p1, p4);
124 sort(p2, p5);
125
126 sort(p4, p7);
127 sort(p4, p2);
128 sort(p6, p4);
129
130 sort(p4, p2);
131
132 vst1_u8(output.ptr(), p4);
133 },
134 input, output);
135}