blob: 2313d9b022210c91f22cb6e4f3588d7e17febb2c [file] [log] [blame]
Georgios Pinitas7900a9e2018-11-23 11:44:58 +00001/*
morgolock74a16962020-01-15 11:40:49 +00002 * Copyright (c) 2018-2020 ARM Limited.
Georgios Pinitas7900a9e2018-11-23 11:44:58 +00003 *
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 "Comparisons.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>
40uint8_t compare_op(ComparisonOperation op, T src1, T src2)
41{
42 uint8_t result = 0;
43 switch(op)
44 {
45 case ComparisonOperation::Equal:
46 result = static_cast<uint8_t>(src1 == src2);
47 break;
48 case ComparisonOperation::NotEqual:
49 result = static_cast<uint8_t>(src1 != src2);
50 break;
51 case ComparisonOperation::GreaterEqual:
52 result = static_cast<uint8_t>(src1 >= src2);
53 break;
54 case ComparisonOperation::Greater:
55 result = static_cast<uint8_t>(src1 > src2);
56 break;
57 case ComparisonOperation::LessEqual:
58 result = static_cast<uint8_t>(src1 <= src2);
59 break;
60 case ComparisonOperation::Less:
61 result = static_cast<uint8_t>(src1 < src2);
62 break;
63 default:
64 ARM_COMPUTE_ERROR("Unsupported operation");
65 }
66 return (result != 0) ? 255 : 0;
67}
68
69template <size_t dim>
70struct BroadcastUnroll
71{
72 template <typename T>
73 static void unroll(ComparisonOperation op,
74 const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, SimpleTensor<uint8_t> &dst,
75 Coordinates &id_src1, Coordinates &id_src2, Coordinates &id_dst)
76 {
77 const bool src1_is_broadcast = (src1.shape()[dim - 1] != dst.shape()[dim - 1]);
78 const bool src2_is_broadcast = (src2.shape()[dim - 1] != dst.shape()[dim - 1]);
79
80 id_src1.set(dim - 1, 0);
81 id_src2.set(dim - 1, 0);
82 id_dst.set(dim - 1, 0);
Michalis Spyroud1d77222020-04-08 14:10:15 +010083#if defined(_OPENMP)
84 #pragma omp parallel for
85#endif /* _OPENMP */
86 for(size_t i = 0; i < dst.shape()[dim - 1]; ++i)
Georgios Pinitas7900a9e2018-11-23 11:44:58 +000087 {
88 BroadcastUnroll < dim - 1 >::unroll(op, src1, src2, dst, id_src1, id_src2, id_dst);
89
90 id_src1[dim - 1] += !src1_is_broadcast;
91 id_src2[dim - 1] += !src2_is_broadcast;
Michalis Spyroud1d77222020-04-08 14:10:15 +010092 ++id_dst[dim - 1];
Georgios Pinitas7900a9e2018-11-23 11:44:58 +000093 }
94 }
95};
96
97template <>
98struct BroadcastUnroll<0>
99{
100 template <typename T>
101 static void unroll(ComparisonOperation op,
102 const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, SimpleTensor<uint8_t> &dst,
103 Coordinates &id_src1, Coordinates &id_src2, Coordinates &id_dst)
104 {
105 dst[coord2index(dst.shape(), id_dst)] = compare_op(op, src1[coord2index(src1.shape(), id_src1)], src2[coord2index(src2.shape(), id_src2)]);
106 }
107};
108} // namespace
109
110template <typename T>
111SimpleTensor<uint8_t> compare(ComparisonOperation op, const SimpleTensor<T> &src1, const SimpleTensor<T> &src2)
112{
113 SimpleTensor<uint8_t> dst(TensorShape::broadcast_shape(src1.shape(), src2.shape()), DataType::U8);
114
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100115 Coordinates id_src1{};
116 Coordinates id_src2{};
117 Coordinates id_dst{};
Georgios Pinitas7900a9e2018-11-23 11:44:58 +0000118 BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1, src2, dst, id_src1, id_src2, id_dst);
119 return dst;
120}
121
122template <>
123SimpleTensor<uint8_t> compare(ComparisonOperation op, const SimpleTensor<uint8_t> &src1, const SimpleTensor<uint8_t> &src2)
124{
125 SimpleTensor<uint8_t> dst(TensorShape::broadcast_shape(src1.shape(), src2.shape()), DataType::U8);
126
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100127 Coordinates id_src1{};
128 Coordinates id_src2{};
129 Coordinates id_dst{};
130
Georgios Pinitas7900a9e2018-11-23 11:44:58 +0000131 if(src1.data_type() == DataType::QASYMM8)
132 {
133 SimpleTensor<float> src1_tmp = convert_from_asymmetric(src1);
134 SimpleTensor<float> src2_tmp = convert_from_asymmetric(src2);
Georgios Pinitas7900a9e2018-11-23 11:44:58 +0000135 BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1_tmp, src2_tmp, dst, id_src1, id_src2, id_dst);
136 }
137 else
138 {
139 // DataType::U8
Georgios Pinitas7900a9e2018-11-23 11:44:58 +0000140 BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1, src2, dst, id_src1, id_src2, id_dst);
141 }
142 return dst;
143}
144
morgolock74a16962020-01-15 11:40:49 +0000145template <>
146SimpleTensor<uint8_t> compare(ComparisonOperation op, const SimpleTensor<int8_t> &src1, const SimpleTensor<int8_t> &src2)
147{
148 SimpleTensor<uint8_t> dst(TensorShape::broadcast_shape(src1.shape(), src2.shape()), DataType::U8);
149
150 Coordinates id_src1{};
151 Coordinates id_src2{};
152 Coordinates id_dst{};
153
154 if(src1.data_type() == DataType::QASYMM8_SIGNED)
155 {
156 SimpleTensor<float> src1_tmp = convert_from_asymmetric(src1);
157 SimpleTensor<float> src2_tmp = convert_from_asymmetric(src2);
158 BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1_tmp, src2_tmp, dst, id_src1, id_src2, id_dst);
159 }
160 else
161 {
162 // DataType::U8
163 BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1, src2, dst, id_src1, id_src2, id_dst);
164 }
165 return dst;
166}
167
Georgios Pinitas7900a9e2018-11-23 11:44:58 +0000168template SimpleTensor<uint8_t> compare(ComparisonOperation op, const SimpleTensor<half> &src1, const SimpleTensor<half> &src2);
169template SimpleTensor<uint8_t> compare(ComparisonOperation op, const SimpleTensor<float> &src1, const SimpleTensor<float> &src2);
170
171} // namespace reference
172} // namespace validation
173} // namespace test
174} // namespace arm_compute