Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016-2019 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | |
| 38 | using namespace arm_compute; |
| 39 | |
| 40 | NEGaussian5x5HorKernel::NEGaussian5x5HorKernel() |
| 41 | : _border_size(0) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | BorderSize NEGaussian5x5HorKernel::border_size() const |
| 46 | { |
| 47 | return _border_size; |
| 48 | } |
| 49 | |
| 50 | void 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 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 76 | void NEGaussian5x5HorKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 77 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 78 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 79 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 80 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 81 | |
| 82 | Window win_in(window); |
| 83 | win_in.shift(Window::DimX, -2); |
| 84 | |
| 85 | Iterator input(_input, win_in); |
| 86 | Iterator output(_output, window); |
| 87 | |
| 88 | static const int16x8_t six = vdupq_n_s16(6); |
| 89 | static const int16x8_t four = vdupq_n_s16(4); |
| 90 | |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 91 | execute_window_loop(window, [&](const Coordinates &) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 92 | { |
| 93 | uint8x16_t data = vld1q_u8(input.ptr()); |
| 94 | |
| 95 | const int16x8x2_t data_s16 = |
| 96 | { |
| 97 | { |
| 98 | vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(data))), |
| 99 | vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(data))) |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | int16x8_t out = vaddq_s16(data_s16.val[0], vextq_s16(data_s16.val[0], data_s16.val[1], 4)); |
| 104 | out = vmlaq_s16(out, vextq_s16(data_s16.val[0], data_s16.val[1], 1), four); |
| 105 | out = vmlaq_s16(out, vextq_s16(data_s16.val[0], data_s16.val[1], 2), six); |
| 106 | out = vmlaq_s16(out, vextq_s16(data_s16.val[0], data_s16.val[1], 3), four); |
| 107 | |
| 108 | vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()), out); |
| 109 | }, |
| 110 | input, output); |
| 111 | } |
| 112 | |
| 113 | BorderSize NEGaussian5x5VertKernel::border_size() const |
| 114 | { |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 115 | return BorderSize{ 2, 0 }; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void NEGaussian5x5VertKernel::configure(const ITensor *input, ITensor *output, bool border_undefined) |
| 119 | { |
SiCong Li | 3eb263e | 2017-06-19 15:31:43 +0100 | [diff] [blame] | 120 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::S16); |
| 121 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 122 | |
| 123 | _input = input; |
| 124 | _output = output; |
| 125 | |
| 126 | // Configure kernel window |
| 127 | constexpr unsigned int num_elems_processed_per_iteration = 16; |
| 128 | constexpr unsigned int num_elems_read_per_iteration = 32; |
| 129 | constexpr unsigned int num_elems_written_per_iteration = 16; |
| 130 | constexpr unsigned int num_rows_read_per_iteration = 5; |
| 131 | |
| 132 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size()); |
| 133 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration); |
| 134 | |
| 135 | update_window_and_padding(win, |
| 136 | AccessWindowRectangle(input->info(), 0, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration), |
| 137 | output_access); |
| 138 | |
| 139 | output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size()); |
| 140 | |
| 141 | INEKernel::configure(win); |
| 142 | } |
| 143 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 144 | void NEGaussian5x5VertKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 145 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 146 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 147 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 148 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INESimpleKernel::window(), window); |
| 149 | |
| 150 | Iterator input(_input, window); |
| 151 | Iterator output(_output, window); |
| 152 | |
| 153 | const uint8_t *input_top2_ptr = _input->ptr_to_element(Coordinates(0, -2)); |
| 154 | const uint8_t *input_top_ptr = _input->ptr_to_element(Coordinates(0, -1)); |
| 155 | const uint8_t *input_mid_ptr = _input->ptr_to_element(Coordinates(0, 0)); |
| 156 | const uint8_t *input_low_ptr = _input->ptr_to_element(Coordinates(0, 1)); |
| 157 | const uint8_t *input_low2_ptr = _input->ptr_to_element(Coordinates(0, 2)); |
| 158 | |
| 159 | const uint16x8_t six = vdupq_n_u16(6); |
| 160 | const uint16x8_t four = vdupq_n_u16(4); |
| 161 | |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 162 | execute_window_loop(window, [&](const Coordinates &) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 163 | { |
| 164 | const size_t input_offset_high_s16 = input.offset(); |
| 165 | const size_t input_offset_low_s16 = input.offset() + 16; |
| 166 | |
| 167 | //HIGH DATA |
| 168 | //top2 |
| 169 | uint16x8_t data_high = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_top2_ptr + input_offset_high_s16))); |
| 170 | uint16x8_t out_high = data_high; |
| 171 | //top |
| 172 | data_high = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_top_ptr + input_offset_high_s16))); |
| 173 | out_high = vmlaq_u16(out_high, data_high, four); |
| 174 | //mid |
| 175 | data_high = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_mid_ptr + input_offset_high_s16))); |
| 176 | out_high = vmlaq_u16(out_high, data_high, six); |
| 177 | //low |
| 178 | data_high = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_low_ptr + input_offset_high_s16))); |
| 179 | out_high = vmlaq_u16(out_high, data_high, four); |
| 180 | //low2 |
| 181 | data_high = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_low2_ptr + input_offset_high_s16))); |
| 182 | out_high = vaddq_u16(out_high, data_high); |
| 183 | |
| 184 | //LOW DATA |
| 185 | //top2 |
| 186 | uint16x8_t data_low = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_top2_ptr + input_offset_low_s16))); |
| 187 | uint16x8_t out_low = data_low; |
| 188 | //top |
| 189 | data_low = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_top_ptr + input_offset_low_s16))); |
| 190 | out_low = vmlaq_u16(out_low, data_low, four); |
| 191 | //mid |
| 192 | data_low = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_mid_ptr + input_offset_low_s16))); |
| 193 | out_low = vmlaq_u16(out_low, data_low, six); |
| 194 | //low |
| 195 | data_low = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_low_ptr + input_offset_low_s16))); |
| 196 | out_low = vmlaq_u16(out_low, data_low, four); |
| 197 | //low2 |
| 198 | data_low = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_low2_ptr + input_offset_low_s16))); |
| 199 | out_low = vaddq_u16(out_low, data_low); |
| 200 | |
| 201 | vst1q_u8(output.ptr(), vcombine_u8(vqshrn_n_u16(out_high, 8), |
| 202 | vqshrn_n_u16(out_low, 8))); |
| 203 | }, |
| 204 | input, output); |
| 205 | } |