blob: d1660fe19eface174cdcdd11b2aada5855cc3f0a [file] [log] [blame]
Georgios Pinitasbdcdc392021-04-22 16:42:03 +01001/*
2 * Copyright (c) 2021 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/Helpers.h"
25#include "arm_compute/core/ITensor.h"
26#include "arm_compute/core/Types.h"
27#include "arm_compute/core/utils/misc/Traits.h"
28#include "src/core/NEON/wrapper/intrinsics/intrinsics.h"
29
30#include "src/core/NEON/SVEMath.h"
31#include "src/core/cpu/kernels/add/sve/impl.h"
32#include <arm_sve.h>
33
34namespace arm_compute
35{
36namespace cpu
37{
38template <typename ScalarType>
39void add_same_sve(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window)
40{
41 const auto all_true_pg = wrapper::svptrue<ScalarType>();
42 const auto window_start_x = static_cast<int>(window.x().start());
43 const auto window_end_x = static_cast<int>(window.x().end());
44 const bool is_broadcast_across_x = src0->info()->tensor_shape().x() != src1->info()->tensor_shape().x();
45 const bool is_sat = (policy == ConvertPolicy::SATURATE);
46
47 // Clear X Dimension on execution window as we handle manually
48 Window win = window;
49 win.set(Window::DimX, Window::Dimension(0, 1, 1));
50
51 // Create input windows
52 Window input1_win = window.broadcast_if_dimension_le_one(src0->info()->tensor_shape());
53 Window input2_win = window.broadcast_if_dimension_le_one(src1->info()->tensor_shape());
54
55 Iterator input1(src0, window.broadcast_if_dimension_le_one(src0->info()->tensor_shape()));
56 Iterator input2(src1, window.broadcast_if_dimension_le_one(src1->info()->tensor_shape()));
57 Iterator output(dst, window);
58
59 if(is_broadcast_across_x)
60 {
61 const bool is_broadcast_input_2 = input2_win.x().step() == 0;
62 Window broadcast_win = is_broadcast_input_2 ? input2_win : input1_win;
63 Window non_broadcast_win = !is_broadcast_input_2 ? input2_win : input1_win;
64 const ITensor *broadcast_tensor = is_broadcast_input_2 ? src1 : src0;
65 const ITensor *non_broadcast_tensor = !is_broadcast_input_2 ? src1 : src0;
66
67 // Clear X Dimension on execution window as we handle manually
68 non_broadcast_win.set(Window::DimX, Window::Dimension(0, 1, 1));
69
70 Iterator broadcast_input(broadcast_tensor, broadcast_win);
71 Iterator non_broadcast_input(non_broadcast_tensor, non_broadcast_win);
72 Iterator output(dst, win);
73
74 execute_window_loop(win, [&](const Coordinates &)
75 {
76 const auto non_broadcast_input_ptr = reinterpret_cast<const ScalarType *>(non_broadcast_input.ptr());
77 const auto output_ptr = reinterpret_cast<ScalarType *>(output.ptr());
78
79 const ScalarType broadcast_value = *reinterpret_cast<const ScalarType *>(broadcast_input.ptr());
80 const auto broadcast_value_vec = wrapper::svdup_n(broadcast_value);
81
82 int x = window_start_x;
83 svbool_t pg = wrapper::svwhilelt<ScalarType>(x, window_end_x);
84 do
85 {
86 const auto non_broadcast_v = svld1(pg, non_broadcast_input_ptr + x);
87 auto res = is_sat ? wrapper::svqadd(broadcast_value_vec, non_broadcast_v) : svadd_z(pg, broadcast_value_vec, non_broadcast_v);
88 svst1(pg, output_ptr + x, res);
89
90 x += wrapper::svcnt<ScalarType>();
91 pg = wrapper::svwhilelt<ScalarType>(x, window_end_x);
92 }
93 while(svptest_any(all_true_pg, pg));
94 },
95 broadcast_input, non_broadcast_input, output);
96 }
97 else
98 {
99 // Clear X Dimension on execution window as we handle manually
100 input1_win.set(Window::DimX, Window::Dimension(0, 1, 1));
101 input2_win.set(Window::DimX, Window::Dimension(0, 1, 1));
102
103 Iterator input1(src0, input1_win);
104 Iterator input2(src1, input2_win);
105 Iterator output(dst, win);
106
107 execute_window_loop(win, [&](const Coordinates &)
108 {
109 const auto input1_ptr = reinterpret_cast<const ScalarType *>(input1.ptr());
110 const auto input2_ptr = reinterpret_cast<const ScalarType *>(input2.ptr());
111 const auto output_ptr = reinterpret_cast<ScalarType *>(output.ptr());
112
113 int x = window_start_x;
114 svbool_t pg = wrapper::svwhilelt<ScalarType>(x, window_end_x);
115 do
116 {
117 const auto val1 = svld1(pg, input1_ptr + x);
118 const auto val2 = svld1(pg, input2_ptr + x);
119 const auto res = is_sat ? wrapper::svqadd(val1, val2) : svadd_z(pg, val1, val2);
120 svst1(pg, output_ptr + x, res);
121
122 x += wrapper::svcnt<ScalarType>();
123 pg = wrapper::svwhilelt<ScalarType>(x, window_end_x);
124 }
125 while(svptest_any(all_true_pg, pg));
126 },
127 input1, input2, output);
128 }
129}
130
131template void add_same_sve<float>(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window);
132template void add_same_sve<float16_t>(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window);
133template void add_same_sve<uint8_t>(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window);
134template void add_same_sve<int16_t>(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window);
135template void add_same_sve<int32_t>(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window);
136} // namespace cpu
137} // namespace arm_compute