blob: e74833cd41e607d2bdee5978e2e3a998425832e7 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitas5a594532018-12-03 14:30:05 +00002 * Copyright (c) 2016-2019 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"
Georgios Pinitas5a594532018-12-03 14:30:05 +000032#include "arm_compute/core/NEON/wrapper/wrapper.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033#include "arm_compute/core/TensorInfo.h"
34#include "arm_compute/core/Validate.h"
35
36#include <algorithm>
37#include <arm_neon.h>
38#include <cstdint>
39#include <map>
40#include <string>
41
42using namespace arm_compute;
43
44namespace arm_compute
45{
46class Coordinates;
47} // namespace arm_compute
48
49namespace
50{
Georgios Pinitas5a594532018-12-03 14:30:05 +000051template <typename T, bool is_sat>
52void add_same(const ITensor *in1, const ITensor *in2, ITensor *out, ConvertPolicy policy, const Window &window)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053{
Georgios Pinitas5a594532018-12-03 14:30:05 +000054 ARM_COMPUTE_UNUSED(policy);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010055
Georgios Pinitas5a594532018-12-03 14:30:05 +000056 /** NEON vector tag type. */
57 using ExactTagType = typename wrapper::traits::neon_bitvector_tag_t<T, wrapper::traits::BitWidth::W128>;
58
59 // Create input windows
60 Window input1_win = window.broadcast_if_dimension_le_one(in1->info()->tensor_shape());
61 Window input2_win = window.broadcast_if_dimension_le_one(in2->info()->tensor_shape());
62
63 // Clear X Dimension on execution window as we handle manually
64 Window win = window;
65 win.set(Window::DimX, Window::Dimension(0, 1, 1));
66
67 constexpr int window_step_x = 16 / sizeof(T);
68 const auto window_start_x = static_cast<int>(window.x().start());
69 const auto window_end_x = static_cast<int>(window.x().end());
70 const bool is_broadcast_across_x = (input1_win.x().step() == 0) || (input2_win.x().step() == 0);
71
72 if(is_broadcast_across_x)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010073 {
Georgios Pinitas5a594532018-12-03 14:30:05 +000074 const bool is_broadcast_input_2 = input2_win.x().step() == 0;
75 Window broadcast_win = is_broadcast_input_2 ? input2_win : input1_win;
76 Window non_broadcast_win = !is_broadcast_input_2 ? input2_win : input1_win;
77 const ITensor *broadcast_tensor = is_broadcast_input_2 ? in2 : in1;
78 const ITensor *non_broadcast_tensor = !is_broadcast_input_2 ? in2 : in1;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010079
Georgios Pinitas5a594532018-12-03 14:30:05 +000080 // Clear X Dimension on execution window as we handle manually
81 non_broadcast_win.set(Window::DimX, Window::Dimension(0, 1, 1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010082
Georgios Pinitas5a594532018-12-03 14:30:05 +000083 Iterator broadcast_input(broadcast_tensor, broadcast_win);
84 Iterator non_broadcast_input(non_broadcast_tensor, non_broadcast_win);
85 Iterator output(out, win);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010086
Georgios Pinitas5a594532018-12-03 14:30:05 +000087 execute_window_loop(win, [&](const Coordinates & id)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010088 {
Georgios Pinitas5a594532018-12-03 14:30:05 +000089 const auto non_broadcast_input_ptr = reinterpret_cast<const T *>(non_broadcast_input.ptr());
90 const auto output_ptr = reinterpret_cast<T *>(output.ptr());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010091
Georgios Pinitas5a594532018-12-03 14:30:05 +000092 const T broadcast_value = *reinterpret_cast<const T *>(broadcast_input.ptr());
93 const auto broadcast_value_vec = wrapper::vdup_n(broadcast_value, ExactTagType{});
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094
Georgios Pinitas5a594532018-12-03 14:30:05 +000095 // Compute S elements per iteration
96 int x = window_start_x;
97 for(; x <= (window_end_x - window_step_x); x += window_step_x)
98 {
99 const auto non_broadcast_v = wrapper::vloadq(non_broadcast_input_ptr + x);
100 const auto res = is_sat ? wrapper::vqadd(broadcast_value_vec, non_broadcast_v) : wrapper::vadd(broadcast_value_vec, non_broadcast_v);
101 wrapper::vstore(output_ptr + x, res);
102 }
103
104 // Compute left-over elements
105 for(; x < window_end_x; ++x)
106 {
107 const auto non_broadcast_v = *(non_broadcast_input_ptr + x);
108 *(output_ptr + x) = is_sat ? wrapper::add_sat(broadcast_value, non_broadcast_v) : broadcast_value + non_broadcast_v;
109 }
110 },
111 broadcast_input, non_broadcast_input, output);
112 }
113 else
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100114 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000115 // Clear X Dimension on execution window as we handle manually
116 input1_win.set(Window::DimX, Window::Dimension(0, 1, 1));
117 input2_win.set(Window::DimX, Window::Dimension(0, 1, 1));
118
119 Iterator input1(in1, input1_win);
120 Iterator input2(in2, input2_win);
121 Iterator output(out, win);
122
123 execute_window_loop(win, [&](const Coordinates & id)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000125 const auto input1_ptr = reinterpret_cast<const T *>(input1.ptr());
126 const auto input2_ptr = reinterpret_cast<const T *>(input2.ptr());
127 const auto output_ptr = reinterpret_cast<T *>(output.ptr());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128
Georgios Pinitas5a594532018-12-03 14:30:05 +0000129 // Compute S elements per iteration
130 int x = window_start_x;
131 for(; x <= (window_end_x - window_step_x); x += window_step_x)
132 {
133 const auto val1 = wrapper::vloadq(input1_ptr + x);
134 const auto val2 = wrapper::vloadq(input2_ptr + x);
135 const auto res = is_sat ? wrapper::vqadd(val1, val2) : wrapper::vadd(val1, val2);
136 wrapper::vstore(output_ptr + x, res);
137 }
138
139 // Compute left-over elements
140 for(; x < window_end_x; ++x)
141 {
142 const auto val1 = *(input1_ptr + x);
143 const auto val2 = *(input2_ptr + x);
144 *(output_ptr + x) = is_sat ? wrapper::add_sat(val1, val2) : val1 + val2;
145 }
146 },
147 input1, input2, output);
148 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100149}
150
Georgios Pinitas5a594532018-12-03 14:30:05 +0000151void add_QASYMM8_QASYMM8_QASYMM8(const ITensor *in1, const ITensor *in2, ITensor *out, ConvertPolicy policy, const Window &window)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152{
Georgios Pinitas5a594532018-12-03 14:30:05 +0000153 ARM_COMPUTE_UNUSED(policy);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100154
Georgios Pinitas5a594532018-12-03 14:30:05 +0000155 // Create input windows
156 Window input1_win = window.broadcast_if_dimension_le_one(in1->info()->tensor_shape());
157 Window input2_win = window.broadcast_if_dimension_le_one(in2->info()->tensor_shape());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100158
Georgios Pinitas5a594532018-12-03 14:30:05 +0000159 // Clear X Dimension on execution window as we handle manually
160 Window win = window;
161 win.set(Window::DimX, Window::Dimension(0, 1, 1));
Pablo Tellod1b0ecc2017-07-11 11:27:04 +0100162
Georgios Pinitas5a594532018-12-03 14:30:05 +0000163 const int window_step_x = 16;
164 const auto window_start_x = static_cast<int>(window.x().start());
165 const auto window_end_x = static_cast<int>(window.x().end());
166 const bool is_broadcast_across_x = (input1_win.x().step() == 0) || (input2_win.x().step() == 0);
Pablo Tellod1b0ecc2017-07-11 11:27:04 +0100167
Georgios Pinitas5a594532018-12-03 14:30:05 +0000168 const float output_scale = out->info()->quantization_info().scale;
169 const float invoutput_scale = 1.f / output_scale;
170 const int output_offset = out->info()->quantization_info().offset;
Georgios Pinitasa84faff2018-12-05 18:17:24 +0000171
172 const float32x4_t vscale1 = vdupq_n_f32(in1->info()->quantization_info().scale);
173 const float32x4_t vscale2 = vdupq_n_f32(in2->info()->quantization_info().scale);
Georgios Pinitas5a594532018-12-03 14:30:05 +0000174 const float32x4_t invvscaleo = vdupq_n_f32(1.f / output_scale);
Georgios Pinitasa84faff2018-12-05 18:17:24 +0000175 const int32x4_t voffset1 = vdupq_n_s32(in1->info()->quantization_info().offset);
176 const int32x4_t voffset2 = vdupq_n_s32(in2->info()->quantization_info().offset);
Georgios Pinitas5a594532018-12-03 14:30:05 +0000177 const float32x4_t voffseto = vdupq_n_f32(output_offset);
Georgios Pinitasa84faff2018-12-05 18:17:24 +0000178
Georgios Pinitas5a594532018-12-03 14:30:05 +0000179 if(is_broadcast_across_x)
Georgios Pinitasa84faff2018-12-05 18:17:24 +0000180 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000181 const bool is_broadcast_input_2 = input2_win.x().step() == 0;
182 Window broadcast_win = is_broadcast_input_2 ? input2_win : input1_win;
183 Window non_broadcast_win = !is_broadcast_input_2 ? input2_win : input1_win;
184 const ITensor *broadcast_tensor = is_broadcast_input_2 ? in2 : in1;
185 const ITensor *non_broadcast_tensor = !is_broadcast_input_2 ? in2 : in1;
186 const QuantizationInfo broadcast_qinfo = broadcast_tensor->info()->quantization_info();
187 const QuantizationInfo non_broadcast_qinfo = non_broadcast_tensor->info()->quantization_info();
Georgios Pinitasa84faff2018-12-05 18:17:24 +0000188
Georgios Pinitas5a594532018-12-03 14:30:05 +0000189 // Clear X Dimension on execution window as we handle manually
190 non_broadcast_win.set(Window::DimX, Window::Dimension(0, 1, 1));
191
192 Iterator broadcast_input(broadcast_tensor, broadcast_win);
193 Iterator non_broadcast_input(non_broadcast_tensor, non_broadcast_win);
194 Iterator output(out, win);
195
196 execute_window_loop(win, [&](const Coordinates & id)
Georgios Pinitasa84faff2018-12-05 18:17:24 +0000197 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000198 const auto non_broadcast_input_ptr = reinterpret_cast<const uint8_t *>(non_broadcast_input.ptr());
199 const auto output_ptr = reinterpret_cast<uint8_t *>(output.ptr());
Georgios Pinitasa84faff2018-12-05 18:17:24 +0000200
Georgios Pinitas5a594532018-12-03 14:30:05 +0000201 const uint8_t broadcast_value = *reinterpret_cast<const uint8_t *>(broadcast_input.ptr());
202 const uint8x16_t broadcast_value_vec = vdupq_n_u8(broadcast_value);
203
204 const float32x4x4_t bf =
205 {
206 {
207 vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_low_u8(broadcast_value_vec))))), voffset2)), vscale2),
208 vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_low_u8(broadcast_value_vec))))), voffset2)), vscale2),
209 vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_high_u8(broadcast_value_vec))))), voffset2)), vscale2),
210 vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_high_u8(broadcast_value_vec))))), voffset2)), vscale2),
211 }
212 };
213 const float bfs = static_cast<int32_t>(broadcast_value - broadcast_qinfo.offset) * broadcast_qinfo.scale;
214
215 // Compute S elements per iteration
216 int x = window_start_x;
217 for(; x <= (window_end_x - window_step_x); x += window_step_x)
218 {
219 const uint8x16_t a = vld1q_u8(non_broadcast_input_ptr + x);
220 const float32x4x4_t af =
221 {
222 {
223 vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_low_u8(a))))), voffset1)), vscale1),
224 vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_low_u8(a))))), voffset1)), vscale1),
225 vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_high_u8(a))))), voffset1)), vscale1),
226 vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_high_u8(a))))), voffset1)), vscale1),
227 }
228 };
229
230 const int32x4x4_t rf =
231 {
232 {
233 vcvtq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[0], bf.val[0]), invvscaleo)),
234 vcvtq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[1], bf.val[1]), invvscaleo)),
235 vcvtq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[2], bf.val[2]), invvscaleo)),
236 vcvtq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[3], bf.val[3]), invvscaleo)),
237 }
238 };
239
240 const uint8x8_t pa = vqmovun_s16(vcombine_s16(vqmovn_s32(rf.val[0]), vqmovn_s32(rf.val[1])));
241 const uint8x8_t pb = vqmovun_s16(vcombine_s16(vqmovn_s32(rf.val[2]), vqmovn_s32(rf.val[3])));
242 vst1q_u8(output_ptr + x, vcombine_u8(pa, pb));
243 }
244
245 // Compute left-over elements
246 for(; x < window_end_x; ++x)
247 {
248 const float afs = static_cast<int32_t>(*(non_broadcast_input_ptr + x) - non_broadcast_qinfo.offset) * non_broadcast_qinfo.scale;
249 *(output_ptr + x) = std::max(0, std::min(static_cast<int32_t>((afs + bfs) * invoutput_scale + output_offset), 255));
250 }
251 },
252 broadcast_input, non_broadcast_input, output);
253 }
254 else
255 {
256 // Clear X Dimension on execution window as we handle manually
257 input1_win.set(Window::DimX, Window::Dimension(0, 1, 1));
258 input2_win.set(Window::DimX, Window::Dimension(0, 1, 1));
259
260 const QuantizationInfo input1_qinfo = in1->info()->quantization_info();
261 const QuantizationInfo input2_qinfo = in2->info()->quantization_info();
262
263 Iterator input1(in1, input1_win);
264 Iterator input2(in2, input2_win);
265 Iterator output(out, win);
266
267 execute_window_loop(win, [&](const Coordinates & id)
Georgios Pinitasa84faff2018-12-05 18:17:24 +0000268 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000269 const auto input1_ptr = reinterpret_cast<const uint8_t *>(input1.ptr());
270 const auto input2_ptr = reinterpret_cast<const uint8_t *>(input2.ptr());
271 const auto output_ptr = reinterpret_cast<uint8_t *>(output.ptr());
Georgios Pinitasa84faff2018-12-05 18:17:24 +0000272
Georgios Pinitas5a594532018-12-03 14:30:05 +0000273 // Compute S elements per iteration
274 int x = window_start_x;
275 for(; x <= (window_end_x - window_step_x); x += window_step_x)
276 {
277 const uint8x16_t a = vld1q_u8(input1_ptr + x);
278 const uint8x16_t b = vld1q_u8(input2_ptr + x);
279
280 const float32x4x4_t af =
281 {
282 {
283 vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_low_u8(a))))), voffset1)), vscale1),
284 vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_low_u8(a))))), voffset1)), vscale1),
285 vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_high_u8(a))))), voffset1)), vscale1),
286 vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_high_u8(a))))), voffset1)), vscale1),
287 }
288 };
289
290 const float32x4x4_t bf =
291 {
292 {
293 vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_low_u8(b))))), voffset2)), vscale2),
294 vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_low_u8(b))))), voffset2)), vscale2),
295 vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_high_u8(b))))), voffset2)), vscale2),
296 vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_high_u8(b))))), voffset2)), vscale2),
297 }
298 };
299
300 const int32x4x4_t rf =
301 {
302 {
303 vcvtq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[0], bf.val[0]), invvscaleo)),
304 vcvtq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[1], bf.val[1]), invvscaleo)),
305 vcvtq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[2], bf.val[2]), invvscaleo)),
306 vcvtq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[3], bf.val[3]), invvscaleo)),
307 }
308 };
309
310 const uint8x8_t pa = vqmovun_s16(vcombine_s16(vqmovn_s32(rf.val[0]), vqmovn_s32(rf.val[1])));
311 const uint8x8_t pb = vqmovun_s16(vcombine_s16(vqmovn_s32(rf.val[2]), vqmovn_s32(rf.val[3])));
312 vst1q_u8(output_ptr + x, vcombine_u8(pa, pb));
313 }
314
315 // Compute left-over elements
316 for(; x < window_end_x; ++x)
317 {
318 const float afs = static_cast<int32_t>((*(input1_ptr + x)) - input1_qinfo.offset) * input1_qinfo.scale;
319 const float bfs = static_cast<int32_t>((*(input2_ptr + x)) - input2_qinfo.offset) * input2_qinfo.scale;
320 *(output_ptr + x) = std::max(0, std::min(static_cast<int32_t>((afs + bfs) * invoutput_scale + output_offset), 255));
321 }
322 },
323 input1, input2, output);
324 }
325}
326
327void add_S16_U8_S16(const ITensor *in1, const ITensor *in2, ITensor *out, ConvertPolicy policy, const Window &window)
328{
329 // Create input windows
330 Window win = window;
331 Window input1_win = window.broadcast_if_dimension_le_one(in1->info()->tensor_shape());
332 Window input2_win = window.broadcast_if_dimension_le_one(in2->info()->tensor_shape());
333
334 // Clear X Dimension on execution window as we handle manually
335 win.set(Window::DimX, Window::Dimension(0, 1, 1));
336 input1_win.set(Window::DimX, Window::Dimension(0, 1, 1));
337 input2_win.set(Window::DimX, Window::Dimension(0, 1, 1));
338
339 Iterator input1(in1, input1_win);
340 Iterator input2(in2, input2_win);
341 Iterator output(out, win);
342
343 const int window_step_x = 8;
344 const auto window_start_x = static_cast<int>(window.x().start());
345 const auto window_end_x = static_cast<int>(window.x().end());
346
347 execute_window_loop(win, [&](const Coordinates & id)
348 {
349 const auto input1_ptr = reinterpret_cast<const int16_t *>(input1.ptr());
350 const auto input2_ptr = reinterpret_cast<const uint8_t *>(input2.ptr());
351 const auto output_ptr = reinterpret_cast<int16_t *>(output.ptr());
352
353 if(policy == ConvertPolicy::WRAP)
Georgios Pinitasa84faff2018-12-05 18:17:24 +0000354 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000355 // Compute S elements per iteration
356 int x = window_start_x;
357 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Georgios Pinitasa84faff2018-12-05 18:17:24 +0000358 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000359 const auto vin1 = wrapper::vloadq(input1_ptr + x);
360 const auto vin2 = vreinterpretq_s16_u16(wrapper::vmovl(wrapper::vload(input2_ptr + x)));
361 wrapper::vstore(output_ptr + x, wrapper::vadd(vin1, vin2));
Georgios Pinitasa84faff2018-12-05 18:17:24 +0000362 }
Georgios Pinitasa84faff2018-12-05 18:17:24 +0000363
Georgios Pinitas5a594532018-12-03 14:30:05 +0000364 // Compute left-over elements
365 for(; x < window_end_x; ++x)
366 {
367 *(output_ptr + x) = *(input1_ptr + x) + static_cast<int16_t>(*(input2_ptr + x));
368 }
369 }
370 else
371 {
372 // Compute S elements per iteration
373 int x = window_start_x;
374 for(; x <= (window_end_x - window_step_x); x += window_step_x)
375 {
376 const auto vin1 = wrapper::vloadq(input1_ptr + x);
377 const auto vin2 = vreinterpretq_s16_u16(wrapper::vmovl(wrapper::vload(input2_ptr + x)));
378 wrapper::vstore(output_ptr + x, wrapper::vqadd(vin1, vin2));
379 }
380
381 // Compute left-over elements
382 for(; x < window_end_x; ++x)
383 {
384 *(output_ptr + x) = wrapper::add_sat(*(input1_ptr + x), static_cast<int16_t>(*(input2_ptr + x)));
385 }
386 }
Georgios Pinitasa84faff2018-12-05 18:17:24 +0000387 },
388 input1, input2, output);
389}
390
Georgios Pinitas5a594532018-12-03 14:30:05 +0000391inline void add_U8_S16_S16(const ITensor *input1, const ITensor *input2, ITensor *output, ConvertPolicy policy, const Window &window)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100392{
Georgios Pinitas5a594532018-12-03 14:30:05 +0000393 // Simply swap the two input buffers:
394 add_S16_U8_S16(input2, input1, output, policy, window);
395}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100396
Georgios Pinitas5a594532018-12-03 14:30:05 +0000397void add_U8_U8_S16(const ITensor *in1, const ITensor *in2, ITensor *out, ConvertPolicy policy, const Window &window)
398{
399 // Create input windows
400 Window win = window;
401 Window input1_win = window.broadcast_if_dimension_le_one(in1->info()->tensor_shape());
402 Window input2_win = window.broadcast_if_dimension_le_one(in2->info()->tensor_shape());
403
404 // Clear X Dimension on execution window as we handle manually
405 win.set(Window::DimX, Window::Dimension(0, 1, 1));
406 input1_win.set(Window::DimX, Window::Dimension(0, 1, 1));
407 input2_win.set(Window::DimX, Window::Dimension(0, 1, 1));
408
409 Iterator input1(in1, input1_win);
410 Iterator input2(in2, input2_win);
411 Iterator output(out, win);
412
413 const int window_step_x = 8;
414 const auto window_start_x = static_cast<int>(window.x().start());
415 const auto window_end_x = static_cast<int>(window.x().end());
416
417 execute_window_loop(win, [&](const Coordinates & id)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100418 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000419 const auto input1_ptr = reinterpret_cast<const uint8_t *>(input1.ptr());
420 const auto input2_ptr = reinterpret_cast<const uint8_t *>(input2.ptr());
421 const auto output_ptr = reinterpret_cast<int16_t *>(output.ptr());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100422
Georgios Pinitas5a594532018-12-03 14:30:05 +0000423 if(policy == ConvertPolicy::WRAP)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100424 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000425 // Compute S elements per iteration
426 int x = window_start_x;
427 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100428 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000429 const auto vin1 = vreinterpretq_s16_u16(wrapper::vmovl(wrapper::vload(input1_ptr + x)));
430 const auto vin2 = vreinterpretq_s16_u16(wrapper::vmovl(wrapper::vload(input2_ptr + x)));
431 wrapper::vstore(output_ptr + x, wrapper::vadd(vin1, vin2));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100432 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100433
Georgios Pinitas5a594532018-12-03 14:30:05 +0000434 // Compute left-over elements
435 for(; x < window_end_x; ++x)
436 {
437 *(output_ptr + x) = static_cast<int16_t>(*(input1_ptr + x)) + static_cast<int16_t>(*(input2_ptr + x));
438 }
439 }
440 else
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100441 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000442 // Compute S elements per iteration
443 int x = window_start_x;
444 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100445 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000446 const auto vin1 = vreinterpretq_s16_u16(wrapper::vmovl(wrapper::vload(input1_ptr + x)));
447 const auto vin2 = vreinterpretq_s16_u16(wrapper::vmovl(wrapper::vload(input2_ptr + x)));
448 wrapper::vstore(output_ptr + x, wrapper::vqadd(vin1, vin2));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100449 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100450
Georgios Pinitas5a594532018-12-03 14:30:05 +0000451 // Compute left-over elements
452 for(; x < window_end_x; ++x)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100453 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000454 *(output_ptr + x) = wrapper::add_sat(static_cast<int16_t>(*(input1_ptr + x)),
455 static_cast<int16_t>(*(input2_ptr + x)));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100456 }
Georgios Pinitas5a594532018-12-03 14:30:05 +0000457 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100458 },
459 input1, input2, output);
460}
Ioan-Cristian Szabo397d58a2017-11-30 15:19:11 +0000461
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000462Status validate_arguments(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output, ConvertPolicy policy)
Ioan-Cristian Szabo397d58a2017-11-30 15:19:11 +0000463{
464 ARM_COMPUTE_UNUSED(policy);
Ioan-Cristian Szabo397d58a2017-11-30 15:19:11 +0000465
Anthony Barbiereaefd002018-07-20 17:49:35 +0100466 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(&input1);
Georgios Pinitasa84faff2018-12-05 18:17:24 +0000467 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input1, 1, DataType::U8, DataType::QASYMM8, DataType::S16, DataType::F16, DataType::F32);
468 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input2, 1, DataType::U8, DataType::QASYMM8, DataType::S16, DataType::F16, DataType::F32);
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000469
470 const TensorShape out_shape = TensorShape::broadcast_shape(input1.tensor_shape(), input2.tensor_shape());
471
472 ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible");
Georgios Pinitas5a594532018-12-03 14:30:05 +0000473 ARM_COMPUTE_RETURN_ERROR_ON_MSG((input1.tensor_shape().x() != input2.tensor_shape().x()) && ((input1.data_type() != input2.data_type()) || (input1.data_type() != output.data_type())
474 || (input2.data_type() != output.data_type())),
475 "Broadcasting across width is supported on configurations where all tensors have the same data type");
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000476
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000477 // Validate in case of configured output
478 if(output.total_size() > 0)
479 {
480 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100481 !(input1.data_type() == DataType::U8 && input2.data_type() == DataType::U8 && output.data_type() == DataType::U8)
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000482 && !(input1.data_type() == DataType::U8 && input2.data_type() == DataType::U8 && output.data_type() == DataType::S16)
483 && !(input1.data_type() == DataType::U8 && input2.data_type() == DataType::S16 && output.data_type() == DataType::S16)
484 && !(input1.data_type() == DataType::S16 && input2.data_type() == DataType::U8 && output.data_type() == DataType::S16)
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000485 && !(input1.data_type() == DataType::S16 && input2.data_type() == DataType::S16 && output.data_type() == DataType::S16)
486 && !(input1.data_type() == DataType::F32 && input2.data_type() == DataType::F32 && output.data_type() == DataType::F32)
Georgios Pinitasa84faff2018-12-05 18:17:24 +0000487 && !(input1.data_type() == DataType::F16 && input2.data_type() == DataType::F16 && output.data_type() == DataType::F16)
488 && !(input1.data_type() == DataType::QASYMM8 && input2.data_type() == DataType::QASYMM8 && output.data_type() == DataType::QASYMM8),
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000489 "You called addition with the wrong image formats");
490
491 ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, output.tensor_shape(), 0),
492 "Wrong shape for output");
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000493 }
Ioan-Cristian Szabo397d58a2017-11-30 15:19:11 +0000494
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000495 return Status{};
Ioan-Cristian Szabo397d58a2017-11-30 15:19:11 +0000496}
497
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000498std::pair<Status, Window> validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output)
Ioan-Cristian Szabo397d58a2017-11-30 15:19:11 +0000499{
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000500 const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(input1, input2);
501 const TensorShape &out_shape = broadcast_pair.first;
502 const ValidRegion &valid_region = broadcast_pair.second;
Ioan-Cristian Szabo397d58a2017-11-30 15:19:11 +0000503
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000504 // Auto initialize output if not initialized
505 {
506 set_shape_if_empty(output, out_shape);
Ioan-Cristian Szabo397d58a2017-11-30 15:19:11 +0000507
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000508 if(input1.data_type() == DataType::S16 || input2.data_type() == DataType::S16)
509 {
510 set_format_if_unknown(output, Format::S16);
511 }
512 else if(input1.data_type() == DataType::F16 && input2.data_type() == DataType::F16)
513 {
514 set_format_if_unknown(output, Format::F16);
515 }
516 else if(input1.data_type() == DataType::F32 || input2.data_type() == DataType::F32)
517 {
518 set_format_if_unknown(output, Format::F32);
519 }
Georgios Pinitasa84faff2018-12-05 18:17:24 +0000520 else if(input1.data_type() == DataType::QASYMM8 || input2.data_type() == DataType::QASYMM8)
521 {
522 set_data_type_if_unknown(output, DataType::QASYMM8);
523 }
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000524 }
Ioan-Cristian Szabo397d58a2017-11-30 15:19:11 +0000525
Georgios Pinitas5a594532018-12-03 14:30:05 +0000526 Window win = calculate_max_window(valid_region, Steps());
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000527
Georgios Pinitas5a594532018-12-03 14:30:05 +0000528 // NEArithmeticAdditionKernel doesn't need padding so update_window_and_padding() can be skipped
529 Coordinates coord;
530 coord.set_num_dimensions(output.num_dimensions());
531 output.set_valid_region(valid_region);
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000532
Georgios Pinitas5a594532018-12-03 14:30:05 +0000533 return std::make_pair(Status{}, win);
534 ;
Ioan-Cristian Szabo397d58a2017-11-30 15:19:11 +0000535}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100536} // namespace
537
538NEArithmeticAdditionKernel::NEArithmeticAdditionKernel()
Georgios Pinitas5a594532018-12-03 14:30:05 +0000539 : _func(nullptr), _input1(nullptr), _input2(nullptr), _output(nullptr), _policy()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100540{
541}
542
543void NEArithmeticAdditionKernel::configure(const ITensor *input1, const ITensor *input2, ITensor *output, ConvertPolicy policy)
544{
545 ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output);
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000546 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*input1->info(), *input2->info(), *output->info(), policy));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100547
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000548 // Configure kernel window
549 auto win_config = validate_and_configure_window(*input1->info(), *input2->info(), *output->info());
550 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100551
552 static std::map<std::string, AddFunction *> map_function =
553 {
Georgios Pinitasa84faff2018-12-05 18:17:24 +0000554 { "add_wrap_QASYMM8_QASYMM8_QASYMM8", &add_QASYMM8_QASYMM8_QASYMM8 },
555 { "add_saturate_QASYMM8_QASYMM8_QASYMM8", &add_QASYMM8_QASYMM8_QASYMM8 },
Georgios Pinitas5a594532018-12-03 14:30:05 +0000556 { "add_wrap_U8_U8_U8", &add_same<uint8_t, false> },
557 { "add_saturate_U8_U8_U8", &add_same<uint8_t, true> },
558 { "add_wrap_S16_U8_S16", &add_S16_U8_S16 },
559 { "add_saturate_S16_U8_S16", &add_S16_U8_S16 },
560 { "add_wrap_U8_S16_S16", &add_U8_S16_S16 },
561 { "add_saturate_U8_S16_S16", &add_U8_S16_S16 },
562 { "add_wrap_U8_U8_S16", &add_U8_U8_S16 },
563 { "add_saturate_U8_U8_S16", &add_U8_U8_S16 },
564 { "add_wrap_S16_S16_S16", &add_same<int16_t, false> },
565 { "add_saturate_S16_S16_S16", &add_same<int16_t, true> },
566 { "add_wrap_F32_F32_F32", &add_same<float, false> },
567 { "add_saturate_F32_F32_F32", &add_same<float, false> },
568#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
569 { "add_wrap_F16_F16_F16", &add_same<float16_t, false> },
570 { "add_saturate_F16_F16_F16", &add_same<float16_t, false> },
571#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100572 };
573
574 _input1 = input1;
575 _input2 = input2;
576 _output = output;
Georgios Pinitas5a594532018-12-03 14:30:05 +0000577 _policy = policy;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100578
579 std::string function_to_call("add_");
580 function_to_call += policy == ConvertPolicy::WRAP ? "wrap_" : "saturate_";
581 function_to_call += string_from_data_type(input1->info()->data_type()) + "_";
582 function_to_call += string_from_data_type(input2->info()->data_type()) + "_";
583 function_to_call += string_from_data_type(output->info()->data_type());
584
585 auto it = map_function.find(function_to_call);
586
587 if(it != map_function.end())
588 {
589 _func = it->second;
590 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100591
Ioan-Cristian Szabo397d58a2017-11-30 15:19:11 +0000592 INEKernel::configure(win_config.second);
593}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100594
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000595Status NEArithmeticAdditionKernel::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, ConvertPolicy policy)
Ioan-Cristian Szabo397d58a2017-11-30 15:19:11 +0000596{
Georgios Pinitascbf39c62018-09-10 15:07:45 +0100597 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output);
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000598
599 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*input1, *input2, *output, policy));
600 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(*input1->clone(), *input2->clone(), *output->clone()).first);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100601
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000602 return Status{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100603}
604
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100605void NEArithmeticAdditionKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100606{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100607 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100608 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
609 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
610 ARM_COMPUTE_ERROR_ON(_func == nullptr);
611
Georgios Pinitas5a594532018-12-03 14:30:05 +0000612 (*_func)(_input1, _input2, _output, _policy, window);
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000613}