Compute Library
 21.02
Logical.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020-2021 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  */
25 #include "src/core/KernelTypes.h"
27 
28 namespace arm_compute
29 {
30 namespace test
31 {
32 namespace validation
33 {
34 namespace reference
35 {
36 template <typename T>
38 {
39  switch(op)
40  {
42  return src1 && src2;
44  return src1 || src2;
45  // The following operators are either invalid or not binary operator
47  // fall through
49  // fall through
50  default:
51  ARM_COMPUTE_ASSERT(true);
52  }
53  return T{};
54 }
55 
56 template <size_t dim>
57 struct BroadcastUnroll
58 {
59  template <typename T>
60  static void unroll(arm_compute::LogicalOperation op, const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, SimpleTensor<T> &dst,
61  Coordinates &id_src1, Coordinates &id_src2, Coordinates &id_dst)
62  {
63  const bool src1_is_broadcast = (src1.shape()[dim - 1] != dst.shape()[dim - 1]);
64  const bool src2_is_broadcast = (src2.shape()[dim - 1] != dst.shape()[dim - 1]);
65 
66  id_src1.set(dim - 1, 0);
67  id_src2.set(dim - 1, 0);
68  id_dst.set(dim - 1, 0);
69 #if defined(_OPENMP)
70  #pragma omp parallel for
71 #endif /* _OPENMP */
72  for(size_t i = 0; i < dst.shape()[dim - 1]; ++i)
73  {
74  BroadcastUnroll < dim - 1 >::unroll(op, src1, src2, dst, id_src1, id_src2, id_dst);
75 
76  id_src1[dim - 1] += !src1_is_broadcast;
77  id_src2[dim - 1] += !src2_is_broadcast;
78  ++id_dst[dim - 1];
79  }
80  }
81 };
82 
83 template <>
84 struct BroadcastUnroll<0>
85 {
86  template <typename T>
87  static void unroll(arm_compute::LogicalOperation op, const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, SimpleTensor<T> &dst,
88  Coordinates &id_src1, Coordinates &id_src2, Coordinates &id_dst)
89  {
90  dst[coord2index(dst.shape(), id_dst)] = logical_binary_op(op, src1[coord2index(src1.shape(), id_src1)], src2[coord2index(src2.shape(), id_src2)]);
91  }
92 };
93 
94 template <typename T>
96 {
97  Coordinates id_src1{};
98  Coordinates id_src2{};
99  Coordinates id_dst{};
101 
102  BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(arm_compute::LogicalOperation::Or, src1, src2, dst, id_src1, id_src2, id_dst);
103 
104  return dst;
105 }
106 
107 template <typename T>
109 {
110  Coordinates id_src1{};
111  Coordinates id_src2{};
112  Coordinates id_dst{};
114 
115  BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(arm_compute::LogicalOperation::And, src1, src2, dst, id_src1, id_src2, id_dst);
116 
117  return dst;
118 }
119 
120 template <typename T>
122 {
123  SimpleTensor<T> dst(src.shape(), src.data_type());
124 #if defined(_OPENMP)
125  #pragma omp parallel for
126 #endif /* _OPENMP */
127  for(int i = 0; i < src.num_elements(); ++i)
128  {
129  dst[i] = !src[i];
130  }
131 
132  return dst;
133 }
134 
138 } // namespace reference
139 } // namespace validation
140 } // namespace test
141 } // namespace arm_compute
void set(size_t dimension, T value, bool increase_dim_unit=true)
Accessor to set the value of one of the dimensions.
Definition: Dimensions.h:76
SimpleTensor< T > logical_not(const SimpleTensor< T > &src)
Definition: Logical.cpp:121
DataType data_type() const override
Data type of the tensor.
Definition: SimpleTensor.h:357
static TensorShape broadcast_shape(const Shapes &... shapes)
If shapes are broadcast compatible, return the broadcasted shape.
Definition: TensorShape.h:211
TensorShape shape() const override
Shape of the tensor.
Definition: SimpleTensor.h:320
SimpleTensor< T > logical_or(const SimpleTensor< T > &src1, const SimpleTensor< T > &src2)
Definition: Logical.cpp:95
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2021 Arm Limited.
int coord2index(const TensorShape &shape, const Coordinates &coord)
Linearise the given coordinate.
Definition: Utils.h:489
Coordinates of an item.
Definition: Coordinates.h:37
T logical_binary_op(arm_compute::LogicalOperation op, T src1, T src2)
Definition: Logical.cpp:37
Simple tensor object that stores elements in a consecutive chunk of memory.
Definition: SimpleTensor.h:58
SimpleTensor< T > logical_and(const SimpleTensor< T > &src1, const SimpleTensor< T > &src2)
Definition: Logical.cpp:108
int num_elements() const override
Number of elements of the tensor.
Definition: SimpleTensor.h:406
#define ARM_COMPUTE_ASSERT(cond)
Definition: Validate.h:37
LogicalOperation
List of supported logical operations.
Definition: KernelTypes.h:30