blob: a6102b159f91d7807dd7c72c6323f40d07513078 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Diego Lopez Recas0021d752017-12-18 14:42:56 +00002 * Copyright (c) 2016-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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/NEArithmeticAdditionKernel.h"
25
Anthony Barbiereaefd002018-07-20 17:49:35 +010026#include "arm_compute/core/CPP/Validate.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/Error.h"
28#include "arm_compute/core/Helpers.h"
29#include "arm_compute/core/IAccessWindow.h"
30#include "arm_compute/core/ITensor.h"
Michele Di Giorgio81f0d152017-07-11 15:00:52 +010031#include "arm_compute/core/NEON/NEFixedPoint.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032#include "arm_compute/core/TensorInfo.h"
33#include "arm_compute/core/Validate.h"
34
35#include <algorithm>
36#include <arm_neon.h>
37#include <cstdint>
38#include <map>
39#include <string>
40
41using namespace arm_compute;
42
43namespace arm_compute
44{
45class Coordinates;
46} // namespace arm_compute
47
48namespace
49{
Diego Lopez Recas0021d752017-12-18 14:42:56 +000050constexpr unsigned int num_elems_processed_per_iteration = 16;
51
Anthony Barbier6ff3b192017-09-04 18:44:23 +010052void add_wrap_U8_U8_U8(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
53{
Diego Lopez Recas0021d752017-12-18 14:42:56 +000054 Iterator input1(in1, window.broadcast_if_dimension_le_one(in1->info()->tensor_shape()));
55 Iterator input2(in2, window.broadcast_if_dimension_le_one(in2->info()->tensor_shape()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010056 Iterator output(out, window);
57
58 execute_window_loop(window, [&](const Coordinates & id)
59 {
60 vst1q_u8(output.ptr(), vaddq_u8(vld1q_u8(input1.ptr()), vld1q_u8(input2.ptr())));
61 },
62 input1, input2, output);
63}
64
65void add_saturate_U8_U8_U8(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
66{
Diego Lopez Recas0021d752017-12-18 14:42:56 +000067 Iterator input1(in1, window.broadcast_if_dimension_le_one(in1->info()->tensor_shape()));
68 Iterator input2(in2, window.broadcast_if_dimension_le_one(in2->info()->tensor_shape()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010069 Iterator output(out, window);
70
71 execute_window_loop(window, [&](const Coordinates & id)
72 {
73 vst1q_u8(output.ptr(), vqaddq_u8(vld1q_u8(input1.ptr()), vld1q_u8(input2.ptr())));
74 },
75 input1, input2, output);
76}
77
78inline int16x8x2_t vadd2q_s16(const int16x8x2_t &a, const int16x8x2_t &b)
79{
80 const int16x8x2_t res =
81 {
82 {
83 vaddq_s16(a.val[0], b.val[0]),
84 vaddq_s16(a.val[1], b.val[1])
85 }
86 };
87
88 return res;
89}
90
91inline float32x4x4_t vadd4q_f32(const float32x4x4_t &a, const float32x4x4_t &b)
92{
93 const float32x4x4_t res =
94 {
95 {
96 vaddq_f32(a.val[0], b.val[0]),
97 vaddq_f32(a.val[1], b.val[1]),
98 vaddq_f32(a.val[2], b.val[2]),
99 vaddq_f32(a.val[3], b.val[3])
100 }
101 };
102
103 return res;
104}
105
106inline int16x8x2_t vqadd2q_s16(const int16x8x2_t &a, const int16x8x2_t &b)
107{
108 const int16x8x2_t res =
109 {
110 {
111 vqaddq_s16(a.val[0], b.val[0]),
112 vqaddq_s16(a.val[1], b.val[1])
113 }
114 };
115
116 return res;
117}
118
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000119#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tellod1b0ecc2017-07-11 11:27:04 +0100120inline float16x8x2_t vadd2q_f16(const float16x8x2_t &a, const float16x8x2_t &b)
121{
122 const float16x8x2_t res =
123 {
124 {
125 vaddq_f16(a.val[0], b.val[0]),
126 vaddq_f16(a.val[1], b.val[1])
127 }
128 };
129
130 return res;
131}
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000132#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Pablo Tellod1b0ecc2017-07-11 11:27:04 +0100133
134void add_F16_F16_F16(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
135{
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000136#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000137 Iterator input1(in1, window.broadcast_if_dimension_le_one(in1->info()->tensor_shape()));
138 Iterator input2(in2, window.broadcast_if_dimension_le_one(in2->info()->tensor_shape()));
Pablo Tellod1b0ecc2017-07-11 11:27:04 +0100139 Iterator output(out, window);
140
141 execute_window_loop(window, [&](const Coordinates & id)
142 {
143 const float16x8x2_t a = vld2q_f16(reinterpret_cast<const float16_t *>(input1.ptr()));
144 const float16x8x2_t b = vld2q_f16(reinterpret_cast<const float16_t *>(input2.ptr()));
145
146 vst2q_f16(reinterpret_cast<float16_t *>(output.ptr()), vadd2q_f16(a, b));
147 },
148 input1, input2, output);
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000149#else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Pablo Tellod1b0ecc2017-07-11 11:27:04 +0100150 ARM_COMPUTE_UNUSED(in1);
151 ARM_COMPUTE_UNUSED(in2);
152 ARM_COMPUTE_UNUSED(out);
153 ARM_COMPUTE_UNUSED(window);
154 ARM_COMPUTE_ERROR("Not supported, recompile the library with arch=arm64-v8.2-a");
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000155#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Pablo Tellod1b0ecc2017-07-11 11:27:04 +0100156}
157
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100158void add_F32_F32_F32(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
159{
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000160 Iterator input1(in1, window.broadcast_if_dimension_le_one(in1->info()->tensor_shape()));
161 Iterator input2(in2, window.broadcast_if_dimension_le_one(in2->info()->tensor_shape()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100162 Iterator output(out, window);
163
164 execute_window_loop(window, [&](const Coordinates & id)
165 {
166 const float32x4x4_t a = vld4q_f32(reinterpret_cast<const float *>(input1.ptr()));
167 const float32x4x4_t b = vld4q_f32(reinterpret_cast<const float *>(input2.ptr()));
168
169 vst4q_f32(reinterpret_cast<float *>(output.ptr()), vadd4q_f32(a, b));
170 },
171 input1, input2, output);
172}
173
174void add_wrap_S16_S16_S16(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
175{
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000176 Iterator input1(in1, window.broadcast_if_dimension_le_one(in1->info()->tensor_shape()));
177 Iterator input2(in2, window.broadcast_if_dimension_le_one(in2->info()->tensor_shape()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100178 Iterator output(out, window);
179
180 execute_window_loop(window, [&](const Coordinates & id)
181 {
182 const int16x8x2_t a = vld2q_s16(reinterpret_cast<const int16_t *>(input1.ptr()));
183 const int16x8x2_t b = vld2q_s16(reinterpret_cast<const int16_t *>(input2.ptr()));
184
185 vst2q_s16(reinterpret_cast<int16_t *>(output.ptr()), vadd2q_s16(a, b));
186 },
187 input1, input2, output);
188}
189
190void add_saturate_S16_S16_S16(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
191{
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000192 Iterator input1(in1, window.broadcast_if_dimension_le_one(in1->info()->tensor_shape()));
193 Iterator input2(in2, window.broadcast_if_dimension_le_one(in2->info()->tensor_shape()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100194 Iterator output(out, window);
195
196 execute_window_loop(window, [&](const Coordinates & id)
197 {
198 const int16x8x2_t a = vld2q_s16(reinterpret_cast<const int16_t *>(input1.ptr()));
199 const int16x8x2_t b = vld2q_s16(reinterpret_cast<const int16_t *>(input2.ptr()));
200
201 vst2q_s16(reinterpret_cast<int16_t *>(output.ptr()), vqadd2q_s16(a, b));
202 },
203 input1, input2, output);
204}
205
206void add_wrap_S16_U8_S16(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
207{
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000208 Iterator input1(in1, window.broadcast_if_dimension_le_one(in1->info()->tensor_shape()));
209 Iterator input2(in2, window.broadcast_if_dimension_le_one(in2->info()->tensor_shape()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100210 Iterator output(out, window);
211
212 execute_window_loop(window, [&](const Coordinates & id)
213 {
214 const int16x8x2_t a =
215 {
216 {
217 vld1q_s16(reinterpret_cast<const int16_t *>(input1.ptr())),
218 vld1q_s16(reinterpret_cast<const int16_t *>(input1.ptr()) + 8)
219 }
220 };
221 const uint8x16_t b = vld1q_u8(input2.ptr());
222
223 vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()), vaddq_s16(a.val[0], vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(b)))));
224 vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()) + 8, vaddq_s16(a.val[1], vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(b)))));
225 },
226 input1, input2, output);
227}
228
229void add_saturate_S16_U8_S16(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
230{
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000231 Iterator input1(in1, window.broadcast_if_dimension_le_one(in1->info()->tensor_shape()));
232 Iterator input2(in2, window.broadcast_if_dimension_le_one(in2->info()->tensor_shape()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100233 Iterator output(out, window);
234
235 execute_window_loop(window, [&](const Coordinates & id)
236 {
237 const int16x8x2_t a =
238 {
239 {
240 vld1q_s16(reinterpret_cast<const int16_t *>(input1.ptr())),
241 vld1q_s16(reinterpret_cast<const int16_t *>(input1.ptr()) + 8)
242 }
243 };
244 const uint8x16_t b = vld1q_u8(input2.ptr());
245
246 vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()), vqaddq_s16(a.val[0], vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(b)))));
247 vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()) + 8, vqaddq_s16(a.val[1], vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(b)))));
248 },
249 input1, input2, output);
250}
251
252inline void add_wrap_U8_S16_S16(const ITensor *input1, const ITensor *input2, ITensor *output, const Window &window)
253{
254 //Simply swap the two input buffers:
255 add_wrap_S16_U8_S16(input2, input1, output, window);
256}
257
258inline void add_saturate_U8_S16_S16(const ITensor *input1, const ITensor *input2, ITensor *output, const Window &window)
259{
260 //Simply swap the two input buffers:
261 add_saturate_S16_U8_S16(input2, input1, output, window);
262}
263
264void add_wrap_U8_U8_S16(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
265{
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000266 Iterator input1(in1, window.broadcast_if_dimension_le_one(in1->info()->tensor_shape()));
267 Iterator input2(in2, window.broadcast_if_dimension_le_one(in2->info()->tensor_shape()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100268 Iterator output(out, window);
269
270 execute_window_loop(window, [&](const Coordinates & id)
271 {
272 const uint8x16_t a = vld1q_u8(input1.ptr());
273 const uint8x16_t b = vld1q_u8(input2.ptr());
274
275 const int16x8x2_t a_s16 =
276 {
277 {
278 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(a))),
279 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(a)))
280 }
281 };
282
283 const int16x8x2_t b_s16 =
284 {
285 {
286 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(b))),
287 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(b)))
288 }
289 };
290
291 vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()), vaddq_s16(a_s16.val[0], b_s16.val[0]));
292 vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()) + 8, vaddq_s16(a_s16.val[1], b_s16.val[1]));
293 },
294 input1, input2, output);
295}
296
297void add_saturate_U8_U8_S16(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
298{
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000299 Iterator input1(in1, window.broadcast_if_dimension_le_one(in1->info()->tensor_shape()));
300 Iterator input2(in2, window.broadcast_if_dimension_le_one(in2->info()->tensor_shape()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100301 Iterator output(out, window);
302
303 execute_window_loop(window, [&](const Coordinates & id)
304 {
305 const uint8x16_t a = vld1q_u8(input1.ptr());
306 const uint8x16_t b = vld1q_u8(input2.ptr());
307
308 const int16x8x2_t a_s16 =
309 {
310 {
311 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(a))),
312 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(a)))
313 }
314 };
315
316 const int16x8x2_t b_s16 =
317 {
318 {
319 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(b))),
320 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(b)))
321 }
322 };
323
324 vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()), vqaddq_s16(a_s16.val[0], b_s16.val[0]));
325 vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()) + 8, vqaddq_s16(a_s16.val[1], b_s16.val[1]));
326 },
327 input1, input2, output);
328}
Ioan-Cristian Szabo397d58a2017-11-30 15:19:11 +0000329
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000330Status validate_arguments(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output, ConvertPolicy policy)
Ioan-Cristian Szabo397d58a2017-11-30 15:19:11 +0000331{
332 ARM_COMPUTE_UNUSED(policy);
Ioan-Cristian Szabo397d58a2017-11-30 15:19:11 +0000333
Anthony Barbiereaefd002018-07-20 17:49:35 +0100334 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(&input1);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100335 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input1, 1, DataType::U8, DataType::S16, DataType::F16, DataType::F32);
336 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input2, 1, DataType::U8, DataType::S16, DataType::F16, DataType::F32);
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000337
338 const TensorShape out_shape = TensorShape::broadcast_shape(input1.tensor_shape(), input2.tensor_shape());
339
340 ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible");
341
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000342 // Validate in case of configured output
343 if(output.total_size() > 0)
344 {
345 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100346 !(input1.data_type() == DataType::U8 && input2.data_type() == DataType::U8 && output.data_type() == DataType::U8)
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000347 && !(input1.data_type() == DataType::U8 && input2.data_type() == DataType::U8 && output.data_type() == DataType::S16)
348 && !(input1.data_type() == DataType::U8 && input2.data_type() == DataType::S16 && output.data_type() == DataType::S16)
349 && !(input1.data_type() == DataType::S16 && input2.data_type() == DataType::U8 && output.data_type() == DataType::S16)
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000350 && !(input1.data_type() == DataType::S16 && input2.data_type() == DataType::S16 && output.data_type() == DataType::S16)
351 && !(input1.data_type() == DataType::F32 && input2.data_type() == DataType::F32 && output.data_type() == DataType::F32)
352 && !(input1.data_type() == DataType::F16 && input2.data_type() == DataType::F16 && output.data_type() == DataType::F16),
353 "You called addition with the wrong image formats");
354
355 ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, output.tensor_shape(), 0),
356 "Wrong shape for output");
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000357 }
Ioan-Cristian Szabo397d58a2017-11-30 15:19:11 +0000358
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000359 return Status{};
Ioan-Cristian Szabo397d58a2017-11-30 15:19:11 +0000360}
361
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000362std::pair<Status, Window> validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output)
Ioan-Cristian Szabo397d58a2017-11-30 15:19:11 +0000363{
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000364 const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(input1, input2);
365 const TensorShape &out_shape = broadcast_pair.first;
366 const ValidRegion &valid_region = broadcast_pair.second;
Ioan-Cristian Szabo397d58a2017-11-30 15:19:11 +0000367
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000368 // Auto initialize output if not initialized
369 {
370 set_shape_if_empty(output, out_shape);
Ioan-Cristian Szabo397d58a2017-11-30 15:19:11 +0000371
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000372 if(input1.data_type() == DataType::S16 || input2.data_type() == DataType::S16)
373 {
374 set_format_if_unknown(output, Format::S16);
375 }
376 else if(input1.data_type() == DataType::F16 && input2.data_type() == DataType::F16)
377 {
378 set_format_if_unknown(output, Format::F16);
379 }
380 else if(input1.data_type() == DataType::F32 || input2.data_type() == DataType::F32)
381 {
382 set_format_if_unknown(output, Format::F32);
383 }
384 }
Ioan-Cristian Szabo397d58a2017-11-30 15:19:11 +0000385
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000386 Window win = calculate_max_window(valid_region, Steps(num_elems_processed_per_iteration));
387 Window win_input1 = win.broadcast_if_dimension_le_one(input1);
388 Window win_input2 = win.broadcast_if_dimension_le_one(input2);
389
390 AccessWindowHorizontal input1_access(&input1, 0, num_elems_processed_per_iteration);
391 AccessWindowHorizontal input2_access(&input2, 0, num_elems_processed_per_iteration);
392 AccessWindowHorizontal output_access(&output, 0, num_elems_processed_per_iteration);
393
394 bool window_changed = update_window_and_padding(win_input1, input1_access)
395 || update_window_and_padding(win_input2, input2_access)
396 || update_window_and_padding(win, output_access);
Ioan-Cristian Szabo397d58a2017-11-30 15:19:11 +0000397
398 output_access.set_valid_region(win, valid_region);
399
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000400 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Ioan-Cristian Szabo397d58a2017-11-30 15:19:11 +0000401 return std::make_pair(err, win);
402}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100403} // namespace
404
405NEArithmeticAdditionKernel::NEArithmeticAdditionKernel()
406 : _func(nullptr), _input1(nullptr), _input2(nullptr), _output(nullptr)
407{
408}
409
410void NEArithmeticAdditionKernel::configure(const ITensor *input1, const ITensor *input2, ITensor *output, ConvertPolicy policy)
411{
412 ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000413 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*input1->info(), *input2->info(), *output->info(), policy));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100414
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000415 // Configure kernel window
416 auto win_config = validate_and_configure_window(*input1->info(), *input2->info(), *output->info());
417 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100418
419 static std::map<std::string, AddFunction *> map_function =
420 {
421 { "add_wrap_U8_U8_U8", &add_wrap_U8_U8_U8 },
422 { "add_saturate_U8_U8_U8", &add_saturate_U8_U8_U8 },
423 { "add_wrap_S16_U8_S16", &add_wrap_S16_U8_S16 },
424 { "add_saturate_S16_U8_S16", &add_saturate_S16_U8_S16 },
425 { "add_wrap_U8_S16_S16", &add_wrap_U8_S16_S16 },
426 { "add_saturate_U8_S16_S16", &add_saturate_U8_S16_S16 },
427 { "add_wrap_U8_U8_S16", &add_wrap_U8_U8_S16 },
428 { "add_saturate_U8_U8_S16", &add_saturate_U8_U8_S16 },
429 { "add_wrap_S16_S16_S16", &add_wrap_S16_S16_S16 },
430 { "add_saturate_S16_S16_S16", &add_saturate_S16_S16_S16 },
431 { "add_wrap_F32_F32_F32", &add_F32_F32_F32 },
432 { "add_saturate_F32_F32_F32", &add_F32_F32_F32 },
Pablo Tellod1b0ecc2017-07-11 11:27:04 +0100433 { "add_wrap_F16_F16_F16", &add_F16_F16_F16 },
434 { "add_saturate_F16_F16_F16", &add_F16_F16_F16 },
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100435 };
436
437 _input1 = input1;
438 _input2 = input2;
439 _output = output;
440
441 std::string function_to_call("add_");
442 function_to_call += policy == ConvertPolicy::WRAP ? "wrap_" : "saturate_";
443 function_to_call += string_from_data_type(input1->info()->data_type()) + "_";
444 function_to_call += string_from_data_type(input2->info()->data_type()) + "_";
445 function_to_call += string_from_data_type(output->info()->data_type());
446
447 auto it = map_function.find(function_to_call);
448
449 if(it != map_function.end())
450 {
451 _func = it->second;
452 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100453
Ioan-Cristian Szabo397d58a2017-11-30 15:19:11 +0000454 INEKernel::configure(win_config.second);
455}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100456
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000457Status NEArithmeticAdditionKernel::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, ConvertPolicy policy)
Ioan-Cristian Szabo397d58a2017-11-30 15:19:11 +0000458{
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000459 ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
460
461 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*input1, *input2, *output, policy));
462 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(*input1->clone(), *input2->clone(), *output->clone()).first);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100463
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000464 return Status{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100465}
466
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100467void NEArithmeticAdditionKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100468{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100469 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100470 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
471 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
472 ARM_COMPUTE_ERROR_ON(_func == nullptr);
473
474 (*_func)(_input1, _input2, _output, window);
475}
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000476
477BorderSize NEArithmeticAdditionKernel::border_size() const
478{
479 const unsigned int replicateSize = _output->info()->dimension(0) - std::min(_input1->info()->dimension(0), _input2->info()->dimension(0));
480 const unsigned int border = std::min<unsigned int>(num_elems_processed_per_iteration - 1U, replicateSize);
481 return BorderSize(0, border, 0, 0);
482}