blob: ecf02f7e392d282910c785b1da6f0c1745a16ced [file] [log] [blame]
Georgios Pinitascbf39c62018-09-10 15:07:45 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Georgios Pinitascbf39c62018-09-10 15:07:45 +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 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "ArithmeticOperations.h"
25
26#include "arm_compute/core/Types.h"
27#include "tests/validation/Helpers.h"
28
29namespace arm_compute
30{
31namespace test
32{
33namespace validation
34{
35namespace reference
36{
37namespace
38{
39template <typename T>
40T arithm_op(ArithmeticOperation op, T src1, T src2, ConvertPolicy convert_policy)
41{
42 using intermediate_type = typename common_promoted_signed_type<T>::intermediate_type;
43
44 intermediate_type val = (op == ArithmeticOperation::ADD) ? static_cast<intermediate_type>(src1) + static_cast<intermediate_type>(src2) : static_cast<intermediate_type>
45 (src1) - static_cast<intermediate_type>(src2);
46
47 T result = (convert_policy == ConvertPolicy::SATURATE) ? saturate_cast<T>(val) : static_cast<T>(val);
48
49 return result;
50}
51
52template <size_t dim>
53struct BroadcastUnroll
54{
55 template <typename T>
56 static void unroll(ArithmeticOperation op, const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, SimpleTensor<T> &dst,
57 ConvertPolicy convert_policy, Coordinates &id_src1, Coordinates &id_src2, Coordinates &id_dst)
58 {
59 const bool src1_is_broadcast = (src1.shape()[dim - 1] != dst.shape()[dim - 1]);
60 const bool src2_is_broadcast = (src2.shape()[dim - 1] != dst.shape()[dim - 1]);
61
62 id_src1.set(dim - 1, 0);
63 id_src2.set(dim - 1, 0);
64 id_dst.set(dim - 1, 0);
Michalis Spyroud1d77222020-04-08 14:10:15 +010065#if defined(_OPENMP)
66 #pragma omp parallel for
67#endif /* _OPENMP */
68 for(size_t i = 0; i < dst.shape()[dim - 1]; ++i)
Georgios Pinitascbf39c62018-09-10 15:07:45 +010069 {
70 BroadcastUnroll < dim - 1 >::unroll(op, src1, src2, dst, convert_policy, id_src1, id_src2, id_dst);
71
72 id_src1[dim - 1] += !src1_is_broadcast;
73 id_src2[dim - 1] += !src2_is_broadcast;
Michalis Spyroud1d77222020-04-08 14:10:15 +010074 ++id_dst[dim - 1];
Georgios Pinitascbf39c62018-09-10 15:07:45 +010075 }
76 }
77};
78
79template <>
80struct BroadcastUnroll<0>
81{
82 template <typename T>
83 static void unroll(ArithmeticOperation op, const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, SimpleTensor<T> &dst,
84 ConvertPolicy convert_policy, Coordinates &id_src1, Coordinates &id_src2, Coordinates &id_dst)
85 {
86 dst[coord2index(dst.shape(), id_dst)] = arithm_op(op, src1[coord2index(src1.shape(), id_src1)], src2[coord2index(src2.shape(), id_src2)], convert_policy);
87 }
88};
89} // namespace
90
91template <typename T>
92SimpleTensor<T> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, SimpleTensor<T> &dst, ConvertPolicy convert_policy)
93{
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010094 Coordinates id_src1{};
95 Coordinates id_src2{};
96 Coordinates id_dst{};
Georgios Pinitascbf39c62018-09-10 15:07:45 +010097
98 BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1, src2, dst, convert_policy, id_src1, id_src2, id_dst);
99
100 return dst;
101}
102
103template <>
104SimpleTensor<uint8_t> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<uint8_t> &src1, const SimpleTensor<uint8_t> &src2, SimpleTensor<uint8_t> &dst, ConvertPolicy convert_policy)
105{
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100106 Coordinates id_src1{};
107 Coordinates id_src2{};
108 Coordinates id_dst{};
109
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100110 if(dst.data_type() == DataType::QASYMM8)
111 {
112 SimpleTensor<float> src1_tmp = convert_from_asymmetric(src1);
113 SimpleTensor<float> src2_tmp = convert_from_asymmetric(src2);
114 SimpleTensor<float> dst_tmp(TensorShape::broadcast_shape(src1.shape(), src2.shape()), dst.data_type());
115
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100116 BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1_tmp, src2_tmp, dst_tmp, convert_policy, id_src1, id_src2, id_dst);
117
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100118 dst = convert_to_asymmetric<uint8_t>(dst_tmp, dst.quantization_info());
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100119 return dst;
120 }
121 else
122 {
123 // DataType::U8
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100124 BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1, src2, dst, convert_policy, id_src1, id_src2, id_dst);
125
126 return dst;
127 }
128}
129
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100130template <>
Michalis Spyroubc4d7c22019-12-03 15:11:09 +0000131SimpleTensor<int8_t> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<int8_t> &src1, const SimpleTensor<int8_t> &src2, SimpleTensor<int8_t> &dst, ConvertPolicy convert_policy)
132{
133 Coordinates id_src1{};
134 Coordinates id_src2{};
135 Coordinates id_dst{};
136
137 if(dst.data_type() == DataType::QASYMM8_SIGNED)
138 {
139 SimpleTensor<float> src1_tmp = convert_from_asymmetric(src1);
140 SimpleTensor<float> src2_tmp = convert_from_asymmetric(src2);
141 SimpleTensor<float> dst_tmp(TensorShape::broadcast_shape(src1.shape(), src2.shape()), dst.data_type());
142
143 BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1_tmp, src2_tmp, dst_tmp, convert_policy, id_src1, id_src2, id_dst);
144
145 dst = convert_to_asymmetric<int8_t>(dst_tmp, dst.quantization_info());
146 return dst;
147 }
148 else
149 {
150 // DataType::S8
151 BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1, src2, dst, convert_policy, id_src1, id_src2, id_dst);
152
153 return dst;
154 }
155}
156
157template <>
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100158SimpleTensor<int16_t> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<int16_t> &src1, const SimpleTensor<int16_t> &src2, SimpleTensor<int16_t> &dst, ConvertPolicy convert_policy)
159{
160 Coordinates id_src1{};
161 Coordinates id_src2{};
162 Coordinates id_dst{};
163
164 if(dst.data_type() == DataType::QSYMM16)
165 {
166 SimpleTensor<float> src1_tmp = convert_from_symmetric<int16_t>(src1);
167 SimpleTensor<float> src2_tmp = convert_from_symmetric<int16_t>(src2);
168 SimpleTensor<float> dst_tmp(TensorShape::broadcast_shape(src1.shape(), src2.shape()), dst.data_type());
169
170 BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1_tmp, src2_tmp, dst_tmp, convert_policy, id_src1, id_src2, id_dst);
171
172 dst = convert_to_symmetric<int16_t>(dst_tmp, dst.quantization_info());
173 return dst;
174 }
175 else
176 {
177 // DataType::S16
178 BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1, src2, dst, convert_policy, id_src1, id_src2, id_dst);
179 return dst;
180 }
181}
182
Manuel Bottini36b8f052020-06-23 12:22:15 +0100183template SimpleTensor<int32_t> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<int32_t> &src1, const SimpleTensor<int32_t> &src2, SimpleTensor<int32_t> &dst, ConvertPolicy convert_policy);
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100184template SimpleTensor<half> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<half> &src1, const SimpleTensor<half> &src2, SimpleTensor<half> &dst, ConvertPolicy convert_policy);
185template SimpleTensor<float> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<float> &src1, const SimpleTensor<float> &src2, SimpleTensor<float> &dst, ConvertPolicy convert_policy);
186
187template <typename T>
188SimpleTensor<T> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, DataType dst_data_type, ConvertPolicy convert_policy)
189{
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100190 ARM_COMPUTE_ERROR_ON_MSG(is_data_type_quantized(dst_data_type), "For quantized input data types, the quantized output tensor should be passed directly.");
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100191
192 SimpleTensor<T> dst(TensorShape::broadcast_shape(src1.shape(), src2.shape()), dst_data_type);
193 arithmetic_operation<T>(op, src1, src2, dst, convert_policy);
194 return dst;
195}
196
Michele Di Giorgio11c562c2020-06-10 16:34:50 +0100197template SimpleTensor<int32_t> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<int32_t> &src1, const SimpleTensor<int32_t> &src2, DataType dst_data_type, ConvertPolicy convert_policy);
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100198template SimpleTensor<int16_t> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<int16_t> &src1, const SimpleTensor<int16_t> &src2, DataType dst_data_type, ConvertPolicy convert_policy);
199template SimpleTensor<int8_t> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<int8_t> &src1, const SimpleTensor<int8_t> &src2, DataType dst_data_type, ConvertPolicy convert_policy);
200template SimpleTensor<half> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<half> &src1, const SimpleTensor<half> &src2, DataType dst_data_type, ConvertPolicy convert_policy);
201template SimpleTensor<float> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<float> &src1, const SimpleTensor<float> &src2, DataType dst_data_type, ConvertPolicy convert_policy);
202
203} // namespace reference
204} // namespace validation
205} // namespace test
206} // namespace arm_compute