blob: a2f3cffdf34ad3ab322e7e5081976c3f3a1cc1df [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"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033
34#include <arm_neon.h>
35#include <climits>
36#include <cmath>
37#include <cstdint>
38#include <cstdlib>
39
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +000040#if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tellodf246182017-07-03 16:25:09 +010041#include <arm_fp16.h> // needed for float16_t
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +000042#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Pablo Tellodf246182017-07-03 16:25:09 +010043
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044using namespace arm_compute;
45
46namespace arm_compute
47{
48class Coordinates;
49} // namespace arm_compute
50
51namespace
52{
53const float scale255_constant = 1.f / 255.f;
54const float32x4_t scale255_constant_f32q = vdupq_n_f32(scale255_constant);
55const float32x4_t positive_round_f32q = vdupq_n_f32(0.5f);
56
57/* Scales a given vector by 1/255.
58 *
59 * @note This does not work for all cases. e.g. for float of 0.49999999999999994 and large floats.
60 *
61 * @param in Input vector to scale.
62 * @return Scaled output rounded to nearest (round half up).
63 */
64inline int32x4_t scale255_S32_S32(int32x4_t in)
65{
66 // Scale
67 const float32x4_t tmp = vmulq_f32(vcvtq_f32_s32(in), scale255_constant_f32q);
68 // Round to nearest (round half up)
69 // Add +0.5 for all values
70 // Afterwards vcvt rounds toward zero
71 return vcvtq_s32_f32(vaddq_f32(tmp, positive_round_f32q));
72}
73
74inline uint16x8_t scale255_U16_U16(uint16x8_t in)
75{
76 const int32x4_t tmp_s1 = scale255_S32_S32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(in))));
77 const int32x4_t tmp_s2 = scale255_S32_S32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(in))));
78 return vreinterpretq_u16_s16(vcombine_s16(vmovn_s32(tmp_s2), vmovn_s32(tmp_s1)));
79}
80
81template <bool is_scale255, bool is_sat>
82void mul_U8_U8_U8_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, int n)
83{
84 const auto input1 = static_cast<const uint8_t *__restrict>(input1_ptr);
85 const auto input2 = static_cast<const uint8_t *__restrict>(input2_ptr);
86 const auto output = static_cast<uint8_t *__restrict>(output_ptr);
87
88 const uint8x16_t ta1 = vld1q_u8(input1);
89 const uint8x16_t ta2 = vld1q_u8(input2);
90
91 uint16x8_t tmp1_high = vmovl_u8(vget_high_u8(ta1));
92 const uint16x8_t tmp2_high = vmovl_u8(vget_high_u8(ta2));
93 uint16x8_t tmp1_low = vmovl_u8(vget_low_u8(ta1));
94 const uint16x8_t tmp2_low = vmovl_u8(vget_low_u8(ta2));
95
96 tmp1_high = vmulq_u16(tmp1_high, tmp2_high);
97 tmp1_low = vmulq_u16(tmp1_low, tmp2_low);
98
99 if(is_scale255)
100 {
101 tmp1_high = scale255_U16_U16(tmp1_high);
102 tmp1_low = scale255_U16_U16(tmp1_low);
103 }
104 else
105 {
106 const int16x8_t vn = vdupq_n_s16(-n);
107
108 if(is_sat)
109 {
110 tmp1_high = vqshlq_u16(tmp1_high, vn);
111 tmp1_low = vqshlq_u16(tmp1_low, vn);
112 }
113 else
114 {
115 tmp1_high = vshlq_u16(tmp1_high, vn);
116 tmp1_low = vshlq_u16(tmp1_low, vn);
117 }
118 }
119
120 if(is_sat)
121 {
122 vst1q_u8(output, vcombine_u8(vqmovn_u16(tmp1_low), vqmovn_u16(tmp1_high)));
123 }
124 else
125 {
126 vst1q_u8(output, vcombine_u8(vmovn_u16(tmp1_low), vmovn_u16(tmp1_high)));
127 }
128}
129
130template <bool is_scale255, bool is_sat>
131void 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)
132{
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100133 const auto output = static_cast<qint8_t *__restrict>(output_ptr);
134
Michele Di Giorgio1b80b6c2017-07-17 15:06:34 +0100135 const qint8x16_t ta1 = vld1q_qs8(static_cast<const qint8_t *__restrict>(input1_ptr));
136 const qint8x16_t ta2 = vld1q_qs8(static_cast<const qint8_t *__restrict>(input2_ptr));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137
Michele Di Giorgio1b80b6c2017-07-17 15:06:34 +0100138 if(is_scale255)
139 {
140 qint16x8_t tmp1_high = vmovl_s8(vget_high_s8(ta1));
141 qint16x8_t tmp1_low = vmovl_s8(vget_low_s8(ta1));
142 const qint16x8_t tmp2_high = vmovl_s8(vget_high_s8(ta2));
143 const qint16x8_t tmp2_low = vmovl_s8(vget_low_s8(ta2));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100144
Michele Di Giorgio1b80b6c2017-07-17 15:06:34 +0100145 const float32x4x2_t scale255_f32 =
146 {
147 {
148 scale255_constant_f32q,
149 scale255_constant_f32q
150 }
151 };
152 const qint16x8_t scale255 = vqcvtq_qs16_f32(scale255_f32, fixed_point_position);
153
154 tmp1_high = vmulq_qs16(tmp1_high, tmp2_high, fixed_point_position);
155 tmp1_low = vmulq_qs16(tmp1_low, tmp2_low, fixed_point_position);
156 tmp1_high = vmulq_qs16(tmp1_high, scale255, fixed_point_position);
157 tmp1_low = vmulq_qs16(tmp1_low, scale255, fixed_point_position);
158
159 if(is_sat)
160 {
161 vst1q_qs8(output, vcombine_s8(vqmovn_s16(tmp1_low), vqmovn_s16(tmp1_high)));
162 }
163 else
164 {
165 vst1q_qs8(output, vcombine_s8(vmovn_s16(tmp1_low), vmovn_s16(tmp1_high)));
166 }
167 }
168 else
169 {
170 const qint8x16_t vn = vdupq_n_s8(-n);
171 qint8x16_t res = ta2;
172
173 if(is_sat)
174 {
175 res = vqshlq_s8(vqmulq_qs8(ta1, res, fixed_point_position), vn);
176 }
177 else
178 {
179 res = vshlq_s8(vmulq_qs8(ta1, res, fixed_point_position), vn);
180 }
181 vst1q_qs8(output, res);
182 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100183}
184
185template <bool is_scale255, bool is_sat>
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100186void 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)
187{
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100188 const qint16x8x2_t ta1 = vld2q_qs16(static_cast<const qint16_t *__restrict>(input1_ptr));
Michele Di Giorgio1b80b6c2017-07-17 15:06:34 +0100189 qint16x8x2_t res = vld2q_qs16(static_cast<const qint16_t *__restrict>(input2_ptr));
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100190
Michele Di Giorgio1b80b6c2017-07-17 15:06:34 +0100191 if(is_scale255)
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100192 {
Michele Di Giorgio1b80b6c2017-07-17 15:06:34 +0100193 const float32x4x2_t scale255_f32 =
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100194 {
195 {
Michele Di Giorgio1b80b6c2017-07-17 15:06:34 +0100196 scale255_constant_f32q,
197 scale255_constant_f32q
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100198 }
199 };
Michele Di Giorgio1b80b6c2017-07-17 15:06:34 +0100200 const qint16x8_t scale255 = vqcvtq_qs16_f32(scale255_f32, fixed_point_position);
201 if(is_sat)
202 {
203 res.val[0] = vqmulq_qs16(vqmulq_qs16(ta1.val[0], res.val[0], fixed_point_position), scale255, fixed_point_position);
204 res.val[1] = vqmulq_qs16(vqmulq_qs16(ta1.val[1], res.val[1], fixed_point_position), scale255, fixed_point_position);
205 }
206 else
207 {
208 res.val[0] = vmulq_qs16(vmulq_qs16(ta1.val[0], res.val[0], fixed_point_position), scale255, fixed_point_position);
209 res.val[1] = vmulq_qs16(vmulq_qs16(ta1.val[1], res.val[1], fixed_point_position), scale255, fixed_point_position);
210 }
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100211 }
212 else
213 {
Michele Di Giorgio1b80b6c2017-07-17 15:06:34 +0100214 const qint16x8_t vn = vdupq_n_s16(-n);
215 if(is_sat)
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100216 {
Michele Di Giorgio1b80b6c2017-07-17 15:06:34 +0100217 res.val[0] = vqshlq_s16(vqmulq_qs16(ta1.val[0], res.val[0], fixed_point_position), vn);
218 res.val[1] = vqshlq_s16(vqmulq_qs16(ta1.val[1], res.val[1], fixed_point_position), vn);
219 }
220 else
221 {
222 res.val[0] = vshlq_s16(vmulq_qs16(ta1.val[0], res.val[0], fixed_point_position), vn);
223 res.val[1] = vshlq_s16(vmulq_qs16(ta1.val[1], res.val[1], fixed_point_position), vn);
224 }
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100225 }
Michele Di Giorgio1b80b6c2017-07-17 15:06:34 +0100226 vst2q_s16(static_cast<qint16_t *__restrict>(output_ptr), res);
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100227}
228
229template <bool is_scale255, bool is_sat>
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100230inline int16x8_t mul_S16_S16_S16_n_loop(const int16x8_t &input1, const int16x8_t &input2, int n)
231{
232 int32x4_t tmp1_high = vmovl_s16(vget_high_s16(input1));
233 const int32x4_t tmp2_high = vmovl_s16(vget_high_s16(input2));
234 int32x4_t tmp1_low = vmovl_s16(vget_low_s16(input1));
235 const int32x4_t tmp2_low = vmovl_s16(vget_low_s16(input2));
236
237 tmp1_high = vmulq_s32(tmp1_high, tmp2_high);
238 tmp1_low = vmulq_s32(tmp1_low, tmp2_low);
239
240 if(is_scale255)
241 {
242 tmp1_high = scale255_S32_S32(tmp1_high);
243 tmp1_low = scale255_S32_S32(tmp1_low);
244 }
245 else
246 {
247 // Right shift amount
248 const int32x4_t vn = vdupq_n_s32(-n);
249 // Left shift amount
250 const int32x4_t vnl = vdupq_n_s32(n);
251 // Calculate conversion bit
252 const uint32x4_t tmp1_high_u = vreinterpretq_u32_s32(tmp1_high);
253 const uint32x4_t tmp1_low_u = vreinterpretq_u32_s32(tmp1_low);
254 const uint32x4_t sign_high = vshrq_n_u32(tmp1_high_u, 31);
255 const uint32x4_t sign_low = vshrq_n_u32(tmp1_low_u, 31);
256 const int32x4_t sign_high_s = vreinterpretq_s32_u32(sign_high);
257 const int32x4_t sign_low_s = vreinterpretq_s32_u32(sign_low);
258 const int32x4_t convert_high = vsubq_s32(vshlq_s32(sign_high_s, vnl), sign_high_s);
259 const int32x4_t convert_low = vsubq_s32(vshlq_s32(sign_low_s, vnl), sign_low_s);
260 if(is_sat)
261 {
262 tmp1_high = vqshlq_s32(vaddq_s32(tmp1_high, convert_high), vn);
263 tmp1_low = vqshlq_s32(vaddq_s32(tmp1_low, convert_low), vn);
264 }
265 else
266 {
267 tmp1_high = vshlq_s32(vaddq_s32(tmp1_high, convert_high), vn);
268 tmp1_low = vshlq_s32(vaddq_s32(tmp1_low, convert_low), vn);
269 }
270 }
271
272 if(is_sat)
273 {
274 return vcombine_s16(vqmovn_s32(tmp1_low), vqmovn_s32(tmp1_high));
275 }
276 else
277 {
278 return vcombine_s16(vmovn_s32(tmp1_low), vmovn_s32(tmp1_high));
279 }
280}
281
282template <bool is_scale255, bool is_sat>
283inline int16x8x2_t mul_S16_S16_S16_n_k(const int16x8x2_t &input1, const int16x8x2_t &input2, int n)
284{
285 const int16x8x2_t result =
286 {
287 {
288 // First 8 elements
289 mul_S16_S16_S16_n_loop<is_scale255, is_sat>(input1.val[0], input2.val[0], n),
290 // Second 8 elements
291 mul_S16_S16_S16_n_loop<is_scale255, is_sat>(input1.val[1], input2.val[1], n)
292 }
293 };
294
295 return result;
296}
297
298template <bool is_scale255, bool is_sat>
299void mul_S16_S16_S16_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, int n)
300{
301 const auto input1 = static_cast<const int16_t *__restrict>(input1_ptr);
302 const auto input2 = static_cast<const int16_t *__restrict>(input2_ptr);
303 const auto output = static_cast<int16_t *__restrict>(output_ptr);
304
305 const int16x8x2_t ta1 = vld2q_s16(input1);
306 const int16x8x2_t ta2 = vld2q_s16(input2);
307 const int16x8x2_t result = mul_S16_S16_S16_n_k<is_scale255, is_sat>(ta1, ta2, n);
308
309 vst2q_s16(output, result);
310}
311
312template <bool is_scale255, bool is_sat>
313void mul_F32_F32_F32_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, float scale)
314{
315 const auto input1 = static_cast<const float *__restrict>(input1_ptr);
316 const auto input2 = static_cast<const float *__restrict>(input2_ptr);
317 const auto output = static_cast<float *__restrict>(output_ptr);
318
319 const float32x4x4_t ta1 = vld4q_f32(input1);
320 const float32x4x4_t ta2 = vld4q_f32(input2);
321 const float32x4_t scale_vec = vdupq_n_f32(scale);
322 const float32x4x4_t result =
323 {
324 {
325 vmulq_f32(vmulq_f32(ta1.val[0], ta2.val[0]), scale_vec),
326 vmulq_f32(vmulq_f32(ta1.val[1], ta2.val[1]), scale_vec),
327 vmulq_f32(vmulq_f32(ta1.val[2], ta2.val[2]), scale_vec),
328 vmulq_f32(vmulq_f32(ta1.val[3], ta2.val[3]), scale_vec)
329 }
330 };
331 vst4q_f32(output, result);
332}
333
334template <bool is_scale255, bool is_sat>
Pablo Tellodf246182017-07-03 16:25:09 +0100335void mul_F16_F16_F16_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, float scale)
336{
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000337#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tellodf246182017-07-03 16:25:09 +0100338 const auto input1 = static_cast<const float16_t *__restrict>(input1_ptr);
339 const auto input2 = static_cast<const float16_t *__restrict>(input2_ptr);
340 const auto output = static_cast<float16_t *__restrict>(output_ptr);
341 const float16x8x2_t ta1 = vld2q_f16(input1);
342 const float16x8x2_t ta2 = vld2q_f16(input2);
343 const float16x8_t scale_vec = vdupq_n_f16(scale);
344 const float16x8x2_t result =
345 {
346 {
347 vmulq_f16(vmulq_f16(ta1.val[0], ta2.val[0]), scale_vec),
348 vmulq_f16(vmulq_f16(ta1.val[1], ta2.val[1]), scale_vec),
349 }
350 };
351 vst2q_f16(output, result);
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000352#else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Georgios Pinitas30f02152017-09-27 11:20:48 +0100353 ARM_COMPUTE_UNUSED(input1_ptr);
354 ARM_COMPUTE_UNUSED(input2_ptr);
355 ARM_COMPUTE_UNUSED(output_ptr);
356 ARM_COMPUTE_UNUSED(scale);
Pablo Tellodf246182017-07-03 16:25:09 +0100357 ARM_COMPUTE_ERROR("Not supported. Recompile the library with arch=arm64-v8.2-a.");
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000358#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Pablo Tellodf246182017-07-03 16:25:09 +0100359}
360
361template <bool is_scale255, bool is_sat>
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100362void 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
409template <bool is_scale255, bool is_sat>
410void 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
431template <bool is_scale255, bool is_sat>
432void 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
439NEPixelWiseMultiplicationKernel::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
444void NEPixelWiseMultiplicationKernel::configure(const ITensor *input1, const ITensor *input2, ITensor *output, float scale, ConvertPolicy overflow_policy, RoundingPolicy rounding_policy)
445{
Georgios Pinitasf0dea702017-07-03 18:17:28 +0100446 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 Tellodf246182017-07-03 16:25:09 +0100460 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 Pinitasf0dea702017-07-03 18:17:28 +0100464 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 Giorgio81f0d152017-07-11 15:00:52 +0100472 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 Barbier6ff3b192017-09-04 18:44:23 +0100475 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 Giorgio81f0d152017-07-11 15:00:52 +0100477 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 +0100478 {
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100479 // 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 Giorgio1b80b6c2017-07-17 15:06:34 +0100481 // 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 Barbier6ff3b192017-09-04 18:44:23 +0100483 }
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 Giorgio81f0d152017-07-11 15:00:52 +0100597 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 Tellodf246182017-07-03 16:25:09 +0100608 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 Barbier6ff3b192017-09-04 18:44:23 +0100613 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 Pflanzerc186b572017-09-07 09:48:04 +0100642void NEPixelWiseMultiplicationKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100643{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100644 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100645 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}