blob: c68c6d4ed6e72173bab75e01563f0aaa290b8edd [file] [log] [blame]
Kaizen8938bd32017-09-28 14:38:23 +01001/*
Anthony Barbier06ea0482018-02-22 15:45:35 +00002 * Copyright (c) 2017-2018 ARM Limited.
Kaizen8938bd32017-09-28 14:38:23 +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 "ArithmeticAddition.h"
25
26#include "arm_compute/core/Types.h"
Kaizen8938bd32017-09-28 14:38:23 +010027#include "tests/validation/Helpers.h"
28
29namespace arm_compute
30{
31namespace test
32{
33namespace validation
34{
35namespace reference
36{
Anthony Barbier06ea0482018-02-22 15:45:35 +000037namespace
38{
39template <typename T>
40T add(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 = static_cast<intermediate_type>(src1) + static_cast<intermediate_type>(src2);
45
46 T result = (convert_policy == ConvertPolicy::SATURATE) ? saturate_cast<T>(val) : static_cast<T>(val);
47
48 return result;
49}
50
51template <size_t dim>
52struct BroadcastUnroll
53{
54 template <typename T>
55 static void unroll(const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, SimpleTensor<T> &dst,
56 ConvertPolicy convert_policy, Coordinates &id_src1, Coordinates &id_src2, Coordinates &id_dst)
57 {
58 const bool src1_is_broadcast = (src1.shape()[dim - 1] != dst.shape()[dim - 1]);
59 const bool src2_is_broadcast = (src2.shape()[dim - 1] != dst.shape()[dim - 1]);
60
61 id_src1.set(dim - 1, 0);
62 id_src2.set(dim - 1, 0);
63 id_dst.set(dim - 1, 0);
64
65 for(size_t i = 0; i < dst.shape()[dim - 1]; ++i, ++id_dst[dim - 1])
66 {
67 BroadcastUnroll < dim - 1 >::unroll(src1, src2, dst, convert_policy, id_src1, id_src2, id_dst);
68
69 id_src1[dim - 1] += !src1_is_broadcast;
70 id_src2[dim - 1] += !src2_is_broadcast;
71 }
72 }
73};
74
75template <>
76struct BroadcastUnroll<0>
77{
78 template <typename T>
79 static void unroll(const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, SimpleTensor<T> &dst,
80 ConvertPolicy convert_policy, Coordinates &id_src1, Coordinates &id_src2, Coordinates &id_dst)
81 {
82 dst[coord2index(dst.shape(), id_dst)] = add(src1[coord2index(src1.shape(), id_src1)], src2[coord2index(src2.shape(), id_src2)], convert_policy);
83 }
84};
85} // namespace
86
Kaizen8938bd32017-09-28 14:38:23 +010087template <typename T>
Jenkins52ba29e2018-08-29 15:32:11 +000088SimpleTensor<T> arithmetic_addition(const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, SimpleTensor<T> &dst, ConvertPolicy convert_policy)
Kaizen8938bd32017-09-28 14:38:23 +010089{
Anthony Barbier06ea0482018-02-22 15:45:35 +000090 Coordinates id_src1, id_src2, id_dst;
Kaizen8938bd32017-09-28 14:38:23 +010091
Anthony Barbier06ea0482018-02-22 15:45:35 +000092 BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(src1, src2, dst, convert_policy, id_src1, id_src2, id_dst);
Kaizen8938bd32017-09-28 14:38:23 +010093
Anthony Barbier06ea0482018-02-22 15:45:35 +000094 return dst;
Kaizen8938bd32017-09-28 14:38:23 +010095}
96
Jenkins52ba29e2018-08-29 15:32:11 +000097template <>
98SimpleTensor<uint8_t> arithmetic_addition(const SimpleTensor<uint8_t> &src1, const SimpleTensor<uint8_t> &src2, SimpleTensor<uint8_t> &dst, ConvertPolicy convert_policy)
99{
100 if(dst.data_type() == DataType::QASYMM8)
101 {
102 SimpleTensor<float> src1_tmp = convert_from_asymmetric(src1);
103 SimpleTensor<float> src2_tmp = convert_from_asymmetric(src2);
104 SimpleTensor<float> dst_tmp(TensorShape::broadcast_shape(src1.shape(), src2.shape()), dst.data_type());
105
106 Coordinates id_src1, id_src2, id_dst;
107
108 BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(src1_tmp, src2_tmp, dst_tmp, convert_policy, id_src1, id_src2, id_dst);
109
110 dst = convert_to_asymmetric(dst_tmp, dst.quantization_info());
111 return dst;
112 }
113 else
114 {
115 // DataType::U8
116 Coordinates id_src1, id_src2, id_dst;
117
118 BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(src1, src2, dst, convert_policy, id_src1, id_src2, id_dst);
119
120 return dst;
121 }
122}
123
124template SimpleTensor<int16_t> arithmetic_addition(const SimpleTensor<int16_t> &src1, const SimpleTensor<int16_t> &src2, SimpleTensor<int16_t> &dst, ConvertPolicy convert_policy);
125template SimpleTensor<int8_t> arithmetic_addition(const SimpleTensor<int8_t> &src1, const SimpleTensor<int8_t> &src2, SimpleTensor<int8_t> &dst, ConvertPolicy convert_policy);
126template SimpleTensor<half> arithmetic_addition(const SimpleTensor<half> &src1, const SimpleTensor<half> &src2, SimpleTensor<half> &dst, ConvertPolicy convert_policy);
127template SimpleTensor<float> arithmetic_addition(const SimpleTensor<float> &src1, const SimpleTensor<float> &src2, SimpleTensor<float> &dst, ConvertPolicy convert_policy);
128
129template <typename T>
130SimpleTensor<T> arithmetic_addition(const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, DataType dst_data_type, ConvertPolicy convert_policy)
131{
132 ARM_COMPUTE_ERROR_ON_MSG(dst_data_type == DataType::QASYMM8, "For QASYMM8, the quantized output tensor should be passed directly.");
133
134 SimpleTensor<T> dst(TensorShape::broadcast_shape(src1.shape(), src2.shape()), dst_data_type);
135 arithmetic_addition<T>(src1, src2, dst, convert_policy);
136 return dst;
137}
138
Kaizen8938bd32017-09-28 14:38:23 +0100139template SimpleTensor<int16_t> arithmetic_addition(const SimpleTensor<int16_t> &src1, const SimpleTensor<int16_t> &src2, DataType dst_data_type, ConvertPolicy convert_policy);
140template SimpleTensor<int8_t> arithmetic_addition(const SimpleTensor<int8_t> &src1, const SimpleTensor<int8_t> &src2, DataType dst_data_type, ConvertPolicy convert_policy);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000141template SimpleTensor<half> arithmetic_addition(const SimpleTensor<half> &src1, const SimpleTensor<half> &src2, DataType dst_data_type, ConvertPolicy convert_policy);
Kaizen8938bd32017-09-28 14:38:23 +0100142template SimpleTensor<float> arithmetic_addition(const SimpleTensor<float> &src1, const SimpleTensor<float> &src2, DataType dst_data_type, ConvertPolicy convert_policy);
Jenkins52ba29e2018-08-29 15:32:11 +0000143
Kaizen8938bd32017-09-28 14:38:23 +0100144} // namespace reference
145} // namespace validation
146} // namespace test
147} // namespace arm_compute