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