blob: b3647fc9cebca54168d2e8d28a12005fc6eca0e4 [file] [log] [blame]
John Richardsondd715f22017-09-18 16:10:48 +01001/*
2 * Copyright (c) 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 * dst OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "PixelWiseMultiplication.h"
25
26#include "tests/validation/FixedPoint.h"
27
28namespace arm_compute
29{
30namespace test
31{
32namespace validation
33{
34namespace reference
35{
36template <class T>
37struct is_floating_point
38 : std::integral_constant < bool,
39 std::is_same<float, typename std::remove_cv<T>::type>::value || std::is_same<half_float::half, typename std::remove_cv<T>::type>::value
40 || std::is_same<double, typename std::remove_cv<T>::type>::value || std::is_same<long double, typename std::remove_cv<T>::type>::value >
41{
42};
43
44template <typename T1, typename T2>
45SimpleTensor<T2> pixel_wise_multiplication(const SimpleTensor<T1> &src1, const SimpleTensor<T2> &src2, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy)
46{
47 SimpleTensor<T2> dst(src2.shape(), src2.data_type());
48
49 if(scale < 0)
50 {
51 ARM_COMPUTE_ERROR("Scale of pixel-wise multiplication must be non-negative");
52 }
53
54 using intermediate_type = typename common_promoted_signed_type<T1, T2, T2>::intermediate_type;
55
56 for(int i = 0; i < src1.num_elements(); ++i)
57 {
58 double val = static_cast<intermediate_type>(src1[i]) * static_cast<intermediate_type>(src2[i]) * static_cast<double>(scale);
59 if(is_floating_point<T2>::value)
60 {
61 dst[i] = val;
62 }
63 else
64 {
65 double rounded_val = 0;
66 switch(rounding_policy)
67 {
68 case(RoundingPolicy::TO_ZERO):
69 rounded_val = support::cpp11::trunc(val);
70 break;
71 case(RoundingPolicy::TO_NEAREST_UP):
72 rounded_val = round_half_up(val);
73 break;
74 case(RoundingPolicy::TO_NEAREST_EVEN):
75 rounded_val = round_half_even(val);
76 break;
77 default:
78 ARM_COMPUTE_ERROR("Unsupported rounding policy");
79 }
80
81 dst[i] = (convert_policy == ConvertPolicy::SATURATE) ? saturate_cast<T2>(rounded_val) : static_cast<T2>(rounded_val);
82 }
83 }
84
85 return dst;
86}
87
88// *INDENT-OFF*
89// clang-format off
90template SimpleTensor<uint8_t> pixel_wise_multiplication(const SimpleTensor<uint8_t> &src1, const SimpleTensor<uint8_t> &src2, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy);
91template SimpleTensor<int16_t> pixel_wise_multiplication(const SimpleTensor<uint8_t> &src1, const SimpleTensor<int16_t> &src2, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy);
92template SimpleTensor<int16_t> pixel_wise_multiplication(const SimpleTensor<int16_t> &src1, const SimpleTensor<int16_t> &src2, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy);
93template SimpleTensor<float> pixel_wise_multiplication(const SimpleTensor<float> &src1, const SimpleTensor<float> &src2, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy);
94template SimpleTensor<half_float::half> pixel_wise_multiplication(const SimpleTensor<half_float::half> &src1, const SimpleTensor<half_float::half> &src2, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy);
95// clang-format on
96// *INDENT-ON*
97} // namespace reference
98} // namespace validation
99} // namespace test
100} // namespace arm_compute