blob: 6be628452837d1d424b0528f941fe27c5fe63035 [file] [log] [blame]
Georgios Pinitas58bce682020-11-13 11:38:58 +00001/*
Georgios Pinitas8a5146f2021-01-12 15:51:07 +00002 * Copyright (c) 2020-2021 Arm Limited.
Georgios Pinitas58bce682020-11-13 11:38:58 +00003 *
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 "src/core/NEON/kernels/NELogicalKernel.h"
25
26#include "arm_compute/core/Helpers.h"
27#include "arm_compute/core/Validate.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010028
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000029#include "src/common/utils/Validate.h"
Georgios Pinitas58bce682020-11-13 11:38:58 +000030#include "src/core/helpers/AutoConfiguration.h"
31#include "src/core/helpers/WindowHelpers.h"
32
33#include <arm_neon.h>
34
35namespace arm_compute
36{
37namespace kernels
38{
39namespace
40{
41static const uint8x8_t c0_x8 = vdup_n_u8(0);
42static const uint8x16_t c0_x16 = vdupq_n_u8(0);
43static const uint8x8_t c1_x8 = vdup_n_u8(1);
44static const uint8x16_t c1_x16 = vdupq_n_u8(1);
Michalis Spyrouc89998f2021-08-26 14:11:44 +010045static const uint32_t step = 16;
46static const uint32_t half_step = step / 2;
Georgios Pinitas58bce682020-11-13 11:38:58 +000047
Michalis Spyrouc89998f2021-08-26 14:11:44 +010048void neon_logical_and(const uint8_t *src0, const uint8_t *src1, uint8_t *dst, uint32_t len)
Georgios Pinitas58bce682020-11-13 11:38:58 +000049{
50 ARM_COMPUTE_ASSERT_NOT_NULLPTR(src0);
51 ARM_COMPUTE_ASSERT_NOT_NULLPTR(src1);
52 ARM_COMPUTE_ASSERT_NOT_NULLPTR(dst);
Georgios Pinitas58bce682020-11-13 11:38:58 +000053
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010054 for (; len >= step; len -= step)
Georgios Pinitas58bce682020-11-13 11:38:58 +000055 {
56 vst1q_u8(dst, vandq_u8(vminq_u8(vld1q_u8(src0), c1_x16), vminq_u8(vld1q_u8(src1), c1_x16)));
57 src0 += step;
58 src1 += step;
59 dst += step;
60 }
61
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010062 for (; len >= half_step; len -= half_step)
Georgios Pinitas58bce682020-11-13 11:38:58 +000063 {
64 vst1_u8(dst, vand_u8(vmin_u8(vld1_u8(src0), c1_x8), vmin_u8(vld1_u8(src1), c1_x8)));
65 src0 += half_step;
66 src1 += half_step;
67 dst += half_step;
68 }
69
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010070 for (; len > 0; --len)
Georgios Pinitas58bce682020-11-13 11:38:58 +000071 {
72 *dst = (*src0) && (*src1);
73 ++src0;
74 ++src1;
75 ++dst;
76 }
77}
78
Michalis Spyrouc89998f2021-08-26 14:11:44 +010079void neon_logical_and_broadcast(const uint8_t *src, uint8_t broadcast_val, uint8_t *dst, uint32_t len)
Georgios Pinitas58bce682020-11-13 11:38:58 +000080{
81 ARM_COMPUTE_ASSERT_NOT_NULLPTR(src);
82 ARM_COMPUTE_ASSERT_NOT_NULLPTR(dst);
Georgios Pinitas58bce682020-11-13 11:38:58 +000083
84 const auto broadcast_val_clamped_s = std::min<uint8_t>(broadcast_val, 1);
85 const auto broadcast_val_clamped_x16 = vdupq_n_u8(broadcast_val_clamped_s);
86 const auto broadcast_val_clamped_x8 = vdup_n_u8(broadcast_val_clamped_s);
87
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010088 for (; len >= step; len -= step)
Georgios Pinitas58bce682020-11-13 11:38:58 +000089 {
90 vst1q_u8(dst, vandq_u8(vminq_u8(vld1q_u8(src), c1_x16), broadcast_val_clamped_x16));
91 src += step;
92 dst += step;
93 }
94
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010095 for (; len >= half_step; len -= half_step)
Georgios Pinitas58bce682020-11-13 11:38:58 +000096 {
97 vst1_u8(dst, vand_u8(vmin_u8(vld1_u8(src), c1_x8), broadcast_val_clamped_x8));
98 src += half_step;
99 dst += half_step;
100 }
101
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100102 for (; len > 0; --len)
Georgios Pinitas58bce682020-11-13 11:38:58 +0000103 {
104 *dst = (*src) && broadcast_val_clamped_s;
105 ++src;
106 ++dst;
107 }
108}
109
Michalis Spyrouc89998f2021-08-26 14:11:44 +0100110void neon_logical_or(const uint8_t *src0, const uint8_t *src1, uint8_t *dst, uint32_t len)
Georgios Pinitas58bce682020-11-13 11:38:58 +0000111{
112 ARM_COMPUTE_ASSERT_NOT_NULLPTR(src0);
113 ARM_COMPUTE_ASSERT_NOT_NULLPTR(src1);
114 ARM_COMPUTE_ASSERT_NOT_NULLPTR(dst);
Georgios Pinitas58bce682020-11-13 11:38:58 +0000115
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100116 for (; len >= step; len -= step)
Georgios Pinitas58bce682020-11-13 11:38:58 +0000117 {
118 vst1q_u8(dst, vorrq_u8(vminq_u8(vld1q_u8(src0), c1_x16), vminq_u8(vld1q_u8(src1), c1_x16)));
119 src0 += step;
120 src1 += step;
121 dst += step;
122 }
123
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100124 for (; len >= half_step; len -= half_step)
Georgios Pinitas58bce682020-11-13 11:38:58 +0000125 {
126 vst1_u8(dst, vorr_u8(vmin_u8(vld1_u8(src0), c1_x8), vmin_u8(vld1_u8(src1), c1_x8)));
127 src0 += half_step;
128 src1 += half_step;
129 dst += half_step;
130 }
131
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100132 for (; len > 0; --len)
Georgios Pinitas58bce682020-11-13 11:38:58 +0000133 {
134 *dst = (*src0) || (*src1);
135 ++src0;
136 ++src1;
137 ++dst;
138 }
139}
140
Michalis Spyrouc89998f2021-08-26 14:11:44 +0100141void neon_logical_or_broadcast(const uint8_t *src, uint8_t broadcast_val, uint8_t *dst, uint32_t len)
Georgios Pinitas58bce682020-11-13 11:38:58 +0000142{
143 ARM_COMPUTE_ASSERT_NOT_NULLPTR(src);
144 ARM_COMPUTE_ASSERT_NOT_NULLPTR(dst);
Georgios Pinitas58bce682020-11-13 11:38:58 +0000145
146 const auto broadcast_val_clamped_s = std::min<uint8_t>(broadcast_val, 1);
147 const auto broadcast_val_clamped_x16 = vdupq_n_u8(broadcast_val_clamped_s);
148 const auto broadcast_val_clamped_x8 = vdup_n_u8(broadcast_val_clamped_s);
149
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100150 for (; len >= step; len -= step)
Georgios Pinitas58bce682020-11-13 11:38:58 +0000151 {
152 vst1q_u8(dst, vorrq_u8(vminq_u8(vld1q_u8(src), c1_x16), broadcast_val_clamped_x16));
153 src += step;
154 dst += step;
155 }
156
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100157 for (; len >= half_step; len -= half_step)
Georgios Pinitas58bce682020-11-13 11:38:58 +0000158 {
159 vst1_u8(dst, vorr_u8(vmin_u8(vld1_u8(src), c1_x8), broadcast_val_clamped_x8));
160 src += half_step;
161 dst += half_step;
162 }
163
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100164 for (; len > 0; --len)
Georgios Pinitas58bce682020-11-13 11:38:58 +0000165 {
166 *dst = (*src) || broadcast_val_clamped_s;
167 ++src;
168 ++dst;
169 }
170}
171
Michalis Spyrouc89998f2021-08-26 14:11:44 +0100172void neon_logical_not(const uint8_t *src, uint8_t *dst, uint32_t len)
Georgios Pinitas58bce682020-11-13 11:38:58 +0000173{
174 ARM_COMPUTE_ASSERT_NOT_NULLPTR(src);
175 ARM_COMPUTE_ASSERT_NOT_NULLPTR(dst);
Georgios Pinitas58bce682020-11-13 11:38:58 +0000176
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100177 for (; len >= step; len -= step)
Georgios Pinitas58bce682020-11-13 11:38:58 +0000178 {
179 vst1q_u8(dst, vbslq_u8(vceqq_u8(vld1q_u8(src), c0_x16), c1_x16, c0_x16));
180 src += step;
181 dst += step;
182 }
183
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100184 for (; len >= half_step; len -= half_step)
Georgios Pinitas58bce682020-11-13 11:38:58 +0000185 {
186 vst1_u8(dst, vbsl_u8(vceq_u8(vld1_u8(src), c0_x8), c1_x8, c0_x8));
187 src += half_step;
188 dst += half_step;
189 }
190
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100191 for (; len > 0; --len)
Georgios Pinitas58bce682020-11-13 11:38:58 +0000192 {
193 *dst = !(*src);
194 ++src;
195 ++dst;
196 }
197}
198
199void run_unary(const Window &window, const ITensor *src, ITensor *dst)
200{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100201 Window win{window};
Georgios Pinitas58bce682020-11-13 11:38:58 +0000202 win.set(Window::DimX, Window::Dimension(0, 1, 1));
Michalis Spyrouc89998f2021-08-26 14:11:44 +0100203 const auto len = window.x().end() - window.x().start();
Georgios Pinitas58bce682020-11-13 11:38:58 +0000204
205 Iterator in(src, win);
206 Iterator out(dst, win);
207
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100208 execute_window_loop(
209 win, [&](const Coordinates &) { neon_logical_not(in.ptr(), out.ptr(), len); }, in, out);
Georgios Pinitas58bce682020-11-13 11:38:58 +0000210}
211
212void run_binary(const Window &window, const ITensor *src0, const ITensor *src1, ITensor *dst, LogicalOperation op)
213{
214 Window src0_win = window.broadcast_if_dimension_le_one(src0->info()->tensor_shape());
215 Window src1_win = window.broadcast_if_dimension_le_one(src1->info()->tensor_shape());
216
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100217 Window win{window};
Georgios Pinitas58bce682020-11-13 11:38:58 +0000218 win.set(Window::DimX, Window::Dimension(0, 1, 1));
219
220 const bool is_broadcast_across_x = src0->info()->tensor_shape().x() != src1->info()->tensor_shape().x();
Michalis Spyrouc89998f2021-08-26 14:11:44 +0100221 const auto len = window.x().end() - window.x().start();
Georgios Pinitas58bce682020-11-13 11:38:58 +0000222
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100223 if (is_broadcast_across_x)
Georgios Pinitas58bce682020-11-13 11:38:58 +0000224 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100225 using LogicalBroadcastUKernelPtr = std::add_pointer<void(const uint8_t *, uint8_t, uint8_t *, uint32_t)>::type;
226 LogicalBroadcastUKernelPtr logical_func =
227 op == LogicalOperation::Or ? &neon_logical_or_broadcast : &neon_logical_and_broadcast;
Georgios Pinitas58bce682020-11-13 11:38:58 +0000228
229 const bool is_broadcast_input_1 = src1_win.x().step() == 0;
230 Window broadcast_win = is_broadcast_input_1 ? src1_win : src0_win;
231 Window non_broadcast_win = !is_broadcast_input_1 ? src1_win : src0_win;
232 const ITensor *broadcast_tensor = is_broadcast_input_1 ? src1 : src0;
233 const ITensor *non_broadcast_tensor = !is_broadcast_input_1 ? src1 : src0;
234 non_broadcast_win.set(Window::DimX, Window::Dimension(0, 1, 1));
235
236 Iterator broadcast_in(broadcast_tensor, broadcast_win);
237 Iterator non_broadcast_in(non_broadcast_tensor, non_broadcast_win);
238 Iterator out(dst, win);
239
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100240 execute_window_loop(
241 win,
242 [&](const Coordinates &)
243 {
244 const uint8_t broadcast_value = *broadcast_in.ptr();
245 logical_func(non_broadcast_in.ptr(), broadcast_value, out.ptr(), len);
246 },
247 broadcast_in, non_broadcast_in, out);
Georgios Pinitas58bce682020-11-13 11:38:58 +0000248 }
249 else
250 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100251 using LogicalUKernelPtr = std::add_pointer<void(const uint8_t *, const uint8_t *, uint8_t *, uint32_t)>::type;
Georgios Pinitas58bce682020-11-13 11:38:58 +0000252 LogicalUKernelPtr logical_func = op == LogicalOperation::Or ? &neon_logical_or : &neon_logical_and;
253
254 src0_win.set(Window::DimX, Window::Dimension(0, 1, 1));
255 src1_win.set(Window::DimX, Window::Dimension(0, 1, 1));
256
257 Iterator in0(src0, src0_win);
258 Iterator in1(src1, src1_win);
259 Iterator out(dst, win);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100260 execute_window_loop(
261 win, [&](const Coordinates &) { logical_func(in0.ptr(), in1.ptr(), out.ptr(), len); }, in0, in1, out);
Georgios Pinitas58bce682020-11-13 11:38:58 +0000262 }
263}
264} // namespace
265const char *NELogicalKernel::name() const
266{
267 return "NELogicalKernel";
268}
269
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100270void NELogicalKernel::configure(const ITensorInfo *input1,
271 const ITensorInfo *input2,
272 ITensorInfo *output,
273 LogicalOperation op)
Georgios Pinitas58bce682020-11-13 11:38:58 +0000274{
275 ARM_COMPUTE_ERROR_ON_NULLPTR(input1, output);
276 ARM_COMPUTE_ERROR_THROW_ON(validate(input1, input2, output, op));
277
278 _op = op;
279
280 Window win = calculate_max_window(*input1, Steps());
281 TensorShape out_shape = input1->tensor_shape();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100282 if (op != LogicalOperation::Not)
Georgios Pinitas58bce682020-11-13 11:38:58 +0000283 {
284 ARM_COMPUTE_ERROR_ON_NULLPTR(input2);
SiCongLic7b1e842021-02-22 14:28:33 +0000285 out_shape = TensorShape::broadcast_shape(input1->tensor_shape(), input2->tensor_shape());
286 win = calculate_max_window(out_shape, Steps());
Georgios Pinitas58bce682020-11-13 11:38:58 +0000287 }
288 ICPPKernel::configure(win);
289
290 // Auto initialize if empty
291 set_shape_if_empty(*output, out_shape);
292 set_data_type_if_unknown(*output, input1->data_type());
293}
294
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100295Status NELogicalKernel::validate(const ITensorInfo *input1,
296 const ITensorInfo *input2,
297 const ITensorInfo *output,
298 LogicalOperation op)
Georgios Pinitas58bce682020-11-13 11:38:58 +0000299{
300 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input1, 1, DataType::U8);
301 ARM_COMPUTE_RETURN_ERROR_ON(op == LogicalOperation::Unknown);
302
303 TensorShape out_shape = input1->tensor_shape();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100304 if (op != LogicalOperation::Not)
Georgios Pinitas58bce682020-11-13 11:38:58 +0000305 {
306 out_shape = TensorShape::broadcast_shape(input1->tensor_shape(), input2->tensor_shape());
307 ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible");
308 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input1, input2);
309 }
310
311 // Checks performed when output is configured
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100312 if ((output != nullptr) && (output->total_size() != 0))
Georgios Pinitas58bce682020-11-13 11:38:58 +0000313 {
314 ARM_COMPUTE_RETURN_ERROR_ON(detail::have_different_dimensions(out_shape, output->tensor_shape(), 0));
315 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input1, output);
316 }
317
318 return Status{};
319}
320
321void NELogicalKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
322{
323 ARM_COMPUTE_UNUSED(info);
324 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
325 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
326 ARM_COMPUTE_ERROR_ON(tensors.empty());
327
328 const ITensor *src0 = tensors.get_const_tensor(TensorType::ACL_SRC_0);
329 const ITensor *src1 = tensors.get_const_tensor(TensorType::ACL_SRC_1);
330 ITensor *dst = tensors.get_tensor(TensorType::ACL_DST);
331
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100332 if (_op == LogicalOperation::Not)
Georgios Pinitas58bce682020-11-13 11:38:58 +0000333 {
334 run_unary(window, src0, dst);
335 }
336 else
337 {
338 run_binary(window, src0, src1, dst, _op);
339 }
340}
341} // namespace kernels
342} // namespace arm_compute