Compute Library
 20.02.1
ArithmeticOperations.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2019 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 "ArithmeticOperations.h"
25 
26 #include "arm_compute/core/Types.h"
28 
29 namespace arm_compute
30 {
31 namespace test
32 {
33 namespace validation
34 {
35 namespace reference
36 {
37 namespace
38 {
39 template <typename T>
40 T 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 
52 template <size_t dim>
53 struct 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 
76 template <>
77 struct 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 
88 template <typename T>
90 {
91  Coordinates id_src1{};
92  Coordinates id_src2{};
93  Coordinates id_dst{};
94 
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 
100 template <>
102 {
103  Coordinates id_src1{};
104  Coordinates id_src2{};
105  Coordinates id_dst{};
106 
107  if(dst.data_type() == DataType::QASYMM8)
108  {
111  SimpleTensor<float> dst_tmp(TensorShape::broadcast_shape(src1.shape(), src2.shape()), dst.data_type());
112 
113  BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1_tmp, src2_tmp, dst_tmp, convert_policy, id_src1, id_src2, id_dst);
114 
115  dst = convert_to_asymmetric<uint8_t>(dst_tmp, dst.quantization_info());
116  return dst;
117  }
118  else
119  {
120  // DataType::U8
121  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 
127 template <>
129 {
130  Coordinates id_src1{};
131  Coordinates id_src2{};
132  Coordinates id_dst{};
133 
134  if(dst.data_type() == DataType::QASYMM8_SIGNED)
135  {
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 
154 template <>
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 
182 
183 template <typename T>
185 {
186  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.");
187 
188  SimpleTensor<T> dst(TensorShape::broadcast_shape(src1.shape(), src2.shape()), dst_data_type);
189  arithmetic_operation<T>(op, src1, src2, dst, convert_policy);
190  return dst;
191 }
192 
197 
198 } // namespace reference
199 } // namespace validation
200 } // namespace test
201 } // namespace arm_compute
bool is_data_type_quantized(DataType dt)
Check if a given data type is of quantized type.
Definition: Utils.h:1117
ArithmeticOperation
Available element-wise operations.
Definition: Types.h:508
quantized, symmetric fixed-point 16-bit number
static TensorShape broadcast_shape(const Shapes &... shapes)
If shapes are broadcast compatible, return the broadcasted shape.
Definition: TensorShape.h:210
SimpleTensor< float > convert_from_asymmetric(const SimpleTensor< uint8_t > &src)
Definition: Helpers.cpp:112
TensorShape shape() const override
Shape of the tensor.
Definition: SimpleTensor.h:321
SimpleTensor< T > arithmetic_operation(ArithmeticOperation op, const SimpleTensor< T > &src1, const SimpleTensor< T > &src2, SimpleTensor< T > &dst, ConvertPolicy convert_policy)
Copyright (c) 2017-2020 ARM Limited.
int coord2index(const TensorShape &shape, const Coordinates &coord)
Linearise the given coordinate.
Definition: Utils.h:485
quantized, asymmetric fixed-point 8-bit number unsigned
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
Coordinates of an item.
Definition: Coordinates.h:37
Simple tensor object that stores elements in a consecutive chunk of memory.
Definition: SimpleTensor.h:59
typename traits::make_signed_conditional_t< promoted_type >::type intermediate_type
Intermediate type.
Definition: Utils.h:433
ArithmeticOperation
Arithmetic operation types.
quantized, asymmetric fixed-point 8-bit number signed
DataType
Available data types.
Definition: Types.h:75
ConvertPolicy
Policy to handle overflow.
Definition: Types.h:359