blob: 859da5ce5942d37547cea222b0a1705b965321f8 [file] [log] [blame]
John Richardsondd715f22017-09-18 16:10:48 +01001/*
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +00002 * Copyright (c) 2017-2018 ARM Limited.
John Richardsondd715f22017-09-18 16:10:48 +01003 *
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
John Richardsondd715f22017-09-18 16:10:48 +010026namespace arm_compute
27{
28namespace test
29{
30namespace validation
31{
32namespace reference
33{
34template <class T>
35struct is_floating_point
36 : std::integral_constant < bool,
37 std::is_same<float, typename std::remove_cv<T>::type>::value || std::is_same<half_float::half, typename std::remove_cv<T>::type>::value
38 || std::is_same<double, typename std::remove_cv<T>::type>::value || std::is_same<long double, typename std::remove_cv<T>::type>::value >
39{
40};
41
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +000042namespace
43{
44/** Compute the result of `src1 * src2 * scale`. The result type always matches the type of @p src2.
45 *
Vidhya Sudhan Loganathan0fc25452018-06-18 14:40:56 +010046 * @param[in] src1 An input value. Data types supported: U8/S16/F16/F32.
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +000047 * @param[in] src2 An input value. Data types supported: same as @p src1.
48 * @param[in] scale Scale to apply after multiplication.
Vidhya Sudhan Loganathan0fc25452018-06-18 14:40:56 +010049 * Scale must be positive and its value must be either 1/255 or 1/2^n where n is between 0 and 15.
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +000050 * @param[in] convert_policy Overflow policy. Supported overflow policies: Wrap, Saturate
51 * @param[in] rounding_policy Rounding policy. Supported rounding modes: to zero, to nearest even.
52 */
53template <typename T1, typename T2>
54T2 mul(const T1 src1, const T2 src2, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy)
55{
56 using intermediate_type = typename common_promoted_signed_type<T1, T2, T2>::intermediate_type;
57
58 const double val = static_cast<intermediate_type>(src1) * static_cast<intermediate_type>(src2) * static_cast<double>(scale);
59
60 if(is_floating_point<T2>::value)
61 {
62 const auto result = static_cast<T2>(val);
63
64 return result;
65 }
66 else
67 {
68 double rounded_val = 0;
69 switch(rounding_policy)
70 {
71 case(RoundingPolicy::TO_ZERO):
72 rounded_val = support::cpp11::trunc(val);
73 break;
74 case(RoundingPolicy::TO_NEAREST_UP):
75 rounded_val = round_half_up(val);
76 break;
77 case(RoundingPolicy::TO_NEAREST_EVEN):
78 rounded_val = round_half_even(val);
79 break;
80 default:
81 ARM_COMPUTE_ERROR("Unsupported rounding policy");
82 }
83
84 const auto result = static_cast<T2>((convert_policy == ConvertPolicy::SATURATE) ? saturate_cast<T2>(rounded_val) : rounded_val);
85
86 return result;
87 }
88}
89
90template <size_t dim>
91struct BroadcastUnroll
92{
93 template <typename T1, typename T2>
94 static void unroll(const SimpleTensor<T1> &src1, const SimpleTensor<T2> &src2, SimpleTensor<T2> &dst,
95 float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy,
96 Coordinates &id_src1, Coordinates &id_src2, Coordinates &id_dst)
97 {
98 const bool src1_is_broadcast = (src1.shape()[dim - 1] != dst.shape()[dim - 1]);
99 const bool src2_is_broadcast = (src2.shape()[dim - 1] != dst.shape()[dim - 1]);
100
101 id_src1.set(dim - 1, 0);
102 id_src2.set(dim - 1, 0);
103 id_dst.set(dim - 1, 0);
104
105 for(size_t i = 0; i < dst.shape()[dim - 1]; ++i, ++id_dst[dim - 1])
106 {
107 BroadcastUnroll < dim - 1 >::unroll(src1, src2, dst, scale, convert_policy, rounding_policy, id_src1, id_src2, id_dst);
108
109 id_src1[dim - 1] += !src1_is_broadcast;
110 id_src2[dim - 1] += !src2_is_broadcast;
111 }
112 }
113};
114
115template <>
116struct BroadcastUnroll<0>
117{
118 template <typename T1, typename T2>
119 static void unroll(const SimpleTensor<T1> &src1, const SimpleTensor<T2> &src2, SimpleTensor<T2> &dst,
120 float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy,
121 Coordinates &id_src1, Coordinates &id_src2, Coordinates &id_dst)
122 {
123 dst[coord2index(dst.shape(), id_dst)] = mul(src1[coord2index(src1.shape(), id_src1)], src2[coord2index(src2.shape(), id_src2)], scale, convert_policy, rounding_policy);
124 }
125};
126} // namespace
127
John Richardsondd715f22017-09-18 16:10:48 +0100128template <typename T1, typename T2>
129SimpleTensor<T2> pixel_wise_multiplication(const SimpleTensor<T1> &src1, const SimpleTensor<T2> &src2, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy)
130{
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000131 SimpleTensor<T2> dst(TensorShape::broadcast_shape(src1.shape(), src2.shape()), src2.data_type());
John Richardsondd715f22017-09-18 16:10:48 +0100132
133 if(scale < 0)
134 {
135 ARM_COMPUTE_ERROR("Scale of pixel-wise multiplication must be non-negative");
136 }
137
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000138 Coordinates id_src1, id_src2, id_dst;
John Richardsondd715f22017-09-18 16:10:48 +0100139
Michele Di Giorgio6259e5f2018-01-17 17:29:33 +0000140 BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(src1, src2, dst, scale, convert_policy, rounding_policy, id_src1, id_src2, id_dst);
John Richardsondd715f22017-09-18 16:10:48 +0100141
142 return dst;
143}
144
145// *INDENT-OFF*
146// clang-format off
147template SimpleTensor<uint8_t> pixel_wise_multiplication(const SimpleTensor<uint8_t> &src1, const SimpleTensor<uint8_t> &src2, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy);
148template SimpleTensor<int16_t> pixel_wise_multiplication(const SimpleTensor<uint8_t> &src1, const SimpleTensor<int16_t> &src2, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy);
149template SimpleTensor<int16_t> pixel_wise_multiplication(const SimpleTensor<int16_t> &src1, const SimpleTensor<int16_t> &src2, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy);
150template SimpleTensor<float> pixel_wise_multiplication(const SimpleTensor<float> &src1, const SimpleTensor<float> &src2, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy);
151template 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);
152// clang-format on
153// *INDENT-ON*
154} // namespace reference
155} // namespace validation
156} // namespace test
157} // namespace arm_compute