blob: 17020a6277afdf4c32a280584e85e4102a87cac5 [file] [log] [blame]
Sanghoon Lee70f82912017-08-24 14:21:24 +01001/*
Diego Lopez Recas0021d752017-12-18 14:42:56 +00002 * Copyright (c) 2017-2018 ARM Limited.
Sanghoon Lee70f82912017-08-24 14:21:24 +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
Georgios Pinitas583137c2017-08-31 18:12:42 +010026#include "arm_compute/core/Types.h"
Sanghoon Lee70f82912017-08-24 14:21:24 +010027#include "tests/validation/FixedPoint.h"
28#include "tests/validation/Helpers.h"
Sanghoon Lee70f82912017-08-24 14:21:24 +010029
30namespace arm_compute
31{
32namespace test
33{
34namespace validation
35{
36namespace reference
37{
Diego Lopez Recas0021d752017-12-18 14:42:56 +000038namespace
39{
40template <typename T>
41T add(T src1, T src2, ConvertPolicy convert_policy)
42{
43 using intermediate_type = typename common_promoted_signed_type<T>::intermediate_type;
44
45 intermediate_type val = static_cast<intermediate_type>(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(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(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(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)] = add(src1[coord2index(src1.shape(), id_src1)], src2[coord2index(src2.shape(), id_src2)], convert_policy);
84 }
85};
86} // namespace
87
Sanghoon Lee70f82912017-08-24 14:21:24 +010088template <typename T>
89SimpleTensor<T> arithmetic_addition(const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, DataType dst_data_type, ConvertPolicy convert_policy)
90{
Diego Lopez Recas0021d752017-12-18 14:42:56 +000091 SimpleTensor<T> dst(TensorShape::broadcast_shape(src1.shape(), src2.shape()), dst_data_type);
Sanghoon Lee70f82912017-08-24 14:21:24 +010092
Diego Lopez Recas0021d752017-12-18 14:42:56 +000093 Coordinates id_src1, id_src2, id_dst;
Sanghoon Lee70f82912017-08-24 14:21:24 +010094
Diego Lopez Recas0021d752017-12-18 14:42:56 +000095 BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(src1, src2, dst, convert_policy, id_src1, id_src2, id_dst);
Sanghoon Lee70f82912017-08-24 14:21:24 +010096
Diego Lopez Recas0021d752017-12-18 14:42:56 +000097 return dst;
Sanghoon Lee70f82912017-08-24 14:21:24 +010098}
99
100template SimpleTensor<uint8_t> arithmetic_addition(const SimpleTensor<uint8_t> &src1, const SimpleTensor<uint8_t> &src2, DataType dst_data_type, ConvertPolicy convert_policy);
101template SimpleTensor<int16_t> arithmetic_addition(const SimpleTensor<int16_t> &src1, const SimpleTensor<int16_t> &src2, DataType dst_data_type, ConvertPolicy convert_policy);
102template SimpleTensor<int8_t> arithmetic_addition(const SimpleTensor<int8_t> &src1, const SimpleTensor<int8_t> &src2, DataType dst_data_type, ConvertPolicy convert_policy);
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000103template SimpleTensor<half> arithmetic_addition(const SimpleTensor<half> &src1, const SimpleTensor<half> &src2, DataType dst_data_type, ConvertPolicy convert_policy);
Sanghoon Lee70f82912017-08-24 14:21:24 +0100104template SimpleTensor<float> arithmetic_addition(const SimpleTensor<float> &src1, const SimpleTensor<float> &src2, DataType dst_data_type, ConvertPolicy convert_policy);
105} // namespace reference
106} // namespace validation
107} // namespace test
108} // namespace arm_compute