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]),
107 elementwise_logic_op<op>(a.val[1], b.val[1]),
108 elementwise_logic_op<op>(a.val[2], b.val[2]),
109 elementwise_logic_op<op>(a.val[3], b.val[3]),
114 template <BinaryLogicalOperation op, typename ScalarType, typename VectorType>
115 inline VectorType elementwise_logic_op_broadcast(const VectorType &a,
116 const ScalarType &broadcast_value,
119 VectorType broadcast_vector = wrapper::vdup_n(broadcast_value, wrapper::traits::vector_128_tag());
120 return elementwise_logic_op<op>(reorder ? broadcast_vector : a, reorder ? a : broadcast_vector);
123 template <BinaryLogicalOperation op, typename ScalarType, typename VectorType>
124 inline int elementwise_logic_op_loop(int window_start_x, int window_end_x, int window_step_x,
125 const ScalarType *input1_ptr, const ScalarType *input2_ptr,
126 ScalarType *output_ptr)
128 int x = window_start_x;
129 for (; x <= (window_end_x - window_step_x); x += window_step_x)
131 const auto a = wrapper::vloadq(input1_ptr + x);
132 const auto b = wrapper::vloadq(input2_ptr + x);
133 wrapper::vstore(output_ptr + x, elementwise_logic_op<op>(a, b));
138 template <BinaryLogicalOperation op, typename ScalarType, typename VectorType>
139 inline int elementwise_logic_op_broadcast_loop(int window_start_x, int window_end_x,
141 const ScalarType *non_broadcast_input_ptr,
142 const ScalarType &broadcast_value,
143 ScalarType *output_ptr, const bool reorder)
145 int x = window_start_x;
146 for (; x <= (window_end_x - window_step_x); x += window_step_x)
148 const auto a = wrapper::vloadq((non_broadcast_input_ptr + x));
149 wrapper::vstore(output_ptr + x,
150 elementwise_logic_op_broadcast<op>(a, broadcast_value, reorder));
155 template <BinaryLogicalOperation op, typename ScalarType, typename VectorType>
156 void elementwise_logic_op(const ITensor *in1, const ITensor *in2, ITensor *out,
157 const Window &window)
159 elementwise_op(in1, in2, out, window, &elementwise_logic_op_scalar<op, ScalarType>,
160 &elementwise_logic_op_broadcast_loop<op, ScalarType, VectorType>,
161 &elementwise_logic_op_loop<op, ScalarType, VectorType>);
164 std::function<void(const ITensor *, const ITensor *, ITensor *, const Window &)> configure_func(
165 const ITensor *input1, const ITensor *input2, ITensor *output,
166 std::map<std::string, NEElementwiseOperationKernel::ElementwiseFunction *> map_function)
168 std::string function_to_call("op_");
169 function_to_call += string_from_data_type(input1->info()->data_type()) + "_";
170 function_to_call += string_from_data_type(input2->info()->data_type()) + "_";
171 function_to_call += string_from_data_type(output->info()->data_type());
173 auto it = map_function.find(function_to_call);
175 if (it != map_function.end())
177 auto func = it->second;
178 return [func](const ITensor *input1, const ITensor *input2, ITensor *output,
179 const Window &window) { func(input1, input2, output, window); };
184 template <BinaryLogicalOperation op>
185 std::function<void(const ITensor *, const ITensor *, ITensor *, const Window &)>
186 configure_logic_func(const ITensor *input1, const ITensor *input2, ITensor *output)
188 static std::map<std::string, NEElementwiseOperationKernel::ElementwiseFunction *> map_function = {
189 {"op_U8_U8_U8", &elementwise_logic_op<op, uint8_t, uint8x16_t>},
190 {"op_QASYMM8_QASYMM8_QASYMM8", &elementwise_logic_op<op, uint8_t, uint8x16_t>}};
192 return configure_func(input1, input2, output, map_function);
195 void NEBinaryLogicalOperationKernel::configure(BinaryLogicalOperation op, const ITensor *input1,
196 const ITensor *input2, ITensor *output)
198 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*input1->info(), *input2->info(), *output->info()));
199 configure_common(input1, input2, output);
202 case BinaryLogicalOperation::AND:
203 _function = configure_logic_func<BinaryLogicalOperation::AND>(input1, input2, output);
205 case BinaryLogicalOperation::OR:
206 _function = configure_logic_func<BinaryLogicalOperation::OR>(input1, input2, output);
209 ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
213 Status NEBinaryLogicalOperationKernel::validate_arguments(const ITensorInfo &input1,
214 const ITensorInfo &input2,
215 const ITensorInfo &output)
217 // Validate in case of configured output
218 if (output.total_size() > 0)
220 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&output, 1, DataType::U8,
223 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input1, 1, DataType::U8, DataType::QASYMM8);
224 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input2, 1, DataType::U8, DataType::QASYMM8);
225 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&input1, &input2);
227 const TensorShape out_shape =
228 TensorShape::broadcast_shape(input1.tensor_shape(), input2.tensor_shape());
230 ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0,
231 "Inputs are not broadcast compatible");
233 // Validate in case of configured output
234 if (output.total_size() > 0)
236 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
237 detail::have_different_dimensions(out_shape, output.tensor_shape(), 0),
238 "Wrong shape for output");
244 Status NEBinaryLogicalOperationKernel::validate(BinaryLogicalOperation op,
245 const ITensorInfo *input1,
246 const ITensorInfo *input2,
247 const ITensorInfo *output)
249 ARM_COMPUTE_UNUSED(op);
250 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output);
251 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*input1, *input2, *output));
255 } // namespace arm_compute