blob: 6d08daf89d4f5f9abac88f3d5455ed4fcae0c890 [file] [log] [blame]
Georgios Pinitas7900a9e2018-11-23 11:44:58 +00001/*
Michalis Spyroubcfd09a2019-05-01 13:03:59 +01002 * Copyright (c) 2018-2019 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);
83
84 for(size_t i = 0; i < dst.shape()[dim - 1]; ++i, ++id_dst[dim - 1])
85 {
86 BroadcastUnroll < dim - 1 >::unroll(op, src1, src2, dst, id_src1, id_src2, id_dst);
87
88 id_src1[dim - 1] += !src1_is_broadcast;
89 id_src2[dim - 1] += !src2_is_broadcast;
90 }
91 }
92};
93
94template <>
95struct BroadcastUnroll<0>
96{
97 template <typename T>
98 static void unroll(ComparisonOperation op,
99 const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, SimpleTensor<uint8_t> &dst,
100 Coordinates &id_src1, Coordinates &id_src2, Coordinates &id_dst)
101 {
102 dst[coord2index(dst.shape(), id_dst)] = compare_op(op, src1[coord2index(src1.shape(), id_src1)], src2[coord2index(src2.shape(), id_src2)]);
103 }
104};
105} // namespace
106
107template <typename T>
108SimpleTensor<uint8_t> compare(ComparisonOperation op, const SimpleTensor<T> &src1, const SimpleTensor<T> &src2)
109{
110 SimpleTensor<uint8_t> dst(TensorShape::broadcast_shape(src1.shape(), src2.shape()), DataType::U8);
111
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100112 Coordinates id_src1{};
113 Coordinates id_src2{};
114 Coordinates id_dst{};
Georgios Pinitas7900a9e2018-11-23 11:44:58 +0000115 BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1, src2, dst, id_src1, id_src2, id_dst);
116 return dst;
117}
118
119template <>
120SimpleTensor<uint8_t> compare(ComparisonOperation op, const SimpleTensor<uint8_t> &src1, const SimpleTensor<uint8_t> &src2)
121{
122 SimpleTensor<uint8_t> dst(TensorShape::broadcast_shape(src1.shape(), src2.shape()), DataType::U8);
123
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100124 Coordinates id_src1{};
125 Coordinates id_src2{};
126 Coordinates id_dst{};
127
Georgios Pinitas7900a9e2018-11-23 11:44:58 +0000128 if(src1.data_type() == DataType::QASYMM8)
129 {
130 SimpleTensor<float> src1_tmp = convert_from_asymmetric(src1);
131 SimpleTensor<float> src2_tmp = convert_from_asymmetric(src2);
Georgios Pinitas7900a9e2018-11-23 11:44:58 +0000132 BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1_tmp, src2_tmp, dst, id_src1, id_src2, id_dst);
133 }
134 else
135 {
136 // DataType::U8
Georgios Pinitas7900a9e2018-11-23 11:44:58 +0000137 BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1, src2, dst, id_src1, id_src2, id_dst);
138 }
139 return dst;
140}
141
142template SimpleTensor<uint8_t> compare(ComparisonOperation op, const SimpleTensor<half> &src1, const SimpleTensor<half> &src2);
143template SimpleTensor<uint8_t> compare(ComparisonOperation op, const SimpleTensor<float> &src1, const SimpleTensor<float> &src2);
144
145} // namespace reference
146} // namespace validation
147} // namespace test
148} // namespace arm_compute