2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * Copyright (c) 2018-2019 ARM Limited.
20 * SPDX-License-Identifier: MIT
22 * Permission is hereby granted, free of charge, to any person obtaining a copy
23 * of this software and associated documentation files (the "Software"), to
24 * deal in the Software without restriction, including without limitation the
25 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
26 * sell copies of the Software, and to permit persons to whom the Software is
27 * furnished to do so, subject to the following conditions:
29 * The above copyright notice and this permission notice shall be included in all
30 * copies or substantial portions of the Software.
32 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
41 #include "arm_compute/core/NEON/kernels/NEBinaryLogicalOperationKernel.h"
43 #include "arm_compute/core/Error.h"
44 #include "arm_compute/core/Helpers.h"
45 #include "arm_compute/core/ITensor.h"
46 #include "arm_compute/core/NEON/wrapper/wrapper.h"
47 #include "arm_compute/core/NEON/NEElementwiseOperationFuncs.h"
48 #include "arm_compute/core/TensorInfo.h"
49 #include "arm_compute/core/Validate.h"
59 } // namespace arm_compute
64 template <BinaryLogicalOperation op, typename ScalarType>
65 inline ScalarType elementwise_logic_op_scalar(const ScalarType &a, const ScalarType &b)
67 auto res = ScalarType(0);
71 case BinaryLogicalOperation::AND:
74 case BinaryLogicalOperation::OR:
78 ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
83 template <BinaryLogicalOperation op, typename VectorType>
84 inline VectorType elementwise_logic_op(const VectorType &a, const VectorType &b)
86 VectorType res = {0, 0, 0, 0};
90 case BinaryLogicalOperation::AND:
91 res = wrapper::vand(a, b);
93 case BinaryLogicalOperation::OR:
94 res = wrapper::vorr(a, b);
97 ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
102 template <BinaryLogicalOperation op>
103 inline uint8x16x4_t elementwise_logic_op(const uint8x16x4_t &a, const uint8x16x4_t &b)
105 uint8x16x4_t out = {{
106 elementwise_logic_op<op>(a.val[0], b.val[0]), elementwise_logic_op<op>(a.val[1], b.val[1]),
107 elementwise_logic_op<op>(a.val[2], b.val[2]), elementwise_logic_op<op>(a.val[3], b.val[3]),
112 template <BinaryLogicalOperation op, typename ScalarType, typename VectorType>
113 inline VectorType elementwise_logic_op_broadcast(const VectorType &a,
114 const ScalarType &broadcast_value,
117 VectorType broadcast_vector = wrapper::vdup_n(broadcast_value, wrapper::traits::vector_128_tag());
118 return elementwise_logic_op<op>(reorder ? broadcast_vector : a, reorder ? a : broadcast_vector);
121 template <BinaryLogicalOperation op, typename ScalarType, typename VectorType>
122 inline int elementwise_logic_op_loop(int window_start_x, int window_end_x, int window_step_x,
123 const ScalarType *input1_ptr, const ScalarType *input2_ptr,
124 ScalarType *output_ptr)
126 int x = window_start_x;
127 for (; x <= (window_end_x - window_step_x); x += window_step_x)
129 const auto a = wrapper::vloadq(input1_ptr + x);
130 const auto b = wrapper::vloadq(input2_ptr + x);
131 wrapper::vstore(output_ptr + x, elementwise_logic_op<op>(a, b));
136 template <BinaryLogicalOperation op, typename ScalarType, typename VectorType>
137 inline int elementwise_logic_op_broadcast_loop(int window_start_x, int window_end_x,
139 const ScalarType *non_broadcast_input_ptr,
140 const ScalarType &broadcast_value,
141 ScalarType *output_ptr, const bool reorder)
143 int x = window_start_x;
144 for (; x <= (window_end_x - window_step_x); x += window_step_x)
146 const auto a = wrapper::vloadq((non_broadcast_input_ptr + x));
147 wrapper::vstore(output_ptr + x,
148 elementwise_logic_op_broadcast<op>(a, broadcast_value, reorder));
153 template <BinaryLogicalOperation op, typename ScalarType, typename VectorType>
154 void elementwise_logic_op(const ITensor *in1, const ITensor *in2, ITensor *out,
155 const Window &window)
157 elementwise_op(in1, in2, out, window, &elementwise_logic_op_scalar<op, ScalarType>,
158 &elementwise_logic_op_broadcast_loop<op, ScalarType, VectorType>,
159 &elementwise_logic_op_loop<op, ScalarType, VectorType>);
162 std::function<void(const ITensor *, const ITensor *, ITensor *, const Window &)> configure_func(
163 const ITensor *input1, const ITensor *input2, ITensor *output,
164 std::map<std::string, NEElementwiseOperationKernel::ElementwiseFunction *> map_function)
166 std::string function_to_call("op_");
167 function_to_call += string_from_data_type(input1->info()->data_type()) + "_";
168 function_to_call += string_from_data_type(input2->info()->data_type()) + "_";
169 function_to_call += string_from_data_type(output->info()->data_type());
171 auto it = map_function.find(function_to_call);
173 if (it != map_function.end())
175 auto func = it->second;
176 return [func](const ITensor *input1, const ITensor *input2, ITensor *output,
177 const Window &window) { func(input1, input2, output, window); };
182 template <BinaryLogicalOperation op>
183 std::function<void(const ITensor *, const ITensor *, ITensor *, const Window &)>
184 configure_logic_func(const ITensor *input1, const ITensor *input2, ITensor *output)
186 static std::map<std::string, NEElementwiseOperationKernel::ElementwiseFunction *> map_function = {
187 {"op_U8_U8_U8", &elementwise_logic_op<op, uint8_t, uint8x16_t>},
188 {"op_QASYMM8_QASYMM8_QASYMM8", &elementwise_logic_op<op, uint8_t, uint8x16_t>}};
190 return configure_func(input1, input2, output, map_function);
193 void NEBinaryLogicalOperationKernel::configure(BinaryLogicalOperation op, const ITensor *input1,
194 const ITensor *input2, ITensor *output)
196 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*input1->info(), *input2->info(), *output->info()));
197 configure_common(input1, input2, output);
200 case BinaryLogicalOperation::AND:
201 _function = configure_logic_func<BinaryLogicalOperation::AND>(input1, input2, output);
203 case BinaryLogicalOperation::OR:
204 _function = configure_logic_func<BinaryLogicalOperation::OR>(input1, input2, output);
207 ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
211 Status NEBinaryLogicalOperationKernel::validate_arguments(const ITensorInfo &input1,
212 const ITensorInfo &input2,
213 const ITensorInfo &output)
215 // Validate in case of configured output
216 if (output.total_size() > 0)
218 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&output, 1, DataType::U8,
221 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input1, 1, DataType::U8, DataType::QASYMM8);
222 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input2, 1, DataType::U8, DataType::QASYMM8);
223 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&input1, &input2);
225 const TensorShape out_shape =
226 TensorShape::broadcast_shape(input1.tensor_shape(), input2.tensor_shape());
228 ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0,
229 "Inputs are not broadcast compatible");
231 // Validate in case of configured output
232 if (output.total_size() > 0)
234 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
235 detail::have_different_dimensions(out_shape, output.tensor_shape(), 0),
236 "Wrong shape for output");
242 Status NEBinaryLogicalOperationKernel::validate(BinaryLogicalOperation op,
243 const ITensorInfo *input1,
244 const ITensorInfo *input2,
245 const ITensorInfo *output)
247 ARM_COMPUTE_UNUSED(op);
248 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output);
249 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*input1, *input2, *output));
253 } // namespace arm_compute