Compute Library
 20.05
Comparisons.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2020 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 "Comparisons.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 uint8_t compare_op(ComparisonOperation op, T src1, T src2)
41 {
42  uint8_t result = 0;
43  switch(op)
44  {
46  result = static_cast<uint8_t>(src1 == src2);
47  break;
49  result = static_cast<uint8_t>(src1 != src2);
50  break;
52  result = static_cast<uint8_t>(src1 >= src2);
53  break;
55  result = static_cast<uint8_t>(src1 > src2);
56  break;
58  result = static_cast<uint8_t>(src1 <= src2);
59  break;
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 
69 template <size_t dim>
70 struct 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 #if defined(_OPENMP)
84  #pragma omp parallel for
85 #endif /* _OPENMP */
86  for(size_t i = 0; i < dst.shape()[dim - 1]; ++i)
87  {
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;
92  ++id_dst[dim - 1];
93  }
94  }
95 };
96 
97 template <>
98 struct 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 
110 template <typename T>
112 {
114 
115  Coordinates id_src1{};
116  Coordinates id_src2{};
117  Coordinates id_dst{};
118  BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1, src2, dst, id_src1, id_src2, id_dst);
119  return dst;
120 }
121 
122 template <>
124 {
126 
127  Coordinates id_src1{};
128  Coordinates id_src2{};
129  Coordinates id_dst{};
130 
131  if(src1.data_type() == DataType::QASYMM8)
132  {
135  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
140  BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1, src2, dst, id_src1, id_src2, id_dst);
141  }
142  return dst;
143 }
144 
145 template <>
147 {
149 
150  Coordinates id_src1{};
151  Coordinates id_src2{};
152  Coordinates id_dst{};
153 
154  if(src1.data_type() == DataType::QASYMM8_SIGNED)
155  {
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 
170 
171 } // namespace reference
172 } // namespace validation
173 } // namespace test
174 } // namespace arm_compute
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
1 channel, 1 U8 per channel
DataType data_type() const override
Data type of the tensor.
Definition: SimpleTensor.h:358
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
Copyright (c) 2017-2020 ARM Limited.
SimpleTensor< uint8_t > compare(ComparisonOperation op, const SimpleTensor< T > &src1, const SimpleTensor< T > &src2)
int coord2index(const TensorShape &shape, const Coordinates &coord)
Linearise the given coordinate.
Definition: Utils.h:489
quantized, asymmetric fixed-point 8-bit number unsigned
Coordinates of an item.
Definition: Coordinates.h:37
ComparisonOperation
Supported comparison operations.
Definition: Types.h:176
quantized, asymmetric fixed-point 8-bit number signed