blob: a2be9c9d11d12eb352b800f762698ebb30ed1687 [file] [log] [blame]
Georgios Pinitascbf39c62018-09-10 15:07:45 +01001/*
Michalis Spyroubcfd09a2019-05-01 13:03:59 +01002 * Copyright (c) 2017-2019 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);
65
66 for(size_t i = 0; i < dst.shape()[dim - 1]; ++i, ++id_dst[dim - 1])
67 {
68 BroadcastUnroll < dim - 1 >::unroll(op, src1, src2, dst, convert_policy, id_src1, id_src2, id_dst);
69
70 id_src1[dim - 1] += !src1_is_broadcast;
71 id_src2[dim - 1] += !src2_is_broadcast;
72 }
73 }
74};
75
76template <>
77struct BroadcastUnroll<0>
78{
79 template <typename T>
80 static void unroll(ArithmeticOperation op, const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, SimpleTensor<T> &dst,
81 ConvertPolicy convert_policy, Coordinates &id_src1, Coordinates &id_src2, Coordinates &id_dst)
82 {
83 dst[coord2index(dst.shape(), id_dst)] = arithm_op(op, src1[coord2index(src1.shape(), id_src1)], src2[coord2index(src2.shape(), id_src2)], convert_policy);
84 }
85};
86} // namespace
87
88template <typename T>
89SimpleTensor<T> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, SimpleTensor<T> &dst, ConvertPolicy convert_policy)
90{
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010091 Coordinates id_src1{};
92 Coordinates id_src2{};
93 Coordinates id_dst{};
Georgios Pinitascbf39c62018-09-10 15:07:45 +010094
95 BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1, src2, dst, convert_policy, id_src1, id_src2, id_dst);
96
97 return dst;
98}
99
100template <>
101SimpleTensor<uint8_t> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<uint8_t> &src1, const SimpleTensor<uint8_t> &src2, SimpleTensor<uint8_t> &dst, ConvertPolicy convert_policy)
102{
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100103 Coordinates id_src1{};
104 Coordinates id_src2{};
105 Coordinates id_dst{};
106
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100107 if(dst.data_type() == DataType::QASYMM8)
108 {
109 SimpleTensor<float> src1_tmp = convert_from_asymmetric(src1);
110 SimpleTensor<float> src2_tmp = convert_from_asymmetric(src2);
111 SimpleTensor<float> dst_tmp(TensorShape::broadcast_shape(src1.shape(), src2.shape()), dst.data_type());
112
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100113 BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1_tmp, src2_tmp, dst_tmp, convert_policy, id_src1, id_src2, id_dst);
114
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100115 dst = convert_to_asymmetric<uint8_t>(dst_tmp, dst.quantization_info());
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100116 return dst;
117 }
118 else
119 {
120 // DataType::U8
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100121 BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1, src2, dst, convert_policy, id_src1, id_src2, id_dst);
122
123 return dst;
124 }
125}
126
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100127template <>
Michalis Spyroubc4d7c22019-12-03 15:11:09 +0000128SimpleTensor<int8_t> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<int8_t> &src1, const SimpleTensor<int8_t> &src2, SimpleTensor<int8_t> &dst, ConvertPolicy convert_policy)
129{
130 Coordinates id_src1{};
131 Coordinates id_src2{};
132 Coordinates id_dst{};
133
134 if(dst.data_type() == DataType::QASYMM8_SIGNED)
135 {
136 SimpleTensor<float> src1_tmp = convert_from_asymmetric(src1);
137 SimpleTensor<float> src2_tmp = convert_from_asymmetric(src2);
138 SimpleTensor<float> dst_tmp(TensorShape::broadcast_shape(src1.shape(), src2.shape()), dst.data_type());
139
140 BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1_tmp, src2_tmp, dst_tmp, convert_policy, id_src1, id_src2, id_dst);
141
142 dst = convert_to_asymmetric<int8_t>(dst_tmp, dst.quantization_info());
143 return dst;
144 }
145 else
146 {
147 // DataType::S8
148 BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1, src2, dst, convert_policy, id_src1, id_src2, id_dst);
149
150 return dst;
151 }
152}
153
154template <>
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100155SimpleTensor<int16_t> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<int16_t> &src1, const SimpleTensor<int16_t> &src2, SimpleTensor<int16_t> &dst, ConvertPolicy convert_policy)
156{
157 Coordinates id_src1{};
158 Coordinates id_src2{};
159 Coordinates id_dst{};
160
161 if(dst.data_type() == DataType::QSYMM16)
162 {
163 SimpleTensor<float> src1_tmp = convert_from_symmetric<int16_t>(src1);
164 SimpleTensor<float> src2_tmp = convert_from_symmetric<int16_t>(src2);
165 SimpleTensor<float> dst_tmp(TensorShape::broadcast_shape(src1.shape(), src2.shape()), dst.data_type());
166
167 BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1_tmp, src2_tmp, dst_tmp, convert_policy, id_src1, id_src2, id_dst);
168
169 dst = convert_to_symmetric<int16_t>(dst_tmp, dst.quantization_info());
170 return dst;
171 }
172 else
173 {
174 // DataType::S16
175 BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1, src2, dst, convert_policy, id_src1, id_src2, id_dst);
176 return dst;
177 }
178}
179
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100180template SimpleTensor<int8_t> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<int8_t> &src1, const SimpleTensor<int8_t> &src2, SimpleTensor<int8_t> &dst, ConvertPolicy convert_policy);
181template SimpleTensor<half> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<half> &src1, const SimpleTensor<half> &src2, SimpleTensor<half> &dst, ConvertPolicy convert_policy);
182template SimpleTensor<float> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<float> &src1, const SimpleTensor<float> &src2, SimpleTensor<float> &dst, ConvertPolicy convert_policy);
183
184template <typename T>
185SimpleTensor<T> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, DataType dst_data_type, ConvertPolicy convert_policy)
186{
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100187 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 +0100188
189 SimpleTensor<T> dst(TensorShape::broadcast_shape(src1.shape(), src2.shape()), dst_data_type);
190 arithmetic_operation<T>(op, src1, src2, dst, convert_policy);
191 return dst;
192}
193
194template SimpleTensor<int16_t> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<int16_t> &src1, const SimpleTensor<int16_t> &src2, DataType dst_data_type, ConvertPolicy convert_policy);
195template SimpleTensor<int8_t> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<int8_t> &src1, const SimpleTensor<int8_t> &src2, DataType dst_data_type, ConvertPolicy convert_policy);
196template SimpleTensor<half> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<half> &src1, const SimpleTensor<half> &src2, DataType dst_data_type, ConvertPolicy convert_policy);
197template SimpleTensor<float> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<float> &src1, const SimpleTensor<float> &src2, DataType dst_data_type, ConvertPolicy convert_policy);
198
199} // namespace reference
200} // namespace validation
201} // namespace test
202} // namespace arm_compute