blob: 85f72c1421160e6f5581d24f6166d0248a7105b8 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2016, 2017 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 "arm_compute/core/NEON/kernels/NEArithmeticSubtractionKernel.h"
25
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/ITensor.h"
Michele Di Giorgio81f0d152017-07-11 15:00:52 +010029#include "arm_compute/core/NEON/NEFixedPoint.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/TensorInfo.h"
31#include "arm_compute/core/Validate.h"
32
33#include <algorithm>
34#include <arm_neon.h>
35#include <cstdint>
36#include <map>
37#include <string>
38
39using namespace arm_compute;
40
41namespace arm_compute
42{
43class Coordinates;
44} // namespace arm_compute
45
46namespace
47{
Michele Di Giorgio81f0d152017-07-11 15:00:52 +010048void sub_wrap_QS8_QS8_QS8(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
49{
50 Iterator input1(in1, window);
51 Iterator input2(in2, window);
52 Iterator output(out, window);
53
54 execute_window_loop(window, [&](const Coordinates & id)
55 {
56 const qint8x16_t a = vld1q_qs8(reinterpret_cast<const qint8_t *>(input1.ptr()));
57 const qint8x16_t b = vld1q_qs8(reinterpret_cast<const qint8_t *>(input2.ptr()));
58
59 vst1q_qs8(reinterpret_cast<qint8_t *>(output.ptr()), vsubq_qs8(a, b));
60 },
61 input1, input2, output);
62}
63
64void sub_saturate_QS8_QS8_QS8(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
65{
66 Iterator input1(in1, window);
67 Iterator input2(in2, window);
68 Iterator output(out, window);
69
70 execute_window_loop(window, [&](const Coordinates & id)
71 {
72 const qint8x16_t a = vld1q_qs8(reinterpret_cast<const qint8_t *>(input1.ptr()));
73 const qint8x16_t b = vld1q_qs8(reinterpret_cast<const qint8_t *>(input2.ptr()));
74
75 vst1q_qs8(reinterpret_cast<qint8_t *>(output.ptr()), vqsubq_qs8(a, b));
76 },
77 input1, input2, output);
78}
79
Anthony Barbier6ff3b192017-09-04 18:44:23 +010080void sub_wrap_U8_U8_U8(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
81{
82 Iterator input1(in1, window);
83 Iterator input2(in2, window);
84 Iterator output(out, window);
85
86 execute_window_loop(window, [&](const Coordinates & id)
87 {
88 const uint8x16_t ta1 = vld1q_u8(input1.ptr());
89 const uint8x16_t ta2 = vld1q_u8(input2.ptr());
90
91 vst1q_u8(output.ptr(), vsubq_u8(ta1, ta2));
92 },
93 input1, input2, output);
94}
95
96void sub_saturate_U8_U8_U8(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
97{
98 Iterator input1(in1, window);
99 Iterator input2(in2, window);
100 Iterator output(out, window);
101
102 execute_window_loop(window, [&](const Coordinates & id)
103 {
104 const uint8x16_t ta1 = vld1q_u8(input1.ptr());
105 const uint8x16_t ta2 = vld1q_u8(input2.ptr());
106
107 vst1q_u8(output.ptr(), vqsubq_u8(ta1, ta2));
108 },
109 input1, input2, output);
110}
111
112void sub_wrap_S16_S16_S16(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
113{
114 Iterator input1(in1, window);
115 Iterator input2(in2, window);
116 Iterator output(out, window);
117
118 execute_window_loop(window, [&](const Coordinates & id)
119 {
120 const int16x8x2_t ta1 = vld2q_s16(reinterpret_cast<const int16_t *>(input1.ptr()));
121 const int16x8x2_t ta2 = vld2q_s16(reinterpret_cast<const int16_t *>(input2.ptr()));
122
123 const int16x8x2_t ta3 =
124 {
125 {
126 vsubq_s16(ta1.val[0], ta2.val[0]),
127 vsubq_s16(ta1.val[1], ta2.val[1])
128 }
129 };
130
131 vst2q_s16(reinterpret_cast<int16_t *>(output.ptr()), ta3);
132 },
133 input1, input2, output);
134}
135
136void sub_saturate_S16_S16_S16(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
137{
138 Iterator input1(in1, window);
139 Iterator input2(in2, window);
140 Iterator output(out, window);
141
142 execute_window_loop(window, [&](const Coordinates & id)
143 {
144 const int16x8x2_t ta1 = vld2q_s16(reinterpret_cast<const int16_t *>(input1.ptr()));
145 const int16x8x2_t ta2 = vld2q_s16(reinterpret_cast<const int16_t *>(input2.ptr()));
146
147 const int16x8x2_t ta3 =
148 {
149 {
150 vqsubq_s16(ta1.val[0], ta2.val[0]),
151 vqsubq_s16(ta1.val[1], ta2.val[1])
152 }
153 };
154
155 vst2q_s16(reinterpret_cast<int16_t *>(output.ptr()), ta3);
156 },
157 input1, input2, output);
158}
159
Pablo Tellod7a5d222017-07-11 13:54:43 +0100160#ifdef ARM_COMPUTE_ENABLE_FP16
161inline float16x8x2_t vsub2q_f16(const float16x8x2_t &a, const float16x8x2_t &b)
162{
163 const float16x8x2_t res =
164 {
165 {
166 vsubq_f16(a.val[0], b.val[0]),
167 vsubq_f16(a.val[1], b.val[1])
168 }
169 };
170
171 return res;
172}
173#endif /* ARM_COMPUTE_ENABLE_FP16 */
174
175void sub_F16_F16_F16(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
176{
177#ifdef ARM_COMPUTE_ENABLE_FP16
178 Iterator input1(in1, window);
179 Iterator input2(in2, window);
180 Iterator output(out, window);
181
182 execute_window_loop(window, [&](const Coordinates & id)
183 {
184 const float16x8x2_t a = vld2q_f16(reinterpret_cast<const float16_t *>(input1.ptr()));
185 const float16x8x2_t b = vld2q_f16(reinterpret_cast<const float16_t *>(input2.ptr()));
186
187 vst2q_f16(reinterpret_cast<float16_t *>(output.ptr()), vsub2q_f16(a, b));
188 },
189 input1, input2, output);
190#else /* ARM_COMPUTE_ENABLE_FP16 */
191 ARM_COMPUTE_UNUSED(in1);
192 ARM_COMPUTE_UNUSED(in2);
193 ARM_COMPUTE_UNUSED(out);
194 ARM_COMPUTE_UNUSED(window);
195 ARM_COMPUTE_ERROR("Not supported, recompile the library with arch=arm64-v8.2-a");
196#endif /* ARM_COMPUTE_ENABLE_FP16 */
197}
198
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100199void sub_F32_F32_F32(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
200{
201 Iterator input1(in1, window);
202 Iterator input2(in2, window);
203 Iterator output(out, window);
204
205 execute_window_loop(window, [&](const Coordinates & id)
206 {
207 const float32x4x4_t ta1 = vld4q_f32(reinterpret_cast<const float *>(input1.ptr()));
208 const float32x4x4_t ta2 = vld4q_f32(reinterpret_cast<const float *>(input2.ptr()));
209
210 const float32x4x4_t ta3 =
211 {
212 {
213 vsubq_f32(ta1.val[0], ta2.val[0]),
214 vsubq_f32(ta1.val[1], ta2.val[1]),
215 vsubq_f32(ta1.val[2], ta2.val[2]),
216 vsubq_f32(ta1.val[3], ta2.val[3]),
217 }
218 };
219
220 vst4q_f32(reinterpret_cast<float *>(output.ptr()), ta3);
221 },
222 input1, input2, output);
223}
224void sub_wrap_S16_U8_S16(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
225{
226 Iterator input1(in1, window);
227 Iterator input2(in2, window);
228 Iterator output(out, window);
229
230 execute_window_loop(window, [&](const Coordinates & id)
231 {
232 const uint8x16_t bv_0 = vld1q_u8(input2.ptr());
233 int16x8_t a1_0 = vld1q_s16(reinterpret_cast<const int16_t *>(input1.ptr()));
234 int16x8_t a2_0 = vld1q_s16(reinterpret_cast<const int16_t *>(input1.ptr()) + 8);
235
236 a1_0 = vsubq_s16(a1_0, vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(bv_0))));
237 a2_0 = vsubq_s16(a2_0, vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(bv_0))));
238
239 vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()), a1_0);
240 vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()) + 8, a2_0);
241 },
242 input1, input2, output);
243}
244
245void sub_saturate_S16_U8_S16(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
246{
247 Iterator input1(in1, window);
248 Iterator input2(in2, window);
249 Iterator output(out, window);
250
251 execute_window_loop(window, [&](const Coordinates & id)
252 {
253 const uint8x16_t bv_0 = vld1q_u8(input2.ptr());
254 int16x8_t a1_0 = vld1q_s16(reinterpret_cast<const int16_t *>(input1.ptr()));
255 int16x8_t a2_0 = vld1q_s16(reinterpret_cast<const int16_t *>(input1.ptr()) + 8);
256
257 a1_0 = vqsubq_s16(a1_0, vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(bv_0))));
258 a2_0 = vqsubq_s16(a2_0, vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(bv_0))));
259
260 vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()), a1_0);
261 vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()) + 8, a2_0);
262 },
263 input1, input2, output);
264}
265
266void sub_wrap_U8_S16_S16(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
267{
268 Iterator input1(in1, window);
269 Iterator input2(in2, window);
270 Iterator output(out, window);
271
272 execute_window_loop(window, [&](const Coordinates & id)
273 {
274 const uint8x16_t bv_0 = vld1q_u8(input1.ptr());
275 int16x8_t a1_0 = vld1q_s16(reinterpret_cast<const int16_t *>(input2.ptr()));
276 int16x8_t a2_0 = vld1q_s16(reinterpret_cast<const int16_t *>(input2.ptr()) + 8);
277
278 a1_0 = vsubq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(bv_0))), a1_0);
279 a2_0 = vsubq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(bv_0))), a2_0);
280
281 vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()), a1_0);
282 vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()) + 8, a2_0);
283 },
284 input1, input2, output);
285}
286
287void sub_saturate_U8_S16_S16(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
288{
289 Iterator input1(in1, window);
290 Iterator input2(in2, window);
291 Iterator output(out, window);
292
293 execute_window_loop(window, [&](const Coordinates & id)
294 {
295 const uint8x16_t bv_0 = vld1q_u8(input1.ptr());
296 int16x8_t a1_0 = vld1q_s16(reinterpret_cast<const int16_t *>(input2.ptr()));
297 int16x8_t a2_0 = vld1q_s16(reinterpret_cast<const int16_t *>(input2.ptr()) + 8);
298
299 a1_0 = vqsubq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(bv_0))), a1_0);
300 a2_0 = vqsubq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(bv_0))), a2_0);
301
302 vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()), a1_0);
303 vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()) + 8, a2_0);
304 },
305 input1, input2, output);
306}
307
308void sub_wrap_U8_U8_S16(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
309{
310 Iterator input1(in1, window);
311 Iterator input2(in2, window);
312 Iterator output(out, window);
313
314 execute_window_loop(window, [&](const Coordinates & id)
315 {
316 const uint8x16_t av_0 = vld1q_u8(input1.ptr());
317 const uint8x16_t bv_0 = vld1q_u8(input2.ptr());
318
319 const int16x8_t a1_0 = vsubq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(av_0))),
320 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(bv_0))));
321 const int16x8_t a2_0 = vsubq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(av_0))),
322 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(bv_0))));
323
324 vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()), a1_0);
325 vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()) + 8, a2_0);
326 },
327 input1, input2, output);
328}
329
330void sub_saturate_U8_U8_S16(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
331{
332 Iterator input1(in1, window);
333 Iterator input2(in2, window);
334 Iterator output(out, window);
335
336 execute_window_loop(window, [&](const Coordinates & id)
337 {
338 const uint8x16_t av_0 = vld1q_u8(input1.ptr());
339 const uint8x16_t bv_0 = vld1q_u8(input2.ptr());
340
341 const int16x8_t a1_0 = vqsubq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(av_0))),
342 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(bv_0))));
343 const int16x8_t a2_0 = vqsubq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(av_0))),
344 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(bv_0))));
345
346 vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()), a1_0);
347 vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()) + 8, a2_0);
348 },
349 input1, input2, output);
350}
351} // namespace
352
353NEArithmeticSubtractionKernel::NEArithmeticSubtractionKernel()
354 : _func(nullptr), _input1(nullptr), _input2(nullptr), _output(nullptr)
355{
356}
357
358void NEArithmeticSubtractionKernel::configure(const ITensor *input1, const ITensor *input2, ITensor *output, ConvertPolicy policy)
359{
360 ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
361
Georgios Pinitasf0dea702017-07-03 18:17:28 +0100362 // Auto initialize output if not initialized
363 {
364 set_shape_if_empty(*output->info(), input1->info()->tensor_shape());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100365
Georgios Pinitasf0dea702017-07-03 18:17:28 +0100366 if(input1->info()->data_type() == DataType::S16 || input2->info()->data_type() == DataType::S16)
367 {
368 set_format_if_unknown(*output->info(), Format::S16);
369 }
Pablo Tellod7a5d222017-07-11 13:54:43 +0100370 else if(input1->info()->data_type() == DataType::F16 || input2->info()->data_type() == DataType::F16)
371 {
372 set_format_if_unknown(*output->info(), Format::F16);
373 }
Georgios Pinitasf0dea702017-07-03 18:17:28 +0100374 else if(input1->info()->data_type() == DataType::F32 || input2->info()->data_type() == DataType::F32)
375 {
376 set_format_if_unknown(*output->info(), Format::F32);
377 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100378 }
379
380 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input1, input2, output);
Pablo Tellod7a5d222017-07-11 13:54:43 +0100381 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input1, 1, DataType::QS8, DataType::U8, DataType::QS16, DataType::S16, DataType::F16, DataType::F32);
382 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input2, 1, DataType::QS8, DataType::U8, DataType::QS16, DataType::S16, DataType::F16, DataType::F32);
383 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QS8, DataType::U8, DataType::QS16, DataType::S16, DataType::F16, DataType::F32);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100384 ARM_COMPUTE_ERROR_ON_MSG(output->info()->data_type() == DataType::U8 && (input1->info()->data_type() != DataType::U8 || input2->info()->data_type() != DataType::U8),
385 "Output can only be U8 if both inputs are U8");
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100386 if(is_data_type_fixed_point(input1->info()->data_type()) || is_data_type_fixed_point(input2->info()->data_type()) || is_data_type_fixed_point(output->info()->data_type()))
387 {
388 // Check that all data types are the same and all fixed-point positions are the same
389 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input1, input2, output);
390 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100391
392 static std::map<std::string, SubFunction *> map_function =
393 {
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100394 { "sub_wrap_QS8_QS8_QS8", &sub_wrap_QS8_QS8_QS8 },
395 { "sub_saturate_QS8_QS8_QS8", &sub_saturate_QS8_QS8_QS8 },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100396 { "sub_wrap_U8_U8_U8", &sub_wrap_U8_U8_U8 },
397 { "sub_wrap_U8_U8_S16", &sub_wrap_U8_U8_S16 },
398 { "sub_saturate_U8_U8_U8", &sub_saturate_U8_U8_U8 },
399 { "sub_saturate_U8_U8_S16", &sub_saturate_U8_U8_S16 },
400 { "sub_wrap_U8_S16_S16", &sub_wrap_U8_S16_S16 },
401 { "sub_wrap_S16_U8_S16", &sub_wrap_S16_U8_S16 },
402 { "sub_saturate_U8_S16_S16", &sub_saturate_U8_S16_S16 },
403 { "sub_saturate_S16_U8_S16", &sub_saturate_S16_U8_S16 },
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100404 { "sub_wrap_QS16_QS16_QS16", &sub_wrap_S16_S16_S16 },
405 { "sub_saturate_QS16_QS16_QS16", &sub_saturate_S16_S16_S16 },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100406 { "sub_wrap_S16_S16_S16", &sub_wrap_S16_S16_S16 },
407 { "sub_saturate_S16_S16_S16", &sub_saturate_S16_S16_S16 },
408 { "sub_wrap_F32_F32_F32", &sub_F32_F32_F32 },
409 { "sub_saturate_F32_F32_F32", &sub_F32_F32_F32 },
Pablo Tellod7a5d222017-07-11 13:54:43 +0100410 { "sub_wrap_F16_F16_F16", &sub_F16_F16_F16 },
411 { "sub_saturate_F16_F16_F16", &sub_F16_F16_F16 },
412
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100413 };
414
415 _input1 = input1;
416 _input2 = input2;
417 _output = output;
418
419 std::string function_to_call("sub_");
420 function_to_call += policy == ConvertPolicy::WRAP ? "wrap_" : "saturate_";
421 function_to_call += string_from_data_type(input1->info()->data_type()) + "_";
422 function_to_call += string_from_data_type(input2->info()->data_type()) + "_";
423 function_to_call += string_from_data_type(output->info()->data_type());
424
425 auto it = map_function.find(function_to_call);
426
427 if(it != map_function.end())
428 {
429 _func = it->second;
430 }
431 else
432 {
433 ARM_COMPUTE_ERROR("You called subtract with the wrong image formats");
434 }
435
436 constexpr unsigned int num_elems_processed_per_iteration = 16;
437
438 // Configure kernel window
439 Window win = calculate_max_window(*input1->info(), Steps(num_elems_processed_per_iteration));
440 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
441
442 update_window_and_padding(win,
443 AccessWindowHorizontal(input1->info(), 0, num_elems_processed_per_iteration),
444 AccessWindowHorizontal(input2->info(), 0, num_elems_processed_per_iteration),
445 output_access);
446
447 ValidRegion valid_region = intersect_valid_regions(input1->info()->valid_region(),
448 input2->info()->valid_region());
449
450 output_access.set_valid_region(win, valid_region);
451
452 INEKernel::configure(win);
453}
454
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100455void NEArithmeticSubtractionKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100456{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100457 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100458 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
459 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
460 ARM_COMPUTE_ERROR_ON(_func == nullptr);
461
462 (*_func)(_input1, _input2, _output, window);
463}