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/NEAbsoluteDifferenceKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.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/TensorInfo.h" |
| 31 | #include "arm_compute/core/Types.h" |
| 32 | #include "arm_compute/core/Validate.h" |
| 33 | |
| 34 | #include <arm_neon.h> |
| 35 | #include <cstdint> |
| 36 | |
| 37 | using namespace arm_compute; |
| 38 | |
| 39 | namespace arm_compute |
| 40 | { |
| 41 | class Coordinates; |
| 42 | } // namespace arm_compute |
| 43 | |
| 44 | namespace |
| 45 | { |
| 46 | void abs_diff_U8_U8_U8(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) |
| 47 | { |
| 48 | Iterator input1(in1, window); |
| 49 | Iterator input2(in2, window); |
| 50 | Iterator output(out, window); |
| 51 | |
| 52 | execute_window_loop(window, [&](const Coordinates & id) |
| 53 | { |
| 54 | const uint8x16_t input1_val = vld1q_u8(input1.ptr()); |
| 55 | const uint8x16_t input2_val = vld1q_u8(input2.ptr()); |
| 56 | |
| 57 | vst1q_u8(output.ptr(), vabdq_u8(input1_val, input2_val)); |
| 58 | }, |
| 59 | input1, input2, output); |
| 60 | } |
| 61 | |
| 62 | inline int16x8x2_t vqabd2q_s16(const int16x8x2_t &v1, const int16x8x2_t &v2) |
| 63 | { |
| 64 | const int16x8x2_t res = |
| 65 | { |
| 66 | { |
| 67 | vqabsq_s16(vqsubq_s16(v1.val[0], v2.val[0])), |
| 68 | vqabsq_s16(vqsubq_s16(v1.val[1], v2.val[1])) |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | return res; |
| 73 | } |
| 74 | |
| 75 | void abs_diff_S16_S16_S16(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) |
| 76 | { |
| 77 | Iterator input1(in1, window); |
| 78 | Iterator input2(in2, window); |
| 79 | Iterator output(out, window); |
| 80 | |
| 81 | execute_window_loop(window, [&](const Coordinates & id) |
| 82 | { |
| 83 | int16x8x2_t input1_val = vld2q_s16(reinterpret_cast<const int16_t *>(input1.ptr())); |
| 84 | int16x8x2_t input2_val = vld2q_s16(reinterpret_cast<const int16_t *>(input2.ptr())); |
| 85 | vst2q_s16(reinterpret_cast<int16_t *>(output.ptr()), vqabd2q_s16(input1_val, input2_val)); |
| 86 | }, |
| 87 | input1, input2, output); |
| 88 | } |
| 89 | |
| 90 | void abs_diff_U8_S16_S16(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) |
| 91 | { |
| 92 | Iterator input1(in1, window); |
| 93 | Iterator input2(in2, window); |
| 94 | Iterator output(out, window); |
| 95 | |
| 96 | execute_window_loop(window, [&](const Coordinates & id) |
| 97 | { |
| 98 | const uint8x16_t input1_val = vld1q_u8(input1.ptr()); |
| 99 | const int16x8x2_t input2_val = |
| 100 | { |
| 101 | { |
| 102 | vld1q_s16(reinterpret_cast<const int16_t *>(input2.ptr())), |
| 103 | vld1q_s16(reinterpret_cast<const int16_t *>(input2.ptr()) + 8) |
| 104 | } |
| 105 | }; |
| 106 | |
| 107 | const int16x8x2_t out_val = |
| 108 | { |
| 109 | { |
| 110 | vqabsq_s16(vqsubq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(input1_val))), input2_val.val[0])), |
| 111 | vqabsq_s16(vqsubq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(input1_val))), input2_val.val[1])) |
| 112 | } |
| 113 | }; |
| 114 | |
| 115 | vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()), out_val.val[0]); |
| 116 | vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()) + 8, out_val.val[1]); |
| 117 | |
| 118 | }, |
| 119 | input1, input2, output); |
| 120 | } |
| 121 | |
| 122 | void abs_diff_S16_U8_S16(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) |
| 123 | { |
| 124 | abs_diff_U8_S16_S16(in2, in1, out, window); |
| 125 | } |
| 126 | } // namespace |
| 127 | |
| 128 | NEAbsoluteDifferenceKernel::NEAbsoluteDifferenceKernel() |
| 129 | : _func(nullptr), _input1(nullptr), _input2(nullptr), _output(nullptr) |
| 130 | { |
| 131 | } |
| 132 | |
| 133 | void NEAbsoluteDifferenceKernel::configure(const ITensor *input1, const ITensor *input2, ITensor *output) |
| 134 | { |
| 135 | ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output); |
| 136 | |
| 137 | set_shape_if_empty(*output->info(), input1->info()->tensor_shape()); |
| 138 | |
| 139 | if(input1->info()->data_type() == DataType::S16 || input2->info()->data_type() == DataType::S16) |
| 140 | { |
| 141 | set_format_if_unknown(*output->info(), Format::S16); |
| 142 | } |
| 143 | else if(input1->info()->data_type() == DataType::F32 || input2->info()->data_type() == DataType::F32) |
| 144 | { |
| 145 | set_format_if_unknown(*output->info(), Format::U8); |
| 146 | } |
| 147 | |
| 148 | ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input1, input2, output); |
| 149 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input1, 1, DataType::U8, DataType::S16); |
| 150 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input2, 1, DataType::U8, DataType::S16); |
| 151 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8, DataType::S16); |
| 152 | ARM_COMPUTE_ERROR_ON_MSG(output->info()->data_type() == DataType::U8 && (input1->info()->data_type() != DataType::U8 || input2->info()->data_type() != DataType::U8), |
| 153 | "The output image can only be U8 if both input images are U8"); |
| 154 | |
| 155 | _input1 = input1; |
| 156 | _input2 = input2; |
| 157 | _output = output; |
| 158 | |
| 159 | const DataType input1_data_type = input1->info()->data_type(); |
| 160 | const DataType input2_data_type = input2->info()->data_type(); |
| 161 | |
| 162 | if(input1_data_type == input2_data_type) |
| 163 | { |
| 164 | if(input1_data_type == DataType::U8) |
| 165 | { |
| 166 | _func = &abs_diff_U8_U8_U8; |
| 167 | } |
| 168 | else |
| 169 | { |
| 170 | _func = &abs_diff_S16_S16_S16; |
| 171 | } |
| 172 | } |
| 173 | else |
| 174 | { |
| 175 | if(input1_data_type == DataType::U8) |
| 176 | { |
| 177 | _func = &abs_diff_U8_S16_S16; |
| 178 | } |
| 179 | else |
| 180 | { |
| 181 | _func = &abs_diff_S16_U8_S16; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | constexpr unsigned int num_elems_processed_per_iteration = 16; |
| 186 | |
| 187 | // Configure kernel window |
| 188 | Window win = calculate_max_window(*input1->info(), Steps(num_elems_processed_per_iteration)); |
| 189 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); |
| 190 | |
| 191 | update_window_and_padding(win, |
| 192 | AccessWindowHorizontal(input1->info(), 0, num_elems_processed_per_iteration), |
| 193 | AccessWindowHorizontal(input2->info(), 0, num_elems_processed_per_iteration), |
| 194 | output_access); |
| 195 | |
| 196 | ValidRegion valid_region = intersect_valid_regions(input1->info()->valid_region(), |
| 197 | input2->info()->valid_region()); |
| 198 | |
| 199 | output_access.set_valid_region(win, valid_region); |
| 200 | |
| 201 | INEKernel::configure(win); |
| 202 | } |
| 203 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 204 | void NEAbsoluteDifferenceKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 205 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 206 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 207 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 208 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 209 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
| 210 | |
| 211 | _func(_input1, _input2, _output, window); |
| 212 | } |