Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2018 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/NEPixelWiseMultiplicationKernel.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/NEON/NEFixedPoint.h" |
| 31 | #include "arm_compute/core/TensorInfo.h" |
| 32 | #include "arm_compute/core/Validate.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 33 | |
| 34 | #include <arm_neon.h> |
| 35 | #include <climits> |
| 36 | #include <cmath> |
| 37 | #include <cstdint> |
| 38 | #include <cstdlib> |
| 39 | |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 40 | #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 41 | #include <arm_fp16.h> // needed for float16_t |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 42 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 43 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 44 | using namespace arm_compute; |
| 45 | |
| 46 | namespace arm_compute |
| 47 | { |
| 48 | class Coordinates; |
| 49 | } // namespace arm_compute |
| 50 | |
| 51 | namespace |
| 52 | { |
| 53 | const float scale255_constant = 1.f / 255.f; |
| 54 | const float32x4_t scale255_constant_f32q = vdupq_n_f32(scale255_constant); |
| 55 | const float32x4_t positive_round_f32q = vdupq_n_f32(0.5f); |
| 56 | |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 57 | constexpr unsigned int num_elems_processed_per_iteration = 16; |
| 58 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 59 | inline Status validate_arguments(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, float scale, ConvertPolicy overflow_policy, RoundingPolicy rounding_policy) |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 60 | { |
| 61 | ARM_COMPUTE_UNUSED(overflow_policy); |
| 62 | ARM_COMPUTE_UNUSED(rounding_policy); |
| 63 | |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 64 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input1, 1, DataType::U8, DataType::QS8, DataType::QS16, DataType::S16, DataType::F16, DataType::F32); |
| 65 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input2, 1, DataType::U8, DataType::QS8, DataType::QS16, DataType::S16, DataType::F16, DataType::F32); |
| 66 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8, DataType::QS8, DataType::QS16, DataType::S16, DataType::F16, DataType::F32); |
| 67 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->data_type() == DataType::U8 && (input1->data_type() != DataType::U8 || input2->data_type() != DataType::U8), |
| 68 | "Output can only be U8 if both inputs are U8"); |
| 69 | |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 70 | const TensorShape &out_shape = TensorShape::broadcast_shape(input1->tensor_shape(), input2->tensor_shape()); |
| 71 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, output->tensor_shape(), 0), "Wrong shape for output"); |
| 72 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible"); |
| 73 | |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 74 | if(is_data_type_fixed_point(input1->data_type()) || is_data_type_fixed_point(input2->data_type()) || is_data_type_fixed_point(output->data_type())) |
| 75 | { |
| 76 | // Check that all data types are the same and all fixed-point positions are the same |
| 77 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input1, input2, output); |
| 78 | // Check if scale is representable in fixed-point with the provided settings |
| 79 | ARM_COMPUTE_RETURN_ERROR_ON_VALUE_NOT_REPRESENTABLE_IN_FIXED_POINT(scale, input1); |
| 80 | } |
| 81 | |
| 82 | if(std::abs(scale - scale255_constant) < 0.00001f) |
| 83 | { |
| 84 | ARM_COMPUTE_RETURN_ERROR_ON(rounding_policy != RoundingPolicy::TO_NEAREST_UP && rounding_policy != RoundingPolicy::TO_NEAREST_EVEN); |
| 85 | } |
| 86 | else |
| 87 | { |
| 88 | ARM_COMPUTE_RETURN_ERROR_ON(rounding_policy != RoundingPolicy::TO_ZERO); |
| 89 | |
| 90 | int exponent = 0; |
| 91 | const float normalized_mantissa = std::frexp(scale, &exponent); |
| 92 | |
| 93 | // Use int scaling if factor is equal to 1/2^n for 0 <= n <= 15 |
| 94 | // frexp returns 0.5 as mantissa which means that the exponent will be in the range of -1 <= e <= 14 |
| 95 | // Moreover, it will be negative as we deal with 1/2^n |
| 96 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!((normalized_mantissa == 0.5f) && (-14 <= exponent) && (exponent <= 1)), "Scale value not supported (Should be 1/(2^n) or 1/255"); |
| 97 | } |
| 98 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 99 | return Status{}; |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 102 | inline std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output) |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 103 | { |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 104 | const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(*input1, *input2); |
| 105 | const ValidRegion &valid_region = broadcast_pair.second; |
| 106 | |
| 107 | // Auto initialize output if not initialized |
| 108 | { |
| 109 | set_shape_if_empty(*output, input1->tensor_shape()); |
| 110 | |
| 111 | if(input1->data_type() == DataType::S16 || input2->data_type() == DataType::S16) |
| 112 | { |
| 113 | set_format_if_unknown(*output, Format::S16); |
| 114 | } |
| 115 | else if(input1->data_type() == DataType::F32 || input2->data_type() == DataType::F32) |
| 116 | { |
| 117 | set_format_if_unknown(*output, Format::F32); |
| 118 | } |
| 119 | else if(input1->data_type() == DataType::F16 || input2->data_type() == DataType::F16) |
| 120 | { |
| 121 | set_format_if_unknown(*output, Format::F16); |
| 122 | } |
| 123 | else if(input1->data_type() == DataType::QS8 && input2->data_type() == DataType::QS8) |
| 124 | { |
| 125 | set_data_type_if_unknown(*output, DataType::QS8); |
| 126 | set_fixed_point_position_if_zero(*output, input1->fixed_point_position()); |
| 127 | } |
| 128 | } |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 129 | |
| 130 | // Configure kernel window |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 131 | Window win = calculate_max_window(valid_region, Steps(num_elems_processed_per_iteration)); |
| 132 | Window win_input1 = win.broadcast_if_dimension_le_one(*input1); |
| 133 | Window win_input2 = win.broadcast_if_dimension_le_one(*input2); |
| 134 | |
| 135 | AccessWindowHorizontal input1_access(input1, 0, num_elems_processed_per_iteration); |
| 136 | AccessWindowHorizontal input2_access(input2, 0, num_elems_processed_per_iteration); |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 137 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 138 | |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 139 | bool window_changed = update_window_and_padding(win_input1, input1_access) |
| 140 | || update_window_and_padding(win_input2, input2_access) |
| 141 | || update_window_and_padding(win, output_access); |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 142 | |
| 143 | output_access.set_valid_region(win, valid_region); |
| 144 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 145 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 146 | return std::make_pair(err, win); |
| 147 | } |
| 148 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 149 | /* Scales a given vector by 1/255. |
| 150 | * |
| 151 | * @note This does not work for all cases. e.g. for float of 0.49999999999999994 and large floats. |
| 152 | * |
| 153 | * @param in Input vector to scale. |
| 154 | * @return Scaled output rounded to nearest (round half up). |
| 155 | */ |
| 156 | inline int32x4_t scale255_S32_S32(int32x4_t in) |
| 157 | { |
| 158 | // Scale |
| 159 | const float32x4_t tmp = vmulq_f32(vcvtq_f32_s32(in), scale255_constant_f32q); |
| 160 | // Round to nearest (round half up) |
| 161 | // Add +0.5 for all values |
| 162 | // Afterwards vcvt rounds toward zero |
| 163 | return vcvtq_s32_f32(vaddq_f32(tmp, positive_round_f32q)); |
| 164 | } |
| 165 | |
| 166 | inline uint16x8_t scale255_U16_U16(uint16x8_t in) |
| 167 | { |
| 168 | const int32x4_t tmp_s1 = scale255_S32_S32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(in)))); |
| 169 | const int32x4_t tmp_s2 = scale255_S32_S32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(in)))); |
| 170 | return vreinterpretq_u16_s16(vcombine_s16(vmovn_s32(tmp_s2), vmovn_s32(tmp_s1))); |
| 171 | } |
| 172 | |
| 173 | template <bool is_scale255, bool is_sat> |
| 174 | void mul_U8_U8_U8_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, int n) |
| 175 | { |
| 176 | const auto input1 = static_cast<const uint8_t *__restrict>(input1_ptr); |
| 177 | const auto input2 = static_cast<const uint8_t *__restrict>(input2_ptr); |
| 178 | const auto output = static_cast<uint8_t *__restrict>(output_ptr); |
| 179 | |
| 180 | const uint8x16_t ta1 = vld1q_u8(input1); |
| 181 | const uint8x16_t ta2 = vld1q_u8(input2); |
| 182 | |
| 183 | uint16x8_t tmp1_high = vmovl_u8(vget_high_u8(ta1)); |
| 184 | const uint16x8_t tmp2_high = vmovl_u8(vget_high_u8(ta2)); |
| 185 | uint16x8_t tmp1_low = vmovl_u8(vget_low_u8(ta1)); |
| 186 | const uint16x8_t tmp2_low = vmovl_u8(vget_low_u8(ta2)); |
| 187 | |
| 188 | tmp1_high = vmulq_u16(tmp1_high, tmp2_high); |
| 189 | tmp1_low = vmulq_u16(tmp1_low, tmp2_low); |
| 190 | |
| 191 | if(is_scale255) |
| 192 | { |
| 193 | tmp1_high = scale255_U16_U16(tmp1_high); |
| 194 | tmp1_low = scale255_U16_U16(tmp1_low); |
| 195 | } |
| 196 | else |
| 197 | { |
| 198 | const int16x8_t vn = vdupq_n_s16(-n); |
| 199 | |
| 200 | if(is_sat) |
| 201 | { |
| 202 | tmp1_high = vqshlq_u16(tmp1_high, vn); |
| 203 | tmp1_low = vqshlq_u16(tmp1_low, vn); |
| 204 | } |
| 205 | else |
| 206 | { |
| 207 | tmp1_high = vshlq_u16(tmp1_high, vn); |
| 208 | tmp1_low = vshlq_u16(tmp1_low, vn); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | if(is_sat) |
| 213 | { |
| 214 | vst1q_u8(output, vcombine_u8(vqmovn_u16(tmp1_low), vqmovn_u16(tmp1_high))); |
| 215 | } |
| 216 | else |
| 217 | { |
| 218 | vst1q_u8(output, vcombine_u8(vmovn_u16(tmp1_low), vmovn_u16(tmp1_high))); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | template <bool is_scale255, bool is_sat> |
| 223 | void mul_QS8_QS8_QS8_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, int n, int fixed_point_position) |
| 224 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 225 | const auto output = static_cast<qint8_t *__restrict>(output_ptr); |
| 226 | |
Michele Di Giorgio | 1b80b6c | 2017-07-17 15:06:34 +0100 | [diff] [blame] | 227 | const qint8x16_t ta1 = vld1q_qs8(static_cast<const qint8_t *__restrict>(input1_ptr)); |
| 228 | const qint8x16_t ta2 = vld1q_qs8(static_cast<const qint8_t *__restrict>(input2_ptr)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 229 | |
Michele Di Giorgio | 1b80b6c | 2017-07-17 15:06:34 +0100 | [diff] [blame] | 230 | if(is_scale255) |
| 231 | { |
| 232 | qint16x8_t tmp1_high = vmovl_s8(vget_high_s8(ta1)); |
| 233 | qint16x8_t tmp1_low = vmovl_s8(vget_low_s8(ta1)); |
| 234 | const qint16x8_t tmp2_high = vmovl_s8(vget_high_s8(ta2)); |
| 235 | const qint16x8_t tmp2_low = vmovl_s8(vget_low_s8(ta2)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 236 | |
Michele Di Giorgio | 1b80b6c | 2017-07-17 15:06:34 +0100 | [diff] [blame] | 237 | const float32x4x2_t scale255_f32 = |
| 238 | { |
| 239 | { |
| 240 | scale255_constant_f32q, |
| 241 | scale255_constant_f32q |
| 242 | } |
| 243 | }; |
| 244 | const qint16x8_t scale255 = vqcvtq_qs16_f32(scale255_f32, fixed_point_position); |
| 245 | |
| 246 | tmp1_high = vmulq_qs16(tmp1_high, tmp2_high, fixed_point_position); |
| 247 | tmp1_low = vmulq_qs16(tmp1_low, tmp2_low, fixed_point_position); |
| 248 | tmp1_high = vmulq_qs16(tmp1_high, scale255, fixed_point_position); |
| 249 | tmp1_low = vmulq_qs16(tmp1_low, scale255, fixed_point_position); |
| 250 | |
| 251 | if(is_sat) |
| 252 | { |
| 253 | vst1q_qs8(output, vcombine_s8(vqmovn_s16(tmp1_low), vqmovn_s16(tmp1_high))); |
| 254 | } |
| 255 | else |
| 256 | { |
| 257 | vst1q_qs8(output, vcombine_s8(vmovn_s16(tmp1_low), vmovn_s16(tmp1_high))); |
| 258 | } |
| 259 | } |
| 260 | else |
| 261 | { |
| 262 | const qint8x16_t vn = vdupq_n_s8(-n); |
| 263 | qint8x16_t res = ta2; |
| 264 | |
| 265 | if(is_sat) |
| 266 | { |
| 267 | res = vqshlq_s8(vqmulq_qs8(ta1, res, fixed_point_position), vn); |
| 268 | } |
| 269 | else |
| 270 | { |
| 271 | res = vshlq_s8(vmulq_qs8(ta1, res, fixed_point_position), vn); |
| 272 | } |
| 273 | vst1q_qs8(output, res); |
| 274 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | template <bool is_scale255, bool is_sat> |
Michele Di Giorgio | 81f0d15 | 2017-07-11 15:00:52 +0100 | [diff] [blame] | 278 | void mul_QS16_QS16_QS16_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, int n, int fixed_point_position) |
| 279 | { |
Michele Di Giorgio | 81f0d15 | 2017-07-11 15:00:52 +0100 | [diff] [blame] | 280 | const qint16x8x2_t ta1 = vld2q_qs16(static_cast<const qint16_t *__restrict>(input1_ptr)); |
Michele Di Giorgio | 1b80b6c | 2017-07-17 15:06:34 +0100 | [diff] [blame] | 281 | qint16x8x2_t res = vld2q_qs16(static_cast<const qint16_t *__restrict>(input2_ptr)); |
Michele Di Giorgio | 81f0d15 | 2017-07-11 15:00:52 +0100 | [diff] [blame] | 282 | |
Michele Di Giorgio | 1b80b6c | 2017-07-17 15:06:34 +0100 | [diff] [blame] | 283 | if(is_scale255) |
Michele Di Giorgio | 81f0d15 | 2017-07-11 15:00:52 +0100 | [diff] [blame] | 284 | { |
Michele Di Giorgio | 1b80b6c | 2017-07-17 15:06:34 +0100 | [diff] [blame] | 285 | const float32x4x2_t scale255_f32 = |
Michele Di Giorgio | 81f0d15 | 2017-07-11 15:00:52 +0100 | [diff] [blame] | 286 | { |
| 287 | { |
Michele Di Giorgio | 1b80b6c | 2017-07-17 15:06:34 +0100 | [diff] [blame] | 288 | scale255_constant_f32q, |
| 289 | scale255_constant_f32q |
Michele Di Giorgio | 81f0d15 | 2017-07-11 15:00:52 +0100 | [diff] [blame] | 290 | } |
| 291 | }; |
Michele Di Giorgio | 1b80b6c | 2017-07-17 15:06:34 +0100 | [diff] [blame] | 292 | const qint16x8_t scale255 = vqcvtq_qs16_f32(scale255_f32, fixed_point_position); |
| 293 | if(is_sat) |
| 294 | { |
| 295 | res.val[0] = vqmulq_qs16(vqmulq_qs16(ta1.val[0], res.val[0], fixed_point_position), scale255, fixed_point_position); |
| 296 | res.val[1] = vqmulq_qs16(vqmulq_qs16(ta1.val[1], res.val[1], fixed_point_position), scale255, fixed_point_position); |
| 297 | } |
| 298 | else |
| 299 | { |
| 300 | res.val[0] = vmulq_qs16(vmulq_qs16(ta1.val[0], res.val[0], fixed_point_position), scale255, fixed_point_position); |
| 301 | res.val[1] = vmulq_qs16(vmulq_qs16(ta1.val[1], res.val[1], fixed_point_position), scale255, fixed_point_position); |
| 302 | } |
Michele Di Giorgio | 81f0d15 | 2017-07-11 15:00:52 +0100 | [diff] [blame] | 303 | } |
| 304 | else |
| 305 | { |
Michele Di Giorgio | 1b80b6c | 2017-07-17 15:06:34 +0100 | [diff] [blame] | 306 | const qint16x8_t vn = vdupq_n_s16(-n); |
| 307 | if(is_sat) |
Michele Di Giorgio | 81f0d15 | 2017-07-11 15:00:52 +0100 | [diff] [blame] | 308 | { |
Michele Di Giorgio | 1b80b6c | 2017-07-17 15:06:34 +0100 | [diff] [blame] | 309 | res.val[0] = vqshlq_s16(vqmulq_qs16(ta1.val[0], res.val[0], fixed_point_position), vn); |
| 310 | res.val[1] = vqshlq_s16(vqmulq_qs16(ta1.val[1], res.val[1], fixed_point_position), vn); |
| 311 | } |
| 312 | else |
| 313 | { |
| 314 | res.val[0] = vshlq_s16(vmulq_qs16(ta1.val[0], res.val[0], fixed_point_position), vn); |
| 315 | res.val[1] = vshlq_s16(vmulq_qs16(ta1.val[1], res.val[1], fixed_point_position), vn); |
| 316 | } |
Michele Di Giorgio | 81f0d15 | 2017-07-11 15:00:52 +0100 | [diff] [blame] | 317 | } |
Michele Di Giorgio | 1b80b6c | 2017-07-17 15:06:34 +0100 | [diff] [blame] | 318 | vst2q_s16(static_cast<qint16_t *__restrict>(output_ptr), res); |
Michele Di Giorgio | 81f0d15 | 2017-07-11 15:00:52 +0100 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | template <bool is_scale255, bool is_sat> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 322 | inline int16x8_t mul_S16_S16_S16_n_loop(const int16x8_t &input1, const int16x8_t &input2, int n) |
| 323 | { |
| 324 | int32x4_t tmp1_high = vmovl_s16(vget_high_s16(input1)); |
| 325 | const int32x4_t tmp2_high = vmovl_s16(vget_high_s16(input2)); |
| 326 | int32x4_t tmp1_low = vmovl_s16(vget_low_s16(input1)); |
| 327 | const int32x4_t tmp2_low = vmovl_s16(vget_low_s16(input2)); |
| 328 | |
| 329 | tmp1_high = vmulq_s32(tmp1_high, tmp2_high); |
| 330 | tmp1_low = vmulq_s32(tmp1_low, tmp2_low); |
| 331 | |
| 332 | if(is_scale255) |
| 333 | { |
| 334 | tmp1_high = scale255_S32_S32(tmp1_high); |
| 335 | tmp1_low = scale255_S32_S32(tmp1_low); |
| 336 | } |
| 337 | else |
| 338 | { |
| 339 | // Right shift amount |
| 340 | const int32x4_t vn = vdupq_n_s32(-n); |
| 341 | // Left shift amount |
| 342 | const int32x4_t vnl = vdupq_n_s32(n); |
| 343 | // Calculate conversion bit |
| 344 | const uint32x4_t tmp1_high_u = vreinterpretq_u32_s32(tmp1_high); |
| 345 | const uint32x4_t tmp1_low_u = vreinterpretq_u32_s32(tmp1_low); |
| 346 | const uint32x4_t sign_high = vshrq_n_u32(tmp1_high_u, 31); |
| 347 | const uint32x4_t sign_low = vshrq_n_u32(tmp1_low_u, 31); |
| 348 | const int32x4_t sign_high_s = vreinterpretq_s32_u32(sign_high); |
| 349 | const int32x4_t sign_low_s = vreinterpretq_s32_u32(sign_low); |
| 350 | const int32x4_t convert_high = vsubq_s32(vshlq_s32(sign_high_s, vnl), sign_high_s); |
| 351 | const int32x4_t convert_low = vsubq_s32(vshlq_s32(sign_low_s, vnl), sign_low_s); |
| 352 | if(is_sat) |
| 353 | { |
| 354 | tmp1_high = vqshlq_s32(vaddq_s32(tmp1_high, convert_high), vn); |
| 355 | tmp1_low = vqshlq_s32(vaddq_s32(tmp1_low, convert_low), vn); |
| 356 | } |
| 357 | else |
| 358 | { |
| 359 | tmp1_high = vshlq_s32(vaddq_s32(tmp1_high, convert_high), vn); |
| 360 | tmp1_low = vshlq_s32(vaddq_s32(tmp1_low, convert_low), vn); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | if(is_sat) |
| 365 | { |
| 366 | return vcombine_s16(vqmovn_s32(tmp1_low), vqmovn_s32(tmp1_high)); |
| 367 | } |
| 368 | else |
| 369 | { |
| 370 | return vcombine_s16(vmovn_s32(tmp1_low), vmovn_s32(tmp1_high)); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | template <bool is_scale255, bool is_sat> |
| 375 | inline int16x8x2_t mul_S16_S16_S16_n_k(const int16x8x2_t &input1, const int16x8x2_t &input2, int n) |
| 376 | { |
| 377 | const int16x8x2_t result = |
| 378 | { |
| 379 | { |
| 380 | // First 8 elements |
| 381 | mul_S16_S16_S16_n_loop<is_scale255, is_sat>(input1.val[0], input2.val[0], n), |
| 382 | // Second 8 elements |
| 383 | mul_S16_S16_S16_n_loop<is_scale255, is_sat>(input1.val[1], input2.val[1], n) |
| 384 | } |
| 385 | }; |
| 386 | |
| 387 | return result; |
| 388 | } |
| 389 | |
| 390 | template <bool is_scale255, bool is_sat> |
| 391 | void mul_S16_S16_S16_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, int n) |
| 392 | { |
| 393 | const auto input1 = static_cast<const int16_t *__restrict>(input1_ptr); |
| 394 | const auto input2 = static_cast<const int16_t *__restrict>(input2_ptr); |
| 395 | const auto output = static_cast<int16_t *__restrict>(output_ptr); |
| 396 | |
| 397 | const int16x8x2_t ta1 = vld2q_s16(input1); |
| 398 | const int16x8x2_t ta2 = vld2q_s16(input2); |
| 399 | const int16x8x2_t result = mul_S16_S16_S16_n_k<is_scale255, is_sat>(ta1, ta2, n); |
| 400 | |
| 401 | vst2q_s16(output, result); |
| 402 | } |
| 403 | |
| 404 | template <bool is_scale255, bool is_sat> |
| 405 | void mul_F32_F32_F32_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, float scale) |
| 406 | { |
| 407 | const auto input1 = static_cast<const float *__restrict>(input1_ptr); |
| 408 | const auto input2 = static_cast<const float *__restrict>(input2_ptr); |
| 409 | const auto output = static_cast<float *__restrict>(output_ptr); |
| 410 | |
| 411 | const float32x4x4_t ta1 = vld4q_f32(input1); |
| 412 | const float32x4x4_t ta2 = vld4q_f32(input2); |
| 413 | const float32x4_t scale_vec = vdupq_n_f32(scale); |
| 414 | const float32x4x4_t result = |
| 415 | { |
| 416 | { |
| 417 | vmulq_f32(vmulq_f32(ta1.val[0], ta2.val[0]), scale_vec), |
| 418 | vmulq_f32(vmulq_f32(ta1.val[1], ta2.val[1]), scale_vec), |
| 419 | vmulq_f32(vmulq_f32(ta1.val[2], ta2.val[2]), scale_vec), |
| 420 | vmulq_f32(vmulq_f32(ta1.val[3], ta2.val[3]), scale_vec) |
| 421 | } |
| 422 | }; |
| 423 | vst4q_f32(output, result); |
| 424 | } |
| 425 | |
| 426 | template <bool is_scale255, bool is_sat> |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 427 | void mul_F16_F16_F16_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, float scale) |
| 428 | { |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 429 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 430 | const auto input1 = static_cast<const float16_t *__restrict>(input1_ptr); |
| 431 | const auto input2 = static_cast<const float16_t *__restrict>(input2_ptr); |
| 432 | const auto output = static_cast<float16_t *__restrict>(output_ptr); |
| 433 | const float16x8x2_t ta1 = vld2q_f16(input1); |
| 434 | const float16x8x2_t ta2 = vld2q_f16(input2); |
| 435 | const float16x8_t scale_vec = vdupq_n_f16(scale); |
| 436 | const float16x8x2_t result = |
| 437 | { |
| 438 | { |
| 439 | vmulq_f16(vmulq_f16(ta1.val[0], ta2.val[0]), scale_vec), |
| 440 | vmulq_f16(vmulq_f16(ta1.val[1], ta2.val[1]), scale_vec), |
| 441 | } |
| 442 | }; |
| 443 | vst2q_f16(output, result); |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 444 | #else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Georgios Pinitas | 30f0215 | 2017-09-27 11:20:48 +0100 | [diff] [blame] | 445 | ARM_COMPUTE_UNUSED(input1_ptr); |
| 446 | ARM_COMPUTE_UNUSED(input2_ptr); |
| 447 | ARM_COMPUTE_UNUSED(output_ptr); |
| 448 | ARM_COMPUTE_UNUSED(scale); |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 449 | ARM_COMPUTE_ERROR("Not supported. Recompile the library with arch=arm64-v8.2-a."); |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 450 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | template <bool is_scale255, bool is_sat> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 454 | void mul_U8_U8_S16_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, int n) |
| 455 | { |
| 456 | const auto input1 = static_cast<const uint8_t *__restrict>(input1_ptr); |
| 457 | const auto input2 = static_cast<const uint8_t *__restrict>(input2_ptr); |
| 458 | const auto output = static_cast<int16_t *__restrict>(output_ptr); |
| 459 | |
| 460 | const uint8x16_t bv = vld1q_u8(input2); |
| 461 | const uint8x16_t av = vld1q_u8(input1); |
| 462 | |
| 463 | uint16x8_t tmp_low = vmovl_u8(vget_low_u8(av)); |
| 464 | uint16x8_t tmp_high = vmovl_u8(vget_high_u8(av)); |
| 465 | tmp_low = vmulq_u16(tmp_low, vmovl_u8(vget_low_u8(bv))); |
| 466 | tmp_high = vmulq_u16(tmp_high, vmovl_u8(vget_high_u8(bv))); |
| 467 | |
| 468 | if(is_scale255) |
| 469 | { |
| 470 | tmp_low = scale255_U16_U16(tmp_low); |
| 471 | tmp_high = scale255_U16_U16(tmp_high); |
| 472 | } |
| 473 | else |
| 474 | { |
| 475 | const int16x8_t vn = vdupq_n_s16(-n); |
| 476 | |
| 477 | if(is_sat) |
| 478 | { |
| 479 | tmp_low = vqshlq_u16(tmp_low, vn); |
| 480 | tmp_high = vqshlq_u16(tmp_high, vn); |
| 481 | } |
| 482 | else |
| 483 | { |
| 484 | tmp_low = vshlq_u16(tmp_low, vn); |
| 485 | tmp_high = vshlq_u16(tmp_high, vn); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | if(is_sat) |
| 490 | { |
| 491 | static const uint16x8_t max = vdupq_n_u16(SHRT_MAX); |
| 492 | |
| 493 | tmp_low = vminq_u16(tmp_low, max); |
| 494 | tmp_high = vminq_u16(tmp_high, max); |
| 495 | } |
| 496 | |
| 497 | vst1q_s16(output, vreinterpretq_s16_u16(tmp_low)); |
| 498 | vst1q_s16(output + 8, vreinterpretq_s16_u16(tmp_high)); |
| 499 | } |
| 500 | |
| 501 | template <bool is_scale255, bool is_sat> |
| 502 | void mul_S16_U8_S16_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, int n) |
| 503 | { |
| 504 | const auto input1 = static_cast<const int16_t *__restrict>(input1_ptr); |
| 505 | const auto input2 = static_cast<const uint8_t *__restrict>(input2_ptr); |
| 506 | const auto output = static_cast<int16_t *__restrict>(output_ptr); |
| 507 | |
| 508 | const int16x8x2_t ta1 = vld2q_s16(input1); |
| 509 | const uint8x8x2_t ta2u = vld2_u8(input2); |
| 510 | const int16x8x2_t ta2 = |
| 511 | { |
| 512 | { |
| 513 | vreinterpretq_s16_u16(vmovl_u8(ta2u.val[0])), |
| 514 | vreinterpretq_s16_u16(vmovl_u8(ta2u.val[1])) |
| 515 | } |
| 516 | }; |
| 517 | |
| 518 | const int16x8x2_t result = mul_S16_S16_S16_n_k<is_scale255, is_sat>(ta1, ta2, n); |
| 519 | |
| 520 | vst2q_s16(output, result); |
| 521 | } |
| 522 | |
| 523 | template <bool is_scale255, bool is_sat> |
| 524 | void mul_U8_S16_S16_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, int n) |
| 525 | { |
| 526 | // Simply swap the two input buffers |
| 527 | mul_S16_U8_S16_n<is_scale255, is_sat>(input2_ptr, input1_ptr, output_ptr, n); |
| 528 | } |
| 529 | } // namespace |
| 530 | |
| 531 | NEPixelWiseMultiplicationKernel::NEPixelWiseMultiplicationKernel() |
| 532 | : _func_float(nullptr), _func_int(nullptr), _func_q_int(nullptr), _input1(nullptr), _input2(nullptr), _output(nullptr), _scale{ 0 }, _scale_exponent{ 0 } |
| 533 | { |
| 534 | } |
| 535 | |
| 536 | void NEPixelWiseMultiplicationKernel::configure(const ITensor *input1, const ITensor *input2, ITensor *output, float scale, ConvertPolicy overflow_policy, RoundingPolicy rounding_policy) |
| 537 | { |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 538 | ARM_COMPUTE_UNUSED(rounding_policy); |
Georgios Pinitas | f0dea70 | 2017-07-03 18:17:28 +0100 | [diff] [blame] | 539 | ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output); |
| 540 | |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 541 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input1->info(), input2->info(), output->info(), scale, overflow_policy, rounding_policy)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 542 | |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 543 | // Configure kernel window |
| 544 | auto win_config = validate_and_configure_window(input1->info(), input2->info(), output->info()); |
| 545 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 546 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 547 | _input1 = input1; |
| 548 | _input2 = input2; |
| 549 | _output = output; |
| 550 | _scale = scale; |
| 551 | _scale_exponent = 0; |
| 552 | _func_int = nullptr; |
| 553 | _func_q_int = nullptr; |
| 554 | _func_float = nullptr; |
| 555 | |
| 556 | bool is_scale_255 = false; |
| 557 | // Check and validate scaling factor |
| 558 | if(std::abs(scale - scale255_constant) < 0.00001f) |
| 559 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 560 | is_scale_255 = true; |
| 561 | } |
| 562 | else |
| 563 | { |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 564 | int exponent = 0; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 565 | |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 566 | std::frexp(scale, &exponent); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 567 | |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 568 | // Store the positive exponent. We know that we compute 1/2^n |
| 569 | // Additionally we need to subtract 1 to compensate that frexp used a mantissa of 0.5 |
| 570 | _scale_exponent = std::abs(exponent - 1); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | const DataType dt_input1 = input1->info()->data_type(); |
| 574 | const DataType dt_input2 = input2->info()->data_type(); |
| 575 | const DataType dt_output = output->info()->data_type(); |
| 576 | const bool is_sat = (overflow_policy == ConvertPolicy::SATURATE); |
| 577 | |
| 578 | if(DataType::U8 == dt_input1 && DataType::U8 == dt_input2 && DataType::U8 == dt_output) |
| 579 | { |
| 580 | if(is_scale_255) |
| 581 | { |
| 582 | _func_int = is_sat ? &mul_U8_U8_U8_n<true, true> : &mul_U8_U8_U8_n<true, false>; |
| 583 | } |
| 584 | else |
| 585 | { |
| 586 | _func_int = is_sat ? &mul_U8_U8_U8_n<false, true> : &mul_U8_U8_U8_n<false, false>; |
| 587 | } |
| 588 | } |
| 589 | else if(DataType::S16 == dt_input1 && DataType::S16 == dt_input2 && DataType::S16 == dt_output) |
| 590 | { |
| 591 | if(is_scale_255) |
| 592 | { |
| 593 | _func_int = is_sat ? &mul_S16_S16_S16_n<true, true> : &mul_S16_S16_S16_n<true, false>; |
| 594 | } |
| 595 | else |
| 596 | { |
| 597 | _func_int = is_sat ? &mul_S16_S16_S16_n<false, true> : &mul_S16_S16_S16_n<false, false>; |
| 598 | } |
| 599 | } |
| 600 | else if(DataType::S16 == dt_input1 && DataType::U8 == dt_input2 && DataType::S16 == dt_output) |
| 601 | { |
| 602 | if(is_scale_255) |
| 603 | { |
| 604 | _func_int = is_sat ? &mul_S16_U8_S16_n<true, true> : &mul_S16_U8_S16_n<true, false>; |
| 605 | } |
| 606 | else |
| 607 | { |
| 608 | _func_int = is_sat ? &mul_S16_U8_S16_n<false, true> : &mul_S16_U8_S16_n<false, false>; |
| 609 | } |
| 610 | } |
| 611 | else if(DataType::U8 == dt_input1 && DataType::S16 == dt_input2 && DataType::S16 == dt_output) |
| 612 | { |
| 613 | if(is_scale_255) |
| 614 | { |
| 615 | _func_int = is_sat ? &mul_U8_S16_S16_n<true, true> : &mul_U8_S16_S16_n<true, false>; |
| 616 | } |
| 617 | else |
| 618 | { |
| 619 | _func_int = is_sat ? &mul_U8_S16_S16_n<false, true> : &mul_U8_S16_S16_n<false, false>; |
| 620 | } |
| 621 | } |
| 622 | else if(DataType::U8 == dt_input1 && DataType::U8 == dt_input2 && DataType::S16 == dt_output) |
| 623 | { |
| 624 | if(is_scale_255) |
| 625 | { |
| 626 | _func_int = is_sat ? &mul_U8_U8_S16_n<true, true> : &mul_U8_U8_S16_n<true, false>; |
| 627 | } |
| 628 | else |
| 629 | { |
| 630 | _func_int = is_sat ? &mul_U8_U8_S16_n<false, true> : &mul_U8_U8_S16_n<false, false>; |
| 631 | } |
| 632 | } |
| 633 | else if(DataType::QS8 == dt_input1 && DataType::QS8 == dt_input2 && DataType::QS8 == dt_output) |
| 634 | { |
| 635 | if(is_scale_255) |
| 636 | { |
| 637 | _func_q_int = is_sat ? &mul_QS8_QS8_QS8_n<true, true> : &mul_QS8_QS8_QS8_n<true, false>; |
| 638 | } |
| 639 | else |
| 640 | { |
| 641 | _func_q_int = is_sat ? &mul_QS8_QS8_QS8_n<false, true> : &mul_QS8_QS8_QS8_n<false, false>; |
| 642 | } |
| 643 | } |
Michele Di Giorgio | 81f0d15 | 2017-07-11 15:00:52 +0100 | [diff] [blame] | 644 | else if(DataType::QS16 == dt_input1 && DataType::QS16 == dt_input2 && DataType::QS16 == dt_output) |
| 645 | { |
| 646 | if(is_scale_255) |
| 647 | { |
| 648 | _func_q_int = is_sat ? &mul_QS16_QS16_QS16_n<true, true> : &mul_QS16_QS16_QS16_n<true, false>; |
| 649 | } |
| 650 | else |
| 651 | { |
| 652 | _func_q_int = is_sat ? &mul_QS16_QS16_QS16_n<false, true> : &mul_QS16_QS16_QS16_n<false, false>; |
| 653 | } |
| 654 | } |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 655 | else if(DataType::F16 == dt_input1 && DataType::F16 == dt_input2 && DataType::F16 == dt_output) |
| 656 | { |
| 657 | _func_float = &mul_F16_F16_F16_n<false, false>; |
| 658 | _func_int = nullptr; |
| 659 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 660 | else if(DataType::F32 == dt_input1 && DataType::F32 == dt_input2 && DataType::F32 == dt_output) |
| 661 | { |
| 662 | _func_float = &mul_F32_F32_F32_n<false, false>; |
| 663 | _func_int = nullptr; |
| 664 | } |
| 665 | else |
| 666 | { |
| 667 | ARM_COMPUTE_ERROR("You called with the wrong img formats"); |
| 668 | } |
| 669 | |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 670 | INEKernel::configure(win_config.second); |
| 671 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 672 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 673 | Status NEPixelWiseMultiplicationKernel::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, float scale, ConvertPolicy overflow_policy, |
| 674 | RoundingPolicy rounding_policy) |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 675 | { |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 676 | ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output); |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 677 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input1, input2, output, scale, overflow_policy, rounding_policy)); |
| 678 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input1->clone().get(), input2->clone().get(), output->clone().get()).first); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 679 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 680 | return Status{}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 681 | } |
| 682 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 683 | void NEPixelWiseMultiplicationKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 684 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 685 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 686 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 687 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 688 | |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 689 | const TensorShape &in_shape1 = _input1->info()->tensor_shape(); |
| 690 | const TensorShape &in_shape2 = _input2->info()->tensor_shape(); |
| 691 | const TensorShape &out_shape = _output->info()->tensor_shape(); |
| 692 | |
| 693 | bool can_collapse = true; |
| 694 | if(std::min(in_shape1.total_size(), in_shape2.total_size()) > 1) |
| 695 | { |
| 696 | can_collapse = (std::min(in_shape1.num_dimensions(), in_shape2.num_dimensions()) > Window::DimZ); |
| 697 | for(size_t d = Window::DimZ; can_collapse && (d < out_shape.num_dimensions()); ++d) |
| 698 | { |
| 699 | can_collapse = (in_shape1[d] == in_shape2[d]); |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | bool has_collapsed = false; |
| 704 | Window collapsed = can_collapse ? window.collapse_if_possible(INEKernel::window(), Window::DimZ, &has_collapsed) : window; |
| 705 | |
| 706 | const TensorShape &in_shape1_collapsed = has_collapsed ? in_shape1.collapsed_from(Window::DimZ) : in_shape1; |
| 707 | const TensorShape &in_shape2_collapsed = has_collapsed ? in_shape2.collapsed_from(Window::DimZ) : in_shape2; |
| 708 | |
| 709 | Window slice = collapsed.first_slice_window_3D(); |
| 710 | Window slice_input1 = slice.broadcast_if_dimension_le_one(in_shape1_collapsed); |
| 711 | Window slice_input2 = slice.broadcast_if_dimension_le_one(in_shape2_collapsed); |
| 712 | |
| 713 | Iterator input1(_input1, slice_input1); |
| 714 | Iterator input2(_input2, slice_input2); |
| 715 | Iterator output(_output, slice); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 716 | |
| 717 | if(_func_int != nullptr) |
| 718 | { |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 719 | execute_window_loop(collapsed, [&](const Coordinates & id) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 720 | { |
| 721 | (*_func_int)(input1.ptr(), input2.ptr(), output.ptr(), _scale_exponent); |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 722 | collapsed.slide_window_slice_3D(slice_input1); |
| 723 | collapsed.slide_window_slice_3D(slice_input2); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 724 | }, |
| 725 | input1, input2, output); |
| 726 | } |
| 727 | else if(_func_q_int != nullptr) |
| 728 | { |
| 729 | int fixed_point_position = _input1->info()->fixed_point_position(); |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 730 | execute_window_loop(collapsed, [&](const Coordinates & id) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 731 | { |
| 732 | (*_func_q_int)(input1.ptr(), input2.ptr(), output.ptr(), _scale_exponent, fixed_point_position); |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 733 | collapsed.slide_window_slice_3D(slice_input1); |
| 734 | collapsed.slide_window_slice_3D(slice_input2); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 735 | }, |
| 736 | input1, input2, output); |
| 737 | } |
| 738 | else |
| 739 | { |
| 740 | ARM_COMPUTE_ERROR_ON(_func_float == nullptr); |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 741 | execute_window_loop(collapsed, [&](const Coordinates & id) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 742 | { |
| 743 | (*_func_float)(input1.ptr(), input2.ptr(), output.ptr(), _scale); |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 744 | collapsed.slide_window_slice_3D(slice_input1); |
| 745 | collapsed.slide_window_slice_3D(slice_input2); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 746 | }, |
| 747 | input1, input2, output); |
| 748 | } |
| 749 | } |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 750 | |
| 751 | BorderSize NEPixelWiseMultiplicationKernel::border_size() const |
| 752 | { |
| 753 | const unsigned int replicateSize = _output->info()->dimension(0) - std::min(_input1->info()->dimension(0), _input2->info()->dimension(0)); |
| 754 | const unsigned int border = std::min<unsigned int>(num_elems_processed_per_iteration - 1U, replicateSize); |
| 755 | return BorderSize(0, border, 0, 0); |
| 756 | } |