blob: 19d45e2cb5f8a321c0c4d15ee40c3592a91d43c2 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
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 Tellodf246182017-07-03 16:25:09 +010041#if ARM_COMPUTE_ENABLE_FP16
42#include <arm_fp16.h> // needed for float16_t
43#endif /* ARM_COMPUTE_ENABLE_FP16 */
44
Anthony Barbier6ff3b192017-09-04 18:44:23 +010045using namespace arm_compute;
46
47namespace arm_compute
48{
49class Coordinates;
50} // namespace arm_compute
51
52namespace
53{
54const float scale255_constant = 1.f / 255.f;
55const float32x4_t scale255_constant_f32q = vdupq_n_f32(scale255_constant);
56const 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 */
65inline 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
75inline 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
82template <bool is_scale255, bool is_sat>
83void 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
131template <bool is_scale255, bool is_sat>
132void 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 Barbier6ff3b192017-09-04 18:44:23 +0100134 const auto output = static_cast<qint8_t *__restrict>(output_ptr);
135
Michele Di Giorgio1b80b6c2017-07-17 15:06:34 +0100136 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 Barbier6ff3b192017-09-04 18:44:23 +0100138
Michele Di Giorgio1b80b6c2017-07-17 15:06:34 +0100139 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 Barbier6ff3b192017-09-04 18:44:23 +0100145
Michele Di Giorgio1b80b6c2017-07-17 15:06:34 +0100146 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 Barbier6ff3b192017-09-04 18:44:23 +0100184}
185
186template <bool is_scale255, bool is_sat>
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100187void 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 Giorgio81f0d152017-07-11 15:00:52 +0100189 const qint16x8x2_t ta1 = vld2q_qs16(static_cast<const qint16_t *__restrict>(input1_ptr));
Michele Di Giorgio1b80b6c2017-07-17 15:06:34 +0100190 qint16x8x2_t res = vld2q_qs16(static_cast<const qint16_t *__restrict>(input2_ptr));
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100191
Michele Di Giorgio1b80b6c2017-07-17 15:06:34 +0100192 if(is_scale255)
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100193 {
Michele Di Giorgio1b80b6c2017-07-17 15:06:34 +0100194 const float32x4x2_t scale255_f32 =
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100195 {
196 {
Michele Di Giorgio1b80b6c2017-07-17 15:06:34 +0100197 scale255_constant_f32q,
198 scale255_constant_f32q
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100199 }
200 };
Michele Di Giorgio1b80b6c2017-07-17 15:06:34 +0100201 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 Giorgio81f0d152017-07-11 15:00:52 +0100212 }
213 else
214 {
Michele Di Giorgio1b80b6c2017-07-17 15:06:34 +0100215 const qint16x8_t vn = vdupq_n_s16(-n);
216 if(is_sat)
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100217 {
Michele Di Giorgio1b80b6c2017-07-17 15:06:34 +0100218 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 Giorgio81f0d152017-07-11 15:00:52 +0100226 }
Michele Di Giorgio1b80b6c2017-07-17 15:06:34 +0100227 vst2q_s16(static_cast<qint16_t *__restrict>(output_ptr), res);
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100228}
229
230template <bool is_scale255, bool is_sat>
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100231inline 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
283template <bool is_scale255, bool is_sat>
284inline 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
299template <bool is_scale255, bool is_sat>
300void 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
313template <bool is_scale255, bool is_sat>
314void 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
335template <bool is_scale255, bool is_sat>
Pablo Tellodf246182017-07-03 16:25:09 +0100336void mul_F16_F16_F16_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, float scale)
337{
Pablo Tellodf246182017-07-03 16:25:09 +0100338#ifdef ARM_COMPUTE_ENABLE_FP16
339 const auto input1 = static_cast<const float16_t *__restrict>(input1_ptr);
340 const auto input2 = static_cast<const float16_t *__restrict>(input2_ptr);
341 const auto output = static_cast<float16_t *__restrict>(output_ptr);
342 const float16x8x2_t ta1 = vld2q_f16(input1);
343 const float16x8x2_t ta2 = vld2q_f16(input2);
344 const float16x8_t scale_vec = vdupq_n_f16(scale);
345 const float16x8x2_t result =
346 {
347 {
348 vmulq_f16(vmulq_f16(ta1.val[0], ta2.val[0]), scale_vec),
349 vmulq_f16(vmulq_f16(ta1.val[1], ta2.val[1]), scale_vec),
350 }
351 };
352 vst2q_f16(output, result);
353#else /* ARM_COMPUTE_ENABLE_FP16 */
Georgios Pinitas30f02152017-09-27 11:20:48 +0100354 ARM_COMPUTE_UNUSED(input1_ptr);
355 ARM_COMPUTE_UNUSED(input2_ptr);
356 ARM_COMPUTE_UNUSED(output_ptr);
357 ARM_COMPUTE_UNUSED(scale);
Pablo Tellodf246182017-07-03 16:25:09 +0100358 ARM_COMPUTE_ERROR("Not supported. Recompile the library with arch=arm64-v8.2-a.");
359#endif /* ARM_COMPUTE_ENABLE_FP16 */
360}
361
362template <bool is_scale255, bool is_sat>
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100363void mul_U8_U8_S16_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, int n)
364{
365 const auto input1 = static_cast<const uint8_t *__restrict>(input1_ptr);
366 const auto input2 = static_cast<const uint8_t *__restrict>(input2_ptr);
367 const auto output = static_cast<int16_t *__restrict>(output_ptr);
368
369 const uint8x16_t bv = vld1q_u8(input2);
370 const uint8x16_t av = vld1q_u8(input1);
371
372 uint16x8_t tmp_low = vmovl_u8(vget_low_u8(av));
373 uint16x8_t tmp_high = vmovl_u8(vget_high_u8(av));
374 tmp_low = vmulq_u16(tmp_low, vmovl_u8(vget_low_u8(bv)));
375 tmp_high = vmulq_u16(tmp_high, vmovl_u8(vget_high_u8(bv)));
376
377 if(is_scale255)
378 {
379 tmp_low = scale255_U16_U16(tmp_low);
380 tmp_high = scale255_U16_U16(tmp_high);
381 }
382 else
383 {
384 const int16x8_t vn = vdupq_n_s16(-n);
385
386 if(is_sat)
387 {
388 tmp_low = vqshlq_u16(tmp_low, vn);
389 tmp_high = vqshlq_u16(tmp_high, vn);
390 }
391 else
392 {
393 tmp_low = vshlq_u16(tmp_low, vn);
394 tmp_high = vshlq_u16(tmp_high, vn);
395 }
396 }
397
398 if(is_sat)
399 {
400 static const uint16x8_t max = vdupq_n_u16(SHRT_MAX);
401
402 tmp_low = vminq_u16(tmp_low, max);
403 tmp_high = vminq_u16(tmp_high, max);
404 }
405
406 vst1q_s16(output, vreinterpretq_s16_u16(tmp_low));
407 vst1q_s16(output + 8, vreinterpretq_s16_u16(tmp_high));
408}
409
410template <bool is_scale255, bool is_sat>
411void mul_S16_U8_S16_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, int n)
412{
413 const auto input1 = static_cast<const int16_t *__restrict>(input1_ptr);
414 const auto input2 = static_cast<const uint8_t *__restrict>(input2_ptr);
415 const auto output = static_cast<int16_t *__restrict>(output_ptr);
416
417 const int16x8x2_t ta1 = vld2q_s16(input1);
418 const uint8x8x2_t ta2u = vld2_u8(input2);
419 const int16x8x2_t ta2 =
420 {
421 {
422 vreinterpretq_s16_u16(vmovl_u8(ta2u.val[0])),
423 vreinterpretq_s16_u16(vmovl_u8(ta2u.val[1]))
424 }
425 };
426
427 const int16x8x2_t result = mul_S16_S16_S16_n_k<is_scale255, is_sat>(ta1, ta2, n);
428
429 vst2q_s16(output, result);
430}
431
432template <bool is_scale255, bool is_sat>
433void mul_U8_S16_S16_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, int n)
434{
435 // Simply swap the two input buffers
436 mul_S16_U8_S16_n<is_scale255, is_sat>(input2_ptr, input1_ptr, output_ptr, n);
437}
438} // namespace
439
440NEPixelWiseMultiplicationKernel::NEPixelWiseMultiplicationKernel()
441 : _func_float(nullptr), _func_int(nullptr), _func_q_int(nullptr), _input1(nullptr), _input2(nullptr), _output(nullptr), _scale{ 0 }, _scale_exponent{ 0 }
442{
443}
444
445void NEPixelWiseMultiplicationKernel::configure(const ITensor *input1, const ITensor *input2, ITensor *output, float scale, ConvertPolicy overflow_policy, RoundingPolicy rounding_policy)
446{
Georgios Pinitasf0dea702017-07-03 18:17:28 +0100447 ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
448
449 // Auto initialize output if not initialized
450 {
451 set_shape_if_empty(*output->info(), input1->info()->tensor_shape());
452
453 if(input1->info()->data_type() == DataType::S16 || input2->info()->data_type() == DataType::S16)
454 {
455 set_format_if_unknown(*output->info(), Format::S16);
456 }
457 else if(input1->info()->data_type() == DataType::F32 || input2->info()->data_type() == DataType::F32)
458 {
459 set_format_if_unknown(*output->info(), Format::F32);
460 }
Pablo Tellodf246182017-07-03 16:25:09 +0100461 else if(input1->info()->data_type() == DataType::F16 || input2->info()->data_type() == DataType::F16)
462 {
463 set_format_if_unknown(*output->info(), Format::F16);
464 }
Georgios Pinitasf0dea702017-07-03 18:17:28 +0100465 else if(input1->info()->data_type() == DataType::QS8 && input2->info()->data_type() == DataType::QS8)
466 {
467 set_data_type_if_unknown(*output->info(), DataType::QS8);
468 set_fixed_point_position_if_zero(*output->info(), input1->info()->fixed_point_position());
469 }
470 }
471
472 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input1, input2, output);
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100473 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input1, 1, DataType::U8, DataType::QS8, DataType::QS16, DataType::S16, DataType::F16, DataType::F32);
474 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input2, 1, DataType::U8, DataType::QS8, DataType::QS16, DataType::S16, DataType::F16, DataType::F32);
475 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8, DataType::QS8, DataType::QS16, DataType::S16, DataType::F16, DataType::F32);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100476 ARM_COMPUTE_ERROR_ON_MSG(output->info()->data_type() == DataType::U8 && (input1->info()->data_type() != DataType::U8 || input2->info()->data_type() != DataType::U8),
477 "Output can only be U8 if both inputs are U8");
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100478 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 Barbier6ff3b192017-09-04 18:44:23 +0100479 {
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100480 // Check that all data types are the same and all fixed-point positions are the same
481 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input1, input2, output);
Michele Di Giorgio1b80b6c2017-07-17 15:06:34 +0100482 // Check if scale is representable in fixed-point with the provided settings
483 ARM_COMPUTE_ERROR_ON_VALUE_NOT_REPRESENTABLE_IN_FIXED_POINT(scale, input1);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100484 }
485
486 _input1 = input1;
487 _input2 = input2;
488 _output = output;
489 _scale = scale;
490 _scale_exponent = 0;
491 _func_int = nullptr;
492 _func_q_int = nullptr;
493 _func_float = nullptr;
494
495 bool is_scale_255 = false;
496 // Check and validate scaling factor
497 if(std::abs(scale - scale255_constant) < 0.00001f)
498 {
499 ARM_COMPUTE_ERROR_ON(rounding_policy != RoundingPolicy::TO_NEAREST_UP && rounding_policy != RoundingPolicy::TO_NEAREST_EVEN);
500 ARM_COMPUTE_UNUSED(rounding_policy);
501
502 is_scale_255 = true;
503 }
504 else
505 {
506 ARM_COMPUTE_ERROR_ON(rounding_policy != RoundingPolicy::TO_ZERO);
507 ARM_COMPUTE_UNUSED(rounding_policy);
508
509 int exponent = 0;
510 const float normalized_mantissa = std::frexp(scale, &exponent);
511
512 // Use int scaling if factor is equal to 1/2^n for 0 <= n <= 15
513 // frexp returns 0.5 as mantissa which means that the exponent will be in the range of -1 <= e <= 14
514 // Moreover, it will be negative as we deal with 1/2^n
515 if((normalized_mantissa == 0.5f) && (-14 <= exponent) && (exponent <= 1))
516 {
517 // Store the positive exponent. We know that we compute 1/2^n
518 // Additionally we need to subtract 1 to compensate that frexp used a mantissa of 0.5
519 _scale_exponent = std::abs(exponent - 1);
520 }
521 else
522 {
523 ARM_COMPUTE_ERROR("Scale value not supported (Should be 1/(2^n) or 1/255");
524 }
525 }
526
527 const DataType dt_input1 = input1->info()->data_type();
528 const DataType dt_input2 = input2->info()->data_type();
529 const DataType dt_output = output->info()->data_type();
530 const bool is_sat = (overflow_policy == ConvertPolicy::SATURATE);
531
532 if(DataType::U8 == dt_input1 && DataType::U8 == dt_input2 && DataType::U8 == dt_output)
533 {
534 if(is_scale_255)
535 {
536 _func_int = is_sat ? &mul_U8_U8_U8_n<true, true> : &mul_U8_U8_U8_n<true, false>;
537 }
538 else
539 {
540 _func_int = is_sat ? &mul_U8_U8_U8_n<false, true> : &mul_U8_U8_U8_n<false, false>;
541 }
542 }
543 else if(DataType::S16 == dt_input1 && DataType::S16 == dt_input2 && DataType::S16 == dt_output)
544 {
545 if(is_scale_255)
546 {
547 _func_int = is_sat ? &mul_S16_S16_S16_n<true, true> : &mul_S16_S16_S16_n<true, false>;
548 }
549 else
550 {
551 _func_int = is_sat ? &mul_S16_S16_S16_n<false, true> : &mul_S16_S16_S16_n<false, false>;
552 }
553 }
554 else if(DataType::S16 == dt_input1 && DataType::U8 == dt_input2 && DataType::S16 == dt_output)
555 {
556 if(is_scale_255)
557 {
558 _func_int = is_sat ? &mul_S16_U8_S16_n<true, true> : &mul_S16_U8_S16_n<true, false>;
559 }
560 else
561 {
562 _func_int = is_sat ? &mul_S16_U8_S16_n<false, true> : &mul_S16_U8_S16_n<false, false>;
563 }
564 }
565 else if(DataType::U8 == dt_input1 && DataType::S16 == dt_input2 && DataType::S16 == dt_output)
566 {
567 if(is_scale_255)
568 {
569 _func_int = is_sat ? &mul_U8_S16_S16_n<true, true> : &mul_U8_S16_S16_n<true, false>;
570 }
571 else
572 {
573 _func_int = is_sat ? &mul_U8_S16_S16_n<false, true> : &mul_U8_S16_S16_n<false, false>;
574 }
575 }
576 else if(DataType::U8 == dt_input1 && DataType::U8 == dt_input2 && DataType::S16 == dt_output)
577 {
578 if(is_scale_255)
579 {
580 _func_int = is_sat ? &mul_U8_U8_S16_n<true, true> : &mul_U8_U8_S16_n<true, false>;
581 }
582 else
583 {
584 _func_int = is_sat ? &mul_U8_U8_S16_n<false, true> : &mul_U8_U8_S16_n<false, false>;
585 }
586 }
587 else if(DataType::QS8 == dt_input1 && DataType::QS8 == dt_input2 && DataType::QS8 == dt_output)
588 {
589 if(is_scale_255)
590 {
591 _func_q_int = is_sat ? &mul_QS8_QS8_QS8_n<true, true> : &mul_QS8_QS8_QS8_n<true, false>;
592 }
593 else
594 {
595 _func_q_int = is_sat ? &mul_QS8_QS8_QS8_n<false, true> : &mul_QS8_QS8_QS8_n<false, false>;
596 }
597 }
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100598 else if(DataType::QS16 == dt_input1 && DataType::QS16 == dt_input2 && DataType::QS16 == dt_output)
599 {
600 if(is_scale_255)
601 {
602 _func_q_int = is_sat ? &mul_QS16_QS16_QS16_n<true, true> : &mul_QS16_QS16_QS16_n<true, false>;
603 }
604 else
605 {
606 _func_q_int = is_sat ? &mul_QS16_QS16_QS16_n<false, true> : &mul_QS16_QS16_QS16_n<false, false>;
607 }
608 }
Pablo Tellodf246182017-07-03 16:25:09 +0100609 else if(DataType::F16 == dt_input1 && DataType::F16 == dt_input2 && DataType::F16 == dt_output)
610 {
611 _func_float = &mul_F16_F16_F16_n<false, false>;
612 _func_int = nullptr;
613 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100614 else if(DataType::F32 == dt_input1 && DataType::F32 == dt_input2 && DataType::F32 == dt_output)
615 {
616 _func_float = &mul_F32_F32_F32_n<false, false>;
617 _func_int = nullptr;
618 }
619 else
620 {
621 ARM_COMPUTE_ERROR("You called with the wrong img formats");
622 }
623
624 constexpr unsigned int num_elems_processed_per_iteration = 16;
625
626 // Configure kernel window
627 Window win = calculate_max_window(*input1->info(), Steps(num_elems_processed_per_iteration));
628 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
629
630 update_window_and_padding(win,
631 AccessWindowHorizontal(input1->info(), 0, num_elems_processed_per_iteration),
632 AccessWindowHorizontal(input2->info(), 0, num_elems_processed_per_iteration),
633 output_access);
634
635 ValidRegion valid_region = intersect_valid_regions(input1->info()->valid_region(),
636 input2->info()->valid_region());
637
638 output_access.set_valid_region(win, valid_region);
639
640 INEKernel::configure(win);
641}
642
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100643void NEPixelWiseMultiplicationKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100644{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100645 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100646 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
647 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
648
649 Iterator input1(_input1, window);
650 Iterator input2(_input2, window);
651 Iterator output(_output, window);
652
653 if(_func_int != nullptr)
654 {
655 execute_window_loop(window, [&](const Coordinates & id)
656 {
657 (*_func_int)(input1.ptr(), input2.ptr(), output.ptr(), _scale_exponent);
658 },
659 input1, input2, output);
660 }
661 else if(_func_q_int != nullptr)
662 {
663 int fixed_point_position = _input1->info()->fixed_point_position();
664 execute_window_loop(window, [&](const Coordinates & id)
665 {
666 (*_func_q_int)(input1.ptr(), input2.ptr(), output.ptr(), _scale_exponent, fixed_point_position);
667 },
668 input1, input2, output);
669 }
670 else
671 {
672 ARM_COMPUTE_ERROR_ON(_func_float == nullptr);
673 execute_window_loop(window, [&](const Coordinates & id)
674 {
675 (*_func_float)(input1.ptr(), input2.ptr(), output.ptr(), _scale);
676 },
677 input1, input2, output);
678 }
679}