arm_compute v18.05
[platform/upstream/armcl.git] / src / core / NEON / kernels / NEPixelWiseMultiplicationKernel.cpp
1 /*
2  * Copyright (c) 2016-2018 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
34 #include <arm_neon.h>
35 #include <climits>
36 #include <cmath>
37 #include <cstdint>
38 #include <cstdlib>
39
40 #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
41 #include <arm_fp16.h> // needed for float16_t
42 #endif                /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
43
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
57 constexpr unsigned int num_elems_processed_per_iteration = 16;
58
59 inline Status validate_arguments(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, float scale, ConvertPolicy overflow_policy, RoundingPolicy rounding_policy)
60 {
61     ARM_COMPUTE_UNUSED(overflow_policy);
62     ARM_COMPUTE_UNUSED(rounding_policy);
63
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
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
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
99     return Status{};
100 }
101
102 inline std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output)
103 {
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     }
129
130     // Configure kernel window
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);
137     AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
138
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);
142
143     output_access.set_valid_region(win, valid_region);
144
145     Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
146     return std::make_pair(err, win);
147 }
148
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 {
225     const auto output = static_cast<qint8_t *__restrict>(output_ptr);
226
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));
229
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));
236
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     }
275 }
276
277 template <bool is_scale255, bool is_sat>
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 {
280     const qint16x8x2_t ta1 = vld2q_qs16(static_cast<const qint16_t *__restrict>(input1_ptr));
281     qint16x8x2_t       res = vld2q_qs16(static_cast<const qint16_t *__restrict>(input2_ptr));
282
283     if(is_scale255)
284     {
285         const float32x4x2_t scale255_f32 =
286         {
287             {
288                 scale255_constant_f32q,
289                 scale255_constant_f32q
290             }
291         };
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         }
303     }
304     else
305     {
306         const qint16x8_t vn = vdupq_n_s16(-n);
307         if(is_sat)
308         {
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         }
317     }
318     vst2q_s16(static_cast<qint16_t *__restrict>(output_ptr), res);
319 }
320
321 template <bool is_scale255, bool is_sat>
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>
427 void mul_F16_F16_F16_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, float scale)
428 {
429 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
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);
444 #else  /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
445     ARM_COMPUTE_UNUSED(input1_ptr);
446     ARM_COMPUTE_UNUSED(input2_ptr);
447     ARM_COMPUTE_UNUSED(output_ptr);
448     ARM_COMPUTE_UNUSED(scale);
449     ARM_COMPUTE_ERROR("Not supported. Recompile the library with arch=arm64-v8.2-a.");
450 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
451 }
452
453 template <bool is_scale255, bool is_sat>
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 {
538     ARM_COMPUTE_UNUSED(rounding_policy);
539     ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
540
541     ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input1->info(), input2->info(), output->info(), scale, overflow_policy, rounding_policy));
542
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
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     {
560         is_scale_255 = true;
561     }
562     else
563     {
564         int exponent = 0;
565
566         std::frexp(scale, &exponent);
567
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);
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     }
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     }
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     }
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
670     INEKernel::configure(win_config.second);
671 }
672
673 Status NEPixelWiseMultiplicationKernel::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, float scale, ConvertPolicy overflow_policy,
674                                                  RoundingPolicy rounding_policy)
675 {
676     ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
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);
679
680     return Status{};
681 }
682
683 void NEPixelWiseMultiplicationKernel::run(const Window &window, const ThreadInfo &info)
684 {
685     ARM_COMPUTE_UNUSED(info);
686     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
687     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
688
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);
716
717     if(_func_int != nullptr)
718     {
719         execute_window_loop(collapsed, [&](const Coordinates & id)
720         {
721             (*_func_int)(input1.ptr(), input2.ptr(), output.ptr(), _scale_exponent);
722             collapsed.slide_window_slice_3D(slice_input1);
723             collapsed.slide_window_slice_3D(slice_input2);
724         },
725         input1, input2, output);
726     }
727     else if(_func_q_int != nullptr)
728     {
729         int fixed_point_position = _input1->info()->fixed_point_position();
730         execute_window_loop(collapsed, [&](const Coordinates & id)
731         {
732             (*_func_q_int)(input1.ptr(), input2.ptr(), output.ptr(), _scale_exponent, fixed_point_position);
733             collapsed.slide_window_slice_3D(slice_input1);
734             collapsed.slide_window_slice_3D(slice_input2);
735         },
736         input1, input2, output);
737     }
738     else
739     {
740         ARM_COMPUTE_ERROR_ON(_func_float == nullptr);
741         execute_window_loop(collapsed, [&](const Coordinates & id)
742         {
743             (*_func_float)(input1.ptr(), input2.ptr(), output.ptr(), _scale);
744             collapsed.slide_window_slice_3D(slice_input1);
745             collapsed.slide_window_slice_3D(slice_input2);
746         },
747         input1, input2, output);
748     }
749 }
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 }