Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +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/NEBox3x3Kernel.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 | #include <arm_neon.h> |
| 33 | |
| 34 | using namespace arm_compute; |
| 35 | |
Georgios Pinitas | fad1838 | 2019-06-05 15:12:22 +0100 | [diff] [blame] | 36 | int16x8_t calculate_kernel(const uint8x16_t &top_data, const uint8x16_t &mid_data, const uint8x16_t &bot_data) |
| 37 | { |
Alan Kelly | 0eb16cf | 2019-04-28 15:53:14 +0200 | [diff] [blame] | 38 | const int16x8x2_t top_s16 = |
| 39 | { |
| 40 | { |
| 41 | vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(top_data))), |
| 42 | vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(top_data))) |
| 43 | } |
| 44 | }; |
| 45 | const int16x8x2_t mid_s16 = |
| 46 | { |
| 47 | { |
| 48 | vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mid_data))), |
| 49 | vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mid_data))) |
| 50 | } |
| 51 | }; |
| 52 | const int16x8x2_t bot_s16 = |
| 53 | { |
| 54 | { |
| 55 | vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(bot_data))), |
| 56 | vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(bot_data))) |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | //top left |
| 61 | int16x8_t out = top_s16.val[0]; |
| 62 | //top mid |
| 63 | out = vaddq_s16(out, vextq_s16(top_s16.val[0], top_s16.val[1], 1)); |
| 64 | //top right |
| 65 | out = vaddq_s16(out, vextq_s16(top_s16.val[0], top_s16.val[1], 2)); |
| 66 | //mid left |
| 67 | out = vaddq_s16(out, mid_s16.val[0]); |
| 68 | //mid mid |
| 69 | out = vaddq_s16(out, vextq_s16(mid_s16.val[0], mid_s16.val[1], 1)); |
| 70 | //mid right |
| 71 | out = vaddq_s16(out, vextq_s16(mid_s16.val[0], mid_s16.val[1], 2)); |
| 72 | //bot left |
| 73 | out = vaddq_s16(out, bot_s16.val[0]); |
| 74 | //bot mid |
| 75 | out = vaddq_s16(out, vextq_s16(bot_s16.val[0], bot_s16.val[1], 1)); |
| 76 | //bot right |
| 77 | out = vaddq_s16(out, vextq_s16(bot_s16.val[0], bot_s16.val[1], 2)); |
| 78 | return out; |
| 79 | } |
| 80 | |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 81 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 82 | void NEBox3x3FP16Kernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 83 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 84 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 85 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 86 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INESimpleKernel::window(), window); |
| 87 | |
| 88 | Iterator input(_input, window); |
| 89 | Iterator output(_output, window); |
| 90 | |
| 91 | unsigned char *const input_top_ptr = _input->ptr_to_element(Coordinates(-1, -1)); |
| 92 | unsigned char *const input_mid_ptr = _input->ptr_to_element(Coordinates(-1, 0)); |
| 93 | unsigned char *const input_bot_ptr = _input->ptr_to_element(Coordinates(-1, +1)); |
| 94 | |
| 95 | const float16x8_t oneovernine = vdupq_n_f16(1.0f / 9.0f); |
| 96 | |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 97 | execute_window_loop(window, [&](const Coordinates &) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 98 | { |
| 99 | const uint8x16_t top_data = vld1q_u8(input_top_ptr + input.offset()); |
| 100 | const uint8x16_t mid_data = vld1q_u8(input_mid_ptr + input.offset()); |
| 101 | const uint8x16_t bot_data = vld1q_u8(input_bot_ptr + input.offset()); |
| 102 | |
Alan Kelly | 0eb16cf | 2019-04-28 15:53:14 +0200 | [diff] [blame] | 103 | int16x8_t out = calculate_kernel(top_data, mid_data, bot_data); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 104 | |
Georgios Pinitas | fad1838 | 2019-06-05 15:12:22 +0100 | [diff] [blame] | 105 | float16x8_t outfloat = vcvtq_f16_s16(out); |
| 106 | outfloat = vmulq_f16(outfloat, oneovernine); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 107 | |
Alan Kelly | efcefea | 2019-04-10 21:37:05 +0200 | [diff] [blame] | 108 | vst1_u8(output.ptr(), vqmovun_s16(vcvtq_s16_f16(outfloat))); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 109 | }, |
| 110 | input, output); |
| 111 | } |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 112 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 113 | |
| 114 | BorderSize NEBox3x3Kernel::border_size() const |
| 115 | { |
| 116 | return BorderSize(1); |
| 117 | } |
| 118 | |
| 119 | void NEBox3x3Kernel::configure(const ITensor *input, ITensor *output, bool border_undefined) |
| 120 | { |
| 121 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 122 | |
| 123 | set_shape_if_empty(*output->info(), input->info()->tensor_shape()); |
| 124 | |
| 125 | set_format_if_unknown(*input->info(), Format::U8); |
| 126 | set_format_if_unknown(*output->info(), Format::U8); |
| 127 | |
| 128 | ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 129 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8); |
| 130 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8); |
| 131 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 132 | |
| 133 | _input = input; |
| 134 | _output = output; |
| 135 | |
| 136 | constexpr unsigned int num_elems_processed_per_iteration = 8; |
| 137 | constexpr unsigned int num_elems_read_per_iteration = 16; |
| 138 | constexpr unsigned int num_elems_written_per_iteration = 8; |
| 139 | constexpr unsigned int num_rows_read_per_iteration = 3; |
| 140 | constexpr int rect_offset_xy = -1; |
| 141 | |
| 142 | // Configure kernel window |
| 143 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size()); |
| 144 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration); |
| 145 | |
| 146 | update_window_and_padding(win, AccessWindowRectangle(input->info(), rect_offset_xy, rect_offset_xy, num_elems_read_per_iteration, num_rows_read_per_iteration), output_access); |
| 147 | |
| 148 | output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size()); |
| 149 | |
| 150 | INEKernel::configure(win); |
| 151 | } |
| 152 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 153 | void NEBox3x3Kernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 154 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 155 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 156 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 157 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INESimpleKernel::window(), window); |
| 158 | |
| 159 | Iterator input(_input, window); |
| 160 | Iterator output(_output, window); |
| 161 | |
| 162 | unsigned char *const input_top_ptr = _input->ptr_to_element(Coordinates(-1, -1)); |
| 163 | unsigned char *const input_mid_ptr = _input->ptr_to_element(Coordinates(-1, 0)); |
| 164 | unsigned char *const input_bot_ptr = _input->ptr_to_element(Coordinates(-1, +1)); |
| 165 | |
Alan Kelly | a400334 | 2019-04-10 21:37:48 +0200 | [diff] [blame] | 166 | const int shift = 19; |
| 167 | int value = (1 << shift) / 9 + 1; //58255 / (2^19) ~= 1/9 |
| 168 | const int32x4_t oneovernine = vdupq_n_s32(value); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 169 | |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 170 | execute_window_loop(window, [&](const Coordinates &) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 171 | { |
| 172 | const uint8x16_t top_data = vld1q_u8(input_top_ptr + input.offset()); |
| 173 | const uint8x16_t mid_data = vld1q_u8(input_mid_ptr + input.offset()); |
| 174 | const uint8x16_t bot_data = vld1q_u8(input_bot_ptr + input.offset()); |
| 175 | |
Alan Kelly | 0eb16cf | 2019-04-28 15:53:14 +0200 | [diff] [blame] | 176 | int16x8_t out = calculate_kernel(top_data, mid_data, bot_data); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 177 | |
Alan Kelly | a400334 | 2019-04-10 21:37:48 +0200 | [diff] [blame] | 178 | int32x4_t outfloathigh = vmovl_s16(vget_high_s16(out)); |
| 179 | int32x4_t outfloatlow = vmovl_s16(vget_low_s16(out)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 180 | |
Alan Kelly | a400334 | 2019-04-10 21:37:48 +0200 | [diff] [blame] | 181 | outfloathigh = vmulq_s32(outfloathigh, oneovernine); |
| 182 | outfloatlow = vmulq_s32(outfloatlow, oneovernine); |
| 183 | outfloathigh = vshrq_n_s32(outfloathigh, shift); |
| 184 | outfloatlow = vshrq_n_s32(outfloatlow, shift); |
| 185 | out = vcombine_s16(vqmovn_s32((outfloatlow)), |
Georgios Pinitas | fad1838 | 2019-06-05 15:12:22 +0100 | [diff] [blame] | 186 | vqmovn_s32((outfloathigh))); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 187 | |
| 188 | vst1_u8(output.ptr(), vqmovun_s16(out)); |
| 189 | }, |
| 190 | input, output); |
| 191 | } |