Compute Library
 20.02.1
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 
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 
94 template <>
95 struct 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 
107 template <typename T>
109 {
111 
112  Coordinates id_src1{};
113  Coordinates id_src2{};
114  Coordinates id_dst{};
115  BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1, src2, dst, id_src1, id_src2, id_dst);
116  return dst;
117 }
118 
119 template <>
121 {
123 
124  Coordinates id_src1{};
125  Coordinates id_src2{};
126  Coordinates id_dst{};
127 
128  if(src1.data_type() == DataType::QASYMM8)
129  {
132  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
137  BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1, src2, dst, id_src1, id_src2, id_dst);
138  }
139  return dst;
140 }
141 
142 template <>
144 {
146 
147  Coordinates id_src1{};
148  Coordinates id_src2{};
149  Coordinates id_dst{};
150 
151  if(src1.data_type() == DataType::QASYMM8_SIGNED)
152  {
155  BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1_tmp, src2_tmp, dst, id_src1, id_src2, id_dst);
156  }
157  else
158  {
159  // DataType::U8
160  BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1, src2, dst, id_src1, id_src2, id_dst);
161  }
162  return dst;
163 }
164 
167 
168 } // namespace reference
169 } // namespace validation
170 } // namespace test
171 } // 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:485
quantized, asymmetric fixed-point 8-bit number unsigned
Coordinates of an item.
Definition: Coordinates.h:37
ComparisonOperation
Supported comparison operations.
Definition: Types.h:173
quantized, asymmetric fixed-point 8-bit number signed