blob: 63b26ab7c09f940e1b913afbb57a7e437ce69f15 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +01002 * Copyright (c) 2016-2020 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 */
Michalis Spyrouebcebf12020-10-21 00:04:14 +010024#include "src/core/NEON/kernels/NEGaussian3x3Kernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
26#include "arm_compute/core/Coordinates.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/ITensor.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/core/Validate.h"
Michalis Spyrouebcebf12020-10-21 00:04:14 +010030#include "src/core/NEON/INEKernel.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010031#include "src/core/helpers/AutoConfiguration.h"
32#include "src/core/helpers/WindowHelpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033
34#include <arm_neon.h>
35
36using namespace arm_compute;
37
38BorderSize NEGaussian3x3Kernel::border_size() const
39{
40 return BorderSize(1);
41}
42
43void NEGaussian3x3Kernel::configure(const ITensor *input, ITensor *output, bool border_undefined)
44{
45 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
46 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
47
48 _input = input;
49 _output = output;
50
51 // Configure kernel window
52 constexpr unsigned int num_elems_processed_per_iteration = 8;
53 constexpr unsigned int num_elems_read_per_iteration = 16;
54 constexpr unsigned int num_elems_written_per_iteration = 8;
55 constexpr unsigned int num_rows_read_per_iteration = 3;
56
57 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
58 AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
59
60 update_window_and_padding(win,
61 AccessWindowRectangle(input->info(), -border_size().left, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration),
62 output_access);
63
64 output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
65
66 INEKernel::configure(win);
67}
68
Moritz Pflanzerc186b572017-09-07 09:48:04 +010069void NEGaussian3x3Kernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010070{
Moritz Pflanzerc186b572017-09-07 09:48:04 +010071 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
73 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INESimpleKernel::window(), window);
74
75 Iterator input(_input, window);
76 Iterator output(_output, window);
77
78 const uint8_t *input_bot_ptr = _input->ptr_to_element(Coordinates(-1, -1));
79 const uint8_t *input_mid_ptr = _input->ptr_to_element(Coordinates(-1, 0));
80 const uint8_t *input_top_ptr = _input->ptr_to_element(Coordinates(-1, +1));
81
82 static const int16x8_t two = vdupq_n_s16(2);
83 static const int16x8_t four = vdupq_n_s16(4);
84
Michalis Spyroua4f378d2019-04-26 14:54:54 +010085 execute_window_loop(window, [&](const Coordinates &)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010086 {
87 uint8x16_t top_data = vld1q_u8(input_top_ptr + input.offset());
88 uint8x16_t mid_data = vld1q_u8(input_mid_ptr + input.offset());
89 uint8x16_t bot_data = vld1q_u8(input_bot_ptr + input.offset());
90
91 const int16x8x2_t top_s16 =
92 {
93 {
94 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(top_data))),
95 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(top_data)))
96 }
97 };
98 const int16x8x2_t mid_s16 =
99 {
100 {
101 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mid_data))),
102 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mid_data)))
103 }
104 };
105 const int16x8x2_t bot_s16 =
106 {
107 {
108 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(bot_data))),
109 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(bot_data)))
110 }
111 };
112
113 //top left
114 int16x8_t out = top_s16.val[0];
115 //top mid
116 out = vmlaq_s16(out, vextq_s16(top_s16.val[0], top_s16.val[1], 1), two);
117 //top right
118 out = vaddq_s16(out, vextq_s16(top_s16.val[0], top_s16.val[1], 2));
119 //mid left
120 out = vmlaq_s16(out, mid_s16.val[0], two);
121 //mid mid
122 out = vmlaq_s16(out, vextq_s16(mid_s16.val[0], mid_s16.val[1], 1), four);
123 //mid right
124 out = vmlaq_s16(out, vextq_s16(mid_s16.val[0], mid_s16.val[1], 2), two);
125 //bot left
126 out = vaddq_s16(out, bot_s16.val[0]);
127 //bot mid
128 out = vmlaq_s16(out, vextq_s16(bot_s16.val[0], bot_s16.val[1], 1), two);
129 //bot right
130 out = vaddq_s16(out, vextq_s16(bot_s16.val[0], bot_s16.val[1], 2));
131
132 vst1_u8(output.ptr(), vqshrun_n_s16(out, 4));
133 },
134 input, output);
135}