Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 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/NEScharr3x3Kernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/Coordinates.h" |
| 27 | #include "arm_compute/core/Error.h" |
| 28 | #include "arm_compute/core/Helpers.h" |
| 29 | #include "arm_compute/core/ITensor.h" |
| 30 | #include "arm_compute/core/Types.h" |
| 31 | #include "arm_compute/core/Validate.h" |
| 32 | |
| 33 | #include <arm_neon.h> |
| 34 | #include <cstdint> |
| 35 | |
| 36 | using namespace arm_compute; |
| 37 | |
| 38 | namespace |
| 39 | { |
| 40 | const int16x8_t three = vdupq_n_s16(3); |
| 41 | const int16x8_t minus_three = vdupq_n_s16(-3); |
| 42 | const int16x8_t ten = vdupq_n_s16(10); |
| 43 | const int16x8_t minus_ten = vdupq_n_s16(-10); |
| 44 | |
| 45 | inline int16x8_t scharr_y(const int16x8x2_t &top, const int16x8x2_t &bottom) |
| 46 | { |
| 47 | // Top left |
| 48 | int16x8_t out = vmulq_s16(top.val[0], minus_three); |
| 49 | // Top center |
| 50 | out = vmlaq_s16(out, vextq_s16(top.val[0], top.val[1], 1), minus_ten); |
| 51 | // Top right |
| 52 | out = vmlaq_s16(out, vextq_s16(top.val[0], top.val[1], 2), minus_three); |
| 53 | |
| 54 | // Bottom left |
| 55 | out = vmlaq_s16(out, bottom.val[0], three); |
| 56 | // Bottom center |
| 57 | out = vmlaq_s16(out, vextq_s16(bottom.val[0], bottom.val[1], 1), ten); |
| 58 | // Bottom right |
| 59 | out = vmlaq_s16(out, vextq_s16(bottom.val[0], bottom.val[1], 2), three); |
| 60 | |
| 61 | return out; |
| 62 | } |
| 63 | |
| 64 | inline int16x8_t scharr_x(const int16x8x2_t &top, const int16x8x2_t &middle, const int16x8x2_t &bottom) |
| 65 | { |
| 66 | // Top left |
| 67 | int16x8_t out = vmulq_s16(top.val[0], minus_three); |
| 68 | // Top right |
| 69 | out = vmlaq_s16(out, vextq_s16(top.val[0], top.val[1], 2), three); |
| 70 | |
| 71 | // Middle left |
| 72 | out = vmlaq_s16(out, middle.val[0], minus_ten); |
| 73 | // Middle right |
| 74 | out = vmlaq_s16(out, vextq_s16(middle.val[0], middle.val[1], 2), ten); |
| 75 | |
| 76 | // Bottom left |
| 77 | out = vmlaq_s16(out, bottom.val[0], minus_three); |
| 78 | // Bottom right |
| 79 | out = vmlaq_s16(out, vextq_s16(bottom.val[0], bottom.val[1], 2), three); |
| 80 | |
| 81 | return out; |
| 82 | } |
| 83 | } // namespace |
| 84 | |
| 85 | NEScharr3x3Kernel::NEScharr3x3Kernel() |
| 86 | : _run_scharr_x(false), _run_scharr_y(false), _input(nullptr), _output_x(nullptr), _output_y(nullptr) |
| 87 | { |
| 88 | } |
| 89 | |
| 90 | void NEScharr3x3Kernel::configure(const ITensor *input, ITensor *output_x, ITensor *output_y, bool border_undefined) |
| 91 | { |
| 92 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8); |
| 93 | ARM_COMPUTE_ERROR_ON((output_x == nullptr) && (output_y == nullptr)); |
| 94 | |
| 95 | _run_scharr_x = output_x != nullptr; |
| 96 | _run_scharr_y = output_y != nullptr; |
| 97 | |
| 98 | if(_run_scharr_x) |
| 99 | { |
| 100 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output_x, 1, DataType::S16); |
| 101 | } |
| 102 | |
| 103 | if(_run_scharr_y) |
| 104 | { |
| 105 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output_y, 1, DataType::S16); |
| 106 | } |
| 107 | |
| 108 | _input = input; |
| 109 | _output_x = output_x; |
| 110 | _output_y = output_y; |
| 111 | |
| 112 | // Configure kernel window |
| 113 | constexpr unsigned int num_elems_processed_per_iteration = 8; |
| 114 | constexpr unsigned int num_elems_read_per_iteration = 16; |
| 115 | constexpr unsigned int num_elems_written_per_iteration = 8; |
| 116 | constexpr unsigned int num_rows_read_per_iteration = 3; |
| 117 | |
| 118 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size()); |
| 119 | AccessWindowHorizontal output_x_access(output_x == nullptr ? nullptr : output_x->info(), 0, num_elems_written_per_iteration); |
| 120 | AccessWindowHorizontal output_y_access(output_y == nullptr ? nullptr : output_y->info(), 0, num_elems_written_per_iteration); |
| 121 | |
| 122 | update_window_and_padding(win, |
| 123 | AccessWindowRectangle(input->info(), -border_size().left, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration), |
| 124 | output_x_access, |
| 125 | output_y_access); |
| 126 | |
| 127 | output_x_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size()); |
| 128 | output_y_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size()); |
| 129 | |
| 130 | INEKernel::configure(win); |
| 131 | } |
| 132 | |
| 133 | BorderSize NEScharr3x3Kernel::border_size() const |
| 134 | { |
| 135 | return BorderSize(1); |
| 136 | } |
| 137 | |
| 138 | void NEScharr3x3Kernel::run(const Window &window) |
| 139 | { |
| 140 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 141 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 142 | |
| 143 | const unsigned char *const input_top_ptr = _input->ptr_to_element(Coordinates(-1, -1)); |
| 144 | const unsigned char *const input_mid_ptr = _input->ptr_to_element(Coordinates(-1, 0)); |
| 145 | const unsigned char *const input_bot_ptr = _input->ptr_to_element(Coordinates(-1, +1)); |
| 146 | |
| 147 | Iterator input(_input, window); |
| 148 | Iterator output_y; |
| 149 | Iterator output_x; |
| 150 | |
| 151 | if(_run_scharr_y) |
| 152 | { |
| 153 | output_y = Iterator(_output_y, window); |
| 154 | } |
| 155 | |
| 156 | if(_run_scharr_x) |
| 157 | { |
| 158 | output_x = Iterator(_output_x, window); |
| 159 | } |
| 160 | |
| 161 | if(_run_scharr_x && _run_scharr_y) |
| 162 | { |
| 163 | execute_window_loop(window, [&](const Coordinates & id) |
| 164 | { |
| 165 | |
| 166 | const uint8x16_t top_data = vld1q_u8(input_top_ptr + input.offset()); |
| 167 | const uint8x16_t mid_data = vld1q_u8(input_mid_ptr + input.offset()); |
| 168 | const uint8x16_t bot_data = vld1q_u8(input_bot_ptr + input.offset()); |
| 169 | |
| 170 | const int16x8x2_t top_s16 = |
| 171 | { |
| 172 | { |
| 173 | vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(top_data))), |
| 174 | vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(top_data))) |
| 175 | } |
| 176 | }; |
| 177 | const int16x8x2_t mid_s16 = |
| 178 | { |
| 179 | { |
| 180 | vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mid_data))), |
| 181 | vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mid_data))) |
| 182 | } |
| 183 | }; |
| 184 | const int16x8x2_t bot_s16 = |
| 185 | { |
| 186 | { |
| 187 | vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(bot_data))), |
| 188 | vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(bot_data))) |
| 189 | } |
| 190 | }; |
| 191 | |
| 192 | vst1q_s16(reinterpret_cast<int16_t *>(output_x.ptr()), scharr_x(top_s16, mid_s16, bot_s16)); |
| 193 | vst1q_s16(reinterpret_cast<int16_t *>(output_y.ptr()), scharr_y(top_s16, bot_s16)); |
| 194 | }, |
| 195 | input, output_x, output_y); |
| 196 | } |
| 197 | else if(_run_scharr_x) |
| 198 | { |
| 199 | execute_window_loop(window, [&](const Coordinates & id) |
| 200 | { |
| 201 | |
| 202 | const uint8x16_t top_data = vld1q_u8(input_top_ptr + input.offset()); |
| 203 | const uint8x16_t mid_data = vld1q_u8(input_mid_ptr + input.offset()); |
| 204 | const uint8x16_t bot_data = vld1q_u8(input_bot_ptr + input.offset()); |
| 205 | |
| 206 | const int16x8x2_t top_s16 = |
| 207 | { |
| 208 | { |
| 209 | vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(top_data))), |
| 210 | vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(top_data))) |
| 211 | } |
| 212 | }; |
| 213 | const int16x8x2_t mid_s16 = |
| 214 | { |
| 215 | { |
| 216 | vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mid_data))), |
| 217 | vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mid_data))) |
| 218 | } |
| 219 | }; |
| 220 | const int16x8x2_t bot_s16 = |
| 221 | { |
| 222 | { |
| 223 | vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(bot_data))), |
| 224 | vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(bot_data))) |
| 225 | } |
| 226 | }; |
| 227 | |
| 228 | vst1q_s16(reinterpret_cast<int16_t *>(output_x.ptr()), scharr_x(top_s16, mid_s16, bot_s16)); |
| 229 | }, |
| 230 | input, output_x); |
| 231 | } |
| 232 | else if(_run_scharr_y) |
| 233 | { |
| 234 | execute_window_loop(window, [&](const Coordinates & id) |
| 235 | { |
| 236 | |
| 237 | const uint8x16_t top_data = vld1q_u8(input_top_ptr + input.offset()); |
| 238 | const uint8x16_t bot_data = vld1q_u8(input_bot_ptr + input.offset()); |
| 239 | |
| 240 | const int16x8x2_t top_s16 = |
| 241 | { |
| 242 | { |
| 243 | vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(top_data))), |
| 244 | vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(top_data))) |
| 245 | } |
| 246 | }; |
| 247 | const int16x8x2_t bot_s16 = |
| 248 | { |
| 249 | { |
| 250 | vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(bot_data))), |
| 251 | vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(bot_data))) |
| 252 | } |
| 253 | }; |
| 254 | |
| 255 | vst1q_s16(reinterpret_cast<int16_t *>(output_y.ptr()), scharr_y(top_s16, bot_s16)); |
| 256 | }, |
| 257 | input, output_y); |
| 258 | } |
| 259 | } |